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