diff --git a/usb/device.go b/usb/device.go index 593b1f8..9daf14a 100644 --- a/usb/device.go +++ b/usb/device.go @@ -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) } diff --git a/usb/device_test.go b/usb/device_test.go index 20f47ea..95fc0c5 100644 --- a/usb/device_test.go +++ b/usb/device_test.go @@ -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) } diff --git a/usb/fakelibusb_test.go b/usb/fakelibusb_test.go index 2aabcac..9bc424c 100644 --- a/usb/fakelibusb_test.go +++ b/usb/fakelibusb_test.go @@ -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{{ diff --git a/usb/libusb.go b/usb/libusb.go index 52f18e1..45b26db 100644 --- a/usb/libusb.go +++ b/usb/libusb.go @@ -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{