replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / resource-encapsulation / include / RCSRemoteResourceObject.h
index 5960766..e5af9a2 100644 (file)
@@ -30,6 +30,7 @@
 #include <vector>
 
 #include "RCSResourceAttributes.h"
+#include "RCSRepresentation.h"
 
 namespace OC
 {
@@ -65,6 +66,12 @@ namespace OIC
             LOST_SIGNAL, /**< Failed to reach the resource. */
         };
 
+        enum class CacheMode
+        {
+            OBSERVE_ONLY,
+            OBSERVE_WITH_POLLING
+        };
+
         /**
          * The states of monitoring.
          *
@@ -83,27 +90,65 @@ namespace OIC
 
         class PrimitiveResource;
 
+        /**
+         * This is to specify query parameters for requests to the server.
+         *
+         * @see RCSRemoteResourceObject
+         */
         class RCSQueryParams
         {
         public:
             typedef std::unordered_map< std::string, std::string > Map;
 
         public:
-            RCSQueryParams& setResourceInterface(const std::string&);
-            RCSQueryParams& setResourceInterface(std::string&&);
 
-            RCSQueryParams& setResuorceType(const std::string&);
-            RCSQueryParams& setResuorceType(std::string&&);
+            /**
+             * Sets an interface of the resource to operate on
+             *
+             * @param interface interface
+             */
+            RCSQueryParams& setResourceInterface(std::string interface);
 
-            RCSQueryParams& put(const std::string&, const std::string&);
-            RCSQueryParams& put(std::string&&, std::string&&);
-            RCSQueryParams& put(const std::string&, std::string&&);
-            RCSQueryParams& put(std::string&&, const std::string&);
+            /**
+             * Sets a resource type of the resource to operate on
+             *
+             * @param type resource type
+             */
+            RCSQueryParams& setResourceType(std::string type);
+
+            /**
+             * Sets a resource type of the resource to operate on
+             *
+             * @param key key to be inserted
+             * @param value value to be inserted
+             *
+             * @note "rt" and "if" are reserved, so you should avoid them as a key.
+             *
+             */
+            RCSQueryParams& put(std::string key, std::string value);
 
+            /**
+             * Returns the resource interface.
+             */
             std::string getResourceInterface() const;
+
+            /**
+             * Returns the resource type.
+             */
             std::string getResourceType() const;
-            std::string get(const std::string&) const;
 
+            /**
+             * Returns a value.
+             *
+             * @param key key of the element whose mapped value is accessed.
+             *
+             * @throws InvalidKeyException If @a key doesn't match the key of any value.
+             */
+            std::string get(const std::string& key) const;
+
+            /**
+             * Returns all params.
+             */
             const Map& getAll() const;
 
         private:
@@ -124,7 +169,7 @@ namespace OIC
          * @see RCSDiscoveryManager
          *
          */
-        class RCSRemoteResourceObject
+        class RCSRemoteResourceObject : public std::enable_shared_from_this<RCSRemoteResourceObject>
         {
         public:
             typedef std::shared_ptr< RCSRemoteResourceObject > Ptr;
@@ -142,7 +187,8 @@ namespace OIC
              *
              * @param attrs the updated attributes
              */
-            typedef std::function< void(const RCSResourceAttributes& attrs) > CacheUpdatedCallback;
+            typedef std::function< void(const RCSResourceAttributes& attrs, int eCode) >
+                CacheUpdatedCallback;
 
             /**
              * Callback definition to be invoked when the response of getRemoteAttributes is
@@ -156,8 +202,17 @@ namespace OIC
             typedef std::function< void(const RCSResourceAttributes& attrs, int eCode) >
                 RemoteAttributesGetCallback;
 
-            typedef std::function< void(const HeaderOpts&, const RCSRepresentation& rep, int eCode) >
-                GetCallback;
+            /**
+             * Callback definition to be invoked when the response of get is received.
+             *
+             * @param HeaderOpts
+             * @param rep the result representation
+             * @param eCode the error code received from the resource
+             *
+             * @see get
+             */
+            typedef std::function< void(const HeaderOpts& headerOpts,
+                    const RCSRepresentation& rep, int eCode) > GetCallback;
 
             /**
              * Callback definition to be invoked when the response of setRemoteAttributes is
@@ -168,11 +223,20 @@ namespace OIC
              *
              * @see setRemoteAttributes
              */
-            typedef std::function< void(const RCSResourceAttributes&, int) >
+            typedef std::function< void(const RCSResourceAttributes& attrs, int eCode) >
                 RemoteAttributesSetCallback;
 
-            typedef std::function< void(const HeaderOpts&, const RCSRepresentation& rep, int eCode) >
-                SetCallback;
+            /**
+             * Callback definition to be invoked when the response of set is received.
+             *
+             * @param HeaderOpts
+             * @param rep the result representation
+             * @param eCode the error code received from the resource
+             *
+             * @see set
+             */
+            typedef std::function< void(const HeaderOpts& headerOpts,
+                    const RCSRepresentation& rep, int eCode) > SetCallback;
 
         private:
             typedef int CacheID;
@@ -185,7 +249,21 @@ namespace OIC
 
             ~RCSRemoteResourceObject();
 
-            static RCSRemoteResourceObject::Ptr fromOCResource(std::shared_ptr< OC::OCResource >);
+            /**
+             * Creates an instance from an OCResource instance.
+             *
+             * @throw RCSInvalidParameterException If ocResource is nullptr.
+             */
+            static RCSRemoteResourceObject::Ptr fromOCResource(
+                    std::shared_ptr< OC::OCResource > ocResource);
+
+            /**
+             * Returns an equivalent OCResource using RCSRemoteResourceObject instance.
+             *
+             * @throw RCSInvalidParameterException If rcsResource is nullptr.
+             */
+            static std::shared_ptr< OC::OCResource > toOCResource(
+                    RCSRemoteResourceObject::Ptr rcsResource);
 
             /**
              * Returns whether monitoring is enabled.
@@ -272,6 +350,7 @@ namespace OIC
              * updates the cached data accordingly.
              *
              * @param cb If non-empty function, it will be invoked whenever the cache updated.
+             * @param mode if CacheMode is OBSERVE_ONLY, it will be invoked when receive observe response only.
              *
              * @throws BadRequestException If caching is already started.
              *
@@ -284,7 +363,7 @@ namespace OIC
              * @see getCachedAttribute(const std::string&) const
              *
              */
-            void startCaching(CacheUpdatedCallback cb);
+            void startCaching(CacheUpdatedCallback cb, CacheMode mode = CacheMode::OBSERVE_WITH_POLLING);
 
             /**
              * Stops caching.
@@ -351,19 +430,41 @@ namespace OIC
              * Gets resource attributes directly from the server.
              *
              * This API send a get request to the resource of interest and provides
-             * the attributes to the caller in the RemoteAttributesReceivedCallback.
+             * the attributes to the caller in the RemoteAttributesGetCallback.
              *
              * @throws PlatformException If the operation failed
              * @throws InvalidParameterException If cb is an empty function or null.
              *
-             * @see RCSResourceAttributes::Value
-             *
              * @note The callback will be invoked in an internal thread.
              */
             void getRemoteAttributes(RemoteAttributesGetCallback cb);
 
+            /**
+             * Gets resource representation with empty query parameters directly from the server.
+             *
+             * @param cb A callback to receive the response.
+             *
+             * @throws PlatformException If the operation failed
+             * @throws InvalidParameterException If cb is an empty function or null.
+             *
+             * @note The callback will be invoked in an internal thread.
+             */
             void get(GetCallback cb);
-            void get(const RCSQueryParams&, GetCallback cb);
+
+            /**
+             * Gets resource representation directly from the server.
+             *
+             * The response could be different by the query parameters, it depends on server.
+             *
+             * @param queryParams Query parameters
+             * @param cb A callback to receive the response.
+             *
+             * @throws PlatformException If the operation failed
+             * @throws InvalidParameterException If cb is an empty function or null.
+             *
+             * @note The callback will be invoked in an internal thread.
+             */
+            void get(const RCSQueryParams& queryParams, GetCallback cb);
 
             /**
              * Sends a set request with resource attributes to the server.
@@ -384,8 +485,63 @@ namespace OIC
             void setRemoteAttributes(const RCSResourceAttributes& attributes,
                     RemoteAttributesSetCallback cb);
 
+            /**
+             * Sends a set request with resource attributes to the server.
+             *
+             * The SetRequest behavior depends on query parameters and the server.
+             *
+             * @param attributes Attributes to set
+             * @param cb A callback to receive the response.
+             *
+             * @throws PlatformException If the operation failed
+             * @throws InvalidParameterException If cb is an empty function or null.
+             *
+             * @see RCSResourceObject
+             * @see RCSResourceObject::SetRequestHandlerPolicy
+             *
+             * @note The callback will be invoked in an internal thread.
+             */
             void set(const RCSResourceAttributes& attributes, SetCallback cb);
-            void set(const RCSQueryParams&, const RCSResourceAttributes& attributes,  SetCallback cb);
+
+            /**
+             * Sends a set request with resource attributes to the server.
+             *
+             * The SetRequest behavior depends on query parameters and the server.
+             *
+             * @param queryParams Query parameters
+             * @param attributes Attributes to set
+             * @param cb A callback to receive the response.
+             *
+             * @throws PlatformException If the operation failed
+             * @throws InvalidParameterException If cb is an empty function or null.
+             *
+             * @see RCSResourceObject
+             * @see RCSResourceObject::SetRequestHandlerPolicy
+             *
+             * @note The callback will be invoked in an internal thread.
+             */
+            void set(const RCSQueryParams& queryParams, const RCSResourceAttributes& attributes,
+                    SetCallback cb);
+
+            /**
+             * Sends a set request with resource representation to the server.
+             *
+             * The SetRequest behavior depends on query parameters and the server.
+             *
+             * @param queryParams Query parameters
+             * @param rep Representation to set
+             * @param cb A callback to receive the response.
+             *
+             * @throws PlatformException If the operation failed
+             * @throws InvalidParameterException If cb is an empty function or null.
+             *
+             * @see RCSResourceObject
+             * @see RCSResourceObject::SetRequestHandlerPolicy
+             *
+             * @note The callback will be invoked in an internal thread.
+             */
+            void set(const RCSQueryParams& queryParams, const RCSRepresentation &rep,
+                    SetCallback cb);
 
             /**
              * Returns the uri of the resource.