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