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 #define BT_CORE_IDLE_TERM_TIME 200 /* 200ms */
35 #define BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX 50
37 #ifdef TIZEN_FEATURE_BT_OTP
38 #define BT_OTP_OBJECT_PATH "/org/projectx/otp"
39 #define BT_OTP_INTERFACE_NAME "org.projectx.otp_service"
40 #define BLE_DISABLED "LeDisabled"
43 static bt_status_t adapter_status = BT_DEACTIVATED;
44 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
45 static gboolean is_recovery_mode = FALSE;
46 static guint core_enable_timer_id;
49 static int bt_status_before[BT_MODE_MAX] = { VCONFKEY_BT_STATUS_OFF, };
50 static int bt_le_status_before[BT_MODE_MAX] = { VCONFKEY_BT_LE_STATUS_OFF, };
52 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value);
54 static void __bt_core_set_status(bt_status_t status)
56 adapter_status = status;
59 bt_status_t _bt_core_get_status(void)
61 return adapter_status;
64 static void __bt_core_set_le_status(bt_le_status_t status)
66 adapter_le_status = status;
69 bt_le_status_t _bt_core_get_le_status(void)
71 return adapter_le_status;
74 int _bt_core_get_bt_status(bt_mode_e mode)
76 return bt_status_before[mode];
79 int _bt_core_get_bt_le_status(bt_mode_e mode)
81 return bt_le_status_before[mode];
84 void _bt_core_set_bt_status(bt_mode_e mode, int status)
86 bt_status_before[mode] = status;
89 void _bt_core_set_bt_le_status(bt_mode_e mode, int status)
91 bt_le_status_before[mode] = status;
94 gboolean _bt_core_is_recovery_mode(void)
96 return is_recovery_mode;
99 static gboolean __bt_core_idle_terminate(gpointer data)
102 _bt_core_terminate();
106 gboolean _bt_core_is_flight_mode_enabled(void)
108 int isFlightMode = 0;
111 if (TIZEN_FEATURE_FLIGHTMODE_ENABLED) {
112 ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &isFlightMode);
114 BT_ERR("vconf_get_bool failed");
122 #ifdef TIZEN_FEATURE_ACTD
123 static int __bt_call_systemact_service(const char *service_name)
127 BT_DBG("Use System Activated : %s", service_name);
129 ret = actd_start_unit(UNIT_CONTROL_BUS_TYPE_SYSTEM, service_name, 5000);
131 if (ret != UNIT_CONTROL_OK) {
132 BT_ERR("Failed to activate the %s: %d", service_name, ret);
133 /* Return Success even if the activating result is fail */
140 static int __bt_call_systemact_service(const char *file_path)
146 BT_ERR("service file path is NULL");
150 if (!access(file_path, F_OK)) {
155 fp = fopen(file_path, "w");
157 BT_ERR("Failed to fopen service file");
169 static int __execute_command(const char *cmd, char *const arg_list[])
179 BT_ERR("fork failed");
185 BT_ERR("fork failed");
186 } else if (pid2 == 0) {
187 if (arg_list != NULL) {
188 execv(cmd, arg_list);
190 char *argv[] = { NULL };
199 BT_DBG("parent : forked[%d]", pid);
200 waitpid(pid, &status, 0);
201 BT_DBG("child is terminated : %d", status);
208 static int __bt_stack_up(void)
212 /* activate HCI logger */
213 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_LOGGER_START);
215 BT_ERR("Failed to call systemact service");
220 ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUEZ_START);
222 BT_ERR("Failed to call systemact service");
226 /* activate bluetooth-share */
227 ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUETOOTH_SHARE_START);
229 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) {
270 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_UP);
272 BT_ERR("Failed to call systemact service");
274 ret = __bt_stack_up();
277 BT_ERR("running script failed");
278 if (TIZEN_FEATURE_BT_USB_DONGLE) {
279 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
281 BT_ERR("Failed to call systemact service");
283 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
285 __bt_core_set_status(BT_DEACTIVATED);
292 int _bt_disable_adapter(void)
296 bt_le_status_t le_status;
299 BT_INFO_C("Disable adapter");
301 #if 0 /* only the concept of private */
302 le_status = _bt_core_get_le_status();
303 BT_DBG("le_status : %d", le_status);
304 if (le_status == BT_LE_ACTIVATED) {
305 /* Turn off PSCAN, (ISCAN if needed) */
306 /* Return with 0 for the Disabled response. */
307 __bt_core_set_status(BT_DEACTIVATED);
308 BT_INFO("BR/EDR is disabled. now LE only mode");
309 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
314 status = _bt_core_get_status();
315 if (status == BT_ACTIVATING) {
316 /* Forcely terminate */
317 if (TIZEN_FEATURE_BT_USB_DONGLE) {
318 if (__bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN) < 0)
319 BT_ERR("Failed to call systemact service");
321 #ifdef TIZEN_FEATURE_RADIO
322 int radio_status = VCONFKEY_RADIO_STATUS_OFF;
324 /* Check if radio status on or off */
325 if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
326 BT_ERR("Fail to get radio status");
328 BT_DBG("Radio status: %d", radio_status);
330 if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
331 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN_WITH_RADIO) < 0)
332 BT_ERR("running script failed");
334 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0)
335 BT_ERR("running script failed");
338 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0)
339 BT_ERR("running script failed");
342 _bt_core_terminate();
344 } else if (status != BT_ACTIVATED) {
345 BT_ERR("Invalid state %d", status);
346 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
349 __bt_core_set_status(BT_DEACTIVATING);
350 if (TIZEN_FEATURE_BT_USB_DONGLE) {
351 if (__bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN) < 0) {
352 BT_ERR("Failed to call systemact service");
353 __bt_core_set_status(BT_ACTIVATED);
357 #ifdef TIZEN_FEATURE_RADIO
358 int radio_status = VCONFKEY_RADIO_STATUS_OFF;
360 /* Check if radio status on or off */
361 if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
362 BT_ERR("Fail to get radio status");
364 BT_DBG("Radio status: %d", radio_status);
366 if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
367 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN_WITH_RADIO) < 0) {
368 BT_ERR("running script failed");
369 __bt_core_set_status(BT_ACTIVATED);
373 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0) {
374 BT_ERR("running script failed");
375 __bt_core_set_status(BT_ACTIVATED);
380 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0) {
381 BT_ERR("running script failed");
382 __bt_core_set_status(BT_ACTIVATED);
391 int _bt_enable_adapter_le(void)
397 bt_le_status_t le_status;
398 le_status = _bt_core_get_le_status();
399 retv_if(le_status == BT_LE_ACTIVATED, 0);
400 retv_if(le_status != BT_LE_DEACTIVATED, -1);
402 status = _bt_core_get_status();
403 if (status == BT_DEACTIVATED) {
404 __bt_core_set_le_status(BT_LE_ACTIVATING);
405 BT_DBG("Activate BT");
406 if (TIZEN_FEATURE_BT_USB_DONGLE) {
407 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_UP);
409 BT_ERR("Failed to call systemact service");
411 ret = __bt_stack_up();
414 BT_ERR("running script failed");
415 if (TIZEN_FEATURE_BT_USB_DONGLE) {
416 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
418 BT_ERR("Failed to call systemact service");
420 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh &", NULL);
422 __bt_core_set_status(BT_DEACTIVATED);
423 __bt_core_set_le_status(BT_LE_DEACTIVATED);
427 __bt_core_set_le_status(BT_LE_ACTIVATED);
428 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
430 #ifdef TIZEN_FEATURE_BT_HPS
431 ret = _bt_core_start_httpproxy();
433 BT_ERR("_bt_core_start_httpproxy() failed");
439 #ifdef TIZEN_FEATURE_BT_OTP
440 static void _bt_core_stop_otp()
442 GError *error = NULL;
443 GDBusMessage *msg = NULL;
444 GDBusConnection *conn = _bt_core_get_gdbus_connection();
445 msg = g_dbus_message_new_signal(BT_OTP_OBJECT_PATH, BT_OTP_INTERFACE_NAME, BLE_DISABLED);
446 if (!g_dbus_connection_send_message(conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, 0, NULL)) {
448 BT_ERR("Failed to send BLE_DISABLED signal to OTP : errCode[%x], \
450 error->code, error->message);
451 g_clear_error(&error);
457 int _bt_disable_adapter_le(void)
462 bt_le_status_t le_status;
464 le_status = _bt_core_get_le_status();
465 retv_if(le_status == BT_LE_DEACTIVATED, 0);
466 retv_if(le_status == BT_LE_DEACTIVATING, -1);
468 #ifdef TIZEN_FEATURE_BT_HPS
469 _bt_core_stop_httpproxy();
472 #ifdef TIZEN_FEATURE_BT_OTP
476 status = _bt_core_get_status();
477 BT_DBG("status : %d", status);
479 if (status == BT_DEACTIVATED) {
480 __bt_core_set_le_status(BT_LE_DEACTIVATING);
482 if (TIZEN_FEATURE_BT_USB_DONGLE) {
483 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
485 BT_ERR("Failed to call systemact service");
487 ret = __execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL);
490 BT_ERR("running script failed");
491 __bt_core_set_le_status(BT_LE_ACTIVATED);
495 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
498 __bt_core_set_le_status(BT_LE_DEACTIVATED);
504 int _bt_core_service_request_adapter(int service_function)
508 GArray *in_param1 = NULL;
509 GArray *in_param2 = NULL;
510 GArray *in_param3 = NULL;
511 GArray *in_param4 = NULL;
512 GArray *out_param = NULL;
514 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
516 ret = _bt_core_service_request(BT_CORE_SERVICE, service_function,
517 in_param1, in_param2, in_param3, in_param4, &out_param);
519 BT_ERR("_bt_core_service_request_adapter() failed");
521 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
526 static gboolean __bt_core_check_the_adapter_path(void)
529 GDBusProxy *manager_proxy = NULL;
530 GVariant *result = NULL;
531 char *adapter_path = NULL;
532 GDBusConnection *conn = NULL;
534 conn = _bt_core_get_gdbus_connection();
538 manager_proxy = g_dbus_proxy_new_sync(conn,
539 G_DBUS_PROXY_FLAGS_NONE, NULL,
542 "org.freedesktop.DBus.ObjectManager",
545 if (!manager_proxy) {
547 BT_ERR("Unable to create proxy: %s", err->message);
550 BT_ERR("Fail to create proxy");
555 result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
556 G_DBUS_CALL_FLAGS_NONE, 1000, NULL, &err);
559 BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
562 BT_ERR("Fail to get DefaultAdapter");
567 if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
568 BT_ERR("Incorrect result\n");
572 g_variant_get(result, "(&o)", &adapter_path);
574 if (adapter_path == NULL ||
575 strlen(adapter_path) >= BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX) {
576 BT_ERR("Adapter path is inproper\n");
580 g_variant_unref(result);
581 g_object_unref(manager_proxy);
587 g_variant_unref(result);
590 g_object_unref(manager_proxy);
595 void _bt_core_update_status(void)
597 int bt_status = VCONFKEY_BT_STATUS_OFF;
598 int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
599 gboolean ret = FALSE;
601 ret = __bt_core_check_the_adapter_path();
602 BT_INFO("check the real status of bt_adapter");
605 __bt_core_set_status(BT_DEACTIVATED);
606 __bt_core_set_le_status(BT_DEACTIVATED);
608 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
609 BT_ERR("no bluetooth device info, so BT was disabled at previous session");
610 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
611 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
613 BT_INFO("bt_status = %d, bt_le_status = %d", bt_status, bt_le_status);
615 if (bt_status & VCONFKEY_BT_STATUS_ON)
616 __bt_core_set_status(BT_ACTIVATED);
617 if (bt_le_status & VCONFKEY_BT_LE_STATUS_ON)
618 __bt_core_set_le_status(BT_ACTIVATED);
622 gboolean _bt_core_enable_adapter(void)
626 _bt_set_flightmode_request(FALSE);
627 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
628 BT_ERR("Set vconf failed");
630 ret = _bt_enable_adapter();
637 static gboolean __bt_core_terminate_cb(gpointer data)
639 _bt_core_terminate();
644 gboolean _bt_core_disable_adapter(void)
647 gboolean adapter_state;
649 _bt_set_flightmode_request(FALSE);
650 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
651 BT_ERR("Set vconf failed");
653 adapter_state = __bt_core_check_the_adapter_path();
654 if (adapter_state == FALSE)
655 BT_INFO("Default adapter is NOT normal.");
657 ret = _bt_disable_adapter();
658 if (adapter_state == FALSE) {
659 g_timeout_add(BT_CORE_IDLE_TERM_TIME,
660 __bt_core_terminate_cb, NULL);
670 gboolean _bt_core_recover_adapter(void)
676 BT_INFO_C("Recover bt adapter");
678 _bt_set_flightmode_request(FALSE);
679 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
680 BT_ERR("Set vconf failed");
682 is_recovery_mode = TRUE;
684 _bt_core_update_status();
686 if (_bt_core_get_status() == BT_ACTIVATED) {
687 _bt_core_set_bt_status(BT_RECOVERY_MODE, 1);
688 #ifdef TIZEN_FEATURE_BUSACT
689 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
692 if (_bt_core_get_le_status() == BT_LE_ACTIVATED) {
693 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 1);
694 #ifdef TIZEN_FEATURE_BUSACT
695 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
699 ret = _bt_disable_adapter();
701 BT_ERR("_bt_disable_adapter() failed");
703 /* In platform, don't seperate BR/EDR and LE status */
705 ret_le = _bt_disable_adapter_le();
707 BT_ERR("_bt_disable_adapter_le() failed");
712 gboolean _bt_core_enable_adapter_le(void)
716 ret = _bt_enable_adapter_le();
723 gboolean _bt_core_disable_adapter_le(void)
729 ret = _bt_disable_adapter_le();
736 gboolean __bt_core_reset_adapter(void)
738 /* Forcely terminate */
739 if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0)
740 BT_ERR("running script failed");
742 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
747 static gboolean __bt_core_enable_core_timeout_cb(gpointer data)
751 core_enable_timer_id = 0;
753 _bt_core_init_vconf_value();
758 gboolean _bt_core_enable_core(void)
762 _bt_core_update_status();
764 if (core_enable_timer_id > 0)
765 g_source_remove(core_enable_timer_id);
767 core_enable_timer_id = g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
773 gboolean _bt_core_set_transfer_value(gboolean value)
775 const char *event_val = NULL;
778 BT_DBG("value: %d", value);
780 event_val = value ? EVT_VAL_BT_TRANSFERING : EVT_VAL_BT_NON_TRANSFERING;
782 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_TRANSFERING_STATE,
783 event_val) != ES_R_OK) {
784 BT_ERR("Fail to set BT state value");
788 if (ret == FALSE || value == FALSE) {
789 BT_DBG("Terminate bt-core process");
790 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
797 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
799 BT_DBG("Test item : %s", type);
801 #ifdef TIZEN_FEATURE_ACTD
802 if (g_strcmp0(type, "Enable_RF_Test") == 0) {
803 __bt_call_systemact_service(BT_SYSTEMACT_EDUTM_ON);
804 } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
805 __bt_call_systemact_service(BT_SYSTEMACT_EDUTM_OFF);
807 BT_DBG("Terminate bt-core process");
808 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
813 char *arg_list[3] = { NULL, NULL, NULL };
815 if (g_strcmp0(type, "Enable_RF_Test") == 0) {
816 cmd = "/usr/etc/bluetooth/bt-edutm-on.sh";
817 arg_list[0] = "bt-edutm-on.sh";
818 } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
819 cmd = "/usr/etc/bluetooth/bt-edutm-off.sh";
820 arg_list[0] = "bt-edutm-off.sh";
821 } else if (g_strcmp0(type, "Slave_Mode") == 0) {
822 cmd = "/usr/etc/bluetooth/bt-mode-slave.sh";
823 arg_list[0] = "bt-mode-slave.sh";
824 } else if (g_strcmp0(type, "Master_Mode") == 0) {
825 cmd = "/usr/etc/bluetooth/bt-mode-master.sh";
826 arg_list[0] = "bt-mode-master.sh";
827 } else if (g_strcmp0(type, "SSP_Debug_Mode") == 0) {
828 cmd = "/usr/etc/bluetooth/bt-set-ssp-debug-mode.sh";
829 arg_list[0] = "bt-set-ssp-debug-mode.sh";
830 arg_list[1] = (char *)arg;
831 } else if (g_strcmp0(type, "RF_Channel") == 0) {
832 cmd = "/usr/etc/bluetooth/bt-enable-rf-channel.sh";
833 arg_list[0] = "bt-enable-rf-channel.sh";
834 arg_list[1] = (char *)arg;
839 BT_DBG("Run %s", cmd);
840 if (__execute_command(cmd, arg_list) < 0)
841 BT_ERR("running script failed");
847 static gboolean __bt_core_recovery_cb(gpointer data)
850 gboolean is_request_failed = FALSE;
851 static gboolean is_first_failure = TRUE;
855 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
856 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
858 is_request_failed = TRUE;
861 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
862 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
864 is_request_failed = TRUE;
867 if (is_request_failed == TRUE) {
868 BT_ERR("Recovery is failed.");
869 if (is_first_failure == TRUE) {
870 g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
871 is_first_failure = FALSE;
874 is_first_failure = TRUE;
878 is_first_failure = TRUE;
880 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
881 _bt_core_set_bt_status(BT_RECOVERY_MODE, 0);
882 ret = _bt_enable_adapter();
884 BT_ERR("_bt_enable_adapter() failed");
888 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
889 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 0);
890 ret = _bt_enable_adapter_le();
892 BT_ERR("_bt_enable_adapter_le() failed");
895 is_recovery_mode = FALSE;
902 static gboolean __bt_core_enable_timeout_cb(gpointer data)
904 bt_status_t adapter_status;
905 bt_le_status_t adapter_status_le;
909 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
910 BT_ERR("Set vconf failed");
912 adapter_status = _bt_core_get_status();
913 adapter_status_le = _bt_core_get_le_status();
915 if (adapter_status == BT_DEACTIVATED &&
916 _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
917 _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
918 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
919 _bt_enable_adapter();
922 if (adapter_status_le == BT_LE_DEACTIVATED &&
923 _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
924 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
925 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
926 _bt_enable_adapter_le();
932 static gboolean __bt_core_disable_timeout_cb(gpointer data)
934 bt_status_t adapter_status;
935 bt_le_status_t adapter_status_le;
939 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
940 BT_ERR("Set vconf failed");
942 adapter_status = _bt_core_get_status();
943 adapter_status_le = _bt_core_get_le_status();
945 if (adapter_status == BT_ACTIVATED) {
946 int bt_status_before_mode = 0;
948 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
949 _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
951 #ifdef TIZEN_FEATURE_BUSACT
952 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
954 _bt_disable_adapter();
957 if (adapter_status_le == BT_LE_ACTIVATED) {
958 int bt_le_status_before_mode = 0;
960 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
961 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
963 #ifdef TIZEN_FEATURE_BUSACT
964 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
966 _bt_disable_adapter_le();
972 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value)
979 bundle_add_str(b, key, value);
981 ret = eventsystem_send_system_event(event, b);
983 BT_DBG("eventsystem_send_system_event result: %d", ret);
990 void _bt_core_adapter_added_cb(void)
993 bt_le_status_t le_status;
994 gboolean flight_mode_status;
998 status = _bt_core_get_status();
999 BT_DBG("status : %d", status);
1000 le_status = _bt_core_get_le_status();
1001 BT_DBG("le_status : %d", le_status);
1003 if (status == BT_ACTIVATING)
1004 __bt_core_set_status(BT_ACTIVATED);
1005 if (le_status == BT_LE_ACTIVATING)
1006 __bt_core_set_le_status(BT_LE_ACTIVATED);
1008 flight_mode_status = _bt_core_is_flight_mode_enabled();
1010 if (flight_mode_status == TRUE && _bt_is_flightmode_request() == TRUE) {
1011 _bt_set_flightmode_request(FALSE);
1012 g_timeout_add(2000, (GSourceFunc)__bt_core_disable_timeout_cb, NULL);
1015 _bt_set_flightmode_request(FALSE);
1017 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1018 EVT_VAL_BT_ON) != ES_R_OK)
1019 BT_ERR("Fail to set BT state value");
1021 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
1022 EVT_VAL_BT_LE_ON) != ES_R_OK)
1023 BT_ERR("Fail to set BT LE state value");
1025 _bt_core_terminate();
1028 void _bt_core_adapter_removed_cb(void)
1030 int flight_mode_value = 0;
1031 int power_saving_mode = 0;
1032 gboolean flight_mode_status;
1033 static int timer_id = -1;
1035 BT_DBG("is_recovery_mode: %d", is_recovery_mode);
1037 __bt_core_set_status(BT_DEACTIVATED);
1038 __bt_core_set_le_status(BT_LE_DEACTIVATED);
1039 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1040 BT_ERR("Set vconf failed");
1041 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
1042 BT_ERR("Set vconf failed");
1044 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1045 EVT_VAL_BT_OFF) != ES_R_OK)
1046 BT_ERR("Fail to set value");
1048 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
1049 EVT_VAL_BT_LE_OFF) != ES_R_OK)
1050 BT_ERR("Fail to set value");
1052 if (is_recovery_mode == TRUE) {
1054 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
1058 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value) != 0)
1059 BT_ERR("Fail to get the flight_mode_deactivated value");
1061 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &power_saving_mode) != 0)
1062 BT_ERR("Fail to get the ps_mode_deactivated value");
1064 flight_mode_status = _bt_core_is_flight_mode_enabled();
1066 if (flight_mode_status == FALSE && _bt_is_flightmode_request() == TRUE) {
1067 _bt_set_flightmode_request(FALSE);
1069 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_enable_timeout_cb, NULL);
1072 _bt_set_flightmode_request(FALSE);
1074 if (flight_mode_value == 1 || power_saving_mode == 1) {
1075 BT_DBG("Bt Core not terminated");
1079 _bt_core_terminate();