Replace anonymous DeviceDesc field import with a named "Desc" field.

This commit is contained in:
Sebastian Zagrodzki
2017-05-06 02:47:24 +02:00
parent 4991f9c89b
commit 9f04f7bdf0
6 changed files with 15 additions and 15 deletions

View File

@@ -22,7 +22,7 @@ import (
"time"
)
// Descriptor is a representation of a USB device descriptor.
// DeviceDesc is a representation of a USB device descriptor.
type DeviceDesc struct {
// Bus information
Bus int // The bus on which the device was detected
@@ -59,7 +59,7 @@ type Device struct {
handle *libusbDevHandle
// Embed the device information for easy access
*DeviceDesc
Desc *DeviceDesc
// Timeout for control commands
ControlTimeout time.Duration
@@ -79,7 +79,7 @@ func (d *DeviceDesc) sortedConfigIds() []int {
// 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)
return fmt.Sprintf("vid=%s,pid=%s,bus=%d,addr=%d", d.Desc.Vendor, d.Desc.Product, d.Desc.Bus, d.Desc.Address)
}
// Reset performs a USB port reset to reinitialize a device.
@@ -117,9 +117,9 @@ 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.DeviceDesc.Configs[cfgNum]
info, ok := d.Desc.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.DeviceDesc.sortedConfigIds())
return nil, fmt.Errorf("configuration id %d not found in the descriptor of the device %s. Available config ids: %v", cfgNum, d, d.Desc.sortedConfigIds())
}
cfg := &Config{
Info: info,

View File

@@ -45,7 +45,7 @@ func main() {
fmt.Printf("%03d.%03d %s:%s %s\n", desc.Bus, desc.Address, desc.Vendor, desc.Product, usbid.Describe(desc))
fmt.Printf(" Protocol: %s\n", usbid.Classify(desc))
// The configurations can be examined from the Descriptor, though they can only
// The configurations can be examined from the DeviceDesc, though they can only
// be set once the device is opened. All configuration references must be closed,
// to free up the memory in libusb.
for _, cfg := range desc.Configs {

View File

@@ -132,7 +132,7 @@ func main() {
case len(devs) == 0:
log.Fatal("No matching devices found.")
case len(devs) > 1:
log.Printf("Warning: multiple devices found. Using bus %d, addr %d.", devs[0].Bus, devs[0].Address)
log.Printf("Warning: multiple devices found. Using bus %d, addr %d.", devs[0].Desc.Bus, devs[0].Desc.Address)
for _, d := range devs[1:] {
d.Close()
}

4
usb.go
View File

@@ -28,7 +28,7 @@ initialized by the host stack, it's possible to retrieve it's descriptor
(the device descriptor). It contains elements such as product and vendor IDs,
bus number and device number (address) on the bus.
In gousb Device struct represents the USB device, and Device.Descriptor
In gousb Device struct represents the USB device, and Device.Desc
contains all the information known about the device.
Among other information in the device descriptor is a list of configuration
@@ -171,7 +171,7 @@ func (c *Context) ListDevices(each func(desc *DeviceDesc) bool) ([]*Device, erro
reterr = err
continue
}
ret = append(ret, &Device{handle: handle, DeviceDesc: desc})
ret = append(ret, &Device{handle: handle, Desc: desc})
} else {
libusb.dereference(dev)
}

View File

@@ -51,8 +51,8 @@ func TestListDevices(t *testing.T) {
}
for i := range devs {
if got, want := devs[i].DeviceDesc, descs[i]; got != want {
t.Errorf("dev[%d].Descriptor = %p, want %p", i, got, want)
if got, want := devs[i].Desc, descs[i]; got != want {
t.Errorf("dev[%d].Desc = %p, want %p", i, got, want)
}
}
}
@@ -81,8 +81,8 @@ func TestOpenDeviceWithVIDPID(t *testing.T) {
t.Errorf("OpenDeviceWithVIDPID(%s/%s): got error %v, want nil", ID(d.vid), ID(d.pid), err)
}
if dev != nil {
if dev.DeviceDesc.Vendor != ID(d.vid) || dev.DeviceDesc.Product != ID(d.pid) {
t.Errorf("OpenDeviceWithVIDPID(%s/%s): the device returned has VID/PID %s/%s, different from specified in the arguments", ID(d.vid), ID(d.pid), dev.DeviceDesc.Vendor, dev.DeviceDesc.Product)
if dev.Desc.Vendor != ID(d.vid) || dev.Desc.Product != ID(d.pid) {
t.Errorf("OpenDeviceWithVIDPID(%s/%s): the device returned has VID/PID %s/%s, different from specified in the arguments", ID(d.vid), ID(d.pid), dev.Desc.Vendor, dev.Desc.Product)
}
dev.Close()
}

View File

@@ -34,7 +34,7 @@ import (
// of the given device.
//
// The given val must be one of the following:
// - *gousb.Descriptor "Product (Vendor)"
// - *gousb.DeviceDesc "Product (Vendor)"
func Describe(val interface{}) string {
switch val := val.(type) {
case *gousb.DeviceDesc:
@@ -53,7 +53,7 @@ func Describe(val interface{}) string {
// and protocol associated with a device or interface.
//
// The given val must be one of the following:
// - *gousb.Descriptor "Class (SubClass) Protocol"
// - *gousb.DeviceDesc "Class (SubClass) Protocol"
// - gousb.InterfaceSetup "IfClass (IfSubClass) IfProtocol"
func Classify(val interface{}) string {
var (