Code Sync [Tizen3.0]: Merged the tizen_2.4 Spin code to tizen.org
[platform/core/connectivity/bluetooth-frwk.git] / bt-core / bt-core-main.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Hocheol Seo <hocheol.seo@samsung.com>
7  *               Girishashok Joshi <girish.joshi@samsung.com>
8  *               Chanyeol Park <chanyeol.park@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *              http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23
24 #include <vconf.h>
25 #include <vconf-keys.h>
26
27 #include "bt-core-adapter.h"
28 #include "bt-core-dbus-handler.h"
29 #include "bt-core-main.h"
30 #include "bt-core-noti-handler.h"
31 #include "bt-core-common.h"
32
33 static GMainLoop *main_loop = NULL;
34
35 gboolean _bt_check_terminating_condition(void)
36 {
37         int bt_off_flight_mode = 0;     /* if BT was off due to FlightMode*/
38         int bt_off_ps_mode = 0;
39
40         if (_bt_core_is_recovery_mode() == TRUE) {
41                 BT_DBG("Bt core not terminated");
42                 return FALSE;
43         }
44
45         if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &bt_off_flight_mode) != 0)
46                 BT_ERR("Fail to get the BT off due to FlightMode value");
47         if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &bt_off_ps_mode) != 0)
48                 BT_ERR("Fail to get the ps_mode_deactivated value");
49
50         if (bt_off_flight_mode == 1 || bt_off_ps_mode == 1) {
51                 BT_DBG("Bt core not terminated");
52                 return FALSE;
53         }
54
55         return TRUE;
56 }
57
58 void _bt_core_terminate(void)
59 {
60         if (_bt_check_terminating_condition() == FALSE)
61                 return;
62
63         _bt_core_gdbus_deinit_proxys();
64         _bt_core_unregister_vconf_handler();
65         if (main_loop) {
66                 g_main_loop_quit(main_loop);
67         } else {
68                 BT_DBG("Terminating bt-core daemon");
69                 exit(0);
70         }
71 }
72
73 static void __bt_core_sigterm_handler(int signo)
74 {
75         BT_DBG("Got the signal: %d", signo);
76
77         _bt_core_terminate();
78 }
79
80 static gboolean __bt_check_bt_core(void *data)
81 {
82         int bt_status = VCONFKEY_BT_STATUS_OFF;
83         int bt_le_status = 0;
84         bt_status_t status = BT_DEACTIVATED;
85         bt_le_status_t le_status = BT_LE_DEACTIVATED;
86         int flight_mode_deactivation = 0;
87         int bt_off_due_to_timeout = 0;
88         int ps_mode_deactivation = 0;
89
90         status = _bt_core_get_status();
91         le_status = _bt_core_get_le_status();
92         BT_DBG("State: %d, LE State: %d", status, le_status);
93
94         if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0) {
95                 BT_DBG("no bluetooth device info, so BT was disabled at previous session");
96         }
97
98 #ifdef ENABLE_TIZEN_2_4
99         if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0) {
100                 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
101         }
102 #endif
103
104         if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_deactivation) != 0)
105                 BT_ERR("Fail to get the flight_mode_deactivation value");
106
107         if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_deactivation) != 0)
108                 BT_ERR("Fail to get the ps_mode_deactivation value");
109
110         if (vconf_get_int(BT_OFF_DUE_TO_TIMEOUT, &bt_off_due_to_timeout) != 0)
111                 BT_ERR("Fail to get BT_OFF_DUE_TO_TIMEOUT");
112
113         if ((bt_status != VCONFKEY_BT_STATUS_OFF || bt_off_due_to_timeout)
114                 && (status == BT_DEACTIVATED)) {
115                 BT_DBG("Previous session was enabled.");
116
117                 /* Enable the BT */
118                 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
119                 _bt_enable_adapter();
120         } else if (bt_status == VCONFKEY_BT_STATUS_OFF &&
121                         (flight_mode_deactivation == 1 || ps_mode_deactivation > 0)) {
122                 _bt_core_handle_flight_mode_noti();
123                 _bt_core_handle_power_saving_mode_noti();
124
125                 _bt_core_set_bt_status(BT_FLIGHT_MODE, flight_mode_deactivation);
126                 _bt_core_set_bt_status(BT_POWER_SAVING_MODE, ps_mode_deactivation);
127         }
128
129         if ((bt_le_status == 1) && (le_status == BT_LE_DEACTIVATED)) {
130                 BT_DBG("Previous session was le enabled. Turn BT LE on automatically.");
131
132                 /* Enable the BT LE */
133                 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
134                 _bt_enable_adapter_le();
135         } else {
136                 status = _bt_core_get_status();
137                 le_status = _bt_core_get_le_status();
138                 BT_DBG("State: %d, LE State: %d", status, le_status);
139
140                 if ((status != BT_ACTIVATING && status != BT_ACTIVATED) &&
141                                 (le_status != BT_LE_ACTIVATING && le_status != BT_LE_ACTIVATED))
142                         _bt_core_terminate();
143         }
144
145         return FALSE;
146 }
147
148 int main(void)
149 {
150         DBusGConnection *conn = NULL;
151         GError *error = NULL;
152         BtCore *bt_core = NULL;
153
154         DBusGProxy *dbus_proxy = NULL;
155         struct sigaction sa;
156
157         g_type_init();
158         BT_INFO_C("Starting bt-core daemeon");
159
160         conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
161         if (error != NULL) {
162                 BT_ERR("ERROR: Can't get on system bus [%s]", error->message);
163                 g_error_free(error);
164                 goto fail;
165         }
166
167         bt_core = g_object_new(BT_CORE_TYPE, NULL);
168
169         dbus_proxy = _bt_core_register_event_filter(conn, bt_core);
170         if (!dbus_proxy) {
171                 BT_ERR("__bt_core_register_event_filter failed");
172                 g_object_unref(bt_core);
173                 bt_core = NULL;
174                 goto fail;
175         }
176
177
178         memset(&sa, 0, sizeof(sa));
179         sa.sa_handler = __bt_core_sigterm_handler;
180         sigaction(SIGINT, &sa, NULL);
181         sigaction(SIGTERM, &sa, NULL);
182
183         g_timeout_add(500, (GSourceFunc)__bt_check_bt_core, NULL);
184
185         main_loop = g_main_loop_new(NULL, FALSE);
186
187         g_main_loop_run(main_loop);
188
189 fail:
190
191         _bt_unregister_event_filter(conn, bt_core, dbus_proxy);
192
193         if (main_loop)
194                 g_main_loop_unref(main_loop);
195
196         dbus_g_connection_unref(conn);
197
198         BT_INFO_C("Terminating bt-core daemon");
199
200         return 0;
201 }