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