From eb5500f387b47cc41a291ce1ad065153703fffb3 Mon Sep 17 00:00:00 2001 From: Sebastian Zagrodzki Date: Sat, 6 May 2017 02:16:22 +0200 Subject: [PATCH] Move Control from Config to Device. Rename Default to DefaultInterface. --- config.go | 12 +----------- device.go | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/config.go b/config.go index 35d9a04..fa0a786 100644 --- a/config.go +++ b/config.go @@ -18,7 +18,6 @@ package gousb import ( "fmt" "sync" - "time" ) // ConfigInfo contains the information about a USB device configuration. @@ -45,8 +44,7 @@ func (c ConfigInfo) String() string { // Config represents a USB device set to use a particular configuration. // Only one Config of a particular device can be used at any one time. type Config struct { - Info ConfigInfo - ControlTimeout time.Duration + Info ConfigInfo dev *Device @@ -81,14 +79,6 @@ func (c *Config) String() string { return fmt.Sprintf("%s,config=%d", c.dev.String(), c.Info.Config) } -// Control sends a control request to the device. -func (c *Config) Control(rType, request uint8, val, idx uint16, data []byte) (int, error) { - if c.dev == nil { - return 0, fmt.Errorf("Control() called on %s after Close", c) - } - return libusb.control(c.dev.handle, c.ControlTimeout, rType, request, val, idx, data) -} - // Interface claims and returns an interface on a USB device. func (c *Config) Interface(intf, alt int) (*Interface, error) { if c.dev == nil { diff --git a/device.go b/device.go index b6fbe4e..077d226 100644 --- a/device.go +++ b/device.go @@ -19,6 +19,7 @@ import ( "fmt" "sort" "sync" + "time" ) // Descriptor is a representation of a USB device descriptor. @@ -55,6 +56,8 @@ type Device struct { // Embed the device information for easy access *Descriptor + // Timeout for control commands + ControlTimeout time.Duration // Claimed config mu sync.Mutex @@ -127,10 +130,10 @@ func (d *Device) Config(cfgNum int) (*Config, error) { return cfg, nil } -// Default opens interface #0 with alternate setting #0 of the currently active +// DefaultInterface 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) { +func (d *Device) DefaultInterface() (*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) @@ -146,6 +149,14 @@ func (d *Device) Default() (*Interface, error) { return intf, nil } +// Control sends a control request to the device. +func (d *Device) Control(rType, request uint8, val, idx uint16, data []byte) (int, error) { + if d.handle == nil { + return 0, fmt.Errorf("Control() called on %s after Close", d) + } + return libusb.control(d.handle, d.ControlTimeout, rType, request, val, idx, data) +} + // Close closes the device. func (d *Device) Close() error { if d.handle == nil {