Beispiel #1
0
Datei: usb.go Projekt: hanwen/usb
func (h *DeviceHandle) GetStringDescriptorASCII(descIndex byte) (string, error) {
	data := make([]byte, 1024)
	start := (*C.uchar)(unsafe.Pointer(&data[0]))
	r := C.libusb_get_string_descriptor_ascii(h.me(),
		C.uint8_t(descIndex), start, C.int(len(data)))
	return C.GoString((*C.char)(unsafe.Pointer(&data[0]))), toErr(r)
}
Beispiel #2
0
func getSerialNumber(dev *C.libusb_device, index C.uint8_t) string {
	data := make([]byte, 1024)
	var devHandle *C.libusb_device_handle
	if errno := C.libusb_open(dev, &devHandle); errno != 0 {
		return ""
	}
	defer Close(devHandle)
	errno := C.libusb_get_string_descriptor_ascii(devHandle, index, (*C.uchar)(unsafe.Pointer(&data[0])), C.int(len(data)))
	if errno <= 0 {
		return ""
	}
	return C.GoString((*C.char)(unsafe.Pointer(&data[0])))
}
Beispiel #3
0
func (d *Device) GetStringDescriptor(desc_index int) (string, error) {

	// allocate 200-byte array limited the length of string descriptor
	goBuffer := make([]byte, 200)

	// get string descriptor from libusb. if errno < 0 then there are any errors.
	// if errno >= 0; it is a length of result string descriptor
	errno := C.libusb_get_string_descriptor_ascii(
		d.handle,
		C.uint8_t(desc_index),
		(*C.uchar)(unsafe.Pointer(&goBuffer[0])),
		200)

	// if any errors occur
	if errno < 0 {
		return "", fmt.Errorf("usb: getstr: %s", usbError(errno))
	}
	// convert slice of byte to string with limited length from errno
	stringDescriptor := string(goBuffer[:errno])

	return stringDescriptor, nil
}