Example #1
0
// First returns the first supported device attached to the host. Unlike Walk,
// the returned Device is opened automatically. This function exists primarily
// for testing.
func First() (*Device, error) {
	handle := C.libusb_open_device_with_vid_pid(context, idVendor, idProduct)
	if handle == nil {
		return nil, errors.New("no devices found")
	}
	if err := C.libusb_claim_interface(handle, interfaceNum); err != C.LIBUSB_SUCCESS {
		C.libusb_close(handle)
		return nil, &libusbError{err}
	}
	return &Device{
		dev:    C.libusb_get_device(handle),
		handle: handle,
	}, nil
}
Example #2
0
// 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
	if err != nil {
		return nil, err
	}

	device := newDevice(handle, desc)
	return device, nil
}
Example #3
0
File: usb.go Project: hanwen/usb
// Get the underlying device for a handle.
func (h *DeviceHandle) Device() *Device {
	return (*Device)(C.libusb_get_device(h.me()))
}
Example #4
0
File: usb.go Project: thequux/gousb
// Return a *Device for the given handle.
func (h *DeviceHandle) GetDevice() *Device {
	return h.ctx.wrapDevice(C.libusb_get_device(h.handle))
}