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