示例#1
0
文件: usb.go 项目: thomasf/bpchart
// ListDevices calls each with each enumerated device.
// If the function returns true, the device is opened and a Device is returned if the operation succeeds.
// Every Device returned (whether an error is also returned or not) must be closed.
// If there are any errors enumerating the devices,
// the final one is returned along with any successfully opened devices.
func (c *Context) ListDevices(each func(desc *Descriptor) bool) ([]*Device, error) {
	var list **C.libusb_device
	cnt := C.libusb_get_device_list(c.ctx, &list)
	if cnt < 0 {
		return nil, usbError(cnt)
	}
	defer C.libusb_free_device_list(list, 1)

	var slice []*C.libusb_device
	*(*reflect.SliceHeader)(unsafe.Pointer(&slice)) = reflect.SliceHeader{
		Data: uintptr(unsafe.Pointer(list)),
		Len:  int(cnt),
		Cap:  int(cnt),
	}

	var reterr error
	ret := []*Device{}
	for _, dev := range slice {
		desc, err := newDescriptor(dev)
		if err != nil {
			reterr = err
			continue
		}

		if each(desc) {
			var handle *C.libusb_device_handle
			if errno := C.libusb_open(dev, &handle); errno != 0 {
				reterr = err
				continue
			}
			ret = append(ret, newDevice(handle, desc))
		}
	}
	return ret, reterr
}
示例#2
0
// Open the device and returns a new Device instance
func (d *Descriptor) Open() (*Device, error) {
	var handle *C.libusb_device_handle
	if errno := C.libusb_open(d.dev, &handle); errno != 0 {
		return nil, usbError(errno)
	}
	return newDevice(handle, d), nil
}
示例#3
0
文件: hid_linux.go 项目: tam7t/hid
func (di *DeviceInfo) Open() (Device, error) {

	var devices **C.struct_libusb_device
	cnt := C.libusb_get_device_list(nil, &devices)
	if cnt < 0 {
		return nil, errors.New(fmt.Sprintf("Couldn't open USB device with path=%s, because couldn't enumerate USB devices.", di.Path))
	}
	defer C.libusb_free_device_list(devices, 1)

	for _, dev := range asSlice(devices, cnt) {
		candidate, err := newDeviceInfo(dev)
		if err != nil {
			return nil, err
		}
		if di.Path == candidate.Path {
			var handle *C.libusb_device_handle
			err := C.libusb_open(dev, &handle)
			if err != 0 {
				return nil, usbError(err)
			}
			dev := &linuxDevice{
				info:   candidate,
				handle: handle,
			}
			return dev, nil
		}
	}
	return nil, errors.New(fmt.Sprintf("Couldn't open USB device vendor=%4x product=%4x", di.VendorId, di.ProductId))
}
示例#4
0
文件: usb.go 项目: thequux/gousb
func (dev *Device) Open() (handle *DeviceHandle, err *UsbError) {
	handle = &DeviceHandle{dev.ctx, nil, 0, make(map[byte]*Interface, 0)}
	_, err = decodeUsbError(C.libusb_open(dev.device, &handle.handle))
	if err != nil {
		handle = nil
	}
	return
}
示例#5
0
// Open a device and obtain a device handle.
func (d *Device) Open() (*DeviceHandle, error) {
	var h *C.libusb_device_handle
	r := C.libusb_open(d.me(), &h)
	if r < 0 {
		return nil, fmt.Errorf("error %d", r)
	}
	return (*DeviceHandle)(h), nil
}
示例#6
0
文件: eeprom.go 项目: sstallion/go
// Open opens an attached device and claims the interface. To ensure proper
// reference counting, Open must be called within the context of a Walk.
func (d *Device) Open() error {
	if err := C.libusb_open(d.dev, &d.handle); err != C.LIBUSB_SUCCESS {
		return &libusbError{err}
	}
	if err := C.libusb_claim_interface(d.handle, interfaceNum); err != C.LIBUSB_SUCCESS {
		C.libusb_close(d.handle)
		return &libusbError{err}
	}
	return nil
}
示例#7
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])))
}
示例#8
0
文件: hid_linux.go 项目: tam7t/hid
func resolveDescriptors(dev *C.libusb_device, iManufacturer C.uint8_t, iProduct C.uint8_t) (manufacturer string, product string, e error) {
	var handle *C.libusb_device_handle
	err := C.libusb_open(dev, &handle)
	if err != 0 {
		return "", "", usbError(err)
	}
	if handle != nil {
		defer C.libusb_close(handle)
		manufacturerStr, err := getStringDescriptor(handle, iManufacturer)
		if err != nil {
			return "", "", err
		}
		productStr, err := getStringDescriptor(handle, iProduct)
		if err != nil {
			return "", "", err
		}
		return manufacturerStr, productStr, nil
	}
	return "", "", errors.New("Couldn't resolve description string")

}
示例#9
0
文件: usb.go 项目: karitra/mp707
func Lookup() (*list.List, *UsbError) {
	var li **C.struct_libusb_device

	n := C.libusb_get_device_list( nil, &li)
	if n <= 0 {
		return nil, MakeUsbError(C.int(n))
	}

	defer C.libusb_free_device_list( li, 1)

	// fmt.Println("Devices => ", n)

	mp707List := list.New()

	var ue *UsbError
	for i := 0; i < int(n); i++ {
		//fmt.Printf("Value: %v\n", C.get_dev(list, C.int(i)))
		if id, vendor, product, err := extractDesc(C.get_dev(li, C.int(i))); err != nil {
			// fmt.Fprintf(os.Stderr, "Error: %s\n", err)
			ue = err
			break
		} else if vendor == VENDOR_ID && product == PRODUCT_ID {
			//fmt.Printf("Vendor => %04x, Product => %04x\n", vendor, product ) 
			handle  := &C.struct_libusb_device_handle{}
			if err  := C.libusb_open( C.get_dev(li, C.int(i)), &handle ); err != 0 {
				// fmt.Fprintf(os.Stderr, "Open error: %s\n", MakeUsbError(err))
				ue = MakeUsbError(err)
				continue
			}

			mp707List.PushBack( &MP707Dev{ id: id, dev: handle } )
		}
	}

	// fmt.Println("List size: ", mp707List.Len() )

	return mp707List, ue
}
示例#10
0
文件: hotplug.go 项目: nkovacs/gousb
//export goCallback
func goCallback(ctx unsafe.Pointer, device unsafe.Pointer, event int, userdata unsafe.Pointer) C.int {
	dataID := userdata
	realCallback, ok := callbacks.get(dataID)
	if !ok {
		panic(fmt.Errorf("USB hotplug callback function not found"))
	}
	dev := (*C.libusb_device)(device)
	descriptor, err := newDescriptor(dev)
	if err != nil {
		// TODO: what to do here? add an error callback?
		panic(fmt.Errorf("Error during USB hotplug callback: %s", err))
		//return 0
	}
	var opener HotplugOpener
	if HotplugEvent(event) == HOTPLUG_EVENT_DEVICE_ARRIVED {
		opener = func() (*Device, error) {
			var handle *C.libusb_device_handle
			if errno := C.libusb_open(dev, &handle); errno != 0 {
				return nil, usbError(errno)
			}
			return newDevice(handle, descriptor), nil
		}
	} else {
		opener = func() (*Device, error) {
			return nil, errors.New("Opener called when event is not HOTPLUG_EVENT_DEVICE_ARRIVED.")
		}
	}
	if realCallback.f(descriptor, HotplugEvent(event), opener) {
		// callback will be unregistered, delete the data from memory.
		callbacks.remove(dataID)
		realCallback.ctx.removeCallback(dataID)
		return 1
	} else {
		return 0
	}
}
示例#11
0
文件: usb.go 项目: hanwen/usb
// Open a device and obtain a device handle.
func (d *Device) Open() (*DeviceHandle, error) {
	var h *C.libusb_device_handle
	r := C.libusb_open(d.me(), &h)
	return (*DeviceHandle)(h), toErr(r)
}