1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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 #include "simulator_resource_server.h"
22 #include "simulator_attribute_automation.h"
23 #include "simulator_logger.h"
25 SimulatorResourceServer::SimulatorResourceServer()
26 : m_resourceHandle(NULL),
27 m_property((OCResourceProperty) (OC_DISCOVERABLE | OC_OBSERVABLE)) {}
29 SimulatorResult SimulatorResourceServer::setURI(const std::string &uri)
32 return SIMULATOR_OPERATION_NOT_ALLOWED;
34 return SIMULATOR_SUCCESS;
37 std::string SimulatorResourceServer::getURI() const
42 SimulatorResult SimulatorResourceServer::setResourceType(const std::string &resourceType)
45 return SIMULATOR_OPERATION_NOT_ALLOWED;
46 m_resourceType = resourceType;
47 return SIMULATOR_SUCCESS;
50 std::string SimulatorResourceServer::getResourceType() const
52 return m_resourceType;
55 SimulatorResult SimulatorResourceServer::setInterfaceType(const std::string &interfaceType)
58 return SIMULATOR_OPERATION_NOT_ALLOWED;
59 m_interfaceType = interfaceType;
60 return SIMULATOR_SUCCESS;
63 std::string SimulatorResourceServer::getInterfaceType() const
65 return m_interfaceType;
68 void SimulatorResourceServer::setName(const std::string &name)
73 std::string SimulatorResourceServer::getName() const
78 SimulatorResult SimulatorResourceServer::setObservable(bool state)
81 return SIMULATOR_OPERATION_NOT_ALLOWED;
84 m_property = static_cast<OCResourceProperty>(m_property | OC_OBSERVABLE);
86 m_property = static_cast<OCResourceProperty>(m_property ^ OC_OBSERVABLE);
88 return SIMULATOR_SUCCESS;
91 bool SimulatorResourceServer::isObservable() const
93 return (m_property & OC_OBSERVABLE);
96 SimulatorResult SimulatorResourceServer::start()
99 return SIMULATOR_RESOURCE_ALREADY_REGISTERED;
101 if (m_uri.empty() || m_resourceType.empty() || m_interfaceType.empty()
104 return SIMULATOR_BAD_INPUT;
107 OCStackResult result = OC::OCPlatform::registerResource(m_resourceHandle,
111 std::bind(&SimulatorResourceServer::entityHandler,
112 this, std::placeholders::_1), m_property);
114 if (OC_STACK_OK != result)
115 return SIMULATOR_ERROR;
116 return SIMULATOR_SUCCESS;
119 SimulatorResult SimulatorResourceServer::stop()
121 if (nullptr == m_resourceHandle)
123 SIM_LOG(ILogger::ERROR, "Resourece is not registered!");
124 return SIMULATOR_RESOURCE_NOT_REGISTERED;
127 OCStackResult result = OC::OCPlatform::unregisterResource(m_resourceHandle);
128 if (OC_STACK_OK != result)
129 return SIMULATOR_ERROR;
131 m_resourceHandle = nullptr;
132 return SIMULATOR_SUCCESS;
135 void SimulatorResourceServer::setRange(const std::string &attrName, const int min, const int max)
137 m_resModel.setRange(attrName, min, max);
140 void SimulatorResourceServer::setUpdateInterval(const std::string &attrName, int interval)
142 m_resModel.setUpdateInterval(attrName, interval);
145 void SimulatorResourceServer::removeAttribute(const std::string &attrName)
147 m_resModel.removeAttribute(attrName);
148 notifyListOfObservers();
151 void SimulatorResourceServer::updateAttributeFromAllowedValues(const std::string &attrName,
152 const int allowedValueIndex)
154 m_resModel.updateAttributeFromAllowedValues(attrName, allowedValueIndex);
155 notifyListOfObservers();
158 SimulatorResourceModel SimulatorResourceServer::getModel() const
163 void SimulatorResourceServer::setModelChangeCallback(ResourceModelChangedCB callback)
165 m_callback = callback;
168 SimulatorResult SimulatorResourceServer::startUpdateAutomation(AutomationType type,
169 updateCompleteCallback callback, int &id)
171 return m_updateAutomationMgr.startResourceAutomation(this, id, callback, type);
174 SimulatorResult SimulatorResourceServer::startUpdateAutomation(const std::string &attrName,
175 AutomationType type, updateCompleteCallback callback, int &id)
177 return m_updateAutomationMgr.startAttributeAutomation(this, attrName, id, callback, type);
180 std::vector<int> SimulatorResourceServer::getResourceAutomationIds()
182 return m_updateAutomationMgr.getResourceAutomationIds();
185 std::vector<int> SimulatorResourceServer::getAttributeAutomationIds()
187 return m_updateAutomationMgr.getAttributeAutomationIds();
190 void SimulatorResourceServer::stopUpdateAutomation(const int id)
192 m_updateAutomationMgr.stop(id);
195 OC::OCRepresentation SimulatorResourceServer::getOCRepresentation()
197 return m_resModel.getOCRepresentation();
200 bool SimulatorResourceServer::modifyResourceModel(OC::OCRepresentation &ocRep)
202 bool status = m_resModel.update(ocRep);
205 notifyListOfObservers();
210 OCEntityHandlerResult SimulatorResourceServer::entityHandler(std::shared_ptr<OC::OCResourceRequest>
213 OCEntityHandlerResult errCode = OC_EH_ERROR;
219 if (OC::RequestHandlerFlag::RequestFlag & request->getRequestHandlerFlag())
221 auto response = std::make_shared<OC::OCResourceResponse>();
222 response->setRequestHandle(request->getRequestHandle());
223 response->setResourceHandle(request->getResourceHandle());
225 if ("GET" == request->getRequestType())
227 SIM_LOG(ILogger::INFO, "[" << m_uri << "] GET request received");
228 response->setErrorCode(200);
229 response->setResponseResult(OC_EH_OK);
230 response->setResourceRepresentation(getOCRepresentation());
232 if (OC_STACK_OK == OC::OCPlatform::sendResponse(response))
237 else if ("PUT" == request->getRequestType())
239 SIM_LOG(ILogger::INFO, "[" << m_uri << "] PUT request received");
240 OC::OCRepresentation rep = request->getResourceRepresentation();
241 if (true == modifyResourceModel(rep))
243 response->setErrorCode(200);
244 response->setResponseResult(OC_EH_OK);
245 response->setResourceRepresentation(getOCRepresentation());
249 response->setErrorCode(400);
250 response->setResponseResult(OC_EH_ERROR);
253 if (OC_STACK_OK == OC::OCPlatform::sendResponse(response))
258 else if ("POST" == request->getRequestType())
260 SIM_LOG(ILogger::INFO, "[" << m_uri << "] POST request received");
261 OC::OCRepresentation rep = request->getResourceRepresentation();
262 if (true == modifyResourceModel(rep))
264 response->setErrorCode(200);
265 response->setResponseResult(OC_EH_OK);
266 response->setResourceRepresentation(getOCRepresentation());
270 response->setErrorCode(400);
271 response->setResponseResult(OC_EH_ERROR);
274 if (OC_STACK_OK == OC::OCPlatform::sendResponse(response))
279 else if ("DELETE" == request->getRequestType())
281 SIM_LOG(ILogger::INFO, "[" << m_uri << "] DELETE request received!");
282 OC::OCRepresentation rep = request->getResourceRepresentation();
284 // DELETE request handling not supported right now
285 response->setErrorCode(400);
286 response->setResponseResult(OC_EH_ERROR);
287 if (OC_STACK_OK == OC::OCPlatform::sendResponse(response))
294 SIM_LOG(ILogger::INFO, "[" << m_uri << "] UNKNOWN type request received");
295 response->setResponseResult(OC_EH_ERROR);
296 OC::OCPlatform::sendResponse(response);
297 errCode = OC_EH_ERROR;
301 if (OC::RequestHandlerFlag::ObserverFlag & request->getRequestHandlerFlag())
303 if (false == isObservable())
305 SIM_LOG(ILogger::INFO, "[" << m_uri << "] OBSERVE request received");
306 SIM_LOG(ILogger::INFO, "[" << m_uri << "] Sending error as resource is in unobservable state");
310 OC::ObservationInfo observationInfo = request->getObservationInfo();
311 if (OC::ObserveAction::ObserveRegister == observationInfo.action)
313 SIM_LOG(ILogger::INFO, "[" << m_uri << "] OBSERVE REGISTER request received");
314 m_interestedObservers.push_back(observationInfo.obsId);
316 else if (OC::ObserveAction::ObserveUnregister == observationInfo.action)
318 SIM_LOG(ILogger::INFO, "[" << m_uri << "] OBSERVE UNREGISTER request received");
319 m_interestedObservers.erase(std::remove(m_interestedObservers.begin(),
320 m_interestedObservers.end(),
321 observationInfo.obsId),
322 m_interestedObservers.end());
330 void SimulatorResourceServer::notifyListOfObservers ()
332 if (!m_resourceHandle)
337 if (m_interestedObservers.size())
339 std::shared_ptr<OC::OCResourceResponse> resourceResponse =
340 {std::make_shared<OC::OCResourceResponse>()};
342 resourceResponse->setErrorCode(200);
343 resourceResponse->setResponseResult(OC_EH_OK);
344 resourceResponse->setResourceRepresentation(getOCRepresentation(), OC::DEFAULT_INTERFACE);
346 SIM_LOG(ILogger::INFO, "[" << m_uri << "] Sending notification to all observers" << m_uri);
347 OC::OCPlatform::notifyListOfObservers(m_resourceHandle, m_interestedObservers, resourceResponse);
352 m_callback(m_uri, m_resModel);