-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathusb.go
113 lines (92 loc) · 2.13 KB
/
usb.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"context"
"io"
"log"
"sort"
"github.com/google/gousb"
"github.com/pkg/errors"
)
type usbDevice struct {
ctx context.Context
in *gousb.InEndpoint
out *gousb.OutEndpoint
dev *gousb.Device
done func()
}
func (u usbDevice) Read(p []byte) (n int, err error) {
buf := p[:u.in.Desc.MaxPacketSize]
return u.in.ReadContext(u.ctx, buf)
}
func (u usbDevice) Write(p []byte) (n int, err error) {
return u.out.WriteContext(u.ctx, p)
}
func (u usbDevice) Close() error {
u.done()
return u.dev.Close()
}
func openUsbDevice(ctx context.Context, d *gousb.Device) (io.ReadWriteCloser, string, error) {
if err := d.SetAutoDetach(true); err != nil {
return nil, "", errors.WithStack(err)
}
serial, err := d.SerialNumber()
if err != nil {
return nil, "", errors.WithStack(err)
}
intf, done, err := d.DefaultInterface()
if err != nil {
return nil, "", errors.WithStack(err)
}
inEp, err := intf.InEndpoint(1)
if err != nil {
done()
return nil, "", errors.WithStack(err)
}
outEp, err := intf.OutEndpoint(2)
if err != nil {
done()
return nil, "", errors.WithStack(err)
}
log.Printf("Opened USB device '%v', serial '%s', packet size %d/%d",
d, serial, inEp.Desc.MaxPacketSize, outEp.Desc.MaxPacketSize)
return usbDevice{
ctx: ctx,
in: inEp,
out: outEp,
dev: d,
done: done,
}, serial, nil
}
type kv struct {
serial string
device io.ReadWriteCloser
}
func openUsbDevices(ctx context.Context) (devices []io.ReadWriteCloser, done func() error, err error) {
usb := gousb.NewContext()
done = usb.Close
devs, err := usb.OpenDevices(func(d *gousb.DeviceDesc) bool {
if d.Vendor == gousb.ID(0x188a) && d.Product == gousb.ID(0x1101) {
return true
}
return false
})
devlist := []kv{}
for i := range devs {
if d, serial, openErr := openUsbDevice(ctx, devs[i]); openErr != nil {
err = openErr
} else {
devlist = append(devlist, kv{
serial: serial,
device: d,
})
}
}
// Ensure order doesn't change
sort.Slice(devlist, func(i, j int) bool {
return devlist[i].serial < devlist[j].serial
})
for _, d := range devlist {
devices = append(devices, d.device)
}
return
}