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 ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
194 BT_ERR("Failed to run script");
195 __bt_call_systemact_service(BT_SYSTEMACT_HCI_LOGGER_STOP);
200 ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUEZ_START);
202 BT_ERR("Failed to call systemact service");
206 /* activate bluetooth-share */
207 ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUETOOTH_SHARE_START);
209 BT_ERR("Failed to call systemact service");
220 int _bt_enable_adapter(void)
225 bt_le_status_t le_status;
229 status = _bt_core_get_status();
230 if (status != BT_DEACTIVATED) {
231 BT_ERR("Invalid state %d", status);
232 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
236 #if 0 /* only the concept of private */
237 le_status = _bt_core_get_le_status();
238 if (le_status == BT_LE_ACTIVATED) {
239 /* Turn on PSCAN, (ISCAN if needed) */
240 /* Return with 0 for the Enabled response. */
241 __bt_core_set_status(BT_ACTIVATED);
242 BT_INFO("BR/EDR is enabled.");
243 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
248 __bt_core_set_status(BT_ACTIVATING);
249 if (TIZEN_FEATURE_BT_USB_DONGLE) {
250 char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
251 ret = __execute_command("/usr/bin/hciconfig", argv_up);
253 ret = __bt_stack_up();
256 BT_ERR("running script failed");
257 if (TIZEN_FEATURE_BT_USB_DONGLE) {
258 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
259 ret = __execute_command("/usr/bin/hciconfig", argv_down);
261 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
263 __bt_core_set_status(BT_DEACTIVATED);
270 int _bt_disable_adapter(void)
274 bt_le_status_t le_status;
277 BT_INFO_C("Disable adapter");
279 #if 0 /* only the concept of private */
280 le_status = _bt_core_get_le_status();
281 BT_DBG("le_status : %d", le_status);
282 if (le_status == BT_LE_ACTIVATED) {
283 /* Turn off PSCAN, (ISCAN if needed) */
284 /* Return with 0 for the Disabled response. */
285 __bt_core_set_status(BT_DEACTIVATED);
286 BT_INFO("BR/EDR is disabled. now LE only mode");
287 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
292 status = _bt_core_get_status();
293 if (status == BT_ACTIVATING) {
294 /* Forcely terminate */
295 if (TIZEN_FEATURE_BT_USB_DONGLE) {
296 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
297 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0)
298 BT_ERR("running script failed");
301 #ifdef TIZEN_FEATURE_RADIO
302 int radio_status = VCONFKEY_RADIO_STATUS_OFF;
304 /* Check if radio status on or off */
305 if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
306 BT_ERR("Fail to get radio status");
308 BT_DBG("Radio status: %d", radio_status);
310 if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
311 if (__execute_command("/usr/etc/bluetooth/bt-stack-down-with-radio.sh", NULL) < 0)
312 BT_ERR("running script failed");
314 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0)
315 BT_ERR("running script failed");
318 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0)
319 BT_ERR("running script failed");
322 _bt_core_terminate();
324 } else if (status != BT_ACTIVATED) {
325 BT_ERR("Invalid state %d", status);
326 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
329 __bt_core_set_status(BT_DEACTIVATING);
330 if (TIZEN_FEATURE_BT_USB_DONGLE) {
331 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
332 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
333 BT_ERR("running script failed");
334 __bt_core_set_status(BT_ACTIVATED);
338 #ifdef TIZEN_FEATURE_RADIO
339 int radio_status = VCONFKEY_RADIO_STATUS_OFF;
341 /* Check if radio status on or off */
342 if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
343 BT_ERR("Fail to get radio status");
345 BT_DBG("Radio status: %d", radio_status);
347 if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
348 if (__execute_command("/usr/etc/bluetooth/bt-stack-down-with-radio.sh", NULL) < 0) {
349 BT_ERR("running script failed");
350 __bt_core_set_status(BT_ACTIVATED);
354 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
355 BT_ERR("running script failed");
356 __bt_core_set_status(BT_ACTIVATED);
361 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
362 BT_ERR("running script failed");
363 __bt_core_set_status(BT_ACTIVATED);
372 int _bt_enable_adapter_le(void)
378 bt_le_status_t le_status;
379 le_status = _bt_core_get_le_status();
380 retv_if(le_status != BT_LE_DEACTIVATED, -1);
382 status = _bt_core_get_status();
383 if (status == BT_DEACTIVATED) {
384 __bt_core_set_le_status(BT_LE_ACTIVATING);
385 BT_DBG("Activate BT");
386 if (TIZEN_FEATURE_BT_USB_DONGLE) {
387 char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
388 ret = __execute_command("/usr/bin/hciconfig", argv_up);
390 ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
393 BT_ERR("running script failed");
394 if (TIZEN_FEATURE_BT_USB_DONGLE) {
395 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
396 ret = __execute_command("/usr/bin/hciconfig", argv_down);
398 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh &", NULL);
400 __bt_core_set_status(BT_DEACTIVATED);
401 __bt_core_set_le_status(BT_LE_DEACTIVATED);
405 __bt_core_set_le_status(BT_LE_ACTIVATED);
406 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
408 #ifdef TIZEN_FEATURE_BT_HPS
409 ret = _bt_core_start_httpproxy();
411 BT_ERR("_bt_core_start_httpproxy() failed");
417 #ifdef TIZEN_FEATURE_BT_OTP
418 static void _bt_core_stop_otp()
420 GError *error = NULL;
421 GDBusMessage *msg = NULL;
422 GDBusConnection *conn = _bt_core_get_gdbus_connection();
423 msg = g_dbus_message_new_signal(BT_OTP_OBJECT_PATH, BT_OTP_INTERFACE_NAME, BLE_DISABLED);
424 if (!g_dbus_connection_send_message(conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, 0, NULL)) {
426 BT_ERR("Failed to send BLE_DISABLED signal to OTP : errCode[%x], \
428 error->code, error->message);
429 g_clear_error(&error);
435 int _bt_disable_adapter_le(void)
440 bt_le_status_t le_status;
442 le_status = _bt_core_get_le_status();
443 retv_if(le_status == BT_LE_DEACTIVATED, 0);
444 retv_if(le_status == BT_LE_DEACTIVATING, -1);
446 #ifdef TIZEN_FEATURE_BT_HPS
447 _bt_core_stop_httpproxy();
450 #ifdef TIZEN_FEATURE_BT_OTP
454 status = _bt_core_get_status();
455 BT_DBG("status : %d", status);
457 if (status == BT_DEACTIVATED) {
458 __bt_core_set_le_status(BT_LE_DEACTIVATING);
460 if (TIZEN_FEATURE_BT_USB_DONGLE) {
461 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
462 ret = __execute_command("/usr/bin/hciconfig", argv_down);
464 ret = __execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL);
467 BT_ERR("running script failed");
468 __bt_core_set_le_status(BT_LE_ACTIVATED);
472 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
475 __bt_core_set_le_status(BT_LE_DEACTIVATED);
481 int _bt_core_service_request_adapter(int service_function)
485 GArray *in_param1 = NULL;
486 GArray *in_param2 = NULL;
487 GArray *in_param3 = NULL;
488 GArray *in_param4 = NULL;
489 GArray *out_param = NULL;
491 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
493 ret = _bt_core_service_request(BT_CORE_SERVICE, service_function,
494 in_param1, in_param2, in_param3, in_param4, &out_param);
496 BT_ERR("_bt_core_service_request_adapter() failed");
498 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
503 static gboolean __bt_core_check_the_adapter_path(void)
506 GDBusProxy *manager_proxy = NULL;
507 GVariant *result = NULL;
508 char *adapter_path = NULL;
509 GDBusConnection *conn = NULL;
511 conn = _bt_core_get_gdbus_connection();
515 manager_proxy = g_dbus_proxy_new_sync(conn,
516 G_DBUS_PROXY_FLAGS_NONE, NULL,
519 "org.freedesktop.DBus.ObjectManager",
522 if (!manager_proxy) {
524 BT_ERR("Unable to create proxy: %s", err->message);
527 BT_ERR("Fail to create proxy");
532 result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
533 G_DBUS_CALL_FLAGS_NONE, 1000, NULL, &err);
536 BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
539 BT_ERR("Fail to get DefaultAdapter");
544 if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
545 BT_ERR("Incorrect result\n");
549 g_variant_get(result, "(&o)", &adapter_path);
551 if (adapter_path == NULL ||
552 strlen(adapter_path) >= BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX) {
553 BT_ERR("Adapter path is inproper\n");
557 g_variant_unref(result);
558 g_object_unref(manager_proxy);
564 g_variant_unref(result);
567 g_object_unref(manager_proxy);
572 void _bt_core_update_status(void)
574 int bt_status = VCONFKEY_BT_STATUS_OFF;
575 int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
576 gboolean ret = FALSE;
578 ret = __bt_core_check_the_adapter_path();
579 BT_INFO("check the real status of bt_adapter");
582 __bt_core_set_status(BT_DEACTIVATED);
583 __bt_core_set_le_status(BT_DEACTIVATED);
585 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
586 BT_ERR("no bluetooth device info, so BT was disabled at previous session");
587 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
588 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
590 BT_INFO("bt_status = %d, bt_le_status = %d", bt_status, bt_le_status);
592 if (bt_status & VCONFKEY_BT_STATUS_ON)
593 __bt_core_set_status(BT_ACTIVATED);
594 if (bt_le_status & VCONFKEY_BT_LE_STATUS_ON)
595 __bt_core_set_le_status(BT_ACTIVATED);
599 gboolean _bt_core_enable_adapter(void)
603 _bt_set_flightmode_request(FALSE);
604 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
605 BT_ERR("Set vconf failed");
607 ret = _bt_enable_adapter();
614 static gboolean __bt_core_terminate_cb(gpointer data)
616 _bt_core_terminate();
621 gboolean _bt_core_disable_adapter(void)
624 gboolean adapter_state;
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 adapter_state = __bt_core_check_the_adapter_path();
631 if (adapter_state == FALSE)
632 BT_INFO("Default adapter is NOT normal.");
634 ret = _bt_disable_adapter();
635 if (adapter_state == FALSE) {
636 g_timeout_add(BT_CORE_IDLE_TERM_TIME,
637 __bt_core_terminate_cb, NULL);
647 gboolean _bt_core_recover_adapter(void)
653 BT_INFO_C("Recover bt adapter");
655 _bt_set_flightmode_request(FALSE);
656 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
657 BT_ERR("Set vconf failed");
659 is_recovery_mode = TRUE;
661 _bt_core_update_status();
663 if (_bt_core_get_status() == BT_ACTIVATED) {
664 _bt_core_set_bt_status(BT_RECOVERY_MODE, 1);
665 #ifdef TIZEN_FEATURE_BUSACT
666 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
669 if (_bt_core_get_le_status() == BT_LE_ACTIVATED) {
670 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 1);
671 #ifdef TIZEN_FEATURE_BUSACT
672 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
676 ret = _bt_disable_adapter();
678 BT_ERR("_bt_disable_adapter() failed");
680 /* In platform, don't seperate BR/EDR and LE status */
682 ret_le = _bt_disable_adapter_le();
684 BT_ERR("_bt_disable_adapter_le() failed");
689 gboolean _bt_core_enable_adapter_le(void)
693 ret = _bt_enable_adapter_le();
700 gboolean _bt_core_disable_adapter_le(void)
706 ret = _bt_disable_adapter_le();
713 gboolean __bt_core_reset_adapter(void)
715 /* Forcely terminate */
716 if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0)
717 BT_ERR("running script failed");
719 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
724 static gboolean __bt_core_enable_core_timeout_cb(gpointer data)
728 core_enable_timer_id = 0;
730 _bt_core_init_vconf_value();
735 gboolean _bt_core_enable_core(void)
739 _bt_core_update_status();
741 if (core_enable_timer_id > 0)
742 g_source_remove(core_enable_timer_id);
744 core_enable_timer_id = g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
750 gboolean _bt_core_set_transfer_value(gboolean value)
752 const char *event_val = NULL;
755 BT_DBG("value: %d", value);
757 event_val = value ? EVT_VAL_BT_TRANSFERING : EVT_VAL_BT_NON_TRANSFERING;
759 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_TRANSFERING_STATE,
760 event_val) != ES_R_OK) {
761 BT_ERR("Fail to set BT state value");
765 if (ret == FALSE || value == FALSE) {
766 BT_DBG("Terminate bt-core process");
767 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
774 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
778 char *arg_list[3] = { NULL, NULL, NULL };
780 BT_DBG("Test item : %s", type);
782 if (g_strcmp0(type, "Enable_RF_Test") == 0) {
783 cmd = "/usr/etc/bluetooth/bt-edutm-on.sh";
784 arg_list[0] = "bt-edutm-on.sh";
785 } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
786 cmd = "/usr/etc/bluetooth/bt-edutm-off.sh";
787 arg_list[0] = "bt-edutm-off.sh";
788 } else if (g_strcmp0(type, "Slave_Mode") == 0) {
789 cmd = "/usr/etc/bluetooth/bt-mode-slave.sh";
790 arg_list[0] = "bt-mode-slave.sh";
791 } else if (g_strcmp0(type, "Master_Mode") == 0) {
792 cmd = "/usr/etc/bluetooth/bt-mode-master.sh";
793 arg_list[0] = "bt-mode-master.sh";
794 } else if (g_strcmp0(type, "SSP_Debug_Mode") == 0) {
795 cmd = "/usr/etc/bluetooth/bt-set-ssp-debug-mode.sh";
796 arg_list[0] = "bt-set-ssp-debug-mode.sh";
797 arg_list[1] = (char *)arg;
798 } else if (g_strcmp0(type, "RF_Channel") == 0) {
799 cmd = "/usr/etc/bluetooth/bt-enable-rf-channel.sh";
800 arg_list[0] = "bt-enable-rf-channel.sh";
801 arg_list[1] = (char *)arg;
806 BT_DBG("Run %s", cmd);
807 if (__execute_command(cmd, arg_list) < 0)
808 BT_ERR("running script failed");
813 static gboolean __bt_core_recovery_cb(gpointer data)
816 #ifdef TIZEN_FEATURE_BUSACT
817 gboolean is_request_failed = FALSE;
818 static gboolean is_first_failure = TRUE;
823 #ifdef TIZEN_FEATURE_BUSACT
824 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
825 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
827 is_request_failed = TRUE;
830 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
831 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
833 is_request_failed = TRUE;
836 if (is_request_failed == TRUE) {
837 BT_ERR("Recovery is failed.");
838 if (is_first_failure == TRUE) {
839 g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
840 is_first_failure = FALSE;
843 is_first_failure = TRUE;
847 is_first_failure = TRUE;
850 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
851 _bt_core_set_bt_status(BT_RECOVERY_MODE, 0);
852 ret = _bt_enable_adapter();
854 BT_ERR("_bt_enable_adapter() failed");
858 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
859 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 0);
860 ret = _bt_enable_adapter_le();
862 BT_ERR("_bt_enable_adapter_le() failed");
865 is_recovery_mode = FALSE;
872 static gboolean __bt_core_enable_timeout_cb(gpointer data)
874 bt_status_t adapter_status;
875 bt_le_status_t adapter_status_le;
879 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
880 BT_ERR("Set vconf failed");
882 adapter_status = _bt_core_get_status();
883 adapter_status_le = _bt_core_get_le_status();
885 if (adapter_status == BT_DEACTIVATED &&
886 _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
887 _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
888 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
889 _bt_enable_adapter();
892 if (adapter_status_le == BT_LE_DEACTIVATED &&
893 _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
894 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
895 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
896 _bt_enable_adapter_le();
902 static gboolean __bt_core_disable_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, 1) != 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_ACTIVATED) {
916 int bt_status_before_mode = 0;
918 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
919 _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
921 #ifdef TIZEN_FEATURE_BUSACT
922 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
924 _bt_disable_adapter();
927 if (adapter_status_le == BT_LE_ACTIVATED) {
928 int bt_le_status_before_mode = 0;
930 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
931 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
933 #ifdef TIZEN_FEATURE_BUSACT
934 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
936 _bt_disable_adapter_le();
942 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value)
949 bundle_add_str(b, key, value);
951 ret = eventsystem_send_system_event(event, b);
953 BT_DBG("eventsystem_send_system_event result: %d", ret);
960 void _bt_core_adapter_added_cb(void)
963 bt_le_status_t le_status;
964 gboolean flight_mode_status;
968 status = _bt_core_get_status();
969 BT_DBG("status : %d", status);
970 le_status = _bt_core_get_le_status();
971 BT_DBG("le_status : %d", le_status);
973 if (status == BT_ACTIVATING)
974 __bt_core_set_status(BT_ACTIVATED);
975 if (le_status == BT_LE_ACTIVATING)
976 __bt_core_set_le_status(BT_LE_ACTIVATED);
978 flight_mode_status = _bt_core_is_flight_mode_enabled();
980 if (flight_mode_status == TRUE && _bt_is_flightmode_request() == TRUE) {
981 _bt_set_flightmode_request(FALSE);
982 g_timeout_add(2000, (GSourceFunc)__bt_core_disable_timeout_cb, NULL);
985 _bt_set_flightmode_request(FALSE);
987 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
988 EVT_VAL_BT_ON) != ES_R_OK)
989 BT_ERR("Fail to set BT state value");
991 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
992 EVT_VAL_BT_LE_ON) != ES_R_OK)
993 BT_ERR("Fail to set BT LE state value");
995 _bt_core_terminate();
998 void _bt_core_adapter_removed_cb(void)
1000 int flight_mode_value = 0;
1001 int power_saving_mode = 0;
1002 gboolean flight_mode_status;
1003 static int timer_id = -1;
1005 BT_DBG("is_recovery_mode: %d", is_recovery_mode);
1007 __bt_core_set_status(BT_DEACTIVATED);
1008 __bt_core_set_le_status(BT_LE_DEACTIVATED);
1009 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1010 BT_ERR("Set vconf failed");
1011 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
1012 BT_ERR("Set vconf failed");
1014 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1015 EVT_VAL_BT_OFF) != ES_R_OK)
1016 BT_ERR("Fail to set value");
1018 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
1019 EVT_VAL_BT_LE_OFF) != ES_R_OK)
1020 BT_ERR("Fail to set value");
1022 if (is_recovery_mode == TRUE) {
1024 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
1028 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value) != 0)
1029 BT_ERR("Fail to get the flight_mode_deactivated value");
1031 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &power_saving_mode) != 0)
1032 BT_ERR("Fail to get the ps_mode_deactivated value");
1034 flight_mode_status = _bt_core_is_flight_mode_enabled();
1036 if (flight_mode_status == FALSE && _bt_is_flightmode_request() == TRUE) {
1037 _bt_set_flightmode_request(FALSE);
1039 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_enable_timeout_cb, NULL);
1042 _bt_set_flightmode_request(FALSE);
1044 if (flight_mode_value == 1 || power_saving_mode == 1) {
1045 BT_DBG("Bt Core not terminated");
1049 _bt_core_terminate();