b3bcc2d8a035adddb91dd806dab619dc9d1c3af0
[platform/upstream/iotivity.git] / service / protocol-plugin / sample-app / tizen / PPMSampleApp / src / ppmsampleapp.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 "ppmsampleapp.h"
22
23 #include <tizen.h>
24 #include <wifi.h>
25 #include <algorithm>
26 #include "string.h"
27 #include <time.h>
28 #include <pthread.h>
29
30 #include "PluginManager.h"
31 #include "OCPlatform.h"
32 #include "PluginManager.h"
33 #include "OCApi.h"
34
35 using namespace OC;
36 using namespace OIC;
37
38 class Fan
39 {
40     public:
41
42         bool m_state;
43         int m_power;
44         std::string m_name;
45
46         Fan() : m_state(false), m_power(0), m_name("")
47         {
48         }
49 };
50
51 typedef struct appdata
52 {
53     Evas_Object *win;
54     Evas_Object *conform;
55     Evas_Object *naviframe;
56     Evas_Object *box;
57
58     Evas_Object *log;
59
60     Evas_Object *send_msg_button;
61     Evas_Object *clear_log_button;
62 } appdata_s;
63
64 typedef struct threadContext
65 {
66     appdata_s *ad;
67     const char *log;
68 } threadContext_s;
69
70 threadContext_s m_ThreadContext;
71 bool isSendMessage = true;
72 const int SUCCESS_RESPONSE = 0;
73 std::shared_ptr<OCResource> curFanResource;
74 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
75 int count = 0;
76 Fan myfan;
77 char log_buffer[10000];
78 PluginManager *m_pm = NULL;
79 char temp_string[2000];
80 char buf[100];
81
82 void putFanRepresentation(std::shared_ptr<OCResource> resource);
83
84 void updateLog(appdata_s *ad, const char *newlog)
85 {
86     const char *log_text = NULL;
87
88     log_text = elm_entry_entry_get(ad->log);
89     strcpy(log_buffer, log_text);
90     strcat(log_buffer, newlog);
91     elm_entry_entry_set(ad->log, log_buffer);
92     elm_entry_cursor_end_set(ad->log);
93 }
94
95 void *updateCallbackLog(void *data)
96 {
97     threadContext_s  *pThreadContext = (threadContext_s *)data;
98     updateLog(pThreadContext->ad, pThreadContext->log);
99     return NULL;
100 }
101
102
103 int observe_count()
104 {
105     static int oc = 0;
106     return ++oc;
107 }
108
109 void onObserve(const HeaderOptions headerOptions, const OCRepresentation &rep,
110                const int &eCode, const int &sequenceNumber)
111 {
112     if (eCode == OC_STACK_OK)
113     {
114         std::cout << "OBSERVE RESULT:" << std::endl;
115         std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
116
117         rep.getValue("state", myfan.m_state);
118         rep.getValue("power", myfan.m_power);
119         rep.getValue("name", myfan.m_name);
120
121         sprintf(buf, "OBSERVE RESULT: sequence number = %d<br>", sequenceNumber);
122         strcpy(temp_string, buf);
123         sprintf(buf, "state: = %d<br>", myfan.m_state);
124         strcat(temp_string, buf);
125         sprintf(buf, "power: = %d<br>", myfan.m_power);
126         strcat(temp_string, buf);
127         sprintf(buf, "name: = %s<br>", myfan.m_name.c_str());
128         strcat(temp_string, buf);
129         m_ThreadContext.log = temp_string;
130         dlog_print(DLOG_INFO, LOG_TAG, " %s", m_ThreadContext.log);
131         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateCallbackLog,
132                                               &m_ThreadContext);
133
134         if (observe_count() > 30)
135         {
136             std::cout << "Cancelling Observe..." << std::endl;
137             OCStackResult result = curFanResource->cancelObserve();
138
139             std::cout << "Cancel result: " << result << std::endl;
140             sleep(10);
141             std::cout << "DONE" << std::endl;
142             std::exit(0);
143         }
144     }
145     else
146     {
147         std::cout << "onObserve Response error: " << eCode << std::endl;
148         sprintf(buf, "onObserve Response error:  = %d<br>", eCode);
149         strcpy(temp_string, buf);
150         m_ThreadContext.log = temp_string;
151         dlog_print(DLOG_INFO, LOG_TAG, " %s", m_ThreadContext.log);
152         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateCallbackLog,
153                                               &m_ThreadContext);
154         std::exit(-1);
155     }
156 }
157
158 void onPost2(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
159 {
160     if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
161     {
162         std::cout << "POST request was successful" << std::endl;
163
164         if (rep.hasAttribute("createduri"))
165         {
166             std::cout << "\tUri of the created resource: "
167                       << rep.getValue<std::string>("createduri") << std::endl;
168         }
169         else
170         {
171             rep.getValue("state", myfan.m_state);
172             rep.getValue("power", myfan.m_power);
173             rep.getValue("name", myfan.m_name);
174
175             std::cout << "\tstate: " << myfan.m_state << std::endl;
176             std::cout << "\tpower: " << myfan.m_power << std::endl;
177             std::cout << "\tname: " << myfan.m_name << std::endl;
178
179             strcpy(temp_string, "POST2 request was successful<br>");
180             sprintf(buf, "state: = %d<br>", myfan.m_state);
181             strcat(temp_string, buf);
182             sprintf(buf, "power: = %d<br>", myfan.m_power);
183             strcat(temp_string, buf);
184             sprintf(buf, "name: = %s<br>", myfan.m_name.c_str());
185             strcat(temp_string, buf);
186             m_ThreadContext.log = temp_string;
187             dlog_print(DLOG_INFO, LOG_TAG, " %s", m_ThreadContext.log);
188             ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateCallbackLog,
189                                                   &m_ThreadContext);
190         }
191
192         if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
193             std::cout << std::endl << "Observe is used." << std::endl;
194         else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
195             std::cout << std::endl << "ObserveAll is used." << std::endl;
196
197         // curFanResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
198
199     }
200     else
201     {
202         std::cout << "onPost Response error: " << eCode << std::endl;
203         sprintf(buf, "onPost Response error:  = %d<br>", eCode);
204         strcpy(temp_string, buf);
205         m_ThreadContext.log = temp_string;
206         dlog_print(DLOG_INFO, LOG_TAG, " %s", m_ThreadContext.log);
207         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateCallbackLog,
208                                               &m_ThreadContext);
209         std::exit(-1);
210     }
211 }
212
213 void onPost(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
214 {
215     if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
216     {
217         std::cout << "POST request was successful" << std::endl;
218
219         if (rep.hasAttribute("createduri"))
220         {
221             std::cout << "\tUri of the created resource: "
222                       << rep.getValue<std::string>("createduri") << std::endl;
223         }
224         else
225         {
226             rep.getValue("state", myfan.m_state);
227             rep.getValue("power", myfan.m_power);
228             rep.getValue("name", myfan.m_name);
229
230             std::cout << "\tstate: " << myfan.m_state << std::endl;
231             std::cout << "\tpower: " << myfan.m_power << std::endl;
232             std::cout << "\tname: " << myfan.m_name << std::endl;
233
234             strcpy(temp_string, "POST request was successful<br>");
235             sprintf(buf, "state: = %d<br>", myfan.m_state);
236             strcat(temp_string, buf);
237             sprintf(buf, "power: = %d<br>", myfan.m_power);
238             strcat(temp_string, buf);
239             sprintf(buf, "name: = %s<br>", myfan.m_name.c_str());
240             strcat(temp_string, buf);
241             m_ThreadContext.log = temp_string;
242             dlog_print(DLOG_INFO, LOG_TAG, " %s", m_ThreadContext.log);
243             ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateCallbackLog,
244                                                   &m_ThreadContext);
245         }
246
247         OCRepresentation rep2;
248
249         std::cout << "Posting fan representation..." << std::endl;
250
251         myfan.m_state = true;
252         myfan.m_power = 55;
253
254         rep2.setValue("state", myfan.m_state);
255         rep2.setValue("power", myfan.m_power);
256
257         curFanResource->post(rep2, QueryParamsMap(), &onPost2);
258     }
259     else
260     {
261         std::cout << "onPost Response error: " << eCode << std::endl;
262         sprintf(buf, "onPost Response error:  = %d<br>", eCode);
263         strcpy(temp_string, buf);
264         m_ThreadContext.log = temp_string;
265         dlog_print(DLOG_INFO, LOG_TAG, " %s", m_ThreadContext.log);
266         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateCallbackLog,
267                                               &m_ThreadContext);
268         std::exit(-1);
269     }
270 }
271
272 // Local function to put a different state for this resource
273 void postFanRepresentation(std::shared_ptr<OCResource> resource)
274 {
275     if (resource)
276     {
277         OCRepresentation rep;
278
279         std::cout << "Posting fan representation..." << std::endl;
280
281         myfan.m_state = false;
282         myfan.m_power = 105;
283
284         rep.setValue("state", myfan.m_state);
285         rep.setValue("power", myfan.m_power);
286
287         // Invoke resource's post API with rep, query map and the callback parameter
288         resource->post(rep, QueryParamsMap(), &onPost);
289     }
290 }
291
292 // Callback handler on PUT request
293 void onPut(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
294 {
295     if (eCode == OC_STACK_OK)
296     {
297         std::cout << "PUT request was successful" << std::endl;
298
299         rep.getValue("state", myfan.m_state);
300         rep.getValue("power", myfan.m_power);
301         rep.getValue("name", myfan.m_name);
302
303         std::cout << "\tstate: " << myfan.m_state << std::endl;
304         std::cout << "\tpower: " << myfan.m_power << std::endl;
305         std::cout << "\tname: " << myfan.m_name << std::endl;
306
307         strcpy(temp_string, "PUT request was successful<br>");
308         sprintf(buf, "state: = %d<br>", myfan.m_state);
309         strcat(temp_string, buf);
310         sprintf(buf, "power: = %d<br>", myfan.m_power);
311         strcat(temp_string, buf);
312         sprintf(buf, "name: = %s<br>", myfan.m_name.c_str());
313         strcat(temp_string, buf);
314         m_ThreadContext.log = temp_string;
315         dlog_print(DLOG_INFO, LOG_TAG, " %s", m_ThreadContext.log);
316         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateCallbackLog,
317                                               &m_ThreadContext);
318
319         putFanRepresentation(curFanResource);
320     }
321     else
322     {
323         std::cout << "onPut Response error: " << eCode << std::endl;
324         sprintf(buf, "onPut Response error:  = %d<br>", eCode);
325         strcpy(temp_string, buf);
326         m_ThreadContext.log = temp_string;
327         dlog_print(DLOG_INFO, LOG_TAG, " %s", m_ThreadContext.log);
328         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateCallbackLog,
329                                               &m_ThreadContext);
330         std::exit(-1);
331     }
332 }
333
334 // Local function to put a different state for this resource
335 void putFanRepresentation(std::shared_ptr<OCResource> resource)
336 {
337     if (resource)
338     {
339         OCRepresentation rep;
340
341         std::cout << "Putting fan representation..." << std::endl;
342
343         myfan.m_state = true;
344         if (myfan.m_power == 1)
345             myfan.m_power = 0;
346         else
347             myfan.m_power = 1;
348         sleep(5);
349         rep.setValue("state", myfan.m_state);
350         rep.setValue("power", myfan.m_power);
351
352         // Invoke resource's put API with rep, query map and the callback parameter
353         resource->put(rep, QueryParamsMap(), &onPut);
354     }
355 }
356
357 // Callback handler on GET request
358 void onFanGet(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
359 {
360     if (eCode == OC_STACK_OK)
361     {
362         std::cout << "GET Fan request was successful" << std::endl;
363         std::cout << "Resource URI: " << rep.getUri() << std::endl;
364
365         rep.getValue("state", myfan.m_state);
366         rep.getValue("power", myfan.m_power);
367         rep.getValue("name", myfan.m_name);
368
369         std::cout << "\tstate: " << myfan.m_state << std::endl;
370         std::cout << "\tpower: " << myfan.m_power << std::endl;
371         std::cout << "\tname: " << myfan.m_name << std::endl;
372
373         strcpy(temp_string, "GET Fan request was successful<br>");
374         sprintf(buf, "state: = %d<br>", myfan.m_state);
375         strcat(temp_string, buf);
376         sprintf(buf, "power: = %d<br>", myfan.m_power);
377         strcat(temp_string, buf);
378         sprintf(buf, "name: = %s<br>", myfan.m_name.c_str());
379         strcat(temp_string, buf);
380         m_ThreadContext.log = temp_string;
381         dlog_print(DLOG_INFO, LOG_TAG, " %s", m_ThreadContext.log);
382         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateCallbackLog,
383                                               &m_ThreadContext);
384
385         putFanRepresentation(curFanResource);
386     }
387     else
388     {
389         std::cout << "onGET Response error: " << eCode << std::endl;
390         sprintf(buf, "onGET Response error:  = %d<br>", eCode);
391         strcpy(temp_string, buf);
392         m_ThreadContext.log = temp_string;
393         dlog_print(DLOG_INFO, LOG_TAG, " %s", m_ThreadContext.log);
394         ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateCallbackLog,
395                                               &m_ThreadContext);
396         std::exit(-1);
397     }
398 }
399
400
401 // Local function to get representation of fan resource
402 void getFanRepresentation(std::shared_ptr<OCResource> resource)
403 {
404     if (resource)
405     {
406         std::cout << "Getting Fan Representation..." << std::endl;
407
408         // Invoke resource's get API with the callback parameter
409         QueryParamsMap test;
410         resource->get(test, &onFanGet);
411     }
412 }
413
414 // Callback to found resources
415 void foundResourceFan(std::shared_ptr<OCResource> resource)
416 {
417     if (curFanResource)
418     {
419         std::cout << "Found another resource, ignoring" << std::endl;
420     }
421
422     std::string resourceURI;
423     std::string hostAddress;
424     try
425     {
426         // Do some operations with resource object.
427         if (resource)
428         {
429             std::cout << "DISCOVERED Resource:" << std::endl;
430             strcpy(temp_string, "DISCOVERED Resource:");
431
432             // Get the resource URI
433             resourceURI = resource->uri();
434             std::cout << "\tURI of the resource: " << resourceURI << std::endl;
435             sprintf(buf, "URI of the resource: = %s<br>", resourceURI.c_str());
436             strcat(temp_string, buf);
437
438             // Get the resource host address
439             hostAddress = resource->host();
440             std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
441             sprintf(buf, "Host address of the resource: = %s<br>", hostAddress.c_str());
442             strcat(temp_string, buf);
443
444             // Get the resource types
445             std::cout << "\tList of resource types: " << std::endl;
446             strcat(temp_string, "List of resource types: <br>");
447             for (auto & resourceTypes : resource->getResourceTypes())
448             {
449                 std::cout << "\t\t" << resourceTypes << std::endl;
450                 sprintf(buf, "%s<br>", resourceTypes.c_str());
451                 strcat(temp_string, buf);
452             }
453
454             // Get the resource interfaces
455             std::cout << "\tList of resource interfaces: " << std::endl;
456             for (auto & resourceInterfaces : resource->getResourceInterfaces())
457             {
458                 std::cout << "\t\t" << resourceInterfaces << std::endl;
459                 sprintf(buf, "%s<br>", resourceInterfaces.c_str());
460                 strcat(temp_string, buf);
461             }
462
463             if (resourceURI == "/a/fan")
464             {
465                 curFanResource = resource;
466                 /* Call a local function which will internally invoke get API
467                    on the resource pointer */
468                 putFanRepresentation(curFanResource);
469             }
470             m_ThreadContext.log = temp_string;
471             dlog_print(DLOG_INFO, LOG_TAG, " %s", m_ThreadContext.log);
472             ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateCallbackLog,
473                                                   &m_ThreadContext);
474         }
475         else
476         {
477             // Resource is invalid
478             std::cout << "Resource is invalid" << std::endl;
479             strcpy(temp_string, "Resource is invalid");
480             m_ThreadContext.log = temp_string;
481             dlog_print(DLOG_INFO, LOG_TAG, " %s", m_ThreadContext.log);
482             ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateCallbackLog,
483                                                   &m_ThreadContext);
484         }
485
486     }
487     catch (std::exception &e)
488     {
489         // log(e.what());
490     }
491 }
492
493 void PrintUsage()
494 {
495     std::cout << std::endl;
496     std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
497     std::cout << "   ObserveType : 1 - Observe" << std::endl;
498     std::cout << "   ObserveType : 2 - ObserveAll" << std::endl;
499 }
500
501 static void
502 win_delete_request_cb(void *data , Evas_Object *obj , void *event_info)
503 {
504     ui_app_exit();
505 }
506
507 static void
508 win_back_cb(void *data, Evas_Object *obj, void *event_info)
509 {
510     ui_app_exit();
511 }
512
513 static void
514 send_msg_clicked_cb(void *data , Evas_Object *obj , void *event_info)
515 {
516     appdata_s *ad = (appdata_s *)data;
517     if (isSendMessage == true)
518     {
519         try
520         {
521             m_pm = new PluginManager();
522
523             // Get Plugins
524             std::vector<Plugin> user_plugin;
525
526             user_plugin = m_pm->getPlugins();
527
528             for (unsigned int i = 0; i < user_plugin.size(); i++)
529             {
530                 strcpy(temp_string, "Calling Get Plugin<br>");
531                 sprintf(buf, "value Name = %s<br>", user_plugin[i].getName().c_str());
532                 strcat(temp_string, buf);
533                 sprintf(buf, "value ID = %s<br>", user_plugin[i].getID().c_str());
534                 strcat(temp_string, buf);
535                 m_ThreadContext.log = temp_string;
536                 dlog_print(DLOG_INFO, LOG_TAG, " %s", m_ThreadContext.log);
537                 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateCallbackLog,
538                                                       &m_ThreadContext);
539                 std::cout << "value Name = " << user_plugin[i].getName() << std::endl;
540                 std::cout << "value ID = " << user_plugin[i].getID() << std::endl;
541             }
542
543
544             std::cout << "start fan Plugin by Resource Type"  << std::endl;
545             strcpy(temp_string, "start fan Plugin<br>");
546             strcat(temp_string, "fan Plugin is getting started. Please wait...<br>");
547             m_ThreadContext.log = temp_string;
548             dlog_print(DLOG_INFO, LOG_TAG, " %s", m_ThreadContext.log);
549             ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateCallbackLog,
550                                                   &m_ThreadContext);
551             m_pm->startPlugins("ResourceType", "oic.fan");
552             sleep(2);
553             // Makes it so that all boolean values are printed as 'true/false' in this stream
554             std::cout.setf(std::ios::boolalpha);
555             std::cout << "Before find Resource... " << std::endl;
556
557             // Find fan resources
558             std::ostringstream requestURI;
559             requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.fan";
560             OCPlatform::findResource("", requestURI.str(), OC_ALL, &foundResourceFan);
561             std::cout << "Finding Resource... " << std::endl;
562         }
563         catch (OCException &e)
564         {
565             // log(e.what());
566         }
567
568         elm_object_text_set(ad->send_msg_button, "Press here to close the App");
569         isSendMessage = false;
570     }
571     else
572     {
573         isSendMessage = true;
574         ui_app_exit();
575     }
576 }
577
578 static void
579 clear_log_clicked_cb(void *data , Evas_Object *obj , void *event_info)
580 {
581     appdata_s *ad = (appdata_s *)data;
582     elm_entry_entry_set(ad->log, "");
583 }
584
585 static void
586 create_base_gui(appdata_s *ad)
587 {
588     // Window
589     ad->win = elm_win_util_standard_add(PACKAGE, PACKAGE);
590     elm_win_autodel_set(ad->win, EINA_TRUE);
591
592     if (elm_win_wm_rotation_supported_get(ad->win))
593     {
594         int rots[4] = { 0, 90, 180, 270 };
595         elm_win_wm_rotation_available_rotations_set(ad->win, (const int *)(&rots), 4);
596     }
597
598     evas_object_smart_callback_add(ad->win, "delete,request", win_delete_request_cb, NULL);
599     eext_object_event_callback_add(ad->win, EEXT_CALLBACK_BACK, win_back_cb, ad);
600
601     // Conformant
602     ad->conform = elm_conformant_add(ad->win);
603     elm_win_indicator_mode_set(ad->win, ELM_WIN_INDICATOR_SHOW);
604     elm_win_indicator_opacity_set(ad->win, ELM_WIN_INDICATOR_OPAQUE);
605     evas_object_size_hint_weight_set(ad->conform, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
606     elm_win_resize_object_add(ad->win, ad->conform);
607     evas_object_show(ad->conform);
608
609     // naviframe
610     ad->naviframe = elm_naviframe_add(ad->conform);
611     elm_object_content_set(ad->conform, ad->naviframe);
612     evas_object_show(ad->naviframe);
613
614     // Box container
615     ad->box = elm_box_add(ad->naviframe);
616     elm_box_horizontal_set(ad->box, EINA_FALSE);
617     evas_object_size_hint_weight_set(ad->box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
618     evas_object_size_hint_align_set(ad->box, EVAS_HINT_FILL, EVAS_HINT_FILL);
619     elm_box_padding_set(ad->box, 0, 15 * elm_config_scale_get());
620     elm_naviframe_item_push(ad->naviframe, "ProtocolPlugin App", NULL, NULL, ad->box, NULL);
621     evas_object_show(ad->box);
622
623     ad->send_msg_button = elm_button_add(ad->box);
624     evas_object_size_hint_align_set(ad->send_msg_button, EVAS_HINT_FILL, 0.0);
625     elm_object_text_set(ad->send_msg_button, "Invoke Fan Plugin");
626     evas_object_smart_callback_add(ad->send_msg_button, "clicked", send_msg_clicked_cb, ad);
627     elm_box_pack_end(ad->box, ad->send_msg_button);
628     evas_object_show(ad->send_msg_button);
629
630     ad->clear_log_button = elm_button_add(ad->box);
631     evas_object_size_hint_align_set(ad->clear_log_button, EVAS_HINT_FILL, 0.0);
632     elm_object_text_set(ad->clear_log_button, "Clear Log");
633     evas_object_smart_callback_add(ad->clear_log_button, "clicked", clear_log_clicked_cb, ad);
634     elm_box_pack_end(ad->box, ad->clear_log_button);
635     evas_object_show(ad->clear_log_button);
636
637     ad->log = elm_entry_add(ad->box);
638     elm_object_part_text_set(ad->log, "elm.guide", "Log messages will be update here!!!");
639     elm_entry_scrollable_set(ad->log, EINA_TRUE);
640     evas_object_size_hint_weight_set(ad->log, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
641     evas_object_size_hint_align_set(ad->log, EVAS_HINT_FILL, EVAS_HINT_FILL);
642     elm_box_pack_end(ad->box, ad->log);
643     evas_object_show(ad->log);
644
645     // Show window after base gui is set up
646     evas_object_show(ad->win);
647
648     std::string name;
649     std::string key = "Name";
650     std::string  state = "";
651     std::string  id = "";
652
653     // Create PlatformConfig object
654     PlatformConfig cfg
655     {
656         OC::ServiceType::InProc,
657         OC::ModeType::Both,
658         "0.0.0.0",
659         0,
660         OC::QualityOfService::LowQos
661     };
662
663     OCPlatform::Configure(cfg);
664 }
665
666 static bool
667 app_create(void *data)
668 {
669     /* Hook to take necessary actions before main event loop starts
670         Initialize UI resources and application's data
671         If this function returns true, the main loop of application starts
672         If this function returns false, the application is terminated */
673     appdata_s *ad = (appdata_s *)data;
674     m_ThreadContext.ad = ad;
675     m_ThreadContext.log = NULL;
676     create_base_gui(ad);
677
678     return true;
679 }
680
681 static void
682 app_control(app_control_h app_control, void *data)
683 {
684     // Handle the launch request.
685     dlog_print(DLOG_INFO, LOG_TAG, "#### in app_control");
686 }
687
688 static void
689 app_pause(void *data)
690 {
691     // Take necessary actions when application becomes invisible.
692     dlog_print(DLOG_INFO, LOG_TAG, "#### in app_pause");
693 }
694
695 static void
696 app_resume(void *data)
697 {
698     // Take necessary actions when application becomes visible.
699     dlog_print(DLOG_INFO, LOG_TAG, "#### in app_resume");
700 }
701
702 static void
703 app_terminate(void *data)
704 {
705     // Release all resources.
706     dlog_print(DLOG_INFO, LOG_TAG, "#### in app_terminate");
707
708 }
709
710 static void
711 ui_app_lang_changed(app_event_info_h event_info, void *user_data)
712 {
713     // APP_EVENT_LANGUAGE_CHANGED
714     char *locale = NULL;
715     system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &locale);
716     elm_language_set(locale);
717     free(locale);
718     return;
719 }
720
721 static void
722 ui_app_orient_changed(app_event_info_h event_info, void *user_data)
723 {
724     // APP_EVENT_DEVICE_ORIENTATION_CHANGED
725     dlog_print(DLOG_INFO, LOG_TAG, "#### app orient changed");
726     return;
727 }
728
729 static void
730 ui_app_region_changed(app_event_info_h event_info, void *user_data)
731 {
732     // APP_EVENT_REGION_FORMAT_CHANGED
733 }
734
735 static void
736 ui_app_low_battery(app_event_info_h event_info, void *user_data)
737 {
738     // APP_EVENT_LOW_BATTERY
739 }
740
741 static void
742 ui_app_low_memory(app_event_info_h event_info, void *user_data)
743 {
744     // APP_EVENT_LOW_MEMORY
745 }
746
747 int
748 main(int argc, char *argv[])
749 {
750     appdata_s ad = {0,};
751     int ret = 0;
752
753     ui_app_lifecycle_callback_s event_callback = {0,};
754     app_event_handler_h handlers[5] = {NULL, };
755
756     event_callback.create = app_create;
757     event_callback.terminate = app_terminate;
758     event_callback.pause = app_pause;
759     event_callback.resume = app_resume;
760     event_callback.app_control = app_control;
761
762     ui_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY,
763                              ui_app_low_battery, &ad);
764     ui_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY,
765                              ui_app_low_memory, &ad);
766     ui_app_add_event_handler(&handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED],
767                              APP_EVENT_DEVICE_ORIENTATION_CHANGED, ui_app_orient_changed, &ad);
768     ui_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED,
769                              ui_app_lang_changed, &ad);
770     ui_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED],
771                              APP_EVENT_REGION_FORMAT_CHANGED, ui_app_region_changed, &ad);
772     ui_app_remove_event_handler(handlers[APP_EVENT_LOW_MEMORY]);
773
774     ret = ui_app_main(argc, argv, &event_callback, &ad);
775
776     if (ret != APP_ERROR_NONE)
777     {
778         dlog_print(DLOG_ERROR, LOG_TAG, "app_main() is failed. err = %d", ret);
779     }
780
781     return ret;
782 }