comparison USBLib/Windows/USB/UsbHub.cs @ 21:dcfec2be27c9

Added USBLib
author Ivo Smits <Ivo@UCIS.nl>
date Mon, 15 Apr 2013 01:04:59 +0200
parents
children 5b14fed54a89
comparison
equal deleted inserted replaced
20:c873e3dd73fe 21:dcfec2be27c9
1 using System;
2 using System.Runtime.InteropServices;
3 using Microsoft.Win32.SafeHandles;
4 using UCIS.USBLib.Internal.Windows;
5 using System.Collections.Generic;
6 using System.Collections.ObjectModel;
7
8 namespace UCIS.HWLib.Windows.USB {
9 public class UsbHub : UsbDevice {
10 public int PortCount { get; private set; }
11 public bool IsBusPowered { get; private set; }
12 public bool IsRootHub { get; private set; }
13 internal USB_NODE_INFORMATION NodeInformation { get; private set; }
14 private List<UsbDevice> devices = new List<UsbDevice>();
15 public IList<UsbDevice> Devices { get { return devices.AsReadOnly(); } }
16 public override string DeviceDescription { get { return IsRootHub ? "RootHub" : "Standard-USB-Hub"; } }
17 internal UsbHub(UsbController parent, USB_DEVICE_DESCRIPTOR deviceDescriptor, string devicePath)
18 : this(null, deviceDescriptor, devicePath, true) { }
19 internal UsbHub(UsbDevice parent, USB_DEVICE_DESCRIPTOR deviceDescriptor, string devicePath, Boolean roothub)
20 : base(parent, deviceDescriptor, 0, devicePath) {
21 this.IsRootHub = roothub;
22
23 // TODO: Get the driver key name for the root hub.
24 // Now let's open the hub (based upon the hub name we got above).
25 using (SafeFileHandle handel2 = Kernel32.CreateFile(this.DevicePath, Kernel32.GENERIC_WRITE, Kernel32.FILE_SHARE_WRITE, IntPtr.Zero, Kernel32.OPEN_EXISTING, 0, IntPtr.Zero)) {
26 if (handel2.IsInvalid) throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
27 USB_NODE_INFORMATION NodeInfo = new USB_NODE_INFORMATION();
28 int nBytes = Marshal.SizeOf(typeof(USB_NODE_INFORMATION)); // Marshal.SizeOf(NodeInfo);
29 // Get the hub information.
30 int nBytesReturned = -1;
31 if (Kernel32.DeviceIoControl(handel2, UsbApi.IOCTL_USB_GET_NODE_INFORMATION, ref NodeInfo, nBytes, out NodeInfo, nBytes, out nBytesReturned, IntPtr.Zero)) {
32 this.NodeInformation = NodeInfo;
33 this.IsBusPowered = Convert.ToBoolean(NodeInfo.HubInformation.HubIsBusPowered);
34 this.PortCount = NodeInfo.HubInformation.HubDescriptor.bNumberOfPorts;
35 }
36
37 for (uint index = 1; index <= PortCount; index++) {
38 devices.Add(BuildDevice(this, index, this.DevicePath, handel2));
39 }
40 }
41 }
42
43 private static UsbDevice BuildDevice(UsbDevice parent, uint portCount, string devicePath, SafeFileHandle handel1) {
44 int nBytesReturned;
45 int nBytes = Marshal.SizeOf(typeof(USB_NODE_CONNECTION_INFORMATION_EX));
46 USB_NODE_CONNECTION_INFORMATION_EX nodeConnection = new USB_NODE_CONNECTION_INFORMATION_EX();
47 nodeConnection.ConnectionIndex = portCount;
48
49 //DateTime t = DateTime.Now;
50 if (!Kernel32.DeviceIoControl(handel1, UsbApi.IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX, ref nodeConnection, nBytes, out nodeConnection, nBytes, out nBytesReturned, IntPtr.Zero))
51 throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
52 //Console.WriteLine("IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX took {0} ms (class={1})", DateTime.Now.Subtract(t).TotalMilliseconds, nodeConnection.DeviceDescriptor.bDeviceClass);
53 bool isConnected = (nodeConnection.ConnectionStatus == USB_CONNECTION_STATUS.DeviceConnected);
54
55 UsbDevice _Device = null;
56 if (!isConnected) {
57 _Device = new UsbDevice(parent, null, portCount);
58 } else if (nodeConnection.DeviceDescriptor.bDeviceClass == UsbDeviceClass.HubDevice) {
59 nBytes = Marshal.SizeOf(typeof(USB_NODE_CONNECTION_NAME));
60 USB_NODE_CONNECTION_NAME nameConnection = new USB_NODE_CONNECTION_NAME();
61 nameConnection.ConnectionIndex = portCount;
62 if (!Kernel32.DeviceIoControl(handel1, UsbApi.IOCTL_USB_GET_NODE_CONNECTION_NAME, ref nameConnection, nBytes, out nameConnection, nBytes, out nBytesReturned, IntPtr.Zero))
63 throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
64 _Device = new UsbHub(parent, nodeConnection.DeviceDescriptor, @"\\?\" + nameConnection.NodeName, false);
65 } else {
66 _Device = new UsbDevice(parent, nodeConnection.DeviceDescriptor, portCount, devicePath);
67 }
68 _Device.NodeConnectionInfo = nodeConnection;
69 _Device.AdapterNumber = _Device.NodeConnectionInfo.ConnectionIndex;
70 _Device.Status = ((USB_CONNECTION_STATUS)_Device.NodeConnectionInfo.ConnectionStatus).ToString();
71 _Device.Speed = ((USB_DEVICE_SPEED)_Device.NodeConnectionInfo.Speed).ToString();
72 _Device.IsConnected = isConnected;
73 _Device.IsHub = Convert.ToBoolean(_Device.NodeConnectionInfo.DeviceIsHub);
74 return _Device;
75 }
76 }
77 }