OCRemoveDeviceWithUuid waste time
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / src / ocprovisioningmanager.c
index 1a3f56c..7a3c40e 100644 (file)
@@ -18,6 +18,7 @@
  *
  * *****************************************************************/
 #include <stdint.h>
+#include <unistd.h>
 #include <stdio.h>
 #include <string.h>
 #include "ocprovisioningmanager.h"
@@ -701,6 +702,16 @@ error:
     return res;
 }
 
+#if !defined(MAX_WAIT_TIME)
+#define MAX_WAIT_TIME 15
+#endif
+
+static int g_reset;
+static void localResultCallback(void* ctx)
+{
+    OC_UNUSED(ctx);
+    g_reset = 0;
+}
 /*
 * Function to device revocation
 * This function will remove credential of target device from all devices in subnet.
@@ -726,7 +737,7 @@ OCStackResult OCRemoveDeviceWithUuid(void* ctx, unsigned short waitTimeForOwnedD
 
     if (!pTargetUuid || 0 == waitTimeForOwnedDeviceDiscovery)
     {
-        OIC_LOG(INFO, TAG, "OCRemoveDeviceWithUuid : Invalied parameters");
+        OIC_LOG(INFO, TAG, "OCRemoveDeviceWithUuid : Invalid parameters");
         return OC_STACK_INVALID_PARAM;
     }
     if (!resultCallback)
@@ -770,41 +781,41 @@ OCStackResult OCRemoveDeviceWithUuid(void* ctx, unsigned short waitTimeForOwnedD
     }
     PDMDestoryOicUuidLinkList(linkedDevices);
 
-    //If there is no linked devices, device revocation step can be skipped.
-    if(0 != numOfLinkedDevices)
+    //2. Find owned device from the network
+    res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
+    if (OC_STACK_OK != res)
     {
-        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;
-        }
+        OIC_LOG(ERROR, TAG, "OCRemoveDeviceWithUuid : Failed to PMDeviceDiscovery");
+        goto error;
+    }
 
-        OCProvisionDev_t* tempDev = NULL;
-        LL_FOREACH(pOwnedDevList, tempDev)
+    OCProvisionDev_t* tempDev = NULL;
+    LL_FOREACH(pOwnedDevList, tempDev)
+    {
+        if(memcmp(&tempDev->doxm->deviceID.id, pTargetUuid->id, sizeof(pTargetUuid->id)) == 0)
         {
-            if(memcmp(&tempDev->doxm->deviceID.id, pTargetUuid->id, sizeof(pTargetUuid->id)) == 0)
-            {
-                break;
-            }
+            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);
-        }
+    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 detected on the network.", strUuid);
+    }
 
+    //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);
         OIC_LOG_V(INFO, TAG, "Trying [%s] revocation.", strUuid);
 
         // Send DELETE requests to linked devices
@@ -829,26 +840,31 @@ OCStackResult OCRemoveDeviceWithUuid(void* ctx, unsigned short waitTimeForOwnedD
         OIC_LOG(INFO, TAG, "Device discovery and SRPRemoveDevice will be skipped.");
     }
 
-    res = RemoveDeviceInfoFromLocal(pTargetDev);
-    if(OC_STACK_OK != res)
+    int maxWait = MAX_WAIT_TIME;
+    g_reset = 1;
+
+    res = SRPResetDevice(pTargetDev, localResultCallback);
+    if(OC_STACK_OK == res)
     {
-        OIC_LOG(ERROR, TAG, "OCRemoveDeviceWithUuid : Filed to remove the device information from local.");
-        goto error;
+        while(g_reset && maxWait)
+        {
+            sleep(1);
+            maxWait--;
+        }
     }
 
     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);
-        }
+        resultCallback(ctx, 0, NULL, false);
         res = OC_STACK_OK;
     }
 
+    res = RemoveDeviceInfoFromLocal(pTargetDev);
+    if(OC_STACK_OK != res)
+    {
+        OIC_LOG(ERROR, TAG, "OCRemoveDeviceWithUuid : Failed to remove the device information from local.");
+        goto error;
+    }
 error:
     OICFree(strUuid);
     PMDeleteDeviceList(pOwnedDevList);
@@ -921,6 +937,44 @@ OCStackResult OCResetSVRDB(void)
 }
 
 /**
+ * This function to register callback, for getting notification if SVR DB was reseted.
+ * @param[in] ResetSVRDBCB notifier callback function.
+ * @return OC_STACK_OK in case of successful reset and other value otherwise.
+ */
+
+OCStackResult OCRegisterResetSVRDBNotifier(ResetSVRDBCB callback)
+{
+    OIC_LOG_V(INFO, TAG, "IN %s", __func__);
+
+    if(NULL != GetResetSVRDBCB()->callback)
+    {
+        OIC_LOG_V(ERROR, TAG,"%s Can't register notifier callback, unregister previous one!" ,__func__);
+        return OC_STACK_ERROR;
+    }
+
+    GetResetSVRDBCB()->callback = callback;
+
+    OIC_LOG_V(INFO, TAG, "Out %s", __func__);
+    return OC_STACK_OK;
+}
+
+
+/**
+ * This function to unregister ResetSVRDBCB notification callback.
+ */
+void OCUnregisterResetSVRDBNotifier(void)
+{
+    OIC_LOG_V(INFO, TAG, "IN %s", __func__);
+
+    if(NULL != GetResetSVRDBCB()->callback)
+    {
+        GetResetSVRDBCB()->callback = NULL;
+    }
+
+    OIC_LOG_V(INFO, TAG, "Out %s", __func__);
+}
+
+/**
  * This function configures SVR DB as self-ownership.
  *
  *@return OC_STACK_OK in case of successful configue and other value otherwise.