rename Setups to AltSettings

This commit is contained in:
Sebastian Zagrodzki
2017-04-09 20:45:35 +02:00
parent 8364724ca4
commit aaef575b06
11 changed files with 50 additions and 74 deletions

View File

@@ -52,12 +52,12 @@ func main() {
// 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 {
for _, intf := range cfg.Interfaces {
fmt.Printf(" --------------\n")
for _, iface := range alt.Setups {
fmt.Printf(" %s\n", iface)
fmt.Printf(" %s\n", usbid.Classify(iface))
for _, end := range iface.Endpoints {
for _, ifSetting := range intf.AltSettings {
fmt.Printf(" %s\n", ifSetting)
fmt.Printf(" %s\n", usbid.Classify(ifSetting))
for _, end := range ifSetting.Endpoints {
fmt.Printf(" %s\n", end)
}
}

View File

@@ -25,7 +25,6 @@ import (
"strings"
"github.com/kylelemons/gousb/usb"
"github.com/kylelemons/gousb/usbid"
)
var (
@@ -139,29 +138,6 @@ func main() {
}
dev := devs[0]
// The usbid package can be used to print out human readable information.
log.Printf(" Protocol: %s\n", usbid.Classify(dev.Descriptor))
// 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.
for _, cfg := range dev.Configs {
// This loop just uses more of the built-in and usbid pretty printing to list
// the USB devices.
log.Printf(" %s:\n", cfg)
for _, alt := range cfg.Interfaces {
log.Printf(" --------------\n")
for _, iface := range alt.Setups {
log.Printf(" %s\n", iface)
log.Printf(" %s\n", usbid.Classify(iface))
for _, end := range iface.Endpoints {
log.Printf(" %s\n", end)
}
}
}
log.Printf(" --------------\n")
}
log.Printf("Connecting to endpoint %d...", *endpoint)
ep, err := dev.OpenEndpoint(uint8(*config), uint8(*iface), uint8(*setup), uint8(*endpoint))
if err != nil {

View File

@@ -74,22 +74,22 @@ type InterfaceInfo struct {
// Number is the number of this interface, a zero-based index in the array
// of interfaces supported by the device configuration.
Number uint8
// Setups is a list of alternate settings supported by the interface.
Setups []InterfaceSetup
// AltSettings is a list of alternate settings supported by the interface.
AltSettings []InterfaceSetting
}
// String returns a human-readable descripton of the interface and it's
// alternate settings.
func (i InterfaceInfo) String() string {
return fmt.Sprintf("Interface %02x (%d alternate settings)", i.Number, len(i.Setups))
return fmt.Sprintf("Interface %d (%d alternate settings)", i.Number, len(i.AltSettings))
}
// InterfaceSetup contains information about a USB interface with a particular
// InterfaceSetting contains information about a USB interface with a particular
// alternate setting, extracted from the descriptor.
type InterfaceSetup struct {
type InterfaceSetting struct {
// Number is the number of this interface, the same as in InterfaceInfo.
Number uint8
// Alternate is the number of the alternate setting.
// Alternate is the number of this alternate setting.
Alternate uint8
// Class is the USB-IF class code, as defined by the USB spec.
Class Class
@@ -104,8 +104,8 @@ type InterfaceSetup struct {
// String returns a human-readable descripton of the particular
// alternate setting of an interface.
func (a InterfaceSetup) String() string {
return fmt.Sprintf("Interface %02x Setup %02x", a.Number, a.Alternate)
func (a InterfaceSetting) String() string {
return fmt.Sprintf("Interface %d alternate setting %d", a.Number, a.Alternate)
}
// ConfigInfo contains the information about a USB device configuration.
@@ -126,5 +126,5 @@ type ConfigInfo struct {
// String returns the human-readable description of the configuration.
func (c ConfigInfo) String() string {
return fmt.Sprintf("Config %02x", c.Config)
return fmt.Sprintf("Config %d", c.Config)
}

View File

@@ -15,8 +15,7 @@
package usb
// To enable internal debugging:
// -ldflags "-X github.com/kylelemons/gousb/usb.debugInternal true"
// To enable internal debugging, set the GOUSB_DEBUG environment variable.
import (
"io"
@@ -26,11 +25,12 @@ import (
)
var debug *log.Logger
var debugInternal string
const debugEnvVarName = "GOUSB_DEBUG"
func init() {
var out io.Writer = ioutil.Discard
if debugInternal != "" {
if os.Getenv(debugEnvVarName) != "" {
out = os.Stderr
}
debug = log.New(out, "usb", log.LstdFlags|log.Lshortfile)

View File

@@ -105,7 +105,7 @@ func (d *Device) OpenEndpoint(epAddr, cfgNum, ifNum, setNum uint8) (*Endpoint, e
var cfg *ConfigInfo
for _, c := range d.Configs {
if c.Config == cfgNum {
debug.Printf("found conf: %#v\n", c)
debug.Printf("found conf: %+v\n", c)
cfg = &c
break
}
@@ -117,7 +117,7 @@ func (d *Device) OpenEndpoint(epAddr, cfgNum, ifNum, setNum uint8) (*Endpoint, e
var intf *InterfaceInfo
for _, i := range cfg.Interfaces {
if i.Number == ifNum {
debug.Printf("found iface: %#v\n", i)
debug.Printf("found iface: %+v\n", i)
intf = &i
break
}
@@ -127,11 +127,11 @@ func (d *Device) OpenEndpoint(epAddr, cfgNum, ifNum, setNum uint8) (*Endpoint, e
}
var setAlternate bool
var ifs *InterfaceSetup
for i, s := range intf.Setups {
var ifs *InterfaceSetting
for i, s := range intf.AltSettings {
if s.Alternate == setNum {
setAlternate = i != 0
debug.Printf("found setup: %#v [default: %v]\n", s, !setAlternate)
debug.Printf("found setup: %+v [default: %v]\n", s, !setAlternate)
ifs = &s
}
}
@@ -142,7 +142,7 @@ func (d *Device) OpenEndpoint(epAddr, cfgNum, ifNum, setNum uint8) (*Endpoint, e
var ep *EndpointInfo
for _, e := range ifs.Endpoints {
if endpointAddr(e.Number, e.Direction) == epAddr {
debug.Printf("found ep 0x%02x %s in %#v\n", e.Number, e.Direction, *ifs)
debug.Printf("found ep #%d %s in %+v\n", e.Number, e.Direction, *ifs)
ep = &e
}
}

View File

@@ -24,7 +24,7 @@ import (
type Endpoint struct {
h *libusbDevHandle
InterfaceSetup
InterfaceSetting
Info EndpointInfo
readTimeout time.Duration
@@ -76,12 +76,12 @@ func (e *Endpoint) transfer(buf []byte, timeout time.Duration) (int, error) {
return n, nil
}
func newEndpoint(h *libusbDevHandle, s InterfaceSetup, e EndpointInfo, rt, wt time.Duration) *Endpoint {
func newEndpoint(h *libusbDevHandle, s InterfaceSetting, e EndpointInfo, rt, wt time.Duration) *Endpoint {
return &Endpoint{
InterfaceSetup: s,
Info: e,
h: h,
readTimeout: rt,
writeTimeout: wt,
InterfaceSetting: s,
Info: e,
h: h,
readTimeout: rt,
writeTimeout: wt,
}
}

View File

@@ -24,7 +24,7 @@ var testBulkInEP = EndpointInfo{
TransferType: TransferTypeBulk,
}
var testBulkInSetup = InterfaceSetup{
var testBulkInSetting = InterfaceSetting{
Number: 0,
Alternate: 0,
Class: ClassVendorSpec,
@@ -40,7 +40,7 @@ var testIsoOutEP = EndpointInfo{
UsageType: IsoUsageTypeData,
}
var testIsoOutSetup = InterfaceSetup{
var testIsoOutSetting = InterfaceSetting{
Number: 0,
Alternate: 0,
Class: ClassVendorSpec,

View File

@@ -24,11 +24,11 @@ func TestEndpoint(t *testing.T) {
defer func(i libusbIntf) { libusb = i }(libusb)
for _, epCfg := range []struct {
method string
InterfaceSetup
InterfaceSetting
EndpointInfo
}{
{"Read", testBulkInSetup, testBulkInEP},
{"Write", testIsoOutSetup, testIsoOutEP},
{"Read", testBulkInSetting, testBulkInEP},
{"Write", testIsoOutSetting, testIsoOutEP},
} {
t.Run(epCfg.method, func(t *testing.T) {
for _, tc := range []struct {
@@ -62,7 +62,7 @@ func TestEndpoint(t *testing.T) {
} {
lib := newFakeLibusb()
libusb = lib
ep := newEndpoint(nil, epCfg.InterfaceSetup, epCfg.EndpointInfo, time.Second, time.Second)
ep := newEndpoint(nil, epCfg.InterfaceSetting, epCfg.EndpointInfo, time.Second, time.Second)
op, ok := reflect.TypeOf(ep).MethodByName(epCfg.method)
if !ok {
t.Fatalf("method %s not found in endpoint struct", epCfg.method)
@@ -89,16 +89,16 @@ func TestEndpoint(t *testing.T) {
func TestEndpointWrongDirection(t *testing.T) {
ep := &Endpoint{
InterfaceSetup: testBulkInSetup,
Info: testBulkInEP,
InterfaceSetting: testBulkInSetting,
Info: testBulkInEP,
}
_, err := ep.Write([]byte{1, 2, 3})
if err == nil {
t.Error("bulkInEP.Write(): got nil error, want non-nil")
}
ep = &Endpoint{
InterfaceSetup: testIsoOutSetup,
Info: testIsoOutEP,
InterfaceSetting: testIsoOutSetting,
Info: testIsoOutEP,
}
_, err = ep.Read(make([]byte, 64))
if err == nil {
@@ -124,7 +124,7 @@ func TestOpenEndpoint(t *testing.T) {
if err != nil {
t.Fatalf("OpenEndpoint(cfg=1, if=1, alt=2, ep=0x86): got error %v, want nil", err)
}
if want := fakeDevices[1].Configs[0].Interfaces[1].Setups[2].Endpoints[1]; !reflect.DeepEqual(got.Info, want) {
if want := fakeDevices[1].Configs[0].Interfaces[1].AltSettings[2].Endpoints[1]; !reflect.DeepEqual(got.Info, want) {
t.Errorf("OpenEndpoint(cfg=1, if=1, alt=2, ep=0x86): got %+v, want %+v", got, want)
}
}

View File

@@ -40,7 +40,7 @@ var (
MaxPower: Milliamperes(100),
Interfaces: []InterfaceInfo{{
Number: 0,
Setups: []InterfaceSetup{{
AltSettings: []InterfaceSetting{{
Number: 0,
Alternate: 0,
Class: ClassVendorSpec,
@@ -76,14 +76,14 @@ var (
MaxPower: Milliamperes(100),
Interfaces: []InterfaceInfo{{
Number: 0,
Setups: []InterfaceSetup{{
AltSettings: []InterfaceSetting{{
Number: 0,
Alternate: 0,
Class: ClassVendorSpec,
}},
}, {
Number: 1,
Setups: []InterfaceSetup{{
AltSettings: []InterfaceSetting{{
Number: 1,
Alternate: 0,
Class: ClassVendorSpec,

View File

@@ -238,9 +238,9 @@ func (libusbImpl) getDeviceDesc(d *libusbDevice) (*Descriptor, error) {
Len: int(iface.num_altsetting),
Cap: int(iface.num_altsetting),
}
descs := make([]InterfaceSetup, 0, len(alts))
descs := make([]InterfaceSetting, 0, len(alts))
for _, alt := range alts {
i := InterfaceSetup{
i := InterfaceSetting{
Number: uint8(alt.bInterfaceNumber),
Alternate: uint8(alt.bAlternateSetting),
Class: Class(alt.bInterfaceClass),
@@ -261,8 +261,8 @@ func (libusbImpl) getDeviceDesc(d *libusbDevice) (*Descriptor, error) {
descs = append(descs, i)
}
c.Interfaces = append(c.Interfaces, InterfaceInfo{
Number: descs[0].Number,
Setups: descs,
Number: descs[0].Number,
AltSettings: descs,
})
}
C.libusb_free_config_descriptor(cfg)

View File

@@ -63,7 +63,7 @@ func Classify(val interface{}) string {
switch val := val.(type) {
case *usb.Descriptor:
class, sub, proto = val.Class, val.SubClass, val.Protocol
case usb.InterfaceSetup:
case usb.InterfaceSetting:
class, sub, proto = val.Class, val.SubClass, val.Protocol
default:
return fmt.Sprintf("Unknown (%T)", val)