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