Initialized previously uninitialized local vairable
[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 #ifndef USB_BLUETOOTH
120                 _bt_enable_adapter();
121 #endif
122         } else if (bt_status == VCONFKEY_BT_STATUS_OFF &&
123                         (flight_mode_deactivation == 1 || ps_mode_deactivation > 0)) {
124                 _bt_core_handle_flight_mode_noti();
125                 _bt_core_handle_power_saving_mode_noti();
126
127                 _bt_core_set_bt_status(BT_FLIGHT_MODE, flight_mode_deactivation);
128                 _bt_core_set_bt_status(BT_POWER_SAVING_MODE, ps_mode_deactivation);
129         }
130
131         if ((bt_le_status == 1) && (le_status == BT_LE_DEACTIVATED)) {
132                 BT_DBG("Previous session was le enabled. Turn BT LE on automatically.");
133
134                 /* Enable the BT LE */
135                 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
136 #ifndef USB_BLUETOOTH
137                 _bt_enable_adapter_le();
138 #endif
139         } else {
140                 status = _bt_core_get_status();
141                 le_status = _bt_core_get_le_status();
142                 BT_DBG("State: %d, LE State: %d", status, le_status);
143
144                 if ((status != BT_ACTIVATING && status != BT_ACTIVATED) &&
145                                 (le_status != BT_LE_ACTIVATING && le_status != BT_LE_ACTIVATED))
146                         _bt_core_terminate();
147         }
148
149         return FALSE;
150 }
151
152 int main(void)
153 {
154         gboolean ret;
155         struct sigaction sa;
156
157         g_type_init();
158         BT_INFO_C("Starting bt-core daemeon");
159
160         ret = _bt_core_register_dbus();
161         if (!ret) {
162                 BT_ERR("_bt_core_register_dbus failed");
163                 goto fail;
164         }
165
166
167         memset(&sa, 0, sizeof(sa));
168         sa.sa_handler = __bt_core_sigterm_handler;
169         sigaction(SIGINT, &sa, NULL);
170         sigaction(SIGTERM, &sa, NULL);
171
172         g_timeout_add(500, (GSourceFunc)__bt_check_bt_core, NULL);
173
174         main_loop = g_main_loop_new(NULL, FALSE);
175         if (!main_loop) {
176                 BT_ERR("creating main loop failed");
177                 goto fail;
178         }
179
180         g_main_loop_run(main_loop);
181
182 fail:
183         _bt_core_unregister_dbus();
184
185         if (main_loop)
186                 g_main_loop_unref(main_loop);
187
188         BT_INFO_C("Terminating bt-core daemon");
189
190         return 0;
191 }