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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 /// @file ThingsConfiguration.h
23 /// @brief This file contains the declaration of classes and its members related to
24 /// ThingsConfiguration.
26 #ifndef __OC_THINGSCONFIGURATION__
27 #define __OC_THINGSCONFIGURATION__
33 #include "GroupManager.h"
34 #include "OCPlatform.h"
39 /// Declearation of Configuation Callback funtion type
40 typedef std::function<
41 void(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode) > ConfigurationCallback;
43 typedef std::string ConfigurationName;
44 typedef std::string ConfigurationValue;
48 * The following class is used as a item stacking in request queue. The class stores a request and
49 * referential information (e.g., a configuration name, a target resource object, a callback functi-
50 * on passed from the applications, and a update value). When the function for updating/getting
51 * configuration value is called from applications, this class instance is created and stored in the
52 * request queue. The queue is maintained in a std::map structure so if desiring to find a specific
53 * request, you can find it by querying a configuration name.
55 class ConfigurationRequestEntry
58 ConfigurationRequestEntry(std::string ID, ConfigurationCallback callback,
59 std::shared_ptr< OCResource > resource, std::string updateVal) :
60 m_ID(ID), m_callback(callback), m_resource(resource), m_updateVal(updateVal)
65 // Configuration Name (used in key value in std::map structure)
66 // e.g., time, network, security, and so on
69 // Reference callback pointer
70 ConfigurationCallback m_callback;
72 // Reference resource object
73 std::shared_ptr< OCResource > m_resource;
75 // Update value only used for configuration update
76 std::string m_updateVal;
81 * The following class is used to store providing configuration name and its relevant information
82 * The relevant information includes a brief description, uri, and attribute key.
83 * Note that a developer only specifies a configuration name, not URI nor attribute key, to
84 * update/get a value to a remote. Thus, using configuration name, we convert it to more specific
85 * information (i.e. uri and attribute key) to send a request. This class is reponsible to storing
88 class ConfigurationUnitInfo
93 std::string m_description;
95 std::string m_attribute;
97 ConfigurationUnitInfo(std::string name, std::string description, std::string uri,
98 std::string attribute) :
99 m_name(name), m_description(description), m_uri(uri), m_attribute(attribute)
104 // If a developer wants to know a list of configuration names, gives it in JSON format.
105 std::string getJSON()
109 res = "{\"name\":\"" + m_name + "\",\"description\":\"" + m_description + "\"}";
115 #define NUMCONFUNIT 6
116 typedef std::string ConfigurationName;
117 typedef std::string ConfigurationValue;
119 class ThingsConfiguration
123 * Constructor for ThingsConfiguration. Constructs a new ThingsConfiguration
125 ThingsConfiguration(void)
127 ConfigurationUnitInfo unit[] =
129 { "installedlocation", "the semantic location at a specific area (e.g., living room)",
130 "/oic/con", "installedLocation" },
131 { "time", "Resource for system time information including time zones etc.", "/oic/time",
133 { "network", "Resource for network information", "/oic/net", "address" },
134 { "security", "Resource for security information (credentials, access control list etc.)",
135 "/oic/sec", "mode" },
136 { "setattr1", "attribute 1 of Set Resource", "/oic/customset", "attr1" },
137 { "getfactoryset", "get all default configuration value", "/factoryset/oic/con", "" } };
139 for (int i = 0; i < NUMCONFUNIT; i++)
140 ConfigurationUnitTable.push_back(unit[i]);
147 ~ThingsConfiguration(void)
152 static ThingsConfiguration *thingsConfigurationInstance;
153 static ThingsConfiguration* getInstance();
154 void deleteInstance();
156 void setGroupManager(GroupManager *groupmanager)
158 g_groupmanager = groupmanager;
164 * API for updating configuration value of multiple things of a target group or a single thing.
165 * Callback is called when a response arrives.
166 * Before using the below function, a developer should acquire a resource pointer of
167 * (collection) resource that he want to send a request by calling findResource() function
168 * provided in OCPlatform. And he should also notice a "Configuration Name" term which
169 * represents a nickname of a target attribute of a resource that he wants to update.
170 * The base motivation to introduce the term is to avoid a usage of URI to access a resource
171 * from a developer. Thus, a developer should know which configuration names are supported
172 * by Things Configuration class and what the configuration name means.
173 * To get a list of supported configuration names, use getListOfSupportedConfigurationUnits()
174 * function, which provides the list in JSON format.
175 * NOTICE: A series of callback functions is called from updateConfigurations() function:
176 * (1) For a collection resource
177 * updateConfiguration()->onDeleteActionSet()->onGetChildInfoForUpdate()->onCreateActionSet()
178 * ->...(CoAP msg. is transmitted)->OnExecuteForGroupAction()->callback function in APP.
179 * (2) For a simple resource
180 * updateConfiguration()->...(CoAP msg. is transmitted)->OnPut()->callback function in APP.
182 * @param resource - resource pointer representing the target group or the single thing.
183 * @param configurations - ConfigurationUnit: a nickname of attribute of target resource
184 * (e.g., installedlocation, currency, (IP)address)
185 * Value : a value to be updated
186 * @param callback - callback.
188 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
190 * NOTE: OCStackResult is defined in ocstack.h.
192 OCStackResult updateConfigurations(std::shared_ptr< OCResource > resource,
193 std::map< ConfigurationName, ConfigurationValue > configurations,
194 ConfigurationCallback callback);
197 * API for getting configuration value of multiple things of a target group or a single thing.
198 * Callback is called when a response arrives.
199 * NOTICE: A series of callback functions is called from getConfigurations() function:
200 * (1) For a collection resource
201 * getConfigurations()->onGetChildInfoForGet()->...(CoAP msg. is transmitted)
202 * ->callback function in APP.
203 * (2) For a simple resource
204 * getConfigurations()->...(CoAP msg. is transmitted)->onGet()->callback function in APP.
206 * @param resource - resource pointer representing the target group or the single thing.
207 * @param configurations - ConfigurationUnit: a nickname of attribute of target resource.
208 * @param callback - callback.
210 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
212 * NOTE: OCStackResult is defined in ocstack.h.
214 OCStackResult getConfigurations(std::shared_ptr< OCResource > resource,
215 std::vector< ConfigurationName > configurations, ConfigurationCallback callback);
218 * API to show a list of supported configuration units (configurable parameters)
219 * Callback call when a response arrives.
221 * @return the list in JSON format
223 std::string getListOfSupportedConfigurationUnits();
226 * API for bootstrapping functionality. Find a bootstrap server and get configuration
227 * information from the bootstrap server. With the information, make a configuration resource.
229 * @param callback - callback.
231 * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
233 * NOTE: OCStackResult is defined in ocstack.h.
235 OCStackResult doBootstrap(ConfigurationCallback callback);
239 GroupManager *g_groupmanager;
241 std::vector< ConfigurationUnitInfo > ConfigurationUnitTable;
243 void onExecuteForGroupAction(const HeaderOptions& headerOptions, const OCRepresentation& rep,
244 const int eCode, std::string conf);
245 void onGetChildInfoForUpdate(const HeaderOptions& headerOptions, const OCRepresentation& rep,
246 const int eCode, std::string conf);
247 void onGetChildInfoForGet(const HeaderOptions& headerOptions, const OCRepresentation& rep,
248 const int eCode, std::string conf);
249 void onCreateActionSet(const HeaderOptions& headerOptions, const OCRepresentation& rep,
250 const int eCode, std::string conf);
251 void onGetActionSet(const HeaderOptions& headerOptions, const OCRepresentation& rep,
252 const int eCode, std::string conf);
253 void onDeleteActionSet(const HeaderOptions& headerOptions, const OCRepresentation& rep,
254 const int eCode, std::string conf);
255 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode,
257 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode,
259 static void onFoundBootstrapServer(std::vector< std::shared_ptr< OCResource > > resources);
260 static void onGetBootstrapInformation(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
262 std::shared_ptr< OCResource > getResource(std::string conf);
263 ConfigurationCallback getCallback(std::string conf);
264 std::string getUpdateVal(std::string conf);
265 std::string getAttributeByConfigurationName(ConfigurationName name);
266 std::string getUriByConfigurationName(ConfigurationName name);
268 std::string getHostFromURI(std::string oldUri);
270 bool isSimpleResource(std::shared_ptr< OCResource > resource);
274 #endif /* __OC_THINGSCONFIGURATION__*/