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