handle error OpenDeviceWithVidPid and add GetStringDescriptor
This commit is contained in:
@@ -203,3 +203,19 @@ found:
|
||||
|
||||
return end, nil
|
||||
}
|
||||
|
||||
func (d *Device) GetStringDescriptor(desc_index int) (string, error) {
|
||||
goBuffer := make([]byte, 200)
|
||||
errno := C.libusb_get_string_descriptor_ascii(
|
||||
d.handle,
|
||||
C.uint8_t(desc_index),
|
||||
(*C.uchar)(unsafe.Pointer(&goBuffer[0])),
|
||||
200)
|
||||
|
||||
if errno < 0 {
|
||||
return "", fmt.Errorf("usb: getstr: %s", usbError(errno))
|
||||
}
|
||||
stringDescriptor := string(goBuffer[:errno])
|
||||
|
||||
return stringDescriptor, nil
|
||||
}
|
||||
|
@@ -112,8 +112,17 @@ 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 := C.libusb_get_device(handle)
|
||||
if dev == nil {
|
||||
return nil, ERROR_NO_DEVICE
|
||||
}
|
||||
|
||||
desc, err := newDescriptor(dev)
|
||||
|
||||
// return an error from nil-handle and nil-device
|
||||
|
Reference in New Issue
Block a user