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.
24 #include <dbus/dbus-glib.h>
28 #include <privilege-control.h>
31 #include "bt-internal-types.h"
32 #include "bt-service-common.h"
33 #include "bt-service-event.h"
34 #include "bt-service-main.h"
35 #include "bt-service-util.h"
36 #include "bt-request-handler.h"
37 #include "bt-service-adapter.h"
38 #include "bt-service-adapter-le.h"
40 static GMainLoop *main_loop;
41 static gboolean terminated = FALSE;
43 static void __bt_release_service(void)
45 _bt_service_unregister_vconf_handler();
47 _bt_service_adapter_le_deinit();
48 _bt_deinit_service_event_sender();
49 _bt_deinit_hf_local_term_event_sender();
50 _bt_deinit_service_event_receiver();
52 _bt_service_unregister();
56 _bt_clear_request_list();
58 BT_DBG("Terminating the bt-service daemon");
61 static void __bt_sigterm_handler(int signo)
63 BT_DBG("Get the signal: %d", signo);
65 _bt_terminate_service(NULL);
68 gboolean _bt_terminate_service(gpointer user_data)
70 int bt_status = VCONFKEY_BT_STATUS_OFF;
72 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0) {
73 BT_ERR("no bluetooth device info, so BT was disabled at previous session");
75 if (bt_status != VCONFKEY_BT_STATUS_OFF) {
76 if(vconf_set_int(VCONFKEY_BT_STATUS,
77 VCONFKEY_BT_STATUS_OFF) != 0)
78 BT_ERR("Set vconf failed\n");
82 #ifdef ENABLE_TIZEN_2_4
83 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_status) < 0) {
84 BT_ERR("no bluetooth device info, so BT was disabled at previous session");
86 if (bt_status != VCONFKEY_BT_LE_STATUS_OFF) {
87 if(vconf_set_int(VCONFKEY_BT_LE_STATUS,
88 VCONFKEY_BT_LE_STATUS_OFF) != 0)
89 BT_ERR("Set vconf failed\n");
94 if (main_loop != NULL) {
95 g_main_loop_quit(main_loop);
97 BT_ERR("main_loop == NULL");
98 __bt_release_service();
106 gboolean _bt_reliable_terminate_service(gpointer user_data)
108 _bt_deinit_service_event_receiver();
112 _bt_clear_request_list();
114 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
116 _bt_service_adapter_le_deinit();
117 _bt_deinit_service_event_sender();
118 _bt_deinit_hf_local_term_event_sender();
120 _bt_service_unregister();
124 BT_INFO_C("Terminating the bt-service daemon");
126 if (main_loop != NULL) {
127 g_main_loop_quit(main_loop);
135 static gboolean __bt_check_bt_service(void *data)
137 int bt_status = VCONFKEY_BT_STATUS_OFF;
138 int bt_le_status = 0;
139 bt_status_t status = BT_DEACTIVATED;
140 bt_le_status_t le_status = BT_LE_DEACTIVATED;
141 int flight_mode_deactivation = 0;
142 int bt_off_due_to_timeout = 0;
144 int ps_mode_deactivation = 0;
147 status = _bt_adapter_get_status();
148 le_status = _bt_adapter_get_le_status();
149 BT_DBG("State: %d, LE State: %d", status, le_status);
151 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0) {
152 BT_DBG("no bluetooth device info, so BT was disabled at previous session");
155 #ifdef ENABLE_TIZEN_2_4
156 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0) {
157 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
161 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_deactivation) != 0)
162 BT_ERR("Fail to get the flight_mode_deactivation value");
165 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_deactivation) != 0)
166 BT_ERR("Fail to get the ps_mode_deactivation value");
169 if (vconf_get_int(BT_OFF_DUE_TO_TIMEOUT, &bt_off_due_to_timeout) != 0)
170 BT_ERR("Fail to get BT_OFF_DUE_TO_TIMEOUT");
172 if ((bt_status != VCONFKEY_BT_STATUS_OFF || bt_off_due_to_timeout)
173 && (status == BT_DEACTIVATED)) {
174 BT_DBG("Previous session was enabled.");
177 _bt_enable_adapter();
178 } else if (bt_status == VCONFKEY_BT_STATUS_OFF && flight_mode_deactivation == 1) {
182 if ((bt_le_status == 1) && (le_status == BT_LE_DEACTIVATED)) {
183 BT_DBG("Previous session was le enabled. Turn BT LE on automatically.");
185 /* Enable the BT LE */
186 _bt_enable_adapter_le();
188 status = _bt_adapter_get_status();
189 le_status = _bt_adapter_get_le_status();
190 BT_DBG("State: %d, LE State: %d", status, le_status);
192 if ((status != BT_ACTIVATING && status != BT_ACTIVATED) &&
193 (le_status != BT_LE_ACTIVATING && le_status != BT_LE_ACTIVATED)){
194 _bt_terminate_service(NULL);
204 BT_INFO_C("Starting the bt-service daemon");
206 memset(&sa, 0, sizeof(sa));
207 sa.sa_handler = __bt_sigterm_handler;
208 sigaction(SIGINT, &sa, NULL);
209 sigaction(SIGTERM, &sa, NULL);
213 if (perm_app_set_privilege("bluetooth-frwk-service", NULL, NULL) !=
214 PC_OPERATION_SUCCESS)
215 BT_ERR("Failed to set app privilege.\n");
217 /* Event reciever Init */
218 if (_bt_init_service_event_receiver() != BLUETOOTH_ERROR_NONE) {
219 BT_ERR("Fail to init event reciever");
223 /* Event sender Init */
224 if (_bt_init_service_event_sender() != BLUETOOTH_ERROR_NONE) {
225 BT_ERR("Fail to init event sender");
229 if (_bt_init_hf_local_term_event_sender() != BLUETOOTH_ERROR_NONE) {
230 BT_ERR("Fail to init core event sender");
234 if (_bt_service_register() != BLUETOOTH_ERROR_NONE) {
235 BT_ERR("Fail to register service");
239 if (_bt_service_adapter_le_init() != BLUETOOTH_ERROR_NONE) {
240 BT_ERR("Fail to init le");
244 _bt_init_request_id();
246 _bt_init_request_list();
248 g_timeout_add(500, (GSourceFunc)__bt_check_bt_service, NULL);
250 if (terminated == TRUE) {
251 __bt_release_service();
255 main_loop = g_main_loop_new(NULL, FALSE);
257 g_main_loop_run(main_loop);
258 BT_DBG("g_main_loop_quit called!");
260 if (main_loop != NULL) {
261 g_main_loop_unref(main_loop);
264 if (terminated == FALSE)
265 __bt_release_service();