#include <vector>
#include <functional>
#include <mutex>
+#include <iostream>
#include <condition_variable>
#include "OCPlatform.h"
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);
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();
return m_representation;
}
-/*******************************************************/
-/*******************************************************/
OCStackResult IOT_Resource::sendRepresentation(std::shared_ptr<OCResourceRequest> _request)
{
auto response = std::make_shared<OCResourceResponse>();
return OCPlatform::sendResponse(response);
}
-/*******************************************************/
-/*******************************************************/
OCStackResult IOT_Resource::propagate()
{
OCStackResult res = OC_STACK_OK;
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;
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;
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);
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,
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;
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;
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)
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)
{
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);
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();
}
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";
return 0;
}
-/*******************************************************/
-/*******************************************************/
+
+
void kill_handler(int _sig)
{
// sd_journal_print(LOG_DEBUG, "[NMDAEMON] should stopped");
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()
-#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
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;
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");
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());
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
private:\r
std::string m_device_name;\r
std::string m_device_model;\r
+ std::string m_device_type;\r
};\r
\r
}\r
{
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();
using namespace OC;
using namespace NetworkManager;
+extern std::string cloud_host;
+
/**
* Test check correct work of IoTivity::signIn()
* 1. Input correct signIn credentials.
{
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();
{
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();
{
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},
{
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});
#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();
}
using namespace NetworkManager;
+extern std::string cloud_host;
+
class IoTDevManagerTest: public ::testing::Test
{
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()));
}
using namespace std;
using namespace NetworkManager;
+extern std::string cloud_host;
class TestIotNotification: public ::testing::Test
{
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));
}
using namespace std;
+extern std::string cloud_host;
+
/**
* Performs init, signin, sign out and cleanup
*/
{
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));
{
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);