Skip to content

Commit

Permalink
fix walker such that it returns 1 device per interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdf committed Sep 6, 2016
1 parent 89d75b8 commit 9954f88
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
4 changes: 2 additions & 2 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func main() {
found := false
hid.UsbWalk(func(device hid.Device) {
info := device.Info()
fmt.Printf("%04x:%04x:%04x\n", info.Vendor, info.Product, info.Revision)
fmt.Printf("%04x:%04x:%04x:%02x\n", info.Vendor, info.Product, info.Revision, info.Interface)
found = true
})
if !found {
Expand All @@ -131,7 +131,7 @@ func main() {

hid.UsbWalk(func(device hid.Device) {
info := device.Info()
id := fmt.Sprintf("%04x:%04x:%04x", info.Vendor, info.Product, info.Revision)
id := fmt.Sprintf("%04x:%04x:%04x:%02x", info.Vendor, info.Product, info.Revision, info.Interface)
if id != os.Args[1] {
return
}
Expand Down
2 changes: 0 additions & 2 deletions hid.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ type Device interface {
Open() error
Close()
Info() Info
SetEndpoint(int)
SetInterface(int)
HIDReport() ([]byte, error)
SetReport(int, []byte) error
GetReport(int) ([]byte, error)
Expand Down
18 changes: 9 additions & 9 deletions usb_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ type usbDevice struct {
path string
}

func (hid *usbDevice) SetEndpoint(ep int) {
hid.epOut = ep
hid.epIn = ep + 0x80
}

func (hid *usbDevice) SetInterface(ifno int) {
hid.info.Interface = uint8(ifno)
}

func (hid *usbDevice) Open() (err error) {
if hid.f != nil {
return errors.New("device is already opened")
Expand Down Expand Up @@ -238,6 +229,10 @@ func walker(path string, cb func(Device)) error {
device = nil
}
case UsbDescTypeInterface:
if device != nil {
cb(device)
device = nil
}
expected[UsbDescTypeEndpoint] = true
expected[UsbDescTypeReport] = true
i := &interfaceDesc{}
Expand All @@ -259,6 +254,11 @@ func walker(path string, cb func(Device)) error {
}
case UsbDescTypeEndpoint:
if device != nil {
if device.epIn != 0 && device.epOut != 0 {
cb(device)
device.epIn = 0
device.epOut = 0
}
e := &endpointDesc{}
if err := cast(body, e); err != nil {
return err
Expand Down

0 comments on commit 9954f88

Please sign in to comment.