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 void __bt_core_set_status(bt_status_t status)
46 adapter_status = status;
49 bt_status_t _bt_core_get_status(void)
51 return adapter_status;
54 static void __bt_core_set_le_status(bt_le_status_t status)
56 adapter_le_status = status;
59 bt_le_status_t _bt_core_get_le_status(void)
61 return adapter_le_status;
64 int _bt_core_get_bt_status(bt_mode_e mode)
66 return bt_status_before[mode];
69 int _bt_core_get_bt_le_status(bt_mode_e mode)
71 return bt_le_status_before[mode];
74 void _bt_core_set_bt_status(bt_mode_e mode, int status)
76 bt_status_before[mode] = status;
79 void _bt_core_set_bt_le_status(bt_mode_e mode, int status)
81 bt_le_status_before[mode] = status;
84 gboolean _bt_core_is_recovery_mode(void)
86 return is_recovery_mode;
89 gboolean _bt_core_is_flight_mode_enabled(void)
91 #ifdef TIZEN_BT_FLIGHTMODE_ENABLED
95 ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &isFlightMode);
97 BT_ERR("vconf_get_bool failed");
105 static int __execute_command(const char *cmd, char *const arg_list[])
115 BT_ERR("fork failed");
121 BT_ERR("fork failed");
122 } else if (pid2 == 0) {
123 execv(cmd, arg_list);
130 BT_DBG("parent : forked[%d]", pid);
131 waitpid(pid, &status, 0);
132 BT_DBG("child is terminated : %d", status);
139 int _bt_enable_adapter(void)
143 bt_le_status_t le_status;
147 status = _bt_core_get_status();
148 if (status != BT_DEACTIVATED) {
149 BT_ERR("Invalid state %d", status);
153 le_status = _bt_core_get_le_status();
154 if (le_status == BT_LE_ACTIVATED) {
155 /* Turn on PSCAN, (ISCAN if needed) */
156 /* Return with 0 for the Enabled response. */
157 __bt_core_set_status(BT_ACTIVATED);
158 BT_INFO("BR/EDR is enabled.");
162 __bt_core_set_status(BT_ACTIVATING);
164 ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
166 BT_ERR("running script failed");
167 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
168 __bt_core_set_status(BT_DEACTIVATED);
175 int _bt_disable_adapter(void)
178 bt_le_status_t le_status;
180 BT_INFO_C("Disable adapter");
182 le_status = _bt_core_get_le_status();
183 BT_DBG("le_status : %d", le_status);
184 if (le_status == BT_LE_ACTIVATED) {
185 /* Turn off PSCAN, (ISCAN if needed) */
186 /* Return with 0 for the Disabled response. */
187 __bt_core_set_status(BT_DEACTIVATED);
188 BT_INFO("BR/EDR is disabled. now LE only mode");
192 status = _bt_core_get_status();
193 if (status == BT_ACTIVATING) {
194 /* Forcely terminate */
195 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
196 BT_ERR("running script failed");
198 _bt_core_terminate();
200 } else if (status != BT_ACTIVATED) {
201 BT_ERR("Invalid state %d", status);
204 __bt_core_set_status(BT_DEACTIVATING);
206 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
207 BT_ERR("running script failed");
208 __bt_core_set_status( BT_ACTIVATED);
215 int _bt_enable_adapter_le(void)
221 bt_le_status_t le_status;
222 le_status = _bt_core_get_le_status();
223 retv_if(le_status != BT_LE_DEACTIVATED, -1);
225 status = _bt_core_get_status();
226 if (status == BT_DEACTIVATED) {
227 __bt_core_set_le_status(BT_LE_ACTIVATING);
228 BT_DBG("Activate BT");
229 ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
231 BT_ERR("running script failed");
232 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh &", NULL);
233 __bt_core_set_status(BT_DEACTIVATED);
234 __bt_core_set_le_status(BT_LE_DEACTIVATED);
238 __bt_core_set_le_status(BT_LE_ACTIVATED);
243 int _bt_disable_adapter_le(void)
248 bt_le_status_t le_status;
250 le_status = _bt_core_get_le_status();
251 retv_if(le_status == BT_LE_DEACTIVATED, 0);
252 retv_if(le_status == BT_LE_DEACTIVATING, -1);
254 status = _bt_core_get_status();
255 BT_DBG("status : %d", status);
257 if (status == BT_DEACTIVATED) {
258 __bt_core_set_le_status(BT_LE_DEACTIVATING);
260 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
261 BT_ERR("running script failed");
262 __bt_core_set_le_status(BT_LE_ACTIVATED);
267 __bt_core_set_le_status(BT_LE_DEACTIVATED);
273 int _bt_core_service_request_adapter(int service_function)
277 GArray *in_param1 = NULL;
278 GArray *in_param2 = NULL;
279 GArray *in_param3 = NULL;
280 GArray *in_param4 = NULL;
281 GArray *out_param = NULL;
283 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
285 ret = _bt_core_service_request(BT_CORE_SERVICE, service_function,
286 in_param1, in_param2, in_param3, in_param4, &out_param);
288 BT_ERR("_bt_core_service_request_adapter() failed");
290 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
295 static void __bt_core_update_status(void)
297 int bt_status = VCONFKEY_BT_STATUS_OFF;
298 int bt_le_status = 0;
300 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
301 BT_ERR("no bluetooth device info, so BT was disabled at previous session");
302 #ifdef ENABLE_TIZEN_2_4
303 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
304 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
307 BT_INFO("bt_status = %d, bt_le_status = %d", bt_status, bt_le_status);
309 if (bt_status == VCONFKEY_BT_STATUS_OFF)
310 __bt_core_set_status(BT_DEACTIVATED);
312 __bt_core_set_status(BT_ACTIVATED);
314 if (bt_le_status == 0)
315 __bt_core_set_le_status(BT_LE_DEACTIVATED);
317 __bt_core_set_le_status(BT_LE_ACTIVATED);
320 gboolean _bt_core_enable_adapter(void)
324 _bt_set_flightmode_request(FALSE);
325 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
326 BT_ERR("Set vconf failed");
328 ret = _bt_enable_adapter();
335 gboolean _bt_core_disable_adapter(void)
339 _bt_set_flightmode_request(FALSE);
340 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
341 BT_ERR("Set vconf failed");
343 ret = _bt_disable_adapter();
350 gboolean _bt_core_recover_adapter(void)
355 BT_INFO_C("Recover bt adapter");
357 _bt_set_flightmode_request(FALSE);
358 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
359 BT_ERR("Set vconf failed");
361 is_recovery_mode = TRUE;
363 __bt_core_update_status();
365 if (_bt_core_get_status() == BT_ACTIVATED) {
366 _bt_core_set_bt_status(BT_RECOVERY_MODE, 1);
367 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
369 if (_bt_core_get_le_status() == BT_LE_ACTIVATED) {
370 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 1);
371 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
374 ret = _bt_disable_adapter();
376 BT_ERR("_bt_disable_adapter() failed");
377 ret_le = _bt_disable_adapter_le();
379 BT_ERR("_bt_disable_adapter_le() failed");
384 gboolean _bt_core_enable_adapter_le(void)
388 ret = _bt_enable_adapter_le();
395 gboolean _bt_core_disable_adapter_le(void)
401 ret = _bt_disable_adapter_le();
408 gboolean __bt_core_reset_adapter(void)
410 /* Forcely terminate */
411 if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0) {
412 BT_ERR("running script failed");
414 _bt_core_terminate();
419 static gboolean __bt_core_enable_core_timeout_cb(gpointer data)
423 _bt_core_init_vconf_value();
428 gboolean _bt_core_enable_core(void)
432 __bt_core_update_status();
434 g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
440 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
444 char *arg_list[3] = { NULL, NULL, NULL };
446 BT_DBG("Test item : %s", type);
448 if (g_strcmp0(type, "Enable_RF_Test") == 0) {
449 cmd = "/usr/etc/bluetooth/bt-edutm-on.sh";
450 arg_list[0] = "bt-edutm-on.sh";
451 } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
452 cmd = "/usr/etc/bluetooth/bt-edutm-off.sh";
453 arg_list[0] = "bt-edutm-off.sh";
454 } else if (g_strcmp0(type, "Slave_Mode") == 0) {
455 cmd = "/usr/etc/bluetooth/bt-mode-slave.sh";
456 arg_list[0] = "bt-mode-slave.sh";
457 } else if (g_strcmp0(type, "Master_Mode") == 0) {
458 cmd = "/usr/etc/bluetooth/bt-mode-master.sh";
459 arg_list[0] = "bt-mode-master.sh";
460 } else if (g_strcmp0(type, "SSP_Debug_Mode") == 0) {
461 cmd = "/usr/etc/bluetooth/bt-set-ssp-debug-mode.sh";
462 arg_list[0] = "bt-set-ssp-debug-mode.sh";
463 arg_list[1] = (char *)arg;
464 } else if (g_strcmp0(type, "RF_Channel") == 0) {
465 cmd = "/usr/etc/bluetooth/bt-enable-rf-channel.sh";
466 arg_list[0] = "bt-enable-rf-channel.sh";
467 arg_list[1] = (char *)arg;
469 _bt_core_terminate();
473 BT_DBG("Run %s", cmd);
474 if (__execute_command(cmd, arg_list) < 0) {
475 BT_ERR("running script failed");
478 _bt_core_terminate();
482 static gboolean __bt_core_recovery_cb(gpointer data)
485 gboolean is_request_failed = FALSE;
486 static gboolean is_first_failure = TRUE;
490 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
491 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
493 is_request_failed = TRUE;
496 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
497 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
499 is_request_failed = TRUE;
502 if (is_request_failed == TRUE) {
503 BT_ERR("Recovery is failed.");
504 if (is_first_failure == TRUE) {
505 g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
506 is_first_failure = FALSE;
509 is_first_failure = TRUE;
513 is_first_failure = TRUE;
515 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
516 _bt_core_set_bt_status(BT_RECOVERY_MODE, 0);
517 ret = _bt_enable_adapter();
519 BT_ERR("_bt_enable_adapter() failed");
521 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
522 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 0);
523 ret = _bt_enable_adapter_le();
525 BT_ERR("_bt_enable_adapter_le() failed");
528 is_recovery_mode = FALSE;
535 static gboolean __bt_core_enable_timeout_cb(gpointer data)
537 bt_status_t adapter_status;
538 bt_le_status_t adapter_status_le;
542 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
543 BT_ERR("Set vconf failed");
545 adapter_status = _bt_core_get_status();
546 adapter_status_le = _bt_core_get_le_status();
548 if (adapter_status == BT_DEACTIVATED &&
549 _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
550 _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
551 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
552 _bt_enable_adapter();
555 if (adapter_status_le == BT_LE_DEACTIVATED &&
556 _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
557 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
558 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
559 _bt_enable_adapter_le();
565 static gboolean __bt_core_disable_timeout_cb(gpointer data)
567 bt_status_t adapter_status;
568 bt_le_status_t adapter_status_le;
572 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
573 BT_ERR("Set vconf failed");
575 adapter_status = _bt_core_get_status();
576 adapter_status_le = _bt_core_get_le_status();
578 if (adapter_status == BT_ACTIVATED) {
579 int bt_status_before_mode = 0;
581 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
582 _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
584 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
585 _bt_disable_adapter();
588 if (adapter_status_le == BT_LE_ACTIVATED) {
589 int bt_le_status_before_mode = 0;
591 #ifdef ENABLE_TIZEN_2_4
592 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
593 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
596 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
597 _bt_disable_adapter_le();
603 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value)
610 bundle_add_str(b, key, value);
612 ret = eventsystem_request_sending_system_event(event, b);
614 BT_DBG("request_sending_system_event result: %d", ret);
621 void _bt_core_adapter_added_cb(void)
624 bt_le_status_t le_status;
625 gboolean flight_mode_status;
629 status = _bt_core_get_status();
630 BT_DBG("status : %d", status);
631 le_status = _bt_core_get_le_status();
632 BT_DBG("le_status : %d", le_status);
634 if (status == BT_ACTIVATING)
635 __bt_core_set_status(BT_ACTIVATED);
636 if (le_status == BT_LE_ACTIVATING)
637 __bt_core_set_le_status(BT_LE_ACTIVATED);
639 flight_mode_status = _bt_core_is_flight_mode_enabled();
641 if (flight_mode_status == TRUE && _bt_is_flightmode_request() == TRUE) {
642 _bt_set_flightmode_request(FALSE);
643 g_timeout_add(2000, (GSourceFunc)__bt_core_disable_timeout_cb, NULL);
646 _bt_set_flightmode_request(FALSE);
647 _bt_core_terminate();
650 void _bt_core_adapter_removed_cb(void)
652 int flight_mode_value = 0;
653 int power_saving_mode = 0;
654 gboolean flight_mode_status;
655 static int timer_id = -1;
659 __bt_core_set_status(BT_DEACTIVATED);
660 __bt_core_set_le_status(BT_LE_DEACTIVATED);
661 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
662 BT_ERR("Set vconf failed");
663 #ifdef ENABLE_TIZEN_2_4
664 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
665 BT_ERR("Set vconf failed");
669 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
670 EVT_VAL_BT_OFF) != ES_R_OK)
671 BT_ERR("Fail to set value");
673 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
674 EVT_VAL_BT_LE_OFF) != ES_R_OK)
675 BT_ERR("Fail to set value");
677 if (is_recovery_mode == TRUE)
680 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
684 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value) != 0)
685 BT_ERR("Fail to get the flight_mode_deactivated value");
687 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &power_saving_mode) != 0)
688 BT_ERR("Fail to get the ps_mode_deactivated value");
690 flight_mode_status = _bt_core_is_flight_mode_enabled();
692 if (flight_mode_status == FALSE && _bt_is_flightmode_request() == TRUE) {
693 _bt_set_flightmode_request(FALSE);
695 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_enable_timeout_cb, NULL);
698 _bt_set_flightmode_request(FALSE);
700 if (flight_mode_value == 1 || power_saving_mode == 1){
701 BT_DBG("Bt Core not terminated");
705 _bt_core_terminate();