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