XnStatus XnDeviceManagerInit(const XnChar* strDevicesDir) { XnStatus nRetVal = XN_STATUS_OK; // check if device manager is already initialized if (g_pDeviceManager != NULL) { return XN_STATUS_ALREADY_INIT; } // allocate data XN_VALIDATE_ALLOC(g_pDeviceManager, XnDeviceManagerData); g_pDeviceManager->nDevicesCount = 0; // load devices nRetVal = XnDeviceManagerLoadAllDevices(strDevicesDir); XN_IS_STATUS_OK(nRetVal); // make sure we found at least one device if (g_pDeviceManager->nDevicesCount == 0) { return (XN_STATUS_IO_NO_DEVICES); } return (XN_STATUS_OK); }
XN_C_API XnStatus xnSchedulerAddTask(XnScheduler* pScheduler, XnUInt64 nInterval, XnTaskCallbackFuncPtr pCallback, void* pCallbackArg, XnScheduledTask** ppTask) { XnStatus nRetVal = XN_STATUS_OK; XN_VALIDATE_INPUT_PTR(pScheduler); XN_VALIDATE_INPUT_PTR(pCallback); XN_VALIDATE_OUTPUT_PTR(ppTask); // create node XnScheduledTask* pTask; XN_VALIDATE_ALLOC(pTask, XnScheduledTask); pTask->nInterval = nInterval; pTask->pCallback = pCallback; pTask->pCallbackArg = pCallbackArg; // calculate next execution XnUInt64 nNow; xnOSGetTimeStamp(&nNow); pTask->nNextTime = nNow + nInterval; pTask->pNextTask = NULL; // enter critical section nRetVal = xnOSEnterCriticalSection(&pScheduler->hCriticalSection); if (nRetVal != XN_STATUS_OK) { xnOSFree(pTask); return (nRetVal); } xnSchedulerAddTaskInternal(pScheduler, pTask); // leave critical section nRetVal = xnOSLeaveCriticalSection(&pScheduler->hCriticalSection); if (nRetVal != XN_STATUS_OK) { xnOSFree(pTask); return (nRetVal); } // notify that the list has changed nRetVal = xnOSSetEvent(pScheduler->hWakeThreadEvent); if (nRetVal != XN_STATUS_OK) { xnLogWarning(XN_MASK_SCHEDULER, "Failed setting event when adding task: %s", xnGetStatusString(nRetVal)); } *ppTask = pTask; return (XN_STATUS_OK); }
//--------------------------------------------------------------------------- // Code //--------------------------------------------------------------------------- XN_C_API XnStatus xnOSCreateCriticalSection(XN_CRITICAL_SECTION_HANDLE* pCriticalSectionHandle) { // Validate the input/output pointers (to make sure none of them is NULL) XN_VALIDATE_INPUT_PTR(pCriticalSectionHandle); // allocate critical section memory XN_VALIDATE_ALLOC(*pCriticalSectionHandle, CRITICAL_SECTION); // initialize the critical section object via OS InitializeCriticalSection(*pCriticalSectionHandle); // All is good... return (XN_STATUS_OK); }
XN_DDK_API XnStatus XnPropertySetCreate(XnPropertySet** ppSet) { XN_VALIDATE_OUTPUT_PTR(ppSet); XnPropertySet* pSet; XN_VALIDATE_ALLOC(pSet, XnPropertySet); pSet->pData = XN_NEW(XnPropertySetData); if (pSet->pData == NULL) { xnOSFree(pSet); return XN_STATUS_ALLOC_FAILED; } *ppSet = pSet; return (XN_STATUS_OK); }
//--------------------------------------------------------------------------- // Code //--------------------------------------------------------------------------- XN_C_API XnStatus xnOSCreateThread(XN_THREAD_PROC_PROTO pThreadProc, const XN_THREAD_PARAM pThreadParam, XN_THREAD_HANDLE* pThreadHandle) { // Validate the input/output pointers (to make sure none of them is NULL) XN_VALIDATE_INPUT_PTR(pThreadProc); XN_VALIDATE_OUTPUT_PTR(pThreadHandle); // allocate thread handle XN_VALIDATE_ALLOC(*pThreadHandle, pthread_t); // Create a thread via the OS int rc = pthread_create(*pThreadHandle, NULL, pThreadProc, pThreadParam); if (rc != 0) { XN_FREE_AND_NULL(*pThreadHandle); return (XN_STATUS_OS_THREAD_CREATION_FAILED); } // All is good... return (XN_STATUS_OK); }
XN_C_API XnStatus xnUSBOpenDeviceImpl(libusb_device* pDevice, XN_USB_DEV_HANDLE* pDevHandlePtr) { XnStatus nRetVal = XN_STATUS_OK; if (pDevice == NULL) { return (XN_STATUS_USB_DEVICE_NOT_FOUND); } // allocate device handle libusb_device_handle* handle; // open device int rc = libusb_open(pDevice, &handle); // in any case, unref the device (we don't need it anymore) libusb_unref_device(pDevice); pDevice = NULL; // now check if open failed if (rc != 0) { return (XN_STATUS_USB_DEVICE_OPEN_FAILED); } /* // set for the first (and only) configuration (this will perform a light-weight reset) rc = libusb_set_configuration(handle, 1); if (rc != 0) { libusb_close(handle); return (XN_STATUS_USB_SET_CONFIG_FAILED); } */ // claim the interface (you cannot open any end point before claiming the interface) rc = libusb_claim_interface(handle, 0); if (rc != 0) { libusb_close(handle); return (XN_STATUS_USB_SET_INTERFACE_FAILED); } /* // set the alternate setting to default rc = libusb_set_interface_alt_setting(handle, 0, 0); if (rc != 0) { libusb_close(handle); return (XN_STATUS_USB_SET_INTERFACE_FAILED); } */ XN_VALIDATE_ALLOC(*pDevHandlePtr, XnUSBDeviceHandle); XN_USB_DEV_HANDLE pDevHandle = *pDevHandlePtr; pDevHandle->hDevice = handle; pDevHandle->nInterface = 0; pDevHandle->nAltSetting = 0; // mark the device is of high-speed pDevHandle->nDevSpeed = XN_USB_DEVICE_HIGH_SPEED; nRetVal = xnUSBAsynchThreadAddRef(); if (nRetVal != XN_STATUS_OK) { xnOSFree(*pDevHandlePtr); return (nRetVal); } return (XN_STATUS_OK); }
XN_C_API XnStatus xnUSBOpenDeviceImpl(libusb_device* pDevice, XN_USB_DEV_HANDLE* pDevHandlePtr) { XnStatus nRetVal = XN_STATUS_OK; if (pDevice == NULL) { return (XN_STATUS_USB_DEVICE_NOT_FOUND); } // allocate device handle libusb_device_handle* handle; // open device int rc = libusb_open(pDevice, &handle); // in any case, unref the device (we don't need it anymore) libusb_unref_device(pDevice); pDevice = NULL; // now check if open failed if (rc != 0) { return (XN_STATUS_USB_DEVICE_OPEN_FAILED); } /* // set for the first (and only) configuration (this will perform a light-weight reset) rc = libusb_set_configuration(handle, 1); if (rc != 0) { libusb_close(handle); return (XN_STATUS_USB_SET_CONFIG_FAILED); } */ // Asking the kernel politely to detach every other driver form this device. // If other drivers or programs are attached to the interface, it cannot be claimed. xnLogVerbose(XN_MASK_USB, "Detaching other kernel drivers."); rc = libusb_detach_kernel_driver(handle, 0); if(rc == 0){ xnLogWarning(XN_MASK_USB, "Detach success. Drivers detached."); } else if(rc == LIBUSB_ERROR_NOT_FOUND){ xnLogWarning(XN_MASK_USB, "Detach success. No drivers were attached."); } else if(rc == LIBUSB_ERROR_INVALID_PARAM){ XN_LOG_ERROR_RETURN(XN_STATUS_USB_SET_CONFIG_FAILED, XN_MASK_USB, "Detach kernel driver error. The specified interface does not exist."); } else if(rc == LIBUSB_ERROR_NO_DEVICE){ XN_LOG_ERROR_RETURN(XN_STATUS_USB_SET_CONFIG_FAILED, XN_MASK_USB, "Detach kernel driver error. Device is disconnectd."); } else { XN_LOG_ERROR_RETURN(XN_STATUS_USB_SET_CONFIG_FAILED, XN_MASK_USB, "Detach kernel driver error. Unknown error."); } xnLogVerbose(XN_MASK_USB, "Claiming the interface."); // claim the interface (you cannot open any end point before claiming the interface) rc = libusb_claim_interface(handle, 0); if (rc != 0) { if(rc == LIBUSB_ERROR_NOT_FOUND){ xnLogError(XN_MASK_USB, "Interface claim failed: The specified interface does not exist."); } else if(rc == LIBUSB_ERROR_BUSY){ xnLogError(XN_MASK_USB, "Interface claim failed: Another program or driver has claimed the interface."); } else if(rc == LIBUSB_ERROR_NO_DEVICE){ xnLogError(XN_MASK_USB, "Interface claim failed: Device disconnected."); } else if (rc == LIBUSB_ERROR_OTHER){ xnLogError(XN_MASK_USB, "Interface claim failed: Other error."); } else { xnLogError(XN_MASK_USB, "Interface claim failed: Unknown error."); } libusb_close(handle); return (XN_STATUS_USB_SET_INTERFACE_FAILED); } /* // set the alternate setting to default rc = libusb_set_interface_alt_setting(handle, 0, 0); if (rc != 0) { libusb_close(handle); return (XN_STATUS_USB_SET_INTERFACE_FAILED); } */ XN_VALIDATE_ALLOC(*pDevHandlePtr, XnUSBDeviceHandle); XN_USB_DEV_HANDLE pDevHandle = *pDevHandlePtr; pDevHandle->hDevice = handle; pDevHandle->nInterface = 0; pDevHandle->nAltSetting = 0; // mark the device is of high-speed pDevHandle->nDevSpeed = XN_USB_DEVICE_HIGH_SPEED; return (XN_STATUS_OK); }