Skip to content

Commit

Permalink
Add a test to usb/ , mimicking lsusb.
Browse files Browse the repository at this point in the history
  • Loading branch information
hanwen committed Feb 17, 2013
1 parent d6f042a commit 52ecdde
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
30 changes: 30 additions & 0 deletions usb/usb.go
Expand Up @@ -20,6 +20,14 @@ const SPEED_FULL = C.LIBUSB_SPEED_FULL
const SPEED_HIGH = C.LIBUSB_SPEED_HIGH
const SPEED_SUPER = C.LIBUSB_SPEED_SUPER

var SPEED_names = map[byte]string{
byte(C.LIBUSB_SPEED_UNKNOWN): "UNKNOWN",
byte(C.LIBUSB_SPEED_LOW): "LOW",
byte(C.LIBUSB_SPEED_FULL): "FULL",
byte(C.LIBUSB_SPEED_HIGH): "HIGH",
byte(C.LIBUSB_SPEED_SUPER): "SUPER",
}

type ControlSetup C.struct_libusb_control_setup
type Transfer C.struct_libusb_transfer

Expand All @@ -43,6 +51,28 @@ const CLASS_WIRELESS = 0xe0
const CLASS_APPLICATION = 0xfe
const CLASS_VENDOR_SPEC = 0xff

// Device and/or interface class codes.
var CLASS_names = map[byte]string{
0: "PER_INTERFACE",
1: "AUDIO",
2: "COMM",
3: "HID",
5: "PHYSICAL",
7: "PRINTER",
6: "IMAGE",
8: "MASS_STORAGE",
9: "HUB",
10: "DATA",
0x0b: "SMART_CARD",
0x0d: "CONTENT_SECURITY",
0x0e: "VIDEO",
0x0f: "PERSONAL_HEALTHCARE",
0xdc: "DIAGNOSTIC_DEVICE",
0xe0: "WIRELESS",
0xfe: "APPLICATION",
0xff: "VENDOR_SPEC",
}

// Descriptor types as defined by the USB specification.
const DT_DEVICE = 0x01
const DT_CONFIG = 0x02
Expand Down
72 changes: 72 additions & 0 deletions usb/usb_test.go
@@ -0,0 +1,72 @@
package usb

import (
"testing"
)

func TestDevice(t *testing.T) {
c := NewContext()
defer c.Exit()
l, err := c.GetDeviceList()
if err != nil {
t.Fatal("GetDeviceList failed:", err)
}

for _, dev := range l {
t.Logf("bus 0x%x addr 0x%x speed 0x%x\n",
dev.GetBusNumber(), dev.GetDeviceAddress(), dev.GetDeviceSpeed())
dd, err := dev.GetDeviceDescriptor()
if err != nil {
t.Logf("GetDeviceDescriptor failed: %v", err)
continue
}

t.Logf("Vendor/Product %x:%x Class/subclass/protocol %x:%x:%x: %s\n",
dd.IdVendor, dd.IdProduct, dd.DeviceClass, dd.DeviceSubClass, dd.DeviceProtocol, CLASS_names[dd.DeviceClass])

stringDescs :=[]byte{
dd.Manufacturer, dd.Product, dd.SerialNumber,
}

for i := 0; i < int(dd.NumConfigurations); i++ {
cd, err := dev.GetConfigDescriptor(byte(i))
if err != nil {
t.Logf("GetConfigDescriptor failed: %v", err)
continue
}
stringDescs = append(stringDescs, cd.ConfigurationIndex)
t.Logf(" config value %x, attributes %x power %d\n", cd.ConfigurationValue,
cd.Attributes, cd.MaxPower)
for idx, iface := range cd.Interfaces {
t.Logf(" iface %d\n", idx)
for _, alt := range iface.AltSetting {
t.Logf(" num %d class/subclass/protocol %x/%x/%x\n",
alt.InterfaceNumber, alt.InterfaceClass, alt.InterfaceSubClass, alt.InterfaceProtocol)
for _, ep := range alt.EndPoints {
t.Logf(" %v", &ep)
}
stringDescs = append(stringDescs, alt.InterfaceStringIndex)
}
}
}

dh, err := dev.Open()
if err != nil {
t.Logf("can't open: %v", err)
continue
}

for _, c := range stringDescs {
str, err := dh.GetStringDescriptorASCII(c)
if err != nil {
t.Logf("GetStringDescriptorASCII %d failed: %v", c, err)
continue
}
t.Logf(" desc %d: %s", c, str)
}
dh.Close()
}
}



0 comments on commit 52ecdde

Please sign in to comment.