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