Rename ListDevices to OpenDevices. Add more doc for RemoteWakeup.

This commit is contained in:
Sebastian Zagrodzki
2017-06-13 10:55:30 +02:00
parent e1fd376a2e
commit 67ddfbabc2
4 changed files with 17 additions and 12 deletions

View File

@@ -28,7 +28,12 @@ type ConfigDesc struct {
// SelfPowered is true if the device is powered externally, i.e. not // SelfPowered is true if the device is powered externally, i.e. not
// drawing power from the USB bus. // drawing power from the USB bus.
SelfPowered bool SelfPowered bool
// RemoteWakeup is true if the device supports remote wakeup. // RemoteWakeup is true if the device supports remote wakeup, i.e.
// an external signal that will wake up a suspended USB device. An example
// might be a keyboard that can wake up through a keypress after
// the host put it in suspend mode. Note that gousb does not support
// device power management, RemoteWakeup only refers to the reported device
// capability.
RemoteWakeup bool RemoteWakeup bool
// MaxPower is the maximum current the device draws from the USB bus // MaxPower is the maximum current the device draws from the USB bus
// in this configuration. // in this configuration.
@@ -89,7 +94,7 @@ func (c *Config) Interface(num, alt int) (*Interface, error) {
if c.dev == nil { if c.dev == nil {
return nil, fmt.Errorf("Interface(%d, %d) called on %s after Close", num, alt, c) return nil, fmt.Errorf("Interface(%d, %d) called on %s after Close", num, alt, c)
} }
if num < 0 || intNum >= len(c.Desc.Interfaces) { if num < 0 || num >= len(c.Desc.Interfaces) {
return nil, fmt.Errorf("interface %d not found in %s, available interfaces 0..%d", num, c, len(c.Desc.Interfaces)-1) return nil, fmt.Errorf("interface %d not found in %s, available interfaces 0..%d", num, c, len(c.Desc.Interfaces)-1)
} }
ifInfo := c.Desc.Interfaces[num] ifInfo := c.Desc.Interfaces[num]

View File

@@ -80,7 +80,7 @@ func Example_complex() {
// Iterate through available Devices, finding all that match a known VID/PID. // Iterate through available Devices, finding all that match a known VID/PID.
vid, pid := gousb.ID(0x04f2), gousb.ID(0xb531) vid, pid := gousb.ID(0x04f2), gousb.ID(0xb531)
devs, err := ctx.ListDevices(func(desc *gousb.DeviceDesc) bool { devs, err := ctx.OpenDevices(func(desc *gousb.DeviceDesc) bool {
// this function is called for every device present. // this function is called for every device present.
// Returning true means the device should be opened. // Returning true means the device should be opened.
return desc.Vendor == vid && desc.Product == pid return desc.Vendor == vid && desc.Product == pid
@@ -90,7 +90,7 @@ func Example_complex() {
defer d.Close() defer d.Close()
} }
if err != nil { if err != nil {
log.Fatalf("ListDevices(): %v", err) log.Fatalf("OpenDevices(): %v", err)
} }
if len(devs) == 0 { if len(devs) == 0 {
log.Fatalf("no devices found matching VID %s and PID %s", vid, pid) log.Fatalf("no devices found matching VID %s and PID %s", vid, pid)

10
usb.go
View File

@@ -145,12 +145,12 @@ func NewContext() *Context {
return ctx return ctx
} }
// ListDevices calls each with each enumerated device. // OpenDevices calls opener with each enumerated device.
// If the function returns true, the device is opened and a Device is returned if the operation succeeds. // If the opener returns true, the device is opened and a Device is returned if the operation succeeds.
// Every Device returned (whether an error is also returned or not) must be closed. // Every Device returned (whether an error is also returned or not) must be closed.
// If there are any errors enumerating the devices, // If there are any errors enumerating the devices,
// the final one is returned along with any successfully opened devices. // the final one is returned along with any successfully opened devices.
func (c *Context) ListDevices(each func(desc *DeviceDesc) bool) ([]*Device, error) { func (c *Context) OpenDevices(opener func(desc *DeviceDesc) bool) ([]*Device, error) {
list, err := libusb.getDevices(c.ctx) list, err := libusb.getDevices(c.ctx)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -166,7 +166,7 @@ func (c *Context) ListDevices(each func(desc *DeviceDesc) bool) ([]*Device, erro
continue continue
} }
if each(desc) { if opener(desc) {
handle, err := libusb.open(dev) handle, err := libusb.open(dev)
if err != nil { if err != nil {
reterr = err reterr = err
@@ -188,7 +188,7 @@ func (c *Context) ListDevices(each func(desc *DeviceDesc) bool) ([]*Device, erro
// be called to release the device if the returned device wasn't nil. // be called to release the device if the returned device wasn't nil.
func (c *Context) OpenDeviceWithVIDPID(vid, pid ID) (*Device, error) { func (c *Context) OpenDeviceWithVIDPID(vid, pid ID) (*Device, error) {
var found bool var found bool
devs, err := c.ListDevices(func(desc *DeviceDesc) bool { devs, err := c.OpenDevices(func(desc *DeviceDesc) bool {
if found { if found {
return false return false
} }

View File

@@ -17,7 +17,7 @@ package gousb
import "testing" import "testing"
func TestListDevices(t *testing.T) { func TestOPenDevices(t *testing.T) {
_, done := newFakeLibusb() _, done := newFakeLibusb()
defer done() defer done()
@@ -26,7 +26,7 @@ func TestListDevices(t *testing.T) {
c.Debug(0) c.Debug(0)
descs := []*DeviceDesc{} descs := []*DeviceDesc{}
devs, err := c.ListDevices(func(desc *DeviceDesc) bool { devs, err := c.OpenDevices(func(desc *DeviceDesc) bool {
descs = append(descs, desc) descs = append(descs, desc)
return true return true
}) })
@@ -36,7 +36,7 @@ func TestListDevices(t *testing.T) {
} }
}() }()
if err != nil { if err != nil {
t.Fatalf("ListDevices(): %s", err) t.Fatalf("OpenDevices(): %s", err)
} }
if got, want := len(devs), len(fakeDevices); got != want { if got, want := len(devs), len(fakeDevices); got != want {