Notification manager Tizen sample application
[platform/upstream/iotivity.git] / service / notification-manager / SampleApp / tizen / NMSampleApp / src / main.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 "nmsampleapp.h"
22 #include "nmutil.h"
23 #include <algorithm>
24 #include <signal.h>
25 #include "hosting.h"
26 #include "virtualResource.h"
27
28 using namespace std;
29 using namespace OC;
30
31 namespace PH = std::placeholders;
32
33 static uint8_t interfaceName[] = "wlan0";
34 int g_quitFlag = 0;
35
36 static void printLog(int logType, string data)
37 {
38     (DLOG_ERROR == logType) ? LOGE(data.c_str()) : LOGI(data.c_str());
39     ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
40                                           &data);
41 }
42
43 // Method for Finding the resource and hosting it
44 static void start_hosting(int seconds)
45 {
46     string logMessage = "OCResourceHosting started <br>";
47     logMessage = logMessage + "Interface Name : " + reinterpret_cast<char *>(interfaceName) + "<br>";
48     printLog(DLOG_INFO, logMessage);
49
50     if (OCInit((char *) NULL, 0, OC_CLIENT_SERVER) != OC_STACK_OK)
51     {
52         logMessage = "OCStack init error <br>";
53         printLog(DLOG_ERROR, logMessage);
54         return;
55     }
56
57     OICStartCoordinate();
58     g_quitFlag = 0;
59     logMessage = "OICStartCoordinate done successfully";
60     printLog(DLOG_INFO, logMessage);
61
62     while (!g_quitFlag)
63     {
64         if (OCProcess() != OC_STACK_OK)
65         {
66             OICStopCoordinate();
67             logMessage = "OCStack process error <br>";
68             printLog(DLOG_ERROR, logMessage);
69             return;
70         }
71         sleep(seconds);
72     }
73
74     OICStopCoordinate();
75     logMessage = "OICStopCoordinate done successfully <br>";
76     printLog(DLOG_INFO, logMessage);
77
78     if (OCStop() != OC_STACK_OK)
79     {
80         logMessage = "OCStack stop error <br>";
81         printLog(DLOG_ERROR, logMessage);
82     }
83     LOGI("start EXIT");
84 }
85
86 // Method for Finding the resource and hosting it
87 void stop_hosting()
88 {
89     string logMessage = "Terminating Resource Hosting <br>";
90     printLog(DLOG_INFO, logMessage);
91
92     g_quitFlag = 1;
93     LOGI("stop_hosting EXIT");
94 }
95
96 // Method to be called when the find and host UI Button is selected
97 void start_cb(void *data, Evas_Object *obj, void *event_info)
98 {
99     std::thread exec(std::function< void(int second) >(start_hosting), 3);
100     exec.detach();
101 }
102
103 // Method to be called when the find and host UI Button is selected
104 void stop_cb(void *data, Evas_Object *obj, void *event_info)
105 {
106     LOGI("Stopping resource hosting");
107     stop_hosting();
108 }