// ListDevices calls each with each enumerated device. // If the function returns true, the device is opened and a Device is returned if the operation succeeds. // Every Device returned (whether an error is also returned or not) must be closed. // 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), } var reterr error ret := []*Device{} for _, dev := range slice { desc, err := newDescriptor(dev) if err != nil { reterr = err continue } if each(desc) { var handle *C.libusb_device_handle if errno := C.libusb_open(dev, &handle); errno != 0 { errName := C.GoString(C.libusb_error_name(errno)) reterr = errors.New("libusb error: " + errName) continue } ret = append(ret, newDevice(handle, desc)) } } return ret, reterr }
func (e Error) Error() string { return C.GoString(C.libusb_error_name(C.int(e))) }
func (e *libusbError) Error() string { return fmt.Sprintf("%s (%s)", C.GoString(C.libusb_strerror(C.enum_libusb_error(e.code))), C.GoString(C.libusb_error_name(e.code))) }
func MakeUsbError(code C.int) *UsbError { return &UsbError{ UsbErrorCode(code) , UsbErrorDesc(C.GoString(C.libusb_error_name(code))) } }