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