forked from gerryd/ms213x-status
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hid.go
74 lines (64 loc) · 1.79 KB
/
hid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"fmt"
"os"
"errors"
"github.com/BertoldVdb/ms-tools/gohid"
"github.com/sstallion/go-hid"
)
func SearchDevice(foundHandler func(info *hid.DeviceInfo) error) error {
for _, vid := range []uint16{uint16(CLI.VID), uint16(CLI.VID2)} {
if err := hid.Enumerate(vid, uint16(CLI.PID), func(info *hid.DeviceInfo) error {
if CLI.Serial != "" && info.SerialNbr != CLI.Serial {
return nil
}
if CLI.RawPath != "" && info.Path != CLI.RawPath {
return nil
}
return foundHandler(info)
}); err != nil {
return err
}
}
return nil
}
func OpenDevice() (gohid.HIDDevice, error) {
var dev *hid.Device
err := SearchDevice(func(info *hid.DeviceInfo) error {
d, err := hid.Open(info.VendorID, info.ProductID, info.SerialNbr)
if err == nil {
dev = d
return errors.New("Done")
}
return err
})
if dev != nil {
return dev, nil
}
if err == nil {
err = os.ErrNotExist
}
return nil, err
}
type ListHIDCmd struct {
}
func (l *ListHIDCmd) Run(c *Context) error {
return SearchDevice(func(info *hid.DeviceInfo) error {
fmt.Printf("%s: ID %04x:%04x %s %s\n",
info.Path, info.VendorID, info.ProductID, info.MfrStr, info.ProductStr)
fmt.Println("Device Information:")
fmt.Printf("\tPath %s\n", info.Path)
fmt.Printf("\tVendorID %04x\n", info.VendorID)
fmt.Printf("\tProductID %04x\n", info.ProductID)
fmt.Printf("\tSerialNbr %s\n", info.SerialNbr)
fmt.Printf("\tReleaseNbr %x.%x\n", info.ReleaseNbr>>8, info.ReleaseNbr&0xff)
fmt.Printf("\tMfrStr %s\n", info.MfrStr)
fmt.Printf("\tProductStr %s\n", info.ProductStr)
fmt.Printf("\tUsagePage %#x\n", info.UsagePage)
fmt.Printf("\tUsage %#x\n", info.Usage)
fmt.Printf("\tInterfaceNbr %d\n", info.InterfaceNbr)
fmt.Println()
return nil
})
return nil
}