Преглед на файлове

using packet size from the endpoint descriptor

Serge A. Zaitsev преди 9 години
родител
ревизия
7e8aadc6e7
променени са 3 файла, в които са добавени 14 реда и са изтрити 20 реда
  1. 6 17
      example/main.go
  2. 1 1
      hid.go
  3. 7 2
      usb_linux.go

+ 6 - 17
example/main.go

@@ -5,7 +5,6 @@ import (
 	"fmt"
 	"log"
 	"os"
-	"strconv"
 	"strings"
 	"time"
 
@@ -13,7 +12,7 @@ import (
 	"github.com/zserge/hid"
 )
 
-func shell(device hid.Device, inputReportSize int) {
+func shell(device hid.Device) {
 	if err := device.Open(); err != nil {
 		log.Println("Open error: ", err)
 		return
@@ -29,7 +28,7 @@ func shell(device hid.Device, inputReportSize int) {
 
 	go func() {
 		for {
-			if buf, err := device.Read(inputReportSize, 1*time.Second); err == nil {
+			if buf, err := device.Read(1 * time.Second); err == nil {
 				log.Println("Input report:  ", hex.EncodeToString(buf))
 			}
 		}
@@ -109,9 +108,9 @@ func main() {
 
 	if len(os.Args) == 2 && (os.Args[1] == "-h" || os.Args[1] == "--help") {
 		fmt.Println("USAGE:")
-		fmt.Printf("  %s                list USB HID devices\n", os.Args[0])
-		fmt.Printf("  %s <id> [size]    open USB HID device shell for the given input report size\n", os.Args[0])
-		fmt.Printf("  %s -h|--help      show this help\n", os.Args[0])
+		fmt.Printf("  %s              list USB HID devices\n", os.Args[0])
+		fmt.Printf("  %s <id>         open USB HID device shell for the given input report size\n", os.Args[0])
+		fmt.Printf("  %s -h|--help    show this help\n", os.Args[0])
 		fmt.Println()
 		return
 	}
@@ -130,16 +129,6 @@ func main() {
 		return
 	}
 
-	inputReportSize := 1
-
-	if len(os.Args) > 2 {
-		if n, err := strconv.Atoi(os.Args[2]); err == nil {
-			inputReportSize = n
-		} else {
-			log.Fatal(err)
-		}
-	}
-
 	hid.UsbWalk(func(device hid.Device) {
 		info := device.Info()
 		id := fmt.Sprintf("%04x:%04x:%04x", info.Vendor, info.Product, info.Revision)
@@ -147,6 +136,6 @@ func main() {
 			return
 		}
 
-		shell(device, inputReportSize)
+		shell(device)
 	})
 }

+ 1 - 1
hid.go

@@ -26,6 +26,6 @@ type Device interface {
 	HIDReport() ([]byte, error)
 	SetReport(int, []byte) error
 	GetReport(int) ([]byte, error)
-	Read(n int, ms time.Duration) ([]byte, error)
+	Read(ms time.Duration) ([]byte, error)
 	Write(data []byte, ms time.Duration) (int, error)
 }

+ 7 - 2
usb_linux.go

@@ -21,6 +21,9 @@ type usbDevice struct {
 	epIn  int
 	epOut int
 
+	inputPacketSize  uint16
+	outputPacketSize uint16
+
 	path string
 }
 
@@ -122,8 +125,8 @@ func (hid *usbDevice) intr(ep int, data []byte, t int) (int, error) {
 	}
 }
 
-func (hid *usbDevice) Read(n int, timeout time.Duration) ([]byte, error) {
-	data := make([]byte, n, n)
+func (hid *usbDevice) Read(timeout time.Duration) ([]byte, error) {
+	data := make([]byte, hid.inputPacketSize, hid.inputPacketSize)
 	ms := timeout / (1 * time.Millisecond)
 	n, err := hid.intr(hid.epIn, data, int(ms))
 	if err == nil {
@@ -250,8 +253,10 @@ func walker(path string, cb func(Device)) error {
 							}
 							if e.Address > 0x80 && device.epIn == 0 {
 								device.epIn = int(e.Address)
+								device.inputPacketSize = e.MaxPacketSize
 							} else if e.Address < 0x80 && device.epOut == 0 {
 								device.epOut = int(e.Address)
+								device.outputPacketSize = e.MaxPacketSize
 							}
 						}
 					}