Merge branch 'tizen' into tizen_5.5
[platform/core/connectivity/bluetooth-frwk.git] / bt-core / bt-core-main.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
21 #include "bt-core-adapter.h"
22 #include "bt-core-dbus-handler.h"
23 #include "bt-core-main.h"
24 #include "bt-core-noti-handler.h"
25 #include "bt-core-common.h"
26
27 static GMainLoop *main_loop = NULL;
28
29 gboolean _bt_check_terminating_condition(void)
30 {
31         int bt_off_flight_mode = 0;     /* if BT was off due to FlightMode*/
32         int bt_off_ps_mode = 0;
33
34         if (_bt_core_is_recovery_mode() == TRUE) {
35                 BT_DBG("Bt core not terminated");
36                 return FALSE;
37         }
38
39         if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &bt_off_flight_mode) != 0)
40                 BT_ERR("Fail to get the BT off due to FlightMode value");
41         if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &bt_off_ps_mode) != 0)
42                 BT_ERR("Fail to get the ps_mode_deactivated value");
43
44         if (bt_off_flight_mode == 1 || bt_off_ps_mode == 1) {
45                 BT_DBG("Bt core not terminated");
46                 return FALSE;
47         }
48
49         return TRUE;
50 }
51
52 void _bt_core_terminate(void)
53 {
54         if (_bt_check_terminating_condition() == FALSE)
55                 return;
56
57         _bt_core_gdbus_deinit_proxys();
58         _bt_core_unregister_vconf_handler();
59         if (main_loop) {
60                 g_main_loop_quit(main_loop);
61         } else {
62                 BT_DBG("Terminating bt-core daemon");
63                 exit(0);
64         }
65 }
66
67 static void __bt_core_sigterm_handler(int signo)
68 {
69         BT_DBG("Got the signal: %d", signo);
70
71         _bt_core_terminate();
72 }
73
74 static gboolean __bt_check_bt_core(void *data)
75 {
76         int bt_status = VCONFKEY_BT_STATUS_OFF;
77         int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
78         bt_status_t status = BT_DEACTIVATED;
79         bt_le_status_t le_status = BT_LE_DEACTIVATED;
80         int flight_mode_deactivation = 0;
81         int bt_off_due_to_timeout = 0;
82         int ps_mode_deactivation = 0;
83
84         status = _bt_core_get_status();
85         le_status = _bt_core_get_le_status();
86         BT_DBG("State: %d, LE State: %d", status, le_status);
87
88         if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
89                 BT_DBG("no bluetooth device info, so BT was disabled at previous session");
90
91 #ifdef ENABLE_TIZEN_2_4
92         if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
93                 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
94 #endif
95
96         if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_deactivation) != 0)
97                 BT_ERR("Fail to get the flight_mode_deactivation value");
98
99         if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_deactivation) != 0)
100                 BT_ERR("Fail to get the ps_mode_deactivation value");
101
102         if (vconf_get_int(BT_OFF_DUE_TO_TIMEOUT, &bt_off_due_to_timeout) != 0)
103                 BT_ERR("Fail to get BT_OFF_DUE_TO_TIMEOUT");
104
105         if ((bt_status != VCONFKEY_BT_STATUS_OFF || bt_off_due_to_timeout) &&
106                 (status == BT_DEACTIVATED)) {
107                 BT_DBG("Previous session was enabled.");
108 #if 0
109                 /* Enable the BT */
110                 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
111                 if (!TIZEN_FEATURE_BT_USB_DONGLE)
112                         _bt_enable_adapter();
113 #endif
114         } else if (bt_status == VCONFKEY_BT_STATUS_OFF &&
115                         (flight_mode_deactivation == 1 || ps_mode_deactivation > 0)) {
116                 _bt_core_handle_flight_mode_noti();
117                 _bt_core_handle_power_saving_mode_noti();
118
119                 _bt_core_set_bt_status(BT_FLIGHT_MODE, flight_mode_deactivation);
120                 _bt_core_set_bt_status(BT_POWER_SAVING_MODE, ps_mode_deactivation);
121         }
122
123         if ((bt_le_status == VCONFKEY_BT_LE_STATUS_ON) && (le_status == BT_LE_DEACTIVATED)) {
124                 BT_DBG("Previous session was le enabled. Turn BT LE on automatically.");
125 #if 0
126                 /* Enable the BT LE */
127                 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
128                 if (!TIZEN_FEATURE_BT_USB_DONGLE)
129                         _bt_enable_adapter_le();
130 #endif
131         } else {
132                 status = _bt_core_get_status();
133                 le_status = _bt_core_get_le_status();
134                 BT_DBG("State: %d, LE State: %d", status, le_status);
135
136                 if ((status != BT_ACTIVATING && status != BT_ACTIVATED) &&
137                                 (le_status != BT_LE_ACTIVATING && le_status != BT_LE_ACTIVATED))
138                         _bt_core_terminate();
139         }
140
141         return FALSE;
142 }
143
144 int main(void)
145 {
146         gboolean ret;
147         struct sigaction sa;
148
149         BT_INFO_C("Starting bt-core daemeon");
150
151         _bt_core_update_status();
152
153         ret = _bt_core_register_dbus();
154         if (!ret) {
155                 BT_ERR("_bt_core_register_dbus failed");
156                 goto fail;
157         }
158
159
160         memset(&sa, 0, sizeof(sa));
161         sa.sa_handler = __bt_core_sigterm_handler;
162         sigaction(SIGINT, &sa, NULL);
163         sigaction(SIGTERM, &sa, NULL);
164
165         g_timeout_add(500, (GSourceFunc)__bt_check_bt_core, NULL);
166
167         main_loop = g_main_loop_new(NULL, FALSE);
168         if (!main_loop) {
169                 BT_ERR("creating main loop failed");
170                 goto fail;
171         }
172
173         g_main_loop_run(main_loop);
174
175 fail:
176         _bt_core_unregister_dbus();
177
178         if (main_loop)
179                 g_main_loop_unref(main_loop);
180
181         BT_INFO_C("Terminating bt-core daemon");
182
183         return 0;
184 }