Exemplo n.º 1
0
        private static HidReport WriteAndReadReport(IHidDevice device, byte reportId, byte[] data)
        {
            var outData = device.CreateReport();

            outData.ReportId = reportId;
            outData.Data     = data;
            device.WriteReport(outData);
            while (outData.ReadStatus != HidDeviceData.ReadStatus.Success)
            {
                Thread.Sleep(1);
            }
            var report = device.ReadReport();

            return(report);
        }
Exemplo n.º 2
0
        private async Task SendCommandAsync(byte command, byte[] data = null)
        {
            if (data == null)
            {
                data = new byte[0];
            }

            var reportSize = CTAP_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();

            System.Diagnostics.Debug.WriteLine($"Payload data: {BitConverter.ToString(payloadData)}");

            {
                var packet = new List <byte>();
                packet.AddRange(cid);
                packet.Add((byte)(CTAP_FRAME_INIT | command));
                packet.Add(bc_h);
                packet.Add(bc_l);
                packet.AddRange(payloadData);
                while (packet.Count < reportSize)
                {
                    packet.Add(0x00);
                }
                var report = hidDevice.CreateReport();
                report.Data = packet.ToArray();
                var sendst = await hidDevice.WriteReportAsync(report, CallTimeoutMs);

                System.Diagnostics.Debug.WriteLine($"send Packet({sendst}): ({report.Data.Length}):{BitConverter.ToString(report.Data)}");
            }


            var remainingData = data.Skip(reportSize - 7).ToArray();
            var seq           = 0;

            while (remainingData.Length > 0)
            {
                payloadData = remainingData.Take(reportSize - 5).ToArray();

                var packet = new List <byte>();
                packet.AddRange(cid);
                packet.Add((byte)(0x7f & seq));
                packet.AddRange(payloadData);
                while (packet.Count < reportSize)
                {
                    packet.Add(0x00);
                }
                var report = hidDevice.CreateReport();
                report.Data = packet.ToArray();

                if (!await hidDevice.WriteReportAsync(report, CallTimeoutMs))
                {
                    throw new Exception("Error writing to device");
                }
                System.Diagnostics.Debug.WriteLine($"send Packet: ({report.Data.Length}):{BitConverter.ToString(report.Data)}");

                remainingData = remainingData.Skip(reportSize - 5).ToArray();
                seq++;
            }
        }
Exemplo n.º 3
0
        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++;
            }
        }