Add Device.OutEndpoint, simplify endpoint test.

This commit is contained in:
Sebastian Zagrodzki
2017-04-10 01:11:38 +02:00
parent 57b10f0dd3
commit b1dcaa1195
3 changed files with 88 additions and 72 deletions

View File

@@ -182,6 +182,7 @@ func (d *Device) openEndpoint(cfgNum, ifNum, setNum, epAddr uint8) (*endpoint, e
return end, nil return end, nil
} }
// InEndpoint prepares an IN endpoint for transfer.
func (d *Device) InEndpoint(cfgNum, ifNum, setNum, epNum uint8) (*InEndpoint, error) { func (d *Device) InEndpoint(cfgNum, ifNum, setNum, epNum uint8) (*InEndpoint, error) {
ep, err := d.openEndpoint(cfgNum, ifNum, setNum, endpointAddr(epNum, EndpointDirectionIn)) ep, err := d.openEndpoint(cfgNum, ifNum, setNum, endpointAddr(epNum, EndpointDirectionIn))
if err != nil { if err != nil {
@@ -193,6 +194,18 @@ func (d *Device) InEndpoint(cfgNum, ifNum, setNum, epNum uint8) (*InEndpoint, er
}, nil }, nil
} }
// OutEndpoint prepares an OUT endpoint for transfer.
func (d *Device) OutEndpoint(cfgNum, ifNum, setNum, epNum uint8) (*OutEndpoint, error) {
ep, err := d.openEndpoint(cfgNum, ifNum, setNum, endpointAddr(epNum, EndpointDirectionOut))
if err != nil {
return nil, err
}
return &OutEndpoint{
endpoint: ep,
timeout: d.WriteTimeout,
}, nil
}
// GetStringDescriptor returns a device string descriptor with the given index // GetStringDescriptor returns a device string descriptor with the given index
// number. The first supported language is always used and the returned // number. The first supported language is always used and the returned
// descriptor string is converted to ASCII (non-ASCII characters are replaced // descriptor string is converted to ASCII (non-ASCII characters are replaced

View File

@@ -110,7 +110,7 @@ func newEndpoint(h *libusbDevHandle, s InterfaceSetting, e EndpointInfo) *endpoi
} }
} }
// OutEndpoint represents an IN endpoint open for transfer. // InEndpoint represents an IN endpoint open for transfer.
type InEndpoint struct { type InEndpoint struct {
*endpoint *endpoint
timeout time.Duration timeout time.Duration

View File

@@ -19,39 +19,41 @@ import (
"time" "time"
) )
// IN bulk endpoint func TestEndpoint(t *testing.T) {
var testBulkInEP = EndpointInfo{ defer func(i libusbIntf) { libusb = i }(libusb)
for _, epData := range []struct {
ei EndpointInfo
intf InterfaceSetting
}{
{
ei: EndpointInfo{
Number: 2, Number: 2,
Direction: EndpointDirectionIn, Direction: EndpointDirectionIn,
MaxPacketSize: 512, MaxPacketSize: 512,
TransferType: TransferTypeBulk, TransferType: TransferTypeBulk,
} },
intf: InterfaceSetting{
var testBulkInSetting = InterfaceSetting{
Number: 0, Number: 0,
Alternate: 0, Alternate: 0,
Class: ClassVendorSpec, Class: ClassVendorSpec,
Endpoints: []EndpointInfo{testBulkInEP}, },
} },
{
// OUT iso endpoint ei: EndpointInfo{
var testIsoOutEP = EndpointInfo{
Number: 6, Number: 6,
MaxPacketSize: 3 * 1024, MaxPacketSize: 3 * 1024,
TransferType: TransferTypeIsochronous, TransferType: TransferTypeIsochronous,
PollInterval: 125 * time.Microsecond, PollInterval: 125 * time.Microsecond,
UsageType: IsoUsageTypeData, UsageType: IsoUsageTypeData,
} },
intf: InterfaceSetting{
var testIsoOutSetting = InterfaceSetting{
Number: 0, Number: 0,
Alternate: 0, Alternate: 0,
Class: ClassVendorSpec, Class: ClassVendorSpec,
Endpoints: []EndpointInfo{testIsoOutEP}, },
} },
} {
func TestInEndpoint(t *testing.T) { epData.intf.Endpoints = []EndpointInfo{epData.ei}
defer func(i libusbIntf) { libusb = i }(libusb)
for _, tc := range []struct { for _, tc := range []struct {
desc string desc string
buf []byte buf []byte
@@ -84,20 +86,21 @@ func TestInEndpoint(t *testing.T) {
lib := newFakeLibusb() lib := newFakeLibusb()
libusb = lib libusb = lib
ep := InEndpoint{newEndpoint(nil, testBulkInSetting, testBulkInEP), time.Second} ep := newEndpoint(nil, epData.intf, epData.ei)
go func() { go func() {
fakeT := lib.waitForSubmitted() fakeT := lib.waitForSubmitted()
fakeT.length = tc.ret fakeT.length = tc.ret
fakeT.status = tc.status fakeT.status = tc.status
close(fakeT.done) close(fakeT.done)
}() }()
got, err := ep.Read(tc.buf) got, err := ep.transfer(tc.buf, time.Second)
if (err != nil) != tc.wantErr { if (err != nil) != tc.wantErr {
t.Errorf("%s: bulkInEP.Read(): got err: %v, err != nil is %v, want %v", tc.desc, err, err != nil, tc.wantErr) t.Errorf("%s, %s: ep.transfer(...): got err: %v, err != nil is %v, want %v", epData.ei, tc.desc, err, err != nil, tc.wantErr)
continue continue
} }
if got != tc.want { if got != tc.want {
t.Errorf("%s: bulkInEP.Read(): got %d bytes, want %d", tc.desc, got, tc.want) t.Errorf("%s, %s: ep.transfer(...): got %d bytes, want %d", epData.ei, tc.desc, got, tc.want)
}
} }
} }
} }