Enable to use USB bluetooth dongle
[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 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;
40
41 static int bt_status_before[BT_MODE_MAX] = { VCONFKEY_BT_STATUS_OFF, };
42 static int bt_le_status_before[BT_MODE_MAX] = { 0, };
43
44 static DBusGConnection *conn = NULL;
45
46 static void __bt_core_set_status(bt_status_t status)
47 {
48         adapter_status = status;
49 }
50
51 bt_status_t _bt_core_get_status(void)
52 {
53         return adapter_status;
54 }
55
56 static void __bt_core_set_le_status(bt_le_status_t status)
57 {
58         adapter_le_status = status;
59 }
60
61 bt_le_status_t _bt_core_get_le_status(void)
62 {
63         return adapter_le_status;
64 }
65
66 int _bt_core_get_bt_status(bt_mode_e mode)
67 {
68         return bt_status_before[mode];
69 }
70
71 int _bt_core_get_bt_le_status(bt_mode_e mode)
72 {
73         return bt_le_status_before[mode];
74 }
75
76 void _bt_core_set_bt_status(bt_mode_e mode, int status)
77 {
78         bt_status_before[mode] = status;
79 }
80
81 void _bt_core_set_bt_le_status(bt_mode_e mode, int status)
82 {
83         bt_le_status_before[mode] = status;
84 }
85
86 gboolean _bt_core_is_recovery_mode(void)
87 {
88         return is_recovery_mode;
89 }
90
91 gboolean _bt_core_is_flight_mode_enabled(void)
92 {
93 #ifdef TIZEN_BT_FLIGHTMODE_ENABLED
94         int isFlightMode = 0;
95         int ret = -1;
96
97         ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &isFlightMode);
98         if (ret != 0) {
99                 BT_ERR("vconf_get_bool failed");
100         }
101         return isFlightMode;
102 #else
103         return FALSE;
104 #endif
105 }
106
107 static int __execute_command(const char *cmd, char *const arg_list[])
108 {
109         int pid;
110         int pid2;
111         int status = 0;
112         BT_DBG("+");
113
114         pid = fork();
115         switch (pid) {
116         case -1:
117                 BT_ERR("fork failed");
118                 return -1;
119
120         case 0:
121                 pid2 = fork();
122                 if (pid2 == -1) {
123                         BT_ERR("fork failed");
124                 } else if (pid2 == 0) {
125                         execv(cmd, arg_list);
126                         exit(256);
127                 }
128                 exit(0);
129                 break;
130
131         default:
132                 BT_DBG("parent : forked[%d]", pid);
133                 waitpid(pid, &status, 0);
134                 BT_DBG("child is terminated : %d", status);
135                 break;
136         }
137         BT_DBG("-");
138         return 0;
139 }
140
141 int _bt_enable_adapter(void)
142 {
143         int ret;
144         bt_status_t status;
145         bt_le_status_t le_status;
146
147         BT_INFO("");
148
149         status = _bt_core_get_status();
150         if (status != BT_DEACTIVATED) {
151                 BT_ERR("Invalid state %d", status);
152                 return -1;
153         }
154
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.");
161                 return 0;
162         }
163
164         __bt_core_set_status(BT_ACTIVATING);
165 #ifdef USB_BLUETOOTH
166         char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", "pscan", NULL};
167         ret = __execute_command("/usr/bin/hciconfig", argv_up);
168 #else
169         ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
170 #endif
171         if (ret < 0) {
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);
175                 return -1;
176         }
177
178         return 0;
179 }
180
181 int _bt_disable_adapter(void)
182 {
183         bt_status_t status;
184         bt_le_status_t le_status;
185
186         BT_INFO_C("Disable adapter");
187
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");
195                 return 0;
196         }
197
198         status = _bt_core_get_status();
199         if (status == BT_ACTIVATING) {
200                 /* Forcely terminate */
201 #ifdef USB_BLUETOOTH
202                 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
203                 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
204 #else
205                 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
206 #endif
207                         BT_ERR("running script failed");
208                 }
209                 _bt_core_terminate();
210                 return 0;
211         } else if (status != BT_ACTIVATED) {
212                 BT_ERR("Invalid state %d", status);
213         }
214
215         __bt_core_set_status(BT_DEACTIVATING);
216 #ifdef USB_BLUETOOTH
217         char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
218         if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
219 #else
220         if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
221 #endif
222                 BT_ERR("running script failed");
223                 __bt_core_set_status( BT_ACTIVATED);
224                 return -1;
225         }
226
227         return 0;
228 }
229
230 int _bt_enable_adapter_le(void)
231 {
232         BT_DBG("");
233
234         int ret;
235         bt_status_t status;
236         bt_le_status_t le_status;
237         le_status = _bt_core_get_le_status();
238         retv_if(le_status != BT_LE_DEACTIVATED, -1);
239
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");
244 #ifdef USB_BLUETOOTH
245                 char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", "pscan", NULL};
246                 ret = __execute_command("/usr/bin/hciconfig", argv_up);
247 #else
248                 ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
249 #endif
250                 if (ret < 0) {
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);
255                         return -1;
256                 }
257         } else {
258                 __bt_core_set_le_status(BT_LE_ACTIVATED);
259         }
260
261         return 0;
262 }
263
264 int _bt_disable_adapter_le(void)
265 {
266         BT_DBG("+");
267
268         bt_status_t status;
269         bt_le_status_t le_status;
270
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);
274
275         status = _bt_core_get_status();
276         BT_DBG("status : %d", status);
277
278         if (status == BT_DEACTIVATED) {
279                 __bt_core_set_le_status(BT_LE_DEACTIVATING);
280 #ifdef USB_BLUETOOTH
281                 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
282                 if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
283 #else
284                 if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
285 #endif
286                         BT_ERR("running script failed");
287                         __bt_core_set_le_status(BT_LE_ACTIVATED);
288                         return -1;
289                 }
290         }
291
292         __bt_core_set_le_status(BT_LE_DEACTIVATED);
293
294         BT_DBG("-");
295         return 0;
296 }
297
298 int _bt_core_service_request_adapter(int service_function)
299 {
300         int ret = -1;
301
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;
307
308         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
309
310         ret = _bt_core_service_request(BT_CORE_SERVICE, service_function,
311                         in_param1, in_param2, in_param3, in_param4, &out_param);
312         if (ret < 0)
313                 BT_ERR("_bt_core_service_request_adapter() failed");
314
315         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
316
317         return ret;
318 }
319
320 static void __bt_core_update_status(void)
321 {
322         int bt_status = VCONFKEY_BT_STATUS_OFF;
323         int bt_le_status = 0;
324
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");
327
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");
330
331         BT_INFO("bt_status = %d, bt_le_status = %d", bt_status, bt_le_status);
332
333         if (bt_status == VCONFKEY_BT_STATUS_OFF)
334                 __bt_core_set_status(BT_DEACTIVATED);
335         else
336                 __bt_core_set_status(BT_ACTIVATED);
337
338         if (bt_le_status == 0)
339                 __bt_core_set_le_status(BT_LE_DEACTIVATED);
340         else
341                 __bt_core_set_le_status(BT_LE_ACTIVATED);
342 }
343
344 gboolean _bt_core_enable_adapter(void)
345 {
346         int ret;
347
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");
351
352         ret = _bt_enable_adapter();
353         if (ret < 0)
354                 return FALSE;
355         else
356                 return TRUE;
357 }
358
359 gboolean _bt_core_disable_adapter(void)
360 {
361         int ret;
362
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");
366
367         ret = _bt_disable_adapter();
368         if (ret < 0)
369                 return FALSE;
370         else
371                 return TRUE;
372 }
373
374 gboolean _bt_core_recover_adapter(void)
375 {
376         int ret;
377         int ret_le;
378
379         BT_INFO_C("Recover bt adapter");
380
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");
384
385         is_recovery_mode = TRUE;
386
387         __bt_core_update_status();
388
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);
392         }
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);
396         }
397
398         ret = _bt_disable_adapter();
399         if (ret < 0)
400                 BT_ERR("_bt_disable_adapter() failed");
401         ret_le = _bt_disable_adapter_le();
402         if (ret_le < 0)
403                 BT_ERR("_bt_disable_adapter_le() failed");
404
405         return TRUE;
406 }
407
408 gboolean _bt_core_enable_adapter_le(void)
409 {
410         int ret;
411
412         ret = _bt_enable_adapter_le();
413         if (ret < 0)
414                 return FALSE;
415         else
416                 return TRUE;
417 }
418
419 gboolean _bt_core_disable_adapter_le(void)
420 {
421         BT_DBG("+");
422
423         int ret;
424
425         ret = _bt_disable_adapter_le();
426         if (ret < 0)
427                 return FALSE;
428         else
429                 return TRUE;
430 }
431
432 gboolean __bt_core_reset_adapter(void)
433 {
434         /* Forcely terminate */
435         if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0) {
436                 BT_ERR("running script failed");
437         }
438         _bt_core_terminate();
439
440         return TRUE;
441 }
442
443 static gboolean __bt_core_enable_core_timeout_cb(gpointer data)
444 {
445         BT_DBG("+");
446
447         _bt_core_init_vconf_value();
448
449         return FALSE;
450 }
451
452 gboolean _bt_core_enable_core(void)
453 {
454         BT_DBG("+");
455
456         __bt_core_update_status();
457
458         g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
459
460         BT_DBG("-");
461         return TRUE;
462 }
463
464 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
465 {
466
467         char *cmd = NULL;
468         char *arg_list[3] = { NULL, NULL, NULL };
469
470         BT_DBG("Test item : %s", type);
471
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;
492         } else {
493                 _bt_core_terminate();
494                 return FALSE;
495         }
496
497         BT_DBG("Run %s", cmd);
498         if (__execute_command(cmd, arg_list) < 0) {
499                 BT_ERR("running script failed");
500         }
501
502         _bt_core_terminate();
503         return TRUE;
504 }
505
506 static gboolean __bt_core_recovery_cb(gpointer data)
507 {
508         int ret = 0;
509         gboolean is_request_failed = FALSE;
510         static gboolean is_first_failure = TRUE;
511
512         BT_DBG("+");
513
514         if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
515                 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
516                 if (ret < 0)
517                         is_request_failed = TRUE;
518         }
519
520         if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
521                 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
522                 if (ret < 0)
523                         is_request_failed = TRUE;
524         }
525
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;
531                         return FALSE;
532                 } else {
533                         is_first_failure = TRUE;
534                         return FALSE;
535                 }
536         } else
537                 is_first_failure = TRUE;
538
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();
542                 if (ret < 0)
543                         BT_ERR("_bt_enable_adapter() failed");
544         }
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();
548                 if (ret < 0)
549                         BT_ERR("_bt_enable_adapter_le() failed");
550         }
551
552         is_recovery_mode = FALSE;
553
554         BT_DBG("-");
555
556         return FALSE;
557 }
558
559 static gboolean __bt_core_enable_timeout_cb(gpointer data)
560 {
561         bt_status_t adapter_status;
562         bt_le_status_t adapter_status_le;
563
564         BT_DBG("");
565
566         if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
567                 BT_ERR("Set vconf failed");
568
569         adapter_status = _bt_core_get_status();
570         adapter_status_le = _bt_core_get_le_status();
571
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();
577         }
578
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();
584         }
585
586         return FALSE;
587 }
588
589 static gboolean __bt_core_disable_timeout_cb(gpointer data)
590 {
591         bt_status_t adapter_status;
592         bt_le_status_t adapter_status_le;
593
594         BT_DBG("");
595
596         if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
597                 BT_ERR("Set vconf failed");
598
599         adapter_status = _bt_core_get_status();
600         adapter_status_le = _bt_core_get_le_status();
601
602         if (adapter_status == BT_ACTIVATED) {
603                 int bt_status_before_mode = 0;
604
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);
607
608                 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
609                 _bt_disable_adapter();
610         }
611
612         if (adapter_status_le == BT_LE_ACTIVATED) {
613                 int bt_le_status_before_mode = 0;
614
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);
617
618                 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
619                 _bt_disable_adapter_le();
620         }
621
622         return FALSE;
623 }
624 #if 0
625 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value)
626 {
627         int ret;
628         bundle *b = NULL;
629
630         b = bundle_create();
631
632         bundle_add_str(b, key, value);
633
634         ret = eventsystem_request_sending_system_event(event, b);
635
636         BT_DBG("request_sending_system_event result: %d", ret);
637
638         bundle_free(b);
639
640         return ret;
641 }
642 #endif
643 void _bt_core_adapter_added_cb(void)
644 {
645         bt_status_t status;
646         bt_le_status_t le_status;
647         gboolean flight_mode_status;
648
649         BT_DBG("");
650
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);
655
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);
660
661         flight_mode_status = _bt_core_is_flight_mode_enabled();
662
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);
666                 return;
667         }
668         _bt_set_flightmode_request(FALSE);
669         _bt_core_terminate();
670 }
671
672 void _bt_core_adapter_removed_cb(void)
673 {
674         int flight_mode_value = 0;
675         int power_saving_mode = 0;
676         gboolean flight_mode_status;
677         static int timer_id = -1;
678
679         BT_DBG("");
680
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");
685
686         if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
687                 BT_ERR("Set vconf failed");
688
689 #if 0
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");
693
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");
697 #endif
698         if (is_recovery_mode == TRUE)
699         {
700                 if (timer_id < 0)
701                         timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
702                 return;
703         }
704
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");
707
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");
710
711         flight_mode_status = _bt_core_is_flight_mode_enabled();
712
713         if (flight_mode_status == FALSE && _bt_is_flightmode_request() == TRUE) {
714                 _bt_set_flightmode_request(FALSE);
715                 if (timer_id < 0)
716                         timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_enable_timeout_cb, NULL);
717                 return;
718         }
719         _bt_set_flightmode_request(FALSE);
720
721         if (flight_mode_value == 1 || power_saving_mode == 1){
722                 BT_DBG("Bt Core not terminated");
723                 return;
724         }
725
726         _bt_core_terminate();
727 }