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