Imported Upstream version 1.0.0
[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 GroupManager *groupMgr = NULL;
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 = groupMgr->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             groupMgr->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             groupMgr->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         groupMgr->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         groupMgr->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     groupMgr->executeActionSet(g_resource, "AllBulbOffScheduledCall", &onPost);
342 }
343
344 static void scheduled_AllbulbOffEx(void *data, Evas_Object *obj, void *event_info)
345 {
346     groupMgr->executeActionSet(g_resource, "AllBulbOffScheduledCall", 10, &onPost);
347 }
348
349 static void cancelScheduled_AllBulbOff(void *data, Evas_Object *obj, void *event_info)
350 {
351     groupMgr->cancelActionSet(g_resource, "AllBulbOffScheduledCall", &onPost);
352 }
353
354 static void recursive_allBulbOn(void *data, Evas_Object *obj, void *event_info)
355 {
356     groupMgr->executeActionSet(g_resource, "AllBulbOnRecursiveCall", &onPost);
357 }
358
359 static void recursive_allBulbOnEx(void *data, Evas_Object *obj, void *event_info)
360 {
361     groupMgr->executeActionSet(g_resource, "AllBulbOnRecursiveCall", 10, &onPost);
362 }
363
364 static void cancelRecursive_allBulbOn(void *data, Evas_Object *obj, void *event_info)
365 {
366
367     groupMgr->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             groupMgr->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             groupMgr->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             groupMgr->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             groupMgr->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             groupMgr->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             groupMgr->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
690     groupMgr = new GroupManager();
691
692     if (NULL != groupMgr)
693     {
694         dlog_print(DLOG_INFO, LOG_TAG, "#### calling findCandidateResources from "
695                    "create_group!!!!");
696         vector< string > types;
697         types.push_back("core.light");
698         groupMgr->findCandidateResources(types, &foundResources, FINDRESOURCE_TIMEOUT);
699     }
700
701     try
702     {
703         dlog_print(DLOG_INFO, LOG_TAG, "#### calling registerResource from create_group!!!!");
704         OCPlatform::registerResource(resourceHandle, resourceURI, resourceTypeName,
705                                      BATCH_INTERFACE, NULL,
706                                      OC_DISCOVERABLE | OC_OBSERVABLE);
707
708         if (NULL != resourceHandle)
709             dlog_print(DLOG_INFO, LOG_TAG, "#### Obtained resourceHandle from "
710                        "registerResource!!!!");
711
712         dlog_print(DLOG_INFO, LOG_TAG, "#### calling bindInterfaceToResource from "
713                    "create_group!!!!");
714         OCPlatform::bindInterfaceToResource(resourceHandle, GROUP_INTERFACE);
715         OCPlatform::bindInterfaceToResource(resourceHandle, DEFAULT_INTERFACE);
716     }
717     catch (std::exception &e)
718     {
719         dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
720     }
721 }
722
723 // Method for Finding the Light Resource
724 void findLightResource(void *data, Evas_Object *obj, void *event_info)
725 {
726     dlog_print(DLOG_INFO, LOG_TAG, "#### findLightResource ENTRY");
727     if (NULL != groupMgr)
728     {
729         vector< string > types;
730         types.push_back("core.light");
731         OCStackResult result = groupMgr->findCandidateResources(types, &foundResources,
732                                FINDRESOURCE_TIMEOUT);
733         if (result == OC_STACK_OK)
734         {
735             dlog_print(DLOG_INFO, LOG_TAG, "#### create_group -- findCandidateResources :: "
736                        "OC_STACK_OK!!!!");
737         }
738         else
739         {
740             dlog_print(DLOG_INFO, LOG_TAG, "#### create_group - findCandidateResources failed!!");
741         }
742     }
743     dlog_print(DLOG_INFO, LOG_TAG, "#### findLightResource EXIT");
744 }
745
746 // Method for observing the Bookmark Resource
747 void observeBookMark(void *data, Evas_Object *obj, void *event_info)
748 {
749     dlog_print(DLOG_INFO, LOG_TAG, "#### observeBookMark ENTRY");
750     if (NULL != groupMgr)
751     {
752         vector< string > types;
753         types.push_back("core.bookmark");
754         OCStackResult result = groupMgr->findCandidateResources(types, &foundResources,
755                                FINDRESOURCE_TIMEOUT);
756         if (OC_STACK_OK == result)
757         {
758             dlog_print(DLOG_INFO, LOG_TAG, "#### create_group - findCandidateResources :: "
759                        "OC_STACK_OK!!!!");
760         }
761         else
762         {
763             dlog_print(DLOG_INFO, LOG_TAG, "#### create_group - findCandidateResources failed!!");
764         }
765     }
766     dlog_print(DLOG_INFO, LOG_TAG, "#### observeBookMark EXIT");
767 }
768
769 static void onDestroy()
770 {
771     dlog_print(DLOG_INFO, LOG_TAG, "#### Destroy sequence called");
772
773     try
774     {
775         if (NULL != foundResourceHandle)
776         {
777             OCPlatform::unregisterResource(foundResourceHandle);
778             dlog_print(DLOG_INFO, LOG_TAG, "#### Light Resource unregistered");
779             lights.clear();
780         }
781         else
782         {
783             dlog_print(DLOG_INFO, LOG_TAG, "#### No resouceHandle found to unregister");
784         }
785     }
786     catch (std::exception &e)
787     {
788         dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
789     }
790
791     try
792     {
793         if (NULL != resourceHandle)
794         {
795             // Unbind Light resource
796             if (NULL != foundResourceHandle)
797             {
798                 OCPlatform::unbindResource(resourceHandle, foundResourceHandle);
799                 dlog_print(DLOG_INFO, LOG_TAG, "#### Resource Unbind Done");
800             }
801             OCPlatform::unregisterResource(resourceHandle);
802             dlog_print(DLOG_INFO, LOG_TAG, "#### Group Unregistered");
803         }
804         else
805         {
806             dlog_print(DLOG_INFO, LOG_TAG, "#### foundResourceHandle is NULL");
807         }
808     }
809     catch (std::exception &e)
810     {
811         dlog_print(DLOG_ERROR, LOG_TAG, "Exception occured! (%s)", e.what());
812     }
813
814     delete groupMgr;
815 }
816
817 static void createActionSet(void *data, Evas_Object *obj, void *event_info)
818 {
819     createActionSet_AllBulbOff();
820     createActionSet_AllBulbOn();
821 }
822
823 static void
824 popup_cancel_clicked_cb(void *data, Evas_Object *obj, void *event_info)
825 {
826     datetime_popup_fields *popup_fields = (datetime_popup_fields *)data;
827     evas_object_del(popup_fields->popup);
828     free(popup_fields);
829 }
830
831 static int parseString(const char *str, int slen, int *beg, int what)
832 {
833     int i, val = 0, ch = '/';
834     if (2 == what)
835     {
836         ch = ' ';
837     }
838     else if (3 <= what)
839     {
840         if (5 == what)
841         {
842             ch = '\0';
843         }
844         else
845         {
846             ch = ':';
847         }
848     }
849     // Remove whitespaces(if any) at the beginning
850     while (str[*beg] == ' ')
851     {
852         (*beg)++;
853     }
854     for (i = *beg; i < slen; i++)
855     {
856         if (str[i] != ch)
857         {
858             val = (val * 10) + (str[i] - 48);
859             continue;
860         }
861         break;
862     }
863     (*beg) = i + 1;
864     return val;
865 }
866
867 static bool validate(int date, int month, int year, int hour, int minute, int second)
868 {
869     if (date <= 0 || month <= 0 || year <= 0 || hour < 0 || minute < 0 || second < 0
870         || month >= 13 || hour >= 24 || minute >= 60 || second >= 60)
871     {
872         return false;
873     }
874     return true;
875 }
876
877 static void
878 popup_set_clicked_cb(void *data, Evas_Object *obj, void *event_info)
879 {
880     datetime_popup_fields *popup_fields = (datetime_popup_fields *)data;
881     Evas_Object *entry = popup_fields->entry;
882     const char *dateTimeValue = elm_entry_entry_get(entry);
883     int len;
884     len = strlen(dateTimeValue);
885     if (NULL == dateTimeValue || 1 > len)
886     {
887         dlog_print(DLOG_INFO, LOG_TAG, "#### Read NULL DateTime Value");
888         string logMessage = "DateTime should not be NULL<br>";
889         logMessage += "----------------------<br>";
890         dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
891         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog, &logMessage);
892     }
893     else
894     {
895         int date, month, year, hour, minute, second;
896         int beg = 0;
897         date = parseString(dateTimeValue, len, &beg, 0);
898         month = parseString(dateTimeValue, len, &beg, 1);
899         year = parseString(dateTimeValue, len, &beg, 2);
900         hour = parseString(dateTimeValue, len, &beg, 3);
901         minute = parseString(dateTimeValue, len, &beg, 4);
902         second = parseString(dateTimeValue, len, &beg, 5);
903
904         dlog_print(DLOG_INFO, LOG_TAG, "#### %d", date);
905         dlog_print(DLOG_INFO, LOG_TAG, "#### %d", month);
906         dlog_print(DLOG_INFO, LOG_TAG, "#### %d", year);
907         dlog_print(DLOG_INFO, LOG_TAG, "#### %d", hour);
908         dlog_print(DLOG_INFO, LOG_TAG, "#### %d", minute);
909         dlog_print(DLOG_INFO, LOG_TAG, "#### %d", second);
910         bool valid = validate(date, month, year, hour, minute, second);
911         if (valid)
912         {
913             createScheduledActionSet_AllBulbOff(date, month, year, hour, minute, second);
914         }
915         else
916         {
917             dlog_print(DLOG_INFO, LOG_TAG, "#### Incorrect date/time values");
918             string logMessage = "Incorrect date/time value<br>";
919             logMessage += "----------------------<br>";
920             dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
921             ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
922                                                   &logMessage);
923         }
924     }
925     evas_object_del(popup_fields->popup);
926     free(popup_fields);
927 }
928
929 static void
930 list_scheduled_actionset_cb(void *data, Evas_Object *obj, void *event_info)
931 {
932     Evas_Object *popup, *btn;
933     Evas_Object *nf = naviframe;
934     Evas_Object *entry;
935     Evas_Object *layout;
936
937     /* popup */
938     popup = elm_popup_add(nf);
939     elm_popup_align_set(popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
940     eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK, eext_popup_back_cb, NULL);
941     evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
942     elm_object_part_text_set(popup, "title,text", "Enter the date and time");
943
944     layout = elm_layout_add(popup);
945     elm_layout_file_set(layout, ELM_DEMO_EDJ, "popup_datetime_text");
946     evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
947     elm_object_content_set(popup, layout);
948
949     entry = elm_entry_add(layout);
950     elm_entry_single_line_set(entry, EINA_TRUE);
951     elm_entry_scrollable_set(entry, EINA_TRUE);
952     evas_object_size_hint_weight_set(entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
953     evas_object_size_hint_align_set(entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
954     eext_entry_selection_back_event_allow_set(entry, EINA_TRUE);
955     elm_object_part_text_set(entry, "elm.guide", "dd/mm/yyyy hh:mm:ss");
956     elm_entry_input_panel_layout_set(entry, ELM_INPUT_PANEL_LAYOUT_NUMBER);
957     elm_object_part_content_set(layout, "elm.swallow.content", entry);
958
959     datetime_popup_fields *popup_fields;
960     popup_fields = (datetime_popup_fields *)malloc(sizeof(datetime_popup_fields));
961     if (NULL == popup_fields)
962     {
963         dlog_print(DLOG_INFO, LOG_TAG, "#### Memory allocation failed");
964     }
965     else
966     {
967         popup_fields->popup = popup;
968         popup_fields->entry = entry;
969     }
970
971     /* Cancel button */
972     btn = elm_button_add(popup);
973     elm_object_style_set(btn, "popup");
974     elm_object_text_set(btn, "Cancel");
975     elm_object_part_content_set(popup, "button1", btn);
976     evas_object_smart_callback_add(btn, "clicked", popup_cancel_clicked_cb, popup_fields);
977
978     /* Set button */
979     btn = elm_button_add(popup);
980     elm_object_style_set(btn, "popup");
981     elm_object_text_set(btn, "Set");
982     elm_object_part_content_set(popup, "button2", btn);
983     evas_object_smart_callback_add(btn, "clicked", popup_set_clicked_cb, popup_fields);
984
985     evas_object_show(popup);
986 }
987
988 void *showGroupAPIs(void *data)
989 {
990     // Add items to the list only if the list is empty
991     const Eina_List *eina_list = elm_list_items_get(list);
992     int count = eina_list_count(eina_list);
993     if (!count)
994     {
995         elm_list_item_append(list, "1. Create ActionSet<br>(ALLBULBON and ALLBULBOFF)", NULL, NULL,
996                              createActionSet, NULL);
997
998         elm_list_item_append(list, "2. Execute ActionSet (ALLBULBON)", NULL, NULL,
999                              executeActionSetOn, NULL);
1000
1001         elm_list_item_append(list, "3. Execute ActionSet (ALLBULBOFF)", NULL, NULL,
1002                              executeActionSetOff, NULL);
1003
1004         elm_list_item_append(list, "4. Create ActionSet<br>(Recursive_ALLBULBON)", NULL, NULL,
1005                              createRecursiveActionSet_AllBulbOn, NULL);
1006
1007         elm_list_item_append(list, "    4.1 Execute ActionSet", NULL, NULL,
1008                              recursive_allBulbOn, NULL);
1009
1010         elm_list_item_append(list, "    4.2 Cancel ActionSet", NULL, NULL,
1011                              cancelRecursive_allBulbOn, NULL);
1012
1013         elm_list_item_append(list, "5. Create ActionSet<br>(Scheduled_ALLBULBOFF)", NULL, NULL,
1014                              list_scheduled_actionset_cb, NULL);
1015
1016         elm_list_item_append(list, "    5.1 Execute ActionSet", NULL, NULL,
1017                              scheduled_AllbulbOff, NULL);
1018
1019         elm_list_item_append(list, "    5.2 Cancel ActionSet", NULL, NULL,
1020                              cancelScheduled_AllBulbOff, NULL);
1021
1022         elm_list_item_append(list, "6. Get ActionSet (All BULBOFF)", NULL, NULL,
1023                              getActionSetOff, NULL);
1024
1025         elm_list_item_append(list, "7. Delete ActionSet (All BULBOFF)", NULL, NULL,
1026                              deleteActionSetOff, NULL);
1027
1028         elm_list_item_append(list, "8. Find BookMark to Observe", NULL, NULL,
1029                              observeBookMark, NULL);
1030
1031         elm_list_go(list);
1032     }
1033     return NULL;
1034 }
1035
1036 // Callback to be called when a resource is found in the network
1037 void foundResource(shared_ptr< OCResource > resource)
1038 {
1039     dlog_print(DLOG_INFO, LOG_TAG, "#### foundResource entry!!!!");
1040
1041     if (resource)
1042     {
1043         string resourceURI = resource->uri();
1044         string hostAddress = resource->host();
1045         string logMessage;
1046         if (resourceURI == "/core/b/collection")
1047         {
1048             g_resource = resource;
1049             dlog_print(DLOG_INFO, LOG_TAG, "#### FOUND URI: %s", resourceURI.c_str());
1050             dlog_print(DLOG_INFO, LOG_TAG, "#### FOUND HOST: %s", hostAddress.c_str());
1051             logMessage = "FOUND RESOURCE URI <br>" + resourceURI + "<br>";
1052             logMessage += "FOUND RESOURCE HOST <br>" + hostAddress + "<br>";
1053             logMessage += "----------------------<br>";
1054             // Show the UI list of group APIs
1055             ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))showGroupAPIs, NULL);
1056
1057         }
1058         dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
1059         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
1060                                               &logMessage);
1061     }
1062
1063     dlog_print(DLOG_INFO, LOG_TAG, "#### foundResource exit!!!!");
1064 }
1065
1066 static void list_selected_cb(void *data, Evas_Object *obj, void *event_info)
1067 {
1068     Elm_Object_Item *it = (Elm_Object_Item *)event_info;
1069     elm_list_item_selected_set(it, EINA_FALSE);
1070 }
1071
1072 // Method for Finding the Group
1073 static void find_group()
1074 {
1075     dlog_print(DLOG_INFO, LOG_TAG, "#### findGroup ENTRY");
1076     std::string query = OC_RSRVD_WELL_KNOWN_URI;
1077     query.append("?rt=");
1078     query.append(resourceTypeName);
1079
1080     OCPlatform::findResource("", query, CT_DEFAULT, &foundResource);
1081
1082     dlog_print(DLOG_INFO, LOG_TAG, "#### findGroup EXIT");
1083 }
1084
1085 // Method to be called when the Find Group UI Button is selected
1086 static void
1087 find_group_cb(void *data, Evas_Object *obj, void *event_info)
1088 {
1089     if (NULL != list)
1090     {
1091         find_group();
1092     }
1093     else
1094     {
1095         dlog_print(DLOG_ERROR, "find_group_cb", "list is NULL - So unable to add items!!!");
1096     }
1097 }
1098
1099 static Eina_Bool
1100 naviframe_pop_cb(void *data, Elm_Object_Item *it)
1101 {
1102     onDestroy();
1103
1104     if (NULL != log_entry)
1105     {
1106         evas_object_del(log_entry);
1107         log_entry = NULL;
1108     }
1109     if (NULL != list)
1110     {
1111         evas_object_del(list);
1112         list = NULL;
1113     }
1114     return EINA_TRUE;
1115 }
1116
1117 // Method to be called when the Group APIs UI Button is selected
1118 void group_cb(void *data, Evas_Object *obj, void *event_info)
1119 {
1120     Evas_Object *layout;
1121     Evas_Object *scroller;
1122     Evas_Object *nf = (Evas_Object *)data;
1123     Evas_Object *find_button;
1124     Elm_Object_Item *nf_it;
1125
1126     naviframe = nf;
1127
1128     // Scroller
1129     scroller = elm_scroller_add(nf);
1130     elm_scroller_bounce_set(scroller, EINA_FALSE, EINA_TRUE);
1131     elm_scroller_policy_set(scroller, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
1132
1133     // Layout
1134     layout = elm_layout_add(nf);
1135     elm_layout_file_set(layout, ELM_DEMO_EDJ, "group_layout");
1136     evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1137
1138     elm_object_content_set(scroller, layout);
1139
1140     // Button
1141     find_button = elm_button_add(layout);
1142     elm_object_part_content_set(layout, "find_button", find_button);
1143     elm_object_text_set(find_button, "Find Group");
1144     evas_object_smart_callback_add(find_button, "clicked", find_group_cb, NULL);
1145
1146     // List
1147     list = elm_list_add(layout);
1148     elm_list_mode_set(list, ELM_LIST_COMPRESS);
1149     evas_object_smart_callback_add(list, "selected", list_selected_cb, NULL);
1150     elm_object_part_content_set(layout, "list", list);
1151     elm_list_go(list);
1152
1153     // log_entry - textarea for log
1154     log_entry = elm_entry_add(layout);
1155     elm_entry_scrollable_set(log_entry, EINA_TRUE);
1156     elm_entry_editable_set(log_entry, EINA_FALSE);
1157     elm_object_part_text_set(log_entry, "elm.guide", "Logs will be updated here!!!");
1158     evas_object_size_hint_weight_set(log_entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1159     evas_object_size_hint_align_set(log_entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
1160     elm_object_part_content_set(layout, "log", log_entry);
1161
1162     nf_it = elm_naviframe_item_push(nf, "Group APIs", NULL, NULL, scroller, NULL);
1163     elm_naviframe_item_pop_cb_set(nf_it, naviframe_pop_cb, NULL);
1164
1165     create_group();
1166 }