func newDescriptor(dev *C.libusb_device) (*Descriptor, error) { var desc C.struct_libusb_device_descriptor if errno := C.libusb_get_device_descriptor(dev, &desc); errno < 0 { return nil, usbError(errno) } // Enumerate configurations var cfgs []ConfigInfo for i := 0; i < int(desc.bNumConfigurations); i++ { var cfg *C.struct_libusb_config_descriptor if errno := C.libusb_get_config_descriptor(dev, C.uint8_t(i), &cfg); errno < 0 { return nil, usbError(errno) } cfgs = append(cfgs, newConfig(dev, cfg)) C.libusb_free_config_descriptor(cfg) } return &Descriptor{ Bus: uint8(C.libusb_get_bus_number(dev)), Address: uint8(C.libusb_get_device_address(dev)), Spec: BCD(desc.bcdUSB), Device: BCD(desc.bcdDevice), Vendor: ID(desc.idVendor), Product: ID(desc.idProduct), Class: uint8(desc.bDeviceClass), SubClass: uint8(desc.bDeviceSubClass), Protocol: uint8(desc.bDeviceProtocol), SerialNumber: getSerialNumber(dev, desc.iSerialNumber), Configs: cfgs, dev: dev, }, nil }
// 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 }
func newDeviceInfo(dev *C.libusb_device) (*DeviceInfo, error) { var desc C.struct_libusb_device_descriptor if err := C.libusb_get_device_descriptor(dev, &desc); err < 0 { return nil, usbError(err) } manufacturer, product, err := resolveDescriptors(dev, desc.iManufacturer, desc.iProduct) if err == usbError(C.LIBUSB_ERROR_ACCESS) { manufacturer = "access not allowed" product = "access not allowed" } else if err != nil { return nil, err } path, err := getPath(dev, desc.idVendor, desc.idProduct) if err != nil { return nil, err } return &DeviceInfo{ Path: path, VendorId: uint16(desc.idVendor), ProductId: uint16(desc.idProduct), VersionNumber: uint16(desc.bcdDevice), Manufacturer: manufacturer, Product: product, }, nil }
func (dev *Device) GetDeviceDescriptor() (DeviceDescriptor, *UsbError) { //var desc = (*C.struct_libusb_device_descriptor)(&C.devdesc) var desc C.struct_libusb_device_descriptor //println(desc) err := returnUsbError(C.libusb_get_device_descriptor(dev.device, &desc)) if err != nil { return DeviceDescriptor{}, err } return parseDeviceDescriptor(&desc), err }
func extractDesc(dev *C.libusb_device) (id uint8, vendor int, product int, err *UsbError) { desc := &C.struct_libusb_device_descriptor{} //var a C.struct_foo if r := C.libusb_get_device_descriptor(dev, desc); r != C.int(0) { return 0, 0, 0, MakeUsbError( C.int(r) ) } return uint8(desc.iSerialNumber), int(desc.idVendor), int(desc.idProduct), nil }
func (d *Device) GetDeviceDescriptor() (*DeviceDescriptor, error) { // this relies on struct packing being equal. var dd DeviceDescriptor r := C.libusb_get_device_descriptor(d.me(), (*C.struct_libusb_device_descriptor)(unsafe.Pointer(&dd))) return &dd, toErr(r) }