XN_C_API XnStatus xnUSBGetDeviceSpeed(XN_USB_DEV_HANDLE pDevHandle, XnUSBDeviceSpeed* pDevSpeed) { // validate parameters XN_VALIDATE_USB_INIT(); XN_VALIDATE_DEVICE_HANDLE(pDevHandle); XN_VALIDATE_OUTPUT_PTR(pDevSpeed); *pDevSpeed = pDevHandle->nDevSpeed; return (XN_STATUS_OK); }
XN_C_API XnStatus xnUSBSetInterface(XN_USB_DEV_HANDLE pDevHandle, XnUInt8 nInterface, XnUInt8 nAltInterface) { // validate parameters XN_VALIDATE_USB_INIT(); XN_VALIDATE_DEVICE_HANDLE(pDevHandle); int rc = libusb_set_interface_alt_setting(pDevHandle->hDevice, nInterface, nAltInterface); if (rc != 0) { return (XN_STATUS_USB_SET_INTERFACE_FAILED); } pDevHandle->nInterface = nInterface; pDevHandle->nAltSetting = nAltInterface; return (XN_STATUS_OK); }
XN_C_API XnStatus xnUSBCloseDevice(XN_USB_DEV_HANDLE pDevHandle) { // validate parameters XN_VALIDATE_USB_INIT(); XN_VALIDATE_DEVICE_HANDLE(pDevHandle); int rc = libusb_release_interface(pDevHandle->hDevice, pDevHandle->nInterface); if (0 != rc) { return (XN_STATUS_USB_DEVICE_CLOSE_FAILED); } libusb_close(pDevHandle->hDevice); XN_FREE_AND_NULL(pDevHandle); return (XN_STATUS_OK); }
XN_C_API XnStatus xnUSBOpenEndPoint(XN_USB_DEV_HANDLE pDevHandle, XnUInt16 nEndPointID, XnUSBEndPointType nEPType, XnUSBDirectionType nDirType, XN_USB_EP_HANDLE* pEPHandlePtr) { // validate parameters XN_VALIDATE_USB_INIT(); XN_VALIDATE_DEVICE_HANDLE(pDevHandle); XN_VALIDATE_OUTPUT_PTR(pEPHandlePtr); // get the device from the handle libusb_device* pDevice = libusb_get_device(pDevHandle->hDevice); // get the configuration descriptor libusb_config_descriptor* pConfig; int rc = libusb_get_active_config_descriptor(pDevice, &pConfig); if (rc != 0) { return (XN_STATUS_USB_CONFIG_QUERY_FAILED); } // make sure configuration contains the interface we need if (pConfig->bNumInterfaces <= pDevHandle->nInterface) { libusb_free_config_descriptor(pConfig); return (XN_STATUS_USB_INTERFACE_QUERY_FAILED); } // take that interface const libusb_interface* pInterface = &pConfig->interface[pDevHandle->nInterface]; // make sure interface contains the alternate setting we work with if (pInterface->num_altsetting <= pDevHandle->nAltSetting) { libusb_free_config_descriptor(pConfig); return (XN_STATUS_USB_INTERFACE_QUERY_FAILED); } // take that setting const libusb_interface_descriptor* pInterfaceDesc = &pInterface->altsetting[pDevHandle->nAltSetting]; // search for the requested endpoint const libusb_endpoint_descriptor* pEndpointDesc = NULL; for (uint8_t i = 0; i < pInterfaceDesc->bNumEndpoints; ++i) { if (pInterfaceDesc->endpoint[i].bEndpointAddress == nEndPointID) { pEndpointDesc = &pInterfaceDesc->endpoint[i]; break; } } if (pEndpointDesc == NULL) { libusb_free_config_descriptor(pConfig); return (XN_STATUS_USB_ENDPOINT_NOT_FOUND); } libusb_transfer_type transfer_type = (libusb_transfer_type)(pEndpointDesc->bmAttributes & 0x3); // lower 2-bits // calculate max packet size // NOTE: we do not use libusb functions (libusb_get_max_packet_size/libusb_get_max_iso_packet_size) because // they hace a bug and does not consider alternative interface XnUInt32 nMaxPacketSize = 0; if (transfer_type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS) { XnUInt32 wMaxPacketSize = pEndpointDesc->wMaxPacketSize; // bits 11 and 12 mark the number of additional transactions, bits 0-10 mark the size XnUInt32 nAdditionalTransactions = wMaxPacketSize >> 11; XnUInt32 nPacketSize = wMaxPacketSize & 0x7FF; nMaxPacketSize = (nAdditionalTransactions + 1) * (nPacketSize); }