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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 /// @file OCResource.h
23 /// @brief This file contains the declaration of classes and its members related to
26 #ifndef __OCRESOURCE_H
27 #define __OCRESOURCE_H
33 #include <boost/property_tree/ptree.hpp>
34 #include <boost/property_tree/json_parser.hpp>
37 #include <ResourceInitException.h>
38 #include <IClientWrapper.h>
39 #include <InProcClientWrapper.h>
40 #include <OCRepresentation.h>
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
54 friend class OCPlatform;
55 friend class InProcClientWrapper;
57 typedef std::shared_ptr<OCResource> Ptr;
62 virtual ~OCResource(void);
65 * Function to get the attributes of a resource.
66 * @param queryParametersMap map which can have the query parameter name and value
67 * @param attributeHandler handles callback
68 * The callback function will be invoked with a map of attribute name and values.
69 * The callback function will also have the result from this Get operation
70 * This will have error codes
71 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
72 * NOTE: OCStackResult is defined in ocstack.h.
74 OCStackResult get(const QueryParamsMap& queryParametersMap, std::function<void(const OCRepresentation, const int)> attributeHandler);
77 * Function to get the attributes of a resource.
79 * @param resourceType resourceType of the resource operate on
80 * @param resourceInterface interface type of the resource to operate on
81 * @param queryParametersMap map which can have the query parameter name and value
82 * @param attributeHandler handles callback
83 * The callback function will be invoked with a map of attribute name and values.
84 * The callback function will be invoked with a list of URIs if 'get' is invoked on a resource container
85 * (list will be empty if not a container)
86 * The callback function will also have the result from this Get operation. This will have error codes
87 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success. <br>
88 * NOTE: OCStackResult is defined in ocstack.h.<br>
90 * Consider resource "a/home" (with link interface and resource type as home) contains links to "a/kitchen" and "a/room".
91 * Step 1: get("home", Link_Interface, &onGet)<br>
92 * Callback onGet will receive a) Empty attribute map because there are no attributes for a/home b) list with
93 * full URI of "a/kitchen" and "a/room" resources and their properties c) error code for GET operation<br>
94 * NOTE: A resource may contain single or multiple resource types. Also, a resource may contain single or multiple interfaces.<br>
95 * Currently, single GET request is allowed to do operate on single resource type or resource interface. In future, a single GET <br>
96 * can operate on multiple resource types and interfaces. <br>
97 * NOTE: A client can traverse a tree or graph by doing successive GETs on the returned resources at a node.<br>
98 * TODO: Implementation
100 OCStackResult get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
101 std::function<void(const OCRepresentation& rep, const std::vector<std::string>& resourceUriList, const int& errorCode)> attributeHandler)
102 { return OC_STACK_OK; }
105 * Function to set the attributes of a resource (via PUT)
106 * @param attributeMap Map which can either have all the attribute names and values
107 (which will represent entire state of the resource) or a
108 * set of attribute names and values which needs to be modified
109 * The callback function will be invoked with a map of attribute name and values.
110 * The callback function will also have the result from this Put operation
111 * This will have error codes
112 * @param queryParametersMap map which can have the query parameter name and value
113 * @param attributeHandler attribute handler
114 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
115 * NOTE: OCStackResult is defined in ocstack.h.
117 OCStackResult put(const OCRepresentation& attributeMap, const QueryParamsMap& queryParametersMap,
118 std::function< void(const OCRepresentation,const int)> attributeHandler);
121 * Function to set the attributes of a resource (via PUT)
122 * @param resourceType resource type of the resource to operate on
123 * @param resourceInterface interface type of the resource to operate on
124 * @param attributeMap attribute map
125 * @param queryParametersMap Map which can have the query parameter name and value
126 * @param attributeHandler attribute handler
127 * The callback function will be invoked with a map of attribute name and values.
128 * The callback function will also have the result from this Put operation
129 * This will have error codes.
130 * The AttributeMap parameter maps which can either have all the attribute names and values
131 * (which will represent entire state of the resource) or a
132 * set of attribute names and values which needs to be modified
133 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success. <br>
134 * NOTE: OCStackResult is defined in ocstack.h. <br>
135 * TODO: consider to input hrefs for resource collection
136 * TODO: Implementation
138 OCStackResult put(const std::string& resourceType, const std::string& resourceInterface,
139 const OCRepresentation& attributeMap, const QueryParamsMap& queryParametersMap,
140 std::function< void(const OCRepresentation&,const int&)> attributeHandler) { return OC_STACK_OK; }
143 * Function to set observation on the resource
144 * @param observeType allows the client to specify how it wants to observe.
145 * @param queryParametersMap map which can have the query parameter name and value
146 * @param observeHandler handles callback
147 * The callback function will be invoked with a map of attribute name and values.
148 * The callback function will also have the result from this observe operation
149 * This will have error codes
150 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
151 * NOTE: OCStackResult is defined in ocstack.h.
153 OCStackResult observe(ObserveType observeType, const QueryParamsMap& queryParametersMap,
154 std::function<void(const OCRepresentation&, const int&, const int&)> observeHandler);
157 * Function to cancel the observation on the resource
158 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
159 * NOTE: OCStackResult is defined in ocstack.h.
161 OCStackResult cancelObserve();
164 * Function to get the host address of this resource
165 * @return std::string host address
166 * NOTE: This might or might not be exposed in future due to security concerns
168 std::string host() const;
171 * Function to get the URI for this resource
172 * @return std::string resource URI
174 std::string uri() const;
177 * Function to provide ability to check if this resource is observable or not
178 * @return bool true indicates resource is observable; false indicates resource is
181 bool isObservable() const;
184 * Function to get the list of resource types
185 * @return vector of resource types
187 std::vector<std::string> getResourceTypes() const
189 return m_resourceTypes;
193 * Function to get the list of resource interfaces
194 * @return vector of resource interface
196 std::vector<std::string> getResourceInterfaces(void) const
202 std::weak_ptr<IClientWrapper> m_clientWrapper;
207 std::vector<std::string> m_resourceTypes;
208 std::vector<std::string> m_interfaces;
209 std::vector<std::string> m_children;
210 OCDoHandle m_observeHandle;
213 OCResource(std::weak_ptr<IClientWrapper> clientWrapper, const std::string& host, const std::string& uri,
214 bool observable, const std::vector<std::string>& resourceTypes, const std::vector<std::string>& interfaces);
219 #endif //__OCRESOURCE_H