Motor::Motor(USB::Context& usbContext,size_t index) { /* Get the index-th Kinect motor device from the context: */ USB::DeviceList deviceList(usbContext); USB::Device::operator=(deviceList.getDevice(0x045e,0x02b0,index)); if(!isValid()) Misc::throwStdErr("Kinect::Motor::Motor: Less than %d Kinect motor devices detected",int(index)); /* Open and prepare the device: */ open(); setConfiguration(1); claimInterface(0); }
struct libusb_device_handle* openUSB(libusb_context *ctx, uint16_t vendor, uint16_t product, int interface, int autoconnect) { int ret = 0; struct libusb_device_handle *handle = NULL; /* Open USB communication */ ret = libusb_init(&ctx); if (ret < 0) { log_err("libusb_init failed: %i.", ret); goto error; } libusb_set_debug(ctx, 3); handle = libusb_open_device_with_vid_pid(ctx, vendor, product); if (!handle) { log_err("Unable to open device."); goto error; } if (interface != -1) { if (!claimInterface(handle, interface, autoconnect)) { goto error; } } exit: return handle; error: closeUSB(ctx, handle, interface); return NULL; }
bool Communicator::initUsbDevice(libusb_device *device) { libusb_device_descriptor desc; libusb_config_descriptor *config; const libusb_interface *inter; const libusb_interface_descriptor *interdesc; const libusb_endpoint_descriptor *epdesc; int r; bool result = false; if (device != NULL) { mDevice = device; r = libusb_open(device, &mHandle); if (r == 0 && mHandle != NULL) { syslog(LOG_INFO, "USB device opened"); r = libusb_get_device_descriptor(mDevice, &desc); if (r == 0) { r = libusb_get_config_descriptor(mDevice, 0, &config); if (r == 0) { int i = 0, j = 0; while (i < config->bNumInterfaces) { inter = &config->interface[i]; j = 0; while (j < inter->num_altsetting) { interdesc = &inter->altsetting[j]; if (interdesc->bInterfaceClass == 6) { uint8_t readEp = 129, writeEp = 2; for (int k = 0; k < (int) interdesc->bNumEndpoints; k++) { epdesc = &interdesc->endpoint[k]; if ((epdesc->bEndpointAddress == (LIBUSB_ENDPOINT_IN | LIBUSB_TRANSFER_TYPE_ISOCHRONOUS)) || (epdesc->bEndpointAddress == (LIBUSB_ENDPOINT_IN | LIBUSB_TRANSFER_TYPE_BULK))) { readEp = epdesc->bEndpointAddress; syslog(LOG_INFO, "Read endpoint adress: %d", readEp); } if ((epdesc->bEndpointAddress == (LIBUSB_ENDPOINT_OUT | LIBUSB_TRANSFER_TYPE_ISOCHRONOUS)) || (epdesc->bEndpointAddress == (LIBUSB_ENDPOINT_OUT | LIBUSB_TRANSFER_TYPE_BULK))) { writeEp = epdesc->bEndpointAddress; syslog(LOG_INFO, "Write endpoint adress: %d", writeEp); } } result = claimInterface(readEp, writeEp, i); if (result) { mVendorId = desc.idVendor; mProductId = desc.idProduct; } j = inter->num_altsetting; i = config->bNumInterfaces; } j++; } i++; } libusb_free_config_descriptor(config); } else syslog(LOG_ERR, "Error opening USB device config descriptor: %d", r); } else syslog(LOG_ERR, "Failed to get device descriptor: %d", r); } else syslog(LOG_ERR, "Error opening device"); } return result; }