コード例 #1
0
        FTDIBoxRequest(FTDIBoxRequestAddress address, IEnumerable <byte> data, IRequest deviceRequest, DeviceOperationScope scope)
        {
            _deviceRequest = deviceRequest;
            Address        = address;
            _scope         = scope;

            switch (Address)
            {
            case FTDIBoxRequestAddress.HS_SET_LOW_SPEED_MODE:
                Serialized    = new byte[] { 0, 16 };
                _responseInfo = new ResponseInfo(ResponseReadMode.CONSTRAINED_BY_ANSWER, 2, new byte[] { 0, 16 });
                break;

            case FTDIBoxRequestAddress.LS_SET_HIGH_SPEED_MODE:
                Serialized    = new byte[] { 92, 12 };
                _responseInfo = new ResponseInfo(ResponseReadMode.CONSTRAINED_BY_ANSWER, 2, new byte[] { 92, 12 });
                break;

            case FTDIBoxRequestAddress.HS_ACTIVATE_CHANNEL4:
                Serialized    = new byte[] { 0, 20 };
                _responseInfo = new ResponseInfo(ResponseReadMode.CONSTRAINED_BY_ANSWER, 2, new byte[] { 0, 20 });
                break;

            case FTDIBoxRequestAddress.LS_ACTIVATE_CHANNEL4:
                Serialized    = new byte[] { 92, 10 };
                _responseInfo = new ResponseInfo(ResponseReadMode.CONSTRAINED_BY_ANSWER, 2, new byte[] { 92, 10 });
                break;

            case FTDIBoxRequestAddress.HS_GET_FLASH_LENGTH:
                Serialized    = new byte[] { 9, 128, 170, 206, 0, 0, 0, 0, 0, 2 };
                _responseInfo = new ResponseInfo(ResponseReadMode.CONSTRAINED_BY_ANSWER, 6, null);
                break;

            case FTDIBoxRequestAddress.HS_READ_ALL_FLASH:
                Timeout       = RequestTimeout.INFINITY;
                Serialized    = new byte[] { 9, 128, 170, 206, 0, 0, 0, 0, 0, 3 };
                _responseInfo = new ResponseInfo(ResponseReadMode.TILL_TIMEOUT, ResponseLength.UNKNOWN, null);
                break;

            case FTDIBoxRequestAddress.LS_DEVICE_REQUEST:
                Serialized = new Enumerable <byte>()
                {
                    0xFF, 0x00,
                    data.Count().ToUInt16().SerializeAsNumber(false),
                    data
                };
                _responseInfo = new ResponseInfo(ResponseReadMode.READ_BY_TARGET, ResponseLength.UNKNOWN, null);
                break;

            default:
                throw new NotSupportedException();
            }
        }
コード例 #2
0
        public async override Task <IResponse> RequestAsync(IRequest request, DeviceOperationScope scope, AsyncOperationInfo operationInfo)
        {
#warning add more smart paths
            foreach (var middleware in _conveyor)
            {
                request = await middleware.HandleOrMutateAsync(request, scope, operationInfo);
            }

            var response = await base.RequestAsync(request, scope, operationInfo);

            foreach (var middleware in _conveyor.Reverse())
            {
                response = await middleware.HandleOrMutateAsync(response, scope, operationInfo);
            }

            return(response);
        }
コード例 #3
0
        async Task <bool> trySetModeAsync(Mode mode, DeviceOperationScope scope, CancellationToken cancellation)
        {
            Logger.LogInfo(null, $"Установка режима: {mode}, текуший режим: {_currentMode}");

            if (_currentMode != mode)
            {
                var ok = true;
                switch (mode)
                {
                case Mode.UNKNOWN:
                    throw new ArgumentOutOfRangeException();

                case Mode.COM_PORT:
                    ok &= await _pipe.RequestAsync(instantiate_HS_SET_LOW_SPEED_MODE(scope), scope, cancellation).ThenDo(r => r.Status == RequestStatus.OK);

                    ok &= await _pipe.RequestAsync(instantiate_LS_SELECT_CHANNEL4(scope), scope, cancellation).ThenDo(r => r.Status == RequestStatus.OK);

                    break;

                case Mode.HIGH_SPEED:
                    ok &= await _pipe.RequestAsync(instantiate_LS_SET_HIGH_SPEED_MODE(scope), scope, cancellation).ThenDo(r => r.Status == RequestStatus.OK);

                    ok &= await _pipe.RequestAsync(instantiate_HS_SELECT_CHANNEL4(scope), scope, cancellation).ThenDo(r => r.Status == RequestStatus.OK);

                    break;

                default:
                    throw new NotSupportedException();
                }
                await Task.Delay(CHANNEL_ACTIVATION_DELAY);

                if (ok)
                {
                    _currentMode = mode;
                }
                else
                {
                    _currentMode = Mode.UNKNOWN;
                }
            }

            return(_currentMode == mode);
        }
コード例 #4
0
 public virtual Task <IResponse> RequestAsync(IRequest request, DeviceOperationScope scope, AsyncOperationInfo cancellation)
 {
     return(_base.RequestAsync(request, scope, cancellation));
 }
コード例 #5
0
 public async Task <bool> TrySwitchToRegularModeAsync(DeviceOperationScope scope, CancellationToken cancellation)
 {
     return(await trySetModeAsync(Mode.COM_PORT, scope, cancellation));
 }
コード例 #6
0
        /// <summary>
        /// Very long operation, returns huge file
        /// </summary>
        /// <param name="asyncOperation"></param>
        /// <returns></returns>
        public async Task <IResponseData> ReadFileAsync(DeviceOperationScope scope, AsyncOperationInfo asyncOperation)
        {
            using (Logger.Indent)
            {
                Logger.LogInfo(null, $"Чтение дампа флеш...");

                IResponseData result = null;
                var           ok     = await trySetModeAsync(Mode.HIGH_SPEED, scope, asyncOperation.CancellationToken);

                if (ok)
                {
                    var estimatedFlashLength = (await tryGetFlashLengthAsync()) ?? MAX_FLASH_SIZE; // Estimated (not exact) length of the file to be transfered
                    asyncOperation.Progress.MaxProgress = estimatedFlashLength;
                    var response = await _pipe.RequestAsync(instantiate_HS_READ_ALL_FLASH(scope), scope, asyncOperation).ThenDo(r => r.To <FTDIBoxResponse>());

                    if (response.Status == RequestStatus.OK)
                    {
                        var serviceData = await response.ServiceData.GetAllAsync(asyncOperation);

                        var status = EnumUtils.TryCastOrNull <FlashReadStatus>(serviceData[5]);
                        if (status == FlashReadStatus.OK)
                        {
                            Logger.LogOKEverywhere("Файл успешно прочитан");
                        }
                        else
                        {
                            Logger.LogError("Ошибка чтения файла", $"-MSG. Сервис данные: {serviceData.ToHex()}");
                        }

                        result = response.Body;
                        asyncOperation.Progress.Report(asyncOperation.Progress.MaxProgress);
                    }
                }

                return(result);
            }

            async Task <long?> tryGetFlashLengthAsync()
            {
                using (Logger.Indent)
                {
                    Logger.LogInfo(null, $"Чтение длины флеш");

                    var result = await _pipe.RequestAsync(instantiate_HS_GET_FIRST_AVAILABLE_FLASH_BLOCK(scope), scope, asyncOperation.CancellationToken);

                    if (result.Status == RequestStatus.OK)
                    {
                        var iterator = result.Data.StartEnumeration();
                        iterator.Pull(2);
                        var lengthInBytes = iterator.Pull(3).Concat(0.ToByte()).DeserializeInt32(false) * (long)FLASH_BLOCK_SIZE;
                        var status        = EnumUtils.TryCastOrNull <GetFirstAvailableBlockStatus>(iterator.AdvanceOrThrow());
                        switch (status)
                        {
                        case null:
                        case GetFirstAvailableBlockStatus.ERROR:
                            Logger.LogErrorEverywhere("Ошибка получения длины флеш");
                            return(null);

                        case GetFirstAvailableBlockStatus.IS_FULL:
                        case GetFirstAvailableBlockStatus.IS_EMPTY:
                        case GetFirstAvailableBlockStatus.IS_PARTIALLY_FILLED:
                            Logger.LogOK(null, $"Длина флеш: {lengthInBytes}");
                            return(lengthInBytes);

                        default:
                            throw new NotSupportedException();
                        }
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
        }
コード例 #7
0
 static FTDIBoxRequest instantiate_HS_READ_ALL_FLASH(DeviceOperationScope scope)
 => FTDIBoxRequest.CreateCommandRequest(FTDIBoxRequestAddress.HS_READ_ALL_FLASH, scope);
コード例 #8
0
        public static async Task <FTDIBoxFeatures> CreateAsync(IRUSConnectionInterface pipe, DeviceOperationScope scope, CancellationToken cancellation)
        {
            var device = new FTDIBoxFeatures(pipe);
            var ok     = await device.trySetModeAsync(Mode.HIGH_SPEED, scope, cancellation);

            ok |= await device.trySetModeAsync(Mode.COM_PORT, scope, cancellation);

            if (!ok)
            {
                Logger.LogErrorEverywhere("Ошибка инициализации устройства");

                device = null;
            }

            return(device);
        }
コード例 #9
0
 internal static FTDIBoxRequest CreateCommandRequest(FTDIBoxRequestAddress address, DeviceOperationScope scope)
 {
     return(new FTDIBoxRequest(address, null, null, scope));
 }
コード例 #10
0
 static FTDIBoxRequest instantiate_HS_GET_FIRST_AVAILABLE_FLASH_BLOCK(DeviceOperationScope scope)
 => FTDIBoxRequest.CreateCommandRequest(FTDIBoxRequestAddress.HS_GET_FLASH_LENGTH, scope);
コード例 #11
0
ファイル: SalachovRequest.cs プロジェクト: GataullinRR/EX1
 SalachovRequest(RUSDeviceId deviceId, Command address, bool isWriteRequest, ResponseLength responseLength, DeviceOperationScope scope)
     : this(deviceId, address, isWriteRequest, responseLength, new IDataEntity[0], scope)
 {
 }
コード例 #12
0
ファイル: SalachovRequest.cs プロジェクト: GataullinRR/EX1
 internal static SalachovRequest CreateReadRequest(RUSDeviceId deviceId, Command address, DeviceOperationScope scope)
 {
     return(new SalachovRequest(deviceId, address, false,
                                address.GetInfo().ReadResponse.GetInfo().FullResponseLength, new DataEntity[0], scope));
 }
コード例 #13
0
 public virtual async Task <IResponse> handleOrMutateAsync(IResponse response, DeviceOperationScope scope, AsyncOperationInfo operationInfo)
 {
     return(response);
 }
コード例 #14
0
 public Task <IResponse> HandleOrMutateAsync(IResponse response, DeviceOperationScope scope, AsyncOperationInfo operationInfo)
 {
     return(handleOrMutateAsync(response, scope, operationInfo));
 }
コード例 #15
0
 public virtual async Task <IRequest> handleOrMutateAsync(IRequest request, DeviceOperationScope scope, AsyncOperationInfo operationInfo)
 {
     return(request);
 }
コード例 #16
0
        public Task <IRequest> HandleOrMutateAsync(IRequest request, DeviceOperationScope scope, AsyncOperationInfo operationInfo)
        {
            InitialRequest = request;

            return(handleOrMutateAsync(request, scope, operationInfo));
        }
コード例 #17
0
 internal static FTDIBoxRequest CreateDeviceRequest(IRequest request, DeviceOperationScope scope)
 {
     return(new FTDIBoxRequest(FTDIBoxRequestAddress.LS_DEVICE_REQUEST, request.Serialized, request, scope));
 }
コード例 #18
0
ファイル: SalachovRequest.cs プロジェクト: GataullinRR/EX1
        SalachovRequest(RUSDeviceId deviceId, Command address, bool isWriteRequest, ResponseLength responseLength, IEnumerable <IDataEntity> dataEntities, DeviceOperationScope scope)
        {
            DeviceId       = deviceId;
            Address        = address;
            IsWriteRequest = isWriteRequest;
            var addressInfo = Address.GetInfo();

            _responseFullLength = responseLength;
            _hasLengthField     = _responseFullLength.IsUnknown;

            _commandWords = new ushort[]
            {
                (ushort)(((byte)DeviceId << 9) | ((~0x80) & Address.GetInfo().Address) | (IsWriteRequest ? 1 << 7 : 0)),
                0x0000
            };
            _header = wordsToBytes(_commandWords).ToArray();

            Serialized = serialize(dataEntities);

            IEnumerable <byte> serialize(IEnumerable <IDataEntity> entities)
            {
                var dataBytes = entities
                                .Select(e => e.RawValue)
                                .Flatten()
                                .ToArray();
                var zeroTrailShouldBeAdded = dataBytes.Length % 2 == 1;
                var actualLength           = zeroTrailShouldBeAdded
                    ? dataBytes.Length + 1
                    : dataBytes.Length;
                var requestBytes = new Enumerable <byte>()
                {
                    _header,
                    IsWriteRequest     // If there is data - we need to add length of the data
                            ? wordsToBytes(((ushort)actualLength).ToSequence())
                            : new byte[0],
                    dataBytes,
                    zeroTrailShouldBeAdded
                            ? new byte[1]
                            : new byte[0]
                };

                requestBytes.Add(wordsToBytes(_checksum.ComputeChecksum(requestBytes).ToSequence()));

                return(requestBytes);
            }
        }
コード例 #19
0
 static FTDIBoxRequest instantiate_HS_SELECT_CHANNEL4(DeviceOperationScope scope)
 => FTDIBoxRequest.CreateCommandRequest(FTDIBoxRequestAddress.HS_ACTIVATE_CHANNEL4, scope);
コード例 #20
0
ファイル: SalachovRequest.cs プロジェクト: GataullinRR/EX1
 internal static SalachovRequest CreateWriteRequest(RUSDeviceId deviceId, Command address, IEnumerable <IDataEntity> data, DeviceOperationScope scope)
 {
     return(new SalachovRequest(deviceId, address, true,
                                address.GetInfo().WriteResponse.GetInfo().FullResponseLength, data, scope));
 }
コード例 #21
0
 static FTDIBoxRequest instantiate_LS_SET_HIGH_SPEED_MODE(DeviceOperationScope scope)
 => FTDIBoxRequest.CreateCommandRequest(FTDIBoxRequestAddress.LS_SET_HIGH_SPEED_MODE, scope);