@@ -15,13 +15,8 @@
|
||||
|
||||
package usb
|
||||
|
||||
// #include <libusb.h>
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type EndpointInfo struct {
|
||||
@@ -87,73 +82,3 @@ type ConfigInfo struct {
|
||||
func (c ConfigInfo) String() string {
|
||||
return fmt.Sprintf("Config %02x", c.Config)
|
||||
}
|
||||
|
||||
func newConfig(dev *C.libusb_device, cfg *C.struct_libusb_config_descriptor) ConfigInfo {
|
||||
c := ConfigInfo{
|
||||
Config: uint8(cfg.bConfigurationValue),
|
||||
Attributes: uint8(cfg.bmAttributes),
|
||||
MaxPower: uint8(cfg.MaxPower),
|
||||
}
|
||||
|
||||
var ifaces []C.struct_libusb_interface
|
||||
*(*reflect.SliceHeader)(unsafe.Pointer(&ifaces)) = reflect.SliceHeader{
|
||||
Data: uintptr(unsafe.Pointer(cfg._interface)),
|
||||
Len: int(cfg.bNumInterfaces),
|
||||
Cap: int(cfg.bNumInterfaces),
|
||||
}
|
||||
c.Interfaces = make([]InterfaceInfo, 0, len(ifaces))
|
||||
for _, iface := range ifaces {
|
||||
if iface.num_altsetting == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
var alts []C.struct_libusb_interface_descriptor
|
||||
*(*reflect.SliceHeader)(unsafe.Pointer(&alts)) = reflect.SliceHeader{
|
||||
Data: uintptr(unsafe.Pointer(iface.altsetting)),
|
||||
Len: int(iface.num_altsetting),
|
||||
Cap: int(iface.num_altsetting),
|
||||
}
|
||||
descs := make([]InterfaceSetup, 0, len(alts))
|
||||
for _, alt := range alts {
|
||||
i := InterfaceSetup{
|
||||
Number: uint8(alt.bInterfaceNumber),
|
||||
Alternate: uint8(alt.bAlternateSetting),
|
||||
IfClass: uint8(alt.bInterfaceClass),
|
||||
IfSubClass: uint8(alt.bInterfaceSubClass),
|
||||
IfProtocol: uint8(alt.bInterfaceProtocol),
|
||||
}
|
||||
var ends []C.struct_libusb_endpoint_descriptor
|
||||
*(*reflect.SliceHeader)(unsafe.Pointer(&ends)) = reflect.SliceHeader{
|
||||
Data: uintptr(unsafe.Pointer(alt.endpoint)),
|
||||
Len: int(alt.bNumEndpoints),
|
||||
Cap: int(alt.bNumEndpoints),
|
||||
}
|
||||
i.Endpoints = make([]EndpointInfo, 0, len(ends))
|
||||
for _, end := range ends {
|
||||
ei := EndpointInfo{
|
||||
Address: uint8(end.bEndpointAddress),
|
||||
Attributes: uint8(end.bmAttributes),
|
||||
MaxPacketSize: uint16(end.wMaxPacketSize),
|
||||
PollInterval: uint8(end.bInterval),
|
||||
RefreshRate: uint8(end.bRefresh),
|
||||
SynchAddress: uint8(end.bSynchAddress),
|
||||
}
|
||||
if ei.TransferType() == TRANSFER_TYPE_ISOCHRONOUS {
|
||||
// bits 0-10 identify the packet size, bits 11-12 are the number of additional transactions per microframe.
|
||||
// Don't use libusb_get_max_iso_packet_size, as it has a bug where it returns the same value
|
||||
// regardless of alternative setting used, where different alternative settings might define different
|
||||
// max packet sizes.
|
||||
// See http://libusb.org/ticket/77 for more background.
|
||||
ei.MaxIsoPacket = uint32(end.wMaxPacketSize) & 0x07ff * (uint32(end.wMaxPacketSize)>>11&3 + 1)
|
||||
}
|
||||
i.Endpoints = append(i.Endpoints, ei)
|
||||
}
|
||||
descs = append(descs, i)
|
||||
}
|
||||
c.Interfaces = append(c.Interfaces, InterfaceInfo{
|
||||
Number: descs[0].Number,
|
||||
Setups: descs,
|
||||
})
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
@@ -15,9 +15,6 @@
|
||||
|
||||
package usb
|
||||
|
||||
// #include <libusb.h>
|
||||
import "C"
|
||||
|
||||
type Descriptor struct {
|
||||
// Bus information
|
||||
Bus uint8 // The bus on which the device was detected
|
||||
@@ -39,34 +36,3 @@ type Descriptor struct {
|
||||
// Configuration information
|
||||
Configs []ConfigInfo
|
||||
}
|
||||
|
||||
func newDescriptor(dev *C.libusb_device) (*Descriptor, error) {
|
||||
var desc C.struct_libusb_device_descriptor
|
||||
if errno := C.libusb_get_device_descriptor(dev, &desc); errno < 0 {
|
||||
return nil, usbError(errno)
|
||||
}
|
||||
|
||||
// Enumerate configurations
|
||||
var cfgs []ConfigInfo
|
||||
for i := 0; i < int(desc.bNumConfigurations); i++ {
|
||||
var cfg *C.struct_libusb_config_descriptor
|
||||
if errno := C.libusb_get_config_descriptor(dev, C.uint8_t(i), &cfg); errno < 0 {
|
||||
return nil, usbError(errno)
|
||||
}
|
||||
cfgs = append(cfgs, newConfig(dev, cfg))
|
||||
C.libusb_free_config_descriptor(cfg)
|
||||
}
|
||||
|
||||
return &Descriptor{
|
||||
Bus: uint8(C.libusb_get_bus_number(dev)),
|
||||
Address: uint8(C.libusb_get_device_address(dev)),
|
||||
Spec: BCD(desc.bcdUSB),
|
||||
Device: BCD(desc.bcdDevice),
|
||||
Vendor: ID(desc.idVendor),
|
||||
Product: ID(desc.idProduct),
|
||||
Class: uint8(desc.bDeviceClass),
|
||||
SubClass: uint8(desc.bDeviceSubClass),
|
||||
Protocol: uint8(desc.bDeviceProtocol),
|
||||
Configs: cfgs,
|
||||
}, nil
|
||||
}
|
||||
|
188
usb/device.go
188
usb/device.go
@@ -15,15 +15,10 @@
|
||||
|
||||
package usb
|
||||
|
||||
// #include <libusb.h>
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sync"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var DefaultReadTimeout = 1 * time.Second
|
||||
@@ -31,7 +26,7 @@ var DefaultWriteTimeout = 1 * time.Second
|
||||
var DefaultControlTimeout = 250 * time.Millisecond //5 * time.Second
|
||||
|
||||
type Device struct {
|
||||
handle *C.libusb_device_handle
|
||||
handle *libusbDevHandle
|
||||
|
||||
// Embed the device information for easy access
|
||||
*Descriptor
|
||||
@@ -46,7 +41,7 @@ type Device struct {
|
||||
claimed map[uint8]int
|
||||
}
|
||||
|
||||
func newDevice(handle *C.libusb_device_handle, desc *Descriptor) *Device {
|
||||
func newDevice(handle *libusbDevHandle, desc *Descriptor) *Device {
|
||||
ifaces := 0
|
||||
d := &Device{
|
||||
handle: handle,
|
||||
@@ -62,48 +57,24 @@ func newDevice(handle *C.libusb_device_handle, desc *Descriptor) *Device {
|
||||
}
|
||||
|
||||
func (d *Device) Reset() error {
|
||||
if errno := C.libusb_reset_device(d.handle); errno != 0 {
|
||||
return usbError(errno)
|
||||
}
|
||||
return nil
|
||||
return libusb.reset(d.handle)
|
||||
}
|
||||
|
||||
func (d *Device) Control(rType, request uint8, val, idx uint16, data []byte) (int, error) {
|
||||
//log.Printf("control xfer: %d:%d/%d:%d %x", idx, rType, request, val, string(data))
|
||||
dataSlice := (*reflect.SliceHeader)(unsafe.Pointer(&data))
|
||||
n := C.libusb_control_transfer(
|
||||
d.handle,
|
||||
C.uint8_t(rType),
|
||||
C.uint8_t(request),
|
||||
C.uint16_t(val),
|
||||
C.uint16_t(idx),
|
||||
(*C.uchar)(unsafe.Pointer(dataSlice.Data)),
|
||||
C.uint16_t(len(data)),
|
||||
C.uint(d.ControlTimeout/time.Millisecond))
|
||||
if n < 0 {
|
||||
return int(n), usbError(n)
|
||||
}
|
||||
return int(n), nil
|
||||
return libusb.control(d.handle, d.ControlTimeout, rType, request, val, idx, data)
|
||||
}
|
||||
|
||||
// ActiveConfig returns the config id (not the index) of the active configuration.
|
||||
// This corresponds to the ConfigInfo.Config field.
|
||||
func (d *Device) ActiveConfig() (uint8, error) {
|
||||
var cfg C.int
|
||||
if errno := C.libusb_get_configuration(d.handle, &cfg); errno < 0 {
|
||||
return 0, usbError(errno)
|
||||
}
|
||||
return uint8(cfg), nil
|
||||
return libusb.getConfig(d.handle)
|
||||
}
|
||||
|
||||
// SetConfig attempts to change the active configuration.
|
||||
// The cfg provided is the config id (not the index) of the configuration to set,
|
||||
// which corresponds to the ConfigInfo.Config field.
|
||||
func (d *Device) SetConfig(cfg uint8) error {
|
||||
if errno := C.libusb_set_configuration(d.handle, C.int(cfg)); errno < 0 {
|
||||
return usbError(errno)
|
||||
}
|
||||
return nil
|
||||
return libusb.setConfig(d.handle, cfg)
|
||||
}
|
||||
|
||||
// Close the device.
|
||||
@@ -114,79 +85,89 @@ func (d *Device) Close() error {
|
||||
d.lock.Lock()
|
||||
defer d.lock.Unlock()
|
||||
for iface := range d.claimed {
|
||||
C.libusb_release_interface(d.handle, C.int(iface))
|
||||
libusb.release(d.handle, iface)
|
||||
}
|
||||
C.libusb_close(d.handle)
|
||||
libusb.close(d.handle)
|
||||
d.handle = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Device) OpenEndpoint(conf, iface, setup, epoint uint8) (Endpoint, error) {
|
||||
end := newEndpoint(d)
|
||||
func (d *Device) OpenEndpoint(cfgNum, ifNum, setNum, epNum uint8) (Endpoint, error) {
|
||||
var cfg *ConfigInfo
|
||||
for _, c := range d.Configs {
|
||||
if c.Config == cfgNum {
|
||||
debug.Printf("found conf: %#v\n", c)
|
||||
cfg = &c
|
||||
break
|
||||
}
|
||||
}
|
||||
if cfg == nil {
|
||||
return nil, fmt.Errorf("usb: unknown configuration %02x", cfgNum)
|
||||
}
|
||||
|
||||
var intf *InterfaceInfo
|
||||
for _, i := range cfg.Interfaces {
|
||||
if i.Number == ifNum {
|
||||
debug.Printf("found iface: %#v\n", i)
|
||||
intf = &i
|
||||
break
|
||||
}
|
||||
}
|
||||
if intf == nil {
|
||||
return nil, fmt.Errorf("usb: unknown interface %02x", ifNum)
|
||||
}
|
||||
|
||||
var setAlternate bool
|
||||
for _, c := range d.Configs {
|
||||
if c.Config != conf {
|
||||
continue
|
||||
var ifs *InterfaceSetup
|
||||
for i, s := range intf.Setups {
|
||||
if s.Alternate == setNum {
|
||||
setAlternate = i != 0
|
||||
debug.Printf("found setup: %#v [default: %v]\n", s, !setAlternate)
|
||||
ifs = &s
|
||||
}
|
||||
debug.Printf("found conf: %#v\n", c)
|
||||
for _, i := range c.Interfaces {
|
||||
if i.Number != iface {
|
||||
continue
|
||||
}
|
||||
debug.Printf("found iface: %#v\n", i)
|
||||
for i, s := range i.Setups {
|
||||
if s.Alternate != setup {
|
||||
continue
|
||||
}
|
||||
setAlternate = i != 0
|
||||
|
||||
debug.Printf("found setup: %#v [default: %v]\n", s, !setAlternate)
|
||||
for _, e := range s.Endpoints {
|
||||
debug.Printf("ep %02x search: %#v\n", epoint, s)
|
||||
if e.Address != epoint {
|
||||
continue
|
||||
}
|
||||
end.InterfaceSetup = s
|
||||
end.EndpointInfo = e
|
||||
goto found
|
||||
}
|
||||
return nil, fmt.Errorf("usb: unknown endpoint %02x", epoint)
|
||||
}
|
||||
return nil, fmt.Errorf("usb: unknown setup %02x", setup)
|
||||
}
|
||||
return nil, fmt.Errorf("usb: unknown interface %02x", iface)
|
||||
}
|
||||
return nil, fmt.Errorf("usb: unknown configuration %02x", conf)
|
||||
if ifs == nil {
|
||||
return nil, fmt.Errorf("usb: unknown setup %02x", setNum)
|
||||
}
|
||||
|
||||
found:
|
||||
var ep *EndpointInfo
|
||||
for _, e := range ifs.Endpoints {
|
||||
if e.Address == epNum {
|
||||
debug.Printf("found ep %02x in %#v\n", epNum, *ifs)
|
||||
ep = &e
|
||||
}
|
||||
}
|
||||
if ep == nil {
|
||||
return nil, fmt.Errorf("usb: unknown endpoint %02x", epNum)
|
||||
}
|
||||
|
||||
end := newEndpoint(d.handle, *ifs, *ep, d.ReadTimeout, d.WriteTimeout)
|
||||
|
||||
// Set the configuration
|
||||
var activeConf C.int
|
||||
if errno := C.libusb_get_configuration(d.handle, &activeConf); errno < 0 {
|
||||
return nil, fmt.Errorf("usb: getcfg: %s", usbError(errno))
|
||||
activeConf, err := libusb.getConfig(d.handle)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("usb: getcfg: %s", err)
|
||||
}
|
||||
if int(activeConf) != int(conf) {
|
||||
if errno := C.libusb_set_configuration(d.handle, C.int(conf)); errno < 0 {
|
||||
return nil, fmt.Errorf("usb: setcfg: %s", usbError(errno))
|
||||
if activeConf != cfgNum {
|
||||
if err := libusb.setConfig(d.handle, cfgNum); err != nil {
|
||||
return nil, fmt.Errorf("usb: setcfg: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Claim the interface
|
||||
if errno := C.libusb_claim_interface(d.handle, C.int(iface)); errno < 0 {
|
||||
return nil, fmt.Errorf("usb: claim: %s", usbError(errno))
|
||||
if err := libusb.claim(d.handle, ifNum); err != nil {
|
||||
return nil, fmt.Errorf("usb: claim: %s", err)
|
||||
}
|
||||
|
||||
// Increment the claim count
|
||||
d.lock.Lock()
|
||||
d.claimed[iface]++
|
||||
d.claimed[ifNum]++
|
||||
d.lock.Unlock() // unlock immediately because the next calls may block
|
||||
|
||||
// Choose the alternate
|
||||
if setAlternate {
|
||||
if errno := C.libusb_set_interface_alt_setting(d.handle, C.int(iface), C.int(setup)); errno < 0 {
|
||||
debug.Printf("altsetting error: %s", usbError(errno))
|
||||
return nil, fmt.Errorf("usb: setalt: %s", usbError(errno))
|
||||
if err := libusb.setAlt(d.handle, ifNum, setNum); err != nil {
|
||||
return nil, fmt.Errorf("usb: setalt: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,26 +175,7 @@ found:
|
||||
}
|
||||
|
||||
func (d *Device) GetStringDescriptor(desc_index int) (string, error) {
|
||||
|
||||
// allocate 200-byte array limited the length of string descriptor
|
||||
goBuffer := make([]byte, 200)
|
||||
|
||||
// get string descriptor from libusb. if errno < 0 then there are any errors.
|
||||
// if errno >= 0; it is a length of result string descriptor
|
||||
errno := C.libusb_get_string_descriptor_ascii(
|
||||
d.handle,
|
||||
C.uint8_t(desc_index),
|
||||
(*C.uchar)(unsafe.Pointer(&goBuffer[0])),
|
||||
200)
|
||||
|
||||
// if any errors occur
|
||||
if errno < 0 {
|
||||
return "", fmt.Errorf("usb: getstr: %s", usbError(errno))
|
||||
}
|
||||
// convert slice of byte to string with limited length from errno
|
||||
stringDescriptor := string(goBuffer[:errno])
|
||||
|
||||
return stringDescriptor, nil
|
||||
return libusb.getStringDesc(d.handle, desc_index)
|
||||
}
|
||||
|
||||
// SetAutoDetach enables/disables libusb's automatic kernel driver detachment.
|
||||
@@ -221,20 +183,12 @@ func (d *Device) GetStringDescriptor(desc_index int) (string, error) {
|
||||
// on the interface and reattach it when releasing the interface.
|
||||
// Automatic kernel driver detachment is disabled on newly opened device handles by default.
|
||||
func (d *Device) SetAutoDetach(autodetach bool) error {
|
||||
autodetachInt := 0
|
||||
if autodetach {
|
||||
var autodetachInt int
|
||||
switch autodetach {
|
||||
case true:
|
||||
autodetachInt = 1
|
||||
case false:
|
||||
autodetachInt = 0
|
||||
}
|
||||
|
||||
err := C.libusb_set_auto_detach_kernel_driver(
|
||||
d.handle,
|
||||
C.int(autodetachInt),
|
||||
)
|
||||
|
||||
// TODO LIBUSB_ERROR_NOT_SUPPORTED (-12) handling
|
||||
// if any errors occur
|
||||
if err != C.int(SUCCESS) {
|
||||
return fmt.Errorf("usb: setautodetach: %s", usbError(err))
|
||||
}
|
||||
return nil
|
||||
return libusb.setAutoDetach(d.handle, autodetachInt)
|
||||
}
|
||||
|
@@ -27,22 +27,14 @@ type Endpoint interface {
|
||||
Info() EndpointInfo
|
||||
}
|
||||
|
||||
type transferIntf interface {
|
||||
submit() error
|
||||
wait() (int, error)
|
||||
free() error
|
||||
}
|
||||
|
||||
type endpoint struct {
|
||||
h *deviceHandle
|
||||
h *libusbDevHandle
|
||||
|
||||
InterfaceSetup
|
||||
EndpointInfo
|
||||
|
||||
readTimeout time.Duration
|
||||
writeTimeout time.Duration
|
||||
|
||||
newUSBTransfer func([]byte, time.Duration) (transferIntf, error)
|
||||
}
|
||||
|
||||
func (e *endpoint) Read(buf []byte) (int, error) {
|
||||
@@ -64,16 +56,12 @@ 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) newLibUSBTransfer(buf []byte, timeout time.Duration) (transferIntf, error) {
|
||||
return newUSBTransfer(e.h, e.EndpointInfo, buf, timeout)
|
||||
}
|
||||
|
||||
func (e *endpoint) transfer(buf []byte, timeout time.Duration) (int, error) {
|
||||
if len(buf) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
t, err := e.newUSBTransfer(buf, timeout)
|
||||
t, err := newUSBTransfer(e.h, e.EndpointInfo, buf, timeout)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -90,12 +78,12 @@ func (e *endpoint) transfer(buf []byte, timeout time.Duration) (int, error) {
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func newEndpoint(d *Device) *endpoint {
|
||||
ep := &endpoint{
|
||||
h: (*deviceHandle)(d.handle),
|
||||
readTimeout: d.ReadTimeout,
|
||||
writeTimeout: d.WriteTimeout,
|
||||
func newEndpoint(h *libusbDevHandle, s InterfaceSetup, e EndpointInfo, rt, wt time.Duration) *endpoint {
|
||||
return &endpoint{
|
||||
InterfaceSetup: s,
|
||||
EndpointInfo: e,
|
||||
h: h,
|
||||
readTimeout: rt,
|
||||
writeTimeout: wt,
|
||||
}
|
||||
ep.newUSBTransfer = ep.newLibUSBTransfer
|
||||
return ep
|
||||
}
|
||||
|
@@ -15,23 +15,13 @@
|
||||
package usb
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type fakeTransfer struct {
|
||||
buf []byte
|
||||
ret int
|
||||
err error
|
||||
}
|
||||
|
||||
func (t *fakeTransfer) submit() error { return nil }
|
||||
func (t *fakeTransfer) wait() (int, error) { return t.ret, t.err }
|
||||
func (t *fakeTransfer) free() error { return nil }
|
||||
|
||||
func TestEndpoint(t *testing.T) {
|
||||
defer func(i libusbIntf) { libusb = i }(libusb)
|
||||
for _, epCfg := range []struct {
|
||||
method string
|
||||
InterfaceSetup
|
||||
@@ -42,35 +32,55 @@ func TestEndpoint(t *testing.T) {
|
||||
} {
|
||||
t.Run(epCfg.method, func(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
desc string
|
||||
buf []byte
|
||||
ret int
|
||||
err error
|
||||
status TransferStatus
|
||||
want int
|
||||
wantErr bool
|
||||
}{
|
||||
{buf: nil, ret: 10, want: 0},
|
||||
{buf: make([]byte, 128), ret: 60, want: 60},
|
||||
{buf: make([]byte, 128), ret: 10, err: errors.New("some error"), want: 10, wantErr: true},
|
||||
{
|
||||
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: LIBUSB_TRANSFER_ERROR,
|
||||
want: 10,
|
||||
wantErr: true,
|
||||
},
|
||||
} {
|
||||
ep := &endpoint{
|
||||
InterfaceSetup: epCfg.InterfaceSetup,
|
||||
EndpointInfo: epCfg.EndpointInfo,
|
||||
newUSBTransfer: func(buf []byte, timeout time.Duration) (transferIntf, error) {
|
||||
return &fakeTransfer{buf: buf, ret: tc.ret, err: tc.err}, nil
|
||||
},
|
||||
}
|
||||
lib := newFakeLibusb()
|
||||
libusb = lib
|
||||
ep := newEndpoint(nil, epCfg.InterfaceSetup, epCfg.EndpointInfo, time.Second, time.Second)
|
||||
op, ok := reflect.TypeOf(ep).MethodByName(epCfg.method)
|
||||
if !ok {
|
||||
t.Fatalf("method %s not found in endpoint struct", epCfg.method)
|
||||
}
|
||||
go func() {
|
||||
fakeT := lib.waitForSubmitted()
|
||||
fakeT.length = tc.ret
|
||||
fakeT.status = tc.status
|
||||
close(fakeT.done)
|
||||
}()
|
||||
opv := op.Func.Interface().(func(*endpoint, []byte) (int, error))
|
||||
got, err := opv(ep, tc.buf)
|
||||
if (err != nil) != tc.wantErr {
|
||||
t.Errorf("bulkInEP.Read(): got err: %v, err != nil is %v, want %v", 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)
|
||||
continue
|
||||
}
|
||||
if got != tc.want {
|
||||
t.Errorf("bulkInEP.Read(): got %d bytes, want %d", got, tc.want)
|
||||
t.Errorf("%s: bulkInEP.Read(): got %d bytes, want %d", tc.desc, got, tc.want)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@@ -28,6 +28,14 @@ func (e usbError) Error() string {
|
||||
return fmt.Sprintf("libusb: %s [code %d]", usbErrorString[e], int(e))
|
||||
}
|
||||
|
||||
func fromUSBError(errno C.int) error {
|
||||
err := usbError(errno)
|
||||
if err == SUCCESS {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
const (
|
||||
SUCCESS usbError = C.LIBUSB_SUCCESS
|
||||
ERROR_IO usbError = C.LIBUSB_ERROR_IO
|
||||
|
85
usb/fakelibusb_test.go
Normal file
85
usb/fakelibusb_test.go
Normal file
@@ -0,0 +1,85 @@
|
||||
// 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 (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type fakeTransfer struct {
|
||||
// done is the channel that needs to be closed when the transfer has finished.
|
||||
done chan struct{}
|
||||
// buf is the slice for reading/writing data between the submit() and wait() returning.
|
||||
buf []byte
|
||||
// status will be returned by wait() on this transfer
|
||||
status TransferStatus
|
||||
// length is the number of bytes used from the buffer (write) or available
|
||||
// in the buffer (read).
|
||||
length int
|
||||
}
|
||||
|
||||
type fakeLibusb struct {
|
||||
libusbIntf
|
||||
|
||||
mu sync.Mutex
|
||||
// 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
|
||||
}
|
||||
|
||||
func (f *fakeLibusb) alloc(_ *libusbDevHandle, _ uint8, _ TransferType, _ time.Duration, _ int, buf []byte) (*libusbTransfer, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
t := new(libusbTransfer)
|
||||
f.ts[t] = &fakeTransfer{buf: buf}
|
||||
return t, nil
|
||||
}
|
||||
|
||||
func (f *fakeLibusb) submit(t *libusbTransfer, done chan struct{}) error {
|
||||
f.mu.Lock()
|
||||
ft := f.ts[t]
|
||||
f.mu.Unlock()
|
||||
ft.done = done
|
||||
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) waitForSubmitted() *fakeTransfer {
|
||||
return <-f.submitted
|
||||
}
|
||||
|
||||
func newFakeLibusb() *fakeLibusb {
|
||||
return &fakeLibusb{
|
||||
ts: make(map[*libusbTransfer]*fakeTransfer),
|
||||
submitted: make(chan *fakeTransfer, 10),
|
||||
libusbIntf: libusbImpl{},
|
||||
}
|
||||
}
|
388
usb/libusb.go
Normal file
388
usb/libusb.go
Normal file
@@ -0,0 +1,388 @@
|
||||
// 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 (
|
||||
"fmt"
|
||||
"log"
|
||||
"reflect"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
/*
|
||||
#cgo pkg-config: libusb-1.0
|
||||
#include <libusb.h>
|
||||
|
||||
int compact_iso_data(struct libusb_transfer *xfer, unsigned char *status);
|
||||
int submit(struct libusb_transfer *xfer);
|
||||
*/
|
||||
import "C"
|
||||
|
||||
type libusbContext C.libusb_context
|
||||
type libusbDevice C.libusb_device
|
||||
type libusbDevHandle C.libusb_device_handle
|
||||
type libusbTransfer C.struct_libusb_transfer
|
||||
type libusbIso C.struct_libusb_iso_packet_descriptor
|
||||
|
||||
// libusbIntf is a set of trivial idiomatic Go wrappers around libusb C functions.
|
||||
// The underlying code is generally not testable or difficult to test,
|
||||
// since libusb interacts directly with the host USB stack.
|
||||
//
|
||||
// All functions here should operate on types defined on C.libusb* data types,
|
||||
// and occasionally on convenience data types (like TransferType or Descriptor).
|
||||
type libusbIntf interface {
|
||||
// context
|
||||
init() (*libusbContext, error)
|
||||
handleEvents(*libusbContext, <-chan struct{})
|
||||
getDevices(*libusbContext) ([]*libusbDevice, error)
|
||||
exit(*libusbContext)
|
||||
setDebug(*libusbContext, int)
|
||||
openVIDPID(*libusbContext, int, int) (*libusbDevice, *libusbDevHandle, error)
|
||||
|
||||
// device
|
||||
dereference(*libusbDevice)
|
||||
getDeviceDesc(*libusbDevice) (*Descriptor, error)
|
||||
open(*libusbDevice) (*libusbDevHandle, error)
|
||||
|
||||
close(*libusbDevHandle)
|
||||
reset(*libusbDevHandle) error
|
||||
control(*libusbDevHandle, time.Duration, uint8, uint8, uint16, uint16, []byte) (int, error)
|
||||
getConfig(*libusbDevHandle) (uint8, error)
|
||||
setConfig(*libusbDevHandle, uint8) error
|
||||
getStringDesc(*libusbDevHandle, int) (string, error)
|
||||
setAutoDetach(*libusbDevHandle, int) error
|
||||
|
||||
// interface
|
||||
claim(*libusbDevHandle, uint8) error
|
||||
release(*libusbDevHandle, uint8)
|
||||
setAlt(*libusbDevHandle, uint8, uint8) error
|
||||
|
||||
// transfer
|
||||
alloc(*libusbDevHandle, uint8, TransferType, time.Duration, int, []byte) (*libusbTransfer, error)
|
||||
cancel(*libusbTransfer) error
|
||||
submit(*libusbTransfer, chan struct{}) error
|
||||
data(*libusbTransfer) (int, TransferStatus)
|
||||
free(*libusbTransfer)
|
||||
setIsoPacketLengths(*libusbTransfer, uint32)
|
||||
}
|
||||
|
||||
// libusbImpl is an implementation of libusbIntf using real CGo-wrapped libusb.
|
||||
type libusbImpl struct{}
|
||||
|
||||
func (libusbImpl) init() (*libusbContext, error) {
|
||||
var ctx *C.libusb_context
|
||||
if err := fromUSBError(C.libusb_init(&ctx)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return (*libusbContext)(ctx), nil
|
||||
}
|
||||
|
||||
func (libusbImpl) handleEvents(c *libusbContext, done <-chan struct{}) {
|
||||
tv := C.struct_timeval{tv_usec: 100e3}
|
||||
for {
|
||||
select {
|
||||
case <-done:
|
||||
return
|
||||
default:
|
||||
}
|
||||
if errno := C.libusb_handle_events_timeout_completed((*C.libusb_context)(c), &tv, nil); errno < 0 {
|
||||
log.Printf("handle_events: error: %s", usbError(errno))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (libusbImpl) getDevices(ctx *libusbContext) ([]*libusbDevice, error) {
|
||||
var list **C.libusb_device
|
||||
cnt := C.libusb_get_device_list((*C.libusb_context)(ctx), &list)
|
||||
if cnt < 0 {
|
||||
return nil, fromUSBError(C.int(cnt))
|
||||
}
|
||||
var devs []*C.libusb_device
|
||||
*(*reflect.SliceHeader)(unsafe.Pointer(&devs)) = reflect.SliceHeader{
|
||||
Data: uintptr(unsafe.Pointer(list)),
|
||||
Len: int(cnt),
|
||||
Cap: int(cnt),
|
||||
}
|
||||
var ret []*libusbDevice
|
||||
for _, d := range devs {
|
||||
ret = append(ret, (*libusbDevice)(d))
|
||||
}
|
||||
// devices will be dereferenced later, during close.
|
||||
C.libusb_free_device_list(list, 0)
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (libusbImpl) exit(c *libusbContext) {
|
||||
C.libusb_exit((*C.libusb_context)(c))
|
||||
}
|
||||
|
||||
func (libusbImpl) setDebug(c *libusbContext, lvl int) {
|
||||
C.libusb_set_debug((*C.libusb_context)(c), C.int(lvl))
|
||||
}
|
||||
|
||||
func (libusbImpl) openVIDPID(ctx *libusbContext, vid, pid int) (*libusbDevice, *libusbDevHandle, error) {
|
||||
h := C.libusb_open_device_with_vid_pid((*C.libusb_context)(ctx), (C.uint16_t)(vid), (C.uint16_t)(pid))
|
||||
if h == nil {
|
||||
return nil, nil, ERROR_NOT_FOUND
|
||||
}
|
||||
dev := C.libusb_get_device(h)
|
||||
if dev == nil {
|
||||
return nil, nil, ERROR_NO_DEVICE
|
||||
}
|
||||
C.libusb_ref_device(dev)
|
||||
return (*libusbDevice)(dev), (*libusbDevHandle)(h), nil
|
||||
}
|
||||
|
||||
func (libusbImpl) getDeviceDesc(d *libusbDevice) (*Descriptor, error) {
|
||||
var desc C.struct_libusb_device_descriptor
|
||||
if err := fromUSBError(C.libusb_get_device_descriptor((*C.libusb_device)(d), &desc)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Enumerate configurations
|
||||
var cfgs []ConfigInfo
|
||||
for i := 0; i < int(desc.bNumConfigurations); i++ {
|
||||
var cfg *C.struct_libusb_config_descriptor
|
||||
if err := fromUSBError(C.libusb_get_config_descriptor((*C.libusb_device)(d), C.uint8_t(i), &cfg)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c := ConfigInfo{
|
||||
Config: uint8(cfg.bConfigurationValue),
|
||||
Attributes: uint8(cfg.bmAttributes),
|
||||
MaxPower: uint8(cfg.MaxPower),
|
||||
}
|
||||
|
||||
var ifaces []C.struct_libusb_interface
|
||||
*(*reflect.SliceHeader)(unsafe.Pointer(&ifaces)) = reflect.SliceHeader{
|
||||
Data: uintptr(unsafe.Pointer(cfg._interface)),
|
||||
Len: int(cfg.bNumInterfaces),
|
||||
Cap: int(cfg.bNumInterfaces),
|
||||
}
|
||||
c.Interfaces = make([]InterfaceInfo, 0, len(ifaces))
|
||||
for _, iface := range ifaces {
|
||||
if iface.num_altsetting == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
var alts []C.struct_libusb_interface_descriptor
|
||||
*(*reflect.SliceHeader)(unsafe.Pointer(&alts)) = reflect.SliceHeader{
|
||||
Data: uintptr(unsafe.Pointer(iface.altsetting)),
|
||||
Len: int(iface.num_altsetting),
|
||||
Cap: int(iface.num_altsetting),
|
||||
}
|
||||
descs := make([]InterfaceSetup, 0, len(alts))
|
||||
for _, alt := range alts {
|
||||
i := InterfaceSetup{
|
||||
Number: uint8(alt.bInterfaceNumber),
|
||||
Alternate: uint8(alt.bAlternateSetting),
|
||||
IfClass: uint8(alt.bInterfaceClass),
|
||||
IfSubClass: uint8(alt.bInterfaceSubClass),
|
||||
IfProtocol: uint8(alt.bInterfaceProtocol),
|
||||
}
|
||||
var ends []C.struct_libusb_endpoint_descriptor
|
||||
*(*reflect.SliceHeader)(unsafe.Pointer(&ends)) = reflect.SliceHeader{
|
||||
Data: uintptr(unsafe.Pointer(alt.endpoint)),
|
||||
Len: int(alt.bNumEndpoints),
|
||||
Cap: int(alt.bNumEndpoints),
|
||||
}
|
||||
i.Endpoints = make([]EndpointInfo, 0, len(ends))
|
||||
for _, end := range ends {
|
||||
ei := EndpointInfo{
|
||||
Address: uint8(end.bEndpointAddress),
|
||||
Attributes: uint8(end.bmAttributes),
|
||||
MaxPacketSize: uint16(end.wMaxPacketSize),
|
||||
PollInterval: uint8(end.bInterval),
|
||||
RefreshRate: uint8(end.bRefresh),
|
||||
SynchAddress: uint8(end.bSynchAddress),
|
||||
}
|
||||
if ei.TransferType() == TRANSFER_TYPE_ISOCHRONOUS {
|
||||
// bits 0-10 identify the packet size, bits 11-12 are the number of additional transactions per microframe.
|
||||
// Don't use libusb_get_max_iso_packet_size, as it has a bug where it returns the same value
|
||||
// regardless of alternative setting used, where different alternative settings might define different
|
||||
// max packet sizes.
|
||||
// See http://libusb.org/ticket/77 for more background.
|
||||
ei.MaxIsoPacket = uint32(end.wMaxPacketSize) & 0x07ff * (uint32(end.wMaxPacketSize)>>11&3 + 1)
|
||||
}
|
||||
i.Endpoints = append(i.Endpoints, ei)
|
||||
}
|
||||
descs = append(descs, i)
|
||||
}
|
||||
c.Interfaces = append(c.Interfaces, InterfaceInfo{
|
||||
Number: descs[0].Number,
|
||||
Setups: descs,
|
||||
})
|
||||
}
|
||||
C.libusb_free_config_descriptor(cfg)
|
||||
cfgs = append(cfgs, c)
|
||||
}
|
||||
|
||||
return &Descriptor{
|
||||
Bus: uint8(C.libusb_get_bus_number((*C.libusb_device)(d))),
|
||||
Address: uint8(C.libusb_get_device_address((*C.libusb_device)(d))),
|
||||
Spec: BCD(desc.bcdUSB),
|
||||
Device: BCD(desc.bcdDevice),
|
||||
Vendor: ID(desc.idVendor),
|
||||
Product: ID(desc.idProduct),
|
||||
Class: uint8(desc.bDeviceClass),
|
||||
SubClass: uint8(desc.bDeviceSubClass),
|
||||
Protocol: uint8(desc.bDeviceProtocol),
|
||||
Configs: cfgs,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (libusbImpl) dereference(d *libusbDevice) {
|
||||
C.libusb_unref_device((*C.libusb_device)(d))
|
||||
}
|
||||
|
||||
func (libusbImpl) open(d *libusbDevice) (*libusbDevHandle, error) {
|
||||
var handle *C.libusb_device_handle
|
||||
if err := fromUSBError(C.libusb_open((*C.libusb_device)(d), &handle)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return (*libusbDevHandle)(handle), nil
|
||||
}
|
||||
|
||||
func (libusbImpl) close(d *libusbDevHandle) {
|
||||
C.libusb_close((*C.libusb_device_handle)(d))
|
||||
}
|
||||
|
||||
func (libusbImpl) reset(d *libusbDevHandle) error {
|
||||
return fromUSBError(C.libusb_reset_device((*C.libusb_device_handle)(d)))
|
||||
}
|
||||
|
||||
func (libusbImpl) control(d *libusbDevHandle, timeout time.Duration, rType, request uint8, val, idx uint16, data []byte) (int, error) {
|
||||
dataSlice := (*reflect.SliceHeader)(unsafe.Pointer(&data))
|
||||
n := C.libusb_control_transfer(
|
||||
(*C.libusb_device_handle)(d),
|
||||
C.uint8_t(rType),
|
||||
C.uint8_t(request),
|
||||
C.uint16_t(val),
|
||||
C.uint16_t(idx),
|
||||
(*C.uchar)(unsafe.Pointer(dataSlice.Data)),
|
||||
C.uint16_t(len(data)),
|
||||
C.uint(timeout/time.Millisecond))
|
||||
if n < 0 {
|
||||
return int(n), fromUSBError(n)
|
||||
}
|
||||
return int(n), nil
|
||||
}
|
||||
|
||||
func (libusbImpl) getConfig(d *libusbDevHandle) (uint8, error) {
|
||||
var cfg C.int
|
||||
if errno := C.libusb_get_configuration((*C.libusb_device_handle)(d), &cfg); errno < 0 {
|
||||
return 0, fromUSBError(errno)
|
||||
}
|
||||
return uint8(cfg), nil
|
||||
}
|
||||
|
||||
func (libusbImpl) setConfig(d *libusbDevHandle, cfg uint8) error {
|
||||
return fromUSBError(C.libusb_set_configuration((*C.libusb_device_handle)(d), C.int(cfg)))
|
||||
}
|
||||
|
||||
func (libusbImpl) getStringDesc(d *libusbDevHandle, index int) (string, error) {
|
||||
// allocate 200-byte array limited the length of string descriptor
|
||||
buf := make([]byte, 200)
|
||||
// get string descriptor from libusb. if errno < 0 then there are any errors.
|
||||
// if errno >= 0; it is a length of result string descriptor
|
||||
errno := C.libusb_get_string_descriptor_ascii(
|
||||
(*C.libusb_device_handle)(d),
|
||||
C.uint8_t(index),
|
||||
(*C.uchar)(unsafe.Pointer(&buf[0])),
|
||||
200)
|
||||
if errno < 0 {
|
||||
return "", fmt.Errorf("usb: getstr: %s", fromUSBError(errno))
|
||||
}
|
||||
return string(buf[:errno]), nil
|
||||
}
|
||||
|
||||
func (libusbImpl) setAutoDetach(d *libusbDevHandle, val int) error {
|
||||
err := fromUSBError(C.libusb_set_auto_detach_kernel_driver((*C.libusb_device_handle)(d), C.int(val)))
|
||||
if err != nil && err != ERROR_NOT_SUPPORTED {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (libusbImpl) claim(d *libusbDevHandle, iface uint8) error {
|
||||
return fromUSBError(C.libusb_claim_interface((*C.libusb_device_handle)(d), C.int(iface)))
|
||||
}
|
||||
|
||||
func (libusbImpl) release(d *libusbDevHandle, iface uint8) {
|
||||
C.libusb_release_interface((*C.libusb_device_handle)(d), C.int(iface))
|
||||
}
|
||||
|
||||
func (libusbImpl) setAlt(d *libusbDevHandle, iface, setup uint8) error {
|
||||
return fromUSBError(C.libusb_set_interface_alt_setting((*C.libusb_device_handle)(d), C.int(iface), C.int(setup)))
|
||||
}
|
||||
|
||||
func (libusbImpl) alloc(d *libusbDevHandle, addr uint8, tt TransferType, timeout time.Duration, isoPackets int, buf []byte) (*libusbTransfer, error) {
|
||||
xfer := C.libusb_alloc_transfer(C.int(isoPackets))
|
||||
if xfer == nil {
|
||||
return nil, fmt.Errorf("libusb_alloc_transfer(%d) failed", isoPackets)
|
||||
}
|
||||
xfer.dev_handle = (*C.libusb_device_handle)(d)
|
||||
xfer.endpoint = C.uchar(addr)
|
||||
xfer.timeout = C.uint(timeout / time.Millisecond)
|
||||
xfer._type = C.uchar(tt)
|
||||
xfer.num_iso_packets = C.int(isoPackets)
|
||||
xfer.buffer = (*C.uchar)((unsafe.Pointer)(&buf[0]))
|
||||
xfer.length = C.int(len(buf))
|
||||
return (*libusbTransfer)(xfer), nil
|
||||
}
|
||||
|
||||
func (libusbImpl) cancel(t *libusbTransfer) error {
|
||||
return fromUSBError(C.libusb_cancel_transfer((*C.struct_libusb_transfer)(t)))
|
||||
}
|
||||
|
||||
func (libusbImpl) submit(t *libusbTransfer, done chan struct{}) error {
|
||||
t.user_data = (unsafe.Pointer)(&done)
|
||||
return fromUSBError(C.submit((*C.struct_libusb_transfer)(t)))
|
||||
}
|
||||
|
||||
func (libusbImpl) data(t *libusbTransfer) (int, TransferStatus) {
|
||||
if TransferType(t._type) == TRANSFER_TYPE_ISOCHRONOUS {
|
||||
var status TransferStatus
|
||||
n := int(C.compact_iso_data((*C.struct_libusb_transfer)(t), (*C.uchar)(unsafe.Pointer(&status))))
|
||||
return n, status
|
||||
}
|
||||
return int(t.actual_length), TransferStatus(t.status)
|
||||
}
|
||||
|
||||
func (libusbImpl) free(t *libusbTransfer) {
|
||||
C.libusb_free_transfer((*C.struct_libusb_transfer)(t))
|
||||
}
|
||||
|
||||
func (libusbImpl) setIsoPacketLengths(t *libusbTransfer, length uint32) {
|
||||
C.libusb_set_iso_packet_lengths((*C.struct_libusb_transfer)(t), C.uint(length))
|
||||
}
|
||||
|
||||
// libusb is an injection point for tests
|
||||
var libusb libusbIntf = libusbImpl{}
|
||||
|
||||
var (
|
||||
libusbIsoSize = C.sizeof_struct_libusb_iso_packet_descriptor
|
||||
libusbIsoOffset = unsafe.Offsetof(C.struct_libusb_transfer{}.iso_packet_desc)
|
||||
)
|
||||
|
||||
//export xfer_callback
|
||||
func xfer_callback(cptr unsafe.Pointer) {
|
||||
ch := *(*chan struct{})(cptr)
|
||||
close(ch)
|
||||
}
|
||||
|
||||
// for benchmarking
|
||||
func libusbSetDebug(c *libusbContext, lvl int) {
|
||||
C.libusb_set_debug((*C.libusb_context)(c), C.int(lvl))
|
||||
}
|
46
usb/libusb_cgo_benchmark_test.go
Normal file
46
usb/libusb_cgo_benchmark_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package usb
|
||||
|
||||
import "testing"
|
||||
|
||||
func BenchmarkCGo(b *testing.B) {
|
||||
for _, bc := range []struct {
|
||||
name string
|
||||
bfunc func(*libusbContext, int)
|
||||
}{
|
||||
{
|
||||
name: "simple function",
|
||||
bfunc: func(ctx *libusbContext, N int) {
|
||||
for i := 0; i < N; i++ {
|
||||
libusbSetDebug(ctx, i&1)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "method",
|
||||
bfunc: func(ctx *libusbContext, N int) {
|
||||
impl := libusbImpl{}
|
||||
for i := 0; i < N; i++ {
|
||||
impl.setDebug(ctx, i&1)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "interface",
|
||||
bfunc: func(ctx *libusbContext, N int) {
|
||||
var intf libusbIntf = libusbImpl{}
|
||||
for i := 0; i < N; i++ {
|
||||
intf.setDebug(ctx, i&1)
|
||||
}
|
||||
},
|
||||
},
|
||||
} {
|
||||
b.Run(bc.name, func(b *testing.B) {
|
||||
ctx, err := libusbImpl{}.init()
|
||||
if err != nil {
|
||||
b.Fatalf("libusb_init() failed: %v", err)
|
||||
}
|
||||
b.ResetTimer()
|
||||
bc.bfunc(ctx, b.N)
|
||||
})
|
||||
}
|
||||
}
|
@@ -14,50 +14,14 @@
|
||||
|
||||
package usb
|
||||
|
||||
/*
|
||||
#include <libusb.h>
|
||||
|
||||
int compact_iso_data(struct libusb_transfer *xfer, unsigned char *status);
|
||||
int submit(struct libusb_transfer *xfer);
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// libusb hooks used as injection points for tests.
|
||||
var (
|
||||
cCancel = func(t *libusbTransfer) usbError {
|
||||
return usbError(C.libusb_cancel_transfer((*C.struct_libusb_transfer)(t)))
|
||||
}
|
||||
cSubmit = func(t *libusbTransfer) usbError {
|
||||
return usbError(C.submit((*C.struct_libusb_transfer)(t)))
|
||||
}
|
||||
)
|
||||
|
||||
// because of a limitation of cgo, tests cannot import C.
|
||||
type deviceHandle C.libusb_device_handle
|
||||
type libusbTransfer C.struct_libusb_transfer
|
||||
type libusbIso C.struct_libusb_iso_packet_descriptor
|
||||
|
||||
// also for tests
|
||||
var (
|
||||
libusbIsoSize = C.sizeof_struct_libusb_iso_packet_descriptor
|
||||
libusbIsoOffset = unsafe.Offsetof(C.struct_libusb_transfer{}.iso_packet_desc)
|
||||
)
|
||||
|
||||
//export xfer_callback
|
||||
func xfer_callback(cptr unsafe.Pointer) {
|
||||
ch := *(*chan struct{})(cptr)
|
||||
close(ch)
|
||||
}
|
||||
|
||||
type usbTransfer struct {
|
||||
// mu protects the transfer state.
|
||||
mu sync.Mutex
|
||||
@@ -83,8 +47,7 @@ func (t *usbTransfer) submit() error {
|
||||
return errors.New("transfer was already submitted and is not finished yet.")
|
||||
}
|
||||
t.done = make(chan struct{})
|
||||
t.xfer.user_data = (unsafe.Pointer)(&t.done)
|
||||
if err := cSubmit(t.xfer); err != SUCCESS {
|
||||
if err := libusb.submit(t.xfer, t.done); err != nil {
|
||||
return err
|
||||
}
|
||||
t.submitted = true
|
||||
@@ -108,14 +71,7 @@ func (t *usbTransfer) wait() (n int, err error) {
|
||||
case <-t.done:
|
||||
}
|
||||
t.submitted = false
|
||||
var status TransferStatus
|
||||
switch TransferType(t.xfer._type) {
|
||||
case TRANSFER_TYPE_ISOCHRONOUS:
|
||||
n = int(C.compact_iso_data((*C.struct_libusb_transfer)(t.xfer), (*C.uchar)(unsafe.Pointer(&status))))
|
||||
default:
|
||||
n = int(t.xfer.actual_length)
|
||||
status = TransferStatus(t.xfer.status)
|
||||
}
|
||||
n, status := libusb.data(t.xfer)
|
||||
if status != LIBUSB_TRANSFER_COMPLETED {
|
||||
return n, status
|
||||
}
|
||||
@@ -130,15 +86,12 @@ func (t *usbTransfer) cancel() error {
|
||||
if !t.submitted {
|
||||
return nil
|
||||
}
|
||||
err := usbError(cCancel(t.xfer))
|
||||
err := libusb.cancel(t.xfer)
|
||||
if err == ERROR_NOT_FOUND {
|
||||
// transfer already completed
|
||||
err = SUCCESS
|
||||
return nil
|
||||
}
|
||||
if err != SUCCESS {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
// free releases the memory allocated for the transfer.
|
||||
@@ -150,7 +103,7 @@ func (t *usbTransfer) free() error {
|
||||
if t.submitted {
|
||||
return errors.New("free() cannot be called on a submitted transfer until wait() returns")
|
||||
}
|
||||
C.libusb_free_transfer((*C.struct_libusb_transfer)(t.xfer))
|
||||
libusb.free(t.xfer)
|
||||
t.xfer = nil
|
||||
t.buf = nil
|
||||
t.done = nil
|
||||
@@ -159,7 +112,7 @@ func (t *usbTransfer) free() error {
|
||||
|
||||
// newUSBTransfer allocates a new transfer structure for communication with a
|
||||
// given device/endpoint, with buf as the underlying transfer buffer.
|
||||
func newUSBTransfer(dev *deviceHandle, ei EndpointInfo, buf []byte, timeout time.Duration) (*usbTransfer, error) {
|
||||
func newUSBTransfer(dev *libusbDevHandle, ei EndpointInfo, buf []byte, timeout time.Duration) (*usbTransfer, error) {
|
||||
var isoPackets int
|
||||
tt := ei.TransferType()
|
||||
if tt == TRANSFER_TYPE_ISOCHRONOUS {
|
||||
@@ -169,26 +122,17 @@ func newUSBTransfer(dev *deviceHandle, ei EndpointInfo, buf []byte, timeout time
|
||||
}
|
||||
}
|
||||
|
||||
xfer := C.libusb_alloc_transfer(C.int(isoPackets))
|
||||
if xfer == nil {
|
||||
return nil, fmt.Errorf("libusb_alloc_transfer(%d) failed", isoPackets)
|
||||
xfer, err := libusb.alloc(dev, ei.Address, tt, timeout, isoPackets, buf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
xfer.dev_handle = (*C.struct_libusb_device_handle)(dev)
|
||||
xfer.timeout = C.uint(timeout / time.Millisecond)
|
||||
xfer.endpoint = C.uchar(ei.Address)
|
||||
xfer._type = C.uchar(tt)
|
||||
|
||||
xfer.buffer = (*C.uchar)((unsafe.Pointer)(&buf[0]))
|
||||
xfer.length = C.int(len(buf))
|
||||
|
||||
if tt == TRANSFER_TYPE_ISOCHRONOUS {
|
||||
xfer.num_iso_packets = C.int(isoPackets)
|
||||
C.libusb_set_iso_packet_lengths(xfer, C.uint(ei.MaxIsoPacket))
|
||||
libusb.setIsoPacketLengths(xfer, ei.MaxIsoPacket)
|
||||
}
|
||||
|
||||
t := &usbTransfer{
|
||||
xfer: (*libusbTransfer)(xfer),
|
||||
xfer: xfer,
|
||||
buf: buf,
|
||||
}
|
||||
runtime.SetFinalizer(t, func(t *usbTransfer) {
|
||||
|
@@ -1,56 +0,0 @@
|
||||
// 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 "sync"
|
||||
|
||||
type fakeLibusb struct {
|
||||
sync.Mutex
|
||||
ts map[*libusbTransfer]chan struct{}
|
||||
}
|
||||
|
||||
func (f *fakeLibusb) submit(t *libusbTransfer) usbError {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
if f.ts[t] == nil {
|
||||
f.ts[t] = make(chan struct{})
|
||||
}
|
||||
close(f.ts[t])
|
||||
return SUCCESS
|
||||
}
|
||||
|
||||
func (f *fakeLibusb) cancel(t *libusbTransfer) usbError { return SUCCESS }
|
||||
|
||||
func (f *fakeLibusb) waitForSubmit(t *usbTransfer) {
|
||||
f.Lock()
|
||||
if f.ts[t.xfer] == nil {
|
||||
f.ts[t.xfer] = make(chan struct{})
|
||||
}
|
||||
ch := f.ts[t.xfer]
|
||||
f.Unlock()
|
||||
<-ch
|
||||
}
|
||||
|
||||
func (f *fakeLibusb) runCallback(t *usbTransfer, cb func(*usbTransfer)) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
delete(f.ts, t.xfer)
|
||||
cb(t)
|
||||
close(t.done)
|
||||
}
|
||||
|
||||
func newFakeLibusb() *fakeLibusb {
|
||||
return &fakeLibusb{ts: make(map[*libusbTransfer]chan struct{})}
|
||||
}
|
@@ -17,7 +17,6 @@ package usb
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func TestNewTransfer(t *testing.T) {
|
||||
@@ -87,12 +86,10 @@ func TestNewTransfer(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTransferProtocol(t *testing.T) {
|
||||
origSubmit, origCancel := cSubmit, cCancel
|
||||
defer func() { cSubmit, cCancel = origSubmit, origCancel }()
|
||||
defer func(i libusbIntf) { libusb = i }(libusb)
|
||||
|
||||
f := newFakeLibusb()
|
||||
cSubmit = f.submit
|
||||
cCancel = f.cancel
|
||||
libusb = f
|
||||
|
||||
xfers := make([]*usbTransfer, 2)
|
||||
var err error
|
||||
@@ -109,24 +106,26 @@ func TestTransferProtocol(t *testing.T) {
|
||||
}
|
||||
|
||||
go func() {
|
||||
f.waitForSubmit(xfers[0])
|
||||
f.runCallback(xfers[0], func(t *usbTransfer) {
|
||||
t.xfer.actual_length = 5
|
||||
t.xfer.status = uint32(SUCCESS)
|
||||
copy(t.buf, []byte{1, 2, 3, 4, 5})
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
f.waitForSubmit(xfers[1])
|
||||
f.runCallback(xfers[1], func(t *usbTransfer) {
|
||||
t.xfer.actual_length = 99
|
||||
t.xfer.status = uint32(SUCCESS)
|
||||
copy(t.buf, []byte{12, 12, 12, 12, 12})
|
||||
})
|
||||
ft := f.waitForSubmitted()
|
||||
ft.length = 5
|
||||
ft.status = LIBUSB_TRANSFER_COMPLETED
|
||||
copy(ft.buf, []byte{1, 2, 3, 4, 5})
|
||||
close(ft.done)
|
||||
|
||||
ft = f.waitForSubmitted()
|
||||
ft.length = 99
|
||||
ft.status = LIBUSB_TRANSFER_COMPLETED
|
||||
copy(ft.buf, []byte{12, 12, 12, 12, 12})
|
||||
close(ft.done)
|
||||
|
||||
ft = f.waitForSubmitted()
|
||||
ft.length = 123
|
||||
ft.status = LIBUSB_TRANSFER_CANCELLED
|
||||
close(ft.done)
|
||||
}()
|
||||
|
||||
xfers[1].submit()
|
||||
xfers[0].submit()
|
||||
xfers[1].submit()
|
||||
got, err := xfers[0].wait()
|
||||
if err != nil {
|
||||
t.Errorf("xfer#0.wait returned error %v, want nil", err)
|
||||
@@ -142,13 +141,6 @@ func TestTransferProtocol(t *testing.T) {
|
||||
t.Errorf("xfer#0.wait returned %d bytes, want %d", got, want)
|
||||
}
|
||||
|
||||
go func() {
|
||||
f.waitForSubmit(xfers[1])
|
||||
f.runCallback(xfers[1], func(t *usbTransfer) {
|
||||
t.xfer.actual_length = 123
|
||||
t.xfer.status = uint32(LIBUSB_TRANSFER_CANCELLED)
|
||||
})
|
||||
}()
|
||||
xfers[1].submit()
|
||||
xfers[1].cancel()
|
||||
got, err = xfers[1].wait()
|
||||
@@ -165,66 +157,3 @@ func TestTransferProtocol(t *testing.T) {
|
||||
x.free()
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsoPackets(t *testing.T) {
|
||||
origSubmit, origCancel := cSubmit, cCancel
|
||||
defer func() { cSubmit, cCancel = origSubmit, origCancel }()
|
||||
|
||||
f := newFakeLibusb()
|
||||
cSubmit = f.submit
|
||||
cCancel = f.cancel
|
||||
|
||||
xfer, err := newUSBTransfer(nil, EndpointInfo{
|
||||
Address: 0x82,
|
||||
Attributes: uint8(TRANSFER_TYPE_ISOCHRONOUS),
|
||||
MaxPacketSize: 3<<11 + 1024,
|
||||
MaxIsoPacket: 3 * 1024,
|
||||
PollInterval: 1,
|
||||
}, make([]byte, 15000), time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("newUSBTransfer: %v", err)
|
||||
}
|
||||
|
||||
// 15000 / (3*1024) = 4.something, rounded up to 5
|
||||
if got, want := int(xfer.xfer.num_iso_packets), 5; got != want {
|
||||
t.Fatalf("newUSBTransfer: got %d iso packets, want %d", got, want)
|
||||
}
|
||||
|
||||
go func() {
|
||||
f.waitForSubmit(xfer)
|
||||
f.runCallback(xfer, func(x *usbTransfer) {
|
||||
x.xfer.actual_length = 1234 // meaningless for iso transfers
|
||||
x.xfer.status = uint32(LIBUSB_TRANSFER_TIMED_OUT)
|
||||
for i := 0; i < int(xfer.xfer.num_iso_packets); i++ {
|
||||
// this is a horrible calculation.
|
||||
// libusb_transfer uses a flexible array for the iso packet
|
||||
// descriptors at the end of the transfer struct.
|
||||
// The only way to get access to the elements of that array
|
||||
// is to use pointer arithmetic.
|
||||
// Calculate the offset of the first descriptor in the struct,
|
||||
// then move by sizeof(iso descriptor) for num_iso_packets.
|
||||
desc := (*libusbIso)(unsafe.Pointer(uintptr(unsafe.Pointer(x.xfer)) + libusbIsoOffset + uintptr(i*libusbIsoSize)))
|
||||
// max iso packet = 3 * 1024
|
||||
if desc.length != 3*1024 {
|
||||
t.Errorf("iso pkt length: got %d, want %d", desc.length, 3*1024)
|
||||
}
|
||||
desc.actual_length = 100
|
||||
// packets 0..2 are successful, packet 3 is timed out
|
||||
if i != 4 {
|
||||
desc.status = uint32(LIBUSB_TRANSFER_COMPLETED)
|
||||
} else {
|
||||
desc.status = uint32(LIBUSB_TRANSFER_TIMED_OUT)
|
||||
}
|
||||
}
|
||||
})
|
||||
}()
|
||||
|
||||
xfer.submit()
|
||||
got, err := xfer.wait()
|
||||
if err == nil {
|
||||
t.Error("Iso transfer: got nil error, want non-nil")
|
||||
}
|
||||
if want := 4 * 100; got != want {
|
||||
t.Errorf("Iso transfer: got %d bytes, want %d", got, want)
|
||||
}
|
||||
}
|
||||
|
95
usb/usb.go
95
usb/usb.go
@@ -16,54 +16,26 @@
|
||||
// Package usb provides a wrapper around libusb-1.0.
|
||||
package usb
|
||||
|
||||
// #cgo pkg-config: libusb-1.0
|
||||
// #include <libusb.h>
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"log"
|
||||
"reflect"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
ctx *C.libusb_context
|
||||
ctx *libusbContext
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
func (c *Context) Debug(level int) {
|
||||
C.libusb_set_debug(c.ctx, C.int(level))
|
||||
libusb.setDebug(c.ctx, level)
|
||||
}
|
||||
|
||||
func NewContext() *Context {
|
||||
c := &Context{
|
||||
c, err := libusb.init()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
ctx := &Context{
|
||||
ctx: c,
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
|
||||
if errno := C.libusb_init(&c.ctx); errno != 0 {
|
||||
panic(usbError(errno))
|
||||
}
|
||||
|
||||
go func() {
|
||||
tv := C.struct_timeval{
|
||||
tv_sec: 0,
|
||||
tv_usec: 100000,
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case <-c.done:
|
||||
return
|
||||
default:
|
||||
}
|
||||
if errno := C.libusb_handle_events_timeout_completed(c.ctx, &tv, nil); errno < 0 {
|
||||
log.Printf("handle_events: error: %s", usbError(errno))
|
||||
continue
|
||||
}
|
||||
//log.Printf("handle_events returned")
|
||||
}
|
||||
}()
|
||||
|
||||
return c
|
||||
go libusb.handleEvents(ctx.ctx, ctx.done)
|
||||
return ctx
|
||||
}
|
||||
|
||||
// ListDevices calls each with each enumerated device.
|
||||
@@ -72,36 +44,30 @@ func NewContext() *Context {
|
||||
// If there are any errors enumerating the devices,
|
||||
// the final one is returned along with any successfully opened devices.
|
||||
func (c *Context) ListDevices(each func(desc *Descriptor) bool) ([]*Device, error) {
|
||||
var list **C.libusb_device
|
||||
cnt := C.libusb_get_device_list(c.ctx, &list)
|
||||
if cnt < 0 {
|
||||
return nil, usbError(cnt)
|
||||
}
|
||||
defer C.libusb_free_device_list(list, 1)
|
||||
|
||||
var slice []*C.libusb_device
|
||||
*(*reflect.SliceHeader)(unsafe.Pointer(&slice)) = reflect.SliceHeader{
|
||||
Data: uintptr(unsafe.Pointer(list)),
|
||||
Len: int(cnt),
|
||||
Cap: int(cnt),
|
||||
list, err := libusb.getDevices(c.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var reterr error
|
||||
var ret []*Device
|
||||
for _, dev := range slice {
|
||||
desc, err := newDescriptor(dev)
|
||||
for _, dev := range list {
|
||||
desc, err := libusb.getDeviceDesc(dev)
|
||||
if err != nil {
|
||||
libusb.dereference(dev)
|
||||
reterr = err
|
||||
continue
|
||||
}
|
||||
|
||||
if each(desc) {
|
||||
var handle *C.libusb_device_handle
|
||||
if errno := C.libusb_open(dev, &handle); errno != 0 {
|
||||
reterr = usbError(errno)
|
||||
handle, err := libusb.open(dev)
|
||||
if err != nil {
|
||||
reterr = err
|
||||
continue
|
||||
}
|
||||
ret = append(ret, newDevice(handle, desc))
|
||||
} else {
|
||||
libusb.dereference(dev)
|
||||
}
|
||||
}
|
||||
return ret, reterr
|
||||
@@ -110,21 +76,14 @@ func (c *Context) ListDevices(each func(desc *Descriptor) bool) ([]*Device, erro
|
||||
// OpenDeviceWithVidPid opens Device from specific VendorId and ProductId.
|
||||
// If there are any errors, it'll returns at second value.
|
||||
func (c *Context) OpenDeviceWithVidPid(vid, pid int) (*Device, error) {
|
||||
|
||||
handle := C.libusb_open_device_with_vid_pid(c.ctx, (C.uint16_t)(vid), (C.uint16_t)(pid))
|
||||
if handle == nil {
|
||||
return nil, ERROR_NOT_FOUND
|
||||
dev, handle, err := libusb.openVIDPID(c.ctx, vid, pid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dev := C.libusb_get_device(handle)
|
||||
if dev == nil {
|
||||
return nil, ERROR_NO_DEVICE
|
||||
}
|
||||
|
||||
desc, err := newDescriptor(dev)
|
||||
|
||||
desc, err := libusb.getDeviceDesc(dev)
|
||||
// return an error from nil-handle and nil-device
|
||||
if err != nil {
|
||||
libusb.dereference(dev)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -135,7 +94,7 @@ func (c *Context) OpenDeviceWithVidPid(vid, pid int) (*Device, error) {
|
||||
func (c *Context) Close() error {
|
||||
c.done <- struct{}{}
|
||||
if c.ctx != nil {
|
||||
C.libusb_exit(c.ctx)
|
||||
libusb.exit(c.ctx)
|
||||
}
|
||||
c.ctx = nil
|
||||
return nil
|
||||
|
Reference in New Issue
Block a user