Add API to allow application to set RD storage filename.
authorTodd Malsbary <todd.malsbary@intel.com>
Wed, 18 Jan 2017 18:51:25 +0000 (10:51 -0800)
committerHabib Virji <habib.virji@samsung.com>
Thu, 9 Feb 2017 18:08:30 +0000 (18:08 +0000)
Change-Id: I4afa1607ea9758747c212906a8ecf5b7f0099f72
Signed-off-by: Todd Malsbary <todd.malsbary@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/16549
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: Dan Mihai <Daniel.Mihai@microsoft.com>
Reviewed-by: Habib Virji <habib.virji@samsung.com>
resource/csdk/resource-directory/SConscript
resource/csdk/resource-directory/include/rd_database.h
resource/csdk/resource-directory/src/internal/rd_database.c
resource/csdk/resource-directory/src/rd_server.c
resource/csdk/resource-directory/unittests/rddatabase.cpp
resource/csdk/stack/include/internal/ocstackinternal.h
resource/csdk/stack/include/ocstack.h
resource/csdk/stack/src/oicresourcedirectory.c

index 51f6fd6..d66a9b2 100755 (executable)
@@ -39,9 +39,12 @@ rd_env.AppendUnique(CPPPATH = ['include',
                                 'src/internal',
                                 '../include',
                                 '../stack/include',
+                                '../stack/include/internal',
                                 '../../include',
                                 '../logger/include',
                                 '../../oc_logger/include',
+                                '../connectivity/api',
+                                '../security/include'
 ])
 
 if 'CLIENT' in rd_mode:
index b544859..a8285da 100644 (file)
@@ -30,11 +30,9 @@ extern "C" {
 /**
  * Opens the RD publish database.
  *
- * @param path to the database file.
- *
  * @return ::OC_STACK_OK in case of success or else other value.
  */
-OCStackResult OCRDDatabaseInit(const char *path);
+OCStackResult OCRDDatabaseInit();
 
 /**
  * Stores in database the published resource.
index d502c94..9271643 100644 (file)
 #include "octypes.h"
 #include "oic_malloc.h"
 #include "oic_string.h"
+#include "ocstackinternal.h"
 
 #ifdef RD_SERVER
 
 #define TAG "OIC_RD_DATABASE"
-#define RD_PATH "RD.db"
 
 static sqlite3 *gRDDB = NULL;
 
@@ -105,7 +105,7 @@ static void errorCallback(void *arg, int errCode, const char *errMsg)
     OIC_LOG_V(ERROR, TAG, "SQLLite Error: %s : %d", errMsg, errCode);
 }
 
-OCStackResult OCRDDatabaseInit(const char *path)
+OCStackResult OCRDDatabaseInit()
 {
     if (SQLITE_OK == sqlite3_config(SQLITE_CONFIG_LOG, errorCallback))
     {
@@ -113,12 +113,13 @@ OCStackResult OCRDDatabaseInit(const char *path)
     }
 
     int sqlRet;
-    sqlRet = sqlite3_open_v2(!path ? RD_PATH : path, &gRDDB, SQLITE_OPEN_READWRITE, NULL);
+    sqlRet = sqlite3_open_v2(OCRDDatabaseGetStorageFilename(), &gRDDB,
+                             SQLITE_OPEN_READWRITE, NULL);
     if (SQLITE_OK != sqlRet)
     {
         OIC_LOG(DEBUG, TAG, "RD database file did not open, as no table exists.");
         OIC_LOG(DEBUG, TAG, "RD creating new table.");
-        sqlRet = sqlite3_open_v2(!path ? RD_PATH : path, &gRDDB,
+        sqlRet = sqlite3_open_v2(OCRDDatabaseGetStorageFilename(), &gRDDB,
                                  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
         if (SQLITE_OK == sqlRet)
         {
index cb243e4..9542b47 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "rd_database.h"
 
+#include <stdlib.h>
 #include <string.h>
 #include "payload_logging.h"
 #include "ocpayload.h"
index 1958346..ed20436 100644 (file)
@@ -77,7 +77,7 @@ class RDDatabaseTests : public testing::Test {
     {
         remove("RD.db");
         OCInit("127.0.0.1", 5683, OC_CLIENT_SERVER);
-        EXPECT_EQ(OC_STACK_OK, OCRDDatabaseInit(NULL));
+        EXPECT_EQ(OC_STACK_OK, OCRDDatabaseInit());
     }
 
     virtual void TearDown()
index 31769b1..cb8e4d5 100644 (file)
@@ -332,6 +332,15 @@ OCStackResult OCUpdateResourceInsWithResponse(const char *requestUri,
                                               const OCClientResponse *response);
 #endif
 
+#if defined(RD_SERVER)
+/**
+ * Returns the filename to be used for database persistent storage.
+ *
+ * @return the filename
+ */
+const char *OCRDDatabaseGetStorageFilename();
+#endif
+
 /**
  * Delete all of the dynamically allocated elements that were created for the resource attributes.
  *
index 3d438e2..fa8258d 100644 (file)
@@ -726,6 +726,14 @@ OCResourceHandle OCGetResourceHandleAtUri(const char *uri);
 
 #ifdef RD_SERVER
 /**
+ * Sets the filename to be used for database persistent storage.
+ * @param   filename            [IN] the filename.
+ *
+ * @return  ::OC_STACK_OK on success, some other value upon failure.
+ */
+OCStackResult OCRDDatabaseSetStorageFilename(const char *filename);
+
+/**
 * Search the RD database for queries.
 *
 * @param interfaceType is the interface type that is queried.
index f0496c8..ec80d90 100644 (file)
@@ -40,7 +40,7 @@
 
 #ifdef RD_SERVER
 
-#define RD_PATH "RD.db"
+static const char *gRDPath = "RD.db";
 
 static sqlite3 *gRDDB = NULL;
 
@@ -64,6 +64,22 @@ if (SQLITE_OK != (arg)) \
     return OC_STACK_ERROR; \
 }
 
+OCStackResult OCRDDatabaseSetStorageFilename(const char *filename)
+{
+    if(!filename)
+    {
+        OIC_LOG(ERROR, TAG, "The persistent storage filename is invalid");
+        return OC_STACK_INVALID_PARAM;
+    }
+    gRDPath = filename;
+    return OC_STACK_OK;
+}
+
+const char *OCRDDatabaseGetStorageFilename()
+{
+    return gRDPath;
+}
+
 static void errorCallback(void *arg, int errCode, const char *errMsg)
 {
     OC_UNUSED(arg);
@@ -72,14 +88,14 @@ static void errorCallback(void *arg, int errCode, const char *errMsg)
     OIC_LOG_V(ERROR, TAG, "SQLLite Error: %s : %d", errMsg, errCode);
 }
 
-static OCStackResult initializeDatabase(const char *path)
+static OCStackResult initializeDatabase()
 {
     if (SQLITE_OK == sqlite3_config(SQLITE_CONFIG_LOG, errorCallback))
     {
         OIC_LOG_V(INFO, TAG, "SQLite debugging log initialized.");
     }
 
-    sqlite3_open_v2(!path ? RD_PATH : path, &gRDDB, SQLITE_OPEN_READONLY, NULL);
+    sqlite3_open_v2(OCRDDatabaseGetStorageFilename(), &gRDDB, SQLITE_OPEN_READONLY, NULL);
     if (!gRDDB)
     {
         return OC_STACK_ERROR;
@@ -222,7 +238,7 @@ exit:
 static OCStackResult CheckResources(const char *interfaceType, const char *resourceType,
         OCDiscoveryPayload *discPayload)
 {
-    if (initializeDatabase(NULL) != OC_STACK_OK)
+    if (initializeDatabase() != OC_STACK_OK)
     {
         return OC_STACK_INTERNAL_SERVER_ERROR;
     }
@@ -313,7 +329,7 @@ OCStackResult OCRDDatabaseDiscoveryPayloadCreate(const char *interfaceType,
         OIC_LOG_V(ERROR, TAG, "Payload is already allocated");
         return OC_STACK_INTERNAL_SERVER_ERROR;
     }
-    if (initializeDatabase(NULL) != OC_STACK_OK)
+    if (initializeDatabase() != OC_STACK_OK)
     {
         goto exit;
     }