Quellcode durchsuchen

fix walker such that it returns 1 device per interface

Matthew Di Ferrante vor 8 Jahren
Ursprung
Commit
4951867aa2
3 geänderte Dateien mit 11 neuen und 13 gelöschten Zeilen
  1. 2 2
      example/main.go
  2. 0 2
      hid.go
  3. 9 9
      usb_linux.go

+ 2 - 2
example/main.go

@@ -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 {
@@ -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
 		}

+ 0 - 2
hid.go

@@ -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)

+ 9 - 9
usb_linux.go

@@ -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")
@@ -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{}
@@ -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