21
|
1 ???using System; |
|
2 using System.Runtime.InteropServices; |
|
3 using System.Text; |
|
4 using Microsoft.Win32.SafeHandles; |
|
5 using ssize_t = System.IntPtr; |
|
6 |
|
7 namespace UCIS.USBLib.Communication.LibUsb1 { |
|
8 class libusb_context : SafeHandleZeroOrMinusOneIsInvalid { |
|
9 public libusb_context() : base(true) { } |
|
10 protected override bool ReleaseHandle() { |
|
11 libusb1.libusb_exit(handle); |
|
12 return true; |
|
13 } |
|
14 } |
|
15 class libusb_device : SafeHandleZeroOrMinusOneIsInvalid { |
|
16 public libusb_device() : base(true) { } |
|
17 public libusb_device(IntPtr handle, Boolean ownsHandle) : base(ownsHandle) { SetHandle(handle); } |
|
18 protected override bool ReleaseHandle() { |
|
19 libusb1.libusb_unref_device(handle); |
|
20 return true; |
|
21 } |
|
22 } |
|
23 class libusb_device_handle : SafeHandleZeroOrMinusOneIsInvalid { |
|
24 public libusb_device_handle() : base(true) { } |
|
25 protected override bool ReleaseHandle() { |
|
26 libusb1.libusb_close(handle); |
|
27 return true; |
|
28 } |
|
29 } |
|
30 unsafe static class libusb1 { |
|
31 const CallingConvention LIBUSB1_CC = CallingConvention.Winapi; |
|
32 const String LIBUSB1_DLL = "libusb-1.0.dll"; |
|
33 |
|
34 [StructLayout(LayoutKind.Sequential, Pack = 1)] |
|
35 public struct libusb_device_descriptor { |
|
36 public Byte bLength; |
|
37 public Byte bDescriptorType; |
|
38 public UInt16 bcdUSB; |
|
39 public Byte bDeviceClass; |
|
40 public Byte bDeviceSubClass; |
|
41 public Byte bDeviceProtocol; |
|
42 public Byte bMaxPacketSize0; |
|
43 public UInt16 idVendor; |
|
44 public UInt16 idProduct; |
|
45 public UInt16 bcdDevice; |
|
46 public Byte iManufacturer; |
|
47 public Byte iProduct; |
|
48 public Byte iSerialNumber; |
|
49 public Byte bNumConfigurations; |
|
50 } |
|
51 |
|
52 [StructLayout(LayoutKind.Sequential)] |
|
53 struct libusb_endpoint_descriptor { |
|
54 Byte bLength; |
|
55 Byte bDescriptorType; |
|
56 Byte bEndpointAddress; |
|
57 Byte bmAttributes; |
|
58 UInt16 wMaxPacketSize; |
|
59 Byte bInterval; |
|
60 Byte bRefresh; |
|
61 Byte bSynchAddress; |
|
62 byte* extra; |
|
63 int extra_length; |
|
64 } |
|
65 |
|
66 [StructLayout(LayoutKind.Sequential)] |
22
|
67 struct libusb_interface_descriptor { |
21
|
68 Byte bLength; |
|
69 Byte bDescriptorType; |
22
|
70 Byte bInterfaceNumber; |
|
71 Byte bAlternateSetting; |
|
72 Byte bNumEndpoints; |
|
73 Byte bInterfaceClass; |
|
74 Byte bInterfaceSubClass; |
|
75 Byte bInterfaceProtocol; |
|
76 Byte iInterface; |
|
77 libusb_endpoint_descriptor* endpoint; |
21
|
78 Byte* extra; |
|
79 int extra_length; |
|
80 } |
22
|
81 [StructLayout(LayoutKind.Sequential)] |
|
82 struct libusb_interface { |
|
83 libusb_interface_descriptor* altsetting; |
|
84 int num_altsetting; |
21
|
85 } |
22
|
86 struct libusb_config_descriptor { |
|
87 Byte bLength; |
|
88 Byte bDescriptorType; |
|
89 Byte wTotalLength; |
|
90 Byte bNumInterfaces; |
|
91 Byte bConfigurationValue; |
|
92 Byte iConfiguration; |
|
93 Byte bmAttributes; |
|
94 Byte MaxPower; |
|
95 libusb_interface* @interface; |
|
96 Byte* extra; |
|
97 int extra_length; |
21
|
98 } |
|
99 |
|
100 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
101 public static extern int libusb_init(out libusb_context ctx); |
|
102 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
103 public static extern void libusb_exit(IntPtr ctx); |
|
104 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
105 static extern void libusb_set_debug(libusb_context ctx, int level); |
|
106 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
107 static extern Byte* libusb_error_name(int errcode); |
|
108 |
|
109 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
110 public static extern ssize_t libusb_get_device_list(libusb_context ctx, out IntPtr* list); //libusb_device** list |
|
111 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
112 public static extern void libusb_free_device_list(IntPtr* list, int unref_devices); |
|
113 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
114 static extern libusb_device libusb_ref_device(libusb_device dev); |
|
115 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
116 public static extern void libusb_unref_device(IntPtr dev); |
|
117 |
|
118 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
119 static extern int libusb_get_configuration(libusb_device_handle dev, out int config); |
|
120 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
121 public static extern int libusb_get_device_descriptor(libusb_device dev, out libusb_device_descriptor desc); |
|
122 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
123 static extern int libusb_get_active_config_descriptor(libusb_device dev, libusb_config_descriptor** config); |
|
124 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
125 static extern int libusb_get_config_descriptor(libusb_device dev, Byte config_index, libusb_config_descriptor** config); |
|
126 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
127 static extern int libusb_get_config_descriptor_by_value(libusb_device dev, Byte bConfigurationValue, libusb_config_descriptor** config); |
|
128 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
129 static extern void libusb_free_config_descriptor(libusb_config_descriptor* config); |
|
130 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
131 public static extern Byte libusb_get_bus_number(libusb_device dev); |
|
132 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
133 public static extern Byte libusb_get_device_address(libusb_device dev); |
|
134 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
135 static extern int libusb_get_device_speed(libusb_device dev); |
|
136 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
137 static extern int libusb_get_max_packet_size(libusb_device dev, Byte endpoint); |
|
138 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
139 static extern int libusb_get_max_iso_packet_size(libusb_device dev, Byte endpoint); |
|
140 |
|
141 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
142 public static extern int libusb_open(libusb_device dev, out libusb_device_handle handle); |
|
143 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
144 public static extern void libusb_close(IntPtr dev_handle); |
|
145 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
146 static extern libusb_device libusb_get_device(libusb_device_handle dev_handle); |
|
147 |
|
148 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
149 public static extern int libusb_set_configuration(libusb_device_handle dev, int configuration); |
|
150 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
151 public static extern int libusb_claim_interface(libusb_device_handle dev, int interface_number); |
|
152 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
153 public static extern int libusb_release_interface(libusb_device_handle dev, int interface_number); |
|
154 |
|
155 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
156 static extern libusb_device_handle libusb_open_device_with_vid_pid(libusb_context ctx, UInt16 vendor_id, UInt16 product_id); |
|
157 |
|
158 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
159 static extern int libusb_set_interface_alt_setting(libusb_device_handle dev, int interface_number, int alternate_setting); |
|
160 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
161 static extern int libusb_clear_halt(libusb_device_handle dev, Byte endpoint); |
|
162 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
163 public static extern int libusb_reset_device(libusb_device_handle dev); |
|
164 |
|
165 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
166 static extern int libusb_kernel_driver_active(libusb_device_handle dev, int interface_number); |
|
167 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
168 public static extern int libusb_detach_kernel_driver(libusb_device_handle dev, int interface_number); |
|
169 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
|
170 public static extern int libusb_attach_kernel_driver(libusb_device_handle dev, int interface_number); |
|
171 |
|
172 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
22
|
173 public static extern int libusb_control_transfer(libusb_device_handle dev_handle, Byte request_type, Byte bRequest, UInt16 wValue, UInt16 wIndex, Byte* data, UInt16 wLength, UInt32 timeout); |
21
|
174 |
|
175 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
22
|
176 public static extern int libusb_bulk_transfer(libusb_device_handle dev_handle, Byte endpoint, Byte* data, int length, out int actual_length, UInt32 timeout); |
21
|
177 |
|
178 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
22
|
179 public static extern int libusb_interrupt_transfer(libusb_device_handle dev_handle, Byte endpoint, Byte* data, int length, out int actual_length, UInt32 timeout); |
21
|
180 |
|
181 [DllImport(LIBUSB1_DLL, CallingConvention = LIBUSB1_CC)] |
22
|
182 public static extern int libusb_get_string_descriptor_ascii(libusb_device_handle dev, Byte desc_index, [MarshalAs(UnmanagedType.LPStr)] StringBuilder data, int length); |
21
|
183 } |
|
184 } |