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 OC_RESOURCE_H_
29 #define OC_RESOURCE_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 #if defined(_MSC_VER) && (_MSC_VER < 1900)
64 OCResourceIdentifier(OCResourceIdentifier&& o):
65 m_resourceUri(std::move(o.m_resourceUri)),
66 m_representation(o.m_representation)
70 OCResourceIdentifier(OCResourceIdentifier&&) = default;
73 OCResourceIdentifier& operator=(const OCResourceIdentifier&) = delete;
75 OCResourceIdentifier& operator=(OCResourceIdentifier&&) = delete;
77 bool operator==(const OCResourceIdentifier &other) const;
79 bool operator!=(const OCResourceIdentifier &other) const;
81 bool operator<(const OCResourceIdentifier &other) const;
83 bool operator>(const OCResourceIdentifier &other) const;
85 bool operator<=(const OCResourceIdentifier &other) const;
87 bool operator>=(const OCResourceIdentifier &other) const;
91 OCResourceIdentifier(const std::string& wireServerIdentifier,
92 const std::string& resourceUri );
95 std::string m_representation;
96 const std::string& m_resourceUri;
100 * @brief OCResource represents an OC resource. A resource could be a light controller,
101 * temperature sensor, smoke detector, etc. A resource comes with a well-defined
102 * contract or interface onto which you can perform different operations, such as
103 * turning on the light, getting the current temperature or subscribing for event
104 * notifications from the smoke detector. A resource can be composed of one or
109 friend class OCPlatform_impl;
110 friend class ListenOCContainer;
112 typedef std::shared_ptr<OCResource> Ptr;
114 #if defined(_MSC_VER) && (_MSC_VER < 1900)
115 OCResource(OCResource&& o):
116 m_clientWrapper(std::move(o.m_clientWrapper)),
117 m_uri(std::move(o.m_uri)),
118 m_resourceId(std::move(o.m_resourceId)),
119 m_devAddr(std::move(o.m_devAddr)),
120 m_useHostString(o.m_useHostString),
121 m_property(o.m_property),
122 m_isCollection(o.m_isCollection),
123 m_resourceTypes(std::move(o.m_resourceTypes)),
124 m_interfaces(std::move(o.m_interfaces)),
125 m_children(std::move(m_children)),
126 m_observeHandle(std::move(m_observeHandle)),
127 m_headerOptions(std::move(m_headerOptions))
131 OCResource(OCResource&&) = default;
133 // Explicitly delete the copy ctor since VS2013 would try to generate one, and
134 // the standard says that defaulting the move ctor should delete the copy ctor.
135 OCResource(const OCResource&) = delete;
137 // We cannot support copy/move assigns since OCResourceIdentifier doesn't.
138 OCResource& operator=(OCResource&&) = delete;
139 OCResource& operator=(const OCResource&) = delete;
144 virtual ~OCResource(void);
147 * Function to get the attributes of a resource.
148 * @param queryParametersMap map which can have the query parameter name and value
149 * @param attributeHandler handles callback
150 * The callback function will be invoked with a map of attribute name and values.
151 * The callback function will also have the result from this Get operation
152 * This will have error codes
153 * @return Returns ::OC_STACK_OK on success, some other value upon failure.
154 * @note OCStackResult is defined in ocstack.h.
156 OCStackResult get(const QueryParamsMap& queryParametersMap, GetCallback attributeHandler);
158 * Function to get the attributes of a resource.
159 * @param queryParametersMap map which can have the query parameter name and value
160 * @param attributeHandler handles callback
161 * The callback function will be invoked with a map of attribute name and values.
162 * The callback function will also have the result from this Get operation
163 * This will have error codes
164 * @param QoS the quality of communication
165 * @return Returns ::OC_STACK_OK on success, some other value upon failure.
166 * @note OCStackResult is defined in ocstack.h.
168 OCStackResult get(const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
169 QualityOfService QoS);
172 * Function to get the attributes of a resource.
174 * @param resourceType resourceType of the resource operate on
175 * @param resourceInterface interface type of the resource to operate on
176 * @param queryParametersMap map which can have the query parameter name and value
177 * @param attributeHandler handles callback
178 * The callback function will be invoked with a map of attribute name and values.
179 * The callback function will be invoked with a list of URIs if 'get' is invoked on a
180 * resource container (list will be empty if not a container)
181 * The callback function will also have the result from this Get operation. This will
183 * @return Returns ::OC_STACK_OK on success, some other value upon failure.
184 * @note OCStackResult is defined in ocstack.h.
186 * Consider resource "a/home" (with link interface and resource type as home) contains links
187 * to "a/kitchen" and "a/room".
188 * -# get("home", Link_Interface, &onGet)
190 * Callback onGet will receive a) Empty attribute map because there are no attributes for
191 * a/home b) list with
192 * full URI of "a/kitchen" and "a/room" resources and their properties c) error code for GET
194 * @note A resource may contain single or multiple resource types. Also, a resource may
195 * contain single or multiple interfaces.
196 * Currently, single GET request is allowed to do operate on single resource type or resource
197 * interface. In future, a single GET
198 * can operate on multiple resource types and interfaces.
199 * @note A client can traverse a tree or graph by doing successive GETs on the returned
200 * resources at a node.
203 OCStackResult get(const std::string& resourceType, const std::string& resourceInterface,
204 const QueryParamsMap& queryParametersMap, GetCallback attributeHandler);
206 * Function to get the attributes of a resource.
208 * @param resourceType resourceType of the resource operate on
209 * @param resourceInterface interface type of the resource to operate on
210 * @param queryParametersMap map which can have the query parameter name and value
211 * @param attributeHandler handles callback
212 * The callback function will be invoked with a map of attribute name and values.
213 * The callback function will be invoked with a list of URIs if 'get' is invoked on a
214 * resource container (list will be empty if not a container)
215 * The callback function will also have the result from this Get operation. This will
217 * @param QoS the quality of communication
218 * @return Returns ::OC_STACK_OK on success, some other value upon failure.
219 * note OCStackResult is defined in ocstack.h.
221 * Consider resource "a/home" (with link interface and resource type as home) contains links
222 * to "a/kitchen" and "a/room".
223 * -# get("home", Link_Interface, &onGet)
225 * Callback onGet will receive a) Empty attribute map because there are no attributes for
226 * a/home b) list with
227 * full URI of "a/kitchen" and "a/room" resources and their properties c) error code for GET
229 * @note A resource may contain single or multiple resource types. Also, a resource may
230 * contain single or multiple interfaces.
231 * Currently, single GET request is allowed to do operate on single resource type or resource
232 * interface. In future, a single GET
233 * can operate on multiple resource types and interfaces.
234 * @note A client can traverse a tree or graph by doing successive GETs on the returned
235 * resources at a node.
238 OCStackResult get(const std::string& resourceType, const std::string& resourceInterface,
239 const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
240 QualityOfService QoS);
243 * Function to set the representation of a resource (via PUT)
245 * @param representation which can either have all the attribute names and values
246 (which will represent entire state of the resource) or a
247 * set of attribute names and values which needs to be modified
248 * The callback function will be invoked with a map of attribute name and values.
249 * The callback function will also have the result from this Put operation
250 * This will have error codes
251 * @param queryParametersMap map which can have the query parameter name and value
252 * @param attributeHandler attribute handler
253 * @return Returns ::OC_STACK_OK on success, some other value upon failure.
254 * @note OCStackResult is defined in ocstack.h.
257 OCStackResult put(const OCRepresentation& representation,
258 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler);
260 * Function to set the representation of a resource (via PUT)
262 * @param representation which can either have all the attribute names and values
263 (which will represent entire state of the resource) or a
264 * set of attribute names and values which needs to be modified
265 * The callback function will be invoked with a map of attribute name and values.
266 * The callback function will also have the result from this Put operation
267 * This will have error codes
268 * @param queryParametersMap map which can have the query parameter name and value
269 * @param attributeHandler attribute handler
270 * @param QoS the quality of communication
271 * @return Returns ::OC_STACK_OK on success, some other value upon failure.
272 * @note OCStackResult is defined in ocstack.h.
275 OCStackResult put(const OCRepresentation& representation,
276 const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
277 QualityOfService QoS);
280 * Function to set the attributes of a resource (via PUT)
282 * @param resourceType resource type of the resource to operate on
283 * @param resourceInterface interface type of the resource to operate on
284 * @param representation representation of the resource
285 * @param queryParametersMap Map which can have the query parameter name and value
286 * @param attributeHandler attribute handler
287 * The callback function will be invoked with a map of attribute name and values.
288 * The callback function will also have the result from this Put operation
289 * This will have error codes.
290 * The Representation parameter maps which can either have all the attribute names
292 * (which will represent entire state of the resource) or a
293 * set of attribute names and values which needs to be modified
294 * @return Returns ::OC_STACK_OK on success, some other value upon failure.
295 * @note OCStackResult is defined in ocstack.h.
298 OCStackResult put(const std::string& resourceType, const std::string& resourceInterface,
299 const OCRepresentation& representation, const QueryParamsMap& queryParametersMap,
300 PutCallback attributeHandler);
302 * Function to set the attributes of a resource (via PUT)
303 * @param resourceType resource type of the resource to operate on
304 * @param resourceInterface interface type of the resource to operate on
305 * @param representation representation of the resource
306 * @param queryParametersMap Map which can have the query parameter name and value
307 * @param attributeHandler attribute handler
308 * The callback function will be invoked with a map of attribute name and values.
309 * The callback function will also have the result from this Put operation
310 * This will have error codes.
311 * The Representation parameter maps which can either have all the attribute names
313 * (which will represent entire state of the resource) or a
314 * set of attribute names and values which needs to be modified
315 * @param QoS the quality of communication
316 * @return Returns ::OC_STACK_OK on success, some other value upon failure.
317 * @note OCStackResult is defined in ocstack.h.
320 OCStackResult put(const std::string& resourceType, const std::string& resourceInterface,
321 const OCRepresentation& representation, const QueryParamsMap& queryParametersMap,
322 PutCallback attributeHandler, QualityOfService QoS);
325 * Function to post on a resource
327 * @param representation which can either have all the attribute names and values
328 * (which will represent entire state of the resource) or a
329 * set of attribute names and values which needs to be modified
330 * The callback function will be invoked with a map of attribute name and values.
331 * The callback function will also have the result from this Put operation
332 * This will have error codes
333 * @param queryParametersMap map which can have the query parameter name and value
334 * @param attributeHandler attribute handler
335 * @return Returns ::OC_STACK_OK on success, some other value upon failure.
336 * @note OCStackResult is defined in ocstack.h.
338 OCStackResult post(const OCRepresentation& representation,
339 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler);
341 * Function to post on a resource
343 * @param representation which can either have all the attribute names and values
344 * (which will represent entire state of the resource) or a
345 * set of attribute names and values which needs to be modified
346 * The callback function will be invoked with a map of attribute name and values.
347 * The callback function will also have the result from this Put operation
348 * This will have error codes
349 * @param queryParametersMap map which can have the query parameter name and value
350 * @param attributeHandler attribute handler
351 * @param QoS the quality of communication
352 * @return Returns ::OC_STACK_OK on success, some other value upon failure.
353 * @note OCStackResult is defined in ocstack.h.
355 OCStackResult post(const OCRepresentation& representation,
356 const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
357 QualityOfService QoS);
360 * Function to post on a resource
362 * @param resourceType resource type of the resource to operate on
363 * @param resourceInterface interface type of the resource to operate on
364 * @param representation representation of the resource
365 * @param queryParametersMap Map which can have the query parameter name and value
366 * @param attributeHandler attribute handler
367 * The callback function will be invoked with a map of attribute name and values.
368 * The callback function will also have the result from this Put operation
369 * This will have error codes.
370 * The Representation parameter maps which can either have all the attribute names
372 * (which will represent entire state of the resource) or a
373 * set of attribute names and values which needs to be modified
374 * @return Returns ::OC_STACK_OK on success, some other value upon failure.
375 * @note OCStackResult is defined in ocstack.h.
378 OCStackResult post(const std::string& resourceType, const std::string& resourceInterface,
379 const OCRepresentation& representation, const QueryParamsMap& queryParametersMap,
380 PostCallback attributeHandler);
382 * Function to post on a resource
384 * @param resourceType resource type of the resource to operate on
385 * @param resourceInterface interface type of the resource to operate on
386 * @param representation representation of the resource
387 * @param queryParametersMap Map which can have the query parameter name and value
388 * @param attributeHandler attribute handler
389 * The callback function will be invoked with a map of attribute name and values.
390 * The callback function will also have the result from this Put operation
391 * This will have error codes.
392 * The Representation parameter maps which can either have all the attribute names
394 * (which will represent entire state of the resource) or a
395 * set of attribute names and values which needs to be modified
396 * @param QoS the quality of communication
397 * @return Returns ::OC_STACK_OK on success, some other value upon failure.
398 * @note OCStackResult is defined in ocstack.h.
401 OCStackResult post(const std::string& resourceType, const std::string& resourceInterface,
402 const OCRepresentation& representation, const QueryParamsMap& queryParametersMap,
403 PostCallback attributeHandler, QualityOfService QoS);
406 * Function to perform DELETE operation
408 * @param deleteHandler handles callback
409 * The callback function will have headerOptions and result from this Delete
410 * operation. This will have error codes
411 * @return Returns ::OC_STACK_OK on success, some other value upon failure.
412 * @note OCStackResult is defined in ocstack.h.
415 OCStackResult deleteResource(DeleteCallback deleteHandler);
416 OCStackResult deleteResource(DeleteCallback deleteHandler, QualityOfService QoS);
419 * Function to set observation on the resource
421 * @param observeType allows the client to specify how it wants to observe.
422 * @param queryParametersMap map which can have the query parameter name and value
423 * @param observeHandler handles callback
424 * The callback function will be invoked with a map of attribute name and values.
425 * The callback function will also have the result from this observe operation
426 * This will have error codes
427 * @return Returns ::OC_STACK_OK on success, some other value upon failure.
428 * @note OCStackResult is defined in ocstack.h.
431 OCStackResult observe(ObserveType observeType, const QueryParamsMap& queryParametersMap,
432 ObserveCallback observeHandler);
434 * Function to set observation on the resource
436 * @param observeType allows the client to specify how it wants to observe.
437 * @param queryParametersMap map which can have the query parameter name and value
438 * @param observeHandler handles callback
439 * The callback function will be invoked with a map of attribute name and values.
440 * The callback function will also have the result from this observe operation
441 * This will have error codes
442 * @param qos the quality of communication
443 * @return Returns ::OC_STACK_OK on success, some other value upon failure.
444 * @note OCStackResult is defined in ocstack.h.
447 OCStackResult observe(ObserveType observeType, const QueryParamsMap& queryParametersMap,
448 ObserveCallback observeHandler, QualityOfService qos);
451 * Function to cancel the observation on the resource
453 * @return Returns ::OC_STACK_OK on success, some other value upon failure.
454 * @note OCStackResult is defined in ocstack.h.
457 OCStackResult cancelObserve();
458 OCStackResult cancelObserve(QualityOfService qos);
461 * Function to set header information.
462 * @param headerOptions std::vector where header information(header optionID and optionData
465 * @note Once the headers information is set, it will be applicable to GET, PUT and observe
467 * setHeaderOptions can be used multiple times if headers need to be modifed by the client.
468 * Latest headers will be used to send in the request. <br>
469 * @note Initial support is only for two headers. If headerMap consists of more than two
470 * header options, they will be ignored. <br>
471 * Use unsetHeaderOptions API to clear the header information.
473 void setHeaderOptions(const HeaderOptions& headerOptions);
476 * Function to unset header options.
478 void unsetHeaderOptions();
481 * Function to get the host address of this resource
482 * @return std::string host address
483 * @note This might or might not be exposed in future due to security concerns
485 std::string host() const;
488 * Function to get the URI for this resource
489 * @return std::string resource URI
491 std::string uri() const;
494 * Function to get the connectivity type of this resource
495 * @return enum connectivity type (flags and adapter)
497 OCConnectivityType connectivityType() const;
500 * Function to provide ability to check if this resource is observable or not
501 * @return bool true indicates resource is observable; false indicates resource is
504 bool isObservable() const;
508 * Function to provide ability to check if this resource is publisher or not
509 * @return bool true indicates resource is publisher; false indicates resource is
512 bool isPublish() const;
516 * Function to get the list of resource types
517 * @return vector of resource types
519 std::vector<std::string> getResourceTypes() const;
522 * Function to get the list of resource interfaces
523 * @return vector of resource interface
525 std::vector<std::string> getResourceInterfaces(void) const;
527 // TODO-CA Revisit this since we are exposing two identifiers
529 * Function to get a unique identifier for this
530 * resource across network interfaces. This will
531 * be guaranteed unique for every resource-per-server
532 * independent of how this was discovered.
533 * @return OCResourceIdentifier object, which can
534 * be used for all comparison and hashing.
536 OCResourceIdentifier uniqueIdentifier() const;
539 * Function to get a string representation of the resource's server ID.
540 * This is unique per- server independent on how it was discovered.
541 * @note The format of the return value is subject to change and will
542 * likely change both in size and contents in the future.
544 std::string sid() const;
548 * Function to discovery Topics from MQ Broker.
550 * @param queryParametersMap map which can have the query parameter name and value
551 * @param attributeHandler handles callback
553 * @return Returns ::OC_STACK_OK on success, some other value upon failure.
554 * @note OCStackResult is defined in ocstack.h.
557 OCStackResult discoveryMQTopics(const QueryParamsMap& queryParametersMap,
558 FindCallback attributeHandler);
560 * Function to create Topic into MQ Broker.
561 * SubTopic is also created through this method.
563 * @param rep representation of the topic
564 * @param topicUri new uri of the topic which want to create
565 * @param queryParametersMap map which can have the query parameter name and value
566 * @param attributeHandler handles callback
568 * @return Returns ::OC_STACK_OK on success, some other value upon failure.
569 * @note OCStackResult is defined in ocstack.h.
572 OCStackResult createMQTopic(const OCRepresentation& rep,
573 const std::string& topicUri,
574 const QueryParamsMap& queryParametersMap,
575 MQCreateTopicCallback attributeHandler);
579 * Function to subscribe Topic to MQ Broker.
581 * @param observeType allows the client to specify how it wants to observe.
582 * @param queryParametersMap map which can have the query parameter name and value
583 * @param observeHandler handles callback
585 * @return Returns ::OC_STACK_OK on success, some other value upon failure.
586 * @note OCStackResult is defined in ocstack.h.
589 OCStackResult subscribeMQTopic(ObserveType observeType,
590 const QueryParamsMap& queryParametersMap,
591 ObserveCallback observeHandler);
594 * Function to unsubscribe Topic to MQ Broker.
596 * @return Returns ::OC_STACK_OK on success, some other value upon failure.
597 * @note OCStackResult is defined in ocstack.h.
600 OCStackResult unsubscribeMQTopic();
603 * Function to request publish to MQ publisher.
604 * Publisher can confirm the request message as key:"req_pub" and value:"true".
606 * @param queryParametersMap map which can have the query parameter name and value
607 * @param attributeHandler handles callback
609 * @return Returns ::OC_STACK_OK on success, some other value upon failure.
610 * @note OCStackResult is defined in ocstack.h.
613 OCStackResult requestMQPublish(const QueryParamsMap& queryParametersMap,
614 PostCallback attributeHandler);
618 * Function to publish Topic information into MQ Broker.
620 * @param rep representation of the topic
621 * @param queryParametersMap map which can have the query parameter name and value
622 * @param attributeHandler handles callback
624 * @return Returns ::OC_STACK_OK on success, some other value upon failure.
625 * @note OCStackResult is defined in ocstack.h.
628 OCStackResult publishMQTopic(const OCRepresentation& rep,
629 const QueryParamsMap& queryParametersMap,
630 PostCallback attributeHandler);
632 // overloaded operators allow for putting into a 'set'
633 // the uniqueidentifier allows for putting into a hash
634 bool operator==(const OCResource &other) const;
636 bool operator!=(const OCResource &other) const;
638 bool operator<(const OCResource &other) const;
640 bool operator>(const OCResource &other) const;
642 bool operator<=(const OCResource &other) const;
644 bool operator>=(const OCResource &other) const;
647 void setHost(const std::string& host);
648 std::weak_ptr<IClientWrapper> m_clientWrapper;
650 OCResourceIdentifier m_resourceId;
652 bool m_useHostString;
655 std::vector<std::string> m_resourceTypes;
656 std::vector<std::string> m_interfaces;
657 std::vector<std::string> m_children;
658 OCDoHandle m_observeHandle;
659 HeaderOptions m_headerOptions;
662 OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
663 const OCDevAddr& devAddr, const std::string& uri,
664 const std::string& serverId, uint8_t property,
665 const std::vector<std::string>& resourceTypes,
666 const std::vector<std::string>& interfaces);
668 OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
669 const std::string& host, const std::string& uri,
670 const std::string& serverId,
671 OCConnectivityType connectivityType, uint8_t property,
672 const std::vector<std::string>& resourceTypes,
673 const std::vector<std::string>& interfaces);
678 #endif // OC_RESOURCE_H