-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
60 lines (50 loc) · 1.19 KB
/
utils.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
package evc2
import (
"github.com/google/gousb"
)
func GetEVCs() (evcs []*EVC2, err error) {
evcs, err = getEVCs(Vid, Pid)
return
}
func getEVCs(vid, pid gousb.ID) (evcs []*EVC2, err error) {
ctx := gousb.NewContext()
devs, err := ctx.OpenDevices(func(desc *gousb.DeviceDesc) bool {
return desc.Vendor == vid && desc.Product == pid
})
for i, device := range devs {
evcs = append(evcs, &EVC2{
context: ctx,
device: device,
})
for num := range device.Desc.Configs {
config, err := device.Config(num)
if err != nil {
continue
}
defer config.Close()
for _, ifacedesc := range config.Desc.Interfaces {
iface, err := config.Interface(ifacedesc.Number, 0)
if err != nil {
continue
}
evcs[i].iface = iface
for _, endpointDesc := range iface.Setting.Endpoints {
switch endpointDesc.Direction {
case gousb.EndpointDirectionIn:
evcs[i].inpipe, err = iface.InEndpoint(endpointDesc.Number)
if err != nil {
continue
}
break
case gousb.EndpointDirectionOut:
evcs[i].outpipe, err = iface.OutEndpoint(endpointDesc.Number)
if err != nil {
continue
}
}
}
}
}
}
return
}