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"
24 #include "pmutility.h"
25 #include "srmutility.h"
26 #include "ownershiptransfermanager.h"
27 #ifdef _ENABLE_MULTIPLE_OWNER_
28 #include "multipleownershiptransfermanager.h"
29 #endif //_ENABLE_MULTIPLE_OWNER_
30 #include "oic_malloc.h"
32 #include "secureresourceprovider.h"
33 #include "provisioningdatabasemanager.h"
34 #include "credresource.h"
36 #include "aclresource.h" //Note: SRM internal header
37 #include "pconfresource.h"
39 #define TAG "OIC_OCPMAPI"
41 typedef struct Linkdata Linkdata_t;
45 const OCProvisionDev_t *pDev1;
46 OicSecAcl_t *pDev1Acl;
47 const OCProvisionDev_t *pDev2;
48 OicSecAcl_t *pDev2Acl;
49 OCProvisionResult_t *resArr;
51 int currentCountResults;
52 OCProvisionResultCB resultCallback;
56 #ifdef _ENABLE_MULTIPLE_OWNER_
57 typedef struct ProvPreconfPINCtx ProvPreconfPINCtx_t;
58 struct ProvPreconfPINCtx
61 const OCProvisionDev_t *devInfo;
64 OCProvisionResultCB resultCallback;
66 #endif //_ENABLE_MULTIPLE_OWNER_
69 * The function is responsible for initializaton of the provisioning manager. It will load
70 * provisioning database which have owned device's list and their linked status.
71 * TODO: In addition, if there is a device(s) which has not up-to-date credentials, this function will
72 * automatically try to update the deivce(s).
74 * @param[in] dbPath file path of the sqlite3 db
76 * @return OC_STACK_OK in case of success and other value otherwise.
78 OCStackResult OCInitPM(const char* dbPath)
80 return PDMInit(dbPath);
84 * The function is responsible for discovery of owned/unowned device is specified endpoint/deviceID.
85 * And this function will only return the specified device's response.
87 * @param[in] timeout Timeout in seconds, value till which function will listen to responses from
88 * server before returning the device.
89 * @param[in] deviceID deviceID of target device.
90 * @param[out] ppFoundDevice OCProvisionDev_t of found device
91 * @return OTM_SUCCESS in case of success and other value otherwise.
93 OCStackResult OCDiscoverSingleDevice(unsigned short timeout, const OicUuid_t* deviceID,
94 OCProvisionDev_t **ppFoundDevice)
96 if( NULL == ppFoundDevice || NULL != *ppFoundDevice || 0 == timeout || NULL == deviceID)
98 return OC_STACK_INVALID_PARAM;
101 return PMSingleDeviceDiscovery(timeout, deviceID, ppFoundDevice);
105 * The function is responsible for discovery of device is current subnet. It will list
106 * all the device in subnet which are not yet owned. Please call OCInit with OC_CLIENT_SERVER as
109 * @param[in] timeout Timeout in seconds, value till which function will listen to responses from
110 * server before returning the list of devices.
111 * @param[out] ppList List of candidate devices to be provisioned
112 * @return OTM_SUCCESS in case of success and other value otherwise.
114 OCStackResult OCDiscoverUnownedDevices(unsigned short timeout, OCProvisionDev_t **ppList)
116 if( ppList == NULL || *ppList != NULL || 0 == timeout)
118 return OC_STACK_INVALID_PARAM;
121 return PMDeviceDiscovery(timeout, false, ppList);
125 * The function is responsible for discovery of owned device is current subnet. It will list
126 * all the device in subnet which are owned by calling provisioning client.
128 * @param[in] timeout Timeout in seconds, value till which function will listen to responses from
129 * server before returning the list of devices.
130 * @param[out] ppList List of device owned by provisioning tool.
131 * @return OTM_SUCCESS in case of success and other value otherwise.
133 OCStackResult OCDiscoverOwnedDevices(unsigned short timeout, OCProvisionDev_t **ppList)
135 if( ppList == NULL || *ppList != NULL || 0 == timeout)
137 return OC_STACK_INVALID_PARAM;
140 return PMDeviceDiscovery(timeout, true, ppList);
143 #ifdef _ENABLE_MULTIPLE_OWNER_
145 * The function is responsible for discovery of MOT enabled device is current subnet.
147 * @param[in] timeout Timeout in seconds, value till which function will listen to responses from
148 * server before returning the list of devices.
149 * @param[out] ppList List of MOT enabled devices.
150 * @return OC_STACK_OK in case of success and other value otherwise.
152 OCStackResult OCDiscoverMultipleOwnerEnabledDevices(unsigned short timeout, OCProvisionDev_t **ppList)
154 if( ppList == NULL || *ppList != NULL || 0 == timeout)
156 return OC_STACK_INVALID_PARAM;
159 return PMMultipleOwnerDeviceDiscovery(timeout, false, ppList);
163 * The function is responsible for discovery of Multiple Owned device is current subnet.
165 * @param[in] timeout Timeout in seconds, value till which function will listen to responses from
166 * server before returning the list of devices.
167 * @param[out] ppList List of Multiple Owned devices.
168 * @return OC_STACK_OK in case of success and other value otherwise.
170 OCStackResult OCDiscoverMultipleOwnedDevices(unsigned short timeout, OCProvisionDev_t **ppList)
172 if( ppList == NULL || *ppList != NULL || 0 == timeout)
174 return OC_STACK_INVALID_PARAM;
177 return PMMultipleOwnerDeviceDiscovery(timeout, true, ppList);
182 * API to add preconfigured PIN to local SVR DB.
184 * @param[in] targetDeviceInfo Selected target device.
185 * @param[in] preconfigPin Preconfig PIN which is used while multiple owner authentication
186 * @param[in] preconfigPinLen Byte length of preconfigPin
188 * @return OC_STACK_OK in case of success and other value otherwise.
190 OCStackResult OCAddPreconfigPin(const OCProvisionDev_t *targetDeviceInfo,
191 const char *preconfigPin,
192 size_t preconfigPinLen)
194 return MOTAddPreconfigPIN(targetDeviceInfo, preconfigPin, preconfigPinLen);
198 OCStackResult OCDoMultipleOwnershipTransfer(void* ctx,
199 OCProvisionDev_t *targetDevices,
200 OCProvisionResultCB resultCallback)
202 if( NULL == targetDevices )
204 return OC_STACK_INVALID_PARAM;
206 if (NULL == resultCallback)
208 OIC_LOG(INFO, TAG, "OCDoOwnershipTransfer : NULL Callback");
209 return OC_STACK_INVALID_CALLBACK;
211 return MOTDoOwnershipTransfer(ctx, targetDevices, resultCallback);
214 #endif //_ENABLE_MULTIPLE_OWNER_
217 * API to register for particular OxM.
219 * @param[in] Ownership transfer method.
220 * @param[in] Implementation of callback functions for owership transfer.
221 * @return OC_STACK_OK in case of success and other value otherwise.
223 OCStackResult OCSetOwnerTransferCallbackData(OicSecOxm_t oxm, OTMCallbackData_t* callbackData)
225 if(NULL == callbackData)
227 return OC_STACK_INVALID_CALLBACK ;
230 return OTMSetOwnershipTransferCallbackData(oxm, callbackData);
233 OCStackResult OCDoOwnershipTransfer(void* ctx,
234 OCProvisionDev_t *targetDevices,
235 OCProvisionResultCB resultCallback)
237 if( NULL == targetDevices )
239 return OC_STACK_INVALID_PARAM;
243 OIC_LOG(INFO, TAG, "OCDoOwnershipTransfer : NULL Callback");
244 return OC_STACK_INVALID_CALLBACK;
246 return OTMDoOwnershipTransfer(ctx, targetDevices, resultCallback);
250 * This function deletes memory allocated to linked list created by OCDiscover_XXX_Devices API.
252 * @param[in] pList Pointer to OCProvisionDev_t which should be deleted.
254 void OCDeleteDiscoveredDevices(OCProvisionDev_t *pList)
256 PMDeleteDeviceList(pList);
260 * this function sends ACL information to resource.
262 * @param[in] ctx Application context would be returned in result callback.
263 * @param[in] selectedDeviceInfo Selected target device.
264 * @param[in] acl ACL to provision.
265 * @param[in] resultCallback callback provided by API user, callback will be called when provisioning
266 request recieves a response from resource server.
267 * @return OC_STACK_OK in case of success and other value otherwise.
269 OCStackResult OCProvisionACL(void* ctx, const OCProvisionDev_t *selectedDeviceInfo, OicSecAcl_t *acl,
270 OCProvisionResultCB resultCallback)
272 return SRPProvisionACL(ctx, selectedDeviceInfo, acl, resultCallback);
276 * function to save ACL which has several ACE into Acl of SVR.
278 * @param acl ACL to be saved in Acl of SVR.
279 * @return OC_STACK_OK in case of success and other value otherwise.
281 OCStackResult OCSaveACL(const OicSecAcl_t* acl)
283 return SRPSaveACL(acl);
287 * this function requests CRED information to resource.
289 * @param[in] ctx Application context would be returned in result callback.
290 * @param[in] selectedDeviceInfo Selected target device.
291 * @param[in] resultCallback callback provided by API user, callback will be called when provisioning
292 request recieves a response from resource server.
293 * @return OC_STACK_OK in case of success and other value otherwise.
295 OCStackResult OCGetCredResource(void* ctx, const OCProvisionDev_t *selectedDeviceInfo,
296 OCProvisionResultCB resultCallback)
298 return SRPGetCredResource(ctx, selectedDeviceInfo, resultCallback);
302 * this function requests ACL information to resource.
304 * @param[in] ctx Application context would be returned in result callback.
305 * @param[in] selectedDeviceInfo Selected target device.
306 * @param[in] resultCallback callback provided by API user, callback will be called when provisioning
307 request recieves a response from resource server.
308 * @return OC_STACK_OK in case of success and other value otherwise.
310 OCStackResult OCGetACLResource(void* ctx, const OCProvisionDev_t *selectedDeviceInfo,
311 OCProvisionResultCB resultCallback)
313 return SRPGetACLResource(ctx, selectedDeviceInfo, resultCallback);
317 OCStackResult OCReadTrustCertChain(uint16_t credId, uint8_t **trustCertChain,
320 return SRPReadTrustCertChain(credId, trustCertChain, chainSize);
323 * function to provision credential to devices.
325 * @param[in] ctx Application context would be returned in result callback.
326 * @param[in] type Type of credentials to be provisioned to the device.
327 * @param[in] pDev1 Pointer to OCProvisionDev_t instance,respresenting resource to be provsioned.
328 @param[in] pDev2 Pointer to OCProvisionDev_t instance,respresenting resource to be provsioned.
329 * @param[in] resultCallback callback provided by API user, callback will be called when
330 * provisioning request recieves a response from first resource server.
331 * @return OC_STACK_OK in case of success and other value otherwise.
333 OCStackResult OCProvisionCredentials(void *ctx, OicSecCredType_t type, size_t keySize,
334 const OCProvisionDev_t *pDev1,
335 const OCProvisionDev_t *pDev2,
336 OCProvisionResultCB resultCallback)
338 return SRPProvisionCredentials(ctx, type, keySize,
339 pDev1, pDev2, resultCallback);
344 * this function sends Direct-Pairing Configuration to a device.
346 * @param[in] ctx Application context would be returned in result callback.
347 * @param[in] selectedDeviceInfo Selected target device.
348 * @param[in] pconf PCONF pointer.
349 * @param[in] resultCallback callback provided by API user, callback will be called when provisioning
350 request recieves a response from resource server.
351 * @return OC_STACK_OK in case of success and other value otherwise.
353 OCStackResult OCProvisionDirectPairing(void* ctx, const OCProvisionDev_t *selectedDeviceInfo, OicSecPconf_t *pconf,
354 OCProvisionResultCB resultCallback)
356 return SRPProvisionDirectPairing(ctx, selectedDeviceInfo, pconf, resultCallback);
359 #ifdef _ENABLE_MULTIPLE_OWNER_
360 static void AddPreconfPinOxMCB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
362 ProvPreconfPINCtx_t* provCtx = (ProvPreconfPINCtx_t*)ctx;
365 OCStackResult res = MOTProvisionPreconfigPIN(provCtx->ctx, provCtx->devInfo, provCtx->pin, provCtx->pinLen, provCtx->resultCallback);
366 if(OC_STACK_OK != res)
369 provCtx->resultCallback(provCtx->ctx, nOfRes, arr, true);
374 OCStackResult OCProvisionPreconfigPin(void *ctx,
375 OCProvisionDev_t *targetDeviceInfo,
376 const char *preconfigPin,
377 size_t preconfigPinLen,
378 OCProvisionResultCB resultCallback)
380 if( NULL == targetDeviceInfo )
382 return OC_STACK_INVALID_PARAM;
384 if (NULL == resultCallback)
386 OIC_LOG(INFO, TAG, "OCProvisionPreconfigPinCredential : NULL Callback");
387 return OC_STACK_INVALID_CALLBACK;
390 ProvPreconfPINCtx_t* provCtx = (ProvPreconfPINCtx_t*)OICCalloc(1, sizeof(ProvPreconfPINCtx_t));
393 return OC_STACK_NO_MEMORY;
396 provCtx->devInfo = targetDeviceInfo;
397 provCtx->pin = preconfigPin;
398 provCtx->pinLen = preconfigPinLen;
399 provCtx->resultCallback = resultCallback;
401 * First of all, update OxMs to support preconfigured PIN OxM.
402 * In case of Preconfigured PIN OxM already supported on the server side,
403 * MOTAddMOTMethod API will be send POST Cred request.
404 * In case of Preconfigure PIN OxM not exist on the server side,
405 * the MOTAddMOTMethod API will be send POST doxm request to update OxMs and then send POST Cred request.
407 return MOTAddMOTMethod((void*)provCtx, targetDeviceInfo, OIC_PRECONFIG_PIN, AddPreconfPinOxMCB);
409 #endif //_ENABLE_MULTIPLE_OWNER_
412 * Function to unlink devices.
413 * This function will remove the credential & relationship between the two devices.
415 * @param[in] ctx Application context would be returned in result callback
416 * @param[in] pTargetDev1 first device information to be unlinked.
417 * @param[in] pTargetDev2 second device information to be unlinked.
418 * @param[in] resultCallback callback provided by API user, callback will be called when
419 * device unlink is finished.
420 * @return OC_STACK_OK in case of success and other value otherwise.
422 OCStackResult OCUnlinkDevices(void* ctx,
423 const OCProvisionDev_t* pTargetDev1,
424 const OCProvisionDev_t* pTargetDev2,
425 OCProvisionResultCB resultCallback)
427 OIC_LOG(INFO, TAG, "IN OCUnlinkDevices");
428 OCUuidList_t* idList = NULL;
431 if (!pTargetDev1 || !pTargetDev2 || !pTargetDev1->doxm || !pTargetDev2->doxm)
433 OIC_LOG(ERROR, TAG, "OCUnlinkDevices : NULL parameters");
434 return OC_STACK_INVALID_PARAM;
438 OIC_LOG(INFO, TAG, "OCUnlinkDevices : NULL Callback");
439 return OC_STACK_INVALID_CALLBACK;
441 if (0 == memcmp(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID, sizeof(OicUuid_t)))
443 OIC_LOG(INFO, TAG, "OCUnlinkDevices : Same device ID");
444 return OC_STACK_INVALID_PARAM;
447 // Get linked devices with the first device.
448 OCStackResult res = PDMGetLinkedDevices(&(pTargetDev1->doxm->deviceID), &idList, &numOfDev);
449 if (OC_STACK_OK != res)
451 OIC_LOG(ERROR, TAG, "OCUnlinkDevices : PDMgetOwnedDevices failed");
456 OIC_LOG(DEBUG, TAG, "OCUnlinkDevices : Can not find linked devices");
457 res = OC_STACK_INVALID_PARAM; // Input devices are not linked, No request is made
461 // Check the linked devices contains the second device. If yes send credential DELETE request.
462 OCUuidList_t* curDev = idList;
463 while (NULL != curDev)
465 if (memcmp(pTargetDev2->doxm->deviceID.id, curDev->dev.id, sizeof(curDev->dev.id)) == 0)
467 res = SRPUnlinkDevices(ctx, pTargetDev1, pTargetDev2, resultCallback);
468 if (OC_STACK_OK != res)
470 OIC_LOG(ERROR, TAG, "OCUnlinkDevices : Failed to unlink devices.");
474 curDev = curDev->next;
476 OIC_LOG(DEBUG, TAG, "No matched pair found from provisioning database");
477 res = OC_STACK_INVALID_PARAM; // Input devices are not linked, No request is made
480 OIC_LOG(INFO, TAG, "OUT OCUnlinkDevices");
482 PDMDestoryOicUuidLinkList(idList);
486 static OCStackResult RemoveDeviceInfoFromLocal(const OCProvisionDev_t* pTargetDev)
488 // Remove credential of revoked device from SVR database
489 OCStackResult res = OC_STACK_ERROR;
490 const OicSecCred_t *cred = NULL;
492 OIC_LOG(DEBUG, TAG, "IN RemoveDeviceInfoFromLocal");
493 cred = GetCredResourceData(&pTargetDev->doxm->deviceID);
496 res = RemoveCredential(&cred->subject);
497 if (res != OC_STACK_RESOURCE_DELETED)
499 OIC_LOG(ERROR, TAG, "RemoveDeviceInfoFromLocal : Failed to remove credential.");
504 * Change the device status as stale status.
505 * If all request are successed, this device information will be deleted.
507 res = PDMSetDeviceStale(&pTargetDev->doxm->deviceID);
508 if (res != OC_STACK_OK)
510 OIC_LOG(WARNING, TAG, "OCRemoveDevice : Failed to set device status as stale");
513 // TODO: We need to add new mechanism to clean up the stale state of the device.
515 // Close the DTLS session of the removed device.
516 CAEndpoint_t *endpoint = (CAEndpoint_t *)&pTargetDev->endpoint;
517 endpoint->port = pTargetDev->securePort;
518 CAResult_t caResult = CAcloseSslSession(endpoint);
519 if(CA_STATUS_OK != caResult)
521 OIC_LOG_V(WARNING, TAG, "OCRemoveDevice : Failed to close DTLS session : %d", caResult);
524 OIC_LOG(DEBUG, TAG, "OUT RemoveDeviceInfoFromLocal");
530 * Function to device revocation
531 * This function will remove credential of target device from all devices in subnet.
533 * @param[in] ctx Application context would be returned in result callback
534 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
535 * @param[in] pTargetDev Device information to be revoked.
536 * @param[in] resultCallback callback provided by API user, callback will be called when
537 * credential revocation is finished.
538 * @return OC_STACK_OK in case of success and other value otherwise.
540 OCStackResult OCRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
541 const OCProvisionDev_t* pTargetDev,
542 OCProvisionResultCB resultCallback)
544 OIC_LOG(INFO, TAG, "IN OCRemoveDevice");
545 OCStackResult res = OC_STACK_ERROR;
546 if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
548 OIC_LOG(INFO, TAG, "OCRemoveDevice : Invalied parameters");
549 return OC_STACK_INVALID_PARAM;
553 OIC_LOG(INFO, TAG, "OCRemoveDevice : NULL Callback");
554 return OC_STACK_INVALID_CALLBACK;
557 // Send DELETE requests to linked devices
558 OCStackResult resReq = OC_STACK_ERROR; // Check that we have to wait callback or not.
559 resReq = SRPRemoveDevice(ctx, waitTimeForOwnedDeviceDiscovery, pTargetDev, resultCallback);
560 if (OC_STACK_OK != resReq)
562 if (OC_STACK_CONTINUE == resReq)
564 OIC_LOG(DEBUG, TAG, "OCRemoveDevice : Revoked device has no linked device except PT.");
568 OIC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to invoke SRPRemoveDevice");
574 res = RemoveDeviceInfoFromLocal(pTargetDev);
575 if(OC_STACK_OK != res)
577 OIC_LOG(ERROR, TAG, "Filed to remove the device information from local.");
581 if(OC_STACK_CONTINUE == resReq)
584 * If there is no linked device, PM does not send any request.
585 * So we should directly invoke the result callback to inform the result of OCRemoveDevice.
589 resultCallback(ctx, 0, NULL, false);
595 OIC_LOG(INFO, TAG, "OUT OCRemoveDevice");
600 * Function to device revocation
601 * This function will remove credential of target device from all devices in subnet.
603 * @param[in] ctx Application context would be returned in result callback
604 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
605 * @param[in] pTargetDev Device information to be revoked.
606 * @param[in] resultCallback callback provided by API user, callback will be called when
607 * credential revocation is finished.
608 * @return OC_STACK_OK in case of success and other value otherwise.
610 OCStackResult OCRemoveDeviceWithUuid(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
611 const OicUuid_t* pTargetUuid,
612 OCProvisionResultCB resultCallback)
614 OIC_LOG(INFO, TAG, "IN OCRemoveDeviceWithUuid");
616 OCStackResult res = OC_STACK_ERROR;
617 OCProvisionDev_t* pTargetDev = NULL;
618 bool discoverdFlag = false;
619 OCProvisionDev_t* pOwnedDevList = NULL;
620 OCStackResult resReq = OC_STACK_CONTINUE;
622 if (!pTargetUuid || 0 == waitTimeForOwnedDeviceDiscovery)
624 OIC_LOG(INFO, TAG, "OCRemoveDeviceWithUuid : Invalied parameters");
625 return OC_STACK_INVALID_PARAM;
629 OIC_LOG(INFO, TAG, "OCRemoveDeviceWithUuid : NULL Callback");
630 return OC_STACK_INVALID_CALLBACK;
633 char* strUuid = NULL;
634 if(OC_STACK_OK != ConvertUuidToStr(pTargetUuid, &strUuid))
636 OIC_LOG(WARNING, TAG, "Failed to covert UUID to String.");
640 //Generate OCProvisionDev_t instance to use when target device not found on the network.
641 //In this case, the device id required only.
642 pTargetDev = (OCProvisionDev_t*)OICCalloc(1, sizeof(OCProvisionDev_t));
643 if(NULL == pTargetDev)
645 OIC_LOG(ERROR, TAG, "Failed to allocate memory.");
646 res = OC_STACK_NO_MEMORY;
649 pTargetDev->doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
650 if(NULL == pTargetDev->doxm)
652 OIC_LOG(ERROR, TAG, "Failed to allocate memory.");
653 res = OC_STACK_NO_MEMORY;
656 memcpy(pTargetDev->doxm->deviceID.id, pTargetUuid->id, sizeof(pTargetUuid->id));
658 OCUuidList_t* linkedDevices = NULL;
659 size_t numOfLinkedDevices = 0;
660 res = PDMGetLinkedDevices(pTargetUuid, &linkedDevices, &numOfLinkedDevices);
661 if(OC_STACK_OK != res)
663 OIC_LOG(ERROR, TAG, "Error in PDMGetLinkedDevices");
666 PDMDestoryOicUuidLinkList(linkedDevices);
668 //If there is no linked devices, device revocation step can be skipped.
669 if(0 != numOfLinkedDevices)
671 OIC_LOG_V(INFO, TAG, "[%s] linked with other devices.", strUuid);
672 //2. Find owned device from the network
673 res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
674 if (OC_STACK_OK != res)
676 OIC_LOG(ERROR, TAG, "OCRemoveDeviceWithUuid : Failed to PMDeviceDiscovery");
680 OCProvisionDev_t* tempDev = NULL;
681 LL_FOREACH(pOwnedDevList, tempDev)
683 if(memcmp(&tempDev->doxm->deviceID.id, pTargetUuid->id, sizeof(pTargetUuid->id)) == 0)
691 OIC_LOG_V(WARNING, TAG, "Can not find [%s] on the network.", strUuid);
692 OIC_LOG_V(WARNING, TAG, "[%s]'s information will be deleted from local and other devices.", strUuid);
696 OICFree(pTargetDev->doxm);
698 pTargetDev = tempDev;
699 discoverdFlag = true;
700 OIC_LOG_V(INFO, TAG, "[%s] is dectected on the network.", strUuid);
703 OIC_LOG_V(INFO, TAG, "Trying [%s] revocation.", strUuid);
705 // Send DELETE requests to linked devices
706 resReq = SRPRemoveDeviceWithoutDiscovery(ctx, pOwnedDevList, pTargetDev, resultCallback);
707 if (OC_STACK_OK != resReq)
709 if (OC_STACK_CONTINUE == resReq)
711 OIC_LOG(INFO, TAG, "OCRemoveDeviceWithUuid : Revoked device has no linked device except PT.");
715 OIC_LOG(ERROR, TAG, "OCRemoveDeviceWithUuid : Failed to invoke SRPRemoveDevice");
723 OIC_LOG_V(INFO, TAG, "There is no linked devices with [%s]", strUuid);
724 OIC_LOG(INFO, TAG, "Device discovery and SRPRemoveDevice will be skipped.");
727 res = RemoveDeviceInfoFromLocal(pTargetDev);
728 if(OC_STACK_OK != res)
730 OIC_LOG(ERROR, TAG, "OCRemoveDeviceWithUuid : Filed to remove the device information from local.");
734 if(OC_STACK_CONTINUE == resReq)
737 * If there is no linked device, PM does not send any request.
738 * So we should directly invoke the result callback to inform the result of OCRemoveDevice.
742 resultCallback(ctx, 0, NULL, false);
749 PMDeleteDeviceList(pOwnedDevList);
750 if(pTargetDev && false == discoverdFlag)
752 OICFree(pTargetDev->doxm);
755 OIC_LOG(INFO, TAG, "OUT OCRemoveDeviceWithUuid");
760 * Function to reset the target device.
761 * This function will remove credential and ACL of target device from all devices in subnet.
763 * @param[in] ctx Application context would be returned in result callback
764 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
765 * @param[in] pTargetDev Device information to be revoked.
766 * @param[in] resultCallback callback provided by API user, callback will be called when
767 * credential revocation is finished.
768 * @return OC_STACK_OK in case of success and other value otherwise.
770 OCStackResult OCResetDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
771 const OCProvisionDev_t* pTargetDev,
772 OCProvisionResultCB resultCallback)
774 OIC_LOG(INFO, TAG, "IN OCResetDevice");
775 OCStackResult res = OC_STACK_ERROR;
776 if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
778 OIC_LOG(INFO, TAG, "OCResetDevice : Invalid parameters");
779 return OC_STACK_INVALID_PARAM;
783 OIC_LOG(INFO, TAG, "OCResetDevice : NULL Callback");
784 return OC_STACK_INVALID_CALLBACK;
787 // Send DELETE requests to linked devices
788 res = SRPSyncDevice(ctx, waitTimeForOwnedDeviceDiscovery, pTargetDev, resultCallback);
789 if (OC_STACK_CONTINUE == res)
791 OIC_LOG(DEBUG, TAG, "OCResetDevice : Target device has no linked device except PT.");
794 resultCallback(ctx, 0, NULL, false);
796 SRPResetDevice(pTargetDev, resultCallback);
799 else if(OC_STACK_OK != res)
801 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to invoke SRPSyncDevice");
803 OIC_LOG(INFO, TAG, "OUT OCResetDevice");
808 * Internal Function to update result in link result array.
810 static void UpdateLinkResults(Linkdata_t *link, int device, OCStackResult stackresult)
813 OIC_LOG_V(INFO,TAG,"value of link->currentCountResults is %d",link->currentCountResults);
816 memcpy(link->resArr[(link->currentCountResults)].deviceId.id, link->pDev1->doxm->deviceID.id,UUID_LENGTH);
820 memcpy(link->resArr[(link->currentCountResults)].deviceId.id, link->pDev2->doxm->deviceID.id,UUID_LENGTH);
822 link->resArr[(link->currentCountResults)].res = stackresult;
823 ++(link->currentCountResults);
828 * Callback to handle ACL provisioning for device 2.
830 static void AclProv2CB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
835 OIC_LOG(ERROR,TAG,"Context is Null in ACLProv 2");
839 Linkdata_t *link = (Linkdata_t*)ctx;
840 OCProvisionResultCB resultCallback = link->resultCallback;
845 UpdateLinkResults(link, 2,arr[0].res);
846 OIC_LOG(ERROR,TAG,"Error occured while ACL provisioning device 1");
847 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
850 OICFree(link->resArr);
854 UpdateLinkResults(link, 2, arr[0].res);
855 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
858 OICFree(link->resArr);
864 * Callback to handle ACL provisioning for device 1
866 static void AclProv1CB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
871 OIC_LOG(ERROR,TAG,"Context is Null in ACLProv1");
875 Linkdata_t *link = (Linkdata_t*)ctx;
876 OCProvisionResultCB resultCallback = link->resultCallback;
880 OIC_LOG(ERROR,TAG,"Error occured while ACL provisioning device 1");
881 UpdateLinkResults(link, 1, arr[0].res);
882 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
885 OICFree(link->resArr);
889 UpdateLinkResults(link, 1, arr[0].res);
890 if (NULL != link->pDev2Acl)
892 OCStackResult res = SRPProvisionACL(ctx, link->pDev2, link->pDev2Acl, &AclProv2CB);
893 if (OC_STACK_OK!=res)
895 UpdateLinkResults(link, 2, res);
896 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
904 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
907 OICFree(link->resArr);
915 * Callback to handle credential provisioning.
917 static void ProvisionCredsCB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
921 OIC_LOG(ERROR,TAG,"Error occured while credential provisioning");
924 Linkdata_t *link = (Linkdata_t*)ctx;
925 OCProvisionResultCB resultCallback = link->resultCallback;
926 OIC_LOG_V(INFO, TAG, "has error returned %d",hasError);
927 UpdateLinkResults(link, 1, arr[0].res);
928 UpdateLinkResults(link, 2, arr[1].res);
931 OIC_LOG(ERROR,TAG,"Error occured while credential provisioning");
932 ((OCProvisionResultCB)(resultCallback))(link->ctx, nOfRes,
935 OICFree(link->resArr);
939 if (NULL != link->pDev1Acl)
942 OCStackResult res = SRPProvisionACL(ctx, link->pDev1, link->pDev1Acl, &AclProv1CB);
943 if (OC_STACK_OK!=res)
945 OIC_LOG(ERROR, TAG, "Error while provisioning ACL for device 1");
946 UpdateLinkResults(link, 1, res);
947 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
950 OICFree(link->resArr);
954 else if (NULL!=link->pDev2Acl)
956 OIC_LOG(ERROR, TAG, "ACL for device 1 is NULL");
957 OCStackResult res = SRPProvisionACL(ctx, link->pDev2, link->pDev2Acl, &AclProv2CB);
958 if (OC_STACK_OK!=res)
960 OIC_LOG(ERROR, TAG, "Error while provisioning ACL for device 2");
961 UpdateLinkResults(link, 2, res);
962 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
965 OICFree(link->resArr);
971 OIC_LOG(INFO, TAG, "ACLs of both devices are NULL");
972 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
975 OICFree(link->resArr);
981 * function to provision credentials between two devices and ACLs for the devices who act as a server.
983 * @param[in] ctx Application context would be returned in result callback.
984 * @param[in] type Type of credentials to be provisioned to the device.
985 * @param[in] pDev1 Pointer to OCProvisionDev_t instance,respresenting resource to be provsioned.
986 * @param[in] acl ACL for device 1. If this is not required set NULL.
987 * @param[in] pDev2 Pointer to OCProvisionDev_t instance,respresenting resource to be provsioned.
988 * @param[in] acl ACL for device 2. If this is not required set NULL.
989 * @param[in] resultCallback callback provided by API user, callback will be called when
990 * provisioning request recieves a response from first resource server.
991 * @return OC_STACK_OK in case of success and other value otherwise.
993 OCStackResult OCProvisionPairwiseDevices(void* ctx, OicSecCredType_t type, size_t keySize,
994 const OCProvisionDev_t *pDev1, OicSecAcl_t *pDev1Acl,
995 const OCProvisionDev_t *pDev2, OicSecAcl_t *pDev2Acl,
996 OCProvisionResultCB resultCallback)
999 if (!pDev1 || !pDev2 || !pDev1->doxm || !pDev2->doxm)
1001 OIC_LOG(ERROR, TAG, "OCProvisionPairwiseDevices : Invalid parameters");
1002 return OC_STACK_INVALID_PARAM;
1004 if (!resultCallback)
1006 OIC_LOG(INFO, TAG, "OCProvisionPairwiseDevices : NULL Callback");
1007 return OC_STACK_INVALID_CALLBACK;
1009 if (!(keySize == OWNER_PSK_LENGTH_128 || keySize == OWNER_PSK_LENGTH_256))
1011 OIC_LOG(INFO, TAG, "OCProvisionPairwiseDevices : Invalid key size");
1012 return OC_STACK_INVALID_PARAM;
1014 if (0 == memcmp(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, sizeof(OicUuid_t)))
1016 OIC_LOG(INFO, TAG, "OCProvisionPairwiseDevices : Same device ID");
1017 return OC_STACK_INVALID_PARAM;
1020 OIC_LOG(DEBUG, TAG, "Checking link in DB");
1021 bool linkExists = true;
1022 OCStackResult res = PDMIsLinkExists(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, &linkExists);
1023 if(res != OC_STACK_OK)
1025 OIC_LOG(ERROR, TAG, "Internal Error Occured");
1030 OIC_LOG(ERROR, TAG, "Link already exists");
1031 return OC_STACK_INVALID_PARAM;
1034 int noOfResults = 2; // Initial Value
1035 if (NULL != pDev1Acl)
1039 if (NULL != pDev2Acl)
1043 Linkdata_t *link = (Linkdata_t*) OICMalloc(sizeof(Linkdata_t));
1046 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1047 return OC_STACK_NO_MEMORY;
1049 OIC_LOG_V(INFO,TAG, "Maximum no od results %d",noOfResults);
1051 link->pDev1 = pDev1;
1052 link->pDev1Acl = pDev1Acl;
1053 link->pDev2 = pDev2;
1054 link->pDev2Acl = pDev2Acl;
1056 // 1 call for each device for credential provisioning. implict call by SRPProvisioning credential
1057 // 1 call for ACL provisioning for device 1 and 1 call for ACL provisioning for device 2.
1058 link->numOfResults = noOfResults;
1059 link->resultCallback = resultCallback;
1060 link->currentCountResults = 0;
1061 link->resArr = (OCProvisionResult_t*) OICMalloc(sizeof(OCProvisionResult_t)*noOfResults);
1062 res = SRPProvisionCredentials(link, type, keySize,
1063 pDev1, pDev2, &ProvisionCredsCB);
1064 if (res != OC_STACK_OK)
1066 OICFree(link->resArr);
1073 OCStackResult OCGetDevInfoFromNetwork(unsigned short waittime,
1074 OCProvisionDev_t** pOwnedDevList,
1075 OCProvisionDev_t** pUnownedDevList)
1077 //TODO will be replaced by more efficient logic
1078 if (pOwnedDevList == NULL || *pOwnedDevList != NULL || pUnownedDevList == NULL
1079 || *pUnownedDevList != NULL || 0 == waittime)
1081 return OC_STACK_INVALID_PARAM;
1084 // Code for unowned discovery
1085 OCProvisionDev_t *unownedDevice = NULL;
1086 OCStackResult res = OCDiscoverUnownedDevices(waittime/2, &unownedDevice);
1087 if (OC_STACK_OK != res)
1089 OIC_LOG(ERROR,TAG, "Error in unowned discovery");
1093 // Code for owned discovery
1094 OCProvisionDev_t *ownedDevice = NULL;
1095 res = OCDiscoverOwnedDevices(waittime/2, &ownedDevice);
1096 if (OC_STACK_OK != res)
1098 OIC_LOG(ERROR,TAG, "Error in owned discovery");
1099 PMDeleteDeviceList(unownedDevice);
1103 // Code to get list of all the owned devices.
1104 OCUuidList_t *uuidList = NULL;
1105 size_t numOfDevices = 0;
1106 res = PDMGetOwnedDevices(&uuidList, &numOfDevices);
1107 if (OC_STACK_OK != res)
1109 OIC_LOG(ERROR, TAG, "Error while getting info from DB");
1110 PMDeleteDeviceList(unownedDevice);
1111 PMDeleteDeviceList(ownedDevice);
1115 // Code to compare devices in unowned list and deviceid from DB
1116 // (In case of hard reset of the device)
1117 OCProvisionDev_t* pUnownedList = unownedDevice;
1118 while (pUnownedList && uuidList)
1120 OCUuidList_t *tmp1 = NULL,*tmp2=NULL;
1121 LL_FOREACH_SAFE(uuidList, tmp1, tmp2)
1123 if(0 == memcmp(tmp1->dev.id, pUnownedList->doxm->deviceID.id,
1124 sizeof(pUnownedList->doxm->deviceID.id)))
1126 OIC_LOG_V(INFO, TAG, "OCGetDevInfoFromNetwork : \
1127 Removing device id = %s in PDM and dat.", pUnownedList->doxm->deviceID.id);
1128 if (OC_STACK_OK != PDMDeleteDevice(&pUnownedList->doxm->deviceID))
1130 OIC_LOG(ERROR, TAG, "OCGetDevInfoFromNetwork : \
1131 Failed to remove device in PDM.");
1133 //remove the cred entry from dat file
1134 if (OC_STACK_OK != RemoveDeviceInfoFromLocal(pUnownedList))
1136 OIC_LOG(ERROR, TAG, "OCGetDevInfoFromNetwork : \
1137 Failed to remove cred entry device in dat file.");
1139 LL_DELETE(uuidList, tmp1);
1143 pUnownedList = pUnownedList->next;
1145 // Code to compare devices in owned list and deviceid from DB.
1146 OCProvisionDev_t* pCurDev = ownedDevice;
1147 size_t deleteCnt = 0;
1150 if(true == PMDeleteFromUUIDList(&uuidList, &pCurDev->doxm->deviceID))
1154 pCurDev = pCurDev->next;
1156 // If there is no remaind device in uuidList, we have to assign NULL to prevent free.
1157 if (deleteCnt == numOfDevices)
1161 // Code to add information of the devices which are currently off in owned list.
1162 OCUuidList_t *powerOffDeviceList = uuidList;
1163 while (powerOffDeviceList)
1165 OCProvisionDev_t *ptr = (OCProvisionDev_t *)OICCalloc(1, sizeof (OCProvisionDev_t));
1168 OIC_LOG(ERROR,TAG,"Fail to allocate memory");
1169 PMDeleteDeviceList(unownedDevice);
1170 PMDeleteDeviceList(ownedDevice);
1171 OCDeleteUuidList(uuidList);
1172 return OC_STACK_NO_MEMORY;
1175 ptr->doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
1176 if (NULL == ptr->doxm)
1178 OIC_LOG(ERROR,TAG,"Fail to allocate memory");
1179 PMDeleteDeviceList(unownedDevice);
1180 PMDeleteDeviceList(ownedDevice);
1181 OCDeleteUuidList(uuidList);
1183 return OC_STACK_NO_MEMORY;
1186 memcpy(ptr->doxm->deviceID.id, powerOffDeviceList->dev.id, sizeof(ptr->doxm->deviceID.id));
1188 ptr->devStatus = DEV_STATUS_OFF;
1189 LL_PREPEND(ownedDevice, ptr);
1190 powerOffDeviceList = powerOffDeviceList->next;
1193 OCDeleteUuidList(uuidList);
1194 *pOwnedDevList = ownedDevice;
1195 *pUnownedDevList = unownedDevice;
1199 OCStackResult OCGetLinkedStatus(const OicUuid_t* uuidOfDevice, OCUuidList_t** uuidList,
1200 size_t* numOfDevices)
1202 return PDMGetLinkedDevices(uuidOfDevice, uuidList, numOfDevices);
1205 void OCDeleteUuidList(OCUuidList_t* pList)
1207 PDMDestoryOicUuidLinkList(pList);
1211 * This function deletes ACL data.
1213 * @param pAcl Pointer to OicSecAcl_t structure.
1215 void OCDeleteACLList(OicSecAcl_t* pAcl)
1217 DeleteACLList(pAcl);
1221 * This function deletes PDACL data.
1223 * @param pPdAcl Pointer to OicSecPdAcl_t structure.
1225 void OCDeletePdAclList(OicSecPdAcl_t* pPdAcl)
1227 FreePdAclList(pPdAcl);
1230 #ifdef _ENABLE_MULTIPLE_OWNER_
1232 * API to update 'doxm.mom' to resource server.
1234 * @param[in] targetDeviceInfo Selected target device.
1235 * @param[in] momType Mode of multiple ownership transfer (ref. oic.sec.mom)
1236 * @param[in] resultCallback callback provided by API user, callback will be called when
1237 * POST 'mom' request recieves a response from resource server.
1238 * @return OC_STACK_OK in case of success and other value otherwise.
1240 OCStackResult OCChangeMOTMode(void *ctx, const OCProvisionDev_t *targetDeviceInfo,
1241 const OicSecMomType_t momType, OCProvisionResultCB resultCallback)
1243 return MOTChangeMode(ctx, targetDeviceInfo, momType, resultCallback);
1247 * API to update 'doxm.oxmsel' to resource server.
1249 * @param[in] targetDeviceInfo Selected target device.
1250 * @param[in] oxmSelValue Method of multiple ownership transfer (ref. oic.sec.oxm)
1251 * @param[in] resultCallback callback provided by API user, callback will be called when
1252 * POST 'oxmsel' request recieves a response from resource server.
1253 * @return OC_STACK_OK in case of success and other value otherwise.
1255 OCStackResult OCSelectMOTMethod(void *ctx, const OCProvisionDev_t *targetDeviceInfo,
1256 const OicSecOxm_t oxmSelValue, OCProvisionResultCB resultCallback)
1258 return MOTSelectMOTMethod(ctx, targetDeviceInfo, oxmSelValue, resultCallback);
1260 #endif //_ENABLE_MULTIPLE_OWNER_
1262 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
1264 * function to provision Trust certificate chain to devices.
1266 * @param[in] ctx Application context would be returned in result callback.
1267 * @param[in] type Type of credentials to be provisioned to the device.
1268 * @param[in] credId CredId of trust certificate chain to be provisioned to the device.
1269 * @param[in] selectedDeviceInfo Pointer to OCProvisionDev_t instance,respresenting resource to be provsioned.
1270 * @param[in] resultCallback callback provided by API user, callback will be called when
1271 * provisioning request recieves a response from first resource server.
1272 * @return OC_STACK_OK in case of success and other value otherwise.
1274 OCStackResult OCProvisionTrustCertChain(void *ctx, OicSecCredType_t type, uint16_t credId,
1275 const OCProvisionDev_t *selectedDeviceInfo,
1276 OCProvisionResultCB resultCallback)
1278 return SRPProvisionTrustCertChain(ctx, type, credId,
1279 selectedDeviceInfo, resultCallback);
1283 * function to save Trust certificate chain into Cred of SVR.
1285 * @param[in] trustCertChain Trust certificate chain to be saved in Cred of SVR.
1286 * @param[in] chainSize Size of trust certificate chain to be saved in Cred of SVR
1287 * @param[in] encodingType Encoding type of trust certificate chain to be saved in Cred of SVR
1288 * @param[out] credId CredId of saved trust certificate chain in Cred of SVR.
1289 * @return OC_STACK_OK in case of success and other value otherwise.
1291 OCStackResult OCSaveTrustCertChain(uint8_t *trustCertChain, size_t chainSize,
1292 OicEncodingType_t encodingType, uint16_t *credId)
1294 return SRPSaveTrustCertChain(trustCertChain, chainSize, encodingType, credId);
1298 * function to register notifier for Trustcertchain change.
1300 * @param[in] ctx user context.
1301 * @param[in] TrustCertChainChangeCB notification callback fucntion.
1302 * @return OC_STACK_OK in case of success and other value otherwise.
1304 OCStackResult OCRegisterTrustCertChainNotifier(void *ctx, TrustCertChainChangeCB Callback)
1306 return SRPRegisterTrustCertChainNotifier(ctx, Callback);
1310 * function to de-register notifier for Trustcertchain change.
1312 void OCRemoveTrustCertChainNotifier()
1314 SRPRemoveTrustCertChainNotifier();
1316 #endif // __WITH_DTLS__ || __WITH_TLS__