From 9026a7bba362e63a37e3636cfb8920edae3d26c2 Mon Sep 17 00:00:00 2001 From: Todd Malsbary Date: Wed, 18 Jan 2017 10:51:25 -0800 Subject: [PATCH] Add API to allow application to set RD storage filename. Change-Id: I4afa1607ea9758747c212906a8ecf5b7f0099f72 Signed-off-by: Todd Malsbary Reviewed-on: https://gerrit.iotivity.org/gerrit/16549 Tested-by: jenkins-iotivity Reviewed-by: Dan Mihai Reviewed-by: Habib Virji --- resource/csdk/resource-directory/SConscript | 3 +++ .../csdk/resource-directory/include/rd_database.h | 4 +--- .../resource-directory/src/internal/rd_database.c | 9 ++++---- resource/csdk/resource-directory/src/rd_server.c | 1 + .../resource-directory/unittests/rddatabase.cpp | 2 +- .../csdk/stack/include/internal/ocstackinternal.h | 9 ++++++++ resource/csdk/stack/include/ocstack.h | 8 +++++++ resource/csdk/stack/src/oicresourcedirectory.c | 26 +++++++++++++++++----- 8 files changed, 49 insertions(+), 13 deletions(-) diff --git a/resource/csdk/resource-directory/SConscript b/resource/csdk/resource-directory/SConscript index 51f6fd6..d66a9b2 100755 --- a/resource/csdk/resource-directory/SConscript +++ b/resource/csdk/resource-directory/SConscript @@ -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: diff --git a/resource/csdk/resource-directory/include/rd_database.h b/resource/csdk/resource-directory/include/rd_database.h index b544859..a8285da 100644 --- a/resource/csdk/resource-directory/include/rd_database.h +++ b/resource/csdk/resource-directory/include/rd_database.h @@ -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. diff --git a/resource/csdk/resource-directory/src/internal/rd_database.c b/resource/csdk/resource-directory/src/internal/rd_database.c index d502c94..9271643 100644 --- a/resource/csdk/resource-directory/src/internal/rd_database.c +++ b/resource/csdk/resource-directory/src/internal/rd_database.c @@ -27,11 +27,11 @@ #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) { diff --git a/resource/csdk/resource-directory/src/rd_server.c b/resource/csdk/resource-directory/src/rd_server.c index cb243e4..9542b47 100644 --- a/resource/csdk/resource-directory/src/rd_server.c +++ b/resource/csdk/resource-directory/src/rd_server.c @@ -21,6 +21,7 @@ #include "rd_database.h" +#include #include #include "payload_logging.h" #include "ocpayload.h" diff --git a/resource/csdk/resource-directory/unittests/rddatabase.cpp b/resource/csdk/resource-directory/unittests/rddatabase.cpp index 1958346..ed20436 100644 --- a/resource/csdk/resource-directory/unittests/rddatabase.cpp +++ b/resource/csdk/resource-directory/unittests/rddatabase.cpp @@ -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() diff --git a/resource/csdk/stack/include/internal/ocstackinternal.h b/resource/csdk/stack/include/internal/ocstackinternal.h index 31769b1..cb8e4d5 100644 --- a/resource/csdk/stack/include/internal/ocstackinternal.h +++ b/resource/csdk/stack/include/internal/ocstackinternal.h @@ -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. * diff --git a/resource/csdk/stack/include/ocstack.h b/resource/csdk/stack/include/ocstack.h index 3d438e2..fa8258d 100644 --- a/resource/csdk/stack/include/ocstack.h +++ b/resource/csdk/stack/include/ocstack.h @@ -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. diff --git a/resource/csdk/stack/src/oicresourcedirectory.c b/resource/csdk/stack/src/oicresourcedirectory.c index f0496c8..ec80d90 100644 --- a/resource/csdk/stack/src/oicresourcedirectory.c +++ b/resource/csdk/stack/src/oicresourcedirectory.c @@ -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; } -- 2.7.4