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_remote_resource_impl.h"
22 #include "request_model_builder.h"
23 #include "simulator_exceptions.h"
26 #define TAG "SIMULATOR_REMOTE_RESOURCE"
28 SimulatorRemoteResourceImpl::SimulatorRemoteResourceImpl(std::shared_ptr<OC::OCResource>
30 : m_observeState(false),
31 m_getRequestSender(new GETRequestSender(ocResource)),
32 m_putRequestSender(new PUTRequestSender(ocResource)),
33 m_postRequestSender(new POSTRequestSender(ocResource)),
34 m_autoRequestGenMngr(nullptr),
35 m_ocResource(ocResource)
37 m_id = m_ocResource->sid().append(m_ocResource->uri());
40 std::string SimulatorRemoteResourceImpl::getURI() const
42 return m_ocResource->uri();
45 std::string SimulatorRemoteResourceImpl::getHost() const
47 return m_ocResource->host();
50 std::string SimulatorRemoteResourceImpl::getID() const
55 SimulatorConnectivityType SimulatorRemoteResourceImpl::getConnectivityType() const
57 return convertConnectivityType(m_ocResource->connectivityType());
60 std::vector < std::string > SimulatorRemoteResourceImpl::getResourceTypes() const
62 return m_ocResource->getResourceTypes();
65 std::vector < std::string > SimulatorRemoteResourceImpl::getResourceInterfaces() const
67 return m_ocResource->getResourceInterfaces();
70 bool SimulatorRemoteResourceImpl::isObservable() const
72 return m_ocResource->isObservable();
75 void SimulatorRemoteResourceImpl::observe(ObserveType type,
76 ObserveNotificationCallback callback)
80 OC_LOG(ERROR, TAG, "Invalid callback!");
81 throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");
84 std::lock_guard<std::mutex> lock(m_observeMutex);
87 OC_LOG(WARNING, TAG, "Resource already in observe state !");
88 throw SimulatorException(SIMULATOR_ERROR, "Resource is already being observed!");
91 OC::ObserveCallback observeCallback = [this, callback](const OC::HeaderOptions & headerOptions,
92 const OC::OCRepresentation & rep, const int errorCode,
93 const int sequenceNum)
95 // Convert OCRepresentation to SimulatorResourceModel
96 SimulatorResourceModelSP repModel = SimulatorResourceModel::create(rep);
97 callback(m_id, static_cast<SimulatorResult>(errorCode), repModel, sequenceNum);
100 OC::ObserveType observeType = OC::ObserveType::Observe;
101 if (type == ObserveType::OBSERVE_ALL)
103 observeType = OC::ObserveType::ObserveAll;
108 OCStackResult ocResult = m_ocResource->observe(observeType, OC::QueryParamsMap(), observeCallback);
109 if (OC_STACK_OK != ocResult)
111 throw SimulatorException(static_cast<SimulatorResult>(ocResult), OC::OCException::reason(ocResult));
114 catch (OC::OCException &e)
116 throw SimulatorException(static_cast<SimulatorResult>(e.code()), e.reason());
119 m_observeState = true;
122 void SimulatorRemoteResourceImpl::cancelObserve()
126 OCStackResult ocResult = m_ocResource->cancelObserve(OC::QualityOfService::HighQos);
127 if (OC_STACK_OK != ocResult)
129 throw SimulatorException(static_cast<SimulatorResult>(ocResult), OC::OCException::reason(ocResult));
132 catch (OC::OCException &e)
134 throw SimulatorException(static_cast<SimulatorResult>(e.code()), e.reason());
137 std::lock_guard<std::mutex> lock(m_observeMutex);
138 m_observeState = false;
141 void SimulatorRemoteResourceImpl::get(const std::string &interfaceType,
142 const std::map<std::string, std::string> &queryParams,
143 ResponseCallback callback)
147 OC_LOG(ERROR, TAG, "Invalid callback!");
148 throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");
151 if (!m_getRequestSender)
153 OC_LOG(ERROR, TAG, "Invalid GET request sender!");
154 throw NoSupportException("Can not send GET request on this resource!");
157 m_getRequestSender->sendRequest(interfaceType, queryParams,
159 &SimulatorRemoteResourceImpl::onResponseReceived,
160 this, std::placeholders::_1, std::placeholders::_2, callback));
163 void SimulatorRemoteResourceImpl::get(const std::map<std::string, std::string> &queryParams,
164 ResponseCallback callback)
168 OC_LOG(ERROR, TAG, "Invalid callback!");
169 throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");
172 if (!m_getRequestSender)
174 OC_LOG(ERROR, TAG, "Invalid GET request sender !");
175 throw NoSupportException("Can not send GET request on this resource!");
178 m_getRequestSender->sendRequest(std::string(), queryParams,
180 &SimulatorRemoteResourceImpl::onResponseReceived,
181 this, std::placeholders::_1, std::placeholders::_2, callback));
184 void SimulatorRemoteResourceImpl::put(const std::string &interfaceType,
185 const std::map<std::string, std::string> &queryParams,
186 SimulatorResourceModelSP representation,
187 ResponseCallback callback)
191 OC_LOG(ERROR, TAG, "Invalid callback!");
192 throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");
195 if (!m_putRequestSender)
197 OC_LOG(ERROR, TAG, "Invalid PUT request sender !");
198 throw NoSupportException("Can not send PUT request on this resource!");
201 m_putRequestSender->sendRequest(interfaceType, queryParams,
202 representation, std::bind(
203 &SimulatorRemoteResourceImpl::onResponseReceived,
204 this, std::placeholders::_1, std::placeholders::_2, callback));
207 void SimulatorRemoteResourceImpl::put(const std::map<std::string, std::string> &queryParams,
208 SimulatorResourceModelSP representation,
209 ResponseCallback callback)
213 OC_LOG(ERROR, TAG, "Invalid callback!");
214 throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");
217 if (!m_putRequestSender)
219 OC_LOG(ERROR, TAG, "Invalid PUT request sender !");
220 throw NoSupportException("Can not send PUT request on this resource!");
223 m_putRequestSender->sendRequest(std::string(), queryParams,
224 representation, std::bind(
225 &SimulatorRemoteResourceImpl::onResponseReceived,
226 this, std::placeholders::_1, std::placeholders::_2, callback));
229 void SimulatorRemoteResourceImpl::post(const std::string &interfaceType,
230 const std::map<std::string, std::string> &queryParams,
231 SimulatorResourceModelSP representation,
232 ResponseCallback callback)
236 OC_LOG(ERROR, TAG, "Invalid callback!");
237 throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");
240 if (!m_postRequestSender)
242 OC_LOG(ERROR, TAG, "Invalid POST request sender !");
243 throw NoSupportException("Can not send POST request on this resource!");
246 m_postRequestSender->sendRequest(interfaceType, queryParams,
247 representation, std::bind(
248 &SimulatorRemoteResourceImpl::onResponseReceived,
249 this, std::placeholders::_1, std::placeholders::_2, callback));
252 void SimulatorRemoteResourceImpl::post(const std::map<std::string, std::string> &queryParams,
253 SimulatorResourceModelSP representation,
254 ResponseCallback callback)
258 OC_LOG(ERROR, TAG, "Invalid callback!");
259 throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");
262 if (!m_postRequestSender)
264 OC_LOG(ERROR, TAG, "Invalid POST request sender !");
265 throw NoSupportException("Can not send POST request on this resource!");
268 m_postRequestSender->sendRequest(std::string(), queryParams,
269 representation, std::bind(
270 &SimulatorRemoteResourceImpl::onResponseReceived,
271 this, std::placeholders::_1, std::placeholders::_2, callback));
274 int SimulatorRemoteResourceImpl::startVerification(RequestType type,
275 StateCallback callback)
279 OC_LOG(ERROR, TAG, "Invalid callback!");
280 throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");
283 if (!m_autoRequestGenMngr)
285 OC_LOG(ERROR, TAG, "Invalid auto request generation manager !");
286 throw NoSupportException("Resource is not configured with RAML!");
289 if (m_requestModelList.end() == m_requestModelList.find(type))
290 throw NoSupportException("Resource does not support this request type!");
292 // Local callback for handling progress sate callback
293 AutoRequestGeneration::ProgressStateCallback localCallback = [this, callback](
294 int sessionId, OperationState state)
296 callback(m_id, sessionId, state);
301 case RequestType::RQ_TYPE_GET:
302 if (m_getRequestSender)
304 return m_autoRequestGenMngr->startOnGET(m_getRequestSender,
305 m_requestModelList[RequestType::RQ_TYPE_GET]->getQueryParams(),
310 case RequestType::RQ_TYPE_PUT:
311 if (m_putRequestSender)
313 return m_autoRequestGenMngr->startOnPUT(m_putRequestSender,
314 m_requestModelList[RequestType::RQ_TYPE_PUT]->getQueryParams(),
315 m_requestModelList[RequestType::RQ_TYPE_PUT]->getRepSchema(),
320 case RequestType::RQ_TYPE_POST:
321 if (m_postRequestSender)
323 return m_autoRequestGenMngr->startOnPOST(m_putRequestSender,
324 m_requestModelList[RequestType::RQ_TYPE_POST]->getQueryParams(),
325 m_requestModelList[RequestType::RQ_TYPE_POST]->getRepSchema(),
330 case RequestType::RQ_TYPE_DELETE:
332 throw NoSupportException("Resource does not support this request type!");
335 return -1; // Code should not reach here
338 void SimulatorRemoteResourceImpl::stopVerification(int id)
342 OC_LOG(ERROR, TAG, "Invalid session id!");
343 throw InvalidArgsException(SIMULATOR_INVALID_PARAM, "Invalid ID!");
346 if (!m_autoRequestGenMngr)
348 OC_LOG(ERROR, TAG, "Invalid auto request generation manager !");
349 throw NoSupportException("Resource is not configured with RAML!");
352 m_autoRequestGenMngr->stop(id);
355 void SimulatorRemoteResourceImpl::configure(const std::string &path)
359 OC_LOG(ERROR, TAG, "Invalid path given for configuration !");
360 throw InvalidArgsException(SIMULATOR_INVALID_PARAM, "Empty path string!");
363 std::shared_ptr<RAML::RamlParser> ramlParser = std::make_shared<RAML::RamlParser>(path);
364 RAML::RamlPtr raml = ramlParser->getRamlPtr();
369 void SimulatorRemoteResourceImpl::configure(std::shared_ptr<RAML::Raml> &raml)
371 m_requestModelList = RequestModelBuilder(raml).build(m_ocResource->uri());
372 if (m_getRequestSender &&
373 m_requestModelList.end() != m_requestModelList.find(RequestType::RQ_TYPE_GET))
375 m_getRequestSender->setRequestModel(m_requestModelList[RequestType::RQ_TYPE_GET]);
378 if (m_putRequestSender &&
379 m_requestModelList.end() != m_requestModelList.find(RequestType::RQ_TYPE_PUT))
381 m_putRequestSender->setRequestModel(m_requestModelList[RequestType::RQ_TYPE_PUT]);
384 if (m_postRequestSender &&
385 m_requestModelList.end() != m_requestModelList.find(RequestType::RQ_TYPE_POST))
387 m_postRequestSender->setRequestModel(m_requestModelList[RequestType::RQ_TYPE_POST]);
390 if (!m_autoRequestGenMngr)
392 m_autoRequestGenMngr = std::make_shared<AutoRequestGenMngr>();
396 void SimulatorRemoteResourceImpl::onResponseReceived(SimulatorResult result,
397 SimulatorResourceModelSP repModel,
398 ResponseCallback clientCallback)
400 clientCallback(m_id, result, repModel);
403 SimulatorConnectivityType SimulatorRemoteResourceImpl::convertConnectivityType(
404 OCConnectivityType type) const
409 return SIMULATOR_CT_ADAPTER_IP;
412 return SIMULATOR_CT_IP_USE_V4 ;
415 return SIMULATOR_CT_IP_USE_V6;
417 case CT_ADAPTER_GATT_BTLE:
418 return SIMULATOR_CT_ADAPTER_GATT_BTLE;
420 case CT_ADAPTER_RFCOMM_BTEDR:
421 return SIMULATOR_CT_ADAPTER_RFCOMM_BTEDR;
424 return SIMULATOR_CT_DEFAULT;