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 * *****************************************************************/
23 #include "OCProvisioningManager.h"
27 OCStackResult OCSecure::provisionInit(const std::string& dbPath)
30 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
34 std::lock_guard<std::recursive_mutex> lock(*cLock);
35 result = OCInitPM(dbPath.c_str());
39 oclog() <<"Mutex not found";
40 result = OC_STACK_ERROR;
46 OCStackResult OCSecure::discoverUnownedDevices(unsigned short timeout,
50 OCProvisionDev_t *pDevList = nullptr, *pCurDev = nullptr, *tmp = nullptr;
51 auto csdkLock = OCPlatform_impl::Instance().csdkLock();
52 auto cLock = csdkLock.lock();
56 std::lock_guard<std::recursive_mutex> lock(*cLock);
57 result = OCDiscoverUnownedDevices(timeout, &pDevList);
58 if (result == OC_STACK_OK)
60 // Create DeviceList of OCSecureResource's
65 list.push_back(std::shared_ptr<OCSecureResource>(
66 new OCSecureResource(csdkLock, pCurDev)));
67 pCurDev = pCurDev->next;
73 oclog() <<"Unowned device discovery failed!";
78 oclog() <<"Mutex not found";
79 result = OC_STACK_ERROR;
85 OCStackResult OCSecure::discoverOwnedDevices(unsigned short timeout,
89 OCProvisionDev_t *pDevList = nullptr, *pCurDev = nullptr, *tmp = nullptr;
90 auto csdkLock = OCPlatform_impl::Instance().csdkLock();
91 auto cLock = csdkLock.lock();
95 std::lock_guard<std::recursive_mutex> lock(*cLock);
96 result = OCDiscoverOwnedDevices(timeout, &pDevList);
97 if (result == OC_STACK_OK)
103 list.push_back(std::shared_ptr<OCSecureResource>(
104 new OCSecureResource(csdkLock, pCurDev)));
105 pCurDev = pCurDev->next;
111 oclog() <<"Owned device discovery failed!";
116 oclog() <<"Mutex not found";
117 result = OC_STACK_ERROR;
123 OCStackResult OCSecure::setOwnerTransferCallbackData(OicSecOxm_t oxm,
124 OTMCallbackData_t* callbackData, InputPinCallback inputPin)
126 if(NULL == callbackData || oxm >= OIC_OXM_COUNT)
128 oclog() <<"Invalid callbackData or OXM type";
129 return OC_STACK_INVALID_PARAM;
132 if((OIC_RANDOM_DEVICE_PIN == oxm) && !inputPin)
134 oclog() <<"for OXM type DEVICE_PIN, inputPin callback can't be null";
135 return OC_STACK_INVALID_PARAM;
138 OCStackResult result;
139 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
143 std::lock_guard<std::recursive_mutex> lock(*cLock);
144 result = OCSetOwnerTransferCallbackData(oxm, callbackData);
145 if(result == OC_STACK_OK && (OIC_RANDOM_DEVICE_PIN == oxm))
147 SetInputPinCB(inputPin);
152 oclog() <<"Mutex not found";
153 result = OC_STACK_ERROR;
160 OCStackResult OCSecure::getDevInfoFromNetwork(unsigned short timeout,
161 DeviceList_t &ownedDevList,
162 DeviceList_t &unownedDevList)
164 OCStackResult result = OC_STACK_OK;
165 OCProvisionDev_t *owned = nullptr, *unowned = nullptr, *tmp = nullptr, *dev = nullptr;
166 auto csdkLock = OCPlatform_impl::Instance().csdkLock();
167 auto cLock = csdkLock.lock();
171 std::lock_guard<std::recursive_mutex> lock(*cLock);
173 result = OCGetDevInfoFromNetwork(timeout, &owned, &unowned);
175 if (result == OC_STACK_OK)
181 ownedDevList.push_back(std::shared_ptr<OCSecureResource>(
182 new OCSecureResource(csdkLock, dev)));
191 unownedDevList.push_back(std::shared_ptr<OCSecureResource>(
192 new OCSecureResource(csdkLock, dev)));
200 oclog() <<"Mutex not found";
201 result = OC_STACK_ERROR;
207 OCStackResult OCSecure::setDisplayPinCB(GeneratePinCallback displayPin)
211 oclog() <<"displayPin can't be null";
212 return OC_STACK_INVALID_PARAM;
215 OCStackResult result = OC_STACK_OK;
216 auto cLock = OCPlatform_impl::Instance().csdkLock().lock();
220 std::lock_guard<std::recursive_mutex> lock(*cLock);
221 SetGeneratePinCB(displayPin);
225 oclog() <<"Mutex not found";
226 result = OC_STACK_ERROR;
232 void OCSecureResource::callbackWrapper(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
234 PMResultList_t *results = nullptr;
235 ProvisionContext* context = static_cast<ProvisionContext*>(ctx);
239 results = new PMResultList_t;
241 catch (std::bad_alloc& e)
243 oclog() <<"Bad alloc exception";
247 for (int i = 0; i < nOfRes; i++)
249 results->push_back(arr[i]);
252 std::thread exec(context->callback, results, hasError);
258 OCSecureResource::OCSecureResource(): m_csdkLock(std::weak_ptr<std::recursive_mutex>()),
263 OCSecureResource::OCSecureResource(std::weak_ptr<std::recursive_mutex> csdkLock,
264 OCProvisionDev_t *dPtr)
265 :m_csdkLock(csdkLock), devPtr(dPtr)
269 OCSecureResource::~OCSecureResource()
273 OCDeleteDiscoveredDevices(devPtr);
277 OCStackResult OCSecureResource::doOwnershipTransfer(ResultCallBack resultCallback)
281 oclog() <<"Result callback can't be null";
282 return OC_STACK_INVALID_CALLBACK;
285 OCStackResult result;
286 auto cLock = m_csdkLock.lock();
290 ProvisionContext* context = new ProvisionContext(resultCallback);
292 std::lock_guard<std::recursive_mutex> lock(*cLock);
293 result = OCDoOwnershipTransfer(static_cast<void*>(context),
294 devPtr, &OCSecureResource::callbackWrapper);
298 oclog() <<"Mutex not found";
299 result = OC_STACK_ERROR;
304 OCStackResult OCSecureResource::provisionACL( const OicSecAcl_t* acl,
305 ResultCallBack resultCallback)
309 oclog() <<"ACL can't be null";
310 return OC_STACK_INVALID_PARAM;
314 oclog() <<"result callback can not be null";
315 return OC_STACK_INVALID_CALLBACK;
318 OCStackResult result;
319 auto cLock = m_csdkLock.lock();
323 ProvisionContext* context = new ProvisionContext(resultCallback);
325 std::lock_guard<std::recursive_mutex> lock(*cLock);
326 result = OCProvisionACL(static_cast<void*>(context),
327 devPtr, const_cast<OicSecAcl_t*>(acl),
328 &OCSecureResource::callbackWrapper);
332 oclog() <<"Mutex not found";
333 result = OC_STACK_ERROR;
338 OCStackResult OCSecureResource::provisionCredentials(const Credential &cred,
339 const OCSecureResource &device2, ResultCallBack resultCallback)
343 oclog() << "Result calback can't be null";
344 return OC_STACK_INVALID_CALLBACK;
347 OCStackResult result;
348 auto cLock = m_csdkLock.lock();
352 ProvisionContext* context = new ProvisionContext(resultCallback);
354 std::lock_guard<std::recursive_mutex> lock(*cLock);
355 result = OCProvisionCredentials(static_cast<void*>(context),
356 cred.getCredentialType(),
357 cred.getCredentialKeySize(),
358 devPtr, device2.getDevPtr(),
359 &OCSecureResource::callbackWrapper);
363 oclog() <<"Mutex not found";
364 result = OC_STACK_ERROR;
369 OCStackResult OCSecureResource::provisionPairwiseDevices(const Credential &cred,
370 const OicSecAcl_t* acl1, const OCSecureResource &device2, const OicSecAcl_t* acl2,
371 ResultCallBack resultCallback)
375 oclog() << "Result callback can not be null";
376 return OC_STACK_INVALID_CALLBACK;
379 OCStackResult result;
380 auto cLock = m_csdkLock.lock();
384 ProvisionContext* context = new ProvisionContext(resultCallback);
386 std::lock_guard<std::recursive_mutex> lock(*cLock);
387 result = OCProvisionPairwiseDevices(static_cast<void*>(context),
388 cred.getCredentialType(),
389 cred.getCredentialKeySize(),
390 devPtr, const_cast<OicSecAcl_t*>(acl1),
391 device2.getDevPtr(), const_cast<OicSecAcl_t*>(acl2),
392 &OCSecureResource::callbackWrapper);
396 oclog() <<"Mutex not found";
397 result = OC_STACK_ERROR;
402 OCStackResult OCSecureResource::unlinkDevices(const OCSecureResource &device2,
403 ResultCallBack resultCallback)
407 oclog() << "Result calback can't be null";
408 return OC_STACK_INVALID_CALLBACK;
411 OCStackResult result;
412 auto cLock = m_csdkLock.lock();
416 ProvisionContext* context = new ProvisionContext(resultCallback);
418 std::lock_guard<std::recursive_mutex> lock(*cLock);
420 result = OCUnlinkDevices(static_cast<void*>(context),
421 devPtr, device2.getDevPtr(), &OCSecureResource::callbackWrapper);
425 oclog() <<"Mutex not found";
426 result = OC_STACK_ERROR;
431 OCStackResult OCSecureResource::removeDevice(unsigned short waitTimeForOwnedDeviceDiscovery,
432 ResultCallBack resultCallback)
436 oclog() << "Result calback can't be null";
437 return OC_STACK_INVALID_CALLBACK;
440 OCStackResult result;
441 auto cLock = m_csdkLock.lock();
445 ProvisionContext* context = new ProvisionContext(resultCallback);
447 std::lock_guard<std::recursive_mutex> lock(*cLock);
449 result = OCRemoveDevice(static_cast<void*>(context), waitTimeForOwnedDeviceDiscovery,
450 devPtr, &OCSecureResource::callbackWrapper);
454 oclog() <<"Mutex not found";
455 result = OC_STACK_ERROR;
460 OCStackResult OCSecureResource::getLinkedDevices(UuidList_t &uuidList)
462 OCStackResult result;
463 size_t numOfDevices = -1;
464 auto devUuid = devPtr->doxm->deviceID;
465 auto cLock = m_csdkLock.lock();
469 std::lock_guard<std::recursive_mutex> lock(*cLock);
471 OCUuidList_t* linkedDevs = nullptr, *tmp = nullptr;
472 result = OCGetLinkedStatus(&devUuid, &linkedDevs, &numOfDevices);
473 if (result == OC_STACK_OK)
475 for (tmp = linkedDevs; tmp; tmp = tmp->next)
477 uuidList.push_back(tmp->dev);
479 OCDeleteUuidList(linkedDevs);
484 oclog() <<"Mutex not found";
485 result = OC_STACK_ERROR;
490 std::string OCSecureResource::getDeviceID()
492 char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(((OicUuid_t*)0)->id)) + 1] = {0,};
494 B64Result b64Ret = B64_OK;
495 std::ostringstream deviceId("");
497 validateSecureResource();
498 b64Ret = b64Encode(devPtr->doxm->deviceID.id, sizeof(devPtr->doxm->deviceID.id), base64Buff,
499 sizeof(base64Buff), &outLen);
501 if (B64_OK == b64Ret)
503 deviceId << base64Buff;
505 return deviceId.str();
508 OCProvisionDev_t* OCSecureResource::getDevPtr() const
513 std::string OCSecureResource::getDevAddr()
515 validateSecureResource();
516 std::string ipAddr(devPtr->endpoint.addr);
520 int OCSecureResource::getDeviceStatus()
522 validateSecureResource();
523 return (int)devPtr->devStatus;
526 bool OCSecureResource::getOwnedStatus()
528 validateSecureResource();
529 return devPtr->doxm->owned;
532 void OCSecureResource::validateSecureResource()
536 throw OCException("Incomplete secure resource", OC_STACK_RESOURCE_ERROR);