libusb 0.3.4-x64-mingw32 → 0.4.0-x64-mingw32

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8574ced09a4724959aa505bfdabb5954b8fcd6dc
4
- data.tar.gz: fbc1e8fad6243fc3c6f2e6e5ee77e46d2df768c7
3
+ metadata.gz: 08ebef43ef23b38d7853e498df47564e821a4106
4
+ data.tar.gz: 72aa8693ac0df18afbf7b6e287c7e695e1e581db
5
5
  SHA512:
6
- metadata.gz: 0da496f019922da5c226c3e49921b0d301fbcafd00042bb82847f5c01a1d63007050a6ebfb74a5f61dd75145ee2ae4220313c7fc53a5b0bcffff10c6cda28079
7
- data.tar.gz: 08f447cb62b881f9dfc1f917a98bc748d242842037320f6779ce73adaad69178286f10d0e4ce5263bd99ff7d90289da08aeb70223f7fc181232676dee4fa5c01
6
+ metadata.gz: c743324f0ce2a2894f77f8079c77d615f32bde11595fab3b09b61bdcf53ab5ab3041d3b29903af8bff6acfce08e9690359ab424b25767e5feaac9baf58c6ea51
7
+ data.tar.gz: 88153b441df6901712c0094e3b72b1cef99d18a8e928394a321b18206769a10a09d1bbdb8efaa3ebb45473d619b1b2078112ce346ec814655934ce0b04b9e17b
@@ -0,0 +1,2 @@
1
+ �g&�v��J��6C��ˢ���h�#�î��<�B��(0�D��gV�ܠ�/R�}�p=��I>�"��˙���)Q�6��
2
+ ����5���i?���P����/�����r����<D���t����z7�iY�s�J��ēF
Binary file
data/Gemfile CHANGED
@@ -5,6 +5,7 @@ gemspec
5
5
 
6
6
  group :test do
7
7
  gem 'eventmachine'
8
+ gem 'minitest'
8
9
  end
9
10
 
10
11
  platforms :rbx do
data/History.md CHANGED
@@ -1,3 +1,13 @@
1
+ 0.4.0 / 2013-11-20
2
+ ------------------
3
+ * Add support for device hotplug notifications.
4
+ * Update to libusbx-1.0.17.
5
+ * Add DevHandle#auto_detach_kernel_driver= of libusb-1.0.16.
6
+ * Add new capabilities introduced with libusb-1.0.16.
7
+ * Offer #has_capability? for libusb versions older than 1.0.9.
8
+ * Add new method port_numbers with alias to port_path.
9
+ * Use libusb_get_port_numbers preferred to now deprecated libusb_get_port_path.
10
+
1
11
  0.3.4 / 2013-04-05
2
12
  ------------------
3
13
  * Avoid closing of pollfds by the Ruby GC when used as IO object.
data/README.md CHANGED
@@ -58,36 +58,49 @@ maximum packet size.
58
58
  Prerequisites
59
59
  -------------
60
60
 
61
- * Linux, MacOSX or Windows system with Ruby MRI 1.8.7/1.9.x, JRuby or recent version of Rubinius
62
- * [libusb](http://libusb.org) or [libusbx](http://libusbx.org) library version 1.0.8+ :
61
+ * Linux, MacOSX or Windows system with Ruby MRI 1.8.7/1.9/2.0, JRuby or recent version of Rubinius
62
+ * Optionally: [libusb](http://libusb.org) or [libusbx](http://libusbx.org) library version 1.0.8+ :
63
63
  * Debian or Ubuntu:
64
64
 
65
65
  ```
66
66
  $ sudo apt-get install libusb-1.0-0-dev
67
67
  ```
68
- * OS-X: install with homebrew:
68
+ * OS-X: install with homebrew (note the `x`):
69
69
 
70
70
  ```
71
- $ brew install libusb
71
+ $ brew install libusbx
72
72
  ```
73
73
  or macports:
74
74
 
75
75
  ```
76
76
  $ port install libusb
77
77
  ```
78
- * Windows: libusb.gem already comes with a precompiled `libusb.dll`, but you need to install a device driver (see below)
79
-
78
+ * Windows: libusb.gem already comes with a precompiled `libusb.dll`, but you need to install a device driver (see [below](#usage-on-windows))
80
79
 
81
80
  Install
82
81
  -------
83
82
 
84
83
  $ gem install libusb
85
84
 
85
+ While ```gem install``` the system is checked for a usable libusb(x) library installation.
86
+ If none could be found, a bundled libusbx version is built and used, instead.
87
+
86
88
  Latest code can be used in this way:
87
89
 
88
90
  $ git clone git://github.com/larskanis/libusb.git
89
91
  $ rake install_gem
90
92
 
93
+ Device hotplug support
94
+ ----------------------
95
+
96
+ Support for device hotplugging can be used, if ```LIBUSB.has_capability?(:CAP_HAS_HOTPLUG)``` returns ```true```.
97
+ This requires libusb(x)-1.0.16 or newer on Linux or OS-X. Windows support is [still on the way](https://github.com/libusbx/libusbx/issues/9).
98
+
99
+ A hotplug event handler can be registered with {LIBUSB::Context#on_hotplug_event}.
100
+ You then need to call {LIBUSB::Context#handle_events} in order to receive any events.
101
+ This can be done as blocking calls (possibly in it's own thread) or by using {LIBUSB::Context#pollfds} to
102
+ detect any events to handle.
103
+
91
104
 
92
105
  Usage on Windows
93
106
  ----------------
data/Rakefile CHANGED
@@ -35,7 +35,7 @@ STATIC_SOURCESDIR = COMPILE_HOME + 'sources'
35
35
  # LIBUSB_TARBALL = STATIC_SOURCESDIR + "libusb-#{LIBUSB_VERSION}.tar.bz2"
36
36
 
37
37
  # Fetch tarball from libusbx
38
- LIBUSB_VERSION = ENV['LIBUSB_VERSION'] || '1.0.14'
38
+ LIBUSB_VERSION = ENV['LIBUSB_VERSION'] || '1.0.17'
39
39
  LIBUSB_SOURCE_URI = URI( "http://downloads.sourceforge.net/project/libusbx/releases/#{LIBUSB_VERSION[/^\d+\.\d+\.\d+/]}/source/libusbx-#{LIBUSB_VERSION}.tar.bz2" )
40
40
  LIBUSB_TARBALL = STATIC_SOURCESDIR + File.basename( LIBUSB_SOURCE_URI.path )
41
41
 
@@ -47,5 +47,9 @@ module LIBUSB
47
47
  r = Call.libusb_has_capability(capability)
48
48
  return r != 0
49
49
  end
50
+ else
51
+ def self.has_capability?(capability)
52
+ false
53
+ end
50
54
  end
51
55
  end
@@ -149,11 +149,44 @@ module LIBUSB
149
149
  ]
150
150
 
151
151
  Capabilities = enum :libusb_capability, [
152
- :CAP_HAS_CAPABILITY, 0,
152
+ :CAP_HAS_CAPABILITY, 0x0000,
153
+ # Hotplug support is available on this platform.
154
+ :CAP_HAS_HOTPLUG, 0x0001,
155
+ # The library can access HID devices without requiring user intervention.
156
+ # Note that before being able to actually access an HID device, you may
157
+ # still have to call additional libusb functions such as
158
+ # {DevHandle#detach_kernel_driver}.
159
+ :CAP_HAS_HID_ACCESS, 0x0100,
160
+ # The library supports detaching of the default USB driver, using
161
+ # {DevHandle#detach_kernel_driver}, if one is set by the OS kernel.
162
+ :CAP_SUPPORTS_DETACH_KERNEL_DRIVER, 0x0101,
163
+ ]
164
+
165
+ # Since libusb version 1.0.16.
166
+ #
167
+ # Hotplug events
168
+ HotplugEvents = enum :libusb_hotplug_event, [
169
+ # A device has been plugged in and is ready to use.
170
+ :HOTPLUG_EVENT_DEVICE_ARRIVED, 0x01,
171
+
172
+ # A device has left and is no longer available.
173
+ # It is the user's responsibility to call libusb_close on any handle associated with a disconnected device.
174
+ # It is safe to call libusb_get_device_descriptor on a device that has left.
175
+ :HOTPLUG_EVENT_DEVICE_LEFT, 0x02,
176
+ ]
177
+
178
+ # Since libusb version 1.0.16.
179
+ #
180
+ # Flags for hotplug events */
181
+ HotplugFlags = enum :libusb_hotplug_flag, [
182
+ # Arm the callback and fire it for all matching currently attached devices.
183
+ :HOTPLUG_ENUMERATE, 1,
153
184
  ]
154
185
 
155
186
  typedef :pointer, :libusb_context
187
+ typedef :pointer, :libusb_device
156
188
  typedef :pointer, :libusb_device_handle
189
+ typedef :int, :libusb_hotplug_callback_handle
157
190
 
158
191
  def self.try_attach_function(method, *args)
159
192
  if ffi_libraries.find{|lib| lib.find_function(method) }
@@ -182,6 +215,7 @@ module LIBUSB
182
215
  try_attach_function 'libusb_get_port_number', [:pointer], :uint8
183
216
  try_attach_function 'libusb_get_parent', [:pointer], :pointer
184
217
  try_attach_function 'libusb_get_port_path', [:pointer, :pointer, :pointer, :uint8], :uint8
218
+ try_attach_function 'libusb_get_port_numbers', [:pointer, :pointer, :uint8], :uint8
185
219
  attach_function 'libusb_get_device_address', [:pointer], :uint8
186
220
  try_attach_function 'libusb_get_device_speed', [:pointer], :libusb_speed
187
221
  attach_function 'libusb_get_max_packet_size', [:pointer, :uint8], :int
@@ -204,6 +238,7 @@ module LIBUSB
204
238
  attach_function 'libusb_kernel_driver_active', [:libusb_device_handle, :int], :int
205
239
  attach_function 'libusb_detach_kernel_driver', [:libusb_device_handle, :int], :int
206
240
  attach_function 'libusb_attach_kernel_driver', [:libusb_device_handle, :int], :int
241
+ try_attach_function 'libusb_set_auto_detach_kernel_driver', [:libusb_device_handle, :int], :int
207
242
 
208
243
  attach_function 'libusb_get_string_descriptor_ascii', [:pointer, :uint8, :pointer, :int], :int
209
244
 
@@ -226,6 +261,13 @@ module LIBUSB
226
261
 
227
262
  callback :libusb_transfer_cb_fn, [:pointer], :void
228
263
 
264
+ callback :libusb_hotplug_callback_fn, [:libusb_context, :libusb_device, :libusb_hotplug_event, :pointer], :int
265
+ try_attach_function 'libusb_hotplug_register_callback', [
266
+ :libusb_context, :libusb_hotplug_event, :libusb_hotplug_flag,
267
+ :int, :int, :int, :libusb_hotplug_callback_fn,
268
+ :pointer, :pointer], :int
269
+ try_attach_function 'libusb_hotplug_deregister_callback', [:libusb_context, :libusb_hotplug_callback_handle], :void
270
+
229
271
  class IsoPacketDescriptor < FFI::Struct
230
272
  layout :length, :uint,
231
273
  :actual_length, :uint,
@@ -26,6 +26,8 @@ module LIBUSB
26
26
  Call::IsoSyncTypes.to_h.each{|k,v| const_set(k,v) }
27
27
  Call::Speeds.to_h.each{|k,v| const_set(k,v) }
28
28
  Call::Capabilities.to_h.each{|k,v| const_set(k,v) }
29
+ Call::HotplugEvents.to_h.each{|k,v| const_set(k,v) }
30
+ Call::HotplugFlags.to_h.each{|k,v| const_set(k,v) }
29
31
 
30
32
  # Base class of libusb errors
31
33
  class Error < RuntimeError
@@ -73,6 +75,9 @@ module LIBUSB
73
75
  POLLIN = 1
74
76
  POLLOUT = 4
75
77
 
78
+ # Wildcard matching for hotplug events.
79
+ HOTPLUG_MATCH_ANY = -1
80
+
76
81
 
77
82
  # http://www.usb.org/developers/defined_class
78
83
  # @private
@@ -70,6 +70,31 @@ module LIBUSB
70
70
  end
71
71
  end
72
72
 
73
+ class HotplugCallback < FFI::Struct
74
+ layout :handle, :int
75
+
76
+ attr_reader :context
77
+
78
+ # @private
79
+ def initialize(context, ctx, callbacks)
80
+ super()
81
+ @context = context
82
+ @ctx = ctx
83
+ @callbacks = callbacks
84
+ end
85
+
86
+ # Deregisters the hotplug callback.
87
+ #
88
+ # Deregister a callback from a {Context}. This function is safe to call from within
89
+ # a hotplug callback.
90
+ #
91
+ # Since libusb version 1.0.16.
92
+ def deregister
93
+ Call.libusb_hotplug_deregister_callback(@ctx, self[:handle])
94
+ @callbacks.delete(self[:handle])
95
+ end
96
+ end
97
+
73
98
 
74
99
  # Initialize libusb context.
75
100
  def initialize
@@ -79,6 +104,7 @@ module LIBUSB
79
104
  @ctx = m.read_pointer
80
105
  @on_pollfd_added = nil
81
106
  @on_pollfd_removed = nil
107
+ @hotplug_callbacks = {}
82
108
  end
83
109
 
84
110
  # Deinitialize libusb.
@@ -304,5 +330,79 @@ module LIBUSB
304
330
  end
305
331
  Call.libusb_set_pollfd_notifiers @ctx, @on_pollfd_added, @on_pollfd_removed, nil
306
332
  end
333
+
334
+ # Register a hotplug event notification.
335
+ #
336
+ # Register a callback with the {LIBUSB::Context}. The callback will fire
337
+ # when a matching event occurs on a matching device. The callback is armed
338
+ # until either it is deregistered with {HotplugCallback#deregister} or the
339
+ # supplied block returns +:finish+ to indicate it is finished processing events.
340
+ #
341
+ # If the flag {Call::HotplugFlags HOTPLUG_ENUMERATE} is passed the callback will be
342
+ # called with a {Call::HotplugEvents :HOTPLUG_EVENT_DEVICE_ARRIVED} for all devices
343
+ # already plugged into the machine. Note that libusb modifies its internal
344
+ # device list from a separate thread, while calling hotplug callbacks from
345
+ # {#handle_events}, so it is possible for a device to already be present
346
+ # on, or removed from, its internal device list, while the hotplug callbacks
347
+ # still need to be dispatched. This means that when using
348
+ # {Call::HotplugFlags HOTPLUG_ENUMERATE}, your callback may be called twice for the arrival
349
+ # of the same device, once from {#on_hotplug_event} and once
350
+ # from {#handle_events}; and/or your callback may be called for the
351
+ # removal of a device for which an arrived call was never made.
352
+ #
353
+ # Since libusb version 1.0.16.
354
+ #
355
+ # @param [Hash] args
356
+ # @option args [Fixnum,Symbol] :events bitwise or of events that will trigger this callback.
357
+ # Default is +LIBUSB::HOTPLUG_EVENT_DEVICE_ARRIVED|LIBUSB::HOTPLUG_EVENT_DEVICE_LEFT+ .
358
+ # See {Call::HotplugEvents HotplugEvents}
359
+ # @option args [Fixnum,Symbol] :flags hotplug callback flags. Default is 0. See {Call::HotplugFlags HotplugFlags}
360
+ # @option args [Fixnum] :vendor_id the vendor id to match. Default is {HOTPLUG_MATCH_ANY}.
361
+ # @option args [Fixnum] :product_id the product id to match. Default is {HOTPLUG_MATCH_ANY}.
362
+ # @option args [Fixnum] :dev_class the device class to match. Default is {HOTPLUG_MATCH_ANY}.
363
+ # @return [HotplugCallback] The handle to the registered callback.
364
+ #
365
+ # @yieldparam [Device] device the attached or removed {Device} is yielded to the block
366
+ # @yieldparam [Symbol] event a {Call::HotplugEvents HotplugEvents} symbol
367
+ # @yieldreturn [Symbol] +:finish+ to deregister the callback, +:repeat+ to receive additional events
368
+ # @raise [ArgumentError, LIBUSB::Error] in case of failure
369
+ def on_hotplug_event(args={}, &block)
370
+ events = args.delete(:events) || (HOTPLUG_EVENT_DEVICE_ARRIVED | HOTPLUG_EVENT_DEVICE_LEFT)
371
+ flags = args.delete(:flags) || 0
372
+ vendor_id = args.delete(:vendor_id) || HOTPLUG_MATCH_ANY
373
+ product_id = args.delete(:product_id) || HOTPLUG_MATCH_ANY
374
+ dev_class = args.delete(:dev_class) || HOTPLUG_MATCH_ANY
375
+ raise ArgumentError, "invalid params #{args.inspect}" unless args.empty?
376
+
377
+ handle = HotplugCallback.new self, @ctx, @hotplug_callbacks
378
+
379
+ block2 = proc do |ctx, pDevice, event, _user_data|
380
+ raise "internal error: unexpected context" unless @ctx==ctx
381
+ dev = Device.new @ctx, pDevice
382
+
383
+ blres = block.call(dev, event)
384
+
385
+ case blres
386
+ when :finish
387
+ 1
388
+ when :repeat
389
+ 0
390
+ else
391
+ raise ArgumentError, "hotplug event handler must return :finish or :repeat"
392
+ end
393
+ end
394
+
395
+ res = Call.libusb_hotplug_register_callback(@ctx,
396
+ events, flags,
397
+ vendor_id, product_id, dev_class,
398
+ block2, nil, handle)
399
+
400
+ LIBUSB.raise_error res, "in libusb_hotplug_register_callback" if res<0
401
+
402
+ # Avoid GC'ing of the block:
403
+ @hotplug_callbacks[handle[:handle]] = block2
404
+
405
+ return handle
406
+ end
307
407
  end
308
408
  end
@@ -225,6 +225,33 @@ module LIBUSB
225
225
  LIBUSB.raise_error res, "in libusb_attach_kernel_driver" if res!=0
226
226
  end
227
227
 
228
+ # @private
229
+ if Call.respond_to?(:libusb_set_auto_detach_kernel_driver)
230
+
231
+ # @method auto_detach_kernel_driver=
232
+ # Enable/disable libusb's automatic kernel driver detachment.
233
+ #
234
+ # When this is enabled libusb will automatically detach the kernel driver on an
235
+ # interface when claiming the interface, and attach it when releasing the
236
+ # interface.
237
+ #
238
+ # Automatic kernel driver detachment is disabled on newly opened device handles by
239
+ # default.
240
+ #
241
+ # On platforms which do not have CAP_SUPPORTS_DETACH_KERNEL_DRIVER this
242
+ # function will return ERROR_NOT_SUPPORTED, and libusb will continue as if
243
+ # this function was never called.
244
+ #
245
+ # Available since libusb-1.0.16.
246
+ #
247
+ # @param [Boolean] enable whether to enable or disable auto kernel driver detachment
248
+ #
249
+ # @see LIBUSB.has_capability?
250
+ def auto_detach_kernel_driver=(enable)
251
+ res = Call.libusb_set_auto_detach_kernel_driver(@pHandle, enable ? 1 : 0)
252
+ LIBUSB.raise_error res, "in libusb_set_auto_detach_kernel_driver" if res!=0
253
+ end
254
+ end
228
255
 
229
256
  # Perform a USB bulk transfer.
230
257
  #
@@ -95,7 +95,7 @@ module LIBUSB
95
95
  # Available since libusb-1.0.12.
96
96
  #
97
97
  # @return [Fixnum, nil] the port number (+nil+ if not available)
98
- # @see #port_path
98
+ # @see #port_numbers
99
99
  def port_number
100
100
  r = Call.libusb_get_port_number(@pDev)
101
101
  r==0 ? nil : r
@@ -105,7 +105,7 @@ module LIBUSB
105
105
  # Available since libusb-1.0.12.
106
106
  #
107
107
  # @return [Device, nil] the device parent or +nil+ if not available
108
- # @see #port_path
108
+ # @see #port_numbers
109
109
  def parent
110
110
  pppDevs = FFI::MemoryPointer.new :pointer
111
111
  Call.libusb_get_device_list(@context.instance_variable_get(:@ctx), pppDevs)
@@ -122,13 +122,19 @@ module LIBUSB
122
122
  # @return [Array<Fixnum>]
123
123
  # @see #parent
124
124
  # @see #port_number
125
- def port_path
125
+ def port_numbers
126
126
  # As per the USB 3.0 specs, the current maximum limit for the depth is 7.
127
127
  path_len = 7
128
128
  pPath = FFI::MemoryPointer.new :pointer, path_len
129
- l = Call.libusb_get_port_path(@context.instance_variable_get(:@ctx), @pDev, pPath, path_len)
129
+
130
+ l = if Call.respond_to?(:libusb_get_port_numbers)
131
+ Call.libusb_get_port_numbers(@pDev, pPath, path_len)
132
+ else
133
+ Call.libusb_get_port_path(@context.instance_variable_get(:@ctx), @pDev, pPath, path_len)
134
+ end
130
135
  pPath.read_array_of_uint8(l)
131
136
  end
137
+ alias port_path port_numbers
132
138
  end
133
139
 
134
140
  if Call.respond_to?(:libusb_get_device_speed)
@@ -15,5 +15,5 @@
15
15
 
16
16
  module LIBUSB
17
17
  # Library version of libusb for Ruby
18
- VERSION = "0.3.4"
18
+ VERSION = "0.4.0"
19
19
  end
@@ -13,10 +13,10 @@
13
13
  # You should have received a copy of the GNU Lesser General Public License
14
14
  # along with Libusb for Ruby. If not, see <http://www.gnu.org/licenses/>.
15
15
 
16
- require "test/unit"
16
+ require "minitest/autorun"
17
17
  require "libusb"
18
18
 
19
- class TestLibusbCapability < Test::Unit::TestCase
19
+ class TestLibusbCapability < Minitest::Test
20
20
  def test_version_parts
21
21
  assert LIBUSB.has_capability?(:CAP_HAS_CAPABILITY)
22
22
  end
@@ -13,10 +13,10 @@
13
13
  # You should have received a copy of the GNU Lesser General Public License
14
14
  # along with Libusb for Ruby. If not, see <http://www.gnu.org/licenses/>.
15
15
 
16
- require "test/unit"
16
+ require "minitest/autorun"
17
17
  require "libusb/compat"
18
18
 
19
- class TestLibusbCompat < Test::Unit::TestCase
19
+ class TestLibusbCompat < Minitest::Test
20
20
  include USB
21
21
 
22
22
  attr_accessor :usb
@@ -13,10 +13,10 @@
13
13
  # You should have received a copy of the GNU Lesser General Public License
14
14
  # along with Libusb for Ruby. If not, see <http://www.gnu.org/licenses/>.
15
15
 
16
- require "test/unit"
16
+ require "minitest/autorun"
17
17
  require "libusb/compat"
18
18
 
19
- class TestLibusbCompatMassStorage < Test::Unit::TestCase
19
+ class TestLibusbCompatMassStorage < Minitest::Test
20
20
  include USB
21
21
 
22
22
  attr_accessor :devh
@@ -13,10 +13,10 @@
13
13
  # You should have received a copy of the GNU Lesser General Public License
14
14
  # along with Libusb for Ruby. If not, see <http://www.gnu.org/licenses/>.
15
15
 
16
- require "test/unit"
16
+ require "minitest/autorun"
17
17
  require "libusb"
18
18
 
19
- class TestLibusbDescriptors < Test::Unit::TestCase
19
+ class TestLibusbDescriptors < Minitest::Test
20
20
  include LIBUSB
21
21
 
22
22
  attr_accessor :usb
@@ -165,6 +165,8 @@ class TestLibusbDescriptors < Test::Unit::TestCase
165
165
  assert_operator dev.bus_number, :>=, 0, "#{dev.inspect} should have a bus_number"
166
166
  assert_operator dev.device_address, :>=, 0, "#{dev.inspect} should have a device_address"
167
167
  assert_operator([:SPEED_UNKNOWN, :SPEED_LOW, :SPEED_FULL, :SPEED_HIGH, :SPEED_SUPER], :include?, dev.device_speed, "#{dev.inspect} should have a device_speed")
168
+ path = dev.port_numbers
169
+ assert_kind_of Array, path, "#{dev.inspect} should have port_numbers"
168
170
  path = dev.port_path
169
171
  assert_kind_of Array, path, "#{dev.inspect} should have a port_path"
170
172
  path.each do |port|
@@ -13,12 +13,12 @@
13
13
  # You should have received a copy of the GNU Lesser General Public License
14
14
  # along with Libusb for Ruby. If not, see <http://www.gnu.org/licenses/>.
15
15
 
16
- require "test/unit"
16
+ require "minitest/autorun"
17
17
  require "libusb"
18
18
  require "libusb/eventmachine"
19
19
  require "eventmachine"
20
20
 
21
- class TestLibusbEventMachine < Test::Unit::TestCase
21
+ class TestLibusbEventMachine < Minitest::Test
22
22
  include LIBUSB
23
23
  BOMS_GET_MAX_LUN = 0xFE
24
24
 
@@ -16,10 +16,10 @@
16
16
  # These tests should be started with valgrind to check for
17
17
  # invalid memmory access.
18
18
 
19
- require "test/unit"
19
+ require "minitest/autorun"
20
20
  require "libusb"
21
21
 
22
- class TestLibusbGc < Test::Unit::TestCase
22
+ class TestLibusbGc < Minitest::Test
23
23
  include LIBUSB
24
24
 
25
25
  def get_some_endpoint
@@ -0,0 +1,115 @@
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 "minitest/autorun"
17
+ require "libusb"
18
+
19
+ class TestLibusbHotplug < Minitest::Test
20
+ include LIBUSB
21
+
22
+ attr_reader :ctx
23
+
24
+ def setup
25
+ @ctx = Context.new
26
+ end
27
+
28
+ def teardown
29
+ @ctx.exit
30
+ end
31
+
32
+ def test_enumerate
33
+ devs = []
34
+ ctx.on_hotplug_event :flags => HOTPLUG_ENUMERATE do |dev, event|
35
+ devs << dev
36
+ assert_equal :HOTPLUG_EVENT_DEVICE_ARRIVED, event
37
+ :repeat
38
+ end
39
+ # Not really necessary, but just to be sure that the callback was called:
40
+ ctx.handle_events 0
41
+
42
+ assert_equal ctx.devices.sort, devs.sort
43
+ end
44
+
45
+ def test_enumerate_with_left
46
+ devs = []
47
+ ctx.on_hotplug_event :flags => HOTPLUG_ENUMERATE, :events => HOTPLUG_EVENT_DEVICE_LEFT do |dev, event|
48
+ devs << dev
49
+ assert_equal :HOTPLUG_EVENT_DEVICE_ARRIVED, event
50
+ :repeat
51
+ end
52
+ # Not really necessary, but just to be sure that the callback was called:
53
+ ctx.handle_events 0
54
+
55
+ assert_equal [], devs.sort, "Enumerate should not send any LEFT events"
56
+ end
57
+
58
+ def test_deregister
59
+ handle1 = ctx.on_hotplug_event{ assert false, "Callback should not be called" }
60
+ handle2 = ctx.on_hotplug_event{ assert false, "Callback should not be called" }
61
+ handle1.deregister
62
+ handle2.deregister
63
+ ctx.handle_events 0
64
+ end
65
+
66
+ def test_wrong_yieldreturn
67
+ ex = assert_raises(ArgumentError) do
68
+ ctx.on_hotplug_event :flags => :HOTPLUG_ENUMERATE do |dev, event|
69
+ end
70
+ end
71
+
72
+ assert_match(/:finish.*:repeat/, ex.to_s, "Should give a useful hint")
73
+ end
74
+
75
+ def test_context
76
+ handle = ctx.on_hotplug_event do |dev, event|
77
+ end
78
+ assert_equal ctx, handle.context, "The callback handle should have it's context"
79
+ end
80
+
81
+ def test_real_device_plugging_and_yieldreturn_and_gc_and_deregister
82
+ # This callback should be triggered once
83
+ devs = []
84
+ ctx.on_hotplug_event do |dev, event|
85
+ devs << [dev, event]
86
+ :finish
87
+ end
88
+
89
+ # This callback should be triggered twice
90
+ devs2 = []
91
+ ctx.on_hotplug_event do |dev, event|
92
+ devs2 << [dev, event]
93
+ puts format(" %p: %p", event, dev)
94
+ :repeat
95
+ end
96
+
97
+ # This callback should never be triggered
98
+ handle = ctx.on_hotplug_event{ assert false, "Deregistered callback should never be called" }
99
+
100
+ # GC shouldn't free any relevant callbacks or blocks
101
+ GC.start
102
+
103
+ print "\nPlease add and remove an USB device (2*5 sec): "
104
+ handle.deregister
105
+ ctx.handle_events 0
106
+ ctx.handle_events 5000
107
+ ctx.handle_events 5000
108
+
109
+ skip if devs.empty? && devs2.empty?
110
+ assert_equal 1, devs.length, "Should be deregistered after the first event"
111
+ assert_equal 2, devs2.length, "Should have received two events"
112
+ assert_operator devs2.map(&:last), :include?, :HOTPLUG_EVENT_DEVICE_ARRIVED, "Should have received ARRIVED"
113
+ assert_operator devs2.map(&:last), :include?, :HOTPLUG_EVENT_DEVICE_LEFT, "Should have received LEFT"
114
+ end
115
+ end
@@ -13,10 +13,10 @@
13
13
  # You should have received a copy of the GNU Lesser General Public License
14
14
  # along with Libusb for Ruby. If not, see <http://www.gnu.org/licenses/>.
15
15
 
16
- require "test/unit"
16
+ require "minitest/autorun"
17
17
  require "libusb"
18
18
 
19
- class TestLibusbIsoTransfer < Test::Unit::TestCase
19
+ class TestLibusbIsoTransfer < Minitest::Test
20
20
  include LIBUSB
21
21
 
22
22
  def setup
@@ -43,7 +43,7 @@ class TestLibusbIsoTransfer < Test::Unit::TestCase
43
43
  assert_equal 12, tr[7].length, "packet length should be set"
44
44
  assert_equal 13, tr[8].length, "packet length should be set"
45
45
 
46
- assert_raise(LIBUSB::ERROR_IO, "the randomly choosen device will probably not handle iso transfer") do
46
+ assert_raises(LIBUSB::ERROR_IO, "the randomly choosen device will probably not handle iso transfer") do
47
47
  tr.submit!
48
48
  end
49
49
  end
@@ -19,10 +19,10 @@
19
19
  # http://en.wikipedia.org/wiki/SCSI_command
20
20
  #
21
21
 
22
- require "test/unit"
22
+ require "minitest/autorun"
23
23
  require "libusb"
24
24
 
25
- class TestLibusbMassStorage < Test::Unit::TestCase
25
+ class TestLibusbMassStorage < Minitest::Test
26
26
  include LIBUSB
27
27
 
28
28
  class CSWError < RuntimeError; end
@@ -215,7 +215,7 @@ class TestLibusbMassStorage < Test::Unit::TestCase
215
215
  sleep 0.01
216
216
  end
217
217
  end
218
- assert_raise(LIBUSB::ERROR_TIMEOUT) do
218
+ assert_raises(LIBUSB::ERROR_TIMEOUT) do
219
219
  begin
220
220
  bulk_transfer(:endpoint=>endpoint_in, :dataIn=>123)
221
221
  rescue LIBUSB::ERROR_TIMEOUT => err
@@ -257,20 +257,10 @@ class TestLibusbMassStorage < Test::Unit::TestCase
257
257
  end
258
258
  end
259
259
 
260
- def test_attach_kernel_driver
261
- dev.release_interface(0)
262
- if RUBY_PLATFORM=~/linux/i
263
- dev.attach_kernel_driver(0)
264
- assert dev.kernel_driver_active?(0), "kernel driver should be active again"
265
- end
266
- dev.close
267
- @dev = nil
268
- end
269
-
270
260
  def test_wrong_argument
271
- assert_raise(ArgumentError){ dev.bulk_transfer(:endpoint=>endpoint_in, :dataOut=>"data") }
272
- assert_raise(ArgumentError){ dev.interrupt_transfer(:endpoint=>endpoint_in, :dataOut=>"data") }
273
- assert_raise(ArgumentError){ dev.control_transfer(
261
+ assert_raises(ArgumentError){ dev.bulk_transfer(:endpoint=>endpoint_in, :dataOut=>"data") }
262
+ assert_raises(ArgumentError){ dev.interrupt_transfer(:endpoint=>endpoint_in, :dataOut=>"data") }
263
+ assert_raises(ArgumentError){ dev.control_transfer(
274
264
  :bmRequestType=>ENDPOINT_OUT|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE,
275
265
  :bRequest=>BOMS_RESET,
276
266
  :wValue=>0, :wIndex=>0, :dataIn=>123) }
@@ -14,10 +14,10 @@
14
14
  # along with Libusb for Ruby. If not, see <http://www.gnu.org/licenses/>.
15
15
  #
16
16
 
17
- require "test/unit"
17
+ require "minitest/autorun"
18
18
  require "libusb"
19
19
 
20
- class TestLibusbMassStorage2 < Test::Unit::TestCase
20
+ class TestLibusbMassStorage2 < Minitest::Test
21
21
  include LIBUSB
22
22
 
23
23
  attr_accessor :usb
@@ -35,7 +35,6 @@ class TestLibusbMassStorage2 < Test::Unit::TestCase
35
35
  # Ensure kernel driver is detached
36
36
  device.open do |dev|
37
37
  if RUBY_PLATFORM=~/linux/i && dev.kernel_driver_active?(interface)
38
- assert dev.kernel_driver_active?(0), "DevHandle#kernel_driver_active? may be called with an Interface instance or a Fixnum"
39
38
  dev.detach_kernel_driver(interface)
40
39
  end
41
40
  end
@@ -70,4 +69,28 @@ class TestLibusbMassStorage2 < Test::Unit::TestCase
70
69
  end
71
70
  assert_equal 12345, res, "Block versions should pass through the result"
72
71
  end
72
+
73
+ def test_attach_kernel_driver
74
+ # Should work with both Fixnum and Interface parameter
75
+ [0, interface].each do |i|
76
+ device.open do |dev|
77
+ dev.attach_kernel_driver(i)
78
+ assert dev.kernel_driver_active?(i), "kernel driver should be active again"
79
+ dev.detach_kernel_driver(i)
80
+ refute dev.kernel_driver_active?(i), "kernel driver should be detached"
81
+ end
82
+ end
83
+ end
84
+
85
+ def test_auto_detach_kernel_driver
86
+ assert LIBUSB.has_capability?(:CAP_SUPPORTS_DETACH_KERNEL_DRIVER), "libusb should have CAP_SUPPORTS_DETACH_KERNEL_DRIVER"
87
+
88
+ device.open do |dev|
89
+ dev.attach_kernel_driver 0
90
+ assert dev.kernel_driver_active?(0), "kernel driver should attached, now"
91
+ dev.auto_detach_kernel_driver = true
92
+ dev.claim_interface(0)
93
+ refute dev.kernel_driver_active?(0), "kernel driver should get detached automatically"
94
+ end
95
+ end
73
96
  end
@@ -13,10 +13,10 @@
13
13
  # You should have received a copy of the GNU Lesser General Public License
14
14
  # along with Libusb for Ruby. If not, see <http://www.gnu.org/licenses/>.
15
15
 
16
- require "test/unit"
16
+ require "minitest/autorun"
17
17
  require "libusb"
18
18
 
19
- class TestLibusbStructs < Test::Unit::TestCase
19
+ class TestLibusbStructs < Minitest::Test
20
20
  def test_struct_Timeval
21
21
  s = LIBUSB::Call::Timeval.new
22
22
  assert_equal 0, s.in_ms
@@ -16,10 +16,10 @@
16
16
  # This test requires two connected, but not mounted mass storage device with
17
17
  # read/write access allowed.
18
18
 
19
- require "test/unit"
19
+ require "minitest/autorun"
20
20
  require "libusb"
21
21
 
22
- class TestLibusbThreads < Test::Unit::TestCase
22
+ class TestLibusbThreads < Minitest::Test
23
23
  include LIBUSB
24
24
 
25
25
  BOMS_GET_MAX_LUN = 0xFE
@@ -13,10 +13,10 @@
13
13
  # You should have received a copy of the GNU Lesser General Public License
14
14
  # along with Libusb for Ruby. If not, see <http://www.gnu.org/licenses/>.
15
15
 
16
- require "test/unit"
16
+ require "minitest/autorun"
17
17
  require "libusb"
18
18
 
19
- class TestLibusbVersion < Test::Unit::TestCase
19
+ class TestLibusbVersion < Minitest::Test
20
20
  def setup
21
21
  @v = LIBUSB.version
22
22
  end
metadata CHANGED
@@ -1,14 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libusb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.4.0
5
5
  platform: x64-mingw32
6
6
  authors:
7
7
  - Lars Kanis
8
8
  autorequire:
9
9
  bindir: bin
10
- cert_chain: []
11
- date: 2013-04-05 00:00:00.000000000 Z
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIDPDCCAiSgAwIBAgIBADANBgkqhkiG9w0BAQUFADBEMQ0wCwYDVQQDDARsYXJz
14
+ MR8wHQYKCZImiZPyLGQBGRYPZ3JlaXotcmVpbnNkb3JmMRIwEAYKCZImiZPyLGQB
15
+ GRYCZGUwHhcNMTMwMzExMjAyMjIyWhcNMTQwMzExMjAyMjIyWjBEMQ0wCwYDVQQD
16
+ DARsYXJzMR8wHQYKCZImiZPyLGQBGRYPZ3JlaXotcmVpbnNkb3JmMRIwEAYKCZIm
17
+ iZPyLGQBGRYCZGUwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDZb4Uv
18
+ RFJfRu/VEWiy3psh2jinETjiuBrL0NeRFGf8H7iU9+gx/DI/FFhfHGLrDeIskrJx
19
+ YIWDMmEjVO10UUdj7wu4ZhmU++0Cd7Kq9/TyP/shIP3IjqHjVLCnJ3P6f1cl5rxZ
20
+ gqo+d3BAoDrmPk0rtaf6QopwUw9RBiF8V4HqvpiY+ruJotP5UQDP4/lVOKvA8PI9
21
+ P0GmVbFBrbc7Zt5h78N3UyOK0u+nvOC23BvyHXzCtcFsXCoEkt+Wwh0RFqVZdnjM
22
+ LMO2vULHKKHDdX54K/sbVCj9pN9h1aotNzrEyo55zxn0G9PHg/G3P8nMvAXPkUTe
23
+ brhXrfCwWRvOXA4TAgMBAAGjOTA3MAsGA1UdDwQEAwIEsDAJBgNVHRMEAjAAMB0G
24
+ A1UdDgQWBBRAHK81igrXodaDj8a8/BIKsaZrETANBgkqhkiG9w0BAQUFAAOCAQEA
25
+ Iswhcol3ytXthaUH3k5LopZ09viZrZHzAw0QleI3Opl/9QEGJ2BPV9+93iC0OrNL
26
+ hmnxig6vKK1EeJ5PHXJ8hOI3nTZBrOmQcEXNBqyToP1FHMWZqwZ8wiBPXtiCqDBR
27
+ ePQ25J9xFNzQ1ItgzNSpx5cs67QNKrx5woocoBHD6kStFbshZPJx4axl3GbUFQd5
28
+ H//3YdPQOH3jaVeUXhS+pz/gfbx8fhFAtsQ+855A3HO7g2ZRIg/atAp/0MFyn5s5
29
+ 0rq+VHOIPyvxF5khT0mYAcNmZTC8z1yPsqdgwfYNDjsSWwiIRSPUSmJRvfjM8hsW
30
+ mMFp4kPUHbWOqCp2mz9gCA==
31
+ -----END CERTIFICATE-----
32
+ date: 2013-11-20 00:00:00.000000000 Z
12
33
  dependencies:
13
34
  - !ruby/object:Gem::Dependency
14
35
  name: ffi
@@ -90,6 +111,7 @@ files:
90
111
  - test/test_libusb_descriptors.rb
91
112
  - test/test_libusb_event_machine.rb
92
113
  - test/test_libusb_gc.rb
114
+ - test/test_libusb_hotplug.rb
93
115
  - test/test_libusb_iso_transfer.rb
94
116
  - test/test_libusb_mass_storage.rb
95
117
  - test/test_libusb_mass_storage2.rb
@@ -119,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
141
  version: '0'
120
142
  requirements: []
121
143
  rubyforge_project: libusb
122
- rubygems_version: 2.0.0.rc.2
144
+ rubygems_version: 2.0.3
123
145
  signing_key:
124
146
  specification_version: 4
125
147
  summary: Access USB devices from Ruby via libusb-1.0
Binary file