public async Task <int> SendOutputReportAsync(Win32HidOutputReport report, CancellationToken cancellationToken = default(CancellationToken)) { if (report == null) { throw new ArgumentNullException(nameof(report)); } var outputBuffer = report.GetOutputBuffer(); log.Trace("Sending output report:\r\n\r\n " + outputBuffer.ToHexString()); log.Trace(outputBuffer.ToLoggableAsHex("Sending output report:")); try { return(await Kernel32Dll.WriteFileAsync(Handle, outputBuffer, cancellationToken)); } catch (Win32Exception exception) { log.Error("Sending output report failed", exception); throw ExceptionConversion.ConvertException(exception); } catch (Exception exception) { log.Error("Sending output report failed", exception); throw; } }
static unsafe void Test(Win32HidDevice device) { var init = new InitializationPacket(); init.CommandIdentifier = (byte)U2FHidCommand.Init; init.ChannelIdentifier = U2FHID_BROADCAST_CID; init.PayloadLength = 8; var caps = device.Information.Capabilities; var buffer = new byte[caps.InputReportByteLength]; fixed(byte *pBuffer = buffer) { Marshal.StructureToPtr(init, new IntPtr(pBuffer + 1), false); buffer[0] = 0x00; buffer[8] = 0xCA; buffer[9] = 0xFE; buffer[10] = 0xBA; buffer[11] = 0xBE; buffer[12] = 0xDE; buffer[13] = 0xAD; buffer[14] = 0xBA; buffer[15] = 0xBE; WriteBuffer(buffer); var task = Kernel32Dll.WriteFileAsync(device.Handle, new IntPtr(pBuffer), buffer.Length); var writen = task.Result; Console.WriteLine("Writen {0} bytes", writen); } var bufferOut = new byte[caps.OutputReportByteLength]; fixed(byte *pBuffer = bufferOut) { var intPtr = new IntPtr(pBuffer); var task = Kernel32Dll.ReadFileAsync(device.Handle, intPtr, bufferOut.Length); var read = task.Result; Console.WriteLine("Read {0} bytes", read); } WriteBuffer(bufferOut); Wink(device, bufferOut[16], bufferOut[17], bufferOut[18], bufferOut[19]); }