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);
237 results = new PMResultList_t;
238 for (int i = 0; i < nOfRes; i++)
240 results->push_back(arr[i]);
243 std::thread exec(context->callback, results, hasError);
249 OCSecureResource::OCSecureResource(): m_csdkLock(std::weak_ptr<std::recursive_mutex>()),
254 OCSecureResource::OCSecureResource(std::weak_ptr<std::recursive_mutex> csdkLock,
255 OCProvisionDev_t *dPtr)
256 :m_csdkLock(csdkLock), devPtr(dPtr)
260 OCSecureResource::~OCSecureResource()
264 OCDeleteDiscoveredDevices(devPtr);
268 OCStackResult OCSecureResource::doOwnershipTransfer(ResultCallBack resultCallback)
272 oclog() <<"Result callback can't be null";
273 return OC_STACK_INVALID_PARAM;
276 OCStackResult result;
277 auto cLock = m_csdkLock.lock();
281 ProvisionContext* context = new ProvisionContext(resultCallback);
283 std::lock_guard<std::recursive_mutex> lock(*cLock);
284 result = OCDoOwnershipTransfer(static_cast<void*>(context),
285 devPtr, &OCSecureResource::callbackWrapper);
289 oclog() <<"Mutex not found";
290 result = OC_STACK_ERROR;
295 OCStackResult OCSecureResource::provisionACL( const OicSecAcl_t* acl,
296 ResultCallBack resultCallback)
298 if(!resultCallback || !acl)
300 oclog() <<"Result callback or ACL can't be null";
301 return OC_STACK_INVALID_PARAM;
304 OCStackResult result;
305 auto cLock = m_csdkLock.lock();
309 ProvisionContext* context = new ProvisionContext(resultCallback);
311 std::lock_guard<std::recursive_mutex> lock(*cLock);
312 result = OCProvisionACL(static_cast<void*>(context),
313 devPtr, const_cast<OicSecAcl_t*>(acl),
314 &OCSecureResource::callbackWrapper);
318 oclog() <<"Mutex not found";
319 result = OC_STACK_ERROR;
324 OCStackResult OCSecureResource::provisionCredentials(const Credential &cred,
325 const OCSecureResource &device2, ResultCallBack resultCallback)
329 oclog() << "Result calback can't be null";
330 return OC_STACK_INVALID_PARAM;
333 OCStackResult result;
334 auto cLock = m_csdkLock.lock();
338 ProvisionContext* context = new ProvisionContext(resultCallback);
340 std::lock_guard<std::recursive_mutex> lock(*cLock);
341 result = OCProvisionCredentials(static_cast<void*>(context),
342 cred.getCredentialType(),
343 cred.getCredentialKeySize(),
344 devPtr, device2.getDevPtr(),
345 &OCSecureResource::callbackWrapper);
349 oclog() <<"Mutex not found";
350 result = OC_STACK_ERROR;
355 OCStackResult OCSecureResource::provisionPairwiseDevices(const Credential &cred,
356 const OicSecAcl_t* acl1, const OCSecureResource &device2, const OicSecAcl_t* acl2,
357 ResultCallBack resultCallback)
361 oclog() << "Result calback can't be null";
362 return OC_STACK_INVALID_PARAM;
365 OCStackResult result;
366 auto cLock = m_csdkLock.lock();
370 ProvisionContext* context = new ProvisionContext(resultCallback);
372 std::lock_guard<std::recursive_mutex> lock(*cLock);
373 result = OCProvisionPairwiseDevices(static_cast<void*>(context),
374 cred.getCredentialType(),
375 cred.getCredentialKeySize(),
376 devPtr, const_cast<OicSecAcl_t*>(acl1),
377 device2.getDevPtr(), const_cast<OicSecAcl_t*>(acl2),
378 &OCSecureResource::callbackWrapper);
382 oclog() <<"Mutex not found";
383 result = OC_STACK_ERROR;
388 OCStackResult OCSecureResource::unlinkDevices(const OCSecureResource &device2,
389 ResultCallBack resultCallback)
393 oclog() << "Result calback can't be null";
394 return OC_STACK_INVALID_PARAM;
397 OCStackResult result;
398 auto cLock = m_csdkLock.lock();
402 ProvisionContext* context = new ProvisionContext(resultCallback);
404 std::lock_guard<std::recursive_mutex> lock(*cLock);
406 result = OCUnlinkDevices(static_cast<void*>(context),
407 devPtr, device2.getDevPtr(), &OCSecureResource::callbackWrapper);
411 oclog() <<"Mutex not found";
412 result = OC_STACK_ERROR;
417 OCStackResult OCSecureResource::removeDevice(unsigned short waitTimeForOwnedDeviceDiscovery,
418 ResultCallBack resultCallback)
422 oclog() << "Result calback can't be null";
423 return OC_STACK_INVALID_PARAM;
426 OCStackResult result;
427 auto cLock = m_csdkLock.lock();
431 ProvisionContext* context = new ProvisionContext(resultCallback);
433 std::lock_guard<std::recursive_mutex> lock(*cLock);
435 result = OCRemoveDevice(static_cast<void*>(context), waitTimeForOwnedDeviceDiscovery,
436 devPtr, &OCSecureResource::callbackWrapper);
440 oclog() <<"Mutex not found";
441 result = OC_STACK_ERROR;
446 OCStackResult OCSecureResource::getLinkedDevices(UuidList_t &uuidList)
448 OCStackResult result;
449 size_t numOfDevices = -1;
450 auto devUuid = devPtr->doxm->deviceID;
451 auto cLock = m_csdkLock.lock();
455 std::lock_guard<std::recursive_mutex> lock(*cLock);
457 OCUuidList_t* linkedDevs = nullptr, *tmp = nullptr;
458 result = OCGetLinkedStatus(&devUuid, &linkedDevs, &numOfDevices);
459 if (result == OC_STACK_OK)
461 for (tmp = linkedDevs; tmp; tmp = tmp->next)
463 uuidList.push_back(tmp->dev);
465 OCDeleteUuidList(linkedDevs);
470 oclog() <<"Mutex not found";
471 result = OC_STACK_ERROR;
476 std::string OCSecureResource::getDeviceID()
478 char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(((OicUuid_t*)0)->id)) + 1] = {0,};
480 B64Result b64Ret = B64_OK;
481 std::ostringstream deviceId("");
483 validateSecureResource();
484 b64Ret = b64Encode(devPtr->doxm->deviceID.id, sizeof(devPtr->doxm->deviceID.id), base64Buff,
485 sizeof(base64Buff), &outLen);
487 if (B64_OK == b64Ret)
489 deviceId << base64Buff;
491 return deviceId.str();
494 OCProvisionDev_t* OCSecureResource::getDevPtr() const
499 std::string OCSecureResource::getDevAddr()
501 validateSecureResource();
502 std::string ipAddr(devPtr->endpoint.addr);
506 int OCSecureResource::getDeviceStatus()
508 validateSecureResource();
509 return (int)devPtr->devStatus;
512 bool OCSecureResource::getOwnedStatus()
514 validateSecureResource();
515 return devPtr->doxm->owned;
518 void OCSecureResource::validateSecureResource()
522 throw OCException("Incomplete secure resource", OC_STACK_RESOURCE_ERROR);