From 9623c1ba8d7f03d6e9ce83b40a1abb3da809bd2f Mon Sep 17 00:00:00 2001 From: Sebastian Zagrodzki Date: Sat, 6 May 2017 00:20:43 +0200 Subject: [PATCH] replace ActiveConfig with ActiveConfigNum. Add a Default() helper that simplifies setup for uncomplicated USB devices. --- device.go | 33 +++++++++++++++++++++++++++------ device_test.go | 6 +++--- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/device.go b/device.go index 203371c..2dd1283 100644 --- a/device.go +++ b/device.go @@ -88,9 +88,10 @@ func (d *Device) Reset() error { return libusb.reset(d.handle) } -// ActiveConfig returns the config id (not the index) of the active configuration. -// This corresponds to the ConfigInfo.Config field. -func (d *Device) ActiveConfig() (int, error) { +// ActiveConfigNum returns the config id of the active configuration. +// The value corresponds to the ConfigInfo.Config field of one of the +// ConfigInfos of this Device. +func (d *Device) ActiveConfigNum() (int, error) { if d.handle == nil { return 0, fmt.Errorf("ActiveConfig() called on %s after Close", d) } @@ -99,10 +100,11 @@ func (d *Device) ActiveConfig() (int, error) { } // Config returns a USB device set to use a particular config. -// The cfg provided is the config id (not the index) of the configuration to set, -// which corresponds to the ConfigInfo.Config field. +// The cfgNum provided is the config id (not the index) of the configuration to +// set, which corresponds to the ConfigInfo.Config field. // USB supports only one active config per device at a time. Config claims the -// device before setting the desired config and keeps it locked until Close is called. +// device before setting the desired config and keeps it locked until Close is +// called. 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) @@ -125,6 +127,25 @@ func (d *Device) Config(cfgNum int) (*Config, error) { return cfg, nil } +// Default opens interface #0 with alternate setting #0 of the currently active +// config. It's intended as a shortcut for devices that have the simplest +// interface of a single config, interface and alternate setting. +func (d *Device) Default() (*Interface, error) { + cfgNum, err := d.ActiveConfigNum() + if err != nil { + return nil, fmt.Errorf("failed to get active config number of device %s: %v", d, err) + } + cfg, err := d.Config(cfgNum) + if err != nil { + return nil, fmt.Errorf("failed to claim config %d of device %s: %v", cfgNum, d, err) + } + intf, err := cfg.Interface(0, 0) + if err != nil { + return nil, fmt.Errorf("failed to select interface #%d alternate setting %d of config %d of device %s: %v", 0, 0, cfgNum, d, err) + } + return intf, nil +} + // Close closes the device. func (d *Device) Close() error { if d.handle == nil { diff --git a/device_test.go b/device_test.go index a157dfd..a6b9777 100644 --- a/device_test.go +++ b/device_test.go @@ -47,10 +47,10 @@ func TestClaimAndRelease(t *testing.T) { t.Fatalf("%s.Config(1): %v", dev, err) } defer cfg.Close() - if got, err := dev.ActiveConfig(); err != nil { - t.Errorf("%s.ActiveConfig(): got error %v, want nil", dev, err) + if got, err := dev.ActiveConfigNum(); err != nil { + t.Errorf("%s.ActiveConfigNum(): got error %v, want nil", dev, err) } else if got != cfgNum { - t.Errorf("%s.ActiveConfig(): got %d, want %d", dev, got, cfgNum) + t.Errorf("%s.ActiveConfigNum(): got %d, want %d", dev, got, cfgNum) } intf, err := cfg.Interface(if1Num, alt1Num)