Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / tizen / TMSampleApp / src / group.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 <algorithm>
22
23 #include "tmsampleapp.h"
24 #include "tmutil.h"
25
26 using namespace std;
27 using namespace OC;
28 using namespace OIC;
29
30 namespace PH = std::placeholders;
31
32 static Evas_Object *log_entry = NULL;
33 static Evas_Object *list = NULL;
34 static Evas_Object *naviframe = NULL;
35
36 string BULBOFF              = "AllBulbOff";
37 string BULBON               = "AllBulbOn";
38 string resourceURI          = "/core/b/collection";
39 string resourceTypeName     = "b.collection";
40
41 std::vector< OCResourceHandle > groupResourceHandleVector;
42 OCResourceHandle resourceHandle = NULL;
43 OCResourceHandle foundResourceHandle = NULL;
44 shared_ptr< OCResource > g_resource ;
45 std::vector< string > lights;
46
47 ThingsManager *groupThingsMgr = new ThingsManager();
48
49 typedef struct datetime_popup
50 {
51     Evas_Object *popup;
52     Evas_Object *entry;
53 } datetime_popup_fields;
54
55 // Function to update the log in UI
56 void *updateGroupLog(void *data)
57 {
58     string *log = (string *)data;
59     // Show the log
60     elm_entry_entry_append(log_entry, (*log).c_str());
61     elm_entry_cursor_end_set(log_entry);
62     return NULL;
63 }
64
65 void onPut(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
66 {
67     string logMessage;
68     dlog_print(DLOG_INFO, LOG_TAG, "#### onPut Callback Received!!!!");
69     logMessage += "API Result: Success<br>onPut Callback Received<br>";
70     if (OC_STACK_OK == eCode)
71     {
72         dlog_print(DLOG_INFO, LOG_TAG, "#### Result is OK");
73     }
74     else
75     {
76         dlog_print(DLOG_INFO, LOG_TAG, "#### Invalid Parameter");
77     }
78     logMessage += "----------------------<br>";
79     dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
80     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
81                                           &logMessage);
82 }
83
84 void onPost(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
85 {
86     dlog_print(DLOG_INFO, LOG_TAG, "#### onPost callback received ENTRY!!!!");
87     string logMessage;
88     logMessage += "API Result: Success<br>onPost Callback Received<br>";
89
90     if (rep.hasAttribute("ActionSet"))
91     {
92         string plainText;
93         if (rep.getValue("ActionSet", plainText))
94         {
95             ActionSet *actionset = groupThingsMgr->getActionSetfromString(plainText);
96             if (NULL != actionset)
97             {
98                 dlog_print(DLOG_INFO, LOG_TAG, "#### ACTIONSET NAME :: (%s)",
99                            actionset->actionsetName.c_str());
100                 logMessage += "ACTIONSET NAME :: " + actionset->actionsetName + "<br>";
101                 for (auto actIter = actionset->listOfAction.begin();
102                      actIter != actionset->listOfAction.end(); ++actIter)
103                 {
104                     dlog_print(DLOG_INFO, LOG_TAG, "#### TARGET :: (%s)",
105                                (*actIter)->target.c_str());
106                     logMessage += logMessage + "TARGET :: " + (*actIter)->target + "<br>";
107                     for (auto capaIter = (*actIter)->listOfCapability.begin();
108                          capaIter != (*actIter)->listOfCapability.end(); ++capaIter)
109                     {
110                         dlog_print(DLOG_INFO, LOG_TAG, "#### POWER :: (%s)",
111                                    (*capaIter)->status.c_str());
112                         logMessage += logMessage + "CAPABILITY :: " +
113                                       (*capaIter)->status + "<br>";
114                     }
115                 }
116             }
117             delete actionset;
118         }
119     }
120     else if (rep.hasAttribute("DoAction"))
121     {
122         string plainText;
123         if (rep.getValue("DoAction", plainText))
124         {
125             logMessage += plainText + "<br>";
126             dlog_print(DLOG_INFO, LOG_TAG, "#### DO ACTION :: (%s)", plainText.c_str());
127
128         }
129     }
130     logMessage += "----------------------<br>";
131     dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
132     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
133                                           &logMessage);
134     dlog_print(DLOG_INFO, LOG_TAG, "#### onPost callback received EXIT!!!!");
135 }
136
137 // Method for Creating the action Set AllBulbOff
138 static void createActionSet_AllBulbOff()
139 {
140     dlog_print(DLOG_INFO, LOG_TAG, "#### createActionSet_AllBulbOff ENTRY");
141
142     OIC::ActionSet *actionSet = new OIC::ActionSet();
143     actionSet->actionsetName = BULBOFF;
144     int size = lights.size();
145
146     if (0 == size)
147     {
148         string logMessage = "NO LIGHTSERVER FOUND <br>";
149         logMessage += "----------------------<br>";
150         dlog_print(DLOG_INFO, LOG_TAG, "#### NO LIGHT SERVER FOUND");
151         dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
152         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
153                                               &logMessage);
154         delete actionSet;
155         return;
156     }
157
158     for (int i = 0; i < size; i++)
159     {
160         OIC::Action *action = new OIC::Action();
161         action->target = lights.at(i);
162
163         OIC::Capability *capability = new OIC::Capability();
164         capability->capability = "power";
165         capability->status = "off";
166
167         action->listOfCapability.push_back(capability);
168         actionSet->listOfAction.push_back(action);
169     }
170     dlog_print(DLOG_INFO, LOG_TAG, "#### G_URI: %s", g_resource->uri().c_str());
171     dlog_print(DLOG_INFO, LOG_TAG, "#### G_HOST: %S", g_resource->host().c_str());
172
173     try
174     {
175         if (g_resource)
176         {
177             groupThingsMgr->addActionSet(g_resource, actionSet, onPut);
178         }
179     }
180     catch (std::exception &e)
181     {
182         dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
183     }
184     delete actionSet;
185
186     string logMessage = "Create actionset AllBulbOFF success <br>";
187     logMessage += "----------------------<br>";
188     dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
189     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
190     dlog_print(DLOG_INFO, LOG_TAG, "#### createActionSet_AllBulbOff EXIT");
191 }
192
193 /* Method for Creating the action Set AllBulbOn
194    once we create the ActionSet we can execute Action Set using executeActionSetOn()
195    or delete using deleteActionSetOn() */
196 static void createActionSet_AllBulbOn()
197 {
198     dlog_print(DLOG_INFO, LOG_TAG, "#### createActionSet_AllBulbOn ENTRY");
199
200     OIC::ActionSet *actionSet = new OIC::ActionSet();
201     actionSet->actionsetName = BULBON;
202     int size = lights.size();
203
204     if (0 == size)
205     {
206         string logMessage = "NO LIGHTSERVER FOUND <br>";
207         logMessage += "----------------------<br>";
208         dlog_print(DLOG_INFO, LOG_TAG, "#### NO LIGHT SERVER FOUND");
209         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
210         delete actionSet;
211         return;
212     }
213
214     for (int i = 0; i < size; i++)
215     {
216         OIC::Action *action = new OIC::Action();
217         action->target = lights.at(i);
218
219         OIC::Capability *capability = new OIC::Capability();
220         capability->capability = "power";
221         capability->status = "on";
222
223         action->listOfCapability.push_back(capability);
224         actionSet->listOfAction.push_back(action);
225     }
226     string URI = g_resource->uri();
227     string host = g_resource->host();
228     dlog_print(DLOG_INFO, LOG_TAG, "#### G_URI: %s", g_resource->uri().c_str());
229     dlog_print(DLOG_INFO, LOG_TAG, "#### G_HOST: %S", g_resource->host().c_str());
230
231     try
232     {
233         if (g_resource)
234         {
235             groupThingsMgr->addActionSet(g_resource, actionSet, onPut);
236         }
237     }
238     catch (std::exception &e)
239     {
240         dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
241     }
242     delete actionSet;
243
244     string logMessage = "Create actionset AllBulbON success <br>";
245     logMessage += "----------------------<br>";
246     dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
247     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
248     dlog_print(DLOG_INFO, LOG_TAG, "#### createActionSet_AllBulbOff OFF EXIT");
249 }
250
251 static void createRecursiveActionSet_AllBulbOn(void *data, Evas_Object *obj, void *event_info)
252 {
253     dlog_print(DLOG_INFO, LOG_TAG, "#### createRecursiveActionSet_AllBulbOn ENTRY");
254     string actionsetDesc;
255     ActionSet *allBulbOn = new ActionSet();
256     allBulbOn->type = OIC::ACTIONSET_TYPE::RECURSIVE;
257
258     allBulbOn->actionsetName = "AllBulbOnRecursiveCall";
259     allBulbOn->mTime.tm_year = 0;
260     allBulbOn->mTime.tm_mon = 0;
261     allBulbOn->mTime.tm_mday = 0;
262     allBulbOn->mTime.tm_hour = 0;
263     allBulbOn->mTime.tm_min = 0;
264     allBulbOn->mTime.tm_sec = 5;
265
266     allBulbOn->setDelay(allBulbOn->getSecAbsTime());
267
268     for (auto iter = lights.begin(); iter != lights.end(); ++iter)
269     {
270         Action *action = new Action();
271         action->target = (*iter);
272
273         Capability *capa = new Capability();
274         capa->capability = "power";
275         capa->status = "on";
276
277         action->listOfCapability.push_back(capa);
278         allBulbOn->listOfAction.push_back(action);
279     }
280     if (g_resource)
281     {
282         groupThingsMgr->addActionSet(g_resource, allBulbOn, onPut);
283     }
284
285     delete allBulbOn;
286     dlog_print(DLOG_INFO, LOG_TAG, "#### createRecursiveActionSet_AllBulbOn EXIT");
287 }
288
289 static void createScheduledActionSet_AllBulbOff(int date, int month, int year,
290         int hour, int minute, int second)
291 {
292     dlog_print(DLOG_INFO, LOG_TAG, "#### createScheduledActionSet_AllBulbOff ENTRY");
293     string actionsetDesc;
294     ActionSet *allBulbOff = new ActionSet();
295     allBulbOff->type = OIC::ACTIONSET_TYPE::SCHEDULED;
296     allBulbOff->actionsetName = "AllBulbOffScheduledCall";
297     allBulbOff->mTime.tm_year = year;
298     allBulbOff->mTime.tm_mon = month;
299     allBulbOff->mTime.tm_mday = date;
300     allBulbOff->mTime.tm_hour = hour;
301     allBulbOff->mTime.tm_min = minute;
302     allBulbOff->mTime.tm_sec = second;
303     dlog_print(DLOG_INFO, LOG_TAG, "#### allBulbOff->mTime.tm_year :: %ld",
304                allBulbOff->mTime.tm_year);
305     dlog_print(DLOG_INFO, LOG_TAG, "#### allBulbOff->mTime.tm_mon :: %ld",
306                allBulbOff->mTime.tm_mon);
307     dlog_print(DLOG_INFO, LOG_TAG, "#### allBulbOff->mTime.tm_mday :: %ld",
308                allBulbOff->mTime.tm_mday);
309     dlog_print(DLOG_INFO, LOG_TAG, "#### allBulbOff->mTime.tm_hour :: %ld",
310                allBulbOff->mTime.tm_hour);
311     dlog_print(DLOG_INFO, LOG_TAG, "#### allBulbOff->mTime.tm_min :: %ld",
312                allBulbOff->mTime.tm_min);
313     dlog_print(DLOG_INFO, LOG_TAG, "#### allBulbOff->mTime.tm_sec :: %ld",
314                allBulbOff->mTime.tm_sec);
315
316     allBulbOff->setDelay(allBulbOff->getSecondsFromAbsoluteTime());
317
318     for (auto iter = lights.begin(); iter != lights.end(); ++iter)
319     {
320         Action *action = new Action();
321         action->target = (*iter);
322
323         Capability *capa = new Capability();
324         capa->capability = "power";
325         capa->status = "off";
326
327         action->listOfCapability.push_back(capa);
328         allBulbOff->listOfAction.push_back(action);
329     }
330     if (g_resource)
331     {
332         groupThingsMgr->addActionSet(g_resource, allBulbOff, onPut);
333     }
334
335     delete allBulbOff;
336     dlog_print(DLOG_INFO, LOG_TAG, "#### createScheduledActionSet_AllBulbOff EXIT");
337 }
338
339 static void scheduled_AllbulbOff(void *data, Evas_Object *obj, void *event_info)
340 {
341     groupThingsMgr->executeActionSet(g_resource, "AllBulbOffScheduledCall", &onPost);
342 }
343
344 static void scheduled_AllbulbOffEx(void *data, Evas_Object *obj, void *event_info)
345 {
346     groupThingsMgr->executeActionSet(g_resource, "AllBulbOffScheduledCall", 10, &onPost);
347 }
348
349 static void cancelScheduled_AllBulbOff(void *data, Evas_Object *obj, void *event_info)
350 {
351     groupThingsMgr->cancelActionSet(g_resource, "AllBulbOffScheduledCall", &onPost);
352 }
353
354 static void recursive_allBulbOn(void *data, Evas_Object *obj, void *event_info)
355 {
356     groupThingsMgr->executeActionSet(g_resource, "AllBulbOnRecursiveCall", &onPost);
357 }
358
359 static void recursive_allBulbOnEx(void *data, Evas_Object *obj, void *event_info)
360 {
361     groupThingsMgr->executeActionSet(g_resource, "AllBulbOnRecursiveCall", 10, &onPost);
362 }
363
364 static void cancelRecursive_allBulbOn(void *data, Evas_Object *obj, void *event_info)
365 {
366
367     groupThingsMgr->cancelActionSet(g_resource, "AllBulbOnRecursiveCall", &onPost);
368 }
369
370 /* Method for executing the action Set AllBulbOff that we have created using
371    createActionSet_AllBulbOff() */
372 static void executeActionSetOff(void *data, Evas_Object *obj, void *event_info)
373 {
374     dlog_print(DLOG_INFO, LOG_TAG, "#### executeActionSetOff ENTRY");
375     int size = lights.size();
376
377     if (0 == size)
378     {
379         string logMessage = "NO LIGHTSERVER FOUND <br>";
380         logMessage += "----------------------<br>";
381         dlog_print(DLOG_INFO, LOG_TAG, "#### NO LIGHT SERVER FOUND");
382         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
383         return;
384     }
385
386     try
387     {
388         if (g_resource)
389         {
390             groupThingsMgr->executeActionSet(g_resource, BULBOFF, &onPost);
391         }
392     }
393     catch (std::exception &e)
394     {
395         dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
396     }
397
398     string logMessage = "Actionset OFF called successfully <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 *))updateGroupLog, &logMessage);
402     dlog_print(DLOG_INFO, LOG_TAG, "#### executeActionSetOff EXIT");
403 }
404
405 /* Method for executing the action Set AllBulbOn that we have created using
406    createActionSet_AllBulbOn() */
407 static void executeActionSetOn(void *data, Evas_Object *obj, void *event_info)
408 {
409     dlog_print(DLOG_INFO, LOG_TAG, "#### executeActionSetOn ENTRY");
410     int size = lights.size();
411
412     if (0 == size)
413     {
414         string logMessage = "NO LIGHTSERVER FOUND <br>";
415         logMessage += "----------------------<br>";
416         dlog_print(DLOG_INFO, LOG_TAG, "#### NO LIGHT SERVER FOUND");
417         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
418         return;
419     }
420
421     try
422     {
423         if (g_resource)
424         {
425             groupThingsMgr->executeActionSet(g_resource, BULBON, &onPost);
426         }
427     }
428     catch (std::exception &e)
429     {
430         dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
431     }
432
433     string logMessage = "Actionset ON called successfully <br>";
434     logMessage += "----------------------<br>";
435     dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
436     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
437     dlog_print(DLOG_INFO, LOG_TAG, "#### executeActionSetOn EXIT");
438 }
439
440 /* Method for getting the action Set AllBulbOff that we have created using
441    createActionSet_AllBulbOff() */
442 static void getActionSetOff(void *data, Evas_Object *obj, void *event_info)
443 {
444     dlog_print(DLOG_INFO, LOG_TAG, "#### getActionSetOff ENTRY");
445     int size = lights.size();
446
447     if (0 == size)
448     {
449         string logMessage = "NO LIGHTSERVER FOUND <br>";
450         logMessage += "----------------------<br>";
451         dlog_print(DLOG_INFO, LOG_TAG, "#### NO LIGHT SERVER FOUND");
452         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
453         return;
454     }
455
456     try
457     {
458         if (g_resource)
459         {
460             groupThingsMgr->getActionSet(g_resource, BULBOFF, &onPost);
461         }
462     }
463     catch (std::exception &e)
464     {
465         dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
466     }
467
468     dlog_print(DLOG_INFO, LOG_TAG, "#### getActionSetOff EXIT");
469 }
470
471 /* Method for getting the action Set AllBulbOn that we have created using
472    createActionSet_AllBulbOn() */
473 static void getActionSetOn(void *data, Evas_Object *obj, void *event_info)
474 {
475     dlog_print(DLOG_INFO, LOG_TAG, "#### getActionSetOn ENTRY");
476     int size = lights.size();
477
478     if (0 == size)
479     {
480         string logMessage = "NO LIGHTSERVER FOUND <br>";
481         logMessage += "----------------------<br>";
482         dlog_print(DLOG_INFO, LOG_TAG, "#### NO LIGHT SERVER FOUND");
483         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
484         return;
485     }
486
487     try
488     {
489         if (g_resource)
490         {
491             groupThingsMgr->getActionSet(g_resource, BULBON, &onPost);
492         }
493     }
494     catch (std::exception &e)
495     {
496         dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
497     }
498
499     dlog_print(DLOG_INFO, LOG_TAG, "#### getActionSetOn EXIT");
500 }
501
502 /* Method for deleting the action Set AllBulbOff that we have created using
503    createActionSet_AllBulbOff() */
504 static void deleteActionSetOff(void *data, Evas_Object *obj, void *event_info)
505 {
506     dlog_print(DLOG_INFO, LOG_TAG, "#### deleteActionSetOff ENTRY");
507     int size = lights.size();
508
509     if (0 == size)
510     {
511         string logMessage = "NO LIGHTSERVER FOUND <br>";
512         logMessage += "----------------------<br>";
513         dlog_print(DLOG_INFO, LOG_TAG, "#### NO LIGHT SERVER FOUND");
514         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
515         return;
516     }
517
518     try
519     {
520         if (g_resource)
521         {
522             groupThingsMgr->deleteActionSet(g_resource, BULBOFF, &onPost);
523         }
524     }
525     catch (std::exception &e)
526     {
527         dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
528     }
529
530     string logMessage = "Actionset OFF DELETED <br>";
531     logMessage += "----------------------<br>";
532     dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
533     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
534     dlog_print(DLOG_INFO, LOG_TAG, "#### deleteActionSetOff EXIT");
535 }
536
537 /* Method for deleting the action Set AllBulbOn that we have created using
538    createActionSet_AllBulbOn() */
539 static void deleteActionSetOn(void *data, Evas_Object *obj, void *event_info)
540 {
541     dlog_print(DLOG_INFO, LOG_TAG, "#### deleteActionSetOn ENTRY");
542     int size = lights.size();
543
544     if (0 == size)
545     {
546         string logMessage = "NO LIGHTSERVER FOUND <br>";
547         logMessage += "----------------------<br>";
548         dlog_print(DLOG_INFO, LOG_TAG, "#### NO LIGHT SERVER FOUND");
549         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
550         return;
551     }
552
553     try
554     {
555         if (g_resource)
556         {
557             groupThingsMgr->deleteActionSet(g_resource, BULBON, &onPost);
558         }
559     }
560     catch (std::exception &e)
561     {
562         dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
563     }
564
565     string logMessage = "Actionset ON DELETED <br>";
566     logMessage += "----------------------<br>";
567     dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
568     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
569     dlog_print(DLOG_INFO, LOG_TAG, "#### deleteActionSetOn EXIT");
570 }
571
572 void onObserve(const HeaderOptions headerOptions, const OCRepresentation &rep, const int &eCode,
573                const int &sequenceNumber)
574 {
575     string logMessage;
576     char *buf;
577     if (OC_STACK_OK == eCode)
578     {
579         int level;
580         buf = (char *)malloc(4 * sizeof(char));
581         if (NULL == buf)
582         {
583             dlog_print(DLOG_INFO, LOG_TAG, " buf malloc failed");
584             return;
585         }
586         sprintf(buf, "%d", sequenceNumber);
587         logMessage = "OBSERVE RESULT <br>";
588         logMessage += "Sequencenumber:" + string(buf) + "<br>";
589
590         if (rep.getValue("level", level))
591         {
592             if (level == 0)
593             {
594                 createActionSet_AllBulbOn();
595                 executeActionSetOn(NULL, NULL, NULL);
596             }
597             else
598             {
599                 createActionSet_AllBulbOff();
600                 executeActionSetOff(NULL, NULL, NULL);
601             }
602         }
603         sprintf(buf, "%d", level);
604         logMessage += "level:" + string(buf) + "<br>";
605         free(buf);
606     }
607     else
608     {
609         logMessage = "onObserve error!!!";
610     }
611     logMessage += "----------------------<br>";
612     dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
613     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
614                                           &logMessage);
615 }
616
617 // Callback to be called when resources are found in the network
618 void foundResources(std::vector< std::shared_ptr< OC::OCResource > > listOfResource)
619 {
620     try
621     {
622         dlog_print(DLOG_INFO, LOG_TAG, "#### foundResources entry!!!!");
623
624         for (auto rsrc = listOfResource.begin(); rsrc != listOfResource.end(); ++rsrc)
625         {
626             string resourceURI = (*rsrc)->uri();
627             string hostAddress = (*rsrc)->host();
628
629             dlog_print(DLOG_INFO, LOG_TAG, "#### found uri: %s", resourceURI.c_str());
630             dlog_print(DLOG_INFO, LOG_TAG, "#### found host address: %s", hostAddress.c_str());
631             string logMessage = "URI: " + resourceURI + "<br>";
632             logMessage = logMessage + "Host:" + hostAddress + "<br>";
633             logMessage += "----------------------<br>";
634
635             if (resourceURI == "/a/light")
636             {
637                 bool found;
638                 found = std::find(lights.begin(), lights.end(),
639                                   hostAddress + resourceURI) != lights.end();
640                 if (found == false)
641                 {
642                     lights.push_back((hostAddress + resourceURI));
643
644                     try
645                     {
646                         dlog_print(DLOG_INFO, LOG_TAG, "#### Registering Resource");
647                         OCStackResult result = OCPlatform::registerResource(foundResourceHandle,
648                                                (*rsrc));
649                         dlog_print(DLOG_INFO, LOG_TAG, "#### %s REGISTERED", resourceURI.c_str());
650                         if (result == OC_STACK_OK)
651                         {
652                             OCPlatform::bindResource(resourceHandle, foundResourceHandle);
653                             dlog_print(DLOG_INFO, LOG_TAG, "#### Bind Resource Done");
654                             groupResourceHandleVector.push_back(foundResourceHandle);
655                         }
656                         else
657                         {
658                             dlog_print(DLOG_ERROR, LOG_TAG, "#### Register Resource ERROR");
659                         }
660                     }
661                     catch (std::exception &e)
662                     {
663                         dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
664                     }
665                 }
666             }
667             else if (resourceURI == "/core/bookmark")
668             {
669                 logMessage += "OBSERVING : " + resourceURI + "<br>";
670                 logMessage += "----------------------<br>";
671                 (*rsrc)->observe(ObserveType::Observe, QueryParamsMap(), &onObserve);
672                 dlog_print(DLOG_INFO, LOG_TAG, "#### after calling observe() for bookmark!!!!");
673             }
674             dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
675             ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
676                                                   &logMessage);
677         }
678
679         dlog_print(DLOG_INFO, LOG_TAG, "#### foundResources exit!!!!");
680     }
681     catch (...)
682     {
683         dlog_print(DLOG_INFO, LOG_TAG, "#### Exception caught in foundResources");
684     }
685 }
686
687 static void create_group()
688 {
689     if (NULL != groupThingsMgr)
690     {
691         dlog_print(DLOG_INFO, LOG_TAG, "#### calling findCandidateResources from "
692                    "create_group!!!!");
693         vector< string > types;
694         types.push_back("core.light");
695         groupThingsMgr->findCandidateResources(types, &foundResources, 5);
696     }
697
698     try
699     {
700         dlog_print(DLOG_INFO, LOG_TAG, "#### calling registerResource from create_group!!!!");
701         OCPlatform::registerResource(resourceHandle, resourceURI, resourceTypeName,
702                                      BATCH_INTERFACE, NULL,
703                                      OC_DISCOVERABLE | OC_OBSERVABLE);
704
705         if (NULL != resourceHandle)
706             dlog_print(DLOG_INFO, LOG_TAG, "#### Obtained resourceHandle from "
707                        "registerResource!!!!");
708
709         dlog_print(DLOG_INFO, LOG_TAG, "#### calling bindInterfaceToResource from "
710                    "create_group!!!!");
711         OCPlatform::bindInterfaceToResource(resourceHandle, GROUP_INTERFACE);
712         OCPlatform::bindInterfaceToResource(resourceHandle, DEFAULT_INTERFACE);
713     }
714     catch (std::exception &e)
715     {
716         dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
717     }
718 }
719
720 // Method for Finding the Light Resource
721 void findLightResource(void *data, Evas_Object *obj, void *event_info)
722 {
723     dlog_print(DLOG_INFO, LOG_TAG, "#### findLightResource ENTRY");
724     if (NULL != groupThingsMgr)
725     {
726         vector< string > types;
727         types.push_back("core.light");
728         OCStackResult result = groupThingsMgr->findCandidateResources(types, &foundResources, 5);
729         if (result == OC_STACK_OK)
730         {
731             dlog_print(DLOG_INFO, LOG_TAG, "#### create_group -- findCandidateResources :: "
732                        "OC_STACK_OK!!!!");
733         }
734         else
735         {
736             dlog_print(DLOG_INFO, LOG_TAG, "#### create_group - findCandidateResources failed!!");
737         }
738     }
739     dlog_print(DLOG_INFO, LOG_TAG, "#### findLightResource EXIT");
740 }
741
742 // Method for observing the Bookmark Resource
743 void observeBookMark(void *data, Evas_Object *obj, void *event_info)
744 {
745     dlog_print(DLOG_INFO, LOG_TAG, "#### observeBookMark ENTRY");
746     if (NULL != groupThingsMgr)
747     {
748         vector< string > types;
749         types.push_back("core.bookmark");
750         OCStackResult result = groupThingsMgr->findCandidateResources(types, &foundResources, 5);
751         if (OC_STACK_OK == result)
752         {
753             dlog_print(DLOG_INFO, LOG_TAG, "#### create_group - findCandidateResources :: "
754                        "OC_STACK_OK!!!!");
755         }
756         else
757         {
758             dlog_print(DLOG_INFO, LOG_TAG, "#### create_group - findCandidateResources failed!!");
759         }
760     }
761     dlog_print(DLOG_INFO, LOG_TAG, "#### observeBookMark EXIT");
762 }
763
764 static void onDestroy()
765 {
766     dlog_print(DLOG_INFO, LOG_TAG, "#### Destroy sequence called");
767
768     try
769     {
770         if (NULL != foundResourceHandle)
771         {
772             OCPlatform::unregisterResource(foundResourceHandle);
773             dlog_print(DLOG_INFO, LOG_TAG, "#### Light Resource unregistered");
774             lights.clear();
775         }
776         else
777         {
778             dlog_print(DLOG_INFO, LOG_TAG, "#### No resouceHandle found to unregister");
779         }
780     }
781     catch (std::exception &e)
782     {
783         dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
784     }
785
786     try
787     {
788         if (NULL != resourceHandle)
789         {
790             // Unbind Light resource
791             if (NULL != foundResourceHandle)
792             {
793                 OCPlatform::unbindResource(resourceHandle, foundResourceHandle);
794                 dlog_print(DLOG_INFO, LOG_TAG, "#### Resource Unbind Done");
795             }
796             OCPlatform::unregisterResource(resourceHandle);
797             dlog_print(DLOG_INFO, LOG_TAG, "#### Group Unregistered");
798         }
799         else
800         {
801             dlog_print(DLOG_INFO, LOG_TAG, "#### foundResourceHandle is NULL");
802         }
803     }
804     catch (std::exception &e)
805     {
806         dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
807     }
808 }
809
810 static void createActionSet(void *data, Evas_Object *obj, void *event_info)
811 {
812     createActionSet_AllBulbOff();
813     createActionSet_AllBulbOn();
814 }
815
816 static void
817 popup_cancel_clicked_cb(void *data, Evas_Object *obj, void *event_info)
818 {
819     datetime_popup_fields *popup_fields = (datetime_popup_fields *)data;
820     evas_object_del(popup_fields->popup);
821     free(popup_fields);
822 }
823
824 static int parseString(const char *str, int slen, int *beg, int what)
825 {
826     int i, val = 0, ch = '/';
827     if (2 == what)
828     {
829         ch = ' ';
830     }
831     else if (3 <= what)
832     {
833         if (5 == what)
834         {
835             ch = '\0';
836         }
837         else
838         {
839             ch = ':';
840         }
841     }
842     // Remove whitespaces(if any) at the beginning
843     while (str[*beg] == ' ')
844     {
845         (*beg)++;
846     }
847     for (i = *beg; i < slen; i++)
848     {
849         if (str[i] != ch)
850         {
851             val = (val * 10) + (str[i] - 48);
852             continue;
853         }
854         break;
855     }
856     (*beg) = i + 1;
857     return val;
858 }
859
860 static bool validate(int date, int month, int year, int hour, int minute, int second)
861 {
862     if (date <= 0 || month <= 0 || year <= 0 || hour < 0 || minute < 0 || second < 0
863         || month >= 13 || hour >= 24 || minute >= 60 || second >= 60)
864     {
865         return false;
866     }
867     return true;
868 }
869
870 static void
871 popup_set_clicked_cb(void *data, Evas_Object *obj, void *event_info)
872 {
873     datetime_popup_fields *popup_fields = (datetime_popup_fields *)data;
874     Evas_Object *entry = popup_fields->entry;
875     const char *dateTimeValue = elm_entry_entry_get(entry);
876     int len;
877     len = strlen(dateTimeValue);
878     if (NULL == dateTimeValue || 1 > len)
879     {
880         dlog_print(DLOG_INFO, LOG_TAG, "#### Read NULL DateTime Value");
881         string logMessage = "DateTime should not be NULL<br>";
882         logMessage += "----------------------<br>";
883         dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
884         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
885     }
886     else
887     {
888         int date, month, year, hour, minute, second;
889         int beg = 0;
890         date = parseString(dateTimeValue, len, &beg, 0);
891         month = parseString(dateTimeValue, len, &beg, 1);
892         year = parseString(dateTimeValue, len, &beg, 2);
893         hour = parseString(dateTimeValue, len, &beg, 3);
894         minute = parseString(dateTimeValue, len, &beg, 4);
895         second = parseString(dateTimeValue, len, &beg, 5);
896
897         dlog_print(DLOG_INFO, LOG_TAG, "#### %d", date);
898         dlog_print(DLOG_INFO, LOG_TAG, "#### %d", month);
899         dlog_print(DLOG_INFO, LOG_TAG, "#### %d", year);
900         dlog_print(DLOG_INFO, LOG_TAG, "#### %d", hour);
901         dlog_print(DLOG_INFO, LOG_TAG, "#### %d", minute);
902         dlog_print(DLOG_INFO, LOG_TAG, "#### %d", second);
903         bool valid = validate(date, month, year, hour, minute, second);
904         if (valid)
905         {
906             createScheduledActionSet_AllBulbOff(date, month, year, hour, minute, second);
907         }
908         else
909         {
910             dlog_print(DLOG_INFO, LOG_TAG, "#### Incorrect date/time values");
911             string logMessage = "Incorrect date/time value<br>";
912             logMessage += "----------------------<br>";
913             dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
914             ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
915                                                   &logMessage);
916         }
917     }
918     evas_object_del(popup_fields->popup);
919     free(popup_fields);
920 }
921
922 static void
923 list_scheduled_actionset_cb(void *data, Evas_Object *obj, void *event_info)
924 {
925     Evas_Object *popup, *btn;
926     Evas_Object *nf = naviframe;
927     Evas_Object *entry;
928     Evas_Object *layout;
929
930     /* popup */
931     popup = elm_popup_add(nf);
932     elm_popup_align_set(popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
933     eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK, eext_popup_back_cb, NULL);
934     evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
935     elm_object_part_text_set(popup, "title,text", "Enter the date and time");
936
937     layout = elm_layout_add(popup);
938     elm_layout_file_set(layout, ELM_DEMO_EDJ, "popup_datetime_text");
939     evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
940     elm_object_content_set(popup, layout);
941
942     entry = elm_entry_add(layout);
943     elm_entry_single_line_set(entry, EINA_TRUE);
944     elm_entry_scrollable_set(entry, EINA_TRUE);
945     evas_object_size_hint_weight_set(entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
946     evas_object_size_hint_align_set(entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
947     eext_entry_selection_back_event_allow_set(entry, EINA_TRUE);
948     elm_object_part_text_set(entry, "elm.guide", "dd/mm/yyyy hh:mm:ss");
949     elm_entry_input_panel_layout_set(entry, ELM_INPUT_PANEL_LAYOUT_NUMBER);
950     elm_object_part_content_set(layout, "elm.swallow.content", entry);
951
952     datetime_popup_fields *popup_fields;
953     popup_fields = (datetime_popup_fields *)malloc(sizeof(datetime_popup_fields));
954     if (NULL == popup_fields)
955     {
956         dlog_print(DLOG_INFO, LOG_TAG, "#### Memory allocation failed");
957     }
958     else
959     {
960         popup_fields->popup = popup;
961         popup_fields->entry = entry;
962     }
963
964     /* Cancel button */
965     btn = elm_button_add(popup);
966     elm_object_style_set(btn, "popup");
967     elm_object_text_set(btn, "Cancel");
968     elm_object_part_content_set(popup, "button1", btn);
969     evas_object_smart_callback_add(btn, "clicked", popup_cancel_clicked_cb, popup_fields);
970
971     /* Set button */
972     btn = elm_button_add(popup);
973     elm_object_style_set(btn, "popup");
974     elm_object_text_set(btn, "Set");
975     elm_object_part_content_set(popup, "button2", btn);
976     evas_object_smart_callback_add(btn, "clicked", popup_set_clicked_cb, popup_fields);
977
978     evas_object_show(popup);
979 }
980
981 void *showGroupAPIs(void *data)
982 {
983     // Add items to the list only if the list is empty
984     const Eina_List *eina_list = elm_list_items_get(list);
985     int count = eina_list_count(eina_list);
986     if (!count)
987     {
988         elm_list_item_append(list, "1. Create ActionSet<br>(ALLBULBON and ALLBULBOFF)", NULL, NULL,
989                              createActionSet, NULL);
990
991         elm_list_item_append(list, "2. Execute ActionSet (ALLBULBON)", NULL, NULL,
992                              executeActionSetOn, NULL);
993
994         elm_list_item_append(list, "3. Execute ActionSet (ALLBULBOFF)", NULL, NULL,
995                              executeActionSetOff, NULL);
996
997         elm_list_item_append(list, "4. Create ActionSet<br>(Recursive_ALLBULBON)", NULL, NULL,
998                              createRecursiveActionSet_AllBulbOn, NULL);
999
1000         elm_list_item_append(list, "    4.1 Execute ActionSet", NULL, NULL,
1001                              recursive_allBulbOn, NULL);
1002
1003         elm_list_item_append(list, "    4.2 Cancel ActionSet", NULL, NULL,
1004                              cancelRecursive_allBulbOn, NULL);
1005
1006         elm_list_item_append(list, "5. Create ActionSet<br>(Scheduled_ALLBULBOFF)", NULL, NULL,
1007                              list_scheduled_actionset_cb, NULL);
1008
1009         elm_list_item_append(list, "    5.1 Execute ActionSet", NULL, NULL,
1010                              scheduled_AllbulbOff, NULL);
1011
1012         elm_list_item_append(list, "    5.2 Cancel ActionSet", NULL, NULL,
1013                              cancelScheduled_AllBulbOff, NULL);
1014
1015         elm_list_item_append(list, "6. Get ActionSet (All BULBOFF)", NULL, NULL,
1016                              getActionSetOff, NULL);
1017
1018         elm_list_item_append(list, "7. Delete ActionSet (All BULBOFF)", NULL, NULL,
1019                              deleteActionSetOff, NULL);
1020
1021         elm_list_item_append(list, "8. Find BookMark to Observe", NULL, NULL,
1022                              observeBookMark, NULL);
1023
1024         elm_list_go(list);
1025     }
1026     return NULL;
1027 }
1028
1029 // Callback to be called when a resource is found in the network
1030 void foundResource(shared_ptr< OCResource > resource)
1031 {
1032     dlog_print(DLOG_INFO, LOG_TAG, "#### foundResource entry!!!!");
1033
1034     if (resource)
1035     {
1036         string resourceURI = resource->uri();
1037         string hostAddress = resource->host();
1038         string logMessage;
1039         if (resourceURI == "/core/b/collection")
1040         {
1041             g_resource = resource;
1042             dlog_print(DLOG_INFO, LOG_TAG, "#### FOUND URI: %s", resourceURI.c_str());
1043             dlog_print(DLOG_INFO, LOG_TAG, "#### FOUND HOST: %s", hostAddress.c_str());
1044             logMessage = "FOUND RESOURCE URI <br>" + resourceURI + "<br>";
1045             logMessage += "FOUND RESOURCE HOST <br>" + hostAddress + "<br>";
1046             logMessage += "----------------------<br>";
1047             // Show the UI list of group APIs
1048             ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))showGroupAPIs, NULL);
1049
1050         }
1051         dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
1052         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
1053                                               &logMessage);
1054     }
1055
1056     dlog_print(DLOG_INFO, LOG_TAG, "#### foundResource exit!!!!");
1057 }
1058
1059 static void list_selected_cb(void *data, Evas_Object *obj, void *event_info)
1060 {
1061     Elm_Object_Item *it = (Elm_Object_Item *)event_info;
1062     elm_list_item_selected_set(it, EINA_FALSE);
1063 }
1064
1065 // Method for Finding the Group
1066 static void find_group()
1067 {
1068     dlog_print(DLOG_INFO, LOG_TAG, "#### findGroup ENTRY");
1069     vector< string > resourceTypes;
1070     resourceTypes.push_back(resourceTypeName);
1071     OCStackResult result = groupThingsMgr->findGroup(resourceTypes, &foundResource);
1072     if (OC_STACK_OK == result)
1073     {
1074         dlog_print(DLOG_INFO, LOG_TAG, "#### findGroup returned OC_STACK_OK");
1075     }
1076     else
1077     {
1078         dlog_print(DLOG_INFO, LOG_TAG, "#### findGroup failed");
1079     }
1080     dlog_print(DLOG_INFO, LOG_TAG, "#### findGroup EXIT");
1081 }
1082
1083 // Method to be called when the Find Group UI Button is selected
1084 static void
1085 find_group_cb(void *data, Evas_Object *obj, void *event_info)
1086 {
1087     if (NULL != list)
1088     {
1089         find_group();
1090     }
1091     else
1092     {
1093         dlog_print(DLOG_ERROR, "find_group_cb", "list is NULL - So unable to add items!!!");
1094     }
1095 }
1096
1097 static Eina_Bool
1098 naviframe_pop_cb(void *data, Elm_Object_Item *it)
1099 {
1100     onDestroy();
1101
1102     if (NULL != log_entry)
1103     {
1104         evas_object_del(log_entry);
1105         log_entry = NULL;
1106     }
1107     if (NULL != list)
1108     {
1109         evas_object_del(list);
1110         list = NULL;
1111     }
1112     return EINA_TRUE;
1113 }
1114
1115 // Method to be called when the Group APIs UI Button is selected
1116 void group_cb(void *data, Evas_Object *obj, void *event_info)
1117 {
1118     Evas_Object *layout;
1119     Evas_Object *scroller;
1120     Evas_Object *nf = (Evas_Object *)data;
1121     Evas_Object *find_button;
1122     Elm_Object_Item *nf_it;
1123
1124     naviframe = nf;
1125
1126     // Scroller
1127     scroller = elm_scroller_add(nf);
1128     elm_scroller_bounce_set(scroller, EINA_FALSE, EINA_TRUE);
1129     elm_scroller_policy_set(scroller, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
1130
1131     // Layout
1132     layout = elm_layout_add(nf);
1133     elm_layout_file_set(layout, ELM_DEMO_EDJ, "group_layout");
1134     evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1135
1136     elm_object_content_set(scroller, layout);
1137
1138     // Button
1139     find_button = elm_button_add(layout);
1140     elm_object_part_content_set(layout, "find_button", find_button);
1141     elm_object_text_set(find_button, "Find Group");
1142     evas_object_smart_callback_add(find_button, "clicked", find_group_cb, NULL);
1143
1144     // List
1145     list = elm_list_add(layout);
1146     elm_list_mode_set(list, ELM_LIST_COMPRESS);
1147     evas_object_smart_callback_add(list, "selected", list_selected_cb, NULL);
1148     elm_object_part_content_set(layout, "list", list);
1149     elm_list_go(list);
1150
1151     // log_entry - textarea for log
1152     log_entry = elm_entry_add(layout);
1153     elm_entry_scrollable_set(log_entry, EINA_TRUE);
1154     elm_entry_editable_set(log_entry, EINA_FALSE);
1155     elm_object_part_text_set(log_entry, "elm.guide", "Logs will be updated here!!!");
1156     evas_object_size_hint_weight_set(log_entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1157     evas_object_size_hint_align_set(log_entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
1158     elm_object_part_content_set(layout, "log", log_entry);
1159
1160     nf_it = elm_naviframe_item_push(nf, "Group APIs", NULL, NULL, scroller, NULL);
1161     elm_naviframe_item_pop_cb_set(nf_it, naviframe_pop_cb, NULL);
1162
1163     create_group();
1164 }