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>
24 #ifdef TIZEN_FEATURE_ACTD
25 #include <actd/unit_control.h>
28 #include "bt-core-main.h"
29 #include "bt-core-adapter.h"
30 #include "bt-core-common.h"
31 #include "bt-core-dbus-handler.h"
32 #include "bt-core-noti-handler.h"
34 #include "bt-internal-types.h"
36 #define BT_CORE_IDLE_TERM_TIME 200 /* 200ms */
37 #define BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX 50
39 #ifdef TIZEN_FEATURE_BT_OTP
40 #define BT_OTP_OBJECT_PATH "/org/projectx/otp"
41 #define BT_OTP_INTERFACE_NAME "org.projectx.otp_service"
42 #define BLE_DISABLED "LeDisabled"
45 static bt_status_t adapter_status = BT_DEACTIVATED;
46 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
47 static gboolean is_recovery_mode = FALSE;
48 static guint core_enable_timer_id;
51 static int bt_status_before[BT_MODE_MAX] = { VCONFKEY_BT_STATUS_OFF, };
52 static int bt_le_status_before[BT_MODE_MAX] = { VCONFKEY_BT_LE_STATUS_OFF, };
54 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value);
56 static void __bt_core_set_status(bt_status_t status)
58 adapter_status = status;
61 bt_status_t _bt_core_get_status(void)
63 return adapter_status;
66 static void __bt_core_set_le_status(bt_le_status_t status)
68 adapter_le_status = status;
71 bt_le_status_t _bt_core_get_le_status(void)
73 return adapter_le_status;
76 int _bt_core_get_bt_status(bt_mode_e mode)
78 return bt_status_before[mode];
81 int _bt_core_get_bt_le_status(bt_mode_e mode)
83 return bt_le_status_before[mode];
86 void _bt_core_set_bt_status(bt_mode_e mode, int status)
88 bt_status_before[mode] = status;
91 void _bt_core_set_bt_le_status(bt_mode_e mode, int status)
93 bt_le_status_before[mode] = status;
96 gboolean _bt_core_is_recovery_mode(void)
98 return is_recovery_mode;
101 static gboolean __bt_core_idle_terminate(gpointer data)
104 _bt_core_terminate();
108 gboolean _bt_core_is_flight_mode_enabled(void)
110 int isFlightMode = 0;
113 if (TIZEN_FEATURE_FLIGHTMODE_ENABLED) {
114 ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &isFlightMode);
116 BT_ERR("vconf_get_bool failed");
124 #ifdef TIZEN_FEATURE_ACTD
125 static int __bt_call_systemact_service(const char *service_name)
129 BT_DBG("Use System Activated : %s", service_name);
131 ret = actd_start_unit(UNIT_CONTROL_BUS_TYPE_SYSTEM, service_name, 5000);
133 if (ret != UNIT_CONTROL_OK) {
134 BT_ERR("Failed to activate the %s: %d", service_name, ret);
135 /* Return Success even if the activating result is fail */
142 static int __bt_call_systemact_service(const char *file_path)
148 BT_ERR("service file path is NULL");
152 if (!access(file_path, F_OK)) {
157 fp = fopen(file_path, "w");
159 BT_ERR("Failed to fopen service file");
171 static int __execute_command(const char *cmd, char *const arg_list[])
181 BT_ERR("fork failed");
187 BT_ERR("fork failed");
188 } else if (pid2 == 0) {
189 if (arg_list != NULL) {
190 execv(cmd, arg_list);
192 char *argv[] = { NULL };
201 BT_DBG("parent : forked[%d]", pid);
202 waitpid(pid, &status, 0);
203 BT_DBG("child is terminated : %d", status);
210 static int __bt_stack_up(void)
214 /* activate HCI logger */
215 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_LOGGER_START);
217 BT_ERR("Failed to call systemact service");
222 ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUEZ_START);
224 BT_ERR("Failed to call systemact service");
228 /* activate bluetooth-share */
229 if (!TIZEN_PROFILE_WEARABLE) {
230 ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUETOOTH_SHARE_START);
232 BT_ERR("Failed to call systemact service");
240 int _bt_enable_adapter(void)
245 bt_le_status_t le_status;
249 status = _bt_core_get_status();
250 if (status != BT_DEACTIVATED) {
251 BT_ERR("Invalid state %d", status);
252 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
256 #if 0 /* only the concept of private */
257 le_status = _bt_core_get_le_status();
258 if (le_status == BT_LE_ACTIVATED) {
259 /* Turn on PSCAN, (ISCAN if needed) */
260 /* Return with 0 for the Enabled response. */
261 __bt_core_set_status(BT_ACTIVATED);
262 BT_INFO("BR/EDR is enabled.");
263 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
268 __bt_core_set_status(BT_ACTIVATING);
269 if (TIZEN_FEATURE_BT_USB_DONGLE) {
271 ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUEZ_START);
273 BT_ERR("If bluez already exist, skip this error");
275 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_UP);
277 BT_ERR("Failed to call systemact service");
279 ret = __bt_stack_up();
282 BT_ERR("running script failed");
283 if (TIZEN_FEATURE_BT_USB_DONGLE) {
284 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
286 BT_ERR("Failed to call systemact service");
288 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
290 __bt_core_set_status(BT_DEACTIVATED);
297 int _bt_disable_adapter(void)
301 bt_le_status_t le_status;
304 BT_INFO_C("Disable adapter");
306 #if 0 /* only the concept of private */
307 le_status = _bt_core_get_le_status();
308 BT_DBG("le_status : %d", le_status);
309 if (le_status == BT_LE_ACTIVATED) {
310 /* Turn off PSCAN, (ISCAN if needed) */
311 /* Return with 0 for the Disabled response. */
312 __bt_core_set_status(BT_DEACTIVATED);
313 BT_INFO("BR/EDR is disabled. now LE only mode");
314 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
319 status = _bt_core_get_status();
320 if (status == BT_ACTIVATING) {
321 /* Forcely terminate */
322 if (TIZEN_FEATURE_BT_USB_DONGLE) {
323 if (__bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN) < 0)
324 BT_ERR("Failed to call systemact service");
326 #ifdef TIZEN_FEATURE_RADIO
327 int radio_status = VCONFKEY_RADIO_STATUS_OFF;
329 /* Check if radio status on or off */
330 if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
331 BT_ERR("Fail to get radio status");
333 BT_DBG("Radio status: %d", radio_status);
335 if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
336 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN_WITH_RADIO) < 0)
337 BT_ERR("running script failed");
339 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0)
340 BT_ERR("running script failed");
343 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0)
344 BT_ERR("running script failed");
347 _bt_core_terminate();
349 } else if (status != BT_ACTIVATED) {
350 BT_ERR("Invalid state %d", status);
351 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
354 __bt_core_set_status(BT_DEACTIVATING);
355 if (TIZEN_FEATURE_BT_USB_DONGLE) {
356 if (__bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN) < 0) {
357 BT_ERR("Failed to call systemact service");
358 __bt_core_set_status(BT_ACTIVATED);
361 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
363 #ifdef TIZEN_FEATURE_RADIO
364 int radio_status = VCONFKEY_RADIO_STATUS_OFF;
366 /* Check if radio status on or off */
367 if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
368 BT_ERR("Fail to get radio status");
370 BT_DBG("Radio status: %d", radio_status);
372 if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
373 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN_WITH_RADIO) < 0) {
374 BT_ERR("running script failed");
375 __bt_core_set_status(BT_ACTIVATED);
379 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0) {
380 BT_ERR("running script failed");
381 __bt_core_set_status(BT_ACTIVATED);
386 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0) {
387 BT_ERR("running script failed");
388 __bt_core_set_status(BT_ACTIVATED);
397 int _bt_enable_adapter_le(void)
403 bt_le_status_t le_status;
404 le_status = _bt_core_get_le_status();
405 retv_if(le_status == BT_LE_ACTIVATED, 0);
406 retv_if(le_status != BT_LE_DEACTIVATED, -1);
408 status = _bt_core_get_status();
409 if (status == BT_DEACTIVATED) {
410 __bt_core_set_le_status(BT_LE_ACTIVATING);
411 BT_DBG("Activate BT");
412 if (TIZEN_FEATURE_BT_USB_DONGLE) {
413 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_UP);
415 BT_ERR("Failed to call systemact service");
417 ret = __bt_stack_up();
420 BT_ERR("running script failed");
421 if (TIZEN_FEATURE_BT_USB_DONGLE) {
422 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
424 BT_ERR("Failed to call systemact service");
426 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh &", NULL);
428 __bt_core_set_status(BT_DEACTIVATED);
429 __bt_core_set_le_status(BT_LE_DEACTIVATED);
433 __bt_core_set_le_status(BT_LE_ACTIVATED);
434 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
436 #ifdef TIZEN_FEATURE_BT_HPS
437 ret = _bt_core_start_httpproxy();
439 BT_ERR("_bt_core_start_httpproxy() failed");
445 #ifdef TIZEN_FEATURE_BT_OTP
446 static void _bt_core_stop_otp()
448 GError *error = NULL;
449 GDBusMessage *msg = NULL;
450 GDBusConnection *conn = _bt_core_get_gdbus_connection();
451 msg = g_dbus_message_new_signal(BT_OTP_OBJECT_PATH, BT_OTP_INTERFACE_NAME, BLE_DISABLED);
452 if (!g_dbus_connection_send_message(conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, 0, NULL)) {
454 BT_ERR("Failed to send BLE_DISABLED signal to OTP : errCode[%x], \
456 error->code, error->message);
457 g_clear_error(&error);
463 int _bt_disable_adapter_le(void)
468 bt_le_status_t le_status;
470 le_status = _bt_core_get_le_status();
471 retv_if(le_status == BT_LE_DEACTIVATED, 0);
472 retv_if(le_status == BT_LE_DEACTIVATING, -1);
474 #ifdef TIZEN_FEATURE_BT_HPS
475 _bt_core_stop_httpproxy();
478 #ifdef TIZEN_FEATURE_BT_OTP
482 status = _bt_core_get_status();
483 BT_DBG("status : %d", status);
485 if (status == BT_DEACTIVATED) {
486 __bt_core_set_le_status(BT_LE_DEACTIVATING);
488 if (TIZEN_FEATURE_BT_USB_DONGLE) {
489 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
491 BT_ERR("Failed to call systemact service");
493 ret = __execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL);
496 BT_ERR("running script failed");
497 __bt_core_set_le_status(BT_LE_ACTIVATED);
501 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
504 __bt_core_set_le_status(BT_LE_DEACTIVATED);
510 int _bt_core_service_request_adapter(int service_function)
514 GArray *in_param1 = NULL;
515 GArray *in_param2 = NULL;
516 GArray *in_param3 = NULL;
517 GArray *in_param4 = NULL;
518 GArray *out_param = NULL;
520 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
522 ret = _bt_core_service_request(BT_CORE_SERVICE, service_function,
523 in_param1, in_param2, in_param3, in_param4, &out_param);
525 BT_ERR("_bt_core_service_request_adapter() failed");
527 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
532 static gboolean __bt_core_check_the_adapter_path(void)
535 GDBusProxy *manager_proxy = NULL;
536 GVariant *result = NULL;
537 char *adapter_path = NULL;
538 GDBusConnection *conn = NULL;
540 conn = _bt_core_get_gdbus_connection();
544 manager_proxy = g_dbus_proxy_new_sync(conn,
545 G_DBUS_PROXY_FLAGS_NONE, NULL,
548 "org.freedesktop.DBus.ObjectManager",
551 if (!manager_proxy) {
553 BT_ERR("Unable to create proxy: %s", err->message);
556 BT_ERR("Fail to create proxy");
561 result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
562 G_DBUS_CALL_FLAGS_NONE, 1000, NULL, &err);
565 BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
568 BT_ERR("Fail to get DefaultAdapter");
573 if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
574 BT_ERR("Incorrect result\n");
578 g_variant_get(result, "(&o)", &adapter_path);
580 if (adapter_path == NULL ||
581 strlen(adapter_path) >= BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX) {
582 BT_ERR("Adapter path is inproper\n");
586 g_variant_unref(result);
587 g_object_unref(manager_proxy);
593 g_variant_unref(result);
596 g_object_unref(manager_proxy);
601 void _bt_core_update_status(void)
603 int bt_status = VCONFKEY_BT_STATUS_OFF;
604 int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
605 gboolean ret = FALSE;
607 ret = __bt_core_check_the_adapter_path();
608 BT_INFO("check the real status of bt_adapter");
611 __bt_core_set_status(BT_DEACTIVATED);
612 __bt_core_set_le_status(BT_DEACTIVATED);
614 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
615 BT_ERR("no bluetooth device info, so BT was disabled at previous session");
616 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
617 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
619 BT_INFO("bt_status = %d, bt_le_status = %d", bt_status, bt_le_status);
621 if (bt_status & VCONFKEY_BT_STATUS_ON)
622 __bt_core_set_status(BT_ACTIVATED);
623 if (bt_le_status & VCONFKEY_BT_LE_STATUS_ON)
624 __bt_core_set_le_status(BT_ACTIVATED);
628 gboolean _bt_core_enable_adapter(void)
632 _bt_set_flightmode_request(FALSE);
633 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
634 BT_ERR("Set vconf failed");
636 ret = _bt_enable_adapter();
643 static gboolean __bt_core_terminate_cb(gpointer data)
645 _bt_core_terminate();
650 gboolean _bt_core_disable_adapter(void)
653 gboolean adapter_state;
655 _bt_set_flightmode_request(FALSE);
657 adapter_state = __bt_core_check_the_adapter_path();
658 if (adapter_state == FALSE)
659 BT_INFO("Default adapter is NOT normal.");
661 ret = _bt_disable_adapter();
662 if (adapter_state == FALSE) {
663 g_timeout_add(BT_CORE_IDLE_TERM_TIME,
664 __bt_core_terminate_cb, NULL);
674 gboolean _bt_core_recover_adapter(void)
680 BT_INFO_C("Recover bt adapter");
682 _bt_set_flightmode_request(FALSE);
683 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
684 BT_ERR("Set vconf failed");
686 is_recovery_mode = TRUE;
688 _bt_core_update_status();
690 if (_bt_core_get_status() == BT_ACTIVATED) {
691 _bt_core_set_bt_status(BT_RECOVERY_MODE, 1);
692 #ifdef TIZEN_FEATURE_BUSACT
693 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
696 if (_bt_core_get_le_status() == BT_LE_ACTIVATED) {
697 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 1);
698 #ifdef TIZEN_FEATURE_BUSACT
699 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
703 ret = _bt_disable_adapter();
705 BT_ERR("_bt_disable_adapter() failed");
707 /* In platform, don't seperate BR/EDR and LE status */
709 ret_le = _bt_disable_adapter_le();
711 BT_ERR("_bt_disable_adapter_le() failed");
716 gboolean _bt_core_enable_adapter_le(void)
720 ret = _bt_enable_adapter_le();
727 gboolean _bt_core_disable_adapter_le(void)
733 ret = _bt_disable_adapter_le();
740 gboolean __bt_core_reset_adapter(void)
742 /* Forcely terminate */
743 if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0)
744 BT_ERR("running script failed");
746 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
751 static gboolean __bt_core_enable_core_timeout_cb(gpointer data)
755 core_enable_timer_id = 0;
757 _bt_core_init_vconf_value();
762 gboolean _bt_core_enable_core(void)
766 _bt_core_update_status();
768 if (core_enable_timer_id > 0)
769 g_source_remove(core_enable_timer_id);
771 core_enable_timer_id = g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
777 gboolean _bt_core_set_transfer_value(gboolean value)
779 const char *event_val = NULL;
782 BT_DBG("value: %d", value);
784 event_val = value ? EVT_VAL_BT_TRANSFERING : EVT_VAL_BT_NON_TRANSFERING;
786 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_TRANSFERING_STATE,
787 event_val) != ES_R_OK) {
788 BT_ERR("Fail to set BT state value");
792 if (ret == FALSE || value == FALSE) {
793 BT_DBG("Terminate bt-core process");
794 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
801 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
804 BT_DBG("Test item : %s", type);
806 #ifdef TIZEN_FEATURE_ACTD
807 if (g_strcmp0(type, "Enable_RF_Test") == 0) {
808 ret = __bt_call_systemact_service(BT_SYSTEMACT_EDUTM_ON);
810 BT_ERR("Failed to call systemact service");
811 } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
812 ret = __bt_call_systemact_service(BT_SYSTEMACT_EDUTM_OFF);
814 BT_ERR("Failed to call systemact service");
816 BT_DBG("Terminate bt-core process");
817 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
822 char *arg_list[3] = { NULL, NULL, NULL };
824 if (g_strcmp0(type, "Enable_RF_Test") == 0) {
825 cmd = "/usr/etc/bluetooth/bt-edutm-on.sh";
826 arg_list[0] = "bt-edutm-on.sh";
827 } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
828 cmd = "/usr/etc/bluetooth/bt-edutm-off.sh";
829 arg_list[0] = "bt-edutm-off.sh";
830 } else if (g_strcmp0(type, "Slave_Mode") == 0) {
831 cmd = "/usr/etc/bluetooth/bt-mode-slave.sh";
832 arg_list[0] = "bt-mode-slave.sh";
833 } else if (g_strcmp0(type, "Master_Mode") == 0) {
834 cmd = "/usr/etc/bluetooth/bt-mode-master.sh";
835 arg_list[0] = "bt-mode-master.sh";
836 } else if (g_strcmp0(type, "SSP_Debug_Mode") == 0) {
837 cmd = "/usr/etc/bluetooth/bt-set-ssp-debug-mode.sh";
838 arg_list[0] = "bt-set-ssp-debug-mode.sh";
839 arg_list[1] = (char *)arg;
840 } else if (g_strcmp0(type, "RF_Channel") == 0) {
841 cmd = "/usr/etc/bluetooth/bt-enable-rf-channel.sh";
842 arg_list[0] = "bt-enable-rf-channel.sh";
843 arg_list[1] = (char *)arg;
848 BT_DBG("Run %s", cmd);
849 if (__execute_command(cmd, arg_list) < 0)
850 BT_ERR("running script failed");
856 static gboolean __bt_core_recovery_cb(gpointer data)
859 gboolean is_request_failed = FALSE;
860 static gboolean is_first_failure = TRUE;
864 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
865 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
867 is_request_failed = TRUE;
870 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
871 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
873 is_request_failed = TRUE;
876 if (is_request_failed == TRUE) {
877 BT_ERR("Recovery is failed.");
878 if (is_first_failure == TRUE) {
879 g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
880 is_first_failure = FALSE;
883 is_first_failure = TRUE;
887 is_first_failure = TRUE;
889 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
890 _bt_core_set_bt_status(BT_RECOVERY_MODE, 0);
891 ret = _bt_enable_adapter();
893 BT_ERR("_bt_enable_adapter() failed");
897 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
898 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 0);
899 ret = _bt_enable_adapter_le();
901 BT_ERR("_bt_enable_adapter_le() failed");
904 is_recovery_mode = FALSE;
911 static gboolean __bt_core_enable_timeout_cb(gpointer data)
913 bt_status_t adapter_status;
914 bt_le_status_t adapter_status_le;
918 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
919 BT_ERR("Set vconf failed");
921 adapter_status = _bt_core_get_status();
922 adapter_status_le = _bt_core_get_le_status();
924 if (adapter_status == BT_DEACTIVATED &&
925 _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
926 _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
927 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
928 _bt_enable_adapter();
931 if (adapter_status_le == BT_LE_DEACTIVATED &&
932 _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
933 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
934 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
935 _bt_enable_adapter_le();
941 static gboolean __bt_core_disable_timeout_cb(gpointer data)
943 bt_status_t adapter_status;
944 bt_le_status_t adapter_status_le;
948 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
949 BT_ERR("Set vconf failed");
951 adapter_status = _bt_core_get_status();
952 adapter_status_le = _bt_core_get_le_status();
954 if (adapter_status == BT_ACTIVATED) {
955 int bt_status_before_mode = 0;
957 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
958 _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
960 #ifdef TIZEN_FEATURE_BUSACT
961 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
963 _bt_disable_adapter();
966 if (adapter_status_le == BT_LE_ACTIVATED) {
967 int bt_le_status_before_mode = 0;
969 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
970 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
972 #ifdef TIZEN_FEATURE_BUSACT
973 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
975 _bt_disable_adapter_le();
981 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value)
988 bundle_add_str(b, key, value);
990 ret = eventsystem_send_system_event(event, b);
992 BT_DBG("eventsystem_send_system_event result: %d", ret);
999 void _bt_core_adapter_added_cb(void)
1002 bt_le_status_t le_status;
1003 gboolean flight_mode_status;
1007 status = _bt_core_get_status();
1008 BT_DBG("status : %d", status);
1009 le_status = _bt_core_get_le_status();
1010 BT_DBG("le_status : %d", le_status);
1012 if (status == BT_ACTIVATING)
1013 __bt_core_set_status(BT_ACTIVATED);
1014 if (le_status == BT_LE_ACTIVATING)
1015 __bt_core_set_le_status(BT_LE_ACTIVATED);
1017 flight_mode_status = _bt_core_is_flight_mode_enabled();
1019 if (flight_mode_status == TRUE && _bt_is_flightmode_request() == TRUE) {
1020 _bt_set_flightmode_request(FALSE);
1021 g_timeout_add(2000, (GSourceFunc)__bt_core_disable_timeout_cb, NULL);
1024 _bt_set_flightmode_request(FALSE);
1026 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1027 EVT_VAL_BT_ON) != ES_R_OK)
1028 BT_ERR("Fail to set BT state value");
1030 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
1031 EVT_VAL_BT_LE_ON) != ES_R_OK)
1032 BT_ERR("Fail to set BT LE state value");
1034 _bt_core_terminate();
1037 void _bt_core_adapter_removed_cb(void)
1039 int flight_mode_value = 0;
1040 int power_saving_mode = 0;
1041 gboolean flight_mode_status;
1042 static int timer_id = -1;
1044 BT_DBG("is_recovery_mode: %d", is_recovery_mode);
1046 __bt_core_set_status(BT_DEACTIVATED);
1047 __bt_core_set_le_status(BT_LE_DEACTIVATED);
1048 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1049 BT_ERR("Set vconf failed");
1050 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
1051 BT_ERR("Set vconf failed");
1053 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1054 EVT_VAL_BT_OFF) != ES_R_OK)
1055 BT_ERR("Fail to set value");
1057 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
1058 EVT_VAL_BT_LE_OFF) != ES_R_OK)
1059 BT_ERR("Fail to set value");
1061 if (is_recovery_mode == TRUE) {
1063 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
1067 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value) != 0)
1068 BT_ERR("Fail to get the flight_mode_deactivated value");
1070 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &power_saving_mode) != 0)
1071 BT_ERR("Fail to get the ps_mode_deactivated value");
1073 flight_mode_status = _bt_core_is_flight_mode_enabled();
1075 if (flight_mode_status == FALSE && _bt_is_flightmode_request() == TRUE) {
1076 _bt_set_flightmode_request(FALSE);
1078 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_enable_timeout_cb, NULL);
1081 _bt_set_flightmode_request(FALSE);
1083 if (flight_mode_value == 1 || power_saving_mode == 1) {
1084 BT_DBG("Bt Core not terminated");
1088 _bt_core_terminate();