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
}
// InEndpoint prepares an IN endpoint for transfer.
func (d *Device) InEndpoint(cfgNum, ifNum, setNum, epNum uint8) (*InEndpoint, error) {
ep, err := d.openEndpoint(cfgNum, ifNum, setNum, endpointAddr(epNum, EndpointDirectionIn))
if err != nil {
@@ -193,6 +194,18 @@ func (d *Device) InEndpoint(cfgNum, ifNum, setNum, epNum uint8) (*InEndpoint, er
}, 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
// number. The first supported language is always used and the returned
// 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 {
*endpoint
timeout time.Duration

View File

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