/
ids.go
131 lines (114 loc) · 2.76 KB
/
ids.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package usb
import (
"bufio"
"bytes"
"encoding/binary"
"encoding/hex"
"io"
"io/ioutil"
"os"
"strings"
"github.com/apex/log"
)
var vmap vMap
type vMap map[uint16]_vendor
type _vendor struct {
name string
products map[uint16]string
}
func createIDMap() vMap {
m := vMap{}
idbuf := make([]byte, 2)
lastVID := uint16(0)
usbids := usbIDs()
defer usbids.Close()
scanner := bufio.NewScanner(usbids)
SCANNER:
for scanner.Scan() {
l := scanner.Bytes()
if len(l) < 1 {
continue
}
if l[0] == '#' {
continue
}
switch {
case bytes.Equal(l[:2], []byte("C ")): //@todo
//@todo
break SCANNER
case bytes.Equal(l[:2], []byte("R ")): //@todo
case bytes.Equal(l[:2], []byte("L ")): //@todo
case bytes.Equal(l[:3], []byte("AT ")): //@todo
case bytes.Equal(l[:3], []byte("VT ")): //@todo
case bytes.Equal(l[:4], []byte("HID ")): //@todo
case bytes.Equal(l[:4], []byte("HCC ")): //@todo
case bytes.Equal(l[:4], []byte("PHY ")): //@todo
case bytes.Equal(l[:4], []byte("HUT ")): //@todo
case bytes.Equal(l[:5], []byte("BIAS ")): //@todo
case l[0] == '\t' && l[1] != '\t':
if lastVID == 0 {
continue
}
if _, err := hex.Decode(idbuf, l[1:5]); err != nil {
continue
}
m[lastVID].products[binary.BigEndian.Uint16(idbuf)] = strings.TrimSpace(string(l[5:]))
//single sub-item
case l[0] == '\t' && l[1] == '\t':
// sub-sub-item
default:
//vendor ID
if _, err := hex.Decode(idbuf, l[:4]); err != nil {
log.WithField("line", string(l)).Info("failed parsing line in usb.ids")
continue
}
vid := binary.BigEndian.Uint16(idbuf)
m[vid] = _vendor{
name: strings.TrimSpace(string(l[4:])),
products: make(map[uint16]string),
}
lastVID = vid
}
}
return m
}
func vendorName(id uint16) string {
if vmap == nil {
vmap = createIDMap()
}
if v, exists := vmap[id]; exists {
return v.name
}
return ""
}
func productName(vid uint16, pid uint16) string {
if vmap == nil {
vmap = createIDMap()
}
if v, exists := vmap[vid]; exists {
if p, ex := v.products[pid]; ex {
return p
}
}
return ""
}
func usbIDs() (r io.ReadCloser) {
idPaths := []string{"/usr/share/hwdata/usb.ids", "/usr/share/usb.ids", "/usr/share/libosinfo/usb.ids", "/usr/share/kcmusb/usb.ids", "/var/lib/usbutils/usb.ids"}
for i := range idPaths {
log.WithField("path", idPaths[i]).Debug("checking for usb.ids at")
if f, err := os.OpenFile(idPaths[i], os.O_RDONLY, 0644); err != nil {
continue
} else {
log.Debug("success")
r = f
break
}
}
if r == nil {
// must not have found any usable files in the above paths
// use the version we shipped with
log.Debug("using built-in usb.ids")
r = ioutil.NopCloser(shippedUsbIds()) // turns the reader into a ReadCloser where Close is a no-op
}
return r
}