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