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