4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 * Girishashok Joshi <girish.joshi@samsung.com>
8 * Chanyeol Park <chanyeol.park@samsung.com>
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
25 #include <vconf-keys.h>
27 #include "bt-core-adapter.h"
28 #include "bt-core-dbus-handler.h"
29 #include "bt-core-main.h"
30 #include "bt-core-noti-handler.h"
31 #include "bt-core-common.h"
33 static GMainLoop *main_loop = NULL;
35 gboolean _bt_check_terminating_condition(void)
37 int bt_off_flight_mode = 0; /* if BT was off due to FlightMode*/
38 int bt_off_ps_mode = 0;
40 if (_bt_core_is_recovery_mode() == TRUE) {
41 BT_DBG("Bt core not terminated");
45 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &bt_off_flight_mode) != 0)
46 BT_ERR("Fail to get the BT off due to FlightMode value");
47 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &bt_off_ps_mode) != 0)
48 BT_ERR("Fail to get the ps_mode_deactivated value");
50 if (bt_off_flight_mode == 1 || bt_off_ps_mode == 1) {
51 BT_DBG("Bt core not terminated");
58 void _bt_core_terminate(void)
60 if (_bt_check_terminating_condition() == FALSE)
63 _bt_core_gdbus_deinit_proxys();
64 _bt_core_unregister_vconf_handler();
66 g_main_loop_quit(main_loop);
68 BT_DBG("Terminating bt-core daemon");
73 static void __bt_core_sigterm_handler(int signo)
75 BT_DBG("Got the signal: %d", signo);
80 static gboolean __bt_check_bt_core(void *data)
82 int bt_status = VCONFKEY_BT_STATUS_OFF;
83 int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
84 bt_status_t status = BT_DEACTIVATED;
85 bt_le_status_t le_status = BT_LE_DEACTIVATED;
86 int flight_mode_deactivation = 0;
87 int bt_off_due_to_timeout = 0;
88 int ps_mode_deactivation = 0;
91 status = _bt_core_get_status();
92 le_status = _bt_core_get_le_status();
93 BT_DBG("State: %d, LE State: %d", status, le_status);
95 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0) {
96 BT_DBG("no bluetooth device info, so BT was disabled at previous session");
99 #ifdef ENABLE_TIZEN_2_4
100 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0) {
101 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
105 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_deactivation) != 0)
106 BT_ERR("Fail to get the flight_mode_deactivation value");
108 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_deactivation) != 0)
109 BT_ERR("Fail to get the ps_mode_deactivation value");
111 if (vconf_get_int(BT_OFF_DUE_TO_TIMEOUT, &bt_off_due_to_timeout) != 0)
112 BT_ERR("Fail to get BT_OFF_DUE_TO_TIMEOUT");
114 if ((bt_status != VCONFKEY_BT_STATUS_OFF || bt_off_due_to_timeout) &&
115 (status == BT_DEACTIVATED)) {
116 BT_DBG("Previous session was enabled.");
119 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
120 #ifndef USB_BLUETOOTH
121 _bt_enable_adapter();
123 } else if (bt_status == VCONFKEY_BT_STATUS_OFF &&
124 (flight_mode_deactivation == 1 || ps_mode_deactivation > 0)) {
125 _bt_core_handle_flight_mode_noti();
126 _bt_core_handle_power_saving_mode_noti();
128 _bt_core_set_bt_status(BT_FLIGHT_MODE, flight_mode_deactivation);
129 _bt_core_set_bt_status(BT_POWER_SAVING_MODE, ps_mode_deactivation);
132 if ((bt_le_status == VCONFKEY_BT_LE_STATUS_ON) && (le_status == BT_LE_DEACTIVATED)) {
133 BT_DBG("Previous session was le enabled. Turn BT LE on automatically.");
135 /* Enable the BT LE */
136 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
137 #ifndef USB_BLUETOOTH
138 _bt_enable_adapter_le();
141 status = _bt_core_get_status();
142 le_status = _bt_core_get_le_status();
143 BT_DBG("State: %d, LE State: %d", status, le_status);
145 if ((status != BT_ACTIVATING && status != BT_ACTIVATED) &&
146 (le_status != BT_LE_ACTIVATING && le_status != BT_LE_ACTIVATED))
147 _bt_core_terminate();
159 BT_INFO_C("Starting bt-core daemeon");
161 _bt_core_update_status();
163 ret = _bt_core_register_dbus();
165 BT_ERR("_bt_core_register_dbus failed");
170 memset(&sa, 0, sizeof(sa));
171 sa.sa_handler = __bt_core_sigterm_handler;
172 sigaction(SIGINT, &sa, NULL);
173 sigaction(SIGTERM, &sa, NULL);
175 g_timeout_add(500, (GSourceFunc)__bt_check_bt_core, NULL);
177 main_loop = g_main_loop_new(NULL, FALSE);
179 BT_ERR("creating main loop failed");
183 g_main_loop_run(main_loop);
186 _bt_core_unregister_dbus();
189 g_main_loop_unref(main_loop);
191 BT_INFO_C("Terminating bt-core daemon");