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