Code Merge [Tizen3.0]: Applied tizen.org patches
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-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 <dbus/dbus-glib.h>
25 #include <glib.h>
26 #include <dlog.h>
27 #include <string.h>
28 #include <privilege-control.h>
29 #include <vconf.h>
30
31 #include "bt-internal-types.h"
32 #include "bt-service-common.h"
33 #include "bt-service-event.h"
34 #include "bt-service-main.h"
35 #include "bt-service-util.h"
36 #include "bt-request-handler.h"
37 #include "bt-service-adapter.h"
38 #include "bt-service-adapter-le.h"
39
40 static GMainLoop *main_loop;
41 static gboolean terminated = FALSE;
42
43 static void __bt_release_service(void)
44 {
45         _bt_service_unregister_vconf_handler();
46
47         _bt_service_adapter_le_deinit();
48         _bt_deinit_service_event_sender();
49         _bt_deinit_hf_local_term_event_sender();
50         _bt_deinit_service_event_receiver();
51
52         _bt_service_unregister();
53
54         _bt_deinit_proxys();
55
56         _bt_clear_request_list();
57
58         BT_DBG("Terminating the bt-service daemon");
59 }
60
61 static void __bt_sigterm_handler(int signo)
62 {
63         BT_DBG("Get the signal: %d", signo);
64
65         _bt_terminate_service(NULL);
66 }
67
68 gboolean _bt_terminate_service(gpointer user_data)
69 {
70         int bt_status = VCONFKEY_BT_STATUS_OFF;
71
72         if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0) {
73                 BT_ERR("no bluetooth device info, so BT was disabled at previous session");
74         } else {
75                 if (bt_status != VCONFKEY_BT_STATUS_OFF) {
76                         if(vconf_set_int(VCONFKEY_BT_STATUS,
77                                         VCONFKEY_BT_STATUS_OFF) != 0)
78                                 BT_ERR("Set vconf failed\n");
79                 }
80         }
81
82 #ifdef ENABLE_TIZEN_2_4
83         if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_status) < 0) {
84                 BT_ERR("no bluetooth device info, so BT was disabled at previous session");
85         } else {
86                 if (bt_status != VCONFKEY_BT_LE_STATUS_OFF) {
87                         if(vconf_set_int(VCONFKEY_BT_LE_STATUS,
88                                         VCONFKEY_BT_LE_STATUS_OFF) != 0)
89                                 BT_ERR("Set vconf failed\n");
90                 }
91         }
92 #endif
93
94         if (main_loop != NULL) {
95                 g_main_loop_quit(main_loop);
96         } else {
97                 BT_ERR("main_loop == NULL");
98                 __bt_release_service();
99                 terminated = TRUE;
100                 exit(0);
101         }
102
103         return FALSE;
104 }
105
106 gboolean _bt_reliable_terminate_service(gpointer user_data)
107 {
108         _bt_deinit_service_event_receiver();
109
110         _bt_deinit_proxys();
111
112         _bt_clear_request_list();
113
114         _bt_set_disabled(BLUETOOTH_ERROR_NONE);
115
116         _bt_service_adapter_le_deinit();
117         _bt_deinit_service_event_sender();
118         _bt_deinit_hf_local_term_event_sender();
119
120         _bt_service_unregister();
121
122         terminated = TRUE;
123
124         BT_INFO_C("Terminating the bt-service daemon");
125
126         if (main_loop != NULL) {
127                 g_main_loop_quit(main_loop);
128         } else {
129                 exit(0);
130         }
131
132         return FALSE;
133 }
134
135 static gboolean __bt_check_bt_service(void *data)
136 {
137         int bt_status = VCONFKEY_BT_STATUS_OFF;
138         int bt_le_status = 0;
139         bt_status_t status = BT_DEACTIVATED;
140         bt_le_status_t le_status = BT_LE_DEACTIVATED;
141         int flight_mode_deactivation = 0;
142         int bt_off_due_to_timeout = 0;
143 #if 0
144         int ps_mode_deactivation = 0;
145 #endif
146
147         status = _bt_adapter_get_status();
148         le_status = _bt_adapter_get_le_status();
149         BT_DBG("State: %d, LE State: %d", status, le_status);
150
151         if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0) {
152                 BT_DBG("no bluetooth device info, so BT was disabled at previous session");
153         }
154
155 #ifdef ENABLE_TIZEN_2_4
156         if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0) {
157                 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
158         }
159 #endif
160
161         if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_deactivation) != 0)
162                 BT_ERR("Fail to get the flight_mode_deactivation value");
163
164 #if 0
165         if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_deactivation) != 0)
166                 BT_ERR("Fail to get the ps_mode_deactivation value");
167 #endif
168
169         if (vconf_get_int(BT_OFF_DUE_TO_TIMEOUT, &bt_off_due_to_timeout) != 0)
170                 BT_ERR("Fail to get BT_OFF_DUE_TO_TIMEOUT");
171
172         if ((bt_status != VCONFKEY_BT_STATUS_OFF || bt_off_due_to_timeout)
173                 && (status == BT_DEACTIVATED)) {
174                 BT_DBG("Previous session was enabled.");
175
176                 /* Enable the BT */
177                 _bt_enable_adapter();
178         } else if (bt_status == VCONFKEY_BT_STATUS_OFF && flight_mode_deactivation == 1) {
179                 _bt_enable_core();
180         }
181
182         if ((bt_le_status == 1) && (le_status == BT_LE_DEACTIVATED)) {
183                 BT_DBG("Previous session was le enabled. Turn BT LE on automatically.");
184
185                 /* Enable the BT LE */
186                 _bt_enable_adapter_le();
187         } else {
188                 status = _bt_adapter_get_status();
189                 le_status = _bt_adapter_get_le_status();
190                 BT_DBG("State: %d, LE State: %d", status, le_status);
191
192                 if ((status != BT_ACTIVATING && status != BT_ACTIVATED) &&
193                                 (le_status != BT_LE_ACTIVATING && le_status != BT_LE_ACTIVATED)){
194                         _bt_terminate_service(NULL);
195                 }
196         }
197
198         return FALSE;
199 }
200
201 int main(void)
202 {
203         struct sigaction sa;
204         BT_INFO_C("Starting the bt-service daemon");
205
206         memset(&sa, 0, sizeof(sa));
207         sa.sa_handler = __bt_sigterm_handler;
208         sigaction(SIGINT, &sa, NULL);
209         sigaction(SIGTERM, &sa, NULL);
210
211         g_type_init();
212
213         if (perm_app_set_privilege("bluetooth-frwk-service", NULL, NULL) !=
214                                                                 PC_OPERATION_SUCCESS)
215                 BT_ERR("Failed to set app privilege.\n");
216
217         /* Event reciever Init */
218         if (_bt_init_service_event_receiver() != BLUETOOTH_ERROR_NONE) {
219                 BT_ERR("Fail to init event reciever");
220                 return 0;
221         }
222
223         /* Event sender Init */
224         if (_bt_init_service_event_sender() != BLUETOOTH_ERROR_NONE) {
225                 BT_ERR("Fail to init event sender");
226                 return 0;
227         }
228
229         if (_bt_init_hf_local_term_event_sender() != BLUETOOTH_ERROR_NONE) {
230                 BT_ERR("Fail to init core event sender");
231                 return 0;
232         }
233
234         if (_bt_service_register() != BLUETOOTH_ERROR_NONE) {
235                 BT_ERR("Fail to register service");
236                 return 0;
237         }
238
239         if (_bt_service_adapter_le_init() != BLUETOOTH_ERROR_NONE) {
240                 BT_ERR("Fail to init le");
241                 return 0;
242         }
243
244         _bt_init_request_id();
245
246         _bt_init_request_list();
247
248         g_timeout_add(500, (GSourceFunc)__bt_check_bt_service, NULL);
249
250         if (terminated == TRUE) {
251                 __bt_release_service();
252                 return 0;
253         }
254
255         main_loop = g_main_loop_new(NULL, FALSE);
256
257         g_main_loop_run(main_loop);
258         BT_DBG("g_main_loop_quit called!");
259
260         if (main_loop != NULL) {
261                 g_main_loop_unref(main_loop);
262         }
263
264         if (terminated == FALSE)
265                 __bt_release_service();
266
267         return 0;
268 }
269