File: usb_test.go

package info (click to toggle)
golang-github-hanwen-usb 0.0~git20141217.69aee45-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 88 kB
  • sloc: makefile: 3
file content (69 lines) | stat: -rw-r--r-- 1,828 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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()
	}
}