1 //******************************************************************
3 // Copyright 2014 Samsung Electronics 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
25 * ThingsConfiguration.
28 #ifndef __OC_THINGSCONFIGURATION__
29 #define __OC_THINGSCONFIGURATION__
35 #include "GroupManager.h"
36 #include "OCPlatform.h"
43 /// Declearation of Configuation Callback funtion type
44 typedef std::function<
45 void(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
46 > ConfigurationCallback;
48 typedef std::string ConfigurationName;
49 typedef std::string ConfigurationValue;
53 * The following class is used as a item stacking in request queue. The class stores a request
54 * and referential information (e.g., a configuration name, a target resource object, a callback
55 * function passed from the applications, and a update value). When the function for updating/
56 * getting configuration value is called from applications, this class instance is created and
57 * stored in the request queue. The queue is maintained in a std::map structure so if desiring
58 * to find a specific request, you can find it by querying a configuration name.
60 class ConfigurationRequestEntry
63 ConfigurationRequestEntry(std::string ID, ConfigurationCallback callback,
64 std::shared_ptr< OCResource > resource, std::string updateVal);
66 // Configuration Name (used in key value in std::map structure)
67 // e.g., time, network, security, and so on
69 // Reference callback pointer
70 ConfigurationCallback m_callback;
71 // Reference resource object
72 std::shared_ptr< OCResource > m_resource;
73 // Update value only used for configuration update
74 std::string m_updateVal;
79 * The following class is used to store providing configuration name and its relevant
80 * information. The relevant information includes a brief description, uri, and attribute key.
81 * Note that a developer only specifies a configuration name, not URI nor attribute key, to
82 * update/get a value to a remote. Thus, using configuration name, we convert it to more
83 * specific information (i.e. uri and attribute key) to send a request. This class is reponsible
84 * to storing these information.
86 class ConfigurationUnitInfo
91 std::string m_attribute;
94 ConfigurationUnitInfo(std::string name, std::string attribute, std::string uri);
96 // If a developer wants to know a list of configuration names, gives it in JSON format.
97 std::string getJSON();
100 #define NUMCONFUNIT 6
101 typedef std::string ConfigurationName;
102 typedef std::string ConfigurationValue;
105 * @class ThingsConfiguration
107 * There are two main usages of this class: (1) On a server side, bootstrapping requisite
108 * information (i.e. system configuration parameters) from a bootstrap server to access other
109 * IoT services, (2) On a client side, getting/updating the system configuration parameters
110 * from/to multiple remote things.
112 class ThingsConfiguration
116 * Constructor for ThingsConfiguration. Constructs a new ThingsConfiguration
118 ThingsConfiguration(void);
123 ~ThingsConfiguration(void);
125 static ThingsConfiguration *thingsConfigurationInstance;
126 static ThingsConfiguration* getInstance();
129 void deleteInstance();
130 void setGroupManager(GroupManager *groupmanager);
133 * API for updating configuration value of multiple things of a target group or a single
135 * Callback is called when a response arrives.
136 * Before using the below function, a developer should acquire a resource pointer of
137 * (collection) resource that he want to send a request by calling findResource() function
138 * provided in OCPlatform. And he should also notice a "Configuration Name" term which
139 * represents a nickname of a target attribute of a resource that he wants to update.
140 * The base motivation to introduce the term is to avoid a usage of URI to access a resource
141 * from a developer. Thus, a developer should know which configuration names are supported
142 * by Things Configuration class and what the configuration name means.
143 * To get a list of supported configuration names, use getListOfSupportedConfigurationUnits(
144 * ) function, which provides the list in JSON format.
145 * NOTICE: A series of callback functions is called from updateConfigurations() function:
146 * (1) For a collection resource
147 * updateConfiguration()->onDeleteActionSet()->onGetChildInfoForUpdate()->onCreateActionSet(
148 * )->...(CoAP msg. is transmitted)->OnExecuteForGroupAction()->callback function in APP.
149 * (2) For a simple resource
150 * updateConfiguration()->...(CoAP msg. is transmitted)->OnPut()->callback function in APP.
152 * @param resource - resource pointer representing the target group or the single thing.
153 * @param configurations - ConfigurationUnit: an attribute key of target resource
154 * (e.g., loc, st, c, r)
155 * Value : a value to be updated
156 * @param callback - callback.
158 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
160 * NOTE: OCStackResult is defined in ocstack.h.
162 OCStackResult updateConfigurations(std::shared_ptr< OCResource > resource,
163 std::map< ConfigurationName, ConfigurationValue > configurations,
164 ConfigurationCallback callback);
167 * API for getting configuration value of multiple things of a target group or a single
169 * Callback is called when a response arrives.
170 * NOTICE: A series of callback functions is called from getConfigurations() function:
171 * (1) For a collection resource
172 * getConfigurations()->onGetChildInfoForGet()->...(CoAP msg. is transmitted)
173 * ->callback function in APP.
174 * (2) For a simple resource
175 * getConfigurations()->...(CoAP msg. is transmitted)->onGet()->callback function in APP.
177 * @param resource - resource pointer representing the target group or the single thing.
178 * @param configurations - ConfigurationUnit: an attribute key of target resource.
179 * @param callback - callback.
181 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
183 * NOTE: OCStackResult is defined in ocstack.h.
185 OCStackResult getConfigurations(std::shared_ptr< OCResource > resource,
186 std::vector< ConfigurationName > configurations, ConfigurationCallback callback);
189 * API to show a list of supported configuration units (configurable parameters)
190 * Callback call when a response arrives.
192 * @return the list in JSON format
194 std::string getListOfSupportedConfigurationUnits();
197 * API for bootstrapping functionality. Find a bootstrap server and get configuration
198 * information from the bootstrap server. With the information, make a configuration
201 * @param callback - callback.
203 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
205 * NOTE: OCStackResult is defined in ocstack.h.
207 OCStackResult doBootstrap(ConfigurationCallback callback);
211 GroupManager *g_groupmanager;
213 std::vector< ConfigurationUnitInfo > ConfigurationUnitTable;
215 void onExecuteForGroupAction(const HeaderOptions& headerOptions,
216 const OCRepresentation& rep, const int eCode, std::string conf);
217 void onGetChildInfoForUpdate(const HeaderOptions& headerOptions,
218 const OCRepresentation& rep, const int eCode, std::string conf);
219 void onGetChildInfoForGet(const HeaderOptions& headerOptions, const OCRepresentation& rep,
220 const int eCode, std::string conf);
221 void onCreateActionSet(const HeaderOptions& headerOptions, const OCRepresentation& rep,
222 const int eCode, std::string conf);
223 void onGetActionSet(const HeaderOptions& headerOptions, const OCRepresentation& rep,
224 const int eCode, std::string conf);
225 void onDeleteActionSet(const HeaderOptions& headerOptions, const OCRepresentation& rep,
226 const int eCode, std::string conf);
227 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode,
229 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode,
231 static void onFoundBootstrapServer(std::vector< std::shared_ptr< OCResource > > resources);
232 static void onGetBootstrapInformation(const HeaderOptions& headerOptions,
233 const OCRepresentation& rep, const int eCode);
235 std::shared_ptr< OCResource > getResource(std::string conf);
236 ConfigurationCallback getCallback(std::string conf);
237 std::string getUpdateVal(std::string conf);
238 std::string getAttributeByConfigurationName(ConfigurationName name);
239 std::string getUriByConfigurationName(ConfigurationName name);
241 std::string getHostFromURI(std::string oldUri);
243 bool isSimpleResource(std::shared_ptr< OCResource > resource);
244 bool hasBatchInterface(std::shared_ptr< OCResource > resource);
248 #endif /* __OC_THINGSCONFIGURATION__*/