Module: USBUtils

Defined in:
lib/usbutils.rb,
lib/usbutils/cli.rb,
lib/usbutils/version.rb

Defined Under Namespace

Classes: CLI

Constant Summary collapse

VERSION =
"0.0.4"

Class Method Summary collapse

Class Method Details

.devicesObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/usbutils.rb', line 16

def devices
  devices = []
  Dir.glob("/sys/bus/usb/devices/usb*") do |device|
    set_device(devices, device, 0, 0)
  end
  usb = LIBUSB::Context.new
  usb.devices.each do |usbdev|
    device = devices.select {|item| item[:serial] == usbdev.serial_number}.first
    device[:usbdev] = usbdev if device
  end
  devices
end

.find_by(by, value) ⇒ Object



9
10
11
12
13
14
# File 'lib/usbutils.rb', line 9

def find_by(by, value)
  devices.each do |device|
    return device if device[by.to_sym] == value
  end
  raise "device not found: #{by}:#{value}"
end

.set_device(devices, devpath, parent, level) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/usbutils.rb', line 29

def set_device(devices, devpath, parent, level)
  return unless FileTest::directory?(devpath)
  Dir.chdir(devpath) do
    next unless File.exists? "busnum"
    busnum = `cat busnum`.chomp
    if File.exists? "serial"
      devnum       = `cat devnum`.chomp
      idvendor     = `cat idVendor`.chomp
      idproduct    = `cat idProduct`.chomp
      manufacturer = `cat manufacturer`.chomp
      product      = `cat product`.chomp
      serial       = `cat serial`.chomp
      devices << {
        :serial       => serial,
        :idvendor     => idvendor,
        :idproduct    => idproduct,
        :manufacturer => manufacturer,
        :product      => product,
      }
    end
    Dir.glob("#{devpath}/#{busnum}-*") do |subdev|
      subdev = subdev.chomp
      set_device devices, subdev, devnum, level + 1
    end
  end
end