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 <actd/unit_control.h>
25 #include <hal-bluetooth.h>
27 #include "bt-core-main.h"
28 #include "bt-core-adapter.h"
29 #include "bt-core-common.h"
30 #include "bt-core-dbus-handler.h"
31 #include "bt-core-noti-handler.h"
33 #include "bt-internal-types.h"
35 #define BT_CORE_IDLE_TERM_TIME 200 /* 200ms */
36 #define BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX 50
38 #ifdef TIZEN_FEATURE_BT_OTP
39 #define BT_OTP_OBJECT_PATH "/org/projectx/otp"
40 #define BT_OTP_INTERFACE_NAME "org.projectx.otp_service"
41 #define BLE_DISABLED "LeDisabled"
44 static bt_status_t adapter_status = BT_DEACTIVATED;
45 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
46 static gboolean is_recovery_mode = FALSE;
47 static guint core_enable_timer_id;
50 static int bt_status_before[BT_MODE_MAX] = { VCONFKEY_BT_STATUS_OFF, };
51 static int bt_le_status_before[BT_MODE_MAX] = { VCONFKEY_BT_LE_STATUS_OFF, };
53 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value);
55 static void __bt_core_set_status(bt_status_t status)
57 adapter_status = status;
60 bt_status_t _bt_core_get_status(void)
62 return adapter_status;
65 static void __bt_core_set_le_status(bt_le_status_t status)
67 adapter_le_status = status;
70 bt_le_status_t _bt_core_get_le_status(void)
72 return adapter_le_status;
75 int _bt_core_get_bt_status(bt_mode_e mode)
77 return bt_status_before[mode];
80 int _bt_core_get_bt_le_status(bt_mode_e mode)
82 return bt_le_status_before[mode];
85 void _bt_core_set_bt_status(bt_mode_e mode, int status)
87 bt_status_before[mode] = status;
90 void _bt_core_set_bt_le_status(bt_mode_e mode, int status)
92 bt_le_status_before[mode] = status;
95 gboolean _bt_core_is_recovery_mode(void)
97 return is_recovery_mode;
100 static gboolean __bt_core_idle_terminate(gpointer data)
103 _bt_core_terminate();
107 gboolean _bt_core_is_flight_mode_enabled(void)
109 int isFlightMode = 0;
112 if (TIZEN_FEATURE_FLIGHTMODE_ENABLED) {
113 ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &isFlightMode);
115 BT_ERR("vconf_get_bool failed");
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 __execute_command(const char *cmd, char *const arg_list[])
150 BT_ERR("fork failed");
156 BT_ERR("fork failed");
157 } else if (pid2 == 0) {
158 if (arg_list != NULL) {
159 execv(cmd, arg_list);
161 char *argv[] = { NULL };
170 BT_DBG("parent : forked[%d]", pid);
171 waitpid(pid, &status, 0);
172 BT_DBG("child is terminated : %d", status);
179 static int __bt_stack_up(void)
183 /* activate HCI logger */
184 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_LOGGER_START);
186 BT_ERR("Failed to call systemact service");
190 /* Precondition to start bluetooth hal */
191 ret = hal_bluetooth_get_backend();
193 BT_ERR("hal_bluetooth_get_backend() failed. ret: %d", ret);
197 /* Start bluetooth hal */
198 ret = hal_bluetooth_start();
200 BT_ERR("hal_bluetooth_start() failed. ret: %d", ret);
203 BT_ERR("Started successfully bluetooth hal");
206 ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUEZ_START);
208 BT_ERR("Failed to call systemact service");
212 /* activate bluetooth-share */
213 if (!TIZEN_PROFILE_WEARABLE) {
214 ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUETOOTH_SHARE_START);
216 BT_ERR("Failed to call systemact service");
224 int _bt_enable_adapter(void)
231 status = _bt_core_get_status();
232 if (status != BT_DEACTIVATED) {
233 BT_ERR("Invalid state %d", status);
234 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
238 /* only the concept of private
240 bt_le_status_t le_status;
242 le_status = _bt_core_get_le_status();
243 if (le_status == BT_LE_ACTIVATED) {
244 // Turn on PSCAN, (ISCAN if needed)
245 // Return with 0 for the Enabled response.
246 __bt_core_set_status(BT_ACTIVATED);
247 BT_INFO("BR/EDR is enabled.");
248 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
254 __bt_core_set_status(BT_ACTIVATING);
255 if (TIZEN_FEATURE_BT_USB_DONGLE) {
257 ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUEZ_START);
259 BT_ERR("If bluez already exist, skip this error");
261 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_UP);
263 BT_ERR("Failed to call systemact service");
265 ret = __bt_stack_up();
268 BT_ERR("running script failed");
269 if (TIZEN_FEATURE_BT_USB_DONGLE) {
270 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
272 BT_ERR("Failed to call systemact service");
274 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
276 __bt_core_set_status(BT_DEACTIVATED);
283 int _bt_disable_adapter(void)
287 BT_INFO_C("Disable adapter");
289 /* only the concept of private
291 bt_le_status_t le_status;
293 le_status = _bt_core_get_le_status();
294 BT_DBG("le_status : %d", le_status);
295 if (le_status == BT_LE_ACTIVATED) {
296 // Turn off PSCAN, (ISCAN if needed)
297 // Return with 0 for the Disabled response.
298 __bt_core_set_status(BT_DEACTIVATED);
299 BT_INFO("BR/EDR is disabled. now LE only mode");
300 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
306 status = _bt_core_get_status();
307 if (status == BT_ACTIVATING) {
308 /* Forcely terminate */
309 if (TIZEN_FEATURE_BT_USB_DONGLE) {
310 if (__bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN) < 0)
311 BT_ERR("Failed to call systemact service");
313 int radio_status = VCONFKEY_RADIO_STATUS_OFF;
315 /* Check if radio status on or off */
316 if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
317 BT_ERR("Fail to get radio status");
319 BT_DBG("Radio status: %d", radio_status);
321 if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
322 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN_WITH_RADIO) < 0)
323 BT_ERR("running script failed");
327 /* Precondition to stop bluetooth hal */
328 ret = hal_bluetooth_get_backend();
330 BT_ERR("hal_bluetooth_get_backend() failed. ret: %d", ret);
334 /* Stop bluetooth hal */
335 ret = hal_bluetooth_stop();
337 BT_ERR("hal_bluetooth_stop() failed. ret: %d", ret);
340 BT_ERR("Stopped successfully bluetooth hal");
343 _bt_core_terminate();
345 } else if (status != BT_ACTIVATED) {
346 BT_ERR("Invalid state %d", status);
347 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
350 __bt_core_set_status(BT_DEACTIVATING);
351 if (TIZEN_FEATURE_BT_USB_DONGLE) {
352 if (__bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN) < 0) {
353 BT_ERR("Failed to call systemact service");
354 __bt_core_set_status(BT_ACTIVATED);
357 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
359 int radio_status = VCONFKEY_RADIO_STATUS_OFF;
361 /* Check if radio status on or off */
362 if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
363 BT_ERR("Fail to get radio status");
365 BT_DBG("Radio status: %d", radio_status);
367 if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
368 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN_WITH_RADIO) < 0) {
369 BT_ERR("running script failed");
370 __bt_core_set_status(BT_ACTIVATED);
376 /* Precondition to stop bluetooth hal */
377 ret = hal_bluetooth_get_backend();
379 BT_ERR("hal_bluetooth_get_backend() failed. ret: %d", ret);
383 /* Stop bluetooth hal */
384 ret = hal_bluetooth_stop();
386 BT_ERR("hal_bluetooth_stop() failed. ret: %d", ret);
389 BT_ERR("Stopped successfully bluetooth hal");
396 int _bt_enable_adapter_le(void)
402 bt_le_status_t le_status;
403 le_status = _bt_core_get_le_status();
404 retv_if(le_status == BT_LE_ACTIVATED, 0);
405 retv_if(le_status != BT_LE_DEACTIVATED, -1);
407 status = _bt_core_get_status();
408 if (status == BT_DEACTIVATED) {
409 __bt_core_set_le_status(BT_LE_ACTIVATING);
410 BT_DBG("Activate BT");
411 if (TIZEN_FEATURE_BT_USB_DONGLE) {
412 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_UP);
414 BT_ERR("Failed to call systemact service");
416 ret = __bt_stack_up();
419 BT_ERR("running script failed");
420 if (TIZEN_FEATURE_BT_USB_DONGLE) {
421 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
423 BT_ERR("Failed to call systemact service");
425 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh &", NULL);
427 __bt_core_set_status(BT_DEACTIVATED);
428 __bt_core_set_le_status(BT_LE_DEACTIVATED);
432 __bt_core_set_le_status(BT_LE_ACTIVATED);
433 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
435 #ifdef TIZEN_FEATURE_BT_HPS
436 ret = _bt_core_start_httpproxy();
438 BT_ERR("_bt_core_start_httpproxy() failed");
444 #ifdef TIZEN_FEATURE_BT_OTP
445 static void _bt_core_stop_otp()
447 GError *error = NULL;
448 GDBusMessage *msg = NULL;
449 GDBusConnection *conn = _bt_core_get_gdbus_connection();
450 msg = g_dbus_message_new_signal(BT_OTP_OBJECT_PATH, BT_OTP_INTERFACE_NAME, BLE_DISABLED);
451 if (!g_dbus_connection_send_message(conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, 0, NULL)) {
453 BT_ERR("Failed to send BLE_DISABLED signal to OTP : errCode[%x], \
455 error->code, error->message);
456 g_clear_error(&error);
462 int _bt_disable_adapter_le(void)
467 bt_le_status_t le_status;
469 le_status = _bt_core_get_le_status();
470 retv_if(le_status == BT_LE_DEACTIVATED, 0);
471 retv_if(le_status == BT_LE_DEACTIVATING, -1);
473 #ifdef TIZEN_FEATURE_BT_HPS
474 _bt_core_stop_httpproxy();
477 #ifdef TIZEN_FEATURE_BT_OTP
481 status = _bt_core_get_status();
482 BT_DBG("status : %d", status);
484 if (status == BT_DEACTIVATED) {
485 __bt_core_set_le_status(BT_LE_DEACTIVATING);
487 if (TIZEN_FEATURE_BT_USB_DONGLE) {
488 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
490 BT_ERR("Failed to call systemact service");
492 ret = __execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL);
495 BT_ERR("running script failed");
496 __bt_core_set_le_status(BT_LE_ACTIVATED);
500 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
503 __bt_core_set_le_status(BT_LE_DEACTIVATED);
509 int _bt_core_service_request_adapter(int service_function)
513 GArray *in_param1 = NULL;
514 GArray *in_param2 = NULL;
515 GArray *in_param3 = NULL;
516 GArray *in_param4 = NULL;
517 GArray *out_param = NULL;
519 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
521 ret = _bt_core_service_request(BT_CORE_SERVICE, service_function,
522 in_param1, in_param2, in_param3, in_param4, &out_param);
524 BT_ERR("_bt_core_service_request_adapter() failed");
526 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
531 static gboolean __bt_core_check_the_adapter_path(void)
534 GDBusProxy *manager_proxy = NULL;
535 GVariant *result = NULL;
536 char *adapter_path = NULL;
537 GDBusConnection *conn = NULL;
539 conn = _bt_core_get_gdbus_connection();
543 manager_proxy = g_dbus_proxy_new_sync(conn,
544 G_DBUS_PROXY_FLAGS_NONE, NULL,
547 "org.freedesktop.DBus.ObjectManager",
550 if (!manager_proxy) {
552 BT_ERR("Unable to create proxy: %s", err->message);
555 BT_ERR("Fail to create proxy");
560 result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
561 G_DBUS_CALL_FLAGS_NONE, 1000, NULL, &err);
564 BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
567 BT_ERR("Fail to get DefaultAdapter");
572 if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
573 BT_ERR("Incorrect result\n");
577 g_variant_get(result, "(&o)", &adapter_path);
579 if (adapter_path == NULL ||
580 strlen(adapter_path) >= BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX) {
581 BT_ERR("Adapter path is inproper\n");
585 g_variant_unref(result);
586 g_object_unref(manager_proxy);
592 g_variant_unref(result);
595 g_object_unref(manager_proxy);
600 void _bt_core_update_status(void)
602 int bt_status = VCONFKEY_BT_STATUS_OFF;
603 int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
604 gboolean ret = FALSE;
606 ret = __bt_core_check_the_adapter_path();
607 BT_INFO("check the real status of bt_adapter");
610 __bt_core_set_status(BT_DEACTIVATED);
611 __bt_core_set_le_status(BT_DEACTIVATED);
613 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
614 BT_ERR("no bluetooth device info, so BT was disabled at previous session");
615 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
616 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
618 BT_INFO("bt_status = %d, bt_le_status = %d", bt_status, bt_le_status);
620 if (bt_status & VCONFKEY_BT_STATUS_ON)
621 __bt_core_set_status(BT_ACTIVATED);
622 if (bt_le_status & VCONFKEY_BT_LE_STATUS_ON)
623 __bt_core_set_le_status(BT_ACTIVATED);
627 gboolean _bt_core_enable_adapter(void)
631 _bt_set_flightmode_request(FALSE);
632 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
633 BT_ERR("Set vconf failed");
635 ret = _bt_enable_adapter();
642 static gboolean __bt_core_terminate_cb(gpointer data)
644 _bt_core_terminate();
649 gboolean _bt_core_disable_adapter(void)
652 gboolean adapter_state;
654 _bt_set_flightmode_request(FALSE);
656 adapter_state = __bt_core_check_the_adapter_path();
657 if (adapter_state == FALSE)
658 BT_INFO("Default adapter is NOT normal.");
660 ret = _bt_disable_adapter();
661 if (adapter_state == FALSE) {
662 g_timeout_add(BT_CORE_IDLE_TERM_TIME,
663 __bt_core_terminate_cb, NULL);
673 gboolean _bt_core_recover_adapter(void)
677 BT_INFO_C("Recover bt adapter");
679 _bt_set_flightmode_request(FALSE);
680 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
681 BT_ERR("Set vconf failed");
683 is_recovery_mode = TRUE;
685 _bt_core_update_status();
687 if (_bt_core_get_status() == BT_ACTIVATED) {
688 _bt_core_set_bt_status(BT_RECOVERY_MODE, 1);
690 if (_bt_core_get_le_status() == BT_LE_ACTIVATED) {
691 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 1);
694 ret = _bt_disable_adapter();
696 BT_ERR("_bt_disable_adapter() failed");
698 /* 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");
710 gboolean _bt_core_enable_adapter_le(void)
714 ret = _bt_enable_adapter_le();
721 gboolean _bt_core_disable_adapter_le(void)
727 ret = _bt_disable_adapter_le();
734 gboolean __bt_core_reset_adapter(void)
736 /* Forcely terminate */
737 if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0)
738 BT_ERR("running script failed");
740 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
745 static gboolean __bt_core_enable_core_timeout_cb(gpointer data)
749 core_enable_timer_id = 0;
751 _bt_core_init_vconf_value();
756 gboolean _bt_core_enable_core(void)
760 _bt_core_update_status();
762 if (core_enable_timer_id > 0)
763 g_source_remove(core_enable_timer_id);
765 core_enable_timer_id = g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
771 gboolean _bt_core_set_transfer_value(gboolean value)
773 const char *event_val = NULL;
776 BT_DBG("value: %d", value);
778 event_val = value ? EVT_VAL_BT_TRANSFERING : EVT_VAL_BT_NON_TRANSFERING;
780 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_TRANSFERING_STATE,
781 event_val) != ES_R_OK) {
782 BT_ERR("Fail to set BT state value");
786 if (ret == FALSE || value == FALSE) {
787 BT_DBG("Terminate bt-core process");
788 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
795 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
798 BT_DBG("Test item : %s", type);
800 if (g_strcmp0(type, "Enable_RF_Test") == 0) {
801 ret = __bt_call_systemact_service(BT_SYSTEMACT_EDUTM_ON);
803 BT_ERR("Failed to call systemact service");
804 } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
805 ret = __bt_call_systemact_service(BT_SYSTEMACT_EDUTM_OFF);
807 BT_ERR("Failed to call systemact service");
809 BT_DBG("Terminate bt-core process");
810 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
817 static gboolean __bt_core_recovery_cb(gpointer data)
820 gboolean is_request_failed = FALSE;
821 static gboolean is_first_failure = TRUE;
825 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
826 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
828 is_request_failed = TRUE;
831 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
832 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
834 is_request_failed = TRUE;
837 if (is_request_failed == TRUE) {
838 BT_ERR("Recovery is failed.");
839 if (is_first_failure == TRUE) {
840 g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
841 is_first_failure = FALSE;
844 is_first_failure = TRUE;
848 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");
857 is_recovery_mode = FALSE;
864 static gboolean __bt_core_enable_timeout_cb(gpointer data)
866 bt_status_t adapter_status;
867 bt_le_status_t adapter_status_le;
871 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
872 BT_ERR("Set vconf failed");
874 adapter_status = _bt_core_get_status();
875 adapter_status_le = _bt_core_get_le_status();
877 if (adapter_status == BT_DEACTIVATED &&
878 _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
879 _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
880 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
881 _bt_enable_adapter();
884 if (adapter_status_le == BT_LE_DEACTIVATED &&
885 _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
886 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
887 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
888 _bt_enable_adapter_le();
894 static gboolean __bt_core_disable_timeout_cb(gpointer data)
896 bt_status_t adapter_status;
897 bt_le_status_t adapter_status_le;
901 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
902 BT_ERR("Set vconf failed");
904 adapter_status = _bt_core_get_status();
905 adapter_status_le = _bt_core_get_le_status();
907 if (adapter_status == BT_ACTIVATED) {
908 int bt_status_before_mode = 0;
910 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
911 _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
913 _bt_disable_adapter();
916 if (adapter_status_le == BT_LE_ACTIVATED) {
917 int bt_le_status_before_mode = 0;
919 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
920 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
922 _bt_disable_adapter_le();
928 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value)
935 bundle_add_str(b, key, value);
937 ret = eventsystem_send_system_event(event, b);
939 BT_DBG("eventsystem_send_system_event result: %d", ret);
946 void _bt_core_adapter_added_cb(void)
949 bt_le_status_t le_status;
950 gboolean flight_mode_status;
954 status = _bt_core_get_status();
955 BT_DBG("status : %d", status);
956 le_status = _bt_core_get_le_status();
957 BT_DBG("le_status : %d", le_status);
959 if (status == BT_ACTIVATING)
960 __bt_core_set_status(BT_ACTIVATED);
961 if (le_status == BT_LE_ACTIVATING)
962 __bt_core_set_le_status(BT_LE_ACTIVATED);
964 flight_mode_status = _bt_core_is_flight_mode_enabled();
966 if (flight_mode_status == TRUE && _bt_is_flightmode_request() == TRUE) {
967 _bt_set_flightmode_request(FALSE);
968 g_timeout_add(2000, (GSourceFunc)__bt_core_disable_timeout_cb, NULL);
971 _bt_set_flightmode_request(FALSE);
973 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
974 EVT_VAL_BT_ON) != ES_R_OK)
975 BT_ERR("Fail to set BT state value");
977 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
978 EVT_VAL_BT_LE_ON) != ES_R_OK)
979 BT_ERR("Fail to set BT LE state value");
981 _bt_core_terminate();
984 void _bt_core_adapter_removed_cb(void)
986 int flight_mode_value = 0;
987 int power_saving_mode = 0;
988 gboolean flight_mode_status;
989 static int timer_id = -1;
991 BT_DBG("is_recovery_mode: %d", is_recovery_mode);
993 __bt_core_set_status(BT_DEACTIVATED);
994 __bt_core_set_le_status(BT_LE_DEACTIVATED);
995 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
996 BT_ERR("Set vconf failed");
997 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
998 BT_ERR("Set vconf failed");
1000 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1001 EVT_VAL_BT_OFF) != ES_R_OK)
1002 BT_ERR("Fail to set value");
1004 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
1005 EVT_VAL_BT_LE_OFF) != ES_R_OK)
1006 BT_ERR("Fail to set value");
1008 if (is_recovery_mode == TRUE) {
1010 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
1014 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value) != 0)
1015 BT_ERR("Fail to get the flight_mode_deactivated value");
1017 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &power_saving_mode) != 0)
1018 BT_ERR("Fail to get the ps_mode_deactivated value");
1020 flight_mode_status = _bt_core_is_flight_mode_enabled();
1022 if (flight_mode_status == FALSE && _bt_is_flightmode_request() == TRUE) {
1023 _bt_set_flightmode_request(FALSE);
1025 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_enable_timeout_cb, NULL);
1028 _bt_set_flightmode_request(FALSE);
1030 if (flight_mode_value == 1 || power_saving_mode == 1) {
1031 BT_DBG("Bt Core not terminated");
1035 _bt_core_terminate();