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>
25 #include "bt-core-main.h"
26 #include "bt-core-adapter.h"
27 #include "bt-core-common.h"
28 #include "bt-core-dbus-handler.h"
29 #include "bt-core-noti-handler.h"
31 #define BT_CORE_IDLE_TERM_TIME 200 /* 200ms */
32 #define BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX 50
34 #ifdef TIZEN_FEATURE_BT_OTP
35 #define BT_OTP_OBJECT_PATH "/org/projectx/otp"
36 #define BT_OTP_INTERFACE_NAME "org.projectx.otp_service"
37 #define BLE_DISABLED "LeDisabled"
40 static bt_status_t adapter_status = BT_DEACTIVATED;
41 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
42 static gboolean is_recovery_mode = FALSE;
43 static guint core_enable_timer_id;
46 static int bt_status_before[BT_MODE_MAX] = { VCONFKEY_BT_STATUS_OFF, };
47 static int bt_le_status_before[BT_MODE_MAX] = { VCONFKEY_BT_LE_STATUS_OFF, };
49 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value);
51 static void __bt_core_set_status(bt_status_t status)
53 adapter_status = status;
56 bt_status_t _bt_core_get_status(void)
58 return adapter_status;
61 static void __bt_core_set_le_status(bt_le_status_t status)
63 adapter_le_status = status;
66 bt_le_status_t _bt_core_get_le_status(void)
68 return adapter_le_status;
71 int _bt_core_get_bt_status(bt_mode_e mode)
73 return bt_status_before[mode];
76 int _bt_core_get_bt_le_status(bt_mode_e mode)
78 return bt_le_status_before[mode];
81 void _bt_core_set_bt_status(bt_mode_e mode, int status)
83 bt_status_before[mode] = status;
86 void _bt_core_set_bt_le_status(bt_mode_e mode, int status)
88 bt_le_status_before[mode] = status;
91 gboolean _bt_core_is_recovery_mode(void)
93 return is_recovery_mode;
96 static gboolean __bt_core_idle_terminate(gpointer data)
103 gboolean _bt_core_is_flight_mode_enabled(void)
105 int isFlightMode = 0;
108 if (TIZEN_FEATURE_FLIGHTMODE_ENABLED) {
109 ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &isFlightMode);
111 BT_ERR("vconf_get_bool failed");
119 static int __bt_call_systemact_service(const char *file_path)
125 BT_ERR("service file path is NULL");
129 if (!access(file_path, F_OK)) {
134 fp = fopen(file_path, "w");
136 BT_ERR("Failed to fopen service file");
147 static int __execute_command(const char *cmd, char *const arg_list[])
157 BT_ERR("fork failed");
163 BT_ERR("fork failed");
164 } else if (pid2 == 0) {
165 execv(cmd, arg_list);
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 /* We need to insert BT driver module by using 'insmod' in case of ARTIK530,
193 * and 'insmod' should be executed by systemd not bt-core. */
194 ret = __bt_call_systemact_service(BT_SYSTEMACT_BT_DRIVER_INSMOD);
196 BT_ERR("Failed to call systemact service");
199 /* Insmod should run before bt-stack-up.sh command */
200 usleep(1000 * 200); /* 200ms */
203 ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
205 BT_ERR("Failed to run script");
206 __bt_call_systemact_service(BT_SYSTEMACT_HCI_LOGGER_STOP);
211 ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUEZ_START);
213 BT_ERR("Failed to call systemact service");
217 /* activate bluetooth-share */
218 ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUETOOTH_SHARE_START);
220 BT_ERR("Failed to call systemact service");
231 int _bt_enable_adapter(void)
236 bt_le_status_t le_status;
240 status = _bt_core_get_status();
241 if (status != BT_DEACTIVATED) {
242 BT_ERR("Invalid state %d", status);
243 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
247 #if 0 /* only the concept of private */
248 le_status = _bt_core_get_le_status();
249 if (le_status == BT_LE_ACTIVATED) {
250 /* Turn on PSCAN, (ISCAN if needed) */
251 /* Return with 0 for the Enabled response. */
252 __bt_core_set_status(BT_ACTIVATED);
253 BT_INFO("BR/EDR is enabled.");
254 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
259 __bt_core_set_status(BT_ACTIVATING);
260 if (TIZEN_FEATURE_BT_USB_DONGLE) {
261 char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
262 ret = __execute_command("/usr/bin/hciconfig", argv_up);
264 ret = __bt_stack_up();
267 BT_ERR("running script failed");
268 if (TIZEN_FEATURE_BT_USB_DONGLE) {
269 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
270 ret = __execute_command("/usr/bin/hciconfig", argv_down);
272 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
274 __bt_core_set_status(BT_DEACTIVATED);
281 int _bt_disable_adapter(void)
285 bt_le_status_t le_status;
288 BT_INFO_C("Disable adapter");
290 #if 0 /* only the concept of private */
291 le_status = _bt_core_get_le_status();
292 BT_DBG("le_status : %d", le_status);
293 if (le_status == BT_LE_ACTIVATED) {
294 /* Turn off PSCAN, (ISCAN if needed) */
295 /* Return with 0 for the Disabled response. */
296 __bt_core_set_status(BT_DEACTIVATED);
297 BT_INFO("BR/EDR is disabled. now LE only mode");
298 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
303 status = _bt_core_get_status();
304 if (status == BT_ACTIVATING) {
305 /* Forcely terminate */
306 if (TIZEN_FEATURE_BT_USB_DONGLE) {
307 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
308 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0)
309 BT_ERR("running script failed");
312 #ifdef TIZEN_FEATURE_RADIO
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");
325 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0)
326 BT_ERR("running script failed");
329 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0)
330 BT_ERR("running script failed");
333 _bt_core_terminate();
335 } else if (status != BT_ACTIVATED) {
336 BT_ERR("Invalid state %d", status);
337 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
340 __bt_core_set_status(BT_DEACTIVATING);
341 if (TIZEN_FEATURE_BT_USB_DONGLE) {
342 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
343 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
344 BT_ERR("running script failed");
345 __bt_core_set_status(BT_ACTIVATED);
349 #ifdef TIZEN_FEATURE_RADIO
350 int radio_status = VCONFKEY_RADIO_STATUS_OFF;
352 /* Check if radio status on or off */
353 if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
354 BT_ERR("Fail to get radio status");
356 BT_DBG("Radio status: %d", radio_status);
358 if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
359 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN_WITH_RADIO) < 0) {
360 BT_ERR("running script failed");
361 __bt_core_set_status(BT_ACTIVATED);
365 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0) {
366 BT_ERR("running script failed");
367 __bt_core_set_status(BT_ACTIVATED);
372 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0) {
373 BT_ERR("running script failed");
374 __bt_core_set_status(BT_ACTIVATED);
383 int _bt_enable_adapter_le(void)
389 bt_le_status_t le_status;
390 le_status = _bt_core_get_le_status();
391 retv_if(le_status != BT_LE_DEACTIVATED, -1);
393 status = _bt_core_get_status();
394 if (status == BT_DEACTIVATED) {
395 __bt_core_set_le_status(BT_LE_ACTIVATING);
396 BT_DBG("Activate BT");
397 if (TIZEN_FEATURE_BT_USB_DONGLE) {
398 char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
399 ret = __execute_command("/usr/bin/hciconfig", argv_up);
401 ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
404 BT_ERR("running script failed");
405 if (TIZEN_FEATURE_BT_USB_DONGLE) {
406 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
407 ret = __execute_command("/usr/bin/hciconfig", argv_down);
409 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh &", NULL);
411 __bt_core_set_status(BT_DEACTIVATED);
412 __bt_core_set_le_status(BT_LE_DEACTIVATED);
416 __bt_core_set_le_status(BT_LE_ACTIVATED);
417 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
419 #ifdef TIZEN_FEATURE_BT_HPS
420 ret = _bt_core_start_httpproxy();
422 BT_ERR("_bt_core_start_httpproxy() failed");
428 #ifdef TIZEN_FEATURE_BT_OTP
429 static void _bt_core_stop_otp()
431 GError *error = NULL;
432 GDBusMessage *msg = NULL;
433 GDBusConnection *conn = _bt_core_get_gdbus_connection();
434 msg = g_dbus_message_new_signal(BT_OTP_OBJECT_PATH, BT_OTP_INTERFACE_NAME, BLE_DISABLED);
435 if (!g_dbus_connection_send_message(conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, 0, NULL)) {
437 BT_ERR("Failed to send BLE_DISABLED signal to OTP : errCode[%x], \
439 error->code, error->message);
440 g_clear_error(&error);
446 int _bt_disable_adapter_le(void)
451 bt_le_status_t le_status;
453 le_status = _bt_core_get_le_status();
454 retv_if(le_status == BT_LE_DEACTIVATED, 0);
455 retv_if(le_status == BT_LE_DEACTIVATING, -1);
457 #ifdef TIZEN_FEATURE_BT_HPS
458 _bt_core_stop_httpproxy();
461 #ifdef TIZEN_FEATURE_BT_OTP
465 status = _bt_core_get_status();
466 BT_DBG("status : %d", status);
468 if (status == BT_DEACTIVATED) {
469 __bt_core_set_le_status(BT_LE_DEACTIVATING);
471 if (TIZEN_FEATURE_BT_USB_DONGLE) {
472 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
473 ret = __execute_command("/usr/bin/hciconfig", argv_down);
475 ret = __execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL);
478 BT_ERR("running script failed");
479 __bt_core_set_le_status(BT_LE_ACTIVATED);
483 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
486 __bt_core_set_le_status(BT_LE_DEACTIVATED);
492 int _bt_core_service_request_adapter(int service_function)
496 GArray *in_param1 = NULL;
497 GArray *in_param2 = NULL;
498 GArray *in_param3 = NULL;
499 GArray *in_param4 = NULL;
500 GArray *out_param = NULL;
502 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
504 ret = _bt_core_service_request(BT_CORE_SERVICE, service_function,
505 in_param1, in_param2, in_param3, in_param4, &out_param);
507 BT_ERR("_bt_core_service_request_adapter() failed");
509 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
514 static gboolean __bt_core_check_the_adapter_path(void)
517 GDBusProxy *manager_proxy = NULL;
518 GVariant *result = NULL;
519 char *adapter_path = NULL;
520 GDBusConnection *conn = NULL;
522 conn = _bt_core_get_gdbus_connection();
526 manager_proxy = g_dbus_proxy_new_sync(conn,
527 G_DBUS_PROXY_FLAGS_NONE, NULL,
530 "org.freedesktop.DBus.ObjectManager",
533 if (!manager_proxy) {
535 BT_ERR("Unable to create proxy: %s", err->message);
538 BT_ERR("Fail to create proxy");
543 result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
544 G_DBUS_CALL_FLAGS_NONE, 1000, NULL, &err);
547 BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
550 BT_ERR("Fail to get DefaultAdapter");
555 if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
556 BT_ERR("Incorrect result\n");
560 g_variant_get(result, "(&o)", &adapter_path);
562 if (adapter_path == NULL ||
563 strlen(adapter_path) >= BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX) {
564 BT_ERR("Adapter path is inproper\n");
568 g_variant_unref(result);
569 g_object_unref(manager_proxy);
575 g_variant_unref(result);
578 g_object_unref(manager_proxy);
583 void _bt_core_update_status(void)
585 int bt_status = VCONFKEY_BT_STATUS_OFF;
586 int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
587 gboolean ret = FALSE;
589 ret = __bt_core_check_the_adapter_path();
590 BT_INFO("check the real status of bt_adapter");
593 __bt_core_set_status(BT_DEACTIVATED);
594 __bt_core_set_le_status(BT_DEACTIVATED);
596 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
597 BT_ERR("no bluetooth device info, so BT was disabled at previous session");
598 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
599 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
601 BT_INFO("bt_status = %d, bt_le_status = %d", bt_status, bt_le_status);
603 if (bt_status & VCONFKEY_BT_STATUS_ON)
604 __bt_core_set_status(BT_ACTIVATED);
605 if (bt_le_status & VCONFKEY_BT_LE_STATUS_ON)
606 __bt_core_set_le_status(BT_ACTIVATED);
610 gboolean _bt_core_enable_adapter(void)
614 _bt_set_flightmode_request(FALSE);
615 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
616 BT_ERR("Set vconf failed");
618 ret = _bt_enable_adapter();
625 static gboolean __bt_core_terminate_cb(gpointer data)
627 _bt_core_terminate();
632 gboolean _bt_core_disable_adapter(void)
635 gboolean adapter_state;
637 _bt_set_flightmode_request(FALSE);
638 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
639 BT_ERR("Set vconf failed");
641 adapter_state = __bt_core_check_the_adapter_path();
642 if (adapter_state == FALSE)
643 BT_INFO("Default adapter is NOT normal.");
645 ret = _bt_disable_adapter();
646 if (adapter_state == FALSE) {
647 g_timeout_add(BT_CORE_IDLE_TERM_TIME,
648 __bt_core_terminate_cb, NULL);
658 gboolean _bt_core_recover_adapter(void)
664 BT_INFO_C("Recover bt adapter");
666 _bt_set_flightmode_request(FALSE);
667 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
668 BT_ERR("Set vconf failed");
670 is_recovery_mode = TRUE;
672 _bt_core_update_status();
674 if (_bt_core_get_status() == BT_ACTIVATED) {
675 _bt_core_set_bt_status(BT_RECOVERY_MODE, 1);
676 #ifdef TIZEN_FEATURE_BUSACT
677 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
680 if (_bt_core_get_le_status() == BT_LE_ACTIVATED) {
681 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 1);
682 #ifdef TIZEN_FEATURE_BUSACT
683 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
687 ret = _bt_disable_adapter();
689 BT_ERR("_bt_disable_adapter() failed");
691 /* In platform, don't seperate BR/EDR and LE status */
693 ret_le = _bt_disable_adapter_le();
695 BT_ERR("_bt_disable_adapter_le() failed");
700 gboolean _bt_core_enable_adapter_le(void)
704 ret = _bt_enable_adapter_le();
711 gboolean _bt_core_disable_adapter_le(void)
717 ret = _bt_disable_adapter_le();
724 gboolean __bt_core_reset_adapter(void)
726 /* Forcely terminate */
727 if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0)
728 BT_ERR("running script failed");
730 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
735 static gboolean __bt_core_enable_core_timeout_cb(gpointer data)
739 core_enable_timer_id = 0;
741 _bt_core_init_vconf_value();
746 gboolean _bt_core_enable_core(void)
750 _bt_core_update_status();
752 if (core_enable_timer_id > 0)
753 g_source_remove(core_enable_timer_id);
755 core_enable_timer_id = g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
761 gboolean _bt_core_set_transfer_value(gboolean value)
763 const char *event_val = NULL;
766 BT_DBG("value: %d", value);
768 event_val = value ? EVT_VAL_BT_TRANSFERING : EVT_VAL_BT_NON_TRANSFERING;
770 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_TRANSFERING_STATE,
771 event_val) != ES_R_OK) {
772 BT_ERR("Fail to set BT state value");
776 if (ret == FALSE || value == FALSE) {
777 BT_DBG("Terminate bt-core process");
778 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
785 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
789 char *arg_list[3] = { NULL, NULL, NULL };
791 BT_DBG("Test item : %s", type);
793 if (g_strcmp0(type, "Enable_RF_Test") == 0) {
794 cmd = "/usr/etc/bluetooth/bt-edutm-on.sh";
795 arg_list[0] = "bt-edutm-on.sh";
796 } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
797 cmd = "/usr/etc/bluetooth/bt-edutm-off.sh";
798 arg_list[0] = "bt-edutm-off.sh";
799 } else if (g_strcmp0(type, "Slave_Mode") == 0) {
800 cmd = "/usr/etc/bluetooth/bt-mode-slave.sh";
801 arg_list[0] = "bt-mode-slave.sh";
802 } else if (g_strcmp0(type, "Master_Mode") == 0) {
803 cmd = "/usr/etc/bluetooth/bt-mode-master.sh";
804 arg_list[0] = "bt-mode-master.sh";
805 } else if (g_strcmp0(type, "SSP_Debug_Mode") == 0) {
806 cmd = "/usr/etc/bluetooth/bt-set-ssp-debug-mode.sh";
807 arg_list[0] = "bt-set-ssp-debug-mode.sh";
808 arg_list[1] = (char *)arg;
809 } else if (g_strcmp0(type, "RF_Channel") == 0) {
810 cmd = "/usr/etc/bluetooth/bt-enable-rf-channel.sh";
811 arg_list[0] = "bt-enable-rf-channel.sh";
812 arg_list[1] = (char *)arg;
817 BT_DBG("Run %s", cmd);
818 if (__execute_command(cmd, arg_list) < 0)
819 BT_ERR("running script failed");
824 static gboolean __bt_core_recovery_cb(gpointer data)
827 #ifdef TIZEN_FEATURE_BUSACT
828 gboolean is_request_failed = FALSE;
829 static gboolean is_first_failure = TRUE;
834 #ifdef TIZEN_FEATURE_BUSACT
835 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
836 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
838 is_request_failed = TRUE;
841 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
842 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
844 is_request_failed = TRUE;
847 if (is_request_failed == TRUE) {
848 BT_ERR("Recovery is failed.");
849 if (is_first_failure == TRUE) {
850 g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
851 is_first_failure = FALSE;
854 is_first_failure = TRUE;
858 is_first_failure = TRUE;
861 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
862 _bt_core_set_bt_status(BT_RECOVERY_MODE, 0);
863 ret = _bt_enable_adapter();
865 BT_ERR("_bt_enable_adapter() failed");
869 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
870 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 0);
871 ret = _bt_enable_adapter_le();
873 BT_ERR("_bt_enable_adapter_le() failed");
876 is_recovery_mode = FALSE;
883 static gboolean __bt_core_enable_timeout_cb(gpointer data)
885 bt_status_t adapter_status;
886 bt_le_status_t adapter_status_le;
890 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
891 BT_ERR("Set vconf failed");
893 adapter_status = _bt_core_get_status();
894 adapter_status_le = _bt_core_get_le_status();
896 if (adapter_status == BT_DEACTIVATED &&
897 _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
898 _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
899 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
900 _bt_enable_adapter();
903 if (adapter_status_le == BT_LE_DEACTIVATED &&
904 _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
905 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
906 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
907 _bt_enable_adapter_le();
913 static gboolean __bt_core_disable_timeout_cb(gpointer data)
915 bt_status_t adapter_status;
916 bt_le_status_t adapter_status_le;
920 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
921 BT_ERR("Set vconf failed");
923 adapter_status = _bt_core_get_status();
924 adapter_status_le = _bt_core_get_le_status();
926 if (adapter_status == BT_ACTIVATED) {
927 int bt_status_before_mode = 0;
929 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
930 _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
932 #ifdef TIZEN_FEATURE_BUSACT
933 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
935 _bt_disable_adapter();
938 if (adapter_status_le == BT_LE_ACTIVATED) {
939 int bt_le_status_before_mode = 0;
941 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
942 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
944 #ifdef TIZEN_FEATURE_BUSACT
945 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
947 _bt_disable_adapter_le();
953 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value)
960 bundle_add_str(b, key, value);
962 ret = eventsystem_send_system_event(event, b);
964 BT_DBG("eventsystem_send_system_event result: %d", ret);
971 void _bt_core_adapter_added_cb(void)
974 bt_le_status_t le_status;
975 gboolean flight_mode_status;
979 status = _bt_core_get_status();
980 BT_DBG("status : %d", status);
981 le_status = _bt_core_get_le_status();
982 BT_DBG("le_status : %d", le_status);
984 if (status == BT_ACTIVATING)
985 __bt_core_set_status(BT_ACTIVATED);
986 if (le_status == BT_LE_ACTIVATING)
987 __bt_core_set_le_status(BT_LE_ACTIVATED);
989 flight_mode_status = _bt_core_is_flight_mode_enabled();
991 if (flight_mode_status == TRUE && _bt_is_flightmode_request() == TRUE) {
992 _bt_set_flightmode_request(FALSE);
993 g_timeout_add(2000, (GSourceFunc)__bt_core_disable_timeout_cb, NULL);
996 _bt_set_flightmode_request(FALSE);
998 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
999 EVT_VAL_BT_ON) != ES_R_OK)
1000 BT_ERR("Fail to set BT state value");
1002 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
1003 EVT_VAL_BT_LE_ON) != ES_R_OK)
1004 BT_ERR("Fail to set BT LE state value");
1006 _bt_core_terminate();
1009 void _bt_core_adapter_removed_cb(void)
1011 int flight_mode_value = 0;
1012 int power_saving_mode = 0;
1013 gboolean flight_mode_status;
1014 static int timer_id = -1;
1016 BT_DBG("is_recovery_mode: %d", is_recovery_mode);
1018 __bt_core_set_status(BT_DEACTIVATED);
1019 __bt_core_set_le_status(BT_LE_DEACTIVATED);
1020 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1021 BT_ERR("Set vconf failed");
1022 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
1023 BT_ERR("Set vconf failed");
1025 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1026 EVT_VAL_BT_OFF) != ES_R_OK)
1027 BT_ERR("Fail to set value");
1029 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
1030 EVT_VAL_BT_LE_OFF) != ES_R_OK)
1031 BT_ERR("Fail to set value");
1033 if (is_recovery_mode == TRUE) {
1035 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
1039 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value) != 0)
1040 BT_ERR("Fail to get the flight_mode_deactivated value");
1042 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &power_saving_mode) != 0)
1043 BT_ERR("Fail to get the ps_mode_deactivated value");
1045 flight_mode_status = _bt_core_is_flight_mode_enabled();
1047 if (flight_mode_status == FALSE && _bt_is_flightmode_request() == TRUE) {
1048 _bt_set_flightmode_request(FALSE);
1050 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_enable_timeout_cb, NULL);
1053 _bt_set_flightmode_request(FALSE);
1055 if (flight_mode_value == 1 || power_saving_mode == 1) {
1056 BT_DBG("Bt Core not terminated");
1060 _bt_core_terminate();