Modify the remove device related API to use client UUID of OCClientResponse
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / src / ocprovisioningmanager.c
1 /* *****************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * *****************************************************************/
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include "ocprovisioningmanager.h"
24 #include "pmutility.h"
25 #include "ownershiptransfermanager.h"
26 #include "oic_malloc.h"
27 #include "logger.h"
28 #include "secureresourceprovider.h"
29 #include "provisioningdatabasemanager.h"
30 #include "credresource.h"
31 #include "utlist.h"
32 #include "aclresource.h" //Note: SRM internal header
33
34 #define TAG "OCPMAPI"
35
36 typedef struct Linkdata Linkdata_t;
37 struct Linkdata
38 {
39     void *ctx;
40     const OCProvisionDev_t *pDev1;
41     OicSecAcl_t *pDev1Acl;
42     const OCProvisionDev_t *pDev2;
43     OicSecAcl_t *pDev2Acl;
44     OCProvisionResult_t *resArr;
45     int numOfResults;
46     int currentCountResults;
47     OCProvisionResultCB resultCallback;
48
49 };
50
51 /**
52  * The function is responsible for initializaton of the provisioning manager. It will load
53  * provisioning database which have owned device's list and their linked status.
54  * TODO: In addition, if there is a device(s) which has not up-to-date credentials, this function will
55  * automatically try to update the deivce(s).
56  *
57  * @param[in] dbPath file path of the sqlite3 db
58  *
59  * @return OC_STACK_OK in case of success and other value otherwise.
60  */
61 OCStackResult OCInitPM(const char* dbPath)
62 {
63     return PDMInit(dbPath);
64 }
65
66 /**
67  * The function is responsible for discovery of device is current subnet. It will list
68  * all the device in subnet which are not yet owned. Please call OCInit with OC_CLIENT_SERVER as
69  * OCMode.
70  *
71  * @param[in] timeout Timeout in seconds, value till which function will listen to responses from
72  *                    client before returning the list of devices.
73  * @param[out] ppList List of candidate devices to be provisioned
74  * @return OTM_SUCCESS in case of success and other value otherwise.
75  */
76 OCStackResult OCDiscoverUnownedDevices(unsigned short timeout, OCProvisionDev_t **ppList)
77 {
78     if( ppList == NULL || *ppList != NULL)
79     {
80         return OC_STACK_INVALID_PARAM;
81     }
82
83     return PMDeviceDiscovery(timeout, false, ppList);
84 }
85
86 /**
87  * The function is responsible for discovery of owned device is current subnet. It will list
88  * all the device in subnet which are owned by calling provisioning client.
89  *
90  * @param[in] timeout Timeout in seconds, value till which function will listen to responses from
91  *                    client before returning the list of devices.
92  * @param[out] ppList List of device owned by provisioning tool.
93  * @return OTM_SUCCESS in case of success and other value otherwise.
94  */
95 OCStackResult OCDiscoverOwnedDevices(unsigned short timeout, OCProvisionDev_t **ppList)
96 {
97     if( ppList == NULL || *ppList != NULL)
98     {
99         return OC_STACK_INVALID_PARAM;
100     }
101
102     return PMDeviceDiscovery(timeout, true, ppList);
103 }
104
105 /**
106  * API to register for particular OxM.
107  *
108  * @param[in] Ownership transfer method.
109  * @param[in] Implementation of callback functions for owership transfer.
110  * @return  OC_STACK_OK in case of success and other value otherwise.
111  */
112 OCStackResult OCSetOwnerTransferCallbackData(OicSecOxm_t oxm, OTMCallbackData_t* callbackData)
113 {
114     if(NULL == callbackData)
115     {
116         return OC_STACK_INVALID_PARAM;
117     }
118
119     return OTMSetOwnershipTransferCallbackData(oxm, callbackData);
120 }
121
122 OCStackResult OCDoOwnershipTransfer(void* ctx,
123                                       OCProvisionDev_t *targetDevices,
124                                       OCProvisionResultCB resultCallback)
125 {
126     if( NULL == targetDevices )
127     {
128         return OC_STACK_INVALID_PARAM;
129     }
130
131     return OTMDoOwnershipTransfer(ctx, targetDevices, resultCallback);
132 }
133
134 /**
135  * This function deletes memory allocated to linked list created by OCDiscover_XXX_Devices API.
136  *
137  * @param[in] pList Pointer to OCProvisionDev_t which should be deleted.
138  */
139 void OCDeleteDiscoveredDevices(OCProvisionDev_t *pList)
140 {
141     PMDeleteDeviceList(pList);
142 }
143
144 /**
145  * this function sends ACL information to resource.
146  *
147  * @param[in] ctx Application context would be returned in result callback.
148  * @param[in] selectedDeviceInfo Selected target device.
149  * @param[in] acl ACL to provision.
150  * @param[in] resultCallback callback provided by API user, callback will be called when provisioning
151               request recieves a response from resource server.
152  * @return  OC_STACK_OK in case of success and other value otherwise.
153  */
154 OCStackResult OCProvisionACL(void* ctx, const OCProvisionDev_t *selectedDeviceInfo, OicSecAcl_t *acl,
155                              OCProvisionResultCB resultCallback)
156 {
157     return SRPProvisionACL(ctx, selectedDeviceInfo, acl, resultCallback);
158 }
159
160 /**
161  * function to provision credential to devices.
162  *
163  * @param[in] ctx Application context would be returned in result callback.
164  * @param[in] type Type of credentials to be provisioned to the device.
165  * @param[in] pDev1 Pointer to OCProvisionDev_t instance,respresenting resource to be provsioned.
166    @param[in] pDev2 Pointer to OCProvisionDev_t instance,respresenting resource to be provsioned.
167  * @param[in] resultCallback callback provided by API user, callback will be called when
168  *            provisioning request recieves a response from first resource server.
169  * @return  OC_STACK_OK in case of success and other value otherwise.
170  */
171 OCStackResult OCProvisionCredentials(void *ctx, OicSecCredType_t type, size_t keySize,
172                                       const OCProvisionDev_t *pDev1,
173                                       const OCProvisionDev_t *pDev2,
174                                       OCProvisionResultCB resultCallback)
175 {
176     return SRPProvisionCredentials(ctx, type, keySize,
177                                       pDev1, pDev2, resultCallback);
178
179 }
180
181 /*
182 * Function to unlink devices.
183 * This function will remove the credential & relationship between the two devices.
184 *
185 * @param[in] ctx Application context would be returned in result callback
186 * @param[in] pTargetDev1 first device information to be unlinked.
187 * @param[in] pTargetDev2 second device information to be unlinked.
188 * @param[in] resultCallback callback provided by API user, callback will be called when
189 *            device unlink is finished.
190  * @return  OC_STACK_OK in case of success and other value otherwise.
191 */
192 OCStackResult OCUnlinkDevices(void* ctx,
193                               const OCProvisionDev_t* pTargetDev1,
194                               const OCProvisionDev_t* pTargetDev2,
195                               OCProvisionResultCB resultCallback)
196 {
197     OC_LOG(INFO, TAG, "IN OCUnlinkDevices");
198     OCUuidList_t* idList = NULL;
199     size_t numOfDev = 0;
200
201     if (!pTargetDev1 || !pTargetDev2 || !resultCallback)
202     {
203         OC_LOG(ERROR, TAG, "OCUnlinkDevices : NULL parameters");
204         return OC_STACK_INVALID_PARAM;
205     }
206
207     // Get linked devices with the first device.
208     OCStackResult res = PDMGetLinkedDevices(&(pTargetDev1->doxm->deviceID), &idList, &numOfDev);
209     if (OC_STACK_OK != res)
210     {
211         OC_LOG(ERROR, TAG, "OCUnlinkDevices : PDMgetOwnedDevices failed");
212         goto error;
213     }
214     if (1 > numOfDev)
215     {
216         OC_LOG(DEBUG, TAG, "OCUnlinkDevices : Can not find linked devices");
217         res = OC_STACK_INVALID_PARAM; // Input devices are not linked, No request is made
218         goto error;
219     }
220
221     // Check the linked devices contains the second device. If yes send credential DELETE request.
222     OCUuidList_t* curDev = idList;
223     while (NULL != curDev)
224     {
225         if (memcmp(pTargetDev2->doxm->deviceID.id, curDev->dev.id, sizeof(curDev->dev.id)) == 0)
226         {
227             res = SRPUnlinkDevices(ctx, pTargetDev1, pTargetDev2, resultCallback);
228             if (OC_STACK_OK != res)
229             {
230                 OC_LOG(ERROR, TAG, "OCUnlinkDevices : Failed to unlink devices.");
231             }
232             goto error;
233         }
234         curDev = curDev->next;
235     }
236     OC_LOG(DEBUG, TAG, "No matched pair found from provisioning database");
237     res = OC_STACK_INVALID_PARAM; // Input devices are not linked, No request is made
238
239 error:
240     OC_LOG(INFO, TAG, "OUT OCUnlinkDevices");
241
242     PDMDestoryOicUuidLinkList(idList);
243     return res;
244 }
245
246 /*
247 * Function to device revocation
248 * This function will remove credential of target device from all devices in subnet.
249 *
250 * @param[in] ctx Application context would be returned in result callback
251 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
252 * @param[in] pTargetDev Device information to be revoked.
253 * @param[in] resultCallback callback provided by API user, callback will be called when
254 *            credential revocation is finished.
255  * @return  OC_STACK_OK in case of success and other value otherwise.
256 */
257 OCStackResult OCRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
258                             const OCProvisionDev_t* pTargetDev,
259                             OCProvisionResultCB resultCallback)
260 {
261     OC_LOG(INFO, TAG, "IN OCRemoveDevice");
262     OCStackResult res = OC_STACK_ERROR;
263     if (!pTargetDev || !resultCallback || 0 == waitTimeForOwnedDeviceDiscovery)
264     {
265         OC_LOG(INFO, TAG, "OCRemoveDevice : Invalied parameters");
266         return OC_STACK_INVALID_PARAM;
267     }
268
269     // Send DELETE requests to linked devices
270     OCStackResult resReq = OC_STACK_ERROR; // Check that we have to wait callback or not.
271     resReq = SRPRemoveDevice(ctx, waitTimeForOwnedDeviceDiscovery, pTargetDev, resultCallback);
272     if (OC_STACK_OK != resReq)
273     {
274         if (OC_STACK_CONTINUE == resReq)
275         {
276             OC_LOG(DEBUG, TAG, "OCRemoveDevice : Revoked device has no linked device except PT.");
277         }
278         else
279         {
280             OC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to invoke SRPRemoveDevice");
281             res = resReq;
282             goto error;
283         }
284     }
285
286     // Remove credential of revoked device from SVR database
287     const OicSecCred_t *cred = NULL;
288     cred = GetCredResourceData(&pTargetDev->doxm->deviceID);
289     if (cred == NULL)
290     {
291         OC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to get credential of remove device.");
292         goto error;
293     }
294
295     res = RemoveCredential(&cred->subject);
296     if (res != OC_STACK_RESOURCE_DELETED)
297     {
298         OC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to remove credential.");
299         goto error;
300     }
301
302     /**
303      * Change the device status as stale status.
304      * If all request are successed, this device information will be deleted.
305      */
306     res = PDMSetDeviceStale(&pTargetDev->doxm->deviceID);
307     if (res != OC_STACK_OK)
308     {
309         OC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to set device status as stale");
310         goto error;
311     }
312
313     // TODO: We need to add new mechanism to clean up the stale state of the device.
314
315     res = resReq;
316
317     //Close the DTLS session of the removed device.
318     CAEndpoint_t* endpoint = (CAEndpoint_t *)&pTargetDev->endpoint;
319     endpoint->port = pTargetDev->securePort;
320     CAResult_t caResult = CACloseDtlsSession(endpoint);
321     if(CA_STATUS_OK != caResult)
322     {
323         OC_LOG_V(WARNING, TAG, "OCRemoveDevice : Failed to close DTLS session : %d", caResult);
324     }
325
326     /**
327      * If there is no linked device, PM does not send any request.
328      * So we should directly invoke the result callback to inform the result of OCRemoveDevice.
329      */
330     if(OC_STACK_CONTINUE == res)
331     {
332         if(resultCallback)
333         {
334             resultCallback(ctx, 0, NULL, false);
335         }
336         res = OC_STACK_OK;
337     }
338
339 error:
340     OC_LOG(INFO, TAG, "OUT OCRemoveDevice");
341     return res;
342 }
343
344
345 /**
346  * Internal Function to update result in link result array.
347  */
348 static void UpdateLinkResults(Linkdata_t *link, int device, OCStackResult stackresult)
349 {
350
351     OC_LOG_V(INFO,TAG,"value of link->currentCountResults is %d",link->currentCountResults);
352     if (1 == device)
353     {
354         memcpy(link->resArr[(link->currentCountResults)].deviceId.id, link->pDev1->doxm->deviceID.id,UUID_LENGTH);
355     }
356     else
357     {
358         memcpy(link->resArr[(link->currentCountResults)].deviceId.id, link->pDev2->doxm->deviceID.id,UUID_LENGTH);
359     }
360     link->resArr[(link->currentCountResults)].res = stackresult;
361     ++(link->currentCountResults);
362
363 }
364
365 /**
366  * Callback to handle ACL provisioning for device 2.
367  */
368 static void AclProv2CB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
369 {
370
371     if (NULL == ctx)
372     {
373         OC_LOG(ERROR,TAG,"Context is Null in ACLProv 2");
374         return;
375     }
376     (void)nOfRes;
377     Linkdata_t *link = (Linkdata_t*)ctx;
378     OCProvisionResultCB resultCallback = link->resultCallback;
379
380
381     if (hasError)
382     {
383         UpdateLinkResults(link, 2,arr[0].res);
384         OC_LOG(ERROR,TAG,"Error occured while ACL provisioning device 1");
385         ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
386                                                 link->resArr,
387                                                 true);
388         OICFree(link->resArr);
389         OICFree(link) ;
390         return;
391     }
392     UpdateLinkResults(link, 2, arr[0].res);
393    ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
394                                            link->resArr,
395                                            false);
396     OICFree(link->resArr);
397     OICFree(link);
398     return;
399 }
400
401 /**
402  * Callback to handle ACL provisioning for device 1
403  */
404 static void AclProv1CB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
405 {
406
407     if (NULL == ctx)
408     {
409         OC_LOG(ERROR,TAG,"Context is Null in ACLProv1");
410         return;
411     }
412     (void)nOfRes;
413     Linkdata_t *link = (Linkdata_t*)ctx;
414     OCProvisionResultCB resultCallback = link->resultCallback;
415
416     if (hasError)
417     {
418         OC_LOG(ERROR,TAG,"Error occured while ACL provisioning device 1");
419         UpdateLinkResults(link, 1, arr[0].res);
420         ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
421                                                 link->resArr,
422                                                 true);
423         OICFree(link->resArr);
424         OICFree(link);
425         return;
426     }
427     UpdateLinkResults(link, 1, arr[0].res);
428     if (NULL != link->pDev2Acl)
429     {
430         OCStackResult res =  SRPProvisionACL(ctx, link->pDev2, link->pDev2Acl, &AclProv2CB);
431         if (OC_STACK_OK!=res)
432         {
433              UpdateLinkResults(link, 2, res);
434              ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
435                                                      link->resArr,
436                                                      true);
437
438         }
439     }
440     else
441     {
442         ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
443                                                 link->resArr,
444                                                 false);
445         OICFree(link->resArr);
446         OICFree(link);
447     }
448
449     return;
450 }
451
452 /**
453  * Callback to handle credential provisioning.
454  */
455 static void ProvisionCredsCB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
456 {
457     if (NULL == ctx)
458     {
459         OC_LOG(ERROR,TAG,"Error occured while credential provisioning");
460         return;
461     }
462     Linkdata_t *link = (Linkdata_t*)ctx;
463     OCProvisionResultCB resultCallback = link->resultCallback;
464     OC_LOG_V(INFO, TAG, "has error returned %d",hasError);
465     UpdateLinkResults(link, 1, arr[0].res);
466     UpdateLinkResults(link, 2, arr[1].res);
467     if (hasError)
468     {
469         OC_LOG(ERROR,TAG,"Error occured while credential provisioning");
470         ((OCProvisionResultCB)(resultCallback))(link->ctx, nOfRes,
471                                                 link->resArr,
472                                                 true);
473          OICFree(link->resArr);
474          OICFree(link);
475          return;
476     }
477     if (NULL != link->pDev1Acl)
478     {
479
480         OCStackResult res =  SRPProvisionACL(ctx, link->pDev1, link->pDev1Acl, &AclProv1CB);
481         if (OC_STACK_OK!=res)
482         {
483              OC_LOG(ERROR, TAG, "Error while provisioning ACL for device 1");
484              UpdateLinkResults(link, 1, res);
485              ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
486                                                      link->resArr,
487                                                      true);
488               OICFree(link->resArr);
489               OICFree(link);
490         }
491     }
492     else if (NULL!=link->pDev2Acl)
493     {
494         OC_LOG(ERROR, TAG, "ACL for device 1 is NULL");
495         OCStackResult res =  SRPProvisionACL(ctx, link->pDev2, link->pDev2Acl, &AclProv2CB);
496         if (OC_STACK_OK!=res)
497         {
498              OC_LOG(ERROR, TAG, "Error while provisioning ACL for device 2");
499               UpdateLinkResults(link, 2, res);
500              ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
501                                                      link->resArr,
502                                                      true);
503               OICFree(link->resArr);
504               OICFree(link);
505         }
506     }
507     else
508     {
509         OC_LOG(INFO, TAG, "ACLs of both devices are NULL");
510         ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
511                                                 link->resArr,
512                                                 false);
513         OICFree(link->resArr);
514         OICFree(link);
515     }
516     return;
517 }
518 /**
519  * function to provision credentials between two devices and ACLs for the devices who act as a server.
520  *
521  * @param[in] ctx Application context would be returned in result callback.
522  * @param[in] type Type of credentials to be provisioned to the device.
523  * @param[in] pDev1 Pointer to OCProvisionDev_t instance,respresenting resource to be provsioned.
524  * @param[in] acl ACL for device 1. If this is not required set NULL.
525  * @param[in] pDev2 Pointer to OCProvisionDev_t instance,respresenting resource to be provsioned.
526  * @param[in] acl ACL for device 2. If this is not required set NULL.
527  * @param[in] resultCallback callback provided by API user, callback will be called when
528  *            provisioning request recieves a response from first resource server.
529  * @return  OC_STACK_OK in case of success and other value otherwise.
530  */
531 OCStackResult OCProvisionPairwiseDevices(void* ctx, OicSecCredType_t type, size_t keySize,
532                                          const OCProvisionDev_t *pDev1, OicSecAcl_t *pDev1Acl,
533                                          const OCProvisionDev_t *pDev2, OicSecAcl_t *pDev2Acl,
534                                          OCProvisionResultCB resultCallback)
535 {
536
537     if (!pDev1 || !pDev2 || !resultCallback)
538     {
539         OC_LOG(ERROR, TAG, "OCProvisionPairwiseDevices : Invalid parameters");
540         return OC_STACK_INVALID_PARAM;
541     }
542     if (!(keySize == OWNER_PSK_LENGTH_128 || keySize == OWNER_PSK_LENGTH_256))
543     {
544         OC_LOG(INFO, TAG, "OCProvisionPairwiseDevices : Invalid key size");
545         return OC_STACK_INVALID_PARAM;
546     }
547
548     OC_LOG(DEBUG, TAG, "Checking link in DB");
549     bool linkExists = true;
550     OCStackResult res = PDMIsLinkExists(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, &linkExists);
551     if(res != OC_STACK_OK)
552     {
553         OC_LOG(ERROR, TAG, "Internal Error Occured");
554         return res;
555     }
556     if (linkExists)
557     {
558         OC_LOG(ERROR, TAG, "Link already exists");
559         return OC_STACK_INVALID_PARAM;
560     }
561
562     int noOfResults = 2; // Initial Value
563     if (NULL != pDev1Acl)
564     {
565         ++noOfResults;
566     }
567     if (NULL != pDev2Acl)
568     {
569        ++noOfResults;
570     }
571     Linkdata_t *link = (Linkdata_t*) OICMalloc(sizeof(Linkdata_t));
572     if (!link)
573     {
574         OC_LOG(ERROR, TAG, "Failed to memory allocation");
575         return OC_STACK_NO_MEMORY;
576     }
577     OC_LOG_V(INFO,TAG, "Maximum no od results %d",noOfResults);
578
579     link->pDev1 = pDev1;
580     link->pDev1Acl = pDev1Acl;
581     link->pDev2 = pDev2;
582     link->pDev2Acl = pDev2Acl;
583     link->ctx = ctx;
584     // 1 call for each device for credential provisioning. implict call by SRPProvisioning credential
585     // 1 call for ACL provisioning for device 1 and 1 call for ACL provisioning for device 2.
586     link->numOfResults = noOfResults;
587     link->resultCallback = resultCallback;
588     link->currentCountResults = 0;
589     link->resArr = (OCProvisionResult_t*) OICMalloc(sizeof(OCProvisionResult_t)*noOfResults);
590     res = SRPProvisionCredentials(link, type, keySize,
591                                      pDev1, pDev2, &ProvisionCredsCB);
592     if (res != OC_STACK_OK)
593     {
594         OICFree(link->resArr);
595         OICFree(link);
596     }
597     return res;
598
599 }
600
601 OCStackResult OCGetDevInfoFromNetwork(unsigned short waittime,
602                                        OCProvisionDev_t** pOwnedDevList,
603                                        OCProvisionDev_t** pUnownedDevList)
604 {
605     //TODO will be replaced by more efficient logic
606     if (pOwnedDevList == NULL || *pOwnedDevList != NULL || pUnownedDevList == NULL
607          || *pUnownedDevList != NULL)
608     {
609         return OC_STACK_INVALID_PARAM;
610     }
611
612     // Code for unowned discovery
613     OCProvisionDev_t *unownedDevice = NULL;
614     OCStackResult res =  OCDiscoverUnownedDevices(waittime/2, &unownedDevice);
615     if (OC_STACK_OK != res)
616     {
617         OC_LOG(ERROR,TAG, "Error in unowned discovery");
618         return res;
619     }
620
621     // Code for owned discovery
622     OCProvisionDev_t *ownedDevice = NULL;
623     res =  OCDiscoverOwnedDevices(waittime/2, &ownedDevice);
624     if (OC_STACK_OK != res)
625     {
626         OC_LOG(ERROR,TAG, "Error in owned discovery");
627         PMDeleteDeviceList(unownedDevice);
628         return res;
629     }
630
631     // Code to get list of all the owned devices.
632     OCUuidList_t *uuidList = NULL;
633     size_t numOfDevices = 0;
634     res =  PDMGetOwnedDevices(&uuidList, &numOfDevices);
635     if (OC_STACK_OK != res)
636     {
637         OC_LOG(ERROR, TAG, "Error while getting info from DB");
638         PMDeleteDeviceList(unownedDevice);
639         PMDeleteDeviceList(ownedDevice);
640         return res;
641     }
642
643     // Code to compare devices in owned list and deviceid from DB.
644     OCProvisionDev_t* pCurDev = ownedDevice;
645     size_t deleteCnt = 0;
646     while (pCurDev)
647     {
648         if(true == PMDeleteFromUUIDList(uuidList, &pCurDev->doxm->deviceID))
649         {
650             deleteCnt++;
651         }
652         pCurDev = pCurDev->next;
653     }
654     // If there is no remaind device in uuidList, we have to assign NULL to prevent free.
655     if (deleteCnt == numOfDevices)
656     {
657         uuidList = NULL;
658     }
659     // Code to add information of the devices which are currently off in owned list.
660     OCUuidList_t *powerOffDeviceList = uuidList;
661     while (powerOffDeviceList)
662     {
663         OCProvisionDev_t *ptr = (OCProvisionDev_t *)OICCalloc(1, sizeof (OCProvisionDev_t));
664         if (NULL == ptr)
665         {
666             OC_LOG(ERROR,TAG,"Fail to allocate memory");
667             PMDeleteDeviceList(unownedDevice);
668             PMDeleteDeviceList(ownedDevice);
669             OCDeleteUuidList(uuidList);
670             return OC_STACK_NO_MEMORY;
671         }
672
673         ptr->doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
674         if (NULL == ptr->doxm)
675         {
676             OC_LOG(ERROR,TAG,"Fail to allocate memory");
677             PMDeleteDeviceList(unownedDevice);
678             PMDeleteDeviceList(ownedDevice);
679             OCDeleteUuidList(uuidList);
680             OICFree(ptr);
681             return OC_STACK_NO_MEMORY;
682         }
683
684         memcpy(ptr->doxm->deviceID.id, powerOffDeviceList->dev.id, sizeof(ptr->doxm->deviceID.id));
685
686         ptr->devStatus = DEV_STATUS_OFF;
687         LL_PREPEND(ownedDevice, ptr);
688         powerOffDeviceList = powerOffDeviceList->next;
689
690     }
691     OCDeleteUuidList(uuidList);
692     *pOwnedDevList = ownedDevice;
693     *pUnownedDevList = unownedDevice;
694     return OC_STACK_OK;
695 }
696
697 OCStackResult OCGetLinkedStatus(const OicUuid_t* uuidOfDevice, OCUuidList_t** uuidList,
698                                  size_t* numOfDevices)
699 {
700     return PDMGetLinkedDevices(uuidOfDevice, uuidList, numOfDevices);
701 }
702
703 void OCDeleteUuidList(OCUuidList_t* pList)
704 {
705     PDMDestoryOicUuidLinkList(pList);
706 }
707
708 /**
709  * This function deletes ACL data.
710  *
711  * @param pAcl Pointer to OicSecAcl_t structure.
712  */
713 void OCDeleteACLList(OicSecAcl_t* pAcl)
714 {
715     DeleteACLList(pAcl);
716 }
717
718
719 #ifdef __WITH_X509__
720 /**
721  * this function sends CRL information to resource.
722  *
723  * @param[in] ctx Application context would be returned in result callback.
724  * @param[in] selectedDeviceInfo Selected target device.
725  * @param[in] crl CRL to provision.
726  * @param[in] resultCallback callback provided by API user, callback will be called when provisioning
727               request recieves a response from resource server.
728  * @return  OC_STACK_OK in case of success and other value otherwise.
729  */
730 OCStackResult OCProvisionCRL(void* ctx, const OCProvisionDev_t *selectedDeviceInfo, OicSecCrl_t *crl,
731                              OCProvisionResultCB resultCallback)
732 {
733     return SRPProvisionCRL(ctx, selectedDeviceInfo, crl, resultCallback);
734 }
735 #endif // __WITH_X509__
736