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 OCStackResult OCSecure::removeDeviceWithUuid(unsigned short waitTimeForOwnedDeviceDiscovery,
235 ResultCallBack resultCallback)
239 oclog() << "Result calback can't be null";
240 return OC_STACK_INVALID_CALLBACK;
243 OCStackResult result;
244 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
248 ProvisionContext* context = new ProvisionContext(resultCallback);
250 std::lock_guard<std::recursive_mutex> lock(*cLock);
253 result = ConvertStrToUuid(uuid.c_str(), &targetDev);
254 if(OC_STACK_OK == result)
256 result = OCRemoveDeviceWithUuid(static_cast<void*>(context), waitTimeForOwnedDeviceDiscovery,
257 &targetDev, &OCSecureResource::callbackWrapper);
261 oclog() <<"Can not convert struuid to uuid";
266 oclog() <<"Mutex not found";
267 result = OC_STACK_ERROR;
273 void OCSecureResource::callbackWrapper(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
275 PMResultList_t *results = nullptr;
276 ProvisionContext* context = static_cast<ProvisionContext*>(ctx);
280 results = new PMResultList_t;
282 catch (std::bad_alloc& e)
284 oclog() <<"Bad alloc exception";
288 for (int i = 0; i < nOfRes; i++)
290 results->push_back(arr[i]);
293 std::thread exec(context->callback, results, hasError);
299 OCSecureResource::OCSecureResource(): m_csdkLock(std::weak_ptr<std::recursive_mutex>()),
304 OCSecureResource::OCSecureResource(std::weak_ptr<std::recursive_mutex> csdkLock,
305 OCProvisionDev_t *dPtr)
306 :m_csdkLock(csdkLock), devPtr(dPtr)
310 OCSecureResource::~OCSecureResource()
314 OCDeleteDiscoveredDevices(devPtr);
318 OCStackResult OCSecureResource::doOwnershipTransfer(ResultCallBack resultCallback)
322 oclog() <<"Result callback can't be null";
323 return OC_STACK_INVALID_CALLBACK;
326 OCStackResult result;
327 auto cLock = m_csdkLock.lock();
331 ProvisionContext* context = new ProvisionContext(resultCallback);
333 std::lock_guard<std::recursive_mutex> lock(*cLock);
334 result = OCDoOwnershipTransfer(static_cast<void*>(context),
335 devPtr, &OCSecureResource::callbackWrapper);
339 oclog() <<"Mutex not found";
340 result = OC_STACK_ERROR;
345 OCStackResult OCSecureResource::provisionACL( const OicSecAcl_t* acl,
346 ResultCallBack resultCallback)
350 oclog() <<"ACL can't be null";
351 return OC_STACK_INVALID_PARAM;
355 oclog() <<"result callback can not be null";
356 return OC_STACK_INVALID_CALLBACK;
359 OCStackResult result;
360 auto cLock = m_csdkLock.lock();
364 ProvisionContext* context = new ProvisionContext(resultCallback);
366 std::lock_guard<std::recursive_mutex> lock(*cLock);
367 result = OCProvisionACL(static_cast<void*>(context),
368 devPtr, const_cast<OicSecAcl_t*>(acl),
369 &OCSecureResource::callbackWrapper);
373 oclog() <<"Mutex not found";
374 result = OC_STACK_ERROR;
379 OCStackResult OCSecureResource::provisionCredentials(const Credential &cred,
380 const OCSecureResource &device2, ResultCallBack resultCallback)
384 oclog() << "Result calback can't be null";
385 return OC_STACK_INVALID_CALLBACK;
388 OCStackResult result;
389 auto cLock = m_csdkLock.lock();
393 ProvisionContext* context = new ProvisionContext(resultCallback);
395 std::lock_guard<std::recursive_mutex> lock(*cLock);
396 result = OCProvisionCredentials(static_cast<void*>(context),
397 cred.getCredentialType(),
398 cred.getCredentialKeySize(),
399 devPtr, device2.getDevPtr(),
400 &OCSecureResource::callbackWrapper);
404 oclog() <<"Mutex not found";
405 result = OC_STACK_ERROR;
410 OCStackResult OCSecureResource::provisionPairwiseDevices(const Credential &cred,
411 const OicSecAcl_t* acl1, const OCSecureResource &device2, const OicSecAcl_t* acl2,
412 ResultCallBack resultCallback)
416 oclog() << "Result callback can not be null";
417 return OC_STACK_INVALID_CALLBACK;
420 OCStackResult result;
421 auto cLock = m_csdkLock.lock();
425 ProvisionContext* context = new ProvisionContext(resultCallback);
427 std::lock_guard<std::recursive_mutex> lock(*cLock);
428 result = OCProvisionPairwiseDevices(static_cast<void*>(context),
429 cred.getCredentialType(),
430 cred.getCredentialKeySize(),
431 devPtr, const_cast<OicSecAcl_t*>(acl1),
432 device2.getDevPtr(), const_cast<OicSecAcl_t*>(acl2),
433 &OCSecureResource::callbackWrapper);
437 oclog() <<"Mutex not found";
438 result = OC_STACK_ERROR;
443 OCStackResult OCSecureResource::unlinkDevices(const OCSecureResource &device2,
444 ResultCallBack resultCallback)
448 oclog() << "Result calback can't be null";
449 return OC_STACK_INVALID_CALLBACK;
452 OCStackResult result;
453 auto cLock = m_csdkLock.lock();
457 ProvisionContext* context = new ProvisionContext(resultCallback);
459 std::lock_guard<std::recursive_mutex> lock(*cLock);
461 result = OCUnlinkDevices(static_cast<void*>(context),
462 devPtr, device2.getDevPtr(), &OCSecureResource::callbackWrapper);
466 oclog() <<"Mutex not found";
467 result = OC_STACK_ERROR;
472 OCStackResult OCSecureResource::removeDevice(unsigned short waitTimeForOwnedDeviceDiscovery,
473 ResultCallBack resultCallback)
477 oclog() << "Result calback can't be null";
478 return OC_STACK_INVALID_CALLBACK;
481 OCStackResult result;
482 auto cLock = m_csdkLock.lock();
486 ProvisionContext* context = new ProvisionContext(resultCallback);
488 std::lock_guard<std::recursive_mutex> lock(*cLock);
490 result = OCRemoveDevice(static_cast<void*>(context), waitTimeForOwnedDeviceDiscovery,
491 devPtr, &OCSecureResource::callbackWrapper);
495 oclog() <<"Mutex not found";
496 result = OC_STACK_ERROR;
501 OCStackResult OCSecureResource::getLinkedDevices(UuidList_t &uuidList)
503 OCStackResult result;
504 size_t numOfDevices = -1;
505 auto devUuid = devPtr->doxm->deviceID;
506 auto cLock = m_csdkLock.lock();
510 std::lock_guard<std::recursive_mutex> lock(*cLock);
512 OCUuidList_t* linkedDevs = nullptr, *tmp = nullptr;
513 result = OCGetLinkedStatus(&devUuid, &linkedDevs, &numOfDevices);
514 if (result == OC_STACK_OK)
516 for (tmp = linkedDevs; tmp; tmp = tmp->next)
518 uuidList.push_back(tmp->dev);
520 OCDeleteUuidList(linkedDevs);
525 oclog() <<"Mutex not found";
526 result = OC_STACK_ERROR;
531 OCStackResult OCSecureResource::provisionDirectPairing( const OicSecPconf_t* pconf,
532 ResultCallBack resultCallback)
536 oclog() <<"PCONF can't be null";
537 return OC_STACK_INVALID_PARAM;
541 oclog() <<"result callback can not be null";
542 return OC_STACK_INVALID_CALLBACK;
545 OCStackResult result;
546 auto cLock = m_csdkLock.lock();
550 ProvisionContext* context = new ProvisionContext(resultCallback);
552 std::lock_guard<std::recursive_mutex> lock(*cLock);
553 result = OCProvisionDirectPairing(static_cast<void*>(context),
554 devPtr, const_cast<OicSecPconf_t*>(pconf),
555 &OCSecureResource::callbackWrapper);
559 oclog() <<"Mutex not found";
560 result = OC_STACK_ERROR;
565 std::string OCSecureResource::getDeviceID()
567 std::ostringstream deviceId("");
568 char *devID = nullptr;
570 validateSecureResource();
572 if (OC_STACK_OK == ConvertUuidToStr(&(devPtr->doxm->deviceID), &devID))
579 oclog() <<"Can not convert uuid to struuid";
581 return deviceId.str();
584 OCProvisionDev_t* OCSecureResource::getDevPtr() const
589 std::string OCSecureResource::getDevAddr()
591 validateSecureResource();
592 std::string ipAddr(devPtr->endpoint.addr);
596 int OCSecureResource::getDeviceStatus()
598 validateSecureResource();
599 return (int)devPtr->devStatus;
602 bool OCSecureResource::getOwnedStatus()
604 validateSecureResource();
605 return devPtr->doxm->owned;
608 void OCSecureResource::validateSecureResource()
612 throw OCException("Incomplete secure resource", OC_STACK_RESOURCE_ERROR);