Update the OCRemoveDeviceWithUuid API to delete the target device information
authorChul Lee <chuls.lee@samsung.com>
Thu, 18 Aug 2016 14:09:20 +0000 (23:09 +0900)
committerRandeep Singh <randeep.s@samsung.com>
Wed, 24 Aug 2016 07:53:07 +0000 (07:53 +0000)
in case of target device not discovered.

Change-Id: Ie6b95a5ce322bb21e64cf7f350640293391cf018
Signed-off-by: Chul Lee <chuls.lee@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/10629
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Heewon Park <h_w.park@samsung.com>
Reviewed-by: Jihun Ha <jihun.ha@samsung.com>
Reviewed-by: Randeep Singh <randeep.s@samsung.com>
resource/csdk/security/provisioning/sample/provisioningclient.c
resource/csdk/security/provisioning/src/ocprovisioningmanager.c
resource/csdk/security/provisioning/src/secureresourceprovider.c

index 0fb014a..b0454ee 100644 (file)
@@ -1076,6 +1076,7 @@ static int removeDeviceWithUuid(void)
         return -1;
     }
 
+    g_doneCB = false;
     rst = OCRemoveDeviceWithUuid("RemoveDeviceWithUUID", DISCOVERY_TIMEOUT, &revUuid, removeDeviceCB);
     if(OC_STACK_OK != rst)
     {
@@ -1083,7 +1084,6 @@ static int removeDeviceWithUuid(void)
         return -1;
     }
 
-    g_doneCB = false;
     if(waitCallbackRet())  // input |g_doneCB| flag implicitly
     {
         OIC_LOG(ERROR, TAG, "OCRemoveDeviceWithUuid callback error");
index be7b864..277e7f1 100755 (executable)
@@ -335,14 +335,14 @@ static OCStackResult RemoveDeviceInfoFromLocal(const OCProvisionDev_t* pTargetDe
     cred = GetCredResourceData(&pTargetDev->doxm->deviceID);
     if (cred == NULL)
     {
-        OIC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to get credential of remove device.");
+        OIC_LOG(ERROR, TAG, "RemoveDeviceInfoFromLocal : Failed to get credential of remove device.");
         goto error;
     }
 
     res = RemoveCredential(&cred->subject);
     if (res != OC_STACK_RESOURCE_DELETED)
     {
-        OIC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to remove credential.");
+        OIC_LOG(ERROR, TAG, "RemoveDeviceInfoFromLocal : Failed to remove credential.");
         goto error;
     }
 
@@ -458,7 +458,11 @@ OCStackResult OCRemoveDeviceWithUuid(void* ctx, unsigned short waitTimeForOwnedD
                             OCProvisionResultCB resultCallback)
 {
     OIC_LOG(INFO, TAG, "IN OCRemoveDeviceWithUuid");
+
     OCStackResult res = OC_STACK_ERROR;
+    OCProvisionDev_t* pTargetDev = NULL;
+    bool discoverdFlag = false;
+
     if (!pTargetUuid || 0 == waitTimeForOwnedDeviceDiscovery)
     {
         OIC_LOG(INFO, TAG, "OCRemoveDeviceWithUuid : Invalied parameters");
@@ -470,6 +474,13 @@ OCStackResult OCRemoveDeviceWithUuid(void* ctx, unsigned short waitTimeForOwnedD
         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;
+    }
+
     OCProvisionDev_t* pOwnedDevList = NULL;
     //2. Find owned device from the network
     res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
@@ -479,7 +490,6 @@ OCStackResult OCRemoveDeviceWithUuid(void* ctx, unsigned short waitTimeForOwnedD
         goto error;
     }
 
-    OCProvisionDev_t* pTargetDev = NULL;
     LL_FOREACH(pOwnedDevList, pTargetDev)
     {
         if(memcmp(&pTargetDev->doxm->deviceID.id, pTargetUuid->id, sizeof(pTargetUuid->id)) == 0)
@@ -488,66 +498,81 @@ OCStackResult OCRemoveDeviceWithUuid(void* ctx, unsigned short waitTimeForOwnedD
         }
     }
 
-    char* strUuid = NULL;
-    if(OC_STACK_OK != ConvertUuidToStr(pTargetUuid, &strUuid))
-    {
-        OIC_LOG(WARNING, TAG, "Failed to covert UUID to String.");
-        goto error;
-    }
-
-    if(pTargetDev)
+    if(NULL == pTargetDev)
     {
-        OIC_LOG_V(INFO, TAG, "[%s] is dectected on the network.", strUuid);
-        OIC_LOG_V(INFO, TAG, "Trying [%s] revocation.", strUuid);
+        OIC_LOG_V(WARNING, TAG, "Can not find [%s] on the network.", strUuid);
+        OIC_LOG(WARNING, TAG, "Device information will be deleted from local and other devices.");
 
-        // Send DELETE requests to linked devices
-        OCStackResult resReq = OC_STACK_ERROR; // Check that we have to wait callback or not.
-        resReq = SRPRemoveDeviceWithoutDiscovery(ctx, pOwnedDevList, pTargetDev, resultCallback);
-        if (OC_STACK_OK != resReq)
+        pTargetDev = (OCProvisionDev_t*)OICCalloc(1, sizeof(OCProvisionDev_t));
+        if(NULL == pTargetDev)
         {
-            if (OC_STACK_CONTINUE == resReq)
-            {
-                OIC_LOG(DEBUG, TAG, "OCRemoveDeviceWithUuid : Revoked device has no linked device except PT.");
-            }
-            else
-            {
-                OIC_LOG(ERROR, TAG, "OCRemoveDeviceWithUuid : Failed to invoke SRPRemoveDevice");
-                res = resReq;
-                OICFree(strUuid);
-                goto error;
-            }
+            OIC_LOG(ERROR, TAG, "Failed to memory allocation.");
+            goto error;
         }
 
-        res = RemoveDeviceInfoFromLocal(pTargetDev);
-        if(OC_STACK_OK != res)
+        pTargetDev->doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
+        if(NULL == pTargetDev->doxm)
         {
-            OIC_LOG(ERROR, TAG, "OCRemoveDeviceWithUuid : Filed to remove the device information from local.");
-            OICFree(strUuid);
+            OIC_LOG(ERROR, TAG, "Failed to memory allocation.");
             goto error;
         }
 
-        if(OC_STACK_CONTINUE == resReq)
+        //in case of can't find target device, the device id required only.
+        memcpy(pTargetDev->doxm->deviceID.id, pTargetUuid->id, sizeof(pTargetUuid->id));
+    }
+    else
+    {
+        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
+    OCStackResult resReq = OC_STACK_ERROR; // Check that we have to wait callback or not.
+    resReq = SRPRemoveDeviceWithoutDiscovery(ctx, pOwnedDevList, pTargetDev, resultCallback);
+    if (OC_STACK_OK != resReq)
+    {
+        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;
+            OIC_LOG(DEBUG, 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
+
+    res = RemoveDeviceInfoFromLocal(pTargetDev);
+    if(OC_STACK_OK != res)
     {
-        OIC_LOG_V(WARNING, TAG, "OCRemoveDeviceWithUuid : Failed to find the [%s] on the network.", strUuid);
-        res = OC_STACK_ERROR;
+        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;
 }
index 1d2891d..0b14334 100644 (file)
@@ -2136,7 +2136,12 @@ OCStackResult SRPRemoveDeviceWithoutDiscovery(void* ctx, const OCProvisionDev_t*
 {
     OIC_LOG(INFO, TAG, "IN SRPRemoveDeviceWithoutDiscovery");
 
-    if (!pTargetDev  || !pOwnedDevList)
+    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;
@@ -2158,15 +2163,14 @@ OCStackResult SRPRemoveDeviceWithoutDiscovery(void* ctx, const OCProvisionDev_t*
     res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
     if (OC_STACK_OK != res)
     {
-        OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Failed to get linked devices information");
-        return 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(DEBUG, TAG, "SRPRemoveDeviceWithoutDiscovery : No linked device found.");
-        res = OC_STACK_CONTINUE;
-        goto error;
+        OIC_LOG(WARNING, TAG, "SRPRemoveDeviceWithoutDiscovery : No linked device found.");
+        return OC_STACK_CONTINUE;
     }
 
     //2. Make a list of devices to send DELETE credential request
@@ -2182,8 +2186,7 @@ OCStackResult SRPRemoveDeviceWithoutDiscovery(void* ctx, const OCProvisionDev_t*
     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.");
-        res = OC_STACK_CONTINUE;
-        goto error;
+        return OC_STACK_CONTINUE;
     }
 
     // 3. Prepare RemoveData Context data.