Use newEndpoint() for initialization. Add an indirection for

newUSBTransfer.
This commit is contained in:
Sebastian Zagrodzki
2017-02-19 15:35:35 +01:00
parent 360740e76c
commit 2efb1a354f
2 changed files with 21 additions and 4 deletions

View File

@@ -122,9 +122,7 @@ func (d *Device) Close() error {
}
func (d *Device) OpenEndpoint(conf, iface, setup, epoint uint8) (Endpoint, error) {
end := &endpoint{
Device: d,
}
end := newEndpoint(d)
var setAlternate bool
for _, c := range d.Configs {

View File

@@ -28,10 +28,17 @@ type Endpoint interface {
Info() EndpointInfo
}
type transferIntf interface {
submit() error
wait() (int, error)
free() error
}
type endpoint struct {
*Device
InterfaceSetup
EndpointInfo
newUSBTransfer func([]byte, time.Duration) (transferIntf, error)
}
func (e *endpoint) Read(buf []byte) (int, error) {
@@ -53,13 +60,17 @@ func (e *endpoint) Write(buf []byte) (int, error) {
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)
}
func (e *endpoint) transfer(buf []byte, timeout time.Duration) (int, error) {
if len(buf) == 0 {
return 0, nil
}
tt := e.TransferType()
t, err := newUSBTransfer((*deviceHandle)(e.Device.handle), e.EndpointInfo, buf, timeout)
t, err := e.newUSBTransfer(buf, timeout)
if err != nil {
return 0, err
}
@@ -77,3 +88,11 @@ func (e *endpoint) transfer(buf []byte, timeout time.Duration) (int, error) {
}
return n, err
}
func newEndpoint(d *Device) *endpoint {
ep := &endpoint{
Device: d,
}
ep.newUSBTransfer = ep.newRealUSBTransfer
return ep
}