Exemplo n.º 1
0
 internal static extern Boolean DeviceIoControl(IntPtr hDevice,
                                                Int32 dwIoControlCode,
                                                ref UsbDescriptorRequest lpInBuffer,
                                                Int32 nInBufferSize,
                                                ref UsbDescriptorRequest lpOutBuffer,
                                                Int32 nOutBufferSize,
                                                out Int32 lpBytesReturned,
                                                IntPtr lpOverlapped);
Exemplo n.º 2
0
        //public static IEnumerable<Datas.UsbNodeConnectionInformation> GetUsbNodeConnectionInformation(String devicePath,Int32 numberOfPorts)
        //{
        //    if (String.IsNullOrWhiteSpace(devicePath)) return null;
        //    IntPtr hcdPtr = Win32Api.CreateFile($@"\\.\{devicePath}",
        //        NativeFileAccess.GENERIC_WRITE,
        //        NativeFileShare.FILE_SHARE_WRITE,
        //        IntPtr.Zero,
        //        NativeFileMode.OPEN_EXISTING,
        //        IntPtr.Zero,
        //        IntPtr.Zero);
        //    if (hcdPtr == Win32Api.INVALID_HANDLE_VALUE) return null;
        //    Datas.UsbNodeConnectionInformation[] connections = new Datas.UsbNodeConnectionInformation[numberOfPorts];
        //    Boolean status;
        //    UsbNodeConnectionInformationEx buffer = new UsbNodeConnectionInformationEx();
        //    for (Int32 i = 1; i <= numberOfPorts; i++)
        //    {
        //        status = Win32Api.DeviceIoControl(hcdPtr,
        //            Win32Api.IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX,
        //            ref buffer,
        //            Marshal.SizeOf(buffer),
        //            ref buffer,
        //            Marshal.SizeOf(buffer),
        //            out int bytesReturned,
        //            IntPtr.Zero);
        //        if (status)
        //        {
        //            Datas.UsbNodeConnectionInformation connection = new Datas.UsbNodeConnectionInformation
        //            {
        //                DevicePath = devicePath,
        //                ConnectionIndex = buffer.ConnectionIndex,
        //                ConnectionStatus = bucffer.ConnectionStatus,
        //            };
        //        }
        //    }
        //}

        #region Descriptors

        /// <summary>
        /// 获取字符串描述符。
        /// </summary>
        /// <param name="hubDevice"></param>
        /// <param name="connectionIndex"></param>
        /// <param name="descriptorIndex"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public static String GetStringDescriptor(IntPtr hubDevice, Int32 connectionIndex, Byte descriptorIndex, UInt16 languageId)
        {
            UsbDescriptorRequest buffer = new UsbDescriptorRequest();

            buffer.ConnectionIndex    = connectionIndex;
            buffer.SetupPacket.Value  = (UInt16)((Globals.USB_STRING_DESCRIPTOR_TYPE << 8) | descriptorIndex);
            buffer.SetupPacket.Length = Globals.MAXIMUM_USB_STRING_LENGTH;
            Boolean status = Win32Api.DeviceIoControl(hubDevice,
                                                      Win32Api.IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION,
                                                      ref buffer,
                                                      Marshal.SizeOf(buffer),
                                                      ref buffer,
                                                      Marshal.SizeOf(buffer),
                                                      out Int32 bytesReturned,
                                                      IntPtr.Zero);

            return(status ? buffer.Data.String : null);
        }
Exemplo n.º 3
0
        static async Task <UsbConfigurationDescriptors> GetConfigurationDescriptor(DeviceFile hub, ushort connectionIndex, byte numConfigurations)
        {
            var result = new UsbConfigurationDescriptors();

            for (byte configIndex = 0; configIndex < numConfigurations; ++configIndex)
            {
                var buf     = new byte[Marshal.SizeOf <UsbDescriptorRequest>() + Marshal.SizeOf <UsbConfigurationDescriptor>()];
                var request = new UsbDescriptorRequest()
                {
                    ConnectionIndex = connectionIndex,
                    SetupPacket     =
                    {
                        wLength = (ushort)Marshal.SizeOf <UsbConfigurationDescriptor>(),
                        wValue  = (ushort)(((byte)UsbDescriptorType.USB_CONFIGURATION_DESCRIPTOR_TYPE << 8) | configIndex)
                    }
                };
                StructToBytes(request, buf, 0);
                await hub.IoControlAsync(IoControl.IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, buf, buf);

                BytesToStruct(buf, Marshal.SizeOf <UsbDescriptorRequest>(), out UsbConfigurationDescriptor configuration);
                buf     = new byte[Marshal.SizeOf <UsbDescriptorRequest>() + configuration.wTotalLength];
                request = new UsbDescriptorRequest()
                {
                    ConnectionIndex = connectionIndex,
                    SetupPacket     =
                    {
                        wLength = configuration.wTotalLength,
                        wValue  = (ushort)(((byte)UsbDescriptorType.USB_CONFIGURATION_DESCRIPTOR_TYPE << 8) | configIndex)
                    }
                };
                StructToBytes(request, buf, 0);
                await hub.IoControlAsync(IoControl.IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, buf, buf);

                result.AddDescriptor(buf.AsSpan(Marshal.SizeOf <UsbDescriptorRequest>()));
            }

            return(result);
        }