SConscript('protocol-plugin/SConscript')
# Build notification manager project
-# SConscript('notification-manager/SConscript')
+ SConscript('notification-manager/SConscript')
#else:
# SConscript('notification-manager/SampleApp/arduino/SConscript')
+++ /dev/null
-//******************************************************************
-//
-// Copyright 2014 Samsung Electronics All Rights Reserved.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-
-#ifndef HOSTINGCONFIG_H_
-#define HOSTINGCONFIG_H_
-
-#include "NotificationManager.h"
-
-
-enum class HostingMode
-{
- None,
- ManualMode,
- AutomaticMode,
-};
-
-enum class AutomaticMethod
-{
- None,
- NetworkStatusChange,
- Timer,
- DeviceStatus
-};
-
-enum class NotifyMethod
-{
- Frequence,
- Equalization,
- Granularity,
- None
-};
-
-enum class NotifyFrequency
-{
- OnTime,
- Periodically,
- None
-};
-
-
-struct HostingConfig
-{
- HostingMode hostingMode;
- AutomaticMethod automaticMethod;
- NotifyMethod notifyMethod;
- NotifyFrequency frequency;
-};
-
-#endif /* HOSTINGCONFIG_H_ */
+++ /dev/null
-//******************************************************************
-//
-// Copyright 2014 Samsung Electronics All Rights Reserved.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-
-#ifndef HOSTINGHANDLER_H_
-#define HOSTINGHANDLER_H_
-
-#include "NotificationManager.h"
-
-class HostingHandler
-{
- public:
-
- static void initialize();
- static void initialize(HostingConfig cfg);
- static HostingHandler *getInstance();
-
- void setHostingConfig(HostingConfig cfg);
-
- void changeHostingMode(HostingMode hostingMode, AutomaticMethod autoMethod = AutomaticMethod::None);
- void changeAutomaticHostingMethod(AutomaticMethod autoMethod);
- void changeNotifiyMethod(NotifyMethod notifyMethod);
- void changeNotifyFrequencyType(NotifyFrequency notifyFrequency);
-
- private:
-
- HostingHandler();
- ~HostingHandler();
-
- static HostingHandler *s_instance;
- static mutex s_mutexForCreation;
-
- HostingConfig hostingCfg;
-
- static std::function< void(bool isHosting) > s_findHostingCandidate;
- static std::function< void(std::string) > s_addExtraStr;
- static std::function< void(std::shared_ptr< OCResource > resource) > s_startHosting;
- static std::function< void(OCResourceHandle resourceHandle) > s_notify;
-
- void startFindHost();
- void onObserve(AttributeMap &AttMap, OCResourceHandle resourceHandle);
- void onFoundCandidate(std::shared_ptr< OCResource > resource);
-
- void runAutomaticHosting(AutomaticMethod autoMethod);
- void stopAutomaticHosting();
-
- void notifyFrequence(OCResourceHandle resourceHandle);
-
-};
-
-#endif /* HOSTINGHANDLER_H_ */
+++ /dev/null
-//******************************************************************
-//
-// Copyright 2014 Samsung Electronics All Rights Reserved.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-
-#ifndef HOSTINGINTERFACE_H_
-#define HOSTINGINTERFACE_H_
-
-#include "NotificationManager.h"
-
-using namespace OC;
-using namespace OCPlatform;
-
-class HostingInterface
-{
-
- public:
- HostingInterface();
- ~HostingInterface();
-
- int setOnFoundHostingCandidate(
- std::function< void(std::shared_ptr< OCResource > resource) > func);
- int setOnObserve(std::function< void(AttributeMap &inputAttMap, OCResourceHandle resourceHandle) >
- func);
-
- int setFindHosting(std::function< void(bool isHosting) > &func);
- int setStartHosting(std::function< void(std::shared_ptr< OCResource > resource) > &func);
- int setNotifyObservers(std::function< void(OCResourceHandle resourceHandle) > &func);
- int setAddExtraStr(std::function< void(std::string) > &func);
-
-};
-
-#endif /* HOSTINGINTERFACE_H_ */
+++ /dev/null
-//******************************************************************
-//
-// Copyright 2014 Samsung Electronics All Rights Reserved.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-
-#ifndef NOTIFICATIONMANAGER_H_
-#define NOTIFICATIONMANAGER_H_
-
-#include <iostream>
-#include <functional>
-#include <pthread.h>
-#include <cstdint>
-#include <memory>
-#include <string>
-#include <vector>
-#include <list>
-#include <condition_variable>
-
-#include "OICPlatformConfig.h"
-#include "HostingConfig.h"
-#include "ResourceManager.h"
-#include "RegistrationManager.h"
-#include "VirtualRepresentation.h"
-#include "HostingHandler.h"
-#include "HostingInterface.h"
-
-#include <sys/ioctl.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <unistd.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <net/if.h>
-
-#define IN
-#define OUT
-
-using namespace OC;
-using namespace OCPlatform;
-
-const std::string VIRTURL_TAG = "/virtual";
-
-class NotificationManager
-{
-
- private:
-
- NotificationManager();
- NotificationManager(HostingConfig cfg);
- ~NotificationManager();
-
- static NotificationManager *s_instance;
- static mutex s_mutexForCreation;
-
- int getNetInfo(IN int &sck, IN struct ifreq *item, OUT std::string &ip_addres);
- bool scanAndGetNetworkInterface(OUT std::string &ip_addres);
-
- public:
-
- static void initialize();
- static void initialize(HostingConfig cfg);
- static NotificationManager *getInstance();
-
- void registerHostingEventListener();
-
-};
-
-#endif /* NOTIFICATIONMANAGER_H_ */
+++ /dev/null
-//******************************************************************
-//
-// Copyright 2014 Samsung Electronics All Rights Reserved.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-#ifndef OICPLATFORMCONFIG_H_
-#define OICPLATFORMCONFIG_H_
-
-#include "OCApi.h"
-#include "OCPlatform.h"
-#include "OCResource.h"
-#include "OCResourceRequest.h"
-#include "OCResourceResponse.h"
-#include "ocstack.h"
-
-using namespace OC;
-using namespace OCPlatform;
-
-class OICPlatformConfig
-{
- private:
-
- OICPlatformConfig();
- ~OICPlatformConfig();
-
- static OICPlatformConfig *s_instance;
- static mutex s_mutexForCreation;
- static PlatformConfig s_cfg;
-
- public:
-
- void initialize();
- static OICPlatformConfig *getInstance();
- void setIP(std::string ipaddress);
-};
-
-#endif /* OICPLATFORMCONFIG_H_ */
+++ /dev/null
-//******************************************************************
-//
-// Copyright 2014 Samsung Electronics All Rights Reserved.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-
-#ifndef RESOURCEMANAGER_H_
-#define RESOURCEMANAGER_H_
-
-#include "NotificationManager.h"
-
-using namespace OC;
-using namespace OCPlatform;
-
-
-class OICPlatformConfig;
-class VirtualRepresentation;
-
-class ResourceManager
-{
-
- private:
- ResourceManager();
- ~ResourceManager();
-
- static ResourceManager *s_instance;
- static mutex s_mutexForCreation;
- static std::list< VirtualRepresentation > s_resourceList;
- static std::string s_extraStr;
-
- void foundResourceforhosting(std::shared_ptr< OCResource > resource);
-
- void checkResourceDBPolicy();
- void saveResourceDB();
-
- public:
-
- std::function< void(std::shared_ptr< OCResource > resource) > m_onFoundforHosting;
- std::function< void(AttributeMap &inputAttMap, OCResourceHandle resourceHandle) > m_onObserve;
- std::function< void(OCResourceHandle resourceHandle) > m_notify;
-
- static ResourceManager *getInstance();
-
- void findNMResource(bool isHosting);
-
- void onFoundforHostingDefault(std::shared_ptr< OCResource > resource);
- void onObserveDefault(AttributeMap &inputAttMap, OCResourceHandle resourceHandle);
- void notifyObserversDefault(OCResourceHandle resourceHandle);
-
- void startHosting(std::shared_ptr< OCResource > resource);
- void notifyObservers(OCResourceHandle resourceHandle);
-
- VirtualRepresentation findVirtualRepresentation(std::string uri);
- AttributeMap copyAttributeMap(AttributeMap &inputAttMap);
- bool isEmptyAttributeMap(AttributeMap &inputAttMap);
- void printAttributeMap(AttributeMap &inputAttMap);
-
- void addExtraStr(std::string str);
- std::string getExtraStr();
-};
-
-#endif /* RESOURCEMANAGER_H_ */
+++ /dev/null
-//******************************************************************
-//
-// Copyright 2014 Samsung Electronics All Rights Reserved.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-
-#ifndef VIRTUALREPRESENTATION_H_
-#define VIRTUALREPRESENTATION_H_
-
-#include "NotificationManager.h"
-
-#define SUCCESS_RESPONSE 0
-
-using namespace OC;
-using namespace OCPlatform;
-using namespace std;
-
-class ResourceManager;
-
-class VirtualRepresentation
-{
-
- private:
- std::string m_virtualUri;
- std::string m_originHostIP;
- std::string m_resourceTypeName;
- std::string m_resourceInterface;
- uint8_t m_resourceProperty;
-
- OCResourceHandle m_resourceHandle;
-
- static AttributeMap s_attributeMap;
- static std::mutex s_mutexAttributeMap;
- static std::condition_variable s_conditionAttributeMap;
- static bool m_isReadyAttributeMap;
-
- int getRepresentation(OCRepresentation &oc);
-
- public:
-
- std::string getUri();
- std::string getHostIP();
- std::string getResourceTypeName();
- std::string getResourceInterface();
- uint8_t getResourceProperty();
- OCResourceHandle getResourceHandle();
-
- int setUri(std::string uri);
- int setHostIP(std::string ip);
- int setResourceTypeName(std::string typeName);
- int setResourceInterface(std::string interface);
- int setResourceProperty(uint8_t property);
- int setResourceHandle(OCResourceHandle &handle);
-
- public:
- VirtualRepresentation();
- virtual ~VirtualRepresentation();
-
- std::string addVirtualTag(std::string uri);
-
- OCEntityHandlerResult entityHandler(const std::shared_ptr<OCResourceRequest> request ,
- const std::shared_ptr<OCResourceResponse> response);
- void onObserve(const HeaderOptions &headerOption, const OCRepresentation &rep ,
- const int eCode , const int sequenceNumber);
-
-};
-
-#endif /* VIRTUALREPRESENTATION_H_ */
// Iotivity Base CAPI
#include "ocstack.h"
-#include "ocsocket.h"
#include "logger.h"
+++ /dev/null
-//******************************************************************
-//
-// Copyright 2014 Samsung Electronics All Rights Reserved.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-
-#include "HostingHandler.h"
-
-HostingHandler *HostingHandler::s_instance = NULL;
-mutex HostingHandler::s_mutexForCreation;
-
-std::function< void(bool isHosting) > HostingHandler::s_findHostingCandidate;
-std::function< void(std::shared_ptr< OCResource > resource) > HostingHandler::s_startHosting;
-std::function< void(OCResourceHandle resourceHandle) > HostingHandler::s_notify;
-std::function< void(std::string) > HostingHandler::s_addExtraStr;
-
-HostingHandler::HostingHandler()
-{
-
-}
-
-HostingHandler::~HostingHandler()
-{
-
-}
-
-HostingHandler *HostingHandler::getInstance()
-{
- if (!s_instance)
- {
- s_mutexForCreation.lock();
- if (!s_instance)
- {
- s_instance = new HostingHandler();
- }
- s_mutexForCreation.unlock();
- }
-
- return s_instance;
-}
-
-void HostingHandler::initialize()
-{
- // Create Hosting Handler instance
- HostingHandler *ptr = HostingHandler::getInstance();
-
- // Registration interface about hosting
- HostingInterface hostingInterface;
-
- hostingInterface.setFindHosting(ptr->s_findHostingCandidate);
- hostingInterface.setStartHosting(ptr->s_startHosting);
- hostingInterface.setNotifyObservers(ptr->s_notify);
- hostingInterface.setAddExtraStr(ptr->s_addExtraStr);
-
- hostingInterface.setOnFoundHostingCandidate(
- std::function< void(std::shared_ptr< OCResource > resource) >(
- std::bind(&HostingHandler::onFoundCandidate , HostingHandler::getInstance() ,
- std::placeholders::_1)));
- hostingInterface.setOnObserve(
- std::function< void(AttributeMap &inputAttMap, OCResourceHandle resourceHandle) >(
- std::bind(&HostingHandler::onObserve , HostingHandler::getInstance() , std::placeholders::_1,
- std::placeholders::_2)));
-
- ptr->s_addExtraStr("virtual");
- ptr->startFindHost();
-}
-
-void HostingHandler::initialize(HostingConfig cfg)
-{
- // Create Hosting Handler instance
- HostingHandler *ptr = HostingHandler::getInstance();
-
- // Registration interface about hosting
- HostingInterface hostingInterface;
-
- hostingInterface.setFindHosting(ptr->s_findHostingCandidate);
- hostingInterface.setStartHosting(ptr->s_startHosting);
- hostingInterface.setNotifyObservers(ptr->s_notify);
- hostingInterface.setAddExtraStr(ptr->s_addExtraStr);
-
- hostingInterface.setOnFoundHostingCandidate(
- std::function< void(std::shared_ptr< OCResource > resource) >(
- std::bind(&HostingHandler::onFoundCandidate , HostingHandler::getInstance() ,
- std::placeholders::_1)));
- hostingInterface.setOnObserve(
- std::function< void(AttributeMap &inputAttMap, OCResourceHandle resourceHandle) >(
- std::bind(&HostingHandler::onObserve , HostingHandler::getInstance() , std::placeholders::_1,
- std::placeholders::_2)));
-
- // Set Hosting Config
- ptr->setHostingConfig(cfg);
- ptr->changeHostingMode(ptr->hostingCfg.hostingMode);
-
- ptr->s_addExtraStr("virtual");
- ptr->startFindHost();
-}
-
-void HostingHandler::setHostingConfig(HostingConfig cfg)
-{
- hostingCfg.automaticMethod = cfg.automaticMethod;
- hostingCfg.frequency = cfg.frequency;
- hostingCfg.hostingMode = cfg.hostingMode;
- hostingCfg.notifyMethod = cfg.notifyMethod;
-}
-
-void HostingHandler::changeHostingMode(HostingMode hostingMode, AutomaticMethod autoMethod)
-{
- if (hostingCfg.hostingMode != hostingMode)
- {
- hostingCfg.hostingMode = hostingMode;
- if (hostingCfg.hostingMode == HostingMode::AutomaticMode)
- {
- runAutomaticHosting(autoMethod);
- }
- else
- {
- stopAutomaticHosting();
- }
- }
-}
-
-void HostingHandler::runAutomaticHosting(AutomaticMethod autoMethod)
-{
- // TODO Triggering from Event Listener.
- if (hostingCfg.automaticMethod != autoMethod)
- {
- hostingCfg.automaticMethod = autoMethod;
- switch (hostingCfg.automaticMethod)
- {
- case AutomaticMethod::None:
- break;
- case AutomaticMethod::Timer:
- case AutomaticMethod::DeviceStatus:
- case AutomaticMethod::NetworkStatusChange:
- default:
- break;
- }
- }
-}
-
-void HostingHandler::stopAutomaticHosting()
-{
- if (hostingCfg.automaticMethod != AutomaticMethod::None)
- {
-// TODO
- }
-}
-
-void HostingHandler::changeAutomaticHostingMethod(AutomaticMethod autoMethod)
-{
-// TODO
-}
-void HostingHandler::changeNotifiyMethod(NotifyMethod notifyMethod)
-{
-// TODO
-}
-void HostingHandler::changeNotifyFrequencyType(NotifyFrequency notifyFrequency)
-{
-// TODO
-}
-
-void HostingHandler::startFindHost()
-{
- if (hostingCfg.hostingMode != HostingMode::None)
- {
- s_findHostingCandidate(true);
- }
-}
-
-void HostingHandler::onFoundCandidate(std::shared_ptr< OCResource > resource)
-{
- // TODO
- // Condition of Hosting
- s_startHosting(resource);
-}
-
-void HostingHandler::onObserve(AttributeMap &AttMap, OCResourceHandle resourceHandle)
-{
-
- switch (hostingCfg.notifyMethod)
- {
- case NotifyMethod::None:
- break;
- case NotifyMethod::Equalization:
- case NotifyMethod::Granularity:
- case NotifyMethod::Frequence:
- default:
- notifyFrequence(resourceHandle);
- break;
- }
-}
-
-void HostingHandler::notifyFrequence(OCResourceHandle resourceHandle)
-{
-
- switch (hostingCfg.frequency)
- {
- case NotifyFrequency::None:
- break;
- case NotifyFrequency::Periodically:
- case NotifyFrequency::OnTime:
- default:
- s_notify(resourceHandle);
- break;
- }
-}
+++ /dev/null
-//******************************************************************
-//
-// Copyright 2014 Samsung Electronics All Rights Reserved.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-
-#include "HostingInterface.h"
-
-HostingInterface::HostingInterface()
-{
-
-}
-
-HostingInterface::~HostingInterface()
-{
- // TODO Auto-generated destructor stub
-}
-
-int HostingInterface::setOnFoundHostingCandidate(
- std::function< void(std::shared_ptr< OCResource > resource) > func)
-{
- if (func != NULL)
- {
- try
- {
- ResourceManager::getInstance()->m_onFoundforHosting = func;
- }
- catch (exception &e)
- {
- return false;
- }
- }
- else
- {
- ResourceManager::getInstance()->m_onFoundforHosting = std::function <
- void(std::shared_ptr< OCResource > resource) > (
- std::bind(&ResourceManager::onFoundforHostingDefault , ResourceManager::getInstance() ,
- std::placeholders::_1));
- }
-
- return true;
-}
-
-int HostingInterface::setOnObserve(
- std::function< void(AttributeMap &inputAttMap, OCResourceHandle resourceHandle) > func)
-{
- if (func != NULL)
- {
- try
- {
- ResourceManager::getInstance()->m_onObserve = func;
- }
- catch (exception &e)
- {
- return false;
- }
- }
- else
- {
- ResourceManager::getInstance()->m_onObserve = std::function <
- void(AttributeMap & inputAttMap, OCResourceHandle resourceHandle) > (
- std::bind(&ResourceManager::onObserveDefault , ResourceManager::getInstance() ,
- std::placeholders::_1, std::placeholders::_2));
- }
-
- return true;
-}
-
-int HostingInterface::setNotifyObservers(std::function< void(OCResourceHandle resourceHandle) >
- &func)
-{
- try
- {
- func = std::function< void(OCResourceHandle resourceHandle) >(
- std::bind(&ResourceManager::notifyObservers , ResourceManager::getInstance() ,
- std::placeholders::_1));
- }
- catch (exception &e)
- {
- return false;
- }
-
- return true;
-}
-
-int HostingInterface::setStartHosting(
- std::function< void(std::shared_ptr< OCResource > resource) > &func)
-{
- try
- {
- func = std::function< void(std::shared_ptr< OCResource > resource) >(
- std::bind(&ResourceManager::startHosting , ResourceManager::getInstance() ,
- std::placeholders::_1));
- }
- catch (exception &e)
- {
- return false;
- }
-
- return true;
-}
-
-int HostingInterface::setFindHosting(std::function< void(bool isHosting) > &func)
-{
- try
- {
- func = std::function< void(bool isHosting) >(
- std::bind(&ResourceManager::findNMResource ,
- ResourceManager::getInstance() ,
- std::placeholders::_1));
- }
- catch (exception &e)
- {
- return false;
- }
-
- return true;
-}
-
-int HostingInterface::setAddExtraStr(std::function< void(std::string) > &func)
-{
- try
- {
- func = std::function< void(std::string str) >(
- std::bind(&ResourceManager::addExtraStr , ResourceManager::getInstance() ,
- std::placeholders::_1));
- }
- catch (exception &e)
- {
- return false;
- }
-
- return true;
-}
+++ /dev/null
-//******************************************************************
-//
-// Copyright 2014 Samsung Electronics All Rights Reserved.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-
-#include "NotificationManager.h"
-
-NotificationManager *NotificationManager::s_instance = NULL;
-mutex NotificationManager::s_mutexForCreation;
-
-NotificationManager::NotificationManager()
-{
-
-}
-
-NotificationManager::NotificationManager(HostingConfig cfg)
-{
-
-}
-
-NotificationManager::~NotificationManager()
-{
-
-}
-
-void NotificationManager::initialize()
-{
- // find local ip address
- std::string ipAddress;
- NotificationManager::getInstance()->scanAndGetNetworkInterface(ipAddress);
-
- // set ip address
- OICPlatformConfig::getInstance()->setIP(ipAddress);
-
- // initialize hosting handler
- HostingHandler::initialize();
-}
-
-void NotificationManager::initialize(HostingConfig cfg)
-{
- // find local ip address
- std::string ipAddress;
- NotificationManager::getInstance()->scanAndGetNetworkInterface(ipAddress);
-
- // set ip address
- OICPlatformConfig::getInstance()->setIP(ipAddress);
-
- // initialize hosting handler
- HostingHandler::initialize(cfg);
-}
-
-void NotificationManager::registerHostingEventListener()
-{
- // TODO : Initial HostingEventListener (v1.0)
-}
-
-NotificationManager *NotificationManager::getInstance()
-{
- if (!s_instance)
- {
- s_mutexForCreation.lock();
- if (!s_instance)
- {
- s_instance = new NotificationManager();
- }
- s_mutexForCreation.unlock();
- }
-
- return s_instance;
-}
-
-int NotificationManager::getNetInfo(IN int &sck, IN struct ifreq *item, OUT std::string &ip_addres)
-{
- struct ifreq temp_ifr;
- memset(&temp_ifr, 0, sizeof(temp_ifr));
- strcpy(temp_ifr.ifr_name, item->ifr_name);
-
- if (ioctl(sck, SIOCGIFFLAGS, &temp_ifr))
- {
- return -1;
- }
-
- if (!((temp_ifr.ifr_flags & IFF_UP) && (temp_ifr.ifr_flags & IFF_RUNNING)))
- {
- return -1;
- }
-
- std::string ip(inet_ntoa(((struct sockaddr_in *) &item->ifr_addr)->sin_addr));
- if (ip.empty())
- {
- return -1;
- }
-
- if (ip.find("127.0.0") == 0)
- {
- return -1;
- }
-
- ip_addres = ip;
- return 0;
-}
-
-bool NotificationManager::scanAndGetNetworkInterface(OUT std::string &ip_addres)
-{
- while (1)
- {
- char buf[1024] = { 0, };
- struct ifconf ifc;
- struct ifreq *ifr;
- int sck;
- int interfaces;
- int i;
-
- sck = socket(AF_INET, SOCK_DGRAM, 0);
- if (sck < 0)
- {
- usleep(10);
- continue;
- }
-
- ifc.ifc_len = sizeof(buf);
- ifc.ifc_buf = buf;
- if (ioctl(sck, SIOCGIFCONF, &ifc) < 0)
- {
- printf( "SIOCGIFCONF Failed ");
- close(sck);
- usleep(10);
- continue;
- }
-
- ifr = ifc.ifc_req;
- interfaces = ifc.ifc_len / sizeof(struct ifreq);
-
- for (i = 0; i < interfaces; i++)
- {
- if ( getNetInfo(sck, &ifr[i], ip_addres) == 0 )
- {
- return 0;
- }
- continue;
- }
- close(sck);
- usleep(10);
- }
-
- return 0;
-}
+++ /dev/null
-//******************************************************************
-//
-// Copyright 2014 Samsung Electronics All Rights Reserved.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-
-#include "OICPlatformConfig.h"
-
-
-OICPlatformConfig *OICPlatformConfig::s_instance = NULL;
-mutex OICPlatformConfig::s_mutexForCreation;
-//OCPlatform *OICPlatformConfig::s_nmOCPlatform = NULL;
-PlatformConfig OICPlatformConfig::s_cfg;
-//(ServiceType::InProc, ModeType::Both,"", 5683, QualityOfService::NonConfirmable);
-
-OICPlatformConfig::OICPlatformConfig()
-{
- // TODO Auto-generated constructor stub
-
-}
-
-OICPlatformConfig::~OICPlatformConfig()
-{
- // TODO Auto-generated destructor stub
-}
-
-
-OICPlatformConfig *OICPlatformConfig::getInstance()
-{
- if (!s_instance)
- {
- s_mutexForCreation.lock();
- if (!s_instance)
- {
- s_instance = new OICPlatformConfig();
- Configure(s_cfg);
- }
- s_mutexForCreation.unlock();
- }
-
- return s_instance;
-}
-
-//void OICPlatformConfig::getOCPlatform()
-//{
-//// if(!s_nmOCPlatform)
-//// {
-//// if(s_cfg.ipAddress.empty())
-//// {
-//// return NULL;
-//// }
-//// s_nmOCPlatform = new OCPlatform(s_cfg);
-//// Configure(s_cfg);
-//// }
-//// return s_nmOCPlatform;
-//}
-
-void OICPlatformConfig::initialize()
-{
-
-}
-
-void OICPlatformConfig::setIP(std::string ipaddress)
-{
- s_cfg.ipAddress = ipaddress;
-}
+++ /dev/null
-//******************************************************************
-//
-// Copyright 2014 Samsung Electronics All Rights Reserved.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-
-#include "RegistrationManager.h"
-
-RegistrationManager *RegistrationManager::s_instance = NULL;
-mutex RegistrationManager::s_mutexForCreation;
-
-RegistrationManager::RegistrationManager()
-{
-}
-
-RegistrationManager::~RegistrationManager()
-{
-}
-
-RegistrationManager *RegistrationManager::getInstance()
-{
- if (!s_instance)
- {
- s_mutexForCreation.lock();
- if (s_instance)
- {
- s_instance = new RegistrationManager();
- }
- s_mutexForCreation.unlock();
- }
-
- return s_instance;
-}
-
-int RegistrationManager::addResource()
-{
- return 0;
-}
-
-int RegistrationManager::removeResource()
-{
- return 0;
-}
-
-int RegistrationManager::updateResource()
-{
- return 0;
-}
-
-bool RegistrationManager::registerNMResource(VirtualRepresentation &resourceObject ,
- std::shared_ptr< OCResource > resource)
-{
- std::string uri = resourceObject.getUri();
- std::string type = resourceObject.getResourceTypeName();
- std::string interface = resourceObject.getResourceInterface();
-
- OCResourceHandle resourceHandle;
-
- OCStackResult result;
- result = registerResource(resourceHandle , uri , type ,
- interface ,
- std::function <
- OCEntityHandlerResult(const std::shared_ptr< OCResourceRequest > request ,
- const std::shared_ptr< OCResourceResponse > response) > (
- std::bind(&VirtualRepresentation::entityHandler , resourceObject ,
- std::placeholders::_1 , std::placeholders::_2)) ,
- resourceObject.getResourceProperty());
-
- resourceObject.setResourceHandle(resourceHandle);
-
- if (OC_STACK_OK != result)
- {
- return false;
- }
- else
- {
- QueryParamsMap queryParmaMap;
- resource->observe(ObserveType::Observe , queryParmaMap ,
- std::function <
- void(const HeaderOptions headerOption,
- const OCRepresentation & rep , const int &eCode ,
- const int &sequenceNumber) > (
- std::bind(&VirtualRepresentation::onObserve , resourceObject ,
- std::placeholders::_1 , std::placeholders::_2 ,
- std::placeholders::_3 , std::placeholders::_4)));
- }
-
- return true;
-}
-
-bool RegistrationManager::unregisterResource()
-{
- return true;
-}
-
+++ /dev/null
-//******************************************************************
-//
-// Copyright 2014 Samsung Electronics All Rights Reserved.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-
-#include "ResourceManager.h"
-
-ResourceManager *ResourceManager::s_instance = NULL;
-mutex ResourceManager::s_mutexForCreation;
-std::list< VirtualRepresentation > ResourceManager::s_resourceList;
-
-std::string ResourceManager::s_extraStr;
-
-ResourceManager::ResourceManager()
-{
- m_onFoundforHosting = NULL;
- m_onObserve = NULL;
- m_notify = NULL;
-}
-
-ResourceManager::~ResourceManager()
-{
-}
-
-ResourceManager *ResourceManager::getInstance()
-{
- if (!s_instance)
- {
- s_mutexForCreation.lock();
- if (!s_instance)
- {
- s_instance = new ResourceManager();
- }
- s_mutexForCreation.unlock();
- }
- return s_instance;
-}
-
-VirtualRepresentation ResourceManager::findVirtualRepresentation(std::string uri)
-{
-
- VirtualRepresentation retObject;
-
- for (auto it = s_resourceList.begin() ; it != s_resourceList.end() ; it++)
- {
- if (it->getUri().compare(uri) == 0)
- {
- retObject = *it;
- return retObject;
- }
- }
-
- return retObject;
-}
-
-void ResourceManager::findNMResource(bool isHosting)
-{
- if (isHosting)
- {
- findResource("" , "coap://224.0.1.187/oc/core",
- std::function< void(std::shared_ptr< OCResource > resource) >(
- std::bind(&ResourceManager::foundResourceforhosting , ResourceManager::getInstance() ,
- std::placeholders::_1)));
- }
-}
-
-void ResourceManager::foundResourceforhosting(std::shared_ptr< OCResource > resource)
-{
- try
- {
- if (resource)
- {
- if (resource->uri().find("/a/NM") != std::string::npos)
- {
- ResourceManager::getInstance()->m_onFoundforHosting(resource);
- }
- }
- else
- {
- // TODO
- }
-
- }
- catch (std::exception &e)
- {
- }
-}
-
-void ResourceManager::startHosting(std::shared_ptr< OCResource > resource)
-{
-
- cout << "start hosting" << endl;
- VirtualRepresentation tmp = findVirtualRepresentation( resource->uri() );
-
- if ( !tmp.getUri().empty() )
- {
- return;
- }
-
- VirtualRepresentation resourceObject;
- resourceObject.setUri(resource->uri());
-
- std::cout << "resourceObject uri: " << resourceObject.getUri() << std::endl;
-
- std::string resourceHostIP;
- std::string resourceType;
- std::string resourceInterface;
- uint8_t resourceProperty;
-
- resourceHostIP = resource->host();
- resourceType = *(resource->getResourceTypes().data());
- resourceInterface = *(resource->getResourceInterfaces().data());
- resourceProperty = (OC_DISCOVERABLE | resource->isObservable());
-
- resourceObject.setHostIP(resourceHostIP);
- resourceObject.setResourceTypeName(resourceType);
- resourceObject.setResourceInterface(resourceInterface);
- resourceObject.setResourceProperty(resourceProperty);
-
- RegistrationManager::getInstance()->registerNMResource(resourceObject , resource);
-
- s_resourceList.push_back(resourceObject);
-
-}
-
-void ResourceManager::notifyObservers(OCResourceHandle resourceHandle)
-{
- OCStackResult result = OC_STACK_OK;
-
- result = notifyAllObservers(resourceHandle);
-
- if (OC_STACK_NO_OBSERVERS == result)
- {
- // No observers.
- // TODO
- }
-}
-
-AttributeMap ResourceManager::copyAttributeMap(AttributeMap &inputAttMap)
-{
-
- AttributeMap retAttMap;
-
- retAttMap = inputAttMap;
-// for(auto it = inputAttMap.begin() ; it != inputAttMap.end() ; ++it)
-// {
-// AttributeValues tmpVal;
-//
-// for(auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
-// {
-// std::string tmpStr;
-//
-// tmpStr.append(*valueItr);
-//
-// tmpVal.push_back(tmpStr);
-// }
-// retAttMap[it->first] = tmpVal;
-//
-// }
- return retAttMap;
-}
-
-bool ResourceManager::isEmptyAttributeMap(AttributeMap &inputAttMap)
-{
- for (auto it = inputAttMap.begin() ; it != inputAttMap.end() ; ++it)
- {
- if (inputAttMap.find(it->first) == inputAttMap.end())
- {
- return true;
- }
- }
- return false;
-}
-
-void ResourceManager::onFoundforHostingDefault(std::shared_ptr< OCResource > resource)
-{
- ResourceManager::getInstance()->startHosting(resource);
-}
-void ResourceManager::onObserveDefault(AttributeMap &inputAttMap , OCResourceHandle resourceHandle)
-{
- ResourceManager::getInstance()->notifyObservers(resourceHandle);
-}
-
-void ResourceManager::printAttributeMap(AttributeMap &inputAttMap)
-{
- for (auto it = inputAttMap.begin() ; it != inputAttMap.end() ; ++it)
- {
- std::cout << "\tAttribute name: " << it->first << " value: ";
-
- for (auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
- {
- std::cout << "\t" << *valueItr << " ";
- }
-
- std::cout << std::endl;
- }
-}
-
-void ResourceManager::addExtraStr(std::string str)
-{
- s_extraStr = str;
-}
-
-std::string ResourceManager::getExtraStr()
-{
- return s_extraStr;
-}
-
-void ResourceManager::checkResourceDBPolicy()
-{
-
-}
-
-void ResourceManager::saveResourceDB()
-{
-
-}
+++ /dev/null
-//******************************************************************
-//
-// Copyright 2014 Samsung Electronics All Rights Reserved.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-
-#include "VirtualRepresentation.h"
-
-AttributeMap VirtualRepresentation::s_attributeMap;
-
-std::mutex VirtualRepresentation::s_mutexAttributeMap;
-std::condition_variable VirtualRepresentation::s_conditionAttributeMap;
-bool VirtualRepresentation::m_isReadyAttributeMap = true;
-
-int g_num = 0;
-
-VirtualRepresentation::VirtualRepresentation()
-{
- m_resourceHandle = NULL;
- m_resourceProperty = 0x00;
-}
-
-VirtualRepresentation::~VirtualRepresentation()
-{
-}
-
-std::string VirtualRepresentation::addVirtualTag(std::string uri)
-{
- std::string ret = uri;
- return ret.append(VIRTURL_TAG + std::to_string(g_num++));
-}
-
-OCResourceHandle VirtualRepresentation::getResourceHandle()
-{
- return m_resourceHandle;
-}
-
-std::string VirtualRepresentation::getUri()
-{
- return m_virtualUri;
-}
-
-std::string VirtualRepresentation::getHostIP()
-{
- return m_originHostIP;
-}
-
-std::string VirtualRepresentation::getResourceTypeName()
-{
- return m_resourceTypeName;
-}
-
-std::string VirtualRepresentation::getResourceInterface()
-{
- return m_resourceInterface;
-}
-
-uint8_t VirtualRepresentation::getResourceProperty()
-{
- return m_resourceProperty;
-}
-
-int VirtualRepresentation::setUri(std::string uri)
-{
- m_virtualUri = addVirtualTag(uri);
- return true;
-}
-
-int VirtualRepresentation::setHostIP(std::string ip)
-{
- m_originHostIP = ip;
- return true;
-}
-
-int VirtualRepresentation::setResourceTypeName(std::string typeName)
-{
- m_resourceTypeName = typeName;
- return true;
-}
-
-int VirtualRepresentation::setResourceInterface(std::string interface)
-{
- m_resourceInterface = interface;
- return true;
-}
-
-int VirtualRepresentation::setResourceProperty(uint8_t property)
-{
- m_resourceProperty = property;
- return true;
-}
-
-int VirtualRepresentation::setResourceHandle(OCResourceHandle &handle)
-{
- m_resourceHandle = handle;
- return true;
-}
-
-int VirtualRepresentation::getRepresentation(OCRepresentation &oc)
-{
- oc.setAttributeMap(s_attributeMap);
- oc.setUri(this->getUri().c_str());
- return true;
-}
-
-OCEntityHandlerResult VirtualRepresentation::entityHandler(const std::shared_ptr<OCResourceRequest>
- request , const std::shared_ptr<OCResourceResponse> response)
-{
- if (request)
- {
- std::string requestType = request->getRequestType();
- int requestFlag = request->getRequestHandlerFlag();
-
- if (requestFlag == RequestHandlerFlag::InitFlag)
- {
- }
-
- else if (requestFlag == RequestHandlerFlag::RequestFlag)
- {
- if ( (requestType == "GET") && response )
- {
-
- std::unique_lock< std::mutex > lck(s_mutexAttributeMap);
- while (!m_isReadyAttributeMap)
- {
- s_conditionAttributeMap.wait(lck);
- }
- m_isReadyAttributeMap = false;
-
- OCRepresentation rep;
- getRepresentation(rep);
-
- response->setErrorCode(200);
- response->setResourceRepresentation(rep , DEFAULT_INTERFACE);
-
- m_isReadyAttributeMap = true;
- s_conditionAttributeMap.notify_all();
- }
- else if (requestType == "PUT")
- {
- // TODO
- }
- else if (requestType == "POST")
- {
- // TODO
- }
- else if (requestType == "DELETE")
- {
- // TODO
- }
- else
- {
- // TODO
- }
- }
- else if (requestFlag == RequestHandlerFlag::InitFlag)
- {
- // TODO
- }
- else if (requestFlag == RequestHandlerFlag::ObserverFlag)
- {
- // TODO
- cout << "requestFlag == RequestHandlerFlag::ObserverFlag\n";
- }
- else
- {
- // requestFlag is not [Request, Init, Observer]
- // TODO
- }
- }
- else
- {
- // Param(request) is empty.
- // TODO
- }
- return OC_EH_OK;
-}
-
-void VirtualRepresentation::onObserve(const HeaderOptions &headerOption,
- const OCRepresentation &rep , const int eCode , const int sequenceNumber)
-{
- if (eCode == SUCCESS_RESPONSE)
- {
- AttributeMap inputAttributeMap = rep.getAttributeMap();
- if (ResourceManager::getInstance()->isEmptyAttributeMap(inputAttributeMap))
- {
- return;
- }
- VirtualRepresentation tmpObj = *this;
- if (!tmpObj.getUri().empty())
- {
- AttributeMap tmpAttMap = ResourceManager::getInstance()->copyAttributeMap(inputAttributeMap);
- {
- std::unique_lock< std::mutex > lck(s_mutexAttributeMap);
- while (!m_isReadyAttributeMap)
- {
- s_conditionAttributeMap.wait(lck);
- }
- m_isReadyAttributeMap = false;
- s_attributeMap = tmpAttMap;
- m_isReadyAttributeMap = true;
- s_conditionAttributeMap.notify_all();
- }
-
- if (ResourceManager::getInstance()->m_onObserve)
- {
- ResourceManager::getInstance()->m_onObserve(inputAttributeMap, tmpObj.getResourceHandle());
- }
- }
- }
- else
- {
- // Check the error.
- // TODO
- }
- cout << "VirtualRepresentation::onObserve Out\n";
-}
s_mirrorResourceList = createMirrorResourceList();
s_requestHandleList = createRequestHandleList();
result = requestPresence(OC_DEFAULT_ADDRESS);
-
if(result != OC_STACK_OK)
{
return OC_STACK_ERROR;
cbData.context = (void *)DEFAULT_CONTEXT_VALUE;
cbData.cd = NULL;
- result = OCDoResource(&handle, OC_REST_GET, queryUri, OIC_COORDINATING_FLAG, 0, OC_LOW_QOS, &cbData,
- NULL, 0);
+ result = OCDoResource(&handle, OC_REST_GET, queryUri, OIC_COORDINATING_FLAG, 0,
+ OC_ETHERNET, OC_LOW_QOS, &cbData, NULL, 0);
if (result != OC_STACK_OK)
{
OC_LOG_V(DEBUG, HOSTING_TAG, "OCStack resource error");
sprintf(queryUri, "coap://%s%s", sourceResourceAddress , OC_PRESENCE_URI);
OC_LOG_V(DEBUG, HOSTING_TAG, "initializePresenceForCoordinating Query : %s", queryUri);
- result = OCDoResource(&handle, OC_REST_PRESENCE, queryUri, 0, 0, OC_LOW_QOS, &cbData, NULL, 0);
+ result = OCDoResource(&handle, OC_REST_PRESENCE, queryUri, 0, 0, OC_ETHERNET, OC_LOW_QOS, &cbData, NULL, 0);
if (result != OC_STACK_OK)
{
OIC_COORDINATING_FLAG);
result = OCDoResource(&mirrorResource->resourceHandle[OIC_REQUEST_HANDLE], OC_REST_OBSERVE, query,
- 0, NULL,
+ 0, NULL, OC_ETHERNET,
OC_HIGH_QOS, &cbData, NULL, 0);
if (result != OC_STACK_OK)
((OCEntityHandlerRequest*)request->requestHandle[OIC_REQUEST_BY_CLIENT])->reqJSONPayload);
result = OCDoResource(&request->requestHandle[OIC_REQUEST_BY_COORDINATOR],
- method, queryFullUri, NULL, payload, OC_LOW_QOS, &cbData, NULL, 0);
+ method, queryFullUri, NULL, payload, OC_ETHERNET, OC_LOW_QOS, &cbData, NULL, 0);
}
else{
result = OCDoResource(&request->requestHandle[OIC_REQUEST_BY_COORDINATOR],
- method, queryFullUri, NULL, 0, OC_LOW_QOS, &cbData, NULL, 0);
+ method, queryFullUri, NULL, 0, OC_ETHERNET, OC_LOW_QOS, &cbData, NULL, 0);
}
if (result != OC_STACK_OK)
+++ /dev/null
-//******************************************************************
-//
-// Copyright 2014 Samsung Electronics All Rights Reserved.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-
-#include <iostream>
-#include "NotificationManager.h"
-
-using namespace OC;
-
-int main(void)
-{
-
- std::cout << endl;
- std::cout << "========================================================" << endl;
- std::cout << "Start Notification Manager : Hosting v0.5\n";
-
-
- HostingConfig cfg;
- cfg.hostingMode = HostingMode::ManualMode;
- cfg.automaticMethod = AutomaticMethod::None;
- cfg.notifyMethod = NotifyMethod::Frequence;
- cfg.frequency = NotifyFrequency::OnTime;
-
- NotificationManager::initialize(cfg);
-
- while (true)
- {
- char signal;
- cin >> signal;
-
- switch (signal)
- {
- case 'q':
- case 'Q':
- std::cout << endl;
- std::cout << "========================================================" << endl;
- std::cout << "End Notification Manager : Hosting v0.5\n";
- return true;
- default:
- break;
- }
-
- }
-
- std::cout << endl;
- std::cout << "========================================================" << endl;
- std::cout << "End Notification Manager : Hosting v0.5\n";
-
- return true;
-}
#include <stdlib.h>
#include "ocstack.h"
-#include "ocsocket.h"
#include "logger.h"
#include "cJSON.h"
env.AppendUnique(CPPDEFINES = ['NDEBUG'])
else:
env.AppendUnique(CCFLAGS = ['-g'])
+
+if env.get('LOGGING'):
env.AppendUnique(CPPDEFINES = ['-DTB_LOG'])
lib_env = env.Clone()
notimgr_env.AppendUnique(CPPPATH = ['NotificationManager/include'])
notimgr_env.AppendUnique(CPPPATH = ['../../extlibs/cjson'])
notimgr_env.AppendUnique(CPPPATH = ['../../resource/csdk/logger/include'])
-notimgr_env.PrependUnique(LIBS = ['oc', 'octbstack', 'oc_logger', 'coap'])
+notimgr_env.PrependUnique(LIBS = ['oc', 'octbstack', 'oc_logger', 'connectivity_abstraction', 'libcoap'])
if target_os not in ['windows', 'winrt']:
notimgr_env.AppendUnique(CXXFLAGS = ['-O2', '-g', '-Wall', '-fmessage-length=0'])
-#******************************************************************
-#
-# Copyright 2014 Samsung Electronics All Rights Reserved.
-#
-#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-
##
# NotificationManager build script
##
# Build flags
######################################################################
notimgr_env.AppendUnique(CPPPATH = ['../../NotificationManager/include'])
-notimgr_env.AppendUnique(LIBS = ['oc', 'octbstack', 'oc_logger', 'coap', 'pthread'])
+notimgr_env.AppendUnique(CPPPATH = ['../../../../extlibs/cjson'])
+notimgr_env.AppendUnique(CPPPATH = ['../../../../resource/csdk/connectivity/api'])
notimgr_env.AppendUnique(CXXFLAGS = ['-O2', '-g', '-Wall', '-fmessage-length=0', '-std=c++0x'])
+notimgr_c_env = notimgr_env.Clone()
+notimgr_env.AppendUnique(LIBS = ['oc', 'octbstack', 'oc_logger', 'connectivity_abstraction', 'coap', 'pthread'])
+notimgr_c_env.AppendUnique(LIBS = ['octbstack', 'oc_logger', 'connectivity_abstraction', 'coap', 'pthread','NOTISDKLibrary'])
-######################################################################
+if env.get('SECURED') == '1':
+ notimgr_env.AppendUnique(LIBS = ['tinydtls'])
+notimgr_env.ParseConfig('pkg-config --libs glib-2.0')
+notimgr_env.ParseConfig('pkg-config --cflags glib-2.0')
+if 'rt' in notimgr_env.get('LIBS'):
+ notimgr_env.Append(LIBS = ['rt'])
+
+if env.get('SECURED') == '1':
+ notimgr_c_env.AppendUnique(LIBS = ['tinydtls'])
+notimgr_c_env.ParseConfig('pkg-config --libs glib-2.0')
+notimgr_c_env.ParseConfig('pkg-config --cflags glib-2.0')
+if 'rt' in notimgr_c_env.get('LIBS'):
+ notimgr_c_env.Append(LIBS = ['rt'])
+
+####################################################################
# Source files and Targets
######################################################################
sampleprovider = notimgr_env.Program('sampleprovider', 'sampleProvider/SampleProvider.cpp')
sampleconsumer = notimgr_env.Program('sampleconsumer', 'sampleConsumer/SampleConsumer.cpp')
+notificationmanager = notimgr_c_env.Program('notificationmanager', 'notificationManager/main.c')
+
+#notimgr_env.InstallTarget(sampleprovider, 'sampleprovider')
+#notimgr_env.InstallTarget(sampleconsumer, 'sampleconsumer')
+#notimgr_env.InstallTarget(notificationmanager, 'notificationmanager')
*/
int main()
{
-
- uint8_t interfaceAddress[20] = {0};
- uint8_t *coordinatingAddress = NULL;
- uint8_t interfaceName[] = "eth0";
-
printf("OCResourceHosting is starting...\n");
- if ( OCGetInterfaceAddress(interfaceName, sizeof(interfaceName), AF_INET, interfaceAddress,
- sizeof(interfaceAddress)) == OC_ERR_SUCCESS)
- {
- printf("Starting OIC resource hosting on address %s\n", interfaceAddress);
- coordinatingAddress = interfaceAddress;
- }
-
- if (OCInit((char *) coordinatingAddress, USE_RANDOM_PORT, OC_CLIENT_SERVER) != OC_STACK_OK)
+ if (OCInit((char *) NULL, 0, OC_CLIENT_SERVER) != OC_STACK_OK)
{
printf("OCStack init error\n");
return 0;
//******************************************************************
//
-// Copyright 2014 Samsung Electronics All Rights Reserved.
+// Copyright 2015 Samsung Electronics All Rights Reserved.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
#include <pthread.h>
#include "OCPlatform.h"
#include "OCApi.h"
-
+#include <mutex>
using namespace OC;
const int SUCCESS_RESPONSE = OC_STACK_OK;
-static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
-std::shared_ptr< OCResource > g_curResource;
+#define OBSERVE 1
+#define GET 2
+#define PUT 3
+#define DELETE 4
+std::shared_ptr< OCResource > g_curResource;
+std::mutex curResourceLock;
OCStackResult nmfindResource(const std::string &host , const std::string &resourceName);
void onObserve(const HeaderOptions &headerOption , const OCRepresentation &rep , const int &eCode,
const int &sequenceNumber);
+void onPut(const HeaderOptions &headerOption, const OCRepresentation &rep, const int eCode);
+void onGet(const HeaderOptions &headerOption , const OCRepresentation &rep , const int eCode);
+void onDelete(const HeaderOptions &headerOption , const int eCode);
+
+
void findResourceCandidate()
{
try
{
- nmfindResource("" , "coap://224.0.1.187/oc/core?rt=NotificationManager.Hosting");
+ nmfindResource("" , "coap://224.0.1.187/oc/core?rt=Resource.Hosting");
std::cout << "Finding Resource... " << std::endl;
- while (true)
- {
- char signal;
- cin >> signal;
-
- switch (signal)
- {
- case 'q':
- case 'Q':
- exit(-1);
- default:
- break;
- }
- }
}
catch (OCException &e)
void startObserve(std::shared_ptr< OCResource > resource)
{
+ if (resource == NULL)
+ {
+ std::cout << "startObserve() error : resource == null" << std::endl;
+ return;
+ }
+
+ QueryParamsMap test;
+ if (OC_STACK_OK != resource->observe(ObserveType::Observe , test , &onObserve))
+ std::cout << "To Fail resource observe() process" << std::endl;
+}
+
+void startGet(std::shared_ptr< OCResource > resource)
+{
+
+ if (resource == NULL)
+ {
+ std::cout << "startObserve() error : resource == null" << std::endl;
+ return;
+ }
+
+ QueryParamsMap test;
+ std::cout << "URI :" << resource->uri() << std::endl;
+ if (OC_STACK_OK != resource->get(test, &onGet))
+ std::cout << "To Fail resource get() process" << std::endl;
+}
+
+void startPut(std::shared_ptr< OCResource > resource)
+{
+ if (resource == NULL)
+ {
+ std::cout << "startObserve() error : resource == null" << std::endl;
+ return;
+ }
+
g_curResource = resource;
+ OCRepresentation rep;
+ rep.setValue("temperature", 25);
+ rep.setValue("humidity", 10);
QueryParamsMap test;
- resource->observe(ObserveType::Observe , test , &onObserve);
+ if (OC_STACK_OK != resource->put(rep, test, &onPut))
+ std::cout << "To Fail resource put() process" << std::endl;
+}
+
+void startDelete(std::shared_ptr< OCResource > resource)
+{
+ if (resource == NULL)
+ {
+ std::cout << "startObserve() error : resource == null" << std::endl;
+ return;
+ }
+
+ g_curResource = resource;
+ if (OC_STACK_OK != resource->deleteResource(&onDelete))
+ std::cout << "To Fail resource delete() process" << std::endl;
}
int observe_count()
const int &sequenceNumber)
{
std::cout << "onObserve" << std::endl;
-// if(eCode == SUCCESS_RESPONSE)
- if (eCode <= OC_STACK_RESOURCE_DELETED)
- {
-
- AttributeMap attributeMap = rep.getAttributeMap();
-
- for (auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
- {
- if (attributeMap.find(it->first) == attributeMap.end())
- {
- return;
- }
- }
-
- if (rep.getUri().empty())
- {
- cout << "uri is null\n";
- return;
- }
+ if (eCode <= OC_STACK_OK)
+ {
std::cout << std::endl;
std::cout << "========================================================" << std::endl;
std::cout << "Receive OBSERVE RESULT:" << std::endl;
+ std::cout << "\tUri: " << rep.getUri() << std::endl;
std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
- for (auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
- {
- std::cout << "\tAttribute name: " << it->first << " value: ";
- for (auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
- {
- std::cout << "\t" << *valueItr << " ";
- }
-
- std::cout << std::endl;
- }
+ std::cout << "\tTemperature : " << rep.getValue<int>("temperature") << std::endl;
+ std::cout << "\tHumidity : " << rep.getValue<int>("humidity") << std::endl;
if (observe_count() > 30)
{
{
std::string resourceURI;
std::string hostAddress;
+
+ std::cout << "foundResource" << std::endl;
+
try
{
+ std::cout << "mutex lock passed" << std::endl;
if (resource)
{
-// if(resource->uri().find("/a/NM/TempHumSensor/virtual") != std::string::npos)
- if (resource->uri().find("/a/NM/TempHumSensor") != std::string::npos)
+ std::cout << resource->uri() << std::endl;
+ if (resource->uri() == "/a/TempHumSensor")
{
std::cout << std::endl;
std::cout << "========================================================" << std::endl;
hostAddress = resource->host();
std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
- startObserve(resource);
+ g_curResource = resource;
}
}
else
OCStackResult nmfindResource(const std::string &host , const std::string &resourceName)
{
- return OCPlatform::findResource(host , resourceName , &foundResource);
+ return OCPlatform::findResource(host , resourceName , OC_ETHERNET, &foundResource);
}
void getRepresentation(std::shared_ptr< OCResource > resource)
}
}
-void onPut(const OCRepresentation &rep , const int eCode)
+void onPut(const HeaderOptions &headerOption, const OCRepresentation &rep, const int eCode)
{
- if (eCode == SUCCESS_RESPONSE)
+ try
{
- std::cout << "PUT request was successful" << std::endl;
+ if (eCode == OC_STACK_OK)
+ {
+ std::cout << "PUT request was successful" << std::endl;
+ int humidity;
+ int temperature;
+ rep.getValue("temperature", temperature);
+ rep.getValue("humidity", humidity);
- AttributeMap attributeMap = rep.getAttributeMap();
- for (auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
+ std::cout << "\t temperature: " << temperature << std::endl;
+ std::cout << "\t humidity: " << humidity << std::endl;
+ }
+ else
{
- std::cout << "\tAttribute name: " << it->first << " value: ";
- for (auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
- {
- std::cout << "\t" << *valueItr << " ";
- }
-
- std::cout << std::endl;
+ std::cout << "onPut Response error: " << eCode << std::endl;
+ std::exit(-1);
}
+ }
+ catch (std::exception &e)
+ {
+ std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
+ }
+}
- std::vector< OCRepresentation > children = rep.getChildren();
-
- for (auto oit = children.begin() ; oit != children.end() ; ++oit)
+//callback hadnler on DELETE request
+void onDelete(const HeaderOptions &headerOption , const int eCode)
+{
+ try
+ {
+ if (eCode == OC_STACK_RESOURCE_DELETED)
{
- attributeMap = oit->getAttributeMap();
-
- for (auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
- {
- std::cout << "\tAttribute name: " << it->first << " value: ";
- for (auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
- {
- std::cout << "\t" << *valueItr << " ";
- }
-
- std::cout << std::endl;
- }
+ std::cout << "DELETE request was successful" << std::endl;
+ }
+ else
+ {
+ std::cout << "onDelete Response error: " << eCode << std::endl;
+ std::exit(-1);
}
-
- if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
- std::cout << std::endl << "Observe is used." << std::endl << std::endl;
- else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
- std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
-
- QueryParamsMap test;
-
- g_curResource->observe(ObserveType::Observe , test , &onObserve);
-
}
- else
+ catch (std::exception &e)
{
- std::cout << "onPut Response error: " << eCode << std::endl;
- std::exit(-1);
+ std::cout << "Exception: " << e.what() << " in onDelete" << std::endl;
}
}
// callback handler on GET request
void onGet(const HeaderOptions &headerOption , const OCRepresentation &rep , const int eCode)
{
+ std::cout << "GET request was successful1" << std::endl;
if (eCode == SUCCESS_RESPONSE)
{
std::cout << "GET request was successful" << std::endl;
-
- AttributeMap attributeMap = rep.getAttributeMap();
-
- std::cout << "Resource URI: " << rep.getUri() << std::endl;
-
- for (auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
- {
- std::cout << "\tAttribute name: " << it->first << " value: ";
- for (auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
- {
- std::cout << "\t" << *valueItr << " ";
- }
-
- std::cout << std::endl;
- }
-
- std::vector< OCRepresentation > children = rep.getChildren();
-
- for (auto oit = children.begin() ; oit != children.end() ; ++oit)
- {
- std::cout << "Child Resource URI: " << oit->getUri() << std::endl;
-
- attributeMap = oit->getAttributeMap();
-
- for (auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
- {
- std::cout << "\tAttribute name: " << it->first << " value: ";
- for (auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
- {
- std::cout << "\t" << *valueItr << " ";
- }
-
- std::cout << std::endl;
- }
- }
+ std::cout << "Resource URI: " << rep.getUri().c_str() << std::endl;
+ std::cout << "\tTemperature : " << rep.getValue<int>("temperature") << std::endl;
+ std::cout << "\tHumidity : " << rep.getValue<int>("humidity") << std::endl;
}
else
{
std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
}
-int main(int argc , char *argv[])
+void PRINT()
{
+ std::cout << std::endl;
+ std::cout << "********************************************" << std::endl;
+ std::cout << "* method Type : 1 - Observe *" << std::endl;
+ std::cout << "* method Type : 2 - Get *" << std::endl;
+ std::cout << "* method Type : 3 - Put *" << std::endl;
+ std::cout << "* method Type : 4 - Delete *" << std::endl;
+ std::cout << "********************************************" << std::endl;
+ std::cout << std::endl;
+}
- if (argc == 1)
- {
- OBSERVE_TYPE_TO_USE = ObserveType::Observe;
- }
- else if (argc == 2)
- {
- int value = atoi(argv[1]);
- if (value == 1)
- OBSERVE_TYPE_TO_USE = ObserveType::Observe;
- else if (value == 2)
- OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
- else
- OBSERVE_TYPE_TO_USE = ObserveType::Observe;
- }
- else
- {
- PrintUsage();
- return -1;
- }
+int main(int argc , char *argv[])
+{
+ int in;
PlatformConfig cfg;
OCPlatform::Configure(cfg);
std::cout << "Created Platform..." << std::endl;
findResourceCandidate();
+ while (1)
+ {
+ PRINT();
+
+ std::cin >> in;
+ switch (in)
+ {
+ case OBSERVE:
+ startObserve(g_curResource);
+ break;
+ case GET:
+ startGet(g_curResource);
+ break;
+ case PUT:
+ startPut(g_curResource);
+ break;
+ case DELETE:
+ startDelete(g_curResource);
+ break;
+ default:
+ std::cout << "Invalid input, please try again" << std::endl;
+ break;
+ }
+ }
+
return 0;
}
-
//******************************************************************
//
-// Copyright 2014 Samsung Electronics All Rights Reserved.
+// Copyright 2015 Samsung Electronics All Rights Reserved.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
#include <functional>
#include <pthread.h>
+#include <signal.h>
#include "OCPlatform.h"
#include "OCApi.h"
using namespace OC;
using namespace std;
+using namespace OC::OCPlatform;
int g_Observation = 0;
+int gQuitFlag = 0;
pthread_cond_t m_cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t m_mutex = PTHREAD_MUTEX_INITIALIZER;
-OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request ,
- std::shared_ptr< OCResourceResponse > response);
+
+
+OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request);
class TempHumidResource
{
int m_temp;
int m_humid;
-
std::string m_uri;
OCResourceHandle m_resourceHandle;
-
+ ObservationIds m_interestedObservers;
OCRepresentation m_Rep;
public:
void createResource()
{
- std::string resourceURI = "/a/NM/TempHumSensor";
- std::string resourceTypeName = "NotificationManager.Hosting";
+ std::string resourceURI = "/a/TempHumSensor/hosting";
+ std::string resourceTypeName = "Resource.Hosting";
std::string resourceInterface = DEFAULT_INTERFACE;
m_uri = resourceURI;
OCRepresentation post(OCRepresentation &rep)
{
static int first = 1;
-
// for the first time it tries to create a resource
if (first)
{
OCRepresentation get()
{
- cout << "resource get\n";
m_Rep.setValue("temperature", m_temp);
m_Rep.setValue("humidity", m_humid);
-
- cout << "resource get : done\n";
-
return m_Rep;
}
-
+ OCStackResult deleteDeviceResource()
+ {
+ OCStackResult result = OCPlatform::unregisterResource(m_resourceHandle);
+ if (OC_STACK_OK != result)
+ {
+ throw std::runtime_error(
+ std::string("Device Resource failed to unregister/delete") + std::to_string(result));
+ }
+ return result;
+ }
};
TempHumidResource myResource;
return NULL;
}
-OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request ,
- std::shared_ptr< OCResourceResponse > response)
+OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request)
{
cout << "Sample Provider entityHandler\n";
+
+ OCEntityHandlerResult ehResult = OC_EH_ERROR;
if (request)
{
cout << "flag : request\n";
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 (requestType == "GET")
{
cout << "\t\trequestType : GET\n";
- try
- {
- if (response)
- {
- OCRepresentation rep = myResource.get();
- cout << rep.getJSONRepresentation() << endl;
- response->setErrorCode(200);
- response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
- }
- else
- {
- cout << "response is null\n";
- }
- }
- catch (exception &e)
+
+ pResponse->setErrorCode(200);
+ pResponse->setResponseResult(OC_EH_OK);
+ pResponse->setResourceRepresentation(myResource.get());
+ if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
{
- cout << e.what() << endl;
+ ehResult = OC_EH_OK;
}
}
+
else if (requestType == "PUT")
{
cout << "\t\t\trequestType : PUT\n";
OCRepresentation rep = request->getResourceRepresentation();
myResource.put(rep);
- if (response)
+ if (pResponse)
{
- response->setErrorCode(200);
- response->setResourceRepresentation(myResource.get());
+ pResponse->setErrorCode(200);
+ pResponse->setResourceRepresentation(myResource.get());
+ }
+ if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
+ {
+ ehResult = OC_EH_OK;
+ }
+ else
+ {
+ cout << "put request Error\n";
}
}
+
else if (requestType == "POST")
{
+ cout << "\t\t\trequestType : POST\n";
}
+
else if (requestType == "DELETE")
{
+ cout << "\t\trequestType : DELETE\n";
+ cout << "DeviceResource Delete Request" << std::endl;
+
+ if (myResource.deleteDeviceResource() == OC_STACK_OK)
+ {
+ cout << "\tSuccess DELETE\n";
+ pResponse->setErrorCode(200);
+ pResponse->setResponseResult(OC_EH_RESOURCE_DELETED);
+ ehResult = OC_EH_OK;
+ }
+ else
+ {
+ pResponse->setResponseResult(OC_EH_ERROR);
+ ehResult = OC_EH_ERROR;
+ }
+
+ OCPlatform::sendResponse(pResponse);
}
}
- else if (requestFlag & RequestHandlerFlag::ObserverFlag)
+ if (requestFlag & RequestHandlerFlag::ObserverFlag)
{
pthread_t threadId;
+ ObservationInfo observationInfo = request->getObservationInfo();
+ if (ObserveAction::ObserveRegister == observationInfo.action)
+ {
+ myResource.m_interestedObservers.push_back(observationInfo.obsId);
+ }
+ else if (ObserveAction::ObserveUnregister == observationInfo.action)
+ {
+ myResource.m_interestedObservers.erase(std::remove(
+ myResource.m_interestedObservers.begin(),
+ myResource.m_interestedObservers.end(),
+ observationInfo.obsId),
+ myResource.m_interestedObservers.end());
+ }
+
cout << request->getResourceUri() << endl;
cout << request->getResourceRepresentation().getUri() << endl;
pthread_create(&threadId , NULL , ChangeLightRepresentation , (void *) NULL);
startedThread = 1;
}
+ ehResult = OC_EH_OK;
}
}
else
std::cout << "Request invalid" << std::endl;
}
- return OC_EH_OK;
+ return ehResult;
}
-int main()
+void quitProcess()
{
+ unregisterResource(myResource.m_resourceHandle);
+ stopPresence();
+ exit(0);
+}
+void handleSigInt(int signum)
+{
+ if (signum == SIGINT)
+ {
+ std::cout << " handleSigInt in" << std::endl;
+ quitProcess();
+ }
+}
+
+int main()
+{
PlatformConfig cfg
{
OC::ServiceType::InProc,
try
{
+ startPresence(30);
myResource.createResource();
+ signal(SIGINT, handleSigInt);
while (true)
{
bool end = false;
cout << "2. Temp is down" << endl;
cout << "3. This Program will be ended." << endl;
cout << "========================================================" << endl;
+
cin >> number;
switch (number)
case 3:
{
cout << "Bye!" << endl;
+ stopPresence();
end = true;
+ quitProcess();
break;
}
default:
{
- cout << "You type wrong number. Try again!" << endl;
+ cout << "Invalid input. Please try again." << endl;
break;
}
}
{
cout << "main exception : " << e.what() << endl;
}
+
+
}