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 void OCSecureResource::callbackWrapper(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
209 PMResultList_t *results = nullptr;
210 ProvisionContext* context = static_cast<ProvisionContext*>(ctx);
212 results = new PMResultList_t;
213 for (int i = 0; i < nOfRes; i++)
215 results->push_back(arr[i]);
218 std::thread exec(context->callback, results, hasError);
224 OCSecureResource::OCSecureResource(): m_csdkLock(std::weak_ptr<std::recursive_mutex>()),
229 OCSecureResource::OCSecureResource(std::weak_ptr<std::recursive_mutex> csdkLock,
230 OCProvisionDev_t *dPtr)
231 :m_csdkLock(csdkLock), devPtr(dPtr)
235 OCSecureResource::~OCSecureResource()
239 OCDeleteDiscoveredDevices(devPtr);
243 OCStackResult OCSecureResource::doOwnershipTransfer(ResultCallBack resultCallback)
247 oclog() <<"Result callback can't be null";
248 return OC_STACK_INVALID_PARAM;
251 OCStackResult result;
252 auto cLock = m_csdkLock.lock();
256 ProvisionContext* context = new ProvisionContext(resultCallback);
258 std::lock_guard<std::recursive_mutex> lock(*cLock);
259 result = OCDoOwnershipTransfer(static_cast<void*>(context),
260 devPtr, &OCSecureResource::callbackWrapper);
264 oclog() <<"Mutex not found";
265 result = OC_STACK_ERROR;
270 OCStackResult OCSecureResource::provisionACL( const OicSecAcl_t* acl,
271 ResultCallBack resultCallback)
273 if(!resultCallback || !acl)
275 oclog() <<"Result callback or ACL can't be null";
276 return OC_STACK_INVALID_PARAM;
279 OCStackResult result;
280 auto cLock = m_csdkLock.lock();
284 ProvisionContext* context = new ProvisionContext(resultCallback);
286 std::lock_guard<std::recursive_mutex> lock(*cLock);
287 result = OCProvisionACL(static_cast<void*>(context),
288 devPtr, const_cast<OicSecAcl_t*>(acl),
289 &OCSecureResource::callbackWrapper);
293 oclog() <<"Mutex not found";
294 result = OC_STACK_ERROR;
299 OCStackResult OCSecureResource::provisionCredentials(const Credential &cred,
300 const OCSecureResource &device2, ResultCallBack resultCallback)
304 oclog() << "Result calback can't be null";
305 return OC_STACK_INVALID_PARAM;
308 OCStackResult result;
309 auto cLock = m_csdkLock.lock();
313 ProvisionContext* context = new ProvisionContext(resultCallback);
315 std::lock_guard<std::recursive_mutex> lock(*cLock);
316 result = OCProvisionCredentials(static_cast<void*>(context),
317 cred.getCredentialType(),
318 cred.getCredentialKeySize(),
319 devPtr, device2.getDevPtr(),
320 &OCSecureResource::callbackWrapper);
324 oclog() <<"Mutex not found";
325 result = OC_STACK_ERROR;
330 OCStackResult OCSecureResource::provisionPairwiseDevices(const Credential &cred,
331 const OicSecAcl_t* acl1, const OCSecureResource &device2, const OicSecAcl_t* acl2,
332 ResultCallBack resultCallback)
336 oclog() << "Result calback can't be null";
337 return OC_STACK_INVALID_PARAM;
340 OCStackResult result;
341 auto cLock = m_csdkLock.lock();
345 ProvisionContext* context = new ProvisionContext(resultCallback);
347 std::lock_guard<std::recursive_mutex> lock(*cLock);
348 result = OCProvisionPairwiseDevices(static_cast<void*>(context),
349 cred.getCredentialType(),
350 cred.getCredentialKeySize(),
351 devPtr, const_cast<OicSecAcl_t*>(acl1),
352 device2.getDevPtr(), const_cast<OicSecAcl_t*>(acl2),
353 &OCSecureResource::callbackWrapper);
357 oclog() <<"Mutex not found";
358 result = OC_STACK_ERROR;
363 OCStackResult OCSecureResource::unlinkDevices(const OCSecureResource &device2,
364 ResultCallBack resultCallback)
368 oclog() << "Result calback can't be null";
369 return OC_STACK_INVALID_PARAM;
372 OCStackResult result;
373 auto cLock = m_csdkLock.lock();
377 ProvisionContext* context = new ProvisionContext(resultCallback);
379 std::lock_guard<std::recursive_mutex> lock(*cLock);
381 result = OCUnlinkDevices(static_cast<void*>(context),
382 devPtr, device2.getDevPtr(), &OCSecureResource::callbackWrapper);
386 oclog() <<"Mutex not found";
387 result = OC_STACK_ERROR;
392 OCStackResult OCSecureResource::removeDevice(unsigned short waitTimeForOwnedDeviceDiscovery,
393 ResultCallBack resultCallback)
397 oclog() << "Result calback can't be null";
398 return OC_STACK_INVALID_PARAM;
401 OCStackResult result;
402 auto cLock = m_csdkLock.lock();
406 ProvisionContext* context = new ProvisionContext(resultCallback);
408 std::lock_guard<std::recursive_mutex> lock(*cLock);
410 result = OCRemoveDevice(static_cast<void*>(context), waitTimeForOwnedDeviceDiscovery,
411 devPtr, &OCSecureResource::callbackWrapper);
415 oclog() <<"Mutex not found";
416 result = OC_STACK_ERROR;
421 OCStackResult OCSecureResource::getLinkedDevices(UuidList_t &uuidList)
423 OCStackResult result;
424 size_t numOfDevices = -1;
425 auto devUuid = devPtr->doxm->deviceID;
426 auto cLock = m_csdkLock.lock();
430 std::lock_guard<std::recursive_mutex> lock(*cLock);
432 OCUuidList_t* linkedDevs = nullptr, *tmp = nullptr;
433 result = OCGetLinkedStatus(&devUuid, &linkedDevs, &numOfDevices);
434 if (result == OC_STACK_OK)
436 for (tmp = linkedDevs; tmp; tmp = tmp->next)
438 uuidList.push_back(tmp->dev);
440 OCDeleteUuidList(linkedDevs);
445 oclog() <<"Mutex not found";
446 result = OC_STACK_ERROR;
451 std::string OCSecureResource::getDeviceID()
453 char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(((OicUuid_t*)0)->id)) + 1] = {0,};
455 B64Result b64Ret = B64_OK;
456 std::ostringstream deviceId("");
458 validateSecureResource();
459 b64Ret = b64Encode(devPtr->doxm->deviceID.id, sizeof(devPtr->doxm->deviceID.id), base64Buff,
460 sizeof(base64Buff), &outLen);
462 if (B64_OK == b64Ret)
464 deviceId << base64Buff;
466 return deviceId.str();
469 OCProvisionDev_t* OCSecureResource::getDevPtr() const
474 std::string OCSecureResource::getDevAddr()
476 validateSecureResource();
477 std::string ipAddr(devPtr->endpoint.addr);
481 int OCSecureResource::getDeviceStatus()
483 validateSecureResource();
484 return (int)devPtr->devStatus;
487 bool OCSecureResource::getOwnedStatus()
489 validateSecureResource();
490 return devPtr->doxm->owned;
493 void OCSecureResource::validateSecureResource()
497 throw OCException("Incomplete secure resource", OC_STACK_RESOURCE_ERROR);