diff --git a/usb/fakelibusb_test.go b/usb/fakelibusb_test.go index 881ac1d..ebd042d 100644 --- a/usb/fakelibusb_test.go +++ b/usb/fakelibusb_test.go @@ -15,6 +15,7 @@ package usb import ( + "errors" "fmt" "sync" "time" @@ -22,13 +23,11 @@ import ( ) var ( - fakeDevices = map[*libusbDevice]*Descriptor{ + fakeDevices = []*Descriptor{ // Bus 001 Device 001: ID 9999:0001 // One config, one interface, one setup, // two endpoints: 0x01 OUT, 0x82 IN. - // TODO(https://github.com/golang/go/issues/19487): replace with - // new(libusbDevice) - (*libusbDevice)(unsafe.Pointer(uintptr(1))): &Descriptor{ + &Descriptor{ Bus: 1, Address: 1, Spec: USB_2_0, @@ -62,9 +61,7 @@ var ( // One config, two interfaces. interface #0 with no endpoints, // interface #1 with two alt setups with different packet sizes for // endpoints. Two isochronous endpoints, 0x05 OUT and 0x86 OUT. - // TODO(https://github.com/golang/go/issues/19487): replace with - // new(libusbDevice) - (*libusbDevice)(unsafe.Pointer(uintptr(2))): &Descriptor{ + &Descriptor{ Bus: 1, Address: 2, Spec: USB_2_0, @@ -136,6 +133,11 @@ var ( } ) +type fakeDevice struct { + desc *Descriptor + alt uint8 +} + type fakeTransfer struct { // done is the channel that needs to be closed when the transfer has finished. done chan struct{} @@ -149,16 +151,101 @@ type fakeTransfer struct { } type fakeLibusb struct { - libusbIntf - mu sync.Mutex + // fakeDevices has a map of + fakeDevices map[*libusbDevice]*fakeDevice // ts has a map of all allocated transfers, indexed by the pointer of // underlying libusbTransfer. ts map[*libusbTransfer]*fakeTransfer // submitted receives a fakeTransfers when submit() is called. submitted chan *fakeTransfer - // handlers is a map of device handles pointing at opened devices. - handlers map[*libusbDevHandle]*libusbDevice + // handles is a map of device handles pointing at opened devices. + handles map[*libusbDevHandle]*libusbDevice + // claims is a map of devices to a set of claimed interfaces + claims map[*libusbDevice]map[uint8]bool +} + +func (f *fakeLibusb) init() (*libusbContext, error) { return new(libusbContext), nil } +func (f *fakeLibusb) handleEvents(c *libusbContext, done <-chan struct{}) { <-done } +func (f *fakeLibusb) getDevices(*libusbContext) ([]*libusbDevice, error) { + ret := make([]*libusbDevice, 0, len(fakeDevices)) + for d := range f.fakeDevices { + ret = append(ret, d) + } + return ret, nil +} +func (f *fakeLibusb) exit(*libusbContext) {} +func (f *fakeLibusb) setDebug(*libusbContext, int) {} + +func (f *fakeLibusb) dereference(d *libusbDevice) {} +func (f *fakeLibusb) getDeviceDesc(d *libusbDevice) (*Descriptor, error) { + if dev, ok := f.fakeDevices[d]; ok { + return dev.desc, nil + } + return nil, fmt.Errorf("invalid USB device %p", d) +} +func (f *fakeLibusb) open(d *libusbDevice) (*libusbDevHandle, error) { + h := new(libusbDevHandle) + f.mu.Lock() + defer f.mu.Unlock() + f.handles[h] = d + return h, nil +} + +func (f *fakeLibusb) close(h *libusbDevHandle) { + f.mu.Lock() + defer f.mu.Unlock() + delete(f.handles, h) +} +func (f *fakeLibusb) reset(*libusbDevHandle) error { return nil } +func (f *fakeLibusb) control(*libusbDevHandle, time.Duration, uint8, uint8, uint16, uint16, []byte) (int, error) { + return 0, errors.New("not implemented") +} +func (f *fakeLibusb) getConfig(*libusbDevHandle) (uint8, error) { return 1, nil } +func (f *fakeLibusb) setConfig(d *libusbDevHandle, cfg uint8) error { + f.mu.Lock() + defer f.mu.Unlock() + if len(f.claims[f.handles[d]]) != 0 { + return fmt.Errorf("can't set device config while interfaces are claimed: %v", f.claims[f.handles[d]]) + } + if cfg != 1 { + return fmt.Errorf("device doesn't have config number %d", cfg) + } + return nil +} +func (f *fakeLibusb) getStringDesc(*libusbDevHandle, int) (string, error) { + return "", errors.New("not implemented") +} +func (f *fakeLibusb) setAutoDetach(*libusbDevHandle, int) error { return nil } + +func (f *fakeLibusb) claim(d *libusbDevHandle, intf uint8) error { + f.mu.Lock() + defer f.mu.Unlock() + c := f.claims[f.handles[d]] + if c == nil { + c = make(map[uint8]bool) + f.claims[f.handles[d]] = c + } + c[intf] = true + return nil +} +func (f *fakeLibusb) release(d *libusbDevHandle, intf uint8) { + f.mu.Lock() + defer f.mu.Unlock() + c := f.claims[f.handles[d]] + if c == nil { + return + } + c[intf] = false +} +func (f *fakeLibusb) setAlt(d *libusbDevHandle, intf, alt uint8) error { + f.mu.Lock() + defer f.mu.Unlock() + if !f.claims[f.handles[d]][intf] { + return fmt.Errorf("interface %d must be claimed before alt setup can be set", intf) + } + f.fakeDevices[f.handles[d]].alt = alt + return nil } func (f *fakeLibusb) alloc(_ *libusbDevHandle, _ uint8, _ TransferType, _ time.Duration, _ int, buf []byte) (*libusbTransfer, error) { @@ -168,7 +255,7 @@ func (f *fakeLibusb) alloc(_ *libusbDevHandle, _ uint8, _ TransferType, _ time.D f.ts[t] = &fakeTransfer{buf: buf} return t, nil } - +func (f *fakeLibusb) cancel(t *libusbTransfer) error { return errors.New("not implemented") } func (f *fakeLibusb) submit(t *libusbTransfer, done chan struct{}) error { f.mu.Lock() ft := f.ts[t] @@ -177,60 +264,36 @@ func (f *fakeLibusb) submit(t *libusbTransfer, done chan struct{}) error { f.submitted <- ft return nil } - -func (f *fakeLibusb) cancel(t *libusbTransfer) error { return nil } -func (f *fakeLibusb) free(t *libusbTransfer) { - f.mu.Lock() - defer f.mu.Unlock() - delete(f.ts, t) -} - func (f *fakeLibusb) data(t *libusbTransfer) (int, TransferStatus) { f.mu.Lock() defer f.mu.Unlock() return f.ts[t].length, f.ts[t].status } +func (f *fakeLibusb) free(t *libusbTransfer) { + f.mu.Lock() + defer f.mu.Unlock() + delete(f.ts, t) +} +func (f *fakeLibusb) setIsoPacketLengths(*libusbTransfer, uint32) {} func (f *fakeLibusb) waitForSubmitted() *fakeTransfer { return <-f.submitted } -func (f *fakeLibusb) getDevices(*libusbContext) ([]*libusbDevice, error) { - ret := make([]*libusbDevice, 0, len(fakeDevices)) - for d := range fakeDevices { - ret = append(ret, d) - } - return ret, nil -} - -func (f *fakeLibusb) getDeviceDesc(d *libusbDevice) (*Descriptor, error) { - if desc, ok := fakeDevices[d]; ok { - return desc, nil - } - return nil, fmt.Errorf("invalid USB device %p", d) -} - -func (f *fakeLibusb) dereference(d *libusbDevice) {} - -func (f *fakeLibusb) open(d *libusbDevice) (*libusbDevHandle, error) { - h := new(libusbDevHandle) - f.mu.Lock() - defer f.mu.Unlock() - f.handlers[h] = d - return h, nil -} - -func (f *fakeLibusb) close(h *libusbDevHandle) { - f.mu.Lock() - defer f.mu.Unlock() - delete(f.handlers, h) -} - func newFakeLibusb() *fakeLibusb { - return &fakeLibusb{ - ts: make(map[*libusbTransfer]*fakeTransfer), - submitted: make(chan *fakeTransfer, 10), - handlers: make(map[*libusbDevHandle]*libusbDevice), - libusbIntf: libusbImpl{}, + fl := &fakeLibusb{ + fakeDevices: make(map[*libusbDevice]*fakeDevice), + ts: make(map[*libusbTransfer]*fakeTransfer), + submitted: make(chan *fakeTransfer, 10), + handles: make(map[*libusbDevHandle]*libusbDevice), + claims: make(map[*libusbDevice]map[uint8]bool), } + for i, d := range fakeDevices { + // TODO(https://golang.org/issue/19487): use new(libusbDevice) after Go 1.9 + fl.fakeDevices[(*libusbDevice)(unsafe.Pointer(uintptr(i)))] = &fakeDevice{ + desc: d, + alt: 0, + } + } + return fl }