Module: USB

Defined in:
lib/libusb/compat.rb

Overview

Compatibility layer for ruby-usb (API based on libusb-0.1)

This module provides some limited compatibility to ruby-usb.

Usage example:

begin
  require 'usb'
rescue LoadError
  require 'libusb/compat'
end
p USB.devices => [#<USB::Device ...>]

Known issues:

  • Exceptions are different between ruby-usb and libusb and they don’t get converted

  • libusb-1.0 doesn’t explicitly manage USB-buses, so only one Bus is used currently

Defined Under Namespace

Classes: Bus, Configuration, DevHandle, Device, Endpoint, Interface, Setting

Constant Summary collapse

USB_CLASS_PER_INTERFACE =
LIBUSB::CLASS_PER_INTERFACE
USB_CLASS_AUDIO =
LIBUSB::CLASS_AUDIO
USB_CLASS_COMM =
LIBUSB::CLASS_COMM
USB_CLASS_HID =
LIBUSB::CLASS_HID
USB_CLASS_PRINTER =
LIBUSB::CLASS_PRINTER
USB_CLASS_PTP =
LIBUSB::CLASS_PTP
USB_CLASS_MASS_STORAGE =
LIBUSB::CLASS_MASS_STORAGE
USB_CLASS_HUB =
LIBUSB::CLASS_HUB
USB_CLASS_DATA =
LIBUSB::CLASS_DATA
USB_CLASS_VENDOR_SPEC =
LIBUSB::CLASS_VENDOR_SPEC
USB_DT_DEVICE =
LIBUSB::DT_DEVICE
USB_DT_CONFIG =
LIBUSB::DT_CONFIG
USB_DT_STRING =
LIBUSB::DT_STRING
USB_DT_INTERFACE =
LIBUSB::DT_INTERFACE
USB_DT_ENDPOINT =
LIBUSB::DT_ENDPOINT
USB_DT_HID =
LIBUSB::DT_HID
USB_DT_REPORT =
LIBUSB::DT_REPORT
USB_DT_PHYSICAL =
LIBUSB::DT_PHYSICAL
USB_DT_HUB =
LIBUSB::DT_HUB
USB_DT_DEVICE_SIZE =
LIBUSB::DT_DEVICE_SIZE
USB_DT_CONFIG_SIZE =
LIBUSB::DT_CONFIG_SIZE
USB_DT_INTERFACE_SIZE =
LIBUSB::DT_INTERFACE_SIZE
USB_DT_ENDPOINT_SIZE =
LIBUSB::DT_ENDPOINT_SIZE
USB_DT_ENDPOINT_AUDIO_SIZE =
LIBUSB::DT_ENDPOINT_AUDIO_SIZE
USB_DT_HUB_NONVAR_SIZE =
LIBUSB::DT_HUB_NONVAR_SIZE
USB_ENDPOINT_ADDRESS_MASK =
LIBUSB::ENDPOINT_ADDRESS_MASK
USB_ENDPOINT_DIR_MASK =
LIBUSB::ENDPOINT_DIR_MASK
USB_ENDPOINT_IN =
LIBUSB::ENDPOINT_IN
USB_ENDPOINT_OUT =
LIBUSB::ENDPOINT_OUT
USB_ENDPOINT_TYPE_MASK =
LIBUSB::TRANSFER_TYPE_MASK
USB_ENDPOINT_TYPE_CONTROL =
LIBUSB::TRANSFER_TYPE_CONTROL
USB_ENDPOINT_TYPE_ISOCHRONOUS =
LIBUSB::TRANSFER_TYPE_ISOCHRONOUS
USB_ENDPOINT_TYPE_BULK =
LIBUSB::TRANSFER_TYPE_BULK
USB_ENDPOINT_TYPE_INTERRUPT =
LIBUSB::TRANSFER_TYPE_INTERRUPT
USB_REQ_GET_STATUS =
LIBUSB::REQUEST_GET_STATUS
USB_REQ_CLEAR_FEATURE =
LIBUSB::REQUEST_CLEAR_FEATURE
USB_REQ_SET_FEATURE =
LIBUSB::REQUEST_SET_FEATURE
USB_REQ_SET_ADDRESS =
LIBUSB::REQUEST_SET_ADDRESS
USB_REQ_GET_DESCRIPTOR =
LIBUSB::REQUEST_GET_DESCRIPTOR
USB_REQ_SET_DESCRIPTOR =
LIBUSB::REQUEST_SET_DESCRIPTOR
USB_REQ_GET_CONFIGURATION =
LIBUSB::REQUEST_GET_CONFIGURATION
USB_REQ_SET_CONFIGURATION =
LIBUSB::REQUEST_SET_CONFIGURATION
USB_REQ_GET_INTERFACE =
LIBUSB::REQUEST_GET_INTERFACE
USB_REQ_SET_INTERFACE =
LIBUSB::REQUEST_SET_INTERFACE
USB_REQ_SYNCH_FRAME =
LIBUSB::REQUEST_SYNCH_FRAME
USB_TYPE_STANDARD =
LIBUSB::REQUEST_TYPE_STANDARD
USB_TYPE_CLASS =
LIBUSB::REQUEST_TYPE_CLASS
USB_TYPE_VENDOR =
LIBUSB::REQUEST_TYPE_VENDOR
USB_TYPE_RESERVED =
LIBUSB::REQUEST_TYPE_RESERVED
USB_RECIP_DEVICE =
LIBUSB::RECIPIENT_DEVICE
USB_RECIP_INTERFACE =
LIBUSB::RECIPIENT_INTERFACE
USB_RECIP_ENDPOINT =
LIBUSB::RECIPIENT_ENDPOINT
USB_RECIP_OTHER =
LIBUSB::RECIPIENT_OTHER
HAS_GET_DRIVER_NP =
!FFI::Platform.windows?
HAS_DETACH_KERNEL_DRIVER_NP =
!FFI::Platform.windows?
USB_MAXENDPOINTS =

not defined by libusb-1.0, but typical values are:

32
USB_MAXINTERFACES =
32
USB_MAXALTSETTING =
128
USB_MAXCONFIG =
8

Class Method Summary collapse

Class Method Details

.bussesObject



118
119
120
# File 'lib/libusb/compat.rb', line 118

def USB.busses
  [default_bus]
end

.configurationsObject



123
# File 'lib/libusb/compat.rb', line 123

def USB.configurations() USB.devices.map {|d| d.configurations }.flatten end

.default_busObject



46
47
48
# File 'lib/libusb/compat.rb', line 46

def self.default_bus
  @default_bus ||= Bus.new(default_context)
end

.default_contextObject



41
42
43
# File 'lib/libusb/compat.rb', line 41

def self.default_context
  @default_context ||= LIBUSB::Context.new
end

.dev_string(base_class, sub_class, protocol) ⇒ Object



158
159
160
# File 'lib/libusb/compat.rb', line 158

def USB.dev_string(base_class, sub_class, protocol)
  LIBUSB.dev_string(base_class, sub_class, protocol)
end

.devicesObject



122
# File 'lib/libusb/compat.rb', line 122

def USB.devices; default_context.devices.map{|c| Device.new(c) }; end

.each_device_by_class(devclass, subclass = nil, protocol = nil) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/libusb/compat.rb', line 132

def USB.each_device_by_class(devclass, subclass=nil, protocol=nil)
  devs = default_context.devices bClass: devclass, bSubClass: subclass, bProtocol: protocol
  devs.each do |dev|
    yield Device.new(dev)
  end
  nil
end

.endpointsObject



126
# File 'lib/libusb/compat.rb', line 126

def USB.endpoints() USB.settings.map {|d| d.endpoints }.flatten end

.find_bus(n) ⇒ Object



128
129
130
# File 'lib/libusb/compat.rb', line 128

def USB.find_bus(n)
  default_bus
end

.interfacesObject



124
# File 'lib/libusb/compat.rb', line 124

def USB.interfaces() USB.configurations.map {|d| d.interfaces }.flatten end

.settingsObject



125
# File 'lib/libusb/compat.rb', line 125

def USB.settings() USB.interfaces.map {|d| d.settings }.flatten end