remove a call to libusb_open_device_with_vid_pid, as it's just a

convenience wrapper. Reimplement the same trivial wrapper in Go.
This commit is contained in:
Sebastian Zagrodzki
2017-03-10 07:22:04 -05:00
parent 5608fa49e7
commit 4b17678362
2 changed files with 17 additions and 26 deletions

View File

@@ -50,7 +50,6 @@ type libusbIntf interface {
getDevices(*libusbContext) ([]*libusbDevice, error) getDevices(*libusbContext) ([]*libusbDevice, error)
exit(*libusbContext) exit(*libusbContext)
setDebug(*libusbContext, int) setDebug(*libusbContext, int)
openVIDPID(*libusbContext, int, int) (*libusbDevice, *libusbDevHandle, error)
// device // device
dereference(*libusbDevice) dereference(*libusbDevice)
@@ -133,19 +132,6 @@ func (libusbImpl) setDebug(c *libusbContext, lvl int) {
C.libusb_set_debug((*C.libusb_context)(c), C.int(lvl)) 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) { func (libusbImpl) getDeviceDesc(d *libusbDevice) (*Descriptor, error) {
var desc C.struct_libusb_device_descriptor var desc C.struct_libusb_device_descriptor
if err := fromUSBError(C.libusb_get_device_descriptor((*C.libusb_device)(d), &desc)); err != nil { if err := fromUSBError(C.libusb_get_device_descriptor((*C.libusb_device)(d), &desc)); err != nil {

View File

@@ -74,21 +74,26 @@ func (c *Context) ListDevices(each func(desc *Descriptor) bool) ([]*Device, erro
} }
// OpenDeviceWithVidPid opens Device from specific VendorId and ProductId. // OpenDeviceWithVidPid opens Device from specific VendorId and ProductId.
// If there are any errors, it'll returns at second value. // If none is found, it returns nil and nil error. If there are multiple devices
// with the same VID/PID, it will return one of them, picked arbitrarily.
// If there were any errors during device list traversal, it is possible
// it will return a non-nil device and non-nil error. A Device.Close() must
// be called to release the device if the returned device wasn't nil.
func (c *Context) OpenDeviceWithVidPid(vid, pid int) (*Device, error) { func (c *Context) OpenDeviceWithVidPid(vid, pid int) (*Device, error) {
dev, handle, err := libusb.openVIDPID(c.ctx, vid, pid) dev * Device
if err != nil { devs, err := ListDevices(func(desc *Descriptor) {
if dev != nil {
return false
}
if desc.Vendor == ID(vid) && desc.Product == ID(pid) {
return true
}
return false
})
if len(devs) == 0 {
return nil, err return nil, err
} }
desc, err := libusb.getDeviceDesc(dev) return devs[0], nil
// return an error from nil-handle and nil-device
if err != nil {
libusb.dereference(dev)
return nil, err
}
device := newDevice(handle, desc)
return device, nil
} }
func (c *Context) Close() error { func (c *Context) Close() error {