comparison USBLib/Windows/USB/UsbController.cs @ 63:309c705d7460

Updates and cleanup in Windows USB enumeration code
author Ivo Smits <Ivo@UCIS.nl>
date Sun, 13 Oct 2013 18:43:45 +0200
parents edc41c861d96
children
comparison
equal deleted inserted replaced
62:edc41c861d96 63:309c705d7460
1 using System; 1 using System;
2 using System.Collections.Generic; 2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Runtime.InteropServices;
3 using Microsoft.Win32.SafeHandles; 5 using Microsoft.Win32.SafeHandles;
4 using UCIS.HWLib.Windows.Devices; 6 using UCIS.HWLib.Windows.Devices;
5 using UCIS.USBLib.Internal.Windows; 7 using UCIS.USBLib.Internal.Windows;
6 8
7 namespace UCIS.HWLib.Windows.USB { 9 namespace UCIS.HWLib.Windows.USB {
8 public class UsbController { 10 public class UsbController {
9 static readonly Guid IID_DEVINTERFACE_USB_HOST_CONTROLLER = new Guid(UsbApi.GUID_DEVINTERFACE_USB_HOST_CONTROLLER);
10 public String DevicePath { get; private set; } 11 public String DevicePath { get; private set; }
11 public DeviceNode DeviceNode { get; private set; } 12 public DeviceNode DeviceNode { get; private set; }
12 public String DeviceDescription { get { return DeviceNode.DeviceDescription; } } 13 public String DeviceDescription { get { return DeviceNode.DeviceDescription; } }
13 public String DriverKey { get { return DeviceNode.DriverKey; } } 14 public String DriverKey { get { return DeviceNode.DriverKey; } }
14 public UsbHub RootHub { 15 public UsbHub RootHub {
15 get { 16 get {
16 String rootHubName; 17 USB_ROOT_HUB_NAME rootHubName = new USB_ROOT_HUB_NAME();
17 using (SafeFileHandle handle = UsbHub.OpenHandle(DevicePath)) rootHubName = UsbHub.GetRootHubName(handle); 18 int nBytesReturned;
18 return new UsbHub(null, new USB_NODE_CONNECTION_INFORMATION_EX(), @"\\?\" + rootHubName, 0, true); 19 using (SafeFileHandle handle = UsbHub.OpenHandle(DevicePath))
20 if (!Kernel32.DeviceIoControl(handle, UsbApi.IOCTL_USB_GET_ROOT_HUB_NAME, IntPtr.Zero, 0, out rootHubName, Marshal.SizeOf(rootHubName), out nBytesReturned, IntPtr.Zero))
21 throw new Win32Exception(Marshal.GetLastWin32Error());
22 if (rootHubName.ActualLength <= 0) return null;
23 return new UsbHub(null, @"\\?\" + rootHubName.RootHubName, 0);
19 } 24 }
20 } 25 }
21 private UsbController(DeviceNode di, String devicePath) { 26 private UsbController(DeviceNode di, String devicePath) {
22 this.DeviceNode = di; 27 this.DeviceNode = di;
23 this.DevicePath = devicePath; 28 this.DevicePath = devicePath;
24 } 29 }
25 30
31 static readonly Guid IID_DEVINTERFACE_USB_HOST_CONTROLLER = new Guid(UsbApi.GUID_DEVINTERFACE_USB_HOST_CONTROLLER);
26 public static UsbController GetControllerForDeviceNode(DeviceNode node) { 32 public static UsbController GetControllerForDeviceNode(DeviceNode node) {
33 if (node == null) return null;
27 String[] interfaces = node.GetInterfaces(IID_DEVINTERFACE_USB_HOST_CONTROLLER); 34 String[] interfaces = node.GetInterfaces(IID_DEVINTERFACE_USB_HOST_CONTROLLER);
28 if (interfaces == null || interfaces.Length == 0) return null; 35 if (interfaces == null || interfaces.Length == 0) return null;
29 return new UsbController(node, interfaces[0]); 36 return new UsbController(node, interfaces[0]);
30 } 37 }
31 38