Modify the remove device related API to use client UUID of OCClientResponse
authorleechul <chuls.lee@samsung.com>
Tue, 15 Dec 2015 01:05:47 +0000 (10:05 +0900)
committerSachin Agrawal <sachin.agrawal@intel.com>
Tue, 29 Dec 2015 00:27:10 +0000 (00:27 +0000)
If we failed remove deivce, PM API will set device status as stale.
We need to add mechanism to clean up the stale status of device.
It will be uploaded to the next.

[Patch #1] : Intial upload
[Patch #2] : Update commit message
[Patch #3] : Modify according to comment.
[Patch #4,#5] : Modify according to Sachin's comments.
[Patch #6] : Modify typo

Change-Id: I0268b0820e2e0dbddeeba247bbc14d679d6fd7bb
Signed-off-by: leechul <chuls.lee@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/4579
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Sachin Agrawal <sachin.agrawal@intel.com>
resource/csdk/security/provisioning/src/ocprovisioningmanager.c
resource/csdk/security/provisioning/src/secureresourceprovider.c [changed mode: 0755->0644]

index 59f6895..68ce38c 100644 (file)
@@ -299,17 +299,43 @@ OCStackResult OCRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDis
         goto error;
     }
 
-    // Remove device info from prvisioning database.
-    res = PDMDeleteDevice(&pTargetDev->doxm->deviceID);
+    /**
+     * Change the device status as stale status.
+     * If all request are successed, this device information will be deleted.
+     */
+    res = PDMSetDeviceStale(&pTargetDev->doxm->deviceID);
     if (res != OC_STACK_OK)
     {
-        OC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to delete device in PDM.");
+        OC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to set device status as stale");
         goto error;
     }
 
-    // Check that we have to wait callback for DELETE request or not
+    // TODO: We need to add new mechanism to clean up the stale state of the device.
+
     res = resReq;
 
+    //Close the DTLS session of the removed device.
+    CAEndpoint_t* endpoint = (CAEndpoint_t *)&pTargetDev->endpoint;
+    endpoint->port = pTargetDev->securePort;
+    CAResult_t caResult = CACloseDtlsSession(endpoint);
+    if(CA_STATUS_OK != caResult)
+    {
+        OC_LOG_V(WARNING, TAG, "OCRemoveDevice : Failed to close DTLS session : %d", caResult);
+    }
+
+    /**
+     * 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(OC_STACK_CONTINUE == res)
+    {
+        if(resultCallback)
+        {
+            resultCallback(ctx, 0, NULL, false);
+        }
+        res = OC_STACK_OK;
+    }
+
 error:
     OC_LOG(INFO, TAG, "OUT OCRemoveDevice");
     return res;
old mode 100755 (executable)
new mode 100644 (file)
index 460609b..e7c12fb
@@ -1238,6 +1238,15 @@ static void registerResultForRemoveDevice(RemoveData_t *removeData, OicUuid_t *p
     // If we get suffcient result from linked devices, we have to call user callback and do free
     if (removeData->sizeOfResArray == removeData->numOfResults)
     {
+        if(!removeData->hasError)
+        {
+            // Remove device info from prvisioning database
+            if (OC_STACK_OK != PDMDeleteDevice(&removeData->revokeTargetDev->doxm->deviceID))
+            {
+                OC_LOG(ERROR, TAG, "ResultForRemoveDevice : Failed to remove device in PDM.");
+                removeData->hasError = true;
+            }
+        }
         removeData->resultCallback(removeData->ctx, removeData->numOfResults, removeData->removeRes,
                                    removeData->hasError);
         DeleteRemoveData_t(removeData);
@@ -1264,45 +1273,66 @@ static OCStackApplicationResult SRPRemoveDeviceCB(void *delDevCtx, OCDoHandle ha
     OCStackResult res = OC_STACK_ERROR;
 
     RemoveData_t* removeData = (RemoveData_t*)delDevCtx;
-    if (clientResponse)
+    if(removeData)
     {
-        // If we can get device's UUID from OCClientResponse, it'd be good to use it in here
-        // but OCIdentity in OCClientResponse is emtpy now.
-        // It seems that we can set identity to CAData_t *cadata in CAPrepareSendData() API
-        // but CA doesn't have deviceID yet.
-        //
-        //TODO: Get OCIdentity from OCClientResponse and use it for 'registerResultForRemoveDevice'
-        //      If we can't complete this task, Provisioning Database has always stale link status
-        //      when Remove device is called.
-
-        if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
+        if (clientResponse)
         {
-            res = PDMUnlinkDevices(&removeData->revokeTargetDev->doxm->deviceID,
-                                   NULL /*TODO: Replace NULL to uuid from OCClientResponse*/);
-            if (OC_STACK_OK != res)
+            OicUuid_t revDevUuid = {.id={0}};
+            if(UUID_LENGTH == clientResponse->identity.id_length)
             {
-                OC_LOG(FATAL, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
-                registerResultForRemoveDevice(removeData,
-                                          NULL /*TODO: Replace NULL to uuid from OCClientResponse*/,
-                                          OC_STACK_INCONSISTENT_DB, true);
-                return OC_STACK_DELETE_TRANSACTION;
+                memcpy(revDevUuid.id, clientResponse->identity.id, sizeof(revDevUuid.id));
+                if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
+                {
+                    res = PDMUnlinkDevices(&removeData->revokeTargetDev->doxm->deviceID, &revDevUuid);
+                    if (OC_STACK_OK != res)
+                    {
+                        OC_LOG(ERROR, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
+                               registerResultForRemoveDevice(removeData, &revDevUuid,
+                               OC_STACK_INCONSISTENT_DB, true);
+
+                        return OC_STACK_DELETE_TRANSACTION;
+                    }
+
+                    registerResultForRemoveDevice(removeData, &revDevUuid,
+                                                  OC_STACK_RESOURCE_DELETED, false);
+                }
+                else
+                {
+                    registerResultForRemoveDevice(removeData, &revDevUuid,
+                                                  clientResponse->result, true);
+                    OC_LOG(ERROR, TAG, "Unexpected result from DELETE credential request!");
+                }
+            }
+            else
+            {
+                OC_LOG_V(WARNING, TAG, "Incorrect length of device UUID was sent from %s:%d",
+                         clientResponse->devAddr.addr, clientResponse->devAddr.port);
+
+                if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
+                {
+                    /**
+                      * Since server's credential was deleted,
+                      * register result as OC_STACK_INCONSISTENT_DB with NULL UUID.
+                      */
+
+                    OC_LOG_V(ERROR, TAG, "But server's credential was deleted.");
+                    registerResultForRemoveDevice(removeData, NULL, OC_STACK_INCONSISTENT_DB, true);
+                }
+                else
+                {
+                    registerResultForRemoveDevice(removeData, NULL, clientResponse->result, true);
+                }
             }
-            registerResultForRemoveDevice(removeData,
-                                          NULL /*TODO: Replace NULL to uuid from OCClientResponse*/,
-                                          OC_STACK_RESOURCE_DELETED, false);
         }
         else
         {
-            registerResultForRemoveDevice(removeData,
-                                          NULL /*TODO: Replace NULL to uuid from OCClientResponse*/,
-                                          clientResponse->result, true);
-            OC_LOG(ERROR, TAG, "Unexpected result from DELETE credential request!");
+            registerResultForRemoveDevice(removeData, NULL, OC_STACK_ERROR, true);
+            OC_LOG(ERROR, TAG, "SRPRemoveDevices received Null clientResponse");
         }
     }
     else
     {
-        registerResultForRemoveDevice(removeData, NULL, OC_STACK_ERROR, true);
-        OC_LOG(ERROR, TAG, "SRPRemoveDevices received Null clientResponse");
+        OC_LOG(WARNING, TAG, "SRPRemoveDevices received null context");
     }
 
     return OC_STACK_DELETE_TRANSACTION;