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(); } }
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); }
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); }
public virtual Task <IResponse> RequestAsync(IRequest request, DeviceOperationScope scope, AsyncOperationInfo cancellation) { return(_base.RequestAsync(request, scope, cancellation)); }
public async Task <bool> TrySwitchToRegularModeAsync(DeviceOperationScope scope, CancellationToken cancellation) { return(await trySetModeAsync(Mode.COM_PORT, scope, cancellation)); }
/// <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); } } } }
static FTDIBoxRequest instantiate_HS_READ_ALL_FLASH(DeviceOperationScope scope) => FTDIBoxRequest.CreateCommandRequest(FTDIBoxRequestAddress.HS_READ_ALL_FLASH, scope);
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); }
internal static FTDIBoxRequest CreateCommandRequest(FTDIBoxRequestAddress address, DeviceOperationScope scope) { return(new FTDIBoxRequest(address, null, null, scope)); }
static FTDIBoxRequest instantiate_HS_GET_FIRST_AVAILABLE_FLASH_BLOCK(DeviceOperationScope scope) => FTDIBoxRequest.CreateCommandRequest(FTDIBoxRequestAddress.HS_GET_FLASH_LENGTH, scope);
SalachovRequest(RUSDeviceId deviceId, Command address, bool isWriteRequest, ResponseLength responseLength, DeviceOperationScope scope) : this(deviceId, address, isWriteRequest, responseLength, new IDataEntity[0], scope) { }
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)); }
public virtual async Task <IResponse> handleOrMutateAsync(IResponse response, DeviceOperationScope scope, AsyncOperationInfo operationInfo) { return(response); }
public Task <IResponse> HandleOrMutateAsync(IResponse response, DeviceOperationScope scope, AsyncOperationInfo operationInfo) { return(handleOrMutateAsync(response, scope, operationInfo)); }
public virtual async Task <IRequest> handleOrMutateAsync(IRequest request, DeviceOperationScope scope, AsyncOperationInfo operationInfo) { return(request); }
public Task <IRequest> HandleOrMutateAsync(IRequest request, DeviceOperationScope scope, AsyncOperationInfo operationInfo) { InitialRequest = request; return(handleOrMutateAsync(request, scope, operationInfo)); }
internal static FTDIBoxRequest CreateDeviceRequest(IRequest request, DeviceOperationScope scope) { return(new FTDIBoxRequest(FTDIBoxRequestAddress.LS_DEVICE_REQUEST, request.Serialized, request, scope)); }
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); } }
static FTDIBoxRequest instantiate_HS_SELECT_CHANNEL4(DeviceOperationScope scope) => FTDIBoxRequest.CreateCommandRequest(FTDIBoxRequestAddress.HS_ACTIVATE_CHANNEL4, scope);
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)); }
static FTDIBoxRequest instantiate_LS_SET_HIGH_SPEED_MODE(DeviceOperationScope scope) => FTDIBoxRequest.CreateCommandRequest(FTDIBoxRequestAddress.LS_SET_HIGH_SPEED_MODE, scope);