This commit is contained in:
Kyle Lemons
2012-03-27 19:40:22 -07:00
parent 25c01a9f60
commit e5a0531ed2
7 changed files with 297 additions and 365 deletions

View File

@@ -24,54 +24,17 @@ func main() {
ctx.Debug(*debug)
// ListDevices is used to find the devices to open.
devs, err := ctx.ListDevices(func(bus, addr int, desc *usb.Descriptor) bool {
// After inspecting the descriptor, return true or false depending on whether
// the device is "interesting" or not. Any descriptor for which true is returned
// generates a DeviceInfo which is retuned in a slice.
return true
})
// All DeviceInfo returned from ListDevices must be closed.
defer func() {
for _, d := range devs {
d.Close()
}
}()
// ListDevices can occaionally fail, so be sure to check its return value.
if err != nil {
log.Fatalf("list: %s", err)
}
for _, dev := range devs {
// The descriptor (which contains useful information) can always be obtained again
// from a DeviceInfo.
desc, err := dev.Descriptor()
if err != nil {
log.Printf("desc: %s", err)
continue
}
// The DeviceInfo contains the bus number and bus address of the device.
bus := dev.BusNumber()
addr := dev.Address()
devs, err := ctx.ListDevices(func(desc *usb.Descriptor) bool {
// The usbid package can be used to print out human readable information.
fmt.Printf("%03d:%03d %s\n", bus, addr, usbid.Describe(desc))
fmt.Printf("%03d.%03d %s\n", desc.Bus, desc.Address, usbid.Describe(desc))
fmt.Printf(" Protocol: %s\n", usbid.Classify(desc))
// The configurations can be examined from the DeviceInfo, though they can only
// The configurations can be examined from the Descriptor, though they can only
// be set once the device is opened. All configuration references must be closed,
// to free up the memory in libusb.
cfgs, err := dev.Configurations()
if err != nil {
log.Printf(" - configs: %s", err)
continue
}
// This loop just uses more of the built-in and usbid pretty printing to list
// the USB devices.
for _, cfg := range cfgs {
for _, cfg := range desc.Configs {
// This loop just uses more of the built-in and usbid pretty printing to list
// the USB devices.
fmt.Printf(" %s:\n", cfg)
for _, alt := range cfg.Interfaces {
fmt.Printf(" --------------\n")
@@ -86,7 +49,27 @@ func main() {
fmt.Printf(" --------------\n")
}
// To actually interact with a device, DevInfo.Open() returns a device handle
// which can be used to do more I/O with the device and its endpoints.
// After inspecting the descriptor, return true or false depending on whether
// the device is "interesting" or not. Any descriptor for which true is returned
// opens a Device which is retuned in a slice (and must be subsequently closed).
return false
})
// All Devices returned from ListDevices must be closed.
defer func() {
for _, d := range devs {
d.Close()
}
}()
// ListDevices can occaionally fail, so be sure to check its return value.
if err != nil {
log.Fatalf("list: %s", err)
}
for _, dev := range devs {
// Once the device has been selected from ListDevices, it is opened
// and can be interacted with.
_ = dev
}
}