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");
223 if (TIZEN_FEATURE_DA_REFERENCE) {
224 /* activate bluetooth-hf-agent */
225 ret = __bt_call_systemact_service(BT_SYSTEMACT_HF_AGENT);
227 BT_ERR("Failed to call systemact service");
235 static void __bt_stack_down()
238 struct service_name_table service_list[] = {
239 {TIZEN_FEATURE_BT_SUPPORTED, BT_SYSTEMACT_BLUEZ_START},
240 {TIZEN_FEATURE_BT_SUPPORTED, BT_SYSTEMACT_HCI_LOGGER_START},
241 {TIZEN_FEATURE_BT_SUPPORTED, BT_SYSTEMACT_HF_AGENT},
242 {TIZEN_FEATURE_BT_SUPPORTED, BT_SYSTEMACT_MAP_AGENT},
243 {TIZEN_FEATURE_OPP_SUPPORTED, BT_SYSTEMACT_BLUETOOTH_SHARE_START},
244 {TIZEN_FEATURE_OPP_SUPPORTED, BT_SYSTEMACT_OBEX},
245 {TIZEN_FEATURE_PBAP_SUPPORTED, BT_SYSTEMACT_PBAP_AGENT},
246 {TIZEN_FEATURE_AUDIO_CALL_SUPPORTED, BT_SYSTEMACT_AG_AGENT},
247 {TIZEN_FEATURE_HID_DEVICE_SUPPORTED, BT_SYSTEMACT_HID_AGENT},
251 BT_DBG("Stop bluetooth-related services");
252 for (i = 0; service_list[i].supported != -1; i++) {
253 if (service_list[i].supported) {
254 ret = actd_stop_unit(UNIT_CONTROL_BUS_TYPE_SYSTEM, service_list[i].name, 5000);
255 if (ret != UNIT_CONTROL_OK)
256 BT_ERR("Failed to stop the %s: %d", service_list[i].name, ret);
258 BT_INFO("%s stopped successfully", service_list[i].name);
263 int _bt_enable_adapter(void)
270 status = _bt_core_get_status();
271 if (status != BT_DEACTIVATED) {
272 BT_ERR("Invalid state %d", status);
273 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
277 /* only the concept of private
279 bt_le_status_t le_status;
281 le_status = _bt_core_get_le_status();
282 if (le_status == BT_LE_ACTIVATED) {
283 // Turn on PSCAN, (ISCAN if needed)
284 // Return with 0 for the Enabled response.
285 __bt_core_set_status(BT_ACTIVATED);
286 BT_INFO("BR/EDR is enabled.");
287 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
293 __bt_core_set_status(BT_ACTIVATING);
294 if (TIZEN_FEATURE_BT_USB_DONGLE) {
296 ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUEZ_START);
298 BT_ERR("If bluez already exist, skip this error");
300 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_UP);
302 BT_ERR("Failed to call systemact service");
304 ret = __bt_stack_up();
307 BT_ERR("running script failed");
308 if (TIZEN_FEATURE_BT_USB_DONGLE) {
309 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
311 BT_ERR("Failed to call systemact service");
313 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
315 __bt_core_set_status(BT_DEACTIVATED);
322 int _bt_disable_adapter(void)
326 BT_INFO_C("Disable adapter");
328 /* only the concept of private
330 bt_le_status_t le_status;
332 le_status = _bt_core_get_le_status();
333 BT_DBG("le_status : %d", le_status);
334 if (le_status == BT_LE_ACTIVATED) {
335 // Turn off PSCAN, (ISCAN if needed)
336 // Return with 0 for the Disabled response.
337 __bt_core_set_status(BT_DEACTIVATED);
338 BT_INFO("BR/EDR is disabled. now LE only mode");
339 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
345 status = _bt_core_get_status();
346 if (status == BT_ACTIVATING) {
347 /* Forcely terminate */
348 if (TIZEN_FEATURE_BT_USB_DONGLE) {
349 if (__bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN) < 0)
350 BT_ERR("Failed to call systemact service");
352 int radio_status = VCONFKEY_RADIO_STATUS_OFF;
354 /* Check if radio status on or off */
355 if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
356 BT_ERR("Fail to get radio status");
358 BT_DBG("Radio status: %d", radio_status);
360 if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
361 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN_WITH_RADIO) < 0)
362 BT_ERR("running script failed");
366 /* Precondition to stop bluetooth hal */
367 ret = hal_bluetooth_get_backend();
369 BT_ERR("hal_bluetooth_get_backend() failed. ret: %d", ret);
373 /* Stop bluetooth hal */
374 ret = hal_bluetooth_stop();
376 BT_ERR("hal_bluetooth_stop() failed. ret: %d", ret);
378 BT_ERR("Stopped successfully bluetooth hal");
383 _bt_core_terminate();
385 } else if (status != BT_ACTIVATED) {
386 BT_ERR("Invalid state %d", status);
387 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
390 __bt_core_set_status(BT_DEACTIVATING);
391 if (TIZEN_FEATURE_BT_USB_DONGLE) {
392 if (__bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN) < 0) {
393 BT_ERR("Failed to call systemact service");
394 __bt_core_set_status(BT_ACTIVATED);
397 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
399 int radio_status = VCONFKEY_RADIO_STATUS_OFF;
401 /* Check if radio status on or off */
402 if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
403 BT_ERR("Fail to get radio status");
405 BT_DBG("Radio status: %d", radio_status);
407 if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
408 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN_WITH_RADIO) < 0) {
409 BT_ERR("running script failed");
410 __bt_core_set_status(BT_ACTIVATED);
416 /* Precondition to stop bluetooth hal */
417 ret = hal_bluetooth_get_backend();
419 BT_ERR("hal_bluetooth_get_backend() failed. ret: %d", ret);
423 /* Stop bluetooth hal */
424 ret = hal_bluetooth_stop();
426 BT_ERR("hal_bluetooth_stop() failed. ret: %d", ret);
428 BT_ERR("Stopped successfully bluetooth hal");
437 int _bt_enable_adapter_le(void)
443 bt_le_status_t le_status;
444 le_status = _bt_core_get_le_status();
445 retv_if(le_status == BT_LE_ACTIVATED, 0);
446 retv_if(le_status != BT_LE_DEACTIVATED, -1);
448 status = _bt_core_get_status();
449 if (status == BT_DEACTIVATED) {
450 __bt_core_set_le_status(BT_LE_ACTIVATING);
451 BT_DBG("Activate BT");
452 if (TIZEN_FEATURE_BT_USB_DONGLE) {
453 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_UP);
455 BT_ERR("Failed to call systemact service");
457 ret = __bt_stack_up();
460 BT_ERR("running script failed");
461 if (TIZEN_FEATURE_BT_USB_DONGLE) {
462 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
464 BT_ERR("Failed to call systemact service");
466 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh &", NULL);
468 __bt_core_set_status(BT_DEACTIVATED);
469 __bt_core_set_le_status(BT_LE_DEACTIVATED);
473 __bt_core_set_le_status(BT_LE_ACTIVATED);
474 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
476 #ifdef TIZEN_FEATURE_BT_HPS
477 ret = _bt_core_start_httpproxy();
479 BT_ERR("_bt_core_start_httpproxy() failed");
485 #ifdef TIZEN_FEATURE_BT_OTP
486 static void _bt_core_stop_otp()
488 GError *error = NULL;
489 GDBusMessage *msg = NULL;
490 GDBusConnection *conn = _bt_core_get_gdbus_connection();
491 msg = g_dbus_message_new_signal(BT_OTP_OBJECT_PATH, BT_OTP_INTERFACE_NAME, BLE_DISABLED);
492 if (!g_dbus_connection_send_message(conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, 0, NULL)) {
494 BT_ERR("Failed to send BLE_DISABLED signal to OTP : errCode[%x], \
496 error->code, error->message);
497 g_clear_error(&error);
503 int _bt_disable_adapter_le(void)
508 bt_le_status_t le_status;
510 le_status = _bt_core_get_le_status();
511 retv_if(le_status == BT_LE_DEACTIVATED, 0);
512 retv_if(le_status == BT_LE_DEACTIVATING, -1);
514 #ifdef TIZEN_FEATURE_BT_HPS
515 _bt_core_stop_httpproxy();
518 #ifdef TIZEN_FEATURE_BT_OTP
522 status = _bt_core_get_status();
523 BT_DBG("status : %d", status);
525 if (status == BT_DEACTIVATED) {
526 __bt_core_set_le_status(BT_LE_DEACTIVATING);
528 if (TIZEN_FEATURE_BT_USB_DONGLE) {
529 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
531 BT_ERR("Failed to call systemact service");
533 ret = __execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL);
536 BT_ERR("running script failed");
537 __bt_core_set_le_status(BT_LE_ACTIVATED);
541 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
544 __bt_core_set_le_status(BT_LE_DEACTIVATED);
550 int _bt_core_service_request_adapter(int service_function)
554 GArray *in_param1 = NULL;
555 GArray *in_param2 = NULL;
556 GArray *in_param3 = NULL;
557 GArray *in_param4 = NULL;
558 GArray *out_param = NULL;
560 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
562 ret = _bt_core_service_request(BT_CORE_SERVICE, service_function,
563 in_param1, in_param2, in_param3, in_param4, &out_param);
565 BT_ERR("_bt_core_service_request_adapter() failed");
567 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
572 static gboolean __bt_core_check_the_adapter_path(void)
575 GDBusProxy *manager_proxy = NULL;
576 GVariant *result = NULL;
577 char *adapter_path = NULL;
578 GDBusConnection *conn = NULL;
580 conn = _bt_core_get_gdbus_connection();
584 manager_proxy = g_dbus_proxy_new_sync(conn,
585 G_DBUS_PROXY_FLAGS_NONE, NULL,
588 "org.freedesktop.DBus.ObjectManager",
591 if (!manager_proxy) {
593 BT_ERR("Unable to create proxy: %s", err->message);
596 BT_ERR("Fail to create proxy");
601 result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
602 G_DBUS_CALL_FLAGS_NONE, 1000, NULL, &err);
605 BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
608 BT_ERR("Fail to get DefaultAdapter");
613 if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
614 BT_ERR("Incorrect result\n");
618 g_variant_get(result, "(&o)", &adapter_path);
620 if (adapter_path == NULL ||
621 strlen(adapter_path) >= BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX) {
622 BT_ERR("Adapter path is inproper\n");
626 g_variant_unref(result);
627 g_object_unref(manager_proxy);
633 g_variant_unref(result);
636 g_object_unref(manager_proxy);
641 void _bt_core_update_status(void)
643 int bt_status = VCONFKEY_BT_STATUS_OFF;
644 int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
645 gboolean ret = FALSE;
647 ret = __bt_core_check_the_adapter_path();
648 BT_INFO("check the real status of bt_adapter");
651 __bt_core_set_status(BT_DEACTIVATED);
652 __bt_core_set_le_status(BT_DEACTIVATED);
654 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
655 BT_ERR("no bluetooth device info, so BT was disabled at previous session");
656 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
657 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
659 BT_INFO("bt_status = %d, bt_le_status = %d", bt_status, bt_le_status);
661 if (bt_status & VCONFKEY_BT_STATUS_ON)
662 __bt_core_set_status(BT_ACTIVATED);
663 if (bt_le_status & VCONFKEY_BT_LE_STATUS_ON)
664 __bt_core_set_le_status(BT_ACTIVATED);
668 gboolean _bt_core_enable_adapter(void)
672 _bt_set_flightmode_request(FALSE);
673 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
674 BT_ERR("Set vconf failed");
676 ret = _bt_enable_adapter();
683 static gboolean __bt_core_terminate_cb(gpointer data)
685 _bt_core_terminate();
690 gboolean _bt_core_disable_adapter(void)
693 gboolean adapter_state;
695 _bt_set_flightmode_request(FALSE);
697 adapter_state = __bt_core_check_the_adapter_path();
698 if (adapter_state == FALSE)
699 BT_INFO("Default adapter is NOT normal.");
701 ret = _bt_disable_adapter();
702 if (adapter_state == FALSE) {
703 g_timeout_add(BT_CORE_IDLE_TERM_TIME,
704 __bt_core_terminate_cb, NULL);
714 gboolean _bt_core_recover_adapter(void)
718 BT_INFO_C("Recover bt adapter");
720 _bt_set_flightmode_request(FALSE);
721 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
722 BT_ERR("Set vconf failed");
724 is_recovery_mode = TRUE;
726 _bt_core_update_status();
728 if (_bt_core_get_status() == BT_ACTIVATED) {
729 _bt_core_set_bt_status(BT_RECOVERY_MODE, 1);
731 if (_bt_core_get_le_status() == BT_LE_ACTIVATED) {
732 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 1);
735 ret = _bt_disable_adapter();
737 BT_ERR("_bt_disable_adapter() failed");
739 /* In platform, don't seperate BR/EDR and LE status
743 ret_le = _bt_disable_adapter_le();
745 BT_ERR("_bt_disable_adapter_le() failed");
751 gboolean _bt_core_enable_adapter_le(void)
755 ret = _bt_enable_adapter_le();
762 gboolean _bt_core_disable_adapter_le(void)
768 ret = _bt_disable_adapter_le();
775 gboolean __bt_core_reset_adapter(void)
777 /* Forcely terminate */
778 if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0)
779 BT_ERR("running script failed");
781 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
786 static gboolean __bt_core_enable_core_timeout_cb(gpointer data)
790 core_enable_timer_id = 0;
792 _bt_core_init_vconf_value();
797 gboolean _bt_core_enable_core(void)
801 _bt_core_update_status();
803 if (core_enable_timer_id > 0)
804 g_source_remove(core_enable_timer_id);
806 core_enable_timer_id = g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
812 gboolean _bt_core_set_transfer_value(gboolean value)
814 const char *event_val = NULL;
817 BT_DBG("value: %d", value);
819 event_val = value ? EVT_VAL_BT_TRANSFERING : EVT_VAL_BT_NON_TRANSFERING;
821 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_TRANSFERING_STATE,
822 event_val) != ES_R_OK) {
823 BT_ERR("Fail to set BT state value");
827 if (ret == FALSE || value == FALSE) {
828 BT_DBG("Terminate bt-core process");
829 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
836 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
839 BT_DBG("Test item : %s", type);
841 if (g_strcmp0(type, "Enable_RF_Test") == 0) {
842 ret = __bt_call_systemact_service(BT_SYSTEMACT_EDUTM_ON);
844 BT_ERR("Failed to call systemact service");
845 } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
846 ret = __bt_call_systemact_service(BT_SYSTEMACT_EDUTM_OFF);
848 BT_ERR("Failed to call systemact service");
850 BT_DBG("Terminate bt-core process");
851 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
858 static gboolean __bt_core_recovery_cb(gpointer data)
861 gboolean is_request_failed = FALSE;
862 static gboolean is_first_failure = TRUE;
866 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
867 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
869 is_request_failed = TRUE;
872 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
873 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
875 is_request_failed = TRUE;
878 if (is_request_failed == TRUE) {
879 BT_ERR("Recovery is failed.");
880 if (is_first_failure == TRUE) {
881 g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
882 is_first_failure = FALSE;
885 is_first_failure = TRUE;
889 is_first_failure = TRUE;
891 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
892 _bt_core_set_bt_status(BT_RECOVERY_MODE, 0);
893 ret = _bt_enable_adapter();
895 BT_ERR("_bt_enable_adapter() failed");
898 is_recovery_mode = FALSE;
905 static gboolean __bt_core_enable_timeout_cb(gpointer data)
907 bt_status_t adapter_status;
908 bt_le_status_t adapter_status_le;
912 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
913 BT_ERR("Set vconf failed");
915 adapter_status = _bt_core_get_status();
916 adapter_status_le = _bt_core_get_le_status();
918 if (adapter_status == BT_DEACTIVATED &&
919 _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
920 _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
921 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
922 _bt_enable_adapter();
925 if (adapter_status_le == BT_LE_DEACTIVATED &&
926 _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
927 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
928 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
929 _bt_enable_adapter_le();
935 static gboolean __bt_core_disable_timeout_cb(gpointer data)
937 bt_status_t adapter_status;
938 bt_le_status_t adapter_status_le;
942 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
943 BT_ERR("Set vconf failed");
945 adapter_status = _bt_core_get_status();
946 adapter_status_le = _bt_core_get_le_status();
948 if (adapter_status == BT_ACTIVATED) {
949 int bt_status_before_mode = 0;
951 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
952 _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
954 _bt_disable_adapter();
957 if (adapter_status_le == BT_LE_ACTIVATED) {
958 int bt_le_status_before_mode = 0;
960 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
961 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
963 _bt_disable_adapter_le();
969 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value)
976 bundle_add_str(b, key, value);
978 ret = eventsystem_send_system_event(event, b);
980 BT_DBG("eventsystem_send_system_event result: %d", ret);
987 void _bt_core_adapter_added_cb(void)
990 bt_le_status_t le_status;
991 gboolean flight_mode_status;
995 status = _bt_core_get_status();
996 BT_DBG("status : %d", status);
997 le_status = _bt_core_get_le_status();
998 BT_DBG("le_status : %d", le_status);
1000 if (status == BT_ACTIVATING)
1001 __bt_core_set_status(BT_ACTIVATED);
1002 if (le_status == BT_LE_ACTIVATING)
1003 __bt_core_set_le_status(BT_LE_ACTIVATED);
1005 flight_mode_status = _bt_core_is_flight_mode_enabled();
1007 if (flight_mode_status == TRUE && _bt_is_flightmode_request() == TRUE) {
1008 _bt_set_flightmode_request(FALSE);
1009 g_timeout_add(2000, (GSourceFunc)__bt_core_disable_timeout_cb, NULL);
1012 _bt_set_flightmode_request(FALSE);
1014 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1015 EVT_VAL_BT_ON) != ES_R_OK)
1016 BT_ERR("Fail to set BT state value");
1018 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
1019 EVT_VAL_BT_LE_ON) != ES_R_OK)
1020 BT_ERR("Fail to set BT LE state value");
1022 _bt_core_terminate();
1025 void _bt_core_adapter_removed_cb(void)
1027 int flight_mode_value = 0;
1028 int power_saving_mode = 0;
1029 gboolean flight_mode_status;
1030 static int timer_id = -1;
1032 BT_DBG("is_recovery_mode: %d", is_recovery_mode);
1034 __bt_core_set_status(BT_DEACTIVATED);
1035 __bt_core_set_le_status(BT_LE_DEACTIVATED);
1036 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1037 BT_ERR("Set vconf failed");
1038 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
1039 BT_ERR("Set vconf failed");
1041 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1042 EVT_VAL_BT_OFF) != ES_R_OK)
1043 BT_ERR("Fail to set value");
1045 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
1046 EVT_VAL_BT_LE_OFF) != ES_R_OK)
1047 BT_ERR("Fail to set value");
1049 if (is_recovery_mode == TRUE) {
1051 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
1055 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value) != 0)
1056 BT_ERR("Fail to get the flight_mode_deactivated value");
1058 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &power_saving_mode) != 0)
1059 BT_ERR("Fail to get the ps_mode_deactivated value");
1061 flight_mode_status = _bt_core_is_flight_mode_enabled();
1063 if (flight_mode_status == FALSE && _bt_is_flightmode_request() == TRUE) {
1064 _bt_set_flightmode_request(FALSE);
1066 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_enable_timeout_cb, NULL);
1069 _bt_set_flightmode_request(FALSE);
1071 if (flight_mode_value == 1 || power_saving_mode == 1) {
1072 BT_DBG("Bt Core not terminated");
1076 _bt_core_terminate();