First cut
This commit is contained in:
41
usbid/load.go
Normal file
41
usbid/load.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package usbid
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"log"
|
||||
|
||||
"github.com/kylelemons/gousb/usb"
|
||||
)
|
||||
|
||||
const (
|
||||
LinuxUsbDotOrg = "http://www.linux-usb.org/usb.ids"
|
||||
)
|
||||
|
||||
var Vendors map[usb.ID]*Vendor
|
||||
|
||||
func LoadFromURL(url string) error {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
ids, err := ParseIDs(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Vendors = ids
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
ids, err := ParseIDs(strings.NewReader(usbIdListData))
|
||||
if err != nil {
|
||||
log.Printf("usbid: failed to parse: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
Vendors = ids
|
||||
}
|
16998
usbid/load_data.go
Normal file
16998
usbid/load_data.go
Normal file
File diff suppressed because it is too large
Load Diff
162
usbid/parse.go
Normal file
162
usbid/parse.go
Normal file
@@ -0,0 +1,162 @@
|
||||
package usbid
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/kylelemons/gousb/usb"
|
||||
)
|
||||
|
||||
type Vendor struct {
|
||||
Name string
|
||||
Devices map[usb.ID]*Device
|
||||
}
|
||||
|
||||
func (v Vendor) String() string {
|
||||
return v.Name
|
||||
}
|
||||
|
||||
// TODO(kevlar) s/device/
|
||||
type Device struct {
|
||||
Name string
|
||||
Interfaces map[usb.ID]string
|
||||
}
|
||||
|
||||
func (d Device) String() string {
|
||||
return d.Name
|
||||
}
|
||||
|
||||
/*
|
||||
type Class struct {
|
||||
Name string
|
||||
SubClass map[usb.ID]*SubClass
|
||||
}
|
||||
|
||||
type SubClass struct {
|
||||
Name string
|
||||
Protocol map[usb.ID]string
|
||||
}
|
||||
*/
|
||||
|
||||
func ParseIDs(r io.Reader) (map[usb.ID]*Vendor, error) {
|
||||
vendors := make(map[usb.ID]*Vendor, 2800)
|
||||
|
||||
split := func(s string) (kind string, level int, id usb.ID, name string, err error) {
|
||||
pieces := strings.SplitN(s, " ", 2)
|
||||
if len(pieces) != 2 {
|
||||
err = fmt.Errorf("malformatted line %q", s)
|
||||
return
|
||||
}
|
||||
|
||||
// Save the name
|
||||
name = pieces[1]
|
||||
|
||||
// Parse out the level
|
||||
for len(pieces[0]) > 0 && pieces[0][0] == '\t' {
|
||||
level, pieces[0] = level+1, pieces[0][1:]
|
||||
}
|
||||
|
||||
// Parse the first piece to see if it has a kind
|
||||
first := strings.SplitN(pieces[0], " ", 2)
|
||||
if len(first) == 2 {
|
||||
kind, pieces[0] = first[0], first[1]
|
||||
}
|
||||
|
||||
// Parse the ID
|
||||
i, err := strconv.ParseUint(pieces[0], 16, 16)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("malformatted id %q: %s", pieces[0], err)
|
||||
return
|
||||
}
|
||||
id = usb.ID(i)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Hold the interim values
|
||||
var vendor *Vendor
|
||||
var device *Device
|
||||
|
||||
parseVendor := func(level int, id usb.ID, name string) error {
|
||||
switch level {
|
||||
case 0:
|
||||
vendor = &Vendor{
|
||||
Name: name,
|
||||
}
|
||||
vendors[id] = vendor
|
||||
|
||||
case 1:
|
||||
if vendor == nil {
|
||||
return fmt.Errorf("product line without vendor line")
|
||||
}
|
||||
|
||||
device = &Device{
|
||||
Name: name,
|
||||
}
|
||||
if vendor.Devices == nil {
|
||||
vendor.Devices = make(map[usb.ID]*Device)
|
||||
}
|
||||
vendor.Devices[id] = device
|
||||
|
||||
case 2:
|
||||
if device == nil {
|
||||
return fmt.Errorf("interface line without device line")
|
||||
}
|
||||
|
||||
if device.Interfaces == nil {
|
||||
device.Interfaces = make(map[usb.ID]string)
|
||||
}
|
||||
device.Interfaces[id] = name
|
||||
|
||||
default:
|
||||
return fmt.Errorf("too many levels of nesting for vendor block")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO(kevlar): Parse class information, etc
|
||||
//var class *Class
|
||||
//var subclass *SubClass
|
||||
|
||||
var kind string
|
||||
|
||||
lines := bufio.NewReaderSize(r, 512)
|
||||
parseLines:
|
||||
for lineno := 0; ; lineno++ {
|
||||
b, isPrefix, err := lines.ReadLine()
|
||||
switch {
|
||||
case err == io.EOF:
|
||||
break parseLines
|
||||
case err != nil:
|
||||
return nil, err
|
||||
case isPrefix:
|
||||
return nil, fmt.Errorf("line %d: line too long", lineno)
|
||||
}
|
||||
line := string(b)
|
||||
|
||||
if len(line) == 0 || line[0] == '#' {
|
||||
continue
|
||||
}
|
||||
|
||||
k, level, id, name, err := split(line)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("line %d: %s", lineno, err)
|
||||
}
|
||||
if k != "" {
|
||||
kind = k
|
||||
}
|
||||
|
||||
switch kind {
|
||||
case "": err = parseVendor(level, id, name)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("line %d: %s", lineno, err)
|
||||
}
|
||||
}
|
||||
|
||||
return vendors, nil
|
||||
}
|
88
usbid/parse_test.go
Normal file
88
usbid/parse_test.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package usbid
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/kylelemons/gousb/usb"
|
||||
)
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
tests := []struct {
|
||||
Input string
|
||||
Output map[usb.ID]*Vendor
|
||||
}{{
|
||||
Input: `
|
||||
# Skip comment
|
||||
abcd Vendor One
|
||||
0123 Product One
|
||||
0124 Product Two
|
||||
efef Vendor Two
|
||||
0aba Product
|
||||
12 Interface One
|
||||
24 Interface Two
|
||||
0abb Product
|
||||
12 Interface
|
||||
|
||||
C 00 (Defined at Interface level)
|
||||
C 01 Audio
|
||||
01 Control Device
|
||||
02 Streaming
|
||||
03 MIDI Streaming
|
||||
C 02 Communications
|
||||
01 Direct Line
|
||||
02 Abstract (modem)
|
||||
00 None
|
||||
01 AT-commands (v.25ter)
|
||||
02 AT-commands (PCCA101)
|
||||
03 AT-commands (PCCA101 + wakeup)
|
||||
04 AT-commands (GSM)
|
||||
05 AT-commands (3G)
|
||||
06 AT-commands (CDMA)
|
||||
fe Defined by command set descriptor
|
||||
ff Vendor Specific (MSFT RNDIS?)
|
||||
`,
|
||||
Output: map[usb.ID]*Vendor{
|
||||
0xabcd: {
|
||||
Name: "Vendor One",
|
||||
Devices: map[usb.ID]*Device{
|
||||
0x0123: {Name: "Product One"},
|
||||
0x0124: {Name: "Product Two"},
|
||||
},
|
||||
},
|
||||
0xefef: {
|
||||
Name: "Vendor Two",
|
||||
Devices: map[usb.ID]*Device{
|
||||
0x0aba: {
|
||||
Name: "Product",
|
||||
Interfaces: map[usb.ID]string{
|
||||
0x12: "Interface One",
|
||||
0x24: "Interface Two",
|
||||
},
|
||||
},
|
||||
0x0abb: {
|
||||
Name: "Product",
|
||||
Interfaces: map[usb.ID]string{
|
||||
0x12: "Interface",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}}
|
||||
|
||||
for idx, test := range tests {
|
||||
vendors, err := ParseIDs(strings.NewReader(test.Input))
|
||||
if err != nil {
|
||||
t.Errorf("%d. ParseIDs: %s", idx, err)
|
||||
continue
|
||||
}
|
||||
if got, want := vendors, test.Output; !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("%d. Parse mismatch", idx)
|
||||
t.Errorf(" - got: %v", idx, got)
|
||||
t.Errorf(" - want: %v", idx, want)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user