1 //******************************************************************
3 // Copyright 2014 Intel Corporation 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>
44 * @brief OCResource represents an OC resource. A resource could be a light controller,
45 * temperature sensor, smoke detector, etc. A resource comes with a well-defined
46 * contract or interface onto which you can perform different operations, such as
47 * turning on the light, getting the current temperature or subscribing for event
48 * notifications from the smoke detector. A resource can be composed of one or
53 friend class OCPlatform;
54 friend class InProcClientWrapper;
56 typedef std::shared_ptr<OCResource> Ptr;
61 virtual ~OCResource(void);
64 * Function to get the attributes of a resource.
65 * @param attributeHandler handles callback
66 * The callback function will be invoked with a map of attribute name and values.
67 * The callback function will also have the result from this Get operation
68 * This will have error codes
69 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
70 * NOTE: OCStackResult is defined in ocstack.h.
72 OCStackResult get(std::function<void(const AttributeMap, const int)> attributeHandler);
75 * Function to get the attributes of a resource.
77 * @param resourceType resourceType of the resource operate on
78 * @param resourceInterface interface type of the resource to operate on
79 * @param attributeHandler handles callback
80 * The callback function will be invoked with a map of attribute name and values.
81 * The callback function will be invoked with a list of URIs if 'get' is invoked on a resource container
82 * (list will be empty if not a container)
83 * The callback function will also have the result from this Get operation. This will have error codes
84 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success. <br>
85 * NOTE: OCStackResult is defined in ocstack.h.<br>
87 * Consider resource "a/home" (with link interface and resource type as home) contains links to "a/kitchen" and "a/room".
88 * Step 1: get("home", Link_Interface, &onGet)<br>
89 * Callback onGet will receive a) Empty attribute map because there are no attributes for a/home b) list with
90 * full URI of "a/kitchen" and "a/room" resources and their properties c) error code for GET operation<br>
91 * NOTE: A resource may contain single or multiple resource types. Also, a resource may contain single or multiple interfaces.<br>
92 * Currently, single GET request is allowed to do operate on single resource type or resource interface. In future, a single GET <br>
93 * can operate on multiple resource types and interfaces. <br>
94 * NOTE: A client can traverse a tree or graph by doing successive GETs on the returned resources at a node.<br>
95 * TODO: Implementation
97 OCStackResult get(const std::string& resourceType, const std::string& resourceInterface,
98 std::function<void(const AttributeMap& attributeMap, const std::vector<std::string>& resourceUriList, const int& errorCode)> attributeHandler) { return OC_STACK_OK; }
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.
113 OCStackResult put(const AttributeMap& attributeMap, const QueryParamsMap& queryParametersMap, std::function< void(const AttributeMap,const int)> attributeHandler);
116 * Function to set the attributes of a resource (via PUT)
117 * @param resourceType resource type of the resource to operate on
118 * @param resourceInterface interface type of the resource to operate on
119 * @param attributeMap attribute map
120 * @param queryParametersMap Map which can have the query parameter name and value
121 * @param attributeHandler attribute handler
122 * The callback function will be invoked with a map of attribute name and values.
123 * The callback function will also have the result from this Put operation
124 * This will have error codes.
125 * The AttributeMap parameter maps which can either have all the attribute names and values
126 * (which will represent entire state of the resource) or a
127 * set of attribute names and values which needs to be modified
128 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success. <br>
129 * NOTE: OCStackResult is defined in ocstack.h. <br>
130 * TODO: consider to input hrefs for resource collection
131 * TODO: Implementation
133 OCStackResult put(const std::string& resourceType, const std::string& resourceInterface,
134 const AttributeMap& attributeMap, const QueryParamsMap& queryParametersMap, std::function< void(const AttributeMap&,const int&)> attributeHandler) { return OC_STACK_OK; }
137 * Function to set observation on the resource
138 * @param observeType allows the client to specify how it wants to observe.
139 * @param observeHandler handles callback
140 * The callback function will be invoked with a map of attribute name and values.
141 * The callback function will also have the result from this observe operation
142 * This will have error codes
143 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
144 * NOTE: OCStackResult is defined in ocstack.h.
146 OCStackResult observe(ObserveType observeType, std::function<void(const AttributeMap&, const int&, const int&)> observeHandler);
149 * Function to cancel the observation on the resource
150 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
151 * NOTE: OCStackResult is defined in ocstack.h.
153 OCStackResult cancelObserve();
156 * Function to get the host address of this resource
157 * @return std::string host address
158 * NOTE: This might or might not be exposed in future due to security concerns
160 std::string host() const;
163 * Function to get the URI for this resource
164 * @return std::string resource URI
166 std::string uri() const;
169 * Function to provide ability to check if this resource is observable or not
170 * @return bool true indicates resource is observable; false indicates resource is
173 bool isObservable() const;
175 // bool whether this is a collection type, and will have children that can be queried
176 //bool isCollection() const {return m_isCollection;}
177 // a collection of the resource-type names
178 //const std::vector<std::string> resourceTypes() const {return m_resourceTypes;}
179 // a collection of the interfaces supported by this resource
180 //const std::vector<std::string> interfaces() const { return m_interfaces;}
181 // a collection of property objects that allow calling of properties on this Resource
182 //const std::vector<std::string> properties() const { return m_properties;}
183 // a collection of the names of the children of this resource, assuming it supports a collection interface
184 //const std::vector<std::string> children() const {return m_children;}
186 /*void post(const AttributeMap&, std::function<void(const int&)> attributeHandler);
188 NOTE: dont expose the host ip .. so some kind of handle is required
189 static OCResource::Ptr getResource(const std::string& host, const std::string& resourceURI, const std::string& resourceName,
190 const std::string interfaceName, bool observerable);*/
194 IClientWrapper::Ptr m_clientWrapper;
199 std::vector<std::string> m_resourceTypes;
200 std::vector<std::string> m_interfaces;
201 std::vector<std::string> m_children;
202 AttributeMap m_attributeMap;
203 OCDoHandle m_observeHandle;
206 OCResource(IClientWrapper::Ptr clientWrapper, const std::string& host, const std::string& uri,
207 bool observable, const std::vector<std::string>& resourceTypes, const std::vector<std::string>& interfaces);
212 #endif //__OCRESOURCE_H