+ * this function sends Direct-Pairing Configuration to a device.
+ *
+ * @param[in] ctx Application context would be returned in result callback.
+ * @param[in] selectedDeviceInfo Selected target device.
+ * @param[in] pconf PCONF pointer.
+ * @param[in] resultCallback callback provided by API user, callback will be called when provisioning
+ request recieves a response from resource server.
+ * @return OC_STACK_OK in case of success and other value otherwise.
+ */
+OCStackResult OCProvisionDirectPairing(void* ctx, const OCProvisionDev_t *selectedDeviceInfo, OicSecPconf_t *pconf,
+ OCProvisionResultCB resultCallback)
+{
+ return SRPProvisionDirectPairing(ctx, selectedDeviceInfo, pconf, resultCallback);
+}
+
+#ifdef MULTIPLE_OWNER
+static void AddPreconfPinOxMCB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
+{
+ ProvPreconfPINCtx_t* provCtx = (ProvPreconfPINCtx_t*)ctx;
+ if(provCtx)
+ {
+ OCStackResult res = MOTProvisionPreconfigPIN(provCtx->ctx, provCtx->devInfo, provCtx->pin, provCtx->pinLen, provCtx->resultCallback);
+ if(OC_STACK_OK != res)
+ {
+ arr->res = res;
+ provCtx->resultCallback(provCtx->ctx, nOfRes, arr, true);
+ }
+ }
+}
+
+OCStackResult OCProvisionPreconfigPin(void *ctx,
+ OCProvisionDev_t *targetDeviceInfo,
+ const char *preconfigPin,
+ size_t preconfigPinLen,
+ OCProvisionResultCB resultCallback)
+{
+ if( NULL == targetDeviceInfo || NULL == preconfigPin || 0 == preconfigPinLen )
+ {
+ return OC_STACK_INVALID_PARAM;
+ }
+ if (NULL == resultCallback)
+ {
+ OIC_LOG(INFO, TAG, "OCProvisionPreconfigPinCredential : NULL Callback");
+ return OC_STACK_INVALID_CALLBACK;
+ }
+
+ ProvPreconfPINCtx_t* provCtx = (ProvPreconfPINCtx_t*)OICCalloc(1, sizeof(ProvPreconfPINCtx_t));
+ if(NULL == provCtx)
+ {
+ return OC_STACK_NO_MEMORY;
+ }
+ provCtx->ctx = ctx;
+ provCtx->devInfo = targetDeviceInfo;
+ provCtx->pin = preconfigPin;
+ provCtx->pinLen = preconfigPinLen;
+ provCtx->resultCallback = resultCallback;
+ /*
+ * First of all, update OxMs to support preconfigured PIN OxM.
+ * In case of Preconfigured PIN OxM already supported on the server side,
+ * MOTAddMOTMethod API will be send POST Cred request.
+ * In case of Preconfigure PIN OxM not exist on the server side,
+ * the MOTAddMOTMethod API will be send POST doxm request to update OxMs and then send POST Cred request.
+ */
+ return MOTAddMOTMethod((void*)provCtx, targetDeviceInfo, OIC_PRECONFIG_PIN, AddPreconfPinOxMCB);
+}
+#endif //MULTIPLE_OWNER
+
+/*
+* Function to unlink devices.
+* This function will remove the credential & relationship between the two devices.
+*
+* @param[in] ctx Application context would be returned in result callback
+* @param[in] pTargetDev1 first device information to be unlinked.
+* @param[in] pTargetDev2 second device information to be unlinked.
+* @param[in] resultCallback callback provided by API user, callback will be called when
+* device unlink is finished.
+ * @return OC_STACK_OK in case of success and other value otherwise.
+*/
+OCStackResult OCUnlinkDevices(void* ctx,
+ const OCProvisionDev_t* pTargetDev1,
+ const OCProvisionDev_t* pTargetDev2,
+ OCProvisionResultCB resultCallback)
+{
+ OIC_LOG(INFO, TAG, "IN OCUnlinkDevices");
+ OCUuidList_t* idList = NULL;
+ size_t numOfDev = 0;
+
+ if (!pTargetDev1 || !pTargetDev2 || !pTargetDev1->doxm || !pTargetDev2->doxm)
+ {
+ OIC_LOG(ERROR, TAG, "OCUnlinkDevices : NULL parameters");
+ return OC_STACK_INVALID_PARAM;
+ }
+ if (!resultCallback)
+ {
+ OIC_LOG(INFO, TAG, "OCUnlinkDevices : NULL Callback");
+ return OC_STACK_INVALID_CALLBACK;
+ }
+ if (0 == memcmp(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID, sizeof(OicUuid_t)))
+ {
+ OIC_LOG(INFO, TAG, "OCUnlinkDevices : Same device ID");
+ return OC_STACK_INVALID_PARAM;
+ }
+
+ // Get linked devices with the first device.
+ OCStackResult res = PDMGetLinkedDevices(&(pTargetDev1->doxm->deviceID), &idList, &numOfDev);
+ if (OC_STACK_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "OCUnlinkDevices : PDMgetOwnedDevices failed");
+ goto error;
+ }
+ if (1 > numOfDev)
+ {
+ OIC_LOG(DEBUG, TAG, "OCUnlinkDevices : Can not find linked devices");
+ res = OC_STACK_INVALID_PARAM; // Input devices are not linked, No request is made
+ goto error;
+ }
+
+ // Check the linked devices contains the second device. If yes send credential DELETE request.
+ OCUuidList_t* curDev = idList;
+ while (NULL != curDev)
+ {
+ if (memcmp(pTargetDev2->doxm->deviceID.id, curDev->dev.id, sizeof(curDev->dev.id)) == 0)
+ {
+ res = SRPUnlinkDevices(ctx, pTargetDev1, pTargetDev2, resultCallback);
+ if (OC_STACK_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "OCUnlinkDevices : Failed to unlink devices.");
+ }
+ goto error;
+ }
+ curDev = curDev->next;
+ }
+ OIC_LOG(DEBUG, TAG, "No matched pair found from provisioning database");
+ res = OC_STACK_INVALID_PARAM; // Input devices are not linked, No request is made
+
+error:
+ OIC_LOG(INFO, TAG, "OUT OCUnlinkDevices");
+
+ PDMDestoryOicUuidLinkList(idList);
+ return res;
+}
+
+static OCStackResult RemoveDeviceInfoFromLocal(const OCProvisionDev_t* pTargetDev)
+{
+ // Remove credential of revoked device from SVR database
+ OCStackResult res = OC_STACK_ERROR;
+ const OicSecCred_t *cred = NULL;
+
+ OIC_LOG(DEBUG, TAG, "IN RemoveDeviceInfoFromLocal");
+ cred = GetCredResourceData(&pTargetDev->doxm->deviceID);
+ if (NULL != cred)
+ {
+ res = RemoveCredential(&cred->subject);
+ if (res != OC_STACK_RESOURCE_DELETED)
+ {
+ OIC_LOG(ERROR, TAG, "RemoveDeviceInfoFromLocal : Failed to remove credential.");
+ goto error;
+ }
+ }
+ /**
+ * Change the device status as stale status.
+ * If all request are successed, this device information will be deleted.
+ */
+ res = PDMSetDeviceState(&pTargetDev->doxm->deviceID, PDM_DEVICE_STALE);
+ if (res != OC_STACK_OK)
+ {
+ OIC_LOG(WARNING, TAG, "OCRemoveDevice : Failed to set device status as stale");
+ }
+
+ // TODO: We need to add new mechanism to clean up the stale state of the device.
+
+ // Close the DTLS session of the removed device.
+ CAResult_t caResult = CAcloseSslConnectionUsingUuid(pTargetDev->doxm->deviceID.id
+ , sizeof(pTargetDev->doxm->deviceID.id));
+ if(CA_STATUS_OK != caResult)
+ {
+ OIC_LOG_V(WARNING, TAG, "OCRemoveDevice : Failed to close (D)TLS session : %d", caResult);
+ }
+ OIC_LOG(DEBUG, TAG, "OUT RemoveDeviceInfoFromLocal");
+error:
+ return res;
+}
+
+/*
+* 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] 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.
+ * @return OC_STACK_OK in case of success and other value otherwise.
+*/
+OCStackResult OCRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
+ const OCProvisionDev_t* pTargetDev,
+ OCProvisionResultCB resultCallback)
+{
+ OIC_LOG(INFO, TAG, "IN OCRemoveDevice");
+ OCStackResult res = OC_STACK_ERROR;
+ if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
+ {
+ OIC_LOG(INFO, TAG, "OCRemoveDevice : Invalied parameters");
+ return OC_STACK_INVALID_PARAM;
+ }
+ if (!resultCallback)
+ {
+ OIC_LOG(INFO, TAG, "OCRemoveDevice : NULL Callback");
+ return OC_STACK_INVALID_CALLBACK;
+ }
+
+ // Send DELETE requests to linked devices
+ OCStackResult resReq = OC_STACK_ERROR; // Check that we have to wait callback or not.
+ resReq = SRPRemoveDevice(ctx, waitTimeForOwnedDeviceDiscovery, pTargetDev, resultCallback);
+ if (OC_STACK_OK != resReq)
+ {
+ if (OC_STACK_CONTINUE == resReq)
+ {
+ OIC_LOG(DEBUG, TAG, "OCRemoveDevice : Revoked device has no linked device except PT.");
+ }
+ else
+ {
+ OIC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to invoke SRPRemoveDevice");
+ res = resReq;
+ goto error;
+ }
+ }
+
+ res = RemoveDeviceInfoFromLocal(pTargetDev);
+ if(OC_STACK_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "Filed to remove the device information from local.");
+ goto error;
+ }
+
+ if(OC_STACK_CONTINUE == resReq)
+ {
+ /**
+ * If there is no linked device, PM does not send any request.
+ * So we should directly invoke the result callback to inform the result of OCRemoveDevice.
+ */
+ if(resultCallback)
+ {
+ resultCallback(ctx, 0, NULL, false);
+ }
+ res = OC_STACK_OK;
+ }
+
+error:
+ OIC_LOG(INFO, TAG, "OUT OCRemoveDevice");
+ return res;
+}
+
+/*
+* 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] 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.
+ * @return OC_STACK_OK in case of success and other value otherwise.
+*/
+OCStackResult OCRemoveDeviceWithUuid(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
+ const OicUuid_t* pTargetUuid,
+ OCProvisionResultCB resultCallback)
+{
+ OIC_LOG(INFO, TAG, "IN OCRemoveDeviceWithUuid");
+
+ OCStackResult res = OC_STACK_ERROR;
+ OCProvisionDev_t* pTargetDev = NULL;
+ bool discoverdFlag = false;
+ OCProvisionDev_t* pOwnedDevList = NULL;
+ OCStackResult resReq = OC_STACK_CONTINUE;
+
+ if (!pTargetUuid || 0 == waitTimeForOwnedDeviceDiscovery)
+ {
+ OIC_LOG(INFO, TAG, "OCRemoveDeviceWithUuid : Invalied parameters");
+ return OC_STACK_INVALID_PARAM;
+ }
+ if (!resultCallback)
+ {
+ OIC_LOG(INFO, TAG, "OCRemoveDeviceWithUuid : NULL Callback");
+ return OC_STACK_INVALID_CALLBACK;
+ }
+
+ char* strUuid = NULL;
+ if(OC_STACK_OK != ConvertUuidToStr(pTargetUuid, &strUuid))
+ {
+ OIC_LOG(WARNING, TAG, "Failed to covert UUID to String.");
+ goto error;
+ }
+
+ //Generate OCProvisionDev_t instance to use when target device not found on the network.
+ //In this case, the device id required only.
+ pTargetDev = (OCProvisionDev_t*)OICCalloc(1, sizeof(OCProvisionDev_t));
+ if(NULL == pTargetDev)
+ {
+ OIC_LOG(ERROR, TAG, "Failed to allocate memory.");
+ res = OC_STACK_NO_MEMORY;
+ goto error;
+ }
+ pTargetDev->doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
+ if(NULL == pTargetDev->doxm)
+ {
+ OIC_LOG(ERROR, TAG, "Failed to allocate memory.");
+ res = OC_STACK_NO_MEMORY;
+ goto error;
+ }
+ memcpy(pTargetDev->doxm->deviceID.id, pTargetUuid->id, sizeof(pTargetUuid->id));
+
+ OCUuidList_t* linkedDevices = NULL;
+ size_t numOfLinkedDevices = 0;
+ res = PDMGetLinkedDevices(pTargetUuid, &linkedDevices, &numOfLinkedDevices);
+ if(OC_STACK_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "Error in PDMGetLinkedDevices");
+ goto error;
+ }
+ PDMDestoryOicUuidLinkList(linkedDevices);
+
+ //If there is no linked devices, device revocation step can be skipped.
+ if(0 != numOfLinkedDevices)
+ {
+ OIC_LOG_V(INFO, TAG, "[%s] linked with other devices.", strUuid);
+ //2. Find owned device from the network
+ res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
+ if (OC_STACK_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "OCRemoveDeviceWithUuid : Failed to PMDeviceDiscovery");
+ goto error;
+ }
+
+ OCProvisionDev_t* tempDev = NULL;
+ LL_FOREACH(pOwnedDevList, tempDev)
+ {
+ if(memcmp(&tempDev->doxm->deviceID.id, pTargetUuid->id, sizeof(pTargetUuid->id)) == 0)
+ {
+ break;
+ }
+ }
+
+ if(NULL == tempDev)
+ {
+ OIC_LOG_V(WARNING, TAG, "Can not find [%s] on the network.", strUuid);
+ OIC_LOG_V(WARNING, TAG, "[%s]'s information will be deleted from local and other devices.", strUuid);
+ }
+ else
+ {
+ OICFree(pTargetDev->doxm);
+ OICFree(pTargetDev);
+ pTargetDev = tempDev;
+ discoverdFlag = true;
+ OIC_LOG_V(INFO, TAG, "[%s] is dectected on the network.", strUuid);
+ }
+
+ OIC_LOG_V(INFO, TAG, "Trying [%s] revocation.", strUuid);
+
+ // Send DELETE requests to linked devices
+ resReq = SRPRemoveDeviceWithoutDiscovery(ctx, pOwnedDevList, pTargetDev, resultCallback);
+ if (OC_STACK_OK != resReq)
+ {
+ if (OC_STACK_CONTINUE == resReq)
+ {
+ OIC_LOG(INFO, TAG, "OCRemoveDeviceWithUuid : Revoked device has no linked device except PT.");
+ }
+ else
+ {
+ OIC_LOG(ERROR, TAG, "OCRemoveDeviceWithUuid : Failed to invoke SRPRemoveDevice");
+ res = resReq;
+ goto error;
+ }
+ }
+ }
+ else
+ {
+ OIC_LOG_V(INFO, TAG, "There is no linked devices with [%s]", strUuid);
+ OIC_LOG(INFO, TAG, "Device discovery and SRPRemoveDevice will be skipped.");
+ }
+
+ res = RemoveDeviceInfoFromLocal(pTargetDev);
+ if(OC_STACK_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "OCRemoveDeviceWithUuid : Filed to remove the device information from local.");
+ goto error;
+ }
+
+ if(OC_STACK_CONTINUE == resReq)
+ {
+ /**
+ * If there is no linked device, PM does not send any request.
+ * So we should directly invoke the result callback to inform the result of OCRemoveDevice.
+ */
+ if(resultCallback)
+ {
+ resultCallback(ctx, 0, NULL, false);
+ }
+ res = OC_STACK_OK;
+ }
+
+error:
+ OICFree(strUuid);
+ PMDeleteDeviceList(pOwnedDevList);
+ if(pTargetDev && false == discoverdFlag)
+ {
+ OICFree(pTargetDev->doxm);
+ OICFree(pTargetDev);
+ }
+ OIC_LOG(INFO, TAG, "OUT OCRemoveDeviceWithUuid");
+ return res;
+}
+
+/*
+ * Function to reset 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.
+ * @return OC_STACK_OK in case of success and other value otherwise.
+ */
+OCStackResult OCResetDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
+ const OCProvisionDev_t* pTargetDev,
+ OCProvisionResultCB resultCallback,
+ OCClientContextDeleter deleteCallback)
+{
+ OIC_LOG(INFO, TAG, "IN OCResetDevice");
+ OCStackResult res = OC_STACK_ERROR;
+ if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
+ {
+ OIC_LOG(INFO, TAG, "OCResetDevice : Invalid parameters");
+ return OC_STACK_INVALID_PARAM;
+ }
+ if (!deleteCallback || !resultCallback)
+ {
+ OIC_LOG(INFO, TAG, "OCResetDevice : NULL Callback");
+ return OC_STACK_INVALID_CALLBACK;
+ }
+
+ // Send DELETE requests to linked devices
+ res = SRPSyncDevice(ctx, waitTimeForOwnedDeviceDiscovery, pTargetDev, resultCallback);
+ if (OC_STACK_CONTINUE == res)
+ {
+ OIC_LOG(DEBUG, TAG, "OCResetDevice : Target device has no linked device except PT.");
+ if(resultCallback)
+ {
+ resultCallback(ctx, 0, NULL, false);
+ }
+ SRPResetDevice(pTargetDev, deleteCallback);
+ res = OC_STACK_OK;
+ }
+ else if(OC_STACK_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to invoke SRPSyncDevice");
+ }
+ OIC_LOG(INFO, TAG, "OUT OCResetDevice");
+ return res;
+}
+
+/**
+ * This function resets SVR DB to its factory setting.
+ *
+ * @return OC_STACK_OK in case of successful reset and other value otherwise.
+ */
+OCStackResult OCResetSVRDB(void)
+{
+ return ResetSecureResourceInPS();
+}
+
+/**
+ * This function configures SVR DB as self-ownership.
+ *
+ *@return OC_STACK_OK in case of successful configue and other value otherwise.
+ */
+OCStackResult OCConfigSelfOwnership(void)
+{
+ return ConfigSelfOwnership();
+}
+
+/**