Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / resource-hosting / SampleApp / tizen / RHSampleApp / src / rhmain.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 "rhsampleapp.h"
22 #include "rhutil.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 (OICStartCoordinate() != OC_STACK_OK)
50     {
51         logMessage = "OICStartCoordinate FAILED <br>";
52         printLog(DLOG_ERROR, logMessage);
53         return;
54     }
55
56     g_quitFlag = 0;
57     logMessage = "OICStartCoordinate done successfully <br>";
58     printLog(DLOG_INFO, logMessage);
59
60     while (!g_quitFlag)
61     {
62         if (OCProcess() != OC_STACK_OK)
63         {
64             OICStopCoordinate();
65             logMessage = "OCStack process error <br>";
66             printLog(DLOG_ERROR, logMessage);
67             return;
68         }
69         sleep(seconds);
70     }
71
72     if (OICStopCoordinate() != OC_STACK_OK)
73     {
74         logMessage = "OICStopCoordinate FAILED <br>";
75         printLog(DLOG_ERROR, logMessage);
76     }
77     logMessage = "OICStopCoordinate done successfully <br>";
78     printLog(DLOG_INFO, logMessage);
79     LOGI("start EXIT");
80 }
81
82 // Method for Finding the resource and hosting it
83 void stop_hosting()
84 {
85     string logMessage = "Terminating Resource Hosting <br>";
86     printLog(DLOG_INFO, logMessage);
87
88     if(!g_quitFlag)
89     {
90         g_quitFlag = 1;
91     }
92     else
93     {
94         string logMessage = "Resource Hosting already terminated <br>";
95         printLog(DLOG_INFO, logMessage);
96     }
97     LOGI("stop_hosting EXIT");
98 }
99
100 // Method to be called when the find and host UI Button is selected
101 void start_cb(void *data, Evas_Object *obj, void *event_info)
102 {
103     std::thread exec(std::function< void(int second) >(start_hosting), 3);
104     exec.detach();
105 }
106
107 // Method to be called when the find and host UI Button is selected
108 void stop_cb(void *data, Evas_Object *obj, void *event_info)
109 {
110     LOGI("Stopping resource hosting");
111     stop_hosting();
112 }