Added persistant storage to nmlib
authorAndriy Gudz <a.gudz@samsung.com>
Wed, 10 May 2017 13:47:15 +0000 (16:47 +0300)
committerAndriy Gudz <a.gudz@samsung.com>
Wed, 10 May 2017 13:47:15 +0000 (16:47 +0300)
demo/iot-es/src/secserver/secserver.cpp
network-manager/nmlib/CMakeLists.txt
network-manager/nmlib/IoT/src/iotivity.cpp
network-manager/test/test_IoT.cpp

index 9b88441..4e86cfa 100644 (file)
@@ -47,6 +47,30 @@ void inputNotificationData(int& notifCode, string& notifMessage, time_t& notifTi
     }
 }
 
+inline void guardErrorCode(OCStackResult resultCode, std::string message)
+{
+    if (resultCode != OC_STACK_OK)
+        throw std::exception();
+}
+
+string getDeviceID()
+{
+    OCUUIdentity deviceId;
+    guardErrorCode(OC::OCPlatform::getDeviceId(&deviceId), "OCPlatform::getDeviceId()");
+
+    char s[128];
+    unsigned char *id = deviceId.id;
+
+    snprintf(s, sizeof(s), "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+             id[0],                    id[1],                  id[2],                  id[3],
+             id[4],                    id[5],                  id[6],                  id[7],
+             id[8],                    id[9],                  id[10],                 id[11],
+             id[12],                   id[13],                 id[14],                 id[15]);
+
+    return string(s);
+
+}
+
 int main(int argc, char* argv[])
 {
     setPersStoragePath();
@@ -69,6 +93,8 @@ int main(int argc, char* argv[])
                          "0.0.0.0", 0, OC::QualityOfService::LowQos, &ps };
     OCPlatform::Configure(cfg);
 
+    cout << "Own device id:DUID:" << getDeviceID() << endl << flush;
+
     try
     {
         NotificationResource notifResource(isSecured);
index c1ee3b2..a03b9c3 100644 (file)
@@ -2,6 +2,8 @@ cmake_minimum_required (VERSION 2.8)
 
 project (${NETWORK_MANAGER_LIB_PROJECT_NAME})
 
+add_definitions(-DPS_PATH=${LIBDIR})
+
 include_directories(
        include
        REST/inc
index 5bc0c76..0d11af1 100644 (file)
@@ -18,6 +18,8 @@
 using namespace std;
 using namespace OC;
 #define TAG "NetworkManager"
+#define STRINGIFY(x) #x
+#define TOSTRING(x) STRINGIFY(x)
 
 namespace
 {
@@ -67,6 +69,7 @@ void printRepresentation(OCRepresentation rep)
         }
     }
 }
+
 }
 
 namespace NetworkManager
@@ -74,11 +77,11 @@ 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
 {
     OC::PlatformConfig config;
-    OCPersistentStorage ps;
     OCAccountManager::Ptr accountMgr;
     OCPlatform::OCPresenceHandle presenceHandle;
     bool subscribed;
@@ -89,6 +92,11 @@ struct Params
 
 IoTivity* IoTivity::instance = nullptr;
 
+static FILE* open_ps(const char*, const char *mode)
+{
+    return fopen(PERSISTENT_STORAGE_PATH, mode);
+}
+
 std::string IoTivity::getAuthCode()
 {
     // TODO add normal implementation
@@ -98,15 +106,10 @@ std::string IoTivity::getAuthCode()
 
 IoTivity::IoTivity(): signedIn(false)
 {
-    params = new Params{OC::PlatformConfig{
-            OC::ServiceType::InProc,
-            OC::ModeType::Client,
-            "0.0.0.0",
-            0,
-            OC::QualityOfService::HighQos
-        },
-//            ,{open_ps, fread, fwrite, fclose, unlink}
-    };
+    static OCPersistentStorage ps{open_ps, 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};
 
     params->subscribed = false;
     params->presenceHandle = nullptr;
@@ -129,6 +132,7 @@ IoTivity* IoTivity::getInstance()
 {
     if (instance == nullptr)
     {
+        cout << "PERSISTENT_STORAGE_PATH=" << PERSISTENT_STORAGE_PATH << endl;
         try
         {
             instance = new IoTivity;
@@ -275,11 +279,12 @@ void IoTivity::signOut()
     signedIn = false;
 }
 
-void devicePresenceHandle(Params* params, const HeaderOptions& hOptions, const OCRepresentation& rep, const int eCode, const int seqN)
+void devicePresenceHandle(Params* params, const HeaderOptions& hOptions, const OCRepresentation& rep, const int eCode,
+                          const int seqN)
 {
     if (params == nullptr) return;
 
-    auto processor = [params](const OCRepresentation& r)
+    auto processor = [params](const OCRepresentation & r)
     {
         std::string state{"off"};
         r.getValue("state", state);
@@ -296,7 +301,8 @@ void devicePresenceHandle(Params* params, const HeaderOptions& hOptions, const O
                 params->owned.emplace(di, dev);
             }
 
-            if ((bool)params->presence_hook) {
+            if ((bool)params->presence_hook)
+            {
                 params->presence_hook(di, state == "on");
             }
         }
@@ -351,7 +357,8 @@ IoTDevicesMap IoTivity::getOwnedDevices()
         params->accountMgr->host(),
         {},
         CT_DEFAULT,
-        std::bind(devicePresenceHandle, params, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)
+        std::bind(devicePresenceHandle, params, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
+                  std::placeholders::_4)
     );
 
     std::this_thread::sleep_for(std::chrono::seconds(3));
@@ -403,32 +410,29 @@ bool IoTivity::isSignedIn()
 
 string IoTivity::getDeviceID()
 {
-       OCUUIdentity deviceId;
-       OCStackResult res = OC::OCPlatform::getDeviceId(&deviceId);
-       if (res != OC_STACK_OK)
-       {
-               cout << "OCPlatform::getDeviceId error: " << res << endl;
-               return string("");
-       }
+    OCUUIdentity deviceId;
+    guardErrorCode(OC::OCPlatform::getDeviceId(&deviceId), "OCPlatform::getDeviceId()");
 
-       char s[128];
-       unsigned char *id = deviceId.id;
+    char s[128];
+    unsigned char *id = deviceId.id;
 
-       snprintf(s, sizeof(s), "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
-                       id[0],                  id[1],                  id[2],                  id[3],
-                       id[4],                  id[5],                  id[6],                  id[7],
-                       id[8],                  id[9],                  id[10],                 id[11],
-                       id[12],                 id[13],                 id[14],                 id[15]);
+    snprintf(s, sizeof(s), "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+             id[0],                    id[1],                  id[2],                  id[3],
+             id[4],                    id[5],                  id[6],                  id[7],
+             id[8],                    id[9],                  id[10],                 id[11],
+             id[12],                   id[13],                 id[14],                 id[15]);
 
-       return string(s);
+    return string(s);
 
 }
 
-void IoTivity::subscribeNotifications(NotificationCallback callback) {
+void IoTivity::subscribeNotifications(NotificationCallback callback)
+{
     throw IoTInternalError("subscribeNotifications() not implemented", EC_NOT_IMPLEMENTED_YET);
 }
 
-void IoTivity::unsubscribeNotifications() {
+void IoTivity::unsubscribeNotifications()
+{
     throw IoTInternalError("unsubscribeNotifications() not implemented", EC_NOT_IMPLEMENTED_YET);
 }
 
index 1a87430..7c463e4 100644 (file)
@@ -70,6 +70,13 @@ TEST(test_IoT, DISABLED_IOT_Enrollee)
 }
 #endif
 
+/**
+ * Test check correct work of IoTivity::signIn()
+ * 1. Input correct signIn credentials.
+ * 2. Check signedIn flag is true
+ * 3. Sign out
+ * 4. Check signedIn flag is false
+ */
 TEST(test_IoT, signInCorrect)
 {
     std::string login("login");
@@ -84,6 +91,9 @@ TEST(test_IoT, signInCorrect)
     ASSERT_FALSE(iot->isSignedIn());
 }
 
+/**
+ * Test check incorrect work of IoTivity::signIn() for empty arguments
+ */
 TEST(test_IoT, signInIncorrectInput)
 {
     std::string login("login");
@@ -94,3 +104,34 @@ TEST(test_IoT, signInIncorrectInput)
     ASSERT_ANY_THROW(IoTivity::getInstance()->signIn(host, "", password));
     ASSERT_ANY_THROW(IoTivity::getInstance()->signIn(host, login, ""));
 }
+
+static void notificationCb(std::string notifMessage) {
+    cout << "notificationCb()" << endl << flush;
+}
+
+/**
+ * Test check correct work of IoTivity::subscribeNotifications()
+ */
+TEST(test_IoT, notificationCorrect)
+{
+    std::string login("login");
+    std::string password("password");
+    std::string host("coap+tcp://106.125.46.44:5683");
+
+    ASSERT_NO_THROW(IoTivity::getInstance()->signIn(host, login, password));
+    ASSERT_ANY_THROW(IoTivity::getInstance()->subscribeNotifications(notificationCb));
+}
+
+/**
+ * Test checks IoTivity::getDeviceID()
+ * 1. Device id should be not empty
+ * 2. No exceptions should be thrown
+ */
+TEST(test_IoT, getDeviceIdCorrect)
+{
+    string duid;
+    auto iot = IoTivity::getInstance();
+    ASSERT_NO_THROW(duid = iot->getDeviceID());
+    cout << duid << endl;
+    ASSERT_NE("", duid);
+}