-#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);
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)
{
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,
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)
throw std::runtime_error("bindInterfaceToResource failed");
}
-/*******************************************************/
-/*******************************************************/
void IOT_Enrollee::publishResource(ResourceHandles& _handles, IOT_Resource& _resource)
{
std::mutex mtx;
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;
-#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();
+}