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