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:
Sebastian Zagrodzki
2017-04-29 11:56:49 +02:00
parent 60d0aba507
commit 77c176cd4c
4 changed files with 22 additions and 26 deletions

View File

@@ -17,6 +17,7 @@ package usb
import ( import (
"fmt" "fmt"
"sort"
"sync" "sync"
) )
@@ -40,16 +41,12 @@ type Descriptor struct {
Protocol Protocol // The protocol (within the sub-class) of this device Protocol Protocol // The protocol (within the sub-class) of this device
// Configuration information // Configuration information
Configs []ConfigInfo Configs map[int]ConfigInfo
} }
// String returns a human-readable version of the device descriptor. // String returns a human-readable version of the device descriptor.
func (d *Descriptor) String() string { func (d *Descriptor) String() string {
var cfgs []int return fmt.Sprintf("%d.%d: %s:%s (available configs: %v)", d.Bus, d.Address, d.Vendor, d.Product, d.sortedConfigIds())
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)
} }
// Device represents an opened USB device. // Device represents an opened USB device.
@@ -64,6 +61,15 @@ type Device struct {
claimed *Config 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. // String represents a human readable representation of the device.
func (d *Device) String() string { func (d *Device) String() string {
return fmt.Sprintf("vid=%s,pid=%s,bus=%d,addr=%d", d.Vendor, d.Product, d.Bus, d.Address) 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 { if d.handle == nil {
return nil, fmt.Errorf("Config(%d) called on %s after Close", cfgNum, d) 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{ cfg := &Config{
Info: info,
dev: d, dev: d,
claimed: make(map[int]bool), 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 { 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) return nil, fmt.Errorf("failed to set active config %d for the device %s: %v", cfgNum, d, err)
} }

View File

@@ -47,7 +47,7 @@ func TestOpenEndpoint(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("%s.InEndpoint(6): got error %v, want nil", intf, err) 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) t.Errorf("%s.InEndpoint(6): got %+v, want %+v", intf, got, want)
} }

View File

@@ -35,7 +35,7 @@ var (
Vendor: ID(0x9999), Vendor: ID(0x9999),
Product: ID(0x0001), Product: ID(0x0001),
Protocol: 255, Protocol: 255,
Configs: []ConfigInfo{{ Configs: map[int]ConfigInfo{1: {
Config: 1, Config: 1,
MaxPower: Milliamperes(100), MaxPower: Milliamperes(100),
Interfaces: []InterfaceInfo{{ Interfaces: []InterfaceInfo{{
@@ -71,7 +71,7 @@ var (
Vendor: ID(0x8888), Vendor: ID(0x8888),
Product: ID(0x0002), Product: ID(0x0002),
Protocol: 255, Protocol: 255,
Configs: []ConfigInfo{{ Configs: map[int]ConfigInfo{1: {
Config: 1, Config: 1,
MaxPower: Milliamperes(100), MaxPower: Milliamperes(100),
Interfaces: []InterfaceInfo{{ Interfaces: []InterfaceInfo{{

View File

@@ -207,7 +207,7 @@ func (libusbImpl) getDeviceDesc(d *libusbDevice) (*Descriptor, error) {
return nil, err return nil, err
} }
// Enumerate configurations // Enumerate configurations
var cfgs []ConfigInfo cfgs := make(map[int]ConfigInfo)
for i := 0; i < int(desc.bNumConfigurations); i++ { for i := 0; i < int(desc.bNumConfigurations); i++ {
var cfg *C.struct_libusb_config_descriptor 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 { 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) C.libusb_free_config_descriptor(cfg)
cfgs = append(cfgs, c) cfgs[c.Config] = c
} }
return &Descriptor{ return &Descriptor{