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