Removed execute permissions from non-executable files.
[platform/upstream/iotivity.git] / service / things-manager / sdk / src / ThingsConfiguration.h
1 //******************************************************************
2 //
3 // Copyright 2014 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 /// @file   ThingsConfiguration.h
22
23 /// @brief  This file contains the declaration of classes and its members related to
24 ///         ThingsConfiguration.
25
26 #ifndef __OC_THINGSCONFIGURATION__
27 #define __OC_THINGSCONFIGURATION__
28
29 #include <string>
30 #include <vector>
31 #include <map>
32 #include <cstdlib>
33 #include "GroupManager.h"
34 #include "OCPlatform.h"
35 #include "OCApi.h"
36
37 using namespace OC;
38
39 /// Declearation of Configuation Callback funtion type
40 typedef std::function<
41         void(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode) > ConfigurationCallback;
42
43 typedef std::string ConfigurationName;
44 typedef std::string ConfigurationValue;
45
46 /**
47  * @brief
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.
54  */
55 class ConfigurationRequestEntry
56 {
57 public:
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)
61     {
62     }
63     ;
64
65     // Configuration Name (used in key value in std::map structure)
66     // e.g., time, network, security, and so on
67     std::string m_ID;
68
69     // Reference callback pointer
70     ConfigurationCallback m_callback;
71
72     // Reference resource object
73     std::shared_ptr< OCResource > m_resource;
74
75     // Update value only used for configuration update
76     std::string m_updateVal;
77 };
78
79 /**
80  * @brief
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
86  * these information.
87  */
88 class ConfigurationUnitInfo
89 {
90 public:
91
92     std::string m_name;
93     std::string m_description;
94     std::string m_uri;
95     std::string m_attribute;
96
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)
100     {
101     }
102     ;
103
104     // If a developer wants to know a list of configuration names, gives it in JSON format.
105     std::string getJSON()
106     {
107         std::string res;
108
109         res = "{\"name\":\"" + m_name + "\",\"description\":\"" + m_description + "\"}";
110
111         return res;
112     }
113 };
114
115 #define NUMCONFUNIT 6
116 typedef std::string ConfigurationName;
117 typedef std::string ConfigurationValue;
118
119 class ThingsConfiguration
120 {
121 public:
122     /**
123      * Constructor for ThingsConfiguration. Constructs a new ThingsConfiguration
124      */
125     ThingsConfiguration(void)
126     {
127         ConfigurationUnitInfo unit[] =
128         {
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",
132                 "currentTime" },
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", "" } };
138
139         for (int i = 0; i < NUMCONFUNIT; i++)
140             ConfigurationUnitTable.push_back(unit[i]);
141     }
142     ;
143
144     /**
145      * Virtual destructor
146      */
147     ~ThingsConfiguration(void)
148     {
149     }
150     ;
151
152     static ThingsConfiguration *thingsConfigurationInstance;
153     static ThingsConfiguration* getInstance();
154     void deleteInstance();
155
156     void setGroupManager(GroupManager *groupmanager)
157     {
158         g_groupmanager = groupmanager;
159     }
160     ;
161
162     /**
163      * @brief
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.
181      *
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.
187      *
188      * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
189      *
190      * NOTE: OCStackResult is defined in ocstack.h.
191      */
192     OCStackResult updateConfigurations(std::shared_ptr< OCResource > resource,
193             std::map< ConfigurationName, ConfigurationValue > configurations,
194             ConfigurationCallback callback);
195
196     /**
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.
205      *
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.
209      *
210      * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
211      *
212      * NOTE: OCStackResult is defined in ocstack.h.
213      */
214     OCStackResult getConfigurations(std::shared_ptr< OCResource > resource,
215             std::vector< ConfigurationName > configurations, ConfigurationCallback callback);
216
217     /**
218      * API to show a list of supported configuration units (configurable parameters)
219      * Callback call when a response arrives.
220      *
221      * @return the list in JSON format
222      */
223     std::string getListOfSupportedConfigurationUnits();
224
225     /**
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.
228      *
229      * @param callback - callback.
230      *
231      * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
232      *
233      * NOTE: OCStackResult is defined in ocstack.h.
234      */
235     OCStackResult doBootstrap(ConfigurationCallback callback);
236
237 private:
238
239     GroupManager *g_groupmanager;
240
241     std::vector< ConfigurationUnitInfo > ConfigurationUnitTable;
242
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,
256             std::string conf);
257     void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode,
258             std::string conf);
259     static void onFoundBootstrapServer(std::vector< std::shared_ptr< OCResource > > resources);
260     static void onGetBootstrapInformation(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
261
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);
267
268     std::string getHostFromURI(std::string oldUri);
269
270     bool isSimpleResource(std::shared_ptr< OCResource > resource);
271
272 };
273
274 #endif  /* __OC_THINGSCONFIGURATION__*/
275