Enable bt_adapter_recover function in the platform
[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         /* busact module is not in platform */
386 #ifndef TIZEN_FEATURE_BUSACT
387         return BLUETOOTH_ERROR_NONE;
388 #else
389         int ret = -1;
390
391         GArray *in_param1 = NULL;
392         GArray *in_param2 = NULL;
393         GArray *in_param3 = NULL;
394         GArray *in_param4 = NULL;
395         GArray *out_param = NULL;
396
397         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
398
399         ret = _bt_core_service_request(BT_CORE_SERVICE, service_function,
400                         in_param1, in_param2, in_param3, in_param4, &out_param);
401         if (ret < 0)
402                 BT_ERR("_bt_core_service_request_adapter() failed");
403
404         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
405
406         return ret;
407 #endif
408 }
409
410 static gboolean __bt_core_check_the_adapter_path(GDBusConnection *conn)
411 {
412         GError *err = NULL;
413         GDBusProxy *manager_proxy = NULL;
414         GVariant *result = NULL;
415         char *adapter_path = NULL;
416
417         if (conn == NULL)
418                 return FALSE;
419
420         manager_proxy =  g_dbus_proxy_new_sync(conn,
421                         G_DBUS_PROXY_FLAGS_NONE, NULL,
422                         "org.bluez",
423                         "/",
424                         "org.freedesktop.DBus.ObjectManager",
425                         NULL, &err);
426
427         if (!manager_proxy) {
428                 if (err != NULL) {
429                         BT_ERR("Unable to create proxy: %s", err->message);
430                         g_clear_error(&err);
431                 } else {
432                         BT_ERR("Fail to create proxy");
433                 }
434                 goto fail;
435         }
436
437         result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
438                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
439         if (!result) {
440                 if (err != NULL) {
441                         BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
442                         g_clear_error(&err);
443                 } else{
444                         BT_ERR("Fail to get DefaultAdapter");
445                 }
446                 goto fail;
447         }
448
449         if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
450                 BT_ERR("Incorrect result\n");
451                 goto fail;
452         }
453
454         g_variant_get(result, "(&o)", &adapter_path);
455
456         if (adapter_path == NULL ||
457                 strlen(adapter_path) >= BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX) {
458                 BT_ERR("Adapter path is inproper\n");
459                 goto fail;
460         }
461
462         g_variant_unref(result);
463         g_object_unref(manager_proxy);
464
465         return TRUE;
466
467 fail:
468         if (result)
469                 g_variant_unref(result);
470
471         if (manager_proxy)
472                 g_object_unref(manager_proxy);
473
474         return FALSE;
475 }
476
477 void _bt_core_update_status(void)
478 {
479         int bt_status = VCONFKEY_BT_STATUS_OFF;
480         int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
481         gboolean ret = FALSE;
482
483         ret = __bt_core_check_the_adapter_path(_bt_core_get_gdbus_connection());
484         BT_INFO("check the real status of bt_adapter");
485
486         if (ret != TRUE) {
487                 __bt_core_set_status(BT_DEACTIVATED);
488                 __bt_core_set_le_status(BT_DEACTIVATED);
489         } else {
490                 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
491                         BT_ERR("no bluetooth device info, so BT was disabled at previous session");
492                 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
493                         BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
494
495                 BT_INFO("bt_status = %d, bt_le_status = %d", bt_status, bt_le_status);
496
497                 if (bt_status & VCONFKEY_BT_STATUS_ON)
498                         __bt_core_set_status(BT_ACTIVATED);
499                 if (bt_le_status & VCONFKEY_BT_LE_STATUS_ON)
500                         __bt_core_set_le_status(BT_ACTIVATED);
501         }
502 }
503
504 gboolean _bt_core_enable_adapter(void)
505 {
506         int ret;
507
508         _bt_set_flightmode_request(FALSE);
509         if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
510                 BT_ERR("Set vconf failed");
511
512         ret = _bt_enable_adapter();
513         if (ret < 0)
514                 return FALSE;
515         else
516                 return TRUE;
517 }
518
519 gboolean _bt_core_disable_adapter(void)
520 {
521         int ret;
522
523         _bt_set_flightmode_request(FALSE);
524         if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
525                 BT_ERR("Set vconf failed");
526
527         ret = _bt_disable_adapter();
528         if (ret < 0)
529                 return FALSE;
530         else
531                 return TRUE;
532 }
533
534 gboolean _bt_core_recover_adapter(void)
535 {
536         int ret;
537 #ifdef TIZEN_FEATURE_BUSACT
538         int ret_le;
539 #endif
540         BT_INFO_C("Recover bt adapter");
541
542         _bt_set_flightmode_request(FALSE);
543         if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
544                 BT_ERR("Set vconf failed");
545
546         is_recovery_mode = TRUE;
547
548         _bt_core_update_status();
549
550         if (_bt_core_get_status() == BT_ACTIVATED) {
551                 _bt_core_set_bt_status(BT_RECOVERY_MODE, 1);
552                 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
553         }
554         if (_bt_core_get_le_status() == BT_LE_ACTIVATED) {
555                 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 1);
556                 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
557         }
558
559         ret = _bt_disable_adapter();
560         if (ret < 0)
561                 BT_ERR("_bt_disable_adapter() failed");
562
563 /* In platform, don't seperate BR/EDR and LE status */
564 #if 0
565         ret_le = _bt_disable_adapter_le();
566         if (ret_le < 0)
567                 BT_ERR("_bt_disable_adapter_le() failed");
568 #endif
569         return TRUE;
570 }
571
572 gboolean _bt_core_enable_adapter_le(void)
573 {
574         int ret;
575
576         ret = _bt_enable_adapter_le();
577         if (ret < 0)
578                 return FALSE;
579         else
580                 return TRUE;
581 }
582
583 gboolean _bt_core_disable_adapter_le(void)
584 {
585         BT_DBG("+");
586
587         int ret;
588
589         ret = _bt_disable_adapter_le();
590         if (ret < 0)
591                 return FALSE;
592         else
593                 return TRUE;
594 }
595
596 gboolean __bt_core_reset_adapter(void)
597 {
598         /* Forcely terminate */
599         if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0) {
600                 BT_ERR("running script failed");
601         }
602         _bt_core_terminate();
603
604         return TRUE;
605 }
606
607 static gboolean __bt_core_enable_core_timeout_cb(gpointer data)
608 {
609         BT_DBG("+");
610
611         _bt_core_init_vconf_value();
612
613         return FALSE;
614 }
615
616 gboolean _bt_core_enable_core(void)
617 {
618         BT_DBG("+");
619
620         _bt_core_update_status();
621
622         g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
623
624         BT_DBG("-");
625         return TRUE;
626 }
627
628 gboolean _bt_core_set_transfer_value(gboolean value)
629 {
630         const char *event_val = NULL;
631         gboolean ret = TRUE;
632
633         BT_DBG("value: %d", value);
634
635         event_val = value ? EVT_VAL_BT_TRANSFERING : EVT_VAL_BT_NON_TRANSFERING;
636
637         if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_TRANSFERING_STATE,
638                                                 event_val) != ES_R_OK) {
639                 BT_ERR("Fail to set BT state value");
640                 ret = FALSE;
641         }
642
643         if (ret == FALSE || value == FALSE) {
644                 BT_DBG("Terminate bt-core process");
645                 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
646         }
647
648         BT_DBG("-");
649         return ret;
650 }
651
652 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
653 {
654
655         char *cmd = NULL;
656         char *arg_list[3] = { NULL, NULL, NULL };
657
658         BT_DBG("Test item : %s", type);
659
660         if (g_strcmp0(type, "Enable_RF_Test") == 0) {
661                 cmd = "/usr/etc/bluetooth/bt-edutm-on.sh";
662                 arg_list[0] = "bt-edutm-on.sh";
663         } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
664                 cmd = "/usr/etc/bluetooth/bt-edutm-off.sh";
665                 arg_list[0] = "bt-edutm-off.sh";
666         } else if (g_strcmp0(type, "Slave_Mode") == 0) {
667                 cmd = "/usr/etc/bluetooth/bt-mode-slave.sh";
668                 arg_list[0] = "bt-mode-slave.sh";
669         } else if (g_strcmp0(type, "Master_Mode") == 0) {
670                 cmd = "/usr/etc/bluetooth/bt-mode-master.sh";
671                 arg_list[0] = "bt-mode-master.sh";
672         } else if (g_strcmp0(type, "SSP_Debug_Mode") == 0) {
673                 cmd = "/usr/etc/bluetooth/bt-set-ssp-debug-mode.sh";
674                 arg_list[0] = "bt-set-ssp-debug-mode.sh";
675                 arg_list[1] = (char *)arg;
676         } else if (g_strcmp0(type, "RF_Channel") == 0) {
677                 cmd = "/usr/etc/bluetooth/bt-enable-rf-channel.sh";
678                 arg_list[0] = "bt-enable-rf-channel.sh";
679                 arg_list[1] = (char *)arg;
680         } else {
681                 _bt_core_terminate();
682                 return FALSE;
683         }
684
685         BT_DBG("Run %s", cmd);
686         if (__execute_command(cmd, arg_list) < 0) {
687                 BT_ERR("running script failed");
688         }
689
690         _bt_core_terminate();
691         return TRUE;
692 }
693
694 static gboolean __bt_core_recovery_cb(gpointer data)
695 {
696         int ret = 0;
697 #ifdef TIZEN_FEATURE_BUSACT
698         gboolean is_request_failed = FALSE;
699         static gboolean is_first_failure = TRUE;
700 #endif
701
702         BT_DBG("+");
703
704 #ifdef TIZEN_FEATURE_BUSACT
705         if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
706                 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
707                 if (ret < 0)
708                         is_request_failed = TRUE;
709         }
710
711         if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
712                 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
713                 if (ret < 0)
714                         is_request_failed = TRUE;
715         }
716
717         if (is_request_failed == TRUE) {
718                 BT_ERR("Recovery is failed.");
719                 if (is_first_failure == TRUE) {
720                         g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
721                         is_first_failure = FALSE;
722                         return FALSE;
723                 } else {
724                         is_first_failure = TRUE;
725                         return FALSE;
726                 }
727         } else
728                 is_first_failure = TRUE;
729 #endif
730
731         if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
732                 _bt_core_set_bt_status(BT_RECOVERY_MODE, 0);
733                 ret = _bt_enable_adapter();
734                 if (ret < 0)
735                         BT_ERR("_bt_enable_adapter() failed");
736         }
737
738 #if 0
739         if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
740                 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 0);
741                 ret = _bt_enable_adapter_le();
742                 if (ret < 0)
743                         BT_ERR("_bt_enable_adapter_le() failed");
744         }
745 #endif
746         is_recovery_mode = FALSE;
747
748         BT_DBG("-");
749
750         return FALSE;
751 }
752
753 static gboolean __bt_core_enable_timeout_cb(gpointer data)
754 {
755         bt_status_t adapter_status;
756         bt_le_status_t adapter_status_le;
757
758         BT_DBG("");
759
760         if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
761                 BT_ERR("Set vconf failed");
762
763         adapter_status = _bt_core_get_status();
764         adapter_status_le = _bt_core_get_le_status();
765
766         if (adapter_status == BT_DEACTIVATED &&
767                         _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
768                 _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
769                 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
770                 _bt_enable_adapter();
771         }
772
773         if (adapter_status_le == BT_LE_DEACTIVATED &&
774                         _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
775                 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
776                 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
777                 _bt_enable_adapter_le();
778         }
779
780         return FALSE;
781 }
782
783 static gboolean __bt_core_disable_timeout_cb(gpointer data)
784 {
785         bt_status_t adapter_status;
786         bt_le_status_t adapter_status_le;
787
788         BT_DBG("");
789
790         if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
791                 BT_ERR("Set vconf failed");
792
793         adapter_status = _bt_core_get_status();
794         adapter_status_le = _bt_core_get_le_status();
795
796         if (adapter_status == BT_ACTIVATED) {
797                 int bt_status_before_mode = 0;
798
799                 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
800                         _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
801
802                 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
803                 _bt_disable_adapter();
804         }
805
806         if (adapter_status_le == BT_LE_ACTIVATED) {
807                 int bt_le_status_before_mode = 0;
808
809                 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
810                         _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
811
812                 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
813                 _bt_disable_adapter_le();
814         }
815
816         return FALSE;
817 }
818
819 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value)
820 {
821         int ret;
822         bundle *b = NULL;
823
824         b = bundle_create();
825
826         bundle_add_str(b, key, value);
827
828         ret = eventsystem_send_system_event(event, b);
829
830         BT_DBG("eventsystem_send_system_event result: %d", ret);
831
832         bundle_free(b);
833
834         return ret;
835 }
836
837 void _bt_core_adapter_added_cb(void)
838 {
839         bt_status_t status;
840         bt_le_status_t le_status;
841         gboolean flight_mode_status;
842
843         BT_DBG("");
844
845         status = _bt_core_get_status();
846         BT_DBG("status : %d", status);
847         le_status = _bt_core_get_le_status();
848         BT_DBG("le_status : %d", le_status);
849
850         if (status == BT_ACTIVATING)
851                 __bt_core_set_status(BT_ACTIVATED);
852         if (le_status == BT_LE_ACTIVATING)
853                 __bt_core_set_le_status(BT_LE_ACTIVATED);
854
855         flight_mode_status = _bt_core_is_flight_mode_enabled();
856
857         if (flight_mode_status == TRUE && _bt_is_flightmode_request() == TRUE) {
858                 _bt_set_flightmode_request(FALSE);
859                 g_timeout_add(2000, (GSourceFunc)__bt_core_disable_timeout_cb, NULL);
860                 return;
861         }
862         _bt_set_flightmode_request(FALSE);
863
864         if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
865                                                 EVT_VAL_BT_ON) != ES_R_OK)
866                 BT_ERR("Fail to set BT state value");
867
868         if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
869                                                 EVT_VAL_BT_LE_ON) != ES_R_OK)
870                 BT_ERR("Fail to set BT LE state value");
871
872         _bt_core_terminate();
873 }
874
875 void _bt_core_adapter_removed_cb(void)
876 {
877         int flight_mode_value = 0;
878         int power_saving_mode = 0;
879         gboolean flight_mode_status;
880         static int timer_id = -1;
881
882         BT_DBG("is_recovery_mode: %d", is_recovery_mode);
883
884         __bt_core_set_status(BT_DEACTIVATED);
885         __bt_core_set_le_status(BT_LE_DEACTIVATED);
886         if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
887                 BT_ERR("Set vconf failed");
888         if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
889                 BT_ERR("Set vconf failed");
890
891         if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
892                                                 EVT_VAL_BT_OFF) != ES_R_OK)
893                 BT_ERR("Fail to set value");
894
895         if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
896                                                 EVT_VAL_BT_LE_OFF) != ES_R_OK)
897                 BT_ERR("Fail to set value");
898
899         if (is_recovery_mode == TRUE) {
900                 if (timer_id < 0)
901                         timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
902                 return;
903         }
904
905         if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value) != 0)
906                 BT_ERR("Fail to get the flight_mode_deactivated value");
907
908         if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &power_saving_mode) != 0)
909                 BT_ERR("Fail to get the ps_mode_deactivated value");
910
911         flight_mode_status = _bt_core_is_flight_mode_enabled();
912
913         if (flight_mode_status == FALSE && _bt_is_flightmode_request() == TRUE) {
914                 _bt_set_flightmode_request(FALSE);
915                 if (timer_id < 0)
916                         timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_enable_timeout_cb, NULL);
917                 return;
918         }
919         _bt_set_flightmode_request(FALSE);
920
921         if (flight_mode_value == 1 || power_saving_mode == 1) {
922                 BT_DBG("Bt Core not terminated");
923                 return;
924         }
925
926         _bt_core_terminate();
927 }