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