From b1dcaa119516e511bde97f725eac2566ff49a5ca Mon Sep 17 00:00:00 2001 From: Sebastian Zagrodzki Date: Mon, 10 Apr 2017 01:11:38 +0200 Subject: [PATCH] Add Device.OutEndpoint, simplify endpoint test. --- usb/device.go | 13 ++++ usb/endpoint.go | 2 +- usb/endpoint_test.go | 145 ++++++++++++++++++++++--------------------- 3 files changed, 88 insertions(+), 72 deletions(-) diff --git a/usb/device.go b/usb/device.go index aaa07bd..0a73c18 100644 --- a/usb/device.go +++ b/usb/device.go @@ -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 diff --git a/usb/endpoint.go b/usb/endpoint.go index 7ccd141..7563b4e 100644 --- a/usb/endpoint.go +++ b/usb/endpoint.go @@ -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 diff --git a/usb/endpoint_test.go b/usb/endpoint_test.go index 198923e..ed3b8d3 100644 --- a/usb/endpoint_test.go +++ b/usb/endpoint_test.go @@ -19,85 +19,88 @@ import ( "time" ) -// IN bulk endpoint -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) { +func TestEndpoint(t *testing.T) { defer func(i libusbIntf) { libusb = i }(libusb) - for _, tc := range []struct { - desc string - buf []byte - ret int - status TransferStatus - want int - wantErr bool + for _, epData := range []struct { + ei EndpointInfo + intf InterfaceSetting }{ { - desc: "empty buffer", - buf: nil, - ret: 10, - want: 0, + ei: EndpointInfo{ + Number: 2, + Direction: EndpointDirectionIn, + MaxPacketSize: 512, + TransferType: TransferTypeBulk, + }, + intf: InterfaceSetting{ + Number: 0, + Alternate: 0, + Class: ClassVendorSpec, + }, }, { - 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, + ei: EndpointInfo{ + Number: 6, + MaxPacketSize: 3 * 1024, + TransferType: TransferTypeIsochronous, + PollInterval: 125 * time.Microsecond, + UsageType: IsoUsageTypeData, + }, + intf: InterfaceSetting{ + Number: 0, + Alternate: 0, + Class: ClassVendorSpec, + }, }, } { - lib := newFakeLibusb() - libusb = lib + epData.intf.Endpoints = []EndpointInfo{epData.ei} + 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} - go func() { - fakeT := lib.waitForSubmitted() - fakeT.length = tc.ret - fakeT.status = tc.status - close(fakeT.done) - }() - got, err := ep.Read(tc.buf) - 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) - continue - } - if got != tc.want { - t.Errorf("%s: bulkInEP.Read(): got %d bytes, want %d", tc.desc, got, tc.want) + 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.transfer(tc.buf, time.Second) + if (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, %s: ep.transfer(...): got %d bytes, want %d", epData.ei, tc.desc, got, tc.want) + } } } }