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
37 #define BT_CORE_SERVICE_NAME_LENGTH_MAX 50
39 #ifdef TIZEN_FEATURE_BT_OTP
40 #define BT_OTP_OBJECT_PATH "/org/projectx/otp"
41 #define BT_OTP_INTERFACE_NAME "org.projectx.otp_service"
42 #define BLE_DISABLED "LeDisabled"
45 struct service_name_table {
47 char name[BT_CORE_SERVICE_NAME_LENGTH_MAX];
50 static bt_status_t adapter_status = BT_DEACTIVATED;
51 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
52 static gboolean is_recovery_mode = FALSE;
53 static guint core_enable_timer_id;
56 static int bt_status_before[BT_MODE_MAX] = { VCONFKEY_BT_STATUS_OFF, };
57 static int bt_le_status_before[BT_MODE_MAX] = { VCONFKEY_BT_LE_STATUS_OFF, };
59 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value);
61 static void __bt_core_set_status(bt_status_t status)
63 adapter_status = status;
66 bt_status_t _bt_core_get_status(void)
68 return adapter_status;
71 static void __bt_core_set_le_status(bt_le_status_t status)
73 adapter_le_status = status;
76 bt_le_status_t _bt_core_get_le_status(void)
78 return adapter_le_status;
81 int _bt_core_get_bt_status(bt_mode_e mode)
83 return bt_status_before[mode];
86 int _bt_core_get_bt_le_status(bt_mode_e mode)
88 return bt_le_status_before[mode];
91 void _bt_core_set_bt_status(bt_mode_e mode, int status)
93 bt_status_before[mode] = status;
96 void _bt_core_set_bt_le_status(bt_mode_e mode, int status)
98 bt_le_status_before[mode] = status;
101 gboolean _bt_core_is_recovery_mode(void)
103 return is_recovery_mode;
106 static gboolean __bt_core_idle_terminate(gpointer data)
109 _bt_core_terminate();
113 gboolean _bt_core_is_flight_mode_enabled(void)
115 int is_flight_mode = 0;
118 ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &is_flight_mode);
120 BT_ERR("vconf_get_bool failed");
122 return is_flight_mode;
125 static int __bt_call_systemact_service(const char *service_name)
129 BT_DBG("Use System Activated : %s", service_name);
131 ret = actd_start_unit(UNIT_CONTROL_BUS_TYPE_SYSTEM, service_name, 5000);
133 if (ret != UNIT_CONTROL_OK) {
134 BT_ERR("Failed to activate the %s: %d", service_name, ret);
135 /* Return Success even if the activating result is fail */
142 static int __execute_command(const char *cmd, char *const arg_list[])
152 BT_ERR("fork failed");
158 BT_ERR("fork failed");
159 } else if (pid2 == 0) {
160 if (arg_list != NULL) {
161 execv(cmd, arg_list);
163 char *argv[] = { NULL };
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 /* Precondition to start bluetooth hal */
193 ret = hal_bluetooth_get_backend();
195 BT_ERR("hal_bluetooth_get_backend() failed. ret: %d", ret);
199 /* Start bluetooth hal */
200 ret = hal_bluetooth_start();
202 BT_ERR("hal_bluetooth_start() failed. ret: %d", ret);
205 BT_ERR("Started successfully bluetooth hal");
208 ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUEZ_START);
210 BT_ERR("Failed to call systemact service");
214 /* activate bluetooth-share */
215 if (!TIZEN_PROFILE_WEARABLE) {
216 ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUETOOTH_SHARE_START);
218 BT_ERR("Failed to call systemact service");
226 static void __bt_stack_down()
229 struct service_name_table service_list[] = {
230 {TIZEN_FEATURE_BT_SUPPORTED, BT_SYSTEMACT_BLUEZ_START},
231 {TIZEN_FEATURE_BT_SUPPORTED, BT_SYSTEMACT_HCI_LOGGER_START},
232 {TIZEN_FEATURE_BT_SUPPORTED, BT_SYSTEMACT_HF_AGENT},
233 {TIZEN_FEATURE_BT_SUPPORTED, BT_SYSTEMACT_MAP_AGENT},
234 {TIZEN_FEATURE_OPP_SUPPORTED, BT_SYSTEMACT_BLUETOOTH_SHARE_START},
235 {TIZEN_FEATURE_OPP_SUPPORTED, BT_SYSTEMACT_OBEX},
236 {TIZEN_FEATURE_PBAP_SUPPORTED, BT_SYSTEMACT_PBAP_AGENT},
237 {TIZEN_FEATURE_AUDIO_CALL_SUPPORTED, BT_SYSTEMACT_AG_AGENT},
238 {TIZEN_FEATURE_HID_DEVICE_SUPPORTED, BT_SYSTEMACT_HID_AGENT},
242 BT_DBG("Stop bluetooth-related services");
243 for (i = 0; service_list[i].supported != -1; i++) {
244 if (service_list[i].supported) {
245 ret = actd_stop_unit(UNIT_CONTROL_BUS_TYPE_SYSTEM, service_list[i].name, 5000);
246 if (ret != UNIT_CONTROL_OK)
247 BT_ERR("Failed to stop the %s: %d", service_list[i].name, ret);
249 BT_INFO("%s stopped successfully", service_list[i].name);
254 int _bt_enable_adapter(void)
261 status = _bt_core_get_status();
262 if (status != BT_DEACTIVATED) {
263 BT_ERR("Invalid state %d", status);
264 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
268 /* only the concept of private
270 bt_le_status_t le_status;
272 le_status = _bt_core_get_le_status();
273 if (le_status == BT_LE_ACTIVATED) {
274 // Turn on PSCAN, (ISCAN if needed)
275 // Return with 0 for the Enabled response.
276 __bt_core_set_status(BT_ACTIVATED);
277 BT_INFO("BR/EDR is enabled.");
278 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
284 __bt_core_set_status(BT_ACTIVATING);
285 if (TIZEN_FEATURE_BT_USB_DONGLE) {
287 ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUEZ_START);
289 BT_ERR("If bluez already exist, skip this error");
291 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_UP);
293 BT_ERR("Failed to call systemact service");
295 ret = __bt_stack_up();
298 BT_ERR("running script failed");
299 if (TIZEN_FEATURE_BT_USB_DONGLE) {
300 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
302 BT_ERR("Failed to call systemact service");
304 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
306 __bt_core_set_status(BT_DEACTIVATED);
313 int _bt_disable_adapter(void)
317 BT_INFO_C("Disable adapter");
319 /* only the concept of private
321 bt_le_status_t le_status;
323 le_status = _bt_core_get_le_status();
324 BT_DBG("le_status : %d", le_status);
325 if (le_status == BT_LE_ACTIVATED) {
326 // Turn off PSCAN, (ISCAN if needed)
327 // Return with 0 for the Disabled response.
328 __bt_core_set_status(BT_DEACTIVATED);
329 BT_INFO("BR/EDR is disabled. now LE only mode");
330 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
336 status = _bt_core_get_status();
337 if (status == BT_ACTIVATING) {
338 /* Forcely terminate */
339 if (TIZEN_FEATURE_BT_USB_DONGLE) {
340 if (__bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN) < 0)
341 BT_ERR("Failed to call systemact service");
343 int radio_status = VCONFKEY_RADIO_STATUS_OFF;
345 /* Check if radio status on or off */
346 if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
347 BT_ERR("Fail to get radio status");
349 BT_DBG("Radio status: %d", radio_status);
351 if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
352 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN_WITH_RADIO) < 0)
353 BT_ERR("running script failed");
357 /* Precondition to stop bluetooth hal */
358 ret = hal_bluetooth_get_backend();
360 BT_ERR("hal_bluetooth_get_backend() failed. ret: %d", ret);
364 /* Stop bluetooth hal */
365 ret = hal_bluetooth_stop();
367 BT_ERR("hal_bluetooth_stop() failed. ret: %d", ret);
370 BT_ERR("Stopped successfully bluetooth hal");
375 _bt_core_terminate();
377 } else if (status != BT_ACTIVATED) {
378 BT_ERR("Invalid state %d", status);
379 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
382 __bt_core_set_status(BT_DEACTIVATING);
383 if (TIZEN_FEATURE_BT_USB_DONGLE) {
384 if (__bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN) < 0) {
385 BT_ERR("Failed to call systemact service");
386 __bt_core_set_status(BT_ACTIVATED);
389 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
391 int radio_status = VCONFKEY_RADIO_STATUS_OFF;
393 /* Check if radio status on or off */
394 if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
395 BT_ERR("Fail to get radio status");
397 BT_DBG("Radio status: %d", radio_status);
399 if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
400 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN_WITH_RADIO) < 0) {
401 BT_ERR("running script failed");
402 __bt_core_set_status(BT_ACTIVATED);
408 /* Precondition to stop bluetooth hal */
409 ret = hal_bluetooth_get_backend();
411 BT_ERR("hal_bluetooth_get_backend() failed. ret: %d", ret);
415 /* Stop bluetooth hal */
416 ret = hal_bluetooth_stop();
418 BT_ERR("hal_bluetooth_stop() failed. ret: %d", ret);
421 BT_ERR("Stopped successfully bluetooth hal");
430 int _bt_enable_adapter_le(void)
436 bt_le_status_t le_status;
437 le_status = _bt_core_get_le_status();
438 retv_if(le_status == BT_LE_ACTIVATED, 0);
439 retv_if(le_status != BT_LE_DEACTIVATED, -1);
441 status = _bt_core_get_status();
442 if (status == BT_DEACTIVATED) {
443 __bt_core_set_le_status(BT_LE_ACTIVATING);
444 BT_DBG("Activate BT");
445 if (TIZEN_FEATURE_BT_USB_DONGLE) {
446 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_UP);
448 BT_ERR("Failed to call systemact service");
450 ret = __bt_stack_up();
453 BT_ERR("running script failed");
454 if (TIZEN_FEATURE_BT_USB_DONGLE) {
455 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
457 BT_ERR("Failed to call systemact service");
459 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh &", NULL);
461 __bt_core_set_status(BT_DEACTIVATED);
462 __bt_core_set_le_status(BT_LE_DEACTIVATED);
466 __bt_core_set_le_status(BT_LE_ACTIVATED);
467 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
469 #ifdef TIZEN_FEATURE_BT_HPS
470 ret = _bt_core_start_httpproxy();
472 BT_ERR("_bt_core_start_httpproxy() failed");
478 #ifdef TIZEN_FEATURE_BT_OTP
479 static void _bt_core_stop_otp()
481 GError *error = NULL;
482 GDBusMessage *msg = NULL;
483 GDBusConnection *conn = _bt_core_get_gdbus_connection();
484 msg = g_dbus_message_new_signal(BT_OTP_OBJECT_PATH, BT_OTP_INTERFACE_NAME, BLE_DISABLED);
485 if (!g_dbus_connection_send_message(conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, 0, NULL)) {
487 BT_ERR("Failed to send BLE_DISABLED signal to OTP : errCode[%x], \
489 error->code, error->message);
490 g_clear_error(&error);
496 int _bt_disable_adapter_le(void)
501 bt_le_status_t le_status;
503 le_status = _bt_core_get_le_status();
504 retv_if(le_status == BT_LE_DEACTIVATED, 0);
505 retv_if(le_status == BT_LE_DEACTIVATING, -1);
507 #ifdef TIZEN_FEATURE_BT_HPS
508 _bt_core_stop_httpproxy();
511 #ifdef TIZEN_FEATURE_BT_OTP
515 status = _bt_core_get_status();
516 BT_DBG("status : %d", status);
518 if (status == BT_DEACTIVATED) {
519 __bt_core_set_le_status(BT_LE_DEACTIVATING);
521 if (TIZEN_FEATURE_BT_USB_DONGLE) {
522 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
524 BT_ERR("Failed to call systemact service");
526 ret = __execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL);
529 BT_ERR("running script failed");
530 __bt_core_set_le_status(BT_LE_ACTIVATED);
534 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
537 __bt_core_set_le_status(BT_LE_DEACTIVATED);
543 int _bt_core_service_request_adapter(int service_function)
547 GArray *in_param1 = NULL;
548 GArray *in_param2 = NULL;
549 GArray *in_param3 = NULL;
550 GArray *in_param4 = NULL;
551 GArray *out_param = NULL;
553 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
555 ret = _bt_core_service_request(BT_CORE_SERVICE, service_function,
556 in_param1, in_param2, in_param3, in_param4, &out_param);
558 BT_ERR("_bt_core_service_request_adapter() failed");
560 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
565 static gboolean __bt_core_check_the_adapter_path(void)
568 GDBusProxy *manager_proxy = NULL;
569 GVariant *result = NULL;
570 char *adapter_path = NULL;
571 GDBusConnection *conn = NULL;
573 conn = _bt_core_get_gdbus_connection();
577 manager_proxy = g_dbus_proxy_new_sync(conn,
578 G_DBUS_PROXY_FLAGS_NONE, NULL,
581 "org.freedesktop.DBus.ObjectManager",
584 if (!manager_proxy) {
586 BT_ERR("Unable to create proxy: %s", err->message);
589 BT_ERR("Fail to create proxy");
594 result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
595 G_DBUS_CALL_FLAGS_NONE, 1000, NULL, &err);
598 BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
601 BT_ERR("Fail to get DefaultAdapter");
606 if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
607 BT_ERR("Incorrect result\n");
611 g_variant_get(result, "(&o)", &adapter_path);
613 if (adapter_path == NULL ||
614 strlen(adapter_path) >= BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX) {
615 BT_ERR("Adapter path is inproper\n");
619 g_variant_unref(result);
620 g_object_unref(manager_proxy);
626 g_variant_unref(result);
629 g_object_unref(manager_proxy);
634 void _bt_core_update_status(void)
636 int bt_status = VCONFKEY_BT_STATUS_OFF;
637 int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
638 gboolean ret = FALSE;
640 ret = __bt_core_check_the_adapter_path();
641 BT_INFO("check the real status of bt_adapter");
644 __bt_core_set_status(BT_DEACTIVATED);
645 __bt_core_set_le_status(BT_DEACTIVATED);
647 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
648 BT_ERR("no bluetooth device info, so BT was disabled at previous session");
649 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
650 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
652 BT_INFO("bt_status = %d, bt_le_status = %d", bt_status, bt_le_status);
654 if (bt_status & VCONFKEY_BT_STATUS_ON)
655 __bt_core_set_status(BT_ACTIVATED);
656 if (bt_le_status & VCONFKEY_BT_LE_STATUS_ON)
657 __bt_core_set_le_status(BT_ACTIVATED);
661 gboolean _bt_core_enable_adapter(void)
665 _bt_set_flightmode_request(FALSE);
666 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
667 BT_ERR("Set vconf failed");
669 ret = _bt_enable_adapter();
676 static gboolean __bt_core_terminate_cb(gpointer data)
678 _bt_core_terminate();
683 gboolean _bt_core_disable_adapter(void)
686 gboolean adapter_state;
688 _bt_set_flightmode_request(FALSE);
690 adapter_state = __bt_core_check_the_adapter_path();
691 if (adapter_state == FALSE)
692 BT_INFO("Default adapter is NOT normal.");
694 ret = _bt_disable_adapter();
695 if (adapter_state == FALSE) {
696 g_timeout_add(BT_CORE_IDLE_TERM_TIME,
697 __bt_core_terminate_cb, NULL);
707 gboolean _bt_core_recover_adapter(void)
711 BT_INFO_C("Recover bt adapter");
713 _bt_set_flightmode_request(FALSE);
714 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
715 BT_ERR("Set vconf failed");
717 is_recovery_mode = TRUE;
719 _bt_core_update_status();
721 if (_bt_core_get_status() == BT_ACTIVATED) {
722 _bt_core_set_bt_status(BT_RECOVERY_MODE, 1);
724 if (_bt_core_get_le_status() == BT_LE_ACTIVATED) {
725 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 1);
728 ret = _bt_disable_adapter();
730 BT_ERR("_bt_disable_adapter() failed");
732 /* In platform, don't seperate BR/EDR and LE status
736 ret_le = _bt_disable_adapter_le();
738 BT_ERR("_bt_disable_adapter_le() failed");
744 gboolean _bt_core_enable_adapter_le(void)
748 ret = _bt_enable_adapter_le();
755 gboolean _bt_core_disable_adapter_le(void)
761 ret = _bt_disable_adapter_le();
768 gboolean __bt_core_reset_adapter(void)
770 /* Forcely terminate */
771 if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0)
772 BT_ERR("running script failed");
774 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
779 static gboolean __bt_core_enable_core_timeout_cb(gpointer data)
783 core_enable_timer_id = 0;
785 _bt_core_init_vconf_value();
790 gboolean _bt_core_enable_core(void)
794 _bt_core_update_status();
796 if (core_enable_timer_id > 0)
797 g_source_remove(core_enable_timer_id);
799 core_enable_timer_id = g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
805 gboolean _bt_core_set_transfer_value(gboolean value)
807 const char *event_val = NULL;
810 BT_DBG("value: %d", value);
812 event_val = value ? EVT_VAL_BT_TRANSFERING : EVT_VAL_BT_NON_TRANSFERING;
814 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_TRANSFERING_STATE,
815 event_val) != ES_R_OK) {
816 BT_ERR("Fail to set BT state value");
820 if (ret == FALSE || value == FALSE) {
821 BT_DBG("Terminate bt-core process");
822 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
829 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
832 BT_DBG("Test item : %s", type);
834 if (g_strcmp0(type, "Enable_RF_Test") == 0) {
835 ret = __bt_call_systemact_service(BT_SYSTEMACT_EDUTM_ON);
837 BT_ERR("Failed to call systemact service");
838 } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
839 ret = __bt_call_systemact_service(BT_SYSTEMACT_EDUTM_OFF);
841 BT_ERR("Failed to call systemact service");
843 BT_DBG("Terminate bt-core process");
844 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
851 static gboolean __bt_core_recovery_cb(gpointer data)
854 gboolean is_request_failed = FALSE;
855 static gboolean is_first_failure = TRUE;
859 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
860 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
862 is_request_failed = TRUE;
865 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
866 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
868 is_request_failed = TRUE;
871 if (is_request_failed == TRUE) {
872 BT_ERR("Recovery is failed.");
873 if (is_first_failure == TRUE) {
874 g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
875 is_first_failure = FALSE;
878 is_first_failure = TRUE;
882 is_first_failure = TRUE;
884 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
885 _bt_core_set_bt_status(BT_RECOVERY_MODE, 0);
886 ret = _bt_enable_adapter();
888 BT_ERR("_bt_enable_adapter() failed");
891 is_recovery_mode = FALSE;
898 static gboolean __bt_core_enable_timeout_cb(gpointer data)
900 bt_status_t adapter_status;
901 bt_le_status_t adapter_status_le;
905 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
906 BT_ERR("Set vconf failed");
908 adapter_status = _bt_core_get_status();
909 adapter_status_le = _bt_core_get_le_status();
911 if (adapter_status == BT_DEACTIVATED &&
912 _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
913 _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
914 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
915 _bt_enable_adapter();
918 if (adapter_status_le == BT_LE_DEACTIVATED &&
919 _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
920 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
921 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
922 _bt_enable_adapter_le();
928 static gboolean __bt_core_disable_timeout_cb(gpointer data)
930 bt_status_t adapter_status;
931 bt_le_status_t adapter_status_le;
935 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
936 BT_ERR("Set vconf failed");
938 adapter_status = _bt_core_get_status();
939 adapter_status_le = _bt_core_get_le_status();
941 if (adapter_status == BT_ACTIVATED) {
942 int bt_status_before_mode = 0;
944 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
945 _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
947 _bt_disable_adapter();
950 if (adapter_status_le == BT_LE_ACTIVATED) {
951 int bt_le_status_before_mode = 0;
953 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
954 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
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();