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 execv(cmd, arg_list);
194 BT_DBG("parent : forked[%d]", pid);
195 waitpid(pid, &status, 0);
196 BT_DBG("child is terminated : %d", status);
203 static int __bt_stack_up(void)
207 /* activate HCI logger */
208 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_LOGGER_START);
210 BT_ERR("Failed to call systemact service");
215 ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUEZ_START);
217 BT_ERR("Failed to call systemact service");
221 /* activate bluetooth-share */
222 ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUETOOTH_SHARE_START);
224 BT_ERR("Failed to call systemact service");
235 int _bt_enable_adapter(void)
240 bt_le_status_t le_status;
244 status = _bt_core_get_status();
245 if (status != BT_DEACTIVATED) {
246 BT_ERR("Invalid state %d", status);
247 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
251 #if 0 /* only the concept of private */
252 le_status = _bt_core_get_le_status();
253 if (le_status == BT_LE_ACTIVATED) {
254 /* Turn on PSCAN, (ISCAN if needed) */
255 /* Return with 0 for the Enabled response. */
256 __bt_core_set_status(BT_ACTIVATED);
257 BT_INFO("BR/EDR is enabled.");
258 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
263 __bt_core_set_status(BT_ACTIVATING);
264 if (TIZEN_FEATURE_BT_USB_DONGLE) {
265 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_UP);
267 BT_ERR("Failed to call systemact service");
269 ret = __bt_stack_up();
272 BT_ERR("running script failed");
273 if (TIZEN_FEATURE_BT_USB_DONGLE) {
274 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
276 BT_ERR("Failed to call systemact service");
278 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
280 __bt_core_set_status(BT_DEACTIVATED);
287 int _bt_disable_adapter(void)
291 bt_le_status_t le_status;
294 BT_INFO_C("Disable adapter");
296 #if 0 /* only the concept of private */
297 le_status = _bt_core_get_le_status();
298 BT_DBG("le_status : %d", le_status);
299 if (le_status == BT_LE_ACTIVATED) {
300 /* Turn off PSCAN, (ISCAN if needed) */
301 /* Return with 0 for the Disabled response. */
302 __bt_core_set_status(BT_DEACTIVATED);
303 BT_INFO("BR/EDR is disabled. now LE only mode");
304 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
309 status = _bt_core_get_status();
310 if (status == BT_ACTIVATING) {
311 /* Forcely terminate */
312 if (TIZEN_FEATURE_BT_USB_DONGLE) {
313 if (__bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN) < 0)
314 BT_ERR("Failed to call systemact service");
316 #ifdef TIZEN_FEATURE_RADIO
317 int radio_status = VCONFKEY_RADIO_STATUS_OFF;
319 /* Check if radio status on or off */
320 if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
321 BT_ERR("Fail to get radio status");
323 BT_DBG("Radio status: %d", radio_status);
325 if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
326 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN_WITH_RADIO) < 0)
327 BT_ERR("running script failed");
329 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0)
330 BT_ERR("running script failed");
333 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0)
334 BT_ERR("running script failed");
337 _bt_core_terminate();
339 } else if (status != BT_ACTIVATED) {
340 BT_ERR("Invalid state %d", status);
341 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
344 __bt_core_set_status(BT_DEACTIVATING);
345 if (TIZEN_FEATURE_BT_USB_DONGLE) {
346 if (__bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN) < 0) {
347 BT_ERR("Failed to call systemact service");
348 __bt_core_set_status(BT_ACTIVATED);
352 #ifdef TIZEN_FEATURE_RADIO
353 int radio_status = VCONFKEY_RADIO_STATUS_OFF;
355 /* Check if radio status on or off */
356 if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
357 BT_ERR("Fail to get radio status");
359 BT_DBG("Radio status: %d", radio_status);
361 if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
362 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN_WITH_RADIO) < 0) {
363 BT_ERR("running script failed");
364 __bt_core_set_status(BT_ACTIVATED);
368 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0) {
369 BT_ERR("running script failed");
370 __bt_core_set_status(BT_ACTIVATED);
375 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0) {
376 BT_ERR("running script failed");
377 __bt_core_set_status(BT_ACTIVATED);
386 int _bt_enable_adapter_le(void)
392 bt_le_status_t le_status;
393 le_status = _bt_core_get_le_status();
394 retv_if(le_status == BT_LE_ACTIVATED, 0);
395 retv_if(le_status != BT_LE_DEACTIVATED, -1);
397 status = _bt_core_get_status();
398 if (status == BT_DEACTIVATED) {
399 __bt_core_set_le_status(BT_LE_ACTIVATING);
400 BT_DBG("Activate BT");
401 if (TIZEN_FEATURE_BT_USB_DONGLE) {
402 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_UP);
404 BT_ERR("Failed to call systemact service");
406 ret = __bt_call_systemact_service(BT_SYSTEMACT_STACK_UP);
408 BT_ERR("Failed to call systemact service");
411 BT_ERR("running script failed");
412 if (TIZEN_FEATURE_BT_USB_DONGLE) {
413 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
415 BT_ERR("Failed to call systemact service");
417 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh &", NULL);
419 __bt_core_set_status(BT_DEACTIVATED);
420 __bt_core_set_le_status(BT_LE_DEACTIVATED);
424 __bt_core_set_le_status(BT_LE_ACTIVATED);
425 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
427 #ifdef TIZEN_FEATURE_BT_HPS
428 ret = _bt_core_start_httpproxy();
430 BT_ERR("_bt_core_start_httpproxy() failed");
436 #ifdef TIZEN_FEATURE_BT_OTP
437 static void _bt_core_stop_otp()
439 GError *error = NULL;
440 GDBusMessage *msg = NULL;
441 GDBusConnection *conn = _bt_core_get_gdbus_connection();
442 msg = g_dbus_message_new_signal(BT_OTP_OBJECT_PATH, BT_OTP_INTERFACE_NAME, BLE_DISABLED);
443 if (!g_dbus_connection_send_message(conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, 0, NULL)) {
445 BT_ERR("Failed to send BLE_DISABLED signal to OTP : errCode[%x], \
447 error->code, error->message);
448 g_clear_error(&error);
454 int _bt_disable_adapter_le(void)
459 bt_le_status_t le_status;
461 le_status = _bt_core_get_le_status();
462 retv_if(le_status == BT_LE_DEACTIVATED, 0);
463 retv_if(le_status == BT_LE_DEACTIVATING, -1);
465 #ifdef TIZEN_FEATURE_BT_HPS
466 _bt_core_stop_httpproxy();
469 #ifdef TIZEN_FEATURE_BT_OTP
473 status = _bt_core_get_status();
474 BT_DBG("status : %d", status);
476 if (status == BT_DEACTIVATED) {
477 __bt_core_set_le_status(BT_LE_DEACTIVATING);
479 if (TIZEN_FEATURE_BT_USB_DONGLE) {
480 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
482 BT_ERR("Failed to call systemact service");
484 ret = __execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL);
487 BT_ERR("running script failed");
488 __bt_core_set_le_status(BT_LE_ACTIVATED);
492 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
495 __bt_core_set_le_status(BT_LE_DEACTIVATED);
501 int _bt_core_service_request_adapter(int service_function)
505 GArray *in_param1 = NULL;
506 GArray *in_param2 = NULL;
507 GArray *in_param3 = NULL;
508 GArray *in_param4 = NULL;
509 GArray *out_param = NULL;
511 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
513 ret = _bt_core_service_request(BT_CORE_SERVICE, service_function,
514 in_param1, in_param2, in_param3, in_param4, &out_param);
516 BT_ERR("_bt_core_service_request_adapter() failed");
518 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
523 static gboolean __bt_core_check_the_adapter_path(void)
526 GDBusProxy *manager_proxy = NULL;
527 GVariant *result = NULL;
528 char *adapter_path = NULL;
529 GDBusConnection *conn = NULL;
531 conn = _bt_core_get_gdbus_connection();
535 manager_proxy = g_dbus_proxy_new_sync(conn,
536 G_DBUS_PROXY_FLAGS_NONE, NULL,
539 "org.freedesktop.DBus.ObjectManager",
542 if (!manager_proxy) {
544 BT_ERR("Unable to create proxy: %s", err->message);
547 BT_ERR("Fail to create proxy");
552 result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
553 G_DBUS_CALL_FLAGS_NONE, 1000, NULL, &err);
556 BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
559 BT_ERR("Fail to get DefaultAdapter");
564 if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
565 BT_ERR("Incorrect result\n");
569 g_variant_get(result, "(&o)", &adapter_path);
571 if (adapter_path == NULL ||
572 strlen(adapter_path) >= BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX) {
573 BT_ERR("Adapter path is inproper\n");
577 g_variant_unref(result);
578 g_object_unref(manager_proxy);
584 g_variant_unref(result);
587 g_object_unref(manager_proxy);
592 void _bt_core_update_status(void)
594 int bt_status = VCONFKEY_BT_STATUS_OFF;
595 int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
596 gboolean ret = FALSE;
598 ret = __bt_core_check_the_adapter_path();
599 BT_INFO("check the real status of bt_adapter");
602 __bt_core_set_status(BT_DEACTIVATED);
603 __bt_core_set_le_status(BT_DEACTIVATED);
605 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
606 BT_ERR("no bluetooth device info, so BT was disabled at previous session");
607 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
608 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
610 BT_INFO("bt_status = %d, bt_le_status = %d", bt_status, bt_le_status);
612 if (bt_status & VCONFKEY_BT_STATUS_ON)
613 __bt_core_set_status(BT_ACTIVATED);
614 if (bt_le_status & VCONFKEY_BT_LE_STATUS_ON)
615 __bt_core_set_le_status(BT_ACTIVATED);
619 gboolean _bt_core_enable_adapter(void)
623 _bt_set_flightmode_request(FALSE);
624 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
625 BT_ERR("Set vconf failed");
627 ret = _bt_enable_adapter();
634 static gboolean __bt_core_terminate_cb(gpointer data)
636 _bt_core_terminate();
641 gboolean _bt_core_disable_adapter(void)
644 gboolean adapter_state;
646 _bt_set_flightmode_request(FALSE);
647 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
648 BT_ERR("Set vconf failed");
650 adapter_state = __bt_core_check_the_adapter_path();
651 if (adapter_state == FALSE)
652 BT_INFO("Default adapter is NOT normal.");
654 ret = _bt_disable_adapter();
655 if (adapter_state == FALSE) {
656 g_timeout_add(BT_CORE_IDLE_TERM_TIME,
657 __bt_core_terminate_cb, NULL);
667 gboolean _bt_core_recover_adapter(void)
673 BT_INFO_C("Recover bt adapter");
675 _bt_set_flightmode_request(FALSE);
676 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
677 BT_ERR("Set vconf failed");
679 is_recovery_mode = TRUE;
681 _bt_core_update_status();
683 if (_bt_core_get_status() == BT_ACTIVATED) {
684 _bt_core_set_bt_status(BT_RECOVERY_MODE, 1);
685 #ifdef TIZEN_FEATURE_BUSACT
686 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
689 if (_bt_core_get_le_status() == BT_LE_ACTIVATED) {
690 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 1);
691 #ifdef TIZEN_FEATURE_BUSACT
692 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
696 ret = _bt_disable_adapter();
698 BT_ERR("_bt_disable_adapter() failed");
700 /* In platform, don't seperate BR/EDR and LE status */
702 ret_le = _bt_disable_adapter_le();
704 BT_ERR("_bt_disable_adapter_le() failed");
709 gboolean _bt_core_enable_adapter_le(void)
713 ret = _bt_enable_adapter_le();
720 gboolean _bt_core_disable_adapter_le(void)
726 ret = _bt_disable_adapter_le();
733 gboolean __bt_core_reset_adapter(void)
735 /* Forcely terminate */
736 if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0)
737 BT_ERR("running script failed");
739 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
744 static gboolean __bt_core_enable_core_timeout_cb(gpointer data)
748 core_enable_timer_id = 0;
750 _bt_core_init_vconf_value();
755 gboolean _bt_core_enable_core(void)
759 _bt_core_update_status();
761 if (core_enable_timer_id > 0)
762 g_source_remove(core_enable_timer_id);
764 core_enable_timer_id = g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
770 gboolean _bt_core_set_transfer_value(gboolean value)
772 const char *event_val = NULL;
775 BT_DBG("value: %d", value);
777 event_val = value ? EVT_VAL_BT_TRANSFERING : EVT_VAL_BT_NON_TRANSFERING;
779 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_TRANSFERING_STATE,
780 event_val) != ES_R_OK) {
781 BT_ERR("Fail to set BT state value");
785 if (ret == FALSE || value == FALSE) {
786 BT_DBG("Terminate bt-core process");
787 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
794 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
798 char *arg_list[3] = { NULL, NULL, NULL };
800 BT_DBG("Test item : %s", type);
802 if (g_strcmp0(type, "Enable_RF_Test") == 0) {
803 cmd = "/usr/etc/bluetooth/bt-edutm-on.sh";
804 arg_list[0] = "bt-edutm-on.sh";
805 } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
806 cmd = "/usr/etc/bluetooth/bt-edutm-off.sh";
807 arg_list[0] = "bt-edutm-off.sh";
808 } else if (g_strcmp0(type, "Slave_Mode") == 0) {
809 cmd = "/usr/etc/bluetooth/bt-mode-slave.sh";
810 arg_list[0] = "bt-mode-slave.sh";
811 } else if (g_strcmp0(type, "Master_Mode") == 0) {
812 cmd = "/usr/etc/bluetooth/bt-mode-master.sh";
813 arg_list[0] = "bt-mode-master.sh";
814 } else if (g_strcmp0(type, "SSP_Debug_Mode") == 0) {
815 cmd = "/usr/etc/bluetooth/bt-set-ssp-debug-mode.sh";
816 arg_list[0] = "bt-set-ssp-debug-mode.sh";
817 arg_list[1] = (char *)arg;
818 } else if (g_strcmp0(type, "RF_Channel") == 0) {
819 cmd = "/usr/etc/bluetooth/bt-enable-rf-channel.sh";
820 arg_list[0] = "bt-enable-rf-channel.sh";
821 arg_list[1] = (char *)arg;
826 BT_DBG("Run %s", cmd);
827 if (__execute_command(cmd, arg_list) < 0)
828 BT_ERR("running script failed");
833 static gboolean __bt_core_recovery_cb(gpointer data)
836 #ifdef TIZEN_FEATURE_BUSACT
837 gboolean is_request_failed = FALSE;
838 static gboolean is_first_failure = TRUE;
843 #ifdef TIZEN_FEATURE_BUSACT
844 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
845 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
847 is_request_failed = TRUE;
850 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
851 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
853 is_request_failed = TRUE;
856 if (is_request_failed == TRUE) {
857 BT_ERR("Recovery is failed.");
858 if (is_first_failure == TRUE) {
859 g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
860 is_first_failure = FALSE;
863 is_first_failure = TRUE;
867 is_first_failure = TRUE;
870 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
871 _bt_core_set_bt_status(BT_RECOVERY_MODE, 0);
872 ret = _bt_enable_adapter();
874 BT_ERR("_bt_enable_adapter() failed");
878 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
879 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 0);
880 ret = _bt_enable_adapter_le();
882 BT_ERR("_bt_enable_adapter_le() failed");
885 is_recovery_mode = FALSE;
892 static gboolean __bt_core_enable_timeout_cb(gpointer data)
894 bt_status_t adapter_status;
895 bt_le_status_t adapter_status_le;
899 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
900 BT_ERR("Set vconf failed");
902 adapter_status = _bt_core_get_status();
903 adapter_status_le = _bt_core_get_le_status();
905 if (adapter_status == BT_DEACTIVATED &&
906 _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
907 _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
908 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
909 _bt_enable_adapter();
912 if (adapter_status_le == BT_LE_DEACTIVATED &&
913 _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
914 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
915 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
916 _bt_enable_adapter_le();
922 static gboolean __bt_core_disable_timeout_cb(gpointer data)
924 bt_status_t adapter_status;
925 bt_le_status_t adapter_status_le;
929 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
930 BT_ERR("Set vconf failed");
932 adapter_status = _bt_core_get_status();
933 adapter_status_le = _bt_core_get_le_status();
935 if (adapter_status == BT_ACTIVATED) {
936 int bt_status_before_mode = 0;
938 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
939 _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
941 #ifdef TIZEN_FEATURE_BUSACT
942 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
944 _bt_disable_adapter();
947 if (adapter_status_le == BT_LE_ACTIVATED) {
948 int bt_le_status_before_mode = 0;
950 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
951 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
953 #ifdef TIZEN_FEATURE_BUSACT
954 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
956 _bt_disable_adapter_le();
962 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value)
969 bundle_add_str(b, key, value);
971 ret = eventsystem_send_system_event(event, b);
973 BT_DBG("eventsystem_send_system_event result: %d", ret);
980 void _bt_core_adapter_added_cb(void)
983 bt_le_status_t le_status;
984 gboolean flight_mode_status;
988 status = _bt_core_get_status();
989 BT_DBG("status : %d", status);
990 le_status = _bt_core_get_le_status();
991 BT_DBG("le_status : %d", le_status);
993 if (status == BT_ACTIVATING)
994 __bt_core_set_status(BT_ACTIVATED);
995 if (le_status == BT_LE_ACTIVATING)
996 __bt_core_set_le_status(BT_LE_ACTIVATED);
998 flight_mode_status = _bt_core_is_flight_mode_enabled();
1000 if (flight_mode_status == TRUE && _bt_is_flightmode_request() == TRUE) {
1001 _bt_set_flightmode_request(FALSE);
1002 g_timeout_add(2000, (GSourceFunc)__bt_core_disable_timeout_cb, NULL);
1005 _bt_set_flightmode_request(FALSE);
1007 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1008 EVT_VAL_BT_ON) != ES_R_OK)
1009 BT_ERR("Fail to set BT state value");
1011 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
1012 EVT_VAL_BT_LE_ON) != ES_R_OK)
1013 BT_ERR("Fail to set BT LE state value");
1015 _bt_core_terminate();
1018 void _bt_core_adapter_removed_cb(void)
1020 int flight_mode_value = 0;
1021 int power_saving_mode = 0;
1022 gboolean flight_mode_status;
1023 static int timer_id = -1;
1025 BT_DBG("is_recovery_mode: %d", is_recovery_mode);
1027 __bt_core_set_status(BT_DEACTIVATED);
1028 __bt_core_set_le_status(BT_LE_DEACTIVATED);
1029 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1030 BT_ERR("Set vconf failed");
1031 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
1032 BT_ERR("Set vconf failed");
1034 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1035 EVT_VAL_BT_OFF) != ES_R_OK)
1036 BT_ERR("Fail to set value");
1038 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
1039 EVT_VAL_BT_LE_OFF) != ES_R_OK)
1040 BT_ERR("Fail to set value");
1042 if (is_recovery_mode == TRUE) {
1044 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
1048 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value) != 0)
1049 BT_ERR("Fail to get the flight_mode_deactivated value");
1051 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &power_saving_mode) != 0)
1052 BT_ERR("Fail to get the ps_mode_deactivated value");
1054 flight_mode_status = _bt_core_is_flight_mode_enabled();
1056 if (flight_mode_status == FALSE && _bt_is_flightmode_request() == TRUE) {
1057 _bt_set_flightmode_request(FALSE);
1059 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_enable_timeout_cb, NULL);
1062 _bt_set_flightmode_request(FALSE);
1064 if (flight_mode_value == 1 || power_saving_mode == 1) {
1065 BT_DBG("Bt Core not terminated");
1069 _bt_core_terminate();