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>
21 #include <eventsystem.h>
23 #include "bt-core-main.h"
24 #include "bt-core-adapter.h"
25 #include "bt-core-common.h"
26 #include "bt-core-dbus-handler.h"
27 #include "bt-core-noti-handler.h"
29 #define BT_CORE_IDLE_TERM_TIME 200 /* 200ms */
30 #define BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX 50
33 static bt_status_t adapter_status = BT_DEACTIVATED;
34 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
35 static gboolean is_recovery_mode = FALSE;
37 static int bt_status_before[BT_MODE_MAX] = { VCONFKEY_BT_STATUS_OFF, };
38 static int bt_le_status_before[BT_MODE_MAX] = { VCONFKEY_BT_LE_STATUS_OFF, };
40 static void __bt_core_set_status(bt_status_t status)
42 adapter_status = status;
45 bt_status_t _bt_core_get_status(void)
47 return adapter_status;
50 static void __bt_core_set_le_status(bt_le_status_t status)
52 adapter_le_status = status;
55 bt_le_status_t _bt_core_get_le_status(void)
57 return adapter_le_status;
60 int _bt_core_get_bt_status(bt_mode_e mode)
62 return bt_status_before[mode];
65 int _bt_core_get_bt_le_status(bt_mode_e mode)
67 return bt_le_status_before[mode];
70 void _bt_core_set_bt_status(bt_mode_e mode, int status)
72 bt_status_before[mode] = status;
75 void _bt_core_set_bt_le_status(bt_mode_e mode, int status)
77 bt_le_status_before[mode] = status;
80 gboolean _bt_core_is_recovery_mode(void)
82 return is_recovery_mode;
85 static gboolean __bt_core_idle_terminate(gpointer data)
92 gboolean _bt_core_is_flight_mode_enabled(void)
94 #ifdef TIZEN_FEATURE_FLIGHTMODE_ENABLED
98 ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &isFlightMode);
100 BT_ERR("vconf_get_bool failed");
108 static int __execute_command(const char *cmd, char *const arg_list[])
118 BT_ERR("fork failed");
124 BT_ERR("fork failed");
125 } else if (pid2 == 0) {
126 execv(cmd, arg_list);
133 BT_DBG("parent : forked[%d]", pid);
134 waitpid(pid, &status, 0);
135 BT_DBG("child is terminated : %d", status);
142 int _bt_enable_adapter(void)
147 bt_le_status_t le_status;
151 status = _bt_core_get_status();
152 if (status != BT_DEACTIVATED) {
153 BT_ERR("Invalid state %d", status);
154 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
158 #if 0 /* only the concept of private */
159 le_status = _bt_core_get_le_status();
160 if (le_status == BT_LE_ACTIVATED) {
161 /* Turn on PSCAN, (ISCAN if needed) */
162 /* Return with 0 for the Enabled response. */
163 __bt_core_set_status(BT_ACTIVATED);
164 BT_INFO("BR/EDR is enabled.");
165 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
170 __bt_core_set_status(BT_ACTIVATING);
172 char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
173 ret = __execute_command("/usr/bin/hciconfig", argv_up);
175 ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
178 BT_ERR("running script failed");
180 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
181 ret = __execute_command("/usr/bin/hciconfig", argv_down);
183 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
185 __bt_core_set_status(BT_DEACTIVATED);
192 int _bt_disable_adapter(void)
196 bt_le_status_t le_status;
199 BT_INFO_C("Disable adapter");
201 #if 0 /* only the concept of private */
202 le_status = _bt_core_get_le_status();
203 BT_DBG("le_status : %d", le_status);
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);
229 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
232 __bt_core_set_status(BT_DEACTIVATING);
234 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
235 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
237 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
239 BT_ERR("running script failed");
240 __bt_core_set_status(BT_ACTIVATED);
247 int _bt_enable_adapter_le(void)
253 bt_le_status_t le_status;
254 le_status = _bt_core_get_le_status();
255 retv_if(le_status != BT_LE_DEACTIVATED, -1);
257 status = _bt_core_get_status();
258 if (status == BT_DEACTIVATED) {
259 __bt_core_set_le_status(BT_LE_ACTIVATING);
260 BT_DBG("Activate BT");
262 char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
263 ret = __execute_command("/usr/bin/hciconfig", argv_up);
265 ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
268 BT_ERR("running script failed");
270 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
271 ret = __execute_command("/usr/bin/hciconfig", argv_down);
273 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh &", NULL);
275 __bt_core_set_status(BT_DEACTIVATED);
276 __bt_core_set_le_status(BT_LE_DEACTIVATED);
280 __bt_core_set_le_status(BT_LE_ACTIVATED);
281 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
284 ret = _bt_core_start_httpproxy();
286 BT_ERR("_bt_core_start_httpproxy() failed");
293 int _bt_disable_adapter_le(void)
298 bt_le_status_t le_status;
300 le_status = _bt_core_get_le_status();
301 retv_if(le_status == BT_LE_DEACTIVATED, 0);
302 retv_if(le_status == BT_LE_DEACTIVATING, -1);
305 _bt_core_stop_httpproxy();
308 status = _bt_core_get_status();
309 BT_DBG("status : %d", status);
311 if (status == BT_DEACTIVATED) {
312 __bt_core_set_le_status(BT_LE_DEACTIVATING);
314 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
315 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
317 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
319 BT_ERR("running script failed");
320 __bt_core_set_le_status(BT_LE_ACTIVATED);
324 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
327 __bt_core_set_le_status(BT_LE_DEACTIVATED);
333 int _bt_core_service_request_adapter(int service_function)
337 GArray *in_param1 = NULL;
338 GArray *in_param2 = NULL;
339 GArray *in_param3 = NULL;
340 GArray *in_param4 = NULL;
341 GArray *out_param = NULL;
343 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
345 ret = _bt_core_service_request(BT_CORE_SERVICE, service_function,
346 in_param1, in_param2, in_param3, in_param4, &out_param);
348 BT_ERR("_bt_core_service_request_adapter() failed");
350 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
355 static gboolean __bt_core_check_the_adapter_path(GDBusConnection *conn)
358 GDBusProxy *manager_proxy = NULL;
359 GVariant *result = NULL;
360 char *adapter_path = NULL;
365 manager_proxy = g_dbus_proxy_new_sync(conn,
366 G_DBUS_PROXY_FLAGS_NONE, NULL,
369 "org.freedesktop.DBus.ObjectManager",
372 if (!manager_proxy) {
374 BT_ERR("Unable to create proxy: %s", err->message);
377 BT_ERR("Fail to create proxy");
382 result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
383 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
386 BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
389 BT_ERR("Fail to get DefaultAdapter");
394 if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
395 BT_ERR("Incorrect result\n");
399 g_variant_get(result, "(&o)", &adapter_path);
401 if (adapter_path == NULL ||
402 strlen(adapter_path) >= BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX) {
403 BT_ERR("Adapter path is inproper\n");
407 g_variant_unref(result);
408 g_object_unref(manager_proxy);
414 g_variant_unref(result);
417 g_object_unref(manager_proxy);
422 void _bt_core_update_status(void)
424 int bt_status = VCONFKEY_BT_STATUS_OFF;
425 int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
426 gboolean ret = FALSE;
428 ret = __bt_core_check_the_adapter_path(_bt_core_get_gdbus_connection());
429 BT_INFO("check the real status of bt_adapter");
432 __bt_core_set_status(BT_DEACTIVATED);
433 __bt_core_set_le_status(BT_DEACTIVATED);
435 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
436 BT_ERR("no bluetooth device info, so BT was disabled at previous session");
437 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
438 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
440 BT_INFO("bt_status = %d, bt_le_status = %d", bt_status, bt_le_status);
442 if (bt_status & VCONFKEY_BT_STATUS_ON)
443 __bt_core_set_status(BT_ACTIVATED);
444 if (bt_le_status & VCONFKEY_BT_LE_STATUS_ON)
445 __bt_core_set_le_status(BT_ACTIVATED);
449 gboolean _bt_core_enable_adapter(void)
453 _bt_set_flightmode_request(FALSE);
454 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
455 BT_ERR("Set vconf failed");
457 ret = _bt_enable_adapter();
464 gboolean _bt_core_disable_adapter(void)
468 _bt_set_flightmode_request(FALSE);
469 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
470 BT_ERR("Set vconf failed");
472 ret = _bt_disable_adapter();
479 gboolean _bt_core_recover_adapter(void)
484 BT_INFO_C("Recover bt adapter");
486 _bt_set_flightmode_request(FALSE);
487 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
488 BT_ERR("Set vconf failed");
490 is_recovery_mode = TRUE;
492 _bt_core_update_status();
494 if (_bt_core_get_status() == BT_ACTIVATED) {
495 _bt_core_set_bt_status(BT_RECOVERY_MODE, 1);
496 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
498 if (_bt_core_get_le_status() == BT_LE_ACTIVATED) {
499 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 1);
500 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
503 ret = _bt_disable_adapter();
505 BT_ERR("_bt_disable_adapter() failed");
506 ret_le = _bt_disable_adapter_le();
508 BT_ERR("_bt_disable_adapter_le() failed");
513 gboolean _bt_core_enable_adapter_le(void)
517 ret = _bt_enable_adapter_le();
524 gboolean _bt_core_disable_adapter_le(void)
530 ret = _bt_disable_adapter_le();
537 gboolean __bt_core_reset_adapter(void)
539 /* Forcely terminate */
540 if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0) {
541 BT_ERR("running script failed");
543 _bt_core_terminate();
548 static gboolean __bt_core_enable_core_timeout_cb(gpointer data)
552 _bt_core_init_vconf_value();
557 gboolean _bt_core_enable_core(void)
561 _bt_core_update_status();
563 g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
569 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
573 char *arg_list[3] = { NULL, NULL, NULL };
575 BT_DBG("Test item : %s", type);
577 if (g_strcmp0(type, "Enable_RF_Test") == 0) {
578 cmd = "/usr/etc/bluetooth/bt-edutm-on.sh";
579 arg_list[0] = "bt-edutm-on.sh";
580 } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
581 cmd = "/usr/etc/bluetooth/bt-edutm-off.sh";
582 arg_list[0] = "bt-edutm-off.sh";
583 } else if (g_strcmp0(type, "Slave_Mode") == 0) {
584 cmd = "/usr/etc/bluetooth/bt-mode-slave.sh";
585 arg_list[0] = "bt-mode-slave.sh";
586 } else if (g_strcmp0(type, "Master_Mode") == 0) {
587 cmd = "/usr/etc/bluetooth/bt-mode-master.sh";
588 arg_list[0] = "bt-mode-master.sh";
589 } else if (g_strcmp0(type, "SSP_Debug_Mode") == 0) {
590 cmd = "/usr/etc/bluetooth/bt-set-ssp-debug-mode.sh";
591 arg_list[0] = "bt-set-ssp-debug-mode.sh";
592 arg_list[1] = (char *)arg;
593 } else if (g_strcmp0(type, "RF_Channel") == 0) {
594 cmd = "/usr/etc/bluetooth/bt-enable-rf-channel.sh";
595 arg_list[0] = "bt-enable-rf-channel.sh";
596 arg_list[1] = (char *)arg;
598 _bt_core_terminate();
602 BT_DBG("Run %s", cmd);
603 if (__execute_command(cmd, arg_list) < 0) {
604 BT_ERR("running script failed");
607 _bt_core_terminate();
611 static gboolean __bt_core_recovery_cb(gpointer data)
614 gboolean is_request_failed = FALSE;
615 static gboolean is_first_failure = TRUE;
619 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
620 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
622 is_request_failed = TRUE;
625 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
626 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
628 is_request_failed = TRUE;
631 if (is_request_failed == TRUE) {
632 BT_ERR("Recovery is failed.");
633 if (is_first_failure == TRUE) {
634 g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
635 is_first_failure = FALSE;
638 is_first_failure = TRUE;
642 is_first_failure = TRUE;
644 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
645 _bt_core_set_bt_status(BT_RECOVERY_MODE, 0);
646 ret = _bt_enable_adapter();
648 BT_ERR("_bt_enable_adapter() failed");
650 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
651 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 0);
652 ret = _bt_enable_adapter_le();
654 BT_ERR("_bt_enable_adapter_le() failed");
657 is_recovery_mode = FALSE;
664 static gboolean __bt_core_enable_timeout_cb(gpointer data)
666 bt_status_t adapter_status;
667 bt_le_status_t adapter_status_le;
671 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
672 BT_ERR("Set vconf failed");
674 adapter_status = _bt_core_get_status();
675 adapter_status_le = _bt_core_get_le_status();
677 if (adapter_status == BT_DEACTIVATED &&
678 _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
679 _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
680 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
681 _bt_enable_adapter();
684 if (adapter_status_le == BT_LE_DEACTIVATED &&
685 _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
686 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
687 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
688 _bt_enable_adapter_le();
694 static gboolean __bt_core_disable_timeout_cb(gpointer data)
696 bt_status_t adapter_status;
697 bt_le_status_t adapter_status_le;
701 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
702 BT_ERR("Set vconf failed");
704 adapter_status = _bt_core_get_status();
705 adapter_status_le = _bt_core_get_le_status();
707 if (adapter_status == BT_ACTIVATED) {
708 int bt_status_before_mode = 0;
710 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
711 _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
713 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
714 _bt_disable_adapter();
717 if (adapter_status_le == BT_LE_ACTIVATED) {
718 int bt_le_status_before_mode = 0;
720 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
721 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
723 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
724 _bt_disable_adapter_le();
730 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value)
737 bundle_add_str(b, key, value);
739 ret = eventsystem_send_system_event(event, b);
741 BT_DBG("eventsystem_send_system_event result: %d", ret);
748 void _bt_core_adapter_added_cb(void)
751 bt_le_status_t le_status;
752 gboolean flight_mode_status;
756 status = _bt_core_get_status();
757 BT_DBG("status : %d", status);
758 le_status = _bt_core_get_le_status();
759 BT_DBG("le_status : %d", le_status);
761 if (status == BT_ACTIVATING)
762 __bt_core_set_status(BT_ACTIVATED);
763 if (le_status == BT_LE_ACTIVATING)
764 __bt_core_set_le_status(BT_LE_ACTIVATED);
766 flight_mode_status = _bt_core_is_flight_mode_enabled();
768 if (flight_mode_status == TRUE && _bt_is_flightmode_request() == TRUE) {
769 _bt_set_flightmode_request(FALSE);
770 g_timeout_add(2000, (GSourceFunc)__bt_core_disable_timeout_cb, NULL);
773 _bt_set_flightmode_request(FALSE);
774 _bt_core_terminate();
777 void _bt_core_adapter_removed_cb(void)
779 int flight_mode_value = 0;
780 int power_saving_mode = 0;
781 gboolean flight_mode_status;
782 static int timer_id = -1;
786 __bt_core_set_status(BT_DEACTIVATED);
787 __bt_core_set_le_status(BT_LE_DEACTIVATED);
788 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
789 BT_ERR("Set vconf failed");
790 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
791 BT_ERR("Set vconf failed");
793 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
794 EVT_VAL_BT_OFF) != ES_R_OK)
795 BT_ERR("Fail to set value");
797 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
798 EVT_VAL_BT_LE_OFF) != ES_R_OK)
799 BT_ERR("Fail to set value");
801 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();