1 /* *****************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * *****************************************************************/
22 #include "srmutility.h"
24 #include "OCProvisioningManager.h"
28 OCStackResult OCSecure::provisionInit(const std::string& dbPath)
31 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
35 std::lock_guard<std::recursive_mutex> lock(*cLock);
36 result = OCInitPM(dbPath.c_str());
40 oclog() <<"Mutex not found";
41 result = OC_STACK_ERROR;
47 OCStackResult OCSecure::discoverUnownedDevices(unsigned short timeout,
51 OCProvisionDev_t *pDevList = nullptr, *pCurDev = nullptr, *tmp = nullptr;
52 auto csdkLock = OCPlatform_impl::Instance().csdkLock();
53 auto cLock = csdkLock.lock();
57 std::lock_guard<std::recursive_mutex> lock(*cLock);
58 result = OCDiscoverUnownedDevices(timeout, &pDevList);
59 if (result == OC_STACK_OK)
61 // Create DeviceList of OCSecureResource's
66 list.push_back(std::shared_ptr<OCSecureResource>(
67 new OCSecureResource(csdkLock, pCurDev)));
68 pCurDev = pCurDev->next;
74 oclog() <<"Unowned device discovery failed!";
79 oclog() <<"Mutex not found";
80 result = OC_STACK_ERROR;
86 OCStackResult OCSecure::discoverOwnedDevices(unsigned short timeout,
90 OCProvisionDev_t *pDevList = nullptr, *pCurDev = nullptr, *tmp = nullptr;
91 auto csdkLock = OCPlatform_impl::Instance().csdkLock();
92 auto cLock = csdkLock.lock();
96 std::lock_guard<std::recursive_mutex> lock(*cLock);
97 result = OCDiscoverOwnedDevices(timeout, &pDevList);
98 if (result == OC_STACK_OK)
104 list.push_back(std::shared_ptr<OCSecureResource>(
105 new OCSecureResource(csdkLock, pCurDev)));
106 pCurDev = pCurDev->next;
112 oclog() <<"Owned device discovery failed!";
117 oclog() <<"Mutex not found";
118 result = OC_STACK_ERROR;
124 OCStackResult OCSecure::setOwnerTransferCallbackData(OicSecOxm_t oxm,
125 OTMCallbackData_t* callbackData, InputPinCallback inputPin)
127 if (NULL == callbackData || oxm >= OIC_OXM_COUNT)
129 oclog() <<"Invalid callbackData or OXM type";
130 return OC_STACK_INVALID_PARAM;
133 if ((OIC_RANDOM_DEVICE_PIN == oxm) && !inputPin)
135 oclog() <<"for OXM type DEVICE_PIN, inputPin callback can't be null";
136 return OC_STACK_INVALID_PARAM;
139 OCStackResult result;
140 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
144 std::lock_guard<std::recursive_mutex> lock(*cLock);
145 result = OCSetOwnerTransferCallbackData(oxm, callbackData);
146 if (result == OC_STACK_OK && (OIC_RANDOM_DEVICE_PIN == oxm))
148 SetInputPinCB(inputPin);
153 oclog() <<"Mutex not found";
154 result = OC_STACK_ERROR;
161 OCStackResult OCSecure::getDevInfoFromNetwork(unsigned short timeout,
162 DeviceList_t &ownedDevList,
163 DeviceList_t &unownedDevList)
165 OCStackResult result = OC_STACK_OK;
166 OCProvisionDev_t *owned = nullptr, *unowned = nullptr, *tmp = nullptr, *dev = nullptr;
167 auto csdkLock = OCPlatform_impl::Instance().csdkLock();
168 auto cLock = csdkLock.lock();
172 std::lock_guard<std::recursive_mutex> lock(*cLock);
174 result = OCGetDevInfoFromNetwork(timeout, &owned, &unowned);
176 if (result == OC_STACK_OK)
182 ownedDevList.push_back(std::shared_ptr<OCSecureResource>(
183 new OCSecureResource(csdkLock, dev)));
192 unownedDevList.push_back(std::shared_ptr<OCSecureResource>(
193 new OCSecureResource(csdkLock, dev)));
201 oclog() <<"Mutex not found";
202 result = OC_STACK_ERROR;
208 OCStackResult OCSecure::setDisplayPinCB(GeneratePinCallback displayPin)
212 oclog() <<"displayPin can't be null";
213 return OC_STACK_INVALID_PARAM;
216 OCStackResult result = OC_STACK_OK;
217 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
221 std::lock_guard<std::recursive_mutex> lock(*cLock);
222 SetGeneratePinCB(displayPin);
226 oclog() <<"Mutex not found";
227 result = OC_STACK_ERROR;
233 void OCSecureResource::callbackWrapper(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
235 PMResultList_t *results = nullptr;
236 ProvisionContext* context = static_cast<ProvisionContext*>(ctx);
240 results = new PMResultList_t;
242 catch (std::bad_alloc& e)
244 oclog() <<"Bad alloc exception";
248 for (int i = 0; i < nOfRes; i++)
250 results->push_back(arr[i]);
253 std::thread exec(context->callback, results, hasError);
259 OCSecureResource::OCSecureResource(): m_csdkLock(std::weak_ptr<std::recursive_mutex>()),
264 OCSecureResource::OCSecureResource(std::weak_ptr<std::recursive_mutex> csdkLock,
265 OCProvisionDev_t *dPtr)
266 :m_csdkLock(csdkLock), devPtr(dPtr)
270 OCSecureResource::~OCSecureResource()
274 OCDeleteDiscoveredDevices(devPtr);
278 OCStackResult OCSecureResource::doOwnershipTransfer(ResultCallBack resultCallback)
282 oclog() <<"Result callback can't be null";
283 return OC_STACK_INVALID_CALLBACK;
286 OCStackResult result;
287 auto cLock = m_csdkLock.lock();
291 ProvisionContext* context = new ProvisionContext(resultCallback);
293 std::lock_guard<std::recursive_mutex> lock(*cLock);
294 result = OCDoOwnershipTransfer(static_cast<void*>(context),
295 devPtr, &OCSecureResource::callbackWrapper);
299 oclog() <<"Mutex not found";
300 result = OC_STACK_ERROR;
305 OCStackResult OCSecureResource::provisionACL( const OicSecAcl_t* acl,
306 ResultCallBack resultCallback)
310 oclog() <<"ACL can't be null";
311 return OC_STACK_INVALID_PARAM;
315 oclog() <<"result callback can not be null";
316 return OC_STACK_INVALID_CALLBACK;
319 OCStackResult result;
320 auto cLock = m_csdkLock.lock();
324 ProvisionContext* context = new ProvisionContext(resultCallback);
326 std::lock_guard<std::recursive_mutex> lock(*cLock);
327 result = OCProvisionACL(static_cast<void*>(context),
328 devPtr, const_cast<OicSecAcl_t*>(acl),
329 &OCSecureResource::callbackWrapper);
333 oclog() <<"Mutex not found";
334 result = OC_STACK_ERROR;
339 OCStackResult OCSecureResource::provisionCredentials(const Credential &cred,
340 const OCSecureResource &device2, ResultCallBack resultCallback)
344 oclog() << "Result calback can't be null";
345 return OC_STACK_INVALID_CALLBACK;
348 OCStackResult result;
349 auto cLock = m_csdkLock.lock();
353 ProvisionContext* context = new ProvisionContext(resultCallback);
355 std::lock_guard<std::recursive_mutex> lock(*cLock);
356 result = OCProvisionCredentials(static_cast<void*>(context),
357 cred.getCredentialType(),
358 cred.getCredentialKeySize(),
359 devPtr, device2.getDevPtr(),
360 &OCSecureResource::callbackWrapper);
364 oclog() <<"Mutex not found";
365 result = OC_STACK_ERROR;
370 OCStackResult OCSecureResource::provisionPairwiseDevices(const Credential &cred,
371 const OicSecAcl_t* acl1, const OCSecureResource &device2, const OicSecAcl_t* acl2,
372 ResultCallBack resultCallback)
376 oclog() << "Result callback can not be null";
377 return OC_STACK_INVALID_CALLBACK;
380 OCStackResult result;
381 auto cLock = m_csdkLock.lock();
385 ProvisionContext* context = new ProvisionContext(resultCallback);
387 std::lock_guard<std::recursive_mutex> lock(*cLock);
388 result = OCProvisionPairwiseDevices(static_cast<void*>(context),
389 cred.getCredentialType(),
390 cred.getCredentialKeySize(),
391 devPtr, const_cast<OicSecAcl_t*>(acl1),
392 device2.getDevPtr(), const_cast<OicSecAcl_t*>(acl2),
393 &OCSecureResource::callbackWrapper);
397 oclog() <<"Mutex not found";
398 result = OC_STACK_ERROR;
403 OCStackResult OCSecureResource::unlinkDevices(const OCSecureResource &device2,
404 ResultCallBack resultCallback)
408 oclog() << "Result calback can't be null";
409 return OC_STACK_INVALID_CALLBACK;
412 OCStackResult result;
413 auto cLock = m_csdkLock.lock();
417 ProvisionContext* context = new ProvisionContext(resultCallback);
419 std::lock_guard<std::recursive_mutex> lock(*cLock);
421 result = OCUnlinkDevices(static_cast<void*>(context),
422 devPtr, device2.getDevPtr(), &OCSecureResource::callbackWrapper);
426 oclog() <<"Mutex not found";
427 result = OC_STACK_ERROR;
432 OCStackResult OCSecureResource::removeDevice(unsigned short waitTimeForOwnedDeviceDiscovery,
433 ResultCallBack resultCallback)
437 oclog() << "Result calback can't be null";
438 return OC_STACK_INVALID_CALLBACK;
441 OCStackResult result;
442 auto cLock = m_csdkLock.lock();
446 ProvisionContext* context = new ProvisionContext(resultCallback);
448 std::lock_guard<std::recursive_mutex> lock(*cLock);
450 result = OCRemoveDevice(static_cast<void*>(context), waitTimeForOwnedDeviceDiscovery,
451 devPtr, &OCSecureResource::callbackWrapper);
455 oclog() <<"Mutex not found";
456 result = OC_STACK_ERROR;
461 OCStackResult OCSecureResource::removeDeviceWithUuid(unsigned short waitTimeForOwnedDeviceDiscovery,
463 ResultCallBack resultCallback)
467 oclog() << "Result calback can't be null";
468 return OC_STACK_INVALID_CALLBACK;
471 OCStackResult result;
472 auto cLock = m_csdkLock.lock();
476 ProvisionContext* context = new ProvisionContext(resultCallback);
478 std::lock_guard<std::recursive_mutex> lock(*cLock);
481 result = ConvertStrToUuid(uuid.c_str(), &targetDev);
482 if(OC_STACK_OK == result)
484 result = OCRemoveDeviceWithUuid(static_cast<void*>(context), waitTimeForOwnedDeviceDiscovery,
485 &targetDev, &OCSecureResource::callbackWrapper);
489 oclog() <<"Can not convert struuid to uuid";
494 oclog() <<"Mutex not found";
495 result = OC_STACK_ERROR;
500 OCStackResult OCSecureResource::getLinkedDevices(UuidList_t &uuidList)
502 OCStackResult result;
503 size_t numOfDevices = -1;
504 auto devUuid = devPtr->doxm->deviceID;
505 auto cLock = m_csdkLock.lock();
509 std::lock_guard<std::recursive_mutex> lock(*cLock);
511 OCUuidList_t* linkedDevs = nullptr, *tmp = nullptr;
512 result = OCGetLinkedStatus(&devUuid, &linkedDevs, &numOfDevices);
513 if (result == OC_STACK_OK)
515 for (tmp = linkedDevs; tmp; tmp = tmp->next)
517 uuidList.push_back(tmp->dev);
519 OCDeleteUuidList(linkedDevs);
524 oclog() <<"Mutex not found";
525 result = OC_STACK_ERROR;
530 OCStackResult OCSecureResource::provisionDirectPairing( const OicSecPconf_t* pconf,
531 ResultCallBack resultCallback)
535 oclog() <<"PCONF can't be null";
536 return OC_STACK_INVALID_PARAM;
540 oclog() <<"result callback can not be null";
541 return OC_STACK_INVALID_CALLBACK;
544 OCStackResult result;
545 auto cLock = m_csdkLock.lock();
549 ProvisionContext* context = new ProvisionContext(resultCallback);
551 std::lock_guard<std::recursive_mutex> lock(*cLock);
552 result = OCProvisionDirectPairing(static_cast<void*>(context),
553 devPtr, const_cast<OicSecPconf_t*>(pconf),
554 &OCSecureResource::callbackWrapper);
558 oclog() <<"Mutex not found";
559 result = OC_STACK_ERROR;
564 std::string OCSecureResource::getDeviceID()
566 std::ostringstream deviceId("");
567 char *devID = nullptr;
569 validateSecureResource();
571 if (OC_STACK_OK == ConvertUuidToStr(&(devPtr->doxm->deviceID), &devID))
578 oclog() <<"Can not convert uuid to struuid";
580 return deviceId.str();
583 OCProvisionDev_t* OCSecureResource::getDevPtr() const
588 std::string OCSecureResource::getDevAddr()
590 validateSecureResource();
591 std::string ipAddr(devPtr->endpoint.addr);
595 int OCSecureResource::getDeviceStatus()
597 validateSecureResource();
598 return (int)devPtr->devStatus;
601 bool OCSecureResource::getOwnedStatus()
603 validateSecureResource();
604 return devPtr->doxm->owned;
607 void OCSecureResource::validateSecureResource()
611 throw OCException("Incomplete secure resource", OC_STACK_RESOURCE_ERROR);