Move Control from Config to Device.

Rename Default to DefaultInterface.
This commit is contained in:
Sebastian Zagrodzki
2017-05-06 02:16:22 +02:00
parent 1c9a11b1ce
commit eb5500f387
2 changed files with 14 additions and 13 deletions

View File

@@ -18,7 +18,6 @@ package gousb
import ( import (
"fmt" "fmt"
"sync" "sync"
"time"
) )
// ConfigInfo contains the information about a USB device configuration. // 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. // 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. // Only one Config of a particular device can be used at any one time.
type Config struct { type Config struct {
Info ConfigInfo Info ConfigInfo
ControlTimeout time.Duration
dev *Device dev *Device
@@ -81,14 +79,6 @@ func (c *Config) String() string {
return fmt.Sprintf("%s,config=%d", c.dev.String(), c.Info.Config) 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. // Interface claims and returns an interface on a USB device.
func (c *Config) Interface(intf, alt int) (*Interface, error) { func (c *Config) Interface(intf, alt int) (*Interface, error) {
if c.dev == nil { if c.dev == nil {

View File

@@ -19,6 +19,7 @@ import (
"fmt" "fmt"
"sort" "sort"
"sync" "sync"
"time"
) )
// Descriptor is a representation of a USB device descriptor. // Descriptor is a representation of a USB device descriptor.
@@ -55,6 +56,8 @@ type Device struct {
// Embed the device information for easy access // Embed the device information for easy access
*Descriptor *Descriptor
// Timeout for control commands
ControlTimeout time.Duration
// Claimed config // Claimed config
mu sync.Mutex mu sync.Mutex
@@ -127,10 +130,10 @@ func (d *Device) Config(cfgNum int) (*Config, error) {
return cfg, nil 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 // config. It's intended as a shortcut for devices that have the simplest
// interface of a single config, interface and alternate setting. // 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() cfgNum, err := d.ActiveConfigNum()
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to get active config number of device %s: %v", d, err) 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 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. // Close closes the device.
func (d *Device) Close() error { func (d *Device) Close() error {
if d.handle == nil { if d.handle == nil {