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