Esempio n. 1
0
// 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
}
Esempio n. 2
0
// Walk calls the specified function for each supported device attached to the
// host. To ensure proper reference counting, Open must be called within the
// context of a Walk.
func Walk(fn func(*Device) error) error {
	var list **C.libusb_device
	var found int

	n := C.libusb_get_device_list(context, &list)
	if n < C.LIBUSB_SUCCESS {
		return &libusbError{C.int(n)}
	}
	defer C.libusb_free_device_list(list, 1)

	h := reflect.SliceHeader{
		Data: uintptr(unsafe.Pointer(list)),
		Len:  int(n),
		Cap:  int(n),
	}
	for _, dev := range *(*[]*C.libusb_device)(unsafe.Pointer(&h)) {
		var desc C.struct_libusb_device_descriptor

		if err := C.libusb_get_device_descriptor(dev, &desc); err != C.LIBUSB_SUCCESS {
			return &libusbError{err}
		}
		if desc.idVendor == idVendor && desc.idProduct == idProduct {
			if err := fn(&Device{dev: dev}); err != nil {
				return err
			}
			found++
		}
	}
	if found == 0 {
		return errors.New("no devices found")
	}
	return nil
}
Esempio n. 3
0
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))
}
Esempio n. 4
0
File: usb.go Progetto: thequux/gousb
func (ctx *Context) GetDeviceList() (dev []*Device, err *UsbError) {
	var (
		baseptr **C.struct_libusb_device
		devlist []*C.struct_libusb_device
	)
	count, err := decodeUsbError(C.int(C.libusb_get_device_list(ctx.ctx, &baseptr)))
	if err != nil {
		dev = nil
		return
	}

	hdr := &reflect.SliceHeader{Data: uintptr(unsafe.Pointer(baseptr)), Len: count, Cap: count}
	devlist = *(*[]*C.struct_libusb_device)(unsafe.Pointer(&hdr))
	dev = make([]*Device, count)
	for i := 0; i < count; i++ {
		dev[i] = ctx.wrapDevice(devlist[i])
	}
	devlist = nil
	C.libusb_free_device_list(baseptr, 1)
	return dev, nil
}
Esempio n. 5
0
File: usb.go Progetto: 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
}
Esempio n. 6
0
func Devices() <-chan *DeviceInfo {
	result := make(chan *DeviceInfo)
	go func() {
		var devices **C.struct_libusb_device
		cnt := C.libusb_get_device_list(nil, &devices)
		if cnt < 0 {
			close(result)
			return
		}
		defer C.libusb_free_device_list(devices, 1)

		for _, dev := range asSlice(devices, cnt) {
			di, err := newDeviceInfo(dev)
			if err != nil {
				fmt.Printf("ERROR: %s\n", err)
				continue
			}
			result <- di
		}

		close(result)
	}()
	return result
}
Esempio n. 7
0
File: usb.go Progetto: hanwen/usb
func (d DeviceList) Done() {
	C.libusb_free_device_list((**C.libusb_device)(unsafe.Pointer((&d[0]))), 1)
}