1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #ifndef _NS_PROVIDER_SERVICE_SIMULATOR_H_
22 #define _NS_PROVIDER_SERVICE_SIMULATOR_H_
27 #include "OCPlatform.h"
29 #include "OCResourceResponse.h"
33 enum class requestType
40 enum class messageType
48 class NSProviderSimulator
51 enum class TopicAllowState
56 typedef std::pair<std::string, TopicAllowState> NS_TopicState;
57 typedef std::map<std::string, TopicAllowState> NS_TopicStateList;
59 typedef std::list<std::string> NS_TopicList;
61 OCResourceHandle m_notificationHandle;
62 OCResourceHandle m_messageHandle;
63 OCResourceHandle m_syncHandle;
64 OCResourceHandle m_topicHandle;
65 OC::OCRepresentation m_syncRep;
66 OC::OCRepresentation m_messageRep;
69 std::string m_notificationUri;
70 std::string m_messageUri;
71 std::string m_syncUri;
72 std::string m_topicUri;
73 NS_TopicList m_topicList;
74 NS_TopicStateList m_allowedTopicList;
76 OC::ObservationIds m_syncObservers;
80 : m_notificationHandle(), m_messageHandle(), m_syncHandle(), m_topicHandle(),
81 m_syncRep(), m_messageRep(), m_accepter(0),
82 m_notificationUri(std::string("/notification")),
83 m_messageUri(std::string("/message")),
84 m_syncUri(std::string("/sync")),
85 m_topicUri(std::string("/topic")),
93 ~NSProviderSimulator() = default;
95 NSProviderSimulator(const NSProviderSimulator &) = delete;
96 NSProviderSimulator & operator = (const NSProviderSimulator &) = delete;
98 NSProviderSimulator(NSProviderSimulator &&) = delete;
99 NSProviderSimulator & operator = (NSProviderSimulator &&) = delete;
102 std::shared_ptr<OC::OCResourceResponse> getResponse(
103 std::shared_ptr< OC::OCResourceRequest > requests, requestType type)
105 auto response = std::make_shared<OC::OCResourceResponse>();
106 response->setRequestHandle(requests->getRequestHandle());
107 response->setResourceHandle(requests->getResourceHandle());
109 int requestFlag = requests->getRequestHandlerFlag();
110 if (requestFlag == OC::RequestHandlerFlag::RequestFlag)
112 std::string request = requests->getRequestType();
114 response->setErrorCode(200);
115 response->setResponseResult(OC_EH_OK);
117 if (request == "GET")
119 OC::OCRepresentation rep;
121 if (type == requestType::NS_NOTIFICATION)
123 std::string msgUri = m_notificationUri + m_messageUri;
124 std::string syncUri = m_notificationUri + m_syncUri;
125 std::string topicUri = m_notificationUri + m_topicUri;
126 std::string providerId = "123456789012345678901234567890123456";
127 rep.setValue("subControllability", m_accepter);
128 rep.setValue("messageUri", msgUri);
129 rep.setValue("syncUri", syncUri);
130 rep.setValue("topicUri", topicUri);
131 rep.setValue("providerId", providerId);
133 else if (type == requestType::NS_SYNC)
137 else if (type == requestType::NS_MESSAGE)
141 else if (type == requestType::NS_TOPIC)
143 if (m_allowedTopicList.empty())
145 std::for_each (m_topicList.begin(), m_topicList.end(),
146 [this](const std::string & topic)
148 m_allowedTopicList.insert(
149 std::make_pair<std::string, TopicAllowState>(
150 std::string(topic), TopicAllowState::DENY));
155 std::vector<OC::OCRepresentation> topicArr;
157 std::for_each (m_allowedTopicList.begin(), m_allowedTopicList.end(),
158 [& topicArr](const NS_TopicState & topicState)
160 OC::OCRepresentation topic;
161 topic.setValue("topicName", topicState.first);
162 topic.setValue("topicState", (int) topicState.second);
163 topicArr.push_back(topic);
167 rep.setValue<std::vector<OC::OCRepresentation>>
168 ("topicList", topicArr);
175 response->setResourceRepresentation(rep);
179 else if (request == "POST")
181 if (type == requestType::NS_SYNC)
183 m_syncRep = requests->getResourceRepresentation();
185 std::cout << "Receive POST for Sync" << std::endl;
186 std::cout << "provider Id : " << m_syncRep.getValueToString("providerId") << std::endl;
187 std::cout << "Sync State : " << m_syncRep.getValueToString("state") << std::endl;
189 response->setResourceRepresentation(m_syncRep);
191 OC::OCPlatform::notifyAllObservers(m_syncHandle);
195 else if (type == requestType::NS_TOPIC)
197 auto receivePayload =
198 requests->getResourceRepresentation()
199 .getValue<std::vector<OC::OCRepresentation>>("topicList");
201 std::for_each (receivePayload.begin(), receivePayload.end(),
202 [this](const OC::OCRepresentation & rep)
204 auto tmp = m_allowedTopicList.find(rep.getValueToString("topicName"));
205 if (tmp != m_allowedTopicList.end())
207 tmp->second = (TopicAllowState) rep.getValue<int>("topicState");
218 void setObserver(std::shared_ptr< OC::OCResourceRequest > requests, requestType type)
220 if (type == requestType::NS_SYNC)
222 OC::ObservationInfo observationInfo = requests->getObservationInfo();
223 if (OC::ObserveAction::ObserveRegister == observationInfo.action)
225 m_syncObservers.push_back(observationInfo.obsId);
227 else if (OC::ObserveAction::ObserveUnregister == observationInfo.action)
229 m_syncObservers.erase(std::remove(
230 m_syncObservers.begin(), m_syncObservers.end(),
231 observationInfo.obsId), m_syncObservers.end());
234 else if (type == requestType::NS_MESSAGE)
236 OC::OCRepresentation rep;
237 std::string providerId = "123456789012345678901234567890123456";
238 rep.setValue<int>("messageId", (int)messageType::NS_ALLOW);
239 rep.setValue("providerId", providerId);
241 auto response = std::make_shared<OC::OCResourceResponse>();
242 response->setRequestHandle(requests->getRequestHandle());
243 response->setResourceHandle(requests->getResourceHandle());
244 response->setErrorCode(200);
245 response->setResponseResult(OC_EH_OK);
246 response->setResourceRepresentation(rep);
248 OC::ObservationIds ids;
249 ids.push_back(requests->getObservationInfo().obsId);
251 OC::OCPlatform::notifyListOfObservers(m_messageHandle, ids, response);
255 OCEntityHandlerResult entityHandler(
256 std::shared_ptr< OC::OCResourceRequest > request, requestType type)
263 std::cout << "Provider : Income request : " << request->getRequestHandlerFlag() << std::endl;
264 if ((request->getRequestHandlerFlag() & OC::RequestHandlerFlag::ObserverFlag))
266 std::cout << "Provider : Income Observe : " << std::endl;
267 setObserver(request, type);
271 auto pResponse = getResponse(request, type);
272 if (pResponse == nullptr)
279 OC::OCPlatform::sendResponse(pResponse);
281 catch (std::exception & e)
291 void setAccepter(int accepter)
293 m_accepter = accepter;
298 std::cout << "Provider : notify~" << std::endl;
299 OC::OCPlatform::notifyAllObservers(m_messageHandle);
302 void notifyMessage(const uint64_t & id, const std::string & title, const std::string & content)
304 setMessage(id, title, content);
308 void sendRead(const uint64_t & id)
310 std::string providerId = "123456789012345678901234567890123456";
311 m_syncRep.setValue<int>("messageId", id);
312 m_syncRep.setValue("state", (int)1);
313 m_syncRep.setValue("providerId", providerId);
314 OC::OCPlatform::notifyAllObservers(m_syncHandle);
316 void sendDismiss(const uint64_t & id)
318 std::string providerId = "123456789012345678901234567890123456";
319 m_syncRep.setValue<int>("messageId", id);
320 m_syncRep.setValue("state", (int)2);
321 m_syncRep.setValue("providerId", providerId);
322 OC::OCPlatform::notifyAllObservers(m_syncHandle);
325 void setMessage(const uint64_t & id, const std::string & title, const std::string & content)
327 std::string providerId = "123456789012345678901234567890123456";
328 m_messageRep.setValue<int>("messageId", id);
329 m_messageRep.setValue("title", title);
330 m_messageRep.setValue("contentText", content);
331 m_messageRep.setValue("providerId", providerId);
334 void setTopics(const NS_TopicList & topics)
336 bool isChanged = false;
337 std::for_each (topics.begin(), topics.end(),
338 [this, & isChanged](const std::string & topic)
340 auto found = std::find(
341 this->m_topicList.begin(), this->m_topicList.end(), topic);
342 if (found == this->m_topicList.end())
344 this->m_topicList.push_back(topic);
351 this->notifyMessage((uint64_t)messageType::NS_TOPIC, "", "");
355 NS_TopicList getTopics() const
360 void updateTopicState(const NS_TopicStateList & allowedTopics)
362 std::for_each (allowedTopics.begin(), allowedTopics.end(),
363 [this](const NS_TopicState & allowedTopic)
365 auto found = this->m_allowedTopicList.find(allowedTopic.first);
366 if (found != this->m_allowedTopicList.end())
368 found->second = allowedTopic.second;
373 NS_TopicStateList getTopicAllowState() const
375 return m_allowedTopicList;
378 void deleteNotificationResource()
380 OC::OCPlatform::unregisterResource(m_notificationHandle);
381 OC::OCPlatform::unregisterResource(m_messageHandle);
382 OC::OCPlatform::unregisterResource(m_syncHandle);
383 OC::OCPlatform::unregisterResource(m_topicHandle);
384 m_allowedTopicList.clear();
388 void createNotificationResource()
390 createNotificationResource(m_notificationUri);
393 void createNotificationResource(const std::string & uri)
395 if (m_notificationUri != uri)
397 m_notificationUri = uri;
400 OC::OCPlatform::startPresence(30);
402 std::string notificationUri = m_notificationUri;
403 std::string resourceTypeName = "oic.wk.notification.topic";
404 std::string resourceInterface = OC::DEFAULT_INTERFACE;
406 uint8_t resourceProperty = OC_OBSERVABLE;
407 std::string childUri = uri + m_topicUri;
410 OC::OCPlatform::registerResource(
411 m_topicHandle, childUri,
412 resourceTypeName, resourceInterface,
413 std::bind(& NSProviderSimulator::entityHandler, this,
414 std::placeholders::_1, requestType::NS_TOPIC),
417 catch(std::exception & e)
419 std::cout << e.what() << std::endl;
422 //resourceProperty |= OC_OBSERVABLE;
423 resourceTypeName = "oic.wk.notification.message";
424 childUri = uri + m_messageUri;
427 OC::OCPlatform::registerResource(
428 m_messageHandle, childUri,
429 resourceTypeName, resourceInterface,
430 std::bind(& NSProviderSimulator::entityHandler, this,
431 std::placeholders::_1, requestType::NS_MESSAGE),
434 catch (std::exception & e)
436 std::cout << e.what() << std::endl;
439 resourceTypeName = "oic.wk.notification.sync";
440 childUri = uri + m_syncUri;
443 OC::OCPlatform::registerResource(
444 m_syncHandle, childUri,
445 resourceTypeName, resourceInterface,
446 std::bind(& NSProviderSimulator::entityHandler, this,
447 std::placeholders::_1, requestType::NS_SYNC),
450 catch (std::exception & e)
452 std::cout << e.what() << std::endl;
455 resourceProperty |= OC_DISCOVERABLE;
456 resourceTypeName = "oic.wk.notification";
459 OC::OCPlatform::registerResource(
460 m_notificationHandle, notificationUri,
461 resourceTypeName, resourceInterface,
462 std::bind(& NSProviderSimulator::entityHandler, this,
463 std::placeholders::_1, requestType::NS_NOTIFICATION),
466 catch (std::exception & e)
468 std::cout << e.what() << std::endl;
473 #endif /* _NS_PROVIDER_SERVICE_SIMULATOR_H_ */