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