Esempio n. 1
0
File: usb.go Progetto: hanwen/usb
func (h *DeviceHandle) InterruptTransfer(endpoint byte, data []byte, timeout int) (actual int, err error) {
	var ptr *byte
	if len(data) > 0 {
		ptr = &data[0]
	}

	var n C.int
	e := C.libusb_bulk_transfer(h.me(), C.uchar(endpoint), (*C.uchar)(ptr),
		C.int(len(data)), &n, C.uint(timeout))
	return int(n), toErr(e)
}
Esempio n. 2
0
File: io.go Progetto: thequux/gousb
func bulkTransfer(ep *EndpointHandle, p []byte, _ bool) (n int, err error) {
	var transferred C.int
	err0 := returnUsbError(C.libusb_bulk_transfer(
		ep.handle.handle,
		C.uchar(ep.ep),
		(*C.uchar)(unsafe.Pointer(&p[0])),
		C.int(len(p)),
		&transferred,
		0))
	if err0 != nil {
		err = err0
	}

	return int(transferred), err
}
Esempio n. 3
0
func bulk_xfer(e *endpoint, buf []byte, timeout time.Duration) (int, error) {
	if len(buf) == 0 {
		return 0, nil
	}

	data := (*reflect.SliceHeader)(unsafe.Pointer(&buf)).Data

	var cnt C.int
	if errno := C.libusb_bulk_transfer(
		e.handle,
		C.uchar(e.Address),
		(*C.uchar)(unsafe.Pointer(data)),
		C.int(len(buf)),
		&cnt,
		C.uint(timeout/time.Millisecond)); errno < 0 {
		return 0, usbError(errno)
	}
	return int(cnt), nil
}
Esempio n. 4
0
func (d *Device) transferN(endpoint uint8, data []byte, n int) error {
	if m := int(C.libusb_get_max_packet_size(d.dev, C.uchar(endpoint))); n == 0 {
		n = m
	} else if n > m {
		return errors.New("invalid packet size")
	}

	for len, off := len(data), 0; len > 0; {
		var transferred int

		if n > len {
			n = len
		}
		if err := C.libusb_bulk_transfer(d.handle, C.uchar(endpoint), (*C.uchar)(&data[off]), C.int(n),
			(*C.int)(unsafe.Pointer(&transferred)), 2500); err != C.LIBUSB_SUCCESS {
			return &libusbError{err}
		}
		len -= transferred
		off += transferred
	}
	return nil
}