Replace descriptor Configs with a map - config ids are arbitrary numbers
(unlike interface or alt setting numbers, which are 0-based array indices). This makes it easier to access a particular config without having to iterate over the descriptors.
This commit is contained in:
@@ -17,6 +17,7 @@ package usb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -40,16 +41,12 @@ type Descriptor struct {
|
||||
Protocol Protocol // The protocol (within the sub-class) of this device
|
||||
|
||||
// Configuration information
|
||||
Configs []ConfigInfo
|
||||
Configs map[int]ConfigInfo
|
||||
}
|
||||
|
||||
// String returns a human-readable version of the device descriptor.
|
||||
func (d *Descriptor) String() string {
|
||||
var cfgs []int
|
||||
for _, c := range d.Configs {
|
||||
cfgs = append(cfgs, c.Config)
|
||||
}
|
||||
return fmt.Sprintf("%d.%d: %s:%s (available configs: %v)", d.Bus, d.Address, d.Vendor, d.Product, cfgs)
|
||||
return fmt.Sprintf("%d.%d: %s:%s (available configs: %v)", d.Bus, d.Address, d.Vendor, d.Product, d.sortedConfigIds())
|
||||
}
|
||||
|
||||
// Device represents an opened USB device.
|
||||
@@ -64,6 +61,15 @@ type Device struct {
|
||||
claimed *Config
|
||||
}
|
||||
|
||||
func (d *Descriptor) sortedConfigIds() []int {
|
||||
var cfgs []int
|
||||
for c := range d.Configs {
|
||||
cfgs = append(cfgs, c)
|
||||
}
|
||||
sort.Ints(cfgs)
|
||||
return cfgs
|
||||
}
|
||||
|
||||
// String represents a human readable representation of the device.
|
||||
func (d *Device) String() string {
|
||||
return fmt.Sprintf("vid=%s,pid=%s,bus=%d,addr=%d", d.Vendor, d.Product, d.Bus, d.Address)
|
||||
@@ -101,25 +107,15 @@ func (d *Device) Config(cfgNum int) (*Config, error) {
|
||||
if d.handle == nil {
|
||||
return nil, fmt.Errorf("Config(%d) called on %s after Close", cfgNum, d)
|
||||
}
|
||||
info, ok := d.Descriptor.Configs[cfgNum]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("configuration id %d not found in the descriptor of the device %s. Available config ids: %v", cfgNum, d, d.Descriptor.sortedConfigIds())
|
||||
}
|
||||
cfg := &Config{
|
||||
Info: info,
|
||||
dev: d,
|
||||
claimed: make(map[int]bool),
|
||||
}
|
||||
var found bool
|
||||
for _, info := range d.Descriptor.Configs {
|
||||
if info.Config == cfgNum {
|
||||
found = true
|
||||
cfg.Info = info
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
var cfgs []int
|
||||
for _, c := range d.Descriptor.Configs {
|
||||
cfgs = append(cfgs, c.Config)
|
||||
}
|
||||
return nil, fmt.Errorf("configuration id %d not found in the descriptor of the device %s. Available config ids: %v", cfgNum, d, cfgs)
|
||||
}
|
||||
if err := libusb.setConfig(d.handle, uint8(cfgNum)); err != nil {
|
||||
return nil, fmt.Errorf("failed to set active config %d for the device %s: %v", cfgNum, d, err)
|
||||
}
|
||||
|
@@ -47,7 +47,7 @@ func TestOpenEndpoint(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("%s.InEndpoint(6): got error %v, want nil", intf, err)
|
||||
}
|
||||
if want := fakeDevices[1].Configs[0].Interfaces[1].AltSettings[1].Endpoints[1]; !reflect.DeepEqual(got.Info, want) {
|
||||
if want := fakeDevices[1].Configs[1].Interfaces[1].AltSettings[1].Endpoints[1]; !reflect.DeepEqual(got.Info, want) {
|
||||
t.Errorf("%s.InEndpoint(6): got %+v, want %+v", intf, got, want)
|
||||
}
|
||||
|
||||
|
@@ -35,7 +35,7 @@ var (
|
||||
Vendor: ID(0x9999),
|
||||
Product: ID(0x0001),
|
||||
Protocol: 255,
|
||||
Configs: []ConfigInfo{{
|
||||
Configs: map[int]ConfigInfo{1: {
|
||||
Config: 1,
|
||||
MaxPower: Milliamperes(100),
|
||||
Interfaces: []InterfaceInfo{{
|
||||
@@ -71,7 +71,7 @@ var (
|
||||
Vendor: ID(0x8888),
|
||||
Product: ID(0x0002),
|
||||
Protocol: 255,
|
||||
Configs: []ConfigInfo{{
|
||||
Configs: map[int]ConfigInfo{1: {
|
||||
Config: 1,
|
||||
MaxPower: Milliamperes(100),
|
||||
Interfaces: []InterfaceInfo{{
|
||||
|
@@ -207,7 +207,7 @@ func (libusbImpl) getDeviceDesc(d *libusbDevice) (*Descriptor, error) {
|
||||
return nil, err
|
||||
}
|
||||
// Enumerate configurations
|
||||
var cfgs []ConfigInfo
|
||||
cfgs := make(map[int]ConfigInfo)
|
||||
for i := 0; i < int(desc.bNumConfigurations); i++ {
|
||||
var cfg *C.struct_libusb_config_descriptor
|
||||
if err := fromErrNo(C.libusb_get_config_descriptor((*C.libusb_device)(d), C.uint8_t(i), &cfg)); err != nil {
|
||||
@@ -273,7 +273,7 @@ func (libusbImpl) getDeviceDesc(d *libusbDevice) (*Descriptor, error) {
|
||||
})
|
||||
}
|
||||
C.libusb_free_config_descriptor(cfg)
|
||||
cfgs = append(cfgs, c)
|
||||
cfgs[c.Config] = c
|
||||
}
|
||||
|
||||
return &Descriptor{
|
||||
|
Reference in New Issue
Block a user