Device info when unowned fixed. EasySetup refactored.
authorLomtev Dmytro <d.lomtev@samsung.com>
Fri, 12 May 2017 09:22:55 +0000 (12:22 +0300)
committerLomtev Dmytro <d.lomtev@samsung.com>
Fri, 12 May 2017 09:23:38 +0000 (12:23 +0300)
network-manager/nmdaemon/IoT/IOT_EasySetup.c [deleted file]
network-manager/nmdaemon/IoT/IOT_EasySetup.cpp [new file with mode: 0644]
network-manager/nmdaemon/IoT/IOT_EasySetup.h
network-manager/nmdaemon/IoT/IOT_Enrollee.cpp
network-manager/nmdaemon/IoT/IOT_Enrollee.h
network-manager/nmdaemon/main_thread.cpp
network-manager/nmlib/IoT/src/iotivity.cpp
network-manager/nmlib/easy-setup/mediator/richsdk/src/EasySetup.cpp
network-manager/scripts/deploy.sh
network-manager/test/test_iot_dev_manager.cpp

diff --git a/network-manager/nmdaemon/IoT/IOT_EasySetup.c b/network-manager/nmdaemon/IoT/IOT_EasySetup.c
deleted file mode 100644 (file)
index 3ac1f80..0000000
+++ /dev/null
@@ -1,67 +0,0 @@
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-
-#include "IOT_EasySetup.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/*******************************************************/
-/*******************************************************/
-bool IOT_EasySetup(ESDeviceProperty* _properties, IOT_EasySetup_Info* _info)
-{
-    volatile bool running = true;
-
-    if(OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
-        return false;
-
-    void cb_wifi_prov(ESWiFiProvData* _data)
-    {
-        (void)(_data);
-    }
-    void cb_conf_prov(ESDevConfProvData* _data)
-    {
-        (void)(_data);
-    }
-    void cb_cloud_prov(ESCloudProvData* _data)
-    {
-        strcpy(_info->cloud_info.host, _data->ciServer);
-        strcpy(_info->cloud_info.auth_provider, _data->authProvider);
-        strcpy(_info->cloud_info.auth_code, _data->authCode);
-        running = false;
-    }
-
-    ESResourceMask mask = ES_WIFI_RESOURCE|ES_CLOUD_RESOURCE|ES_DEVCONF_RESOURCE;
-    ESProvisioningCallbacks callbacks =
-    {
-        .WiFiProvCb = &cb_wifi_prov,
-        .DevConfProvCb = &cb_conf_prov,
-        .CloudDataProvCb = &cb_cloud_prov,
-    };
-    if(ESInitEnrollee(false, mask, callbacks) != ES_OK)
-    {
-        OCStop();
-        return false;
-    }
-
-    if(ESSetDeviceProperty(_properties) != ES_OK)
-    {
-        ESTerminateEnrollee();
-        OCStop();
-        return false;
-    }
-
-    while(running && (OCProcess() == OC_STACK_OK))
-        ;
-
-    ESTerminateEnrollee();
-    OCStop();
-
-    return !running;
-}
-
-#ifdef __cplusplus
-}
-#endif
diff --git a/network-manager/nmdaemon/IoT/IOT_EasySetup.cpp b/network-manager/nmdaemon/IoT/IOT_EasySetup.cpp
new file mode 100644 (file)
index 0000000..30e73a0
--- /dev/null
@@ -0,0 +1,71 @@
+/**
+ * @brief  IoTivity EasySetup support implementation
+ * @date   Created 12.05.2017
+ * @author Created 2017 in Samsung Ukraine R&D Center (SURC) under a contract
+ *         between LLC "Samsung Electronics Ukraine Company" (Kiev, Ukraine)
+ *         and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea).
+ *         Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
+ * @author Mail to: <A HREF="mailto:d.lomtev@samsung.com">Dmytro Lomtev, d.lomtev@samsung.com</A>
+ */
+
+#include "IOT_EasySetup.h"
+
+void cb_wifi_prov(ESWiFiProvData*) { }
+
+void cb_conf_prov(ESDevConfProvData*) { }
+
+void cb_cloud_prov(ESCloudProvData* _data)
+{
+    IOT_EasySetup_Info& info = IOT_EasySetup::instance().info;
+    strcpy(info.cloud_info.host, _data->ciServer);
+    strcpy(info.cloud_info.auth_provider, _data->authProvider);
+    strcpy(info.cloud_info.auth_code, _data->authCode);
+    IOT_EasySetup::instance().running = false;
+    IOT_EasySetup::instance().signal.notify_all();
+}
+
+IOT_EasySetup::IOT_EasySetup()
+{
+    info.cloud_info.auth_code[0] = '\0';
+    info.cloud_info.auth_provider[0] = '\0';
+    info.cloud_info.host[0] = '\0';
+    running = false;
+}
+
+IOT_EasySetup IOT_EasySetup::inst;
+
+IOT_EasySetup& IOT_EasySetup::instance()
+{
+    return inst;
+}
+
+bool IOT_EasySetup::run(const ESDeviceProperty& properties)
+{
+    running = true;
+
+    ESResourceMask mask = static_cast<ESResourceMask>(ES_WIFI_RESOURCE|ES_CLOUD_RESOURCE|ES_DEVCONF_RESOURCE);
+    ESProvisioningCallbacks callbacks =
+    {
+        .WiFiProvCb = &cb_wifi_prov,
+        .DevConfProvCb = &cb_conf_prov,
+        .CloudDataProvCb = &cb_cloud_prov,
+    };
+
+    if(ESInitEnrollee(false, mask, callbacks) != ES_OK)
+    {
+        return false;
+    }
+
+    if(ESSetDeviceProperty(const_cast<ESDeviceProperty*>(&properties)) != ES_OK)
+    {
+        ESTerminateEnrollee();
+        return false;
+    }
+
+    std::unique_lock<std::mutex> lock(lock_mutex);
+    signal.wait(lock, [this] { return !running || (OCProcess() != OC_STACK_OK) ;});
+
+    ESTerminateEnrollee();
+
+    return !running;
+}
index deb3e41..42aa289 100644 (file)
@@ -1,14 +1,21 @@
+/**
+ * @brief  IoTivity EasySetup support interface
+ * @date   Created 12.05.2017
+ * @author Created 2017 in Samsung Ukraine R&D Center (SURC) under a contract
+ *         between LLC "Samsung Electronics Ukraine Company" (Kiev, Ukraine)
+ *         and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea).
+ *         Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
+ * @author Mail to: <A HREF="mailto:d.lomtev@samsung.com">Dmytro Lomtev, d.lomtev@samsung.com</A>
+ */
 #ifndef __IOT_EASY_SETUP_H__
 #define __IOT_EASY_SETUP_H__
 
-#include <stdlib.h>
-
 #include "easysetup.h"
 #include "escommon.h"
 #include "ESEnrolleeCommon.h"
+#include <mutex>
+#include <condition_variable>
 
-/*******************************************************/
-/*******************************************************/
 typedef struct
 {
     int unused;
@@ -38,12 +45,58 @@ typedef struct
 extern "C" {
 #endif
 
-/*******************************************************/
-/*******************************************************/
-extern bool IOT_EasySetup(ESDeviceProperty* _properties, IOT_EasySetup_Info* _info);
+void cb_wifi_prov(ESWiFiProvData*);
+
+void cb_conf_prov(ESDevConfProvData*);
+
+void cb_cloud_prov(ESCloudProvData*);
 
 #ifdef __cplusplus
 }
 #endif
 
+/**
+ * @brief The IOT_EasySetup class singletone providing IoTivity EasySetup features
+ */
+class IOT_EasySetup
+{
+public:
+    /**
+     * @brief returns instance of IOT_EasySetup class
+     * @return
+     */
+    static IOT_EasySetup& instance();
+
+    /**
+     * @brief Run EasySetup process, function blocks until master setuts up cloud credentials
+     * @param properties [in] Device properties
+     * @return true if successfull
+     */
+    bool run(const ESDeviceProperty& properties);
+
+    /**
+     * @brief getInfo
+     * @return Cloud credentials
+     */
+    const IOT_EasySetup_Info& getInfo()
+    {
+        return info;
+    }
+
+private:
+    IOT_EasySetup();
+
+    IOT_EasySetup_Info info;
+    static IOT_EasySetup inst;
+    bool running = true;
+    std::mutex lock_mutex;
+    std::condition_variable signal;
+
+    friend void cb_wifi_prov(ESWiFiProvData*);
+
+    friend void cb_conf_prov(ESDevConfProvData*);
+
+    friend void cb_cloud_prov(ESCloudProvData*);
+};
+
 #endif /* __IOT_EASY_SETUP_H__ */
index 543170d..0cd5319 100644 (file)
@@ -1,4 +1,3 @@
-#include <iostream>
 #include <memory>
 #include <string>
 #include <functional>
 
 using namespace OC;
 
-/*******************************************************/
-/*******************************************************/
-/*static*/ IOT_Enrollee* IOT_Enrollee::Create(  const ESDeviceProperty& _device_properties,
-                                                const PlatformConfig& _platform_config,
-                                                const OCPlatformInfo& _platform_info,
-                                                const OCDeviceInfo& _device_info,
+IOT_Enrollee* IOT_Enrollee::Create(  const ESDeviceProperty& _device_properties,
                                                 OCConnectivityType _conn_type   )
 {
-    OCPlatform::Configure(_platform_config);
-
-    IOT_EasySetup_Info info;
-    if(!IOT_EasySetup(const_cast<ESDeviceProperty*>(&_device_properties), &info))
+    if(!IOT_EasySetup::instance().run(_device_properties))
+    {
         throw std::runtime_error("IOT_EasySetup failed");
+    }
 
-    /*
-    OCPlatform::Configure(_platform_config);
-    */
-    /*
-    if(OCPlatform::registerPlatformInfo(_platform_info) != OC_STACK_OK)
-        throw std::runtime_error("registerPlatformInfo failed");
-        */
-    if(OCPlatform::registerDeviceInfo(_device_info) != OC_STACK_OK)
-        throw std::runtime_error("registerDeviceInfo failed");
-
+    const IOT_EasySetup_Info& info = IOT_EasySetup::instance().getInfo();
     return new IOT_Enrollee(_conn_type, info.cloud_info.host, info.cloud_info.auth_provider, info.cloud_info.auth_code);
 }
-/*static*/ IOT_Enrollee* IOT_Enrollee::Create(  const PlatformConfig& _platform_config,
-                                                const OCPlatformInfo& _platform_info,
-                                                const OCDeviceInfo& _device_info,
-                                                OCConnectivityType _conn_type,
+
+IOT_Enrollee* IOT_Enrollee::Create(  OCConnectivityType _conn_type,
                                                 const std::string& _host,
                                                 const std::string& _auth_provider,
                                                 const std::string& _auth_code,
                                                 const std::string& _uid,
                                                 const std::string& _access_token    )
 {
-    OCPlatform::Configure(_platform_config);
-    /*
-    if(OCPlatform::registerPlatformInfo(_platform_info) != OC_STACK_OK)
-        throw std::runtime_error("registerPlatformInfo failed");
-        */
-    if(OCPlatform::registerDeviceInfo(_device_info) != OC_STACK_OK)
-        throw std::runtime_error("registerDeviceInfo failed");
-
     return new IOT_Enrollee(_conn_type, _host, _auth_provider, _auth_code, _uid, _access_token);
 }
 
-/*******************************************************/
-/*******************************************************/
+
+
 IOT_Enrollee::IOT_Enrollee(OCConnectivityType _conn_type, const std::string& _host, const std::string& _auth_provider, const std::string& _auth_code) :
     m_conn_type(_conn_type), m_host(_host), m_auth_provider(_auth_provider), m_auth_code(_auth_code), m_account_manager(nullptr), m_uid(""), m_access_token("")
 {
     m_account_manager = OCPlatform::constructAccountManagerObject(m_host, m_conn_type);
+    if (!m_account_manager) throw std::runtime_error("Failed to create account manager");
 
     std::mutex mtx;
     std::unique_lock<std::mutex> lck(mtx);
@@ -91,13 +66,14 @@ IOT_Enrollee::IOT_Enrollee(OCConnectivityType _conn_type, const std::string& _ho
 
     if(m_account_manager->signIn(   m_uid,
                                     m_access_token,
-                                    [&](const HeaderOptions& /*_hopt*/, const OCRepresentation& /*_rep*/, const int /*_ecode*/)
+                                    [&](const HeaderOptions& /*_hopt*/, const OCRepresentation& /*_rep*/, const int /*ecode*/)
                                     {
                                         cvar.notify_all();
                                     }   ) != OC_STACK_OK)
         throw std::runtime_error("signIn failed");
     cvar.wait(lck);
 }
+
 IOT_Enrollee::IOT_Enrollee(OCConnectivityType _conn_type, const std::string& _host, const std::string& _auth_provider, const std::string& _auth_code, const std::string& _uid, const std::string& _access_token) :
     m_conn_type(_conn_type), m_host(_host), m_auth_provider(_auth_provider), m_auth_code(_auth_code), m_account_manager(nullptr), m_uid(_uid), m_access_token(_access_token)
 {
@@ -117,14 +93,10 @@ IOT_Enrollee::IOT_Enrollee(OCConnectivityType _conn_type, const std::string& _ho
     cvar.wait(lck);
 }
 
-/*******************************************************/
-/*******************************************************/
 IOT_Enrollee::~IOT_Enrollee()
 {
 }
 
-/*******************************************************/
-/*******************************************************/
 void IOT_Enrollee::registerResource(IOT_Resource& _resource, std::string& _uri, std::string& _type, std::string& _interface, uint8_t _property)
 {
     if(OCPlatform::registerResource(    _resource.m_handle,
@@ -136,8 +108,6 @@ void IOT_Enrollee::registerResource(IOT_Resource& _resource, std::string& _uri,
         throw std::runtime_error("registerResource failed");
 }
 
-/*******************************************************/
-/*******************************************************/
 void IOT_Enrollee::bindTypeToResource(IOT_Resource& _resource, std::string& _type)
 {
     if(OCPlatform::bindTypeToResource(_resource.m_handle, _type) != OC_STACK_OK)
@@ -149,8 +119,6 @@ void IOT_Enrollee::bindInterfaceToResource(IOT_Resource& _resource, std::string&
         throw std::runtime_error("bindInterfaceToResource failed");
 }
 
-/*******************************************************/
-/*******************************************************/
 void IOT_Enrollee::publishResource(ResourceHandles& _handles, IOT_Resource& _resource)
 {
     std::mutex mtx;
@@ -180,9 +148,7 @@ void IOT_Enrollee::publishResource(ResourceHandles& _handles, IOT_Resource& _res
     cvar.wait(lck);
 }
 
-/*******************************************************/
-/*******************************************************/
-/*friend*/ std::ostream& operator<<(std::ostream& _os, const IOT_Enrollee& _obj)
+std::ostream& operator<<(std::ostream& _os, const IOT_Enrollee& _obj)
 {
     _os << "host : " << _obj.m_host << " auth_provider : " << _obj.m_auth_provider << " auth_code : " << _obj.m_auth_code << " uid : " << _obj.m_uid << " access_token : " << _obj.m_access_token;
     return _os;
index da9c813..604f3d9 100644 (file)
@@ -19,14 +19,8 @@ class IOT_Enrollee final
 {
 public:
     static IOT_Enrollee* Create(    const ESDeviceProperty& _device_properties,
-                                    const PlatformConfig& _platform_config,
-                                    const OCPlatformInfo& _platform_info,
-                                    const OCDeviceInfo& _device_info,
                                     OCConnectivityType _conn_type   );
-    static IOT_Enrollee* Create(    const PlatformConfig& _platform_config,
-                                    const OCPlatformInfo& _platform_info,
-                                    const OCDeviceInfo& _device_info,
-                                    OCConnectivityType _conn_type,
+    static IOT_Enrollee* Create(    OCConnectivityType _conn_type,
                                     const std::string& _host,
                                     const std::string& _auth_provider,
                                     const std::string& _auth_code,
index 37c76e9..6fff6ea 100644 (file)
-#include <iostream>\r
-#include <fstream>\r
-#include <string>\r
-\r
-#include "main_def.h"\r
-\r
-#include "main_thread.h"\r
-\r
-#include "socket_base.h"\r
-#include "utils.h"\r
-\r
-#include "easysetup.h"\r
-#include "ocpayload.h"\r
-\r
-#include "IOT_Enrollee.h"\r
-#include "IOT_Resource.h"\r
-#include "IOT_AirconResource.h"\r
+#include <iostream>
+#include <fstream>
+#include <string>
+
+#include "main_def.h"
+
+#include "main_thread.h"
+
+#include "socket_base.h"
+#include "utils.h"
+
+#include "easysetup.h"
+#include "ocpayload.h"
+
+#include "IOT_Enrollee.h"
+#include "IOT_Resource.h"
+#include "IOT_AirconResource.h"
 #include "IOT_BinarySwitchResource.h"
-#include "IOT_TemperatureResource.h"\r
-\r
-using namespace NMD;\r
-\r
-/*******************************************************/\r
-/*******************************************************/\r
-static const char* s_device_type = "oic.d.airconditioner";\r
-static const char* s_device_name = "IoTivity Simple Aircon";\r
-static const char* s_device_model = "IoTivity Simple Aircon Model";\r
-\r
-/*******************************************************/\r
-/*******************************************************/\r
-static FILE* server_fopen(const char *path, const char *mode)\r
-{\r
-    static const char CRED_FILE[] = "/var/run/oic_nmdaemon_db.dat";\r
-    (void) path;\r
-    return fopen(CRED_FILE, mode);\r
-}\r
-\r
-/*******************************************************/\r
-/*******************************************************/\r
-std::string getDeviceID()\r
-{\r
-    OCUUIdentity deviceId;\r
-    OCStackResult res = OC::OCPlatform::getDeviceId(&deviceId);\r
-    if (res != OC_STACK_OK)\r
-        return std::string("");\r
-\r
-    char s[128];\r
-    unsigned char *id = deviceId.id;\r
-    snprintf(s, sizeof(s), "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",\r
-            id[0],                     id[1],                  id[2],                  id[3],\r
-            id[4],                     id[5],                  id[6],                  id[7],\r
-            id[8],                     id[9],                  id[10],                 id[11],\r
-            id[12],                    id[13],                 id[14],                 id[15]);\r
-\r
-    return std::string(s);\r
-}\r
-\r
-/*******************************************************/\r
-/*******************************************************/\r
-main_thread::main_thread() : thread_base()\r
-{\r
-}\r
-/*virtual*/ main_thread::~main_thread()\r
-{\r
-}\r
-\r
-/*******************************************************/\r
-/*******************************************************/\r
-/*virtual*/ void main_thread::routine()\r
-{\r
-    write_log("[MAIN_THREADS] started\n");\r
-    try\r
-    {\r
-        /*static*/ OCPersistentStorage ps{server_fopen, fread, fwrite, fclose, unlink};\r
-        /*static*/ OC::PlatformConfig cfg{OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos, &ps};\r
-\r
-        /*\r
-        SetPlatformInfo(platformId, manufacturerName, manufacturerLink, modelNumber, dateOfManufacture, platformVersion, operatingSystemVersion, hardwareVersion, firmwareVersion, supportLink, systemTime);\r
-        SetDeviceInfo(deviceName, specVersion, dataModelVersions);\r
-        OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.wk.d");\r
-        */\r
-\r
-        OCPlatformInfo pi = {0};\r
-        OCDeviceInfo di;\r
-        OCStringLL dt;\r
-        OCStringLL dm;\r
-        dt.value = (char*)s_device_type; dt.next = NULL;\r
-        dm.value = (char*)s_device_model; dm.next = NULL;\r
-        di.deviceName = (char*)s_device_name;\r
-        di.types = &dt;\r
-        di.specVersion = NULL;\r
-        di.dataModelVersions = &dm;\r
-\r
-        IOT_Enrollee* enr = nullptr;\r
-\r
-        std::string host, auth_provider, auth_code, uid, access_token;\r
-        if(!rdat(host, auth_provider, auth_code, uid, access_token))\r
-        {\r
-            ESDeviceProperty dp = {{{WIFI_11G, WIFI_11N, WIFI_11AC, WiFi_EOF}, WIFI_5G}, {"IoTivity Simple Aircon", "IoTivity Simple Aircon Model"}};\r
-            enr = IOT_Enrollee::Create(dp, cfg, pi, di, CT_ADAPTER_TCP);\r
-            wdat(enr->host(), enr->auth_provider(), enr->auth_code(), enr->uid(), enr->access_token());\r
-        }\r
-        else\r
-        {\r
-            enr = IOT_Enrollee::Create(cfg, pi, di, CT_ADAPTER_TCP, host, auth_provider, auth_code, uid, access_token);\r
-        }\r
-\r
-        write_log(  "[MAIN_THREADS] enrollee : host[%s] auth_provider[%s] auth_code[%s] uid[%s] access_token[%s] device_id[%s]\n",\r
-                    enr->host().c_str(),\r
-                    enr->auth_provider().c_str(),\r
-                    enr->auth_code().c_str(),\r
-                    enr->uid().c_str(),\r
-                    enr->access_token().c_str(),\r
-                    getDeviceID().c_str()   );\r
-\r
-\r
-        std::string uri;\r
-        std::string rt;\r
-        std::string ri;\r
-\r
-        IOT_AirconResource aircon("/sec/aircon/0", {"x.com.samsung.da.device"}, {DEFAULT_INTERFACE, BATCH_INTERFACE, LINK_INTERFACE});\r
-        uri = aircon.getUri(); rt = aircon.getTypes()[0]; ri = aircon.getInterfaces()[0];\r
-        enr->registerResource(aircon, uri, rt, ri, OC_DISCOVERABLE);\r
-        ri = aircon.getInterfaces()[1]; enr->bindInterfaceToResource(aircon, ri);\r
-        ri = aircon.getInterfaces()[2]; enr->bindInterfaceToResource(aircon, ri);\r
-\r
-        IOT_BinarySwitchResource bswitch("/power/0", {"oic.r.switch.binary"}, {DEFAULT_INTERFACE});\r
-        uri = bswitch.getUri(); rt = bswitch.getTypes()[0]; ri = bswitch.getInterfaces()[0];\r
-        enr->registerResource(bswitch, uri, rt, ri, OC_OBSERVABLE);\r
-\r
-        IOT_TemperatureResource temp("/temperature/0", {"oic.r.temperature"}, {DEFAULT_INTERFACE});\r
-        uri = temp.getUri(); rt = temp.getTypes()[0]; ri = temp.getInterfaces()[0];\r
-        enr->registerResource(temp, uri, rt, ri, OC_OBSERVABLE);\r
-\r
-        aircon.addChildResource(&bswitch);\r
-        aircon.addChildResource(&temp);\r
-\r
-        ResourceHandles rhandles;\r
-        enr->publishResource(rhandles, aircon);\r
-\r
-        while(m_running)\r
-            std::this_thread::sleep_for(std::chrono::milliseconds(100));\r
-\r
-        delete enr;\r
-    }\r
-    catch(const std::exception& _e)\r
-    {\r
-        write_log("[MAIN_THREADS] error - %s\n", _e.what());\r
-    }\r
-    write_log("[MAIN_THREADS] stopped\n");\r
-}\r
-\r
-/*******************************************************/\r
-/*******************************************************/\r
-bool main_thread::rdat(std::string& _host, std::string& _auth_provider, std::string& _auth_code, std::string& _uid, std::string& _access_token)\r
-{\r
-    std::ifstream in(DAT_FILE_NAME);\r
-    std::vector<std::string> v;\r
-    std::string s;\r
-    while(std::getline(in, s))\r
-        v.push_back(s);\r
-    in.close();\r
-\r
-    if(v.size() == 5)\r
-    {\r
-        _host = v[0]; _auth_provider = v[1]; _auth_code = v[2]; _uid = v[3]; _access_token = v[4];\r
-        return true;\r
-    }\r
-\r
-    return false;\r
-}\r
-void main_thread::wdat(const std::string& _host, const std::string& _auth_provider, const std::string& _auth_code, const std::string& _uid, const std::string& _access_token)\r
-{\r
-    std::ofstream out(DAT_FILE_NAME, std::ofstream::out|std::ofstream::trunc);\r
-    out << _host << std::endl << _auth_provider << std::endl << _auth_code << std::endl << _uid << std::endl << _access_token;\r
-    out.close();\r
-}\r
+#include "IOT_TemperatureResource.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 = "IoTivity Simple Aircon Model";
+
+/*******************************************************/
+/*******************************************************/
+static FILE* server_fopen(const char *path, const char *mode)
+{
+    static const char CRED_FILE[] = "/var/run/oic_nmdaemon_db.dat";
+    (void) path;
+    return fopen(CRED_FILE, mode);
+}
+
+/*******************************************************/
+/*******************************************************/
+std::string getDeviceID()
+{
+    OCUUIdentity deviceId;
+    OCStackResult res = OC::OCPlatform::getDeviceId(&deviceId);
+    if (res != OC_STACK_OK)
+        return std::string("");
+
+    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 std::string(s);
+}
+
+/*******************************************************/
+/*******************************************************/
+main_thread::main_thread() : thread_base()
+{
+}
+/*virtual*/ main_thread::~main_thread()
+{
+}
+
+/*******************************************************/
+/*******************************************************/
+/*virtual*/ void main_thread::routine()
+{
+    write_log("[MAIN_THREADS] started\n");
+    try
+    {
+        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;
+        di.types = &dt;
+        di.specVersion = NULL;
+        di.dataModelVersions = &dm;
+
+        OCPlatform::Configure(cfg);
+
+        if(OCPlatform::registerDeviceInfo(di) != OC_STACK_OK)
+        {
+            write_log("[ERROR] registerDeviceInfo failed\n");
+            throw std::runtime_error("registerDeviceInfo failed");
+        }
+
+//        if(OCPlatform::registerPlatformInfo(_platform_info) != OC_STACK_OK)
+//            throw std::runtime_error("registerPlatformInfo failed");
+
+        IOT_Enrollee* enr = nullptr;
+
+        std::string host, auth_provider, auth_code, uid, access_token;
+        if(!rdat(host, auth_provider, auth_code, uid, access_token))
+        {
+            ESDeviceProperty dp = {{{WIFI_11G, WIFI_11N, WIFI_11AC, WiFi_EOF}, WIFI_5G}, {"IoTivity Simple Aircon", "IoTivity Simple Aircon Model"}};
+            enr = IOT_Enrollee::Create(dp, CT_ADAPTER_TCP);
+            wdat(enr->host(), enr->auth_provider(), enr->auth_code(), enr->uid(), enr->access_token());
+        }
+        else
+        {
+            enr = IOT_Enrollee::Create(CT_ADAPTER_TCP, host, auth_provider, auth_code, uid, access_token);
+        }
+
+        write_log(  "[MAIN_THREADS] enrollee : \n\thost[%s] \n\tauth_provider[%s] \n\tauth_code[%s] \n\tuid[%s] \n\taccess_token[%s] \n\tdevice_id[%s]\n",
+                    enr->host().c_str(),
+                    enr->auth_provider().c_str(),
+                    enr->auth_code().c_str(),
+                    enr->uid().c_str(),
+                    enr->access_token().c_str(),
+                    getDeviceID().c_str()   );
+
+        std::string uri;
+        std::string rt;
+        std::string ri;
+
+        IOT_AirconResource aircon("/sec/aircon/0", {"x.com.samsung.da.device"}, {DEFAULT_INTERFACE, BATCH_INTERFACE, LINK_INTERFACE});
+        uri = aircon.getUri(); rt = aircon.getTypes()[0]; ri = aircon.getInterfaces()[0];
+        enr->registerResource(aircon, uri, rt, ri, OC_DISCOVERABLE | OC_OBSERVABLE);
+        ri = aircon.getInterfaces()[1]; enr->bindInterfaceToResource(aircon, ri);
+        ri = aircon.getInterfaces()[2]; enr->bindInterfaceToResource(aircon, ri);
+
+        IOT_BinarySwitchResource bswitch("/power/0", {"oic.r.switch.binary"}, {DEFAULT_INTERFACE});
+        uri = bswitch.getUri(); rt = bswitch.getTypes()[0]; ri = bswitch.getInterfaces()[0];
+        enr->registerResource(bswitch, uri, rt, ri, OC_DISCOVERABLE | OC_OBSERVABLE);
+
+        IOT_TemperatureResource temp("/temperature/0", {"oic.r.temperature"}, {DEFAULT_INTERFACE});
+        uri = temp.getUri(); rt = temp.getTypes()[0]; ri = temp.getInterfaces()[0];
+        enr->registerResource(temp, uri, rt, ri, OC_DISCOVERABLE | OC_OBSERVABLE);
+
+        aircon.addChildResource(&bswitch);
+        aircon.addChildResource(&temp);
+
+        ResourceHandles rhandles;
+        enr->publishResource(rhandles, aircon);
+
+        while(m_running)
+            std::this_thread::sleep_for(std::chrono::milliseconds(100));
+
+        delete enr;
+    }
+    catch(const std::exception& _e)
+    {
+        write_log("[MAIN_THREADS] error - %s\n", _e.what());
+    }
+    write_log("[MAIN_THREADS] stopped\n");
+}
+
+/*******************************************************/
+/*******************************************************/
+bool main_thread::rdat(std::string& _host, std::string& _auth_provider, std::string& _auth_code, std::string& _uid, std::string& _access_token)
+{
+    std::ifstream in(DAT_FILE_NAME);
+    std::vector<std::string> v;
+    std::string s;
+    while(std::getline(in, s))
+        v.push_back(s);
+    in.close();
+
+    if(v.size() == 5)
+    {
+        _host = v[0]; _auth_provider = v[1]; _auth_code = v[2]; _uid = v[3]; _access_token = v[4];
+        return true;
+    }
+
+    return false;
+}
+void main_thread::wdat(const std::string& _host, const std::string& _auth_provider, const std::string& _auth_code, const std::string& _uid, const std::string& _access_token)
+{
+    std::ofstream out(DAT_FILE_NAME, std::ofstream::out|std::ofstream::trunc);
+    out << _host << std::endl << _auth_provider << std::endl << _auth_code << std::endl << _uid << std::endl << _access_token;
+    out.close();
+}
index 9831f35..1aac444 100644 (file)
@@ -88,7 +88,7 @@ struct Params
     OC::PlatformConfig config;
     OCAccountManager::Ptr accountMgr;
     OCPlatform::OCPresenceHandle presenceHandle;
-    bool subscribed;
+//    bool subscribed;
     IoTDevicesMap owned;
     PresenceHook presence_hook;
     std::mutex owned_mutex;
@@ -118,7 +118,7 @@ IoTivity::IoTivity(): signedIn(false)
                                  "0.0.0.0", 0, OC::QualityOfService::HighQos, &ps};
     params = new Params{pc};
 
-    params->subscribed = false;
+//    params->subscribed = false;
     params->presenceHandle = nullptr;
 }
 
@@ -126,10 +126,10 @@ IoTivity::~IoTivity()
 {
     if (params != nullptr)
     {
-        if (params->subscribed)
+        /*if (params->subscribed)
         {
             OCPlatform::unsubscribePresence(params->presenceHandle);
-        }
+        }*/
 
         delete params;
     }
@@ -263,11 +263,11 @@ void IoTivity::signOut()
         return;
     }
 
-    if (params->subscribed)
+    /*if (params->subscribed)
     {
         OCPlatform::unsubscribePresence(params->presenceHandle);
         params->subscribed = false;
-    }
+    }*/
 
     OCAccountManager::Ptr accountMgr = params->accountMgr;
     std::mutex mtx;
@@ -308,7 +308,7 @@ void devicePresenceHandle(Params* params, const HeaderOptions& hOptions, const O
 
         if (r.getValue("di", di))
         {
-            std::unique_lock<std::mutex> owned_mutex;
+            std::unique_lock<std::mutex> lock(params->owned_mutex);
             auto it = params->owned.find(di);
             if (it == params->owned.end())
             {
@@ -361,10 +361,9 @@ void IoTivity::removeDevicePresenceHook()
 
 IoTDevicesMap IoTivity::getOwnedDevices()
 {
-    if (params->subscribed)
     {
-        OCPlatform::unsubscribePresence(params->presenceHandle);
-        params->presenceHandle = nullptr;
+        std::unique_lock<std::mutex> lock(params->owned_mutex);
+        params->owned.clear();
     }
 
     OCPlatform::subscribeDevicePresence(
index 73eab32..5d14e95 100755 (executable)
@@ -55,7 +55,7 @@ namespace OIC
             if(resource)
             {
                 if(resource->getResourceTypes().at(0) != OC_RSRVD_ES_RES_TYPE_PROV ||
-                   resource->connectivityType() & CT_ADAPTER_TCP)
+                   resource->connectivityType() & (CT_ADAPTER_TCP | CT_IP_USE_V4))
                 {
                     OIC_LOG (ERROR, EASYSETUP_TAG, "Given resource is not valid due to wrong rt or conntype");
                     return nullptr;
index 3f19e19..f8bd9f1 100755 (executable)
@@ -28,4 +28,4 @@ sdb push ${RPMS_TO_PUSH} /tmp/nm/
 
 sdb shell "rpm -Uvi --nodeps --force --replacefiles /tmp/nm/nwmanager-*.rpm"
 
-sdb shell "/usr/apps/network-manager/test"
+#sdb shell "/usr/apps/network-manager/test"
index 9843d08..8e6f450 100644 (file)
@@ -92,6 +92,18 @@ TEST_F(IoTDevManagerTest, device_discovery)
     }
 
     ASSERT_EQ(EC_OK, NM_freeDeviceList(&dev_list));
+
+    std::this_thread::sleep_for(std::chrono::seconds(1));
+
+    ASSERT_EQ(EC_OK, NM_getOwnedDevices(ctx, &dev_list));
+
+    list_size = NM_getListSize(dev_list);
+
+    std::cout << "Repeat owned devices found: " << list_size << std::endl;
+
+    ASSERT_EQ(EC_OK, NM_deviceListForEach(dev_list, each_callback, &list_size));
+    ASSERT_EQ(0, list_size) << "For each skip " << list_size << " devices";
+    ASSERT_EQ(EC_OK, NM_freeDeviceList(&dev_list));
 }
 
 void deviceStateChangedCb(const char* id, DeviceState state, void* user_data)