handle error OpenDeviceWithVidPid and add GetStringDescriptor

This commit is contained in:
nodtem66
2015-03-04 01:32:24 +07:00
parent 4bf19c7f86
commit f3b93aa5fa
2 changed files with 25 additions and 0 deletions

View File

@@ -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
}

View File

@@ -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