Added cloud server ip as program argument to tests and secserver.
authorLomtev Dmytro <d.lomtev@samsung.com>
Wed, 24 May 2017 05:59:59 +0000 (08:59 +0300)
committerLomtev Dmytro <d.lomtev@samsung.com>
Wed, 24 May 2017 05:59:59 +0000 (08:59 +0300)
device_core/iotivity_lib/IoT/IOT_Resource.cpp
device_core/iotivity_lib/IoT/IOT_Resource.h
device_core/nmdaemon/main.cpp
device_core/nmdaemon/main_thread.cpp
device_core/nmdaemon/main_thread.h
device_core/secserver/secserver.cpp
device_core/utest/test_IoT.cpp
device_core/utest/test_all.cpp
device_core/utest/test_iot_dev_manager.cpp
device_core/utest/test_iot_notification.cpp
device_core/utest/test_nmlibapi.cpp

index 670cd8e..9b3a68e 100644 (file)
@@ -3,6 +3,7 @@
 #include <vector>
 #include <functional>
 #include <mutex>
+#include <iostream>
 #include <condition_variable>
 
 #include "OCPlatform.h"
@@ -13,8 +14,6 @@
 
 using namespace OC;
 
-/*******************************************************/
-/*******************************************************/
 IOT_Resource::IOT_Resource(const std::string& _uri, const std::vector<std::string>& _types, const std::vector<std::string>& _interfaces)
 {
     m_representation.setUri(_uri);
@@ -22,16 +21,12 @@ IOT_Resource::IOT_Resource(const std::string& _uri, const std::vector<std::strin
     m_representation.setResourceInterfaces(_interfaces);
 }
 
-/*******************************************************/
-/*******************************************************/
 OCStackResult IOT_Resource::addChildResource(IOT_Resource* _resource)
 {
     m_child_resources.push_back(_resource);
     return OCPlatform::bindResource(m_handle, _resource->m_handle);
 }
 
-/*******************************************************/
-/*******************************************************/
 OCRepresentation IOT_Resource::getRepresentation()
 {
     m_representation.clearChildren();
@@ -42,8 +37,6 @@ OCRepresentation IOT_Resource::getRepresentation()
     return m_representation;
 }
 
-/*******************************************************/
-/*******************************************************/
 OCStackResult IOT_Resource::sendRepresentation(std::shared_ptr<OCResourceRequest> _request)
 {
     auto response = std::make_shared<OCResourceResponse>();
@@ -64,8 +57,6 @@ OCStackResult IOT_Resource::sendRepresentation(std::shared_ptr<OCResourceRequest
     return OCPlatform::sendResponse(response);
 }
 
-/*******************************************************/
-/*******************************************************/
 OCStackResult IOT_Resource::propagate()
 {
     OCStackResult res = OC_STACK_OK;
@@ -81,26 +72,21 @@ OCStackResult IOT_Resource::propagate()
     return res;
 }
 
-/*******************************************************/
-/*******************************************************/
 OCStackResult IOT_Resource::bindType(const std::string& _type)
 {
     return OCPlatform::bindTypeToResource(m_handle, _type);
 }
+
 OCStackResult IOT_Resource::bindInterface(const std::string& _interface)
 {
     return OCPlatform::bindInterfaceToResource(m_handle, _interface);
 }
 
-/*******************************************************/
-/*******************************************************/
 OCStackResult IOT_Resource::registerResource(std::string& _uri, std::string& _type, std::string& _interface, uint8_t _property)
 {
     return OCPlatform::registerResource(m_handle, _uri, _type, _interface, std::bind(&IOT_Resource::entityHandler, this, std::placeholders::_1), _property);
 }
 
-/*******************************************************/
-/*******************************************************/
 OCStackResult IOT_Resource::publishResource(ResourceHandles& _handles, const std::string& _host, OCConnectivityType _conn_type)
 {
     OCStackResult res = OC_STACK_OK;
@@ -135,42 +121,34 @@ OCStackResult IOT_Resource::publishResource(ResourceHandles& _handles, const std
     return res;
 }
 
-/*******************************************************/
-/*******************************************************/
-/*static*/ std::vector<std::shared_ptr<OCResource>> IOT_Resource::findResources(const std::string& _host, const std::string& _search_query, OCConnectivityType _connectivity_type, int _wait_for_sec)
+std::shared_ptr<OCResource> IOT_Resource::findResource(const std::string& _host, const std::string& _search_query, OCConnectivityType _connectivity_type, int _wait_for_sec, const std::string& _type)
 {
-    std::vector<std::shared_ptr<OCResource>> res;
+    static std::mutex mtx;
+    std::shared_ptr<OCResource> res;
 
     OCPlatform::findResource(   _host,
                                 _search_query,
                                 _connectivity_type,
-                                [&](std::shared_ptr<OCResource> _resource)
+                                [&res, &_type](std::shared_ptr<OCResource> _resource)
                                 {
-                                    res.push_back(_resource);
+                                    std::unique_lock<std::mutex> lock(mtx);
+                                    if (!res)
+                                    {
+                                        for(auto i : _resource->getResourceTypes())
+                                        {
+                                            if(i.compare(_type) == 0)
+                                            {
+                                                res = _resource;
+                                            }
+                                        }
+                                    }
                                 }   );
     std::this_thread::sleep_for(std::chrono::seconds(_wait_for_sec));
 
     return res;
 }
-/*static*/ std::shared_ptr<OCResource> IOT_Resource::findResource(const std::string& _host, const std::string& _search_query, OCConnectivityType _connectivity_type, int _wait_for_sec, const std::string& _type)
-{
-    std::vector<std::shared_ptr<OCResource>> res = findResources(_host, _search_query, _connectivity_type, _wait_for_sec);
 
-    for(auto it : res)
-    {
-        for(auto i : it->getResourceTypes())
-        {
-            if(i.compare(_type) == 0)
-                return it;
-        }
-    }
-
-    return nullptr;
-}
-
-/*******************************************************/
-/*******************************************************/
-/*static*/ std::vector<OCRepresentation> IOT_Resource::getResourceRepresentations(const std::shared_ptr<OCResource> _resource, const QueryParamsMap& _query_params)
+std::vector<OCRepresentation> IOT_Resource::getResourceRepresentations(const std::shared_ptr<OCResource> _resource, const QueryParamsMap& _query_params)
 {
     std::vector<OCRepresentation> res;
 
@@ -190,7 +168,8 @@ OCStackResult IOT_Resource::publishResource(ResourceHandles& _handles, const std
 
     return res;
 }
-/*static*/ OCRepresentation IOT_Resource::getResourceRepresentation(const std::shared_ptr<OCResource> _resource, const QueryParamsMap& _query_params, const std::string& _type)
+
+OCRepresentation IOT_Resource::getResourceRepresentation(const std::shared_ptr<OCResource> _resource, const QueryParamsMap& _query_params, const std::string& _type)
 {
     std::vector<OCRepresentation> res = getResourceRepresentations(_resource, _query_params);
 
@@ -203,9 +182,7 @@ OCStackResult IOT_Resource::publishResource(ResourceHandles& _handles, const std
     return OCRepresentation();
 }
 
-/*******************************************************/
-/*******************************************************/
-/*static*/ std::shared_ptr<OCResource> IOT_Resource::createResourceObject(  const std::string& _host,
+std::shared_ptr<OCResource> IOT_Resource::createResourceObject(  const std::string& _host,
                                                                             const std::string& _uri,
                                                                             OCConnectivityType _connectivity_type,
                                                                             bool _is_observable,
@@ -215,9 +192,7 @@ OCStackResult IOT_Resource::publishResource(ResourceHandles& _handles, const std
     return OCPlatform::constructResourceObject(_host, _uri, _connectivity_type, _is_observable, _types, _interfaces);
 }
 
-/*******************************************************/
-/*******************************************************/
-/*static*/ OCStackResult IOT_Resource::observeResource(const std::shared_ptr<OCResource> _resource, const QueryParamsMap& _query_params, ObserveCallback _observe_handler)
+OCStackResult IOT_Resource::observeResource(const std::shared_ptr<OCResource> _resource, const QueryParamsMap& _query_params, ObserveCallback _observe_handler)
 {
     if(!_resource)
         return OC_STACK_ERROR;
@@ -225,9 +200,7 @@ OCStackResult IOT_Resource::publishResource(ResourceHandles& _handles, const std
     return _resource->observe(ObserveType::Observe, _query_params, _observe_handler);
 }
 
-/*******************************************************/
-/*******************************************************/
-/*static*/ OCStackResult IOT_Resource::post(const std::shared_ptr<OC::OCResource> _resource, const std::string& _type, const std::string& _interface, const OCRepresentation& _representation, const QueryParamsMap& _query_params)
+OCStackResult IOT_Resource::post(const std::shared_ptr<OC::OCResource> _resource, const std::string& _type, const std::string& _interface, const OCRepresentation& _representation, const QueryParamsMap& _query_params)
 {
     OCStackResult res = OC_STACK_ERROR;
 
@@ -252,9 +225,7 @@ OCStackResult IOT_Resource::publishResource(ResourceHandles& _handles, const std
     return res;
 }
 
-/*******************************************************/
-/*******************************************************/
-/*static*/ std::string IOT_Resource::representationToString(const OCRepresentation& _rep)
+std::string IOT_Resource::representationToString(const OCRepresentation& _rep)
 {
     std::ostringstream oss;
     for(auto it = _rep.begin(); it != _rep.end(); ++it)
@@ -262,9 +233,7 @@ OCStackResult IOT_Resource::publishResource(ResourceHandles& _handles, const std
     return oss.str();
 }
 
-/*******************************************************/
-/*******************************************************/
-/*static*/ void IOT_Resource::printRepresentation(const OCRepresentation &_rep)
+void IOT_Resource::printRepresentation(const OCRepresentation &_rep)
 {
     for(auto itr = _rep.begin(); itr != _rep.end(); ++itr)
     {
index 5eb7a71..88b5f7c 100644 (file)
@@ -53,7 +53,6 @@ public:
 
     virtual OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> _request) = 0;
 
-    static std::vector<std::shared_ptr<OC::OCResource>> findResources(const std::string& _host, const std::string& _search_query, OCConnectivityType _connectivity_type, int _wait_for_sec);
     static std::shared_ptr<OC::OCResource> findResource(const std::string& _host, const std::string& _search_query, OCConnectivityType _connectivity_type, int _wait_for_sec, const std::string& _type);
 
     static std::vector<OCRepresentation> getResourceRepresentations(const std::shared_ptr<OC::OCResource> _resource, const QueryParamsMap& _query_params);
index d9e59ca..b3211ea 100644 (file)
 
 using namespace NMD;
 
-/*******************************************************/
-/*******************************************************/
-static std::string g_device_name = "";
-static std::string g_device_model = "";
+static std::string g_device_name = "Generic device";
+static std::string g_device_model = "Model 1";
+static std::string g_device_type = "iotdevice";
 
 static main_thread g_main_thread;
 
 static bool volatile g_running = true;
 
-/*******************************************************/
-/*******************************************************/
 static void kill_handler(int _sig);
 
 static bool threads_init(void);
 static void threads_done(void);
 
-/*******************************************************/
-/*******************************************************/
 int main(int argc, char** argv)
 {
     struct sigaction act;
     int ret;
 
-    if(argc < 3)
+    if(argc < 4)
     {
-        std::cout << "[NMDAEMON] please specify device name and model" << std::endl;
+        std::cout << "[NMDAEMON] please specify device name, model and type [optional path]" << std::endl;
         std::cout << "[NMDAEMON] stopped" << std::endl;
         return 1;
     }
 
     g_device_name = std::string({argv[1]});
     g_device_model = std::string({argv[2]});
+    g_device_type = std::string({argv[3]});
 
-    if(argc != 4)
+    if(argc != 5)
     {
         std::string curr_path = current_path();
 
@@ -66,7 +62,7 @@ int main(int argc, char** argv)
     }
     else
     {
-        std::string path{argv[3]};
+        std::string path{argv[4]};
         dat_file_path_name = path + "/nmdaemon.dat";
         cfg_file_path_name = path + "/nmdaemon.cfg";
         log_file_path_name = path + "/nmdaemon.log";
@@ -119,8 +115,8 @@ int main(int argc, char** argv)
     return 0;
 }
 
-/*******************************************************/
-/*******************************************************/
+
+
 void kill_handler(int _sig)
 {
 //    sd_journal_print(LOG_DEBUG, "[NMDAEMON] should stopped");
@@ -128,12 +124,11 @@ void kill_handler(int _sig)
     g_running = false;
 }
 
-/*******************************************************/
-/*******************************************************/
 bool threads_init(void)
 {
     g_main_thread.set_device_name(g_device_name);
     g_main_thread.set_device_model(g_device_model);
+    g_main_thread.set_device_type(g_device_type);
     return g_main_thread.start();
 }
 void threads_done()
index 2c907f6..1472ceb 100644 (file)
@@ -1,8 +1,5 @@
-#include <iostream>
-#include <fstream>
 #include <string>
 #include <memory>
-#include <string.h>
 
 #include "main_thread.h"
 #include "rmi_thread.h"
 
 using namespace NMD;
 
-/*******************************************************/
-/*******************************************************/
-static const char* s_device_type = "oic.d.airconditioner";
-//static const char* s_device_name = "IoTivity Simple Aircon";
-//static const char* s_device_model = "Model ZZZ";
-
-/*******************************************************/
-/*******************************************************/
 static FILE* server_fopen(const char* _path, const char* _mode)
 {
     (void)_path;
     return fopen(dat_file_path_name.c_str(), _mode);
 }
 
-/*******************************************************/
-/*******************************************************/
-main_thread::main_thread(std::string _device_name, std::string _device_model) :
-    thread_base(), m_device_name(_device_name), m_device_model(_device_model)
+main_thread::main_thread(const std::string& _device_name, const std::string& _device_model, const std::string& _device_type) :
+    thread_base(), m_device_name(_device_name), m_device_model(_device_model), m_device_type(_device_type)
 {
 }
-/*virtual*/ main_thread::~main_thread()
+
+main_thread::~main_thread()
 {
 }
 
-/*******************************************************/
-/*******************************************************/
-/*virtual*/ void main_thread::routine()
+void main_thread::routine()
 {
     write_log("[MAIN_THREADS] started\n");
     try
@@ -58,13 +44,11 @@ main_thread::main_thread(std::string _device_name, std::string _device_model) :
         OCPersistentStorage ps{server_fopen, fread, fwrite, fclose, unlink};
         OC::PlatformConfig cfg{OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos, &ps};
 
-        //OCPlatformInfo pi = {0};
         OCDeviceInfo di;
         OCStringLL dt;
         OCStringLL dm;
-        dt.value = (char*)s_device_type; dt.next = NULL;
-//        dm.value = (char*)s_device_model; dm.next = NULL;
-//        di.deviceName = (char*)s_device_name;
+
+        dt.value = (char*)m_device_type.c_str(); dt.next = NULL;
         dm.value = (char*)m_device_model.c_str(); dm.next = NULL;
         di.deviceName = (char*)m_device_name.c_str();
         di.types = &dt;
@@ -73,9 +57,6 @@ main_thread::main_thread(std::string _device_name, std::string _device_model) :
 
         OCPlatform::Configure(cfg);
 
-        //if(OCPlatform::registerPlatformInfo(_platform_info) != OC_STACK_OK)
-        //  throw std::runtime_error("registerPlatformInfo failed");
-
         if(OCPlatform::registerDeviceInfo(di) != OC_STACK_OK)
             throw std::runtime_error("registerDeviceInfo failed");
 
@@ -84,7 +65,6 @@ main_thread::main_thread(std::string _device_name, std::string _device_model) :
         nmdaemon_config config;
         if(!read_config(config))
         {
-//            ESDeviceProperty dp = {{{WIFI_11G, WIFI_11N, WIFI_11AC, WiFi_EOF}, WIFI_5G}, {"IoTivity Simple Aircon", "Model ZZZ"}};
             ESDeviceProperty dp = {{{WIFI_11G, WIFI_11N, WIFI_11AC, WiFi_EOF}, WIFI_5G}, {"", ""}};
             if(m_device_name.size() < OIC_STRING_MAX_VALUE)
                 strcpy(dp.DevConf.deviceName, m_device_name.c_str());
index 8676cdc..42c5ab3 100644 (file)
 namespace NMD\r
 {\r
 \r
-/*******************************************************/\r
-/*******************************************************/\r
 class main_thread : public thread_base\r
 {\r
 public:\r
-    main_thread(std::string _device_name = "", std::string _device_model = "");\r
+    main_thread(const std::string& _device_name = "", const std::string& _device_model = "", const std::string& _device_type = "");\r
 \r
-    void set_device_name(std::string _device_name)\r
+    void set_device_name(const std::string& _device_name)\r
     {\r
         m_device_name = _device_name;\r
     }\r
-    void set_device_model(std::string _device_model)\r
+\r
+    void set_device_model(const std::string& _device_model)\r
     {\r
         m_device_model = _device_model;\r
     }\r
 \r
+    void set_device_type(const std::string& _device_type)\r
+    {\r
+        m_device_type = "oic.d." + _device_type;\r
+    }\r
+\r
     virtual ~main_thread();\r
 \r
     virtual void routine();\r
@@ -33,6 +37,7 @@ public:
 private:\r
     std::string m_device_name;\r
     std::string m_device_model;\r
+    std::string m_device_type;\r
 };\r
 \r
 }\r
index fb2055c..84c0cfd 100644 (file)
@@ -80,7 +80,6 @@ static void mainLoop()
 {
     std::string login("login");
     std::string password("password");
-//    std::string host("coap+tcp://106.125.46.44:5683");
     std::string host("coap+tcp://" + cloud_ip + ":5683");
 
     auto iot = IoTivity::getInstance();
index 645855c..88f8ef6 100644 (file)
@@ -35,6 +35,8 @@ using namespace std;
 using namespace OC;
 using namespace NetworkManager;
 
+extern std::string cloud_host;
+
 /**
  * Test check correct work of IoTivity::signIn()
  * 1. Input correct signIn credentials.
@@ -46,7 +48,7 @@ TEST(test_IoT, signInCorrect)
 {
     std::string login("login");
     std::string password("password");
-    std::string host("coap+tcp://106.125.46.44:5683");
+    std::string host(cloud_host);
 
     auto iot = IoTivity::getInstance();
 
@@ -63,7 +65,7 @@ TEST(test_IoT, signInIncorrectInput)
 {
     std::string login("login");
     std::string password("password");
-    std::string host("coap+tcp://106.125.46.44:5683");
+    std::string host(cloud_host);
 
     auto iot = IoTivity::getInstance();
 
@@ -113,7 +115,7 @@ TEST(test_IoT, test_IOT_ReportSender)
         {
             OCPlatform::Configure(cfg);
 
-            IOT_Enroller enroller(CT_ADAPTER_TCP, "coap+tcp://106.125.46.44:5683", "Samsung", "AuthCode_A");
+            IOT_Enroller enroller(CT_ADAPTER_TCP, cloud_host, "Samsung", "AuthCode_A");
 
             shared_ptr<OCResource> report_res = IOT_Resource::findResource( enroller.host(),
                                                                             {OC_RSRVD_WELL_KNOWN_URI},
@@ -151,21 +153,21 @@ TEST(test_IoT, test_IOT_PolicySender)
 {
     OCPersistentStorage ps{client_open, fread, fwrite, fclose, unlink};
     PlatformConfig cfg{ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, QualityOfService::HighQos, &ps};
-
+    std::string host(cloud_host);
     bool res = false;
 
     ASSERT_NO_THROW(
         {
             OCPlatform::Configure(cfg);
 
-            IOT_Enroller enroller(CT_ADAPTER_TCP, "coap+tcp://106.125.46.44:5683", "Samsung", "AuthCode_A");
+            IOT_Enroller enroller(CT_ADAPTER_TCP, host, "Samsung", "AuthCode_A");
 
             shared_ptr<OCResource> policy_res = IOT_Resource::findResource(enroller.host(), {OC_RSRVD_WELL_KNOWN_URI}, static_cast<OCConnectivityType>(CT_DEFAULT), 1, "core.policy");
             if(!policy_res)
                 throw runtime_error("Failed to find policy resource");
 
             string device_id = IOT_Device::getDeviceId();
-            string agent_id = /*"AGENT_NUMBER_42"*/"3574462a-7efa-9449-4d9f-488734c79c0a";
+            string agent_id = "3574462a-7efa-9449-4d9f-488734c79c0a";
             IOT_PolicyItem item1("usb", "off", {});
             IOT_PolicyItem item2("screen-capture", "on", {});
             IOT_PolicyGroup group("tv-extension", {item1, item2});
index 5cee3cb..ba68e09 100644 (file)
@@ -1,9 +1,15 @@
 #include <iostream>
-
+#include <string>
 #include <gtest/gtest.h>
 
+std::string cloud_host{"coap+tcp://106.125.46.44:5683"};
+
 int main(int argc, char** argv)
 {
+    if (argc > 1 && argv[1][0] != '-')
+    {
+        cloud_host = std::string{"coap+tcp://"} + argv[1] + std::string{":5683"};
+    }
     ::testing::InitGoogleTest(&argc, argv);
     return RUN_ALL_TESTS();
 }
index a19f06a..147d0fd 100644 (file)
@@ -9,6 +9,8 @@
 
 using namespace NetworkManager;
 
+extern std::string cloud_host;
+
 class IoTDevManagerTest: public ::testing::Test
 {
 public:
@@ -18,7 +20,7 @@ public:
 
         std::string login("login");
         std::string password("password");
-        std::string host("coap+tcp://106.125.46.44:5683");
+        std::string host(cloud_host);
 
         ASSERT_EQ(EC_OK, NM_signIn(ctx, host.c_str(), login.c_str(), password.c_str()));
     }
index c31ca94..d310fd5 100644 (file)
@@ -18,6 +18,7 @@
 
 using namespace std;
 using namespace NetworkManager;
+extern std::string cloud_host;
 
 class TestIotNotification: public ::testing::Test
 {
@@ -28,7 +29,7 @@ public:
 
         std::string login("login");
         std::string password("password");
-        std::string host("coap+tcp://106.125.46.44:5683");
+        std::string host(cloud_host);
 
         ASSERT_NO_THROW(iot->signIn(host, login, password));
     }
index 2212b44..28fc37a 100644 (file)
@@ -18,6 +18,8 @@
 
 using namespace std;
 
+extern std::string cloud_host;
+
 /**
  * Performs init, signin, sign out and cleanup
  */
@@ -25,7 +27,7 @@ TEST(test_nmlibapi, signInCorrect)
 {
     std::string login("login");
     std::string password("password");
-    std::string host("coap+tcp://106.125.46.44:5683");
+    std::string host(cloud_host);
 
     NM_hContext ctx;
     ASSERT_EQ(EC_OK, NM_init(&ctx));
@@ -74,7 +76,7 @@ TEST(test_nmlibapi, notificationCorrect)
 {
     std::string login("login");
     std::string password("password");
-    std::string host("coap+tcp://106.125.46.44:5683");
+    std::string host(cloud_host);
 
     std::mutex notificationMtx;
     std::unique_lock<std::mutex> notificationLock(notificationMtx);