Merge the code from tizen_2.4
[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 = VCONFKEY_BT_LE_STATUS_OFF;
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         int ret;
90
91         status = _bt_core_get_status();
92         le_status = _bt_core_get_le_status();
93         BT_DBG("State: %d, LE State: %d", status, le_status);
94
95         if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0) {
96                 BT_DBG("no bluetooth device info, so BT was disabled at previous session");
97         }
98
99 #ifdef ENABLE_TIZEN_2_4
100         if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0) {
101                 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
102         }
103 #endif
104
105         if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_deactivation) != 0)
106                 BT_ERR("Fail to get the flight_mode_deactivation value");
107
108         if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_deactivation) != 0)
109                 BT_ERR("Fail to get the ps_mode_deactivation value");
110
111         if (vconf_get_int(BT_OFF_DUE_TO_TIMEOUT, &bt_off_due_to_timeout) != 0)
112                 BT_ERR("Fail to get BT_OFF_DUE_TO_TIMEOUT");
113
114         if ((bt_status != VCONFKEY_BT_STATUS_OFF || bt_off_due_to_timeout) &&
115                 (status == BT_DEACTIVATED)) {
116                 BT_DBG("Previous session was enabled.");
117
118                 /* Enable the BT */
119                 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
120 #ifndef USB_BLUETOOTH
121                 _bt_enable_adapter();
122 #endif
123         } else if (bt_status == VCONFKEY_BT_STATUS_OFF &&
124                         (flight_mode_deactivation == 1 || ps_mode_deactivation > 0)) {
125                 _bt_core_handle_flight_mode_noti();
126                 _bt_core_handle_power_saving_mode_noti();
127
128                 _bt_core_set_bt_status(BT_FLIGHT_MODE, flight_mode_deactivation);
129                 _bt_core_set_bt_status(BT_POWER_SAVING_MODE, ps_mode_deactivation);
130         }
131
132         if ((bt_le_status == VCONFKEY_BT_LE_STATUS_ON) && (le_status == BT_LE_DEACTIVATED)) {
133                 BT_DBG("Previous session was le enabled. Turn BT LE on automatically.");
134
135                 /* Enable the BT LE */
136                 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
137 #ifndef USB_BLUETOOTH
138                 _bt_enable_adapter_le();
139 #endif
140         } else {
141                 status = _bt_core_get_status();
142                 le_status = _bt_core_get_le_status();
143                 BT_DBG("State: %d, LE State: %d", status, le_status);
144
145                 if ((status != BT_ACTIVATING && status != BT_ACTIVATED) &&
146                                 (le_status != BT_LE_ACTIVATING && le_status != BT_LE_ACTIVATED))
147                         _bt_core_terminate();
148         }
149
150         return FALSE;
151 }
152
153 int main(void)
154 {
155         gboolean ret;
156         struct sigaction sa;
157
158         g_type_init();
159         BT_INFO_C("Starting bt-core daemeon");
160
161         _bt_core_update_status();
162
163         ret = _bt_core_register_dbus();
164         if (!ret) {
165                 BT_ERR("_bt_core_register_dbus failed");
166                 goto fail;
167         }
168
169
170         memset(&sa, 0, sizeof(sa));
171         sa.sa_handler = __bt_core_sigterm_handler;
172         sigaction(SIGINT, &sa, NULL);
173         sigaction(SIGTERM, &sa, NULL);
174
175         g_timeout_add(500, (GSourceFunc)__bt_check_bt_core, NULL);
176
177         main_loop = g_main_loop_new(NULL, FALSE);
178         if (!main_loop) {
179                 BT_ERR("creating main loop failed");
180                 goto fail;
181         }
182
183         g_main_loop_run(main_loop);
184
185 fail:
186         _bt_core_unregister_dbus();
187
188         if (main_loop)
189                 g_main_loop_unref(main_loop);
190
191         BT_INFO_C("Terminating bt-core daemon");
192
193         return 0;
194 }