Add bt-driver-insmod.service trigger logic
[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         ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
193         if (ret < 0) {
194                 BT_ERR("Failed to run script");
195                 __bt_call_systemact_service(BT_SYSTEMACT_HCI_LOGGER_STOP);
196                 return -1;
197         }
198
199         /* We need to insert BT driver module by using 'insmod' in case of ARTIK530,
200          * and 'insmod' should be executed by systemd not bt-core. */
201         ret = __bt_call_systemact_service(BT_SYSTEMACT_BT_DRIVER_INSMOD);
202         if (ret < 0) {
203                 BT_ERR("Failed to call systemact service");
204                 return -1;
205         }
206
207         /* activate Bluez */
208         ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUEZ_START);
209         if (ret < 0) {
210                 BT_ERR("Failed to call systemact service");
211                 return -1;
212         }
213
214         /* activate bluetooth-share */
215         ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUETOOTH_SHARE_START);
216         if (ret < 0) {
217                 BT_ERR("Failed to call systemact service");
218                 return -1;
219         }
220
221
222         return 0;
223
224
225
226 }
227
228 int _bt_enable_adapter(void)
229 {
230         int ret;
231         bt_status_t status;
232 #if 0
233         bt_le_status_t le_status;
234 #endif
235         BT_INFO("");
236
237         status = _bt_core_get_status();
238         if (status != BT_DEACTIVATED) {
239                 BT_ERR("Invalid state %d", status);
240                 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
241                 return -1;
242         }
243
244 #if 0 /* only the concept of private */
245         le_status = _bt_core_get_le_status();
246         if (le_status == BT_LE_ACTIVATED) {
247                 /* Turn on PSCAN, (ISCAN if needed) */
248                 /* Return with 0 for the Enabled response. */
249                 __bt_core_set_status(BT_ACTIVATED);
250                 BT_INFO("BR/EDR is enabled.");
251                 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
252                 return 0;
253         }
254 #endif
255
256         __bt_core_set_status(BT_ACTIVATING);
257         if (TIZEN_FEATURE_BT_USB_DONGLE) {
258                 char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
259                 ret = __execute_command("/usr/bin/hciconfig", argv_up);
260         } else {
261                 ret = __bt_stack_up();
262         }
263         if (ret < 0) {
264                 BT_ERR("running script failed");
265                 if (TIZEN_FEATURE_BT_USB_DONGLE) {
266                         char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
267                         ret = __execute_command("/usr/bin/hciconfig", argv_down);
268                 } else {
269                         ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
270                 }
271                 __bt_core_set_status(BT_DEACTIVATED);
272                 return -1;
273         }
274
275         return 0;
276 }
277
278 int _bt_disable_adapter(void)
279 {
280         bt_status_t status;
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         return 0;
378 }
379
380 int _bt_enable_adapter_le(void)
381 {
382         BT_DBG("");
383
384         int ret;
385         bt_status_t status;
386         bt_le_status_t le_status;
387         le_status = _bt_core_get_le_status();
388         retv_if(le_status != BT_LE_DEACTIVATED, -1);
389
390         status = _bt_core_get_status();
391         if (status == BT_DEACTIVATED) {
392                 __bt_core_set_le_status(BT_LE_ACTIVATING);
393                 BT_DBG("Activate BT");
394                 if (TIZEN_FEATURE_BT_USB_DONGLE) {
395                         char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
396                         ret = __execute_command("/usr/bin/hciconfig", argv_up);
397                 } else {
398                         ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
399                 }
400                 if (ret < 0) {
401                         BT_ERR("running script failed");
402                         if (TIZEN_FEATURE_BT_USB_DONGLE) {
403                                 char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
404                                 ret = __execute_command("/usr/bin/hciconfig", argv_down);
405                         } else {
406                                 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh &", NULL);
407                         }
408                         __bt_core_set_status(BT_DEACTIVATED);
409                         __bt_core_set_le_status(BT_LE_DEACTIVATED);
410                         return -1;
411                 }
412         } else {
413                 __bt_core_set_le_status(BT_LE_ACTIVATED);
414                 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
415         }
416 #ifdef TIZEN_FEATURE_BT_HPS
417         ret = _bt_core_start_httpproxy();
418         if (ret < 0)
419                 BT_ERR("_bt_core_start_httpproxy() failed");
420 #endif
421
422         return 0;
423 }
424
425 #ifdef TIZEN_FEATURE_BT_OTP
426 static void _bt_core_stop_otp()
427 {
428         GError *error = NULL;
429         GDBusMessage *msg = NULL;
430         GDBusConnection *conn = _bt_core_get_gdbus_connection();
431         msg = g_dbus_message_new_signal(BT_OTP_OBJECT_PATH, BT_OTP_INTERFACE_NAME, BLE_DISABLED);
432         if (!g_dbus_connection_send_message(conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, 0, NULL)) {
433                 if (error != NULL) {
434                         BT_ERR("Failed to send BLE_DISABLED signal to OTP : errCode[%x], \
435                                         message[%s]",
436                                         error->code, error->message);
437                         g_clear_error(&error);
438                 }
439         }
440 }
441 #endif
442
443 int _bt_disable_adapter_le(void)
444 {
445         BT_DBG("+");
446
447         bt_status_t status;
448         bt_le_status_t le_status;
449
450         le_status = _bt_core_get_le_status();
451         retv_if(le_status == BT_LE_DEACTIVATED, 0);
452         retv_if(le_status == BT_LE_DEACTIVATING, -1);
453
454 #ifdef TIZEN_FEATURE_BT_HPS
455         _bt_core_stop_httpproxy();
456 #endif
457
458 #ifdef TIZEN_FEATURE_BT_OTP
459         _bt_core_stop_otp();
460 #endif
461
462         status = _bt_core_get_status();
463         BT_DBG("status : %d", status);
464
465         if (status == BT_DEACTIVATED) {
466                 __bt_core_set_le_status(BT_LE_DEACTIVATING);
467                 int ret;
468                 if (TIZEN_FEATURE_BT_USB_DONGLE) {
469                         char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
470                         ret = __execute_command("/usr/bin/hciconfig", argv_down);
471                 } else {
472                         ret = __execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL);
473                 }
474                 if (ret < 0) {
475                         BT_ERR("running script failed");
476                         __bt_core_set_le_status(BT_LE_ACTIVATED);
477                         return -1;
478                 }
479         } else {
480                         g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
481         }
482
483         __bt_core_set_le_status(BT_LE_DEACTIVATED);
484
485         BT_DBG("-");
486         return 0;
487 }
488
489 int _bt_core_service_request_adapter(int service_function)
490 {
491         int ret = -1;
492
493         GArray *in_param1 = NULL;
494         GArray *in_param2 = NULL;
495         GArray *in_param3 = NULL;
496         GArray *in_param4 = NULL;
497         GArray *out_param = NULL;
498
499         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
500
501         ret = _bt_core_service_request(BT_CORE_SERVICE, service_function,
502                         in_param1, in_param2, in_param3, in_param4, &out_param);
503         if (ret < 0)
504                 BT_ERR("_bt_core_service_request_adapter() failed");
505
506         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
507
508         return ret;
509 }
510
511 static gboolean __bt_core_check_the_adapter_path(void)
512 {
513         GError *err = NULL;
514         GDBusProxy *manager_proxy = NULL;
515         GVariant *result = NULL;
516         char *adapter_path = NULL;
517         GDBusConnection *conn = NULL;
518
519         conn = _bt_core_get_gdbus_connection();
520         if (conn == NULL)
521                 return FALSE;
522
523         manager_proxy =  g_dbus_proxy_new_sync(conn,
524                         G_DBUS_PROXY_FLAGS_NONE, NULL,
525                         "org.bluez",
526                         "/",
527                         "org.freedesktop.DBus.ObjectManager",
528                         NULL, &err);
529
530         if (!manager_proxy) {
531                 if (err != NULL) {
532                         BT_ERR("Unable to create proxy: %s", err->message);
533                         g_clear_error(&err);
534                 } else {
535                         BT_ERR("Fail to create proxy");
536                 }
537                 goto fail;
538         }
539
540         result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
541                         G_DBUS_CALL_FLAGS_NONE, 1000, NULL, &err);
542         if (!result) {
543                 if (err != NULL) {
544                         BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
545                         g_clear_error(&err);
546                 } else{
547                         BT_ERR("Fail to get DefaultAdapter");
548                 }
549                 goto fail;
550         }
551
552         if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
553                 BT_ERR("Incorrect result\n");
554                 goto fail;
555         }
556
557         g_variant_get(result, "(&o)", &adapter_path);
558
559         if (adapter_path == NULL ||
560                 strlen(adapter_path) >= BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX) {
561                 BT_ERR("Adapter path is inproper\n");
562                 goto fail;
563         }
564
565         g_variant_unref(result);
566         g_object_unref(manager_proxy);
567
568         return TRUE;
569
570 fail:
571         if (result)
572                 g_variant_unref(result);
573
574         if (manager_proxy)
575                 g_object_unref(manager_proxy);
576
577         return FALSE;
578 }
579
580 void _bt_core_update_status(void)
581 {
582         int bt_status = VCONFKEY_BT_STATUS_OFF;
583         int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
584         gboolean ret = FALSE;
585
586         ret = __bt_core_check_the_adapter_path();
587         BT_INFO("check the real status of bt_adapter");
588
589         if (ret != TRUE) {
590                 __bt_core_set_status(BT_DEACTIVATED);
591                 __bt_core_set_le_status(BT_DEACTIVATED);
592         } else {
593                 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
594                         BT_ERR("no bluetooth device info, so BT was disabled at previous session");
595                 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
596                         BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
597
598                 BT_INFO("bt_status = %d, bt_le_status = %d", bt_status, bt_le_status);
599
600                 if (bt_status & VCONFKEY_BT_STATUS_ON)
601                         __bt_core_set_status(BT_ACTIVATED);
602                 if (bt_le_status & VCONFKEY_BT_LE_STATUS_ON)
603                         __bt_core_set_le_status(BT_ACTIVATED);
604         }
605 }
606
607 gboolean _bt_core_enable_adapter(void)
608 {
609         int ret;
610
611         _bt_set_flightmode_request(FALSE);
612         if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
613                 BT_ERR("Set vconf failed");
614
615         ret = _bt_enable_adapter();
616         if (ret < 0)
617                 return FALSE;
618         else
619                 return TRUE;
620 }
621
622 static gboolean __bt_core_terminate_cb(gpointer data)
623 {
624         _bt_core_terminate();
625
626         return FALSE;
627 }
628
629 gboolean _bt_core_disable_adapter(void)
630 {
631         int ret;
632         gboolean adapter_state;
633
634         _bt_set_flightmode_request(FALSE);
635         if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
636                 BT_ERR("Set vconf failed");
637
638         adapter_state = __bt_core_check_the_adapter_path();
639         if (adapter_state == FALSE)
640                 BT_INFO("Default adapter is NOT normal.");
641
642         ret = _bt_disable_adapter();
643         if (adapter_state == FALSE) {
644                 g_timeout_add(BT_CORE_IDLE_TERM_TIME,
645                               __bt_core_terminate_cb, NULL);
646                 return TRUE;
647         } else {
648                 if (ret < 0)
649                         return FALSE;
650                 else
651                         return TRUE;
652         }
653 }
654
655 gboolean _bt_core_recover_adapter(void)
656 {
657         int ret;
658 #if 0
659         int ret_le;
660 #endif
661         BT_INFO_C("Recover bt adapter");
662
663         _bt_set_flightmode_request(FALSE);
664         if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
665                 BT_ERR("Set vconf failed");
666
667         is_recovery_mode = TRUE;
668
669         _bt_core_update_status();
670
671         if (_bt_core_get_status() == BT_ACTIVATED) {
672                 _bt_core_set_bt_status(BT_RECOVERY_MODE, 1);
673 #ifdef TIZEN_FEATURE_BUSACT
674                 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
675 #endif
676         }
677         if (_bt_core_get_le_status() == BT_LE_ACTIVATED) {
678                 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 1);
679 #ifdef TIZEN_FEATURE_BUSACT
680                 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
681 #endif
682         }
683
684         ret = _bt_disable_adapter();
685         if (ret < 0)
686                 BT_ERR("_bt_disable_adapter() failed");
687
688 /* In platform, don't seperate BR/EDR and LE status */
689 #if 0
690         ret_le = _bt_disable_adapter_le();
691         if (ret_le < 0)
692                 BT_ERR("_bt_disable_adapter_le() failed");
693 #endif
694         return TRUE;
695 }
696
697 gboolean _bt_core_enable_adapter_le(void)
698 {
699         int ret;
700
701         ret = _bt_enable_adapter_le();
702         if (ret < 0)
703                 return FALSE;
704         else
705                 return TRUE;
706 }
707
708 gboolean _bt_core_disable_adapter_le(void)
709 {
710         BT_DBG("+");
711
712         int ret;
713
714         ret = _bt_disable_adapter_le();
715         if (ret < 0)
716                 return FALSE;
717         else
718                 return TRUE;
719 }
720
721 gboolean __bt_core_reset_adapter(void)
722 {
723         /* Forcely terminate */
724         if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0)
725                 BT_ERR("running script failed");
726
727         g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
728
729         return TRUE;
730 }
731
732 static gboolean __bt_core_enable_core_timeout_cb(gpointer data)
733 {
734         BT_DBG("+");
735
736         core_enable_timer_id = 0;
737
738         _bt_core_init_vconf_value();
739
740         return FALSE;
741 }
742
743 gboolean _bt_core_enable_core(void)
744 {
745         BT_DBG("+");
746
747         _bt_core_update_status();
748
749         if (core_enable_timer_id > 0)
750                 g_source_remove(core_enable_timer_id);
751
752         core_enable_timer_id = g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
753
754         BT_DBG("-");
755         return TRUE;
756 }
757
758 gboolean _bt_core_set_transfer_value(gboolean value)
759 {
760         const char *event_val = NULL;
761         gboolean ret = TRUE;
762
763         BT_DBG("value: %d", value);
764
765         event_val = value ? EVT_VAL_BT_TRANSFERING : EVT_VAL_BT_NON_TRANSFERING;
766
767         if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_TRANSFERING_STATE,
768                                                 event_val) != ES_R_OK) {
769                 BT_ERR("Fail to set BT state value");
770                 ret = FALSE;
771         }
772
773         if (ret == FALSE || value == FALSE) {
774                 BT_DBG("Terminate bt-core process");
775                 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
776         }
777
778         BT_DBG("-");
779         return ret;
780 }
781
782 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
783 {
784
785         char *cmd = NULL;
786         char *arg_list[3] = { NULL, NULL, NULL };
787
788         BT_DBG("Test item : %s", type);
789
790         if (g_strcmp0(type, "Enable_RF_Test") == 0) {
791                 cmd = "/usr/etc/bluetooth/bt-edutm-on.sh";
792                 arg_list[0] = "bt-edutm-on.sh";
793         } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
794                 cmd = "/usr/etc/bluetooth/bt-edutm-off.sh";
795                 arg_list[0] = "bt-edutm-off.sh";
796         } else if (g_strcmp0(type, "Slave_Mode") == 0) {
797                 cmd = "/usr/etc/bluetooth/bt-mode-slave.sh";
798                 arg_list[0] = "bt-mode-slave.sh";
799         } else if (g_strcmp0(type, "Master_Mode") == 0) {
800                 cmd = "/usr/etc/bluetooth/bt-mode-master.sh";
801                 arg_list[0] = "bt-mode-master.sh";
802         } else if (g_strcmp0(type, "SSP_Debug_Mode") == 0) {
803                 cmd = "/usr/etc/bluetooth/bt-set-ssp-debug-mode.sh";
804                 arg_list[0] = "bt-set-ssp-debug-mode.sh";
805                 arg_list[1] = (char *)arg;
806         } else if (g_strcmp0(type, "RF_Channel") == 0) {
807                 cmd = "/usr/etc/bluetooth/bt-enable-rf-channel.sh";
808                 arg_list[0] = "bt-enable-rf-channel.sh";
809                 arg_list[1] = (char *)arg;
810         } else {
811                 return FALSE;
812         }
813
814         BT_DBG("Run %s", cmd);
815         if (__execute_command(cmd, arg_list) < 0)
816                 BT_ERR("running script failed");
817
818         return TRUE;
819 }
820
821 static gboolean __bt_core_recovery_cb(gpointer data)
822 {
823         int ret = 0;
824 #ifdef TIZEN_FEATURE_BUSACT
825         gboolean is_request_failed = FALSE;
826         static gboolean is_first_failure = TRUE;
827 #endif
828
829         BT_DBG("+");
830
831 #ifdef TIZEN_FEATURE_BUSACT
832         if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
833                 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
834                 if (ret < 0)
835                         is_request_failed = TRUE;
836         }
837
838         if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
839                 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
840                 if (ret < 0)
841                         is_request_failed = TRUE;
842         }
843
844         if (is_request_failed == TRUE) {
845                 BT_ERR("Recovery is failed.");
846                 if (is_first_failure == TRUE) {
847                         g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
848                         is_first_failure = FALSE;
849                         return FALSE;
850                 } else {
851                         is_first_failure = TRUE;
852                         return FALSE;
853                 }
854         } else
855                 is_first_failure = TRUE;
856 #endif
857
858         if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
859                 _bt_core_set_bt_status(BT_RECOVERY_MODE, 0);
860                 ret = _bt_enable_adapter();
861                 if (ret < 0)
862                         BT_ERR("_bt_enable_adapter() failed");
863         }
864
865 #if 0
866         if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
867                 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 0);
868                 ret = _bt_enable_adapter_le();
869                 if (ret < 0)
870                         BT_ERR("_bt_enable_adapter_le() failed");
871         }
872 #endif
873         is_recovery_mode = FALSE;
874
875         BT_DBG("-");
876
877         return FALSE;
878 }
879
880 static gboolean __bt_core_enable_timeout_cb(gpointer data)
881 {
882         bt_status_t adapter_status;
883         bt_le_status_t adapter_status_le;
884
885         BT_DBG("");
886
887         if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
888                 BT_ERR("Set vconf failed");
889
890         adapter_status = _bt_core_get_status();
891         adapter_status_le = _bt_core_get_le_status();
892
893         if (adapter_status == BT_DEACTIVATED &&
894                         _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
895                 _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
896                 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
897                 _bt_enable_adapter();
898         }
899
900         if (adapter_status_le == BT_LE_DEACTIVATED &&
901                         _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
902                 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
903                 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
904                 _bt_enable_adapter_le();
905         }
906
907         return FALSE;
908 }
909
910 static gboolean __bt_core_disable_timeout_cb(gpointer data)
911 {
912         bt_status_t adapter_status;
913         bt_le_status_t adapter_status_le;
914
915         BT_DBG("");
916
917         if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
918                 BT_ERR("Set vconf failed");
919
920         adapter_status = _bt_core_get_status();
921         adapter_status_le = _bt_core_get_le_status();
922
923         if (adapter_status == BT_ACTIVATED) {
924                 int bt_status_before_mode = 0;
925
926                 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
927                         _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
928
929 #ifdef TIZEN_FEATURE_BUSACT
930                 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
931 #endif
932                 _bt_disable_adapter();
933         }
934
935         if (adapter_status_le == BT_LE_ACTIVATED) {
936                 int bt_le_status_before_mode = 0;
937
938                 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
939                         _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
940
941 #ifdef TIZEN_FEATURE_BUSACT
942                 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
943 #endif
944                 _bt_disable_adapter_le();
945         }
946
947         return FALSE;
948 }
949
950 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value)
951 {
952         int ret;
953         bundle *b = NULL;
954
955         b = bundle_create();
956
957         bundle_add_str(b, key, value);
958
959         ret = eventsystem_send_system_event(event, b);
960
961         BT_DBG("eventsystem_send_system_event result: %d", ret);
962
963         bundle_free(b);
964
965         return ret;
966 }
967
968 void _bt_core_adapter_added_cb(void)
969 {
970         bt_status_t status;
971         bt_le_status_t le_status;
972         gboolean flight_mode_status;
973
974         BT_DBG("");
975
976         status = _bt_core_get_status();
977         BT_DBG("status : %d", status);
978         le_status = _bt_core_get_le_status();
979         BT_DBG("le_status : %d", le_status);
980
981         if (status == BT_ACTIVATING)
982                 __bt_core_set_status(BT_ACTIVATED);
983         if (le_status == BT_LE_ACTIVATING)
984                 __bt_core_set_le_status(BT_LE_ACTIVATED);
985
986         flight_mode_status = _bt_core_is_flight_mode_enabled();
987
988         if (flight_mode_status == TRUE && _bt_is_flightmode_request() == TRUE) {
989                 _bt_set_flightmode_request(FALSE);
990                 g_timeout_add(2000, (GSourceFunc)__bt_core_disable_timeout_cb, NULL);
991                 return;
992         }
993         _bt_set_flightmode_request(FALSE);
994
995         if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
996                                                 EVT_VAL_BT_ON) != ES_R_OK)
997                 BT_ERR("Fail to set BT state value");
998
999         if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
1000                                                 EVT_VAL_BT_LE_ON) != ES_R_OK)
1001                 BT_ERR("Fail to set BT LE state value");
1002
1003         _bt_core_terminate();
1004 }
1005
1006 void _bt_core_adapter_removed_cb(void)
1007 {
1008         int flight_mode_value = 0;
1009         int power_saving_mode = 0;
1010         gboolean flight_mode_status;
1011         static int timer_id = -1;
1012
1013         BT_DBG("is_recovery_mode: %d", is_recovery_mode);
1014
1015         __bt_core_set_status(BT_DEACTIVATED);
1016         __bt_core_set_le_status(BT_LE_DEACTIVATED);
1017         if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1018                 BT_ERR("Set vconf failed");
1019         if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
1020                 BT_ERR("Set vconf failed");
1021
1022         if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1023                                                 EVT_VAL_BT_OFF) != ES_R_OK)
1024                 BT_ERR("Fail to set value");
1025
1026         if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
1027                                                 EVT_VAL_BT_LE_OFF) != ES_R_OK)
1028                 BT_ERR("Fail to set value");
1029
1030         if (is_recovery_mode == TRUE) {
1031                 if (timer_id < 0)
1032                         timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
1033                 return;
1034         }
1035
1036         if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value) != 0)
1037                 BT_ERR("Fail to get the flight_mode_deactivated value");
1038
1039         if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &power_saving_mode) != 0)
1040                 BT_ERR("Fail to get the ps_mode_deactivated value");
1041
1042         flight_mode_status = _bt_core_is_flight_mode_enabled();
1043
1044         if (flight_mode_status == FALSE && _bt_is_flightmode_request() == TRUE) {
1045                 _bt_set_flightmode_request(FALSE);
1046                 if (timer_id < 0)
1047                         timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_enable_timeout_cb, NULL);
1048                 return;
1049         }
1050         _bt_set_flightmode_request(FALSE);
1051
1052         if (flight_mode_value == 1 || power_saving_mode == 1) {
1053                 BT_DBG("Bt Core not terminated");
1054                 return;
1055         }
1056
1057         _bt_core_terminate();
1058 }