2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <vconf-keys.h>
20 #include <vconf-internal-radio-keys.h>
22 #include <eventsystem.h>
25 #include "bt-core-main.h"
26 #include "bt-core-adapter.h"
27 #include "bt-core-common.h"
28 #include "bt-core-dbus-handler.h"
29 #include "bt-core-noti-handler.h"
31 #define BT_CORE_IDLE_TERM_TIME 200 /* 200ms */
32 #define BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX 50
34 #ifdef TIZEN_FEATURE_BT_OTP
35 #define BT_OTP_OBJECT_PATH "/org/projectx/otp"
36 #define BT_OTP_INTERFACE_NAME "org.projectx.otp_service"
37 #define BLE_DISABLED "LeDisabled"
40 static bt_status_t adapter_status = BT_DEACTIVATED;
41 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
42 static gboolean is_recovery_mode = FALSE;
43 static guint core_enable_timer_id;
46 static int bt_status_before[BT_MODE_MAX] = { VCONFKEY_BT_STATUS_OFF, };
47 static int bt_le_status_before[BT_MODE_MAX] = { VCONFKEY_BT_LE_STATUS_OFF, };
49 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value);
51 static void __bt_core_set_status(bt_status_t status)
53 adapter_status = status;
56 bt_status_t _bt_core_get_status(void)
58 return adapter_status;
61 static void __bt_core_set_le_status(bt_le_status_t status)
63 adapter_le_status = status;
66 bt_le_status_t _bt_core_get_le_status(void)
68 return adapter_le_status;
71 int _bt_core_get_bt_status(bt_mode_e mode)
73 return bt_status_before[mode];
76 int _bt_core_get_bt_le_status(bt_mode_e mode)
78 return bt_le_status_before[mode];
81 void _bt_core_set_bt_status(bt_mode_e mode, int status)
83 bt_status_before[mode] = status;
86 void _bt_core_set_bt_le_status(bt_mode_e mode, int status)
88 bt_le_status_before[mode] = status;
91 gboolean _bt_core_is_recovery_mode(void)
93 return is_recovery_mode;
96 static gboolean __bt_core_idle_terminate(gpointer data)
103 gboolean _bt_core_is_flight_mode_enabled(void)
105 int isFlightMode = 0;
108 if (TIZEN_FEATURE_FLIGHTMODE_ENABLED) {
109 ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &isFlightMode);
111 BT_ERR("vconf_get_bool failed");
119 static int __bt_call_systemact_service(const char *file_path)
125 BT_ERR("service file path is NULL");
129 if (!access(file_path, F_OK)) {
134 fp = fopen(file_path, "w");
136 BT_ERR("Failed to fopen service file");
147 static int __execute_command(const char *cmd, char *const arg_list[])
157 BT_ERR("fork failed");
163 BT_ERR("fork failed");
164 } else if (pid2 == 0) {
165 execv(cmd, arg_list);
172 BT_DBG("parent : forked[%d]", pid);
173 waitpid(pid, &status, 0);
174 BT_DBG("child is terminated : %d", status);
181 static int __bt_stack_up(void)
185 /* activate HCI logger */
186 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_LOGGER_START);
188 BT_ERR("Failed to call systemact service");
192 /* We need to insert BT driver module by using 'insmod' in case of ARTIK530,
193 * and 'insmod' should be executed by systemd not bt-core. */
194 ret = __bt_call_systemact_service(BT_SYSTEMACT_BT_DRIVER_INSMOD);
196 BT_ERR("Failed to call systemact service");
199 /* Insmod should run before bt-stack-up.sh command */
200 usleep(1000 * 200); /* 200ms */
203 ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
205 BT_ERR("Failed to run script");
206 __bt_call_systemact_service(BT_SYSTEMACT_HCI_LOGGER_STOP);
211 ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUEZ_START);
213 BT_ERR("Failed to call systemact service");
217 /* activate bluetooth-share */
218 ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUETOOTH_SHARE_START);
220 BT_ERR("Failed to call systemact service");
231 int _bt_enable_adapter(void)
236 bt_le_status_t le_status;
240 status = _bt_core_get_status();
241 if (status != BT_DEACTIVATED) {
242 BT_ERR("Invalid state %d", status);
243 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
247 #if 0 /* only the concept of private */
248 le_status = _bt_core_get_le_status();
249 if (le_status == BT_LE_ACTIVATED) {
250 /* Turn on PSCAN, (ISCAN if needed) */
251 /* Return with 0 for the Enabled response. */
252 __bt_core_set_status(BT_ACTIVATED);
253 BT_INFO("BR/EDR is enabled.");
254 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
259 __bt_core_set_status(BT_ACTIVATING);
260 if (TIZEN_FEATURE_BT_USB_DONGLE) {
261 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_UP);
263 BT_ERR("Failed to call systemact service");
265 ret = __bt_stack_up();
268 BT_ERR("running script failed");
269 if (TIZEN_FEATURE_BT_USB_DONGLE) {
270 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
272 BT_ERR("Failed to call systemact service");
274 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
276 __bt_core_set_status(BT_DEACTIVATED);
283 int _bt_disable_adapter(void)
287 bt_le_status_t le_status;
290 BT_INFO_C("Disable adapter");
292 #if 0 /* only the concept of private */
293 le_status = _bt_core_get_le_status();
294 BT_DBG("le_status : %d", le_status);
295 if (le_status == BT_LE_ACTIVATED) {
296 /* Turn off PSCAN, (ISCAN if needed) */
297 /* Return with 0 for the Disabled response. */
298 __bt_core_set_status(BT_DEACTIVATED);
299 BT_INFO("BR/EDR is disabled. now LE only mode");
300 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
305 status = _bt_core_get_status();
306 if (status == BT_ACTIVATING) {
307 /* Forcely terminate */
308 if (TIZEN_FEATURE_BT_USB_DONGLE) {
309 if (__bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN) < 0)
310 BT_ERR("Failed to call systemact service");
312 #ifdef TIZEN_FEATURE_RADIO
313 int radio_status = VCONFKEY_RADIO_STATUS_OFF;
315 /* Check if radio status on or off */
316 if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
317 BT_ERR("Fail to get radio status");
319 BT_DBG("Radio status: %d", radio_status);
321 if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
322 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN_WITH_RADIO) < 0)
323 BT_ERR("running script failed");
325 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0)
326 BT_ERR("running script failed");
329 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0)
330 BT_ERR("running script failed");
333 _bt_core_terminate();
335 } else if (status != BT_ACTIVATED) {
336 BT_ERR("Invalid state %d", status);
337 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
340 __bt_core_set_status(BT_DEACTIVATING);
341 if (TIZEN_FEATURE_BT_USB_DONGLE) {
342 if (__bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN) < 0) {
343 BT_ERR("Failed to call systemact service");
344 __bt_core_set_status(BT_ACTIVATED);
348 #ifdef TIZEN_FEATURE_RADIO
349 int radio_status = VCONFKEY_RADIO_STATUS_OFF;
351 /* Check if radio status on or off */
352 if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
353 BT_ERR("Fail to get radio status");
355 BT_DBG("Radio status: %d", radio_status);
357 if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
358 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN_WITH_RADIO) < 0) {
359 BT_ERR("running script failed");
360 __bt_core_set_status(BT_ACTIVATED);
364 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0) {
365 BT_ERR("running script failed");
366 __bt_core_set_status(BT_ACTIVATED);
371 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0) {
372 BT_ERR("running script failed");
373 __bt_core_set_status(BT_ACTIVATED);
382 int _bt_enable_adapter_le(void)
388 bt_le_status_t le_status;
389 le_status = _bt_core_get_le_status();
390 retv_if(le_status != BT_LE_DEACTIVATED, -1);
392 status = _bt_core_get_status();
393 if (status == BT_DEACTIVATED) {
394 __bt_core_set_le_status(BT_LE_ACTIVATING);
395 BT_DBG("Activate BT");
396 if (TIZEN_FEATURE_BT_USB_DONGLE) {
397 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_UP);
399 BT_ERR("Failed to call systemact service");
401 ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
404 BT_ERR("running script failed");
405 if (TIZEN_FEATURE_BT_USB_DONGLE) {
406 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
408 BT_ERR("Failed to call systemact service");
410 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh &", NULL);
412 __bt_core_set_status(BT_DEACTIVATED);
413 __bt_core_set_le_status(BT_LE_DEACTIVATED);
417 __bt_core_set_le_status(BT_LE_ACTIVATED);
418 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
420 #ifdef TIZEN_FEATURE_BT_HPS
421 ret = _bt_core_start_httpproxy();
423 BT_ERR("_bt_core_start_httpproxy() failed");
429 #ifdef TIZEN_FEATURE_BT_OTP
430 static void _bt_core_stop_otp()
432 GError *error = NULL;
433 GDBusMessage *msg = NULL;
434 GDBusConnection *conn = _bt_core_get_gdbus_connection();
435 msg = g_dbus_message_new_signal(BT_OTP_OBJECT_PATH, BT_OTP_INTERFACE_NAME, BLE_DISABLED);
436 if (!g_dbus_connection_send_message(conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, 0, NULL)) {
438 BT_ERR("Failed to send BLE_DISABLED signal to OTP : errCode[%x], \
440 error->code, error->message);
441 g_clear_error(&error);
447 int _bt_disable_adapter_le(void)
452 bt_le_status_t le_status;
454 le_status = _bt_core_get_le_status();
455 retv_if(le_status == BT_LE_DEACTIVATED, 0);
456 retv_if(le_status == BT_LE_DEACTIVATING, -1);
458 #ifdef TIZEN_FEATURE_BT_HPS
459 _bt_core_stop_httpproxy();
462 #ifdef TIZEN_FEATURE_BT_OTP
466 status = _bt_core_get_status();
467 BT_DBG("status : %d", status);
469 if (status == BT_DEACTIVATED) {
470 __bt_core_set_le_status(BT_LE_DEACTIVATING);
472 if (TIZEN_FEATURE_BT_USB_DONGLE) {
473 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
475 BT_ERR("Failed to call systemact service");
477 ret = __execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL);
480 BT_ERR("running script failed");
481 __bt_core_set_le_status(BT_LE_ACTIVATED);
485 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
488 __bt_core_set_le_status(BT_LE_DEACTIVATED);
494 int _bt_core_service_request_adapter(int service_function)
498 GArray *in_param1 = NULL;
499 GArray *in_param2 = NULL;
500 GArray *in_param3 = NULL;
501 GArray *in_param4 = NULL;
502 GArray *out_param = NULL;
504 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
506 ret = _bt_core_service_request(BT_CORE_SERVICE, service_function,
507 in_param1, in_param2, in_param3, in_param4, &out_param);
509 BT_ERR("_bt_core_service_request_adapter() failed");
511 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
516 static gboolean __bt_core_check_the_adapter_path(void)
519 GDBusProxy *manager_proxy = NULL;
520 GVariant *result = NULL;
521 char *adapter_path = NULL;
522 GDBusConnection *conn = NULL;
524 conn = _bt_core_get_gdbus_connection();
528 manager_proxy = g_dbus_proxy_new_sync(conn,
529 G_DBUS_PROXY_FLAGS_NONE, NULL,
532 "org.freedesktop.DBus.ObjectManager",
535 if (!manager_proxy) {
537 BT_ERR("Unable to create proxy: %s", err->message);
540 BT_ERR("Fail to create proxy");
545 result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
546 G_DBUS_CALL_FLAGS_NONE, 1000, NULL, &err);
549 BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
552 BT_ERR("Fail to get DefaultAdapter");
557 if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
558 BT_ERR("Incorrect result\n");
562 g_variant_get(result, "(&o)", &adapter_path);
564 if (adapter_path == NULL ||
565 strlen(adapter_path) >= BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX) {
566 BT_ERR("Adapter path is inproper\n");
570 g_variant_unref(result);
571 g_object_unref(manager_proxy);
577 g_variant_unref(result);
580 g_object_unref(manager_proxy);
585 void _bt_core_update_status(void)
587 int bt_status = VCONFKEY_BT_STATUS_OFF;
588 int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
589 gboolean ret = FALSE;
591 ret = __bt_core_check_the_adapter_path();
592 BT_INFO("check the real status of bt_adapter");
595 __bt_core_set_status(BT_DEACTIVATED);
596 __bt_core_set_le_status(BT_DEACTIVATED);
598 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
599 BT_ERR("no bluetooth device info, so BT was disabled at previous session");
600 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
601 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
603 BT_INFO("bt_status = %d, bt_le_status = %d", bt_status, bt_le_status);
605 if (bt_status & VCONFKEY_BT_STATUS_ON)
606 __bt_core_set_status(BT_ACTIVATED);
607 if (bt_le_status & VCONFKEY_BT_LE_STATUS_ON)
608 __bt_core_set_le_status(BT_ACTIVATED);
612 gboolean _bt_core_enable_adapter(void)
616 _bt_set_flightmode_request(FALSE);
617 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
618 BT_ERR("Set vconf failed");
620 ret = _bt_enable_adapter();
627 static gboolean __bt_core_terminate_cb(gpointer data)
629 _bt_core_terminate();
634 gboolean _bt_core_disable_adapter(void)
637 gboolean adapter_state;
639 _bt_set_flightmode_request(FALSE);
640 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
641 BT_ERR("Set vconf failed");
643 adapter_state = __bt_core_check_the_adapter_path();
644 if (adapter_state == FALSE)
645 BT_INFO("Default adapter is NOT normal.");
647 ret = _bt_disable_adapter();
648 if (adapter_state == FALSE) {
649 g_timeout_add(BT_CORE_IDLE_TERM_TIME,
650 __bt_core_terminate_cb, NULL);
660 gboolean _bt_core_recover_adapter(void)
666 BT_INFO_C("Recover bt adapter");
668 _bt_set_flightmode_request(FALSE);
669 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
670 BT_ERR("Set vconf failed");
672 is_recovery_mode = TRUE;
674 _bt_core_update_status();
676 if (_bt_core_get_status() == BT_ACTIVATED) {
677 _bt_core_set_bt_status(BT_RECOVERY_MODE, 1);
678 #ifdef TIZEN_FEATURE_BUSACT
679 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
682 if (_bt_core_get_le_status() == BT_LE_ACTIVATED) {
683 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 1);
684 #ifdef TIZEN_FEATURE_BUSACT
685 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
689 ret = _bt_disable_adapter();
691 BT_ERR("_bt_disable_adapter() failed");
693 /* In platform, don't seperate BR/EDR and LE status */
695 ret_le = _bt_disable_adapter_le();
697 BT_ERR("_bt_disable_adapter_le() failed");
702 gboolean _bt_core_enable_adapter_le(void)
706 ret = _bt_enable_adapter_le();
713 gboolean _bt_core_disable_adapter_le(void)
719 ret = _bt_disable_adapter_le();
726 gboolean __bt_core_reset_adapter(void)
728 /* Forcely terminate */
729 if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0)
730 BT_ERR("running script failed");
732 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
737 static gboolean __bt_core_enable_core_timeout_cb(gpointer data)
741 core_enable_timer_id = 0;
743 _bt_core_init_vconf_value();
748 gboolean _bt_core_enable_core(void)
752 _bt_core_update_status();
754 if (core_enable_timer_id > 0)
755 g_source_remove(core_enable_timer_id);
757 core_enable_timer_id = g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
763 gboolean _bt_core_set_transfer_value(gboolean value)
765 const char *event_val = NULL;
768 BT_DBG("value: %d", value);
770 event_val = value ? EVT_VAL_BT_TRANSFERING : EVT_VAL_BT_NON_TRANSFERING;
772 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_TRANSFERING_STATE,
773 event_val) != ES_R_OK) {
774 BT_ERR("Fail to set BT state value");
778 if (ret == FALSE || value == FALSE) {
779 BT_DBG("Terminate bt-core process");
780 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
787 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
791 char *arg_list[3] = { NULL, NULL, NULL };
793 BT_DBG("Test item : %s", type);
795 if (g_strcmp0(type, "Enable_RF_Test") == 0) {
796 cmd = "/usr/etc/bluetooth/bt-edutm-on.sh";
797 arg_list[0] = "bt-edutm-on.sh";
798 } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
799 cmd = "/usr/etc/bluetooth/bt-edutm-off.sh";
800 arg_list[0] = "bt-edutm-off.sh";
801 } else if (g_strcmp0(type, "Slave_Mode") == 0) {
802 cmd = "/usr/etc/bluetooth/bt-mode-slave.sh";
803 arg_list[0] = "bt-mode-slave.sh";
804 } else if (g_strcmp0(type, "Master_Mode") == 0) {
805 cmd = "/usr/etc/bluetooth/bt-mode-master.sh";
806 arg_list[0] = "bt-mode-master.sh";
807 } else if (g_strcmp0(type, "SSP_Debug_Mode") == 0) {
808 cmd = "/usr/etc/bluetooth/bt-set-ssp-debug-mode.sh";
809 arg_list[0] = "bt-set-ssp-debug-mode.sh";
810 arg_list[1] = (char *)arg;
811 } else if (g_strcmp0(type, "RF_Channel") == 0) {
812 cmd = "/usr/etc/bluetooth/bt-enable-rf-channel.sh";
813 arg_list[0] = "bt-enable-rf-channel.sh";
814 arg_list[1] = (char *)arg;
819 BT_DBG("Run %s", cmd);
820 if (__execute_command(cmd, arg_list) < 0)
821 BT_ERR("running script failed");
826 static gboolean __bt_core_recovery_cb(gpointer data)
829 #ifdef TIZEN_FEATURE_BUSACT
830 gboolean is_request_failed = FALSE;
831 static gboolean is_first_failure = TRUE;
836 #ifdef TIZEN_FEATURE_BUSACT
837 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
838 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
840 is_request_failed = TRUE;
843 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
844 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
846 is_request_failed = TRUE;
849 if (is_request_failed == TRUE) {
850 BT_ERR("Recovery is failed.");
851 if (is_first_failure == TRUE) {
852 g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
853 is_first_failure = FALSE;
856 is_first_failure = TRUE;
860 is_first_failure = TRUE;
863 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
864 _bt_core_set_bt_status(BT_RECOVERY_MODE, 0);
865 ret = _bt_enable_adapter();
867 BT_ERR("_bt_enable_adapter() failed");
871 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
872 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 0);
873 ret = _bt_enable_adapter_le();
875 BT_ERR("_bt_enable_adapter_le() failed");
878 is_recovery_mode = FALSE;
885 static gboolean __bt_core_enable_timeout_cb(gpointer data)
887 bt_status_t adapter_status;
888 bt_le_status_t adapter_status_le;
892 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
893 BT_ERR("Set vconf failed");
895 adapter_status = _bt_core_get_status();
896 adapter_status_le = _bt_core_get_le_status();
898 if (adapter_status == BT_DEACTIVATED &&
899 _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
900 _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
901 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
902 _bt_enable_adapter();
905 if (adapter_status_le == BT_LE_DEACTIVATED &&
906 _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
907 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
908 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
909 _bt_enable_adapter_le();
915 static gboolean __bt_core_disable_timeout_cb(gpointer data)
917 bt_status_t adapter_status;
918 bt_le_status_t adapter_status_le;
922 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
923 BT_ERR("Set vconf failed");
925 adapter_status = _bt_core_get_status();
926 adapter_status_le = _bt_core_get_le_status();
928 if (adapter_status == BT_ACTIVATED) {
929 int bt_status_before_mode = 0;
931 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
932 _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
934 #ifdef TIZEN_FEATURE_BUSACT
935 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
937 _bt_disable_adapter();
940 if (adapter_status_le == BT_LE_ACTIVATED) {
941 int bt_le_status_before_mode = 0;
943 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
944 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
946 #ifdef TIZEN_FEATURE_BUSACT
947 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
949 _bt_disable_adapter_le();
955 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value)
962 bundle_add_str(b, key, value);
964 ret = eventsystem_send_system_event(event, b);
966 BT_DBG("eventsystem_send_system_event result: %d", ret);
973 void _bt_core_adapter_added_cb(void)
976 bt_le_status_t le_status;
977 gboolean flight_mode_status;
981 status = _bt_core_get_status();
982 BT_DBG("status : %d", status);
983 le_status = _bt_core_get_le_status();
984 BT_DBG("le_status : %d", le_status);
986 if (status == BT_ACTIVATING)
987 __bt_core_set_status(BT_ACTIVATED);
988 if (le_status == BT_LE_ACTIVATING)
989 __bt_core_set_le_status(BT_LE_ACTIVATED);
991 flight_mode_status = _bt_core_is_flight_mode_enabled();
993 if (flight_mode_status == TRUE && _bt_is_flightmode_request() == TRUE) {
994 _bt_set_flightmode_request(FALSE);
995 g_timeout_add(2000, (GSourceFunc)__bt_core_disable_timeout_cb, NULL);
998 _bt_set_flightmode_request(FALSE);
1000 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1001 EVT_VAL_BT_ON) != ES_R_OK)
1002 BT_ERR("Fail to set BT state value");
1004 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
1005 EVT_VAL_BT_LE_ON) != ES_R_OK)
1006 BT_ERR("Fail to set BT LE state value");
1008 _bt_core_terminate();
1011 void _bt_core_adapter_removed_cb(void)
1013 int flight_mode_value = 0;
1014 int power_saving_mode = 0;
1015 gboolean flight_mode_status;
1016 static int timer_id = -1;
1018 BT_DBG("is_recovery_mode: %d", is_recovery_mode);
1020 __bt_core_set_status(BT_DEACTIVATED);
1021 __bt_core_set_le_status(BT_LE_DEACTIVATED);
1022 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1023 BT_ERR("Set vconf failed");
1024 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
1025 BT_ERR("Set vconf failed");
1027 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1028 EVT_VAL_BT_OFF) != ES_R_OK)
1029 BT_ERR("Fail to set value");
1031 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
1032 EVT_VAL_BT_LE_OFF) != ES_R_OK)
1033 BT_ERR("Fail to set value");
1035 if (is_recovery_mode == TRUE) {
1037 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
1041 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value) != 0)
1042 BT_ERR("Fail to get the flight_mode_deactivated value");
1044 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &power_saving_mode) != 0)
1045 BT_ERR("Fail to get the ps_mode_deactivated value");
1047 flight_mode_status = _bt_core_is_flight_mode_enabled();
1049 if (flight_mode_status == FALSE && _bt_is_flightmode_request() == TRUE) {
1050 _bt_set_flightmode_request(FALSE);
1052 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_enable_timeout_cb, NULL);
1055 _bt_set_flightmode_request(FALSE);
1057 if (flight_mode_value == 1 || power_saving_mode == 1) {
1058 BT_DBG("Bt Core not terminated");
1062 _bt_core_terminate();