Partial Implementation of US1574:
[platform/upstream/iotivity.git] / include / OCResource.h
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 /// @file OCResource.h
22
23 /// @brief  This file contains the declaration of classes and its members related to 
24 ///         Resource.
25
26 #ifndef __OCRESOURCE_H
27 #define __OCRESOURCE_H
28
29 #include <memory>
30 #include <random>
31 #include <algorithm>
32
33 #include <boost/property_tree/ptree.hpp>
34 #include <boost/property_tree/json_parser.hpp>
35
36 #include <OCApi.h>
37 #include <ResourceInitException.h>
38 #include <IClientWrapper.h>
39 #include <InProcClientWrapper.h>
40 #include <OCRepresentation.h>
41
42 namespace OC
43 {
44     /**
45     *   @brief  OCResource represents an OC resource. A resource could be a light controller, 
46     *           temperature sensor, smoke detector, etc. A resource comes with a well-defined 
47     *           contract or interface onto which you can perform different operations, such as 
48     *           turning on the light, getting the current temperature or subscribing for event 
49     *           notifications from the smoke detector. A resource can be composed of one or
50     *           more resources. 
51     */
52     class OCResource
53     {
54     friend class OCPlatform;
55     friend class InProcClientWrapper;
56     public:
57         typedef std::shared_ptr<OCResource> Ptr;
58         /**
59         * Virtual destructor
60         */
61         virtual ~OCResource(void);
62         
63         /**
64         * Function to get the attributes of a resource. 
65         * @param queryParametersMap map which can have the query parameter name and value
66         * @param attributeHandler handles callback
67         *        The callback function will be invoked with a map of attribute name and values. 
68         *        The callback function will also have the result from this Get operation 
69         *        This will have error codes
70         * @return OCStackResult return value of this API. Returns OC_STACK_OK if success. 
71         * NOTE: OCStackResult is defined in ocstack.h.
72         */
73         OCStackResult get(const QueryParamsMap& queryParametersMap, GetCallback attributeHandler);
74         
75         /**
76         * Function to get the attributes of a resource. 
77         *
78         * @param resourceType resourceType of the resource operate on
79         * @param resourceInterface interface type of the resource to operate on
80         * @param queryParametersMap map which can have the query parameter name and value
81         * @param attributeHandler handles callback
82         *        The callback function will be invoked with a map of attribute name and values. 
83         *        The callback function will be invoked with a list of URIs if 'get' is invoked on a resource container 
84         *        (list will be empty if not a container)
85         *        The callback function will also have the result from this Get operation. This will have error codes
86         * @return OCStackResult return value of this API. Returns OC_STACK_OK if success. <br>
87         * NOTE: OCStackResult is defined in ocstack.h.<br>
88         * <b>Example:</b><br>
89         * Consider resource "a/home" (with link interface and resource type as home) contains links to "a/kitchen" and "a/room". 
90         * Step 1: get("home", Link_Interface, &onGet)<br>
91         * Callback onGet will receive a) Empty attribute map because there are no attributes for a/home b) list with 
92         * full URI of "a/kitchen" and "a/room" resources and their properties c) error code for GET operation<br>
93         * NOTE: A resource may contain single or multiple resource types. Also, a resource may contain single or multiple interfaces.<br>
94         * Currently, single GET request is allowed to do operate on single resource type or resource interface. In future, a single GET <br>
95         * can operate on multiple resource types and interfaces. <br>
96         * NOTE: A client can traverse a tree or graph by doing successive GETs on the returned resources at a node.<br>
97         */
98         OCStackResult get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler); 
99
100         /**
101         * Function to set the attributes of a resource (via PUT)
102         * @param attributeMap Map which can either have all the attribute names and values
103                  (which will represent entire state of the resource) or a
104         *        set of attribute names and values which needs to be modified
105         *        The callback function will be invoked with a map of attribute name and values.
106         *        The callback function will also have the result from this Put operation
107         *        This will have error codes
108         * @param queryParametersMap map which can have the query parameter name and value
109         * @param attributeHandler attribute handler
110         * @return OCStackResult return value of this API. Returns OC_STACK_OK if success. 
111         * NOTE: OCStackResult is defined in ocstack.h.
112         */
113         OCStackResult put(const OCRepresentation& attributeMap, const QueryParamsMap& queryParametersMap, 
114             PutCallback attributeHandler);
115
116         /**
117         * Function to set the attributes of a resource (via PUT)
118         * @param resourceType resource type of the resource to operate on
119         * @param resourceInterface interface type of the resource to operate on
120         * @param attributeMap attribute map
121         * @param queryParametersMap Map which can have the query parameter name and value
122         * @param attributeHandler attribute handler
123         *        The callback function will be invoked with a map of attribute name and values.
124         *        The callback function will also have the result from this Put operation
125         *        This will have error codes.
126         *        The AttributeMap parameter maps which can either have all the attribute names and values
127         *        (which will represent entire state of the resource) or a
128         *        set of attribute names and values which needs to be modified
129         * @return OCStackResult return value of this API. Returns OC_STACK_OK if success. <br>
130         * NOTE: OCStackResult is defined in ocstack.h. <br>
131         */
132         OCStackResult put(const std::string& resourceType, const std::string& resourceInterface,
133             const OCRepresentation& attributeMap, const QueryParamsMap& queryParametersMap, 
134             PutCallback attributeHandler);
135
136         /**
137         * Function to set observation on the resource
138         * @param observeType allows the client to specify how it wants to observe.
139         * @param queryParametersMap map which can have the query parameter name and value
140         * @param observeHandler handles callback
141         *        The callback function will be invoked with a map of attribute name and values.
142         *        The callback function will also have the result from this observe operation
143         *        This will have error codes
144         * @return OCStackResult return value of this API. Returns OC_STACK_OK if success. 
145         * NOTE: OCStackResult is defined in ocstack.h.
146         */
147         OCStackResult observe(ObserveType observeType, const QueryParamsMap& queryParametersMap, 
148             ObserveCallback observeHandler);
149
150         /**
151         * Function to cancel the observation on the resource
152         * @return OCStackResult return value of this API. Returns OC_STACK_OK if success. 
153         * NOTE: OCStackResult is defined in ocstack.h.
154         */
155         OCStackResult cancelObserve();
156
157         /**
158         * Function to get the host address of this resource
159         * @return std::string host address
160         * NOTE: This might or might not be exposed in future due to security concerns
161         */
162         std::string host() const;
163
164         /**
165         * Function to get the URI for this resource
166         * @return std::string resource URI
167         */
168         std::string uri() const;
169
170         /**
171         * Function to provide ability to check if this resource is observable or not
172         * @return bool true indicates resource is observable; false indicates resource is
173         *         not observable.
174         */
175         bool isObservable() const;
176
177         /**
178         * Function to get the list of resource types
179         * @return vector of resource types 
180         */
181         std::vector<std::string> getResourceTypes() const
182         {
183             return m_resourceTypes;
184         }
185         
186         /**
187         * Function to get the list of resource interfaces
188         * @return vector of resource interface
189         */
190         std::vector<std::string> getResourceInterfaces(void) const
191         {
192             return m_interfaces;
193         }
194
195     private:
196         std::weak_ptr<IClientWrapper> m_clientWrapper;
197         std::string m_uri;
198         std::string m_host;
199         bool m_isObservable;
200         bool m_isCollection;
201         std::vector<std::string> m_resourceTypes;
202         std::vector<std::string> m_interfaces;
203         std::vector<std::string> m_children;
204         OCDoHandle m_observeHandle;
205
206     private:
207         OCResource(std::weak_ptr<IClientWrapper> clientWrapper, const std::string& host, const std::string& uri, 
208             bool observable, const std::vector<std::string>& resourceTypes, const std::vector<std::string>& interfaces); 
209     };
210
211 } // namespace OC
212
213 #endif //__OCRESOURCE_H