Mesh: Fix coding style
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / 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 #ifdef TIZEN_FEATURE_BT_FACTORY_MODE
23 #include <bincfg.h>
24 #endif
25
26 #include <unistd.h>
27 #include <sys/socket.h>
28 #include <bundle.h>
29 #include <eventsystem.h>
30 #include <ell/ell.h>
31
32 #include "bt-internal-types.h"
33 #include "bt-service-common.h"
34 #include "bt-service-audio-common.h"
35 #include "bt-request-handler.h"
36 #include "bt-service-obex-event.h"
37 #ifdef TIZEN_FEATURE_BT_PAN_NAP
38 #include "bt-service-pan-nap-event.h"
39 #endif
40 #include "bt-service-event.h"
41 #include "bt-service-util.h"
42
43 #include "bt-service-core-adapter.h"
44 #include "bt-service-core-adapter-le.h"
45
46
47
48 /* OAL headers */
49 #include <oal-event.h>
50 #include <oal-manager.h>
51
52 /* ELL Start */
53 struct ell_event_source {
54         GSource source;
55         GPollFD pollfd;
56 };
57
58 static gboolean event_prepare(GSource *source, gint *timeout)
59 {
60         int r = l_main_prepare();
61         *timeout = r;
62
63         return FALSE;
64 }
65
66 static gboolean event_check(GSource *source)
67 {
68         l_main_iterate(0);
69         return FALSE;
70 }
71
72 static GSourceFuncs event_funcs = {
73         .prepare = event_prepare,
74         .check = event_check,
75 };
76 /* ELL End */
77
78 static GMainLoop *main_loop;
79 static gboolean terminated = FALSE;
80 static gboolean is_initialized = FALSE;
81
82 static void __on_log_glib(const gchar *log_domain, GLogLevelFlags log_level,
83                 const gchar *msg, gpointer user_data)
84 {
85         BT_ERR_C("%s", msg);
86 }
87
88 static void __bt_release_service(void)
89 {
90 #ifdef TIZEN_FEATURE_BT_AVC_TARGET
91         _bt_audio_deinit_absolute_volume_control();
92 #endif
93
94         _bt_service_le_deinit();
95         _bt_deinit_hf_local_term_event_sender();
96         _bt_deinit_service_event_sender();
97         _bt_service_unregister();
98         _bt_service_cynara_deinit();
99
100         if (!TIZEN_PROFILE_TV)
101                 _bt_deinit_obex_event_receiver();
102
103 #ifdef TIZEN_FEATURE_BT_PAN_NAP
104          _bt_deinit_pan_nap_event_receiver();
105 #endif
106
107         _bt_cleanup_profiles();
108         oal_bt_deinit();
109
110         BT_DBG("Terminating the bt-service daemon");
111 }
112
113 static void __bt_sigterm_handler(int signo, siginfo_t *info, void *data)
114 {
115         BT_INFO("signal [%d] is sent by [%d]", signo, info->si_pid);
116
117         if (!TIZEN_PROFILE_TV) {
118                 int ret;
119                 ret = _bt_recover_adapter();
120                 if (ret != BLUETOOTH_ERROR_NONE)
121                         BT_ERR("_bt_recover_adapter is failed : %d", ret);
122         }
123         return;
124 }
125
126 gboolean _bt_terminate_service(gpointer user_data)
127 {
128         __bt_release_service();
129
130         terminated = TRUE;
131
132         BT_INFO_C("Terminating the bt-service daemon");
133
134         if (main_loop != NULL) {
135                 g_main_loop_quit(main_loop);
136         } else {
137                 BT_ERR("main_loop == NULL");
138                 exit(0);
139         }
140
141         return FALSE;
142 }
143
144 static gboolean __bt_main_loop_quit_idle_cb(gpointer user_data)
145 {
146         _bt_deinit_proxys();
147         _bt_deinit_osp_server();
148
149         _bt_clear_request_list();
150
151         _bt_set_disabled(BLUETOOTH_ERROR_NONE);
152
153         _bt_deinit_service_event_sender();
154
155         terminated = TRUE;
156
157         BT_INFO_C("Terminating the bt-service daemon");
158
159         if (main_loop != NULL) {
160                 g_main_loop_quit(main_loop);
161         } else {
162                 BT_ERR("main_loop == NULL");
163                 exit(0);
164         }
165
166         return FALSE;
167 }
168
169 gboolean _bt_reliable_terminate_service(gpointer user_data)
170 {
171         _bt_service_unregister();
172
173         /* Handle remaining pending requests finally by using g_idle_add()
174            again without well-known name. */
175         g_idle_add((GSourceFunc)__bt_main_loop_quit_idle_cb, NULL);
176
177         return FALSE;
178 }
179
180 static void __bt_check_factory_mode(void)
181 {
182 #ifdef TIZEN_FEATURE_BT_FACTORY_MODE
183         int factorymode_status = 0;
184         /* BT should not be activated automatically in factory binary */
185         /* factorymode_status :: 0 - normal binary, 1 - factory binary */
186         factorymode_status = bincfg_is_factory_binary();
187         if (factorymode_status != 0) {
188                 BT_INFO("factory mode [%d]", factorymode_status);
189                 if(vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
190                         BT_ERR("Set vconf failed");
191                 if(vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
192                         BT_ERR("Set vconf failed");
193         }
194 #endif
195 }
196
197 static gboolean __bt_check_bt_service(void *data)
198 {
199         int ret;
200         bt_status_t status = BT_DEACTIVATED;
201         bt_le_status_t le_status = BT_LE_DEACTIVATED;
202         int bt_status = VCONFKEY_BT_STATUS_OFF;
203         int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
204         int flight_mode_deactivation = 0;
205         int bt_off_due_to_timeout = 0;
206 #if 0
207         int ps_mode_deactivation = 0;
208 #endif
209
210         if (_is_name_acquired() == FALSE) {
211                 BT_ERR("dbus name is NOT acquired yet");
212                 return TRUE;
213         }
214
215         status = _bt_adapter_get_status();
216         le_status = _bt_adapter_get_le_status();
217         BT_DBG("State: %d, LE State: %d", status, le_status);
218
219         if (TIZEN_PROFILE_WEARABLE) {
220                 if (status == BT_ACTIVATED) {
221                         BT_INFO("Need to recover because bt has been activated without bt-service");
222                         ret = _bt_recover_adapter();
223                         if (ret != BLUETOOTH_ERROR_NONE)
224                                 BT_ERR("_bt_recover_adapter is failed : %d", ret);
225                         return FALSE;
226                 }
227         }
228
229         if (TIZEN_PROFILE_TV) {
230 #if TODO_40 /* Need to add this function */
231                 if (_bt_get_enable_timer_id() == 0)
232 #endif
233                         _bt_enable_adapter();
234         } else if (!headed_plugin_info->plugin_headed_enabled) {
235                 BT_DBG("Enable adapter if headless device");
236                 _bt_enable_adapter();
237         } else {
238                 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
239                         BT_DBG("no bluetooth device info, so BT was disabled at previous session");
240
241                 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
242                         BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
243
244                 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_deactivation) != 0)
245                         BT_ERR("Fail to get the flight_mode_deactivation value");
246
247 #if 0
248         if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_deactivation) != 0)
249                 BT_ERR("Fail to get the ps_mode_deactivation value");
250 #endif
251
252                 if (vconf_get_int(BT_OFF_DUE_TO_TIMEOUT, &bt_off_due_to_timeout) != 0)
253                         BT_ERR("Fail to get BT_OFF_DUE_TO_TIMEOUT");
254
255                 BT_DBG("headed VCONF BT_STATUS:[%d] LE_STATUS:[%d] BT_OFF_DUE_TO_TIMEOUT:[%d]",
256                 bt_status, bt_le_status, bt_off_due_to_timeout);
257
258                 if (bt_off_due_to_timeout) {
259                         /* Set the vconf flag to 0 here, if BT need to be enabled
260                         bt_off_due_to_timeout variable already hold the old value */
261
262                         if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 0) != 0)
263                                 BT_ERR("Set vconf failed");
264                 }
265
266
267                 if ((bt_status != VCONFKEY_BT_STATUS_OFF || bt_off_due_to_timeout) &&
268                         (status == BT_DEACTIVATED)) {
269                         BT_DBG("Previous session was enabled.");
270
271                         /* Enable the BT */
272                         _bt_enable_adapter();
273                 }
274
275                 if ((bt_le_status == VCONFKEY_BT_LE_STATUS_ON) && (le_status == BT_LE_DEACTIVATED)) {
276                         BT_DBG("Previous session was le enabled. Turn BT LE on automatically.");
277
278
279                         /* Enable the BT LE */
280                         _bt_enable_adapter_le();
281
282                 } else {
283                         status = _bt_adapter_get_status();
284                         le_status = _bt_adapter_get_le_status();
285                         BT_DBG("State: %d, LE State: %d", status, le_status);
286
287                         if ((status != BT_ACTIVATING && status != BT_ACTIVATED) &&
288                                         (le_status != BT_LE_ACTIVATING && le_status != BT_LE_ACTIVATED)) {
289                                 _bt_terminate_service(NULL);
290                         }
291                 }
292         }
293         return FALSE;
294 }
295
296 int _bt_service_initialize(void)
297 {
298         int ret;
299
300         if (is_initialized == TRUE)
301                 return BLUETOOTH_ERROR_NONE;
302
303         /* Security Initialization */
304         if (_bt_service_cynara_init() != BLUETOOTH_ERROR_NONE) {
305                 BT_ERR("Fail to init cynara");
306                 return EXIT_FAILURE;
307         }
308
309         /* Flight mode handler */
310         _bt_service_register_vconf_handler();
311
312         /* Event sender Init */
313         ret = _bt_init_service_event_sender();
314         if (ret != BLUETOOTH_ERROR_NONE) {
315                 BT_ERR("Fail to init event sender");
316                 return ret;
317         }
318
319         ret = _bt_init_hf_local_term_event_sender();
320         if (ret != BLUETOOTH_ERROR_NONE) {
321                 BT_ERR("Fail to init core event sender");
322                 return ret;
323         }
324
325         /* Event reciever Init */
326         if (!TIZEN_PROFILE_TV) {
327                 if (_bt_init_obex_event_receiver() != BLUETOOTH_ERROR_NONE)
328                         BT_ERR("Fail to init obex event reciever");
329         }
330
331 #ifdef TIZEN_FEATURE_BT_PAN_NAP
332         /* Event reciever Init */
333         if (_bt_init_pan_nap_event_receiver() != BLUETOOTH_ERROR_NONE) {
334                 BT_ERR("Fail to init PAN NAP reciever");
335                 return 0;
336         }
337 #endif
338 #ifdef TIZEN_FEATURE_BT_IPSP
339         if (_bt_init_ipsp_receiver() != BLUETOOTH_ERROR_NONE) {
340                 BT_ERR("Fail to init ipsp reciever");
341                 return 0;
342         }
343 #endif
344         ret = _bt_service_register();
345         if (ret != BLUETOOTH_ERROR_NONE) {
346                 BT_ERR("Fail to register service");
347                 return ret;
348         }
349
350         /* BT Stack Init */
351         ret = _bt_stack_init();
352         if (ret != BLUETOOTH_ERROR_NONE) {
353                 BT_ERR("Fail to init BT Stack");
354                 return ret;
355         }
356
357         ret = _bt_service_le_init();
358         if (ret != BLUETOOTH_ERROR_NONE) {
359                 BT_ERR("Fail to init le");
360                 return ret;
361         }
362
363         _bt_init_request_id();
364
365         _bt_init_request_list();
366
367 #ifdef TIZEN_FEATURE_BT_AVC_TARGET
368         ret = _bt_audio_init_absolute_volume_control();
369         if (ret != BLUETOOTH_ERROR_NONE)
370                 BT_ERR("Fail to init AVC");
371 #endif
372
373         is_initialized = TRUE;
374
375         return BLUETOOTH_ERROR_NONE;
376 }
377
378 int main(void)
379 {
380         struct sigaction sa;
381         /* ELL Start */
382         struct ell_event_source *source;
383         /* ELL End */
384         BT_INFO_C("### Starting the bt-service daemon");
385
386         if (!TIZEN_FEATURE_BT_SUPPORTED) {
387                 BT_INFO_C("BT feature is not supported, terminate bt-service");
388                 return 0;
389         }
390
391         memset(&sa, 0, sizeof(sa));
392         sa.sa_sigaction = __bt_sigterm_handler;
393         sa.sa_flags = SA_SIGINFO;
394         sigaction(SIGINT, &sa, NULL);
395         sigaction(SIGTERM, &sa, NULL);
396
397         __bt_check_factory_mode();
398
399         if (_bt_service_initialize() != BLUETOOTH_ERROR_NONE)
400                 return 0;
401
402         g_timeout_add(500, (GSourceFunc)__bt_check_bt_service, NULL);
403
404         if (terminated == TRUE) {
405                 __bt_release_service();
406                 return 0;
407         }
408
409         /* ELL Start */
410         BT_INFO("ELL Main init");
411         l_main_init();
412         /* ELL End */
413
414         bluetooth_plugin_init();
415
416         g_log_set_default_handler(__on_log_glib, NULL);
417
418         main_loop = g_main_loop_new(NULL, FALSE);
419
420         /* ELL Start */
421         source = (struct ell_event_source *) g_source_new(&event_funcs,
422                         sizeof(struct ell_event_source));
423
424         source->pollfd.fd = l_main_get_epoll_fd();
425         source->pollfd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
426
427         g_source_add_poll((GSource *)source, &source->pollfd);
428         g_source_attach((GSource *) source,
429                         g_main_loop_get_context(main_loop));
430
431         /* ELL End */
432         g_main_loop_run(main_loop);
433         BT_DBG("g_main_loop_quit called!");
434
435         bluetooth_plugin_deinit();
436
437         _bt_service_unref_connection();
438
439         /* ELL Start */
440         g_source_destroy((GSource *) source);
441         /* ELL End */
442
443         if (main_loop != NULL)
444                 g_main_loop_unref(main_loop);
445
446         if (terminated == FALSE)
447                 __bt_release_service();
448
449         /* ELL Start */
450         l_main_exit();
451         /* ELL End */
452
453         return 0;
454 }
455