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