module USB
USB module is a binding for libusb.
It needs appropriate privilege to access USB. For example, the process should be an member of plugdev group on Debin GNU/Linux (etch).
Example¶ ↑
-
list up USB devices
require 'usb' require 'pp' pp USB.devices #=> [#<USB::Device 001/001 0000:0000 Linux 2.6.17-2-486 uhci_hcd UHCI Host Controller 0000:00:1d.0 (Full speed Hub)>, #<USB::Device 002/001 0000:0000 Linux 2.6.17-2-486 uhci_hcd UHCI Host Controller 0000:00:1d.1 (Full speed Hub)>, #<USB::Device 003/001 0000:0000 Linux 2.6.17-2-486 uhci_hcd UHCI Host Controller 0000:00:1d.2 (Full speed Hub)>, #<USB::Device 004/001 0000:0000 Linux 2.6.17-2-486 ehci_hcd EHCI Host Controller 0000:00:1d.7 (Hi-speed Hub with single TT)>]
-
find a device by bus id and device id
# find the device "004/001" in the above list. dev = USB.find_bus(4).find_device(1) p dev #=> #<USB::Device 004/001 0000:0000 Linux 2.6.17-2-486 ehci_hcd EHCI Host Controller 0000:00:1d.7 (Hi-speed Hub with single TT)>
-
open a device
dev.open {|handle| p handle } #=> #<USB::DevHandle:0xa7d94688>
USB overview¶ ↑
-
A host has busses.
-
A bus has devices.
-
A device has configurations.
-
A configuration has interfaces.
-
A interface has settings.
-
A setting has endpoints.
——– libusb binding initialization ——–
Public Class Methods
busses()
click to toggle source
# File lib/usb.rb, line 64 def USB.busses result = [] bus = USB.first_bus while bus result << bus bus = bus.next end result.sort_by {|b| b.dirname } end
configurations()
click to toggle source
# File lib/usb.rb, line 75 def USB.configurations() USB.devices.map {|d| d.configurations }.flatten end
devices()
click to toggle source
# File lib/usb.rb, line 74 def USB.devices() USB.busses.map {|b| b.devices }.flatten end
each_device_by_class(devclass, subclass=nil, protocol=nil) { |dev| ... }
click to toggle source
searches devices by USB device class, subclass and protocol.
# find hubs. USB.each_device_by_class(USB::USB_CLASS_HUB) {|d| p d }' # find Full speed Hubs USB.each_device_by_class(USB::USB_CLASS_HUB, 0, 0) {|d| p d }' # find Hi-speed Hubs with single TT USB.each_device_by_class(USB::USB_CLASS_HUB, 0, 1) {|d| p d }' # find Hi-speed Hubs with multiple TT USB.each_device_by_class(USB::USB_CLASS_HUB, 0, 2) {|d| p d }'
# File lib/usb.rb, line 103 def USB.each_device_by_class(devclass, subclass=nil, protocol=nil) USB.devices.each {|dev| if dev.bDeviceClass == USB::USB_CLASS_PER_INTERFACE found = dev.settings.any? {|s| s.bInterfaceClass == devclass && (!subclass || s.bInterfaceSubClass == subclass) && (!protocol || s.bInterfaceProtocol == protocol) } else found = dev.bDeviceClass == devclass && (!subclass || dev.bDeviceSubClass == subclass) && (!protocol || dev.bDeviceProtocol == protocol) end yield dev if found } nil end
endpoints()
click to toggle source
# File lib/usb.rb, line 78 def USB.endpoints() USB.settings.map {|d| d.endpoints }.flatten end
find_bus(n)
click to toggle source
# File lib/usb.rb, line 80 def USB.find_bus(n) bus = USB.first_bus while bus return bus if n == bus.dirname.to_i bus = bus.next end return nil end
find_busses()
click to toggle source
static VALUE rusb_find_busses(VALUE cUSB) { st_foreach(bus_objects, revoke_data_i, 0); st_foreach(device_objects, revoke_data_i, 0); st_foreach(config_descriptor_objects, revoke_data_i, 0); st_foreach(interface_objects, revoke_data_i, 0); st_foreach(interface_descriptor_objects, revoke_data_i, 0); st_foreach(endpoint_descriptor_objects, revoke_data_i, 0); return INT2NUM(usb_find_busses()); }
find_devices()
click to toggle source
static VALUE rusb_find_devices(VALUE cUSB) { return INT2NUM(usb_find_devices()); }
first_bus()
click to toggle source
static VALUE rusb_first_bus(VALUE cUSB) { struct usb_bus *bus = usb_get_busses(); return rusb_bus_make(bus, Qnil); }
interfaces()
click to toggle source
# File lib/usb.rb, line 76 def USB.interfaces() USB.configurations.map {|d| d.interfaces }.flatten end
settings()
click to toggle source
# File lib/usb.rb, line 77 def USB.settings() USB.interfaces.map {|d| d.settings }.flatten end