diff --git a/usb/endpoint.go b/usb/endpoint.go index ac23ab1..0f5f9e5 100644 --- a/usb/endpoint.go +++ b/usb/endpoint.go @@ -35,9 +35,14 @@ type transferIntf interface { } type endpoint struct { - *Device + h *deviceHandle + InterfaceSetup EndpointInfo + + ReadTimeout time.Duration + WriteTimeout time.Duration + newUSBTransfer func([]byte, time.Duration) (transferIntf, error) } @@ -61,7 +66,7 @@ func (e *endpoint) Interface() InterfaceSetup { return e.InterfaceSetup } func (e *endpoint) Info() EndpointInfo { return e.EndpointInfo } func (e *endpoint) newRealUSBTransfer(buf []byte, timeout time.Duration) (transferIntf, error) { - return newUSBTransfer((*deviceHandle)(e.Device.handle), e.EndpointInfo, buf, timeout) + return newUSBTransfer(e.h, e.EndpointInfo, buf, timeout) } func (e *endpoint) transfer(buf []byte, timeout time.Duration) (int, error) { @@ -91,7 +96,9 @@ func (e *endpoint) transfer(buf []byte, timeout time.Duration) (int, error) { func newEndpoint(d *Device) *endpoint { ep := &endpoint{ - Device: d, + h: (*deviceHandle)(d.handle), + ReadTimeout: d.ReadTimeout, + WriteTimeout: d.WriteTimeout, } ep.newUSBTransfer = ep.newRealUSBTransfer return ep diff --git a/usb/endpoint_info_test.go b/usb/endpoint_info_test.go new file mode 100644 index 0000000..b212f95 --- /dev/null +++ b/usb/endpoint_info_test.go @@ -0,0 +1,30 @@ +// Copyright 2017 the gousb Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package usb + +// IN bulk endpoint +var testBulkInEP = EndpointInfo{ + Address: 0x82, + Attributes: uint8(TRANSFER_TYPE_BULK), + MaxPacketSize: 512, + PollInterval: 1, +} + +var testBulkInSetup = InterfaceSetup{ + Number: 0, + Alternate: 0, + IfClass: uint8(CLASS_VENDOR_SPEC), + Endpoints: []EndpointInfo{testBulkInEP}, +} diff --git a/usb/endpoint_test.go b/usb/endpoint_test.go new file mode 100644 index 0000000..be765a7 --- /dev/null +++ b/usb/endpoint_test.go @@ -0,0 +1,46 @@ +// Copyright 2017 the gousb Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package usb + +import ( + "testing" + "time" +) + +type fakeTransfer struct { + buf []byte +} + +func (*fakeTransfer) submit() error { return nil } +func (t *fakeTransfer) wait() (int, error) { return len(t.buf) / 2, nil } +func (*fakeTransfer) free() error { return nil } + +func TestEndpoint(t *testing.T) { + ep := endpoint{ + InterfaceSetup: testBulkInSetup, + EndpointInfo: testBulkInEP, + newUSBTransfer: func(buf []byte, timeout time.Duration) (transferIntf, error) { + return &fakeTransfer{buf}, nil + }, + } + b := make([]byte, 128) + got, err := ep.Read(b) + if err != nil { + t.Fatalf("bulkInEP.Read(): got error: %v, want nil", err) + } + if want := len(b)/2 + 1; got != want { + t.Errorf("bulkInEP.Read(): got %d bytes, want half of the buffer length: %d", got, want) + } +}