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 static bt_status_t adapter_status = BT_DEACTIVATED;
38 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
39 static gboolean is_recovery_mode = FALSE;
41 static int bt_status_before[BT_MODE_MAX] = { VCONFKEY_BT_STATUS_OFF, };
42 static int bt_le_status_before[BT_MODE_MAX] = { 0, };
44 static DBusGConnection *conn = NULL;
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 gboolean _bt_core_is_flight_mode_enabled(void)
93 #ifdef TIZEN_BT_FLIGHTMODE_ENABLED
97 ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &isFlightMode);
99 BT_ERR("vconf_get_bool failed");
107 static int __execute_command(const char *cmd, char *const arg_list[])
117 BT_ERR("fork failed");
123 BT_ERR("fork failed");
124 } else if (pid2 == 0) {
125 execv(cmd, arg_list);
132 BT_DBG("parent : forked[%d]", pid);
133 waitpid(pid, &status, 0);
134 BT_DBG("child is terminated : %d", status);
141 int _bt_enable_adapter(void)
145 bt_le_status_t le_status;
149 status = _bt_core_get_status();
150 if (status != BT_DEACTIVATED) {
151 BT_ERR("Invalid state %d", status);
155 le_status = _bt_core_get_le_status();
156 if (le_status == BT_LE_ACTIVATED) {
157 /* Turn on PSCAN, (ISCAN if needed) */
158 /* Return with 0 for the Enabled response. */
159 __bt_core_set_status(BT_ACTIVATED);
160 BT_INFO("BR/EDR is enabled.");
164 __bt_core_set_status(BT_ACTIVATING);
166 char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", "pscan", NULL};
167 ret = __execute_command("/usr/bin/hciconfig", argv_up);
169 ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
172 BT_ERR("running script failed");
173 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
174 __bt_core_set_status(BT_DEACTIVATED);
181 int _bt_disable_adapter(void)
184 bt_le_status_t le_status;
186 BT_INFO_C("Disable adapter");
188 le_status = _bt_core_get_le_status();
189 BT_DBG("le_status : %d", le_status);
190 if (le_status == BT_LE_ACTIVATED) {
191 /* Turn off PSCAN, (ISCAN if needed) */
192 /* Return with 0 for the Disabled response. */
193 __bt_core_set_status(BT_DEACTIVATED);
194 BT_INFO("BR/EDR is disabled. now LE only mode");
198 status = _bt_core_get_status();
199 if (status == BT_ACTIVATING) {
200 /* Forcely terminate */
202 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
203 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
205 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
207 BT_ERR("running script failed");
209 _bt_core_terminate();
211 } else if (status != BT_ACTIVATED) {
212 BT_ERR("Invalid state %d", status);
215 __bt_core_set_status(BT_DEACTIVATING);
217 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
218 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
220 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
222 BT_ERR("running script failed");
223 __bt_core_set_status( BT_ACTIVATED);
230 int _bt_enable_adapter_le(void)
236 bt_le_status_t le_status;
237 le_status = _bt_core_get_le_status();
238 retv_if(le_status != BT_LE_DEACTIVATED, -1);
240 status = _bt_core_get_status();
241 if (status == BT_DEACTIVATED) {
242 __bt_core_set_le_status(BT_LE_ACTIVATING);
243 BT_DBG("Activate BT");
245 char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", "pscan", NULL};
246 ret = __execute_command("/usr/bin/hciconfig", argv_up);
248 ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
251 BT_ERR("running script failed");
252 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh &", NULL);
253 __bt_core_set_status(BT_DEACTIVATED);
254 __bt_core_set_le_status(BT_LE_DEACTIVATED);
258 __bt_core_set_le_status(BT_LE_ACTIVATED);
264 int _bt_disable_adapter_le(void)
269 bt_le_status_t le_status;
271 le_status = _bt_core_get_le_status();
272 retv_if(le_status == BT_LE_DEACTIVATED, 0);
273 retv_if(le_status == BT_LE_DEACTIVATING, -1);
275 status = _bt_core_get_status();
276 BT_DBG("status : %d", status);
278 if (status == BT_DEACTIVATED) {
279 __bt_core_set_le_status(BT_LE_DEACTIVATING);
281 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
282 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
284 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
286 BT_ERR("running script failed");
287 __bt_core_set_le_status(BT_LE_ACTIVATED);
292 __bt_core_set_le_status(BT_LE_DEACTIVATED);
298 int _bt_core_service_request_adapter(int service_function)
302 GArray *in_param1 = NULL;
303 GArray *in_param2 = NULL;
304 GArray *in_param3 = NULL;
305 GArray *in_param4 = NULL;
306 GArray *out_param = NULL;
308 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
310 ret = _bt_core_service_request(BT_CORE_SERVICE, service_function,
311 in_param1, in_param2, in_param3, in_param4, &out_param);
313 BT_ERR("_bt_core_service_request_adapter() failed");
315 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
320 static void __bt_core_update_status(void)
322 int bt_status = VCONFKEY_BT_STATUS_OFF;
323 int bt_le_status = 0;
325 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
326 BT_ERR("no bluetooth device info, so BT was disabled at previous session");
328 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
329 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
331 BT_INFO("bt_status = %d, bt_le_status = %d", bt_status, bt_le_status);
333 if (bt_status == VCONFKEY_BT_STATUS_OFF)
334 __bt_core_set_status(BT_DEACTIVATED);
336 __bt_core_set_status(BT_ACTIVATED);
338 if (bt_le_status == 0)
339 __bt_core_set_le_status(BT_LE_DEACTIVATED);
341 __bt_core_set_le_status(BT_LE_ACTIVATED);
344 gboolean _bt_core_enable_adapter(void)
348 _bt_set_flightmode_request(FALSE);
349 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
350 BT_ERR("Set vconf failed");
352 ret = _bt_enable_adapter();
359 gboolean _bt_core_disable_adapter(void)
363 _bt_set_flightmode_request(FALSE);
364 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
365 BT_ERR("Set vconf failed");
367 ret = _bt_disable_adapter();
374 gboolean _bt_core_recover_adapter(void)
379 BT_INFO_C("Recover bt adapter");
381 _bt_set_flightmode_request(FALSE);
382 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
383 BT_ERR("Set vconf failed");
385 is_recovery_mode = TRUE;
387 __bt_core_update_status();
389 if (_bt_core_get_status() == BT_ACTIVATED) {
390 _bt_core_set_bt_status(BT_RECOVERY_MODE, 1);
391 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
393 if (_bt_core_get_le_status() == BT_LE_ACTIVATED) {
394 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 1);
395 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
398 ret = _bt_disable_adapter();
400 BT_ERR("_bt_disable_adapter() failed");
401 ret_le = _bt_disable_adapter_le();
403 BT_ERR("_bt_disable_adapter_le() failed");
408 gboolean _bt_core_enable_adapter_le(void)
412 ret = _bt_enable_adapter_le();
419 gboolean _bt_core_disable_adapter_le(void)
425 ret = _bt_disable_adapter_le();
432 gboolean __bt_core_reset_adapter(void)
434 /* Forcely terminate */
435 if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0) {
436 BT_ERR("running script failed");
438 _bt_core_terminate();
443 static gboolean __bt_core_enable_core_timeout_cb(gpointer data)
447 _bt_core_init_vconf_value();
452 gboolean _bt_core_enable_core(void)
456 __bt_core_update_status();
458 g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
464 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
468 char *arg_list[3] = { NULL, NULL, NULL };
470 BT_DBG("Test item : %s", type);
472 if (g_strcmp0(type, "Enable_RF_Test") == 0) {
473 cmd = "/usr/etc/bluetooth/bt-edutm-on.sh";
474 arg_list[0] = "bt-edutm-on.sh";
475 } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
476 cmd = "/usr/etc/bluetooth/bt-edutm-off.sh";
477 arg_list[0] = "bt-edutm-off.sh";
478 } else if (g_strcmp0(type, "Slave_Mode") == 0) {
479 cmd = "/usr/etc/bluetooth/bt-mode-slave.sh";
480 arg_list[0] = "bt-mode-slave.sh";
481 } else if (g_strcmp0(type, "Master_Mode") == 0) {
482 cmd = "/usr/etc/bluetooth/bt-mode-master.sh";
483 arg_list[0] = "bt-mode-master.sh";
484 } else if (g_strcmp0(type, "SSP_Debug_Mode") == 0) {
485 cmd = "/usr/etc/bluetooth/bt-set-ssp-debug-mode.sh";
486 arg_list[0] = "bt-set-ssp-debug-mode.sh";
487 arg_list[1] = (char *)arg;
488 } else if (g_strcmp0(type, "RF_Channel") == 0) {
489 cmd = "/usr/etc/bluetooth/bt-enable-rf-channel.sh";
490 arg_list[0] = "bt-enable-rf-channel.sh";
491 arg_list[1] = (char *)arg;
493 _bt_core_terminate();
497 BT_DBG("Run %s", cmd);
498 if (__execute_command(cmd, arg_list) < 0) {
499 BT_ERR("running script failed");
502 _bt_core_terminate();
506 static gboolean __bt_core_recovery_cb(gpointer data)
509 gboolean is_request_failed = FALSE;
510 static gboolean is_first_failure = TRUE;
514 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
515 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
517 is_request_failed = TRUE;
520 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
521 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
523 is_request_failed = TRUE;
526 if (is_request_failed == TRUE) {
527 BT_ERR("Recovery is failed.");
528 if (is_first_failure == TRUE) {
529 g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
530 is_first_failure = FALSE;
533 is_first_failure = TRUE;
537 is_first_failure = TRUE;
539 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
540 _bt_core_set_bt_status(BT_RECOVERY_MODE, 0);
541 ret = _bt_enable_adapter();
543 BT_ERR("_bt_enable_adapter() failed");
545 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
546 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 0);
547 ret = _bt_enable_adapter_le();
549 BT_ERR("_bt_enable_adapter_le() failed");
552 is_recovery_mode = FALSE;
559 static gboolean __bt_core_enable_timeout_cb(gpointer data)
561 bt_status_t adapter_status;
562 bt_le_status_t adapter_status_le;
566 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
567 BT_ERR("Set vconf failed");
569 adapter_status = _bt_core_get_status();
570 adapter_status_le = _bt_core_get_le_status();
572 if (adapter_status == BT_DEACTIVATED &&
573 _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
574 _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
575 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
576 _bt_enable_adapter();
579 if (adapter_status_le == BT_LE_DEACTIVATED &&
580 _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
581 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
582 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
583 _bt_enable_adapter_le();
589 static gboolean __bt_core_disable_timeout_cb(gpointer data)
591 bt_status_t adapter_status;
592 bt_le_status_t adapter_status_le;
596 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
597 BT_ERR("Set vconf failed");
599 adapter_status = _bt_core_get_status();
600 adapter_status_le = _bt_core_get_le_status();
602 if (adapter_status == BT_ACTIVATED) {
603 int bt_status_before_mode = 0;
605 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
606 _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
608 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
609 _bt_disable_adapter();
612 if (adapter_status_le == BT_LE_ACTIVATED) {
613 int bt_le_status_before_mode = 0;
615 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
616 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
618 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
619 _bt_disable_adapter_le();
625 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value)
632 bundle_add_str(b, key, value);
634 ret = eventsystem_request_sending_system_event(event, b);
636 BT_DBG("request_sending_system_event result: %d", ret);
643 void _bt_core_adapter_added_cb(void)
646 bt_le_status_t le_status;
647 gboolean flight_mode_status;
651 status = _bt_core_get_status();
652 BT_DBG("status : %d", status);
653 le_status = _bt_core_get_le_status();
654 BT_DBG("le_status : %d", le_status);
656 if (status == BT_ACTIVATING)
657 __bt_core_set_status(BT_ACTIVATED);
658 if (le_status == BT_LE_ACTIVATING)
659 __bt_core_set_le_status(BT_LE_ACTIVATED);
661 flight_mode_status = _bt_core_is_flight_mode_enabled();
663 if (flight_mode_status == TRUE && _bt_is_flightmode_request() == TRUE) {
664 _bt_set_flightmode_request(FALSE);
665 g_timeout_add(2000, (GSourceFunc)__bt_core_disable_timeout_cb, NULL);
668 _bt_set_flightmode_request(FALSE);
669 _bt_core_terminate();
672 void _bt_core_adapter_removed_cb(void)
674 int flight_mode_value = 0;
675 int power_saving_mode = 0;
676 gboolean flight_mode_status;
677 static int timer_id = -1;
681 __bt_core_set_status(BT_DEACTIVATED);
682 __bt_core_set_le_status(BT_LE_DEACTIVATED);
683 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
684 BT_ERR("Set vconf failed");
686 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
687 BT_ERR("Set vconf failed");
690 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
691 EVT_VAL_BT_OFF) != ES_R_OK)
692 BT_ERR("Fail to set value");
694 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
695 EVT_VAL_BT_LE_OFF) != ES_R_OK)
696 BT_ERR("Fail to set value");
698 if (is_recovery_mode == TRUE)
701 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
705 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value) != 0)
706 BT_ERR("Fail to get the flight_mode_deactivated value");
708 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &power_saving_mode) != 0)
709 BT_ERR("Fail to get the ps_mode_deactivated value");
711 flight_mode_status = _bt_core_is_flight_mode_enabled();
713 if (flight_mode_status == FALSE && _bt_is_flightmode_request() == TRUE) {
714 _bt_set_flightmode_request(FALSE);
716 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_enable_timeout_cb, NULL);
719 _bt_set_flightmode_request(FALSE);
721 if (flight_mode_value == 1 || power_saving_mode == 1){
722 BT_DBG("Bt Core not terminated");
726 _bt_core_terminate();