libusb 0.8.0-aarch64-mingw-ucrt

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/.appveyor.yml +33 -0
  3. data/.github/workflows/ci.yml +185 -0
  4. data/.gitignore +9 -0
  5. data/.travis.yml +26 -0
  6. data/.yardopts +6 -0
  7. data/COPYING +165 -0
  8. data/Gemfile +20 -0
  9. data/History.md +225 -0
  10. data/README.md +184 -0
  11. data/Rakefile +82 -0
  12. data/lib/libusb/bos.rb +501 -0
  13. data/lib/libusb/call.rb +707 -0
  14. data/lib/libusb/compat.rb +376 -0
  15. data/lib/libusb/configuration.rb +154 -0
  16. data/lib/libusb/constants.rb +181 -0
  17. data/lib/libusb/context.rb +577 -0
  18. data/lib/libusb/context_reference.rb +43 -0
  19. data/lib/libusb/dependencies.rb +7 -0
  20. data/lib/libusb/dev_handle.rb +571 -0
  21. data/lib/libusb/device.rb +456 -0
  22. data/lib/libusb/endpoint.rb +195 -0
  23. data/lib/libusb/eventmachine.rb +203 -0
  24. data/lib/libusb/gem_helper.rb +152 -0
  25. data/lib/libusb/interface.rb +60 -0
  26. data/lib/libusb/libusb_recipe.rb +29 -0
  27. data/lib/libusb/setting.rb +132 -0
  28. data/lib/libusb/ss_companion.rb +72 -0
  29. data/lib/libusb/stdio.rb +25 -0
  30. data/lib/libusb/transfer.rb +418 -0
  31. data/lib/libusb/version_gem.rb +19 -0
  32. data/lib/libusb/version_struct.rb +63 -0
  33. data/lib/libusb-1.0.dll +0 -0
  34. data/lib/libusb.rb +146 -0
  35. data/libusb.gemspec +28 -0
  36. data/test/test_libusb.rb +42 -0
  37. data/test/test_libusb_bos.rb +162 -0
  38. data/test/test_libusb_bulk_stream_transfer.rb +61 -0
  39. data/test/test_libusb_compat.rb +78 -0
  40. data/test/test_libusb_compat_mass_storage.rb +81 -0
  41. data/test/test_libusb_context.rb +88 -0
  42. data/test/test_libusb_descriptors.rb +258 -0
  43. data/test/test_libusb_event_machine.rb +118 -0
  44. data/test/test_libusb_event_machine_eintr.rb +81 -0
  45. data/test/test_libusb_gc.rb +52 -0
  46. data/test/test_libusb_hotplug.rb +129 -0
  47. data/test/test_libusb_iso_transfer.rb +56 -0
  48. data/test/test_libusb_mass_storage.rb +268 -0
  49. data/test/test_libusb_mass_storage2.rb +96 -0
  50. data/test/test_libusb_structs.rb +87 -0
  51. data/test/test_libusb_threads.rb +89 -0
  52. data/wireshark-usb-sniffer.png +0 -0
  53. metadata +110 -0
@@ -0,0 +1,707 @@
1
+ # This file is part of Libusb for Ruby.
2
+ #
3
+ # Libusb for Ruby is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU Lesser General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # Libusb for Ruby is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public License
14
+ # along with Libusb for Ruby. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ require 'rubygems'
17
+ require 'ffi'
18
+
19
+ module LIBUSB
20
+ # C level interface - for internal use only
21
+ #
22
+ # All enum codes are available as constants in {LIBUSB} namespace.
23
+ module Call
24
+ extend FFI::Library
25
+
26
+ root_path = File.expand_path("../../..", __FILE__)
27
+ ext = FFI::Platform::LIBSUFFIX
28
+ prefix = FFI::Platform::LIBPREFIX.empty? ? 'lib' : FFI::Platform::LIBPREFIX
29
+ bundled_dll = File.join(root_path, "lib/#{prefix}usb-1.0.#{ext}")
30
+ bundled_dll_cygwin = File.join(root_path, "bin/#{prefix}usb-1.0.#{ext}")
31
+ ffi_lib([bundled_dll, bundled_dll_cygwin, "#{prefix}usb-1.0.#{ext}.0", "#{prefix}usb-1.0", "#{prefix}usb"])
32
+
33
+ ClassCodes = enum :libusb_class_code, [
34
+ :CLASS_PER_INTERFACE, 0,
35
+ :CLASS_AUDIO, 1,
36
+ :CLASS_COMM, 2,
37
+ :CLASS_HID, 3,
38
+ :CLASS_PRINTER, 7,
39
+ :CLASS_PTP, 6,
40
+ :CLASS_MASS_STORAGE, 8,
41
+ :CLASS_HUB, 9,
42
+ :CLASS_DATA, 10,
43
+ :CLASS_WIRELESS, 0xe0,
44
+ :CLASS_APPLICATION, 0xfe,
45
+ :CLASS_VENDOR_SPEC, 0xff
46
+ ]
47
+
48
+ Errors = enum :libusb_error, [
49
+ :SUCCESS, 0,
50
+ :ERROR_IO, -1,
51
+ :ERROR_INVALID_PARAM, -2,
52
+ :ERROR_ACCESS, -3,
53
+ :ERROR_NO_DEVICE, -4,
54
+ :ERROR_NOT_FOUND, -5,
55
+ :ERROR_BUSY, -6,
56
+ :ERROR_TIMEOUT, -7,
57
+ :ERROR_OVERFLOW, -8,
58
+ :ERROR_PIPE, -9,
59
+ :ERROR_INTERRUPTED, -10,
60
+ :ERROR_NO_MEM, -11,
61
+ :ERROR_NOT_SUPPORTED, -12,
62
+ :ERROR_OTHER, -99,
63
+ ]
64
+
65
+ # Transfer status codes
66
+ TransferStatus = enum :libusb_transfer_status, [
67
+ :TRANSFER_COMPLETED,
68
+ :TRANSFER_ERROR,
69
+ :TRANSFER_TIMED_OUT,
70
+ :TRANSFER_CANCELLED,
71
+ :TRANSFER_STALL,
72
+ :TRANSFER_NO_DEVICE,
73
+ :TRANSFER_OVERFLOW,
74
+ ]
75
+
76
+ # libusb_transfer.flags values
77
+ TransferFlags = enum :libusb_transfer_flags, [
78
+ :TRANSFER_SHORT_NOT_OK, 1 << 0,
79
+ :TRANSFER_FREE_BUFFER, 1 << 1,
80
+ :TRANSFER_FREE_TRANSFER, 1 << 2,
81
+ :TRANSFER_ADD_ZERO_PACKET, 1 << 3,
82
+ ]
83
+
84
+ # Values for {Endpoint#transfer_type}.
85
+ TransferTypes = enum :libusb_transfer_type, [
86
+ # Control endpoint
87
+ :TRANSFER_TYPE_CONTROL, 0,
88
+ # Isochronous endpoint
89
+ :TRANSFER_TYPE_ISOCHRONOUS, 1,
90
+ # Bulk endpoint
91
+ :TRANSFER_TYPE_BULK, 2,
92
+ # Interrupt endpoint
93
+ :TRANSFER_TYPE_INTERRUPT, 3,
94
+ # Stream endpoint
95
+ :TRANSFER_TYPE_BULK_STREAM, 4,
96
+ ]
97
+
98
+ StandardRequests = enum :libusb_standard_request, [
99
+ :REQUEST_GET_STATUS, 0x00,
100
+ :REQUEST_CLEAR_FEATURE, 0x01,
101
+ :REQUEST_SET_FEATURE, 0x03,
102
+ :REQUEST_SET_ADDRESS, 0x05,
103
+ :REQUEST_GET_DESCRIPTOR, 0x06,
104
+ :REQUEST_SET_DESCRIPTOR, 0x07,
105
+ :REQUEST_GET_CONFIGURATION, 0x08,
106
+ :REQUEST_SET_CONFIGURATION, 0x09,
107
+ :REQUEST_GET_INTERFACE, 0x0A,
108
+ :REQUEST_SET_INTERFACE, 0x0B,
109
+ :REQUEST_SYNCH_FRAME, 0x0C,
110
+ ]
111
+
112
+ EndpointDirections = enum :libusb_endpoint_direction, [
113
+ :ENDPOINT_IN, 0x80,
114
+ :ENDPOINT_OUT, 0x00,
115
+ ]
116
+
117
+ DescriptorTypes = enum :libusb_descriptor_type, [
118
+ # Device descriptor. See {Device}
119
+ :DT_DEVICE, 0x01,
120
+ # Configuration descriptor. See {Configuration}
121
+ :DT_CONFIG, 0x02,
122
+ # String descriptor
123
+ :DT_STRING, 0x03,
124
+ # Interface descriptor. See {Interface}
125
+ :DT_INTERFACE, 0x04,
126
+ # Endpoint descriptor. See {Endpoint}
127
+ :DT_ENDPOINT, 0x05,
128
+ # BOS descriptor
129
+ :DT_BOS, 0x0f,
130
+ # Device Capability descriptor
131
+ :DT_DEVICE_CAPABILITY, 0x10,
132
+ # HID descriptor
133
+ :DT_HID, 0x21,
134
+ # HID report descriptor
135
+ :DT_REPORT, 0x22,
136
+ # Physical descriptor
137
+ :DT_PHYSICAL, 0x23,
138
+ # Hub descriptor
139
+ :DT_HUB, 0x29,
140
+ # SuperSpeed Hub descriptor
141
+ :DT_SUPERSPEED_HUB, 0x2a,
142
+ # SuperSpeed Endpoint Companion descriptor
143
+ :DT_SS_ENDPOINT_COMPANION, 0x30,
144
+ ]
145
+
146
+ RequestTypes = enum :libusb_request_type, [
147
+ :REQUEST_TYPE_STANDARD, (0x00 << 5),
148
+ :REQUEST_TYPE_CLASS, (0x01 << 5),
149
+ :REQUEST_TYPE_VENDOR, (0x02 << 5),
150
+ :REQUEST_TYPE_RESERVED, (0x03 << 5),
151
+ ]
152
+
153
+ RequestRecipients = enum :libusb_request_recipient, [
154
+ :RECIPIENT_DEVICE, 0x00,
155
+ :RECIPIENT_INTERFACE, 0x01,
156
+ :RECIPIENT_ENDPOINT, 0x02,
157
+ :RECIPIENT_OTHER, 0x03,
158
+ ]
159
+
160
+ IsoSyncTypes = enum :libusb_iso_sync_type, [
161
+ :ISO_SYNC_TYPE_NONE, 0,
162
+ :ISO_SYNC_TYPE_ASYNC, 1,
163
+ :ISO_SYNC_TYPE_ADAPTIVE, 2,
164
+ :ISO_SYNC_TYPE_SYNC, 3,
165
+ ]
166
+
167
+ # Speed codes. Indicates the speed at which the device is operating.
168
+ Speeds = enum :libusb_speed, [
169
+ # The OS doesn't report or know the device speed.
170
+ :SPEED_UNKNOWN, 0,
171
+ # The device is operating at low speed (1.5MBit/s).
172
+ :SPEED_LOW, 1,
173
+ # The device is operating at full speed (12MBit/s).
174
+ :SPEED_FULL, 2,
175
+ # The device is operating at high speed (480MBit/s).
176
+ :SPEED_HIGH, 3,
177
+ # The device is operating at super speed (5000MBit/s).
178
+ :SPEED_SUPER, 4,
179
+ # The device is operating at super speed plus (10000MBit/s).
180
+ # Available since libusb-1.0.22
181
+ :SPEED_SUPER_PLUS, 5,
182
+ ]
183
+
184
+ # Supported speeds (wSpeedSupported) bitfield. Indicates what
185
+ # speeds the device supports.
186
+ SupportedSpeeds = enum :libusb_supported_speed, [
187
+ # Low speed operation supported (1.5MBit/s).
188
+ :LOW_SPEED_OPERATION, 1,
189
+ # Full speed operation supported (12MBit/s).
190
+ :FULL_SPEED_OPERATION, 2,
191
+ # High speed operation supported (480MBit/s).
192
+ :HIGH_SPEED_OPERATION, 4,
193
+ # Superspeed operation supported (5000MBit/s).
194
+ :SUPER_SPEED_OPERATION, 8,
195
+ ]
196
+
197
+ Capabilities = enum :libusb_capability, [
198
+ :CAP_HAS_CAPABILITY, 0x0000,
199
+ # Hotplug support is available on this platform.
200
+ :CAP_HAS_HOTPLUG, 0x0001,
201
+ # The library can access HID devices without requiring user intervention.
202
+ # Note that before being able to actually access an HID device, you may
203
+ # still have to call additional libusb functions such as
204
+ # {DevHandle#detach_kernel_driver}.
205
+ :CAP_HAS_HID_ACCESS, 0x0100,
206
+ # The library supports detaching of the default USB driver, using
207
+ # {DevHandle#detach_kernel_driver}, if one is set by the OS kernel.
208
+ :CAP_SUPPORTS_DETACH_KERNEL_DRIVER, 0x0101,
209
+ ]
210
+
211
+ # Masks for the bits of the
212
+ # {Bos::Usb20Extension#bmAttributes} field
213
+ # of the USB 2.0 Extension descriptor.
214
+ Usb20ExtensionAttributes = enum :libusb_usb_2_0_extension_attributes, [
215
+ # Supports Link Power Management (LPM)
216
+ :BM_LPM_SUPPORT, 2,
217
+ ]
218
+
219
+ # Masks for the bits of the
220
+ # {Bos::SsUsbDeviceCapability#bmAttributes} field
221
+ # field of the SuperSpeed USB Device Capability descriptor.
222
+ SsUsbDeviceCapabilityAttributes = enum :libusb_ss_usb_device_capability_attributes, [
223
+ # Supports Latency Tolerance Messages (LTM)
224
+ :BM_LTM_SUPPORT, 2,
225
+ ]
226
+
227
+
228
+ # enum used in SsplusSublinkAttribute
229
+ SuperspeedplusSublinkAttributeSublinkType = enum :libusb_superspeedplus_sublink_attribute_sublink_type, [
230
+ :SSPLUS_ATTR_TYPE_SYM, 0,
231
+ :SSPLUS_ATTR_TYPE_ASYM, 1,
232
+ ]
233
+
234
+ # enum used in SsplusSublinkAttribute
235
+ SuperspeedplusSublinkAttributeSublinkDirection = enum :libusb_superspeedplus_sublink_attribute_sublink_direction, [
236
+ :SSPLUS_ATTR_DIR_RX, 0,
237
+ :SSPLUS_ATTR_DIR_TX, 1,
238
+ ]
239
+
240
+ # enum used in SsplusSublinkAttribute
241
+ #
242
+ # This field defines the base 10 exponent times 3, that shall be applied to the mantissa.
243
+ # * Bit = Bits per second
244
+ # * Kb = Kbps
245
+ # * Mb = Mbps
246
+ # * Gb = Gbps
247
+ SuperspeedplusSublinkAttributeExponent = enum :libusb_superspeedplus_sublink_attribute_exponent, [
248
+ :SSPLUS_ATTR_EXP_BPS, 0,
249
+ :SSPLUS_ATTR_EXP_KBS, 1,
250
+ :SSPLUS_ATTR_EXP_MBS, 2,
251
+ :SSPLUS_ATTR_EXP_GBS, 3,
252
+ ]
253
+
254
+ # enum used in SsplusSublinkAttribute
255
+ SuperspeedplusSublinkAttributeLinkProtocol = enum :libusb_superspeedplus_sublink_attribute_link_protocol, [
256
+ :SSPLUS_ATTR_PROT_SS, 0,
257
+ :SSPLUS_ATTR_PROT_SSPLUS, 1,
258
+ ]
259
+
260
+ # USB capability types
261
+ #
262
+ # @see Bos::DeviceCapability
263
+ BosTypes = enum :libusb_bos_type, [
264
+ :BT_WIRELESS_USB_DEVICE_CAPABILITY, 0x01, # Wireless USB device capability
265
+ :BT_USB_2_0_EXTENSION, 0x02, # USB 2.0 extensions
266
+ :BT_SS_USB_DEVICE_CAPABILITY, 0x03, # SuperSpeed USB device capability
267
+ :BT_CONTAINER_ID, 0x04, # Container ID type
268
+ :BT_PLATFORM_DESCRIPTOR, 0x05, # Platform descriptor
269
+ :BT_POWER_DELIVERY_CAPABILITY, 0x06, # Defines the various PD Capabilities of this device
270
+ :BT_BATTERY_INFO_CAPABILITY, 0x07, # Provides information on each battery supported by the device
271
+ :BT_PD_CONSUMER_PORT_CAPABILITY, 0x08, # The consumer characteristics of a port on the device
272
+ :BT_PD_PROVIDER_PORT_CAPABILITY, 0x09, # The provider characteristics of a port on the device
273
+ :BT_SUPERSPEED_PLUS_CAPABILITY, 0x0A, # Defines the set of SuperSpeed Plus USB specific device level capabilities
274
+ :BT_PRECISION_TIME_MEASUREMENT, 0x0B, # Precision Time Measurement (PTM) Capability Descriptor
275
+ :BT_Wireless_USB_Ext, 0x0C, # Defines the set of Wireless USB 1.1-specific device level capabilities
276
+ :BT_BILLBOARD, 0x0D, # Billboard capability
277
+ :BT_AUTHENTICATION, 0x0E, # Authentication Capability Descriptor
278
+ :BT_BILLBOARD_EX, 0x0F, # Billboard Ex capability
279
+ :BT_CONFIGURATION_SUMMARY, 0x10, # Summarizes configuration information for a function implemented by the device
280
+ :BT_FWStatus_Capability, 0x11, # Describes the capability to support FWStatus
281
+ ]
282
+
283
+ # Since libusb version 1.0.16.
284
+ #
285
+ # Hotplug events
286
+ HotplugEvents = enum :libusb_hotplug_event, [
287
+ # A device has been plugged in and is ready to use.
288
+ :HOTPLUG_EVENT_DEVICE_ARRIVED, 0x01,
289
+
290
+ # A device has left and is no longer available.
291
+ # It is the user's responsibility to call libusb_close on any handle associated with a disconnected device.
292
+ # It is safe to call libusb_get_device_descriptor on a device that has left.
293
+ :HOTPLUG_EVENT_DEVICE_LEFT, 0x02,
294
+ ]
295
+
296
+ # Since libusb version 1.0.16.
297
+ #
298
+ # Flags for hotplug events */
299
+ HotplugFlags = enum :libusb_hotplug_flag, [
300
+ # Arm the callback and fire it for all matching currently attached devices.
301
+ :HOTPLUG_ENUMERATE, 1,
302
+ ]
303
+
304
+ # The device string type.
305
+ DeviceStringType = enum :libusb_device_string_type, [
306
+ :DEVICE_STRING_MANUFACTURER,
307
+ :DEVICE_STRING_PRODUCT,
308
+ :DEVICE_STRING_SERIAL_NUMBER,
309
+ :DEVICE_STRING_COUNT # The total number of string types.
310
+ ]
311
+
312
+ # Log message levels.
313
+ #
314
+ # - :LOG_LEVEL_NONE (0) : no messages ever printed by the library (default)
315
+ # - :LOG_LEVEL_ERROR (1) : error messages are printed to stderr
316
+ # - :LOG_LEVEL_WARNING (2) : warning and error messages are printed to stderr
317
+ # - :LOG_LEVEL_INFO (3) : informational messages are printed to stderr
318
+ # - :LOG_LEVEL_DEBUG (4) : debug and informational messages are printed to stderr
319
+ LogLevels = enum :libusb_log_level, [
320
+ :LOG_LEVEL_NONE, 0,
321
+ :LOG_LEVEL_ERROR, 1,
322
+ :LOG_LEVEL_WARNING, 2,
323
+ :LOG_LEVEL_INFO, 3,
324
+ :LOG_LEVEL_DEBUG, 4,
325
+ ]
326
+
327
+ # /** \ingroup libusb_lib
328
+ # * Log callback mode.
329
+ # *
330
+ # * Since version 1.0.23, \ref LIBUSB_API_VERSION >= 0x01000107
331
+ # *
332
+ # * \see libusb_set_log_cb()
333
+ # */
334
+ # enum libusb_log_cb_mode {
335
+ # /** Callback function handling all log messages. */
336
+ # LIBUSB_LOG_CB_GLOBAL = (1 << 0),
337
+ #
338
+ # /** Callback function handling context related log messages. */
339
+ # LIBUSB_LOG_CB_CONTEXT = (1 << 1)
340
+ # };
341
+ LogCbMode = enum :libusb_log_cb_mode, [
342
+ :LOG_CB_GLOBAL, (1 << 0),
343
+ :LOG_CB_CONTEXT, (1 << 1),
344
+ ]
345
+
346
+ # Available option values for {Context#set_option}.
347
+ Options = enum :libusb_option, [
348
+ # Set the log message verbosity.
349
+ #
350
+ # The default level is :LOG_LEVEL_NONE, which means no messages are ever
351
+ # printed. If you choose to increase the message verbosity level, ensure
352
+ # that your application does not close the stderr file descriptor.
353
+ #
354
+ # You are advised to use level :LOG_LEVEL_WARNING. libusb is conservative
355
+ # with its message logging and most of the time, will only log messages that
356
+ # explain error conditions and other oddities. This will help you debug
357
+ # your software.
358
+ #
359
+ # If the +LIBUSB_DEBUG+ environment variable was set when libusb was
360
+ # initialized, this function does nothing: the message verbosity is fixed
361
+ # to the value in the environment variable.
362
+ #
363
+ # If libusb was compiled without any message logging, this function does
364
+ # nothing: you'll never get any messages.
365
+ #
366
+ # If libusb was compiled with verbose debug message logging, this function
367
+ # does nothing: you'll always get messages from all levels.
368
+ :OPTION_LOG_LEVEL, 0,
369
+
370
+ # Use the UsbDk backend for a specific context, if available.
371
+ #
372
+ # This option should be set immediately after calling {Context.new}, otherwise
373
+ # unspecified behavior may occur.
374
+ #
375
+ # Only valid on Windows.
376
+ #
377
+ # Available since libusb-1.0.22.
378
+ :OPTION_USE_USBDK, 1,
379
+
380
+ # Do not scan for devices
381
+ #
382
+ # With this option set, libusb will skip scanning devices in
383
+ # libusb_init_context().
384
+ #
385
+ # Hotplug functionality will also be deactivated.
386
+ #
387
+ # The option is useful in combination with libusb_wrap_sys_device(),
388
+ # which can access a device directly without prior device scanning.
389
+ #
390
+ # This is typically needed on Android, where access to USB devices
391
+ # is limited.
392
+ #
393
+ # Only valid on Linux. Ignored on all other platforms.
394
+ :OPTION_NO_DEVICE_DISCOVERY, 2,
395
+
396
+ # Set the context log callback functon.
397
+ #
398
+ # Set the log callback function either on a context or globally.
399
+ # This option must be provided a Proc argument or +nil+ to remove the callback.
400
+ # Using this option with a +nil+ context is equivalent to calling libusb_set_log_cb with mode :LOG_CB_GLOBAL.
401
+ # Using it with a non- +nil+ context is equivalent to calling libusb_set_log_cb with mode :LOG_CB_CONTEXT.
402
+ :OPTION_LOG_CB, 3,
403
+
404
+ :OPTION_MAX, 4,
405
+ ]
406
+
407
+ typedef :pointer, :libusb_context
408
+ typedef :pointer, :libusb_device
409
+ typedef :pointer, :libusb_device_handle
410
+ typedef :pointer, :libusb_transfer
411
+ typedef :int, :libusb_hotplug_callback_handle
412
+
413
+ # /** \ingroup libusb_lib
414
+ # * Callback function for handling log messages.
415
+ # * \param ctx the context which is related to the log message, or +nil+ if it
416
+ # * is a global log message
417
+ # * \param level the log level, see \ref libusb_log_level for a description
418
+ # * \param str the log message
419
+ # *
420
+ # * Since version 1.0.23, \ref LIBUSB_API_VERSION >= 0x01000107
421
+ # *
422
+ # * \see libusb_set_log_cb()
423
+ # */
424
+ # typedef void (LIBUSB_CALL *libusb_log_cb)(libusb_context *ctx,
425
+ # enum libusb_log_level level, const char *str);
426
+ callback :libusb_log_cb, [:libusb_context, :libusb_log_level, :string], :void
427
+
428
+ # @private
429
+ # /** \ingroup libusb_lib
430
+ # * Structure used for setting options through \ref libusb_init_context.
431
+ # *
432
+ # */
433
+ # struct libusb_init_option {
434
+ # /** Which option to set */
435
+ # enum libusb_option option;
436
+ # /** An integer value used by the option (if applicable). */
437
+ # union {
438
+ # int64_t ival;
439
+ # libusb_log_cb log_cbval;
440
+ # } value;
441
+ # };
442
+ class InitOptionValue < FFI::Union
443
+ layout :ival, :int64_t,
444
+ :log_cbval, :libusb_log_cb
445
+ end
446
+ class InitOption < FFI::Struct
447
+ layout :option, :libusb_option,
448
+ :value, InitOptionValue
449
+ end
450
+
451
+ class << self
452
+ private def try_attach_function(method, *args, **kwargs)
453
+ if ffi_libraries.find{|lib| lib.find_function(method) }
454
+ attach_function method, *args, **kwargs
455
+ end
456
+ end
457
+ end
458
+
459
+ try_attach_function 'libusb_get_version', [], :pointer
460
+
461
+ attach_function 'libusb_init', [ :pointer ], :int
462
+ # int LIBUSB_CALL libusb_init_context(libusb_context **ctx, const struct libusb_init_option options[], int num_options);
463
+ try_attach_function 'libusb_init_context', [:pointer, :pointer, :int], :int
464
+ # may be deprecated in the future in favor of libusb_init_context()+libusb_set_option()
465
+ # void LIBUSB_CALL libusb_set_log_cb(libusb_context *ctx, libusb_log_cb cb, int mode);
466
+ try_attach_function 'libusb_set_log_cb', [:libusb_context, :libusb_log_cb, :int], :void
467
+ attach_function 'libusb_exit', [ :pointer ], :void
468
+ attach_function 'libusb_set_debug', [:pointer, :libusb_log_level], :void
469
+ try_attach_function 'libusb_set_option', [:libusb_context, :libusb_option, :varargs], :int
470
+ try_attach_function 'libusb_has_capability', [:libusb_capability], :int
471
+
472
+ attach_function 'libusb_get_device_list', [:pointer, :pointer], :ssize_t
473
+ attach_function 'libusb_free_device_list', [:pointer, :int], :void
474
+ attach_function 'libusb_ref_device', [:pointer], :pointer
475
+ attach_function 'libusb_unref_device', [:pointer], :void
476
+
477
+ attach_function 'libusb_get_device_descriptor', [:pointer, :pointer], :int
478
+ attach_function 'libusb_get_active_config_descriptor', [:pointer, :pointer], :int
479
+ attach_function 'libusb_get_config_descriptor', [:pointer, :uint8, :pointer], :int
480
+ attach_function 'libusb_get_config_descriptor_by_value', [:pointer, :uint8, :pointer], :int
481
+ attach_function 'libusb_free_config_descriptor', [:pointer], :void
482
+ attach_function 'libusb_get_bus_number', [:pointer], :uint8
483
+ try_attach_function 'libusb_get_port_number', [:pointer], :uint8
484
+ try_attach_function 'libusb_get_parent', [:pointer], :pointer
485
+ try_attach_function 'libusb_get_port_path', [:pointer, :pointer, :pointer, :uint8], :uint8
486
+ try_attach_function 'libusb_get_port_numbers', [:pointer, :pointer, :uint8], :uint8
487
+ attach_function 'libusb_get_device_address', [:pointer], :uint8
488
+ try_attach_function 'libusb_get_device_speed', [:pointer], :libusb_speed
489
+ attach_function 'libusb_get_max_packet_size', [:pointer, :uint8], :int
490
+ attach_function 'libusb_get_max_iso_packet_size', [:pointer, :uint8], :int
491
+ # int API_EXPORTED libusb_get_max_alt_packet_size(libusb_device *dev, int interface_number, int alternate_setting, unsigned char endpoint)
492
+ try_attach_function 'libusb_get_max_alt_packet_size', [:pointer, :int, :int, :uchar], :int
493
+
494
+ try_attach_function 'libusb_get_ss_endpoint_companion_descriptor', [:pointer, :pointer, :pointer], :int
495
+ try_attach_function 'libusb_free_ss_endpoint_companion_descriptor', [:pointer], :void
496
+
497
+ try_attach_function 'libusb_get_bos_descriptor', [:libusb_device_handle, :pointer], :int, blocking: true
498
+ try_attach_function 'libusb_free_bos_descriptor', [:pointer], :void
499
+ try_attach_function 'libusb_get_usb_2_0_extension_descriptor', [:libusb_context, :pointer, :pointer], :int
500
+ try_attach_function 'libusb_free_usb_2_0_extension_descriptor', [:pointer], :void
501
+ try_attach_function 'libusb_get_ss_usb_device_capability_descriptor', [:libusb_context, :pointer, :pointer], :int
502
+ try_attach_function 'libusb_free_ss_usb_device_capability_descriptor', [:pointer], :void
503
+ try_attach_function 'libusb_get_ssplus_usb_device_capability_descriptor', [:libusb_context, :pointer, :pointer], :int
504
+ try_attach_function 'libusb_free_ssplus_usb_device_capability_descriptor', [:pointer], :void
505
+ try_attach_function 'libusb_get_container_id_descriptor', [:libusb_context, :pointer, :pointer], :int
506
+ try_attach_function 'libusb_free_container_id_descriptor', [:pointer], :void
507
+ try_attach_function 'libusb_get_platform_descriptor', [:libusb_context, :pointer, :pointer], :int
508
+ try_attach_function 'libusb_free_platform_descriptor', [:pointer], :void
509
+
510
+ # int LIBUSB_CALL libusb_wrap_sys_device(libusb_context *ctx, intptr_t sys_dev, libusb_device_handle **dev_handle);
511
+ try_attach_function 'libusb_wrap_sys_device', [:libusb_context, :intptr_t, :pointer], :int
512
+ attach_function 'libusb_open', [:pointer, :pointer], :int
513
+ attach_function 'libusb_close', [:pointer], :void
514
+ attach_function 'libusb_get_device', [:libusb_device_handle], :pointer
515
+
516
+ attach_function 'libusb_set_configuration', [:libusb_device_handle, :int], :int, blocking: true
517
+ attach_function 'libusb_claim_interface', [:libusb_device_handle, :int], :int
518
+ attach_function 'libusb_release_interface', [:libusb_device_handle, :int], :int, blocking: true
519
+
520
+ attach_function 'libusb_open_device_with_vid_pid', [:pointer, :int, :int], :pointer
521
+
522
+ attach_function 'libusb_set_interface_alt_setting', [:libusb_device_handle, :int, :int], :int, blocking: true
523
+ attach_function 'libusb_clear_halt', [:libusb_device_handle, :int], :int, blocking: true
524
+ attach_function 'libusb_reset_device', [:libusb_device_handle], :int, blocking: true
525
+ try_attach_function 'libusb_alloc_streams', [:libusb_device_handle, :uint32, :pointer, :int], :int
526
+ try_attach_function 'libusb_free_streams', [:libusb_device_handle, :pointer, :int], :int
527
+ try_attach_function 'libusb_dev_mem_alloc', [:libusb_device_handle, :size_t], :pointer
528
+ try_attach_function 'libusb_dev_mem_free', [:libusb_device_handle, :pointer, :size_t], :int
529
+
530
+ attach_function 'libusb_kernel_driver_active', [:libusb_device_handle, :int], :int
531
+ attach_function 'libusb_detach_kernel_driver', [:libusb_device_handle, :int], :int
532
+ attach_function 'libusb_attach_kernel_driver', [:libusb_device_handle, :int], :int
533
+ try_attach_function 'libusb_set_auto_detach_kernel_driver', [:libusb_device_handle, :int], :int
534
+
535
+ attach_function 'libusb_get_string_descriptor_ascii', [:pointer, :uint8, :pointer, :int], :int
536
+
537
+ attach_function 'libusb_alloc_transfer', [:int], :pointer
538
+ attach_function 'libusb_submit_transfer', [:pointer], :int
539
+ attach_function 'libusb_cancel_transfer', [:pointer], :int
540
+ attach_function 'libusb_free_transfer', [:pointer], :void
541
+ try_attach_function 'libusb_transfer_set_stream_id', [:libusb_transfer, :uint32], :void
542
+ try_attach_function 'libusb_transfer_get_stream_id', [:libusb_transfer], :uint32
543
+
544
+ attach_function 'libusb_handle_events', [:libusb_context], :int, blocking: true
545
+ try_attach_function 'libusb_handle_events_completed', [:libusb_context, :pointer], :int, blocking: true
546
+ attach_function 'libusb_handle_events_timeout', [:libusb_context, :pointer], :int, blocking: true
547
+ try_attach_function 'libusb_handle_events_timeout_completed', [:libusb_context, :pointer, :pointer], :int, blocking: true
548
+ try_attach_function 'libusb_interrupt_event_handler', [:libusb_context], :void
549
+
550
+ callback :libusb_pollfd_added_cb, [:int, :short, :pointer], :void
551
+ callback :libusb_pollfd_removed_cb, [:int, :pointer], :void
552
+
553
+ attach_function 'libusb_get_pollfds', [:libusb_context], :pointer
554
+ attach_function 'libusb_get_next_timeout', [:libusb_context, :pointer], :int
555
+ attach_function 'libusb_set_pollfd_notifiers', [:libusb_context, :libusb_pollfd_added_cb, :libusb_pollfd_removed_cb, :pointer], :void
556
+ try_attach_function 'libusb_free_pollfds', [:pointer], :void
557
+
558
+ callback :libusb_transfer_cb_fn, [:pointer], :void
559
+
560
+ callback :libusb_hotplug_callback_fn, [:libusb_context, :libusb_device, :libusb_hotplug_event, :pointer], :int
561
+ try_attach_function 'libusb_hotplug_register_callback', [
562
+ :libusb_context, :libusb_hotplug_event, :libusb_hotplug_flag,
563
+ :int, :int, :int, :libusb_hotplug_callback_fn,
564
+ :pointer, :pointer], :int
565
+ try_attach_function 'libusb_hotplug_deregister_callback', [:libusb_context, :libusb_hotplug_callback_handle], :void
566
+
567
+ # Retrieve a device string without needing to open the device.
568
+ #
569
+ # Since version v1.0.30 \ref LIBUSB_API_VERSION >= 0x0100010C
570
+ #
571
+ # \param dev the target device
572
+ # \param string_type the string type to retrieve
573
+ # \param data the data buffer for the UTF-8 encoded string.
574
+ # \param length the size of the data buffer in bytes.
575
+ # USB string descriptors cannot be longer than
576
+ # LIBUSB_DEVICE_STRING_BYTES_MAX.
577
+ # \returns a negative error code or
578
+ # the actual string length in bytes including the null terminator.
579
+ # \see libusb_get_string_descriptor()
580
+ # \see libusb_get_string_descriptor_ascii()
581
+ #
582
+ # This function works when the device is still closed since it relies
583
+ # on the operating system to provide the string. The operating system
584
+ # normally reads and caches the common string descriptors during
585
+ # USB enumeration.
586
+ #
587
+ # Since the USB string descriptor could be processed by the OS,
588
+ # this function returns a UTF-8 encoded string.
589
+ #
590
+ # The string will be returned untranslated or in the default OS language
591
+ # when supported by the OS and USB device.
592
+ #
593
+ # One way to call this function is using a buffer on the stack:
594
+ #
595
+ # char buffer[LIBUSB_DEVICE_STRING_BYTES_MAX];
596
+ # int ret = libusb_get_device_string(dev, LIBUSB_DEVICE_STRING_SERIAL_NUMBER, buffer, sizeof(buffer));
597
+ # if (ret < 0) {
598
+ # // handle error
599
+ # }
600
+ #
601
+ # This function is commonly used to get the serial number to allow
602
+ # for device selection before opening the selected device.
603
+ try_attach_function 'libusb_get_device_string', [:libusb_device, DeviceStringType, :pointer, :int], :int
604
+
605
+ try_attach_function 'libusb_get_session_data', [:libusb_device], :ulong
606
+
607
+ class IsoPacketDescriptor < FFI::Struct
608
+ layout :length, :uint,
609
+ :actual_length, :uint,
610
+ :status, :libusb_transfer_status
611
+ end
612
+
613
+ # Setup packet for control transfers.
614
+ class ControlSetup < FFI::Struct
615
+ layout :bmRequestType, :uint8,
616
+ :bRequest, :uint8,
617
+ :wValue, :uint16,
618
+ :wIndex, :uint16,
619
+ :wLength, :uint16
620
+ end
621
+
622
+ class Transfer < FFI::Struct
623
+ layout :dev_handle, :libusb_device_handle,
624
+ :flags, :uint8,
625
+ :endpoint, :uchar,
626
+ :type, :uchar,
627
+ :timeout, :uint,
628
+ :status, :libusb_transfer_status,
629
+ :length, :int,
630
+ :actual_length, :int,
631
+ :callback, :libusb_transfer_cb_fn,
632
+ :user_data, :pointer,
633
+ :buffer, :pointer,
634
+ :num_iso_packets, :int
635
+
636
+ def initialize(*)
637
+ super
638
+
639
+ ptr = pointer
640
+ # @private
641
+ # called by GC
642
+ def ptr.free_struct(id)
643
+ Call.libusb_free_transfer(self)
644
+ return unless @ctx
645
+ @ctx.unref_context
646
+ end
647
+ # The ctx pointer is not yet assigned.
648
+ # It happens at LIBUSB::Transfer#dev_handle= later on.
649
+ ptr.instance_variable_set(:@ctx, nil)
650
+ ObjectSpace.define_finalizer(self, ptr.method(:free_struct))
651
+ end
652
+ end
653
+
654
+ class DeviceDescriptor < FFI::Struct
655
+ include Comparable
656
+
657
+ layout :bLength, :uint8,
658
+ :bDescriptorType, :uint8,
659
+ :bcdUSB, :uint16,
660
+ :bDeviceClass, :uint8,
661
+ :bDeviceSubClass, :uint8,
662
+ :bDeviceProtocol, :uint8,
663
+ :bMaxPacketSize0, :uint8,
664
+ :idVendor, :uint16,
665
+ :idProduct, :uint16,
666
+ :bcdDevice, :uint16,
667
+ :iManufacturer, :uint8,
668
+ :iProduct, :uint8,
669
+ :iSerialNumber, :uint8,
670
+ :bNumConfigurations, :uint8
671
+ end
672
+
673
+ class Timeval < FFI::Struct
674
+ layout :tv_sec, :long,
675
+ :tv_usec, :long
676
+
677
+ # set timeval to the number of milliseconds
678
+ # @param [Fixnum] value
679
+ def in_ms=(value)
680
+ self[:tv_sec], self[:tv_usec] = (value*1000).divmod(1000000)
681
+ end
682
+
683
+ # get the number of milliseconds in timeval
684
+ # @return [Fixnum]
685
+ def in_ms
686
+ self[:tv_sec]*1000 + self[:tv_usec]/1000
687
+ end
688
+
689
+ # set timeval to the number of seconds
690
+ # @param [Numeric] value
691
+ def in_s=(value)
692
+ self[:tv_sec], self[:tv_usec] = (value*1000000).divmod(1000000)
693
+ end
694
+
695
+ # get the number of seconds in timeval
696
+ # @return [Float]
697
+ def in_s
698
+ self[:tv_sec] + self[:tv_usec]/1000000.0
699
+ end
700
+ end
701
+
702
+ class Pollfd < FFI::Struct
703
+ layout :fd, :int,
704
+ :events, :short
705
+ end
706
+ end
707
+ end