use ConfigDesc, InterfaceDesc and EndpointDesc.
This commit is contained in:
@@ -23,4 +23,4 @@ script:
|
|||||||
- |-
|
- |-
|
||||||
echo 'mode: count' > coverage.merged && go list ./... | xargs -n1 -I{} sh -c ': > coverage.tmp; go test -v -covermode=count -coverprofile=coverage.tmp {} && tail -n +2 coverage.tmp >> coverage.merged' && rm coverage.tmp
|
echo 'mode: count' > coverage.merged && go list ./... | xargs -n1 -I{} sh -c ': > coverage.tmp; go test -v -covermode=count -coverprofile=coverage.tmp {} && tail -n +2 coverage.tmp >> coverage.merged' && rm coverage.tmp
|
||||||
- |-
|
- |-
|
||||||
$HOME/gopath/bin/goveralls -coverprofile=coverage.merged -service=travis-ci -ignore libusb_wrappers.go,error.go || true
|
$HOME/gopath/bin/goveralls -coverprofile=coverage.merged -service=travis-ci -ignore libusb.go,error.go || true
|
||||||
|
@@ -34,7 +34,7 @@ type ConfigDesc struct {
|
|||||||
// in this configuration.
|
// in this configuration.
|
||||||
MaxPower Milliamperes
|
MaxPower Milliamperes
|
||||||
// Interfaces has a list of USB interfaces available in this configuration.
|
// Interfaces has a list of USB interfaces available in this configuration.
|
||||||
Interfaces []InterfaceInfo
|
Interfaces []InterfaceDesc
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns the human-readable description of the configuration descriptor.
|
// String returns the human-readable description of the configuration descriptor.
|
||||||
|
@@ -23,7 +23,7 @@ import (
|
|||||||
|
|
||||||
// EndpointInfo contains the information about an interface endpoint, extracted
|
// EndpointInfo contains the information about an interface endpoint, extracted
|
||||||
// from the descriptor.
|
// from the descriptor.
|
||||||
type EndpointInfo struct {
|
type EndpointDesc struct {
|
||||||
// Number represents the endpoint number. Note that the endpoint number is different from the
|
// Number represents the endpoint number. Note that the endpoint number is different from the
|
||||||
// address field in the descriptor - address 0x82 means endpoint number 2,
|
// address field in the descriptor - address 0x82 means endpoint number 2,
|
||||||
// with endpoint direction IN.
|
// with endpoint direction IN.
|
||||||
@@ -55,7 +55,7 @@ func endpointAddr(n int, d EndpointDirection) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// String returns the human-readable description of the endpoint.
|
// String returns the human-readable description of the endpoint.
|
||||||
func (e EndpointInfo) String() string {
|
func (e EndpointDesc) String() string {
|
||||||
ret := make([]string, 0, 3)
|
ret := make([]string, 0, 3)
|
||||||
ret = append(ret, fmt.Sprintf("ep #%d %s (address 0x%02x) %s", e.Number, e.Direction, endpointAddr(e.Number, e.Direction), e.TransferType))
|
ret = append(ret, fmt.Sprintf("ep #%d %s (address 0x%02x) %s", e.Number, e.Direction, endpointAddr(e.Number, e.Direction), e.TransferType))
|
||||||
switch e.TransferType {
|
switch e.TransferType {
|
||||||
@@ -72,7 +72,7 @@ type endpoint struct {
|
|||||||
h *libusbDevHandle
|
h *libusbDevHandle
|
||||||
|
|
||||||
InterfaceSetting
|
InterfaceSetting
|
||||||
Info EndpointInfo
|
Info EndpointDesc
|
||||||
|
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
}
|
}
|
||||||
|
@@ -24,11 +24,11 @@ func TestEndpoint(t *testing.T) {
|
|||||||
defer done()
|
defer done()
|
||||||
|
|
||||||
for _, epData := range []struct {
|
for _, epData := range []struct {
|
||||||
ei EndpointInfo
|
ei EndpointDesc
|
||||||
intf InterfaceSetting
|
intf InterfaceSetting
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
ei: EndpointInfo{
|
ei: EndpointDesc{
|
||||||
Number: 2,
|
Number: 2,
|
||||||
Direction: EndpointDirectionIn,
|
Direction: EndpointDirectionIn,
|
||||||
MaxPacketSize: 512,
|
MaxPacketSize: 512,
|
||||||
@@ -41,7 +41,7 @@ func TestEndpoint(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ei: EndpointInfo{
|
ei: EndpointDesc{
|
||||||
Number: 6,
|
Number: 6,
|
||||||
MaxPacketSize: 3 * 1024,
|
MaxPacketSize: 3 * 1024,
|
||||||
TransferType: TransferTypeIsochronous,
|
TransferType: TransferTypeIsochronous,
|
||||||
@@ -55,7 +55,7 @@ func TestEndpoint(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
epData.intf.Endpoints = map[int]EndpointInfo{epData.ei.Number: epData.ei}
|
epData.intf.Endpoints = map[int]EndpointDesc{epData.ei.Number: epData.ei}
|
||||||
for _, tc := range []struct {
|
for _, tc := range []struct {
|
||||||
desc string
|
desc string
|
||||||
buf []byte
|
buf []byte
|
||||||
@@ -115,11 +115,11 @@ func TestEndpoint(t *testing.T) {
|
|||||||
|
|
||||||
func TestEndpointInfo(t *testing.T) {
|
func TestEndpointInfo(t *testing.T) {
|
||||||
for _, tc := range []struct {
|
for _, tc := range []struct {
|
||||||
ep EndpointInfo
|
ep EndpointDesc
|
||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
ep: EndpointInfo{
|
ep: EndpointDesc{
|
||||||
Number: 6,
|
Number: 6,
|
||||||
Direction: EndpointDirectionIn,
|
Direction: EndpointDirectionIn,
|
||||||
TransferType: TransferTypeBulk,
|
TransferType: TransferTypeBulk,
|
||||||
@@ -128,7 +128,7 @@ func TestEndpointInfo(t *testing.T) {
|
|||||||
want: "ep #6 IN (address 0x86) bulk [512 bytes]",
|
want: "ep #6 IN (address 0x86) bulk [512 bytes]",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ep: EndpointInfo{
|
ep: EndpointDesc{
|
||||||
Number: 2,
|
Number: 2,
|
||||||
Direction: EndpointDirectionOut,
|
Direction: EndpointDirectionOut,
|
||||||
TransferType: TransferTypeIsochronous,
|
TransferType: TransferTypeIsochronous,
|
||||||
@@ -139,7 +139,7 @@ func TestEndpointInfo(t *testing.T) {
|
|||||||
want: "ep #2 OUT (address 0x02) isochronous - asynchronous data [512 bytes]",
|
want: "ep #2 OUT (address 0x02) isochronous - asynchronous data [512 bytes]",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ep: EndpointInfo{
|
ep: EndpointDesc{
|
||||||
Number: 3,
|
Number: 3,
|
||||||
Direction: EndpointDirectionIn,
|
Direction: EndpointDirectionIn,
|
||||||
TransferType: TransferTypeInterrupt,
|
TransferType: TransferTypeInterrupt,
|
||||||
|
@@ -30,13 +30,13 @@ var fakeDevices = []*DeviceDesc{
|
|||||||
Configs: map[int]ConfigDesc{1: {
|
Configs: map[int]ConfigDesc{1: {
|
||||||
Config: 1,
|
Config: 1,
|
||||||
MaxPower: Milliamperes(100),
|
MaxPower: Milliamperes(100),
|
||||||
Interfaces: []InterfaceInfo{{
|
Interfaces: []InterfaceDesc{{
|
||||||
Number: 0,
|
Number: 0,
|
||||||
AltSettings: []InterfaceSetting{{
|
AltSettings: []InterfaceSetting{{
|
||||||
Number: 0,
|
Number: 0,
|
||||||
Alternate: 0,
|
Alternate: 0,
|
||||||
Class: ClassVendorSpec,
|
Class: ClassVendorSpec,
|
||||||
Endpoints: map[int]EndpointInfo{
|
Endpoints: map[int]EndpointDesc{
|
||||||
1: {
|
1: {
|
||||||
Number: 1,
|
Number: 1,
|
||||||
Direction: EndpointDirectionOut,
|
Direction: EndpointDirectionOut,
|
||||||
@@ -69,7 +69,7 @@ var fakeDevices = []*DeviceDesc{
|
|||||||
Configs: map[int]ConfigDesc{1: {
|
Configs: map[int]ConfigDesc{1: {
|
||||||
Config: 1,
|
Config: 1,
|
||||||
MaxPower: Milliamperes(100),
|
MaxPower: Milliamperes(100),
|
||||||
Interfaces: []InterfaceInfo{{
|
Interfaces: []InterfaceDesc{{
|
||||||
Number: 0,
|
Number: 0,
|
||||||
AltSettings: []InterfaceSetting{{
|
AltSettings: []InterfaceSetting{{
|
||||||
Number: 0,
|
Number: 0,
|
||||||
@@ -82,7 +82,7 @@ var fakeDevices = []*DeviceDesc{
|
|||||||
Number: 1,
|
Number: 1,
|
||||||
Alternate: 0,
|
Alternate: 0,
|
||||||
Class: ClassVendorSpec,
|
Class: ClassVendorSpec,
|
||||||
Endpoints: map[int]EndpointInfo{
|
Endpoints: map[int]EndpointDesc{
|
||||||
5: {
|
5: {
|
||||||
Number: 5,
|
Number: 5,
|
||||||
Direction: EndpointDirectionOut,
|
Direction: EndpointDirectionOut,
|
||||||
@@ -102,7 +102,7 @@ var fakeDevices = []*DeviceDesc{
|
|||||||
Number: 1,
|
Number: 1,
|
||||||
Alternate: 1,
|
Alternate: 1,
|
||||||
Class: ClassVendorSpec,
|
Class: ClassVendorSpec,
|
||||||
Endpoints: map[int]EndpointInfo{
|
Endpoints: map[int]EndpointDesc{
|
||||||
5: {
|
5: {
|
||||||
Number: 5,
|
Number: 5,
|
||||||
Direction: EndpointDirectionOut,
|
Direction: EndpointDirectionOut,
|
||||||
@@ -120,7 +120,7 @@ var fakeDevices = []*DeviceDesc{
|
|||||||
Number: 1,
|
Number: 1,
|
||||||
Alternate: 2,
|
Alternate: 2,
|
||||||
Class: ClassVendorSpec,
|
Class: ClassVendorSpec,
|
||||||
Endpoints: map[int]EndpointInfo{
|
Endpoints: map[int]EndpointDesc{
|
||||||
5: {
|
5: {
|
||||||
Number: 5,
|
Number: 5,
|
||||||
Direction: EndpointDirectionIn,
|
Direction: EndpointDirectionIn,
|
||||||
|
@@ -147,7 +147,7 @@ func (f *fakeLibusb) setAlt(d *libusbDevHandle, intf, alt uint8) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fakeLibusb) alloc(_ *libusbDevHandle, _ *EndpointInfo, _ time.Duration, _ int, buf []byte, done chan struct{}) (*libusbTransfer, error) {
|
func (f *fakeLibusb) alloc(_ *libusbDevHandle, _ *EndpointDesc, _ time.Duration, _ int, buf []byte, done chan struct{}) (*libusbTransfer, error) {
|
||||||
f.mu.Lock()
|
f.mu.Lock()
|
||||||
defer f.mu.Unlock()
|
defer f.mu.Unlock()
|
||||||
t := newFakeTransferPointer()
|
t := newFakeTransferPointer()
|
||||||
|
@@ -22,7 +22,7 @@ import (
|
|||||||
|
|
||||||
// InterfaceInfo contains information about a USB interface, extracted from
|
// InterfaceInfo contains information about a USB interface, extracted from
|
||||||
// the descriptor.
|
// the descriptor.
|
||||||
type InterfaceInfo struct {
|
type InterfaceDesc struct {
|
||||||
// Number is the number of this interface, a zero-based index in the array
|
// Number is the number of this interface, a zero-based index in the array
|
||||||
// of interfaces supported by the device configuration.
|
// of interfaces supported by the device configuration.
|
||||||
Number int
|
Number int
|
||||||
@@ -32,7 +32,7 @@ type InterfaceInfo struct {
|
|||||||
|
|
||||||
// String returns a human-readable descripton of the interface descriptor and
|
// String returns a human-readable descripton of the interface descriptor and
|
||||||
// it's alternate settings.
|
// it's alternate settings.
|
||||||
func (i InterfaceInfo) String() string {
|
func (i InterfaceDesc) String() string {
|
||||||
return fmt.Sprintf("Interface %d (%d alternate settings)", i.Number, len(i.AltSettings))
|
return fmt.Sprintf("Interface %d (%d alternate settings)", i.Number, len(i.AltSettings))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,7 +51,7 @@ type InterfaceSetting struct {
|
|||||||
Protocol Protocol
|
Protocol Protocol
|
||||||
// Endpoints enumerates the endpoints available on this interface with
|
// Endpoints enumerates the endpoints available on this interface with
|
||||||
// this alternate setting.
|
// this alternate setting.
|
||||||
Endpoints map[int]EndpointInfo
|
Endpoints map[int]EndpointDesc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a InterfaceSetting) sortedEndpointIds() []string {
|
func (a InterfaceSetting) sortedEndpointIds() []string {
|
||||||
@@ -95,7 +95,7 @@ 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 EndpointDesc
|
||||||
ep, ok := i.Setting.Endpoints[epNum]
|
ep, ok := i.Setting.Endpoints[epNum]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("%s does not have endpoint number %d. Available endpoints: %v", i, epNum, i.Setting.sortedEndpointIds())
|
return nil, fmt.Errorf("%s does not have endpoint number %d. Available endpoints: %v", i, epNum, i.Setting.sortedEndpointIds())
|
||||||
|
@@ -39,8 +39,8 @@ type libusbTransfer C.struct_libusb_transfer
|
|||||||
type libusbIso C.struct_libusb_iso_packet_descriptor
|
type libusbIso C.struct_libusb_iso_packet_descriptor
|
||||||
type libusbEndpoint C.struct_libusb_endpoint_descriptor
|
type libusbEndpoint C.struct_libusb_endpoint_descriptor
|
||||||
|
|
||||||
func (ep libusbEndpoint) endpointInfo(dev *DeviceDesc) EndpointInfo {
|
func (ep libusbEndpoint) endpointDesc(dev *DeviceDesc) EndpointDesc {
|
||||||
ei := EndpointInfo{
|
ei := EndpointDesc{
|
||||||
Number: int(ep.bEndpointAddress & endpointNumMask),
|
Number: int(ep.bEndpointAddress & endpointNumMask),
|
||||||
Direction: EndpointDirection((ep.bEndpointAddress & endpointDirectionMask) != 0),
|
Direction: EndpointDirection((ep.bEndpointAddress & endpointDirectionMask) != 0),
|
||||||
TransferType: TransferType(ep.bmAttributes & transferTypeMask),
|
TransferType: TransferType(ep.bmAttributes & transferTypeMask),
|
||||||
@@ -139,7 +139,7 @@ type libusbIntf interface {
|
|||||||
setAlt(*libusbDevHandle, uint8, uint8) error
|
setAlt(*libusbDevHandle, uint8, uint8) error
|
||||||
|
|
||||||
// transfer
|
// transfer
|
||||||
alloc(*libusbDevHandle, *EndpointInfo, time.Duration, int, []byte, chan struct{}) (*libusbTransfer, error)
|
alloc(*libusbDevHandle, *EndpointDesc, time.Duration, int, []byte, chan struct{}) (*libusbTransfer, error)
|
||||||
cancel(*libusbTransfer) error
|
cancel(*libusbTransfer) error
|
||||||
submit(*libusbTransfer) error
|
submit(*libusbTransfer) error
|
||||||
data(*libusbTransfer) (int, TransferStatus)
|
data(*libusbTransfer) (int, TransferStatus)
|
||||||
@@ -227,7 +227,7 @@ func (libusbImpl) getDeviceDesc(d *libusbDevice) (*DeviceDesc, error) {
|
|||||||
Len: int(cfg.bNumInterfaces),
|
Len: int(cfg.bNumInterfaces),
|
||||||
Cap: int(cfg.bNumInterfaces),
|
Cap: int(cfg.bNumInterfaces),
|
||||||
}
|
}
|
||||||
c.Interfaces = make([]InterfaceInfo, 0, len(ifaces))
|
c.Interfaces = make([]InterfaceDesc, 0, len(ifaces))
|
||||||
for ifNum, iface := range ifaces {
|
for ifNum, iface := range ifaces {
|
||||||
if iface.num_altsetting == 0 {
|
if iface.num_altsetting == 0 {
|
||||||
continue
|
continue
|
||||||
@@ -260,15 +260,15 @@ func (libusbImpl) getDeviceDesc(d *libusbDevice) (*DeviceDesc, error) {
|
|||||||
Len: int(alt.bNumEndpoints),
|
Len: int(alt.bNumEndpoints),
|
||||||
Cap: int(alt.bNumEndpoints),
|
Cap: int(alt.bNumEndpoints),
|
||||||
}
|
}
|
||||||
i.Endpoints = make(map[int]EndpointInfo, len(ends))
|
i.Endpoints = make(map[int]EndpointDesc, len(ends))
|
||||||
for _, end := range ends {
|
for _, end := range ends {
|
||||||
// TODO(sebek): pass the device descriptor too.
|
// TODO(sebek): pass the device descriptor too.
|
||||||
epi := libusbEndpoint(end).endpointInfo(nil)
|
epi := libusbEndpoint(end).endpointDesc(nil)
|
||||||
i.Endpoints[epi.Number] = epi
|
i.Endpoints[epi.Number] = epi
|
||||||
}
|
}
|
||||||
descs = append(descs, i)
|
descs = append(descs, i)
|
||||||
}
|
}
|
||||||
c.Interfaces = append(c.Interfaces, InterfaceInfo{
|
c.Interfaces = append(c.Interfaces, InterfaceDesc{
|
||||||
Number: descs[0].Number,
|
Number: descs[0].Number,
|
||||||
AltSettings: descs,
|
AltSettings: descs,
|
||||||
})
|
})
|
||||||
@@ -379,7 +379,7 @@ func (libusbImpl) setAlt(d *libusbDevHandle, iface, setup uint8) error {
|
|||||||
return fromErrNo(C.libusb_set_interface_alt_setting((*C.libusb_device_handle)(d), C.int(iface), C.int(setup)))
|
return fromErrNo(C.libusb_set_interface_alt_setting((*C.libusb_device_handle)(d), C.int(iface), C.int(setup)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (libusbImpl) alloc(d *libusbDevHandle, ep *EndpointInfo, timeout time.Duration, isoPackets int, buf []byte, done chan struct{}) (*libusbTransfer, error) {
|
func (libusbImpl) alloc(d *libusbDevHandle, ep *EndpointDesc, timeout time.Duration, isoPackets int, buf []byte, done chan struct{}) (*libusbTransfer, error) {
|
||||||
xfer := C.libusb_alloc_transfer(C.int(isoPackets))
|
xfer := C.libusb_alloc_transfer(C.int(isoPackets))
|
||||||
if xfer == nil {
|
if xfer == nil {
|
||||||
return nil, fmt.Errorf("libusb_alloc_transfer(%d) failed", isoPackets)
|
return nil, fmt.Errorf("libusb_alloc_transfer(%d) failed", isoPackets)
|
@@ -111,7 +111,7 @@ func (t *usbTransfer) data() []byte {
|
|||||||
|
|
||||||
// newUSBTransfer allocates a new transfer structure for communication with a
|
// newUSBTransfer allocates a new transfer structure for communication with a
|
||||||
// given device/endpoint, with buf as the underlying transfer buffer.
|
// given device/endpoint, with buf as the underlying transfer buffer.
|
||||||
func newUSBTransfer(dev *libusbDevHandle, ei *EndpointInfo, buf []byte, timeout time.Duration) (*usbTransfer, error) {
|
func newUSBTransfer(dev *libusbDevHandle, ei *EndpointDesc, buf []byte, timeout time.Duration) (*usbTransfer, error) {
|
||||||
var isoPackets, isoPktSize int
|
var isoPackets, isoPktSize int
|
||||||
if ei.TransferType == TransferTypeIsochronous {
|
if ei.TransferType == TransferTypeIsochronous {
|
||||||
isoPktSize = ei.MaxPacketSize
|
isoPktSize = ei.MaxPacketSize
|
||||||
|
@@ -52,7 +52,7 @@ func TestNewTransfer(t *testing.T) {
|
|||||||
wantLength: 10000,
|
wantLength: 10000,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
xfer, err := newUSBTransfer(nil, &EndpointInfo{
|
xfer, err := newUSBTransfer(nil, &EndpointDesc{
|
||||||
Number: 2,
|
Number: 2,
|
||||||
Direction: tc.dir,
|
Direction: tc.dir,
|
||||||
TransferType: tc.tt,
|
TransferType: tc.tt,
|
||||||
@@ -76,7 +76,7 @@ func TestTransferProtocol(t *testing.T) {
|
|||||||
xfers := make([]*usbTransfer, 2)
|
xfers := make([]*usbTransfer, 2)
|
||||||
var err error
|
var err error
|
||||||
for i := 0; i < 2; i++ {
|
for i := 0; i < 2; i++ {
|
||||||
xfers[i], err = newUSBTransfer(nil, &EndpointInfo{
|
xfers[i], err = newUSBTransfer(nil, &EndpointDesc{
|
||||||
Number: 6,
|
Number: 6,
|
||||||
Direction: EndpointDirectionIn,
|
Direction: EndpointDirectionIn,
|
||||||
TransferType: TransferTypeBulk,
|
TransferType: TransferTypeBulk,
|
||||||
|
Reference in New Issue
Block a user