+
+ std::shared_ptr<OCDirectPairing> cloneDevice(const OCDPDev_t* dev)
+ {
+ if (!dev)
+ {
+ return nullptr;
+ }
+
+ OCDPDev_t* result = new OCDPDev_t(*dev);
+ result->prm = new OCPrm_t[dev->prmLen];
+ memcpy(result->prm, dev->prm, sizeof(OCPrm_t)*dev->prmLen);
+ return std::shared_ptr<OCDirectPairing>(new OCDirectPairing(result));
+ }
+
+ void InProcClientWrapper::convert(const OCDPDev_t *list, PairedDevices& dpList)
+ {
+ while(list)
+ {
+ dpList.push_back(cloneDevice(list));
+ list = list->next;
+ }
+ }
+
+ OCStackResult InProcClientWrapper::FindDirectPairingDevices(unsigned short waittime,
+ GetDirectPairedCallback& callback)
+ {
+ if (!callback || 0 == waittime)
+ {
+ return OC_STACK_INVALID_PARAM;
+ }
+
+ OCStackResult result = OC_STACK_ERROR;
+ const OCDPDev_t *list = nullptr;
+ PairedDevices dpDeviceList;
+
+ auto cLock = m_csdkLock.lock();
+
+ if (cLock)
+ {
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
+
+ list = OCDiscoverDirectPairingDevices(waittime);
+ if (NULL == list)
+ {
+ result = OC_STACK_NO_RESOURCE;
+ oclog() << "findDirectPairingDevices(): No device found for direct pairing"
+ << std::flush;
+ }
+ else {
+ convert(list, dpDeviceList);
+ std::thread exec(callback, dpDeviceList);
+ exec.detach();
+ result = OC_STACK_OK;
+ }
+ }
+ else
+ {
+ result = OC_STACK_ERROR;
+ }
+
+ return result;
+ }
+
+ OCStackResult InProcClientWrapper::GetDirectPairedDevices(GetDirectPairedCallback& callback)
+ {
+ if (!callback)
+ {
+ return OC_STACK_INVALID_PARAM;
+ }
+
+ OCStackResult result = OC_STACK_ERROR;
+ const OCDPDev_t *list = nullptr;
+ PairedDevices dpDeviceList;
+
+ auto cLock = m_csdkLock.lock();
+
+ if (cLock)
+ {
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
+
+ list = OCGetDirectPairedDevices();
+ if (NULL == list)
+ {
+ result = OC_STACK_NO_RESOURCE;
+ oclog() << "findDirectPairingDevices(): No device found for direct pairing"
+ << std::flush;
+ }
+ else {
+ convert(list, dpDeviceList);
+ std::thread exec(callback, dpDeviceList);
+ exec.detach();
+ result = OC_STACK_OK;
+ }
+ }
+ else
+ {
+ result = OC_STACK_ERROR;
+ }
+
+ return result;
+ }
+
+ void directPairingCallback(void *ctx, OCDPDev_t *peer,
+ OCStackResult result)
+ {
+
+ ClientCallbackContext::DirectPairingContext* context =
+ static_cast<ClientCallbackContext::DirectPairingContext*>(ctx);
+
+ std::thread exec(context->callback, cloneDevice(peer), result);
+ exec.detach();
+ }
+
+ OCStackResult InProcClientWrapper::DoDirectPairing(std::shared_ptr<OCDirectPairing> peer,
+ const OCPrm_t& pmSel, const std::string& pinNumber, DirectPairingCallback& callback)
+ {
+ if (!peer || !callback)
+ {
+ oclog() << "Invalid parameters" << std::flush;
+ return OC_STACK_INVALID_PARAM;
+ }
+
+ OCStackResult result = OC_STACK_ERROR;
+ ClientCallbackContext::DirectPairingContext* context =
+ new ClientCallbackContext::DirectPairingContext(callback);
+
+ auto cLock = m_csdkLock.lock();
+ if (cLock)
+ {
+ std::lock_guard<std::recursive_mutex> lock(*cLock);
+ result = OCDoDirectPairing(static_cast<void*>(context), peer->getDev(),
+ pmSel, const_cast<char*>(pinNumber.c_str()), directPairingCallback);
+ }
+ else
+ {
+ delete context;
+ result = OC_STACK_ERROR;
+ }
+ return result;
+ }