diff --git a/cmd/hidread/main.go b/cmd/hidread/main.go index 42d065d..ebad554 100644 --- a/cmd/hidread/main.go +++ b/cmd/hidread/main.go @@ -10,6 +10,7 @@ import ( "os" "runtime/debug" "strings" + "time" "github.com/sstallion/go-hid" ) @@ -38,6 +39,10 @@ func main() { path := flag.String("f", "", "File path to device") size := flag.Int("s", 64, "Size of Input Reports to read") versionOnly := flag.Bool("v", false, "Output version information.") + enumerate := flag.Bool("e", false, "Enumerate devices.") + vid := flag.Uint("vid", 0, "VID for device to open.") + pid := flag.Uint("pid", 0, "PID for device to open.") + continue_search := flag.Bool("c", false, "Continue scanning for device with specified VID and PID, open when it appears.") flag.Parse() @@ -50,11 +55,54 @@ func main() { os.Exit(0) } - if *help || *path == "" { + if *help { flag.Usage() os.Exit(0) } + if *enumerate { + hid.Enumerate(hid.VendorIDAny, hid.ProductIDAny, func(info *hid.DeviceInfo) error { + fmt.Printf("%s: ID %04x:%04x %s %s\n", + info.Path, + info.VendorID, + info.ProductID, + info.MfrStr, + info.ProductStr) + return nil + }) + os.Exit(0) + } + + var hid_dev hid.DeviceInfo + if *vid != 0 && *pid != 0 { + for { + hid.Enumerate(uint16(*vid), uint16(*pid), func(info *hid.DeviceInfo) error { + fmt.Printf("%s: ID %04x:%04x %s %s\n", + info.Path, + info.VendorID, + info.ProductID, + info.MfrStr, + info.ProductStr) + hid_dev = *info + + return nil + }) + + *path = hid_dev.Path + + if *path != "" || !*continue_search { + break + } + time.Sleep(1 * time.Second) + fmt.Printf("Searching..\n") + } + } + + if *path == "" { + fmt.Printf("Path empty\n") + os.Exit(0) + } + dev, err := hid.OpenPath(*path) if err != nil { fmt.Printf("%v\n", err)