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