Mercurial > hg > ucis.core
annotate USBLib/Communication/LibUsb1/LibUsb1Registry.cs @ 75:50d4aed66c67
Improved HTTP classes
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Mon, 03 Feb 2014 22:53:31 +0100 |
parents | 99ed461509fe |
children |
rev | line source |
---|---|
21 | 1 ???using System; |
2 using System.Collections.Generic; | |
3 using System.Text; | |
4 | |
5 namespace UCIS.USBLib.Communication.LibUsb1 { | |
6 public class LibUsb1Registry : IUsbDeviceRegistry { | |
7 static libusb_context Context; | |
8 | |
9 libusb_device Device; | |
10 libusb1.libusb_device_descriptor? DeviceDescriptor = null; | |
11 | |
12 public unsafe static List<LibUsb1Registry> DeviceList { | |
13 get { | |
14 List<LibUsb1Registry> deviceList = new List<LibUsb1Registry>(); | |
15 if (Context == null) { | |
16 int ret = libusb1.libusb_init(out Context); | |
17 if (ret != 0) throw new Exception("libusb_init returned " + ret.ToString()); | |
18 } | |
19 IntPtr* list; | |
20 IntPtr count = libusb1.libusb_get_device_list(Context, out list); | |
21 for (IntPtr* item = list; *item != IntPtr.Zero; item++) { | |
22 deviceList.Add(new LibUsb1Registry(new libusb_device(*item, true))); | |
23 } | |
24 libusb1.libusb_free_device_list(list, 0); | |
25 return deviceList; | |
26 } | |
27 } | |
28 | |
29 private LibUsb1Registry(libusb_device device) { | |
30 this.Device = device; | |
31 } | |
32 private libusb1.libusb_device_descriptor GetDeviceDescriptor() { | |
33 if (DeviceDescriptor == null) { | |
34 libusb1.libusb_device_descriptor descriptor; | |
35 int ret = libusb1.libusb_get_device_descriptor(Device, out descriptor); | |
36 if (ret < 0) throw new Exception("libusb_get_device_descriptor returned " + ret.ToString()); | |
37 DeviceDescriptor = descriptor; | |
38 } | |
39 return DeviceDescriptor.Value; | |
40 } | |
41 | |
64
99ed461509fe
Fixed data type for USB vendor and product IDs
Ivo Smits <Ivo@UCIS.nl>
parents:
37
diff
changeset
|
42 public UInt16 Vid { get { return GetDeviceDescriptor().idVendor; } } |
99ed461509fe
Fixed data type for USB vendor and product IDs
Ivo Smits <Ivo@UCIS.nl>
parents:
37
diff
changeset
|
43 public UInt16 Pid { get { return GetDeviceDescriptor().idProduct; } } |
21 | 44 public byte InterfaceID { get { return 0; } } |
45 | |
46 public string Name { | |
47 get { | |
48 byte iProduct = GetDeviceDescriptor().iProduct; | |
49 libusb_device_handle handle; | |
50 int ret = libusb1.libusb_open(Device, out handle); | |
51 if (ret != 0) return null; | |
52 if (ret != 0) throw new Exception("libusb_open returned " + ret.ToString()); | |
53 StringBuilder data = new StringBuilder(1024); | |
54 ret = libusb1.libusb_get_string_descriptor_ascii(handle, iProduct, data, data.Capacity); | |
55 if (ret < 0) throw new Exception("libusb_get_string_descriptor_ascii returned " + ret.ToString()); | |
56 handle.Close(); | |
57 return data.ToString(); | |
58 } | |
59 } | |
60 public string Manufacturer { | |
61 get { | |
62 byte iProduct = GetDeviceDescriptor().iManufacturer; | |
63 libusb_device_handle handle; | |
64 int ret = libusb1.libusb_open(Device, out handle); | |
65 if (ret != 0) return null; | |
66 if (ret != 0) throw new Exception("libusb_open returned " + ret.ToString()); | |
67 StringBuilder data = new StringBuilder(1024); | |
68 ret = libusb1.libusb_get_string_descriptor_ascii(handle, iProduct, data, data.Capacity); | |
69 if (ret < 0) throw new Exception("libusb_get_string_descriptor_ascii returned " + ret.ToString()); | |
70 handle.Close(); | |
71 return data.ToString(); | |
72 } | |
73 } | |
74 public string FullName { | |
75 get { | |
76 libusb1.libusb_device_descriptor descriptor = GetDeviceDescriptor(); | |
77 String mfg = null, prod = null; | |
78 libusb_device_handle handle; | |
79 int ret = libusb1.libusb_open(Device, out handle); | |
80 if (ret != 0) return null; | |
81 if (ret != 0) throw new Exception("libusb_open returned " + ret.ToString()); | |
82 if (descriptor.iManufacturer != 0) { | |
83 StringBuilder data = new StringBuilder(1024); | |
84 ret = libusb1.libusb_get_string_descriptor_ascii(handle, descriptor.iManufacturer, data, data.Capacity); | |
85 if (ret < 0) throw new Exception("libusb_get_string_descriptor_ascii returned " + ret.ToString()); | |
86 mfg = data.ToString(); | |
87 } | |
88 if (descriptor.iProduct != 0) { | |
89 StringBuilder data = new StringBuilder(1024); | |
90 ret = libusb1.libusb_get_string_descriptor_ascii(handle, descriptor.iProduct, data, data.Capacity); | |
91 if (ret < 0) throw new Exception("libusb_get_string_descriptor_ascii returned " + ret.ToString()); | |
92 prod = data.ToString(); | |
93 } | |
94 handle.Close(); | |
95 if (mfg == null && prod == null) return null; | |
96 if (mfg == null) return prod; | |
97 if (prod == null) return mfg; | |
98 return mfg + " - " + prod; | |
99 } | |
100 } | |
101 public Byte BusNumber { | |
102 get { | |
103 return libusb1.libusb_get_bus_number(Device); | |
104 } | |
105 } | |
106 public Byte DeviceAddress { | |
107 get { | |
108 return libusb1.libusb_get_device_address(Device); | |
109 } | |
110 } | |
111 public String SymbolicName { get { return String.Format("libusb;bus={0};address={1}", BusNumber, DeviceAddress); } } | |
112 IDictionary<String, Object> IUsbDeviceRegistry.DeviceProperties { get { return null; } } | |
113 public LibUsb1Device Open() { | |
114 return new LibUsb1Device(Device, this); | |
115 } | |
116 IUsbDevice IUsbDeviceRegistry.Open() { | |
117 return Open(); | |
118 } | |
119 | |
120 public override bool Equals(object obj) { | |
121 LibUsb1Registry r = obj as LibUsb1Registry; | |
122 if (r == null) return false; | |
123 return r.Device.DangerousGetHandle() == Device.DangerousGetHandle(); | |
124 } | |
37 | 125 |
126 public override int GetHashCode() { | |
127 return Device.DangerousGetHandle().GetHashCode(); | |
128 } | |
21 | 129 } |
130 } |