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)
146 bt_le_status_t le_status;
150 status = _bt_core_get_status();
151 if (status != BT_DEACTIVATED) {
152 BT_ERR("Invalid state %d", status);
156 le_status = _bt_core_get_le_status();
157 if (le_status == BT_LE_ACTIVATED) {
158 /* Turn on PSCAN, (ISCAN if needed) */
159 /* Return with 0 for the Enabled response. */
160 __bt_core_set_status(BT_ACTIVATED);
161 BT_INFO("BR/EDR is enabled.");
162 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
166 __bt_core_set_status(BT_ACTIVATING);
168 char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
169 ret = __execute_command("/usr/bin/hciconfig", argv_up);
171 ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
174 BT_ERR("running script failed");
176 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
177 ret = __execute_command("/usr/bin/hciconfig", argv_down);
179 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
181 __bt_core_set_status(BT_DEACTIVATED);
188 int _bt_disable_adapter(void)
191 bt_le_status_t le_status;
193 BT_INFO_C("Disable adapter");
195 le_status = _bt_core_get_le_status();
196 BT_DBG("le_status : %d", le_status);
197 #if 0 /* only the concept of private */
198 if (le_status == BT_LE_ACTIVATED) {
199 /* Turn off PSCAN, (ISCAN if needed) */
200 /* Return with 0 for the Disabled response. */
201 __bt_core_set_status(BT_DEACTIVATED);
202 BT_INFO("BR/EDR is disabled. now LE only mode");
203 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
208 status = _bt_core_get_status();
209 if (status == BT_ACTIVATING) {
210 /* Forcely terminate */
212 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
213 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
215 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
217 BT_ERR("running script failed");
219 _bt_core_terminate();
221 } else if (status != BT_ACTIVATED) {
222 BT_ERR("Invalid state %d", status);
225 __bt_core_set_status(BT_DEACTIVATING);
227 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
228 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
230 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
232 BT_ERR("running script failed");
233 __bt_core_set_status(BT_ACTIVATED);
240 int _bt_enable_adapter_le(void)
246 bt_le_status_t le_status;
247 le_status = _bt_core_get_le_status();
248 retv_if(le_status != BT_LE_DEACTIVATED, -1);
250 status = _bt_core_get_status();
251 if (status == BT_DEACTIVATED) {
252 __bt_core_set_le_status(BT_LE_ACTIVATING);
253 BT_DBG("Activate BT");
255 char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
256 ret = __execute_command("/usr/bin/hciconfig", argv_up);
258 ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
261 BT_ERR("running script failed");
263 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
264 ret = __execute_command("/usr/bin/hciconfig", argv_down);
266 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh &", NULL);
268 __bt_core_set_status(BT_DEACTIVATED);
269 __bt_core_set_le_status(BT_LE_DEACTIVATED);
273 __bt_core_set_le_status(BT_LE_ACTIVATED);
274 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
277 ret = _bt_core_start_httpproxy();
279 BT_ERR("_bt_core_start_httpproxy() failed");
286 int _bt_disable_adapter_le(void)
291 bt_le_status_t le_status;
293 le_status = _bt_core_get_le_status();
294 retv_if(le_status == BT_LE_DEACTIVATED, 0);
295 retv_if(le_status == BT_LE_DEACTIVATING, -1);
298 _bt_core_stop_httpproxy();
301 status = _bt_core_get_status();
302 BT_DBG("status : %d", status);
304 if (status == BT_DEACTIVATED) {
305 __bt_core_set_le_status(BT_LE_DEACTIVATING);
307 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
308 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
310 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
312 BT_ERR("running script failed");
313 __bt_core_set_le_status(BT_LE_ACTIVATED);
317 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
320 __bt_core_set_le_status(BT_LE_DEACTIVATED);
326 int _bt_core_service_request_adapter(int service_function)
330 GArray *in_param1 = NULL;
331 GArray *in_param2 = NULL;
332 GArray *in_param3 = NULL;
333 GArray *in_param4 = NULL;
334 GArray *out_param = NULL;
336 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
338 ret = _bt_core_service_request(BT_CORE_SERVICE, service_function,
339 in_param1, in_param2, in_param3, in_param4, &out_param);
341 BT_ERR("_bt_core_service_request_adapter() failed");
343 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
348 static gboolean __bt_core_check_the_adapter_path(GDBusConnection *conn)
351 GDBusProxy *manager_proxy = NULL;
352 GVariant *result = NULL;
353 char *adapter_path = NULL;
358 manager_proxy = g_dbus_proxy_new_sync(conn,
359 G_DBUS_PROXY_FLAGS_NONE, NULL,
362 "org.freedesktop.DBus.ObjectManager",
365 if (!manager_proxy) {
367 BT_ERR("Unable to create proxy: %s", err->message);
370 BT_ERR("Fail to create proxy");
375 result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
376 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
379 BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
382 BT_ERR("Fail to get DefaultAdapter");
387 if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
388 BT_ERR("Incorrect result\n");
392 g_variant_get(result, "(&o)", &adapter_path);
394 if (adapter_path == NULL ||
395 strlen(adapter_path) >= BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX) {
396 BT_ERR("Adapter path is inproper\n");
400 g_variant_unref(result);
401 g_object_unref(manager_proxy);
407 g_variant_unref(result);
410 g_object_unref(manager_proxy);
415 void _bt_core_update_status(void)
417 int bt_status = VCONFKEY_BT_STATUS_OFF;
418 int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
419 gboolean ret = FALSE;
421 ret = __bt_core_check_the_adapter_path(_bt_core_get_gdbus_connection());
422 BT_INFO("check the real status of bt_adapter");
425 __bt_core_set_status(BT_DEACTIVATED);
426 __bt_core_set_le_status(BT_DEACTIVATED);
428 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
429 BT_ERR("no bluetooth device info, so BT was disabled at previous session");
430 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
431 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
433 BT_INFO("bt_status = %d, bt_le_status = %d", bt_status, bt_le_status);
435 if (bt_status & VCONFKEY_BT_STATUS_ON)
436 __bt_core_set_status(BT_ACTIVATED);
437 if (bt_le_status & VCONFKEY_BT_LE_STATUS_ON)
438 __bt_core_set_le_status(BT_ACTIVATED);
442 gboolean _bt_core_enable_adapter(void)
446 _bt_set_flightmode_request(FALSE);
447 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
448 BT_ERR("Set vconf failed");
450 ret = _bt_enable_adapter();
457 gboolean _bt_core_disable_adapter(void)
461 _bt_set_flightmode_request(FALSE);
462 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
463 BT_ERR("Set vconf failed");
465 ret = _bt_disable_adapter();
472 gboolean _bt_core_recover_adapter(void)
477 BT_INFO_C("Recover bt adapter");
479 _bt_set_flightmode_request(FALSE);
480 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
481 BT_ERR("Set vconf failed");
483 is_recovery_mode = TRUE;
485 _bt_core_update_status();
487 if (_bt_core_get_status() == BT_ACTIVATED) {
488 _bt_core_set_bt_status(BT_RECOVERY_MODE, 1);
489 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
491 if (_bt_core_get_le_status() == BT_LE_ACTIVATED) {
492 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 1);
493 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
496 ret = _bt_disable_adapter();
498 BT_ERR("_bt_disable_adapter() failed");
499 ret_le = _bt_disable_adapter_le();
501 BT_ERR("_bt_disable_adapter_le() failed");
506 gboolean _bt_core_enable_adapter_le(void)
510 ret = _bt_enable_adapter_le();
517 gboolean _bt_core_disable_adapter_le(void)
523 ret = _bt_disable_adapter_le();
530 gboolean __bt_core_reset_adapter(void)
532 /* Forcely terminate */
533 if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0) {
534 BT_ERR("running script failed");
536 _bt_core_terminate();
541 static gboolean __bt_core_enable_core_timeout_cb(gpointer data)
545 _bt_core_init_vconf_value();
550 gboolean _bt_core_enable_core(void)
554 _bt_core_update_status();
556 g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
562 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
566 char *arg_list[3] = { NULL, NULL, NULL };
568 BT_DBG("Test item : %s", type);
570 if (g_strcmp0(type, "Enable_RF_Test") == 0) {
571 cmd = "/usr/etc/bluetooth/bt-edutm-on.sh";
572 arg_list[0] = "bt-edutm-on.sh";
573 } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
574 cmd = "/usr/etc/bluetooth/bt-edutm-off.sh";
575 arg_list[0] = "bt-edutm-off.sh";
576 } else if (g_strcmp0(type, "Slave_Mode") == 0) {
577 cmd = "/usr/etc/bluetooth/bt-mode-slave.sh";
578 arg_list[0] = "bt-mode-slave.sh";
579 } else if (g_strcmp0(type, "Master_Mode") == 0) {
580 cmd = "/usr/etc/bluetooth/bt-mode-master.sh";
581 arg_list[0] = "bt-mode-master.sh";
582 } else if (g_strcmp0(type, "SSP_Debug_Mode") == 0) {
583 cmd = "/usr/etc/bluetooth/bt-set-ssp-debug-mode.sh";
584 arg_list[0] = "bt-set-ssp-debug-mode.sh";
585 arg_list[1] = (char *)arg;
586 } else if (g_strcmp0(type, "RF_Channel") == 0) {
587 cmd = "/usr/etc/bluetooth/bt-enable-rf-channel.sh";
588 arg_list[0] = "bt-enable-rf-channel.sh";
589 arg_list[1] = (char *)arg;
591 _bt_core_terminate();
595 BT_DBG("Run %s", cmd);
596 if (__execute_command(cmd, arg_list) < 0) {
597 BT_ERR("running script failed");
600 _bt_core_terminate();
604 static gboolean __bt_core_recovery_cb(gpointer data)
607 gboolean is_request_failed = FALSE;
608 static gboolean is_first_failure = TRUE;
612 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
613 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
615 is_request_failed = TRUE;
618 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
619 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
621 is_request_failed = TRUE;
624 if (is_request_failed == TRUE) {
625 BT_ERR("Recovery is failed.");
626 if (is_first_failure == TRUE) {
627 g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
628 is_first_failure = FALSE;
631 is_first_failure = TRUE;
635 is_first_failure = TRUE;
637 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
638 _bt_core_set_bt_status(BT_RECOVERY_MODE, 0);
639 ret = _bt_enable_adapter();
641 BT_ERR("_bt_enable_adapter() failed");
643 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
644 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 0);
645 ret = _bt_enable_adapter_le();
647 BT_ERR("_bt_enable_adapter_le() failed");
650 is_recovery_mode = FALSE;
657 static gboolean __bt_core_enable_timeout_cb(gpointer data)
659 bt_status_t adapter_status;
660 bt_le_status_t adapter_status_le;
664 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
665 BT_ERR("Set vconf failed");
667 adapter_status = _bt_core_get_status();
668 adapter_status_le = _bt_core_get_le_status();
670 if (adapter_status == BT_DEACTIVATED &&
671 _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
672 _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
673 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
674 _bt_enable_adapter();
677 if (adapter_status_le == BT_LE_DEACTIVATED &&
678 _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
679 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
680 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
681 _bt_enable_adapter_le();
687 static gboolean __bt_core_disable_timeout_cb(gpointer data)
689 bt_status_t adapter_status;
690 bt_le_status_t adapter_status_le;
694 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
695 BT_ERR("Set vconf failed");
697 adapter_status = _bt_core_get_status();
698 adapter_status_le = _bt_core_get_le_status();
700 if (adapter_status == BT_ACTIVATED) {
701 int bt_status_before_mode = 0;
703 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
704 _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
706 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
707 _bt_disable_adapter();
710 if (adapter_status_le == BT_LE_ACTIVATED) {
711 int bt_le_status_before_mode = 0;
713 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
714 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
716 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
717 _bt_disable_adapter_le();
723 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value)
730 bundle_add_str(b, key, value);
732 ret = eventsystem_send_system_event(event, b);
734 BT_DBG("eventsystem_send_system_event result: %d", ret);
741 void _bt_core_adapter_added_cb(void)
744 bt_le_status_t le_status;
745 gboolean flight_mode_status;
749 status = _bt_core_get_status();
750 BT_DBG("status : %d", status);
751 le_status = _bt_core_get_le_status();
752 BT_DBG("le_status : %d", le_status);
754 if (status == BT_ACTIVATING)
755 __bt_core_set_status(BT_ACTIVATED);
756 if (le_status == BT_LE_ACTIVATING)
757 __bt_core_set_le_status(BT_LE_ACTIVATED);
759 flight_mode_status = _bt_core_is_flight_mode_enabled();
761 if (flight_mode_status == TRUE && _bt_is_flightmode_request() == TRUE) {
762 _bt_set_flightmode_request(FALSE);
763 g_timeout_add(2000, (GSourceFunc)__bt_core_disable_timeout_cb, NULL);
766 _bt_set_flightmode_request(FALSE);
767 _bt_core_terminate();
770 void _bt_core_adapter_removed_cb(void)
772 int flight_mode_value = 0;
773 int power_saving_mode = 0;
774 gboolean flight_mode_status;
775 static int timer_id = -1;
779 __bt_core_set_status(BT_DEACTIVATED);
780 __bt_core_set_le_status(BT_LE_DEACTIVATED);
781 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
782 BT_ERR("Set vconf failed");
783 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
784 BT_ERR("Set vconf failed");
786 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
787 EVT_VAL_BT_OFF) != ES_R_OK)
788 BT_ERR("Fail to set value");
790 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
791 EVT_VAL_BT_LE_OFF) != ES_R_OK)
792 BT_ERR("Fail to set value");
794 if (is_recovery_mode == TRUE) {
796 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
800 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value) != 0)
801 BT_ERR("Fail to get the flight_mode_deactivated value");
803 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &power_saving_mode) != 0)
804 BT_ERR("Fail to get the ps_mode_deactivated value");
806 flight_mode_status = _bt_core_is_flight_mode_enabled();
808 if (flight_mode_status == FALSE && _bt_is_flightmode_request() == TRUE) {
809 _bt_set_flightmode_request(FALSE);
811 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_enable_timeout_cb, NULL);
814 _bt_set_flightmode_request(FALSE);
816 if (flight_mode_value == 1 || power_saving_mode == 1) {
817 BT_DBG("Bt Core not terminated");
821 _bt_core_terminate();