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