예제 #1
0
        private static IHidDeviceInformation InfoFromData(SafeDeviceInfoSetHandle infoList, SP_DEVICE_INTERFACE_DATA interfaceData)
        {
            var path = SetupDiGetDeviceInterfaceDetail(infoList, interfaceData, IntPtr.Zero);

            using (var device = Win32HidDevice.TryFromPath(path, 0))
            {
                if (device == null)
                {
                    if (log.IsDebugEnabled)
                    {
                        log.Debug($"Unable to open device {path}");
                    }
                    return(null);
                }

                var information = new Win32HidDeviceInformationStored(device.Information);
                if (log.IsDebugEnabled)
                {
                    log.Debug(
                        $"Found device '{information.Product}' (PID=0x{information.ProductId:X2}) "
                        + $"by '{information.Manufacturer}' (VID=0x{information.VendorId:X2})");
                }
                return(information);
            }
        }
예제 #2
0
        private static Win32HidDevice FromId([NotNull] string deviceId, HidDeviceAccessMode accessMode,
                                             [CanBeNull] Win32HidDeviceInformation knownInformation)
        {
            if (deviceId == null)
            {
                throw new ArgumentNullException(nameof(deviceId));
            }
            switch (accessMode)
            {
            case HidDeviceAccessMode.Read:
                return(Win32HidDevice.FromPath(deviceId, ACCESS_MASK.GenericRight.GENERIC_READ, knownInformation));

            case HidDeviceAccessMode.Write:
                return(Win32HidDevice.FromPath(deviceId, ACCESS_MASK.GenericRight.GENERIC_WRITE, knownInformation));

            case HidDeviceAccessMode.ReadWrite:
                return(Win32HidDevice.FromPath(deviceId,
                                               ACCESS_MASK.GenericRight.GENERIC_READ | ACCESS_MASK.GenericRight.GENERIC_WRITE, knownInformation));

            default:
                throw new ArgumentException("Access mode not supported: " + accessMode, nameof(accessMode));
            }
        }
예제 #3
0
        static unsafe void Wink(Win32HidDevice device, byte b1, byte b2, byte b3, byte b4)
        {
            var msg = new FidoU2FHidMessage(
                (uint)(unchecked (b1 << 24 | b2 << 16 | b3 << 8 | b4)),
                U2FHidCommand.Wink);
            device.WriteFidoU2FHidMessageAsync(msg, CancellationToken.None).Wait();

            var caps = device.Information.Capabilities;

            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);
        }
예제 #4
0
        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]);
        }