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 #include "bt-core-main.h"
25 #include "bt-core-adapter.h"
26 #include "bt-core-common.h"
27 #include "bt-core-dbus-handler.h"
28 #include "bt-core-noti-handler.h"
30 #define BT_CORE_IDLE_TERM_TIME 200 /* 200ms */
31 #define BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX 50
33 #ifdef TIZEN_FEATURE_BT_OTP
34 #define BT_OTP_OBJECT_PATH "/org/projectx/otp"
35 #define BT_OTP_INTERFACE_NAME "org.projectx.otp_service"
36 #define BLE_DISABLED "LeDisabled"
39 static bt_status_t adapter_status = BT_DEACTIVATED;
40 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
41 static gboolean is_recovery_mode = FALSE;
42 static guint core_enable_timer_id;
45 static int bt_status_before[BT_MODE_MAX] = { VCONFKEY_BT_STATUS_OFF, };
46 static int bt_le_status_before[BT_MODE_MAX] = { VCONFKEY_BT_LE_STATUS_OFF, };
48 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value);
50 static void __bt_core_set_status(bt_status_t status)
52 adapter_status = status;
55 bt_status_t _bt_core_get_status(void)
57 return adapter_status;
60 static void __bt_core_set_le_status(bt_le_status_t status)
62 adapter_le_status = status;
65 bt_le_status_t _bt_core_get_le_status(void)
67 return adapter_le_status;
70 int _bt_core_get_bt_status(bt_mode_e mode)
72 return bt_status_before[mode];
75 int _bt_core_get_bt_le_status(bt_mode_e mode)
77 return bt_le_status_before[mode];
80 void _bt_core_set_bt_status(bt_mode_e mode, int status)
82 bt_status_before[mode] = status;
85 void _bt_core_set_bt_le_status(bt_mode_e mode, int status)
87 bt_le_status_before[mode] = status;
90 gboolean _bt_core_is_recovery_mode(void)
92 return is_recovery_mode;
95 static gboolean __bt_core_idle_terminate(gpointer data)
102 gboolean _bt_core_is_flight_mode_enabled(void)
104 int isFlightMode = 0;
107 if (TIZEN_FEATURE_FLIGHTMODE_ENABLED) {
108 ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &isFlightMode);
110 BT_ERR("vconf_get_bool failed");
118 static int __execute_command(const char *cmd, char *const arg_list[])
128 BT_ERR("fork failed");
134 BT_ERR("fork failed");
135 } else if (pid2 == 0) {
136 execv(cmd, arg_list);
143 BT_DBG("parent : forked[%d]", pid);
144 waitpid(pid, &status, 0);
145 BT_DBG("child is terminated : %d", status);
152 int _bt_enable_adapter(void)
157 bt_le_status_t le_status;
161 status = _bt_core_get_status();
162 if (status != BT_DEACTIVATED) {
163 BT_ERR("Invalid state %d", status);
164 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
168 #if 0 /* only the concept of private */
169 le_status = _bt_core_get_le_status();
170 if (le_status == BT_LE_ACTIVATED) {
171 /* Turn on PSCAN, (ISCAN if needed) */
172 /* Return with 0 for the Enabled response. */
173 __bt_core_set_status(BT_ACTIVATED);
174 BT_INFO("BR/EDR is enabled.");
175 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
180 __bt_core_set_status(BT_ACTIVATING);
181 if (TIZEN_FEATURE_BT_USB_DONGLE) {
182 char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
183 ret = __execute_command("/usr/bin/hciconfig", argv_up);
185 ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
188 BT_ERR("running script failed");
189 if (TIZEN_FEATURE_BT_USB_DONGLE) {
190 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
191 ret = __execute_command("/usr/bin/hciconfig", argv_down);
193 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
195 __bt_core_set_status(BT_DEACTIVATED);
202 int _bt_disable_adapter(void)
206 bt_le_status_t le_status;
209 BT_INFO_C("Disable adapter");
211 #if 0 /* only the concept of private */
212 le_status = _bt_core_get_le_status();
213 BT_DBG("le_status : %d", le_status);
214 if (le_status == BT_LE_ACTIVATED) {
215 /* Turn off PSCAN, (ISCAN if needed) */
216 /* Return with 0 for the Disabled response. */
217 __bt_core_set_status(BT_DEACTIVATED);
218 BT_INFO("BR/EDR is disabled. now LE only mode");
219 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
224 status = _bt_core_get_status();
225 if (status == BT_ACTIVATING) {
226 /* Forcely terminate */
227 if (TIZEN_FEATURE_BT_USB_DONGLE) {
228 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
229 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0)
230 BT_ERR("running script failed");
233 #ifdef TIZEN_FEATURE_RADIO
234 int radio_status = VCONFKEY_RADIO_STATUS_OFF;
236 /* Check if radio status on or off */
237 if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
238 BT_ERR("Fail to get radio status");
240 BT_DBG("Radio status: %d", radio_status);
242 if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
243 if (__execute_command("/usr/etc/bluetooth/bt-stack-down-with-radio.sh", NULL) < 0)
244 BT_ERR("running script failed");
246 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0)
247 BT_ERR("running script failed");
250 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0)
251 BT_ERR("running script failed");
254 _bt_core_terminate();
256 } else if (status != BT_ACTIVATED) {
257 BT_ERR("Invalid state %d", status);
258 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
261 __bt_core_set_status(BT_DEACTIVATING);
262 if (TIZEN_FEATURE_BT_USB_DONGLE) {
263 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
264 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
265 BT_ERR("running script failed");
266 __bt_core_set_status(BT_ACTIVATED);
270 #ifdef TIZEN_FEATURE_RADIO
271 int radio_status = VCONFKEY_RADIO_STATUS_OFF;
273 /* Check if radio status on or off */
274 if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
275 BT_ERR("Fail to get radio status");
277 BT_DBG("Radio status: %d", radio_status);
279 if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
280 if (__execute_command("/usr/etc/bluetooth/bt-stack-down-with-radio.sh", NULL) < 0) {
281 BT_ERR("running script failed");
282 __bt_core_set_status(BT_ACTIVATED);
286 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
287 BT_ERR("running script failed");
288 __bt_core_set_status(BT_ACTIVATED);
293 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
294 BT_ERR("running script failed");
295 __bt_core_set_status(BT_ACTIVATED);
304 int _bt_enable_adapter_le(void)
310 bt_le_status_t le_status;
311 le_status = _bt_core_get_le_status();
312 retv_if(le_status != BT_LE_DEACTIVATED, -1);
314 status = _bt_core_get_status();
315 if (status == BT_DEACTIVATED) {
316 __bt_core_set_le_status(BT_LE_ACTIVATING);
317 BT_DBG("Activate BT");
318 if (TIZEN_FEATURE_BT_USB_DONGLE) {
319 char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
320 ret = __execute_command("/usr/bin/hciconfig", argv_up);
322 ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
325 BT_ERR("running script failed");
326 if (TIZEN_FEATURE_BT_USB_DONGLE) {
327 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
328 ret = __execute_command("/usr/bin/hciconfig", argv_down);
330 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh &", NULL);
332 __bt_core_set_status(BT_DEACTIVATED);
333 __bt_core_set_le_status(BT_LE_DEACTIVATED);
337 __bt_core_set_le_status(BT_LE_ACTIVATED);
338 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
340 #ifdef TIZEN_FEATURE_BT_HPS
341 ret = _bt_core_start_httpproxy();
343 BT_ERR("_bt_core_start_httpproxy() failed");
349 #ifdef TIZEN_FEATURE_BT_OTP
350 static void _bt_core_stop_otp()
352 GError *error = NULL;
353 GDBusMessage *msg = NULL;
354 GDBusConnection *conn = _bt_core_get_gdbus_connection();
355 msg = g_dbus_message_new_signal(BT_OTP_OBJECT_PATH, BT_OTP_INTERFACE_NAME, BLE_DISABLED);
356 if (!g_dbus_connection_send_message(conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, 0, NULL)) {
358 BT_ERR("Failed to send BLE_DISABLED signal to OTP : errCode[%x], \
360 error->code, error->message);
361 g_clear_error(&error);
367 int _bt_disable_adapter_le(void)
372 bt_le_status_t le_status;
374 le_status = _bt_core_get_le_status();
375 retv_if(le_status == BT_LE_DEACTIVATED, 0);
376 retv_if(le_status == BT_LE_DEACTIVATING, -1);
378 #ifdef TIZEN_FEATURE_BT_HPS
379 _bt_core_stop_httpproxy();
382 #ifdef TIZEN_FEATURE_BT_OTP
386 status = _bt_core_get_status();
387 BT_DBG("status : %d", status);
389 if (status == BT_DEACTIVATED) {
390 __bt_core_set_le_status(BT_LE_DEACTIVATING);
392 if (TIZEN_FEATURE_BT_USB_DONGLE) {
393 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
394 ret = __execute_command("/usr/bin/hciconfig", argv_down);
396 ret = __execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL);
399 BT_ERR("running script failed");
400 __bt_core_set_le_status(BT_LE_ACTIVATED);
404 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
407 __bt_core_set_le_status(BT_LE_DEACTIVATED);
413 int _bt_core_service_request_adapter(int service_function)
417 GArray *in_param1 = NULL;
418 GArray *in_param2 = NULL;
419 GArray *in_param3 = NULL;
420 GArray *in_param4 = NULL;
421 GArray *out_param = NULL;
423 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
425 ret = _bt_core_service_request(BT_CORE_SERVICE, service_function,
426 in_param1, in_param2, in_param3, in_param4, &out_param);
428 BT_ERR("_bt_core_service_request_adapter() failed");
430 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
435 static gboolean __bt_core_check_the_adapter_path(void)
438 GDBusProxy *manager_proxy = NULL;
439 GVariant *result = NULL;
440 char *adapter_path = NULL;
441 GDBusConnection *conn = NULL;
443 conn = _bt_core_get_gdbus_connection();
447 manager_proxy = g_dbus_proxy_new_sync(conn,
448 G_DBUS_PROXY_FLAGS_NONE, NULL,
451 "org.freedesktop.DBus.ObjectManager",
454 if (!manager_proxy) {
456 BT_ERR("Unable to create proxy: %s", err->message);
459 BT_ERR("Fail to create proxy");
464 result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
465 G_DBUS_CALL_FLAGS_NONE, 1000, NULL, &err);
468 BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
471 BT_ERR("Fail to get DefaultAdapter");
476 if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
477 BT_ERR("Incorrect result\n");
481 g_variant_get(result, "(&o)", &adapter_path);
483 if (adapter_path == NULL ||
484 strlen(adapter_path) >= BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX) {
485 BT_ERR("Adapter path is inproper\n");
489 g_variant_unref(result);
490 g_object_unref(manager_proxy);
496 g_variant_unref(result);
499 g_object_unref(manager_proxy);
504 void _bt_core_update_status(void)
506 int bt_status = VCONFKEY_BT_STATUS_OFF;
507 int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
508 gboolean ret = FALSE;
510 ret = __bt_core_check_the_adapter_path();
511 BT_INFO("check the real status of bt_adapter");
514 __bt_core_set_status(BT_DEACTIVATED);
515 __bt_core_set_le_status(BT_DEACTIVATED);
517 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
518 BT_ERR("no bluetooth device info, so BT was disabled at previous session");
519 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
520 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
522 BT_INFO("bt_status = %d, bt_le_status = %d", bt_status, bt_le_status);
524 if (bt_status & VCONFKEY_BT_STATUS_ON)
525 __bt_core_set_status(BT_ACTIVATED);
526 if (bt_le_status & VCONFKEY_BT_LE_STATUS_ON)
527 __bt_core_set_le_status(BT_ACTIVATED);
531 gboolean _bt_core_enable_adapter(void)
535 _bt_set_flightmode_request(FALSE);
536 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
537 BT_ERR("Set vconf failed");
539 ret = _bt_enable_adapter();
546 static gboolean __bt_core_terminate_cb(gpointer data)
548 _bt_core_terminate();
553 gboolean _bt_core_disable_adapter(void)
556 gboolean adapter_state;
558 _bt_set_flightmode_request(FALSE);
559 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
560 BT_ERR("Set vconf failed");
562 adapter_state = __bt_core_check_the_adapter_path();
563 if (adapter_state == FALSE)
564 BT_INFO("Default adapter is NOT normal.");
566 ret = _bt_disable_adapter();
567 if (adapter_state == FALSE) {
568 g_timeout_add(BT_CORE_IDLE_TERM_TIME,
569 __bt_core_terminate_cb, NULL);
579 gboolean _bt_core_recover_adapter(void)
585 BT_INFO_C("Recover bt adapter");
587 _bt_set_flightmode_request(FALSE);
588 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
589 BT_ERR("Set vconf failed");
591 is_recovery_mode = TRUE;
593 _bt_core_update_status();
595 if (_bt_core_get_status() == BT_ACTIVATED) {
596 _bt_core_set_bt_status(BT_RECOVERY_MODE, 1);
597 #ifdef TIZEN_FEATURE_BUSACT
598 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
601 if (_bt_core_get_le_status() == BT_LE_ACTIVATED) {
602 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 1);
603 #ifdef TIZEN_FEATURE_BUSACT
604 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
608 ret = _bt_disable_adapter();
610 BT_ERR("_bt_disable_adapter() failed");
612 /* In platform, don't seperate BR/EDR and LE status */
614 ret_le = _bt_disable_adapter_le();
616 BT_ERR("_bt_disable_adapter_le() failed");
621 gboolean _bt_core_enable_adapter_le(void)
625 ret = _bt_enable_adapter_le();
632 gboolean _bt_core_disable_adapter_le(void)
638 ret = _bt_disable_adapter_le();
645 gboolean __bt_core_reset_adapter(void)
647 /* Forcely terminate */
648 if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0)
649 BT_ERR("running script failed");
651 _bt_core_terminate();
656 static gboolean __bt_core_enable_core_timeout_cb(gpointer data)
660 core_enable_timer_id = 0;
662 _bt_core_init_vconf_value();
667 gboolean _bt_core_enable_core(void)
671 _bt_core_update_status();
673 if (core_enable_timer_id > 0)
674 g_source_remove(core_enable_timer_id);
676 core_enable_timer_id = g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
682 gboolean _bt_core_set_transfer_value(gboolean value)
684 const char *event_val = NULL;
687 BT_DBG("value: %d", value);
689 event_val = value ? EVT_VAL_BT_TRANSFERING : EVT_VAL_BT_NON_TRANSFERING;
691 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_TRANSFERING_STATE,
692 event_val) != ES_R_OK) {
693 BT_ERR("Fail to set BT state value");
697 if (ret == FALSE || value == FALSE) {
698 BT_DBG("Terminate bt-core process");
699 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
706 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
710 char *arg_list[3] = { NULL, NULL, NULL };
712 BT_DBG("Test item : %s", type);
714 if (g_strcmp0(type, "Enable_RF_Test") == 0) {
715 cmd = "/usr/etc/bluetooth/bt-edutm-on.sh";
716 arg_list[0] = "bt-edutm-on.sh";
717 } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
718 cmd = "/usr/etc/bluetooth/bt-edutm-off.sh";
719 arg_list[0] = "bt-edutm-off.sh";
720 } else if (g_strcmp0(type, "Slave_Mode") == 0) {
721 cmd = "/usr/etc/bluetooth/bt-mode-slave.sh";
722 arg_list[0] = "bt-mode-slave.sh";
723 } else if (g_strcmp0(type, "Master_Mode") == 0) {
724 cmd = "/usr/etc/bluetooth/bt-mode-master.sh";
725 arg_list[0] = "bt-mode-master.sh";
726 } else if (g_strcmp0(type, "SSP_Debug_Mode") == 0) {
727 cmd = "/usr/etc/bluetooth/bt-set-ssp-debug-mode.sh";
728 arg_list[0] = "bt-set-ssp-debug-mode.sh";
729 arg_list[1] = (char *)arg;
730 } else if (g_strcmp0(type, "RF_Channel") == 0) {
731 cmd = "/usr/etc/bluetooth/bt-enable-rf-channel.sh";
732 arg_list[0] = "bt-enable-rf-channel.sh";
733 arg_list[1] = (char *)arg;
738 BT_DBG("Run %s", cmd);
739 if (__execute_command(cmd, arg_list) < 0)
740 BT_ERR("running script failed");
745 static gboolean __bt_core_recovery_cb(gpointer data)
748 #ifdef TIZEN_FEATURE_BUSACT
749 gboolean is_request_failed = FALSE;
750 static gboolean is_first_failure = TRUE;
755 #ifdef TIZEN_FEATURE_BUSACT
756 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
757 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
759 is_request_failed = TRUE;
762 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
763 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
765 is_request_failed = TRUE;
768 if (is_request_failed == TRUE) {
769 BT_ERR("Recovery is failed.");
770 if (is_first_failure == TRUE) {
771 g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
772 is_first_failure = FALSE;
775 is_first_failure = TRUE;
779 is_first_failure = TRUE;
782 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
783 _bt_core_set_bt_status(BT_RECOVERY_MODE, 0);
784 ret = _bt_enable_adapter();
786 BT_ERR("_bt_enable_adapter() failed");
790 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
791 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 0);
792 ret = _bt_enable_adapter_le();
794 BT_ERR("_bt_enable_adapter_le() failed");
797 is_recovery_mode = FALSE;
804 static gboolean __bt_core_enable_timeout_cb(gpointer data)
806 bt_status_t adapter_status;
807 bt_le_status_t adapter_status_le;
811 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
812 BT_ERR("Set vconf failed");
814 adapter_status = _bt_core_get_status();
815 adapter_status_le = _bt_core_get_le_status();
817 if (adapter_status == BT_DEACTIVATED &&
818 _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
819 _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
820 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
821 _bt_enable_adapter();
824 if (adapter_status_le == BT_LE_DEACTIVATED &&
825 _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
826 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
827 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
828 _bt_enable_adapter_le();
834 static gboolean __bt_core_disable_timeout_cb(gpointer data)
836 bt_status_t adapter_status;
837 bt_le_status_t adapter_status_le;
841 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
842 BT_ERR("Set vconf failed");
844 adapter_status = _bt_core_get_status();
845 adapter_status_le = _bt_core_get_le_status();
847 if (adapter_status == BT_ACTIVATED) {
848 int bt_status_before_mode = 0;
850 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
851 _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
853 #ifdef TIZEN_FEATURE_BUSACT
854 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
856 _bt_disable_adapter();
859 if (adapter_status_le == BT_LE_ACTIVATED) {
860 int bt_le_status_before_mode = 0;
862 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
863 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
865 #ifdef TIZEN_FEATURE_BUSACT
866 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
868 _bt_disable_adapter_le();
874 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value)
881 bundle_add_str(b, key, value);
883 ret = eventsystem_send_system_event(event, b);
885 BT_DBG("eventsystem_send_system_event result: %d", ret);
892 void _bt_core_adapter_added_cb(void)
895 bt_le_status_t le_status;
896 gboolean flight_mode_status;
900 status = _bt_core_get_status();
901 BT_DBG("status : %d", status);
902 le_status = _bt_core_get_le_status();
903 BT_DBG("le_status : %d", le_status);
905 if (status == BT_ACTIVATING)
906 __bt_core_set_status(BT_ACTIVATED);
907 if (le_status == BT_LE_ACTIVATING)
908 __bt_core_set_le_status(BT_LE_ACTIVATED);
910 flight_mode_status = _bt_core_is_flight_mode_enabled();
912 if (flight_mode_status == TRUE && _bt_is_flightmode_request() == TRUE) {
913 _bt_set_flightmode_request(FALSE);
914 g_timeout_add(2000, (GSourceFunc)__bt_core_disable_timeout_cb, NULL);
917 _bt_set_flightmode_request(FALSE);
919 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
920 EVT_VAL_BT_ON) != ES_R_OK)
921 BT_ERR("Fail to set BT state value");
923 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
924 EVT_VAL_BT_LE_ON) != ES_R_OK)
925 BT_ERR("Fail to set BT LE state value");
927 _bt_core_terminate();
930 void _bt_core_adapter_removed_cb(void)
932 int flight_mode_value = 0;
933 int power_saving_mode = 0;
934 gboolean flight_mode_status;
935 static int timer_id = -1;
937 BT_DBG("is_recovery_mode: %d", is_recovery_mode);
939 __bt_core_set_status(BT_DEACTIVATED);
940 __bt_core_set_le_status(BT_LE_DEACTIVATED);
941 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
942 BT_ERR("Set vconf failed");
943 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
944 BT_ERR("Set vconf failed");
946 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
947 EVT_VAL_BT_OFF) != ES_R_OK)
948 BT_ERR("Fail to set value");
950 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
951 EVT_VAL_BT_LE_OFF) != ES_R_OK)
952 BT_ERR("Fail to set value");
954 if (is_recovery_mode == TRUE) {
956 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
960 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value) != 0)
961 BT_ERR("Fail to get the flight_mode_deactivated value");
963 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &power_saving_mode) != 0)
964 BT_ERR("Fail to get the ps_mode_deactivated value");
966 flight_mode_status = _bt_core_is_flight_mode_enabled();
968 if (flight_mode_status == FALSE && _bt_is_flightmode_request() == TRUE) {
969 _bt_set_flightmode_request(FALSE);
971 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_enable_timeout_cb, NULL);
974 _bt_set_flightmode_request(FALSE);
976 if (flight_mode_value == 1 || power_saving_mode == 1) {
977 BT_DBG("Bt Core not terminated");
981 _bt_core_terminate();