1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 * This file contains the declaration of classes and its members related to
28 #ifndef __OCRESOURCE_H
29 #define __OCRESOURCE_H
36 #include <ResourceInitException.h>
37 #include <IClientWrapper.h>
38 #include <InProcClientWrapper.h>
39 #include <OCRepresentation.h>
44 class OCResourceIdentifier;
45 std::ostream& operator <<(std::ostream& os, const OCResourceIdentifier& ri);
47 * @brief OCResourceIdentifier represents the identity information for a server. This
48 * object combined with the OCResource's URI property uniquely identify an
49 * OCResource on or across networks.
50 * Equality operators are implemented. However, internal representation is subject
51 * to change and thus should not be accessed or depended on.
53 class OCResourceIdentifier
55 friend class OCResource;
56 friend std::ostream& operator <<(std::ostream& os, const OCResourceIdentifier& ri);
59 OCResourceIdentifier() = delete;
61 OCResourceIdentifier(const OCResourceIdentifier&) = default;
63 OCResourceIdentifier(OCResourceIdentifier&&) = default;
65 OCResourceIdentifier& operator=(const OCResourceIdentifier&) = delete;
67 OCResourceIdentifier& operator=(OCResourceIdentifier&&) = delete;
69 bool operator==(const OCResourceIdentifier &other) const;
71 bool operator!=(const OCResourceIdentifier &other) const;
73 bool operator<(const OCResourceIdentifier &other) const;
75 bool operator>(const OCResourceIdentifier &other) const;
77 bool operator<=(const OCResourceIdentifier &other) const;
79 bool operator>=(const OCResourceIdentifier &other) const;
83 OCResourceIdentifier(const std::string& wireServerIdentifier,
84 const std::string& resourceUri );
87 uint32_t m_representation;
88 const std::string& m_resourceUri;
92 * @brief OCResource represents an OC resource. A resource could be a light controller,
93 * temperature sensor, smoke detector, etc. A resource comes with a well-defined
94 * contract or interface onto which you can perform different operations, such as
95 * turning on the light, getting the current temperature or subscribing for event
96 * notifications from the smoke detector. A resource can be composed of one or
101 friend class OCPlatform_impl;
102 friend class ListenOCContainer;
104 typedef std::shared_ptr<OCResource> Ptr;
106 OCResource(OCResource&&) = default;
107 OCResource& operator=(OCResource&&) = default;
112 virtual ~OCResource(void);
115 * Function to get the attributes of a resource.
116 * @param queryParametersMap map which can have the query parameter name and value
117 * @param attributeHandler handles callback
118 * The callback function will be invoked with a map of attribute name and values.
119 * The callback function will also have the result from this Get operation
120 * This will have error codes
121 * @param QualityOfService the quality of communication
122 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
123 * NOTE: OCStackResult is defined in ocstack.h.
125 OCStackResult get(const QueryParamsMap& queryParametersMap, GetCallback attributeHandler);
126 OCStackResult get(const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
127 QualityOfService QoS);
130 * Function to get the attributes of a resource.
132 * @param resourceType resourceType of the resource operate on
133 * @param resourceInterface interface type of the resource to operate on
134 * @param queryParametersMap map which can have the query parameter name and value
135 * @param attributeHandler handles callback
136 * The callback function will be invoked with a map of attribute name and values.
137 * The callback function will be invoked with a list of URIs if 'get' is invoked on a
138 * resource container (list will be empty if not a container)
139 * The callback function will also have the result from this Get operation. This will
141 * @param QualityOfService the quality of communication
142 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success. <br>
143 * NOTE: OCStackResult is defined in ocstack.h.<br>
144 * <b>Example:</b><br>
145 * Consider resource "a/home" (with link interface and resource type as home) contains links
146 * to "a/kitchen" and "a/room".
147 * Step 1: get("home", Link_Interface, &onGet)<br>
148 * Callback onGet will receive a) Empty attribute map because there are no attributes for
149 * a/home b) list with
150 * full URI of "a/kitchen" and "a/room" resources and their properties c) error code for GET
152 * NOTE: A resource may contain single or multiple resource types. Also, a resource may
153 * contain single or multiple interfaces.<br>
154 * Currently, single GET request is allowed to do operate on single resource type or resource
155 * interface. In future, a single GET <br>
156 * can operate on multiple resource types and interfaces. <br>
157 * NOTE: A client can traverse a tree or graph by doing successive GETs on the returned
158 * resources at a node.<br>
160 OCStackResult get(const std::string& resourceType, const std::string& resourceInterface,
161 const QueryParamsMap& queryParametersMap, GetCallback attributeHandler);
162 OCStackResult get(const std::string& resourceType, const std::string& resourceInterface,
163 const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
164 QualityOfService QoS);
167 * Function to set the representation of a resource (via PUT)
168 * @param representation which can either have all the attribute names and values
169 (which will represent entire state of the resource) or a
170 * set of attribute names and values which needs to be modified
171 * The callback function will be invoked with a map of attribute name and values.
172 * The callback function will also have the result from this Put operation
173 * This will have error codes
174 * @param queryParametersMap map which can have the query parameter name and value
175 * @param attributeHandler attribute handler
176 * @param QualityOfService the quality of communication
177 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
178 * NOTE: OCStackResult is defined in ocstack.h.
180 OCStackResult put(const OCRepresentation& representation,
181 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler);
182 OCStackResult put(const OCRepresentation& representation,
183 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
184 QualityOfService QoS);
187 * Function to set the attributes of a resource (via PUT)
188 * @param resourceType resource type of the resource to operate on
189 * @param resourceInterface interface type of the resource to operate on
190 * @param representation representation of the resource
191 * @param queryParametersMap Map which can have the query parameter name and value
192 * @param attributeHandler attribute handler
193 * The callback function will be invoked with a map of attribute name and values.
194 * The callback function will also have the result from this Put operation
195 * This will have error codes.
196 * The Representation parameter maps which can either have all the attribute names
198 * (which will represent entire state of the resource) or a
199 * set of attribute names and values which needs to be modified
200 * @param QualityOfService the quality of communication
201 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success. <br>
202 * NOTE: OCStackResult is defined in ocstack.h. <br>
204 OCStackResult put(const std::string& resourceType, const std::string& resourceInterface,
205 const OCRepresentation& representation, const QueryParamsMap& queryParametersMap,
206 PutCallback attributeHandler);
207 OCStackResult put(const std::string& resourceType, const std::string& resourceInterface,
208 const OCRepresentation& representation, const QueryParamsMap& queryParametersMap,
209 PutCallback attributeHandler, QualityOfService QoS);
212 * Function to post on a resource
213 * @param representation which can either have all the attribute names and values
214 (which will represent entire state of the resource) or a
215 * set of attribute names and values which needs to be modified
216 * The callback function will be invoked with a map of attribute name and values.
217 * The callback function will also have the result from this Put operation
218 * This will have error codes
219 * @param queryParametersMap map which can have the query parameter name and value
220 * @param attributeHandler attribute handler
221 * @param QualityOfService the quality of communication
222 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
223 * NOTE: OCStackResult is defined in ocstack.h.
225 OCStackResult post(const OCRepresentation& representation,
226 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler);
227 OCStackResult post(const OCRepresentation& representation,
228 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
229 QualityOfService QoS);
232 * Function to post on a resource
233 * @param resourceType resource type of the resource to operate on
234 * @param resourceInterface interface type of the resource to operate on
235 * @param representation representation of the resource
236 * @param queryParametersMap Map which can have the query parameter name and value
237 * @param attributeHandler attribute handler
238 * The callback function will be invoked with a map of attribute name and values.
239 * The callback function will also have the result from this Put operation
240 * This will have error codes.
241 * The Representation parameter maps which can either have all the attribute names
243 * (which will represent entire state of the resource) or a
244 * set of attribute names and values which needs to be modified
245 * @param QualityOfService the quality of communication
246 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success. <br>
247 * NOTE: OCStackResult is defined in ocstack.h. <br>
249 OCStackResult post(const std::string& resourceType, const std::string& resourceInterface,
250 const OCRepresentation& representation, const QueryParamsMap& queryParametersMap,
251 PostCallback attributeHandler);
252 OCStackResult post(const std::string& resourceType, const std::string& resourceInterface,
253 const OCRepresentation& representation, const QueryParamsMap& queryParametersMap,
254 PostCallback attributeHandler, QualityOfService QoS);
257 * Function to perform DELETE operation
258 * @param observeHandler handles callback
259 * The callback function will have headerOptions and result from this Delete
260 * operation. This will have error codes
261 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
262 * NOTE: OCStackResult is defined in ocstack.h.
264 OCStackResult deleteResource(DeleteCallback deleteHandler);
265 OCStackResult deleteResource(DeleteCallback deleteHandler, QualityOfService QoS);
268 * Function to set observation on the resource
269 * @param observeType allows the client to specify how it wants to observe.
270 * @param queryParametersMap map which can have the query parameter name and value
271 * @param observeHandler handles callback
272 * The callback function will be invoked with a map of attribute name and values.
273 * The callback function will also have the result from this observe operation
274 * This will have error codes
275 * @param QualityOfService the quality of communication
276 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
277 * NOTE: OCStackResult is defined in ocstack.h.
279 OCStackResult observe(ObserveType observeType, const QueryParamsMap& queryParametersMap,
280 ObserveCallback observeHandler);
281 OCStackResult observe(ObserveType observeType, const QueryParamsMap& queryParametersMap,
282 ObserveCallback observeHandler, QualityOfService qos);
285 * Function to cancel the observation on the resource
286 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
287 * NOTE: OCStackResult is defined in ocstack.h.
289 OCStackResult cancelObserve();
290 OCStackResult cancelObserve(QualityOfService qos);
293 * Function to set header information.
294 * @param headerOptions std::vector where header information(header optionID and optionData
297 * NOTE: Once the headers information is set, it will be applicable to GET, PUT and observe
299 * setHeaderOptions can be used multiple times if headers need to be modifed by the client.
300 * Latest headers will be used to send in the request. <br>
301 * NOTE: Initial support is only for two headers. If headerMap consists of more than two
302 * header options, they will be ignored. <br>
303 * Use unsetHeaderOptions API to clear the header information.
305 void setHeaderOptions(const HeaderOptions& headerOptions)
307 m_headerOptions = headerOptions;
311 * Function to unset header options.
313 void unsetHeaderOptions()
315 m_headerOptions.clear();
319 * Function to get the host address of this resource
320 * @return std::string host address
321 * NOTE: This might or might not be exposed in future due to security concerns
323 std::string host() const;
326 * Function to get the URI for this resource
327 * @return std::string resource URI
329 std::string uri() const;
332 * Function to get the connectivity type of this resource
333 * @return uint8_t connectivity type
335 OCConnectivityType connectivityType() const;
338 * Function to provide ability to check if this resource is observable or not
339 * @return bool true indicates resource is observable; false indicates resource is
342 bool isObservable() const;
345 * Function to get the list of resource types
346 * @return vector of resource types
348 std::vector<std::string> getResourceTypes() const
350 return m_resourceTypes;
354 * Function to get the list of resource interfaces
355 * @return vector of resource interface
357 std::vector<std::string> getResourceInterfaces(void) const
362 // TODO-CA Revisit this since we are exposing two identifiers
364 * Function to get a unique identifier for this
365 * resource across network interfaces. This will
366 * be guaranteed unique for every resource-per-server
367 * independent of how this was discovered.
368 * @return OCResourceIdentifier object, which can
369 * be used for all comparison and hashing.
371 OCResourceIdentifier uniqueIdentifier() const;
374 * Function to get a string representation of the resource's server ID.
375 * This is unique per- server independent on how it was discovered.
376 * Note: The format of the return value is subject to change and will
377 * likely change both in size and contents in the future.
379 std::string sid() const;
381 // overloaded operators allow for putting into a 'set'
382 // the uniqueidentifier allows for putting into a hash
383 bool operator==(const OCResource &other) const;
385 bool operator!=(const OCResource &other) const;
387 bool operator<(const OCResource &other) const;
389 bool operator>(const OCResource &other) const;
391 bool operator<=(const OCResource &other) const;
393 bool operator>=(const OCResource &other) const;
396 std::weak_ptr<IClientWrapper> m_clientWrapper;
398 OCResourceIdentifier m_resourceId;
400 OCConnectivityType m_connectivityType;
403 std::vector<std::string> m_resourceTypes;
404 std::vector<std::string> m_interfaces;
405 std::vector<std::string> m_children;
406 OCDoHandle m_observeHandle;
407 HeaderOptions m_headerOptions;
410 OCResource(std::weak_ptr<IClientWrapper> clientWrapper, const std::string& host,
411 const std::string& uri, const std::string& serverId,
412 OCConnectivityType m_connectivityType, bool observable,
413 const std::vector<std::string>& resourceTypes,
414 const std::vector<std::string>& interfaces);
419 #endif //__OCRESOURCE_H