iotivity 0.9.0
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / linux / configuration / ConfigurationCollection.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 ///
22 /// This sample shows how one could create a resource (collection) with children.
23 ///
24
25 #include <functional>
26 #include <thread>
27
28 #include "OCPlatform.h"
29 #include "OCApi.h"
30 #include "ThingsManager.h"
31
32 #pragma once
33
34 using namespace OC;
35
36 typedef std::function< OCEntityHandlerResult(std::shared_ptr< OCResourceRequest > request) > ResourceEntityHandler;
37
38 static std::string defaultURIPrefix = "/oic/con";
39 static std::string defaultResourceTypePrefix = "oic.con";
40
41 extern std::string defaultTimeValue;
42 extern std::string defaultCurrentTimeValue;
43
44 class TimeCollection
45 {
46 public:
47
48     // diagnostics members
49     std::string m_timeUri;
50     std::string m_timeValue;
51     std::vector< std::string > m_timeTypes;
52     std::vector< std::string > m_timeInterfaces;
53     OCResourceHandle m_timeHandle;
54     OCRepresentation m_timeRep;
55
56     // factory reset members
57     std::string m_currentTimeUri;
58     std::string m_currentTimeValue;
59     std::vector< std::string > m_currentTimeTypes;
60     std::vector< std::string > m_currentTimeInterfaces;
61     OCResourceHandle m_currentTimeHandle;
62     OCRepresentation m_currentTimeRep;
63
64 public:
65     /// Constructor
66     TimeCollection() :
67             m_timeValue(defaultTimeValue), m_currentTimeValue(defaultCurrentTimeValue)
68     {
69         m_currentTimeUri = defaultURIPrefix + "/time/0/currentTime"; // URI of the resource
70         m_currentTimeTypes.push_back("oic.con.time.currentTime"); // resource type name.
71         m_currentTimeInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
72
73         m_currentTimeRep.setUri(m_currentTimeUri);
74         m_currentTimeRep.setResourceTypes(m_currentTimeTypes);
75         m_currentTimeRep.setResourceInterfaces(m_currentTimeInterfaces);
76         m_currentTimeRep.setValue("value", m_currentTimeValue);
77         m_currentTimeHandle = NULL;
78
79         m_timeUri = defaultURIPrefix + "/time"; // URI of the resource
80         m_timeTypes.push_back("oic.con.time"); // resource type name.
81         m_timeInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
82         m_timeInterfaces.push_back(BATCH_INTERFACE); // resource interface.
83         m_timeInterfaces.push_back(LINK_INTERFACE); // resource interface.
84         m_timeRep.setValue("value", m_timeValue);
85         m_timeRep.setUri(m_timeUri);
86         m_timeRep.setResourceTypes(m_timeTypes);
87         m_timeRep.setResourceInterfaces(m_timeInterfaces);
88         m_timeHandle = NULL;
89     }
90     ;
91
92     /// Constructor
93     TimeCollection(std::string URIPrefix, std::string ResourceTypePrefix) :
94             m_timeValue(defaultTimeValue), m_currentTimeValue(defaultCurrentTimeValue)
95     {
96         m_currentTimeUri = URIPrefix + "/time/0/currentTime"; // URI of the resource
97         m_currentTimeTypes.push_back(ResourceTypePrefix + ".time.currentTime"); // type name.
98         m_currentTimeInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
99
100         m_currentTimeRep.setUri(m_currentTimeUri);
101         m_currentTimeRep.setResourceTypes(m_currentTimeTypes);
102         m_currentTimeRep.setResourceInterfaces(m_currentTimeInterfaces);
103         m_currentTimeRep.setValue("value", m_currentTimeValue);
104         m_currentTimeHandle = NULL;
105
106         m_timeUri = URIPrefix + "/time"; // URI of the resource
107         m_timeTypes.push_back(ResourceTypePrefix + ".time"); // resource type name.
108         m_timeInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
109         m_timeInterfaces.push_back(BATCH_INTERFACE); // resource interface.
110         m_timeInterfaces.push_back(LINK_INTERFACE); // resource interface.
111         m_timeRep.setValue("value", m_timeValue);
112         m_timeRep.setUri(m_timeUri);
113         m_timeRep.setResourceTypes(m_timeTypes);
114         m_timeRep.setResourceInterfaces(m_timeInterfaces);
115         m_timeHandle = NULL;
116     }
117     ;
118
119     /// This function internally calls registerResource API.
120     void createResources(ResourceEntityHandler callback);
121
122     void setTimeRepresentation(OCRepresentation& rep);
123     void setCurrentTimeRepresentation(OCRepresentation& rep);
124
125     OCRepresentation getTimeRepresentation();
126     OCRepresentation getCurrentTimeRepresentation();
127
128     std::string getTimeUri();
129     std::string getCurrentTimeUri();
130
131     void factoryReset();
132
133 };
134
135 extern std::string defaultNetworkValue;
136 extern std::string defaultIPAddressValue;
137
138 class NetworkCollection
139 {
140 public:
141
142     // diagnostics members
143     std::string m_networkUri;
144     std::string m_networkValue;
145     std::vector< std::string > m_networkTypes;
146     std::vector< std::string > m_networkInterfaces;
147     OCResourceHandle m_networkHandle;
148     OCRepresentation m_networkRep;
149
150     // factory reset members
151     std::string m_IPAddressUri;
152     std::string m_IPAddressValue;
153     std::vector< std::string > m_IPAddressTypes;
154     std::vector< std::string > m_IPAddressInterfaces;
155     OCResourceHandle m_IPAddressHandle;
156     OCRepresentation m_IPAddressRep;
157
158 public:
159
160     /// Constructor
161     NetworkCollection() :
162             m_networkValue(defaultNetworkValue), m_IPAddressValue(defaultIPAddressValue)
163     {
164         m_IPAddressUri = defaultURIPrefix + "/network/0/IPAddress"; // URI of the resource
165         m_IPAddressTypes.push_back("oic.con.network.IPAddress"); // resource type name.
166         m_IPAddressInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
167
168         m_IPAddressRep.setUri(m_IPAddressUri);
169         m_IPAddressRep.setResourceTypes(m_IPAddressTypes);
170         m_IPAddressRep.setResourceInterfaces(m_IPAddressInterfaces);
171         m_IPAddressRep.setValue("value", m_IPAddressValue);
172         m_IPAddressHandle = NULL;
173
174         m_networkUri = defaultURIPrefix + "/network"; // URI of the resource
175         m_networkTypes.push_back("oic.con.network"); // resource type name.
176         m_networkInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
177         m_networkInterfaces.push_back(BATCH_INTERFACE); // resource interface.
178         m_networkInterfaces.push_back(LINK_INTERFACE); // resource interface.
179         m_networkRep.setValue("value", m_networkValue);
180         m_networkRep.setUri(m_networkUri);
181         m_networkRep.setResourceTypes(m_networkTypes);
182         m_networkRep.setResourceInterfaces(m_networkInterfaces);
183         m_networkHandle = NULL;
184     }
185     ;
186
187     /// Constructor
188     NetworkCollection(std::string URIPrefix, std::string ResourceTypePrefix) :
189             m_networkValue(defaultNetworkValue), m_IPAddressValue(defaultIPAddressValue)
190     {
191         m_IPAddressUri = URIPrefix + "/network/0/IPAddress"; // URI of the resource
192         m_IPAddressTypes.push_back(ResourceTypePrefix + "network.IPAddress"); // resource type name.
193         m_IPAddressInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
194
195         m_IPAddressRep.setUri(m_IPAddressUri);
196         m_IPAddressRep.setResourceTypes(m_IPAddressTypes);
197         m_IPAddressRep.setResourceInterfaces(m_IPAddressInterfaces);
198         m_IPAddressRep.setValue("value", m_IPAddressValue);
199         m_IPAddressHandle = NULL;
200
201         m_networkUri = URIPrefix + "/network"; // URI of the resource
202         m_networkTypes.push_back(ResourceTypePrefix + ".network"); // resource type name.
203         m_networkInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
204         m_networkInterfaces.push_back(BATCH_INTERFACE); // resource interface.
205         m_networkInterfaces.push_back(LINK_INTERFACE); // resource interface.
206         m_networkRep.setValue("value", m_networkValue);
207         m_networkRep.setUri(m_networkUri);
208         m_networkRep.setResourceTypes(m_networkTypes);
209         m_networkRep.setResourceInterfaces(m_networkInterfaces);
210         m_networkHandle = NULL;
211     }
212     ;
213
214     /// This function internally calls registerResource API.
215     void createResources(ResourceEntityHandler callback);
216
217     void setNetworkRepresentation(OCRepresentation& rep);
218     void setIPAddressRepresentation(OCRepresentation& rep);
219
220     OCRepresentation getNetworkRepresentation();
221     OCRepresentation getIPAddressRepresentation();
222
223     std::string getNetworkUri();
224     std::string getIPAddressUri();
225
226     void factoryReset();
227
228 };
229
230 extern std::string defaultSecurityValue;
231 extern std::string defaultModeValue;
232
233 class SecurityCollection
234 {
235 public:
236
237     // diagnostics members
238     std::string m_securityUri;
239     std::string m_securityValue;
240     std::vector< std::string > m_securityTypes;
241     std::vector< std::string > m_securityInterfaces;
242     OCResourceHandle m_securityHandle;
243     OCRepresentation m_securityRep;
244
245     // factory reset members
246     std::string m_modeUri;
247     std::string m_modeValue;
248     std::vector< std::string > m_modeTypes;
249     std::vector< std::string > m_modeInterfaces;
250     OCResourceHandle m_modeHandle;
251     OCRepresentation m_modeRep;
252
253 public:
254     /// Constructor
255     SecurityCollection() :
256             m_securityValue(defaultSecurityValue), m_modeValue(defaultModeValue)
257     {
258         m_modeUri = defaultURIPrefix + "/security/0/mode"; // URI of the resource
259         m_modeTypes.push_back("oic.con.security.mode"); // resource type name.
260         m_modeInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
261
262         m_modeRep.setUri(m_modeUri);
263         m_modeRep.setResourceTypes(m_modeTypes);
264         m_modeRep.setResourceInterfaces(m_modeInterfaces);
265         m_modeRep.setValue("value", m_modeValue);
266         m_modeHandle = NULL;
267
268         m_securityUri = defaultURIPrefix + "/security"; // URI of the resource
269         m_securityTypes.push_back("oic.con.security"); // resource type name.
270         m_securityInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
271         m_securityInterfaces.push_back(BATCH_INTERFACE); // resource interface.
272         m_securityInterfaces.push_back(LINK_INTERFACE); // resource interface.
273         m_securityRep.setValue("value", m_securityValue);
274         m_securityRep.setUri(m_securityUri);
275         m_securityRep.setResourceTypes(m_securityTypes);
276         m_securityRep.setResourceInterfaces(m_securityInterfaces);
277         m_securityHandle = NULL;
278     }
279     ;
280
281     /// Constructor
282     SecurityCollection(std::string URIPrefix, std::string ResourceTypePrefix) :
283             m_securityValue(defaultSecurityValue), m_modeValue(defaultModeValue)
284     {
285         m_modeUri = URIPrefix + "/security/0/mode"; // URI of the resource
286         m_modeTypes.push_back(ResourceTypePrefix + ".security.mode"); // resource type name.
287         m_modeInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
288
289         m_modeRep.setUri(m_modeUri);
290         m_modeRep.setResourceTypes(m_modeTypes);
291         m_modeRep.setResourceInterfaces(m_modeInterfaces);
292         m_modeRep.setValue("value", m_modeValue);
293         m_modeHandle = NULL;
294
295         m_securityUri = URIPrefix + "/security"; // URI of the resource
296         m_securityTypes.push_back(ResourceTypePrefix + ".security"); // resource type name.
297         m_securityInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
298         m_securityInterfaces.push_back(BATCH_INTERFACE); // resource interface.
299         m_securityInterfaces.push_back(LINK_INTERFACE); // resource interface.
300         m_securityRep.setValue("value", m_securityValue);
301         m_securityRep.setUri(m_securityUri);
302         m_securityRep.setResourceTypes(m_securityTypes);
303         m_securityRep.setResourceInterfaces(m_securityInterfaces);
304         m_securityHandle = NULL;
305     }
306     ;
307
308     /// This function internally calls registerResource API.
309     void createResources(ResourceEntityHandler callback);
310
311     void setSecurityRepresentation(OCRepresentation& rep);
312     void setModeRepresentation(OCRepresentation& rep);
313
314     OCRepresentation getSecurityRepresentation();
315     OCRepresentation getModeRepresentation();
316
317     std::string getSecurityUri();
318     std::string getModeUri();
319
320     void factoryReset();
321
322 };
323
324 extern std::string defaultConfigurationValue;
325 extern std::string defaultRegionValue;
326 static std::string defaultTimeLink = "/con/con/0/time";
327 static std::string defaultNetworkLink = "/con/con/0/network";
328 static std::string defaultSecurityLink = "/con/con/0/security";
329
330 class ConfigurationCollection
331 {
332 public:
333     TimeCollection *myTimeCollection;
334     NetworkCollection *myNetworkCollection;
335     SecurityCollection *mySecurityCollection;
336
337 public:
338     // Configuration members
339     std::string m_configurationUri;
340     std::string m_configurationValue;
341     std::vector< std::string > m_configurationTypes;
342     std::vector< std::string > m_configurationInterfaces;
343     OCResourceHandle m_configurationHandle;
344     OCRepresentation m_configurationRep;
345
346     // Security members
347     std::string m_regionUri;
348     std::string m_regionValue;
349     std::vector< std::string > m_regionTypes;
350     std::vector< std::string > m_regionInterfaces;
351     OCResourceHandle m_regionHandle;
352     OCRepresentation m_regionRep;
353
354     // Time members
355     std::string m_timeUri;
356     std::string m_timeLink;
357     std::vector< std::string > m_timeTypes;
358     std::vector< std::string > m_timeInterfaces;
359     OCResourceHandle m_timeHandle;
360     OCRepresentation m_timeRep;
361
362     // Network members
363     std::string m_networkUri;
364     std::string m_networkLink;
365     std::vector< std::string > m_networkTypes;
366     std::vector< std::string > m_networkInterfaces;
367     OCResourceHandle m_networkHandle;
368     OCRepresentation m_networkRep;
369
370     // Security members
371     std::string m_securityUri;
372     std::string m_securityLink;
373     std::vector< std::string > m_securityTypes;
374     std::vector< std::string > m_securityInterfaces;
375     OCResourceHandle m_securityHandle;
376     OCRepresentation m_securityRep;
377
378 public:
379     /// Constructor
380     ConfigurationCollection() :
381             m_configurationValue(defaultConfigurationValue), m_regionValue(defaultRegionValue), m_timeLink(
382                     defaultTimeLink), m_networkLink(defaultNetworkLink), m_securityLink(
383                     defaultSecurityLink)
384     {
385         m_regionUri = defaultURIPrefix + "/0/region"; // URI of the resource
386         m_regionTypes.push_back(defaultResourceTypePrefix + ".region"); // resource type name.
387         m_regionInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
388
389         m_regionRep.setUri(m_regionUri);
390         m_regionRep.setResourceTypes(m_regionTypes);
391         m_regionRep.setResourceInterfaces(m_regionInterfaces);
392         m_regionRep.setValue("value", m_regionValue);
393         m_regionHandle = NULL;
394
395         m_timeUri = defaultURIPrefix + "/0/time"; // URI of the resource
396         m_timeTypes.push_back(defaultResourceTypePrefix + ".time"); // resource type name.
397         m_timeInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
398
399         m_timeRep.setUri(m_timeUri);
400         m_timeRep.setResourceTypes(m_timeTypes);
401         m_timeRep.setResourceInterfaces(m_timeInterfaces);
402         m_timeRep.setValue("link", m_timeLink);
403         m_timeHandle = NULL;
404
405         m_networkUri = defaultURIPrefix + "/0/net"; // URI of the resource
406         m_networkTypes.push_back(defaultResourceTypePrefix + ".net"); // resource type name.
407         m_networkInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
408
409         m_networkRep.setUri(m_networkUri);
410         m_networkRep.setResourceTypes(m_networkTypes);
411         m_networkRep.setResourceInterfaces(m_networkInterfaces);
412         m_networkRep.setValue("link", m_networkLink);
413         m_networkHandle = NULL;
414
415         m_securityUri = defaultURIPrefix + "/0/sec"; // URI of the resource
416         m_securityTypes.push_back(defaultResourceTypePrefix + ".sec"); // resource type name.
417         m_securityInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
418
419         m_securityRep.setUri(m_securityUri);
420         m_securityRep.setResourceTypes(m_securityTypes);
421         m_securityRep.setResourceInterfaces(m_securityInterfaces);
422         m_securityRep.setValue("link", m_securityLink);
423         m_securityHandle = NULL;
424
425         m_configurationUri = defaultURIPrefix + ""; // URI of the resource
426         m_configurationTypes.push_back(defaultResourceTypePrefix + ""); // resource type name.
427         m_configurationInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
428         m_configurationInterfaces.push_back(BATCH_INTERFACE); // resource interface.
429         m_configurationInterfaces.push_back(LINK_INTERFACE); // resource interface.
430         m_configurationRep.setValue("value", m_configurationValue);
431         m_configurationRep.setUri(m_configurationUri);
432         m_configurationRep.setResourceTypes(m_configurationTypes);
433         m_configurationRep.setResourceInterfaces(m_configurationInterfaces);
434         m_configurationHandle = NULL;
435
436         myTimeCollection = new TimeCollection();
437         myNetworkCollection = new NetworkCollection();
438         mySecurityCollection = new SecurityCollection();
439     }
440     ;
441
442     /// Constructor
443     ConfigurationCollection(std::string URIPrefix, std::string ResourceTypePrefix) :
444             m_configurationValue(defaultConfigurationValue), m_regionValue(defaultRegionValue), m_timeLink(
445                     defaultTimeLink), m_networkLink(defaultNetworkLink), m_securityLink(
446                     defaultSecurityLink)
447     {
448         m_regionUri = URIPrefix + "/0/region"; // URI of the resource
449         m_regionTypes.push_back(ResourceTypePrefix + ".region"); // type name.
450         m_regionInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
451
452         m_regionRep.setUri(m_regionUri);
453         m_regionRep.setResourceTypes(m_regionTypes);
454         m_regionRep.setResourceInterfaces(m_regionInterfaces);
455         m_regionRep.setValue("value", m_regionValue);
456         m_regionHandle = NULL;
457
458         m_timeUri = URIPrefix + "/0/time"; // URI of the resource
459         m_timeTypes.push_back(ResourceTypePrefix + ".time"); // resource type name.
460         m_timeInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
461
462         m_timeRep.setUri(m_timeUri);
463         m_timeRep.setResourceTypes(m_timeTypes);
464         m_timeRep.setResourceInterfaces(m_timeInterfaces);
465         m_timeRep.setValue("link", m_timeLink);
466         m_timeHandle = NULL;
467
468         m_networkUri = URIPrefix + "/0/net"; // URI of the resource
469         m_networkTypes.push_back(ResourceTypePrefix + ".net"); // resource type name.
470         m_networkInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
471
472         m_networkRep.setUri(m_networkUri);
473         m_networkRep.setResourceTypes(m_networkTypes);
474         m_networkRep.setResourceInterfaces(m_networkInterfaces);
475         m_networkRep.setValue("link", m_networkLink);
476         m_networkHandle = NULL;
477
478         m_securityUri = URIPrefix + "/0/sec"; // URI of the resource
479         m_securityTypes.push_back(ResourceTypePrefix + ".sec"); // resource type name.
480         m_securityInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
481
482         m_securityRep.setUri(m_securityUri);
483         m_securityRep.setResourceTypes(m_securityTypes);
484         m_securityRep.setResourceInterfaces(m_securityInterfaces);
485         m_securityRep.setValue("link", m_securityLink);
486         m_securityHandle = NULL;
487
488         m_configurationUri = URIPrefix + ""; // URI of the resource
489         m_configurationTypes.push_back(ResourceTypePrefix + ""); // resource type name.
490         m_configurationInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
491         m_configurationInterfaces.push_back(BATCH_INTERFACE); // resource interface.
492         m_configurationInterfaces.push_back(LINK_INTERFACE); // resource interface.
493         m_configurationRep.setValue("value", m_configurationValue);
494         m_configurationRep.setUri(m_configurationUri);
495         m_configurationRep.setResourceTypes(m_configurationTypes);
496         m_configurationRep.setResourceInterfaces(m_configurationInterfaces);
497         m_configurationHandle = NULL;
498
499         myTimeCollection = new TimeCollection(URIPrefix, ResourceTypePrefix);
500         myNetworkCollection = new NetworkCollection(URIPrefix, ResourceTypePrefix);
501         mySecurityCollection = new SecurityCollection(URIPrefix, ResourceTypePrefix);
502     }
503     ;
504
505     ~ConfigurationCollection()
506     {
507         free(myTimeCollection);
508         free(myNetworkCollection);
509         free(mySecurityCollection);
510     }
511
512     /// This function internally calls registerResource API.
513     void createResources(ResourceEntityHandler callback);
514
515     void setConfigurationRepresentation(OCRepresentation& rep);
516     void setTimeRepresentation(OCRepresentation& rep);
517     void setNetworkRepresentation(OCRepresentation& rep);
518     void setSecurityRepresentation(OCRepresentation& rep);
519     void setRegionRepresentation(OCRepresentation& rep);
520
521     OCRepresentation getTimeRepresentation();
522     OCRepresentation getNetworkRepresentation();
523     OCRepresentation getSecurityRepresentation();
524     OCRepresentation getRegionRepresentation();
525     OCRepresentation getConfigurationRepresentation();
526
527     std::string getConfigurationUri();
528     std::string getTimeUri();
529     std::string getNetworkUri();
530     std::string getSecurityUri();
531     std::string getRegionUri();
532
533     void factoryReset();
534
535 };