Skip to content

Commit

Permalink
add USB bus and device fields to info struct
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrake committed Oct 18, 2018
1 parent c86e7ad commit 08ec69b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions hid.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type Info struct {
Protocol uint8

Interface uint8
Bus int
Device int
}

//
Expand Down
14 changes: 14 additions & 0 deletions usb_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"syscall"
"time"
"unsafe"
Expand Down Expand Up @@ -186,6 +188,9 @@ func cast(b []byte, to interface{}) error {
return binary.Read(r, binary.LittleEndian, to)
}

// typical /dev bus entry: /dev/bus/usb/006/003
var reDevBusDevice = regexp.MustCompile(`/dev/bus/usb/(\d+)/(\d+)`)

func walker(path string, cb func(Device)) error {
if desc, err := ioutil.ReadFile(path); err != nil {
return err
Expand Down Expand Up @@ -241,6 +246,13 @@ func walker(path string, cb func(Device)) error {
return err
}
if i.InterfaceClass == UsbHidClass {
matches := reDevBusDevice.FindStringSubmatch(path)
bus := 0
dev := 0
if len(matches) >= 3 {
bus, _ = strconv.Atoi(matches[1])
dev, _ = strconv.Atoi(matches[2])
}
device = &usbDevice{
info: Info{
Vendor: devDesc.Vendor,
Expand All @@ -249,6 +261,8 @@ func walker(path string, cb func(Device)) error {
SubClass: i.InterfaceSubClass,
Protocol: i.InterfaceProtocol,
Interface: i.Number,
Bus: bus,
Device: dev,
},
path: path,
}
Expand Down

0 comments on commit 08ec69b

Please sign in to comment.