/*******************************************************/
OCStackResult IOT_Resource::publishResource(ResourceHandles& _handles, const std::string& _host, OCConnectivityType _conn_type)
{
+ OCStackResult res = OC_STACK_OK;
+
std::mutex mtx;
std::unique_lock<std::mutex> lck(mtx);
std::condition_variable cvar;
- OCStackResult res = OC_STACK_OK;
-
res = RDClient::Instance().publishResourceToRD( _host,
_conn_type,
_handles,
{
std::vector<OCRepresentation> res;
- std::mutex mtx;
- std::unique_lock<std::mutex> lck(mtx);
- std::condition_variable cvar;
- _resource->get( _query_params,
- [&](const HeaderOptions&, const OCRepresentation& _rep, const int /*_ecode*/)
- {
- res = _rep.getChildren();
- cvar.notify_all();
- } );
- cvar.wait(lck);
+ if(_resource)
+ {
+ std::mutex mtx;
+ std::unique_lock<std::mutex> lck(mtx);
+ std::condition_variable cvar;
+ _resource->get( _query_params,
+ [&](const HeaderOptions&, const OCRepresentation& _rep, const int /*_ecode*/)
+ {
+ res = _rep.getChildren();
+ cvar.notify_all();
+ } );
+ cvar.wait(lck);
+ }
return res;
}
/*******************************************************/
/*static*/ 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 res = OC_STACK_OK;
+ OCStackResult res = OC_STACK_ERROR;
- std::mutex mtx;
- std::unique_lock<std::mutex> lck(mtx);
- std::condition_variable cvar;
- res = _resource->post( _type,
- _interface,
- _representation,
- _query_params,
- [&](const HeaderOptions&, const OCRepresentation& _rep, const int _ecode)
- {
- cvar.notify_all();
- } );
- cvar.wait(lck);
+ if(_resource)
+ {
+ std::mutex mtx;
+ std::unique_lock<std::mutex> lck(mtx);
+ std::condition_variable cvar;
+ res = _resource->post( _type,
+ _interface,
+ _representation,
+ _query_params,
+ [&](const HeaderOptions&, const OCRepresentation& _rep, const int _ecode)
+ {
+ cvar.notify_all();
+ } );
+ cvar.wait(lck);
+ }
return res;
}
string(REPLACE " " "_" ProjectId ${ProjectId})
project(${ProjectId} C CXX)
-include_directories( IoT )
+include_directories(../iotivity_lib/IoT)
-file(GLOB IOT_SOURCES IoT/*.cpp IoT/*.c)
+file(GLOB IOT_SOURCES ../iotivity_lib/IoT/*.cpp)
file(GLOB NMDAEMON_SOURCES *.cpp)
SET (SOURCES
+++ /dev/null
-#include <memory>
-#include <string>
-#include <vector>
-
-#include "OCPlatform.h"
-#include "OCApi.h"
-
-#include "IOT_Resource.h"
-#include "IOT_AirconResource.h"
-
-using namespace OC;
-
-/*******************************************************/
-/*******************************************************/
-IOT_AirconResource::IOT_AirconResource(const std::string& _uri, const std::vector<std::string>& _types, const std::vector<std::string>& _interfaces) :
- IOT_Resource(_uri, _types, _interfaces)
-{
-}
-
-/*******************************************************/
-/*******************************************************/
-/*virtual*/ OCEntityHandlerResult IOT_AirconResource::entityHandler(std::shared_ptr<OCResourceRequest> _request)
-{
- OCEntityHandlerResult res = OC_EH_ERROR;
-
- if(_request)
- {
- std::string rt = _request->getRequestType();
- int rf = _request->getRequestHandlerFlag();
-
- if(rf & RequestHandlerFlag::RequestFlag)
- {
- if(rt == "GET")
- {
- std::string fr = _request->getQueryParameters().find("if")->second;
- if(fr.compare(LINK_INTERFACE) == 0)
- {
- if(sendRepresentation(_request) == OC_STACK_OK)
- res = OC_EH_OK;
- }
- else
- {
- res = OC_EH_FORBIDDEN;
- }
- }
- else if (rt == "PUT")
- {
-#if 0
- cout << "\t\t\trequestType : PUT\n";
- // Call these functions to prepare the response for child resources and
- // then send the final response using sendRoomResponse function
-
- /*
- for (auto it = m_childResources.begin();
- it != m_childResources.end(); it++)
- {
- (*it)->entityHandler(request);
- }
-
- if (OC_STACK_OK == sendRepresentation(request))
- {
- ehResult = OC_EH_OK;
- }
- */
-#endif
- }
- else if(rt == "POST")
- {
- // POST request operations
- }
- else if (rt == "DELETE")
- {
- // DELETE request operations
- }
- }
-
- if(rf & RequestHandlerFlag::ObserverFlag)
- {
- //cout << "\t\trequestFlag : Observer\n";
- }
- }
- else
- {
- //cout << "Request invalid" << endl;
- }
-
- return res;
-}
+++ /dev/null
-#ifndef __IOT_AIRCON_RESOURCE_H__
-#define __IOT_AIRCON_RESOURCE_H__
-
-#include <memory>
-#include <string>
-#include <vector>
-
-#include "OCPlatform.h"
-#include "OCApi.h"
-
-#include "IOT_Resource.h"
-
-using namespace OC;
-
-/*******************************************************/
-/*******************************************************/
-class IOT_AirconResource : public IOT_Resource
-{
-public:
- IOT_AirconResource(const std::string& _uri, const std::vector<std::string>& _types, const std::vector<std::string>& _interfaces);
- IOT_AirconResource(const IOT_AirconResource& _obj) = default;
-
- virtual ~IOT_AirconResource() = default;
-
- IOT_AirconResource& operator=(const IOT_AirconResource& _obj) = default;
-
- virtual OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> _request);
-};
-
-#endif /* __IOT_AIRCON_RESOURCE_H__ */
+++ /dev/null
-#include <memory>
-#include <string>
-#include <vector>
-
-#include "OCPlatform.h"
-#include "OCApi.h"
-
-#include "IOT_Resource.h"
-#include "IOT_BinarySwitchResource.h"
-
-using namespace OC;
-
-/*******************************************************/
-/*******************************************************/
-IOT_BinarySwitchResource::IOT_BinarySwitchResource(const std::string& _uri, const std::vector<std::string>& _types, const std::vector<std::string>& _interfaces) :
- IOT_Resource(_uri, _types, _interfaces), m_value(false)
-{
- m_representation.setValue("value", m_value);
-}
-
-/*******************************************************/
-/*******************************************************/
-/*virtual*/ void IOT_BinarySwitchResource::setRepresentation(OCRepresentation& _rep)
-{
- bool value;
-
- if(_rep.getValue("value", value))
- {
- m_value = value;
- m_representation.setValue("value", m_value);
- propagate();
- }
-}
-
-/*******************************************************/
-/*******************************************************/
-/*virtual*/ OCEntityHandlerResult IOT_BinarySwitchResource::entityHandler(std::shared_ptr<OCResourceRequest> _request)
-{
- OCEntityHandlerResult res = OC_EH_ERROR;
-
- if(_request)
- {
- std::string rt = _request->getRequestType();
- int rf = _request->getRequestHandlerFlag();
-
- if(rf & RequestHandlerFlag::RequestFlag)
- {
- if(rt == "GET")
- {
- if(sendRepresentation(_request) == OC_STACK_OK)
- res = OC_EH_OK;
- }
- else if(rt == "PUT")
- {
- // PUT request operations
- }
- else if(rt == "POST")
- {
- OCRepresentation rep = _request->getResourceRepresentation();
- setRepresentation(rep);
- if(sendRepresentation(_request) == OC_STACK_OK)
- res = OC_EH_OK;
- }
- else if(rt == "DELETE")
- {
- // DELETE request operations
- }
- }
-
- if(rf & RequestHandlerFlag::ObserverFlag)
- {
- ObservationInfo info = _request->getObservationInfo();
- if(info.action == ObserveAction::ObserveRegister)
- {
- m_interested_observers.push_back(info.obsId);
- }
- else if(info.action == ObserveAction::ObserveUnregister)
- {
- m_interested_observers.erase(remove(m_interested_observers.begin(), m_interested_observers.end(), info.obsId), m_interested_observers.end());
- }
- }
- }
- else
- {
- //cout << "Request invalid" << endl;
- }
-
- return res;
-}
+++ /dev/null
-#ifndef __IOT_BINARY_SWITCH_RESOURCE_H__
-#define __IOT_BINARY_SWITCH_RESOURCE_H__
-
-#include <memory>
-#include <string>
-#include <vector>
-
-#include "OCPlatform.h"
-#include "OCApi.h"
-
-#include "IOT_Resource.h"
-
-using namespace OC;
-
-/*******************************************************/
-/*******************************************************/
-class IOT_BinarySwitchResource : public IOT_Resource
-{
-public:
- IOT_BinarySwitchResource(const std::string& _uri, const std::vector<std::string>& _types, const std::vector<std::string>& _interfaces);
- IOT_BinarySwitchResource(const IOT_BinarySwitchResource& _obj) = default;
-
- virtual ~IOT_BinarySwitchResource() = default;
-
- IOT_BinarySwitchResource& operator=(const IOT_BinarySwitchResource& _obj) = default;
-
- virtual void setRepresentation(OCRepresentation& _rep);
-
- virtual OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> _request);
-
-private:
- bool m_value;
-};
-
-#endif /* __IOT_BINARY_SWITCH_RESOURCE_H__ */
+++ /dev/null
-/**
- * @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;
-}
+++ /dev/null
-/**
- * @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 "easysetup.h"
-#include "escommon.h"
-#include "ESEnrolleeCommon.h"
-#include <mutex>
-#include <condition_variable>
-
-typedef struct
-{
- int unused;
-} IOT_EasySetup_WiFiInfo;
-
-typedef struct
-{
- int unused;
-} IOT_EasySetup_ConfInfo;
-
-typedef struct
-{
- char host[OIC_STRING_MAX_VALUE];
- char auth_provider[OIC_STRING_MAX_VALUE];
- char auth_code[OIC_STRING_MAX_VALUE];
-} IOT_EasySetup_CloudInfo;
-
-typedef struct
-{
- IOT_EasySetup_WiFiInfo wifi_info;
- IOT_EasySetup_ConfInfo conf_info;
- IOT_EasySetup_CloudInfo cloud_info;
-} IOT_EasySetup_Info;
-
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-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__ */
+++ /dev/null
-#include <memory>
-#include <string>
-#include <functional>
-#include <mutex>
-#include <condition_variable>
-#include <exception>
-#include <stdexcept>
-
-#include "OCPlatform.h"
-#include "OCApi.h"
-#include "OCAccountManager.h"
-#include "RDClient.h"
-
-#include "IOT_EasySetup.h"
-#include "IOT_Enrollee.h"
-
-using namespace OC;
-
-IOT_Enrollee* IOT_Enrollee::Create( const ESDeviceProperty& _device_properties,
- OCConnectivityType _conn_type )
-{
- if(!IOT_EasySetup::instance().run(_device_properties))
- {
- throw std::runtime_error("IOT_EasySetup 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);
-}
-
-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 )
-{
- 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);
- std::condition_variable cvar;
-
- if(m_account_manager->signUp( m_auth_provider,
- m_auth_code,
- [&](const HeaderOptions& /*_hopt*/, const OCRepresentation& _rep, const int _ecode)
- {
- if(_ecode == 4)
- {
- m_uid = _rep.getValueToString("uid");
- m_access_token = _rep.getValueToString("accesstoken");
- }
- cvar.notify_all();
- } ) != OC_STACK_OK)
- throw std::runtime_error("signUp failed");
- cvar.wait(lck);
-
- if(m_account_manager->signIn( m_uid,
- m_access_token,
- [&](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)
-{
- m_account_manager = OCPlatform::constructAccountManagerObject(m_host, m_conn_type);
-
- std::mutex mtx;
- std::unique_lock<std::mutex> lck(mtx);
- std::condition_variable cvar;
-
- if(m_account_manager->signIn( m_uid,
- m_access_token,
- [&](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()
-{
-}
-
-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,
- _uri,
- _type,
- _interface,
- std::bind(&IOT_Resource::entityHandler, &_resource, std::placeholders::_1),
- _property ) != OC_STACK_OK)
- 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("bindTypeToResource failed");
-}
-void IOT_Enrollee::bindInterfaceToResource(IOT_Resource& _resource, std::string& _interface)
-{
- if(OCPlatform::bindInterfaceToResource(_resource.m_handle, _interface) != OC_STACK_OK)
- throw std::runtime_error("bindInterfaceToResource failed");
-}
-
-void IOT_Enrollee::publishResource(ResourceHandles& _handles, IOT_Resource& _resource)
-{
- std::mutex mtx;
- std::unique_lock<std::mutex> lck(mtx);
- std::condition_variable cvar;
-
- if(RDClient::Instance().publishResourceToRD( m_host,
- m_conn_type,
- _handles,
- [&](const OCRepresentation& /*_rep*/, const int& /*_ecode*/)
- {
- cvar.notify_all();
- } ) != OC_STACK_OK)
- throw std::runtime_error("publishResourceToRD failed");
- cvar.wait(lck);
-
- _handles.push_back(_resource.m_handle);
-
- if(RDClient::Instance().publishResourceToRD( m_host,
- m_conn_type,
- _handles,
- [&](const OCRepresentation& /*_rep*/, const int& /*_ecode*/)
- {
- cvar.notify_all();
- } ) != OC_STACK_OK)
- throw std::runtime_error("publishResourceToRD failed");
- cvar.wait(lck);
-}
-
-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;
-}
+++ /dev/null
-#ifndef __IOT_ENROLLEE_H__
-#define __IOT_ENROLLEE_H__
-
-#include <iostream>
-#include <memory>
-#include <string>
-
-#include "OCPlatform.h"
-#include "OCApi.h"
-#include "OCAccountManager.h"
-
-#include "IOT_Resource.h"
-
-using namespace OC;
-
-/*******************************************************/
-/*******************************************************/
-class IOT_Enrollee final
-{
-public:
- static IOT_Enrollee* Create( const ESDeviceProperty& _device_properties,
- OCConnectivityType _conn_type );
- static 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 );
-
- ~IOT_Enrollee();
-
- std::string host() const { return m_host; }
- std::string auth_provider() const { return m_auth_provider; }
- std::string auth_code() const { return m_auth_code; }
-
- std::string uid() const { return m_uid; }
- std::string access_token() const { return m_access_token; }
-
- void registerResource(IOT_Resource& _resource, std::string& _uri, std::string& _type, std::string& _interface, uint8_t _property);
-
- void bindTypeToResource(IOT_Resource& _resource, std::string& _type);
- void bindInterfaceToResource(IOT_Resource& _resource, std::string& _interface);
-
- void publishResource(ResourceHandles& _handles, IOT_Resource& _resource);
-
- friend std::ostream& operator<<(std::ostream& _os, const IOT_Enrollee& _obj);
-
-private:
- IOT_Enrollee(OCConnectivityType _conn_type, const std::string& _host, const std::string& _auth_provider, const std::string& _auth_code);
- 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);
- IOT_Enrollee(const IOT_Enrollee& _obj) = delete;
-
- IOT_Enrollee& operator=(const IOT_Enrollee& _obj) = delete;
-
- OCConnectivityType m_conn_type;
-
- std::string m_host;
- std::string m_auth_provider;
- std::string m_auth_code;
-
- std::shared_ptr<OCAccountManager> m_account_manager;
-
- std::string m_uid;
- std::string m_access_token;
-};
-
-#endif /* __IOT_ENROLLEE_H__ */
+++ /dev/null
-#ifndef __IOT_RESOURCE_H__
-#define __IOT_RESOURCE_H__
-
-#include <memory>
-#include <string>
-#include <vector>
-
-#include "OCPlatform.h"
-#include "OCApi.h"
-
-using namespace OC;
-
-class IOT_Enrollee;
-
-/*******************************************************/
-/*******************************************************/
-class IOT_Resource
-{
-public:
- friend class IOT_Enrollee;
-
- IOT_Resource(const std::string& _uri, const std::vector<std::string>& _types, const std::vector<std::string>& _interfaces)
- {
- m_representation.setUri(_uri);
- m_representation.setResourceTypes(_types);
- m_representation.setResourceInterfaces(_interfaces);
- }
- IOT_Resource(const IOT_Resource& _obj) = default;
-
- virtual ~IOT_Resource() = default;
-
- IOT_Resource& operator=(const IOT_Resource& _obj) = default;
-
- std::string getUri() const
- {
- return m_representation.getUri();
- }
- std::vector<std::string> getTypes() const
- {
- return m_representation.getResourceTypes();
- }
- std::vector<std::string> getInterfaces() const
- {
- return m_representation.getResourceInterfaces();
- }
-
- virtual OCStackResult addChildResource(IOT_Resource* _resource)
- {
- m_child_resources.push_back(_resource);
- return OCPlatform::bindResource(m_handle, _resource->m_handle);
- }
-
- virtual OCRepresentation getRepresentation()
- {
- m_representation.clearChildren();
-
- for(auto it : m_child_resources)
- //m_representation.addChild((*it)->getRepresentation());
- m_representation.addChild(it->getRepresentation());
-
- return m_representation;
- }
- virtual void setRepresentation(OCRepresentation& _rep)
- {
- }
-
- virtual OCStackResult sendRepresentation(std::shared_ptr<OCResourceRequest> _request)
- {
- auto response = std::make_shared<OCResourceResponse>();
-
- response->setRequestHandle(_request->getRequestHandle());
- response->setResourceHandle(_request->getResourceHandle());
-
- QueryParamsMap qpm = _request->getQueryParameters();
- auto fr = qpm.find("if");
-
- if (fr != qpm.end())
- response->setResourceRepresentation(getRepresentation(), fr->second);
- else
- response->setResourceRepresentation(getRepresentation(), DEFAULT_INTERFACE);
-
- response->setErrorCode(200);
- response->setResponseResult(OC_EH_OK);
-
- return OCPlatform::sendResponse(response);
- }
-
- virtual OCStackResult propagate()
- {
- OCStackResult res = OC_STACK_OK;
-
- if(m_interested_observers.size() > 0)
- {
- std::shared_ptr<OCResourceResponse> rr = {std::make_shared<OCResourceResponse>()};
-
- rr->setErrorCode(200);
- rr->setResourceRepresentation(getRepresentation(), DEFAULT_INTERFACE);
-
- res = OCPlatform::notifyListOfObservers(m_handle, m_interested_observers, rr);
- }
-
- return res;
- }
-
- virtual OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> _request) = 0;
-
-protected:
- OCResourceHandle m_handle;
- OCRepresentation m_representation;
- std::vector<IOT_Resource*> m_child_resources;
- ObservationIds m_interested_observers;
-};
-
-#endif /* __IOT_RESOURCE_H__ */
+++ /dev/null
-#include <memory>
-#include <string>
-#include <vector>
-
-#include "OCPlatform.h"
-#include "OCApi.h"
-
-#include "IOT_Resource.h"
-#include "IOT_TemperatureResource.h"
-
-using namespace OC;
-
-/*******************************************************/
-/*******************************************************/
-IOT_TemperatureResource::IOT_TemperatureResource(const std::string& _uri, const std::vector<std::string>& _types, const std::vector<std::string>& _interfaces) :
- IOT_Resource(_uri, _types, _interfaces), m_temperature(0)
-{
- m_representation.setValue("temperature", m_temperature);
-}
-
-/*******************************************************/
-/*******************************************************/
-/*virtual*/ void IOT_TemperatureResource::setRepresentation(OCRepresentation& _rep)
-{
- int temperature;
-
- if(_rep.getValue("temperature", temperature))
- {
- m_temperature = temperature;
- m_representation.setValue("temperature", m_temperature);
- propagate();
- }
-}
-
-/*******************************************************/
-/*******************************************************/
-/*virtual*/ OCEntityHandlerResult IOT_TemperatureResource::entityHandler(std::shared_ptr<OCResourceRequest> _request)
-{
- OCEntityHandlerResult res = OC_EH_ERROR;
-
- if(_request)
- {
- std::string /*requestType*/rt = _request->getRequestType();
- int /*requestFlag*/rf = _request->getRequestHandlerFlag();
-
- if(rf & RequestHandlerFlag::RequestFlag)
- {
- if(rt == "GET")
- {
- if (sendRepresentation(_request) == OC_STACK_OK)
- res = OC_EH_OK;
- }
- else if(rt == "PUT")
- {
- // PUT requeist operations
- }
- else if(rt == "POST")
- {
- OCRepresentation rep = _request->getResourceRepresentation();
- setRepresentation(rep);
- if(sendRepresentation(_request) == OC_STACK_OK)
- res = OC_EH_OK;
- }
- else if(rt == "DELETE")
- {
- // DELETE request operations
- }
- }
-
- if(rf & RequestHandlerFlag::ObserverFlag)
- {
- ObservationInfo info = _request->getObservationInfo();
- if(info.action == ObserveAction::ObserveRegister)
- {
- m_interested_observers.push_back(info.obsId);
- }
- else if(info.action == ObserveAction::ObserveUnregister)
- {
- m_interested_observers.erase(remove(m_interested_observers.begin(), m_interested_observers.end(), info.obsId), m_interested_observers.end());
- }
- }
- }
- else
- {
- //cout << "Request invalid" << endl;
- }
-
- return res;
-}
+++ /dev/null
-#ifndef __IOT_TEMPERATURE_RESOURCE_H__
-#define __IOT_TEMPERATURE_RESOURCE_H__
-
-#include <memory>
-#include <string>
-#include <vector>
-
-#include "OCPlatform.h"
-#include "OCApi.h"
-
-#include "IOT_Resource.h"
-
-using namespace OC;
-
-/*******************************************************/
-/*******************************************************/
-class IOT_TemperatureResource : public IOT_Resource
-{
-public:
- IOT_TemperatureResource(const std::string& _uri, const std::vector<std::string>& _types, const std::vector<std::string>& _interfaces);
- IOT_TemperatureResource(const IOT_TemperatureResource& _obj) = default;
-
- virtual ~IOT_TemperatureResource() = default;
-
- IOT_TemperatureResource& operator=(const IOT_TemperatureResource& _obj) = default;
-
- virtual void setRepresentation(OCRepresentation& _rep);
-
- virtual OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> _request);
-
-private:
- int m_temperature;
-};
-
-#endif /* __IOT_TEMPERATURE_RESOURCE_H__ */
#include <iostream>
#include <fstream>
#include <string>
+#include <memory>
#include "main_def.h"
#include "IOT_Enrollee.h"
#include "IOT_Resource.h"
#include "IOT_AirconResource.h"
-#include "IOT_BinarySwitchResource.h"
-#include "IOT_TemperatureResource.h"
+#include "IOT_PowerResource.h"
+#include "IOT_ReportResource.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 const char* s_device_model = "Model ZZZ";
/*******************************************************/
/*******************************************************/
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};
+ //OCPlatformInfo pi = {0};
OCDeviceInfo di;
OCStringLL dt;
OCStringLL dm;
OCPlatform::Configure(cfg);
+ //if(OCPlatform::registerPlatformInfo(_platform_info) != OC_STACK_OK)
+ // throw std::runtime_error("registerPlatformInfo failed");
+
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"}};
+ ESDeviceProperty dp = {{{WIFI_11G, WIFI_11N, WIFI_11AC, WiFi_EOF}, WIFI_5G}, {"IoTivity Simple Aircon", "Model ZZZ"}};
enr = IOT_Enrollee::Create(dp, CT_ADAPTER_TCP);
wdat(enr->host(), enr->auth_provider(), enr->auth_code(), enr->uid(), enr->access_token());
}
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);
+ if(aircon.registerResource(uri, rt, ri, OC_DISCOVERABLE) != OC_STACK_OK)
+ throw std::runtime_error("registerResource failed");
+ ri = aircon.getInterfaces()[1]; aircon.bindInterface(ri);
+ ri = aircon.getInterfaces()[2]; aircon.bindInterface(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_PowerResource power("/power/0", {"oic.r.power"}, {DEFAULT_INTERFACE});
+ uri = power.getUri(); rt = power.getTypes()[0]; ri = power.getInterfaces()[0];
+ if(power.registerResource(uri, rt, ri, OC_OBSERVABLE) != OC_STACK_OK)
+ throw std::runtime_error("registerResource failed");
- 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);
+ aircon.addChildResource(&power);
ResourceHandles rhandles;
- enr->publishResource(rhandles, aircon);
+ if(aircon.publishResource(rhandles, enr->host(), CT_ADAPTER_TCP) != OC_STACK_OK)
+ throw std::runtime_error("publishResource failed");
+
+ std::shared_ptr<OC::OCResource> report_res = nullptr;
while(m_running)
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
+ {
+ std::this_thread::sleep_for(std::chrono::milliseconds(1000));
+
+ if(!report_res)
+ report_res = IOT_Resource::findResource(enr->host(), {OC_RSRVD_WELL_KNOWN_URI}, static_cast<OCConnectivityType>(CT_DEFAULT), 1, "core.security");
+
+ if(report_res)
+ {
+ OCRepresentation rpr;
+ rpr.setValue("report", IOT_Report::makeReport("1", IOT_Report::currentDateTime(), "sim", 0, "report data"));
+ QueryParamsMap qry;
+ if(IOT_Resource::post(report_res, {"core.security"}, {DEFAULT_INTERFACE}, rpr, qry) != OC_STACK_OK)
+ report_res = nullptr;
+ }
+ }
delete enr;
}