+
+/*
+* Function to device revocation
+* This function will remove credential of target device from all devices in subnet.
+*
+* @param[in] ctx Application context would be returned in result callback
+* @param[in] pOwnedDevList List of owned devices
+* @param[in] pTargetDev Device information to be revoked.
+* @param[in] resultCallback callback provided by API user, callback will be called when
+* credential revocation is finished.
+* @return OC_STACK_OK in case of success and other value otherwise.
+* If OC_STACK_OK is returned, the caller of this API should wait for callback.
+* OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
+*/
+OCStackResult SRPRemoveDeviceWithoutDiscovery(void* ctx, const OCProvisionDev_t* pOwnedDevList,
+ const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
+{
+ OIC_LOG(INFO, TAG, "IN SRPRemoveDeviceWithoutDiscovery");
+
+ if (!pOwnedDevList)
+ {
+ OIC_LOG(WARNING, TAG, "SRPRemoveDeviceWithoutDiscovery : Owned Device List is empty");
+ return OC_STACK_CONTINUE;
+ }
+ if (!pTargetDev)
+ {
+ OIC_LOG(INFO, TAG, "SRPRemoveDeviceWithoutDiscovery : NULL parameters");
+ return OC_STACK_INVALID_PARAM;
+ }
+ if (!resultCallback)
+ {
+ OIC_LOG(INFO, TAG, "SRPRemoveDeviceWithoutDiscovery : NULL Callback");
+ return OC_STACK_INVALID_CALLBACK;
+ }
+
+ // Declare variables in here to handle error cases with goto statement.
+ OCProvisionDev_t* pLinkedDevList = NULL;
+ RemoveData_t* removeData = NULL;
+
+ //1. Find all devices that has a credential of the revoked device
+ OCUuidList_t* pLinkedUuidList = NULL;
+ size_t numOfDevices = 0;
+ OCStackResult res = OC_STACK_ERROR;
+ res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
+ if (OC_STACK_OK != res)
+ {
+ OIC_LOG(WARNING, TAG, "SRPRemoveDeviceWithoutDiscovery : Failed to get linked devices information");
+ return OC_STACK_CONTINUE;
+ }
+ // if there is no related device, we can skip further process.
+ if (0 == numOfDevices)
+ {
+ OIC_LOG(WARNING, TAG, "SRPRemoveDeviceWithoutDiscovery : No linked device found.");
+ return OC_STACK_CONTINUE;
+ }
+
+ //2. Make a list of devices to send DELETE credential request
+ // by comparing owned devices from provisioning database with mutlicast discovery result.
+ size_t numOfLinkedDev = 0;
+ res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
+ &pLinkedDevList, &numOfLinkedDev);
+ if (OC_STACK_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : GetListofDevToReqDeleteCred() failed");
+ goto error;
+ }
+ if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
+ { // So we don't have to send request message.
+ OIC_LOG(DEBUG, TAG, "SRPRemoveDeviceWithoutDiscovery : No alived & linked device found.");
+ return OC_STACK_CONTINUE;
+ }
+
+ // 3. Prepare RemoveData Context data.
+ removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
+ if (!removeData)
+ {
+ OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Failed to memory allocation");
+ res = OC_STACK_NO_MEMORY;
+ goto error;
+ }
+
+ removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
+ if (!removeData->revokeTargetDev)
+ {
+ OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : PMCloneOCProvisionDev Failed");
+ res = OC_STACK_NO_MEMORY;
+ goto error;
+ }
+
+ removeData->removeRes =
+ (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
+ if (!removeData->removeRes)
+ {
+ OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Failed to memory allocation");
+ res = OC_STACK_NO_MEMORY;
+ goto error;
+ }
+
+ removeData->ctx = ctx;
+ removeData->linkedDevList = pLinkedDevList;
+ removeData->resultCallback = resultCallback;
+ removeData->numOfResults = 0;
+ removeData->sizeOfResArray = numOfLinkedDev;
+ removeData->hasError = false;
+
+ // 5. Send DELETE credential request to linked devices.
+ OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
+ OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
+ LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
+ {
+ res = SendDeleteCredentialRequest((void*)removeData, &SRPRemoveDeviceCB,
+ removeData->revokeTargetDev, curDev);
+ if (OC_STACK_OK != res)
+ {
+ OIC_LOG_V(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Fail to send the DELETE credential request to\
+ %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
+ }
+ else
+ {
+ totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
+ }
+ }
+
+ PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
+ OIC_LOG(INFO, TAG, "OUT SRPRemoveDeviceWithoutDiscovery");
+
+ return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
+
+error:
+ PDMDestoryOicUuidLinkList(pLinkedUuidList);
+ PMDeleteDeviceList(pLinkedDevList);
+ if (removeData)
+ {
+ OICFree(removeData->revokeTargetDev);
+ OICFree(removeData->removeRes);
+ OICFree(removeData);
+ }
+ OIC_LOG(INFO, TAG, "OUT ERROR case SRPRemoveDeviceWithoutDiscovery");
+ return res;
+}
+
+/*
+ * Function to sync-up credential and ACL of the target device.
+ * This function will remove credential and ACL of target device from all devices in subnet.
+ *
+ * @param[in] ctx Application context would be returned in result callback
+ * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
+ * @param[in] pTargetDev Device information to be revoked.
+ * @param[in] resultCallback callback provided by API user, callback will be called when
+ * credential revocation is finished.
+ * when there is an error, this user callback is called immediately.
+ * @return OC_STACK_OK in case of success and other value otherwise.
+ * If OC_STACK_OK is returned, the caller of this API should wait for callback.
+ * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
+ */
+OCStackResult SRPSyncDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
+ const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
+{
+ OIC_LOG(INFO, TAG, "IN SRPSyncDevice");
+ if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
+ {
+ OIC_LOG(INFO, TAG, "SRPSyncDevice : NULL parameters");
+ return OC_STACK_INVALID_PARAM;
+ }
+ if (!resultCallback)
+ {
+ OIC_LOG(INFO, TAG, "SRPSyncDevice : NULL Callback");
+ return OC_STACK_INVALID_CALLBACK;
+ }
+
+ // Declare variables in here to handle error cases with goto statement.
+ OCProvisionDev_t* pOwnedDevList = NULL;
+ OCProvisionDev_t* pLinkedDevList = NULL;
+ RemoveData_t* removeData = NULL;
+
+ //1. Find all devices that has a credential of the revoked device
+ OCUuidList_t* pLinkedUuidList = NULL;
+ size_t numOfDevices = 0;
+ OCStackResult res = OC_STACK_ERROR;
+ res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
+ if (OC_STACK_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to get linked devices information");
+ return res;
+ }
+ // if there is no related device, we can skip further process.
+ if (0 == numOfDevices)
+ {
+ OIC_LOG(DEBUG, TAG, "SRPSyncDevice : No linked device found.");
+ res = OC_STACK_CONTINUE;
+ goto error;
+ }
+
+ //2. Find owned device from the network
+ res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
+ if (OC_STACK_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to PMDeviceDiscovery");
+ goto error;
+ }
+
+ //3. Make a list of devices to send DELETE credential request
+ // by comparing owned devices from provisioning database with mutlicast discovery result.
+ size_t numOfLinkedDev = 0;
+ res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
+ &pLinkedDevList, &numOfLinkedDev);
+ if (OC_STACK_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "SRPSyncDevice : GetListofDevToReqDeleteCred() failed");
+ goto error;
+ }
+ if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
+ { // So we don't have to send request message.
+ OIC_LOG(DEBUG, TAG, "SRPSyncDevice : No alived & linked device found.");
+ res = OC_STACK_CONTINUE;
+ goto error;
+ }
+
+ // 4. Prepare RemoveData Context data.
+ removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
+ if (!removeData)
+ {
+ OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to memory allocation");
+ res = OC_STACK_NO_MEMORY;
+ goto error;
+ }
+
+ removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
+ if (!removeData->revokeTargetDev)
+ {
+ OIC_LOG(ERROR, TAG, "SRPSyncDevice : PMCloneOCProvisionDev Failed");
+ res = OC_STACK_NO_MEMORY;
+ goto error;
+ }
+
+ removeData->removeRes =
+ (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
+ if (!removeData->removeRes)
+ {
+ OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to allocate memory");
+ res = OC_STACK_NO_MEMORY;
+ goto error;
+ }
+
+ removeData->ctx = ctx;
+ removeData->linkedDevList = pLinkedDevList;
+ removeData->resultCallback = resultCallback;
+ removeData->numOfResults = 0;
+ removeData->sizeOfResArray = numOfLinkedDev;
+ removeData->hasError = false;
+
+ // 5. Send DELETE credential request to linked devices.
+ OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
+ OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
+ LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
+ {
+ res = SendDeleteACLRequest((void*)removeData, &SRPSyncDeviceACLCB,
+ removeData->revokeTargetDev, curDev);
+ if (OC_STACK_OK != res)
+ {
+ OIC_LOG_V(ERROR, TAG, "SRPSyncDevice : Fail to send the DELETE ACL request to\
+ %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
+ goto error;
+ }
+ res = SendDeleteCredentialRequest((void*)removeData, &SRPSyncDeviceCredCB,
+ removeData->revokeTargetDev, curDev);
+ if (OC_STACK_OK != res)
+ {
+ OIC_LOG_V(ERROR, TAG, "SRPSyncDevice : Fail to send the DELETE credential request to\
+ %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
+ totalRes = OC_STACK_ERROR;
+ }
+ else
+ {
+ totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
+ }
+ }
+
+ PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
+ PMDeleteDeviceList(pOwnedDevList);
+ OIC_LOG(INFO, TAG, "OUT SRPSyncDevice");
+
+ return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
+
+error:
+ PDMDestoryOicUuidLinkList(pLinkedUuidList);
+ PMDeleteDeviceList(pOwnedDevList);
+ PMDeleteDeviceList(pLinkedDevList);
+ if (removeData)
+ {
+ OICFree(removeData->revokeTargetDev);
+ OICFree(removeData->removeRes);
+ OICFree(removeData);
+ }
+ OIC_LOG(INFO, TAG, "OUT ERROR case SRPSyncDevice");
+ return res;
+}
+
+/*
+ * Function for remote reset
+ * This function will send pstat PUT message to the target device to initiate remote reset.
+ *
+ * @param[in] pTargetDev Device information to be revoked.
+ * @param[in] resultCallback callback provided by API user, callback will be called when
+ * credential revocation is finished.
+ * when there is an error, this user callback is called immediately.
+ * @return OC_STACK_OK in case of success and other value otherwise.
+ * If OC_STACK_OK is returned, the caller of this API should wait for callback.
+ * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
+ */
+OCStackResult SRPResetDevice(const OCProvisionDev_t* pTargetDev,
+ OCClientContextDeleter deleteCallback)
+{
+ OIC_LOG(INFO, TAG, "IN SRPResetDevice");
+ if (!pTargetDev)
+ {
+ OIC_LOG(INFO, TAG, "SRPResetDevice : NULL parameters");
+ return OC_STACK_INVALID_PARAM;
+ }
+ if (!deleteCallback)
+ {
+ OIC_LOG(INFO, TAG, "SRPResetDevice : NULL Callback");
+ return OC_STACK_INVALID_CALLBACK;
+ }
+
+ OCStackResult res = OC_STACK_ERROR;
+ OicSecPstat_t * pstat = (OicSecPstat_t *) OICCalloc(1, sizeof(OicSecPstat_t));
+ if (!pstat)
+ {
+ OIC_LOG(ERROR, TAG, "Failed to allocate memory");
+ return OC_STACK_NO_MEMORY;
+ }
+
+ pstat->cm = RESET;
+ pstat->isOp = false;
+ memcpy(pstat->deviceID.id, pTargetDev->doxm->deviceID.id, sizeof(OicUuid_t));
+ pstat->tm = TAKE_OWNER;
+ pstat->om = (OicSecDpom_t)(SINGLE_SERVICE_CLIENT_DRIVEN); // the only mode IoTivity supports currently
+ pstat->smLen = 1;
+ pstat->sm = (OicSecDpom_t *) OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
+ if (NULL == pstat->sm)
+ {
+ OIC_LOG(ERROR, TAG, "Failed to allocate memory");
+ OICFree(pstat);
+ return OC_STACK_NO_MEMORY;
+ }
+ pstat->sm[0] = (OicSecDpom_t)(SINGLE_SERVICE_CLIENT_DRIVEN); // the only mode IoTivity supports currently
+
+ OCSecurityPayload * secPayload = (OCSecurityPayload *) OICCalloc(1, sizeof(OCSecurityPayload));
+ if (!secPayload)
+ {
+ OIC_LOG(ERROR, TAG, "Failed to allocate memory");
+ res = OC_STACK_NO_MEMORY;
+ goto error;
+ }
+ secPayload->base.type = PAYLOAD_TYPE_SECURITY;
+
+ if (OC_STACK_OK != PstatToCBORPayload(pstat, &(secPayload->securityData),
+ &(secPayload->payloadSize), true))
+ {
+ OCPayloadDestroy((OCPayload *) secPayload);
+ OIC_LOG(ERROR, TAG, "Failed to PstatToCBORPayload");
+ res = OC_STACK_NO_MEMORY;
+ goto error;
+ }
+ OIC_LOG(DEBUG, TAG, "Created payload for pstat set");
+ OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
+
+ char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
+ if (!PMGenerateQuery(true,
+ pTargetDev->endpoint.addr,
+ pTargetDev->securePort,
+ pTargetDev->connType,
+ query, sizeof(query), OIC_RSRC_PSTAT_URI))
+ {
+ OIC_LOG(ERROR, TAG, "SRPResetDevice : Failed to generate query");
+ OCPayloadDestroy((OCPayload *) secPayload);
+ res = OC_STACK_ERROR;
+ goto error;
+ }
+ OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
+
+ OCCallbackData cbData = { .context = NULL, .cb = NULL, .cd = NULL };
+ OCMethod method = OC_REST_POST;
+ OCDoHandle handle = NULL;
+ OCProvisionDev_t * targetDev = PMCloneOCProvisionDev(pTargetDev);
+ if (NULL == targetDev)
+ {
+ OIC_LOG(ERROR, TAG, "target dev is null");
+ res = OC_STACK_ERROR;
+ goto error;
+ }
+ cbData.cb = &SRPResetDeviceCB;
+ cbData.context = (void *) targetDev;
+ cbData.cd = deleteCallback;
+ OIC_LOG(DEBUG, TAG, "Sending PSTAT info to resource server");
+ res = OCDoResource(&handle, method, query,
+ &targetDev->endpoint, (OCPayload *)secPayload,
+ targetDev->connType, OC_LOW_QOS, &cbData, NULL, 0);\
+ if (OC_STACK_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "OCStack resource error");
+ }
+
+error:
+ OICFree(pstat->sm);
+ OICFree(pstat);
+ OIC_LOG(INFO, TAG, "OUT SRPResetDevice");
+ return res;
+}
+
+/**
+ * Internal Function to store results in result array during GetCredResourceCB.
+ */
+static void registerResultForGetCredResourceCB(GetSecData_t *GetSecData,
+ OCStackResult stackresult)
+{
+ OIC_LOG_V(INFO, TAG, "Inside registerResultForGetCredResourceCB "
+ "GetSecData->numOfResults is %d\n", GetSecData->numOfResults);
+ memcpy(GetSecData->resArr[(GetSecData->numOfResults)].deviceId.id,
+ GetSecData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
+ GetSecData->resArr[(GetSecData->numOfResults)].res = stackresult;
+ ++(GetSecData->numOfResults);
+}
+
+/**
+ * Callback handler of SRPGetCredResource.
+ *
+ * @param[in] ctx ctx value passed to callback from calling function.
+ * @param[in] UNUSED handle to an invocation
+ * @param[in] clientResponse Response from queries to remote servers.
+ * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
+ * and OC_STACK_KEEP_TRANSACTION to keep it.
+ */
+static OCStackApplicationResult SRPGetCredResourceCB(void *ctx, OCDoHandle UNUSED,
+ OCClientResponse *clientResponse)
+{
+ OIC_LOG_V(INFO, TAG, "Inside SRPGetCredResourceCB.");
+ (void)UNUSED;
+ VERIFY_NON_NULL_RET(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
+ GetSecData_t *GetSecData = (GetSecData_t*)ctx;
+ OCProvisionResultCB resultCallback = GetSecData->resultCallback;
+
+ if (clientResponse)
+ {
+ if(OC_STACK_OK == clientResponse->result)
+ {
+ uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
+ size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
+
+ OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
+ (void)size;
+ (void)payload;
+
+ registerResultForGetCredResourceCB(GetSecData, OC_STACK_OK);
+ ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
+ GetSecData->resArr,
+ false);
+ OICFree(GetSecData->resArr);
+ OICFree(GetSecData);
+
+ return OC_STACK_DELETE_TRANSACTION;
+ }
+ }
+ registerResultForGetCredResourceCB(GetSecData, OC_STACK_OK);
+ ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
+ GetSecData->resArr,
+ false);
+ OIC_LOG_V(ERROR, TAG, "SRPGetCredResourceCB received Null clientResponse");
+ OICFree(GetSecData->resArr);
+ OICFree(GetSecData);
+
+ return OC_STACK_DELETE_TRANSACTION;
+}
+
+
+OCStackResult SRPGetCredResource(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
+ OCProvisionResultCB resultCallback)
+{
+ VERIFY_NON_NULL_RET(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
+ VERIFY_NON_NULL_RET(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
+
+ char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
+ if(!PMGenerateQuery(true,
+ selectedDeviceInfo->endpoint.addr,
+ selectedDeviceInfo->securePort,
+ selectedDeviceInfo->connType,
+ query, sizeof(query), OIC_RSRC_CRED_URI))
+ {
+ OIC_LOG(ERROR, TAG, "SRPGetCredResource : Failed to generate query");
+ return OC_STACK_ERROR;
+ }
+ OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
+
+ OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
+ cbData.cb = &SRPGetCredResourceCB;
+ GetSecData_t* GetSecData = (GetSecData_t*)OICCalloc(1, sizeof(GetSecData_t));
+ if (NULL == GetSecData)
+ {
+ OIC_LOG(ERROR, TAG, "Unable to allocate memory");
+ return OC_STACK_NO_MEMORY;
+ }
+ GetSecData->deviceInfo = selectedDeviceInfo;
+ GetSecData->resultCallback = resultCallback;
+ GetSecData->numOfResults=0;
+ GetSecData->ctx = ctx;
+
+ int noOfRiCalls = 1;
+ GetSecData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
+ if (NULL == GetSecData->resArr)
+ {
+ OICFree(GetSecData);
+ OIC_LOG(ERROR, TAG, "Unable to allocate memory");
+ return OC_STACK_NO_MEMORY;
+ }
+ cbData.context = (void *)GetSecData;
+ cbData.cd = NULL;
+ OCMethod method = OC_REST_GET;
+ OCDoHandle handle = NULL;
+ OIC_LOG(DEBUG, TAG, "Sending Get Cred to resource server");
+ OCStackResult ret = OCDoResource(&handle, method, query, NULL, NULL,
+ selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
+ if (OC_STACK_OK != ret)
+ {
+ OIC_LOG(ERROR, TAG, "OCStack resource error");
+ OICFree(GetSecData->resArr);
+ OICFree(GetSecData);
+ }
+ VERIFY_SUCCESS_RET(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
+ OIC_LOG(DEBUG, TAG, "OUT SRPGetCredResource");
+
+ return OC_STACK_OK;
+}
+
+/**
+ * Internal Function to store results in result array during GetACLResourceCB.
+ */
+static void registerResultForGetACLResourceCB(GetSecData_t *GetSecData,
+ OCStackResult stackresult)
+{
+ OIC_LOG_V(INFO, TAG, "Inside registerResultForGetACLResourceCB "
+ "GetSecData->numOfResults is %d\n", GetSecData->numOfResults);
+ memcpy(GetSecData->resArr[(GetSecData->numOfResults)].deviceId.id,
+ GetSecData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
+ GetSecData->resArr[(GetSecData->numOfResults)].res = stackresult;
+ ++(GetSecData->numOfResults);
+}
+
+/**
+ * Callback handler of SRPGetACLResource.
+ *
+ * @param[in] ctx ctx value passed to callback from calling function.
+ * @param[in] UNUSED handle to an invocation
+ * @param[in] clientResponse Response from queries to remote servers.
+ * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
+ * and OC_STACK_KEEP_TRANSACTION to keep it.
+ */
+static OCStackApplicationResult SRPGetACLResourceCB(void *ctx, OCDoHandle UNUSED,
+ OCClientResponse *clientResponse)
+{
+ OIC_LOG_V(INFO, TAG, "Inside SRPGetACLResourceCB.");
+ (void)UNUSED;
+ VERIFY_NON_NULL_RET(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
+ GetSecData_t *GetSecData = (GetSecData_t*)ctx;
+ OCProvisionResultCB resultCallback = GetSecData->resultCallback;
+
+ if (clientResponse)
+ {
+ if(OC_STACK_OK == clientResponse->result)
+ {
+ uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
+ size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
+
+ OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
+ (void)payload;
+ (void)size;
+
+ registerResultForGetACLResourceCB(GetSecData, OC_STACK_OK);
+ ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
+ GetSecData->resArr,
+ false);
+ OICFree(GetSecData->resArr);
+ OICFree(GetSecData);
+
+ return OC_STACK_DELETE_TRANSACTION;
+ }
+ }
+ registerResultForGetACLResourceCB(GetSecData, OC_STACK_OK);
+ ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
+ GetSecData->resArr,
+ false);
+ OIC_LOG_V(ERROR, TAG, "SRPGetACLResourceCB received Null clientResponse");
+ OICFree(GetSecData->resArr);
+ OICFree(GetSecData);
+
+ return OC_STACK_DELETE_TRANSACTION;
+}
+
+
+OCStackResult SRPGetACLResource(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
+ OCProvisionResultCB resultCallback)
+{
+ VERIFY_NON_NULL_RET(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
+ VERIFY_NON_NULL_RET(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
+
+ char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
+ if(!PMGenerateQuery(true,
+ selectedDeviceInfo->endpoint.addr,
+ selectedDeviceInfo->securePort,
+ selectedDeviceInfo->connType,
+ query, sizeof(query), OIC_RSRC_ACL_URI))
+ {
+ OIC_LOG(ERROR, TAG, "SRPGetACLResource : Failed to generate query");
+ return OC_STACK_ERROR;
+ }
+ OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
+
+ OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
+ cbData.cb = &SRPGetACLResourceCB;
+ GetSecData_t* GetSecData = (GetSecData_t*)OICCalloc(1, sizeof(GetSecData_t));
+ if (NULL == GetSecData)
+ {
+ OIC_LOG(ERROR, TAG, "Unable to allocate memory");
+ return OC_STACK_NO_MEMORY;
+ }
+ GetSecData->deviceInfo = selectedDeviceInfo;
+ GetSecData->resultCallback = resultCallback;
+ GetSecData->numOfResults=0;
+ GetSecData->ctx = ctx;
+
+ int noOfRiCalls = 1;
+ GetSecData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
+ if (NULL == GetSecData->resArr)
+ {
+ OICFree(GetSecData);
+ OIC_LOG(ERROR, TAG, "Unable to allocate memory");
+ return OC_STACK_NO_MEMORY;
+ }
+ cbData.context = (void *)GetSecData;
+ cbData.cd = NULL;
+ OCMethod method = OC_REST_GET;
+ OCDoHandle handle = NULL;
+ OIC_LOG(DEBUG, TAG, "Sending Get ACL to resource server");
+ OCStackResult ret = OCDoResource(&handle, method, query, NULL, NULL,
+ selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
+ if (OC_STACK_OK != ret)
+ {
+ OIC_LOG(ERROR, TAG, "OCStack resource error");
+ OICFree(GetSecData->resArr);
+ OICFree(GetSecData);
+ }
+ VERIFY_SUCCESS_RET(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
+ OIC_LOG(DEBUG, TAG, "OUT SRPGetACLResource");
+
+ return OC_STACK_OK;
+}
+
+OCStackResult SRPReadTrustCertChain(uint16_t credId, uint8_t **trustCertChain,
+ size_t *chainSize)
+{
+ OIC_LOG(DEBUG, TAG, "IN SRPReadTrustCertChain");
+
+ OCStackResult res = OC_STACK_ERROR;
+ int secureFlag = 0;
+ OicSecCred_t* credData = GetCredEntryByCredId(credId);
+ if(credData)
+ {
+ res = CredToCBORPayload((const OicSecCred_t*) credData, trustCertChain,
+ chainSize, secureFlag);
+ if(OC_STACK_OK != res)
+ {
+ OIC_LOG(INFO, TAG, "CredToCBORPayload failed");
+ }
+ }
+ DeleteCredList(credData);
+ return res;
+}