Add OIC_ prefix for security module's log.
[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 "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"
31 #include "logger.h"
32 #include "secureresourceprovider.h"
33 #include "provisioningdatabasemanager.h"
34 #include "credresource.h"
35 #include "utlist.h"
36 #include "aclresource.h" //Note: SRM internal header
37 #include "pconfresource.h"
38
39 #define TAG "OIC_OCPMAPI"
40
41 typedef struct Linkdata Linkdata_t;
42 struct Linkdata
43 {
44     void *ctx;
45     const OCProvisionDev_t *pDev1;
46     OicSecAcl_t *pDev1Acl;
47     const OCProvisionDev_t *pDev2;
48     OicSecAcl_t *pDev2Acl;
49     OCProvisionResult_t *resArr;
50     int numOfResults;
51     int currentCountResults;
52     OCProvisionResultCB resultCallback;
53
54 };
55
56 #ifdef _ENABLE_MULTIPLE_OWNER_
57 typedef struct ProvPreconfPINCtx ProvPreconfPINCtx_t;
58 struct ProvPreconfPINCtx
59 {
60     void *ctx;
61     const OCProvisionDev_t *devInfo;
62     const char* pin;
63     size_t pinLen;
64     OCProvisionResultCB resultCallback;
65 };
66 #endif //_ENABLE_MULTIPLE_OWNER_
67
68 /**
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).
73  *
74  * @param[in] dbPath file path of the sqlite3 db
75  *
76  * @return OC_STACK_OK in case of success and other value otherwise.
77  */
78 OCStackResult OCInitPM(const char* dbPath)
79 {
80     return PDMInit(dbPath);
81 }
82
83 /**
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.
86  *
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.
92  */
93 OCStackResult OCDiscoverSingleDevice(unsigned short timeout, const OicUuid_t* deviceID,
94                              OCProvisionDev_t **ppFoundDevice)
95 {
96     if( NULL == ppFoundDevice || NULL != *ppFoundDevice || 0 == timeout || NULL == deviceID)
97     {
98         return OC_STACK_INVALID_PARAM;
99     }
100
101     return PMSingleDeviceDiscovery(timeout, deviceID, ppFoundDevice);
102 }
103
104 /**
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
107  * OCMode.
108  *
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.
113  */
114 OCStackResult OCDiscoverUnownedDevices(unsigned short timeout, OCProvisionDev_t **ppList)
115 {
116     if( ppList == NULL || *ppList != NULL || 0 == timeout)
117     {
118         return OC_STACK_INVALID_PARAM;
119     }
120
121     return PMDeviceDiscovery(timeout, false, ppList);
122 }
123
124 /**
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.
127  *
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.
132  */
133 OCStackResult OCDiscoverOwnedDevices(unsigned short timeout, OCProvisionDev_t **ppList)
134 {
135     if( ppList == NULL || *ppList != NULL || 0 == timeout)
136     {
137         return OC_STACK_INVALID_PARAM;
138     }
139
140     return PMDeviceDiscovery(timeout, true, ppList);
141 }
142
143 #ifdef _ENABLE_MULTIPLE_OWNER_
144 /**
145  * The function is responsible for discovery of MOT enabled device is current subnet.
146  *
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.
151  */
152 OCStackResult OCDiscoverMultipleOwnerEnabledDevices(unsigned short timeout, OCProvisionDev_t **ppList)
153 {
154     if( ppList == NULL || *ppList != NULL || 0 == timeout)
155     {
156         return OC_STACK_INVALID_PARAM;
157     }
158
159     return PMMultipleOwnerDeviceDiscovery(timeout, false, ppList);
160 }
161
162 /**
163  * The function is responsible for discovery of Multiple Owned device is current subnet.
164  *
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.
169  */
170 OCStackResult OCDiscoverMultipleOwnedDevices(unsigned short timeout, OCProvisionDev_t **ppList)
171 {
172     if( ppList == NULL || *ppList != NULL || 0 == timeout)
173     {
174         return OC_STACK_INVALID_PARAM;
175     }
176
177     return PMMultipleOwnerDeviceDiscovery(timeout, true, ppList);
178 }
179
180
181 /**
182  * API to add preconfigured PIN to local SVR DB.
183  *
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
187  *
188  * @return OC_STACK_OK in case of success and other value otherwise.
189  */
190 OCStackResult OCAddPreconfigPin(const OCProvisionDev_t *targetDeviceInfo,
191                                 const char *preconfigPin,
192                                 size_t preconfigPinLen)
193 {
194     return MOTAddPreconfigPIN(targetDeviceInfo, preconfigPin, preconfigPinLen);
195 }
196
197
198 OCStackResult OCDoMultipleOwnershipTransfer(void* ctx,
199                                       OCProvisionDev_t *targetDevices,
200                                       OCProvisionResultCB resultCallback)
201 {
202     if( NULL == targetDevices )
203     {
204         return OC_STACK_INVALID_PARAM;
205     }
206     if (NULL == resultCallback)
207     {
208         OIC_LOG(INFO, TAG, "OCDoOwnershipTransfer : NULL Callback");
209         return OC_STACK_INVALID_CALLBACK;
210     }
211     return MOTDoOwnershipTransfer(ctx, targetDevices, resultCallback);
212 }
213
214 #endif //_ENABLE_MULTIPLE_OWNER_
215
216 /**
217  * API to register for particular OxM.
218  *
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.
222  */
223 OCStackResult OCSetOwnerTransferCallbackData(OicSecOxm_t oxm, OTMCallbackData_t* callbackData)
224 {
225     if(NULL == callbackData)
226     {
227         return OC_STACK_INVALID_CALLBACK ;
228     }
229
230     return OTMSetOwnershipTransferCallbackData(oxm, callbackData);
231 }
232
233 OCStackResult OCDoOwnershipTransfer(void* ctx,
234                                       OCProvisionDev_t *targetDevices,
235                                       OCProvisionResultCB resultCallback)
236 {
237     if( NULL == targetDevices )
238     {
239         return OC_STACK_INVALID_PARAM;
240     }
241     if (!resultCallback)
242     {
243         OIC_LOG(INFO, TAG, "OCDoOwnershipTransfer : NULL Callback");
244         return OC_STACK_INVALID_CALLBACK;
245     }
246     return OTMDoOwnershipTransfer(ctx, targetDevices, resultCallback);
247 }
248
249 /**
250  * This function deletes memory allocated to linked list created by OCDiscover_XXX_Devices API.
251  *
252  * @param[in] pList Pointer to OCProvisionDev_t which should be deleted.
253  */
254 void OCDeleteDiscoveredDevices(OCProvisionDev_t *pList)
255 {
256     PMDeleteDeviceList(pList);
257 }
258
259 /**
260  * this function sends ACL information to resource.
261  *
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.
268  */
269 OCStackResult OCProvisionACL(void* ctx, const OCProvisionDev_t *selectedDeviceInfo, OicSecAcl_t *acl,
270                              OCProvisionResultCB resultCallback)
271 {
272     return SRPProvisionACL(ctx, selectedDeviceInfo, acl, resultCallback);
273 }
274
275 /**
276  * function to save ACL which has several ACE into Acl of SVR.
277  *
278  * @param acl ACL to be saved in Acl of SVR.
279  * @return  OC_STACK_OK in case of success and other value otherwise.
280  */
281 OCStackResult OCSaveACL(const OicSecAcl_t* acl)
282 {
283     return SRPSaveACL(acl);
284 }
285
286 /**
287  * this function requests CRED information to resource.
288  *
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.
294  */
295 OCStackResult OCGetCredResource(void* ctx, const OCProvisionDev_t *selectedDeviceInfo,
296                              OCProvisionResultCB resultCallback)
297 {
298     return SRPGetCredResource(ctx, selectedDeviceInfo, resultCallback);
299 }
300
301 /**
302  * this function requests ACL information to resource.
303  *
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.
309  */
310 OCStackResult OCGetACLResource(void* ctx, const OCProvisionDev_t *selectedDeviceInfo,
311                              OCProvisionResultCB resultCallback)
312 {
313     return SRPGetACLResource(ctx, selectedDeviceInfo, resultCallback);
314 }
315
316
317 OCStackResult OCReadTrustCertChain(uint16_t credId, uint8_t **trustCertChain,
318                                      size_t *chainSize)
319 {
320     return SRPReadTrustCertChain(credId, trustCertChain, chainSize);
321 }
322 /**
323  * function to provision credential to devices.
324  *
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.
332  */
333 OCStackResult OCProvisionCredentials(void *ctx, OicSecCredType_t type, size_t keySize,
334                                       const OCProvisionDev_t *pDev1,
335                                       const OCProvisionDev_t *pDev2,
336                                       OCProvisionResultCB resultCallback)
337 {
338     return SRPProvisionCredentials(ctx, type, keySize,
339                                       pDev1, pDev2, resultCallback);
340
341 }
342
343 /**
344  * this function sends Direct-Pairing Configuration to a device.
345  *
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.
352  */
353 OCStackResult OCProvisionDirectPairing(void* ctx, const OCProvisionDev_t *selectedDeviceInfo, OicSecPconf_t *pconf,
354                              OCProvisionResultCB resultCallback)
355 {
356     return SRPProvisionDirectPairing(ctx, selectedDeviceInfo, pconf, resultCallback);
357 }
358
359 #ifdef _ENABLE_MULTIPLE_OWNER_
360 static void AddPreconfPinOxMCB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
361 {
362     ProvPreconfPINCtx_t* provCtx = (ProvPreconfPINCtx_t*)ctx;
363     if(provCtx)
364     {
365         OCStackResult res = MOTProvisionPreconfigPIN(provCtx->ctx, provCtx->devInfo, provCtx->pin, provCtx->pinLen, provCtx->resultCallback);
366         if(OC_STACK_OK != res)
367         {
368             arr->res = res;
369             provCtx->resultCallback(provCtx->ctx, nOfRes, arr, true);
370         }
371     }
372 }
373
374 OCStackResult OCProvisionPreconfigPin(void *ctx,
375                                       OCProvisionDev_t *targetDeviceInfo,
376                                       const char *preconfigPin,
377                                       size_t preconfigPinLen,
378                                       OCProvisionResultCB resultCallback)
379 {
380     if( NULL == targetDeviceInfo )
381     {
382         return OC_STACK_INVALID_PARAM;
383     }
384     if (NULL == resultCallback)
385     {
386         OIC_LOG(INFO, TAG, "OCProvisionPreconfigPinCredential : NULL Callback");
387         return OC_STACK_INVALID_CALLBACK;
388     }
389
390     ProvPreconfPINCtx_t* provCtx = (ProvPreconfPINCtx_t*)OICCalloc(1, sizeof(ProvPreconfPINCtx_t));
391     if(NULL == provCtx)
392     {
393         return OC_STACK_NO_MEMORY;
394     }
395     provCtx->ctx = ctx;
396     provCtx->devInfo = targetDeviceInfo;
397     provCtx->pin = preconfigPin;
398     provCtx->pinLen = preconfigPinLen;
399     provCtx->resultCallback = resultCallback;
400     /*
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.
406      */
407     return MOTAddMOTMethod((void*)provCtx, targetDeviceInfo, OIC_PRECONFIG_PIN, AddPreconfPinOxMCB);
408 }
409 #endif //_ENABLE_MULTIPLE_OWNER_
410
411 /*
412 * Function to unlink devices.
413 * This function will remove the credential & relationship between the two devices.
414 *
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.
421 */
422 OCStackResult OCUnlinkDevices(void* ctx,
423                               const OCProvisionDev_t* pTargetDev1,
424                               const OCProvisionDev_t* pTargetDev2,
425                               OCProvisionResultCB resultCallback)
426 {
427     OIC_LOG(INFO, TAG, "IN OCUnlinkDevices");
428     OCUuidList_t* idList = NULL;
429     size_t numOfDev = 0;
430
431     if (!pTargetDev1 || !pTargetDev2 || !pTargetDev1->doxm || !pTargetDev2->doxm)
432     {
433         OIC_LOG(ERROR, TAG, "OCUnlinkDevices : NULL parameters");
434         return OC_STACK_INVALID_PARAM;
435     }
436     if (!resultCallback)
437     {
438         OIC_LOG(INFO, TAG, "OCUnlinkDevices : NULL Callback");
439         return OC_STACK_INVALID_CALLBACK;
440     }
441     if (0 == memcmp(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID, sizeof(OicUuid_t)))
442     {
443         OIC_LOG(INFO, TAG, "OCUnlinkDevices : Same device ID");
444         return OC_STACK_INVALID_PARAM;
445     }
446
447     // Get linked devices with the first device.
448     OCStackResult res = PDMGetLinkedDevices(&(pTargetDev1->doxm->deviceID), &idList, &numOfDev);
449     if (OC_STACK_OK != res)
450     {
451         OIC_LOG(ERROR, TAG, "OCUnlinkDevices : PDMgetOwnedDevices failed");
452         goto error;
453     }
454     if (1 > numOfDev)
455     {
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
458         goto error;
459     }
460
461     // Check the linked devices contains the second device. If yes send credential DELETE request.
462     OCUuidList_t* curDev = idList;
463     while (NULL != curDev)
464     {
465         if (memcmp(pTargetDev2->doxm->deviceID.id, curDev->dev.id, sizeof(curDev->dev.id)) == 0)
466         {
467             res = SRPUnlinkDevices(ctx, pTargetDev1, pTargetDev2, resultCallback);
468             if (OC_STACK_OK != res)
469             {
470                 OIC_LOG(ERROR, TAG, "OCUnlinkDevices : Failed to unlink devices.");
471             }
472             goto error;
473         }
474         curDev = curDev->next;
475     }
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
478
479 error:
480     OIC_LOG(INFO, TAG, "OUT OCUnlinkDevices");
481
482     PDMDestoryOicUuidLinkList(idList);
483     return res;
484 }
485
486 static OCStackResult RemoveDeviceInfoFromLocal(const OCProvisionDev_t* pTargetDev)
487 {
488     // Remove credential of revoked device from SVR database
489     OCStackResult res = OC_STACK_ERROR;
490     const OicSecCred_t *cred = NULL;
491
492     OIC_LOG(DEBUG, TAG, "IN RemoveDeviceInfoFromLocal");
493     cred = GetCredResourceData(&pTargetDev->doxm->deviceID);
494     if (NULL != cred)
495     {
496         res = RemoveCredential(&cred->subject);
497         if (res != OC_STACK_RESOURCE_DELETED)
498         {
499             OIC_LOG(ERROR, TAG, "RemoveDeviceInfoFromLocal : Failed to remove credential.");
500             goto error;
501         }
502     }
503     /**
504      * Change the device status as stale status.
505      * If all request are successed, this device information will be deleted.
506      */
507     res = PDMSetDeviceStale(&pTargetDev->doxm->deviceID);
508     if (res != OC_STACK_OK)
509     {
510         OIC_LOG(WARNING, TAG, "OCRemoveDevice : Failed to set device status as stale");
511     }
512
513     // TODO: We need to add new mechanism to clean up the stale state of the device.
514
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)
520     {
521         OIC_LOG_V(WARNING, TAG, "OCRemoveDevice : Failed to close DTLS session : %d", caResult);
522     }
523
524     OIC_LOG(DEBUG, TAG, "OUT RemoveDeviceInfoFromLocal");
525 error:
526     return res;
527 }
528
529 /*
530 * Function to device revocation
531 * This function will remove credential of target device from all devices in subnet.
532 *
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.
539 */
540 OCStackResult OCRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
541                             const OCProvisionDev_t* pTargetDev,
542                             OCProvisionResultCB resultCallback)
543 {
544     OIC_LOG(INFO, TAG, "IN OCRemoveDevice");
545     OCStackResult res = OC_STACK_ERROR;
546     if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
547     {
548         OIC_LOG(INFO, TAG, "OCRemoveDevice : Invalied parameters");
549         return OC_STACK_INVALID_PARAM;
550     }
551     if (!resultCallback)
552     {
553         OIC_LOG(INFO, TAG, "OCRemoveDevice : NULL Callback");
554         return OC_STACK_INVALID_CALLBACK;
555     }
556
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)
561     {
562         if (OC_STACK_CONTINUE == resReq)
563         {
564             OIC_LOG(DEBUG, TAG, "OCRemoveDevice : Revoked device has no linked device except PT.");
565         }
566         else
567         {
568             OIC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to invoke SRPRemoveDevice");
569             res = resReq;
570             goto error;
571         }
572     }
573
574     res = RemoveDeviceInfoFromLocal(pTargetDev);
575     if(OC_STACK_OK != res)
576     {
577         OIC_LOG(ERROR, TAG, "Filed to remove the device information from local.");
578         goto error;
579     }
580
581     if(OC_STACK_CONTINUE == resReq)
582     {
583         /**
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.
586           */
587         if(resultCallback)
588         {
589             resultCallback(ctx, 0, NULL, false);
590         }
591         res = OC_STACK_OK;
592     }
593
594 error:
595     OIC_LOG(INFO, TAG, "OUT OCRemoveDevice");
596     return res;
597 }
598
599 /*
600 * Function to device revocation
601 * This function will remove credential of target device from all devices in subnet.
602 *
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.
609 */
610 OCStackResult OCRemoveDeviceWithUuid(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
611                             const OicUuid_t* pTargetUuid,
612                             OCProvisionResultCB resultCallback)
613 {
614     OIC_LOG(INFO, TAG, "IN OCRemoveDeviceWithUuid");
615
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;
621
622     if (!pTargetUuid || 0 == waitTimeForOwnedDeviceDiscovery)
623     {
624         OIC_LOG(INFO, TAG, "OCRemoveDeviceWithUuid : Invalied parameters");
625         return OC_STACK_INVALID_PARAM;
626     }
627     if (!resultCallback)
628     {
629         OIC_LOG(INFO, TAG, "OCRemoveDeviceWithUuid : NULL Callback");
630         return OC_STACK_INVALID_CALLBACK;
631     }
632
633     char* strUuid = NULL;
634     if(OC_STACK_OK != ConvertUuidToStr(pTargetUuid, &strUuid))
635     {
636         OIC_LOG(WARNING, TAG, "Failed to covert UUID to String.");
637         goto error;
638     }
639
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)
644     {
645         OIC_LOG(ERROR, TAG, "Failed to allocate memory.");
646         res = OC_STACK_NO_MEMORY;
647         goto error;
648     }
649     pTargetDev->doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
650     if(NULL == pTargetDev->doxm)
651     {
652         OIC_LOG(ERROR, TAG, "Failed to allocate memory.");
653         res = OC_STACK_NO_MEMORY;
654         goto error;
655     }
656     memcpy(pTargetDev->doxm->deviceID.id, pTargetUuid->id, sizeof(pTargetUuid->id));
657
658     OCUuidList_t* linkedDevices = NULL;
659     size_t numOfLinkedDevices = 0;
660     res = PDMGetLinkedDevices(pTargetUuid, &linkedDevices, &numOfLinkedDevices);
661     if(OC_STACK_OK != res)
662     {
663         OIC_LOG(ERROR, TAG, "Error in PDMGetLinkedDevices");
664         goto error;
665     }
666     PDMDestoryOicUuidLinkList(linkedDevices);
667
668     //If there is no linked devices, device revocation step can be skipped.
669     if(0 != numOfLinkedDevices)
670     {
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)
675         {
676             OIC_LOG(ERROR, TAG, "OCRemoveDeviceWithUuid : Failed to PMDeviceDiscovery");
677             goto error;
678         }
679
680         OCProvisionDev_t* tempDev = NULL;
681         LL_FOREACH(pOwnedDevList, tempDev)
682         {
683             if(memcmp(&tempDev->doxm->deviceID.id, pTargetUuid->id, sizeof(pTargetUuid->id)) == 0)
684             {
685                 break;
686             }
687         }
688
689         if(NULL == tempDev)
690         {
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);
693         }
694         else
695         {
696             OICFree(pTargetDev->doxm);
697             OICFree(pTargetDev);
698             pTargetDev = tempDev;
699             discoverdFlag = true;
700             OIC_LOG_V(INFO, TAG, "[%s] is dectected on the network.", strUuid);
701         }
702
703         OIC_LOG_V(INFO, TAG, "Trying [%s] revocation.", strUuid);
704
705         // Send DELETE requests to linked devices
706         resReq = SRPRemoveDeviceWithoutDiscovery(ctx, pOwnedDevList, pTargetDev, resultCallback);
707         if (OC_STACK_OK != resReq)
708         {
709             if (OC_STACK_CONTINUE == resReq)
710             {
711                 OIC_LOG(INFO, TAG, "OCRemoveDeviceWithUuid : Revoked device has no linked device except PT.");
712             }
713             else
714             {
715                 OIC_LOG(ERROR, TAG, "OCRemoveDeviceWithUuid : Failed to invoke SRPRemoveDevice");
716                 res = resReq;
717                 goto error;
718             }
719         }
720     }
721     else
722     {
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.");
725     }
726
727     res = RemoveDeviceInfoFromLocal(pTargetDev);
728     if(OC_STACK_OK != res)
729     {
730         OIC_LOG(ERROR, TAG, "OCRemoveDeviceWithUuid : Filed to remove the device information from local.");
731         goto error;
732     }
733
734     if(OC_STACK_CONTINUE == resReq)
735     {
736         /**
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.
739           */
740         if(resultCallback)
741         {
742             resultCallback(ctx, 0, NULL, false);
743         }
744         res = OC_STACK_OK;
745     }
746
747 error:
748     OICFree(strUuid);
749     PMDeleteDeviceList(pOwnedDevList);
750     if(pTargetDev && false == discoverdFlag)
751     {
752         OICFree(pTargetDev->doxm);
753         OICFree(pTargetDev);
754     }
755     OIC_LOG(INFO, TAG, "OUT OCRemoveDeviceWithUuid");
756     return res;
757 }
758
759 /*
760  * Function to reset the target device.
761  * This function will remove credential and ACL of target device from all devices in subnet.
762  *
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.
769  */
770 OCStackResult OCResetDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
771                             const OCProvisionDev_t* pTargetDev,
772                             OCProvisionResultCB resultCallback)
773 {
774     OIC_LOG(INFO, TAG, "IN OCResetDevice");
775     OCStackResult res = OC_STACK_ERROR;
776     if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
777     {
778         OIC_LOG(INFO, TAG, "OCResetDevice : Invalid parameters");
779         return OC_STACK_INVALID_PARAM;
780     }
781     if (!resultCallback)
782     {
783         OIC_LOG(INFO, TAG, "OCResetDevice : NULL Callback");
784         return OC_STACK_INVALID_CALLBACK;
785     }
786
787     // Send DELETE requests to linked devices
788     res = SRPSyncDevice(ctx, waitTimeForOwnedDeviceDiscovery, pTargetDev, resultCallback);
789     if (OC_STACK_CONTINUE == res)
790     {
791         OIC_LOG(DEBUG, TAG, "OCResetDevice : Target device has no linked device except PT.");
792         if(resultCallback)
793         {
794             resultCallback(ctx, 0, NULL, false);
795         }
796         SRPResetDevice(pTargetDev, resultCallback);
797         res = OC_STACK_OK;
798     }
799     else if(OC_STACK_OK != res)
800     {
801         OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to invoke SRPSyncDevice");
802     }
803     OIC_LOG(INFO, TAG, "OUT OCResetDevice");
804     return res;
805 }
806
807 /**
808  * Internal Function to update result in link result array.
809  */
810 static void UpdateLinkResults(Linkdata_t *link, int device, OCStackResult stackresult)
811 {
812
813     OIC_LOG_V(INFO,TAG,"value of link->currentCountResults is %d",link->currentCountResults);
814     if (1 == device)
815     {
816         memcpy(link->resArr[(link->currentCountResults)].deviceId.id, link->pDev1->doxm->deviceID.id,UUID_LENGTH);
817     }
818     else
819     {
820         memcpy(link->resArr[(link->currentCountResults)].deviceId.id, link->pDev2->doxm->deviceID.id,UUID_LENGTH);
821     }
822     link->resArr[(link->currentCountResults)].res = stackresult;
823     ++(link->currentCountResults);
824
825 }
826
827 /**
828  * Callback to handle ACL provisioning for device 2.
829  */
830 static void AclProv2CB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
831 {
832
833     if (NULL == ctx)
834     {
835         OIC_LOG(ERROR,TAG,"Context is Null in ACLProv 2");
836         return;
837     }
838     (void)nOfRes;
839     Linkdata_t *link = (Linkdata_t*)ctx;
840     OCProvisionResultCB resultCallback = link->resultCallback;
841
842
843     if (hasError)
844     {
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,
848                                                 link->resArr,
849                                                 true);
850         OICFree(link->resArr);
851         OICFree(link) ;
852         return;
853     }
854     UpdateLinkResults(link, 2, arr[0].res);
855    ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
856                                            link->resArr,
857                                            false);
858     OICFree(link->resArr);
859     OICFree(link);
860     return;
861 }
862
863 /**
864  * Callback to handle ACL provisioning for device 1
865  */
866 static void AclProv1CB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
867 {
868
869     if (NULL == ctx)
870     {
871         OIC_LOG(ERROR,TAG,"Context is Null in ACLProv1");
872         return;
873     }
874     (void)nOfRes;
875     Linkdata_t *link = (Linkdata_t*)ctx;
876     OCProvisionResultCB resultCallback = link->resultCallback;
877
878     if (hasError)
879     {
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,
883                                                 link->resArr,
884                                                 true);
885         OICFree(link->resArr);
886         OICFree(link);
887         return;
888     }
889     UpdateLinkResults(link, 1, arr[0].res);
890     if (NULL != link->pDev2Acl)
891     {
892         OCStackResult res =  SRPProvisionACL(ctx, link->pDev2, link->pDev2Acl, &AclProv2CB);
893         if (OC_STACK_OK!=res)
894         {
895              UpdateLinkResults(link, 2, res);
896              ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
897                                                      link->resArr,
898                                                      true);
899
900         }
901     }
902     else
903     {
904         ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
905                                                 link->resArr,
906                                                 false);
907         OICFree(link->resArr);
908         OICFree(link);
909     }
910
911     return;
912 }
913
914 /**
915  * Callback to handle credential provisioning.
916  */
917 static void ProvisionCredsCB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
918 {
919     if (NULL == ctx)
920     {
921         OIC_LOG(ERROR,TAG,"Error occured while credential provisioning");
922         return;
923     }
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);
929     if (hasError)
930     {
931         OIC_LOG(ERROR,TAG,"Error occured while credential provisioning");
932         ((OCProvisionResultCB)(resultCallback))(link->ctx, nOfRes,
933                                                 link->resArr,
934                                                 true);
935          OICFree(link->resArr);
936          OICFree(link);
937          return;
938     }
939     if (NULL != link->pDev1Acl)
940     {
941
942         OCStackResult res =  SRPProvisionACL(ctx, link->pDev1, link->pDev1Acl, &AclProv1CB);
943         if (OC_STACK_OK!=res)
944         {
945              OIC_LOG(ERROR, TAG, "Error while provisioning ACL for device 1");
946              UpdateLinkResults(link, 1, res);
947              ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
948                                                      link->resArr,
949                                                      true);
950               OICFree(link->resArr);
951               OICFree(link);
952         }
953     }
954     else if (NULL!=link->pDev2Acl)
955     {
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)
959         {
960              OIC_LOG(ERROR, TAG, "Error while provisioning ACL for device 2");
961               UpdateLinkResults(link, 2, res);
962              ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
963                                                      link->resArr,
964                                                      true);
965               OICFree(link->resArr);
966               OICFree(link);
967         }
968     }
969     else
970     {
971         OIC_LOG(INFO, TAG, "ACLs of both devices are NULL");
972         ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
973                                                 link->resArr,
974                                                 false);
975         OICFree(link->resArr);
976         OICFree(link);
977     }
978     return;
979 }
980 /**
981  * function to provision credentials between two devices and ACLs for the devices who act as a server.
982  *
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.
992  */
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)
997 {
998
999     if (!pDev1 || !pDev2 || !pDev1->doxm || !pDev2->doxm)
1000     {
1001         OIC_LOG(ERROR, TAG, "OCProvisionPairwiseDevices : Invalid parameters");
1002         return OC_STACK_INVALID_PARAM;
1003     }
1004     if (!resultCallback)
1005     {
1006         OIC_LOG(INFO, TAG, "OCProvisionPairwiseDevices : NULL Callback");
1007         return OC_STACK_INVALID_CALLBACK;
1008     }
1009     if (!(keySize == OWNER_PSK_LENGTH_128 || keySize == OWNER_PSK_LENGTH_256))
1010     {
1011         OIC_LOG(INFO, TAG, "OCProvisionPairwiseDevices : Invalid key size");
1012         return OC_STACK_INVALID_PARAM;
1013     }
1014     if (0 == memcmp(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, sizeof(OicUuid_t)))
1015     {
1016         OIC_LOG(INFO, TAG, "OCProvisionPairwiseDevices : Same device ID");
1017         return OC_STACK_INVALID_PARAM;
1018     }
1019
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)
1024     {
1025         OIC_LOG(ERROR, TAG, "Internal Error Occured");
1026         return res;
1027     }
1028     if (linkExists)
1029     {
1030         OIC_LOG(ERROR, TAG, "Link already exists");
1031         return OC_STACK_INVALID_PARAM;
1032     }
1033
1034     int noOfResults = 2; // Initial Value
1035     if (NULL != pDev1Acl)
1036     {
1037         ++noOfResults;
1038     }
1039     if (NULL != pDev2Acl)
1040     {
1041        ++noOfResults;
1042     }
1043     Linkdata_t *link = (Linkdata_t*) OICMalloc(sizeof(Linkdata_t));
1044     if (!link)
1045     {
1046         OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1047         return OC_STACK_NO_MEMORY;
1048     }
1049     OIC_LOG_V(INFO,TAG, "Maximum no od results %d",noOfResults);
1050
1051     link->pDev1 = pDev1;
1052     link->pDev1Acl = pDev1Acl;
1053     link->pDev2 = pDev2;
1054     link->pDev2Acl = pDev2Acl;
1055     link->ctx = ctx;
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)
1065     {
1066         OICFree(link->resArr);
1067         OICFree(link);
1068     }
1069     return res;
1070
1071 }
1072
1073 OCStackResult OCGetDevInfoFromNetwork(unsigned short waittime,
1074                                        OCProvisionDev_t** pOwnedDevList,
1075                                        OCProvisionDev_t** pUnownedDevList)
1076 {
1077     //TODO will be replaced by more efficient logic
1078     if (pOwnedDevList == NULL || *pOwnedDevList != NULL || pUnownedDevList == NULL
1079          || *pUnownedDevList != NULL || 0 == waittime)
1080     {
1081         return OC_STACK_INVALID_PARAM;
1082     }
1083
1084     // Code for unowned discovery
1085     OCProvisionDev_t *unownedDevice = NULL;
1086     OCStackResult res =  OCDiscoverUnownedDevices(waittime/2, &unownedDevice);
1087     if (OC_STACK_OK != res)
1088     {
1089         OIC_LOG(ERROR,TAG, "Error in unowned discovery");
1090         return res;
1091     }
1092
1093     // Code for owned discovery
1094     OCProvisionDev_t *ownedDevice = NULL;
1095     res =  OCDiscoverOwnedDevices(waittime/2, &ownedDevice);
1096     if (OC_STACK_OK != res)
1097     {
1098         OIC_LOG(ERROR,TAG, "Error in owned discovery");
1099         PMDeleteDeviceList(unownedDevice);
1100         return res;
1101     }
1102
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)
1108     {
1109         OIC_LOG(ERROR, TAG, "Error while getting info from DB");
1110         PMDeleteDeviceList(unownedDevice);
1111         PMDeleteDeviceList(ownedDevice);
1112         return res;
1113     }
1114
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)
1119     {
1120         OCUuidList_t *tmp1 = NULL,*tmp2=NULL;
1121         LL_FOREACH_SAFE(uuidList, tmp1, tmp2)
1122         {
1123             if(0 == memcmp(tmp1->dev.id, pUnownedList->doxm->deviceID.id,
1124                             sizeof(pUnownedList->doxm->deviceID.id)))
1125             {
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))
1129                 {
1130                     OIC_LOG(ERROR, TAG, "OCGetDevInfoFromNetwork : \
1131                             Failed to remove device in PDM.");
1132                 }
1133                 //remove the cred entry from dat file
1134                 if (OC_STACK_OK != RemoveDeviceInfoFromLocal(pUnownedList))
1135                 {
1136                     OIC_LOG(ERROR, TAG, "OCGetDevInfoFromNetwork : \
1137                             Failed to remove cred entry device in dat file.");
1138                 }
1139                 LL_DELETE(uuidList, tmp1);
1140                 OICFree(tmp1);
1141             }
1142         }
1143         pUnownedList = pUnownedList->next;
1144     }
1145     // Code to compare devices in owned list and deviceid from DB.
1146     OCProvisionDev_t* pCurDev = ownedDevice;
1147     size_t deleteCnt = 0;
1148     while (pCurDev)
1149     {
1150         if(true == PMDeleteFromUUIDList(&uuidList, &pCurDev->doxm->deviceID))
1151         {
1152             deleteCnt++;
1153         }
1154         pCurDev = pCurDev->next;
1155     }
1156     // If there is no remaind device in uuidList, we have to assign NULL to prevent free.
1157     if (deleteCnt == numOfDevices)
1158     {
1159         uuidList = NULL;
1160     }
1161     // Code to add information of the devices which are currently off in owned list.
1162     OCUuidList_t *powerOffDeviceList = uuidList;
1163     while (powerOffDeviceList)
1164     {
1165         OCProvisionDev_t *ptr = (OCProvisionDev_t *)OICCalloc(1, sizeof (OCProvisionDev_t));
1166         if (NULL == ptr)
1167         {
1168             OIC_LOG(ERROR,TAG,"Fail to allocate memory");
1169             PMDeleteDeviceList(unownedDevice);
1170             PMDeleteDeviceList(ownedDevice);
1171             OCDeleteUuidList(uuidList);
1172             return OC_STACK_NO_MEMORY;
1173         }
1174
1175         ptr->doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
1176         if (NULL == ptr->doxm)
1177         {
1178             OIC_LOG(ERROR,TAG,"Fail to allocate memory");
1179             PMDeleteDeviceList(unownedDevice);
1180             PMDeleteDeviceList(ownedDevice);
1181             OCDeleteUuidList(uuidList);
1182             OICFree(ptr);
1183             return OC_STACK_NO_MEMORY;
1184         }
1185
1186         memcpy(ptr->doxm->deviceID.id, powerOffDeviceList->dev.id, sizeof(ptr->doxm->deviceID.id));
1187
1188         ptr->devStatus = DEV_STATUS_OFF;
1189         LL_PREPEND(ownedDevice, ptr);
1190         powerOffDeviceList = powerOffDeviceList->next;
1191
1192     }
1193     OCDeleteUuidList(uuidList);
1194     *pOwnedDevList = ownedDevice;
1195     *pUnownedDevList = unownedDevice;
1196     return OC_STACK_OK;
1197 }
1198
1199 OCStackResult OCGetLinkedStatus(const OicUuid_t* uuidOfDevice, OCUuidList_t** uuidList,
1200                                  size_t* numOfDevices)
1201 {
1202     return PDMGetLinkedDevices(uuidOfDevice, uuidList, numOfDevices);
1203 }
1204
1205 void OCDeleteUuidList(OCUuidList_t* pList)
1206 {
1207     PDMDestoryOicUuidLinkList(pList);
1208 }
1209
1210 /**
1211  * This function deletes ACL data.
1212  *
1213  * @param pAcl Pointer to OicSecAcl_t structure.
1214  */
1215 void OCDeleteACLList(OicSecAcl_t* pAcl)
1216 {
1217     DeleteACLList(pAcl);
1218 }
1219
1220 /**
1221  * This function deletes PDACL data.
1222  *
1223  * @param pPdAcl Pointer to OicSecPdAcl_t structure.
1224  */
1225 void OCDeletePdAclList(OicSecPdAcl_t* pPdAcl)
1226 {
1227     FreePdAclList(pPdAcl);
1228 }
1229
1230 #ifdef _ENABLE_MULTIPLE_OWNER_
1231 /**
1232  * API to update 'doxm.mom' to resource server.
1233  *
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.
1239  */
1240 OCStackResult OCChangeMOTMode(void *ctx, const OCProvisionDev_t *targetDeviceInfo,
1241                             const OicSecMomType_t momType, OCProvisionResultCB resultCallback)
1242 {
1243     return MOTChangeMode(ctx, targetDeviceInfo, momType, resultCallback);
1244 }
1245
1246 /**
1247  * API to update 'doxm.oxmsel' to resource server.
1248  *
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.
1254  */
1255 OCStackResult OCSelectMOTMethod(void *ctx, const OCProvisionDev_t *targetDeviceInfo,
1256                                  const OicSecOxm_t oxmSelValue, OCProvisionResultCB resultCallback)
1257 {
1258     return MOTSelectMOTMethod(ctx, targetDeviceInfo, oxmSelValue, resultCallback);
1259 }
1260 #endif //_ENABLE_MULTIPLE_OWNER_
1261
1262 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
1263 /**
1264  * function to provision Trust certificate chain to devices.
1265  *
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.
1273  */
1274 OCStackResult OCProvisionTrustCertChain(void *ctx, OicSecCredType_t type, uint16_t credId,
1275                                       const OCProvisionDev_t *selectedDeviceInfo,
1276                                       OCProvisionResultCB resultCallback)
1277 {
1278     return SRPProvisionTrustCertChain(ctx, type, credId,
1279                                       selectedDeviceInfo, resultCallback);
1280 }
1281
1282 /**
1283  * function to save Trust certificate chain into Cred of SVR.
1284  *
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.
1290  */
1291 OCStackResult OCSaveTrustCertChain(uint8_t *trustCertChain, size_t chainSize,
1292                                     OicEncodingType_t encodingType, uint16_t *credId)
1293 {
1294     return SRPSaveTrustCertChain(trustCertChain, chainSize, encodingType, credId);
1295 }
1296
1297 /**
1298  * function to register notifier for Trustcertchain change.
1299  *
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.
1303  */
1304 OCStackResult OCRegisterTrustCertChainNotifier(void *ctx, TrustCertChainChangeCB Callback)
1305 {
1306     return SRPRegisterTrustCertChainNotifier(ctx, Callback);
1307 }
1308
1309 /**
1310  * function to de-register notifier for Trustcertchain change.
1311  */
1312 void OCRemoveTrustCertChainNotifier()
1313 {
1314     SRPRemoveTrustCertChainNotifier();
1315 }
1316 #endif // __WITH_DTLS__ || __WITH_TLS__
1317