1 //******************************************************************
3 // Copyright 2015 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 #include "RCSRemoteResourceObject.h"
23 #include "OCPlatform.h"
25 #include "ResourceBroker.h"
26 #include "ResourceCacheManager.h"
28 #include "ScopeLogger.h"
30 #define TAG PCF("RCSRemoteResourceObject")
34 using namespace OIC::Service;
36 ResourceState convertBrokerState(BROKER_STATE state)
38 SCOPE_LOG_F(DEBUG, TAG);
42 case BROKER_STATE::ALIVE:
43 return ResourceState::ALIVE;
45 case BROKER_STATE::REQUESTED:
46 return ResourceState::REQUESTED;
48 case BROKER_STATE::LOST_SIGNAL:
49 return ResourceState::LOST_SIGNAL;
51 case BROKER_STATE::DESTROYED:
52 return ResourceState::DESTROYED;
54 case BROKER_STATE::NONE:
55 return ResourceState::NONE;
58 return ResourceState::NONE;
61 CacheState convertCacheState(CACHE_STATE state)
63 SCOPE_LOG_F(DEBUG, TAG);
67 case CACHE_STATE::READY:
68 return CacheState::READY;
70 case CACHE_STATE::READY_YET:
71 case CACHE_STATE::UPDATING:
72 return CacheState::UNREADY;
74 case CACHE_STATE::LOST_SIGNAL:
75 return CacheState::LOST_SIGNAL;
77 case CACHE_STATE::DESTROYED:
78 case CACHE_STATE::NONE:
79 return CacheState::NONE;
82 return CacheState::NONE;
85 OCStackResult hostingCallback(BROKER_STATE state,
86 RCSRemoteResourceObject::StateChangedCallback onResourceStateChanged)
88 SCOPE_LOG_F(DEBUG, TAG);
90 onResourceStateChanged(convertBrokerState(state));
94 OCStackResult cachingCallback(std::shared_ptr< PrimitiveResource >,
95 const RCSResourceAttributes& data, int eCode,
96 RCSRemoteResourceObject::CacheUpdatedCallback onCacheUpdated,
97 std::weak_ptr<RCSRemoteResourceObject> resourcePtr)
99 SCOPE_LOG_F(DEBUG, TAG);
101 //If error code is failure then RE Cache module should
102 //do clean up for caching flags, maps etc.
105 OIC_LOG_V(ERROR, TAG, "Error code: %d",eCode);
108 std::shared_ptr<RCSRemoteResourceObject> resource = resourcePtr.lock();
111 resource->stopCaching();
115 OIC_LOG(ERROR, TAG, "Resource object is null");
120 //Exception will be thrown: stack will return OC_STACK_ERROR
121 // if it already stopped observe. This call is reqired for clearing
123 OIC_LOG(DEBUG, TAG, "Cleared Cache");
127 //Calling application callback
128 onCacheUpdated(data, eCode);
132 void setRemoteAttributesCb(const HeaderOptions&, const ResponseStatement& response, int eCode,
133 RCSRemoteResourceObject::RemoteAttributesSetCallback onRemoteAttributesSet)
135 SCOPE_LOG_F(DEBUG, TAG);
137 onRemoteAttributesSet(response.getAttributes(), eCode);
140 void getRemoteAttributesCb(const HeaderOptions&, const ResponseStatement& response, int eCode,
141 RCSRemoteResourceObject::RemoteAttributesGetCallback onRemoteAttributesReceived)
143 SCOPE_LOG_F(DEBUG, TAG);
145 onRemoteAttributesReceived(response.getAttributes(), eCode);
154 RCSQueryParams& RCSQueryParams::setResourceInterface(std::string resourceInterface)
156 m_resourceInterface = std::move(resourceInterface);
160 RCSQueryParams& RCSQueryParams::setResourceType(std::string resourceType)
162 m_resourceType = std::move(resourceType);
166 RCSQueryParams& RCSQueryParams::put(std::string key, std::string value)
168 m_map[std::move(key)] = std::move(value);
172 std::string RCSQueryParams::getResourceInterface() const
174 return m_resourceInterface;
177 std::string RCSQueryParams::getResourceType() const
179 return m_resourceType;
182 std::string RCSQueryParams::get(const std::string& key) const
186 return m_map.at(key);
188 catch (const std::out_of_range&)
190 throw RCSInvalidKeyException(key + " is an invalid key");
194 const RCSQueryParams::Map& RCSQueryParams::getAll() const
199 RCSRemoteResourceObject::RCSRemoteResourceObject(
200 std::shared_ptr< PrimitiveResource > primtiveResource) :
201 m_primitiveResource{ primtiveResource },
207 RCSRemoteResourceObject::~RCSRemoteResourceObject()
209 SCOPE_LOG_F(DEBUG, TAG);
215 catch(std::exception &e){
216 OIC_LOG_V(ERROR, TAG, "%s", e.what());
220 RCSRemoteResourceObject::Ptr RCSRemoteResourceObject::fromOCResource(
221 std::shared_ptr< OC::OCResource > ocResource)
225 throw RCSInvalidParameterException("the oc resource must not be nullptr.");
228 return std::make_shared< RCSRemoteResourceObject >(
229 PrimitiveResource::create(ocResource));
232 std::shared_ptr< OC::OCResource > RCSRemoteResourceObject::toOCResource(
233 RCSRemoteResourceObject::Ptr rcsResource)
237 throw RCSInvalidParameterException("the rcs resource must not be nullptr.");
240 OC::OCResource::Ptr ocResource = OC::OCPlatform::constructResourceObject(rcsResource->getAddress(),
241 rcsResource->getUri(),
242 rcsResource->m_primitiveResource->getConnectivityType(),
243 rcsResource->isObservable(),
244 rcsResource->getTypes(),
245 rcsResource->getInterfaces());
250 bool RCSRemoteResourceObject::isMonitoring() const
252 return m_brokerId != 0;
255 bool RCSRemoteResourceObject::isCaching() const
257 return m_cacheId != 0;
260 bool RCSRemoteResourceObject::isObservable() const
262 return m_primitiveResource->isObservable();
265 void RCSRemoteResourceObject::startMonitoring(StateChangedCallback cb)
267 SCOPE_LOG_F(DEBUG, TAG);
271 throw RCSInvalidParameterException{ "startMonitoring : Callback is NULL" };
276 OIC_LOG(DEBUG, TAG, "startMonitoring : already started");
277 throw RCSBadRequestException{ "Monitoring already started." };
280 m_brokerId = ResourceBroker::getInstance()->hostResource(m_primitiveResource,
281 std::bind(hostingCallback, std::placeholders::_1, std::move(cb)));
284 void RCSRemoteResourceObject::stopMonitoring()
286 SCOPE_LOG_F(DEBUG, TAG);
290 OIC_LOG(DEBUG, TAG, "stopMonitoring : Not started");
294 ResourceBroker::getInstance()->cancelHostResource(m_brokerId);
298 ResourceState RCSRemoteResourceObject::getState() const
300 SCOPE_LOG_F(DEBUG, TAG);
304 return ResourceState::NONE;
307 return convertBrokerState(
308 ResourceBroker::getInstance()->getResourceState(m_primitiveResource));
311 void RCSRemoteResourceObject::startCaching()
316 void RCSRemoteResourceObject::startCaching(CacheUpdatedCallback cb, CacheMode mode)
318 SCOPE_LOG_F(DEBUG, TAG);
322 OIC_LOG(DEBUG, TAG, "startCaching : already Started");
323 throw RCSBadRequestException{ "Caching already started." };
326 if (mode == CacheMode::OBSERVE_ONLY)
328 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
330 std::bind(cachingCallback, std::placeholders::_1,
331 std::placeholders::_2, std::placeholders::_3,
332 std::move(cb), shared_from_this()), CACHE_METHOD::OBSERVE_ONLY,
333 REPORT_FREQUENCY::UPTODATE, 0);
338 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
340 std::bind(cachingCallback, std::placeholders::_1,
341 std::placeholders::_2, std::placeholders::_3,
342 std::move(cb), shared_from_this()), CACHE_METHOD::ITERATED_GET,
343 REPORT_FREQUENCY::UPTODATE, 0);
347 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
348 m_primitiveResource, { }, CACHE_METHOD::ITERATED_GET,
349 REPORT_FREQUENCY::NONE, 0);
352 OIC_LOG_V(DEBUG, TAG, "startCaching CACHE ID %d", m_cacheId);
355 void RCSRemoteResourceObject::stopCaching()
357 SCOPE_LOG_F(DEBUG, TAG);
361 OIC_LOG(DEBUG, TAG, "Caching already terminated");
367 ResourceCacheManager::getInstance()->cancelResourceCache(m_cacheId);
369 catch (const RCSInvalidParameterException &)
381 CacheState RCSRemoteResourceObject::getCacheState() const
383 SCOPE_LOG_F(DEBUG, TAG);
387 return CacheState::NONE;
390 return convertCacheState(
391 ResourceCacheManager::getInstance()->getResourceCacheState(m_cacheId));
394 bool RCSRemoteResourceObject::isCachedAvailable() const
401 return ResourceCacheManager::getInstance()->isCachedData(m_cacheId);
404 RCSResourceAttributes RCSRemoteResourceObject::getCachedAttributes() const
406 SCOPE_LOG_F(DEBUG, TAG);
410 throw RCSBadRequestException{ "Caching not started." };
413 if (!isCachedAvailable())
415 throw RCSBadRequestException{ "Cache data is not available." };
418 return ResourceCacheManager::getInstance()->getCachedData(m_cacheId);
421 RCSResourceAttributes::Value RCSRemoteResourceObject::getCachedAttribute(
422 const std::string& key) const
424 SCOPE_LOG_F(DEBUG, TAG);
426 return getCachedAttributes().at(key);
429 std::string RCSRemoteResourceObject::getUri() const
431 return m_primitiveResource->getUri();
434 std::string RCSRemoteResourceObject::getAddress() const
436 return m_primitiveResource->getHost();
439 std::vector< std::string > RCSRemoteResourceObject::getTypes() const
441 return m_primitiveResource->getTypes();
444 std::vector< std::string > RCSRemoteResourceObject::getInterfaces() const
446 return m_primitiveResource->getInterfaces();
449 void RCSRemoteResourceObject::getRemoteAttributes(RemoteAttributesGetCallback cb)
451 SCOPE_LOG_F(DEBUG, TAG);
455 throw RCSInvalidParameterException{ "getRemoteAttributes : Callback is empty" };
458 m_primitiveResource->requestGet(
459 std::bind(getRemoteAttributesCb, std::placeholders::_1, std::placeholders::_2,
460 std::placeholders::_3, std::move(cb)));
463 void RCSRemoteResourceObject::get(GetCallback cb)
465 SCOPE_LOG_F(DEBUG, TAG);
469 throw RCSInvalidParameterException{ "get : Callback is empty" };
472 m_primitiveResource->requestGet(std::move(cb));
475 void RCSRemoteResourceObject::get(const RCSQueryParams& queryParams, GetCallback cb)
477 SCOPE_LOG_F(DEBUG, TAG);
481 throw RCSInvalidParameterException{ "get : Callback is empty" };
484 const auto& paramMap = queryParams.getAll();
486 m_primitiveResource->requestGetWith(
487 queryParams.getResourceType(), queryParams.getResourceInterface(),
488 OC::QueryParamsMap{ paramMap.begin(), paramMap.end() },
492 void RCSRemoteResourceObject::setRemoteAttributes(const RCSResourceAttributes& attribute,
493 RemoteAttributesSetCallback cb)
495 SCOPE_LOG_F(DEBUG, TAG);
499 throw RCSInvalidParameterException{ "setRemoteAttributes : Callback is empty" };
502 m_primitiveResource->requestSet(attribute,
503 std::bind(setRemoteAttributesCb, std::placeholders::_1, std::placeholders::_2,
504 std::placeholders::_3, cb));
507 void RCSRemoteResourceObject::set(const RCSResourceAttributes& attributes, SetCallback cb)
509 SCOPE_LOG_F(DEBUG, TAG);
513 throw RCSInvalidParameterException{ "set : Callback is empty" };
516 m_primitiveResource->requestSet(attributes, std::move(cb));
519 void RCSRemoteResourceObject::set(const RCSQueryParams& queryParams,
520 const RCSResourceAttributes& attributes, SetCallback cb)
522 SCOPE_LOG_F(DEBUG, TAG);
526 throw RCSInvalidParameterException{ "set : Callback is empty" };
529 const auto& paramMap = queryParams.getAll();
531 m_primitiveResource->requestSetWith(
532 queryParams.getResourceType(), queryParams.getResourceInterface(),
533 OC::QueryParamsMap{ paramMap.begin(), paramMap.end() }, attributes,
537 void RCSRemoteResourceObject::set(const RCSQueryParams& queryParams,
538 const RCSRepresentation& rep, SetCallback cb)
540 SCOPE_LOG_F(DEBUG, TAG);
544 throw RCSInvalidParameterException{ "set : Callback is empty" };
547 const auto& paramMap = queryParams.getAll();
549 m_primitiveResource->requestSetWith(
550 queryParams.getResourceType(), queryParams.getResourceInterface(),
551 OC::QueryParamsMap{ paramMap.begin(), paramMap.end() }, rep,