Go naming for error codes. Export USBError type.
This commit is contained in:
@@ -55,7 +55,7 @@ func TestEndpoint(t *testing.T) {
|
|||||||
desc: "128B buffer, 10 transferred and then error",
|
desc: "128B buffer, 10 transferred and then error",
|
||||||
buf: make([]byte, 128),
|
buf: make([]byte, 128),
|
||||||
ret: 10,
|
ret: 10,
|
||||||
status: LIBUSB_TRANSFER_ERROR,
|
status: TransferError,
|
||||||
want: 10,
|
want: 10,
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
|
104
usb/error.go
104
usb/error.go
@@ -22,80 +22,86 @@ import (
|
|||||||
// #include <libusb.h>
|
// #include <libusb.h>
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
type usbError C.int
|
// USBError is an error code returned by libusb.
|
||||||
|
type USBError C.int
|
||||||
|
|
||||||
func (e usbError) Error() string {
|
// Error implements the error interface.
|
||||||
return fmt.Sprintf("libusb: %s [code %d]", usbErrorString[e], int(e))
|
func (e USBError) Error() string {
|
||||||
|
return fmt.Sprintf("libusb: %s [code %d]", USBErrorString[e], e)
|
||||||
}
|
}
|
||||||
|
|
||||||
func fromUSBError(errno C.int) error {
|
func fromUSBError(errno C.int) error {
|
||||||
err := usbError(errno)
|
err := USBError(errno)
|
||||||
if err == SUCCESS {
|
if err == Success {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Error codes defined by libusb.
|
||||||
const (
|
const (
|
||||||
SUCCESS usbError = C.LIBUSB_SUCCESS
|
Success USBError = C.LIBUSB_SUCCESS
|
||||||
ERROR_IO usbError = C.LIBUSB_ERROR_IO
|
ErrorIO USBError = C.LIBUSB_ERROR_IO
|
||||||
ERROR_INVALID_PARAM usbError = C.LIBUSB_ERROR_INVALID_PARAM
|
ErrorInvalidParam USBError = C.LIBUSB_ERROR_INVALID_PARAM
|
||||||
ERROR_ACCESS usbError = C.LIBUSB_ERROR_ACCESS
|
ErrorAccess USBError = C.LIBUSB_ERROR_ACCESS
|
||||||
ERROR_NO_DEVICE usbError = C.LIBUSB_ERROR_NO_DEVICE
|
ErrorNoDevice USBError = C.LIBUSB_ERROR_NO_DEVICE
|
||||||
ERROR_NOT_FOUND usbError = C.LIBUSB_ERROR_NOT_FOUND
|
ErrorNotFound USBError = C.LIBUSB_ERROR_NOT_FOUND
|
||||||
ERROR_BUSY usbError = C.LIBUSB_ERROR_BUSY
|
ErrorBusy USBError = C.LIBUSB_ERROR_BUSY
|
||||||
ERROR_TIMEOUT usbError = C.LIBUSB_ERROR_TIMEOUT
|
ErrorTimeout USBError = C.LIBUSB_ERROR_TIMEOUT
|
||||||
ERROR_OVERFLOW usbError = C.LIBUSB_ERROR_OVERFLOW
|
ErrorOverflow USBError = C.LIBUSB_ERROR_OVERFLOW
|
||||||
ERROR_PIPE usbError = C.LIBUSB_ERROR_PIPE
|
ErrorPipe USBError = C.LIBUSB_ERROR_PIPE
|
||||||
ERROR_INTERRUPTED usbError = C.LIBUSB_ERROR_INTERRUPTED
|
ErrorInterrupted USBError = C.LIBUSB_ERROR_INTERRUPTED
|
||||||
ERROR_NO_MEM usbError = C.LIBUSB_ERROR_NO_MEM
|
ErrorNoMem USBError = C.LIBUSB_ERROR_NO_MEM
|
||||||
ERROR_NOT_SUPPORTED usbError = C.LIBUSB_ERROR_NOT_SUPPORTED
|
ErrorNotSupported USBError = C.LIBUSB_ERROR_NOT_SUPPORTED
|
||||||
ERROR_OTHER usbError = C.LIBUSB_ERROR_OTHER
|
ErrorOther USBError = C.LIBUSB_ERROR_OTHER
|
||||||
)
|
)
|
||||||
|
|
||||||
var usbErrorString = map[usbError]string{
|
var USBErrorString = map[USBError]string{
|
||||||
C.LIBUSB_SUCCESS: "success",
|
Success: "success",
|
||||||
C.LIBUSB_ERROR_IO: "i/o error",
|
ErrorIO: "i/o error",
|
||||||
C.LIBUSB_ERROR_INVALID_PARAM: "invalid param",
|
ErrorInvalidParam: "invalid param",
|
||||||
C.LIBUSB_ERROR_ACCESS: "bad access",
|
ErrorAccess: "bad access",
|
||||||
C.LIBUSB_ERROR_NO_DEVICE: "no device",
|
ErrorNoDevice: "no device",
|
||||||
C.LIBUSB_ERROR_NOT_FOUND: "not found",
|
ErrorNotFound: "not found",
|
||||||
C.LIBUSB_ERROR_BUSY: "device or resource busy",
|
ErrorBusy: "device or resource busy",
|
||||||
C.LIBUSB_ERROR_TIMEOUT: "timeout",
|
ErrorTimeout: "timeout",
|
||||||
C.LIBUSB_ERROR_OVERFLOW: "overflow",
|
ErrorOverflow: "overflow",
|
||||||
C.LIBUSB_ERROR_PIPE: "pipe error",
|
ErrorPipe: "pipe error",
|
||||||
C.LIBUSB_ERROR_INTERRUPTED: "interrupted",
|
ErrorInterrupted: "interrupted",
|
||||||
C.LIBUSB_ERROR_NO_MEM: "out of memory",
|
ErrorNoMem: "out of memory",
|
||||||
C.LIBUSB_ERROR_NOT_SUPPORTED: "not supported",
|
ErrorNotSupported: "not supported",
|
||||||
C.LIBUSB_ERROR_OTHER: "unknown error",
|
ErrorOther: "unknown error",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TransferStatus contains information about the result of a transfer.
|
||||||
type TransferStatus uint8
|
type TransferStatus uint8
|
||||||
|
|
||||||
const (
|
const (
|
||||||
LIBUSB_TRANSFER_COMPLETED TransferStatus = C.LIBUSB_TRANSFER_COMPLETED
|
TransferCompleted TransferStatus = C.LIBUSB_TRANSFER_COMPLETED
|
||||||
LIBUSB_TRANSFER_ERROR TransferStatus = C.LIBUSB_TRANSFER_ERROR
|
TransferError TransferStatus = C.LIBUSB_TRANSFER_ERROR
|
||||||
LIBUSB_TRANSFER_TIMED_OUT TransferStatus = C.LIBUSB_TRANSFER_TIMED_OUT
|
TransferTimedOut TransferStatus = C.LIBUSB_TRANSFER_TIMED_OUT
|
||||||
LIBUSB_TRANSFER_CANCELLED TransferStatus = C.LIBUSB_TRANSFER_CANCELLED
|
TransferCancelled TransferStatus = C.LIBUSB_TRANSFER_CANCELLED
|
||||||
LIBUSB_TRANSFER_STALL TransferStatus = C.LIBUSB_TRANSFER_STALL
|
TransferStall TransferStatus = C.LIBUSB_TRANSFER_STALL
|
||||||
LIBUSB_TRANSFER_NO_DEVICE TransferStatus = C.LIBUSB_TRANSFER_NO_DEVICE
|
TransferNoDevice TransferStatus = C.LIBUSB_TRANSFER_NO_DEVICE
|
||||||
LIBUSB_TRANSFER_OVERFLOW TransferStatus = C.LIBUSB_TRANSFER_OVERFLOW
|
TransferOverflow TransferStatus = C.LIBUSB_TRANSFER_OVERFLOW
|
||||||
)
|
)
|
||||||
|
|
||||||
var transferStatusDescription = map[TransferStatus]string{
|
var transferStatusDescription = map[TransferStatus]string{
|
||||||
LIBUSB_TRANSFER_COMPLETED: "transfer completed without error",
|
TransferCompleted: "transfer completed without error",
|
||||||
LIBUSB_TRANSFER_ERROR: "transfer failed",
|
TransferError: "transfer failed",
|
||||||
LIBUSB_TRANSFER_TIMED_OUT: "transfer timed out",
|
TransferTimedOut: "transfer timed out",
|
||||||
LIBUSB_TRANSFER_CANCELLED: "transfer was cancelled",
|
TransferCancelled: "transfer was cancelled",
|
||||||
LIBUSB_TRANSFER_STALL: "halt condition detected (endpoint stalled) or control request not supported",
|
TransferStall: "halt condition detected (endpoint stalled) or control request not supported",
|
||||||
LIBUSB_TRANSFER_NO_DEVICE: "device was disconnected",
|
TransferNoDevice: "device was disconnected",
|
||||||
LIBUSB_TRANSFER_OVERFLOW: "device sent more data than requested",
|
TransferOverflow: "device sent more data than requested",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns a human-readable transfer status.
|
||||||
func (ts TransferStatus) String() string {
|
func (ts TransferStatus) String() string {
|
||||||
return transferStatusDescription[ts]
|
return transferStatusDescription[ts]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Error implements the error interface.
|
||||||
func (ts TransferStatus) Error() string {
|
func (ts TransferStatus) Error() string {
|
||||||
return "libusb: " + ts.String()
|
return ts.String()
|
||||||
}
|
}
|
||||||
|
@@ -166,7 +166,7 @@ func (libusbImpl) handleEvents(c *libusbContext, done <-chan struct{}) {
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
if errno := C.libusb_handle_events_timeout_completed((*C.libusb_context)(c), &tv, nil); errno < 0 {
|
if errno := C.libusb_handle_events_timeout_completed((*C.libusb_context)(c), &tv, nil); errno < 0 {
|
||||||
log.Printf("handle_events: error: %s", usbError(errno))
|
log.Printf("handle_events: error: %s", USBError(errno))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -350,7 +350,7 @@ func (libusbImpl) getStringDesc(d *libusbDevHandle, index int) (string, error) {
|
|||||||
|
|
||||||
func (libusbImpl) setAutoDetach(d *libusbDevHandle, val int) error {
|
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)))
|
err := fromUSBError(C.libusb_set_auto_detach_kernel_driver((*C.libusb_device_handle)(d), C.int(val)))
|
||||||
if err != nil && err != ERROR_NOT_SUPPORTED {
|
if err != nil && err != ErrorNotSupported {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@@ -72,7 +72,7 @@ func (t *usbTransfer) wait() (n int, err error) {
|
|||||||
}
|
}
|
||||||
t.submitted = false
|
t.submitted = false
|
||||||
n, status := libusb.data(t.xfer)
|
n, status := libusb.data(t.xfer)
|
||||||
if status != LIBUSB_TRANSFER_COMPLETED {
|
if status != TransferCompleted {
|
||||||
return n, status
|
return n, status
|
||||||
}
|
}
|
||||||
return n, err
|
return n, err
|
||||||
@@ -87,7 +87,7 @@ func (t *usbTransfer) cancel() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
err := libusb.cancel(t.xfer)
|
err := libusb.cancel(t.xfer)
|
||||||
if err == ERROR_NOT_FOUND {
|
if err == ErrorNotFound {
|
||||||
// transfer already completed
|
// transfer already completed
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -91,19 +91,19 @@ func TestTransferProtocol(t *testing.T) {
|
|||||||
go func() {
|
go func() {
|
||||||
ft := f.waitForSubmitted()
|
ft := f.waitForSubmitted()
|
||||||
ft.length = 5
|
ft.length = 5
|
||||||
ft.status = LIBUSB_TRANSFER_COMPLETED
|
ft.status = TransferCompleted
|
||||||
copy(ft.buf, []byte{1, 2, 3, 4, 5})
|
copy(ft.buf, []byte{1, 2, 3, 4, 5})
|
||||||
close(ft.done)
|
close(ft.done)
|
||||||
|
|
||||||
ft = f.waitForSubmitted()
|
ft = f.waitForSubmitted()
|
||||||
ft.length = 99
|
ft.length = 99
|
||||||
ft.status = LIBUSB_TRANSFER_COMPLETED
|
ft.status = TransferCompleted
|
||||||
copy(ft.buf, []byte{12, 12, 12, 12, 12})
|
copy(ft.buf, []byte{12, 12, 12, 12, 12})
|
||||||
close(ft.done)
|
close(ft.done)
|
||||||
|
|
||||||
ft = f.waitForSubmitted()
|
ft = f.waitForSubmitted()
|
||||||
ft.length = 123
|
ft.length = 123
|
||||||
ft.status = LIBUSB_TRANSFER_CANCELLED
|
ft.status = TransferCancelled
|
||||||
close(ft.done)
|
close(ft.done)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user