Use a map to store endpoint descriptors. Allows easy access to a
particular endpoint number.
This commit is contained in:
@@ -55,7 +55,7 @@ func TestEndpoint(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
epData.intf.Endpoints = []EndpointInfo{epData.ei}
|
epData.intf.Endpoints = map[int]EndpointInfo{epData.ei.Number: epData.ei}
|
||||||
for _, tc := range []struct {
|
for _, tc := range []struct {
|
||||||
desc string
|
desc string
|
||||||
buf []byte
|
buf []byte
|
||||||
|
@@ -44,17 +44,20 @@ var (
|
|||||||
Number: 0,
|
Number: 0,
|
||||||
Alternate: 0,
|
Alternate: 0,
|
||||||
Class: ClassVendorSpec,
|
Class: ClassVendorSpec,
|
||||||
Endpoints: []EndpointInfo{{
|
Endpoints: map[int]EndpointInfo{
|
||||||
|
1: {
|
||||||
Number: 1,
|
Number: 1,
|
||||||
Direction: EndpointDirectionOut,
|
Direction: EndpointDirectionOut,
|
||||||
MaxPacketSize: 512,
|
MaxPacketSize: 512,
|
||||||
TransferType: TransferTypeBulk,
|
TransferType: TransferTypeBulk,
|
||||||
}, {
|
},
|
||||||
|
2: {
|
||||||
Number: 2,
|
Number: 2,
|
||||||
Direction: EndpointDirectionIn,
|
Direction: EndpointDirectionIn,
|
||||||
MaxPacketSize: 512,
|
MaxPacketSize: 512,
|
||||||
TransferType: TransferTypeBulk,
|
TransferType: TransferTypeBulk,
|
||||||
}},
|
},
|
||||||
|
},
|
||||||
}},
|
}},
|
||||||
}},
|
}},
|
||||||
}},
|
}},
|
||||||
@@ -87,49 +90,58 @@ var (
|
|||||||
Number: 1,
|
Number: 1,
|
||||||
Alternate: 0,
|
Alternate: 0,
|
||||||
Class: ClassVendorSpec,
|
Class: ClassVendorSpec,
|
||||||
Endpoints: []EndpointInfo{{
|
Endpoints: map[int]EndpointInfo{
|
||||||
|
5: {
|
||||||
Number: 5,
|
Number: 5,
|
||||||
Direction: EndpointDirectionOut,
|
Direction: EndpointDirectionOut,
|
||||||
MaxPacketSize: 3 * 1024,
|
MaxPacketSize: 3 * 1024,
|
||||||
TransferType: TransferTypeIsochronous,
|
TransferType: TransferTypeIsochronous,
|
||||||
UsageType: IsoUsageTypeData,
|
UsageType: IsoUsageTypeData,
|
||||||
}, {
|
},
|
||||||
|
6: {
|
||||||
Number: 6,
|
Number: 6,
|
||||||
Direction: EndpointDirectionIn,
|
Direction: EndpointDirectionIn,
|
||||||
MaxPacketSize: 3 * 1024,
|
MaxPacketSize: 3 * 1024,
|
||||||
TransferType: TransferTypeIsochronous,
|
TransferType: TransferTypeIsochronous,
|
||||||
UsageType: IsoUsageTypeData,
|
UsageType: IsoUsageTypeData,
|
||||||
}},
|
},
|
||||||
|
},
|
||||||
}, {
|
}, {
|
||||||
Number: 1,
|
Number: 1,
|
||||||
Alternate: 1,
|
Alternate: 1,
|
||||||
Class: ClassVendorSpec,
|
Class: ClassVendorSpec,
|
||||||
Endpoints: []EndpointInfo{{
|
Endpoints: map[int]EndpointInfo{
|
||||||
|
5: {
|
||||||
Number: 5,
|
Number: 5,
|
||||||
Direction: EndpointDirectionOut,
|
Direction: EndpointDirectionOut,
|
||||||
MaxPacketSize: 2 * 1024,
|
MaxPacketSize: 2 * 1024,
|
||||||
TransferType: TransferTypeIsochronous,
|
TransferType: TransferTypeIsochronous,
|
||||||
}, {
|
},
|
||||||
|
6: {
|
||||||
Number: 6,
|
Number: 6,
|
||||||
Direction: EndpointDirectionIn,
|
Direction: EndpointDirectionIn,
|
||||||
MaxPacketSize: 2 * 1024,
|
MaxPacketSize: 2 * 1024,
|
||||||
TransferType: TransferTypeIsochronous,
|
TransferType: TransferTypeIsochronous,
|
||||||
}},
|
},
|
||||||
|
},
|
||||||
}, {
|
}, {
|
||||||
Number: 1,
|
Number: 1,
|
||||||
Alternate: 2,
|
Alternate: 2,
|
||||||
Class: ClassVendorSpec,
|
Class: ClassVendorSpec,
|
||||||
Endpoints: []EndpointInfo{{
|
Endpoints: map[int]EndpointInfo{
|
||||||
|
5: {
|
||||||
Number: 5,
|
Number: 5,
|
||||||
Direction: EndpointDirectionIn,
|
Direction: EndpointDirectionIn,
|
||||||
MaxPacketSize: 1024,
|
MaxPacketSize: 1024,
|
||||||
TransferType: TransferTypeIsochronous,
|
TransferType: TransferTypeIsochronous,
|
||||||
}, {
|
},
|
||||||
|
6: {
|
||||||
Number: 6,
|
Number: 6,
|
||||||
Direction: EndpointDirectionIn,
|
Direction: EndpointDirectionIn,
|
||||||
MaxPacketSize: 1024,
|
MaxPacketSize: 1024,
|
||||||
TransferType: TransferTypeIsochronous,
|
TransferType: TransferTypeIsochronous,
|
||||||
}},
|
},
|
||||||
|
},
|
||||||
}},
|
}},
|
||||||
}},
|
}},
|
||||||
}},
|
}},
|
||||||
|
@@ -17,6 +17,7 @@ package usb
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
// InterfaceInfo contains information about a USB interface, extracted from
|
// InterfaceInfo contains information about a USB interface, extracted from
|
||||||
@@ -48,15 +49,24 @@ type InterfaceSetting struct {
|
|||||||
SubClass Class
|
SubClass Class
|
||||||
// Protocol is USB protocol code, as defined by the USB spe.c
|
// Protocol is USB protocol code, as defined by the USB spe.c
|
||||||
Protocol Protocol
|
Protocol Protocol
|
||||||
// Endpoints has the list of endpoints available on this interface with
|
// Endpoints enumerates the endpoints available on this interface with
|
||||||
// this alternate setting.
|
// this alternate setting.
|
||||||
Endpoints []EndpointInfo
|
Endpoints map[int]EndpointInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a InterfaceSetting) sortedEndpointIds() []string {
|
||||||
|
var eps []string
|
||||||
|
for _, ei := range a.Endpoints {
|
||||||
|
eps = append(eps, fmt.Sprintf("%d(%s)", ei.Number, ei.Direction))
|
||||||
|
}
|
||||||
|
sort.Strings(eps)
|
||||||
|
return eps
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns a human-readable descripton of the particular
|
// String returns a human-readable descripton of the particular
|
||||||
// alternate setting of an interface.
|
// alternate setting of an interface.
|
||||||
func (a InterfaceSetting) String() string {
|
func (a InterfaceSetting) String() string {
|
||||||
return fmt.Sprintf("Interface %d alternate setting %d", a.Number, a.Alternate)
|
return fmt.Sprintf("Interface %d alternate setting %d (available endpoints: %v)", a.Number, a.Alternate, a.sortedEndpointIds())
|
||||||
}
|
}
|
||||||
|
|
||||||
type Interface struct {
|
type Interface struct {
|
||||||
@@ -83,17 +93,9 @@ func (i *Interface) Close() {
|
|||||||
|
|
||||||
func (i *Interface) openEndpoint(epNum int) (*endpoint, error) {
|
func (i *Interface) openEndpoint(epNum int) (*endpoint, error) {
|
||||||
var ep EndpointInfo
|
var ep EndpointInfo
|
||||||
var found bool
|
ep, ok := i.Setting.Endpoints[epNum]
|
||||||
for _, e := range i.Setting.Endpoints {
|
if !ok {
|
||||||
if e.Number == epNum {
|
return nil, fmt.Errorf("%s does not have endpoint number %d. Available endpoints: %v", i, epNum, i.Setting.sortedEndpointIds())
|
||||||
debug.Printf("found ep %s in %s\n", e, i)
|
|
||||||
ep = e
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
return nil, fmt.Errorf("%s does not have endpoint number %d. Available endpoints: %v", i, epNum, i.Setting.Endpoints)
|
|
||||||
}
|
}
|
||||||
return &endpoint{
|
return &endpoint{
|
||||||
InterfaceSetting: i.Setting,
|
InterfaceSetting: i.Setting,
|
||||||
|
@@ -260,10 +260,11 @@ func (libusbImpl) getDeviceDesc(d *libusbDevice) (*Descriptor, error) {
|
|||||||
Len: int(alt.bNumEndpoints),
|
Len: int(alt.bNumEndpoints),
|
||||||
Cap: int(alt.bNumEndpoints),
|
Cap: int(alt.bNumEndpoints),
|
||||||
}
|
}
|
||||||
i.Endpoints = make([]EndpointInfo, len(ends))
|
i.Endpoints = make(map[int]EndpointInfo, len(ends))
|
||||||
for n, end := range ends {
|
for _, end := range ends {
|
||||||
// TODO(sebek): pass the device descriptor too.
|
// TODO(sebek): pass the device descriptor too.
|
||||||
i.Endpoints[n] = libusbEndpoint(end).endpointInfo(nil)
|
epi := libusbEndpoint(end).endpointInfo(nil)
|
||||||
|
i.Endpoints[epi.Number] = epi
|
||||||
}
|
}
|
||||||
descs = append(descs, i)
|
descs = append(descs, i)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user