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>
28 #include <eventsystem.h>
31 #include "bt-core-main.h"
32 #include "bt-core-adapter.h"
33 #include "bt-core-common.h"
34 #include "bt-core-dbus-handler.h"
35 #include "bt-core-noti-handler.h"
37 #define BT_CORE_IDLE_TERM_TIME 200 /* 200ms */
39 static bt_status_t adapter_status = BT_DEACTIVATED;
40 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
41 static gboolean is_recovery_mode = FALSE;
43 static int bt_status_before[BT_MODE_MAX] = { VCONFKEY_BT_STATUS_OFF, };
44 static int bt_le_status_before[BT_MODE_MAX] = { 0, };
46 static DBusGConnection *conn = NULL;
48 static void __bt_core_set_status(bt_status_t status)
50 adapter_status = status;
53 bt_status_t _bt_core_get_status(void)
55 return adapter_status;
58 static void __bt_core_set_le_status(bt_le_status_t status)
60 adapter_le_status = status;
63 bt_le_status_t _bt_core_get_le_status(void)
65 return adapter_le_status;
68 int _bt_core_get_bt_status(bt_mode_e mode)
70 return bt_status_before[mode];
73 int _bt_core_get_bt_le_status(bt_mode_e mode)
75 return bt_le_status_before[mode];
78 void _bt_core_set_bt_status(bt_mode_e mode, int status)
80 bt_status_before[mode] = status;
83 void _bt_core_set_bt_le_status(bt_mode_e mode, int status)
85 bt_le_status_before[mode] = status;
88 gboolean _bt_core_is_recovery_mode(void)
90 return is_recovery_mode;
93 static gboolean __bt_core_idle_terminate(gpointer data)
100 gboolean _bt_core_is_flight_mode_enabled(void)
102 #ifdef TIZEN_BT_FLIGHTMODE_ENABLED
103 int isFlightMode = 0;
106 ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &isFlightMode);
108 BT_ERR("vconf_get_bool failed");
116 static int __execute_command(const char *cmd, char *const arg_list[])
126 BT_ERR("fork failed");
132 BT_ERR("fork failed");
133 } else if (pid2 == 0) {
134 execv(cmd, arg_list);
141 BT_DBG("parent : forked[%d]", pid);
142 waitpid(pid, &status, 0);
143 BT_DBG("child is terminated : %d", status);
150 int _bt_enable_adapter(void)
154 bt_le_status_t le_status;
158 status = _bt_core_get_status();
159 if (status != BT_DEACTIVATED) {
160 BT_ERR("Invalid state %d", status);
164 le_status = _bt_core_get_le_status();
165 if (le_status == BT_LE_ACTIVATED) {
166 /* Turn on PSCAN, (ISCAN if needed) */
167 /* Return with 0 for the Enabled response. */
168 __bt_core_set_status(BT_ACTIVATED);
169 BT_INFO("BR/EDR is enabled.");
173 __bt_core_set_status(BT_ACTIVATING);
175 char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
176 ret = __execute_command("/usr/bin/hciconfig", argv_up);
178 ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
181 BT_ERR("running script failed");
182 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
183 __bt_core_set_status(BT_DEACTIVATED);
190 int _bt_disable_adapter(void)
193 bt_le_status_t le_status;
195 BT_INFO_C("Disable adapter");
197 le_status = _bt_core_get_le_status();
198 BT_DBG("le_status : %d", le_status);
199 if (le_status == BT_LE_ACTIVATED) {
200 /* Turn off PSCAN, (ISCAN if needed) */
201 /* Return with 0 for the Disabled response. */
202 __bt_core_set_status(BT_DEACTIVATED);
203 BT_INFO("BR/EDR is disabled. now LE only mode");
204 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
208 status = _bt_core_get_status();
209 if (status == BT_ACTIVATING) {
210 /* Forcely terminate */
212 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
213 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
215 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
217 BT_ERR("running script failed");
219 _bt_core_terminate();
221 } else if (status != BT_ACTIVATED) {
222 BT_ERR("Invalid state %d", status);
225 __bt_core_set_status(BT_DEACTIVATING);
227 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
228 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
230 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
232 BT_ERR("running script failed");
233 __bt_core_set_status( BT_ACTIVATED);
240 int _bt_enable_adapter_le(void)
246 bt_le_status_t le_status;
247 le_status = _bt_core_get_le_status();
248 retv_if(le_status != BT_LE_DEACTIVATED, -1);
250 status = _bt_core_get_status();
251 if (status == BT_DEACTIVATED) {
252 __bt_core_set_le_status(BT_LE_ACTIVATING);
253 BT_DBG("Activate BT");
255 char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
256 ret = __execute_command("/usr/bin/hciconfig", argv_up);
258 ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
261 BT_ERR("running script failed");
262 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh &", NULL);
263 __bt_core_set_status(BT_DEACTIVATED);
264 __bt_core_set_le_status(BT_LE_DEACTIVATED);
268 __bt_core_set_le_status(BT_LE_ACTIVATED);
269 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
275 int _bt_disable_adapter_le(void)
280 bt_le_status_t le_status;
282 le_status = _bt_core_get_le_status();
283 retv_if(le_status == BT_LE_DEACTIVATED, 0);
284 retv_if(le_status == BT_LE_DEACTIVATING, -1);
286 status = _bt_core_get_status();
287 BT_DBG("status : %d", status);
289 if (status == BT_DEACTIVATED) {
290 __bt_core_set_le_status(BT_LE_DEACTIVATING);
292 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
293 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
295 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
297 BT_ERR("running script failed");
298 __bt_core_set_le_status(BT_LE_ACTIVATED);
302 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
305 __bt_core_set_le_status(BT_LE_DEACTIVATED);
311 int _bt_core_service_request_adapter(int service_function)
315 GArray *in_param1 = NULL;
316 GArray *in_param2 = NULL;
317 GArray *in_param3 = NULL;
318 GArray *in_param4 = NULL;
319 GArray *out_param = NULL;
321 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
323 ret = _bt_core_service_request(BT_CORE_SERVICE, service_function,
324 in_param1, in_param2, in_param3, in_param4, &out_param);
326 BT_ERR("_bt_core_service_request_adapter() failed");
328 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
333 void _bt_core_update_status(void)
335 int bt_status = VCONFKEY_BT_STATUS_OFF;
336 int bt_le_status = 0;
338 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
339 BT_ERR("no bluetooth device info, so BT was disabled at previous session");
341 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
342 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
344 BT_INFO("bt_status = %d, bt_le_status = %d", bt_status, bt_le_status);
346 if (bt_status == VCONFKEY_BT_STATUS_OFF)
347 __bt_core_set_status(BT_DEACTIVATED);
349 __bt_core_set_status(BT_ACTIVATED);
351 if (bt_le_status == 0)
352 __bt_core_set_le_status(BT_LE_DEACTIVATED);
354 __bt_core_set_le_status(BT_LE_ACTIVATED);
357 gboolean _bt_core_enable_adapter(void)
361 _bt_set_flightmode_request(FALSE);
362 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
363 BT_ERR("Set vconf failed");
365 ret = _bt_enable_adapter();
372 gboolean _bt_core_disable_adapter(void)
376 _bt_set_flightmode_request(FALSE);
377 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
378 BT_ERR("Set vconf failed");
380 ret = _bt_disable_adapter();
387 gboolean _bt_core_recover_adapter(void)
392 BT_INFO_C("Recover bt adapter");
394 _bt_set_flightmode_request(FALSE);
395 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
396 BT_ERR("Set vconf failed");
398 is_recovery_mode = TRUE;
400 _bt_core_update_status();
402 if (_bt_core_get_status() == BT_ACTIVATED) {
403 _bt_core_set_bt_status(BT_RECOVERY_MODE, 1);
404 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
406 if (_bt_core_get_le_status() == BT_LE_ACTIVATED) {
407 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 1);
408 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
411 ret = _bt_disable_adapter();
413 BT_ERR("_bt_disable_adapter() failed");
414 ret_le = _bt_disable_adapter_le();
416 BT_ERR("_bt_disable_adapter_le() failed");
421 gboolean _bt_core_enable_adapter_le(void)
425 ret = _bt_enable_adapter_le();
432 gboolean _bt_core_disable_adapter_le(void)
438 ret = _bt_disable_adapter_le();
445 gboolean __bt_core_reset_adapter(void)
447 /* Forcely terminate */
448 if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0) {
449 BT_ERR("running script failed");
451 _bt_core_terminate();
456 static gboolean __bt_core_enable_core_timeout_cb(gpointer data)
460 _bt_core_init_vconf_value();
465 gboolean _bt_core_enable_core(void)
469 _bt_core_update_status();
471 g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
477 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
481 char *arg_list[3] = { NULL, NULL, NULL };
483 BT_DBG("Test item : %s", type);
485 if (g_strcmp0(type, "Enable_RF_Test") == 0) {
486 cmd = "/usr/etc/bluetooth/bt-edutm-on.sh";
487 arg_list[0] = "bt-edutm-on.sh";
488 } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
489 cmd = "/usr/etc/bluetooth/bt-edutm-off.sh";
490 arg_list[0] = "bt-edutm-off.sh";
491 } else if (g_strcmp0(type, "Slave_Mode") == 0) {
492 cmd = "/usr/etc/bluetooth/bt-mode-slave.sh";
493 arg_list[0] = "bt-mode-slave.sh";
494 } else if (g_strcmp0(type, "Master_Mode") == 0) {
495 cmd = "/usr/etc/bluetooth/bt-mode-master.sh";
496 arg_list[0] = "bt-mode-master.sh";
497 } else if (g_strcmp0(type, "SSP_Debug_Mode") == 0) {
498 cmd = "/usr/etc/bluetooth/bt-set-ssp-debug-mode.sh";
499 arg_list[0] = "bt-set-ssp-debug-mode.sh";
500 arg_list[1] = (char *)arg;
501 } else if (g_strcmp0(type, "RF_Channel") == 0) {
502 cmd = "/usr/etc/bluetooth/bt-enable-rf-channel.sh";
503 arg_list[0] = "bt-enable-rf-channel.sh";
504 arg_list[1] = (char *)arg;
506 _bt_core_terminate();
510 BT_DBG("Run %s", cmd);
511 if (__execute_command(cmd, arg_list) < 0) {
512 BT_ERR("running script failed");
515 _bt_core_terminate();
519 static gboolean __bt_core_recovery_cb(gpointer data)
522 gboolean is_request_failed = FALSE;
523 static gboolean is_first_failure = TRUE;
527 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
528 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
530 is_request_failed = TRUE;
533 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
534 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
536 is_request_failed = TRUE;
539 if (is_request_failed == TRUE) {
540 BT_ERR("Recovery is failed.");
541 if (is_first_failure == TRUE) {
542 g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
543 is_first_failure = FALSE;
546 is_first_failure = TRUE;
550 is_first_failure = TRUE;
552 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
553 _bt_core_set_bt_status(BT_RECOVERY_MODE, 0);
554 ret = _bt_enable_adapter();
556 BT_ERR("_bt_enable_adapter() failed");
558 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
559 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 0);
560 ret = _bt_enable_adapter_le();
562 BT_ERR("_bt_enable_adapter_le() failed");
565 is_recovery_mode = FALSE;
572 static gboolean __bt_core_enable_timeout_cb(gpointer data)
574 bt_status_t adapter_status;
575 bt_le_status_t adapter_status_le;
579 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
580 BT_ERR("Set vconf failed");
582 adapter_status = _bt_core_get_status();
583 adapter_status_le = _bt_core_get_le_status();
585 if (adapter_status == BT_DEACTIVATED &&
586 _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
587 _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
588 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
589 _bt_enable_adapter();
592 if (adapter_status_le == BT_LE_DEACTIVATED &&
593 _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
594 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
595 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
596 _bt_enable_adapter_le();
602 static gboolean __bt_core_disable_timeout_cb(gpointer data)
604 bt_status_t adapter_status;
605 bt_le_status_t adapter_status_le;
609 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
610 BT_ERR("Set vconf failed");
612 adapter_status = _bt_core_get_status();
613 adapter_status_le = _bt_core_get_le_status();
615 if (adapter_status == BT_ACTIVATED) {
616 int bt_status_before_mode = 0;
618 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
619 _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
621 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
622 _bt_disable_adapter();
625 if (adapter_status_le == BT_LE_ACTIVATED) {
626 int bt_le_status_before_mode = 0;
628 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
629 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
631 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
632 _bt_disable_adapter_le();
638 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value)
645 bundle_add_str(b, key, value);
647 ret = eventsystem_request_sending_system_event(event, b);
649 BT_DBG("request_sending_system_event result: %d", ret);
656 void _bt_core_adapter_added_cb(void)
659 bt_le_status_t le_status;
660 gboolean flight_mode_status;
664 status = _bt_core_get_status();
665 BT_DBG("status : %d", status);
666 le_status = _bt_core_get_le_status();
667 BT_DBG("le_status : %d", le_status);
669 if (status == BT_ACTIVATING)
670 __bt_core_set_status(BT_ACTIVATED);
671 if (le_status == BT_LE_ACTIVATING)
672 __bt_core_set_le_status(BT_LE_ACTIVATED);
674 flight_mode_status = _bt_core_is_flight_mode_enabled();
676 if (flight_mode_status == TRUE && _bt_is_flightmode_request() == TRUE) {
677 _bt_set_flightmode_request(FALSE);
678 g_timeout_add(2000, (GSourceFunc)__bt_core_disable_timeout_cb, NULL);
681 _bt_set_flightmode_request(FALSE);
682 _bt_core_terminate();
685 void _bt_core_adapter_removed_cb(void)
687 int flight_mode_value = 0;
688 int power_saving_mode = 0;
689 gboolean flight_mode_status;
690 static int timer_id = -1;
694 __bt_core_set_status(BT_DEACTIVATED);
695 __bt_core_set_le_status(BT_LE_DEACTIVATED);
696 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
697 BT_ERR("Set vconf failed");
699 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
700 BT_ERR("Set vconf failed");
703 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
704 EVT_VAL_BT_OFF) != ES_R_OK)
705 BT_ERR("Fail to set value");
707 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
708 EVT_VAL_BT_LE_OFF) != ES_R_OK)
709 BT_ERR("Fail to set value");
711 if (is_recovery_mode == TRUE)
714 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
718 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value) != 0)
719 BT_ERR("Fail to get the flight_mode_deactivated value");
721 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &power_saving_mode) != 0)
722 BT_ERR("Fail to get the ps_mode_deactivated value");
724 flight_mode_status = _bt_core_is_flight_mode_enabled();
726 if (flight_mode_status == FALSE && _bt_is_flightmode_request() == TRUE) {
727 _bt_set_flightmode_request(FALSE);
729 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_enable_timeout_cb, NULL);
732 _bt_set_flightmode_request(FALSE);
734 if (flight_mode_value == 1 || power_saving_mode == 1){
735 BT_DBG("Bt Core not terminated");
739 _bt_core_terminate();