replace Endpoint interface with a real Endpoint struct.

This commit is contained in:
Sebastian Zagrodzki
2017-03-27 22:15:45 +02:00
parent 3b8abbe38b
commit 341fde410e
3 changed files with 12 additions and 19 deletions

View File

@@ -92,7 +92,7 @@ func (d *Device) Close() error {
return nil return nil
} }
func (d *Device) OpenEndpoint(cfgNum, ifNum, setNum, epNum uint8) (Endpoint, error) { func (d *Device) OpenEndpoint(cfgNum, ifNum, setNum, epNum uint8) (*Endpoint, error) {
var cfg *ConfigInfo var cfg *ConfigInfo
for _, c := range d.Configs { for _, c := range d.Configs {
if c.Config == cfgNum { if c.Config == cfgNum {

View File

@@ -20,14 +20,7 @@ import (
"time" "time"
) )
type Endpoint interface { type Endpoint struct {
Read(b []byte) (int, error)
Write(b []byte) (int, error)
Interface() InterfaceSetup
Info() EndpointInfo
}
type endpoint struct {
h *libusbDevHandle h *libusbDevHandle
InterfaceSetup InterfaceSetup
@@ -37,7 +30,7 @@ type endpoint struct {
writeTimeout time.Duration writeTimeout time.Duration
} }
func (e *endpoint) Read(buf []byte) (int, error) { func (e *Endpoint) Read(buf []byte) (int, error) {
if EndpointDirection(e.Address)&ENDPOINT_DIR_MASK != ENDPOINT_DIR_IN { if EndpointDirection(e.Address)&ENDPOINT_DIR_MASK != ENDPOINT_DIR_IN {
return 0, fmt.Errorf("usb: read: not an IN endpoint") return 0, fmt.Errorf("usb: read: not an IN endpoint")
} }
@@ -45,7 +38,7 @@ func (e *endpoint) Read(buf []byte) (int, error) {
return e.transfer(buf, e.readTimeout) return e.transfer(buf, e.readTimeout)
} }
func (e *endpoint) Write(buf []byte) (int, error) { func (e *Endpoint) Write(buf []byte) (int, error) {
if EndpointDirection(e.Address)&ENDPOINT_DIR_MASK != ENDPOINT_DIR_OUT { if EndpointDirection(e.Address)&ENDPOINT_DIR_MASK != ENDPOINT_DIR_OUT {
return 0, fmt.Errorf("usb: write: not an OUT endpoint") return 0, fmt.Errorf("usb: write: not an OUT endpoint")
} }
@@ -53,10 +46,10 @@ func (e *endpoint) Write(buf []byte) (int, error) {
return e.transfer(buf, e.writeTimeout) return e.transfer(buf, e.writeTimeout)
} }
func (e *endpoint) Interface() InterfaceSetup { return e.InterfaceSetup } func (e *Endpoint) Interface() InterfaceSetup { return e.InterfaceSetup }
func (e *endpoint) Info() EndpointInfo { return e.EndpointInfo } func (e *Endpoint) Info() EndpointInfo { return e.EndpointInfo }
func (e *endpoint) transfer(buf []byte, timeout time.Duration) (int, error) { func (e *Endpoint) transfer(buf []byte, timeout time.Duration) (int, error) {
if len(buf) == 0 { if len(buf) == 0 {
return 0, nil return 0, nil
} }
@@ -78,8 +71,8 @@ func (e *endpoint) transfer(buf []byte, timeout time.Duration) (int, error) {
return n, nil return n, nil
} }
func newEndpoint(h *libusbDevHandle, s InterfaceSetup, e EndpointInfo, rt, wt time.Duration) *endpoint { func newEndpoint(h *libusbDevHandle, s InterfaceSetup, e EndpointInfo, rt, wt time.Duration) *Endpoint {
return &endpoint{ return &Endpoint{
InterfaceSetup: s, InterfaceSetup: s,
EndpointInfo: e, EndpointInfo: e,
h: h, h: h,

View File

@@ -73,7 +73,7 @@ func TestEndpoint(t *testing.T) {
fakeT.status = tc.status fakeT.status = tc.status
close(fakeT.done) close(fakeT.done)
}() }()
opv := op.Func.Interface().(func(*endpoint, []byte) (int, error)) opv := op.Func.Interface().(func(*Endpoint, []byte) (int, error))
got, err := opv(ep, tc.buf) got, err := opv(ep, tc.buf)
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: bulkInEP.Read(): got err: %v, err != nil is %v, want %v", tc.desc, err, err != nil, tc.wantErr)
@@ -88,7 +88,7 @@ func TestEndpoint(t *testing.T) {
} }
func TestEndpointWrongDirection(t *testing.T) { func TestEndpointWrongDirection(t *testing.T) {
ep := &endpoint{ ep := &Endpoint{
InterfaceSetup: testBulkInSetup, InterfaceSetup: testBulkInSetup,
EndpointInfo: testBulkInEP, EndpointInfo: testBulkInEP,
} }
@@ -96,7 +96,7 @@ func TestEndpointWrongDirection(t *testing.T) {
if err == nil { if err == nil {
t.Error("bulkInEP.Write(): got nil error, want non-nil") t.Error("bulkInEP.Write(): got nil error, want non-nil")
} }
ep = &endpoint{ ep = &Endpoint{
InterfaceSetup: testIsoOutSetup, InterfaceSetup: testIsoOutSetup,
EndpointInfo: testIsoOutEP, EndpointInfo: testIsoOutEP,
} }