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_collection_resource_impl.h"
22 #include "simulator_utils.h"
23 #include "simulator_logger.h"
26 #define TAG "SIM_COLLECTION_RESOURCE"
28 SimulatorCollectionResourceImpl::SimulatorCollectionResourceImpl()
29 : m_type(SimulatorResource::Type::COLLECTION_RESOURCE),
30 m_interfaces {OC::DEFAULT_INTERFACE, OC::LINK_INTERFACE},
31 m_resourceHandle(NULL)
33 m_property = static_cast<OCResourceProperty>(OC_DISCOVERABLE | OC_OBSERVABLE);
36 std::string SimulatorCollectionResourceImpl::getName() const
41 SimulatorResource::Type SimulatorCollectionResourceImpl::getType() const
46 std::string SimulatorCollectionResourceImpl::getURI() const
51 std::string SimulatorCollectionResourceImpl::getResourceType() const
53 return m_resourceType;
56 std::vector<std::string> SimulatorCollectionResourceImpl::getInterface() const
61 void SimulatorCollectionResourceImpl::setInterface(const std::vector<std::string> &interfaces)
63 m_interfaces = interfaces;
66 void SimulatorCollectionResourceImpl::setName(const std::string &name)
68 VALIDATE_INPUT(name.empty(), "Name is empty!")
70 std::lock_guard<std::recursive_mutex> lock(m_objectLock);
73 throw SimulatorException(SIMULATOR_OPERATION_NOT_ALLOWED,
74 "Name can not be set when collection is started!");
80 void SimulatorCollectionResourceImpl::setURI(const std::string &uri)
82 VALIDATE_INPUT(uri.empty(), "Uri is empty!")
84 std::lock_guard<std::recursive_mutex> lock(m_objectLock);
87 throw SimulatorException(SIMULATOR_OPERATION_NOT_ALLOWED,
88 "URI can not be set when collection is started!");
94 void SimulatorCollectionResourceImpl::setResourceType(const std::string &resourceType)
96 VALIDATE_INPUT(resourceType.empty(), "Resource type is empty!")
98 std::lock_guard<std::recursive_mutex> lock(m_objectLock);
101 throw SimulatorException(SIMULATOR_OPERATION_NOT_ALLOWED,
102 "Resource type can not be set when collection is started!");
105 m_resourceType = resourceType;
108 void SimulatorCollectionResourceImpl::addInterface(std::string interfaceType)
110 VALIDATE_INPUT(interfaceType.empty(), "Interface type is empty!")
112 if (interfaceType == OC::GROUP_INTERFACE)
114 throw NoSupportException("Collection resource does not support this interface type!");
117 std::lock_guard<std::recursive_mutex> lock(m_objectLock);
118 if (m_resourceHandle)
120 throw SimulatorException(SIMULATOR_OPERATION_NOT_ALLOWED,
121 "Interface type can not be set when resource is started!");
124 auto found = std::find(m_interfaces.begin(), m_interfaces.end(), interfaceType);
125 if (found != m_interfaces.end())
126 m_interfaces.push_back(interfaceType);
129 void SimulatorCollectionResourceImpl::setObservable(bool state)
131 std::lock_guard<std::recursive_mutex> lock(m_objectLock);
132 if (m_resourceHandle)
134 throw SimulatorException(SIMULATOR_OPERATION_NOT_ALLOWED,
135 "Observation state can not be changed when resource is started!");
139 m_property = static_cast<OCResourceProperty>(m_property | OC_OBSERVABLE);
141 m_property = static_cast<OCResourceProperty>(m_property ^ OC_OBSERVABLE);
144 void SimulatorCollectionResourceImpl::setObserverCallback(ObserverCallback callback)
146 VALIDATE_CALLBACK(callback)
147 m_observeCallback = callback;
150 void SimulatorCollectionResourceImpl::setModelChangeCallback(ResourceModelChangedCallback callback)
152 VALIDATE_CALLBACK(callback)
153 m_modelCallback = callback;
156 bool SimulatorCollectionResourceImpl::isObservable()
158 return (m_property & OC_OBSERVABLE);
161 bool SimulatorCollectionResourceImpl::isStarted()
163 return (nullptr != m_resourceHandle);
166 void SimulatorCollectionResourceImpl::start()
168 std::lock_guard<std::recursive_mutex> lock(m_objectLock);
169 if (m_resourceHandle)
171 throw SimulatorException(SIMULATOR_ERROR, "Collection already registered!");
174 if (m_uri.empty() || m_resourceType.empty())
176 throw SimulatorException(SIMULATOR_ERROR, "Found incomplete data to start resource!");
179 typedef OCStackResult (*RegisterResource)(OCResourceHandle &, std::string &, const std::string &,
180 const std::string &, OC::EntityHandler, uint8_t);
182 invokeocplatform(static_cast<RegisterResource>(OC::OCPlatform::registerResource),
183 m_resourceHandle, m_uri, m_resourceType, m_interfaces[0],
184 std::bind(&SimulatorCollectionResourceImpl::handleRequests,
185 this, std::placeholders::_1), m_property);
187 for (size_t index = 1; m_interfaces.size() > 1 && index < m_interfaces.size(); index++)
189 typedef OCStackResult (*bindInterfaceToResource)(const OCResourceHandle &,
190 const std::string &);
194 invokeocplatform(static_cast<bindInterfaceToResource>(
195 OC::OCPlatform::bindInterfaceToResource), m_resourceHandle,
196 m_interfaces[index]);
198 catch (SimulatorException &e)
206 void SimulatorCollectionResourceImpl::stop()
208 std::lock_guard<std::recursive_mutex> lock(m_objectLock);
209 if (!m_resourceHandle)
212 typedef OCStackResult (*UnregisterResource)(const OCResourceHandle &);
214 invokeocplatform(static_cast<UnregisterResource>(OC::OCPlatform::unregisterResource),
217 m_resourceHandle = nullptr;
220 SimulatorResourceModel SimulatorCollectionResourceImpl::getResourceModel()
222 std::lock_guard<std::mutex> lock(m_modelLock);
226 void SimulatorCollectionResourceImpl::setResourceModel(const SimulatorResourceModel &resModel)
228 std::lock_guard<std::mutex> lock(m_modelLock);
229 m_resModel = resModel;
232 std::vector<ObserverInfo> SimulatorCollectionResourceImpl::getObserversList()
234 return m_observersList;
237 void SimulatorCollectionResourceImpl::notify(int id)
239 std::lock_guard<std::recursive_mutex> lock(m_objectLock);
240 if (!m_resourceHandle)
243 OC::ObservationIds observers {static_cast<OCObservationId>(id)};
244 sendNotification(observers);
247 void SimulatorCollectionResourceImpl::notifyAll()
249 std::lock_guard<std::recursive_mutex> lock(m_objectLock);
250 if (!m_resourceHandle)
253 if (!m_observersList.size())
256 OC::ObservationIds observers;
257 for (auto &observer : m_observersList)
258 observers.push_back(observer.id);
259 sendNotification(observers);
262 std::vector<std::string> SimulatorCollectionResourceImpl::getSupportedResources()
264 return m_supportedTypes;
267 void SimulatorCollectionResourceImpl::addChildResource(SimulatorResourceSP &resource)
269 VALIDATE_INPUT(!resource, "Invalid child resource!")
271 std::lock_guard<std::mutex> lock(m_childResourcesLock);
272 if (m_childResources.end() != m_childResources.find(resource->getURI()))
274 throw SimulatorException(SIMULATOR_ERROR, "Child resource with same URI is already exisit!");
277 m_childResources[resource->getURI()] = resource;
280 // Notify application and observers
282 m_modelCallback(m_uri, m_resModel);
286 void SimulatorCollectionResourceImpl::removeChildResource(SimulatorResourceSP &resource)
288 VALIDATE_INPUT(!resource, "Invalid child resource!")
290 std::lock_guard<std::mutex> lock(m_childResourcesLock);
291 if (m_childResources.end() == m_childResources.find(resource->getURI()))
293 throw SimulatorException(SIMULATOR_ERROR, "Child resource not found in collection!");
296 removeLink(resource->getURI());
297 m_childResources.erase(m_childResources.find(resource->getURI()));
299 // Notify application and observers
301 m_modelCallback(m_uri, m_resModel);
305 void SimulatorCollectionResourceImpl::removeChildResource(const std::string &uri)
307 VALIDATE_INPUT(uri.empty(), "Uri is empty!")
309 std::lock_guard<std::mutex> lock(m_childResourcesLock);
310 if (m_childResources.end() == m_childResources.find(uri))
312 throw SimulatorException(SIMULATOR_ERROR, "Child resource not found in collection!");
316 m_childResources.erase(m_childResources.find(uri));
318 // Notify application and observers
320 m_modelCallback(m_uri, m_resModel);
324 std::vector<SimulatorResourceSP> SimulatorCollectionResourceImpl::getChildResources()
326 std::lock_guard<std::mutex> lock(m_childResourcesLock);
328 std::vector<SimulatorResourceSP> result;
329 for (auto &entry : m_childResources)
330 result.push_back(entry.second);
335 OCEntityHandlerResult SimulatorCollectionResourceImpl::handleRequests(
336 std::shared_ptr<OC::OCResourceRequest> request)
341 if (OC::RequestHandlerFlag::RequestFlag & request->getRequestHandlerFlag())
344 OC::OCRepresentation rep = request->getResourceRepresentation();
345 std::string payload = getPayloadString(rep);
346 SIM_LOG(ILogger::INFO, "[" << m_name << "] " << request->getRequestType()
347 << " request received. \n**Payload details**\n" << payload)
350 // Handover the request to appropriate interface handler
351 std::string interfaceType(OC::DEFAULT_INTERFACE);
352 OC::QueryParamsMap queryParams = request->getQueryParameters();
353 if (queryParams.end() != queryParams.find("if"))
354 interfaceType = queryParams["if"];
356 std::shared_ptr<OC::OCResourceResponse> response;
357 if (interfaceType == OC::DEFAULT_INTERFACE)
359 response = requestOnBaseLineInterface(request);
361 else if (interfaceType == OC::LINK_INTERFACE)
363 response = requestOnLinkListInterface(request);
365 else if (interfaceType == OC::BATCH_INTERFACE)
367 response = requestOnBatchInterface(request);
370 // Send response if the request handled by resource
373 if (OC_STACK_OK != OC::OCPlatform::sendResponse(response))
378 SIM_LOG(ILogger::ERROR, "[" << m_name << "] " << "Unsupported request received!")
383 if (OC::RequestHandlerFlag::ObserverFlag & request->getRequestHandlerFlag())
387 SIM_LOG(ILogger::INFO, "[" << m_uri << "] OBSERVE request received")
388 SIM_LOG(ILogger::INFO, "[" << m_uri << "] Sending error as resource is in unobservable state!")
392 OC::ObservationInfo observationInfo = request->getObservationInfo();
393 if (OC::ObserveAction::ObserveRegister == observationInfo.action)
395 SIM_LOG(ILogger::INFO, "[" << m_uri << "] OBSERVE REGISTER request received");
397 ObserverInfo info {observationInfo.obsId, observationInfo.address, observationInfo.port};
398 m_observersList.push_back(info);
400 if (m_observeCallback)
401 m_observeCallback(m_uri, ObservationStatus::REGISTER, info);
403 else if (OC::ObserveAction::ObserveUnregister == observationInfo.action)
405 SIM_LOG(ILogger::INFO, "[" << m_uri << "] OBSERVE UNREGISTER request received");
408 for (auto iter = m_observersList.begin(); iter != m_observersList.end(); iter++)
410 if ((info = *iter), info.id == observationInfo.obsId)
412 m_observersList.erase(iter);
417 if (m_observeCallback)
418 m_observeCallback(m_uri, ObservationStatus::UNREGISTER, info);
425 std::shared_ptr<OC::OCResourceResponse> SimulatorCollectionResourceImpl::requestOnBaseLineInterface(
426 std::shared_ptr<OC::OCResourceRequest> request)
428 std::shared_ptr<OC::OCResourceResponse> response;
429 if ("GET" == request->getRequestType())
431 // Construct the representation
432 OC::OCRepresentation ocRep = m_resModel.getOCRepresentation();
433 response = std::make_shared<OC::OCResourceResponse>();
434 response->setErrorCode(200);
435 response->setResponseResult(OC_EH_OK);
436 response->setResourceRepresentation(ocRep);
437 std::string resPayload = getPayloadString(ocRep);
438 SIM_LOG(ILogger::INFO, "[" << m_uri <<
439 "] Sending response for GET request. \n**Payload details**" << resPayload)
442 // TODO: Handle PUT, POST and DELETE requests
446 response->setRequestHandle(request->getRequestHandle());
447 response->setResourceHandle(request->getResourceHandle());
453 std::shared_ptr<OC::OCResourceResponse> SimulatorCollectionResourceImpl::requestOnLinkListInterface(
454 std::shared_ptr<OC::OCResourceRequest> request)
456 std::lock_guard<std::mutex> lock(m_childResourcesLock);
457 std::shared_ptr<OC::OCResourceResponse> response;
458 if ("GET" == request->getRequestType())
460 // Construct the representation
461 OC::OCRepresentation ocRep;
462 std::vector<OC::OCRepresentation> links;
463 for (auto &entry : m_childResources)
465 OC::OCRepresentation oicLink;
466 oicLink.setValue("href", entry.second->getURI());
467 oicLink.setValue("rt", entry.second->getResourceType());
468 oicLink.setValue("if", entry.second->getInterface()[0]);
469 links.push_back(oicLink);
472 ocRep.setValue("links", links);
474 response = std::make_shared<OC::OCResourceResponse>();
475 response->setRequestHandle(request->getRequestHandle());
476 response->setResourceHandle(request->getResourceHandle());
477 response->setErrorCode(200);
478 response->setResponseResult(OC_EH_OK);
479 response->setResourceRepresentation(ocRep);
480 std::string resPayload = getPayloadString(ocRep);
481 SIM_LOG(ILogger::INFO, "[" << m_uri <<
482 "] Sending response for GET request. \n**Payload details**" << resPayload)
488 std::shared_ptr<OC::OCResourceResponse> SimulatorCollectionResourceImpl::requestOnBatchInterface(
489 std::shared_ptr<OC::OCResourceRequest>)
491 // TODO: Handle this interface
495 void SimulatorCollectionResourceImpl::sendNotification(OC::ObservationIds &observers)
497 std::lock_guard<std::recursive_mutex> lock(m_objectLock);
498 std::shared_ptr<OC::OCResourceResponse> response(new OC::OCResourceResponse());
499 response->setErrorCode(200);
500 response->setResponseResult(OC_EH_OK);
502 OC::OCRepresentation ocRep = m_resModel.getOCRepresentation();
503 response->setResourceRepresentation(ocRep, OC::DEFAULT_INTERFACE);
505 typedef OCStackResult (*NotifyListOfObservers)(OCResourceHandle, OC::ObservationIds &,
506 const std::shared_ptr<OC::OCResourceResponse>);
508 invokeocplatform(static_cast<NotifyListOfObservers>(OC::OCPlatform::notifyListOfObservers),
509 m_resourceHandle, observers, response);
512 void SimulatorCollectionResourceImpl::addLink(SimulatorResourceSP &resource)
514 std::lock_guard<std::mutex> lock(m_modelLock);
515 if (!m_resModel.containsAttribute("links"))
518 // Create new OIC Link
519 SimulatorResourceModel newLink;
520 newLink.add("href", resource->getURI());
521 newLink.add("rt", resource->getResourceType());
522 newLink.add("if", resource->getInterface()[0]);
524 // Add OIC Link if it is not present
526 std::vector<SimulatorResourceModel> links = m_resModel.get<std::vector<SimulatorResourceModel>>("links");
527 for (auto &link : links)
529 std::string linkURI = link.get<std::string>("href");
530 if (linkURI == resource->getURI())
539 links.push_back(newLink);
540 m_resModel.updateValue("links", links);
544 void SimulatorCollectionResourceImpl::removeLink(std::string uri)
546 std::lock_guard<std::mutex> lock(m_modelLock);
547 if (!m_resModel.containsAttribute("links"))
550 // Add OIC Link if it is not present
551 std::vector<SimulatorResourceModel> links = m_resModel.get<std::vector<SimulatorResourceModel>>("links");
552 for (size_t i = 0; i < links.size(); i++)
554 std::string linkURI = links[i].get<std::string>("href");
557 links.erase(links.begin()+i);
558 m_resModel.updateValue("links", links);