Exemple #1
0
func (h *DeviceHandle) GetStringDescriptor(index byte, langid uint16) (string, *UsbError) {
	buf := make([]uint16, 128)

	rlen, err := decodeUsbError(C.libusb_get_string_descriptor(h.handle, C.uint8_t(index), C.uint16_t(langid), (*C.uchar)(unsafe.Pointer(&buf[0])), 256))
	if err != nil {
		return "", err
	}
	return string(utf16.Decode(buf[1 : rlen/2])), nil
}
Exemple #2
0
func (h *DeviceHandle) GetLangIds() ([]uint16, *UsbError) {
	var buf [256]C.uchar
	u16buf := (*[128]C.uint16_t)(unsafe.Pointer(&buf[0]))

	rlen, err := decodeUsbError(C.libusb_get_string_descriptor(h.handle, 0, 0, &buf[0], 256))

	if err != nil {
		return nil, err
	}
	// I'm explicitly ignoring the first two bytes, which are length and descriptor type, respectively.
	ret := make([]uint16, rlen/2-1)
	for i := 1; i < rlen/2; i++ {
		ret[i-1] = uint16(u16buf[i])
	}
	return ret, nil
}
Exemple #3
0
func getStringDescriptor(dev *C.libusb_device_handle, id C.uint8_t) (string, error) {
	var buf [128]C.char
	const langId = 0
	err := C.libusb_get_string_descriptor(dev, id, C.uint16_t(langId), (*C.uchar)(unsafe.Pointer(&buf[0])), C.int(len(buf)))
	if err < 0 {
		return "", usbError(err)
	}
	if err < 2 {
		return "", errors.New("not enough data for USB string descriptor")
	}
	l := C.int(buf[0])
	if l > err {
		return "", errors.New("USB string descriptor is too short")
	}
	b := buf[2:l]
	uni16 := make([]uint16, len(b)/2)
	for i := range uni16 {
		uni16[i] = uint16(b[i*2]) | uint16(b[i*2+1])<<8
	}
	return string(utf16.Decode(uni16)), nil
}