private async Task <byte[]> ReadResponseAsync(byte command) { //Log.Debug("ReadResponse"); //var reportSize = hidDevice.Capabilities.OutputReportByteLength; var reportSize = HID_RPT_SIZE; var byteArrayBuilder = new ByteArrayBuilder(); byteArrayBuilder.Append(cid); byteArrayBuilder.Append((byte)(TYPE_INIT | command)); var resp = Encoding.ASCII.GetBytes("."); var header = byteArrayBuilder.GetBytes(); HidReport report = null; while (!resp.Take(header.Length).SequenceEqual(header)) { report = await hidDevice.ReadReportAsync(HidTimeoutMs); if (report.ReadStatus != HidDeviceData.ReadStatus.Success) { throw new Exception("Error reading from device"); } resp = report.Data; byteArrayBuilder.Clear(); byteArrayBuilder.Append(cid); byteArrayBuilder.Append(STAT_ERR); if (resp.Take(header.Length).SequenceEqual(byteArrayBuilder.GetBytes())) { throw new Exception("Error in response header"); } } var dataLength = (report.Data[5] << 8) + report.Data[6]; var payloadData = report.Data.Skip(7).Take(Math.Min(dataLength, reportSize)).ToArray(); //Log.Debug($"Payload data: {BitConverter.ToString(payloadData)}"); byteArrayBuilder.Clear(); byteArrayBuilder.Append(payloadData); dataLength -= (int)byteArrayBuilder.Length; var seq = 0; while (dataLength > 0) { report = await hidDevice.ReadReportAsync(HidTimeoutMs); if (report.ReadStatus != HidDeviceData.ReadStatus.Success) { throw new Exception("Error reading from device"); } if (!report.Data.Take(4).SequenceEqual(cid)) { throw new Exception("Wrong CID from device"); } if (report.Data[4] != (byte)(seq & 0x7f)) { throw new Exception("Wrong SEQ from device"); } seq++; payloadData = report.Data.Skip(5).Take(Math.Min(dataLength, reportSize)).ToArray(); //Log.Debug($"Payload data: {BitConverter.ToString(payloadData)}"); dataLength -= payloadData.Length; byteArrayBuilder.Append(payloadData); } var result = byteArrayBuilder.GetBytes(); return(result); }
private async Task SendRequestAsync(byte command, byte[] data = null) { //Log.Debug($"SendRequest: {command:X2}"); if (data == null) { data = new byte[0]; } //Log.Debug($"Data: {BitConverter.ToString(data)}"); //var reportSize = hidDevice.Capabilities.InputReportByteLength; var reportSize = HID_RPT_SIZE; var size = data.Length; var bc_l = (byte)(size & 0xff); var bc_h = (byte)(size >> 8 & 0xff); var payloadData = data.Take(reportSize - 7).ToArray(); //Log.Debug($"Payload data: {BitConverter.ToString(payloadData)}"); var payloadBuilder = new ByteArrayBuilder(); payloadBuilder.Append(cid); payloadBuilder.Append((byte)(TYPE_INIT | command)); payloadBuilder.Append(bc_h); payloadBuilder.Append(bc_l); payloadBuilder.Append(payloadData); while (payloadBuilder.Length < reportSize) { payloadBuilder.Append(0x00); } var payload = payloadBuilder.GetBytes(); var report = hidDevice.CreateReport(); report.Data = payload; await hidDevice.WriteReportAsync(report, HidTimeoutMs); var remainingData = data.Skip(reportSize - 7).ToArray(); var seq = 0; while (remainingData.Length > 0) { payloadData = remainingData.Take(reportSize - 5).ToArray(); //Log.Debug($"Payload data: {BitConverter.ToString(payloadData)}"); payloadBuilder.Clear(); payloadBuilder.Append(cid); payloadBuilder.Append((byte)(0x7f & seq)); payloadBuilder.Append(payloadData); while (payloadBuilder.Length < reportSize) { payloadBuilder.Append(0x00); } payload = payloadBuilder.GetBytes(); report = hidDevice.CreateReport(); report.Data = payload; if (!await hidDevice.WriteReportAsync(report, HidTimeoutMs)) { throw new Exception("Error writing to device"); } remainingData = remainingData.Skip(reportSize - 5).ToArray(); seq++; } }