Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 5 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
1  Manifest.txt
View
@@ -13,3 +13,4 @@ test/test_libusb_descriptors.rb
test/test_libusb_gc.rb
test/test_libusb_iso_transfer.rb
test/test_libusb_mass_storage.rb
+test/test_libusb_mass_storage2.rb
6 README.rdoc
View
@@ -27,9 +27,9 @@ LIBUSB for Ruby is covered by the GNU Lesser General Public License version 3.
usb = LIBUSB::Context.new
device = usb.devices(:idVendor => 0x04b4, :idProduct => 0x8613).first
device.open do |handle|
- handle.claim_interface(0)
- handle.control_transfer(:bmRequestType => 0x40, :bRequest => 0xa0, :wValue => 0xe600, :wIndex => 0x0000, :dataOut => 1.chr)
- handle.release_interface(0)
+ handle.claim_interface(0) do
+ handle.control_transfer(:bmRequestType => 0x40, :bRequest => 0xa0, :wValue => 0xe600, :wIndex => 0x0000, :dataOut => 1.chr)
+ end
end
{LIBUSB::Context#devices} is used to get all or only particular devices.
After opening the {LIBUSB::Device} the resulting {LIBUSB::DevHandle} can be
41 lib/libusb.rb
View
@@ -165,15 +165,15 @@ module Call
attach_function 'libusb_close', [:pointer], :void
attach_function 'libusb_get_device', [:libusb_device_handle], :pointer
- attach_function 'libusb_set_configuration', [:libusb_device_handle, :int], :int
+ attach_function 'libusb_set_configuration', [:libusb_device_handle, :int], :int, :blocking=>true
attach_function 'libusb_claim_interface', [:libusb_device_handle, :int], :int
- attach_function 'libusb_release_interface', [:libusb_device_handle, :int], :int
+ attach_function 'libusb_release_interface', [:libusb_device_handle, :int], :int, :blocking=>true
attach_function 'libusb_open_device_with_vid_pid', [:pointer, :int, :int], :pointer
- attach_function 'libusb_set_interface_alt_setting', [:libusb_device_handle, :int, :int], :int
- attach_function 'libusb_clear_halt', [:libusb_device_handle, :int], :int
- attach_function 'libusb_reset_device', [:libusb_device_handle], :int
+ attach_function 'libusb_set_interface_alt_setting', [:libusb_device_handle, :int, :int], :int, :blocking=>true
+ attach_function 'libusb_clear_halt', [:libusb_device_handle, :int], :int, :blocking=>true
+ attach_function 'libusb_reset_device', [:libusb_device_handle], :int, :blocking=>true
attach_function 'libusb_kernel_driver_active', [:libusb_device_handle, :int], :int
attach_function 'libusb_detach_kernel_driver', [:libusb_device_handle, :int], :int
@@ -186,7 +186,7 @@ module Call
attach_function 'libusb_cancel_transfer', [:pointer], :int
attach_function 'libusb_free_transfer', [:pointer], :void
- attach_function 'libusb_handle_events', [:libusb_context], :int
+ attach_function 'libusb_handle_events', [:libusb_context], :int, :blocking=>true
callback :libusb_transfer_cb_fn, [:pointer], :void
@@ -971,7 +971,7 @@ def pDev.unref_device(id)
LIBUSB.raise_error res, "in libusb_get_device_descriptor" if res!=0
end
- # Open a device and obtain a device handle.
+ # Open the device and obtain a device handle.
#
# A handle allows you to perform I/O on the device in question.
# This is a non-blocking function; no requests are sent over the bus.
@@ -996,6 +996,21 @@ def open
end
end
+ # Open the device and claim an interface.
+ #
+ # This is a convenience method to {Device#open} and {DevHandle#claim_interface}.
+ # Must be called with a block. When the block has finished, the interface
+ # will be released and the device will be closed.
+ #
+ # @param [Interface, Fixnum] interface the interface or it's bInterfaceNumber you wish to claim
+ def open_interface(interface)
+ open do |dev|
+ dev.claim_interface(interface) do
+ yield dev
+ end
+ end
+ end
+
# Get the number of the bus that a device is connected to.
def bus_number
Call.libusb_get_bus_number(@pDev)
@@ -1199,11 +1214,20 @@ def string_descriptor_ascii(index)
#
# This is a non-blocking function.
#
+ # If called with a block, the device handle is passed through to the block
+ # and the interface is released when the block has finished.
+ #
# @param [Interface, Fixnum] interface the interface or it's bInterfaceNumber you wish to claim
def claim_interface(interface)
interface = interface.bInterfaceNumber if interface.respond_to? :bInterfaceNumber
res = Call.libusb_claim_interface(@pHandle, interface)
LIBUSB.raise_error res, "in libusb_claim_interface" if res!=0
+ return self unless block_given?
+ begin
+ yield self
+ ensure
+ release_interface(interface)
+ end
end
# Release an interface previously claimed with {DevHandle#claim_interface}.
@@ -1272,8 +1296,7 @@ def set_configuration(configuration)
# to activate or the bInterfaceNumber of the previously-claimed interface
# @param [Fixnum, nil] alternate_setting the bAlternateSetting of the alternate setting to activate
# (only if first param is a Fixnum)
- def set_interface_alt_setting(setting_or_interface_number,
-alternate_setting=nil)
+ def set_interface_alt_setting(setting_or_interface_number, alternate_setting=nil)
alternate_setting ||= setting_or_interface_number.bAlternateSetting if setting_or_interface_number.respond_to? :bAlternateSetting
setting_or_interface_number = setting_or_interface_number.bInterfaceNumber if setting_or_interface_number.respond_to? :bInterfaceNumber
res = Call.libusb_set_interface_alt_setting(@pHandle, setting_or_interface_number, alternate_setting)
69 test/test_libusb_mass_storage2.rb
View
@@ -0,0 +1,69 @@
+# This file is part of Libusb for Ruby.
+#
+# Libusb for Ruby is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Libusb for Ruby is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with Libusb for Ruby. If not, see <http://www.gnu.org/licenses/>.
+#
+
+require "test/unit"
+require "libusb"
+
+class TestLibusbMassStorage2 < Test::Unit::TestCase
+ include LIBUSB
+
+ attr_accessor :usb
+ attr_accessor :device
+
+ def setup
+ @usb = Context.new
+ @usb.debug = 3
+ @device = usb.devices( :bClass=>CLASS_MASS_STORAGE, :bSubClass=>[0x06,0x01], :bProtocol=>0x50 ).last
+ abort "no mass storage device found" unless @device
+
+ # Ensure kernel driver is detached
+ device.open do |dev|
+ if RUBY_PLATFORM=~/linux/i && dev.kernel_driver_active?(0)
+ dev.detach_kernel_driver(0)
+ end
+ end
+ end
+
+ def teardown
+ end
+
+ def test_open_with_block
+ device.open do |dev|
+ assert_kind_of DevHandle, dev
+ assert_kind_of String, dev.string_descriptor_ascii(1)
+ end
+ end
+
+ def test_claim_interface_with_block
+ res = device.open do |dev|
+ dev.claim_interface(0) do |dev2|
+ assert_kind_of DevHandle, dev2
+ assert_kind_of String, dev2.string_descriptor_ascii(1)
+ 12345
+ end
+ end
+ assert_equal 12345, res, "Block versions should pass through the result"
+ end
+
+ def test_open_interface
+ res = device.open_interface(0) do |dev|
+ assert_kind_of DevHandle, dev
+ assert_kind_of String, dev.string_descriptor_ascii(1)
+ 12345
+ end
+ assert_equal 12345, res, "Block versions should pass through the result"
+ end
+end

No commit comments for this range

Something went wrong with that request. Please try again.