[IoTivity Simulator] Handling resource interfaces.
[platform/upstream/iotivity.git] / service / simulator / src / server / simulator_single_resource_impl.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include "simulator_single_resource_impl.h"
22 #include "simulator_utils.h"
23 #include "simulator_logger.h"
24 #include "logger.h"
25
26 #define TAG "SIM_SINGLE_RESOURCE"
27
28 SimulatorSingleResourceImpl::SimulatorSingleResourceImpl()
29     :   m_type(SimulatorResource::Type::SINGLE_RESOURCE),
30         m_resourceHandle(NULL),
31         m_interfaces {OC::DEFAULT_INTERFACE}
32 {
33     m_requestTypes[OC::DEFAULT_INTERFACE] = {"GET", "PUT", "POST"};
34     m_requestTypes[OC::READ_INTERFACE] = {"GET"};
35     m_requestTypes[OC::READWRITE_INTERFACE] = {"GET", "PUT", "POST"};
36     m_requestTypes[OC::ACTUATOR_INTERFACE] = {"GET", "PUT", "POST"};
37     m_requestTypes[OC::SENSOR_INTERFACE] = {"GET"};
38     m_property = static_cast<OCResourceProperty>(OC_DISCOVERABLE | OC_OBSERVABLE);
39 }
40
41 std::string SimulatorSingleResourceImpl::getName() const
42 {
43     return m_name;
44 }
45
46 SimulatorResource::Type SimulatorSingleResourceImpl::getType() const
47 {
48     return m_type;
49 }
50
51 std::string SimulatorSingleResourceImpl::getURI() const
52 {
53     return m_uri;
54 }
55
56 std::string SimulatorSingleResourceImpl::getResourceType() const
57 {
58     return m_resourceType;
59 }
60
61 std::vector<std::string> SimulatorSingleResourceImpl::getInterface() const
62 {
63     return m_interfaces;
64 }
65
66 void SimulatorSingleResourceImpl::setInterface(const std::vector<std::string> &interfaces)
67 {
68     // Replace m_interfaces if the list is not empty
69     if (!interfaces.empty())
70         m_interfaces = interfaces;
71 }
72
73 void SimulatorSingleResourceImpl::setName(const std::string &name)
74 {
75     VALIDATE_INPUT(name.empty(), "Name is empty!")
76
77     m_name = name;
78 }
79
80 void SimulatorSingleResourceImpl::setURI(const std::string &uri)
81 {
82     VALIDATE_INPUT(uri.empty(), "Uri is empty!")
83
84     std::lock_guard<std::recursive_mutex> lock(m_objectLock);
85     if (m_resourceHandle)
86     {
87         throw SimulatorException(SIMULATOR_OPERATION_NOT_ALLOWED,
88                                  "URI can not be set when resource is started!");
89     }
90
91     m_uri = uri;
92 }
93
94 void SimulatorSingleResourceImpl::setResourceType(const std::string &resourceType)
95 {
96     VALIDATE_INPUT(resourceType.empty(), "Resource type is empty!")
97
98     std::lock_guard<std::recursive_mutex> lock(m_objectLock);
99     if (m_resourceHandle)
100     {
101         throw SimulatorException(SIMULATOR_OPERATION_NOT_ALLOWED,
102                                  "Resource type cannot be set when resource is started!");
103     }
104
105     m_resourceType = resourceType;
106 }
107
108 void SimulatorSingleResourceImpl::addInterface(const std::string &interfaceType)
109 {
110     VALIDATE_INPUT(interfaceType.empty(), "Interface type is empty!")
111
112     if (interfaceType == OC::READ_INTERFACE
113         || interfaceType == OC::READWRITE_INTERFACE
114         || interfaceType == OC::ACTUATOR_INTERFACE
115         || interfaceType == OC::SENSOR_INTERFACE
116         || interfaceType == OC::DEFAULT_INTERFACE)
117     {
118         if (m_interfaces.end() != std::find(m_interfaces.begin(), m_interfaces.end(), interfaceType))
119         {
120             SIM_LOG(ILogger::ERROR, "Interface " << interfaceType << " is already supported!");
121             return;
122         }
123
124         std::lock_guard<std::recursive_mutex> lock(m_objectLock);
125         typedef OCStackResult (*bindInterfaceToResource)(const OCResourceHandle &,
126                     const std::string &);
127
128         try
129         {
130             invokeocplatform(static_cast<bindInterfaceToResource>(
131                              OC::OCPlatform::bindInterfaceToResource), m_resourceHandle,
132                              interfaceType);
133         }
134         catch (SimulatorException &e)
135         {
136             throw;
137         }
138     }
139     else
140     {
141         throw NoSupportException("Interface is not supported!");
142     }
143
144     m_interfaces.push_back(interfaceType);
145 }
146
147 void SimulatorSingleResourceImpl::removeInterface(const std::string &interfaceType)
148 {
149     if (m_interfaces.end() == std::find(m_interfaces.begin(), m_interfaces.end(), interfaceType))
150     {
151         SIM_LOG(ILogger::ERROR, "Interface " << interfaceType << " is not being supported currently!");
152         return;
153     }
154
155     m_interfaces.erase(std::remove(m_interfaces.begin(), m_interfaces.end(), interfaceType), m_interfaces.end());
156 }
157
158 void SimulatorSingleResourceImpl::addInterface(const std::vector<std::string> &interfaceList)
159 {
160     VALIDATE_INPUT(!interfaceList.size(), "Interface list is empty!")
161
162     for (auto interfaceType : interfaceList)
163     {
164         addInterface(interfaceType);
165     }
166 }
167
168 void SimulatorSingleResourceImpl::removeInterface(const std::vector<std::string> &interfaceList)
169 {
170     VALIDATE_INPUT(!interfaceList.size(), "Interface list is empty!")
171
172     for (auto interfaceType : interfaceList)
173     {
174         removeInterface(interfaceType);
175     }
176 }
177
178 void SimulatorSingleResourceImpl::setObservable(bool state)
179 {
180     std::lock_guard<std::recursive_mutex> lock(m_objectLock);
181     if (m_resourceHandle)
182     {
183         throw SimulatorException(SIMULATOR_OPERATION_NOT_ALLOWED,
184                                  "Observation state can not be changed when resource is started!");
185     }
186
187     if (true == state)
188         m_property = static_cast<OCResourceProperty>(m_property | OC_OBSERVABLE);
189     else
190         m_property = static_cast<OCResourceProperty>(m_property ^ OC_OBSERVABLE);
191 }
192
193 void SimulatorSingleResourceImpl::setObserverCallback(ObserverCallback callback)
194 {
195     VALIDATE_CALLBACK(callback)
196     m_observeCallback = callback;
197 }
198
199 bool SimulatorSingleResourceImpl::isObservable()
200 {
201     return (m_property & OC_OBSERVABLE);
202 }
203
204 bool SimulatorSingleResourceImpl::isStarted()
205 {
206     return (nullptr != m_resourceHandle);
207 }
208
209 void SimulatorSingleResourceImpl::start()
210 {
211     std::lock_guard<std::recursive_mutex> lock(m_objectLock);
212     if (m_resourceHandle)
213     {
214         SIM_LOG(ILogger::INFO, "[" << m_name << "] " << "Resource already registered!")
215     }
216
217     if (m_uri.empty() || m_resourceType.empty())
218     {
219         throw SimulatorException(SIMULATOR_ERROR, "Found incomplete data to start resource!");
220     }
221
222     typedef OCStackResult (*RegisterResource)(OCResourceHandle &, std::string &, const std::string &,
223             const std::string &, OC::EntityHandler, uint8_t);
224
225     invokeocplatform(static_cast<RegisterResource>(OC::OCPlatform::registerResource),
226                      m_resourceHandle, m_uri, m_resourceType, m_interfaces[0],
227                      std::bind(&SimulatorSingleResourceImpl::handleRequests,
228                                this, std::placeholders::_1), m_property);
229
230     for (size_t index = 1; m_interfaces.size() > 1 && index < m_interfaces.size(); index++)
231     {
232         typedef OCStackResult (*bindInterfaceToResource)(const OCResourceHandle &,
233                 const std::string &);
234
235         try
236         {
237             invokeocplatform(static_cast<bindInterfaceToResource>(
238                                  OC::OCPlatform::bindInterfaceToResource), m_resourceHandle,
239                              m_interfaces[index]);
240         }
241         catch (SimulatorException &e)
242         {
243             stop();
244             throw;
245         }
246     }
247 }
248
249 void SimulatorSingleResourceImpl::stop()
250 {
251     std::lock_guard<std::recursive_mutex> lock(m_objectLock);
252     if (!m_resourceHandle)
253         return;
254
255     // Stop all the update automation of this resource
256     m_updateAutomationMgr.stopAll();
257
258     // Clear all the observers
259     removeAllObservers();
260
261     // Unregister the resource from stack
262     typedef OCStackResult (*UnregisterResource)(const OCResourceHandle &);
263
264     invokeocplatform(static_cast<UnregisterResource>(OC::OCPlatform::unregisterResource),
265                      m_resourceHandle);
266
267     m_resourceHandle = nullptr;
268 }
269
270 std::vector<ObserverInfo> SimulatorSingleResourceImpl::getObserversList()
271 {
272     return m_observersList;
273 }
274
275 void SimulatorSingleResourceImpl::notify(int id, SimulatorResourceModel &resModel)
276 {
277     std::lock_guard<std::recursive_mutex> lock(m_objectLock);
278     if (!m_resourceHandle)
279         return;
280
281     std::shared_ptr<OC::OCResourceResponse> resourceResponse =
282     {std::make_shared<OC::OCResourceResponse>()};
283
284     resourceResponse->setErrorCode(200);
285     resourceResponse->setResponseResult(OC_EH_OK);
286     resourceResponse->setResourceRepresentation(resModel.getOCRepresentation(),
287             OC::DEFAULT_INTERFACE);
288
289     OC::ObservationIds observers;
290     observers.push_back(id);
291
292     typedef OCStackResult (*NotifyListOfObservers)(OCResourceHandle, OC::ObservationIds &,
293             const std::shared_ptr<OC::OCResourceResponse>);
294
295     invokeocplatform(static_cast<NotifyListOfObservers>(OC::OCPlatform::notifyListOfObservers),
296                      m_resourceHandle, observers, resourceResponse);
297 }
298
299 void SimulatorSingleResourceImpl::notifyAll(SimulatorResourceModel &resModel)
300 {
301     std::lock_guard<std::recursive_mutex> lock(m_objectLock);
302     if (!m_resourceHandle)
303         return;
304
305     if (!m_observersList.size())
306         return;
307
308     std::shared_ptr<OC::OCResourceResponse> resourceResponse =
309     {std::make_shared<OC::OCResourceResponse>()};
310
311     resourceResponse->setErrorCode(200);
312     resourceResponse->setResponseResult(OC_EH_OK);
313     resourceResponse->setResourceRepresentation(resModel.getOCRepresentation(),
314             OC::DEFAULT_INTERFACE);
315
316     OC::ObservationIds observers;
317     for (auto &observer : m_observersList)
318         observers.push_back(observer.id);
319
320     typedef OCStackResult (*NotifyListOfObservers)(OCResourceHandle, OC::ObservationIds &,
321             const std::shared_ptr<OC::OCResourceResponse>);
322
323     invokeocplatform(static_cast<NotifyListOfObservers>(OC::OCPlatform::notifyListOfObservers),
324                      m_resourceHandle, observers, resourceResponse);
325 }
326
327 void SimulatorSingleResourceImpl::notify(int id)
328 {
329     notify(id, m_resModel);
330 }
331
332 void SimulatorSingleResourceImpl::notifyAll()
333 {
334     notifyAll(m_resModel);
335 }
336
337 bool SimulatorSingleResourceImpl::getAttribute(const std::string &attrName,
338         SimulatorResourceModel::Attribute &attribute)
339 {
340     VALIDATE_INPUT(attrName.empty(), "Attribute name is empty!")
341
342     std::lock_guard<std::mutex> lock(m_modelLock);
343     return m_resModel.getAttribute(attrName, attribute);
344 }
345
346 void SimulatorSingleResourceImpl::addAttribute(const SimulatorResourceModel::Attribute &attribute,
347         bool notify)
348 {
349     std::lock_guard<std::mutex> lock(m_modelLock);
350     if (m_resModel.containsAttribute(attribute.getName()))
351         throw SimulatorException(SIMULATOR_ERROR, "Attribute exist with same name!");
352
353     if (!m_resModel.add(attribute))
354         throw SimulatorException(SIMULATOR_ERROR, "Failed to add attribute!");
355
356     if (notify && isStarted())
357         notifyAll();
358 }
359
360 bool SimulatorSingleResourceImpl::getAttributeProperty(const std::string &attrName,
361         SimulatorResourceModel::AttributeProperty &property)
362 {
363     VALIDATE_INPUT(attrName.empty(), "Attribute name is empty!")
364
365     std::lock_guard<std::mutex> lock(m_modelLock);
366     return m_resModel.getAttributeProperty(attrName, property);
367 }
368
369 bool SimulatorSingleResourceImpl::setAttributeProperty(const std::string &attrName,
370         const SimulatorResourceModel::AttributeProperty &property)
371 {
372     VALIDATE_INPUT(attrName.empty(), "Attribute name is empty!")
373
374     std::lock_guard<std::mutex> lock(m_modelLock);
375     return m_resModel.setAttributeProperty(attrName, property);
376 }
377
378 bool SimulatorSingleResourceImpl::updateAttributeValue(
379     const SimulatorResourceModel::Attribute &attribute,
380     bool notify)
381 {
382     std::lock_guard<std::mutex> lock(m_modelLock);
383     if (m_resModel.updateValue(attribute))
384     {
385         if (notify && isStarted())
386             notifyAll();
387         return true;
388     }
389
390     return false;
391 }
392
393 bool SimulatorSingleResourceImpl::removeAttribute(const std::string &attrName, bool notify)
394 {
395     VALIDATE_INPUT(attrName.empty(), "Attribute name is empty!")
396
397     std::lock_guard<std::mutex> lock(m_modelLock);
398     if (m_resModel.removeAttribute(attrName))
399     {
400         if (notify && isStarted())
401             notifyAll();
402         return true;
403     }
404
405     return false;
406 }
407
408 SimulatorResourceModel SimulatorSingleResourceImpl::getResourceModel()
409 {
410     std::lock_guard<std::mutex> lock(m_modelLock);
411     return m_resModel;
412 }
413
414 void SimulatorSingleResourceImpl::setModelChangeCallback(ResourceModelChangedCallback callback)
415 {
416     VALIDATE_CALLBACK(callback)
417     m_modelCallback = callback;
418 }
419
420 int SimulatorSingleResourceImpl::startResourceUpdation(AutomationType type,
421         int updateInterval, updateCompleteCallback callback)
422 {
423     VALIDATE_CALLBACK(callback)
424
425     std::lock_guard<std::recursive_mutex> lock(m_objectLock);
426     if (!m_resourceHandle)
427         throw SimulatorException(SIMULATOR_NO_RESOURCE, "Resource is not registered!");
428
429     return m_updateAutomationMgr.startResourceAutomation(this, type, updateInterval, callback);
430 }
431
432 int SimulatorSingleResourceImpl::startAttributeUpdation(const std::string &attrName,
433         AutomationType type, int updateInterval, updateCompleteCallback callback)
434 {
435     VALIDATE_CALLBACK(callback)
436
437     std::lock_guard<std::recursive_mutex> lock(m_objectLock);
438     if (!m_resourceHandle)
439         throw SimulatorException(SIMULATOR_NO_RESOURCE, "Resource is not registered!");
440
441     return m_updateAutomationMgr.startAttributeAutomation(this, attrName, type,
442             updateInterval, callback);
443 }
444
445 std::vector<int> SimulatorSingleResourceImpl::getResourceUpdationIds()
446 {
447     return m_updateAutomationMgr.getResourceAutomationIds();
448 }
449
450 std::vector<int> SimulatorSingleResourceImpl::getAttributeUpdationIds()
451 {
452     return m_updateAutomationMgr.getAttributeAutomationIds();
453 }
454
455 void SimulatorSingleResourceImpl::stopUpdation(const int id)
456 {
457     m_updateAutomationMgr.stop(id);
458 }
459
460 void SimulatorSingleResourceImpl::setResourceModel(const SimulatorResourceModel &resModel)
461 {
462     std::lock_guard<std::mutex> lock(m_modelLock);
463     m_resModel = resModel;
464 }
465
466 void SimulatorSingleResourceImpl::setPutErrorResponseModel(const SimulatorResourceModel &resModel)
467 {
468     m_putErrorResModel = resModel;
469 }
470
471 void SimulatorSingleResourceImpl::setPostErrorResponseModel(const SimulatorResourceModel &resModel)
472 {
473     m_postErrorResModel = resModel;
474 }
475
476 void SimulatorSingleResourceImpl::setActionType(std::map<RAML::ActionType, RAML::ActionPtr> &actionType)
477 {
478     m_actionTypes = actionType;
479 }
480
481 void SimulatorSingleResourceImpl::notifyApp(SimulatorResourceModel &resModel)
482 {
483     if (m_modelCallback)
484     {
485         m_modelCallback(m_uri, resModel);
486     }
487 }
488
489 void SimulatorSingleResourceImpl::notifyApp()
490 {
491     notifyApp(m_resModel);
492 }
493
494 bool SimulatorSingleResourceImpl::updateResourceModel(OC::OCRepresentation &ocRep,
495         SimulatorResourceModel &resModel)
496 {
497     std::lock_guard<std::mutex> lock(m_modelLock);
498     if (m_resModel.update(ocRep))
499     {
500         resModel = m_resModel;
501         return true;
502     }
503     return false;
504 }
505
506 OCEntityHandlerResult SimulatorSingleResourceImpl::handleRequests(
507     std::shared_ptr<OC::OCResourceRequest> request)
508 {
509     OCEntityHandlerResult errCode = OC_EH_ERROR;
510     if (!request)
511         return OC_EH_ERROR;
512
513     if (OC::RequestHandlerFlag::RequestFlag & request->getRequestHandlerFlag())
514     {
515         {
516             OC::OCRepresentation rep = request->getResourceRepresentation();
517             std::string payload = getPayloadString(rep);
518             SIM_LOG(ILogger::INFO, "[" << m_name << "] " << request->getRequestType()
519                     << " request received. \n**Payload details**\n" << payload)
520         }
521
522         std::string interfaceType(OC::DEFAULT_INTERFACE);
523         OC::QueryParamsMap queryParams = request->getQueryParameters();
524         if (queryParams.end() != queryParams.find("if"))
525         {
526             interfaceType = queryParams["if"];
527         }
528
529         std::shared_ptr<OC::OCResourceResponse> response;
530
531         // Check if the interface type is supported by the resource
532         if (m_interfaces.end() == std::find(m_interfaces.begin(), m_interfaces.end(), interfaceType))
533         {
534             return sendErrorResponse(request, 403, OC_EH_FORBIDDEN);
535         }
536
537         //Check if requestType is supported by interface.
538         std::map<std::string, std::vector<std::string>>::iterator it = m_requestTypes.find(interfaceType);
539         if (it != m_requestTypes.end())
540         {
541             if (it->second.end() == std::find(it->second.begin(), it->second.end(), request->getRequestType()))
542             {
543                 return sendErrorResponse(request, 405, OC_EH_ERROR);
544             }
545         }
546
547         if ("GET" == request->getRequestType())
548         {
549             response = handleReadRequest(request, interfaceType);
550         }
551         else if ("PUT" == request->getRequestType()
552                  || "POST" == request->getRequestType())
553         {
554             response = handleWriteRequest(request, interfaceType);
555         }
556         else if ("DELETE" == request->getRequestType())
557         {
558             SIM_LOG(ILogger::ERROR, "[" << m_name << "] " << "Unsupported request received!")
559             return OC_EH_ERROR;
560         }
561
562         // Send response if the request handled by resource
563         if (response)
564         {
565             response->setRequestHandle(request->getRequestHandle());
566             response->setResourceHandle(request->getResourceHandle());
567             if (OC_STACK_OK != OC::OCPlatform::sendResponse(response))
568                 return OC_EH_ERROR;
569         }
570     }
571
572     if (OC::RequestHandlerFlag::ObserverFlag & request->getRequestHandlerFlag())
573     {
574         if (false == isObservable())
575         {
576             SIM_LOG(ILogger::INFO, "[" << m_uri << "] OBSERVE request received")
577             SIM_LOG(ILogger::INFO, "[" << m_uri << "] Sending error as resource is in unobservable state")
578             return OC_EH_ERROR;
579         }
580
581         OC::ObservationInfo observationInfo = request->getObservationInfo();
582         if (OC::ObserveAction::ObserveRegister == observationInfo.action)
583         {
584             SIM_LOG(ILogger::INFO, "[" << m_uri << "] OBSERVE REGISTER request received");
585             addObserver(observationInfo);
586         }
587         else if (OC::ObserveAction::ObserveUnregister == observationInfo.action)
588         {
589             SIM_LOG(ILogger::INFO, "[" << m_uri << "] OBSERVE UNREGISTER request received");
590             removeObserver(observationInfo);
591         }
592     }
593
594     return OC_EH_OK;
595 }
596
597 void SimulatorSingleResourceImpl::addObserver(OC::ObservationInfo ocObserverInfo)
598 {
599     ObserverInfo info {ocObserverInfo.obsId, ocObserverInfo.address, ocObserverInfo.port};
600     m_observersList.push_back(info);
601
602     if (m_observeCallback)
603         m_observeCallback(m_uri, ObservationStatus::REGISTER, info);
604 }
605
606 void SimulatorSingleResourceImpl::removeObserver(OC::ObservationInfo ocObserverInfo)
607 {
608     ObserverInfo info;
609     for (auto iter = m_observersList.begin(); iter != m_observersList.end(); iter++)
610     {
611         if ((info = *iter), info.id == ocObserverInfo.obsId)
612         {
613             m_observersList.erase(iter);
614             break;
615         }
616     }
617
618     if (m_observeCallback)
619         m_observeCallback(m_uri, ObservationStatus::UNREGISTER, info);
620 }
621
622 void SimulatorSingleResourceImpl::removeAllObservers()
623 {
624     std::vector<ObserverInfo> observerList = m_observersList;
625     m_observersList.clear();
626     for (int index = 0; index < observerList.size(); index++)
627     {
628         if (m_observeCallback)
629             m_observeCallback(m_uri, ObservationStatus::UNREGISTER, observerList[index]);
630     }
631 }
632
633 RAML::ActionType SimulatorSingleResourceImpl::getActionType(std::string requestType)
634 {
635     if (!requestType.compare("GET"))
636         return RAML::ActionType::GET;
637
638     if (!requestType.compare("PUT"))
639         return RAML::ActionType::PUT;
640
641     if (!requestType.compare("POST"))
642         return RAML::ActionType::POST;
643
644     if (!requestType.compare("DELETE"))
645         return RAML::ActionType::DELETE;
646
647     return RAML::ActionType::NONE;
648 }
649
650 OCEntityHandlerResult SimulatorSingleResourceImpl::sendErrorResponse
651     (std::shared_ptr<OC::OCResourceRequest> request, const int errCode,
652      OCEntityHandlerResult responseCode)
653 {
654     std::shared_ptr<OC::OCResourceResponse> response;
655
656     response = std::make_shared<OC::OCResourceResponse>();
657     response->setErrorCode(errCode);
658     response->setResponseResult(responseCode);
659
660     // Setting error response body as representation in response if available in RAML file
661     if ("PUT" == request->getRequestType())
662     {
663         if (!m_putErrorResModel.getOCRepresentation().empty())
664             response->setResourceRepresentation(m_putErrorResModel.getOCRepresentation());
665     }
666
667     if ("POST" == request->getRequestType())
668     {
669         if (!m_postErrorResModel.getOCRepresentation().empty())
670             response->setResourceRepresentation(m_postErrorResModel.getOCRepresentation());
671     }
672
673     response->setRequestHandle(request->getRequestHandle());
674     response->setResourceHandle(request->getResourceHandle());
675     if (OC_STACK_OK != OC::OCPlatform::sendResponse(response))
676         return OC_EH_ERROR;
677
678     return OC_EH_OK;
679 }
680
681 std::shared_ptr<OC::OCResourceResponse> SimulatorSingleResourceImpl::handleReadRequest
682     (std::shared_ptr<OC::OCResourceRequest> request, const std::string &interfaceType)
683 {
684     std::shared_ptr<OC::OCResourceResponse> response;
685     OC::OCRepresentation ocRep;
686
687     response = std::make_shared<OC::OCResourceResponse>();
688     response->setErrorCode(200);
689     response->setResponseResult(OC_EH_OK);
690
691     if (OC::ACTUATOR_INTERFACE == interfaceType ||
692         OC::SENSOR_INTERFACE == interfaceType)
693     {
694         // Only send the representation without the common properties.
695         ocRep = m_resModel.getOCRepresentation();
696         response->setResourceRepresentation(ocRep, interfaceType);
697     }
698     else
699     {
700         // Send the representation along with common properties for the resource.
701         SimulatorResourceModel resModel = m_resModel;
702         setCommonProperties(resModel);
703         ocRep = resModel.getOCRepresentation();
704         response->setResourceRepresentation(ocRep, interfaceType);
705     }
706
707     std::string resPayload = getPayloadString(ocRep);
708     SIM_LOG(ILogger::INFO, "[" << m_uri <<
709             "] Sending response for GET request. \n**Payload details**" << resPayload)
710
711     return response;
712 }
713
714 std::shared_ptr<OC::OCResourceResponse> SimulatorSingleResourceImpl::handleWriteRequest
715     (std::shared_ptr<OC::OCResourceRequest> request, const std::string &interfaceType)
716 {
717     std::shared_ptr<OC::OCResourceResponse> response;
718     OC::OCRepresentation requestRep = request->getResourceRepresentation();
719     SimulatorResourceModel resModel;
720     if (true == updateResourceModel(requestRep, resModel))
721     {
722         notifyAll(resModel);
723         notifyApp(resModel);
724
725         response = std::make_shared<OC::OCResourceResponse>();
726         response->setErrorCode(200);
727         response->setResponseResult(OC_EH_OK);
728         response->setResourceRepresentation(resModel.getOCRepresentation(), interfaceType);
729         std::string resPayload = getPayloadString(resModel.getOCRepresentation());
730         SIM_LOG(ILogger::INFO, "[" << m_uri <<
731              "] Sending response for " << request->getRequestType() << " request. \n**Payload details**" <<
732              resPayload)
733     }
734     else
735     {
736         sendErrorResponse(request, 400, OC_EH_ERROR);
737     }
738
739     return response;
740 }
741
742 void SimulatorSingleResourceImpl::setCommonProperties(SimulatorResourceModel &resModel)
743 {
744     resModel.add("rt", m_resourceType);
745     resModel.add("if", m_interfaces);
746     resModel.add("p", m_property);
747     resModel.add("n", m_name);
748 }