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>
27 #include <eventsystem.h>
29 #include "bt-core-main.h"
30 #include "bt-core-adapter.h"
31 #include "bt-core-common.h"
32 #include "bt-core-dbus-handler.h"
33 #include "bt-core-noti-handler.h"
35 #define BT_CORE_IDLE_TERM_TIME 200 /* 200ms */
36 #define BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX 50
39 static bt_status_t adapter_status = BT_DEACTIVATED;
40 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
41 static gboolean is_recovery_mode = FALSE;
43 static int bt_status_before[BT_MODE_MAX] = { VCONFKEY_BT_STATUS_OFF, };
44 static int bt_le_status_before[BT_MODE_MAX] = { VCONFKEY_BT_LE_STATUS_OFF, };
46 static void __bt_core_set_status(bt_status_t status)
48 adapter_status = status;
51 bt_status_t _bt_core_get_status(void)
53 return adapter_status;
56 static void __bt_core_set_le_status(bt_le_status_t status)
58 adapter_le_status = status;
61 bt_le_status_t _bt_core_get_le_status(void)
63 return adapter_le_status;
66 int _bt_core_get_bt_status(bt_mode_e mode)
68 return bt_status_before[mode];
71 int _bt_core_get_bt_le_status(bt_mode_e mode)
73 return bt_le_status_before[mode];
76 void _bt_core_set_bt_status(bt_mode_e mode, int status)
78 bt_status_before[mode] = status;
81 void _bt_core_set_bt_le_status(bt_mode_e mode, int status)
83 bt_le_status_before[mode] = status;
86 gboolean _bt_core_is_recovery_mode(void)
88 return is_recovery_mode;
91 static gboolean __bt_core_idle_terminate(gpointer data)
98 gboolean _bt_core_is_flight_mode_enabled(void)
100 #ifdef TIZEN_BT_FLIGHTMODE_ENABLED
101 int isFlightMode = 0;
104 ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &isFlightMode);
106 BT_ERR("vconf_get_bool failed");
114 static int __execute_command(const char *cmd, char *const arg_list[])
124 BT_ERR("fork failed");
130 BT_ERR("fork failed");
131 } else if (pid2 == 0) {
132 execv(cmd, arg_list);
139 BT_DBG("parent : forked[%d]", pid);
140 waitpid(pid, &status, 0);
141 BT_DBG("child is terminated : %d", status);
148 int _bt_enable_adapter(void)
152 bt_le_status_t le_status;
156 status = _bt_core_get_status();
157 if (status != BT_DEACTIVATED) {
158 BT_ERR("Invalid state %d", status);
162 le_status = _bt_core_get_le_status();
163 if (le_status == BT_LE_ACTIVATED) {
164 /* Turn on PSCAN, (ISCAN if needed) */
165 /* Return with 0 for the Enabled response. */
166 __bt_core_set_status(BT_ACTIVATED);
167 BT_INFO("BR/EDR is enabled.");
168 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
172 __bt_core_set_status(BT_ACTIVATING);
174 char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
175 ret = __execute_command("/usr/bin/hciconfig", argv_up);
177 ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
180 BT_ERR("running script failed");
182 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
183 ret = __execute_command("/usr/bin/hciconfig", argv_down);
185 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
187 __bt_core_set_status(BT_DEACTIVATED);
194 int _bt_disable_adapter(void)
197 bt_le_status_t le_status;
199 BT_INFO_C("Disable adapter");
201 le_status = _bt_core_get_le_status();
202 BT_DBG("le_status : %d", le_status);
203 #if 0 /* only the concept of private */
204 if (le_status == BT_LE_ACTIVATED) {
205 /* Turn off PSCAN, (ISCAN if needed) */
206 /* Return with 0 for the Disabled response. */
207 __bt_core_set_status(BT_DEACTIVATED);
208 BT_INFO("BR/EDR is disabled. now LE only mode");
209 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
214 status = _bt_core_get_status();
215 if (status == BT_ACTIVATING) {
216 /* Forcely terminate */
218 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
219 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
221 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
223 BT_ERR("running script failed");
225 _bt_core_terminate();
227 } else if (status != BT_ACTIVATED) {
228 BT_ERR("Invalid state %d", status);
231 __bt_core_set_status(BT_DEACTIVATING);
233 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
234 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
236 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
238 BT_ERR("running script failed");
239 __bt_core_set_status( BT_ACTIVATED);
246 int _bt_enable_adapter_le(void)
252 bt_le_status_t le_status;
253 le_status = _bt_core_get_le_status();
254 retv_if(le_status != BT_LE_DEACTIVATED, -1);
256 status = _bt_core_get_status();
257 if (status == BT_DEACTIVATED) {
258 __bt_core_set_le_status(BT_LE_ACTIVATING);
259 BT_DBG("Activate BT");
261 char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
262 ret = __execute_command("/usr/bin/hciconfig", argv_up);
264 ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
267 BT_ERR("running script failed");
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);
275 __bt_core_set_le_status(BT_LE_DEACTIVATED);
279 __bt_core_set_le_status(BT_LE_ACTIVATED);
280 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
283 ret = _bt_core_start_httpproxy();
285 BT_ERR("_bt_core_start_httpproxy() failed");
292 int _bt_disable_adapter_le(void)
297 bt_le_status_t le_status;
299 le_status = _bt_core_get_le_status();
300 retv_if(le_status == BT_LE_DEACTIVATED, 0);
301 retv_if(le_status == BT_LE_DEACTIVATING, -1);
304 _bt_core_stop_httpproxy();
307 status = _bt_core_get_status();
308 BT_DBG("status : %d", status);
310 if (status == BT_DEACTIVATED) {
311 __bt_core_set_le_status(BT_LE_DEACTIVATING);
313 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
314 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
316 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
318 BT_ERR("running script failed");
319 __bt_core_set_le_status(BT_LE_ACTIVATED);
323 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
326 __bt_core_set_le_status(BT_LE_DEACTIVATED);
332 int _bt_core_service_request_adapter(int service_function)
336 GArray *in_param1 = NULL;
337 GArray *in_param2 = NULL;
338 GArray *in_param3 = NULL;
339 GArray *in_param4 = NULL;
340 GArray *out_param = NULL;
342 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
344 ret = _bt_core_service_request(BT_CORE_SERVICE, service_function,
345 in_param1, in_param2, in_param3, in_param4, &out_param);
347 BT_ERR("_bt_core_service_request_adapter() failed");
349 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
354 static gboolean __bt_core_check_the_adapter_path(GDBusConnection *conn)
357 GDBusProxy *manager_proxy = NULL;
358 GVariant *result = NULL;
359 char *adapter_path = NULL;
364 manager_proxy = g_dbus_proxy_new_sync(conn,
365 G_DBUS_PROXY_FLAGS_NONE, NULL,
368 "org.freedesktop.DBus.ObjectManager",
371 if (!manager_proxy) {
373 BT_ERR("Unable to create proxy: %s", err->message);
376 BT_ERR("Fail to create proxy");
381 result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
382 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
385 BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
388 BT_ERR("Fail to get DefaultAdapter");
393 if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
394 BT_ERR("Incorrect result\n");
398 g_variant_get(result, "(&o)", &adapter_path);
400 if (adapter_path == NULL ||
401 strlen(adapter_path) >= BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX) {
402 BT_ERR("Adapter path is inproper\n");
406 g_variant_unref(result);
407 g_object_unref(manager_proxy);
413 g_variant_unref(result);
416 g_object_unref(manager_proxy);
421 void _bt_core_update_status(void)
423 int bt_status = VCONFKEY_BT_STATUS_OFF;
424 int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
425 gboolean ret = FALSE;
427 ret = __bt_core_check_the_adapter_path(_bt_core_get_gdbus_connection());
428 BT_INFO("check the real status of bt_adapter");
431 __bt_core_set_status(BT_DEACTIVATED);
432 __bt_core_set_le_status(BT_DEACTIVATED);
434 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
435 BT_ERR("no bluetooth device info, so BT was disabled at previous session");
436 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
437 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
439 BT_INFO("bt_status = %d, bt_le_status = %d", bt_status, bt_le_status);
441 if(bt_status & VCONFKEY_BT_STATUS_ON)
442 __bt_core_set_status(BT_ACTIVATED);
443 if(bt_le_status & VCONFKEY_BT_LE_STATUS_ON)
444 __bt_core_set_le_status(BT_ACTIVATED);
448 gboolean _bt_core_enable_adapter(void)
452 _bt_set_flightmode_request(FALSE);
453 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
454 BT_ERR("Set vconf failed");
456 ret = _bt_enable_adapter();
463 gboolean _bt_core_disable_adapter(void)
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 ret = _bt_disable_adapter();
478 gboolean _bt_core_recover_adapter(void)
483 BT_INFO_C("Recover bt adapter");
485 _bt_set_flightmode_request(FALSE);
486 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
487 BT_ERR("Set vconf failed");
489 is_recovery_mode = TRUE;
491 _bt_core_update_status();
493 if (_bt_core_get_status() == BT_ACTIVATED) {
494 _bt_core_set_bt_status(BT_RECOVERY_MODE, 1);
495 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
497 if (_bt_core_get_le_status() == BT_LE_ACTIVATED) {
498 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 1);
499 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
502 ret = _bt_disable_adapter();
504 BT_ERR("_bt_disable_adapter() failed");
505 ret_le = _bt_disable_adapter_le();
507 BT_ERR("_bt_disable_adapter_le() failed");
512 gboolean _bt_core_enable_adapter_le(void)
516 ret = _bt_enable_adapter_le();
523 gboolean _bt_core_disable_adapter_le(void)
529 ret = _bt_disable_adapter_le();
536 gboolean __bt_core_reset_adapter(void)
538 /* Forcely terminate */
539 if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0) {
540 BT_ERR("running script failed");
542 _bt_core_terminate();
547 static gboolean __bt_core_enable_core_timeout_cb(gpointer data)
551 _bt_core_init_vconf_value();
556 gboolean _bt_core_enable_core(void)
560 _bt_core_update_status();
562 g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
568 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
572 char *arg_list[3] = { NULL, NULL, NULL };
574 BT_DBG("Test item : %s", type);
576 if (g_strcmp0(type, "Enable_RF_Test") == 0) {
577 cmd = "/usr/etc/bluetooth/bt-edutm-on.sh";
578 arg_list[0] = "bt-edutm-on.sh";
579 } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
580 cmd = "/usr/etc/bluetooth/bt-edutm-off.sh";
581 arg_list[0] = "bt-edutm-off.sh";
582 } else if (g_strcmp0(type, "Slave_Mode") == 0) {
583 cmd = "/usr/etc/bluetooth/bt-mode-slave.sh";
584 arg_list[0] = "bt-mode-slave.sh";
585 } else if (g_strcmp0(type, "Master_Mode") == 0) {
586 cmd = "/usr/etc/bluetooth/bt-mode-master.sh";
587 arg_list[0] = "bt-mode-master.sh";
588 } else if (g_strcmp0(type, "SSP_Debug_Mode") == 0) {
589 cmd = "/usr/etc/bluetooth/bt-set-ssp-debug-mode.sh";
590 arg_list[0] = "bt-set-ssp-debug-mode.sh";
591 arg_list[1] = (char *)arg;
592 } else if (g_strcmp0(type, "RF_Channel") == 0) {
593 cmd = "/usr/etc/bluetooth/bt-enable-rf-channel.sh";
594 arg_list[0] = "bt-enable-rf-channel.sh";
595 arg_list[1] = (char *)arg;
597 _bt_core_terminate();
601 BT_DBG("Run %s", cmd);
602 if (__execute_command(cmd, arg_list) < 0) {
603 BT_ERR("running script failed");
606 _bt_core_terminate();
610 static gboolean __bt_core_recovery_cb(gpointer data)
613 gboolean is_request_failed = FALSE;
614 static gboolean is_first_failure = TRUE;
618 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
619 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
621 is_request_failed = TRUE;
624 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
625 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
627 is_request_failed = TRUE;
630 if (is_request_failed == TRUE) {
631 BT_ERR("Recovery is failed.");
632 if (is_first_failure == TRUE) {
633 g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
634 is_first_failure = FALSE;
637 is_first_failure = TRUE;
641 is_first_failure = TRUE;
643 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
644 _bt_core_set_bt_status(BT_RECOVERY_MODE, 0);
645 ret = _bt_enable_adapter();
647 BT_ERR("_bt_enable_adapter() failed");
649 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
650 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 0);
651 ret = _bt_enable_adapter_le();
653 BT_ERR("_bt_enable_adapter_le() failed");
656 is_recovery_mode = FALSE;
663 static gboolean __bt_core_enable_timeout_cb(gpointer data)
665 bt_status_t adapter_status;
666 bt_le_status_t adapter_status_le;
670 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
671 BT_ERR("Set vconf failed");
673 adapter_status = _bt_core_get_status();
674 adapter_status_le = _bt_core_get_le_status();
676 if (adapter_status == BT_DEACTIVATED &&
677 _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
678 _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
679 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
680 _bt_enable_adapter();
683 if (adapter_status_le == BT_LE_DEACTIVATED &&
684 _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
685 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
686 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
687 _bt_enable_adapter_le();
693 static gboolean __bt_core_disable_timeout_cb(gpointer data)
695 bt_status_t adapter_status;
696 bt_le_status_t adapter_status_le;
700 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
701 BT_ERR("Set vconf failed");
703 adapter_status = _bt_core_get_status();
704 adapter_status_le = _bt_core_get_le_status();
706 if (adapter_status == BT_ACTIVATED) {
707 int bt_status_before_mode = 0;
709 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
710 _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
712 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
713 _bt_disable_adapter();
716 if (adapter_status_le == BT_LE_ACTIVATED) {
717 int bt_le_status_before_mode = 0;
719 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
720 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
722 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
723 _bt_disable_adapter_le();
729 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value)
736 bundle_add_str(b, key, value);
738 ret = eventsystem_send_system_event(event, b);
740 BT_DBG("eventsystem_send_system_event result: %d", ret);
747 void _bt_core_adapter_added_cb(void)
750 bt_le_status_t le_status;
751 gboolean flight_mode_status;
755 status = _bt_core_get_status();
756 BT_DBG("status : %d", status);
757 le_status = _bt_core_get_le_status();
758 BT_DBG("le_status : %d", le_status);
760 if (status == BT_ACTIVATING)
761 __bt_core_set_status(BT_ACTIVATED);
762 if (le_status == BT_LE_ACTIVATING)
763 __bt_core_set_le_status(BT_LE_ACTIVATED);
765 flight_mode_status = _bt_core_is_flight_mode_enabled();
767 if (flight_mode_status == TRUE && _bt_is_flightmode_request() == TRUE) {
768 _bt_set_flightmode_request(FALSE);
769 g_timeout_add(2000, (GSourceFunc)__bt_core_disable_timeout_cb, NULL);
772 _bt_set_flightmode_request(FALSE);
773 _bt_core_terminate();
776 void _bt_core_adapter_removed_cb(void)
778 int flight_mode_value = 0;
779 int power_saving_mode = 0;
780 gboolean flight_mode_status;
781 static int timer_id = -1;
785 __bt_core_set_status(BT_DEACTIVATED);
786 __bt_core_set_le_status(BT_LE_DEACTIVATED);
787 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
788 BT_ERR("Set vconf failed");
789 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
790 BT_ERR("Set vconf failed");
792 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
793 EVT_VAL_BT_OFF) != ES_R_OK)
794 BT_ERR("Fail to set value");
796 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
797 EVT_VAL_BT_LE_OFF) != ES_R_OK)
798 BT_ERR("Fail to set value");
800 if (is_recovery_mode == TRUE)
803 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
807 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value) != 0)
808 BT_ERR("Fail to get the flight_mode_deactivated value");
810 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &power_saving_mode) != 0)
811 BT_ERR("Fail to get the ps_mode_deactivated value");
813 flight_mode_status = _bt_core_is_flight_mode_enabled();
815 if (flight_mode_status == FALSE && _bt_is_flightmode_request() == TRUE) {
816 _bt_set_flightmode_request(FALSE);
818 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_enable_timeout_cb, NULL);
821 _bt_set_flightmode_request(FALSE);
823 if (flight_mode_value == 1 || power_saving_mode == 1){
824 BT_DBG("Bt Core not terminated");
828 _bt_core_terminate();