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 * *****************************************************************/
27 #include "ocprovisioningmanager.h"
28 #include "secureresourceprovider.h"
30 #include "oic_malloc.h"
31 #include "aclresource.h"
32 #include "pstatresource.h"
33 #include "srmresourcestrings.h"
34 #include "credresource.h"
35 #include "doxmresource.h"
36 #include "pconfresource.h"
37 #include "credentialgenerator.h"
38 #include "cainterface.h"
41 #include "pmutility.h"
42 #include "provisioningdatabasemanager.h"
45 #include "ocpayload.h"
48 #include "crlresource.h"
54 * Macro to verify argument is not equal to NULL.
55 * eg: VERIFY_NON_NULL(TAG, ptrData, ERROR,OC_STACK_ERROR);
57 #define VERIFY_NON_NULL(tag, arg, logLevel, retValue) { if (NULL == (arg)) \
58 { OIC_LOG((logLevel), tag, #arg " is NULL"); return retValue; } }
61 * Macro to verify success of operation.
62 * eg: VERIFY_SUCCESS(TAG, OC_STACK_OK == foo(), ERROR, OC_STACK_ERROR);
64 #define VERIFY_SUCCESS(tag, op, logLevel, retValue) { if (!(op)) \
65 {OIC_LOG((logLevel), tag, #op " failed!!"); return retValue;} }
68 * Structure to carry credential data to callback.
70 typedef struct CredentialData CredentialData_t;
73 void *ctx; /**< Pointer to user context.**/
74 const OCProvisionDev_t *deviceInfo1; /**< Pointer to OCProvisionDev_t.**/
75 const OCProvisionDev_t *deviceInfo2; /**< Pointer to OCProvisionDev_t.**/
76 OicSecCred_t *credInfo; /**< Pointer to OicSecCred_t.**/
77 OicSecCred_t *credInfoFirst; /**< Pointer to OicSecCred_t.**/
78 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
79 OCProvisionResult_t *resArr; /**< Result array.**/
80 int numOfResults; /**< Number of results in result array.**/
84 * Structure to carry ACL provision API data to callback.
86 typedef struct ACLData ACLData_t;
89 void *ctx; /**< Pointer to user context.**/
90 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
91 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
92 OCProvisionResult_t *resArr; /**< Result array.**/
93 int numOfResults; /**< Number of results in result array.**/
96 // Structure to carry get security resource APIs data to callback.
97 typedef struct GetSecData GetSecData_t;
100 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
101 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
102 OCProvisionResult_t *resArr; /**< Result array.**/
103 int numOfResults; /**< Number of results in result array.**/
107 * Structure to carry PCONF provision API data to callback.
109 typedef struct PconfData PconfData_t;
112 void *ctx; /**< Pointer to user context.**/
113 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
114 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
115 OCProvisionResult_t *resArr; /**< Result array.**/
116 int numOfResults; /**< Number of results in result array.**/
119 // Enum type index for unlink callback.
121 IDX_FIRST_DEVICE_RES = 0, // index for resulf of the first device
122 IDX_SECOND_DEVICE_RES, // index for result of the second device
123 IDX_DB_UPDATE_RES // index for result of updating provisioning database.
126 // Structure to carry unlink APIs data to callback.
127 typedef struct UnlinkData UnlinkData_t;
130 OCProvisionDev_t* unlinkDev; /**< Pointer to OCProvisionDev_t to be unlinked.**/
131 OCProvisionResult_t* unlinkRes; /**< Result array.**/
132 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
133 int numOfResults; /**< Number of results in result array.**/
136 //Example of DELETE cred request -> coaps://0.0.0.0:5684/oic/sec/cred?sub=(BASE64 ENCODED UUID)
137 const char * SRP_FORM_DELETE_CREDENTIAL = "coaps://[%s]:%d%s?%s=%s";
139 // Structure to carry remove APIs data to callback.
140 typedef struct RemoveData RemoveData_t;
143 OCProvisionDev_t* revokeTargetDev; /**< Device which is going to be revoked..**/
144 OCProvisionDev_t* linkedDevList; /**< A list of devices which have invalid credential.**/
145 OCProvisionResult_t* removeRes; /**< Result array.**/
146 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
147 size_t numOfResults; /**< Number of results in result array.**/
148 size_t sizeOfResArray;
155 static OCStackResult provisionCredentials(const OicSecCred_t *cred,
156 const OCProvisionDev_t *deviceInfo, CredentialData_t *credData,
157 OCClientResponseHandler responseHandler);
161 * Internal function to update result in result array.
163 static void registerResultForCredProvisioning(CredentialData_t *credData,
164 OCStackResult stackresult, int cause)
167 OIC_LOG_V(INFO,TAG,"value of credData->numOfResults is %d",credData->numOfResults);
170 memcpy(credData->resArr[(credData->numOfResults)].deviceId.id,
171 credData->deviceInfo1->doxm->deviceID.id,UUID_LENGTH);
175 memcpy(credData->resArr[(credData->numOfResults)].deviceId.id,
176 credData->deviceInfo2->doxm->deviceID.id,UUID_LENGTH);
178 credData->resArr[(credData->numOfResults)].res = stackresult;
179 ++(credData->numOfResults);
183 * Callback handler for handling callback of provisioning device 2.
185 * @param[in] ctx ctx value passed to callback from calling function.
186 * @param[in] UNUSED handle to an invocation
187 * @param[in] clientResponse Response from queries to remote servers.
188 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
189 * and OC_STACK_KEEP_TRANSACTION to keep it.
191 static OCStackApplicationResult provisionCredentialCB2(void *ctx, OCDoHandle UNUSED,
192 OCClientResponse *clientResponse)
194 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
195 CredentialData_t *credData = (CredentialData_t *) ctx;
198 OCProvisionResultCB resultCallback = credData->resultCallback;
199 OIC_LOG(INFO, TAG, "provisionCredentialCB2 called");
202 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
204 registerResultForCredProvisioning(credData, OC_STACK_RESOURCE_CREATED, 2);
205 OCStackResult res = PDMLinkDevices(&credData->deviceInfo1->doxm->deviceID,
206 &credData->deviceInfo2->doxm->deviceID);
207 if (OC_STACK_OK != res)
209 OIC_LOG(ERROR, TAG, "Error occured on PDMLinkDevices");
210 return OC_STACK_DELETE_TRANSACTION;
212 OIC_LOG(INFO, TAG, "Link created successfully");
214 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
217 OICFree(credData->resArr);
219 return OC_STACK_DELETE_TRANSACTION;
223 OIC_LOG(INFO, TAG, "provisionCredentialCB2 received Null clientResponse");
224 registerResultForCredProvisioning(credData, OC_STACK_ERROR, 2);
225 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
228 OICFree(credData->resArr);
230 return OC_STACK_DELETE_TRANSACTION;
234 * Callback handler for handling callback of provisioning device 1.
236 * @param[in] ctx ctx value passed to callback from calling function.
237 * @param[in] UNUSED handle to an invocation
238 * @param[in] clientResponse Response from queries to remote servers.
239 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
240 * and OC_STACK_KEEP_TRANSACTION to keep it.
242 static OCStackApplicationResult provisionCredentialCB1(void *ctx, OCDoHandle UNUSED,
243 OCClientResponse *clientResponse)
245 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
247 CredentialData_t* credData = (CredentialData_t*) ctx;
248 OICFree(credData->credInfoFirst);
249 const OCProvisionDev_t *deviceInfo = credData->deviceInfo2;
250 OicSecCred_t *credInfo = credData->credInfo;
251 const OCProvisionResultCB resultCallback = credData->resultCallback;
254 if (OC_STACK_RESOURCE_CREATED == clientResponse->result)
256 // send credentials to second device
257 registerResultForCredProvisioning(credData, OC_STACK_RESOURCE_CREATED,1);
258 OCStackResult res = provisionCredentials(credInfo, deviceInfo, credData,
259 provisionCredentialCB2);
260 DeleteCredList(credInfo);
261 if (OC_STACK_OK != res)
263 registerResultForCredProvisioning(credData, res,2);
264 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
267 OICFree(credData->resArr);
274 registerResultForCredProvisioning(credData, OC_STACK_ERROR,1);
275 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
278 OICFree(credData->resArr);
285 OIC_LOG(INFO, TAG, "provisionCredentialCB received Null clientResponse for first device");
286 registerResultForCredProvisioning(credData, OC_STACK_ERROR,1);
287 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
290 DeleteCredList(credInfo);
291 OICFree(credData->resArr);
295 return OC_STACK_DELETE_TRANSACTION;
301 * Internal function for handling credential generation and sending credential to resource server.
303 * @param[in] cred Instance of cred resource.
304 * @param[in] deviceInfo information about device to which credential is to be provisioned.
305 * @param[in] responseHandler callbak called by OC stack when request API receives response.
306 * @return OC_STACK_OK in case of success and other value otherwise.
308 static OCStackResult provisionCredentials(const OicSecCred_t *cred,
309 const OCProvisionDev_t *deviceInfo, CredentialData_t *credData,
310 OCClientResponseHandler responseHandler)
312 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
315 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
316 return OC_STACK_NO_MEMORY;
318 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
320 OCStackResult res = CredToCBORPayload(cred, &secPayload->securityData,
321 &secPayload->payloadSize, secureFlag);
322 if((OC_STACK_OK != res) && (NULL == secPayload->securityData))
324 OCPayloadDestroy((OCPayload *)secPayload);
325 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
326 return OC_STACK_NO_MEMORY;
329 OIC_LOG(DEBUG, TAG, "Created payload for Cred:");
330 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
331 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
332 if(!PMGenerateQuery(true,
333 deviceInfo->endpoint.addr,
334 deviceInfo->securePort,
335 deviceInfo->connType,
336 query, sizeof(query), OIC_RSRC_CRED_URI))
338 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
339 return OC_STACK_ERROR;
341 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
343 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
344 cbData.cb = responseHandler;
345 cbData.context = (void *) credData;
348 OCDoHandle handle = NULL;
349 OCMethod method = OC_REST_POST;
350 OCStackResult ret = OCDoResource(&handle, method, query, 0, (OCPayload*)secPayload,
351 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
352 OIC_LOG_V(INFO, TAG, "OCDoResource::Credential provisioning returned : %d",ret);
353 if (ret != OC_STACK_OK)
355 OIC_LOG(ERROR, TAG, "OCStack resource error");
363 * Structure to carry certificate data to callback.
365 typedef struct CertificateData CertData_t;
366 struct CertificateData
368 void *ctx; /**< Pointer to user context.**/
369 const OCProvisionDev_t *deviceInfo; /**< Pointer to OCProvisionDev_t.**/
370 OicSecCred_t *credInfo; /**< Pointer to OicSecCred_t.**/
371 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
372 OCProvisionResult_t *resArr; /**< Result array.**/
373 int numOfResults; /**< Number of results in result array.**/
377 * Structure to carry CRL provision API data to callback.
379 typedef struct CRLData CRLData_t;
382 void *ctx; /**< Pointer to user context.**/
383 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
384 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
385 OCProvisionResult_t *resArr; /**< Result array.**/
386 int numOfResults; /**< Number of results in result array.**/
390 * Internal function to update result in result array.
392 static void registerResultForCertProvisioning(CertData_t *certData,
393 OCStackResult stackresult)
396 OIC_LOG_V(INFO,TAG,"value of credData->numOfResults is %d",certData->numOfResults);
397 memcpy(certData->resArr[(certData->numOfResults)].deviceId.id,
398 certData->deviceInfo->doxm->deviceID.id,UUID_LENGTH);
399 certData->resArr[(certData->numOfResults)].res = stackresult;
400 ++(certData->numOfResults);
404 * Internal Function to store results in result array during ACL provisioning.
406 static void registerResultForCRLProvisioning(CRLData_t *crlData,
407 OCStackResult stackresult)
409 OIC_LOG_V(INFO, TAG, "Inside registerResultForCRLProvisioning crlData->numOfResults is %d\n",
410 crlData->numOfResults);
411 memcpy(crlData->resArr[(crlData->numOfResults)].deviceId.id,
412 crlData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
413 crlData->resArr[(crlData->numOfResults)].res = stackresult;
414 ++(crlData->numOfResults);
419 * Callback handler of SRPProvisionCRL.
421 * @param[in] ctx ctx value passed to callback from calling function.
422 * @param[in] UNUSED handle to an invocation
423 * @param[in] clientResponse Response from queries to remote servers.
424 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
425 * and OC_STACK_KEEP_TRANSACTION to keep it.
427 static OCStackApplicationResult SRPProvisionCRLCB(void *ctx, OCDoHandle UNUSED,
428 OCClientResponse *clientResponse)
430 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionCRLCB.");
432 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
433 CRLData_t *crlData = (CRLData_t*)ctx;
434 OCProvisionResultCB resultCallback = crlData->resultCallback;
438 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
440 registerResultForCRLProvisioning(crlData, OC_STACK_RESOURCE_CREATED);
441 ((OCProvisionResultCB)(resultCallback))(crlData->ctx, crlData->numOfResults,
444 OICFree(crlData->resArr);
446 return OC_STACK_DELETE_TRANSACTION;
449 registerResultForCRLProvisioning(crlData, OC_STACK_ERROR);
450 ((OCProvisionResultCB)(resultCallback))(crlData->ctx, crlData->numOfResults,
453 OIC_LOG_V(ERROR, TAG, "SRPProvisionCRLCB received Null clientResponse");
454 OICFree(crlData->resArr);
456 return OC_STACK_DELETE_TRANSACTION;
459 OCStackResult SRPProvisionCRL(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
460 OicSecCrl_t *crl, OCProvisionResultCB resultCallback)
462 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
463 VERIFY_NON_NULL(TAG, crl, ERROR, OC_STACK_INVALID_PARAM);
464 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
466 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
469 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
470 return OC_STACK_NO_MEMORY;
473 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
474 OCStackResult res = CrlToCBORPayload(crl, &secPayload->securityData, &secPayload->payloadSize);
475 if((OC_STACK_OK != res) && (NULL == secPayload->securityData))
478 OIC_LOG(ERROR, TAG, "Failed to BinToCrlJSON");
479 return OC_STACK_NO_MEMORY;
481 OIC_LOG(DEBUG, TAG, "Created payload for CRL:");
482 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
484 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
485 if(!PMGenerateQuery(true,
486 selectedDeviceInfo->endpoint.addr,
487 selectedDeviceInfo->securePort,
488 selectedDeviceInfo->connType,
489 query, sizeof(query), OIC_RSRC_CRL_URI))
491 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
492 OCPayloadDestroy((OCPayload *)secPayload);
493 return OC_STACK_ERROR;
495 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
497 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
498 cbData.cb = &SRPProvisionCRLCB;
499 CRLData_t *crlData = (CRLData_t *) OICCalloc(1, sizeof(CRLData_t));
502 OCPayloadDestroy((OCPayload *)secPayload);
503 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
504 return OC_STACK_NO_MEMORY;
507 crlData->deviceInfo = selectedDeviceInfo;
508 crlData->resultCallback = resultCallback;
509 crlData->numOfResults=0;
512 crlData->resArr = (OCProvisionResult_t*)OICCalloc(1, sizeof(OCProvisionResult_t));
513 if (crlData->resArr == NULL)
515 OCPayloadDestroy((OCPayload *)secPayload);
516 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
517 return OC_STACK_NO_MEMORY;
520 cbData.context = (void *)crlData;
522 OCMethod method = OC_REST_POST;
523 OCDoHandle handle = NULL;
524 OIC_LOG(DEBUG, TAG, "Sending CRL info to resource server");
526 OCStackResult ret = OCDoResource(&handle, method, query,
527 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
528 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
530 if (ret != OC_STACK_OK)
532 OICFree(crlData->resArr);
540 * Internal function for handling credential generation and sending cretificate credential.
542 * @param[in] cred Instance of cred resource.
543 * @param[in] deviceInfo information about device to which credential is to be provisioned.
544 * @param[in] responseHandler callbak called by OC stack when request API receives response.
545 * @return OC_STACK_OK in case of success and other value otherwise.
547 static OCStackResult provisionCertCred(const OicSecCred_t *cred,
548 const OCProvisionDev_t *deviceInfo, CertData_t *certData,
549 OCClientResponseHandler responseHandler)
551 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
554 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
555 return OC_STACK_NO_MEMORY;
557 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
559 OCStackResult res = CredToCBORPayload(cred, &secPayload->securityData,
560 &secPayload->payloadSize, secureFlag);
562 if ((OC_STACK_OK != res) || (NULL == secPayload->securityData))
565 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
566 return OC_STACK_NO_MEMORY;
569 OIC_LOG(DEBUG, TAG, "Created payload for Cred:");
570 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
571 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
572 if(!PMGenerateQuery(true,
573 deviceInfo->endpoint.addr,
574 deviceInfo->securePort,
575 deviceInfo->connType,
576 query, sizeof(query), OIC_RSRC_CRED_URI))
578 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
579 OCPayloadDestroy((OCPayload *)secPayload);
580 return OC_STACK_ERROR;
582 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
584 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
585 cbData.cb = responseHandler;
586 cbData.context = (void *) certData;
589 OCDoHandle handle = NULL;
590 OCMethod method = OC_REST_POST;
591 OCStackResult ret = OCDoResource(&handle, method, query, 0, (OCPayload*)secPayload,
592 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
593 OIC_LOG_V(INFO, TAG, "OCDoResource::Certificate provisioning returned : %d",ret);
594 if (ret != OC_STACK_OK)
596 OIC_LOG(ERROR, TAG, "OCStack resource error");
603 * Callback handler for handling callback of certificate provisioning device.
605 * @param[in] ctx ctx value passed to callback from calling function.
606 * @param[in] UNUSED handle to an invocation
607 * @param[in] clientResponse Response from queries to remote servers.
608 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
609 * and OC_STACK_KEEP_TRANSACTION to keep it.
611 static OCStackApplicationResult provisionCertCB(void *ctx, OCDoHandle UNUSED,
612 OCClientResponse *clientResponse)
614 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
615 CertData_t *certData = (CertData_t *) ctx;
618 OCProvisionResultCB resultCallback = certData->resultCallback;
619 OIC_LOG(INFO, TAG, "provisionCertCred called");
622 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
624 registerResultForCertProvisioning(certData, OC_STACK_RESOURCE_CREATED);
625 ((OCProvisionResultCB)(resultCallback))(certData->ctx, certData->numOfResults,
628 OICFree(certData->resArr);
630 return OC_STACK_DELETE_TRANSACTION;
634 OIC_LOG(INFO, TAG, "provisionCertCredCB received Null clientResponse");
635 registerResultForCertProvisioning(certData, OC_STACK_ERROR);
636 ((OCProvisionResultCB)(resultCallback))(certData->ctx, certData->numOfResults,
639 OICFree(certData->resArr);
641 return OC_STACK_DELETE_TRANSACTION;
643 #endif // __WITH_X509__
645 OCStackResult SRPProvisionCredentials(void *ctx, OicSecCredType_t type, size_t keySize,
646 const OCProvisionDev_t *pDev1,
647 const OCProvisionDev_t *pDev2,
648 OCProvisionResultCB resultCallback)
650 VERIFY_NON_NULL(TAG, pDev1, ERROR, OC_STACK_INVALID_PARAM);
651 if (SYMMETRIC_PAIR_WISE_KEY == type)
653 VERIFY_NON_NULL(TAG, pDev2, ERROR, OC_STACK_INVALID_PARAM);
657 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL Callback");
658 return OC_STACK_INVALID_CALLBACK;
660 if (SYMMETRIC_PAIR_WISE_KEY == type &&
661 0 == memcmp(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, sizeof(OicUuid_t)))
663 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : Same device ID");
664 return OC_STACK_INVALID_PARAM;
667 if (SYMMETRIC_PAIR_WISE_KEY == type &&
668 !(OWNER_PSK_LENGTH_128 == keySize || OWNER_PSK_LENGTH_256 == keySize))
670 OIC_LOG(INFO, TAG, "Invalid key size");
671 return OC_STACK_INVALID_PARAM;
674 OIC_LOG(INFO, TAG, "In SRPProvisionCredentials");
676 if (SYMMETRIC_PAIR_WISE_KEY == type)
678 bool linkExisits = true;
679 OCStackResult res = PDMIsLinkExists(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, &linkExisits);
681 if (res != OC_STACK_OK)
683 OIC_LOG(ERROR, TAG, "Internal error occured");
688 OIC_LOG(ERROR, TAG, "Link already exists");
689 return OC_STACK_INVALID_PARAM;
693 OicUuid_t provTooldeviceID = {{0,}};
694 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
696 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
697 return OC_STACK_ERROR;
699 OIC_LOG(INFO, TAG, "retrieved deviceid");
702 case SYMMETRIC_PAIR_WISE_KEY:
704 const OCProvisionDev_t *firstDevice = pDev1;
705 const OCProvisionDev_t *secondDevice = pDev2;
707 OicSecCred_t *firstCred = NULL;
708 OicSecCred_t *secondCred = NULL;
709 OCStackResult res = PMGeneratePairWiseCredentials(type, keySize, &provTooldeviceID,
710 &firstDevice->doxm->deviceID, &secondDevice->doxm->deviceID,
711 &firstCred, &secondCred);
712 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
713 OIC_LOG(INFO, TAG, "Credentials generated successfully");
714 CredentialData_t *credData =
715 (CredentialData_t *) OICCalloc(1, sizeof(CredentialData_t));
716 if (NULL == credData)
720 OIC_LOG(ERROR, TAG, "Memory allocation problem");
721 return OC_STACK_NO_MEMORY;
723 credData->deviceInfo1 = firstDevice;
724 credData->deviceInfo2 = secondDevice;
725 credData->credInfo = secondCred;
727 credData->credInfoFirst = firstCred;
728 credData->numOfResults = 0;
729 credData->resultCallback = resultCallback;
730 // first call to provision creds to device1.
731 // second call to provision creds to device2.
734 (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
735 if (NULL == credData->resArr)
740 OIC_LOG(ERROR, TAG, "Memory allocation problem");
741 return OC_STACK_NO_MEMORY;
743 res = provisionCredentials(firstCred, firstDevice, credData, &provisionCredentialCB1);
744 if (OC_STACK_OK != res)
746 DeleteCredList(firstCred);
747 DeleteCredList(secondCred);
748 OICFree(credData->resArr);
751 OIC_LOG_V(INFO, TAG, "provisionCredentials returned: %d",res);
752 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
756 case SIGNED_ASYMMETRIC_KEY:
758 const OCProvisionDev_t *firstDevice = pDev1;
759 OicSecCred_t *cred = NULL;
760 OCStackResult res = PMGenerateCertificateCredentials(&provTooldeviceID,
761 &firstDevice->doxm->deviceID,&cred);
762 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
763 OIC_LOG(INFO, TAG, "Certificate credentials generated successfully");
764 CertData_t *certData = (CertData_t *) OICCalloc(1, sizeof(CertData_t));
765 if (NULL == certData)
768 OIC_LOG(ERROR, TAG, "Memory allocation problem");
769 return OC_STACK_NO_MEMORY;
772 certData->deviceInfo = firstDevice;
774 certData->credInfo = cred;
775 certData->numOfResults = 0;
776 certData->resultCallback = resultCallback;
778 certData->resArr = (OCProvisionResult_t*)OICCalloc(1, sizeof(OCProvisionResult_t));
779 if (NULL == certData->resArr)
781 DeleteCredList(cred);
783 OIC_LOG(ERROR, TAG, "Memory allocation problem");
784 return OC_STACK_NO_MEMORY;
787 res = provisionCertCred(cred, firstDevice, certData, &provisionCertCB);
788 if (OC_STACK_OK != res)
790 OICFree(certData->resArr);
793 DeleteCredList(cred);
794 OIC_LOG_V(INFO, TAG, "provisionCertCredentials returned: %d",res);
801 OIC_LOG(ERROR, TAG, "Invalid option.");
802 return OC_STACK_INVALID_PARAM;
805 return OC_STACK_ERROR;
809 * Internal Function to store results in result array during ACL provisioning.
811 static void registerResultForACLProvisioning(ACLData_t *aclData,
812 OCStackResult stackresult)
814 OIC_LOG_V(INFO, TAG, "Inside registerResultForACLProvisioning aclData->numOfResults is %d\n",
815 aclData->numOfResults);
816 memcpy(aclData->resArr[(aclData->numOfResults)].deviceId.id,
817 aclData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
818 aclData->resArr[(aclData->numOfResults)].res = stackresult;
819 ++(aclData->numOfResults);
823 * Callback handler of SRPProvisionACL.
825 * @param[in] ctx ctx value passed to callback from calling function.
826 * @param[in] UNUSED handle to an invocation
827 * @param[in] clientResponse Response from queries to remote servers.
828 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
829 * and OC_STACK_KEEP_TRANSACTION to keep it.
831 static OCStackApplicationResult SRPProvisionACLCB(void *ctx, OCDoHandle UNUSED,
832 OCClientResponse *clientResponse)
834 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionACLCB.");
836 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
837 ACLData_t *aclData = (ACLData_t*)ctx;
838 OCProvisionResultCB resultCallback = aclData->resultCallback;
842 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
844 registerResultForACLProvisioning(aclData, OC_STACK_RESOURCE_CREATED);
845 ((OCProvisionResultCB)(resultCallback))(aclData->ctx, aclData->numOfResults,
848 OICFree(aclData->resArr);
850 return OC_STACK_DELETE_TRANSACTION;
853 registerResultForACLProvisioning(aclData, OC_STACK_ERROR);
854 ((OCProvisionResultCB)(resultCallback))(aclData->ctx, aclData->numOfResults,
857 OIC_LOG_V(ERROR, TAG, "SRPProvisionACLCB received Null clientResponse");
858 OICFree(aclData->resArr);
860 return OC_STACK_DELETE_TRANSACTION;
863 OCStackResult SRPProvisionACL(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
864 OicSecAcl_t *acl, OCProvisionResultCB resultCallback)
866 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
867 VERIFY_NON_NULL(TAG, acl, ERROR, OC_STACK_INVALID_PARAM);
868 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
870 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
873 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
874 return OC_STACK_NO_MEMORY;
876 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
877 if(OC_STACK_OK != AclToCBORPayload(acl, &secPayload->securityData, &secPayload->payloadSize))
879 OCPayloadDestroy((OCPayload *)secPayload);
880 OIC_LOG(ERROR, TAG, "Failed to AclToCBORPayload");
881 return OC_STACK_NO_MEMORY;
883 OIC_LOG(DEBUG, TAG, "Created payload for ACL:");
884 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
886 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
887 if(!PMGenerateQuery(true,
888 selectedDeviceInfo->endpoint.addr,
889 selectedDeviceInfo->securePort,
890 selectedDeviceInfo->connType,
891 query, sizeof(query), OIC_RSRC_ACL_URI))
893 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
894 return OC_STACK_ERROR;
896 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
898 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
899 cbData.cb = &SRPProvisionACLCB;
900 ACLData_t *aclData = (ACLData_t *) OICCalloc(1, sizeof(ACLData_t));
903 OCPayloadDestroy((OCPayload *)secPayload);
904 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
905 return OC_STACK_NO_MEMORY;
907 aclData->deviceInfo = selectedDeviceInfo;
908 aclData->resultCallback = resultCallback;
909 aclData->numOfResults=0;
911 // call to provision ACL to device1.
913 aclData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
914 if (aclData->resArr == NULL)
917 OCPayloadDestroy((OCPayload *)secPayload);
918 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
919 return OC_STACK_NO_MEMORY;
921 cbData.context = (void *)aclData;
923 OCMethod method = OC_REST_POST;
924 OCDoHandle handle = NULL;
925 OIC_LOG(DEBUG, TAG, "Sending ACL info to resource server");
926 OCStackResult ret = OCDoResource(&handle, method, query,
927 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
928 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
929 if (ret != OC_STACK_OK)
931 OICFree(aclData->resArr);
934 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
939 * Internal Function to store results in result array during Direct-Pairing provisioning.
941 static void registerResultForDirectPairingProvisioning(PconfData_t *pconfData,
942 OCStackResult stackresult)
944 OIC_LOG_V(INFO, TAG, "Inside registerResultForDirectPairingProvisioning "
945 "pconfData->numOfResults is %d\n", pconfData->numOfResults);
946 memcpy(pconfData->resArr[(pconfData->numOfResults)].deviceId.id,
947 pconfData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
948 pconfData->resArr[(pconfData->numOfResults)].res = stackresult;
949 ++(pconfData->numOfResults);
953 * Callback handler of SRPProvisionDirectPairing.
955 * @param[in] ctx ctx value passed to callback from calling function.
956 * @param[in] UNUSED handle to an invocation
957 * @param[in] clientResponse Response from queries to remote servers.
958 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
959 * and OC_STACK_KEEP_TRANSACTION to keep it.
961 static OCStackApplicationResult SRPProvisionDirectPairingCB(void *ctx, OCDoHandle UNUSED,
962 OCClientResponse *clientResponse)
964 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionDirectPairingCB.");
966 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
967 PconfData_t *pconfData = (PconfData_t*)ctx;
968 OCProvisionResultCB resultCallback = pconfData->resultCallback;
972 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
974 registerResultForDirectPairingProvisioning(pconfData, OC_STACK_OK);
975 ((OCProvisionResultCB)(resultCallback))(pconfData->ctx, pconfData->numOfResults,
978 OICFree(pconfData->resArr);
980 return OC_STACK_DELETE_TRANSACTION;
983 registerResultForDirectPairingProvisioning(pconfData, OC_STACK_ERROR);
984 ((OCProvisionResultCB)(resultCallback))(pconfData->ctx, pconfData->numOfResults,
987 OIC_LOG_V(ERROR, TAG, "SRPProvisionDirectPairingCB received Null clientResponse");
988 OICFree(pconfData->resArr);
990 return OC_STACK_DELETE_TRANSACTION;
993 OCStackResult SRPProvisionDirectPairing(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
994 OicSecPconf_t *pconf, OCProvisionResultCB resultCallback)
996 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
997 VERIFY_NON_NULL(TAG, pconf, ERROR, OC_STACK_INVALID_PARAM);
998 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
1000 // check direct-pairing capability
1001 if (true != selectedDeviceInfo->doxm->dpc)
1003 OIC_LOG(ERROR, TAG, "Resouce server does not have Direct-Pairing Capability ");
1004 return OC_STACK_UNAUTHORIZED_REQ;
1007 OicUuid_t provTooldeviceID = {.id={0}};
1008 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
1010 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
1011 return OC_STACK_ERROR;
1013 memcpy(&pconf->rownerID, &provTooldeviceID, sizeof(OicUuid_t));
1015 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1018 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1019 return OC_STACK_NO_MEMORY;
1021 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1023 if (OC_STACK_OK != PconfToCBORPayload(pconf, &(secPayload->securityData),
1024 &(secPayload->payloadSize)))
1026 OCPayloadDestroy((OCPayload*)secPayload);
1027 OIC_LOG(ERROR, TAG, "Failed to PconfToCborPayload");
1028 return OC_STACK_NO_MEMORY;
1030 OIC_LOG(DEBUG, TAG, "Created payload for pconf set");
1031 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1034 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1035 if(!PMGenerateQuery(true,
1036 selectedDeviceInfo->endpoint.addr,
1037 selectedDeviceInfo->securePort,
1038 selectedDeviceInfo->connType,
1039 query, sizeof(query), OIC_RSRC_PCONF_URI))
1041 OIC_LOG(ERROR, TAG, "SRPProvisionDirectPairing : Failed to generate query");
1042 return OC_STACK_ERROR;
1044 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1046 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1047 cbData.cb = &SRPProvisionDirectPairingCB;
1048 PconfData_t *pconfData = (PconfData_t *) OICCalloc(1, sizeof(PconfData_t));
1049 if (NULL == pconfData)
1051 OCPayloadDestroy((OCPayload*)secPayload);
1052 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1053 return OC_STACK_NO_MEMORY;
1055 pconfData->deviceInfo = selectedDeviceInfo;
1056 pconfData->resultCallback = resultCallback;
1057 pconfData->numOfResults=0;
1058 pconfData->ctx = ctx;
1059 // call to provision PCONF to device1.
1060 int noOfRiCalls = 1;
1061 pconfData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
1062 if (NULL == pconfData->resArr)
1065 OCPayloadDestroy((OCPayload*)secPayload);
1066 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1067 return OC_STACK_NO_MEMORY;
1069 cbData.context = (void *)pconfData;
1071 OCMethod method = OC_REST_POST;
1072 OCDoHandle handle = NULL;
1073 OIC_LOG(DEBUG, TAG, "Sending PCONF info to resource server");
1074 OCStackResult ret = OCDoResource(&handle, method, query,
1075 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
1076 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1077 if (OC_STACK_OK != ret)
1079 OICFree(pconfData->resArr);
1082 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
1086 static void DeleteUnlinkData_t(UnlinkData_t *unlinkData)
1090 OICFree(unlinkData->unlinkDev);
1091 OICFree(unlinkData->unlinkRes);
1092 OICFree(unlinkData);
1096 static void registerResultForUnlinkDevices(UnlinkData_t *unlinkData, OCStackResult stackresult,
1099 if (NULL != unlinkData)
1101 OIC_LOG_V(INFO, TAG, "Inside registerResultForUnlinkDevices unlinkData->numOfResults is %d\n",
1102 unlinkData->numOfResults);
1103 OIC_LOG_V(INFO, TAG, "Stack result :: %d", stackresult);
1105 OicUuid_t *pUuid = &unlinkData->unlinkRes[(unlinkData->numOfResults)].deviceId;
1107 // Set result in the result array according to the position (devNum).
1108 if (idx != IDX_DB_UPDATE_RES)
1110 memcpy(pUuid->id, unlinkData->unlinkDev[idx].doxm->deviceID.id, sizeof(pUuid->id));
1113 { // When deivce ID is 000... this means it's the result of database update.
1114 memset(pUuid->id, 0, sizeof(pUuid->id));
1116 unlinkData->unlinkRes[(unlinkData->numOfResults)].res = stackresult;
1117 ++(unlinkData->numOfResults);
1118 OIC_LOG (INFO, TAG, "Out registerResultForUnlinkDevices");
1122 static OCStackResult SendDeleteCredentialRequest(void* ctx,
1123 OCClientResponseHandler respHandler,
1124 const OCProvisionDev_t* revokedDev,
1125 const OCProvisionDev_t* destDev)
1127 OIC_LOG(DEBUG, TAG, "IN SendDeleteCredentialRequest");
1129 if (NULL == ctx || NULL == respHandler || NULL == revokedDev || NULL == destDev)
1131 return OC_STACK_INVALID_PARAM;
1135 OCStackResult ret = ConvertUuidToStr(&revokedDev->doxm->deviceID, &subID);
1136 if(OC_STACK_OK != ret)
1138 OIC_LOG(ERROR, TAG, "SendDeleteCredentialRequest : Failed to canonical UUID encoding");
1139 return OC_STACK_ERROR;
1142 char reqBuf[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1144 //coaps://0.0.0.0:5684/oic/sec/cred?subjectid=(Canonical ENCODED UUID)
1145 snRet = snprintf(reqBuf, sizeof(reqBuf), SRP_FORM_DELETE_CREDENTIAL, destDev->endpoint.addr,
1146 destDev->securePort, OIC_RSRC_CRED_URI, OIC_JSON_SUBJECTID_NAME, subID);
1150 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error (snprintf) %d\n", snRet);
1151 return OC_STACK_ERROR;
1153 else if ((size_t)snRet >= sizeof(reqBuf))
1155 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Truncated (snprintf) %d\n", snRet);
1156 return OC_STACK_ERROR;
1159 OCCallbackData cbData;
1160 memset(&cbData, 0, sizeof(cbData));
1161 cbData.context = ctx;
1162 cbData.cb = respHandler;
1164 OIC_LOG_V(INFO, TAG, "URI: %s",reqBuf);
1166 OIC_LOG(DEBUG, TAG, "Sending remove credential request to resource server");
1168 ret = OCDoResource(NULL, OC_REST_DELETE, reqBuf,
1169 &destDev->endpoint, NULL,
1170 CT_ADAPTER_IP, OC_HIGH_QOS, &cbData, NULL, 0);
1171 if (OC_STACK_OK != ret)
1173 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error in OCDoResource %d", ret);
1175 OIC_LOG(DEBUG, TAG, "OUT SendDeleteCredentialRequest");
1180 static OCStackResult SendDeleteACLRequest(void* ctx,
1181 OCClientResponseHandler respHandler,
1182 const OCProvisionDev_t* revokedDev,
1183 const OCProvisionDev_t* destDev)
1185 OIC_LOG(DEBUG, TAG, "IN SendDeleteACLRequest");
1187 if (NULL == ctx || NULL == respHandler || NULL == revokedDev || NULL == destDev)
1189 return OC_STACK_INVALID_PARAM;
1193 OCStackResult ret = ConvertUuidToStr(&revokedDev->doxm->deviceID, &subID);
1194 if(OC_STACK_OK != ret)
1196 OIC_LOG(ERROR, TAG, "SendDeleteACLRequest : Failed to canonical UUID encoding");
1197 return OC_STACK_ERROR;
1200 char reqBuf[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1202 //coaps://0.0.0.0:5684/oic/sec/acl?subjectuuid=(Canonical ENCODED UUID)
1203 snRet = snprintf(reqBuf, sizeof(reqBuf), SRP_FORM_DELETE_CREDENTIAL, destDev->endpoint.addr,
1204 destDev->securePort, OIC_RSRC_ACL_URI, OIC_JSON_SUBJECTID_NAME, subID);
1208 OIC_LOG_V(ERROR, TAG, "SendDeleteACLRequest : Error (snprintf) %d\n", snRet);
1209 return OC_STACK_ERROR;
1211 else if ((size_t)snRet >= sizeof(reqBuf))
1213 OIC_LOG_V(ERROR, TAG, "SendDeleteACLRequest : Truncated (snprintf) %d\n", snRet);
1214 return OC_STACK_ERROR;
1217 OCCallbackData cbData;
1218 memset(&cbData, 0, sizeof(cbData));
1219 cbData.context = ctx;
1220 cbData.cb = respHandler;
1222 OIC_LOG_V(INFO, TAG, "URI: %s",reqBuf);
1224 OIC_LOG(DEBUG, TAG, "Sending remove ACL request to resource server");
1226 ret = OCDoResource(NULL, OC_REST_DELETE, reqBuf,
1227 &destDev->endpoint, NULL,
1228 CT_ADAPTER_IP, OC_HIGH_QOS, &cbData, NULL, 0);
1229 if (OC_STACK_OK != ret)
1231 OIC_LOG_V(ERROR, TAG, "SendDeleteACLRequest : Error in OCDoResource %d", ret);
1233 OIC_LOG(DEBUG, TAG, "OUT SendDeleteACLRequest");
1239 * Callback handler of unlink second device.
1241 * @param[in] ctx ctx value passed to callback from calling function.
1242 * @param[in] handle handle to an invocation
1243 * @param[in] clientResponse Response from queries to remote servers.
1244 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1245 * OC_STACK_KEEP_TRANSACTION to keep it.
1247 static OCStackApplicationResult SRPUnlinkDevice2CB(void *unlinkCtx, OCDoHandle handle,
1248 OCClientResponse *clientResponse)
1251 OIC_LOG(DEBUG, TAG, "IN SRPUnlinkDevice2CB");
1252 VERIFY_NON_NULL(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1253 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1257 OIC_LOG(DEBUG, TAG, "Valid client response for device 2");
1258 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_SECOND_DEVICE_RES);
1260 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1262 OIC_LOG(DEBUG, TAG, "Credential of device2 revoked");
1266 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 2");
1267 unlinkData->resultCallback(unlinkData->ctx,
1268 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1274 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1275 IDX_SECOND_DEVICE_RES);
1276 unlinkData->resultCallback(unlinkData->ctx,
1277 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1278 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice2CB received Null clientResponse");
1282 //Update provisioning DB when succes case.
1283 if (OC_STACK_OK != PDMUnlinkDevices(&unlinkData->unlinkDev[0].doxm->deviceID,
1284 &unlinkData->unlinkDev[1].doxm->deviceID))
1286 OIC_LOG(FATAL, TAG, "All requests are successfully done but update provisioning DB FAILED.");
1287 registerResultForUnlinkDevices(unlinkData, OC_STACK_INCONSISTENT_DB, IDX_DB_UPDATE_RES);
1288 unlinkData->resultCallback(unlinkData->ctx,
1289 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1292 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults, unlinkData->unlinkRes,
1296 DeleteUnlinkData_t(unlinkData);
1297 OIC_LOG(DEBUG, TAG, "OUT SRPUnlinkDevice2CB");
1298 return OC_STACK_DELETE_TRANSACTION;
1303 * Callback handler of unlink first device.
1305 * @param[in] ctx ctx value passed to callback from calling function.
1306 * @param[in] handle handle to an invocation
1307 * @param[in] clientResponse Response from queries to remote servers.
1308 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1309 * OC_STACK_KEEP_TRANSACTION to keep it.
1311 static OCStackApplicationResult SRPUnlinkDevice1CB(void *unlinkCtx, OCDoHandle handle,
1312 OCClientResponse *clientResponse)
1314 OIC_LOG_V(INFO, TAG, "Inside SRPUnlinkDevice1CB ");
1315 VERIFY_NON_NULL(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1316 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1321 OIC_LOG(DEBUG, TAG, "Valid client response for device 1");
1322 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_FIRST_DEVICE_RES);
1324 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1326 OIC_LOG(DEBUG, TAG, "Credential of device 1 is revoked");
1328 // Second revocation request to second device.
1329 OCStackResult res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice2CB,
1330 &unlinkData->unlinkDev[0],
1331 &unlinkData->unlinkDev[1] /*Dest*/);
1332 OIC_LOG_V(DEBUG, TAG, "Credential revocation request device 2, result :: %d",res);
1333 if (OC_STACK_OK != res)
1335 OIC_LOG(ERROR, TAG, "Error while sending revocation request for device 2");
1336 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1337 IDX_SECOND_DEVICE_RES);
1338 unlinkData->resultCallback(unlinkData->ctx,
1339 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1344 OIC_LOG(DEBUG, TAG, "Request for credential revocation successfully sent");
1345 return OC_STACK_DELETE_TRANSACTION;
1350 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 1");
1352 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults,
1353 unlinkData->unlinkRes, true);
1359 OIC_LOG(DEBUG, TAG, "Invalid response from server");
1360 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1361 IDX_FIRST_DEVICE_RES );
1362 unlinkData->resultCallback(unlinkData->ctx,
1363 unlinkData->numOfResults, unlinkData->unlinkRes,
1365 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice1CB received Null clientResponse");
1369 OIC_LOG_V(INFO, TAG, "Out SRPUnlinkDevice1CB");
1370 DeleteUnlinkData_t(unlinkData);
1371 return OC_STACK_DELETE_TRANSACTION;
1375 * Function to unlink devices.
1376 * This function will remove the credential & relationship between the two devices.
1378 * @param[in] ctx Application context would be returned in result callback
1379 * @param[in] pTargetDev1 first device information to be unlinked.
1380 * @param[in] pTargetDev2 second device information to be unlinked.
1381 * @param[in] resultCallback callback provided by API user, callback will be called when
1382 * device unlink is finished.
1383 * @return OC_STACK_OK in case of success and other value otherwise.
1385 OCStackResult SRPUnlinkDevices(void* ctx,
1386 const OCProvisionDev_t* pTargetDev1,
1387 const OCProvisionDev_t* pTargetDev2,
1388 OCProvisionResultCB resultCallback)
1390 OIC_LOG(INFO, TAG, "IN SRPUnlinkDevices");
1392 if (!pTargetDev1 || !pTargetDev2 || !pTargetDev1->doxm || !pTargetDev2->doxm)
1394 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL parameters");
1395 return OC_STACK_INVALID_PARAM;
1397 if (!resultCallback)
1399 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL Callback");
1400 return OC_STACK_INVALID_CALLBACK;
1402 if (0 == memcmp(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID, sizeof(OicUuid_t)))
1404 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : Same device ID");
1405 return OC_STACK_INVALID_PARAM;
1408 OIC_LOG(INFO, TAG, "Unlinking following devices: ");
1409 PMPrintOCProvisionDev(pTargetDev1);
1410 PMPrintOCProvisionDev(pTargetDev2);
1412 // Mark the link status stale
1413 OCStackResult res = PDMSetLinkStale(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID);
1414 if (OC_STACK_OK != res)
1416 OIC_LOG(FATAL, TAG, "unable to update DB. Try again.");
1420 UnlinkData_t* unlinkData = (UnlinkData_t*)OICCalloc(1, sizeof(UnlinkData_t));
1421 VERIFY_NON_NULL(TAG, unlinkData, ERROR, OC_STACK_NO_MEMORY);
1423 //Initialize unlink data
1424 unlinkData->ctx = ctx;
1425 unlinkData->unlinkDev = (OCProvisionDev_t*)OICCalloc(2, sizeof(OCProvisionDev_t));
1426 if (NULL == unlinkData->unlinkDev)
1428 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1429 res = OC_STACK_NO_MEMORY;
1433 unlinkData->unlinkRes = (OCProvisionResult_t*)OICCalloc(3, sizeof(OCProvisionResult_t));
1434 if (NULL == unlinkData->unlinkRes)
1436 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1437 res = OC_STACK_NO_MEMORY;
1441 memcpy(&unlinkData->unlinkDev[0], pTargetDev1, sizeof(OCProvisionDev_t));
1442 memcpy(&unlinkData->unlinkDev[1], pTargetDev2, sizeof(OCProvisionDev_t));
1444 unlinkData->numOfResults = 0;
1445 unlinkData->resultCallback = resultCallback;
1447 res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice1CB,
1448 &unlinkData->unlinkDev[1], &unlinkData->unlinkDev[0]);
1449 if (OC_STACK_OK != res)
1451 OIC_LOG(ERROR, TAG, "SRPUnlinkDevices : SendDeleteCredentialRequest failed");
1458 OIC_LOG(INFO, TAG, "OUT SRPUnlinkDevices");
1459 DeleteUnlinkData_t(unlinkData);
1463 static void DeleteRemoveData_t(RemoveData_t* pRemoveData)
1467 OICFree(pRemoveData->revokeTargetDev);
1468 OCDeleteDiscoveredDevices(pRemoveData->linkedDevList);
1469 OICFree(pRemoveData->removeRes);
1470 OICFree(pRemoveData);
1474 static void registerResultForRemoveDevice(RemoveData_t *removeData, OicUuid_t *pLinkedDevId,
1475 OCStackResult stackresult, bool hasError)
1477 OIC_LOG_V(INFO, TAG, "Inside registerResultForRemoveDevice removeData->numOfResults is %zu\n",
1478 removeData->numOfResults + 1);
1481 memcpy(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1482 &pLinkedDevId->id, sizeof(pLinkedDevId->id));
1486 memset(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1487 0, sizeof(pLinkedDevId->id) );
1489 removeData->removeRes[(removeData->numOfResults)].res = stackresult;
1490 removeData->hasError = hasError;
1491 ++(removeData->numOfResults);
1493 // If we get suffcient result from linked devices, we have to call user callback and do free
1494 if (removeData->sizeOfResArray == removeData->numOfResults)
1496 if(!removeData->hasError)
1498 // Remove device info from prvisioning database
1499 if (OC_STACK_OK != PDMDeleteDevice(&removeData->revokeTargetDev->doxm->deviceID))
1501 OIC_LOG(ERROR, TAG, "ResultForRemoveDevice : Failed to remove device in PDM.");
1502 removeData->hasError = true;
1505 removeData->resultCallback(removeData->ctx, removeData->numOfResults, removeData->removeRes,
1506 removeData->hasError);
1507 DeleteRemoveData_t(removeData);
1511 static void registerResultForResetDevice(RemoveData_t *removeData, OicUuid_t *pLinkedDevId,
1512 OCStackResult stackresult, bool hasError)
1514 OIC_LOG_V(INFO, TAG, "Inside registerResultForResetDevice removeData->numOfResults is %zu\n",
1515 removeData->numOfResults + 1);
1518 memcpy(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1519 &pLinkedDevId->id, sizeof(pLinkedDevId->id));
1523 memset(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1524 0, sizeof(pLinkedDevId->id) );
1526 removeData->removeRes[(removeData->numOfResults)].res = stackresult;
1527 removeData->hasError = hasError;
1528 ++(removeData->numOfResults);
1530 // If we get suffcient result from linked devices, we have to call user callback and do free
1531 if (removeData->sizeOfResArray == removeData->numOfResults)
1533 removeData->resultCallback(removeData->ctx, removeData->numOfResults, removeData->removeRes,
1534 removeData->hasError);
1535 DeleteRemoveData_t(removeData);
1540 * Callback handler of unlink first device.
1542 * @param[in] ctx ctx value passed to callback from calling function.
1543 * @param[in] handle handle to an invocation
1544 * @param[in] clientResponse Response from queries to remote servers.
1545 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1546 * and OC_STACK_KEEP_TRANSACTION to keep it.
1548 static OCStackApplicationResult SRPRemoveDeviceCB(void *delDevCtx, OCDoHandle handle,
1549 OCClientResponse *clientResponse)
1551 //Update the delete credential into delete device context
1552 //Save the deleted status in delDevCtx
1554 OIC_LOG_V(INFO, TAG, "Inside SRPRemoveDeviceCB.");
1555 VERIFY_NON_NULL(TAG, delDevCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1556 OCStackResult res = OC_STACK_ERROR;
1558 RemoveData_t* removeData = (RemoveData_t*)delDevCtx;
1562 OicUuid_t revDevUuid = {.id={0}};
1563 if(UUID_LENGTH == clientResponse->identity.id_length)
1565 memcpy(revDevUuid.id, clientResponse->identity.id, sizeof(revDevUuid.id));
1566 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1568 res = PDMUnlinkDevices(&removeData->revokeTargetDev->doxm->deviceID, &revDevUuid);
1569 if (OC_STACK_OK != res)
1571 OIC_LOG(ERROR, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1572 registerResultForRemoveDevice(removeData, &revDevUuid,
1573 OC_STACK_INCONSISTENT_DB, true);
1575 return OC_STACK_DELETE_TRANSACTION;
1578 registerResultForRemoveDevice(removeData, &revDevUuid,
1579 OC_STACK_RESOURCE_DELETED, false);
1583 registerResultForRemoveDevice(removeData, &revDevUuid,
1584 clientResponse->result, true);
1585 OIC_LOG(ERROR, TAG, "Unexpected result from DELETE credential request!");
1590 OIC_LOG_V(WARNING, TAG, "Incorrect length of device UUID was sent from %s:%d",
1591 clientResponse->devAddr.addr, clientResponse->devAddr.port);
1593 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1596 * Since server's credential was deleted,
1597 * register result as OC_STACK_INCONSISTENT_DB with NULL UUID.
1599 OIC_LOG_V(ERROR, TAG, "But server's credential was deleted.");
1600 registerResultForRemoveDevice(removeData, NULL, OC_STACK_INCONSISTENT_DB, true);
1604 registerResultForRemoveDevice(removeData, NULL, clientResponse->result, true);
1610 registerResultForRemoveDevice(removeData, NULL, OC_STACK_ERROR, true);
1611 OIC_LOG(ERROR, TAG, "SRPRemoveDevices received Null clientResponse");
1615 return OC_STACK_DELETE_TRANSACTION;
1619 * Callback handler of reset device.
1621 * @param[in] ctx ctx value passed to callback from calling function.
1622 * @param[in] handle handle to an invocation
1623 * @param[in] clientResponse Response from queries to remote servers.
1624 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1625 * and OC_STACK_KEEP_TRANSACTION to keep it.
1627 static OCStackApplicationResult SRPSyncDeviceCredCB(void *delDevCtx, OCDoHandle handle,
1628 OCClientResponse *clientResponse)
1630 //Update the delete credential into delete device context
1631 //Save the deleted status in delDevCtx
1633 OIC_LOG_V(INFO, TAG, "Inside SRPSyncDeviceCredCB.");
1634 VERIFY_NON_NULL(TAG, delDevCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1635 OCStackResult res = OC_STACK_ERROR;
1637 RemoveData_t* removeData = (RemoveData_t*)delDevCtx;
1638 OCProvisionDev_t * pTargetDev = PMCloneOCProvisionDev(removeData->revokeTargetDev);
1639 OCProvisionResultCB resultCallback = removeData->resultCallback;
1642 OicUuid_t revDevUuid = {.id={0}};
1643 if(UUID_LENGTH == clientResponse->identity.id_length)
1645 memcpy(revDevUuid.id, clientResponse->identity.id, sizeof(revDevUuid.id));
1646 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1648 res = PDMUnlinkDevices(&removeData->revokeTargetDev->doxm->deviceID, &revDevUuid);
1649 if (OC_STACK_OK != res)
1651 OIC_LOG(ERROR, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1652 registerResultForResetDevice(removeData, &revDevUuid,
1653 OC_STACK_INCONSISTENT_DB, true);
1655 return OC_STACK_DELETE_TRANSACTION;
1658 registerResultForResetDevice(removeData, &revDevUuid,
1659 OC_STACK_RESOURCE_DELETED, false);
1663 registerResultForResetDevice(removeData, &revDevUuid,
1664 clientResponse->result, false);
1665 OIC_LOG(ERROR, TAG, "Unexpected result from DELETE credential request!");
1670 OIC_LOG_V(WARNING, TAG, "Incorrect length of device UUID was sent from %s:%d",
1671 clientResponse->devAddr.addr, clientResponse->devAddr.port);
1673 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1676 * Since server's credential was deleted,
1677 * register result as OC_STACK_INCONSISTENT_DB with NULL UUID.
1679 OIC_LOG_V(ERROR, TAG, "But server's credential was deleted.");
1680 registerResultForResetDevice(removeData, NULL, OC_STACK_INCONSISTENT_DB, true);
1684 registerResultForResetDevice(removeData, NULL, clientResponse->result, true);
1690 registerResultForResetDevice(removeData, NULL, OC_STACK_ERROR, true);
1691 OIC_LOG(ERROR, TAG, "SRPSyncDevice received Null clientResponse");
1694 SRPResetDevice(pTargetDev, resultCallback);
1696 return OC_STACK_DELETE_TRANSACTION;
1700 * Callback handler of reset device sync-up
1702 * @param[in] ctx ctx value passed to callback from calling function.
1703 * @param[in] handle handle to an invocation
1704 * @param[in] clientResponse Response from queries to remote servers.
1705 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1706 * and OC_STACK_KEEP_TRANSACTION to keep it.
1708 static OCStackApplicationResult SRPSyncDeviceACLCB(void *ctx, OCDoHandle handle,
1709 OCClientResponse *clientResponse)
1711 return OC_STACK_DELETE_TRANSACTION;
1715 * Callback handler of device remote reset.
1717 * @param[in] ctx ctx value passed to callback from calling function.
1718 * @param[in] handle handle to an invocation
1719 * @param[in] clientResponse Response from queries to remote servers.
1720 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1721 * and OC_STACK_KEEP_TRANSACTION to keep it.
1723 static OCStackApplicationResult SRPResetDeviceCB(void *ctx, OCDoHandle handle,
1724 OCClientResponse *clientResponse)
1726 OIC_LOG(DEBUG, TAG, "IN SRPResetDeviceCB");
1727 if(OC_STACK_OK == clientResponse->result)
1729 OIC_LOG(DEBUG, TAG, "Change Target Device Pstat Cm SUCCEEDED");
1732 // Delete Cred and ACL related to the target device.
1733 const OicSecCred_t *cred = NULL;
1734 OCProvisionDev_t * pTargetDev = (OCProvisionDev_t *)ctx;
1735 cred = GetCredResourceData(&pTargetDev->doxm->deviceID);
1738 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to get credential of target device.");
1742 OCStackResult res = RemoveCredential(&cred->subject);
1743 if (res != OC_STACK_RESOURCE_DELETED && res != OC_STACK_NO_RESOURCE)
1745 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to remove credential.");
1749 res = RemoveACE(&cred->subject, NULL);
1750 if (res != OC_STACK_RESOURCE_DELETED && res != OC_STACK_NO_RESOURCE)
1752 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to remove ACL.");
1755 if (OC_STACK_OK != PDMDeleteDevice(&pTargetDev->doxm->deviceID))
1757 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to delete device from PDM");
1760 //Close the DTLS session of the reset device.
1761 CAEndpoint_t* endpoint = (CAEndpoint_t *)&clientResponse->devAddr;
1762 CAResult_t caResult = CACloseDtlsSession(endpoint);
1763 if(CA_STATUS_OK != caResult)
1765 OIC_LOG_V(WARNING, TAG, "OCResetDevice : Failed to close DTLS session : %d", caResult);
1769 * If there is no linked device, PM does not send any request.
1770 * So we should directly invoke the result callback to inform the result of OCResetDevice.
1772 if(OC_STACK_NO_RESOURCE == res)
1778 OICFree(pTargetDev);
1779 return OC_STACK_DELETE_TRANSACTION;
1783 static OCStackResult GetListofDevToReqDeleteCred(const OCProvisionDev_t* pRevokeTargetDev,
1784 OCProvisionDev_t* pOwnedDevList,
1785 OCUuidList_t* pLinkedUuidList,
1786 OCProvisionDev_t** ppLinkedDevList,
1787 size_t *numOfLinkedDev)
1789 // pOwnedDevList could be NULL. It means no alived and owned device now.
1790 if (pRevokeTargetDev == NULL || pLinkedUuidList == NULL ||\
1791 ppLinkedDevList == NULL || numOfLinkedDev == NULL)
1793 return OC_STACK_INVALID_PARAM;
1797 OCUuidList_t *curUuid = NULL, *tmpUuid = NULL;
1798 LL_FOREACH_SAFE(pLinkedUuidList, curUuid, tmpUuid)
1800 // Mark the link status stale.
1801 OCStackResult res = PDMSetLinkStale(&curUuid->dev, &pRevokeTargetDev->doxm->deviceID);
1802 if (OC_STACK_OK != res)
1804 OIC_LOG(FATAL, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1805 return OC_STACK_INCONSISTENT_DB;
1810 // If this linked device is alive (power-on), add the deivce to the list.
1811 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
1812 LL_FOREACH_SAFE(pOwnedDevList, curDev, tmpDev)
1814 if (memcmp(curDev->doxm->deviceID.id, curUuid->dev.id, sizeof(curUuid->dev.id)) == 0)
1816 OCProvisionDev_t* targetDev = PMCloneOCProvisionDev(curDev);
1817 if (NULL == targetDev)
1819 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Cloning OCProvisionDev_t Failed.");
1820 return OC_STACK_NO_MEMORY;
1823 LL_PREPEND(*ppLinkedDevList, targetDev);
1830 *numOfLinkedDev = cnt;
1835 * Function to device revocation
1836 * This function will remove credential of target device from all devices in subnet.
1838 * @param[in] ctx Application context would be returned in result callback
1839 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
1840 * @param[in] pTargetDev Device information to be revoked.
1841 * @param[in] resultCallback callback provided by API user, callback will be called when
1842 * credential revocation is finished.
1843 * @return OC_STACK_OK in case of success and other value otherwise.
1844 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
1845 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
1847 OCStackResult SRPRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
1848 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
1850 OIC_LOG(INFO, TAG, "IN SRPRemoveDevice");
1852 if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
1854 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL parameters");
1855 return OC_STACK_INVALID_PARAM;
1857 if (!resultCallback)
1859 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL Callback");
1860 return OC_STACK_INVALID_CALLBACK;
1863 // Declare variables in here to handle error cases with goto statement.
1864 OCProvisionDev_t* pOwnedDevList = NULL;
1865 OCProvisionDev_t* pLinkedDevList = NULL;
1866 RemoveData_t* removeData = NULL;
1868 //1. Find all devices that has a credential of the revoked device
1869 OCUuidList_t* pLinkedUuidList = NULL;
1870 size_t numOfDevices = 0;
1871 OCStackResult res = OC_STACK_ERROR;
1872 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
1873 if (OC_STACK_OK != res)
1875 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to get linked devices information");
1878 // if there is no related device, we can skip further process.
1879 if (0 == numOfDevices)
1881 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No linked device found.");
1882 res = OC_STACK_CONTINUE;
1886 //2. Find owned device from the network
1887 res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
1888 if (OC_STACK_OK != res)
1890 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to PMDeviceDiscovery");
1894 //3. Make a list of devices to send DELETE credential request
1895 // by comparing owned devices from provisioning database with mutlicast discovery result.
1896 size_t numOfLinkedDev = 0;
1897 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
1898 &pLinkedDevList, &numOfLinkedDev);
1899 if (OC_STACK_OK != res)
1901 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : GetListofDevToReqDeleteCred() failed");
1904 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
1905 { // So we don't have to send request message.
1906 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No alived & linked device found.");
1907 res = OC_STACK_CONTINUE;
1911 // 4. Prepare RemoveData Context data.
1912 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
1915 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
1916 res = OC_STACK_NO_MEMORY;
1920 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
1921 if (!removeData->revokeTargetDev)
1923 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : PMCloneOCProvisionDev Failed");
1924 res = OC_STACK_NO_MEMORY;
1928 removeData->removeRes =
1929 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
1930 if (!removeData->removeRes)
1932 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
1933 res = OC_STACK_NO_MEMORY;
1937 removeData->ctx = ctx;
1938 removeData->linkedDevList = pLinkedDevList;
1939 removeData->resultCallback = resultCallback;
1940 removeData->numOfResults = 0;
1941 removeData->sizeOfResArray = numOfLinkedDev;
1942 removeData->hasError = false;
1944 // 5. Send DELETE credential request to linked devices.
1945 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
1946 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
1947 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
1949 res = SendDeleteCredentialRequest((void*)removeData, &SRPRemoveDeviceCB,
1950 removeData->revokeTargetDev, curDev);
1951 if (OC_STACK_OK != res)
1953 OIC_LOG_V(ERROR, TAG, "SRPRemoveDevice : Fail to send the DELETE credential request to\
1954 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
1958 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
1962 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
1963 PMDeleteDeviceList(pOwnedDevList);
1964 OIC_LOG(INFO, TAG, "OUT SRPRemoveDevice");
1966 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
1969 PDMDestoryOicUuidLinkList(pLinkedUuidList);
1970 PMDeleteDeviceList(pOwnedDevList);
1971 PMDeleteDeviceList(pLinkedDevList);
1974 OICFree(removeData->revokeTargetDev);
1975 OICFree(removeData->removeRes);
1976 OICFree(removeData);
1978 OIC_LOG(INFO, TAG, "OUT ERROR case SRPRemoveDevice");
1983 * Function to sync-up credential and ACL of the target device.
1984 * This function will remove credential and ACL of target device from all devices in subnet.
1986 * @param[in] ctx Application context would be returned in result callback
1987 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
1988 * @param[in] pTargetDev Device information to be revoked.
1989 * @param[in] resultCallback callback provided by API user, callback will be called when
1990 * credential revocation is finished.
1991 * when there is an error, this user callback is called immediately.
1992 * @return OC_STACK_OK in case of success and other value otherwise.
1993 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
1994 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
1996 OCStackResult SRPSyncDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
1997 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
1999 OIC_LOG(INFO, TAG, "IN SRPSyncDevice");
2000 if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
2002 OIC_LOG(INFO, TAG, "SRPSyncDevice : NULL parameters");
2003 return OC_STACK_INVALID_PARAM;
2005 if (!resultCallback)
2007 OIC_LOG(INFO, TAG, "SRPSyncDevice : NULL Callback");
2008 return OC_STACK_INVALID_CALLBACK;
2011 // Declare variables in here to handle error cases with goto statement.
2012 OCProvisionDev_t* pOwnedDevList = NULL;
2013 OCProvisionDev_t* pLinkedDevList = NULL;
2014 RemoveData_t* removeData = NULL;
2016 //1. Find all devices that has a credential of the revoked device
2017 OCUuidList_t* pLinkedUuidList = NULL;
2018 size_t numOfDevices = 0;
2019 OCStackResult res = OC_STACK_ERROR;
2020 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
2021 if (OC_STACK_OK != res)
2023 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to get linked devices information");
2026 // if there is no related device, we can skip further process.
2027 if (0 == numOfDevices)
2029 OIC_LOG(DEBUG, TAG, "SRPSyncDevice : No linked device found.");
2030 res = OC_STACK_CONTINUE;
2034 //2. Find owned device from the network
2035 res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
2036 if (OC_STACK_OK != res)
2038 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to PMDeviceDiscovery");
2042 //3. Make a list of devices to send DELETE credential request
2043 // by comparing owned devices from provisioning database with mutlicast discovery result.
2044 size_t numOfLinkedDev = 0;
2045 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
2046 &pLinkedDevList, &numOfLinkedDev);
2047 if (OC_STACK_OK != res)
2049 OIC_LOG(ERROR, TAG, "SRPSyncDevice : GetListofDevToReqDeleteCred() failed");
2052 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
2053 { // So we don't have to send request message.
2054 OIC_LOG(DEBUG, TAG, "SRPSyncDevice : No alived & linked device found.");
2055 res = OC_STACK_CONTINUE;
2059 // 4. Prepare RemoveData Context data.
2060 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
2063 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to memory allocation");
2064 res = OC_STACK_NO_MEMORY;
2068 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
2069 if (!removeData->revokeTargetDev)
2071 OIC_LOG(ERROR, TAG, "SRPSyncDevice : PMCloneOCProvisionDev Failed");
2072 res = OC_STACK_NO_MEMORY;
2076 removeData->removeRes =
2077 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
2078 if (!removeData->removeRes)
2080 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to allocate memory");
2081 res = OC_STACK_NO_MEMORY;
2085 removeData->ctx = ctx;
2086 removeData->linkedDevList = pLinkedDevList;
2087 removeData->resultCallback = resultCallback;
2088 removeData->numOfResults = 0;
2089 removeData->sizeOfResArray = numOfLinkedDev;
2090 removeData->hasError = false;
2092 // 5. Send DELETE credential request to linked devices.
2093 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
2094 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
2095 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
2097 res = SendDeleteACLRequest((void*)removeData, &SRPSyncDeviceACLCB,
2098 removeData->revokeTargetDev, curDev);
2099 if (OC_STACK_OK != res)
2101 OIC_LOG_V(ERROR, TAG, "SRPSyncDevice : Fail to send the DELETE ACL request to\
2102 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2105 res = SendDeleteCredentialRequest((void*)removeData, &SRPSyncDeviceCredCB,
2106 removeData->revokeTargetDev, curDev);
2107 if (OC_STACK_OK != res)
2109 OIC_LOG_V(ERROR, TAG, "SRPSyncDevice : Fail to send the DELETE credential request to\
2110 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2111 totalRes = OC_STACK_ERROR;
2115 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
2119 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
2120 PMDeleteDeviceList(pOwnedDevList);
2121 OIC_LOG(INFO, TAG, "OUT SRPSyncDevice");
2123 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
2126 PDMDestoryOicUuidLinkList(pLinkedUuidList);
2127 PMDeleteDeviceList(pOwnedDevList);
2128 PMDeleteDeviceList(pLinkedDevList);
2131 OICFree(removeData->revokeTargetDev);
2132 OICFree(removeData->removeRes);
2133 OICFree(removeData);
2135 OIC_LOG(INFO, TAG, "OUT ERROR case SRPSyncDevice");
2140 * Function for remote reset
2141 * This function will send pstat PUT message to the target device to initiate remote reset.
2143 * @param[in] pTargetDev Device information to be revoked.
2144 * @param[in] resultCallback callback provided by API user, callback will be called when
2145 * credential revocation is finished.
2146 * when there is an error, this user callback is called immediately.
2147 * @return OC_STACK_OK in case of success and other value otherwise.
2148 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
2149 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
2151 OCStackResult SRPResetDevice(const OCProvisionDev_t* pTargetDev,
2152 OCProvisionResultCB resultCallback)
2154 OIC_LOG(INFO, TAG, "IN SRPResetDevice");
2157 OIC_LOG(INFO, TAG, "SRPResetDevice : NULL parameters");
2158 return OC_STACK_INVALID_PARAM;
2160 if (!resultCallback)
2162 OIC_LOG(INFO, TAG, "SRPResetDevice : NULL Callback");
2163 return OC_STACK_INVALID_CALLBACK;
2166 OCStackResult res = OC_STACK_ERROR;
2167 OicSecPstat_t * pstat = (OicSecPstat_t *) OICCalloc(1, sizeof(OicSecPstat_t));
2170 OIC_LOG(ERROR, TAG, "Failed to allocate memory");
2171 return OC_STACK_NO_MEMORY;
2174 pstat->cm = (OicSecDpm_t) 1;
2176 memcpy(pstat->deviceID.id, pTargetDev->doxm->deviceID.id, sizeof(OicUuid_t));
2177 pstat->tm = (OicSecDpm_t) 2;
2178 pstat->om = (OicSecDpom_t) 0;
2180 pstat->sm = (OicSecDpom_t *) OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
2181 if (NULL == pstat->sm)
2183 OIC_LOG(ERROR, TAG, "Failed to allocate memory");
2185 return OC_STACK_NO_MEMORY;
2187 pstat->sm[0] = (OicSecDpom_t) 3;
2189 OCSecurityPayload * secPayload = (OCSecurityPayload *) OICCalloc(1, sizeof(OCSecurityPayload));
2192 OIC_LOG(ERROR, TAG, "Failed to allocate memory");
2193 res = OC_STACK_NO_MEMORY;
2196 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
2198 if (OC_STACK_OK != PstatToCBORPayload(pstat, &(secPayload->securityData),
2199 &(secPayload->payloadSize)))
2201 OCPayloadDestroy((OCPayload *) secPayload);
2202 OIC_LOG(ERROR, TAG, "Failed to PstatToCBORPayload");
2203 res = OC_STACK_NO_MEMORY;
2206 OIC_LOG(DEBUG, TAG, "Created payload for pstat set");
2207 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
2209 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2210 if (!PMGenerateQuery(true,
2211 pTargetDev->endpoint.addr,
2212 pTargetDev->securePort,
2213 pTargetDev->connType,
2214 query, sizeof(query), OIC_RSRC_PSTAT_URI))
2216 OIC_LOG(ERROR, TAG, "SRPResetDevice : Failed to generate query");
2217 OCPayloadDestroy((OCPayload *) secPayload);
2218 res = OC_STACK_ERROR;
2221 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2223 OCProvisionDev_t * targetDev = PMCloneOCProvisionDev(pTargetDev);
2224 OCCallbackData cbData = { .context = NULL, .cb = NULL, .cd = NULL };
2225 cbData.cb = &SRPResetDeviceCB;
2226 cbData.context = (void *) targetDev;
2228 OCMethod method = OC_REST_POST;
2229 OCDoHandle handle = NULL;
2230 OIC_LOG(DEBUG, TAG, "Sending PSTAT info to resource server");
2231 res = OCDoResource(&handle, method, query,
2232 &targetDev->endpoint, (OCPayload *)secPayload,
2233 targetDev->connType, OC_LOW_QOS, &cbData, NULL, 0);\
2234 if (OC_STACK_OK != res)
2236 OIC_LOG(ERROR, TAG, "OCStack resource error");
2242 OIC_LOG(INFO, TAG, "OUT SRPResetDevice");
2247 * Internal Function to store results in result array during GetCredResourceCB.
2249 static void registerResultForGetCredResourceCB(GetSecData_t *GetSecData,
2250 OCStackResult stackresult)
2252 OIC_LOG_V(INFO, TAG, "Inside registerResultForGetCredResourceCB "
2253 "GetSecData->numOfResults is %d\n", GetSecData->numOfResults);
2254 memcpy(GetSecData->resArr[(GetSecData->numOfResults)].deviceId.id,
2255 GetSecData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
2256 GetSecData->resArr[(GetSecData->numOfResults)].res = stackresult;
2257 ++(GetSecData->numOfResults);
2261 * Callback handler of SRPGetCredResource.
2263 * @param[in] ctx ctx value passed to callback from calling function.
2264 * @param[in] UNUSED handle to an invocation
2265 * @param[in] clientResponse Response from queries to remote servers.
2266 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
2267 * and OC_STACK_KEEP_TRANSACTION to keep it.
2269 static OCStackApplicationResult SRPGetCredResourceCB(void *ctx, OCDoHandle UNUSED,
2270 OCClientResponse *clientResponse)
2272 OIC_LOG_V(INFO, TAG, "Inside SRPGetCredResourceCB.");
2274 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
2275 GetSecData_t *GetSecData = (GetSecData_t*)ctx;
2276 OCProvisionResultCB resultCallback = GetSecData->resultCallback;
2280 if(OC_STACK_OK == clientResponse->result)
2282 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
2283 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
2285 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
2287 registerResultForGetCredResourceCB(GetSecData, OC_STACK_OK);
2288 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2291 OICFree(GetSecData->resArr);
2292 OICFree(GetSecData);
2294 return OC_STACK_DELETE_TRANSACTION;
2297 registerResultForGetCredResourceCB(GetSecData, OC_STACK_OK);
2298 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2301 OIC_LOG_V(ERROR, TAG, "SRPGetCredResourceCB received Null clientResponse");
2302 OICFree(GetSecData->resArr);
2303 OICFree(GetSecData);
2305 return OC_STACK_DELETE_TRANSACTION;
2309 OCStackResult SRPGetCredResource(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
2310 OCProvisionResultCB resultCallback)
2312 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
2313 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
2315 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2316 if(!PMGenerateQuery(true,
2317 selectedDeviceInfo->endpoint.addr,
2318 selectedDeviceInfo->securePort,
2319 selectedDeviceInfo->connType,
2320 query, sizeof(query), OIC_RSRC_CRED_URI))
2322 OIC_LOG(ERROR, TAG, "SRPGetCredResource : Failed to generate query");
2323 return OC_STACK_ERROR;
2325 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2327 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
2328 cbData.cb = &SRPGetCredResourceCB;
2329 GetSecData_t* GetSecData = (GetSecData_t*)OICCalloc(1, sizeof(GetSecData_t));
2330 if (NULL == GetSecData)
2332 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2333 return OC_STACK_NO_MEMORY;
2335 GetSecData->deviceInfo = selectedDeviceInfo;
2336 GetSecData->resultCallback = resultCallback;
2337 GetSecData->numOfResults=0;
2338 GetSecData->ctx = ctx;
2340 int noOfRiCalls = 1;
2341 GetSecData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
2342 if (NULL == GetSecData->resArr)
2344 OICFree(GetSecData);
2345 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2346 return OC_STACK_NO_MEMORY;
2348 cbData.context = (void *)GetSecData;
2350 OCMethod method = OC_REST_GET;
2351 OCDoHandle handle = NULL;
2352 OIC_LOG(DEBUG, TAG, "Sending Get Cred to resource server");
2353 OCStackResult ret = OCDoResource(&handle, method, query, NULL, NULL,
2354 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
2355 if (OC_STACK_OK != ret)
2357 OIC_LOG(ERROR, TAG, "OCStack resource error");
2358 OICFree(GetSecData->resArr);
2359 OICFree(GetSecData);
2361 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
2362 OIC_LOG(DEBUG, TAG, "OUT SRPGetCredResource");
2368 * Internal Function to store results in result array during GetACLResourceCB.
2370 static void registerResultForGetACLResourceCB(GetSecData_t *GetSecData,
2371 OCStackResult stackresult)
2373 OIC_LOG_V(INFO, TAG, "Inside registerResultForGetACLResourceCB "
2374 "GetSecData->numOfResults is %d\n", GetSecData->numOfResults);
2375 memcpy(GetSecData->resArr[(GetSecData->numOfResults)].deviceId.id,
2376 GetSecData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
2377 GetSecData->resArr[(GetSecData->numOfResults)].res = stackresult;
2378 ++(GetSecData->numOfResults);
2382 * Callback handler of SRPGetACLResource.
2384 * @param[in] ctx ctx value passed to callback from calling function.
2385 * @param[in] UNUSED handle to an invocation
2386 * @param[in] clientResponse Response from queries to remote servers.
2387 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
2388 * and OC_STACK_KEEP_TRANSACTION to keep it.
2390 static OCStackApplicationResult SRPGetACLResourceCB(void *ctx, OCDoHandle UNUSED,
2391 OCClientResponse *clientResponse)
2393 OIC_LOG_V(INFO, TAG, "Inside SRPGetACLResourceCB.");
2395 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
2396 GetSecData_t *GetSecData = (GetSecData_t*)ctx;
2397 OCProvisionResultCB resultCallback = GetSecData->resultCallback;
2401 if(OC_STACK_OK == clientResponse->result)
2403 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
2404 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
2406 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
2408 registerResultForGetACLResourceCB(GetSecData, OC_STACK_OK);
2409 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2412 OICFree(GetSecData->resArr);
2413 OICFree(GetSecData);
2415 return OC_STACK_DELETE_TRANSACTION;
2418 registerResultForGetACLResourceCB(GetSecData, OC_STACK_OK);
2419 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2422 OIC_LOG_V(ERROR, TAG, "SRPGetACLResourceCB received Null clientResponse");
2423 OICFree(GetSecData->resArr);
2424 OICFree(GetSecData);
2426 return OC_STACK_DELETE_TRANSACTION;
2430 OCStackResult SRPGetACLResource(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
2431 OCProvisionResultCB resultCallback)
2433 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
2434 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
2436 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2437 if(!PMGenerateQuery(true,
2438 selectedDeviceInfo->endpoint.addr,
2439 selectedDeviceInfo->securePort,
2440 selectedDeviceInfo->connType,
2441 query, sizeof(query), OIC_RSRC_ACL_URI))
2443 OIC_LOG(ERROR, TAG, "SRPGetACLResource : Failed to generate query");
2444 return OC_STACK_ERROR;
2446 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2448 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
2449 cbData.cb = &SRPGetACLResourceCB;
2450 GetSecData_t* GetSecData = (GetSecData_t*)OICCalloc(1, sizeof(GetSecData_t));
2451 if (NULL == GetSecData)
2453 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2454 return OC_STACK_NO_MEMORY;
2456 GetSecData->deviceInfo = selectedDeviceInfo;
2457 GetSecData->resultCallback = resultCallback;
2458 GetSecData->numOfResults=0;
2459 GetSecData->ctx = ctx;
2461 int noOfRiCalls = 1;
2462 GetSecData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
2463 if (NULL == GetSecData->resArr)
2465 OICFree(GetSecData);
2466 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2467 return OC_STACK_NO_MEMORY;
2469 cbData.context = (void *)GetSecData;
2471 OCMethod method = OC_REST_GET;
2472 OCDoHandle handle = NULL;
2473 OIC_LOG(DEBUG, TAG, "Sending Get ACL to resource server");
2474 OCStackResult ret = OCDoResource(&handle, method, query, NULL, NULL,
2475 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
2476 if (OC_STACK_OK != ret)
2478 OIC_LOG(ERROR, TAG, "OCStack resource error");
2479 OICFree(GetSecData->resArr);
2480 OICFree(GetSecData);
2482 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
2483 OIC_LOG(DEBUG, TAG, "OUT SRPGetACLResource");