protected unsafe static string GetStringDescriptor(USBPort port, Byte stringIndex) { UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST descriptor = new UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST(); descriptor.ConnectionIndex = port.portIndex; // USBD will automatically initialize these fields: // bmRequest = 0x80 // bRequest = 0x06 // wValue = Descriptor Type (high) and Descriptor Index (low byte) descriptor.wValue = (ushort)(((ushort)UsbApi.UsbNativeType.UsbDescriptorType.USB_STRING_DESCRIPTOR_TYPE << 8) | (ushort)stringIndex); // wIndex = Zero (or Language ID for String Descriptors) descriptor.wIndex = (ushort)0; // wLength = Length of descriptor buffer descriptor.wLength = (ushort)Marshal.SizeOf(typeof(UsbApi.UsbNativeType.USB_STRING_DESCRIPTOR)); // Collect the information string IOCTLcommand ioctl = new IOCTLcommand(); ioctl.ioctlNo = UsbApi.CTL_CODE(0x00000022, (uint)UsbApi.UsbIoctlFunction.USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, 0, 0x0); ioctl.inputBuffer = UsbApi.UsbNativeType.Serialize(descriptor); ioctl.outputMaxSize = (uint)Marshal.SizeOf(descriptor); string result = null; try { // Open Root hub IntPtr hHub = UsbApi.OpenDevice(port.rootHub.devicePath, true); IoStatus status = UsbApi.DeviceIoControl(hHub, ioctl); UsbApi.CloseDevice(hHub); descriptor = (UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST)UsbApi.UsbNativeType.Deserialize( status.buffer, 0, typeof(UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST)); UsbApi.UsbNativeType.USB_STRING_DESCRIPTOR strDescr = (UsbApi.UsbNativeType.USB_STRING_DESCRIPTOR)UsbApi.UsbNativeType.Deserialize( descriptor.Data, 0, typeof(UsbApi.UsbNativeType.USB_STRING_DESCRIPTOR)); result = new String(Encoding.Unicode.GetChars(strDescr.bString, 0, strDescr.bLength)); } catch (Exception) { result = "Invalid string value"; } return(result); }
internal unsafe static List <UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR> GetConfigurationDescriptors(USBPort port, Byte configIndex) { UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST descriptor = new UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST(); descriptor.ConnectionIndex = port.portIndex; // USBD will automatically initialize these fields: // bmRequest = 0x80 // bRequest = 0x06 // wValue = Descriptor Type (high) and Descriptor Index (low byte) descriptor.wValue = (ushort)(((ushort)UsbApi.UsbNativeType.UsbDescriptorType.USB_CONFIGURATION_DESCRIPTOR_TYPE << 8) | (ushort)configIndex); // wIndex = Zero (or Language ID for String Descriptors) descriptor.wIndex = (ushort)0; // wLength = Length of descriptor buffer descriptor.wLength = (ushort)512; // Collect the information string IOCTLcommand ioctl = new IOCTLcommand(); ioctl.ioctlNo = UsbApi.CTL_CODE(0x00000022, (uint)UsbApi.UsbIoctlFunction.USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, 0, 0x0); ioctl.inputBuffer = UsbApi.UsbNativeType.Serialize(descriptor); ioctl.outputMaxSize = (uint)Marshal.SizeOf(descriptor); List <UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR> descriptorList = new List <UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR>(); try { // Open Root hub IntPtr hHub = UsbApi.OpenDevice(port.rootHub.devicePath, true); IoStatus status = UsbApi.DeviceIoControl(hHub, ioctl); UsbApi.CloseDevice(hHub); descriptor = (UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST)UsbApi.UsbNativeType.Deserialize( status.buffer, 0, typeof(UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST)); UsbApi.UsbNativeType.USB_CONFIGURATION_DESCRIPTOR confDescr = (UsbApi.UsbNativeType.USB_CONFIGURATION_DESCRIPTOR)UsbApi.UsbNativeType.Deserialize( descriptor.Data, 0, typeof(UsbApi.UsbNativeType.USB_CONFIGURATION_DESCRIPTOR)); descriptorList.Add(confDescr); // Now deserialize all returned descriptors int descrStart = confDescr.bLength; UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR common; while (descrStart < confDescr.wTotalLength) { common = (UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR)UsbApi.UsbNativeType.Deserialize( descriptor.Data, descrStart, typeof(UsbApi.UsbNativeType.USB_COMMON_DESCRIPTOR)); switch (common.DescriptorType) { case UsbApi.UsbNativeType.UsbDescriptorType.USB_CONFIGURATION_DESCRIPTOR_TYPE: common = (UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR)UsbApi.UsbNativeType.Deserialize( descriptor.Data, descrStart, typeof(UsbApi.UsbNativeType.USB_CONFIGURATION_DESCRIPTOR)); break; case UsbApi.UsbNativeType.UsbDescriptorType.USB_INTERFACE_DESCRIPTOR_TYPE: common = (UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR)UsbApi.UsbNativeType.Deserialize( descriptor.Data, descrStart, typeof(UsbApi.UsbNativeType.USB_INTERFACE_DESCRIPTOR)); break; case UsbApi.UsbNativeType.UsbDescriptorType.USB_ENDPOINT_DESCRIPTOR_TYPE: common = (UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR)UsbApi.UsbNativeType.Deserialize( descriptor.Data, descrStart, typeof(UsbApi.UsbNativeType.USB_ENDPOINT_DESCRIPTOR)); break; } descrStart += common.Length; descriptorList.Add(common); } } catch (Exception) {} return(descriptorList); }