Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / resource / csdk / security / src / psinterface.c
index f295fce..992fe70 100644 (file)
@@ -30,7 +30,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-#define TAG  PCF("SRM-PSI")
+#define TAG  "SRM-PSI"
 
 //SVR database buffer block size
 const size_t DB_FILE_SIZE_BLOCK = 1023;
@@ -81,7 +81,7 @@ char * GetSVRDatabase()
     int size = GetSVRDatabaseSize(ps);
     if (0 == size)
     {
-        OC_LOG (ERROR, TAG, PCF("FindSVRDatabaseSize failed"));
+        OC_LOG (ERROR, TAG, "FindSVRDatabaseSize failed");
         return NULL;
     }
 
@@ -96,13 +96,13 @@ char * GetSVRDatabase()
             size_t bytesRead = ps->read(jsonStr, 1, size, fp);
             jsonStr[bytesRead] = '\0';
 
-            OC_LOG_V(INFO, TAG, PCF("Read %d bytes from SVR database file"), bytesRead);
+            OC_LOG_V(DEBUG, TAG, "Read %d bytes from SVR database file", bytesRead);
             ps->close(fp);
             fp = NULL;
         }
         else
         {
-            OC_LOG (ERROR, TAG, PCF("Unable to open SVR database file!!"));
+            OC_LOG (ERROR, TAG, "Unable to open SVR database file!!");
         }
     }
 
@@ -128,6 +128,7 @@ OCStackResult UpdateSVRDatabase(const char* rsrcName, cJSON* jsonObj)
 {
     OCStackResult ret = OC_STACK_ERROR;
     cJSON *jsonSVRDb = NULL;
+    OCPersistentStorage* ps = NULL;
 
     // Read SVR database from PS
     char* jsonSVRDbStr = GetSVRDatabase();
@@ -140,7 +141,13 @@ OCStackResult UpdateSVRDatabase(const char* rsrcName, cJSON* jsonObj)
     OICFree(jsonSVRDbStr);
     jsonSVRDbStr = NULL;
 
-    if (jsonObj->child )
+    //If Cred resource gets updated with empty list then delete the Cred
+    //object from database.
+    if(NULL == jsonObj && (0 == strcmp(rsrcName, OIC_JSON_CRED_NAME)))
+    {
+        cJSON_DeleteItemFromObject(jsonSVRDb, rsrcName);
+    }
+    else if (jsonObj->child )
     {
         // Create a duplicate of the JSON object which was passed.
         cJSON* jsonDuplicateObj = cJSON_Duplicate(jsonObj, 1);
@@ -152,7 +159,8 @@ OCStackResult UpdateSVRDatabase(const char* rsrcName, cJSON* jsonObj)
          ACL, PStat & Doxm resources at least have default entries in the database but
          Cred resource may have no entries. The first cred resource entry (for provisioning tool)
          is created when the device is owned by provisioning tool and it's ownerpsk is generated.*/
-        if((strcmp(rsrcName, OIC_JSON_CRED_NAME) == 0) && (!jsonObj))
+        if((strcmp(rsrcName, OIC_JSON_CRED_NAME) == 0 || strcmp(rsrcName, OIC_JSON_CRL_NAME) == 0)
+                                                                                    && (!jsonObj))
         {
             // Add the fist cred object in existing SVR database json
             cJSON_AddItemToObject(jsonSVRDb, rsrcName, jsonDuplicateObj->child);
@@ -164,31 +172,31 @@ OCStackResult UpdateSVRDatabase(const char* rsrcName, cJSON* jsonObj)
             // Replace the modified json object in existing SVR database json
             cJSON_ReplaceItemInObject(jsonSVRDb, rsrcName, jsonDuplicateObj->child);
         }
+    }
 
-        // Generate string representation of updated SVR database json object
-        jsonSVRDbStr = cJSON_PrintUnformatted(jsonSVRDb);
-        VERIFY_NON_NULL(TAG,jsonSVRDbStr, ERROR);
+    // Generate string representation of updated SVR database json object
+    jsonSVRDbStr = cJSON_PrintUnformatted(jsonSVRDb);
+    VERIFY_NON_NULL(TAG,jsonSVRDbStr, ERROR);
 
-        // Update the persistent storage with new SVR database
-        OCPersistentStorage* ps = SRMGetPersistentStorageHandler();
-        if (ps && ps->open)
+    // Update the persistent storage with new SVR database
+    ps = SRMGetPersistentStorageHandler();
+    if (ps && ps->open)
+    {
+        FILE* fp = ps->open(SVR_DB_FILE_NAME, "w");
+        if (fp)
         {
-            FILE* fp = ps->open(SVR_DB_FILE_NAME, "w");
-            if (fp)
+            size_t bytesWritten = ps->write(jsonSVRDbStr, 1, strlen(jsonSVRDbStr), fp);
+            if (bytesWritten == strlen(jsonSVRDbStr))
             {
-                size_t bytesWritten = ps->write(jsonSVRDbStr, 1, strlen(jsonSVRDbStr), fp);
-                if (bytesWritten == strlen(jsonSVRDbStr))
-                {
-                    ret = OC_STACK_OK;
-                }
-                OC_LOG_V(INFO, TAG, PCF("Written %d bytes into SVR database file"), bytesWritten);
-                ps->close(fp);
-                fp = NULL;
-            }
-            else
-            {
-                OC_LOG (ERROR, TAG, PCF("Unable to open SVR database file!! "));
+                ret = OC_STACK_OK;
             }
+            OC_LOG_V(DEBUG, TAG, "Written %d bytes into SVR database file", bytesWritten);
+            ps->close(fp);
+            fp = NULL;
+        }
+        else
+        {
+            OC_LOG (ERROR, TAG, "Unable to open SVR database file!! ");
         }
     }