Added ability to change persistent storage
authorAndriy Gudz <a.gudz@samsung.com>
Thu, 11 May 2017 11:34:27 +0000 (14:34 +0300)
committerAndriy Gudz <a.gudz@samsung.com>
Thu, 11 May 2017 11:35:38 +0000 (14:35 +0300)
network-manager/nmlib/IoT/src/iotivity.cpp
network-manager/nmlib/include/iotivity.h
network-manager/nmlib/json_policy/inc/json_policy.h
network-manager/nmlib/json_policy/src/json_policy.cpp

index 070c472..19400ae 100644 (file)
@@ -77,7 +77,6 @@ namespace NetworkManager
 
 const std::string IoTivity::DEFAULT_PROVIDER = "samsung";
 const int IoTivity::DEFAULT_TIMEOUT = 10;
-const char PERSISTENT_STORAGE_PATH[] = TOSTRING(PS_PATH) "/./nmlib_ps.dat"; /**< Path to persistent storage to get device id */
 
 struct Params
 {
@@ -92,9 +91,10 @@ struct Params
 
 IoTivity* IoTivity::instance = nullptr;
 
-static FILE* open_ps(const char*, const char *mode)
+static FILE* openPersistentStorage(const char*, const char *mode)
 {
-    return fopen(PERSISTENT_STORAGE_PATH, mode);
+    string path = IoTivity::getInstance()->getPersistentStoragePath();
+    return fopen(path.c_str(), mode);
 }
 
 std::string IoTivity::getAuthCode()
@@ -106,7 +106,9 @@ std::string IoTivity::getAuthCode()
 
 IoTivity::IoTivity(): signedIn(false)
 {
-    static OCPersistentStorage ps{open_ps, fread, fwrite, fclose, unlink};
+    persistentStoragePath = TOSTRING(PS_PATH) "/./nmlib_ps.dat";
+
+    static OCPersistentStorage ps{openPersistentStorage, fread, fwrite, fclose, unlink};
     static OC::PlatformConfig pc{OC::ServiceType::InProc, OC::ModeType::Both,
                                  "0.0.0.0", 0, OC::QualityOfService::HighQos, &ps};
     params = new Params{pc};
@@ -132,7 +134,6 @@ IoTivity* IoTivity::getInstance()
 {
     if (instance == nullptr)
     {
-        cout << "PERSISTENT_STORAGE_PATH=" << PERSISTENT_STORAGE_PATH << endl;
         try
         {
             instance = new IoTivity;
@@ -439,4 +440,14 @@ void IoTivity::unsubscribeNotifications()
     throw IoTInternalError("unsubscribeNotifications() not implemented", EC_NOT_IMPLEMENTED_YET);
 }
 
+void IoTivity::setPersistentStoragePath(std::string newPath)
+{
+    persistentStoragePath = newPath;
+}
+
+std::string IoTivity::getPersistentStoragePath()
+{
+    return persistentStoragePath;
+}
+
 }
index 2645f0d..b824cd6 100644 (file)
@@ -104,7 +104,7 @@ public:
      * @brief getDeviceID returns device id (DUID) from iotivity persistant storage
      * @return string that indicates duid
      */
-    static std::string getDeviceID();
+    std::string getDeviceID();
 
     /**
      * @brief subscribeNotifications subscribes to secure server notifications.
@@ -118,10 +118,28 @@ public:
      */
     void unsubscribeNotifications();
 
+    /**
+     * @brief setPersistentStoragePath changes default path to persistent storage file (.dat)
+     * @param psPath path to persistent storage file (.dat)
+     */
+    void setPersistentStoragePath(std::string psPath);
+
+    /**
+     * @brief getPersistentStoragePath returns path to persistent storage file (.dat)
+     * @return path to persistent storage file (.dat)
+     */
+    std::string getPersistentStoragePath();
+
 private:
     static const std::string DEFAULT_PROVIDER;
     static const int DEFAULT_TIMEOUT;
 
+    /**
+     * Path to persistent storage to get device id.
+     * Default depends on -DPS_PATH definition.
+     */
+    std::string persistentStoragePath;
+
     IoTivity();
     ~IoTivity();
     static IoTivity* instance;
index d2e7cda..2f2ed36 100644 (file)
@@ -166,7 +166,7 @@ public:
                                 vector<string> &params_) :
                                         Policy(name),
                                         sender(sender_),
-                                        device_id(NetworkManager::IoTivity::getDeviceID()),
+                     device_id(NetworkManager::IoTivity::getInstance()->getDeviceID()),
                                         action(action_),
                                         params(params_) {}
 
index fc33009..b3e9db2 100644 (file)
@@ -187,7 +187,7 @@ PolicyUpdate::PolicyUpdate(string sender_,
                                                   string &json) :
                                         Policy(name),
                                         sender(sender_),
-                                        device_id(NetworkManager::IoTivity::getDeviceID()),
+                     device_id(NetworkManager::IoTivity::getInstance()->getDeviceID()),
                                         action(action_)
 {
        params = params_;
@@ -201,7 +201,7 @@ PolicyUpdate::PolicyUpdate(string sender_,
                                                   string &json) :
                                         Policy(name),
                                         sender(sender_),
-                                        device_id(NetworkManager::IoTivity::getDeviceID()),
+                     device_id(NetworkManager::IoTivity::getInstance()->getDeviceID()),
                                         action(action_)
 {
        params.push_back(params_);