rename Descriptor to DeviceDesc
This commit is contained in:
12
device.go
12
device.go
@@ -23,7 +23,7 @@ import (
|
||||
)
|
||||
|
||||
// Descriptor is a representation of a USB device descriptor.
|
||||
type Descriptor struct {
|
||||
type DeviceDesc struct {
|
||||
// Bus information
|
||||
Bus int // The bus on which the device was detected
|
||||
Address int // The address of the device on the bus
|
||||
@@ -46,7 +46,7 @@ type Descriptor struct {
|
||||
}
|
||||
|
||||
// String returns a human-readable version of the device descriptor.
|
||||
func (d *Descriptor) String() string {
|
||||
func (d *DeviceDesc) String() string {
|
||||
return fmt.Sprintf("%d.%d: %s:%s (available configs: %v)", d.Bus, d.Address, d.Vendor, d.Product, d.sortedConfigIds())
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ type Device struct {
|
||||
handle *libusbDevHandle
|
||||
|
||||
// Embed the device information for easy access
|
||||
*Descriptor
|
||||
*DeviceDesc
|
||||
// Timeout for control commands
|
||||
ControlTimeout time.Duration
|
||||
|
||||
@@ -68,7 +68,7 @@ type Device struct {
|
||||
claimed *Config
|
||||
}
|
||||
|
||||
func (d *Descriptor) sortedConfigIds() []int {
|
||||
func (d *DeviceDesc) sortedConfigIds() []int {
|
||||
var cfgs []int
|
||||
for c := range d.Configs {
|
||||
cfgs = append(cfgs, c)
|
||||
@@ -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.Descriptor.Configs[cfgNum]
|
||||
info, ok := d.DeviceDesc.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.Descriptor.sortedConfigIds())
|
||||
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())
|
||||
}
|
||||
cfg := &Config{
|
||||
Info: info,
|
||||
|
@@ -15,11 +15,11 @@
|
||||
package gousb
|
||||
|
||||
// fake devices connected through the fakeLibusb stack.
|
||||
var fakeDevices = []*Descriptor{
|
||||
var fakeDevices = []*DeviceDesc{
|
||||
// Bus 001 Device 001: ID 9999:0001
|
||||
// One config, one interface, one setup,
|
||||
// two endpoints: 0x01 OUT, 0x82 IN.
|
||||
&Descriptor{
|
||||
&DeviceDesc{
|
||||
Bus: 1,
|
||||
Address: 1,
|
||||
Spec: Version(2, 0),
|
||||
@@ -58,7 +58,7 @@ var fakeDevices = []*Descriptor{
|
||||
// One config, two interfaces. interface #0 with no endpoints,
|
||||
// interface #1 with two alt setups with different packet sizes for
|
||||
// endpoints. Two isochronous endpoints, 0x05 OUT and 0x86 OUT.
|
||||
&Descriptor{
|
||||
&DeviceDesc{
|
||||
Bus: 1,
|
||||
Address: 2,
|
||||
Spec: Version(2, 0),
|
||||
|
@@ -22,7 +22,7 @@ import (
|
||||
)
|
||||
|
||||
type fakeDevice struct {
|
||||
desc *Descriptor
|
||||
desc *DeviceDesc
|
||||
alt uint8
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ func (f *fakeLibusb) exit(*libusbContext) {}
|
||||
func (f *fakeLibusb) setDebug(*libusbContext, int) {}
|
||||
|
||||
func (f *fakeLibusb) dereference(d *libusbDevice) {}
|
||||
func (f *fakeLibusb) getDeviceDesc(d *libusbDevice) (*Descriptor, error) {
|
||||
func (f *fakeLibusb) getDeviceDesc(d *libusbDevice) (*DeviceDesc, error) {
|
||||
if dev, ok := f.fakeDevices[d]; ok {
|
||||
return dev.desc, nil
|
||||
}
|
||||
|
10
libusb.go
10
libusb.go
@@ -39,7 +39,7 @@ type libusbTransfer C.struct_libusb_transfer
|
||||
type libusbIso C.struct_libusb_iso_packet_descriptor
|
||||
type libusbEndpoint C.struct_libusb_endpoint_descriptor
|
||||
|
||||
func (ep libusbEndpoint) endpointInfo(dev *Descriptor) EndpointInfo {
|
||||
func (ep libusbEndpoint) endpointInfo(dev *DeviceDesc) EndpointInfo {
|
||||
ei := EndpointInfo{
|
||||
Number: int(ep.bEndpointAddress & endpointNumMask),
|
||||
Direction: EndpointDirection((ep.bEndpointAddress & endpointDirectionMask) != 0),
|
||||
@@ -111,7 +111,7 @@ func (ep libusbEndpoint) endpointInfo(dev *Descriptor) EndpointInfo {
|
||||
// since libusb interacts directly with the host USB stack.
|
||||
//
|
||||
// All functions here should operate on types defined on C.libusb* data types,
|
||||
// and occasionally on convenience data types (like TransferType or Descriptor).
|
||||
// and occasionally on convenience data types (like TransferType or DeviceDesc).
|
||||
type libusbIntf interface {
|
||||
// context
|
||||
init() (*libusbContext, error)
|
||||
@@ -122,7 +122,7 @@ type libusbIntf interface {
|
||||
|
||||
// device
|
||||
dereference(*libusbDevice)
|
||||
getDeviceDesc(*libusbDevice) (*Descriptor, error)
|
||||
getDeviceDesc(*libusbDevice) (*DeviceDesc, error)
|
||||
open(*libusbDevice) (*libusbDevHandle, error)
|
||||
|
||||
close(*libusbDevHandle)
|
||||
@@ -201,7 +201,7 @@ func (libusbImpl) setDebug(c *libusbContext, lvl int) {
|
||||
C.libusb_set_debug((*C.libusb_context)(c), C.int(lvl))
|
||||
}
|
||||
|
||||
func (libusbImpl) getDeviceDesc(d *libusbDevice) (*Descriptor, error) {
|
||||
func (libusbImpl) getDeviceDesc(d *libusbDevice) (*DeviceDesc, error) {
|
||||
var desc C.struct_libusb_device_descriptor
|
||||
if err := fromErrNo(C.libusb_get_device_descriptor((*C.libusb_device)(d), &desc)); err != nil {
|
||||
return nil, err
|
||||
@@ -277,7 +277,7 @@ func (libusbImpl) getDeviceDesc(d *libusbDevice) (*Descriptor, error) {
|
||||
cfgs[c.Config] = c
|
||||
}
|
||||
|
||||
return &Descriptor{
|
||||
return &DeviceDesc{
|
||||
Bus: int(C.libusb_get_bus_number((*C.libusb_device)(d))),
|
||||
Address: int(C.libusb_get_device_address((*C.libusb_device)(d))),
|
||||
Spec: BCD(desc.bcdUSB),
|
||||
|
@@ -40,7 +40,7 @@ func main() {
|
||||
ctx.Debug(*debug)
|
||||
|
||||
// ListDevices is used to find the devices to open.
|
||||
devs, err := ctx.ListDevices(func(desc *gousb.Descriptor) bool {
|
||||
devs, err := ctx.ListDevices(func(desc *gousb.DeviceDesc) bool {
|
||||
// The usbid package can be used to print out human readable information.
|
||||
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))
|
||||
|
@@ -108,7 +108,7 @@ func main() {
|
||||
|
||||
log.Printf("Scanning for device %q...", devName)
|
||||
// ListDevices is used to find the devices to open.
|
||||
devs, err := ctx.ListDevices(func(desc *gousb.Descriptor) bool {
|
||||
devs, err := ctx.ListDevices(func(desc *gousb.DeviceDesc) bool {
|
||||
switch {
|
||||
case vid == desc.Vendor && pid == desc.Product:
|
||||
return true
|
||||
|
6
usb.go
6
usb.go
@@ -149,7 +149,7 @@ func NewContext() *Context {
|
||||
// Every Device returned (whether an error is also returned or not) must be closed.
|
||||
// If there are any errors enumerating the devices,
|
||||
// the final one is returned along with any successfully opened devices.
|
||||
func (c *Context) ListDevices(each func(desc *Descriptor) bool) ([]*Device, error) {
|
||||
func (c *Context) ListDevices(each func(desc *DeviceDesc) bool) ([]*Device, error) {
|
||||
list, err := libusb.getDevices(c.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -171,7 +171,7 @@ func (c *Context) ListDevices(each func(desc *Descriptor) bool) ([]*Device, erro
|
||||
reterr = err
|
||||
continue
|
||||
}
|
||||
ret = append(ret, &Device{handle: handle, Descriptor: desc})
|
||||
ret = append(ret, &Device{handle: handle, DeviceDesc: desc})
|
||||
} else {
|
||||
libusb.dereference(dev)
|
||||
}
|
||||
@@ -187,7 +187,7 @@ func (c *Context) ListDevices(each func(desc *Descriptor) bool) ([]*Device, erro
|
||||
// be called to release the device if the returned device wasn't nil.
|
||||
func (c *Context) OpenDeviceWithVIDPID(vid, pid ID) (*Device, error) {
|
||||
var found bool
|
||||
devs, err := c.ListDevices(func(desc *Descriptor) bool {
|
||||
devs, err := c.ListDevices(func(desc *DeviceDesc) bool {
|
||||
if found {
|
||||
return false
|
||||
}
|
||||
|
12
usb_test.go
12
usb_test.go
@@ -29,8 +29,8 @@ func TestListDevices(t *testing.T) {
|
||||
defer c.Close()
|
||||
c.Debug(0)
|
||||
|
||||
descs := []*Descriptor{}
|
||||
devs, err := c.ListDevices(func(desc *Descriptor) bool {
|
||||
descs := []*DeviceDesc{}
|
||||
devs, err := c.ListDevices(func(desc *DeviceDesc) bool {
|
||||
descs = append(descs, desc)
|
||||
return true
|
||||
})
|
||||
@@ -51,7 +51,7 @@ func TestListDevices(t *testing.T) {
|
||||
}
|
||||
|
||||
for i := range devs {
|
||||
if got, want := devs[i].Descriptor, descs[i]; got != want {
|
||||
if got, want := devs[i].DeviceDesc, descs[i]; got != want {
|
||||
t.Errorf("dev[%d].Descriptor = %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.Descriptor.Vendor != ID(d.vid) || dev.Descriptor.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.Descriptor.Vendor, dev.Descriptor.Product)
|
||||
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)
|
||||
}
|
||||
dev.Close()
|
||||
}
|
||||
@@ -148,7 +148,7 @@ func Example_complex() {
|
||||
|
||||
// Iterate through available Devices, finding all that match a known VID/PID.
|
||||
vid, pid := ID(0x04f2), ID(0xb531)
|
||||
devs, err := ctx.ListDevices(func(desc *Descriptor) bool {
|
||||
devs, err := ctx.ListDevices(func(desc *DeviceDesc) bool {
|
||||
// this function is called for every device present.
|
||||
// Returning true means the device should be opened.
|
||||
return desc.Vendor == vid && desc.Product == pid
|
||||
|
@@ -37,7 +37,7 @@ import (
|
||||
// - *gousb.Descriptor "Product (Vendor)"
|
||||
func Describe(val interface{}) string {
|
||||
switch val := val.(type) {
|
||||
case *gousb.Descriptor:
|
||||
case *gousb.DeviceDesc:
|
||||
if v, ok := Vendors[val.Vendor]; ok {
|
||||
if d, ok := v.Product[val.Product]; ok {
|
||||
return fmt.Sprintf("%s (%s)", d, v)
|
||||
@@ -61,7 +61,7 @@ func Classify(val interface{}) string {
|
||||
proto gousb.Protocol
|
||||
)
|
||||
switch val := val.(type) {
|
||||
case *gousb.Descriptor:
|
||||
case *gousb.DeviceDesc:
|
||||
class, sub, proto = val.Class, val.SubClass, val.Protocol
|
||||
case gousb.InterfaceSetting:
|
||||
class, sub, proto = val.Class, val.SubClass, val.Protocol
|
||||
|
Reference in New Issue
Block a user