1 /* *****************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * *****************************************************************/
23 #include "ocprovisioningmanager.h"
24 #include "pmutility.h"
25 #include "srmutility.h"
26 #include "ownershiptransfermanager.h"
27 #include "oic_malloc.h"
29 #include "secureresourceprovider.h"
30 #include "provisioningdatabasemanager.h"
31 #include "credresource.h"
33 #include "aclresource.h" //Note: SRM internal header
34 #include "pconfresource.h"
38 typedef struct Linkdata Linkdata_t;
42 const OCProvisionDev_t *pDev1;
43 OicSecAcl_t *pDev1Acl;
44 const OCProvisionDev_t *pDev2;
45 OicSecAcl_t *pDev2Acl;
46 OCProvisionResult_t *resArr;
48 int currentCountResults;
49 OCProvisionResultCB resultCallback;
54 * The function is responsible for initializaton of the provisioning manager. It will load
55 * provisioning database which have owned device's list and their linked status.
56 * TODO: In addition, if there is a device(s) which has not up-to-date credentials, this function will
57 * automatically try to update the deivce(s).
59 * @param[in] dbPath file path of the sqlite3 db
61 * @return OC_STACK_OK in case of success and other value otherwise.
63 OCStackResult OCInitPM(const char* dbPath)
65 return PDMInit(dbPath);
69 * The function is responsible for discovery of owned/unowned device is specified endpoint/deviceID.
70 * And this function will only return the specified device's response.
72 * @param[in] timeout Timeout in seconds, value till which function will listen to responses from
73 * server before returning the device.
74 * @param[in] deviceID deviceID of target device.
75 * @param[out] ppFoundDevice OCProvisionDev_t of found device
76 * @return OTM_SUCCESS in case of success and other value otherwise.
78 OCStackResult OCDiscoverSingleDevice(unsigned short timeout, const OicUuid_t* deviceID,
79 OCProvisionDev_t **ppFoundDevice)
81 if( NULL == ppFoundDevice || NULL != *ppFoundDevice || 0 == timeout || NULL == deviceID)
83 return OC_STACK_INVALID_PARAM;
86 return PMSingleDeviceDiscovery(timeout, deviceID, ppFoundDevice);
90 * The function is responsible for discovery of device is current subnet. It will list
91 * all the device in subnet which are not yet owned. Please call OCInit with OC_CLIENT_SERVER as
94 * @param[in] timeout Timeout in seconds, value till which function will listen to responses from
95 * server before returning the list of devices.
96 * @param[out] ppList List of candidate devices to be provisioned
97 * @return OTM_SUCCESS in case of success and other value otherwise.
99 OCStackResult OCDiscoverUnownedDevices(unsigned short timeout, OCProvisionDev_t **ppList)
101 if( ppList == NULL || *ppList != NULL || 0 == timeout)
103 return OC_STACK_INVALID_PARAM;
106 return PMDeviceDiscovery(timeout, false, ppList);
110 * The function is responsible for discovery of owned device is current subnet. It will list
111 * all the device in subnet which are owned by calling provisioning client.
113 * @param[in] timeout Timeout in seconds, value till which function will listen to responses from
114 * server before returning the list of devices.
115 * @param[out] ppList List of device owned by provisioning tool.
116 * @return OTM_SUCCESS in case of success and other value otherwise.
118 OCStackResult OCDiscoverOwnedDevices(unsigned short timeout, OCProvisionDev_t **ppList)
120 if( ppList == NULL || *ppList != NULL || 0 == timeout)
122 return OC_STACK_INVALID_PARAM;
125 return PMDeviceDiscovery(timeout, true, ppList);
129 * API to register for particular OxM.
131 * @param[in] Ownership transfer method.
132 * @param[in] Implementation of callback functions for owership transfer.
133 * @return OC_STACK_OK in case of success and other value otherwise.
135 OCStackResult OCSetOwnerTransferCallbackData(OicSecOxm_t oxm, OTMCallbackData_t* callbackData)
137 if(NULL == callbackData)
139 return OC_STACK_INVALID_CALLBACK ;
142 return OTMSetOwnershipTransferCallbackData(oxm, callbackData);
145 OCStackResult OCDoOwnershipTransfer(void* ctx,
146 OCProvisionDev_t *targetDevices,
147 OCProvisionResultCB resultCallback)
149 if( NULL == targetDevices )
151 return OC_STACK_INVALID_PARAM;
155 OIC_LOG(INFO, TAG, "OCDoOwnershipTransfer : NULL Callback");
156 return OC_STACK_INVALID_CALLBACK;
158 return OTMDoOwnershipTransfer(ctx, targetDevices, resultCallback);
162 * This function deletes memory allocated to linked list created by OCDiscover_XXX_Devices API.
164 * @param[in] pList Pointer to OCProvisionDev_t which should be deleted.
166 void OCDeleteDiscoveredDevices(OCProvisionDev_t *pList)
168 PMDeleteDeviceList(pList);
172 * this function sends ACL information to resource.
174 * @param[in] ctx Application context would be returned in result callback.
175 * @param[in] selectedDeviceInfo Selected target device.
176 * @param[in] acl ACL to provision.
177 * @param[in] resultCallback callback provided by API user, callback will be called when provisioning
178 request recieves a response from resource server.
179 * @return OC_STACK_OK in case of success and other value otherwise.
181 OCStackResult OCProvisionACL(void* ctx, const OCProvisionDev_t *selectedDeviceInfo, OicSecAcl_t *acl,
182 OCProvisionResultCB resultCallback)
184 return SRPProvisionACL(ctx, selectedDeviceInfo, acl, resultCallback);
188 * this function requests CRED information to resource.
190 * @param[in] ctx Application context would be returned in result callback.
191 * @param[in] selectedDeviceInfo Selected target device.
192 * @param[in] resultCallback callback provided by API user, callback will be called when provisioning
193 request recieves a response from resource server.
194 * @return OC_STACK_OK in case of success and other value otherwise.
196 OCStackResult OCGetCredResource(void* ctx, const OCProvisionDev_t *selectedDeviceInfo,
197 OCProvisionResultCB resultCallback)
199 return SRPGetCredResource(ctx, selectedDeviceInfo, resultCallback);
203 * this function requests ACL information to resource.
205 * @param[in] ctx Application context would be returned in result callback.
206 * @param[in] selectedDeviceInfo Selected target device.
207 * @param[in] resultCallback callback provided by API user, callback will be called when provisioning
208 request recieves a response from resource server.
209 * @return OC_STACK_OK in case of success and other value otherwise.
211 OCStackResult OCGetACLResource(void* ctx, const OCProvisionDev_t *selectedDeviceInfo,
212 OCProvisionResultCB resultCallback)
214 return SRPGetACLResource(ctx, selectedDeviceInfo, resultCallback);
218 * function to provision credential to devices.
220 * @param[in] ctx Application context would be returned in result callback.
221 * @param[in] type Type of credentials to be provisioned to the device.
222 * @param[in] pDev1 Pointer to OCProvisionDev_t instance,respresenting resource to be provsioned.
223 @param[in] pDev2 Pointer to OCProvisionDev_t instance,respresenting resource to be provsioned.
224 * @param[in] resultCallback callback provided by API user, callback will be called when
225 * provisioning request recieves a response from first resource server.
226 * @return OC_STACK_OK in case of success and other value otherwise.
228 OCStackResult OCProvisionCredentials(void *ctx, OicSecCredType_t type, size_t keySize,
229 const OCProvisionDev_t *pDev1,
230 const OCProvisionDev_t *pDev2,
231 OCProvisionResultCB resultCallback)
233 return SRPProvisionCredentials(ctx, type, keySize,
234 pDev1, pDev2, resultCallback);
239 * this function sends Direct-Pairing Configuration to a device.
241 * @param[in] ctx Application context would be returned in result callback.
242 * @param[in] selectedDeviceInfo Selected target device.
243 * @param[in] pconf PCONF pointer.
244 * @param[in] resultCallback callback provided by API user, callback will be called when provisioning
245 request recieves a response from resource server.
246 * @return OC_STACK_OK in case of success and other value otherwise.
248 OCStackResult OCProvisionDirectPairing(void* ctx, const OCProvisionDev_t *selectedDeviceInfo, OicSecPconf_t *pconf,
249 OCProvisionResultCB resultCallback)
251 return SRPProvisionDirectPairing(ctx, selectedDeviceInfo, pconf, resultCallback);
255 * Function to unlink devices.
256 * This function will remove the credential & relationship between the two devices.
258 * @param[in] ctx Application context would be returned in result callback
259 * @param[in] pTargetDev1 first device information to be unlinked.
260 * @param[in] pTargetDev2 second device information to be unlinked.
261 * @param[in] resultCallback callback provided by API user, callback will be called when
262 * device unlink is finished.
263 * @return OC_STACK_OK in case of success and other value otherwise.
265 OCStackResult OCUnlinkDevices(void* ctx,
266 const OCProvisionDev_t* pTargetDev1,
267 const OCProvisionDev_t* pTargetDev2,
268 OCProvisionResultCB resultCallback)
270 OIC_LOG(INFO, TAG, "IN OCUnlinkDevices");
271 OCUuidList_t* idList = NULL;
274 if (!pTargetDev1 || !pTargetDev2 || !pTargetDev1->doxm || !pTargetDev2->doxm)
276 OIC_LOG(ERROR, TAG, "OCUnlinkDevices : NULL parameters");
277 return OC_STACK_INVALID_PARAM;
281 OIC_LOG(INFO, TAG, "OCUnlinkDevices : NULL Callback");
282 return OC_STACK_INVALID_CALLBACK;
284 if (0 == memcmp(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID, sizeof(OicUuid_t)))
286 OIC_LOG(INFO, TAG, "OCUnlinkDevices : Same device ID");
287 return OC_STACK_INVALID_PARAM;
290 // Get linked devices with the first device.
291 OCStackResult res = PDMGetLinkedDevices(&(pTargetDev1->doxm->deviceID), &idList, &numOfDev);
292 if (OC_STACK_OK != res)
294 OIC_LOG(ERROR, TAG, "OCUnlinkDevices : PDMgetOwnedDevices failed");
299 OIC_LOG(DEBUG, TAG, "OCUnlinkDevices : Can not find linked devices");
300 res = OC_STACK_INVALID_PARAM; // Input devices are not linked, No request is made
304 // Check the linked devices contains the second device. If yes send credential DELETE request.
305 OCUuidList_t* curDev = idList;
306 while (NULL != curDev)
308 if (memcmp(pTargetDev2->doxm->deviceID.id, curDev->dev.id, sizeof(curDev->dev.id)) == 0)
310 res = SRPUnlinkDevices(ctx, pTargetDev1, pTargetDev2, resultCallback);
311 if (OC_STACK_OK != res)
313 OIC_LOG(ERROR, TAG, "OCUnlinkDevices : Failed to unlink devices.");
317 curDev = curDev->next;
319 OIC_LOG(DEBUG, TAG, "No matched pair found from provisioning database");
320 res = OC_STACK_INVALID_PARAM; // Input devices are not linked, No request is made
323 OIC_LOG(INFO, TAG, "OUT OCUnlinkDevices");
325 PDMDestoryOicUuidLinkList(idList);
329 static OCStackResult RemoveDeviceInfoFromLocal(const OCProvisionDev_t* pTargetDev)
331 // Remove credential of revoked device from SVR database
332 OCStackResult res = OC_STACK_ERROR;
333 const OicSecCred_t *cred = NULL;
335 OIC_LOG(DEBUG, TAG, "IN RemoveDeviceInfoFromLocal");
336 cred = GetCredResourceData(&pTargetDev->doxm->deviceID);
339 res = RemoveCredential(&cred->subject);
340 if (res != OC_STACK_RESOURCE_DELETED)
342 OIC_LOG(ERROR, TAG, "RemoveDeviceInfoFromLocal : Failed to remove credential.");
347 * Change the device status as stale status.
348 * If all request are successed, this device information will be deleted.
350 res = PDMSetDeviceStale(&pTargetDev->doxm->deviceID);
351 if (res != OC_STACK_OK)
353 OIC_LOG(WARNING, TAG, "OCRemoveDevice : Failed to set device status as stale");
356 // TODO: We need to add new mechanism to clean up the stale state of the device.
358 // Close the DTLS session of the removed device.
359 CAEndpoint_t *endpoint = (CAEndpoint_t *)&pTargetDev->endpoint;
360 endpoint->port = pTargetDev->securePort;
361 CAResult_t caResult = CAcloseSslSession(endpoint);
362 if(CA_STATUS_OK != caResult)
364 OIC_LOG_V(WARNING, TAG, "OCRemoveDevice : Failed to close DTLS session : %d", caResult);
367 OIC_LOG(DEBUG, TAG, "OUT RemoveDeviceInfoFromLocal");
373 * Function to device revocation
374 * This function will remove credential of target device from all devices in subnet.
376 * @param[in] ctx Application context would be returned in result callback
377 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
378 * @param[in] pTargetDev Device information to be revoked.
379 * @param[in] resultCallback callback provided by API user, callback will be called when
380 * credential revocation is finished.
381 * @return OC_STACK_OK in case of success and other value otherwise.
383 OCStackResult OCRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
384 const OCProvisionDev_t* pTargetDev,
385 OCProvisionResultCB resultCallback)
387 OIC_LOG(INFO, TAG, "IN OCRemoveDevice");
388 OCStackResult res = OC_STACK_ERROR;
389 if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
391 OIC_LOG(INFO, TAG, "OCRemoveDevice : Invalied parameters");
392 return OC_STACK_INVALID_PARAM;
396 OIC_LOG(INFO, TAG, "OCRemoveDevice : NULL Callback");
397 return OC_STACK_INVALID_CALLBACK;
400 // Send DELETE requests to linked devices
401 OCStackResult resReq = OC_STACK_ERROR; // Check that we have to wait callback or not.
402 resReq = SRPRemoveDevice(ctx, waitTimeForOwnedDeviceDiscovery, pTargetDev, resultCallback);
403 if (OC_STACK_OK != resReq)
405 if (OC_STACK_CONTINUE == resReq)
407 OIC_LOG(DEBUG, TAG, "OCRemoveDevice : Revoked device has no linked device except PT.");
411 OIC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to invoke SRPRemoveDevice");
417 res = RemoveDeviceInfoFromLocal(pTargetDev);
418 if(OC_STACK_OK != res)
420 OIC_LOG(ERROR, TAG, "Filed to remove the device information from local.");
424 if(OC_STACK_CONTINUE == resReq)
427 * If there is no linked device, PM does not send any request.
428 * So we should directly invoke the result callback to inform the result of OCRemoveDevice.
432 resultCallback(ctx, 0, NULL, false);
438 OIC_LOG(INFO, TAG, "OUT OCRemoveDevice");
443 * Function to device revocation
444 * This function will remove credential of target device from all devices in subnet.
446 * @param[in] ctx Application context would be returned in result callback
447 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
448 * @param[in] pTargetDev Device information to be revoked.
449 * @param[in] resultCallback callback provided by API user, callback will be called when
450 * credential revocation is finished.
451 * @return OC_STACK_OK in case of success and other value otherwise.
453 OCStackResult OCRemoveDeviceWithUuid(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
454 const OicUuid_t* pTargetUuid,
455 OCProvisionResultCB resultCallback)
457 OIC_LOG(INFO, TAG, "IN OCRemoveDeviceWithUuid");
459 OCStackResult res = OC_STACK_ERROR;
460 OCProvisionDev_t* pTargetDev = NULL;
461 bool discoverdFlag = false;
462 OCProvisionDev_t* pOwnedDevList = NULL;
464 if (!pTargetUuid || 0 == waitTimeForOwnedDeviceDiscovery)
466 OIC_LOG(INFO, TAG, "OCRemoveDeviceWithUuid : Invalied parameters");
467 return OC_STACK_INVALID_PARAM;
471 OIC_LOG(INFO, TAG, "OCRemoveDeviceWithUuid : NULL Callback");
472 return OC_STACK_INVALID_CALLBACK;
475 char* strUuid = NULL;
476 if(OC_STACK_OK != ConvertUuidToStr(pTargetUuid, &strUuid))
478 OIC_LOG(WARNING, TAG, "Failed to covert UUID to String.");
482 //2. Find owned device from the network
483 res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
484 if (OC_STACK_OK != res)
486 OIC_LOG(ERROR, TAG, "OCRemoveDeviceWithUuid : Failed to PMDeviceDiscovery");
490 LL_FOREACH(pOwnedDevList, pTargetDev)
492 if(memcmp(&pTargetDev->doxm->deviceID.id, pTargetUuid->id, sizeof(pTargetUuid->id)) == 0)
498 if(NULL == pTargetDev)
500 OIC_LOG_V(WARNING, TAG, "Can not find [%s] on the network.", strUuid);
501 OIC_LOG(WARNING, TAG, "Device information will be deleted from local and other devices.");
503 pTargetDev = (OCProvisionDev_t*)OICCalloc(1, sizeof(OCProvisionDev_t));
504 if(NULL == pTargetDev)
506 OIC_LOG(ERROR, TAG, "Failed to memory allocation.");
510 pTargetDev->doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
511 if(NULL == pTargetDev->doxm)
513 OIC_LOG(ERROR, TAG, "Failed to memory allocation.");
517 //in case of can't find target device, the device id required only.
518 memcpy(pTargetDev->doxm->deviceID.id, pTargetUuid->id, sizeof(pTargetUuid->id));
522 discoverdFlag = true;
523 OIC_LOG_V(INFO, TAG, "[%s] is dectected on the network.", strUuid);
526 OIC_LOG_V(INFO, TAG, "Trying [%s] revocation.", strUuid);
528 // Send DELETE requests to linked devices
529 OCStackResult resReq = OC_STACK_ERROR; // Check that we have to wait callback or not.
530 resReq = SRPRemoveDeviceWithoutDiscovery(ctx, pOwnedDevList, pTargetDev, resultCallback);
531 if (OC_STACK_OK != resReq)
533 if (OC_STACK_CONTINUE == resReq)
535 OIC_LOG(DEBUG, TAG, "OCRemoveDeviceWithUuid : Revoked device has no linked device except PT.");
539 OIC_LOG(ERROR, TAG, "OCRemoveDeviceWithUuid : Failed to invoke SRPRemoveDevice");
545 res = RemoveDeviceInfoFromLocal(pTargetDev);
546 if(OC_STACK_OK != res)
548 OIC_LOG(ERROR, TAG, "OCRemoveDeviceWithUuid : Filed to remove the device information from local.");
552 if(OC_STACK_CONTINUE == resReq)
555 * If there is no linked device, PM does not send any request.
556 * So we should directly invoke the result callback to inform the result of OCRemoveDevice.
560 resultCallback(ctx, 0, NULL, false);
567 PMDeleteDeviceList(pOwnedDevList);
568 if(pTargetDev && false == discoverdFlag)
570 OICFree(pTargetDev->doxm);
573 OIC_LOG(INFO, TAG, "OUT OCRemoveDeviceWithUuid");
578 * Function to reset the target device.
579 * This function will remove credential and ACL of target device from all devices in subnet.
581 * @param[in] ctx Application context would be returned in result callback
582 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
583 * @param[in] pTargetDev Device information to be revoked.
584 * @param[in] resultCallback callback provided by API user, callback will be called when
585 * credential revocation is finished.
586 * @return OC_STACK_OK in case of success and other value otherwise.
588 OCStackResult OCResetDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
589 const OCProvisionDev_t* pTargetDev,
590 OCProvisionResultCB resultCallback)
592 OIC_LOG(INFO, TAG, "IN OCResetDevice");
593 OCStackResult res = OC_STACK_ERROR;
594 if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
596 OIC_LOG(INFO, TAG, "OCResetDevice : Invalid parameters");
597 return OC_STACK_INVALID_PARAM;
601 OIC_LOG(INFO, TAG, "OCResetDevice : NULL Callback");
602 return OC_STACK_INVALID_CALLBACK;
605 // Send DELETE requests to linked devices
606 res = SRPSyncDevice(ctx, waitTimeForOwnedDeviceDiscovery, pTargetDev, resultCallback);
607 if (OC_STACK_CONTINUE == res)
609 OIC_LOG(DEBUG, TAG, "OCResetDevice : Target device has no linked device except PT.");
612 resultCallback(ctx, 0, NULL, false);
614 SRPResetDevice(pTargetDev, resultCallback);
617 else if(OC_STACK_OK != res)
619 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to invoke SRPSyncDevice");
621 OIC_LOG(INFO, TAG, "OUT OCResetDevice");
626 * Internal Function to update result in link result array.
628 static void UpdateLinkResults(Linkdata_t *link, int device, OCStackResult stackresult)
631 OIC_LOG_V(INFO,TAG,"value of link->currentCountResults is %d",link->currentCountResults);
634 memcpy(link->resArr[(link->currentCountResults)].deviceId.id, link->pDev1->doxm->deviceID.id,UUID_LENGTH);
638 memcpy(link->resArr[(link->currentCountResults)].deviceId.id, link->pDev2->doxm->deviceID.id,UUID_LENGTH);
640 link->resArr[(link->currentCountResults)].res = stackresult;
641 ++(link->currentCountResults);
646 * Callback to handle ACL provisioning for device 2.
648 static void AclProv2CB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
653 OIC_LOG(ERROR,TAG,"Context is Null in ACLProv 2");
657 Linkdata_t *link = (Linkdata_t*)ctx;
658 OCProvisionResultCB resultCallback = link->resultCallback;
663 UpdateLinkResults(link, 2,arr[0].res);
664 OIC_LOG(ERROR,TAG,"Error occured while ACL provisioning device 1");
665 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
668 OICFree(link->resArr);
672 UpdateLinkResults(link, 2, arr[0].res);
673 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
676 OICFree(link->resArr);
682 * Callback to handle ACL provisioning for device 1
684 static void AclProv1CB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
689 OIC_LOG(ERROR,TAG,"Context is Null in ACLProv1");
693 Linkdata_t *link = (Linkdata_t*)ctx;
694 OCProvisionResultCB resultCallback = link->resultCallback;
698 OIC_LOG(ERROR,TAG,"Error occured while ACL provisioning device 1");
699 UpdateLinkResults(link, 1, arr[0].res);
700 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
703 OICFree(link->resArr);
707 UpdateLinkResults(link, 1, arr[0].res);
708 if (NULL != link->pDev2Acl)
710 OCStackResult res = SRPProvisionACL(ctx, link->pDev2, link->pDev2Acl, &AclProv2CB);
711 if (OC_STACK_OK!=res)
713 UpdateLinkResults(link, 2, res);
714 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
722 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
725 OICFree(link->resArr);
733 * Callback to handle credential provisioning.
735 static void ProvisionCredsCB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
739 OIC_LOG(ERROR,TAG,"Error occured while credential provisioning");
742 Linkdata_t *link = (Linkdata_t*)ctx;
743 OCProvisionResultCB resultCallback = link->resultCallback;
744 OIC_LOG_V(INFO, TAG, "has error returned %d",hasError);
745 UpdateLinkResults(link, 1, arr[0].res);
746 UpdateLinkResults(link, 2, arr[1].res);
749 OIC_LOG(ERROR,TAG,"Error occured while credential provisioning");
750 ((OCProvisionResultCB)(resultCallback))(link->ctx, nOfRes,
753 OICFree(link->resArr);
757 if (NULL != link->pDev1Acl)
760 OCStackResult res = SRPProvisionACL(ctx, link->pDev1, link->pDev1Acl, &AclProv1CB);
761 if (OC_STACK_OK!=res)
763 OIC_LOG(ERROR, TAG, "Error while provisioning ACL for device 1");
764 UpdateLinkResults(link, 1, res);
765 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
768 OICFree(link->resArr);
772 else if (NULL!=link->pDev2Acl)
774 OIC_LOG(ERROR, TAG, "ACL for device 1 is NULL");
775 OCStackResult res = SRPProvisionACL(ctx, link->pDev2, link->pDev2Acl, &AclProv2CB);
776 if (OC_STACK_OK!=res)
778 OIC_LOG(ERROR, TAG, "Error while provisioning ACL for device 2");
779 UpdateLinkResults(link, 2, res);
780 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
783 OICFree(link->resArr);
789 OIC_LOG(INFO, TAG, "ACLs of both devices are NULL");
790 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
793 OICFree(link->resArr);
799 * function to provision credentials between two devices and ACLs for the devices who act as a server.
801 * @param[in] ctx Application context would be returned in result callback.
802 * @param[in] type Type of credentials to be provisioned to the device.
803 * @param[in] pDev1 Pointer to OCProvisionDev_t instance,respresenting resource to be provsioned.
804 * @param[in] acl ACL for device 1. If this is not required set NULL.
805 * @param[in] pDev2 Pointer to OCProvisionDev_t instance,respresenting resource to be provsioned.
806 * @param[in] acl ACL for device 2. If this is not required set NULL.
807 * @param[in] resultCallback callback provided by API user, callback will be called when
808 * provisioning request recieves a response from first resource server.
809 * @return OC_STACK_OK in case of success and other value otherwise.
811 OCStackResult OCProvisionPairwiseDevices(void* ctx, OicSecCredType_t type, size_t keySize,
812 const OCProvisionDev_t *pDev1, OicSecAcl_t *pDev1Acl,
813 const OCProvisionDev_t *pDev2, OicSecAcl_t *pDev2Acl,
814 OCProvisionResultCB resultCallback)
817 if (!pDev1 || !pDev2 || !pDev1->doxm || !pDev2->doxm)
819 OIC_LOG(ERROR, TAG, "OCProvisionPairwiseDevices : Invalid parameters");
820 return OC_STACK_INVALID_PARAM;
824 OIC_LOG(INFO, TAG, "OCProvisionPairwiseDevices : NULL Callback");
825 return OC_STACK_INVALID_CALLBACK;
827 if (!(keySize == OWNER_PSK_LENGTH_128 || keySize == OWNER_PSK_LENGTH_256))
829 OIC_LOG(INFO, TAG, "OCProvisionPairwiseDevices : Invalid key size");
830 return OC_STACK_INVALID_PARAM;
832 if (0 == memcmp(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, sizeof(OicUuid_t)))
834 OIC_LOG(INFO, TAG, "OCProvisionPairwiseDevices : Same device ID");
835 return OC_STACK_INVALID_PARAM;
838 OIC_LOG(DEBUG, TAG, "Checking link in DB");
839 bool linkExists = true;
840 OCStackResult res = PDMIsLinkExists(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, &linkExists);
841 if(res != OC_STACK_OK)
843 OIC_LOG(ERROR, TAG, "Internal Error Occured");
848 OIC_LOG(ERROR, TAG, "Link already exists");
849 return OC_STACK_INVALID_PARAM;
852 int noOfResults = 2; // Initial Value
853 if (NULL != pDev1Acl)
857 if (NULL != pDev2Acl)
861 Linkdata_t *link = (Linkdata_t*) OICMalloc(sizeof(Linkdata_t));
864 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
865 return OC_STACK_NO_MEMORY;
867 OIC_LOG_V(INFO,TAG, "Maximum no od results %d",noOfResults);
870 link->pDev1Acl = pDev1Acl;
872 link->pDev2Acl = pDev2Acl;
874 // 1 call for each device for credential provisioning. implict call by SRPProvisioning credential
875 // 1 call for ACL provisioning for device 1 and 1 call for ACL provisioning for device 2.
876 link->numOfResults = noOfResults;
877 link->resultCallback = resultCallback;
878 link->currentCountResults = 0;
879 link->resArr = (OCProvisionResult_t*) OICMalloc(sizeof(OCProvisionResult_t)*noOfResults);
880 res = SRPProvisionCredentials(link, type, keySize,
881 pDev1, pDev2, &ProvisionCredsCB);
882 if (res != OC_STACK_OK)
884 OICFree(link->resArr);
891 OCStackResult OCGetDevInfoFromNetwork(unsigned short waittime,
892 OCProvisionDev_t** pOwnedDevList,
893 OCProvisionDev_t** pUnownedDevList)
895 //TODO will be replaced by more efficient logic
896 if (pOwnedDevList == NULL || *pOwnedDevList != NULL || pUnownedDevList == NULL
897 || *pUnownedDevList != NULL || 0 == waittime)
899 return OC_STACK_INVALID_PARAM;
902 // Code for unowned discovery
903 OCProvisionDev_t *unownedDevice = NULL;
904 OCStackResult res = OCDiscoverUnownedDevices(waittime/2, &unownedDevice);
905 if (OC_STACK_OK != res)
907 OIC_LOG(ERROR,TAG, "Error in unowned discovery");
911 // Code for owned discovery
912 OCProvisionDev_t *ownedDevice = NULL;
913 res = OCDiscoverOwnedDevices(waittime/2, &ownedDevice);
914 if (OC_STACK_OK != res)
916 OIC_LOG(ERROR,TAG, "Error in owned discovery");
917 PMDeleteDeviceList(unownedDevice);
921 // Code to get list of all the owned devices.
922 OCUuidList_t *uuidList = NULL;
923 size_t numOfDevices = 0;
924 res = PDMGetOwnedDevices(&uuidList, &numOfDevices);
925 if (OC_STACK_OK != res)
927 OIC_LOG(ERROR, TAG, "Error while getting info from DB");
928 PMDeleteDeviceList(unownedDevice);
929 PMDeleteDeviceList(ownedDevice);
933 // Code to compare devices in unowned list and deviceid from DB
934 // (In case of hard reset of the device)
935 OCProvisionDev_t* pUnownedList = unownedDevice;
936 while (pUnownedList && uuidList)
938 OCUuidList_t *tmp1 = NULL,*tmp2=NULL;
939 LL_FOREACH_SAFE(uuidList, tmp1, tmp2)
941 if(0 == memcmp(tmp1->dev.id, pUnownedList->doxm->deviceID.id,
942 sizeof(pUnownedList->doxm->deviceID.id)))
944 OIC_LOG_V(INFO, TAG, "OCGetDevInfoFromNetwork : \
945 Removing device id = %s in PDM and dat.", pUnownedList->doxm->deviceID.id);
946 if (OC_STACK_OK != PDMDeleteDevice(&pUnownedList->doxm->deviceID))
948 OIC_LOG(ERROR, TAG, "OCGetDevInfoFromNetwork : \
949 Failed to remove device in PDM.");
951 //remove the cred entry from dat file
952 if (OC_STACK_OK != RemoveDeviceInfoFromLocal(pUnownedList))
954 OIC_LOG(ERROR, TAG, "OCGetDevInfoFromNetwork : \
955 Failed to remove cred entry device in dat file.");
957 LL_DELETE(uuidList, tmp1);
961 pUnownedList = pUnownedList->next;
963 // Code to compare devices in owned list and deviceid from DB.
964 OCProvisionDev_t* pCurDev = ownedDevice;
965 size_t deleteCnt = 0;
968 if(true == PMDeleteFromUUIDList(&uuidList, &pCurDev->doxm->deviceID))
972 pCurDev = pCurDev->next;
974 // If there is no remaind device in uuidList, we have to assign NULL to prevent free.
975 if (deleteCnt == numOfDevices)
979 // Code to add information of the devices which are currently off in owned list.
980 OCUuidList_t *powerOffDeviceList = uuidList;
981 while (powerOffDeviceList)
983 OCProvisionDev_t *ptr = (OCProvisionDev_t *)OICCalloc(1, sizeof (OCProvisionDev_t));
986 OIC_LOG(ERROR,TAG,"Fail to allocate memory");
987 PMDeleteDeviceList(unownedDevice);
988 PMDeleteDeviceList(ownedDevice);
989 OCDeleteUuidList(uuidList);
990 return OC_STACK_NO_MEMORY;
993 ptr->doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
994 if (NULL == ptr->doxm)
996 OIC_LOG(ERROR,TAG,"Fail to allocate memory");
997 PMDeleteDeviceList(unownedDevice);
998 PMDeleteDeviceList(ownedDevice);
999 OCDeleteUuidList(uuidList);
1001 return OC_STACK_NO_MEMORY;
1004 memcpy(ptr->doxm->deviceID.id, powerOffDeviceList->dev.id, sizeof(ptr->doxm->deviceID.id));
1006 ptr->devStatus = DEV_STATUS_OFF;
1007 LL_PREPEND(ownedDevice, ptr);
1008 powerOffDeviceList = powerOffDeviceList->next;
1011 OCDeleteUuidList(uuidList);
1012 *pOwnedDevList = ownedDevice;
1013 *pUnownedDevList = unownedDevice;
1017 OCStackResult OCGetLinkedStatus(const OicUuid_t* uuidOfDevice, OCUuidList_t** uuidList,
1018 size_t* numOfDevices)
1020 return PDMGetLinkedDevices(uuidOfDevice, uuidList, numOfDevices);
1023 void OCDeleteUuidList(OCUuidList_t* pList)
1025 PDMDestoryOicUuidLinkList(pList);
1029 * This function deletes ACL data.
1031 * @param pAcl Pointer to OicSecAcl_t structure.
1033 void OCDeleteACLList(OicSecAcl_t* pAcl)
1035 DeleteACLList(pAcl);
1039 * This function deletes PDACL data.
1041 * @param pPdAcl Pointer to OicSecPdAcl_t structure.
1043 void OCDeletePdAclList(OicSecPdAcl_t* pPdAcl)
1045 FreePdAclList(pPdAcl);
1047 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
1049 * function to provision Trust certificate chain to devices.
1051 * @param[in] ctx Application context would be returned in result callback.
1052 * @param[in] type Type of credentials to be provisioned to the device.
1053 * @param[in] credId CredId of trust certificate chain to be provisioned to the device.
1054 * @param[in] selectedDeviceInfo Pointer to OCProvisionDev_t instance,respresenting resource to be provsioned.
1055 * @param[in] resultCallback callback provided by API user, callback will be called when
1056 * provisioning request recieves a response from first resource server.
1057 * @return OC_STACK_OK in case of success and other value otherwise.
1059 OCStackResult OCProvisionTrustCertChain(void *ctx, OicSecCredType_t type, uint16_t credId,
1060 const OCProvisionDev_t *selectedDeviceInfo,
1061 OCProvisionResultCB resultCallback)
1063 return SRPProvisionTrustCertChain(ctx, type, credId,
1064 selectedDeviceInfo, resultCallback);
1067 * function to save Trust certificate chain into Cred of SVR.
1069 * @param[in] trustCertChain Trust certificate chain to be saved in Cred of SVR.
1070 * @param[in] chainSize Size of trust certificate chain to be saved in Cred of SVR
1071 * @param[in] encodingType Encoding type of trust certificate chain to be saved in Cred of SVR
1072 * @param[out] credId CredId of saved trust certificate chain in Cred of SVR.
1073 * @return OC_STACK_OK in case of success and other value otherwise.
1075 OCStackResult OCSaveTrustCertChain(uint8_t *trustCertChain, size_t chainSize,
1076 OicEncodingType_t encodingType, uint16_t *credId)
1078 return SRPSaveTrustCertChain(trustCertChain, chainSize, encodingType, credId);
1081 #endif // __WITH_DTLS__ || __WITH_TLS__