Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / tizen / TMSampleApp / src / configuration.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 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 #include "tmsampleapp.h"
22 #include "tmutil.h"
23
24 using namespace std;
25 using namespace OC;
26 using namespace OIC;
27
28 int isWaiting = 0; // 0: none to wait, 1: wait for the response of "getConfigurationValue"
29 const int SUCCESS_RESPONSE = 0;
30
31 OCResourceHandle configurationCollectionHandle = NULL;
32 OCResourceHandle configurationFoundHandle = NULL;
33 std::shared_ptr< OCResource > g_configurationCollection; // For a group of multiple resources
34 std::shared_ptr< OCResource > g_configurationResource; // For a single resource
35
36 OCResourceHandle diagnosticsCollectionHandle = NULL;
37 OCResourceHandle diagnosticsFoundHandle = NULL;
38 std::shared_ptr< OCResource > g_diagnosticsCollection; // For a group of multiple resources
39 std::shared_ptr< OCResource > g_diagnosticsResource; // For a single resource
40
41 OCResourceHandle setCollectionHandle = NULL;
42 OCResourceHandle setFoundHandle = NULL;
43 std::shared_ptr< OCResource > g_setCollection; // For a group of multiple resources
44 std::shared_ptr< OCResource > g_setResource; // For a single resource
45
46 std::map< std::string, std::shared_ptr< OCResource > > resourceTable;
47 std::vector< OCResourceHandle > configResourceHandleVector;
48
49 typedef std::string ConfigurationName;
50 typedef std::string ConfigurationValue;
51
52 static Evas_Object *log_entry = NULL;
53
54 string CONFIGURATION_COLLECTION_RESOURCE_URI  = "/core/a/configuration/resourceset";
55 string CONFIGURATION_COLLECTION_RESOURCE_TYPE = "core.configuration.resourceset";
56 string DIAGNOSTIC_COLLECTION_RESOURCE_URI     = "/core/a/diagnostics/resourceset";
57 string DIAGNOSTIC_COLLECTION_RESOURCE_TYPE    = "core.diagnostics.resourceset";
58 string FACTORYSET_COLLECTION_RESOURCE_URI     = "/core/a/factoryset/resourceset";
59 string FACTORYSET_COLLECTION_RESOURCE_TYPE    = "core.factoryset.resourceset";
60
61 string CONFIGURATION_RESOURCE_URI             = "/oic/con";
62 string DIAGNOSTIC_RESOURCE_URI                = "/oic/diag";
63 string FACTORYSET_RESOURCE_URI                = "/factorySet";
64
65 ThingsManager *configthingsMgr = new ThingsManager();
66
67 typedef struct region_popup
68 {
69     Evas_Object *popup;
70     Evas_Object *entry;
71 } region_popup_fields;
72
73 void *updateConfigLog(void *data)
74 {
75     string *log = (string *)data;
76     // Show the log
77     elm_entry_entry_append(log_entry, (*log).c_str());
78     elm_entry_cursor_end_set(log_entry);
79     return NULL;
80 }
81
82 // Callback to found collection resource
83 void onFoundCollectionResource(std::vector< std::shared_ptr< OCResource > > resources)
84 {
85     dlog_print(DLOG_INFO, LOG_TAG, "#### onFoundCollectionResource ENTRY!!!!");
86     try
87     {
88         // Do some operations with resource object.
89         for (unsigned int i = 0; i < resources.size(); ++i)
90         {
91             string logMessage;
92             std::shared_ptr< OCResource > resource = resources.at(i);
93             if (resource)
94             {
95                 string resourceURI = resource->host();
96                 string hostAddress = resource->uri();
97                 logMessage = "FoundHost: " + resource->host() + "<br>";
98                 logMessage += "FoundUri : " + resource->uri() + "<br>";
99                 logMessage += "----------------------<br>";
100                 dlog_print(DLOG_INFO, LOG_TAG, "FoundHost: %s", resourceURI.c_str());
101                 dlog_print(DLOG_INFO, LOG_TAG, "FoundUri : %s", hostAddress.c_str());
102                 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateConfigLog,
103                                                       &logMessage);
104
105                 if (resource->uri() == CONFIGURATION_COLLECTION_RESOURCE_URI)
106                     g_configurationCollection = resource;
107                 else if (resource->uri() == DIAGNOSTIC_COLLECTION_RESOURCE_URI)
108                     g_diagnosticsCollection = resource;
109                 else if (resource->uri() == FACTORYSET_COLLECTION_RESOURCE_URI)
110                     g_setCollection = resource;
111                 else
112                 {
113                     isWaiting = 0;
114                     return;
115                 }
116             }
117             else
118             {
119                 // Resource is invalid
120                 logMessage = "Found Resource invalid!";
121                 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateConfigLog,
122                                                       &logMessage);
123             }
124         }
125     }
126     catch (std::exception &e)
127     {
128         dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
129     }
130     isWaiting = 0;
131     dlog_print(DLOG_INFO, LOG_TAG, "#### onFoundCollectionResource EXIT!!!!");
132 }
133
134 // Callback to found candidate resources
135 void onFoundCandidateResource(std::vector< std::shared_ptr< OCResource > > resources)
136 {
137     dlog_print(DLOG_INFO, LOG_TAG, "#### onFoundCandidateCollection ENTRY!!!!");
138     try
139     {
140         // Do some operations with resource object.
141         for (unsigned int i = 0; i < resources.size(); ++i)
142         {
143             string logMessage;
144             std::shared_ptr< OCResource > resource = resources.at(i);
145             if (resource)
146             {
147                 // Check if the resource is new one. If so, store it.
148                 std::map< std::string, std::shared_ptr< OCResource > >::iterator iter =
149                     resourceTable.find(resource->host() + resource->uri());
150
151                 if (iter == resourceTable.end()) // new one
152                 {
153                     resourceTable[resource->host() + resource->uri()] = resource;
154
155                     OCResourceHandle foundResourceHandle = NULL;
156                     OCStackResult result = OCPlatform::registerResource(foundResourceHandle,
157                                            resource);
158                     // TODO: null check for foundResourceHandle
159                     dlog_print(DLOG_INFO, LOG_TAG, "#### (%s) REGISTERED",
160                                resource->host().c_str());
161                     if (OC_STACK_OK == result)
162                     {
163                         string resourceURI = resource->host();
164                         string hostAddress = resource->uri();
165                         logMessage = "FoundHost: " + resource->host() + "<br>";
166                         logMessage += "FoundUri: " + resource->uri()
167                                       + " Registered <br>";
168                         logMessage += "----------------------<br>";
169                         dlog_print(DLOG_INFO, LOG_TAG, "Host: %s", resourceURI.c_str());
170                         dlog_print(DLOG_INFO, LOG_TAG, "Uri : %s", hostAddress.c_str());
171                         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateConfigLog,
172                                                               &logMessage);
173
174                         if (resource->uri() == CONFIGURATION_RESOURCE_URI &&
175                             NULL != configurationCollectionHandle)
176                         {
177                             OCPlatform::bindResource(configurationCollectionHandle,
178                                                      foundResourceHandle);
179                             configurationFoundHandle = foundResourceHandle;
180                             if (NULL == g_configurationResource)
181                             {
182                                 dlog_print(DLOG_INFO, LOG_TAG, "g_configurationResource updated");
183                                 g_configurationResource = resource;
184                             }
185                             else
186                             {
187                                 dlog_print(DLOG_INFO, LOG_TAG,
188                                            "g_configurationResource is not null");
189                             }
190                         }
191                         else if (resource->uri() == DIAGNOSTIC_RESOURCE_URI &&
192                                  NULL != diagnosticsCollectionHandle)
193                         {
194                             OCPlatform::bindResource(diagnosticsCollectionHandle,
195                                                      foundResourceHandle);
196                             diagnosticsFoundHandle = foundResourceHandle;
197                             if (NULL == g_diagnosticsResource)
198                             {
199                                 dlog_print(DLOG_INFO, LOG_TAG,
200                                            "g_diagnosticsResource updated");
201                                 g_diagnosticsResource = resource;
202                             }
203                             else
204                             {
205                                 dlog_print(DLOG_INFO, LOG_TAG,
206                                            "g_diagnosticsResource is not null");
207                             }
208                         }
209                         else if (resource->uri() == FACTORYSET_RESOURCE_URI &&
210                                  NULL != setCollectionHandle)
211                         {
212                             OCPlatform::bindResource(setCollectionHandle, foundResourceHandle);
213                             setFoundHandle = foundResourceHandle;
214                             if (NULL == g_setResource)
215                             {
216                                 dlog_print(DLOG_INFO, LOG_TAG, "g_setResource updated");
217                                 g_setResource = resource;
218                             }
219                             else
220                             {
221                                 dlog_print(DLOG_INFO, LOG_TAG, "g_setResource is not null");
222                             }
223                         }
224                         configResourceHandleVector.push_back(foundResourceHandle);
225                     }
226                     else
227                     {
228                         logMessage = "Resource Error!";
229                         dlog_print(DLOG_INFO, LOG_TAG, "Resource Error!");
230                         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateConfigLog,
231                                                               &logMessage);
232                     }
233                 }
234             }
235             else
236             {
237                 // Resource is invalid
238                 logMessage = "Resource is invalid!";
239                 dlog_print(DLOG_INFO, LOG_TAG, "Resource is invalid!");
240                 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateConfigLog,
241                                                       &logMessage);
242             }
243         }
244     }
245     catch (std::exception &e)
246     {
247         dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
248     }
249
250     isWaiting = 0;
251     dlog_print(DLOG_INFO, LOG_TAG, "#### onFoundCandidateCollection EXIT!!!!");
252 }
253
254 // Callback to updateConfiguration
255 static void onUpdateConfigurationsCallback(const HeaderOptions &headerOptions,
256         const OCRepresentation &rep,
257         const int eCode)
258 {
259     dlog_print(DLOG_INFO, LOG_TAG, "#### onUpdateConfigurationsCallback: ENTRY!!!!");
260     isWaiting = 0;
261
262     if (SUCCESS_RESPONSE != eCode)
263     {
264         dlog_print(DLOG_ERROR, LOG_TAG, "#### onUpdateConfigurationsCallback: "
265                    "ERROR RESPONSE!!!!");
266         return;
267     }
268
269     string logMessage = "Resource URI: " + rep.getUri() + "<br>";
270     dlog_print(DLOG_INFO, LOG_TAG, "#### Resource URI: %s", rep.getUri().c_str());
271
272     if (rep.hasAttribute("loc"))
273     {
274         dlog_print(DLOG_INFO, LOG_TAG, "#### Location : %s",
275                    rep.getValue< std::string >("loc").c_str());
276         logMessage = logMessage + "Location : " + rep.getValue< std::string >("loc") + "<br>";
277     }
278     if (rep.hasAttribute("st"))
279     {
280         dlog_print(DLOG_INFO, LOG_TAG, "#### SystemTime : %s",
281                    rep.getValue< std::string >("st").c_str());
282         logMessage = logMessage + "SystemTime : " + rep.getValue< std::string >("st") + "<br>";
283     }
284     if (rep.hasAttribute("c"))
285     {
286         dlog_print(DLOG_INFO, LOG_TAG, "#### Currency : %s",
287                    rep.getValue< std::string >("c").c_str());
288         logMessage = logMessage + "Currency : " + rep.getValue< std::string >("c") + "<br>";
289     }
290     if (rep.hasAttribute("r"))
291     {
292         dlog_print(DLOG_INFO, LOG_TAG, "#### Region : %s",
293                    rep.getValue< std::string >("r").c_str());
294         logMessage = logMessage + "Region : " + rep.getValue< std::string >("r") + "<br>";
295     }
296     logMessage += "----------------------<br>";
297     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateConfigLog,
298                                           &logMessage);
299     dlog_print(DLOG_INFO, LOG_TAG, "#### onUpdateConfigurationsCallback: EXIT!!!!");
300 }
301
302 // Callback to getConfiguration
303 static void onGetConfigurationsCallback(const HeaderOptions &headerOptions,
304                                         const OCRepresentation &rep,
305                                         const int eCode)
306 {
307     dlog_print(DLOG_INFO, LOG_TAG, "#### onGetConfigurationsCallback: enter!!!!");
308     isWaiting = 0;
309
310     if (SUCCESS_RESPONSE != eCode)
311     {
312         dlog_print(DLOG_ERROR, LOG_TAG, "#### onGetConfigurationsCallback: "
313                    "ERROR RESPONSE!!!!");
314         return ;
315     }
316
317     string logMessage = "Resource URI: " + rep.getUri() + "<br>";
318     dlog_print(DLOG_INFO, LOG_TAG, "#### Resource URI: %s", rep.getUri().c_str());
319
320     if (rep.hasAttribute("loc"))
321     {
322         dlog_print(DLOG_INFO, LOG_TAG, "#### Location : %s",
323                    rep.getValue< std::string >("loc").c_str());
324         logMessage = logMessage + "Location : " + rep.getValue< std::string >("loc") + "<br>";
325     }
326     if (rep.hasAttribute("st"))
327     {
328         dlog_print(DLOG_INFO, LOG_TAG, "#### SystemTime : %s",
329                    rep.getValue< std::string >("st").c_str());
330         logMessage = logMessage + "SystemTime : " + rep.getValue< std::string >("st") + "<br>";
331     }
332     if (rep.hasAttribute("c"))
333     {
334         dlog_print(DLOG_INFO, LOG_TAG, "#### Currency : %s",
335                    rep.getValue< std::string >("c").c_str());
336         logMessage = logMessage + "Currency : " + rep.getValue< std::string >("c") + "<br>";
337     }
338     if (rep.hasAttribute("r"))
339     {
340         dlog_print(DLOG_INFO, LOG_TAG, "#### Region : %s",
341                    rep.getValue< std::string >("r").c_str());
342         logMessage = logMessage + "Region : " + rep.getValue< std::string >("r") + "<br>";
343     }
344
345     logMessage += "----------------------<br>";
346     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateConfigLog, &logMessage);
347     dlog_print(DLOG_INFO, LOG_TAG, "#### onGetConfigurationsCallback: exit!!!!");
348 }
349
350 static void onFactoryReset(const HeaderOptions &headerOptions, const OCRepresentation &rep,
351                            const int eCode)
352 {
353     dlog_print(DLOG_INFO, LOG_TAG, "#### onFactoryReset: enter!!!!");
354     isWaiting = 0;
355
356     if (SUCCESS_RESPONSE != eCode)
357     {
358         dlog_print(DLOG_ERROR, LOG_TAG, "#### onFactoryReset: ERROR RESPONSE!!!!");
359         return ;
360     }
361
362     dlog_print(DLOG_INFO, LOG_TAG, "#### Resource URI: %s", rep.getUri().c_str());
363     dlog_print(DLOG_INFO, LOG_TAG, "#### FactoryReset : %s",
364                rep.getValue< std::string >("value").c_str());
365     string logMessage = "Resource URI : " + rep.getUri() + "<br>";
366     logMessage = logMessage + "FactoryReset : " +
367                  rep.getValue< std::string >("value") + "<br>";
368     logMessage += "----------------------<br>";
369     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateConfigLog, &logMessage);
370     dlog_print(DLOG_INFO, LOG_TAG, "#### onFactoryReset: exit!!!!");
371 }
372
373 static void onReboot(const HeaderOptions &headerOptions, const OCRepresentation &rep,
374                      const int eCode)
375 {
376     dlog_print(DLOG_INFO, LOG_TAG, "#### onReboot: enter!!!!");
377     isWaiting = 0;
378
379     if (SUCCESS_RESPONSE != eCode)
380     {
381         dlog_print(DLOG_ERROR, LOG_TAG, "#### onReboot: ERROR RESPONSE!!!!");
382         return ;
383     }
384
385     dlog_print(DLOG_INFO, LOG_TAG, "#### Resource URI: %s", rep.getUri().c_str());
386     dlog_print(DLOG_INFO, LOG_TAG, "#### Reboot : %s",
387                rep.getValue< std::string >("value").c_str());
388     string logMessage = "Resource URI : " + rep.getUri() + "<br>";
389     logMessage = logMessage + "Reboot : " +
390                  rep.getValue< std::string >("value") + "<br>";
391     logMessage += "----------------------<br>";
392     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateConfigLog, &logMessage);
393     dlog_print(DLOG_INFO, LOG_TAG, "#### onReboot: exit!!!!");
394 }
395
396 static void createResourceCollection(string uri, string typeName, OCResourceHandle &tempHandle)
397 {
398     dlog_print(DLOG_INFO, LOG_TAG, "#### createResourceCollection: enter!!!!");
399     if (NULL != tempHandle)
400     {
401         dlog_print(DLOG_INFO, LOG_TAG, "#### Resource already exists!!!!");
402         return;
403     }
404
405     // Create resource collection
406     OCPlatform::registerResource(tempHandle, uri, typeName, BATCH_INTERFACE, NULL,
407                                  OC_DISCOVERABLE);
408     OCPlatform::bindInterfaceToResource(tempHandle, GROUP_INTERFACE);
409     OCPlatform::bindInterfaceToResource(tempHandle, DEFAULT_INTERFACE);
410
411     dlog_print(DLOG_INFO, LOG_TAG, "#### Resource created : %s", typeName.c_str());
412     string logMessage;
413     logMessage = "Resource created : <br>" + typeName + "<br>";
414     logMessage += "----------------------<br>";
415     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateConfigLog, &logMessage);
416     dlog_print(DLOG_INFO, LOG_TAG, "#### createResourceCollection: exit!!!!");
417 }
418
419 // Unbinds and unregisters all resources
420 static void deleteResource(OCResourceHandle &tempCollectionHandle,
421                            OCResourceHandle &tempResourceHandle)
422 {
423     dlog_print(DLOG_INFO, LOG_TAG, "#### calling deleteResources ENTRY!!!!");
424
425     if (NULL == tempCollectionHandle || NULL == tempResourceHandle)
426     {
427         dlog_print(DLOG_INFO, LOG_TAG, "#### Collection or resource does not exists");
428         return;
429     }
430
431     try
432     {
433         OCPlatform::unbindResource(tempCollectionHandle, tempResourceHandle);
434         dlog_print(DLOG_INFO, LOG_TAG, "#### unbindResource DONE!!!!");
435     }
436     catch (std::exception &e)
437     {
438         dlog_print(DLOG_ERROR, LOG_TAG, "Exception Occured! (%s)", e.what());
439     }
440
441     try
442     {
443         OCPlatform::unregisterResource(tempResourceHandle);
444         dlog_print(DLOG_INFO, LOG_TAG, "#### unregisterResource DONE!!!!");
445     }
446     catch (std::exception &e)
447     {
448         dlog_print(DLOG_ERROR, LOG_TAG, "Exception Occured! (%s)", e.what());
449     }
450
451     dlog_print(DLOG_INFO, LOG_TAG, "#### calling deleteResources EXIT!!!!");
452 }
453
454 static void findAllGroups(void *data, Evas_Object *obj, void *event_info)
455 {
456     dlog_print(DLOG_INFO, LOG_TAG, "#### calling findCandidateResources ENTRY!!!!");
457     std::vector<string> resourceTypes;
458     resourceTypes.push_back(CONFIGURATION_COLLECTION_RESOURCE_TYPE);
459
460     if (NULL != configthingsMgr)
461     {
462         configthingsMgr->findCandidateResources(resourceTypes, &onFoundCollectionResource, 5);
463     }
464
465     resourceTypes.clear();
466     resourceTypes.push_back(DIAGNOSTIC_COLLECTION_RESOURCE_TYPE);
467     if (NULL != configthingsMgr)
468     {
469         configthingsMgr->findCandidateResources(resourceTypes, &onFoundCollectionResource, 5);
470     }
471
472     resourceTypes.clear();
473     resourceTypes.push_back(FACTORYSET_COLLECTION_RESOURCE_TYPE);
474     if (NULL != configthingsMgr)
475     {
476         configthingsMgr->findCandidateResources(resourceTypes, &onFoundCollectionResource, 5);
477     }
478
479     dlog_print(DLOG_INFO, LOG_TAG, "#### calling findCandidateResources EXIT!!!!");
480 }
481
482 static void findAllResources(void *data, Evas_Object *obj, void *event_info)
483 {
484     dlog_print(DLOG_INFO, LOG_TAG, "#### calling findCandidateResources ENTRY!!!!");
485     std::vector<string> resourceTypes;
486     resourceTypes.push_back("oic.con");
487
488     if (NULL != configthingsMgr)
489     {
490         configthingsMgr->findCandidateResources(resourceTypes, &onFoundCandidateResource, 7);
491     }
492
493     resourceTypes.clear();
494     resourceTypes.push_back("oic.diag");
495     if (NULL != configthingsMgr)
496     {
497         configthingsMgr->findCandidateResources(resourceTypes, &onFoundCandidateResource, 7);
498     }
499
500     resourceTypes.clear();
501     resourceTypes.push_back("factorySet");
502     if (NULL != configthingsMgr)
503     {
504         configthingsMgr->findCandidateResources(resourceTypes, &onFoundCandidateResource, 7);
505     }
506
507     dlog_print(DLOG_INFO, LOG_TAG, "#### calling findCandidateResources EXIT!!!!");
508 }
509
510 static void getConfiguration(void *data, Evas_Object *obj, void *event_info)
511 {
512     dlog_print(DLOG_INFO, LOG_TAG, "#### getConfiguration ENTRY!!!!");
513     if (NULL == g_configurationCollection || NULL == g_configurationCollection.get())
514     {
515         dlog_print(DLOG_ERROR, LOG_TAG, "Note that you first create a group to use this command");
516         string logMessage = "FIRST CREATE GROUP <br>";
517         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateConfigLog, &logMessage);
518         return;
519     }
520
521     ConfigurationName name = "all";
522     std::vector< ConfigurationName > configurations;
523     configurations.push_back(name);
524
525     try
526     {
527         configthingsMgr->getConfigurations(g_configurationCollection, configurations,
528                                            &onGetConfigurationsCallback);
529     }
530     catch (std::exception &e)
531     {
532         dlog_print(DLOG_ERROR, LOG_TAG, "Exception is occured! (%s)", e.what());
533     }
534
535     isWaiting = 1;
536     dlog_print(DLOG_INFO, LOG_TAG, "#### getConfiguration EXIT!!!!");
537 }
538
539 // Updates the configuration i.e. region value to INDIA
540 static void updateConfiguration(std::string newRegionValue)
541 {
542     dlog_print(DLOG_INFO, LOG_TAG, "#### updateConfiguration ENTRY!!!!");
543     dlog_print(DLOG_INFO, LOG_TAG, "#### %s", newRegionValue.c_str());
544
545     if (NULL == g_configurationCollection || NULL == g_configurationCollection.get())
546     {
547         dlog_print(DLOG_ERROR, LOG_TAG, "Note that you first create a group to use this command");
548         string logMessage = "FIRST CREATE GROUP <br>";
549         logMessage += "----------------------<br>";
550         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateConfigLog, &logMessage);
551         return;
552     }
553
554     OCStackResult result;
555     ConfigurationName name = "r";
556     ConfigurationValue value = newRegionValue;
557
558     std::map< ConfigurationName, ConfigurationValue > configurations;
559     configurations.insert(std::make_pair(name, value));
560
561     try
562     {
563         result = configthingsMgr->updateConfigurations(g_configurationCollection, configurations,
564                  &onUpdateConfigurationsCallback);
565     }
566     catch (std::exception &e)
567     {
568         dlog_print(DLOG_ERROR, LOG_TAG, "Exception is occured! (%s)", e.what());
569     }
570
571     if (OC_STACK_OK == result)
572     {
573         string logMessage = "UpdateConfigurations called successfully<br>";
574         logMessage += "----------------------<br>";
575         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateConfigLog, &logMessage);
576     }
577
578     dlog_print(DLOG_INFO, LOG_TAG, "#### updateConfiguration EXIT!!!!");
579 }
580
581 // This method will reset all the configuration attributes to their default values
582 static void factoryReset(void *data, Evas_Object *obj, void *event_info)
583 {
584     dlog_print(DLOG_INFO, LOG_TAG, "#### factoryReset ENTRY!!!!");
585     if (NULL == g_diagnosticsCollection || NULL == g_diagnosticsCollection.get())
586     {
587         dlog_print(DLOG_ERROR, LOG_TAG, "Note that you first create a group to use this command");
588         string logMessage = "FIRST CREATE GROUP <br>";
589         logMessage += "----------------------<br>";
590         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateConfigLog, &logMessage);
591         return;
592     }
593
594     OCStackResult result;
595
596     try
597     {
598         result = configthingsMgr->factoryReset(g_diagnosticsCollection, &onFactoryReset);
599     }
600     catch (std::exception &e)
601     {
602         dlog_print(DLOG_ERROR, LOG_TAG, "Exception is occured! (%s)", e.what());
603     }
604
605     if (OC_STACK_OK == result)
606     {
607         string logMessage = "FactoryReset called successfully<br>";
608         logMessage += "----------------------<br>";
609         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateConfigLog, &logMessage);
610     }
611
612     dlog_print(DLOG_INFO, LOG_TAG, "#### factoryReset EXIT!!!!");
613 }
614
615 // Reboots the server
616 static void reboot(void *data, Evas_Object *obj, void *event_info)
617 {
618     dlog_print(DLOG_INFO, LOG_TAG, "#### reboot ENTRY!!!!");
619     if (NULL == g_diagnosticsCollection || NULL == g_diagnosticsCollection.get())
620     {
621         dlog_print(DLOG_INFO, LOG_TAG, "Note that you first create a group to use this command");
622         string logMessage = "FIRST CREATE GROUP <br>";
623         logMessage += "----------------------<br>";
624         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateConfigLog, &logMessage);
625         return;
626     }
627
628     OCStackResult result;
629
630     try
631     {
632         result = configthingsMgr->reboot(g_diagnosticsCollection, &onReboot);
633     }
634     catch (std::exception &e)
635     {
636         dlog_print(DLOG_ERROR, LOG_TAG, "Exception is occured! (%s)", e.what());
637     }
638
639     if (OC_STACK_OK == result)
640     {
641         string logMessage = "Reboot called successfully<br>";
642         logMessage += "----------------------<br>";
643         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateConfigLog, &logMessage);
644     }
645     dlog_print(DLOG_INFO, LOG_TAG, "#### reboot EXIT!!!!");
646 }
647
648 /* For getting list of all supported configuration Values it will give all
649    configuration in JSON format (key-value pair) */
650 static void getListOfSupportedConfigurationUnits(void *data, Evas_Object *obj, void *event_info)
651 {
652     dlog_print(DLOG_INFO, LOG_TAG, "#### getListOfSupportedConfigurationUnits ENTRY!!!!");
653     string listOfSupportedConfigurationUnits =
654         configthingsMgr->getListOfSupportedConfigurationUnits();
655     dlog_print(DLOG_INFO, LOG_TAG, "#### List : %s", listOfSupportedConfigurationUnits.c_str());
656
657     string logMessage;
658     logMessage = "Supported Configuration List :<br>" + listOfSupportedConfigurationUnits + "<br>";
659     logMessage += "----------------------<br>";
660     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateConfigLog, &logMessage);
661     dlog_print(DLOG_INFO, LOG_TAG, "#### getListOfSupportedConfigurationUnits EXIT!!!!");
662 }
663
664 // Creates all the resources
665 static void onStartConfigure()
666 {
667     createResourceCollection(CONFIGURATION_COLLECTION_RESOURCE_URI,
668                              CONFIGURATION_COLLECTION_RESOURCE_TYPE,
669                              configurationCollectionHandle);
670     createResourceCollection(DIAGNOSTIC_COLLECTION_RESOURCE_URI,
671                              DIAGNOSTIC_COLLECTION_RESOURCE_TYPE,
672                              diagnosticsCollectionHandle);
673     createResourceCollection(FACTORYSET_COLLECTION_RESOURCE_URI,
674                              FACTORYSET_COLLECTION_RESOURCE_TYPE,
675                              setCollectionHandle);
676 }
677
678 // Deletes all the resources
679 static void onDestroyConfigure()
680 {
681     dlog_print(DLOG_INFO, LOG_TAG, "#### Destroy sequence called");
682
683     deleteResource(configurationCollectionHandle, configurationFoundHandle);
684
685     deleteResource(diagnosticsCollectionHandle, diagnosticsFoundHandle);
686
687     deleteResource(setCollectionHandle, setFoundHandle);
688
689     dlog_print(DLOG_INFO, LOG_TAG, "#### Resources destroyed successfully");
690 }
691
692 static void
693 popup_cancel_clicked_cb(void *data, Evas_Object *obj, void *event_info)
694 {
695     region_popup_fields *popup_fields = (region_popup_fields *)data;
696     evas_object_del(popup_fields->popup);
697     free(popup_fields);
698 }
699
700 static void
701 popup_set_clicked_cb(void *data, Evas_Object *obj, void *event_info)
702 {
703     region_popup_fields *popup_fields = (region_popup_fields *)data;
704     Evas_Object *entry = popup_fields->entry;
705     const char *newRegionValue = elm_entry_entry_get(entry);
706     if (NULL == newRegionValue || strlen(newRegionValue) < 1)
707     {
708         dlog_print(DLOG_INFO, LOG_TAG, "#### Read NULL RegionValue");
709         string logMessage = "Region Value should not be NULL<br>";
710         logMessage += "----------------------<br>";
711         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateConfigLog, &logMessage);
712     }
713     else
714     {
715         std::string regionValue = std::string(newRegionValue);
716         updateConfiguration(regionValue);
717     }
718     evas_object_del(popup_fields->popup);
719     free(popup_fields);
720 }
721
722 static void
723 list_update_region_cb(void *data, Evas_Object *obj, void *event_info)
724 {
725     if (NULL == g_configurationCollection || NULL == g_configurationCollection.get())
726     {
727         dlog_print(DLOG_ERROR, LOG_TAG, "Note that you first create a group to use this command");
728         string logMessage = "FIRST CREATE GROUP <br>";
729         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateConfigLog, &logMessage);
730         return;
731     }
732
733     Evas_Object *popup, *btn;
734     Evas_Object *nf = (Evas_Object *)data;
735     Evas_Object *entry;
736     Evas_Object *layout;
737
738     /* popup */
739     popup = elm_popup_add(nf);
740     elm_popup_align_set(popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
741     eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK, eext_popup_back_cb, NULL);
742     evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
743     elm_object_part_text_set(popup, "title,text", "Enter New Region Value");
744
745     layout = elm_layout_add(popup);
746     elm_layout_file_set(layout, ELM_DEMO_EDJ, "popup_region_text");
747     evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
748     elm_object_content_set(popup, layout);
749
750     entry = elm_entry_add(layout);
751     elm_entry_single_line_set(entry, EINA_TRUE);
752     elm_entry_scrollable_set(entry, EINA_TRUE);
753     evas_object_size_hint_weight_set(entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
754     evas_object_size_hint_align_set(entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
755     eext_entry_selection_back_event_allow_set(entry, EINA_TRUE);
756     elm_object_part_text_set(entry, "elm.guide", "region value");
757     elm_object_part_content_set(layout, "elm.swallow.content" , entry);
758
759     region_popup_fields *popup_fields;
760     popup_fields = (region_popup_fields *)malloc(sizeof(region_popup_fields));
761     if (NULL == popup_fields)
762     {
763         dlog_print(DLOG_INFO, LOG_TAG, "#### Memory allocation failed");
764         popup_fields->popup = NULL;
765         popup_fields->entry = NULL;
766     }
767     else
768     {
769         popup_fields->popup = popup;
770         popup_fields->entry = entry;
771     }
772
773     /* Cancel button */
774     btn = elm_button_add(popup);
775     elm_object_style_set(btn, "popup");
776     elm_object_text_set(btn, "Cancel");
777     elm_object_part_content_set(popup, "button1", btn);
778     evas_object_smart_callback_add(btn, "clicked", popup_cancel_clicked_cb, popup_fields);
779
780     /* Set button */
781     btn = elm_button_add(popup);
782     elm_object_style_set(btn, "popup");
783     elm_object_text_set(btn, "Set");
784     elm_object_part_content_set(popup, "button2", btn);
785     evas_object_smart_callback_add(btn, "clicked", popup_set_clicked_cb, popup_fields);
786
787     evas_object_show(popup);
788 }
789
790 static void
791 list_selected_cb(void *data, Evas_Object *obj, void *event_info)
792 {
793     Elm_Object_Item *it = (Elm_Object_Item *)event_info;
794     elm_list_item_selected_set(it, EINA_FALSE);
795 }
796
797 void
798 test_fn(void *data, Evas_Object *obj, void *event_info)
799 {
800     if (NULL != log_entry)
801     {
802         elm_entry_entry_append(log_entry, "First item selected");
803         elm_entry_cursor_end_set(log_entry);
804     }
805     else
806     {
807         dlog_print(DLOG_ERROR, "test_fn", "log_entry object is NULL");
808     }
809 }
810
811 static Eina_Bool
812 naviframe_pop_cb(void *data, Elm_Object_Item *it)
813 {
814     onDestroyConfigure();
815     if (NULL != log_entry)
816     {
817         evas_object_del(log_entry);
818         log_entry = NULL;
819     }
820     return EINA_TRUE;
821 }
822
823 // Method to be called when configuration API UI button is selected
824 void
825 configuration_cb(void *data, Evas_Object *obj, void *event_info)
826 {
827     Evas_Object *layout;
828     Evas_Object *scroller;
829     Evas_Object *list;
830     Evas_Object *nf = (Evas_Object *)data;
831     Elm_Object_Item *nf_it;
832
833     // Scroller
834     scroller = elm_scroller_add(nf);
835     elm_scroller_bounce_set(scroller, EINA_FALSE, EINA_TRUE);
836     elm_scroller_policy_set(scroller, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
837
838     // Layout
839     layout = elm_layout_add(nf);
840     elm_layout_file_set(layout, ELM_DEMO_EDJ, "configuration_layout");
841     evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
842
843     elm_object_content_set(scroller, layout);
844
845     // List
846     list = elm_list_add(layout);
847     elm_list_mode_set(list, ELM_LIST_COMPRESS);
848     evas_object_smart_callback_add(list, "selected", list_selected_cb, NULL);
849     elm_object_part_content_set(layout, "list", list);
850     elm_list_go(list);
851
852     // log_entry - text area for log
853     log_entry = elm_entry_add(layout);
854     elm_entry_scrollable_set(log_entry, EINA_TRUE);
855     elm_entry_editable_set(log_entry, EINA_FALSE);
856     elm_object_part_text_set(log_entry, "elm.guide", "logs will be updated here!!!");
857     evas_object_size_hint_weight_set(log_entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
858     evas_object_size_hint_align_set(log_entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
859     elm_object_part_content_set(layout, "log", log_entry);
860
861     nf_it = elm_naviframe_item_push(nf, "configuration APIs", NULL, NULL, scroller, NULL);
862     elm_naviframe_item_pop_cb_set(nf_it, naviframe_pop_cb, NULL);
863
864     onStartConfigure();
865
866     // Shows the UI list of group APIs
867     elm_list_item_append(list, "Find All Groups", NULL, NULL, findAllGroups, NULL);
868     elm_list_item_append(list, "Find All Resources",
869                          NULL, NULL, findAllResources, NULL);
870     elm_list_item_append(list, "Get a Configuration Resource", NULL, NULL, getConfiguration, NULL);
871     elm_list_item_append(list, "Update Attribute (Region)", NULL, NULL,
872                          list_update_region_cb, nf);
873     elm_list_item_append(list, "Factory Reset", NULL, NULL, factoryReset, NULL);
874     elm_list_item_append(list, "Reboot", NULL, NULL, reboot, NULL);
875     elm_list_item_append(list, "Get Supported Configuration Units", NULL, NULL,
876                          getListOfSupportedConfigurationUnits, NULL);
877
878     elm_list_go(list);
879 }