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);
199 /* We need to insert BT driver module by using 'insmod' in case of ARTIK530,
200 * and 'insmod' should be executed by systemd not bt-core. */
201 ret = __bt_call_systemact_service(BT_SYSTEMACT_BT_DRIVER_INSMOD);
203 BT_ERR("Failed to call systemact service");
208 ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUEZ_START);
210 BT_ERR("Failed to call systemact service");
214 /* activate bluetooth-share */
215 ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUETOOTH_SHARE_START);
217 BT_ERR("Failed to call systemact service");
228 int _bt_enable_adapter(void)
233 bt_le_status_t le_status;
237 status = _bt_core_get_status();
238 if (status != BT_DEACTIVATED) {
239 BT_ERR("Invalid state %d", status);
240 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
244 #if 0 /* only the concept of private */
245 le_status = _bt_core_get_le_status();
246 if (le_status == BT_LE_ACTIVATED) {
247 /* Turn on PSCAN, (ISCAN if needed) */
248 /* Return with 0 for the Enabled response. */
249 __bt_core_set_status(BT_ACTIVATED);
250 BT_INFO("BR/EDR is enabled.");
251 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
256 __bt_core_set_status(BT_ACTIVATING);
257 if (TIZEN_FEATURE_BT_USB_DONGLE) {
258 char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
259 ret = __execute_command("/usr/bin/hciconfig", argv_up);
261 ret = __bt_stack_up();
264 BT_ERR("running script failed");
265 if (TIZEN_FEATURE_BT_USB_DONGLE) {
266 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
267 ret = __execute_command("/usr/bin/hciconfig", argv_down);
269 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
271 __bt_core_set_status(BT_DEACTIVATED);
278 int _bt_disable_adapter(void)
282 bt_le_status_t le_status;
285 BT_INFO_C("Disable adapter");
287 #if 0 /* only the concept of private */
288 le_status = _bt_core_get_le_status();
289 BT_DBG("le_status : %d", le_status);
290 if (le_status == BT_LE_ACTIVATED) {
291 /* Turn off PSCAN, (ISCAN if needed) */
292 /* Return with 0 for the Disabled response. */
293 __bt_core_set_status(BT_DEACTIVATED);
294 BT_INFO("BR/EDR is disabled. now LE only mode");
295 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
300 status = _bt_core_get_status();
301 if (status == BT_ACTIVATING) {
302 /* Forcely terminate */
303 if (TIZEN_FEATURE_BT_USB_DONGLE) {
304 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
305 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0)
306 BT_ERR("running script failed");
309 #ifdef TIZEN_FEATURE_RADIO
310 int radio_status = VCONFKEY_RADIO_STATUS_OFF;
312 /* Check if radio status on or off */
313 if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
314 BT_ERR("Fail to get radio status");
316 BT_DBG("Radio status: %d", radio_status);
318 if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
319 if (__execute_command("/usr/etc/bluetooth/bt-stack-down-with-radio.sh", NULL) < 0)
320 BT_ERR("running script failed");
322 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0)
323 BT_ERR("running script failed");
326 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0)
327 BT_ERR("running script failed");
330 _bt_core_terminate();
332 } else if (status != BT_ACTIVATED) {
333 BT_ERR("Invalid state %d", status);
334 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
337 __bt_core_set_status(BT_DEACTIVATING);
338 if (TIZEN_FEATURE_BT_USB_DONGLE) {
339 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
340 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
341 BT_ERR("running script failed");
342 __bt_core_set_status(BT_ACTIVATED);
346 #ifdef TIZEN_FEATURE_RADIO
347 int radio_status = VCONFKEY_RADIO_STATUS_OFF;
349 /* Check if radio status on or off */
350 if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
351 BT_ERR("Fail to get radio status");
353 BT_DBG("Radio status: %d", radio_status);
355 if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
356 if (__execute_command("/usr/etc/bluetooth/bt-stack-down-with-radio.sh", NULL) < 0) {
357 BT_ERR("running script failed");
358 __bt_core_set_status(BT_ACTIVATED);
362 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
363 BT_ERR("running script failed");
364 __bt_core_set_status(BT_ACTIVATED);
369 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
370 BT_ERR("running script failed");
371 __bt_core_set_status(BT_ACTIVATED);
380 int _bt_enable_adapter_le(void)
386 bt_le_status_t le_status;
387 le_status = _bt_core_get_le_status();
388 retv_if(le_status != BT_LE_DEACTIVATED, -1);
390 status = _bt_core_get_status();
391 if (status == BT_DEACTIVATED) {
392 __bt_core_set_le_status(BT_LE_ACTIVATING);
393 BT_DBG("Activate BT");
394 if (TIZEN_FEATURE_BT_USB_DONGLE) {
395 char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
396 ret = __execute_command("/usr/bin/hciconfig", argv_up);
398 ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
401 BT_ERR("running script failed");
402 if (TIZEN_FEATURE_BT_USB_DONGLE) {
403 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
404 ret = __execute_command("/usr/bin/hciconfig", argv_down);
406 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh &", NULL);
408 __bt_core_set_status(BT_DEACTIVATED);
409 __bt_core_set_le_status(BT_LE_DEACTIVATED);
413 __bt_core_set_le_status(BT_LE_ACTIVATED);
414 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
416 #ifdef TIZEN_FEATURE_BT_HPS
417 ret = _bt_core_start_httpproxy();
419 BT_ERR("_bt_core_start_httpproxy() failed");
425 #ifdef TIZEN_FEATURE_BT_OTP
426 static void _bt_core_stop_otp()
428 GError *error = NULL;
429 GDBusMessage *msg = NULL;
430 GDBusConnection *conn = _bt_core_get_gdbus_connection();
431 msg = g_dbus_message_new_signal(BT_OTP_OBJECT_PATH, BT_OTP_INTERFACE_NAME, BLE_DISABLED);
432 if (!g_dbus_connection_send_message(conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, 0, NULL)) {
434 BT_ERR("Failed to send BLE_DISABLED signal to OTP : errCode[%x], \
436 error->code, error->message);
437 g_clear_error(&error);
443 int _bt_disable_adapter_le(void)
448 bt_le_status_t le_status;
450 le_status = _bt_core_get_le_status();
451 retv_if(le_status == BT_LE_DEACTIVATED, 0);
452 retv_if(le_status == BT_LE_DEACTIVATING, -1);
454 #ifdef TIZEN_FEATURE_BT_HPS
455 _bt_core_stop_httpproxy();
458 #ifdef TIZEN_FEATURE_BT_OTP
462 status = _bt_core_get_status();
463 BT_DBG("status : %d", status);
465 if (status == BT_DEACTIVATED) {
466 __bt_core_set_le_status(BT_LE_DEACTIVATING);
468 if (TIZEN_FEATURE_BT_USB_DONGLE) {
469 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
470 ret = __execute_command("/usr/bin/hciconfig", argv_down);
472 ret = __execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL);
475 BT_ERR("running script failed");
476 __bt_core_set_le_status(BT_LE_ACTIVATED);
480 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
483 __bt_core_set_le_status(BT_LE_DEACTIVATED);
489 int _bt_core_service_request_adapter(int service_function)
493 GArray *in_param1 = NULL;
494 GArray *in_param2 = NULL;
495 GArray *in_param3 = NULL;
496 GArray *in_param4 = NULL;
497 GArray *out_param = NULL;
499 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
501 ret = _bt_core_service_request(BT_CORE_SERVICE, service_function,
502 in_param1, in_param2, in_param3, in_param4, &out_param);
504 BT_ERR("_bt_core_service_request_adapter() failed");
506 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
511 static gboolean __bt_core_check_the_adapter_path(void)
514 GDBusProxy *manager_proxy = NULL;
515 GVariant *result = NULL;
516 char *adapter_path = NULL;
517 GDBusConnection *conn = NULL;
519 conn = _bt_core_get_gdbus_connection();
523 manager_proxy = g_dbus_proxy_new_sync(conn,
524 G_DBUS_PROXY_FLAGS_NONE, NULL,
527 "org.freedesktop.DBus.ObjectManager",
530 if (!manager_proxy) {
532 BT_ERR("Unable to create proxy: %s", err->message);
535 BT_ERR("Fail to create proxy");
540 result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
541 G_DBUS_CALL_FLAGS_NONE, 1000, NULL, &err);
544 BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
547 BT_ERR("Fail to get DefaultAdapter");
552 if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
553 BT_ERR("Incorrect result\n");
557 g_variant_get(result, "(&o)", &adapter_path);
559 if (adapter_path == NULL ||
560 strlen(adapter_path) >= BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX) {
561 BT_ERR("Adapter path is inproper\n");
565 g_variant_unref(result);
566 g_object_unref(manager_proxy);
572 g_variant_unref(result);
575 g_object_unref(manager_proxy);
580 void _bt_core_update_status(void)
582 int bt_status = VCONFKEY_BT_STATUS_OFF;
583 int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
584 gboolean ret = FALSE;
586 ret = __bt_core_check_the_adapter_path();
587 BT_INFO("check the real status of bt_adapter");
590 __bt_core_set_status(BT_DEACTIVATED);
591 __bt_core_set_le_status(BT_DEACTIVATED);
593 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
594 BT_ERR("no bluetooth device info, so BT was disabled at previous session");
595 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
596 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
598 BT_INFO("bt_status = %d, bt_le_status = %d", bt_status, bt_le_status);
600 if (bt_status & VCONFKEY_BT_STATUS_ON)
601 __bt_core_set_status(BT_ACTIVATED);
602 if (bt_le_status & VCONFKEY_BT_LE_STATUS_ON)
603 __bt_core_set_le_status(BT_ACTIVATED);
607 gboolean _bt_core_enable_adapter(void)
611 _bt_set_flightmode_request(FALSE);
612 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
613 BT_ERR("Set vconf failed");
615 ret = _bt_enable_adapter();
622 static gboolean __bt_core_terminate_cb(gpointer data)
624 _bt_core_terminate();
629 gboolean _bt_core_disable_adapter(void)
632 gboolean adapter_state;
634 _bt_set_flightmode_request(FALSE);
635 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
636 BT_ERR("Set vconf failed");
638 adapter_state = __bt_core_check_the_adapter_path();
639 if (adapter_state == FALSE)
640 BT_INFO("Default adapter is NOT normal.");
642 ret = _bt_disable_adapter();
643 if (adapter_state == FALSE) {
644 g_timeout_add(BT_CORE_IDLE_TERM_TIME,
645 __bt_core_terminate_cb, NULL);
655 gboolean _bt_core_recover_adapter(void)
661 BT_INFO_C("Recover bt adapter");
663 _bt_set_flightmode_request(FALSE);
664 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
665 BT_ERR("Set vconf failed");
667 is_recovery_mode = TRUE;
669 _bt_core_update_status();
671 if (_bt_core_get_status() == BT_ACTIVATED) {
672 _bt_core_set_bt_status(BT_RECOVERY_MODE, 1);
673 #ifdef TIZEN_FEATURE_BUSACT
674 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
677 if (_bt_core_get_le_status() == BT_LE_ACTIVATED) {
678 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 1);
679 #ifdef TIZEN_FEATURE_BUSACT
680 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
684 ret = _bt_disable_adapter();
686 BT_ERR("_bt_disable_adapter() failed");
688 /* In platform, don't seperate BR/EDR and LE status */
690 ret_le = _bt_disable_adapter_le();
692 BT_ERR("_bt_disable_adapter_le() failed");
697 gboolean _bt_core_enable_adapter_le(void)
701 ret = _bt_enable_adapter_le();
708 gboolean _bt_core_disable_adapter_le(void)
714 ret = _bt_disable_adapter_le();
721 gboolean __bt_core_reset_adapter(void)
723 /* Forcely terminate */
724 if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0)
725 BT_ERR("running script failed");
727 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
732 static gboolean __bt_core_enable_core_timeout_cb(gpointer data)
736 core_enable_timer_id = 0;
738 _bt_core_init_vconf_value();
743 gboolean _bt_core_enable_core(void)
747 _bt_core_update_status();
749 if (core_enable_timer_id > 0)
750 g_source_remove(core_enable_timer_id);
752 core_enable_timer_id = g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
758 gboolean _bt_core_set_transfer_value(gboolean value)
760 const char *event_val = NULL;
763 BT_DBG("value: %d", value);
765 event_val = value ? EVT_VAL_BT_TRANSFERING : EVT_VAL_BT_NON_TRANSFERING;
767 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_TRANSFERING_STATE,
768 event_val) != ES_R_OK) {
769 BT_ERR("Fail to set BT state value");
773 if (ret == FALSE || value == FALSE) {
774 BT_DBG("Terminate bt-core process");
775 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
782 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
786 char *arg_list[3] = { NULL, NULL, NULL };
788 BT_DBG("Test item : %s", type);
790 if (g_strcmp0(type, "Enable_RF_Test") == 0) {
791 cmd = "/usr/etc/bluetooth/bt-edutm-on.sh";
792 arg_list[0] = "bt-edutm-on.sh";
793 } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
794 cmd = "/usr/etc/bluetooth/bt-edutm-off.sh";
795 arg_list[0] = "bt-edutm-off.sh";
796 } else if (g_strcmp0(type, "Slave_Mode") == 0) {
797 cmd = "/usr/etc/bluetooth/bt-mode-slave.sh";
798 arg_list[0] = "bt-mode-slave.sh";
799 } else if (g_strcmp0(type, "Master_Mode") == 0) {
800 cmd = "/usr/etc/bluetooth/bt-mode-master.sh";
801 arg_list[0] = "bt-mode-master.sh";
802 } else if (g_strcmp0(type, "SSP_Debug_Mode") == 0) {
803 cmd = "/usr/etc/bluetooth/bt-set-ssp-debug-mode.sh";
804 arg_list[0] = "bt-set-ssp-debug-mode.sh";
805 arg_list[1] = (char *)arg;
806 } else if (g_strcmp0(type, "RF_Channel") == 0) {
807 cmd = "/usr/etc/bluetooth/bt-enable-rf-channel.sh";
808 arg_list[0] = "bt-enable-rf-channel.sh";
809 arg_list[1] = (char *)arg;
814 BT_DBG("Run %s", cmd);
815 if (__execute_command(cmd, arg_list) < 0)
816 BT_ERR("running script failed");
821 static gboolean __bt_core_recovery_cb(gpointer data)
824 #ifdef TIZEN_FEATURE_BUSACT
825 gboolean is_request_failed = FALSE;
826 static gboolean is_first_failure = TRUE;
831 #ifdef TIZEN_FEATURE_BUSACT
832 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
833 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
835 is_request_failed = TRUE;
838 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
839 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
841 is_request_failed = TRUE;
844 if (is_request_failed == TRUE) {
845 BT_ERR("Recovery is failed.");
846 if (is_first_failure == TRUE) {
847 g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
848 is_first_failure = FALSE;
851 is_first_failure = TRUE;
855 is_first_failure = TRUE;
858 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
859 _bt_core_set_bt_status(BT_RECOVERY_MODE, 0);
860 ret = _bt_enable_adapter();
862 BT_ERR("_bt_enable_adapter() failed");
866 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
867 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 0);
868 ret = _bt_enable_adapter_le();
870 BT_ERR("_bt_enable_adapter_le() failed");
873 is_recovery_mode = FALSE;
880 static gboolean __bt_core_enable_timeout_cb(gpointer data)
882 bt_status_t adapter_status;
883 bt_le_status_t adapter_status_le;
887 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
888 BT_ERR("Set vconf failed");
890 adapter_status = _bt_core_get_status();
891 adapter_status_le = _bt_core_get_le_status();
893 if (adapter_status == BT_DEACTIVATED &&
894 _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
895 _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
896 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
897 _bt_enable_adapter();
900 if (adapter_status_le == BT_LE_DEACTIVATED &&
901 _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
902 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
903 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
904 _bt_enable_adapter_le();
910 static gboolean __bt_core_disable_timeout_cb(gpointer data)
912 bt_status_t adapter_status;
913 bt_le_status_t adapter_status_le;
917 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
918 BT_ERR("Set vconf failed");
920 adapter_status = _bt_core_get_status();
921 adapter_status_le = _bt_core_get_le_status();
923 if (adapter_status == BT_ACTIVATED) {
924 int bt_status_before_mode = 0;
926 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
927 _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
929 #ifdef TIZEN_FEATURE_BUSACT
930 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
932 _bt_disable_adapter();
935 if (adapter_status_le == BT_LE_ACTIVATED) {
936 int bt_le_status_before_mode = 0;
938 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
939 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
941 #ifdef TIZEN_FEATURE_BUSACT
942 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
944 _bt_disable_adapter_le();
950 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value)
957 bundle_add_str(b, key, value);
959 ret = eventsystem_send_system_event(event, b);
961 BT_DBG("eventsystem_send_system_event result: %d", ret);
968 void _bt_core_adapter_added_cb(void)
971 bt_le_status_t le_status;
972 gboolean flight_mode_status;
976 status = _bt_core_get_status();
977 BT_DBG("status : %d", status);
978 le_status = _bt_core_get_le_status();
979 BT_DBG("le_status : %d", le_status);
981 if (status == BT_ACTIVATING)
982 __bt_core_set_status(BT_ACTIVATED);
983 if (le_status == BT_LE_ACTIVATING)
984 __bt_core_set_le_status(BT_LE_ACTIVATED);
986 flight_mode_status = _bt_core_is_flight_mode_enabled();
988 if (flight_mode_status == TRUE && _bt_is_flightmode_request() == TRUE) {
989 _bt_set_flightmode_request(FALSE);
990 g_timeout_add(2000, (GSourceFunc)__bt_core_disable_timeout_cb, NULL);
993 _bt_set_flightmode_request(FALSE);
995 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
996 EVT_VAL_BT_ON) != ES_R_OK)
997 BT_ERR("Fail to set BT state value");
999 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
1000 EVT_VAL_BT_LE_ON) != ES_R_OK)
1001 BT_ERR("Fail to set BT LE state value");
1003 _bt_core_terminate();
1006 void _bt_core_adapter_removed_cb(void)
1008 int flight_mode_value = 0;
1009 int power_saving_mode = 0;
1010 gboolean flight_mode_status;
1011 static int timer_id = -1;
1013 BT_DBG("is_recovery_mode: %d", is_recovery_mode);
1015 __bt_core_set_status(BT_DEACTIVATED);
1016 __bt_core_set_le_status(BT_LE_DEACTIVATED);
1017 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1018 BT_ERR("Set vconf failed");
1019 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
1020 BT_ERR("Set vconf failed");
1022 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1023 EVT_VAL_BT_OFF) != ES_R_OK)
1024 BT_ERR("Fail to set value");
1026 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
1027 EVT_VAL_BT_LE_OFF) != ES_R_OK)
1028 BT_ERR("Fail to set value");
1030 if (is_recovery_mode == TRUE) {
1032 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
1036 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value) != 0)
1037 BT_ERR("Fail to get the flight_mode_deactivated value");
1039 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &power_saving_mode) != 0)
1040 BT_ERR("Fail to get the ps_mode_deactivated value");
1042 flight_mode_status = _bt_core_is_flight_mode_enabled();
1044 if (flight_mode_status == FALSE && _bt_is_flightmode_request() == TRUE) {
1045 _bt_set_flightmode_request(FALSE);
1047 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_enable_timeout_cb, NULL);
1050 _bt_set_flightmode_request(FALSE);
1052 if (flight_mode_value == 1 || power_saving_mode == 1) {
1053 BT_DBG("Bt Core not terminated");
1057 _bt_core_terminate();