Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceClient / RCSRemoteResourceObject.cpp
index 388ac21..54e7ce8 100644 (file)
@@ -99,7 +99,7 @@ namespace
         return OC_STACK_OK;
     }
 
-    void setCallback(const HeaderOptions&, const ResponseStatement& response, int eCode,
+    void setRemoteAttributesCb(const HeaderOptions&, const ResponseStatement& response, int eCode,
             RCSRemoteResourceObject::RemoteAttributesSetCallback onRemoteAttributesSet)
     {
         SCOPE_LOG_F(DEBUG, TAG);
@@ -107,7 +107,7 @@ namespace
         onRemoteAttributesSet(response.getAttributes(), eCode);
     }
 
-    void getCallback(const HeaderOptions&, const ResponseStatement& response, int eCode,
+    void getRemoteAttributesCb(const HeaderOptions&, const ResponseStatement& response, int eCode,
             RCSRemoteResourceObject::RemoteAttributesGetCallback onRemoteAttributesReceived)
     {
         SCOPE_LOG_F(DEBUG, TAG);
@@ -120,9 +120,56 @@ namespace OIC
 {
     namespace Service
     {
+
+        RCSQueryParams& RCSQueryParams::setResourceInterface(std::string resourceInterface)
+        {
+            m_resourceInterface = std::move(resourceInterface);
+            return *this;
+        }
+
+        RCSQueryParams& RCSQueryParams::setResourceType(std::string resourceType)
+        {
+            m_resourceType = std::move(resourceType);
+            return *this;
+        }
+
+        RCSQueryParams& RCSQueryParams::put(std::string key, std::string value)
+        {
+            m_map[std::move(key)] = std::move(value);
+            return *this;
+        }
+
+        std::string RCSQueryParams::getResourceInterface() const
+        {
+            return m_resourceInterface;
+        }
+
+        std::string RCSQueryParams::getResourceType() const
+        {
+            return m_resourceType;
+        }
+
+        std::string RCSQueryParams::get(const std::string& key) const
+        {
+            try
+            {
+                return m_map.at(key);
+            }
+            catch (const std::out_of_range&)
+            {
+                throw RCSInvalidKeyException(key + " is an invalid key");
+            }
+        }
+
+        const RCSQueryParams::Map& RCSQueryParams::getAll() const
+        {
+            return m_map;
+        }
+
+
         RCSRemoteResourceObject::RCSRemoteResourceObject(
-                std::shared_ptr< PrimitiveResource > pResource) :
-                m_primitiveResource{ pResource },
+                std::shared_ptr< PrimitiveResource > primtiveResource) :
+                m_primitiveResource{ primtiveResource },
                 m_cacheId{ },
                 m_brokerId{ }
         {
@@ -132,8 +179,26 @@ namespace OIC
         {
             SCOPE_LOG_F(DEBUG, TAG);
 
-            stopCaching();
-            stopMonitoring();
+            try{
+                stopCaching();
+                stopMonitoring();
+            }
+            catch(std::exception &e){
+                OIC_LOG_V(ERROR, TAG, "%s", e.what());
+            }
+
+        }
+
+        RCSRemoteResourceObject::Ptr RCSRemoteResourceObject::fromOCResource(
+                std::shared_ptr< OC::OCResource > ocResource)
+        {
+            if (!ocResource)
+            {
+                throw RCSInvalidParameterException("the oc resource must not be nullptr.");
+            }
+
+            return std::make_shared< RCSRemoteResourceObject >(
+                    PrimitiveResource::create(ocResource));
         }
 
         bool RCSRemoteResourceObject::isMonitoring() const
@@ -162,7 +227,7 @@ namespace OIC
 
             if (isMonitoring())
             {
-                OC_LOG(DEBUG, TAG, "startMonitoring : already started");
+                OIC_LOG(DEBUG, TAG, "startMonitoring : already started");
                 throw RCSBadRequestException{ "Monitoring already started." };
             }
 
@@ -176,7 +241,7 @@ namespace OIC
 
             if (!isMonitoring())
             {
-                OC_LOG(DEBUG, TAG, "stopMonitoring : Not started");
+                OIC_LOG(DEBUG, TAG, "stopMonitoring : Not started");
                 return;
             }
 
@@ -208,7 +273,7 @@ namespace OIC
 
             if (isCaching())
             {
-                OC_LOG(DEBUG, TAG, "startCaching : already Started");
+                OIC_LOG(DEBUG, TAG, "startCaching : already Started");
                 throw RCSBadRequestException{ "Caching already started." };
             }
 
@@ -225,7 +290,7 @@ namespace OIC
                         m_primitiveResource, { }, REPORT_FREQUENCY::NONE, 0);
             }
 
-            OC_LOG_V(DEBUG, TAG, "startCaching CACHE ID %d", m_cacheId);
+            OIC_LOG_V(DEBUG, TAG, "startCaching CACHE ID %d", m_cacheId);
         }
 
         void RCSRemoteResourceObject::stopCaching()
@@ -234,7 +299,7 @@ namespace OIC
 
             if (!isCaching())
             {
-                OC_LOG(DEBUG, TAG, "Caching already terminated");
+                OIC_LOG(DEBUG, TAG, "Caching already terminated");
                 return;
             }
 
@@ -320,10 +385,39 @@ namespace OIC
             }
 
             m_primitiveResource->requestGet(
-                    std::bind(getCallback, std::placeholders::_1, std::placeholders::_2,
+                    std::bind(getRemoteAttributesCb, std::placeholders::_1, std::placeholders::_2,
                             std::placeholders::_3, std::move(cb)));
         }
 
+        void RCSRemoteResourceObject::get(GetCallback cb)
+        {
+            SCOPE_LOG_F(DEBUG, TAG);
+
+            if (!cb)
+            {
+                throw RCSInvalidParameterException{ "get : Callback is empty" };
+            }
+
+            m_primitiveResource->requestGet(std::move(cb));
+        }
+
+        void RCSRemoteResourceObject::get(const RCSQueryParams& queryParams, GetCallback cb)
+        {
+            SCOPE_LOG_F(DEBUG, TAG);
+
+            if (!cb)
+            {
+                throw RCSInvalidParameterException{ "get : Callback is empty" };
+            }
+
+            const auto& paramMap = queryParams.getAll();
+
+            m_primitiveResource->requestGetWith(
+                    queryParams.getResourceType(), queryParams.getResourceInterface(),
+                    OC::QueryParamsMap{ paramMap.begin(), paramMap.end() },
+                    std::move(cb));
+        }
+
         void RCSRemoteResourceObject::setRemoteAttributes(const RCSResourceAttributes& attribute,
                 RemoteAttributesSetCallback cb)
         {
@@ -335,8 +429,39 @@ namespace OIC
             }
 
             m_primitiveResource->requestSet(attribute,
-                    std::bind(setCallback, std::placeholders::_1, std::placeholders::_2,
+                    std::bind(setRemoteAttributesCb, std::placeholders::_1, std::placeholders::_2,
                             std::placeholders::_3, cb));
         }
+
+        void RCSRemoteResourceObject::set(const RCSResourceAttributes& attributes, SetCallback cb)
+        {
+            SCOPE_LOG_F(DEBUG, TAG);
+
+            if (!cb)
+            {
+                throw RCSInvalidParameterException{ "set : Callback is empty" };
+            }
+
+            m_primitiveResource->requestSet(attributes, std::move(cb));
+        }
+
+        void RCSRemoteResourceObject::set(const RCSQueryParams& queryParams,
+                const RCSResourceAttributes& attributes, SetCallback cb)
+        {
+            SCOPE_LOG_F(DEBUG, TAG);
+
+            if (!cb)
+            {
+                throw RCSInvalidParameterException{ "set : Callback is empty" };
+            }
+
+            const auto& paramMap = queryParams.getAll();
+
+            m_primitiveResource->requestSetWith(
+                    queryParams.getResourceType(), queryParams.getResourceInterface(),
+                    OC::QueryParamsMap{ paramMap.begin(), paramMap.end() }, attributes,
+                    std::move(cb));
+        }
+
     }
 }