Sha256: 0351818037b655c5115009ed99118a636f6a5449da1c27182b3ec661ee4673fa

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

require "libusb"
require "pp"

require "usbutils/version"
require "usbutils/cli"

module USBUtils
  class << self
    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

    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|
        idvendor  = format("%04x", usbdev.idVendor)
        idproduct = format("%04x", usbdev.idProduct)
        device = devices.select {|item| item[:idvendor] == idvendor && item[:idproduct] == idproduct}.first
        device[:usbdev] = usbdev if device
      end
      devices
    end

    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
usbutils-0.0.3 lib/usbutils.rb
usbutils-0.0.2 lib/usbutils.rb