Mercurial > hg > ucis.core
comparison USBLib/Descriptor/UsbInfo.cs @ 68:e811297f5aa4
Updated USBLib: removed old LibUsbDotNet compatibility code and added new information helper classes
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Wed, 16 Oct 2013 16:58:39 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
67:2d16447eff12 | 68:e811297f5aa4 |
---|---|
1 using System; | |
2 using System.Collections.Generic; | |
3 using System.Text; | |
4 using UCIS.USBLib.Communication; | |
5 using UCIS.Util; | |
6 | |
7 namespace UCIS.USBLib.Descriptor { | |
8 public class UsbDeviceInfo { | |
9 private UsbDeviceDescriptor mDeviceDescriptor; | |
10 private Boolean mHasDeviceDescriptor = false; | |
11 private UsbConfigurationInfo[] mConfigurations = null; | |
12 private short language = 0; | |
13 public IUsbInterface Device { get; private set; } | |
14 public UsbDeviceInfo(IUsbInterface device) { | |
15 if (device == null) throw new ArgumentNullException("device"); | |
16 this.Device = device; | |
17 } | |
18 private void GetDescriptor() { | |
19 if (mHasDeviceDescriptor) return; | |
20 mDeviceDescriptor = UsbDeviceDescriptor.FromDevice(Device); | |
21 mHasDeviceDescriptor = (mDeviceDescriptor.Length >= UsbDeviceDescriptor.Size && mDeviceDescriptor.Type == UsbDescriptorType.Device); | |
22 } | |
23 public String GetString(Byte index, short langId) { | |
24 if (index == 0) return null; | |
25 return UsbStringDescriptor.GetStringFromDevice(Device, index, langId); | |
26 } | |
27 public String GetString(Byte index) { | |
28 if (language == 0) { | |
29 Byte[] buff = new Byte[4]; | |
30 int len = Device.GetDescriptor((Byte)UsbDescriptorType.String, 0, 0, buff, 0, buff.Length); | |
31 if (len >= 4) language = BitConverter.ToInt16(buff, 2); | |
32 } | |
33 return GetString(index, language); | |
34 } | |
35 public UsbDeviceDescriptor Descriptor { get { GetDescriptor(); return mDeviceDescriptor; } } | |
36 public String ManufacturerString { get { return GetString(Descriptor.ManufacturerStringID); } } | |
37 public String ProductString { get { return GetString(Descriptor.ProductStringID); } } | |
38 public String SerialString { get { return GetString(Descriptor.SerialNumberStringID); } } | |
39 public IList<UsbConfigurationInfo> Configurations { | |
40 get { | |
41 if (mConfigurations == null) { | |
42 mConfigurations = new UsbConfigurationInfo[Descriptor.NumConfigurations]; | |
43 for (Byte i = 0; i < mConfigurations.Length; i++) mConfigurations[i] = new UsbConfigurationInfo(this, i); | |
44 } | |
45 return mConfigurations; | |
46 } | |
47 } | |
48 public UsbConfigurationInfo FindConfiguration(Byte value) { | |
49 foreach (UsbConfigurationInfo configuration in Configurations) if (configuration.Descriptor.ConfigurationValue == value) return configuration; | |
50 return null; | |
51 } | |
52 public static UsbDeviceInfo FromDevice(IUsbInterface device) { | |
53 return new UsbDeviceInfo(device); | |
54 } | |
55 public static UsbDeviceInfo FromDeviceNode(UCIS.HWLib.Windows.Devices.DeviceNode devnode) { | |
56 IUsbInterface device = UCIS.HWLib.Windows.USB.UsbDevice.GetUsbDevice(devnode); | |
57 if (device == null) return null; | |
58 return new UsbDeviceInfo(device); | |
59 } | |
60 } | |
61 public class UsbConfigurationInfo { | |
62 private UsbConfigurationDescriptor mConfigurationDescriptor; | |
63 private Boolean mHasConfigurationDescriptor = false; | |
64 private Byte[] mConfigurationBlob = null; | |
65 private UsbDescriptorBlob[] mDescriptors = null; | |
66 private UsbInterfaceInfo[] mInterfaces = null; | |
67 public UsbDeviceInfo Device { get; private set; } | |
68 public Byte Index { get; private set; } | |
69 internal UsbConfigurationInfo(UsbDeviceInfo device, Byte index) { | |
70 this.Device = device; | |
71 this.Index = index; | |
72 } | |
73 private void GetConfigurationBlob() { | |
74 if (mConfigurationBlob != null) return; | |
75 int length = Descriptor.TotalLength; | |
76 if (!mHasConfigurationDescriptor) throw new Exception("Configuration descriptor is invalid"); | |
77 Byte[] blob = new Byte[length]; | |
78 length = Device.Device.GetDescriptor((Byte)UsbDescriptorType.Configuration, Index, 0, blob, 0, length); | |
79 if (length != blob.Length) throw new Exception("Could not read configuration descriptor"); | |
80 List<UsbDescriptorBlob> descriptors = new List<UsbDescriptorBlob>(); | |
81 for (int offset = 0; offset < length; ) { | |
82 if (length - offset < 2) throw new Exception("Descriptor has been truncated"); | |
83 UsbDescriptorBlob descriptor = new UsbDescriptorBlob(blob, offset); | |
84 if (length - offset < descriptor.Length) throw new Exception("Descriptor has been truncated"); | |
85 descriptors.Add(descriptor); | |
86 offset += descriptor.Length; | |
87 } | |
88 mConfigurationBlob = blob; | |
89 mDescriptors = descriptors.ToArray(); | |
90 } | |
91 public UsbConfigurationDescriptor Descriptor { | |
92 get { | |
93 if (!mHasConfigurationDescriptor) { | |
94 mConfigurationDescriptor = UsbConfigurationDescriptor.FromDevice(Device.Device, Index); | |
95 mHasConfigurationDescriptor = (mConfigurationDescriptor.Length >= UsbConfigurationDescriptor.Size && mConfigurationDescriptor.Type == UsbDescriptorType.Configuration); | |
96 } | |
97 return mConfigurationDescriptor; | |
98 } | |
99 } | |
100 public IList<UsbDescriptorBlob> Descriptors { get { GetConfigurationBlob(); return mDescriptors; } } | |
101 private void GetInterfaces() { | |
102 if (mInterfaces != null) return; | |
103 GetConfigurationBlob(); | |
104 UsbInterfaceInfo[] interfaces = new UsbInterfaceInfo[Descriptor.NumInterfaces]; | |
105 int index = 0; | |
106 int first = -1; | |
107 for (int i = 0; i < mDescriptors.Length; i++) { | |
108 UsbDescriptorBlob descriptor = mDescriptors[i]; | |
109 if (descriptor.Type != UsbDescriptorType.Interface) continue; | |
110 if (first != -1) { | |
111 if (index >= interfaces.Length) Array.Resize(ref interfaces, index + 1); | |
112 interfaces[index] = new UsbInterfaceInfo(this, ArrayUtil.Slice(mDescriptors, first, i - first)); | |
113 index++; | |
114 } | |
115 first = i; | |
116 } | |
117 if (first != -1) { | |
118 if (index >= interfaces.Length) Array.Resize(ref interfaces, index + 1); | |
119 interfaces[index] = new UsbInterfaceInfo(this, ArrayUtil.Slice(mDescriptors, first, mDescriptors.Length - first)); | |
120 } | |
121 mInterfaces = interfaces; | |
122 } | |
123 public IList<UsbInterfaceInfo> Interfaces { get { GetInterfaces(); return mInterfaces; } } | |
124 public UsbInterfaceInfo FindInterface(Predicate<UsbInterfaceInfo> predicate) { | |
125 GetInterfaces(); | |
126 foreach (UsbInterfaceInfo interf in mInterfaces) if (predicate(interf)) return interf; | |
127 return null; | |
128 } | |
129 public UsbInterfaceInfo FindInterface(Byte number) { | |
130 return FindInterface(number, 0); | |
131 } | |
132 public UsbInterfaceInfo FindInterface(Byte number, Byte alternateSetting) { | |
133 return FindInterface(delegate(UsbInterfaceInfo interf) { return interf.Descriptor.InterfaceNumber == number && interf.Descriptor.AlternateSetting == alternateSetting; }); | |
134 } | |
135 public UsbInterfaceInfo FindInterfaceByClass(UsbClassCode usbclass) { | |
136 return FindInterface(delegate(UsbInterfaceInfo interf) { return interf.Descriptor.InterfaceClass == usbclass; }); | |
137 } | |
138 public UsbInterfaceInfo FindInterfaceByClass(UsbClassCode usbclass, Byte subclass) { | |
139 return FindInterface(delegate(UsbInterfaceInfo interf) { return interf.Descriptor.InterfaceClass == usbclass && interf.Descriptor.InterfaceSubClass == subclass; }); | |
140 } | |
141 public UsbDescriptorBlob FindDescriptor(UsbDescriptorType type) { | |
142 GetConfigurationBlob(); | |
143 return Array.Find(mDescriptors, delegate(UsbDescriptorBlob descriptor) { return descriptor.Type == type; }); | |
144 } | |
145 public UsbDescriptorBlob[] FindDescriptors(UsbDescriptorType type) { | |
146 GetConfigurationBlob(); | |
147 return Array.FindAll(mDescriptors, delegate(UsbDescriptorBlob descriptor) { return descriptor.Type == type; }); | |
148 } | |
149 } | |
150 public class UsbInterfaceInfo { | |
151 public UsbConfigurationInfo Configuration { get; private set; } | |
152 private UsbDescriptorBlob[] mDescriptors; | |
153 public UsbInterfaceInfo(UsbConfigurationInfo configuration, UsbDescriptorBlob[] descriptors) { | |
154 this.Configuration = configuration; | |
155 this.mDescriptors = descriptors; | |
156 } | |
157 public UsbInterfaceDescriptor Descriptor { get { return (UsbInterfaceDescriptor)mDescriptors[0]; } } | |
158 public IList<UsbEndpointDescriptor> Endpoints { | |
159 get { | |
160 return Array.ConvertAll(FindDescriptors(UsbDescriptorType.Endpoint), delegate(UsbDescriptorBlob descriptor) { return (UsbEndpointDescriptor)descriptor; }); | |
161 } | |
162 } | |
163 public UsbEndpointDescriptor FindEndpoint(Predicate<UsbEndpointDescriptor> predicate) { | |
164 foreach (UsbDescriptorBlob descriptor in mDescriptors) { | |
165 if (descriptor.Type != UsbDescriptorType.Endpoint) continue; | |
166 UsbEndpointDescriptor ep = (UsbEndpointDescriptor)descriptor; | |
167 if (predicate(ep)) return ep; | |
168 } | |
169 return default(UsbEndpointDescriptor); | |
170 } | |
171 public UsbEndpointDescriptor FindEndpoint(Byte address) { | |
172 return FindEndpoint(delegate(UsbEndpointDescriptor ep) { return ep.EndpointAddress == address; }); | |
173 } | |
174 public UsbEndpointDescriptor FindEndpoint(Boolean input) { | |
175 return FindEndpoint(delegate(UsbEndpointDescriptor ep) { return ep.IsInput == input; }); | |
176 } | |
177 public UsbEndpointDescriptor FindEndpoint(Boolean input, int transferType) { | |
178 return FindEndpoint(delegate(UsbEndpointDescriptor ep) { return ep.TransferType == transferType && ep.IsInput == input; }); | |
179 } | |
180 public UsbDescriptorBlob FindDescriptor(UsbDescriptorType type) { | |
181 return Array.Find(mDescriptors, delegate(UsbDescriptorBlob descriptor) { return descriptor.Type == type; }); | |
182 } | |
183 public UsbDescriptorBlob[] FindDescriptors(UsbDescriptorType type) { | |
184 return Array.FindAll(mDescriptors, delegate(UsbDescriptorBlob descriptor) { return descriptor.Type == type; }); | |
185 } | |
186 } | |
187 } |