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:
@@ -50,7 +50,6 @@ type libusbIntf interface {
|
||||
getDevices(*libusbContext) ([]*libusbDevice, error)
|
||||
exit(*libusbContext)
|
||||
setDebug(*libusbContext, int)
|
||||
openVIDPID(*libusbContext, int, int) (*libusbDevice, *libusbDevHandle, error)
|
||||
|
||||
// device
|
||||
dereference(*libusbDevice)
|
||||
@@ -133,19 +132,6 @@ 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 {
|
||||
|
29
usb/usb.go
29
usb/usb.go
@@ -74,21 +74,26 @@ 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.
|
||||
// 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) {
|
||||
dev, handle, err := libusb.openVIDPID(c.ctx, vid, pid)
|
||||
if err != nil {
|
||||
dev * Device
|
||||
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
|
||||
}
|
||||
desc, err := libusb.getDeviceDesc(dev)
|
||||
// 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
|
||||
return devs[0], nil
|
||||
}
|
||||
|
||||
func (c *Context) Close() error {
|
||||
|
Reference in New Issue
Block a user