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_
26 #include "OCPlatform.h"
28 #include "OCResourceResponse.h"
32 enum class requestType
40 class NSProviderSimulator
43 OCResourceHandle m_notificationHandle;
44 OCResourceHandle m_messageHandle;
45 OCResourceHandle m_syncHandle;
46 OC::OCRepresentation m_syncRep;
47 OC::OCRepresentation m_messageRep;
50 std::string m_notificationUri;
51 std::string m_messageUri;
52 std::string m_syncUri;
54 OC::ObservationIds m_syncObservers;
58 : m_notificationHandle(), m_messageHandle(), m_syncHandle(),
59 m_syncRep(), m_messageRep(), m_accepter(0),
60 m_notificationUri(std::string("/notification")),
61 m_messageUri(std::string("/message")),
62 m_syncUri(std::string("/sync")),
68 ~NSProviderSimulator() = default;
70 NSProviderSimulator(const NSProviderSimulator &) = delete;
71 NSProviderSimulator & operator = (const NSProviderSimulator &) = delete;
73 NSProviderSimulator(NSProviderSimulator &&) = delete;
74 NSProviderSimulator & operator = (NSProviderSimulator &&) = delete;
77 std::shared_ptr<OC::OCResourceResponse> getResponse(
78 std::shared_ptr< OC::OCResourceRequest > requests, requestType type)
80 auto response = std::make_shared<OC::OCResourceResponse>();
81 response->setRequestHandle(requests->getRequestHandle());
82 response->setResourceHandle(requests->getResourceHandle());
84 int requestFlag = requests->getRequestHandlerFlag();
85 if (requestFlag == OC::RequestHandlerFlag::RequestFlag)
87 std::string request = requests->getRequestType();
89 response->setErrorCode(200);
90 response->setResponseResult(OC_EH_OK);
94 OC::OCRepresentation rep;
96 if (type == requestType::NS_NOTIFICATION)
98 std::string msgUri = m_notificationUri + m_messageUri;
99 std::string syncUri = m_notificationUri + m_syncUri;
100 std::string providerId = "123456789012345678901234567890123456";
101 rep.setValue("ACCEPTER", m_accepter);
102 rep.setValue("MESSAGE_URI", msgUri);
103 rep.setValue("SYNC_URI", syncUri);
104 rep.setValue("PROVIDER_ID", providerId);
106 else if (type == requestType::NS_SYNC)
110 else if (type == requestType::NS_MESSAGE)
119 response->setResourceRepresentation(rep);
123 else if (request == "POST" && type == requestType::NS_SYNC)
125 m_syncRep = requests->getResourceRepresentation();
127 std::cout << "Receive POST at Sync" << std::endl;
128 std::cout << "Sync Id : " << m_syncRep.getValueToString("ID") << std::endl;
129 std::cout << "Sync State : " << m_syncRep.getValueToString("STATE") << std::endl;
131 response->setResourceRepresentation(m_syncRep);
133 OC::OCPlatform::notifyListOfObservers(m_syncHandle, m_syncObservers, response);
142 void setObserver(std::shared_ptr< OC::OCResourceRequest > requests, requestType type)
144 if (type == requestType::NS_SYNC)
146 OC::ObservationInfo observationInfo = requests->getObservationInfo();
147 if (OC::ObserveAction::ObserveRegister == observationInfo.action)
149 m_syncObservers.push_back(observationInfo.obsId);
151 else if (OC::ObserveAction::ObserveUnregister == observationInfo.action)
153 m_syncObservers.erase(std::remove(
154 m_syncObservers.begin(), m_syncObservers.end(),
155 observationInfo.obsId), m_syncObservers.end());
158 else if (type == requestType::NS_MESSAGE)
160 OC::OCRepresentation rep;
161 std::string providerId = "123456789012345678901234567890123456";
162 rep.setValue<int>("MESSAGE_ID", (int)1);
163 rep.setValue("PROVIDER_ID", providerId);
165 auto response = std::make_shared<OC::OCResourceResponse>();
166 response->setRequestHandle(requests->getRequestHandle());
167 response->setResourceHandle(requests->getResourceHandle());
168 response->setErrorCode(200);
169 response->setResponseResult(OC_EH_OK);
170 response->setResourceRepresentation(rep);
172 OC::ObservationIds ids;
173 ids.push_back(requests->getObservationInfo().obsId);
175 OC::OCPlatform::notifyListOfObservers(m_messageHandle, ids, response);
179 OCEntityHandlerResult entityHandler(
180 std::shared_ptr< OC::OCResourceRequest > request, requestType type)
187 std::cout << "Provider : Income request : " << request->getRequestHandlerFlag() << std::endl;
188 if ((request->getRequestHandlerFlag() & OC::RequestHandlerFlag::ObserverFlag))
190 std::cout << "Provider : Income Observe : " << std::endl;
191 setObserver(request, type);
195 auto pResponse = getResponse(request, type);
196 if (pResponse == nullptr)
203 OC::OCPlatform::sendResponse(pResponse);
205 catch (std::exception & e)
215 void setAccepter(int accepter)
217 m_accepter = accepter;
222 std::cout << "Provider : notify~" << std::endl;
223 OC::OCPlatform::notifyAllObservers(m_messageHandle);
226 void notifyMessage(const uint64_t & id, const std::string & title, const std::string & content)
228 setMessage(id, title, content);
232 void sendRead(const uint64_t & id)
234 std::string providerId = "123456789012345678901234567890123456";
235 m_syncRep.setValue<int>("MESSAGE_ID", id);
236 m_syncRep.setValue("STATE", (int)1);
237 m_syncRep.setValue("PROVIDER_ID", providerId);
238 OC::OCPlatform::notifyAllObservers(m_syncHandle);
240 void sendDismiss(const uint64_t & id)
242 std::string providerId = "123456789012345678901234567890123456";
243 m_syncRep.setValue<int>("MESSAGE_ID", id);
244 m_syncRep.setValue("STATE", (int)2);
245 m_syncRep.setValue("PROVIDER_ID", providerId);
246 OC::OCPlatform::notifyAllObservers(m_syncHandle);
249 void setMessage(const uint64_t & id, const std::string & title, const std::string & content)
251 std::string providerId = "123456789012345678901234567890123456";
252 m_messageRep.setValue<int>("MESSAGE_ID", id);
253 m_messageRep.setValue("TITLE", title);
254 m_messageRep.setValue("CONTENTTEXT", content);
255 m_messageRep.setValue("PROVIDER_ID", providerId);
258 void deleteNotificationResource()
260 OC::OCPlatform::unregisterResource(m_notificationHandle);
261 OC::OCPlatform::unregisterResource(m_messageHandle);
262 OC::OCPlatform::unregisterResource(m_syncHandle);
265 void createNotificationResource()
267 createNotificationResource(m_notificationUri);
270 void createNotificationResource(const std::string & uri)
272 if (m_notificationUri != uri)
274 m_notificationUri = uri;
277 OC::OCPlatform::startPresence(30);
279 std::string notificationUri = m_notificationUri;
280 std::string resourceTypeName = "oic.r.message.notification";
281 std::string resourceInterface = OC::DEFAULT_INTERFACE;
283 uint8_t resourceProperty = OC_OBSERVABLE;
284 std::string childUri = uri + m_messageUri;
287 OC::OCPlatform::registerResource(
288 m_messageHandle, childUri,
289 resourceTypeName, resourceInterface,
290 std::bind(& NSProviderSimulator::entityHandler, this,
291 std::placeholders::_1, requestType::NS_MESSAGE),
294 catch (std::exception & e)
296 std::cout << e.what() << std::endl;
299 resourceTypeName = "oic.r.sync.notification";
300 childUri = uri + m_syncUri;
303 OC::OCPlatform::registerResource(
304 m_syncHandle, childUri,
305 resourceTypeName, resourceInterface,
306 std::bind(& NSProviderSimulator::entityHandler, this,
307 std::placeholders::_1, requestType::NS_SYNC),
310 catch (std::exception & e)
312 std::cout << e.what() << std::endl;
315 resourceProperty = OC_DISCOVERABLE;
316 resourceTypeName = "oic.r.notification";
319 OC::OCPlatform::registerResource(
320 m_notificationHandle, notificationUri,
321 resourceTypeName, resourceInterface,
322 std::bind(& NSProviderSimulator::entityHandler, this,
323 std::placeholders::_1, requestType::NS_NOTIFICATION),
326 catch (std::exception & e)
328 std::cout << e.what() << std::endl;
333 #endif /* _NS_PROVIDER_SERVICE_SIMULATOR_H_ */