comparison USBLib/Windows/USB/UsbDevice.cs @ 62:edc41c861d96

Updates and cleanup in Windows USB enumeration code
author Ivo Smits <Ivo@UCIS.nl>
date Sun, 13 Oct 2013 02:47:08 +0200
parents 2b24666cd759
children 309c705d7460
comparison
equal deleted inserted replaced
61:2b24666cd759 62:edc41c861d96
37 } 37 }
38 protected unsafe static int GetDescriptor(SafeFileHandle handle, UInt32 port, byte descriptorType, byte index, short langId, byte[] buffer, int offset, int length) { 38 protected unsafe static int GetDescriptor(SafeFileHandle handle, UInt32 port, byte descriptorType, byte index, short langId, byte[] buffer, int offset, int length) {
39 int szRequest = Marshal.SizeOf(typeof(USB_DESCRIPTOR_REQUEST)); 39 int szRequest = Marshal.SizeOf(typeof(USB_DESCRIPTOR_REQUEST));
40 USB_DESCRIPTOR_REQUEST request = new USB_DESCRIPTOR_REQUEST(); 40 USB_DESCRIPTOR_REQUEST request = new USB_DESCRIPTOR_REQUEST();
41 request.ConnectionIndex = port; 41 request.ConnectionIndex = port;
42 request.SetupPacket.wValue = (ushort)((descriptorType << 8) + index); 42 request.SetupPacket.Value = (short)((descriptorType << 8) + index);
43 request.SetupPacket.wIndex = (ushort)langId; 43 request.SetupPacket.Index = (short)langId;
44 request.SetupPacket.wLength = (ushort)length; 44 request.SetupPacket.Length = (short)length;
45 int nBytes = length + szRequest; 45 int nBytes = length + szRequest;
46 Byte[] bigbuffer = new Byte[nBytes]; 46 Byte[] bigbuffer = new Byte[nBytes];
47 if (!Kernel32.DeviceIoControl(handle, UsbApi.IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ref request, Marshal.SizeOf(typeof(USB_DESCRIPTOR_REQUEST)), bigbuffer, nBytes, out nBytes, IntPtr.Zero)) { 47 if (!Kernel32.DeviceIoControl(handle, UsbApi.IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ref request, Marshal.SizeOf(typeof(USB_DESCRIPTOR_REQUEST)), bigbuffer, nBytes, out nBytes, IntPtr.Zero)) {
48 int err = Marshal.GetLastWin32Error(); 48 int err = Marshal.GetLastWin32Error();
49 if (err != 2 && err != 31 && err != 87) throw new Win32Exception(err); 49 if (err != 2 && err != 31 && err != 87) throw new Win32Exception(err);
74 74
75 public UsbDevice Parent { get; protected set; } 75 public UsbDevice Parent { get; protected set; }
76 public String DevicePath { get; private set; } 76 public String DevicePath { get; private set; }
77 public UInt32 AdapterNumber { get; private set; } 77 public UInt32 AdapterNumber { get; private set; }
78 internal USB_NODE_CONNECTION_INFORMATION_EX NodeConnectionInfo { get; set; } 78 internal USB_NODE_CONNECTION_INFORMATION_EX NodeConnectionInfo { get; set; }
79 private USB_DEVICE_DESCRIPTOR DeviceDescriptor { get { return NodeConnectionInfo.DeviceDescriptor; } } 79 public UsbDeviceDescriptor DeviceDescriptor { get { return NodeConnectionInfo.DeviceDescriptor; } }
80 80
81 public bool IsHub { get { return NodeConnectionInfo.DeviceIsHub != 0; } } 81 public bool IsHub { get { return NodeConnectionInfo.DeviceIsHub != 0; } }
82 public bool IsConnected { get { return NodeConnectionInfo.ConnectionStatus == USB_CONNECTION_STATUS.DeviceConnected; } } 82 public bool IsConnected { get { return NodeConnectionInfo.ConnectionStatus == USB_CONNECTION_STATUS.DeviceConnected; } }
83 public string Status { get { return NodeConnectionInfo.ConnectionStatus.ToString(); } } 83 public string Status { get { return NodeConnectionInfo.ConnectionStatus.ToString(); } }
84 public string Speed { get { return NodeConnectionInfo.Speed.ToString(); } } 84 public string Speed { get { return NodeConnectionInfo.Speed.ToString(); } }
85 public Byte CurrentConfigurationValue { get { return NodeConnectionInfo.CurrentConfigurationValue; } }
86 public UInt16 DeviceAddress { get { return NodeConnectionInfo.DeviceAddress; } }
87 public UInt32 NumberOfOpenPipes { get { return NodeConnectionInfo.NumberOfOpenPipes; } }
85 88
86 SafeFileHandle OpenHandle() { 89 SafeFileHandle OpenHandle() {
87 return OpenHandle(DevicePath); 90 return OpenHandle(DevicePath);
88 } 91 }
89 92
90 public int NumConfigurations { get { return DeviceDescriptor == null ? 0 : DeviceDescriptor.bNumConfigurations; } } 93 public int NumConfigurations { get { return DeviceDescriptor.NumConfigurations; } }
91 public int VendorID { get { return DeviceDescriptor == null ? 0 : DeviceDescriptor.idVendor; } } 94 public int VendorID { get { return DeviceDescriptor.VendorID; } }
92 public int ProductID { get { return DeviceDescriptor == null ? 0 : DeviceDescriptor.idProduct; } } 95 public int ProductID { get { return DeviceDescriptor.ProductID; } }
93 96
94 private String GetStringSafe(Byte id) { 97 private String GetStringSafe(Byte id) {
95 if (id == 0) return null; 98 if (id == 0) return null;
96 String s = GetStringDescriptor(id); 99 String s = GetStringDescriptor(id);
97 if (s == null) return s; 100 if (s == null) return s;
98 return s.Trim(' ', '\0'); 101 return s.Trim(' ', '\0');
99 } 102 }
100 103
101 public string Manufacturer { get { return DeviceDescriptor == null ? null : GetStringSafe(DeviceDescriptor.iManufacturer); } } 104 public string Manufacturer { get { return GetStringSafe(DeviceDescriptor.ManufacturerStringID); } }
102 public string Product { get { return DeviceDescriptor == null ? null : GetStringSafe(DeviceDescriptor.iProduct); } } 105 public string Product { get { return GetStringSafe(DeviceDescriptor.ProductStringID); } }
103 public string SerialNumber { get { return DeviceDescriptor == null ? null : GetStringSafe(DeviceDescriptor.iSerialNumber); } } 106 public string SerialNumber { get { return GetStringSafe(DeviceDescriptor.SerialNumberStringID); } }
104 public virtual string DriverKey { get { using (SafeFileHandle handle = OpenHandle(DevicePath)) return UsbHub.GetNodeConnectionDriverKey(handle, AdapterNumber); } } 107 public virtual string DriverKey { get { using (SafeFileHandle handle = OpenHandle(DevicePath)) return UsbHub.GetNodeConnectionDriverKey(handle, AdapterNumber); } }
105 108
106 public virtual string DeviceDescription { get { return DeviceNode == null ? null : DeviceNode.DeviceDescription; } } 109 public virtual string DeviceDescription { get { return DeviceNode == null ? null : DeviceNode.DeviceDescription; } }
107 public string DeviceID { get { return DeviceNode == null ? null : DeviceNode.DeviceID; } } 110 public string DeviceID { get { return DeviceNode == null ? null : DeviceNode.DeviceID; } }
108 111