1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
21 #include "tmsampleapp.h"
28 int isWaiting = 0; // 0: none to wait, 1: wait for the response of "getConfigurationValue"
29 const int SUCCESS_RESPONSE = 0;
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
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
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
46 std::map< std::string, std::shared_ptr< OCResource > > resourceTable;
47 std::vector< OCResourceHandle > configResourceHandleVector;
49 typedef std::string ConfigurationName;
50 typedef std::string ConfigurationValue;
52 static Evas_Object *log_entry = NULL;
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";
61 string CONFIGURATION_RESOURCE_URI = "/oic/con";
62 string MAINTENANCE_RESOURCE_URI = "/oic/mnt";
63 string FACTORYSET_RESOURCE_URI = "/factoryset";
65 GroupManager *g_groupManager = nullptr;
66 ThingsConfiguration *g_thingsConfig = nullptr;
67 ThingsMaintenance *g_thingsMnt = nullptr;
70 typedef struct region_popup
74 } region_popup_fields;
76 void *updateConfigLog(void *data)
78 string *log = (string *)data;
80 elm_entry_entry_append(log_entry, (*log).c_str());
81 elm_entry_cursor_end_set(log_entry);
85 // Callback to found collection resource
86 void onFoundCollectionResource(std::vector< std::shared_ptr< OCResource > > resources)
88 dlog_print(DLOG_INFO, LOG_TAG, "#### onFoundCollectionResource ENTRY!!!!");
91 // Do some operations with resource object.
92 for (unsigned int i = 0; i < resources.size(); ++i)
95 std::shared_ptr< OCResource > resource = resources.at(i);
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,
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;
123 // Resource is invalid
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,
132 catch (std::exception &e)
134 dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
137 dlog_print(DLOG_INFO, LOG_TAG, "#### onFoundCollectionResource EXIT!!!!");
140 // Callback to found candidate resources
141 void onFoundCandidateResource(std::vector< std::shared_ptr< OCResource > > resources)
143 dlog_print(DLOG_INFO, LOG_TAG, "#### onFoundCandidateCollection ENTRY!!!!");
146 // Do some operations with resource object.
147 for (unsigned int i = 0; i < resources.size(); ++i)
150 std::shared_ptr< OCResource > resource = resources.at(i);
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());
157 if (iter == resourceTable.end()) // new one
159 resourceTable[resource->host() + resource->uri()] = resource;
161 OCResourceHandle foundResourceHandle = NULL;
162 OCStackResult result = OCPlatform::registerResource(foundResourceHandle,
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)
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,
181 if (resource->uri() == CONFIGURATION_RESOURCE_URI &&
182 NULL != configurationCollectionHandle)
184 OCPlatform::bindResource(configurationCollectionHandle,
185 foundResourceHandle);
186 configurationFoundHandle = foundResourceHandle;
187 if (NULL == g_configurationResource)
189 dlog_print(DLOG_INFO, LOG_TAG, "g_configurationResource updated");
190 g_configurationResource = resource;
194 dlog_print(DLOG_INFO, LOG_TAG,
195 "g_configurationResource is not null");
198 else if (resource->uri() == MAINTENANCE_RESOURCE_URI &&
199 NULL != maintenanceCollectionHandle)
201 OCPlatform::bindResource(maintenanceCollectionHandle,
202 foundResourceHandle);
203 maintenanceFoundHandle = foundResourceHandle;
204 if (NULL == g_maintenanceResource)
206 dlog_print(DLOG_INFO, LOG_TAG,
207 "g_maintenanceResource updated");
208 g_maintenanceResource = resource;
212 dlog_print(DLOG_INFO, LOG_TAG,
213 "g_maintenanceResource is not null");
216 else if (resource->uri() == FACTORYSET_RESOURCE_URI &&
217 NULL != setCollectionHandle)
219 OCPlatform::bindResource(setCollectionHandle, foundResourceHandle);
220 setFoundHandle = foundResourceHandle;
221 if (NULL == g_setResource)
223 dlog_print(DLOG_INFO, LOG_TAG, "g_setResource updated");
224 g_setResource = resource;
228 dlog_print(DLOG_INFO, LOG_TAG, "g_setResource is not null");
231 configResourceHandleVector.push_back(foundResourceHandle);
235 logMessage = "Resource Error!";
236 dlog_print(DLOG_INFO, LOG_TAG, "Resource Error!");
237 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateConfigLog,
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,
252 catch (std::exception &e)
254 dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
258 dlog_print(DLOG_INFO, LOG_TAG, "#### onFoundCandidateCollection EXIT!!!!");
261 // Callback to updateConfiguration
262 static void onUpdateConfigurationsCallback(const HeaderOptions &headerOptions,
263 const OCRepresentation &rep,
266 dlog_print(DLOG_INFO, LOG_TAG, "#### onUpdateConfigurationsCallback: ENTRY!!!!");
269 if (SUCCESS_RESPONSE != eCode)
271 dlog_print(DLOG_ERROR, LOG_TAG, "#### onUpdateConfigurationsCallback: "
272 "ERROR RESPONSE!!!!");
276 string logMessage = "Resource URI: " + rep.getUri() + "<br>";
277 dlog_print(DLOG_INFO, LOG_TAG, "#### Resource URI: %s", rep.getUri().c_str());
279 if (rep.hasAttribute(DEFAULT_DEVICENAME))
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>";
286 if (rep.hasAttribute(DEFAULT_LOCATION))
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>";
293 if (rep.hasAttribute(DEFAULT_LOCATIONNAME))
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>";
300 if (rep.hasAttribute(DEFAULT_CURRENCY))
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>";
307 if (rep.hasAttribute(DEFAULT_REGION))
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>";
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,
318 dlog_print(DLOG_INFO, LOG_TAG, "#### onUpdateConfigurationsCallback: EXIT!!!!");
321 // Callback to getConfiguration
322 static void onGetConfigurationsCallback(const HeaderOptions &headerOptions,
323 const OCRepresentation &rep,
326 dlog_print(DLOG_INFO, LOG_TAG, "#### onGetConfigurationsCallback: enter!!!!");
329 if (SUCCESS_RESPONSE != eCode)
331 dlog_print(DLOG_ERROR, LOG_TAG, "#### onGetConfigurationsCallback: "
332 "ERROR RESPONSE!!!!");
336 string logMessage = "Resource URI: " + rep.getUri() + "<br>";
337 dlog_print(DLOG_INFO, LOG_TAG, "#### Resource URI: %s", rep.getUri().c_str());
339 if (rep.hasAttribute(DEFAULT_DEVICENAME))
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>";
346 if (rep.hasAttribute(DEFAULT_LOCATION))
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>";
353 if (rep.hasAttribute(DEFAULT_LOCATIONNAME))
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>";
360 if (rep.hasAttribute(DEFAULT_CURRENCY))
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>";
367 if (rep.hasAttribute(DEFAULT_REGION))
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>";
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!!!!");
381 static void onFactoryReset(const HeaderOptions &headerOptions, const OCRepresentation &rep,
384 dlog_print(DLOG_INFO, LOG_TAG, "#### onFactoryReset: enter!!!!");
387 if (SUCCESS_RESPONSE != eCode)
389 dlog_print(DLOG_ERROR, LOG_TAG, "#### onFactoryReset: ERROR RESPONSE!!!!");
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!!!!");
405 static void onReboot(const HeaderOptions &headerOptions, const OCRepresentation &rep,
408 dlog_print(DLOG_INFO, LOG_TAG, "#### onReboot: enter!!!!");
411 if (SUCCESS_RESPONSE != eCode)
413 dlog_print(DLOG_ERROR, LOG_TAG, "#### onReboot: ERROR RESPONSE!!!!");
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!!!!");
429 static void createResourceCollection(string uri, string typeName, OCResourceHandle &tempHandle)
431 dlog_print(DLOG_INFO, LOG_TAG, "#### createResourceCollection: enter!!!!");
432 if (NULL != tempHandle)
434 dlog_print(DLOG_INFO, LOG_TAG, "#### Resource already exists!!!!");
438 // Create resource collection
439 OCPlatform::registerResource(tempHandle, uri, typeName, BATCH_INTERFACE, NULL,
441 OCPlatform::bindInterfaceToResource(tempHandle, GROUP_INTERFACE);
442 OCPlatform::bindInterfaceToResource(tempHandle, DEFAULT_INTERFACE);
444 dlog_print(DLOG_INFO, LOG_TAG, "#### Resource created : %s", typeName.c_str());
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!!!!");
453 // Unbinds and unregisters all resources
454 static void deleteResource(OCResourceHandle &tempCollectionHandle,
455 OCResourceHandle &tempResourceHandle)
457 dlog_print(DLOG_INFO, LOG_TAG, "#### calling deleteResources ENTRY!!!!");
459 if (NULL == tempCollectionHandle || NULL == tempResourceHandle)
461 dlog_print(DLOG_INFO, LOG_TAG, "#### Collection or resource does not exists");
467 OCPlatform::unbindResource(tempCollectionHandle, tempResourceHandle);
468 dlog_print(DLOG_INFO, LOG_TAG, "#### unbindResource DONE!!!!");
470 catch (std::exception &e)
472 dlog_print(DLOG_ERROR, LOG_TAG, "Exception Occured! (%s)", e.what());
477 OCPlatform::unregisterResource(tempResourceHandle);
478 dlog_print(DLOG_INFO, LOG_TAG, "#### unregisterResource DONE!!!!");
480 catch (std::exception &e)
482 dlog_print(DLOG_ERROR, LOG_TAG, "Exception Occured! (%s)", e.what());
485 dlog_print(DLOG_INFO, LOG_TAG, "#### calling deleteResources EXIT!!!!");
488 static void findAllGroups(void *data, Evas_Object *obj, void *event_info)
490 dlog_print(DLOG_INFO, LOG_TAG, "#### calling findCandidateResources ENTRY!!!!");
491 std::vector<string> resourceTypes;
492 resourceTypes.push_back(CONFIGURATION_COLLECTION_RESOURCE_TYPE);
494 if (NULL != g_groupManager)
496 g_groupManager->findCandidateResources(resourceTypes, &onFoundCollectionResource,
500 resourceTypes.clear();
501 resourceTypes.push_back(MAINTENANCE_COLLECTION_RESOURCE_TYPE);
502 if (NULL != g_groupManager)
504 g_groupManager->findCandidateResources(resourceTypes, &onFoundCollectionResource,
508 resourceTypes.clear();
509 resourceTypes.push_back(FACTORYSET_COLLECTION_RESOURCE_TYPE);
510 if (NULL != g_groupManager)
512 g_groupManager->findCandidateResources(resourceTypes, &onFoundCollectionResource,
516 dlog_print(DLOG_INFO, LOG_TAG, "#### calling findCandidateResources EXIT!!!!");
519 static void findAllResources(void *data, Evas_Object *obj, void *event_info)
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");
527 if (NULL != g_groupManager)
529 g_groupManager->findCandidateResources(resourceTypes, &onFoundCandidateResource,
530 FINDRESOURCE_TIMEOUT);
533 dlog_print(DLOG_INFO, LOG_TAG, "#### calling findCandidateResources EXIT!!!!");
536 static void getConfiguration(void *data, Evas_Object *obj, void *event_info)
538 dlog_print(DLOG_INFO, LOG_TAG, "#### getConfiguration ENTRY!!!!");
539 if (NULL == g_configurationCollection || NULL == g_configurationCollection.get())
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);
548 if (NULL == g_configurationResource)
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);
557 ConfigurationName name = "all";
558 std::vector< ConfigurationName > configurations;
559 configurations.push_back(name);
563 g_thingsConfig->getConfigurations(g_configurationCollection, configurations,
564 &onGetConfigurationsCallback);
568 catch (std::exception &e)
570 dlog_print(DLOG_ERROR, LOG_TAG, "Exception is occured! (%s)", e.what());
574 dlog_print(DLOG_INFO, LOG_TAG, "#### getConfiguration EXIT!!!!");
577 // Updates the configuration i.e. Device name to OIC Device
578 static void updateConfiguration(std::string newDeviceName)
580 dlog_print(DLOG_INFO, LOG_TAG, "#### updateConfiguration ENTRY!!!!");
581 dlog_print(DLOG_INFO, LOG_TAG, "#### %s", newDeviceName.c_str());
583 if (NULL == g_configurationCollection || NULL == g_configurationCollection.get())
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);
593 if (NULL == g_configurationResource)
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);
602 OCStackResult result;
603 ConfigurationName name = DEFAULT_DEVICENAME;
604 ConfigurationValue value = newDeviceName;
606 std::map< ConfigurationName, ConfigurationValue > configurations;
607 configurations.insert(std::make_pair(name, value));
611 result = g_thingsConfig->updateConfigurations(g_configurationCollection, configurations,
612 &onUpdateConfigurationsCallback);
614 catch (std::exception &e)
616 dlog_print(DLOG_ERROR, LOG_TAG, "Exception is occured! (%s)", e.what());
619 if (OC_STACK_OK == result)
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);
627 dlog_print(DLOG_INFO, LOG_TAG, "#### updateConfiguration EXIT!!!!");
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)
633 dlog_print(DLOG_INFO, LOG_TAG, "#### factoryReset ENTRY!!!!");
634 if (NULL == g_maintenanceCollection || NULL == g_maintenanceCollection.get())
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);
644 if (NULL == g_maintenanceResource)
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);
653 OCStackResult result;
657 result = g_thingsMnt->factoryReset(g_maintenanceCollection, &onFactoryReset);
659 catch (std::exception &e)
661 dlog_print(DLOG_ERROR, LOG_TAG, "Exception is occured! (%s)", e.what());
664 if (OC_STACK_OK == result)
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);
672 dlog_print(DLOG_INFO, LOG_TAG, "#### factoryReset EXIT!!!!");
675 // Reboots the server
676 static void reboot(void *data, Evas_Object *obj, void *event_info)
678 dlog_print(DLOG_INFO, LOG_TAG, "#### reboot ENTRY!!!!");
679 if (NULL == g_maintenanceCollection || NULL == g_maintenanceCollection.get())
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);
689 if (NULL == g_maintenanceResource)
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);
698 OCStackResult result;
702 result = g_thingsMnt->reboot(g_maintenanceCollection, &onReboot);
704 catch (std::exception &e)
706 dlog_print(DLOG_ERROR, LOG_TAG, "Exception is occured! (%s)", e.what());
709 if (OC_STACK_OK == result)
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);
716 dlog_print(DLOG_INFO, LOG_TAG, "#### reboot EXIT!!!!");
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)
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());
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!!!!");
736 // Creates all the resources
737 static void onStartConfigure()
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);
749 g_groupManager = new GroupManager();
750 g_thingsConfig = new ThingsConfiguration();
751 g_thingsMnt = new ThingsMaintenance();
754 // Deletes all the resources
755 static void onDestroyConfigure()
757 dlog_print(DLOG_INFO, LOG_TAG, "#### Destroy sequence called");
759 deleteResource(setCollectionHandle, setFoundHandle);
761 deleteResource(maintenanceCollectionHandle, maintenanceFoundHandle);
763 deleteResource(configurationCollectionHandle, configurationFoundHandle);
766 delete g_thingsConfig;
767 delete g_groupManager;
769 dlog_print(DLOG_INFO, LOG_TAG, "#### Resources destroyed successfully");
773 popup_cancel_clicked_cb(void *data, Evas_Object *obj, void *event_info)
775 region_popup_fields *popup_fields = (region_popup_fields *)data;
776 evas_object_del(popup_fields->popup);
781 popup_set_clicked_cb(void *data, Evas_Object *obj, void *event_info)
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)
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);
796 std::string regionValue = std::string(newRegionValue);
797 updateConfiguration(regionValue);
799 evas_object_del(popup_fields->popup);
804 list_update_devicename_cb(void *data, Evas_Object *obj, void *event_info)
806 if (NULL == g_configurationCollection || NULL == g_configurationCollection.get())
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);
815 Evas_Object *popup, *btn;
816 Evas_Object *nf = (Evas_Object *)data;
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");
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);
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);
841 region_popup_fields *popup_fields;
842 popup_fields = (region_popup_fields *)malloc(sizeof(region_popup_fields));
843 if (NULL == popup_fields)
845 dlog_print(DLOG_INFO, LOG_TAG, "#### Memory allocation failed");
849 popup_fields->popup = popup;
850 popup_fields->entry = entry;
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);
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);
867 evas_object_show(popup);
871 list_selected_cb(void *data, Evas_Object *obj, void *event_info)
873 Elm_Object_Item *it = (Elm_Object_Item *)event_info;
874 elm_list_item_selected_set(it, EINA_FALSE);
878 test_fn(void *data, Evas_Object *obj, void *event_info)
880 if (NULL != log_entry)
882 elm_entry_entry_append(log_entry, "First item selected");
883 elm_entry_cursor_end_set(log_entry);
887 dlog_print(DLOG_ERROR, "test_fn", "log_entry object is NULL");
892 naviframe_pop_cb(void *data, Elm_Object_Item *it)
894 onDestroyConfigure();
895 resourceTable.clear();
896 if (NULL != log_entry)
898 evas_object_del(log_entry);
904 // Method to be called when configuration API UI button is selected
906 configuration_cb(void *data, Evas_Object *obj, void *event_info)
909 Evas_Object *scroller;
911 Evas_Object *nf = (Evas_Object *)data;
912 Elm_Object_Item *nf_it;
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);
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);
924 elm_object_content_set(scroller, layout);
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);
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);
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);
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);