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