Fix the bug on TV profile's BT on
[platform/core/connectivity/bluetooth-frwk.git] / bt-core / bt-core-adapter.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Hocheol Seo <hocheol.seo@samsung.com>
7  *               Girishashok Joshi <girish.joshi@samsung.com>
8  *               Chanyeol Park <chanyeol.park@samsung.com>
9  *
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
13  *
14  *              http://www.apache.org/licenses/LICENSE-2.0
15  *
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.
21  *
22  */
23
24 #include <vconf.h>
25 #include <vconf-keys.h>
26 #include <bundle.h>
27 #if 0
28 #include <eventsystem.h>
29 #endif
30
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"
36
37 #define BT_CORE_IDLE_TERM_TIME 200 /* 200ms */
38 #define BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX 50
39
40
41 static bt_status_t adapter_status = BT_DEACTIVATED;
42 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
43 static gboolean is_recovery_mode = FALSE;
44
45 static int bt_status_before[BT_MODE_MAX] = { VCONFKEY_BT_STATUS_OFF, };
46 static int bt_le_status_before[BT_MODE_MAX] = { 0, };
47
48 static DBusGConnection *conn = NULL;
49
50 static void __bt_core_set_status(bt_status_t status)
51 {
52         adapter_status = status;
53 }
54
55 bt_status_t _bt_core_get_status(void)
56 {
57         return adapter_status;
58 }
59
60 static void __bt_core_set_le_status(bt_le_status_t status)
61 {
62         adapter_le_status = status;
63 }
64
65 bt_le_status_t _bt_core_get_le_status(void)
66 {
67         return adapter_le_status;
68 }
69
70 int _bt_core_get_bt_status(bt_mode_e mode)
71 {
72         return bt_status_before[mode];
73 }
74
75 int _bt_core_get_bt_le_status(bt_mode_e mode)
76 {
77         return bt_le_status_before[mode];
78 }
79
80 void _bt_core_set_bt_status(bt_mode_e mode, int status)
81 {
82         bt_status_before[mode] = status;
83 }
84
85 void _bt_core_set_bt_le_status(bt_mode_e mode, int status)
86 {
87         bt_le_status_before[mode] = status;
88 }
89
90 gboolean _bt_core_is_recovery_mode(void)
91 {
92         return is_recovery_mode;
93 }
94
95 static gboolean __bt_core_idle_terminate(gpointer data)
96 {
97         BT_DBG("+");
98         _bt_core_terminate();
99         return FALSE;
100 }
101
102 gboolean _bt_core_is_flight_mode_enabled(void)
103 {
104 #ifdef TIZEN_BT_FLIGHTMODE_ENABLED
105         int isFlightMode = 0;
106         int ret = -1;
107
108         ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &isFlightMode);
109         if (ret != 0) {
110                 BT_ERR("vconf_get_bool failed");
111         }
112         return isFlightMode;
113 #else
114         return FALSE;
115 #endif
116 }
117
118 static int __execute_command(const char *cmd, char *const arg_list[])
119 {
120         int pid;
121         int pid2;
122         int status = 0;
123         BT_DBG("+");
124
125         pid = fork();
126         switch (pid) {
127         case -1:
128                 BT_ERR("fork failed");
129                 return -1;
130
131         case 0:
132                 pid2 = fork();
133                 if (pid2 == -1) {
134                         BT_ERR("fork failed");
135                 } else if (pid2 == 0) {
136                         execv(cmd, arg_list);
137                         exit(256);
138                 }
139                 exit(0);
140                 break;
141
142         default:
143                 BT_DBG("parent : forked[%d]", pid);
144                 waitpid(pid, &status, 0);
145                 BT_DBG("child is terminated : %d", status);
146                 break;
147         }
148         BT_DBG("-");
149         return 0;
150 }
151
152 int _bt_enable_adapter(void)
153 {
154         int ret;
155         bt_status_t status;
156         bt_le_status_t le_status;
157
158         BT_INFO("");
159
160         status = _bt_core_get_status();
161         if (status != BT_DEACTIVATED) {
162                 BT_ERR("Invalid state %d", status);
163                 return -1;
164         }
165
166         le_status = _bt_core_get_le_status();
167         if (le_status == BT_LE_ACTIVATED) {
168                 /* Turn on PSCAN, (ISCAN if needed) */
169                 /* Return with 0 for the Enabled response. */
170                 __bt_core_set_status(BT_ACTIVATED);
171                 BT_INFO("BR/EDR is enabled.");
172                 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
173                 return 0;
174         }
175
176         __bt_core_set_status(BT_ACTIVATING);
177 #ifdef USB_BLUETOOTH
178         char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
179         ret = __execute_command("/usr/bin/hciconfig", argv_up);
180 #else
181         ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
182 #endif
183         if (ret < 0) {
184                 BT_ERR("running script failed");
185                 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
186                 __bt_core_set_status(BT_DEACTIVATED);
187                 return -1;
188         }
189
190         return 0;
191 }
192
193 int _bt_disable_adapter(void)
194 {
195         bt_status_t status;
196         bt_le_status_t le_status;
197
198         BT_INFO_C("Disable adapter");
199
200         le_status = _bt_core_get_le_status();
201         BT_DBG("le_status : %d", le_status);
202         if (le_status == BT_LE_ACTIVATED) {
203                 /* Turn off PSCAN, (ISCAN if needed) */
204                 /* Return with 0 for the Disabled response. */
205                 __bt_core_set_status(BT_DEACTIVATED);
206                 BT_INFO("BR/EDR is disabled. now LE only mode");
207                 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
208                 return 0;
209         }
210
211         status = _bt_core_get_status();
212         if (status == BT_ACTIVATING) {
213                 /* Forcely terminate */
214 #ifdef USB_BLUETOOTH
215                 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
216                 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
217 #else
218                 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
219 #endif
220                         BT_ERR("running script failed");
221                 }
222                 _bt_core_terminate();
223                 return 0;
224         } else if (status != BT_ACTIVATED) {
225                 BT_ERR("Invalid state %d", status);
226         }
227
228         __bt_core_set_status(BT_DEACTIVATING);
229 #ifdef USB_BLUETOOTH
230         char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
231         if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
232 #else
233         if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
234 #endif
235                 BT_ERR("running script failed");
236                 __bt_core_set_status( BT_ACTIVATED);
237                 return -1;
238         }
239
240         return 0;
241 }
242
243 int _bt_enable_adapter_le(void)
244 {
245         BT_DBG("");
246
247         int ret;
248         bt_status_t status;
249         bt_le_status_t le_status;
250         le_status = _bt_core_get_le_status();
251         retv_if(le_status != BT_LE_DEACTIVATED, -1);
252
253         status = _bt_core_get_status();
254         if (status == BT_DEACTIVATED) {
255                 __bt_core_set_le_status(BT_LE_ACTIVATING);
256                 BT_DBG("Activate BT");
257 #ifdef USB_BLUETOOTH
258                 char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
259                 ret = __execute_command("/usr/bin/hciconfig", argv_up);
260 #else
261                 ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
262 #endif
263                 if (ret < 0) {
264                         BT_ERR("running script failed");
265                         ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh &", NULL);
266                         __bt_core_set_status(BT_DEACTIVATED);
267                         __bt_core_set_le_status(BT_LE_DEACTIVATED);
268                         return -1;
269                 }
270         } else {
271                 __bt_core_set_le_status(BT_LE_ACTIVATED);
272                 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
273         }
274
275         return 0;
276 }
277
278 int _bt_disable_adapter_le(void)
279 {
280         BT_DBG("+");
281
282         bt_status_t status;
283         bt_le_status_t le_status;
284
285         le_status = _bt_core_get_le_status();
286         retv_if(le_status == BT_LE_DEACTIVATED, 0);
287         retv_if(le_status == BT_LE_DEACTIVATING, -1);
288
289         status = _bt_core_get_status();
290         BT_DBG("status : %d", status);
291
292         if (status == BT_DEACTIVATED) {
293                 __bt_core_set_le_status(BT_LE_DEACTIVATING);
294 #ifdef USB_BLUETOOTH
295                 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
296                 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
297 #else
298                 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
299 #endif
300                         BT_ERR("running script failed");
301                         __bt_core_set_le_status(BT_LE_ACTIVATED);
302                         return -1;
303                 }
304         } else {
305                 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
306         }
307
308         __bt_core_set_le_status(BT_LE_DEACTIVATED);
309
310         BT_DBG("-");
311         return 0;
312 }
313
314 int _bt_core_service_request_adapter(int service_function)
315 {
316         int ret = -1;
317
318         GArray *in_param1 = NULL;
319         GArray *in_param2 = NULL;
320         GArray *in_param3 = NULL;
321         GArray *in_param4 = NULL;
322         GArray *out_param = NULL;
323
324         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
325
326         ret = _bt_core_service_request(BT_CORE_SERVICE, service_function,
327                         in_param1, in_param2, in_param3, in_param4, &out_param);
328         if (ret < 0)
329                 BT_ERR("_bt_core_service_request_adapter() failed");
330
331         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
332
333         return ret;
334 }
335
336 static gboolean __bt_core_check_the_adapter_path(GDBusConnection *conn)
337 {
338         GError *err = NULL;
339         GDBusProxy *manager_proxy = NULL;
340         GVariant *result = NULL;
341         char *adapter_path = NULL;
342
343         if(conn == NULL)
344                 return FALSE;
345
346         manager_proxy =  g_dbus_proxy_new_sync(conn,
347                         G_DBUS_PROXY_FLAGS_NONE, NULL,
348                         "org.bluez",
349                         "/",
350                         "org.freedesktop.DBus.ObjectManager",
351                         NULL, &err);
352
353         if (!manager_proxy) {
354                 if (err != NULL) {
355                         BT_ERR("Unable to create proxy: %s", err->message);
356                         g_clear_error(&err);
357                 } else {
358                         BT_ERR("Fail to create proxy");
359                 }
360                 goto fail;
361         }
362
363         result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
364                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
365         if (!result) {
366                 if (err != NULL) {
367                         BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
368                         g_clear_error(&err);
369                 } else{
370                         BT_ERR("Fail to get DefaultAdapter");
371                 }
372                 goto fail;
373         }
374
375         if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
376                 BT_ERR("Incorrect result\n");
377                 goto fail;
378         }
379
380         g_variant_get(result, "(&o)", &adapter_path);
381
382         if (adapter_path == NULL ||
383                 strlen(adapter_path) >= BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX) {
384                 BT_ERR("Adapter path is inproper\n");
385                 goto fail;
386         }
387
388         g_variant_unref(result);
389         g_object_unref(manager_proxy);
390
391         return TRUE;
392
393 fail:
394         if (result)
395                 g_variant_unref(result);
396
397         if (manager_proxy)
398                 g_object_unref(manager_proxy);
399
400         return FALSE;
401 }
402
403 void _bt_core_update_status(void)
404 {
405         int bt_status = VCONFKEY_BT_STATUS_OFF;
406         int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
407         gboolean ret = FALSE;
408
409         ret = __bt_core_check_the_adapter_path(_bt_core_get_gdbus_connection());
410         BT_INFO("check the real status of bt_adapter");
411
412         if (ret != TRUE) {
413                 __bt_core_set_status(BT_DEACTIVATED);
414                 __bt_core_set_le_status(BT_DEACTIVATED);
415         } else {
416                 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
417                         BT_ERR("no bluetooth device info, so BT was disabled at previous session");
418                 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
419                         BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
420
421                 BT_INFO("bt_status = %d, bt_le_status = %d", bt_status, bt_le_status);
422
423                 if(bt_status == VCONFKEY_BT_STATUS_ON)
424                         __bt_core_set_status(BT_ACTIVATED);
425                 if(bt_le_status == VCONFKEY_BT_LE_STATUS_ON)
426                         __bt_core_set_le_status(BT_ACTIVATED);
427         }
428 }
429
430 gboolean _bt_core_enable_adapter(void)
431 {
432         int ret;
433
434         _bt_set_flightmode_request(FALSE);
435         if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
436                 BT_ERR("Set vconf failed");
437
438         ret = _bt_enable_adapter();
439         if (ret < 0)
440                 return FALSE;
441         else
442                 return TRUE;
443 }
444
445 gboolean _bt_core_disable_adapter(void)
446 {
447         int ret;
448
449         _bt_set_flightmode_request(FALSE);
450         if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
451                 BT_ERR("Set vconf failed");
452
453         ret = _bt_disable_adapter();
454         if (ret < 0)
455                 return FALSE;
456         else
457                 return TRUE;
458 }
459
460 gboolean _bt_core_recover_adapter(void)
461 {
462         int ret;
463         int ret_le;
464
465         BT_INFO_C("Recover bt adapter");
466
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");
470
471         is_recovery_mode = TRUE;
472
473         _bt_core_update_status();
474
475         if (_bt_core_get_status() == BT_ACTIVATED) {
476                 _bt_core_set_bt_status(BT_RECOVERY_MODE, 1);
477                 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
478         }
479         if (_bt_core_get_le_status() == BT_LE_ACTIVATED) {
480                 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 1);
481                 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
482         }
483
484         ret = _bt_disable_adapter();
485         if (ret < 0)
486                 BT_ERR("_bt_disable_adapter() failed");
487         ret_le = _bt_disable_adapter_le();
488         if (ret_le < 0)
489                 BT_ERR("_bt_disable_adapter_le() failed");
490
491         return TRUE;
492 }
493
494 gboolean _bt_core_enable_adapter_le(void)
495 {
496         int ret;
497
498         ret = _bt_enable_adapter_le();
499         if (ret < 0)
500                 return FALSE;
501         else
502                 return TRUE;
503 }
504
505 gboolean _bt_core_disable_adapter_le(void)
506 {
507         BT_DBG("+");
508
509         int ret;
510
511         ret = _bt_disable_adapter_le();
512         if (ret < 0)
513                 return FALSE;
514         else
515                 return TRUE;
516 }
517
518 gboolean __bt_core_reset_adapter(void)
519 {
520         /* Forcely terminate */
521         if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0) {
522                 BT_ERR("running script failed");
523         }
524         _bt_core_terminate();
525
526         return TRUE;
527 }
528
529 static gboolean __bt_core_enable_core_timeout_cb(gpointer data)
530 {
531         BT_DBG("+");
532
533         _bt_core_init_vconf_value();
534
535         return FALSE;
536 }
537
538 gboolean _bt_core_enable_core(void)
539 {
540         BT_DBG("+");
541
542         _bt_core_update_status();
543
544         g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
545
546         BT_DBG("-");
547         return TRUE;
548 }
549
550 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
551 {
552
553         char *cmd = NULL;
554         char *arg_list[3] = { NULL, NULL, NULL };
555
556         BT_DBG("Test item : %s", type);
557
558         if (g_strcmp0(type, "Enable_RF_Test") == 0) {
559                 cmd = "/usr/etc/bluetooth/bt-edutm-on.sh";
560                 arg_list[0] = "bt-edutm-on.sh";
561         } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
562                 cmd = "/usr/etc/bluetooth/bt-edutm-off.sh";
563                 arg_list[0] = "bt-edutm-off.sh";
564         } else if (g_strcmp0(type, "Slave_Mode") == 0) {
565                 cmd = "/usr/etc/bluetooth/bt-mode-slave.sh";
566                 arg_list[0] = "bt-mode-slave.sh";
567         } else if (g_strcmp0(type, "Master_Mode") == 0) {
568                 cmd = "/usr/etc/bluetooth/bt-mode-master.sh";
569                 arg_list[0] = "bt-mode-master.sh";
570         } else if (g_strcmp0(type, "SSP_Debug_Mode") == 0) {
571                 cmd = "/usr/etc/bluetooth/bt-set-ssp-debug-mode.sh";
572                 arg_list[0] = "bt-set-ssp-debug-mode.sh";
573                 arg_list[1] = (char *)arg;
574         } else if (g_strcmp0(type, "RF_Channel") == 0) {
575                 cmd = "/usr/etc/bluetooth/bt-enable-rf-channel.sh";
576                 arg_list[0] = "bt-enable-rf-channel.sh";
577                 arg_list[1] = (char *)arg;
578         } else {
579                 _bt_core_terminate();
580                 return FALSE;
581         }
582
583         BT_DBG("Run %s", cmd);
584         if (__execute_command(cmd, arg_list) < 0) {
585                 BT_ERR("running script failed");
586         }
587
588         _bt_core_terminate();
589         return TRUE;
590 }
591
592 static gboolean __bt_core_recovery_cb(gpointer data)
593 {
594         int ret = 0;
595         gboolean is_request_failed = FALSE;
596         static gboolean is_first_failure = TRUE;
597
598         BT_DBG("+");
599
600         if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
601                 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
602                 if (ret < 0)
603                         is_request_failed = TRUE;
604         }
605
606         if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
607                 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
608                 if (ret < 0)
609                         is_request_failed = TRUE;
610         }
611
612         if (is_request_failed == TRUE) {
613                 BT_ERR("Recovery is failed.");
614                 if (is_first_failure == TRUE) {
615                         g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
616                         is_first_failure = FALSE;
617                         return FALSE;
618                 } else {
619                         is_first_failure = TRUE;
620                         return FALSE;
621                 }
622         } else
623                 is_first_failure = TRUE;
624
625         if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
626                 _bt_core_set_bt_status(BT_RECOVERY_MODE, 0);
627                 ret = _bt_enable_adapter();
628                 if (ret < 0)
629                         BT_ERR("_bt_enable_adapter() failed");
630         }
631         if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
632                 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 0);
633                 ret = _bt_enable_adapter_le();
634                 if (ret < 0)
635                         BT_ERR("_bt_enable_adapter_le() failed");
636         }
637
638         is_recovery_mode = FALSE;
639
640         BT_DBG("-");
641
642         return FALSE;
643 }
644
645 static gboolean __bt_core_enable_timeout_cb(gpointer data)
646 {
647         bt_status_t adapter_status;
648         bt_le_status_t adapter_status_le;
649
650         BT_DBG("");
651
652         if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
653                 BT_ERR("Set vconf failed");
654
655         adapter_status = _bt_core_get_status();
656         adapter_status_le = _bt_core_get_le_status();
657
658         if (adapter_status == BT_DEACTIVATED &&
659                         _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
660                 _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
661                 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
662                 _bt_enable_adapter();
663         }
664
665         if (adapter_status_le == BT_LE_DEACTIVATED &&
666                         _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
667                 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
668                 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
669                 _bt_enable_adapter_le();
670         }
671
672         return FALSE;
673 }
674
675 static gboolean __bt_core_disable_timeout_cb(gpointer data)
676 {
677         bt_status_t adapter_status;
678         bt_le_status_t adapter_status_le;
679
680         BT_DBG("");
681
682         if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
683                 BT_ERR("Set vconf failed");
684
685         adapter_status = _bt_core_get_status();
686         adapter_status_le = _bt_core_get_le_status();
687
688         if (adapter_status == BT_ACTIVATED) {
689                 int bt_status_before_mode = 0;
690
691                 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
692                         _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
693
694                 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
695                 _bt_disable_adapter();
696         }
697
698         if (adapter_status_le == BT_LE_ACTIVATED) {
699                 int bt_le_status_before_mode = 0;
700
701                 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
702                         _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
703
704                 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
705                 _bt_disable_adapter_le();
706         }
707
708         return FALSE;
709 }
710 #if 0
711 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value)
712 {
713         int ret;
714         bundle *b = NULL;
715
716         b = bundle_create();
717
718         bundle_add_str(b, key, value);
719
720         ret = eventsystem_request_sending_system_event(event, b);
721
722         BT_DBG("request_sending_system_event result: %d", ret);
723
724         bundle_free(b);
725
726         return ret;
727 }
728 #endif
729 void _bt_core_adapter_added_cb(void)
730 {
731         bt_status_t status;
732         bt_le_status_t le_status;
733         gboolean flight_mode_status;
734
735         BT_DBG("");
736
737         status = _bt_core_get_status();
738         BT_DBG("status : %d", status);
739         le_status = _bt_core_get_le_status();
740         BT_DBG("le_status : %d", le_status);
741
742         if (status == BT_ACTIVATING)
743                 __bt_core_set_status(BT_ACTIVATED);
744         if (le_status == BT_LE_ACTIVATING)
745                 __bt_core_set_le_status(BT_LE_ACTIVATED);
746
747         flight_mode_status = _bt_core_is_flight_mode_enabled();
748
749         if (flight_mode_status == TRUE && _bt_is_flightmode_request() == TRUE) {
750                 _bt_set_flightmode_request(FALSE);
751                 g_timeout_add(2000, (GSourceFunc)__bt_core_disable_timeout_cb, NULL);
752                 return;
753         }
754         _bt_set_flightmode_request(FALSE);
755         _bt_core_terminate();
756 }
757
758 void _bt_core_adapter_removed_cb(void)
759 {
760         int flight_mode_value = 0;
761         int power_saving_mode = 0;
762         gboolean flight_mode_status;
763         static int timer_id = -1;
764
765         BT_DBG("");
766
767         __bt_core_set_status(BT_DEACTIVATED);
768         __bt_core_set_le_status(BT_LE_DEACTIVATED);
769         if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
770                 BT_ERR("Set vconf failed");
771
772         if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
773                 BT_ERR("Set vconf failed");
774
775 #if 0
776         if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
777                                                 EVT_VAL_BT_OFF) != ES_R_OK)
778                 BT_ERR("Fail to set value");
779
780         if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
781                                                 EVT_VAL_BT_LE_OFF) != ES_R_OK)
782                 BT_ERR("Fail to set value");
783 #endif
784         if (is_recovery_mode == TRUE)
785         {
786                 if (timer_id < 0)
787                         timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
788                 return;
789         }
790
791         if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value) != 0)
792                 BT_ERR("Fail to get the flight_mode_deactivated value");
793
794         if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &power_saving_mode) != 0)
795                 BT_ERR("Fail to get the ps_mode_deactivated value");
796
797         flight_mode_status = _bt_core_is_flight_mode_enabled();
798
799         if (flight_mode_status == FALSE && _bt_is_flightmode_request() == TRUE) {
800                 _bt_set_flightmode_request(FALSE);
801                 if (timer_id < 0)
802                         timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_enable_timeout_cb, NULL);
803                 return;
804         }
805         _bt_set_flightmode_request(FALSE);
806
807         if (flight_mode_value == 1 || power_saving_mode == 1){
808                 BT_DBG("Bt Core not terminated");
809                 return;
810         }
811
812         _bt_core_terminate();
813 }