From 9f04f7bdf0b1949bca4a31f4cc7b17e053857951 Mon Sep 17 00:00:00 2001 From: Sebastian Zagrodzki Date: Sat, 6 May 2017 02:47:24 +0200 Subject: [PATCH] Replace anonymous DeviceDesc field import with a named "Desc" field. --- device.go | 10 +++++----- lsusb/main.go | 2 +- rawread/main.go | 2 +- usb.go | 4 ++-- usb_test.go | 8 ++++---- usbid/describe.go | 4 ++-- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/device.go b/device.go index f1d2fce..1419591 100644 --- a/device.go +++ b/device.go @@ -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, diff --git a/lsusb/main.go b/lsusb/main.go index f48548f..083ac5c 100644 --- a/lsusb/main.go +++ b/lsusb/main.go @@ -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 { diff --git a/rawread/main.go b/rawread/main.go index f5dd050..dedcd5c 100644 --- a/rawread/main.go +++ b/rawread/main.go @@ -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() } diff --git a/usb.go b/usb.go index 12c87d4..e4e9486 100644 --- a/usb.go +++ b/usb.go @@ -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) } diff --git a/usb_test.go b/usb_test.go index f82dd82..59a2f4d 100644 --- a/usb_test.go +++ b/usb_test.go @@ -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() } diff --git a/usbid/describe.go b/usbid/describe.go index e83c73f..a1fb52a 100644 --- a/usbid/describe.go +++ b/usbid/describe.go @@ -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 (