target_link_libraries( ${PROJECT_NAME}
pthread
- c_common oc octbstack oc_logger resource_directory connectivity_abstraction
+ c_common oc octbstack oc_logger resource_directory connectivity_abstraction
ESEnrolleeSDK
)
--- /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__ */
/*******************************************************/
/*******************************************************/
-bool IOT_EasySetup(ESDeviceProperty *_properties, IOT_EasySetup_Info* _info)
+bool IOT_EasySetup(ESDeviceProperty* _properties, IOT_EasySetup_Info* _info)
{
volatile bool running = true;
#include "IOT_EasySetup.h"
#include "IOT_Enrollee.h"
+using namespace OC;
+
/*******************************************************/
/*******************************************************/
-/*static*/ IOT_Enrollee* IOT_Enrollee::Create( const OC::PlatformConfig& _platform_config,
+/*static*/ IOT_Enrollee* IOT_Enrollee::Create( const ESDeviceProperty& _device_properties,
+ const PlatformConfig& _platform_config,
const OCPlatformInfo& _platform_info,
const OCDeviceInfo& _device_info,
OCConnectivityType _conn_type )
{
- ESDeviceProperty properties = {{{WIFI_11G, WIFI_11N, WIFI_11AC, WiFi_EOF}, WIFI_5G}, {"IoTivity Simple Device", "core.1.1.0"}};
+ OCPlatform::Configure(_platform_config);
+
IOT_EasySetup_Info info;
- if(!IOT_EasySetup(&properties, &info))
+ if(!IOT_EasySetup(const_cast<ESDeviceProperty*>(&_device_properties), &info))
throw std::runtime_error("IOT_EasySetup failed");
- OC::OCPlatform::Configure(_platform_config);
-
+ /*
+ 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, info.cloud_info.host, info.cloud_info.auth_provider, info.cloud_info.auth_code);
}
-/*static*/ IOT_Enrollee* IOT_Enrollee::Create( const OC::PlatformConfig& _platform_config,
+/*static*/ IOT_Enrollee* IOT_Enrollee::Create( const PlatformConfig& _platform_config,
const OCPlatformInfo& _platform_info,
const OCDeviceInfo& _device_info,
OCConnectivityType _conn_type,
const std::string& _uid,
const std::string& _access_token )
{
- OC::OCPlatform::Configure(_platform_config);
-
+ 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");
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 = OC::OCPlatform::constructAccountManagerObject(m_host, m_conn_type);
+ m_account_manager = OCPlatform::constructAccountManagerObject(m_host, m_conn_type);
std::mutex mtx;
std::unique_lock<std::mutex> lck(mtx);
if(m_account_manager->signUp( m_auth_provider,
m_auth_code,
- [&](const OC::HeaderOptions& /*_hopt*/, const OC::OCRepresentation& _rep, const int _ecode)
+ [&](const HeaderOptions& /*_hopt*/, const OCRepresentation& _rep, const int _ecode)
{
if(_ecode == 4)
{
if(m_account_manager->signIn( m_uid,
m_access_token,
- [&](const OC::HeaderOptions& /*_hopt*/, const OC::OCRepresentation& /*_rep*/, const int /*_ecode*/)
+ [&](const HeaderOptions& /*_hopt*/, const OCRepresentation& /*_rep*/, const int /*_ecode*/)
{
cvar.notify_all();
} ) != OC_STACK_OK)
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 = OC::OCPlatform::constructAccountManagerObject(m_host, m_conn_type);
+ m_account_manager = OCPlatform::constructAccountManagerObject(m_host, m_conn_type);
std::mutex mtx;
std::unique_lock<std::mutex> lck(mtx);
if(m_account_manager->signIn( m_uid,
m_access_token,
- [&](const OC::HeaderOptions& /*_hopt*/, const OC::OCRepresentation& /*_rep*/, const int /*_ecode*/)
+ [&](const HeaderOptions& /*_hopt*/, const OCRepresentation& /*_rep*/, const int /*_ecode*/)
{
cvar.notify_all();
} ) != OC_STACK_OK)
/*******************************************************/
/*******************************************************/
-void IOT_Enrollee::registerResource(IOT_Resource& _resource, uint8_t _property)
+void IOT_Enrollee::registerResource(IOT_Resource& _resource, std::string& _uri, std::string& _type, std::string& _interface, uint8_t _property)
{
- std::string uri = _resource.getUri();
- std::vector<std::string> rtv = _resource.getTypes();
- std::vector<std::string> riv = _resource.getInterfaces();
-
- if(OC::OCPlatform::registerResource( _resource.m_handle,
- uri,
- rtv[0],
- riv[0],
- std::bind(&IOT_Resource::handler, &_resource, std::placeholders::_1),
- _property ) != OC_STACK_OK)
+ 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");
-
- for(auto it = rtv.cbegin() + 1; it != rtv.cend(); ++it)
- {
- if(OC::OCPlatform::bindTypeToResource(_resource.m_handle, *it) != OC_STACK_OK)
- throw std::runtime_error("bindTypeToResource failed");
- }
- for(auto it = riv.cbegin() + 1; it != riv.cend(); ++it)
- {
- if(OC::OCPlatform::bindInterfaceToResource(_resource.m_handle, *it) != OC_STACK_OK)
- throw std::runtime_error("bindInterfaceToResource failed");
- }
}
/*******************************************************/
/*******************************************************/
-void IOT_Enrollee::publishResource(IOT_Resource& _resource)
+void IOT_Enrollee::bindTypeToResource(IOT_Resource& _resource, std::string& _type)
{
- ResourceHandles handles;
+ 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,
+ _handles,
[&](const OCRepresentation& /*_rep*/, const int& /*_ecode*/)
{
cvar.notify_all();
throw std::runtime_error("publishResourceToRD failed");
cvar.wait(lck);
- handles.push_back(_resource.m_handle);
+ _handles.push_back(_resource.m_handle);
if(RDClient::Instance().publishResourceToRD( m_host,
m_conn_type,
- handles,
+ _handles,
[&](const OCRepresentation& /*_rep*/, const int& /*_ecode*/)
{
cvar.notify_all();
#include "IOT_Resource.h"
+using namespace OC;
+
/*******************************************************/
/*******************************************************/
class IOT_Enrollee final
{
public:
- static IOT_Enrollee* Create( const OC::PlatformConfig& _platform_config,
+ 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 OC::PlatformConfig& _platform_config,
+ static IOT_Enrollee* Create( const PlatformConfig& _platform_config,
const OCPlatformInfo& _platform_info,
const OCDeviceInfo& _device_info,
OCConnectivityType _conn_type,
std::string uid() const { return m_uid; }
std::string access_token() const { return m_access_token; }
- void registerResource(IOT_Resource& _resource, uint8_t _property);
+ 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(IOT_Resource& _resource);
+ void publishResource(ResourceHandles& _handles, IOT_Resource& _resource);
friend std::ostream& operator<<(std::ostream& _os, const IOT_Enrollee& _obj);
std::string m_auth_provider;
std::string m_auth_code;
- std::shared_ptr<OC::OCAccountManager> m_account_manager;
+ std::shared_ptr<OCAccountManager> m_account_manager;
std::string m_uid;
std::string m_access_token;
+++ /dev/null
-#include <memory>
-#include <string>
-#include <vector>
-
-#include "OCPlatform.h"
-#include "OCApi.h"
-
-#include "IOT_LampResource.h"
-
-/*******************************************************/
-/*******************************************************/
-IOT_LampResource::IOT_LampResource(const std::string& _uri, const std::vector<std::string>& _types, const std::vector<std::string>& _interfaces) :
- IOT_Resource(_uri, _types, _interfaces), m_power("")
-{
- m_representation.setValue("power", m_power);
-}
-
-/*******************************************************/
-/*******************************************************/
-void IOT_LampResource::put(OC::OCRepresentation& _rep)
-{
- _rep.getValue("power", m_power);
-}
-OC::OCRepresentation IOT_LampResource::post(OC::OCRepresentation& _rep)
-{
- put(_rep);
- return get();
-}
-OC::OCRepresentation IOT_LampResource::get()
-{
- m_representation.setValue("power", m_power);
- return m_representation;
-}
-
-/*******************************************************/
-/*******************************************************/
-/*virtual*/ OCEntityHandlerResult IOT_LampResource::handler(std::shared_ptr<OC::OCResourceRequest> _request)
-{
- OCEntityHandlerResult res = OC_EH_ERROR;
-
- if(_request)
- {
- std::string type = _request->getRequestType();
- int flag = _request->getRequestHandlerFlag();
-
- std::cout << "type : " << type << " flag : " << flag << std::endl;
- }
-#if 0
- cout << "\tIn Server CPP entity handler:\n";
- OCEntityHandlerResult ehResult = OC_EH_ERROR;
- if(request)
- {
- // Get the request type and request flag
- std::string requestType = request->getRequestType();
- int requestFlag = request->getRequestHandlerFlag();
-
- if(requestFlag & RequestHandlerFlag::RequestFlag)
- {
- cout << "\t\trequestFlag : Request\n";
- auto pResponse = std::make_shared<OC::OCResourceResponse>();
- pResponse->setRequestHandle(request->getRequestHandle());
- pResponse->setResourceHandle(request->getResourceHandle());
-
- // If the request type is GET
- if(requestType == "GET")
- {
- cout << "\t\t\trequestType : GET\n";
- if(isSlowResponse) // Slow response case
- {
- static int startedThread = 0;
- if(!startedThread)
- {
- std::thread t(handleSlowResponse, (void *)this, request);
- startedThread = 1;
- t.detach();
- }
- ehResult = OC_EH_SLOW;
- }
- else // normal response case.
- {
- pResponse->setErrorCode(200);
- pResponse->setResponseResult(OC_EH_OK);
- pResponse->setResourceRepresentation(get());
- if(OC_STACK_OK == OCPlatform::sendResponse(pResponse))
- {
- ehResult = OC_EH_OK;
- }
- }
- }
- else if(requestType == "PUT")
- {
- cout << "\t\t\trequestType : PUT\n";
- OCRepresentation rep = request->getResourceRepresentation();
-
- // Do related operations related to PUT request
- // Update the lightResource
- put(rep);
- pResponse->setErrorCode(200);
- pResponse->setResponseResult(OC_EH_OK);
- pResponse->setResourceRepresentation(get());
- if(OC_STACK_OK == OCPlatform::sendResponse(pResponse))
- {
- ehResult = OC_EH_OK;
- }
- }
- else if(requestType == "POST")
- {
- cout << "\t\t\trequestType : POST\n";
-
- OCRepresentation rep = request->getResourceRepresentation();
-
- // Do related operations related to POST request
- OCRepresentation rep_post = post(rep);
- pResponse->setResourceRepresentation(rep_post);
- pResponse->setErrorCode(200);
- if(rep_post.hasAttribute("createduri"))
- {
- pResponse->setResponseResult(OC_EH_RESOURCE_CREATED);
- pResponse->setNewResourceUri(rep_post.getValue<std::string>("createduri"));
- }
-
- if(OC_STACK_OK == OCPlatform::sendResponse(pResponse))
- {
- ehResult = OC_EH_OK;
- }
- }
- else if(requestType == "DELETE")
- {
- // DELETE request operations
- }
- }
- }
- else
- {
- std::cout << "Request invalid" << std::endl;
- }
-#endif
-
- return res;
-}
+++ /dev/null
-#ifndef __IOT_LAMP_RESOURCE_H__
-#define __IOT_LAMP_RESOURCE_H__
-
-#include <memory>
-#include <string>
-#include <vector>
-
-#include "OCPlatform.h"
-#include "OCApi.h"
-
-#include "IOT_Resource.h"
-
-/*******************************************************/
-/*******************************************************/
-class IOT_LampResource : public IOT_Resource
-{
-public:
- IOT_LampResource(const std::string& _uri, const std::vector<std::string>& _types, const std::vector<std::string>& _interfaces);
- IOT_LampResource(const IOT_LampResource& _obj) = default;
-
- virtual ~IOT_LampResource() = default;
-
- IOT_LampResource& operator=(const IOT_LampResource& _obj) = default;
-
- void put(OC::OCRepresentation& _rep);
- OC::OCRepresentation post(OC::OCRepresentation& _rep);
- OC::OCRepresentation get();
-
-private:
- virtual OCEntityHandlerResult handler(std::shared_ptr<OC::OCResourceRequest> _request);
-
- std::string m_power;
-};
-
-#endif /* __IOT_LAMP_RESOURCE_H__ */
#include "OCPlatform.h"
#include "OCApi.h"
+using namespace OC;
+
class IOT_Enrollee;
/*******************************************************/
public:
friend class IOT_Enrollee;
- IOT_Resource(const std::string& _uri, const std::vector<std::string>& _types, const std::vector<std::string>& _interfaces) :
- m_handle(nullptr)
+ 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);
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;
- OC::OCRepresentation m_representation;
-
-private:
- virtual OCEntityHandlerResult handler(std::shared_ptr<OC::OCResourceRequest> _request) = 0;
+ 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>\r
#include <fstream>\r
-//#include <stdlib.h>\r
#include <string>\r
-//#include <unistd.h>\r
\r
#include "main_def.h"\r
\r
#include "ocpayload.h"\r
\r
#include "IOT_Enrollee.h"\r
-#include "IOT_LampResource.h"\r
+#include "IOT_Resource.h"\r
+#include "IOT_AirconResource.h"\r
+#include "IOT_BinarySwitchResource.h"
+#include "IOT_TemperatureResource.h"\r
\r
using namespace NMD;\r
\r
/*******************************************************/\r
/*******************************************************/\r
-main_thread::main_thread() : thread_base()\r
-{\r
-}\r
-/*virtual*/ main_thread::~main_thread()\r
-{\r
-}\r
-\r
-\r
-\r
-\r
-\r
-static std::string platformId = "0A3E0D6F-DBF5-404E-8719-D6880042463A";\r
-static std::string manufacturerName = "OCF";\r
-static std::string manufacturerLink = "https://www.iotivity.org";\r
-static std::string modelNumber = "myModelNumber";\r
-static std::string dateOfManufacture = "2016-01-15";\r
-static std::string platformVersion = "myPlatformVersion";\r
-static std::string operatingSystemVersion = "myOS";\r
-static std::string hardwareVersion = "myHardwareVersion";\r
-static std::string firmwareVersion = "1.0";\r
-static std::string supportLink = "https://www.iotivity.org";\r
-static std::string systemTime = "2016-01-15T11.01";\r
-\r
-static std::string deviceName = "IoTivity Simple Device";\r
-static std::string specVersion = "core.1.1.0";\r
-static std::string dataModelVersions = "res.1.1.0,sh.1.1.0";\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
-static OCPlatformInfo platformInfo;\r
-static OCDeviceInfo deviceInfo;\r
-\r
-static void DuplicateString(char** targetString, std::string sourceString)\r
+/*******************************************************/\r
+/*******************************************************/\r
+static FILE* server_fopen(const char *path, const char *mode)\r
{\r
- *targetString = new char[sourceString.length() + 1];\r
- strncpy(*targetString, sourceString.c_str(), (sourceString.length() + 1));\r
+ static const char CRED_FILE[] = "/var/run/oic_nmdaemon_db.dat";\r
+ (void) path;\r
+ return fopen(CRED_FILE, mode);\r
}\r
\r
-static OCStackResult SetPlatformInfo(std::string platformID, std::string manufacturerName,\r
- std::string manufacturerUrl, std::string modelNumber, std::string dateOfManufacture,\r
- std::string platformVersion, std::string operatingSystemVersion,\r
- std::string hardwareVersion, std::string firmwareVersion, std::string supportUrl,\r
- std::string systemTime)\r
+/*******************************************************/\r
+/*******************************************************/\r
+std::string getDeviceID()\r
{\r
- DuplicateString(&platformInfo.platformID, platformID);\r
- DuplicateString(&platformInfo.manufacturerName, manufacturerName);\r
- DuplicateString(&platformInfo.manufacturerUrl, manufacturerUrl);\r
- DuplicateString(&platformInfo.modelNumber, modelNumber);\r
- DuplicateString(&platformInfo.dateOfManufacture, dateOfManufacture);\r
- DuplicateString(&platformInfo.platformVersion, platformVersion);\r
- DuplicateString(&platformInfo.operatingSystemVersion, operatingSystemVersion);\r
- DuplicateString(&platformInfo.hardwareVersion, hardwareVersion);\r
- DuplicateString(&platformInfo.firmwareVersion, firmwareVersion);\r
- DuplicateString(&platformInfo.supportUrl, supportUrl);\r
- DuplicateString(&platformInfo.systemTime, systemTime);\r
-\r
- return OC_STACK_OK;\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
-static OCStackResult SetDeviceInfo(std::string deviceName, std::string specVersion, std::string dataModelVersions)\r
-{\r
- DuplicateString(&deviceInfo.deviceName, deviceName);\r
\r
- if (!specVersion.empty())\r
- {\r
- DuplicateString(&deviceInfo.specVersion, specVersion);\r
- }\r
-\r
- if (!dataModelVersions.empty())\r
- {\r
- OCResourcePayloadAddStringLL(&deviceInfo.dataModelVersions, dataModelVersions.c_str());\r
- }\r
-\r
- return OC_STACK_OK;\r
-}\r
-\r
-static void DeletePlatformInfo()\r
-{\r
- delete[] platformInfo.platformID;\r
- delete[] platformInfo.manufacturerName;\r
- delete[] platformInfo.manufacturerUrl;\r
- delete[] platformInfo.modelNumber;\r
- delete[] platformInfo.dateOfManufacture;\r
- delete[] platformInfo.platformVersion;\r
- delete[] platformInfo.operatingSystemVersion;\r
- delete[] platformInfo.hardwareVersion;\r
- delete[] platformInfo.firmwareVersion;\r
- delete[] platformInfo.supportUrl;\r
- delete[] platformInfo.systemTime;\r
-}\r
-static void DeleteDeviceInfo()\r
+/*******************************************************/\r
+/*******************************************************/\r
+main_thread::main_thread() : thread_base()\r
{\r
- delete[] deviceInfo.deviceName;\r
- delete[] deviceInfo.specVersion;\r
- OCFreeOCStringLL(deviceInfo.dataModelVersions);\r
}\r
-\r
-\r
-static FILE* server_fopen(const char *path, const char *mode)\r
+/*virtual*/ main_thread::~main_thread()\r
{\r
- static const char CRED_FILE[] = "oic_svr_db_server.dat";\r
- (void) path;\r
- return fopen(CRED_FILE, mode);\r
}\r
\r
/*******************************************************/\r
write_log("[MAIN_THREADS] started\n");\r
try\r
{\r
- OCPersistentStorage ps{server_fopen, fread, fwrite, fclose, unlink};\r
- OC::PlatformConfig cfg{OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos, &ps};\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
- enr = IOT_Enrollee::Create(cfg, platformInfo, deviceInfo, CT_ADAPTER_TCP);\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, platformInfo, deviceInfo, CT_ADAPTER_TCP, host, auth_provider, auth_code, uid, access_token);\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 : %s - %s - %s - %s - %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());\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
- IOT_LampResource lamp("/a/light", {"core.light", "core.brightlight"}, {OC::DEFAULT_INTERFACE, OC::LINK_INTERFACE});\r
- enr->registerResource(lamp, OC_DISCOVERABLE | OC_OBSERVABLE);\r
- enr->publishResource(lamp);\r
+ aircon.addChildResource(&bswitch);\r
+ aircon.addChildResource(&temp);\r
\r
- DeletePlatformInfo();\r
- DeleteDeviceInfo();\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
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("/var/run/nmdaemon.dat");\r
+ std::ifstream in(DAT_FILE_NAME);\r
std::vector<std::string> v;\r
std::string s;\r
while(std::getline(in, s))\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("/var/run/nmdaemon.dat", std::ofstream::out|std::ofstream::trunc);\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
cond_var.wait_for(lock, std::chrono::seconds(CALLBACK_WAIT_TIMEOUT_S));
+ /* !!! Dima please review !!! */
+ this->uuid = device_resource->sid();
+
OCPlatform::getDeviceInfo(device_resource->host(),
dev_info_uri,
device_resource->connectivityType(),