Handle the pending dbus calls before termination
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / bt-service-main.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <glib.h>
19 #include <dlog.h>
20 #include <string.h>
21 #include <vconf.h>
22
23 #include <bundle.h>
24 #include <eventsystem.h>
25
26 #include "bt-internal-types.h"
27 #include "bt-service-common.h"
28 #include "bt-request-handler.h"
29 #include "bt-service-obex-event.h"
30 #ifdef TIZEN_FEATURE_BT_PAN_NAP
31 #include "bt-service-pan-nap-event.h"
32 #endif
33 #include "bt-service-event.h"
34 #include "bt-service-util.h"
35
36 #include "bt-service-core-adapter.h"
37 #include "bt-service-core-adapter-le.h"
38
39
40
41 /* OAL headers */
42 #include <oal-event.h>
43 #include <oal-manager.h>
44
45 static GMainLoop *main_loop;
46 static gboolean terminated = FALSE;
47 static gboolean is_initialized = FALSE;
48
49 static void __on_log_glib(const gchar *log_domain, GLogLevelFlags log_level,
50                 const gchar *msg, gpointer user_data)
51 {
52         BT_ERR_C("%s", msg);
53 }
54
55 static void __bt_release_service(void)
56 {
57         _bt_service_le_deinit();
58         _bt_deinit_hf_local_term_event_sender();
59         _bt_deinit_service_event_sender();
60         _bt_service_unregister();
61         _bt_service_cynara_deinit();
62
63         if (!TIZEN_PROFILE_TV)
64                 _bt_deinit_obex_event_receiver();
65
66 #ifdef TIZEN_FEATURE_BT_PAN_NAP
67          _bt_deinit_pan_nap_event_receiver();
68 #endif
69
70         _bt_cleanup_profiles();
71         oal_bt_deinit();
72
73         BT_DBG("Terminating the bt-service daemon");
74 }
75
76 static void __bt_sigterm_handler(int signo, siginfo_t *info, void *data)
77 {
78         BT_INFO("signal [%d] is sent by [%d]", signo, info->si_pid);
79
80         if (!TIZEN_PROFILE_TV) {
81                 int ret;
82                 ret = _bt_recover_adapter();
83                 if (ret != BLUETOOTH_ERROR_NONE)
84                         BT_ERR("_bt_recover_adapter is failed : %d", ret);
85         }
86         return;
87 }
88
89 gboolean _bt_terminate_service(gpointer user_data)
90 {
91         __bt_release_service();
92
93         terminated = TRUE;
94
95         BT_INFO_C("Terminating the bt-service daemon");
96
97         if (main_loop != NULL) {
98                 g_main_loop_quit(main_loop);
99         } else {
100                 BT_ERR("main_loop == NULL");
101                 exit(0);
102         }
103
104         return FALSE;
105 }
106
107 static gboolean __bt_main_loop_quit_idle_cb(gpointer user_data)
108 {
109         _bt_deinit_proxys();
110
111         _bt_clear_request_list();
112
113         _bt_set_disabled(BLUETOOTH_ERROR_NONE);
114
115         _bt_deinit_service_event_sender();
116
117         terminated = TRUE;
118
119         BT_INFO_C("Terminating the bt-service daemon");
120
121         if (main_loop != NULL) {
122                 g_main_loop_quit(main_loop);
123         } else {
124                 BT_ERR("main_loop == NULL");
125                 exit(0);
126         }
127
128         return FALSE;
129 }
130
131 gboolean _bt_reliable_terminate_service(gpointer user_data)
132 {
133         _bt_service_unregister();
134
135         /* Handle remaining pending requests finally by using g_idle_add()
136            again without well-known name. */
137         g_idle_add((GSourceFunc)__bt_main_loop_quit_idle_cb, NULL);
138
139         return FALSE;
140 }
141
142 static gboolean __bt_check_bt_service(void *data)
143 {
144         bt_status_t status = BT_DEACTIVATED;
145         bt_le_status_t le_status = BT_LE_DEACTIVATED;
146         int bt_status = VCONFKEY_BT_STATUS_OFF;
147         int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
148         int flight_mode_deactivation = 0;
149         int bt_off_due_to_timeout = 0;
150 #if 0
151         int ps_mode_deactivation = 0;
152 #endif
153
154         if (_is_name_acquired() == FALSE) {
155                 BT_ERR("dbus name is NOT acquired yet");
156                 return TRUE;
157         }
158
159         status = _bt_adapter_get_status();
160         le_status = _bt_adapter_get_le_status();
161         BT_DBG("State: %d, LE State: %d", status, le_status);
162
163         if (TIZEN_PROFILE_TV) {
164 #if TODO_40 /* Need to add this function */
165                 if (_bt_get_enable_timer_id() == 0)
166 #endif
167                         _bt_enable_adapter();
168         } else if (!headed_plugin_info->plugin_headed_enabled) {
169                 BT_DBG("Enable adapter if headless device");
170                 _bt_enable_adapter();
171         } else {
172                 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
173                         BT_DBG("no bluetooth device info, so BT was disabled at previous session");
174
175                 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
176                         BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
177
178                 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_deactivation) != 0)
179                         BT_ERR("Fail to get the flight_mode_deactivation value");
180
181 #if 0
182         if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_deactivation) != 0)
183                 BT_ERR("Fail to get the ps_mode_deactivation value");
184 #endif
185
186                 if (vconf_get_int(BT_OFF_DUE_TO_TIMEOUT, &bt_off_due_to_timeout) != 0)
187                         BT_ERR("Fail to get BT_OFF_DUE_TO_TIMEOUT");
188
189                 BT_DBG("headed VCONF BT_STATUS:[%d] LE_STATUS:[%d] BT_OFF_DUE_TO_TIMEOUT:[%d]",
190                 bt_status, bt_le_status, bt_off_due_to_timeout);
191
192                 if (bt_off_due_to_timeout) {
193                         /* Set the vconf flag to 0 here, if BT need to be enabled
194                         bt_off_due_to_timeout variable already hold the old value */
195
196                         if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 0) != 0)
197                                 BT_ERR("Set vconf failed");
198                 }
199
200
201                 if ((bt_status != VCONFKEY_BT_STATUS_OFF || bt_off_due_to_timeout) &&
202                         (status == BT_DEACTIVATED)) {
203                         BT_DBG("Previous session was enabled.");
204
205                         /* Enable the BT */
206                         _bt_enable_adapter();
207                 } else if (bt_status == VCONFKEY_BT_STATUS_OFF && flight_mode_deactivation == 1) {
208                         _bt_enable_core();
209                 }
210
211                 if ((bt_le_status == VCONFKEY_BT_LE_STATUS_ON) && (le_status == BT_LE_DEACTIVATED)) {
212                         BT_DBG("Previous session was le enabled. Turn BT LE on automatically.");
213
214
215                         /* Enable the BT LE */
216                         _bt_enable_adapter_le();
217
218                 } else {
219                         status = _bt_adapter_get_status();
220                         le_status = _bt_adapter_get_le_status();
221                         BT_DBG("State: %d, LE State: %d", status, le_status);
222
223                         if ((status != BT_ACTIVATING && status != BT_ACTIVATED) &&
224                                         (le_status != BT_LE_ACTIVATING && le_status != BT_LE_ACTIVATED)) {
225                                 _bt_terminate_service(NULL);
226                         }
227                 }
228         }
229         return FALSE;
230 }
231
232 int _bt_service_initialize(void)
233 {
234         int ret;
235
236         if (is_initialized == TRUE)
237                 return BLUETOOTH_ERROR_NONE;
238
239         /* Security Initialization */
240         if (_bt_service_cynara_init() != BLUETOOTH_ERROR_NONE) {
241                 BT_ERR("Fail to init cynara");
242                 return EXIT_FAILURE;
243         }
244
245         /* Flight mode handler implemented in HAL */
246
247         /* Event sender Init */
248         ret = _bt_init_service_event_sender();
249         if (ret != BLUETOOTH_ERROR_NONE) {
250                 BT_ERR("Fail to init event sender");
251                 return ret;
252         }
253
254         ret = _bt_init_hf_local_term_event_sender();
255         if (ret != BLUETOOTH_ERROR_NONE) {
256                 BT_ERR("Fail to init core event sender");
257                 return ret;
258         }
259
260         /* Event reciever Init */
261         if (!TIZEN_PROFILE_TV) {
262                 if (_bt_init_obex_event_receiver() != BLUETOOTH_ERROR_NONE)
263                         BT_ERR("Fail to init obex event reciever");
264         }
265
266 #ifdef TIZEN_FEATURE_BT_PAN_NAP
267         /* Event reciever Init */
268         if (_bt_init_pan_nap_event_receiver() != BLUETOOTH_ERROR_NONE) {
269                 BT_ERR("Fail to init PAN NAP reciever");
270                 return 0;
271         }
272 #endif
273 #ifdef TIZEN_FEATURE_BT_IPSP
274         if (_bt_init_ipsp_receiver() != BLUETOOTH_ERROR_NONE) {
275                 BT_ERR("Fail to init ipsp reciever");
276                 return 0;
277         }
278 #endif
279         ret = _bt_service_register();
280         if (ret != BLUETOOTH_ERROR_NONE) {
281                 BT_ERR("Fail to register service");
282                 return ret;
283         }
284
285         /* BT Stack Init */
286         ret = _bt_stack_init();
287         if (ret != BLUETOOTH_ERROR_NONE) {
288                 BT_ERR("Fail to init BT Stack");
289                 return ret;
290         }
291
292         ret = _bt_service_le_init();
293         if (ret != BLUETOOTH_ERROR_NONE) {
294                 BT_ERR("Fail to init le");
295                 return ret;
296         }
297
298         _bt_init_request_id();
299
300         _bt_init_request_list();
301
302         is_initialized = TRUE;
303
304         return BLUETOOTH_ERROR_NONE;
305 }
306
307 int main(void)
308 {
309         struct sigaction sa;
310         BT_INFO_C("### Starting the bt-service daemon");
311
312         if (!TIZEN_FEATURE_BT_SUPPORTED) {
313                 BT_INFO_C("BT feature is not supported, terminate bt-service");
314                 return 0;
315         }
316
317         memset(&sa, 0, sizeof(sa));
318         sa.sa_sigaction = __bt_sigterm_handler;
319         sa.sa_flags = SA_SIGINFO;
320         sigaction(SIGINT, &sa, NULL);
321         sigaction(SIGTERM, &sa, NULL);
322
323         if (_bt_service_initialize() != BLUETOOTH_ERROR_NONE)
324                 return 0;
325
326         g_timeout_add(500, (GSourceFunc)__bt_check_bt_service, NULL);
327
328         if (terminated == TRUE) {
329                 __bt_release_service();
330                 return 0;
331         }
332
333         bluetooth_plugin_init();
334
335         g_log_set_default_handler(__on_log_glib, NULL);
336
337         main_loop = g_main_loop_new(NULL, FALSE);
338
339         g_main_loop_run(main_loop);
340         BT_DBG("g_main_loop_quit called!");
341
342         bluetooth_plugin_deinit();
343
344         _bt_service_unref_connection();
345
346         if (main_loop != NULL)
347                 g_main_loop_unref(main_loop);
348
349         if (terminated == FALSE)
350                 __bt_release_service();
351
352         return 0;
353 }
354