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,85 +19,88 @@ import (
"time" "time"
) )
// IN bulk endpoint func TestEndpoint(t *testing.T) {
var testBulkInEP = EndpointInfo{
Number: 2,
Direction: EndpointDirectionIn,
MaxPacketSize: 512,
TransferType: TransferTypeBulk,
}
var testBulkInSetting = InterfaceSetting{
Number: 0,
Alternate: 0,
Class: ClassVendorSpec,
Endpoints: []EndpointInfo{testBulkInEP},
}
// OUT iso endpoint
var testIsoOutEP = EndpointInfo{
Number: 6,
MaxPacketSize: 3 * 1024,
TransferType: TransferTypeIsochronous,
PollInterval: 125 * time.Microsecond,
UsageType: IsoUsageTypeData,
}
var testIsoOutSetting = InterfaceSetting{
Number: 0,
Alternate: 0,
Class: ClassVendorSpec,
Endpoints: []EndpointInfo{testIsoOutEP},
}
func TestInEndpoint(t *testing.T) {
defer func(i libusbIntf) { libusb = i }(libusb) defer func(i libusbIntf) { libusb = i }(libusb)
for _, tc := range []struct { for _, epData := range []struct {
desc string ei EndpointInfo
buf []byte intf InterfaceSetting
ret int
status TransferStatus
want int
wantErr bool
}{ }{
{ {
desc: "empty buffer", ei: EndpointInfo{
buf: nil, Number: 2,
ret: 10, Direction: EndpointDirectionIn,
want: 0, MaxPacketSize: 512,
TransferType: TransferTypeBulk,
},
intf: InterfaceSetting{
Number: 0,
Alternate: 0,
Class: ClassVendorSpec,
},
}, },
{ {
desc: "128B buffer, 60 transferred", ei: EndpointInfo{
buf: make([]byte, 128), Number: 6,
ret: 60, MaxPacketSize: 3 * 1024,
want: 60, TransferType: TransferTypeIsochronous,
}, PollInterval: 125 * time.Microsecond,
{ UsageType: IsoUsageTypeData,
desc: "128B buffer, 10 transferred and then error", },
buf: make([]byte, 128), intf: InterfaceSetting{
ret: 10, Number: 0,
status: TransferError, Alternate: 0,
want: 10, Class: ClassVendorSpec,
wantErr: true, },
}, },
} { } {
lib := newFakeLibusb() epData.intf.Endpoints = []EndpointInfo{epData.ei}
libusb = lib for _, tc := range []struct {
desc string
buf []byte
ret int
status TransferStatus
want int
wantErr bool
}{
{
desc: "empty buffer",
buf: nil,
ret: 10,
want: 0,
},
{
desc: "128B buffer, 60 transferred",
buf: make([]byte, 128),
ret: 60,
want: 60,
},
{
desc: "128B buffer, 10 transferred and then error",
buf: make([]byte, 128),
ret: 10,
status: TransferError,
want: 10,
wantErr: true,
},
} {
lib := newFakeLibusb()
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)
}
} }
} }
} }