4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 * Girishashok Joshi <girish.joshi@samsung.com>
8 * Chanyeol Park <chanyeol.park@samsung.com>
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
25 #include <vconf-keys.h>
28 #include <eventsystem.h>
31 #include "bt-core-main.h"
32 #include "bt-core-adapter.h"
33 #include "bt-core-common.h"
34 #include "bt-core-dbus-handler.h"
35 #include "bt-core-noti-handler.h"
37 #define BT_CORE_IDLE_TERM_TIME 200 /* 200ms */
38 #define BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX 50
41 static bt_status_t adapter_status = BT_DEACTIVATED;
42 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
43 static gboolean is_recovery_mode = FALSE;
45 static int bt_status_before[BT_MODE_MAX] = { VCONFKEY_BT_STATUS_OFF, };
46 static int bt_le_status_before[BT_MODE_MAX] = { 0, };
48 static DBusGConnection *conn = NULL;
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 #ifdef TIZEN_BT_FLIGHTMODE_ENABLED
105 int isFlightMode = 0;
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)
156 bt_le_status_t le_status;
160 status = _bt_core_get_status();
161 if (status != BT_DEACTIVATED) {
162 BT_ERR("Invalid state %d", status);
166 le_status = _bt_core_get_le_status();
167 if (le_status == BT_LE_ACTIVATED) {
168 /* Turn on PSCAN, (ISCAN if needed) */
169 /* Return with 0 for the Enabled response. */
170 __bt_core_set_status(BT_ACTIVATED);
171 BT_INFO("BR/EDR is enabled.");
172 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
176 __bt_core_set_status(BT_ACTIVATING);
178 char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
179 ret = __execute_command("/usr/bin/hciconfig", argv_up);
181 ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
184 BT_ERR("running script failed");
185 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
186 __bt_core_set_status(BT_DEACTIVATED);
193 int _bt_disable_adapter(void)
196 bt_le_status_t le_status;
198 BT_INFO_C("Disable adapter");
200 le_status = _bt_core_get_le_status();
201 BT_DBG("le_status : %d", le_status);
202 if (le_status == BT_LE_ACTIVATED) {
203 /* Turn off PSCAN, (ISCAN if needed) */
204 /* Return with 0 for the Disabled response. */
205 __bt_core_set_status(BT_DEACTIVATED);
206 BT_INFO("BR/EDR is disabled. now LE only mode");
207 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
211 status = _bt_core_get_status();
212 if (status == BT_ACTIVATING) {
213 /* Forcely terminate */
215 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
216 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
218 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
220 BT_ERR("running script failed");
222 _bt_core_terminate();
224 } else if (status != BT_ACTIVATED) {
225 BT_ERR("Invalid state %d", status);
228 __bt_core_set_status(BT_DEACTIVATING);
230 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
231 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
233 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
235 BT_ERR("running script failed");
236 __bt_core_set_status( BT_ACTIVATED);
243 int _bt_enable_adapter_le(void)
249 bt_le_status_t le_status;
250 le_status = _bt_core_get_le_status();
251 retv_if(le_status != BT_LE_DEACTIVATED, -1);
253 status = _bt_core_get_status();
254 if (status == BT_DEACTIVATED) {
255 __bt_core_set_le_status(BT_LE_ACTIVATING);
256 BT_DBG("Activate BT");
258 char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
259 ret = __execute_command("/usr/bin/hciconfig", argv_up);
261 ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
264 BT_ERR("running script failed");
265 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh &", NULL);
266 __bt_core_set_status(BT_DEACTIVATED);
267 __bt_core_set_le_status(BT_LE_DEACTIVATED);
271 __bt_core_set_le_status(BT_LE_ACTIVATED);
272 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
278 int _bt_disable_adapter_le(void)
283 bt_le_status_t le_status;
285 le_status = _bt_core_get_le_status();
286 retv_if(le_status == BT_LE_DEACTIVATED, 0);
287 retv_if(le_status == BT_LE_DEACTIVATING, -1);
289 status = _bt_core_get_status();
290 BT_DBG("status : %d", status);
292 if (status == BT_DEACTIVATED) {
293 __bt_core_set_le_status(BT_LE_DEACTIVATING);
295 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
296 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
298 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
300 BT_ERR("running script failed");
301 __bt_core_set_le_status(BT_LE_ACTIVATED);
305 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
308 __bt_core_set_le_status(BT_LE_DEACTIVATED);
314 int _bt_core_service_request_adapter(int service_function)
318 GArray *in_param1 = NULL;
319 GArray *in_param2 = NULL;
320 GArray *in_param3 = NULL;
321 GArray *in_param4 = NULL;
322 GArray *out_param = NULL;
324 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
326 ret = _bt_core_service_request(BT_CORE_SERVICE, service_function,
327 in_param1, in_param2, in_param3, in_param4, &out_param);
329 BT_ERR("_bt_core_service_request_adapter() failed");
331 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
336 static gboolean __bt_core_check_the_adapter_path(GDBusConnection *conn)
339 GDBusProxy *manager_proxy = NULL;
340 GVariant *result = NULL;
341 char *adapter_path = NULL;
346 manager_proxy = g_dbus_proxy_new_sync(conn,
347 G_DBUS_PROXY_FLAGS_NONE, NULL,
350 "org.freedesktop.DBus.ObjectManager",
353 if (!manager_proxy) {
355 BT_ERR("Unable to create proxy: %s", err->message);
358 BT_ERR("Fail to create proxy");
363 result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
364 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
367 BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
370 BT_ERR("Fail to get DefaultAdapter");
375 if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
376 BT_ERR("Incorrect result\n");
380 g_variant_get(result, "(&o)", &adapter_path);
382 if (adapter_path == NULL ||
383 strlen(adapter_path) >= BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX) {
384 BT_ERR("Adapter path is inproper\n");
388 g_variant_unref(result);
389 g_object_unref(manager_proxy);
395 g_variant_unref(result);
398 g_object_unref(manager_proxy);
403 void _bt_core_update_status(void)
405 int bt_status = VCONFKEY_BT_STATUS_OFF;
406 int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
407 gboolean ret = FALSE;
409 ret = __bt_core_check_the_adapter_path(_bt_core_get_gdbus_connection());
410 BT_INFO("check the real status of bt_adapter");
413 __bt_core_set_status(BT_DEACTIVATED);
414 __bt_core_set_le_status(BT_DEACTIVATED);
416 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
417 BT_ERR("no bluetooth device info, so BT was disabled at previous session");
418 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
419 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
421 BT_INFO("bt_status = %d, bt_le_status = %d", bt_status, bt_le_status);
423 if(bt_status == VCONFKEY_BT_STATUS_ON)
424 __bt_core_set_status(BT_ACTIVATED);
425 if(bt_le_status == VCONFKEY_BT_LE_STATUS_ON)
426 __bt_core_set_le_status(BT_ACTIVATED);
430 gboolean _bt_core_enable_adapter(void)
434 _bt_set_flightmode_request(FALSE);
435 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
436 BT_ERR("Set vconf failed");
438 ret = _bt_enable_adapter();
445 gboolean _bt_core_disable_adapter(void)
449 _bt_set_flightmode_request(FALSE);
450 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
451 BT_ERR("Set vconf failed");
453 ret = _bt_disable_adapter();
460 gboolean _bt_core_recover_adapter(void)
465 BT_INFO_C("Recover bt adapter");
467 _bt_set_flightmode_request(FALSE);
468 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
469 BT_ERR("Set vconf failed");
471 is_recovery_mode = TRUE;
473 _bt_core_update_status();
475 if (_bt_core_get_status() == BT_ACTIVATED) {
476 _bt_core_set_bt_status(BT_RECOVERY_MODE, 1);
477 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
479 if (_bt_core_get_le_status() == BT_LE_ACTIVATED) {
480 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 1);
481 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
484 ret = _bt_disable_adapter();
486 BT_ERR("_bt_disable_adapter() failed");
487 ret_le = _bt_disable_adapter_le();
489 BT_ERR("_bt_disable_adapter_le() failed");
494 gboolean _bt_core_enable_adapter_le(void)
498 ret = _bt_enable_adapter_le();
505 gboolean _bt_core_disable_adapter_le(void)
511 ret = _bt_disable_adapter_le();
518 gboolean __bt_core_reset_adapter(void)
520 /* Forcely terminate */
521 if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0) {
522 BT_ERR("running script failed");
524 _bt_core_terminate();
529 static gboolean __bt_core_enable_core_timeout_cb(gpointer data)
533 _bt_core_init_vconf_value();
538 gboolean _bt_core_enable_core(void)
542 _bt_core_update_status();
544 g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
550 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
554 char *arg_list[3] = { NULL, NULL, NULL };
556 BT_DBG("Test item : %s", type);
558 if (g_strcmp0(type, "Enable_RF_Test") == 0) {
559 cmd = "/usr/etc/bluetooth/bt-edutm-on.sh";
560 arg_list[0] = "bt-edutm-on.sh";
561 } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
562 cmd = "/usr/etc/bluetooth/bt-edutm-off.sh";
563 arg_list[0] = "bt-edutm-off.sh";
564 } else if (g_strcmp0(type, "Slave_Mode") == 0) {
565 cmd = "/usr/etc/bluetooth/bt-mode-slave.sh";
566 arg_list[0] = "bt-mode-slave.sh";
567 } else if (g_strcmp0(type, "Master_Mode") == 0) {
568 cmd = "/usr/etc/bluetooth/bt-mode-master.sh";
569 arg_list[0] = "bt-mode-master.sh";
570 } else if (g_strcmp0(type, "SSP_Debug_Mode") == 0) {
571 cmd = "/usr/etc/bluetooth/bt-set-ssp-debug-mode.sh";
572 arg_list[0] = "bt-set-ssp-debug-mode.sh";
573 arg_list[1] = (char *)arg;
574 } else if (g_strcmp0(type, "RF_Channel") == 0) {
575 cmd = "/usr/etc/bluetooth/bt-enable-rf-channel.sh";
576 arg_list[0] = "bt-enable-rf-channel.sh";
577 arg_list[1] = (char *)arg;
579 _bt_core_terminate();
583 BT_DBG("Run %s", cmd);
584 if (__execute_command(cmd, arg_list) < 0) {
585 BT_ERR("running script failed");
588 _bt_core_terminate();
592 static gboolean __bt_core_recovery_cb(gpointer data)
595 gboolean is_request_failed = FALSE;
596 static gboolean is_first_failure = TRUE;
600 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
601 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
603 is_request_failed = TRUE;
606 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
607 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
609 is_request_failed = TRUE;
612 if (is_request_failed == TRUE) {
613 BT_ERR("Recovery is failed.");
614 if (is_first_failure == TRUE) {
615 g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
616 is_first_failure = FALSE;
619 is_first_failure = TRUE;
623 is_first_failure = TRUE;
625 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
626 _bt_core_set_bt_status(BT_RECOVERY_MODE, 0);
627 ret = _bt_enable_adapter();
629 BT_ERR("_bt_enable_adapter() failed");
631 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
632 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 0);
633 ret = _bt_enable_adapter_le();
635 BT_ERR("_bt_enable_adapter_le() failed");
638 is_recovery_mode = FALSE;
645 static gboolean __bt_core_enable_timeout_cb(gpointer data)
647 bt_status_t adapter_status;
648 bt_le_status_t adapter_status_le;
652 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
653 BT_ERR("Set vconf failed");
655 adapter_status = _bt_core_get_status();
656 adapter_status_le = _bt_core_get_le_status();
658 if (adapter_status == BT_DEACTIVATED &&
659 _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
660 _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
661 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
662 _bt_enable_adapter();
665 if (adapter_status_le == BT_LE_DEACTIVATED &&
666 _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
667 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
668 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
669 _bt_enable_adapter_le();
675 static gboolean __bt_core_disable_timeout_cb(gpointer data)
677 bt_status_t adapter_status;
678 bt_le_status_t adapter_status_le;
682 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
683 BT_ERR("Set vconf failed");
685 adapter_status = _bt_core_get_status();
686 adapter_status_le = _bt_core_get_le_status();
688 if (adapter_status == BT_ACTIVATED) {
689 int bt_status_before_mode = 0;
691 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
692 _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
694 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
695 _bt_disable_adapter();
698 if (adapter_status_le == BT_LE_ACTIVATED) {
699 int bt_le_status_before_mode = 0;
701 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
702 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
704 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
705 _bt_disable_adapter_le();
711 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value)
718 bundle_add_str(b, key, value);
720 ret = eventsystem_request_sending_system_event(event, b);
722 BT_DBG("request_sending_system_event result: %d", ret);
729 void _bt_core_adapter_added_cb(void)
732 bt_le_status_t le_status;
733 gboolean flight_mode_status;
737 status = _bt_core_get_status();
738 BT_DBG("status : %d", status);
739 le_status = _bt_core_get_le_status();
740 BT_DBG("le_status : %d", le_status);
742 if (status == BT_ACTIVATING)
743 __bt_core_set_status(BT_ACTIVATED);
744 if (le_status == BT_LE_ACTIVATING)
745 __bt_core_set_le_status(BT_LE_ACTIVATED);
747 flight_mode_status = _bt_core_is_flight_mode_enabled();
749 if (flight_mode_status == TRUE && _bt_is_flightmode_request() == TRUE) {
750 _bt_set_flightmode_request(FALSE);
751 g_timeout_add(2000, (GSourceFunc)__bt_core_disable_timeout_cb, NULL);
754 _bt_set_flightmode_request(FALSE);
755 _bt_core_terminate();
758 void _bt_core_adapter_removed_cb(void)
760 int flight_mode_value = 0;
761 int power_saving_mode = 0;
762 gboolean flight_mode_status;
763 static int timer_id = -1;
767 __bt_core_set_status(BT_DEACTIVATED);
768 __bt_core_set_le_status(BT_LE_DEACTIVATED);
769 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
770 BT_ERR("Set vconf failed");
772 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
773 BT_ERR("Set vconf failed");
776 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
777 EVT_VAL_BT_OFF) != ES_R_OK)
778 BT_ERR("Fail to set value");
780 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
781 EVT_VAL_BT_LE_OFF) != ES_R_OK)
782 BT_ERR("Fail to set value");
784 if (is_recovery_mode == TRUE)
787 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
791 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value) != 0)
792 BT_ERR("Fail to get the flight_mode_deactivated value");
794 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &power_saving_mode) != 0)
795 BT_ERR("Fail to get the ps_mode_deactivated value");
797 flight_mode_status = _bt_core_is_flight_mode_enabled();
799 if (flight_mode_status == FALSE && _bt_is_flightmode_request() == TRUE) {
800 _bt_set_flightmode_request(FALSE);
802 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_enable_timeout_cb, NULL);
805 _bt_set_flightmode_request(FALSE);
807 if (flight_mode_value == 1 || power_saving_mode == 1){
808 BT_DBG("Bt Core not terminated");
812 _bt_core_terminate();