Get local IRK value
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / services / bt-service-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 <glib.h>
19 #include <dlog.h>
20 #include <string.h>
21 #include <vconf.h>
22 #ifdef TIZEN_FEATURE_BT_FACTORY_MODE
23 #include <bincfg.h>
24 #endif
25
26 #include <unistd.h>
27 #include <sys/socket.h>
28 #include <bundle.h>
29 #include <eventsystem.h>
30 #include <ell/ell.h>
31
32 #include "bt-internal-types.h"
33 #include "bt-service-common.h"
34 #include "bt-service-audio-common.h"
35 #include "bt-request-handler.h"
36 #include "bt-service-obex-event.h"
37 #ifdef TIZEN_FEATURE_BT_PAN_NAP
38 #include "bt-service-pan-nap-event.h"
39 #endif
40 #include "bt-service-event.h"
41 #include "bt-service-util.h"
42
43 #include "bt-service-core-adapter.h"
44 #include "bt-service-core-adapter-le.h"
45
46
47
48 /* OAL headers */
49 #include <oal-event.h>
50 #include <oal-manager.h>
51
52 /* ELL Start */
53 struct ell_event_source {
54         GSource source;
55         GPollFD pollfd;
56 };
57
58 static gboolean event_prepare(GSource *source, gint *timeout)
59 {
60         int r = l_main_prepare();
61         *timeout = r;
62
63         return FALSE;
64 }
65
66 static gboolean event_check(GSource *source)
67 {
68         l_main_iterate(0);
69         return FALSE;
70 }
71
72 static GSourceFuncs event_funcs = {
73         .prepare = event_prepare,
74         .check = event_check,
75 };
76 /* ELL End */
77
78 static GMainLoop *main_loop;
79 static gboolean terminated = FALSE;
80 static gboolean is_initialized = FALSE;
81
82 static void __on_log_glib(const gchar *log_domain, GLogLevelFlags log_level,
83                 const gchar *msg, gpointer user_data)
84 {
85         BT_ERR_C("%s", msg);
86 }
87
88 static void __bt_release_service(void)
89 {
90         _bt_audio_deinit_absolute_volume_control();
91
92         _bt_service_le_deinit();
93         _bt_deinit_hf_local_term_event_sender();
94         _bt_deinit_service_event_sender();
95         _bt_service_unregister();
96         _bt_service_cynara_deinit();
97
98         if (!TIZEN_PROFILE_TV)
99                 _bt_deinit_obex_event_receiver();
100
101 #ifdef TIZEN_FEATURE_BT_PAN_NAP
102          _bt_deinit_pan_nap_event_receiver();
103 #endif
104
105         _bt_cleanup_profiles();
106         oal_bt_deinit();
107
108         BT_DBG("Terminating the bt-service daemon");
109 }
110
111 static void __bt_sigterm_handler(int signo, siginfo_t *info, void *data)
112 {
113         BT_INFO("signal [%d] is sent by [%d]", signo, info->si_pid);
114
115         if (!TIZEN_PROFILE_TV) {
116                 int ret;
117                 ret = _bt_recover_adapter();
118                 if (ret != BLUETOOTH_ERROR_NONE)
119                         BT_ERR("_bt_recover_adapter is failed : %d", ret);
120         }
121         return;
122 }
123
124 gboolean _bt_terminate_service(gpointer user_data)
125 {
126         __bt_release_service();
127
128         terminated = TRUE;
129
130         BT_INFO_C("Terminating the bt-service daemon");
131
132         if (main_loop != NULL) {
133                 g_main_loop_quit(main_loop);
134         } else {
135                 BT_ERR("main_loop == NULL");
136                 exit(0);
137         }
138
139         return FALSE;
140 }
141
142 static gboolean __bt_main_loop_quit_idle_cb(gpointer user_data)
143 {
144         _bt_deinit_proxys();
145         _bt_deinit_osp_server();
146
147         _bt_clear_request_list();
148
149         _bt_set_disabled(BLUETOOTH_ERROR_NONE);
150
151         _bt_deinit_service_event_sender();
152
153         terminated = TRUE;
154
155         BT_INFO_C("Terminating the bt-service daemon");
156
157         if (main_loop != NULL) {
158                 g_main_loop_quit(main_loop);
159         } else {
160                 BT_ERR("main_loop == NULL");
161                 exit(0);
162         }
163
164         return FALSE;
165 }
166
167 gboolean _bt_reliable_terminate_service(gpointer user_data)
168 {
169         _bt_service_unregister();
170
171         /* Handle remaining pending requests finally by using g_idle_add()
172            again without well-known name. */
173         g_idle_add((GSourceFunc)__bt_main_loop_quit_idle_cb, NULL);
174
175         return FALSE;
176 }
177
178 static void __bt_check_factory_mode(void)
179 {
180 #ifdef TIZEN_FEATURE_BT_FACTORY_MODE
181         int factorymode_status = 0;
182         /* BT should not be activated automatically in factory binary */
183         /* factorymode_status :: 0 - normal binary, 1 - factory binary */
184         factorymode_status = bincfg_is_factory_binary();
185         if (factorymode_status != 0) {
186                 BT_INFO("factory mode [%d]", factorymode_status);
187                 if(vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
188                         BT_ERR("Set vconf failed");
189                 if(vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
190                         BT_ERR("Set vconf failed");
191         }
192 #endif
193 }
194
195 static gboolean __bt_check_bt_service(void *data)
196 {
197         int ret;
198         bt_status_t status = BT_DEACTIVATED;
199         bt_le_status_t le_status = BT_LE_DEACTIVATED;
200         int bt_status = VCONFKEY_BT_STATUS_OFF;
201         int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
202         int flight_mode_deactivation = 0;
203         int bt_off_due_to_timeout = 0;
204
205         if (_is_name_acquired() == FALSE) {
206                 BT_ERR("dbus name is NOT acquired yet");
207                 return TRUE;
208         }
209
210         status = _bt_adapter_get_status();
211         le_status = _bt_adapter_get_le_status();
212         BT_DBG("State: %d, LE State: %d", status, le_status);
213
214         if (TIZEN_PROFILE_WEARABLE) {
215                 if (status == BT_ACTIVATED) {
216                         BT_INFO("Need to recover because bt has been activated without bt-service");
217                         ret = _bt_recover_adapter();
218                         if (ret != BLUETOOTH_ERROR_NONE)
219                                 BT_ERR("_bt_recover_adapter is failed : %d", ret);
220                         return FALSE;
221                 }
222         }
223
224         if (TIZEN_PROFILE_TV || TIZEN_FEATURE_ROBOT_REFERENCE) {
225 /*
226 #if TODO_40
227                 if (_bt_get_enable_timer_id() == 0)
228 #endif
229 */
230                         _bt_enable_adapter();
231         } else if (!headed_plugin_info->plugin_headed_enabled) {
232                 BT_DBG("Enable adapter if headless device");
233                 _bt_enable_adapter();
234         } else {
235                 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
236                         BT_DBG("no bluetooth device info, so BT was disabled at previous session");
237
238                 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
239                         BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
240
241                 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_deactivation) != 0)
242                         BT_ERR("Fail to get the flight_mode_deactivation value");
243
244                 if (vconf_get_int(BT_OFF_DUE_TO_TIMEOUT, &bt_off_due_to_timeout) != 0)
245                         BT_ERR("Fail to get BT_OFF_DUE_TO_TIMEOUT");
246
247                 BT_DBG("headed VCONF BT_STATUS:[%d] LE_STATUS:[%d] BT_OFF_DUE_TO_TIMEOUT:[%d]",
248                 bt_status, bt_le_status, bt_off_due_to_timeout);
249
250                 if (bt_off_due_to_timeout) {
251                         /* Set the vconf flag to 0 here, if BT need to be enabled
252                         bt_off_due_to_timeout variable already hold the old value */
253
254                         if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 0) != 0)
255                                 BT_ERR("Set vconf failed");
256                 }
257
258
259                 if ((bt_status != VCONFKEY_BT_STATUS_OFF || bt_off_due_to_timeout) &&
260                         (status == BT_DEACTIVATED)) {
261                         BT_DBG("Previous session was enabled.");
262
263                         /* Enable the BT */
264                         _bt_enable_adapter();
265                 }
266
267                 if ((bt_le_status == VCONFKEY_BT_LE_STATUS_ON) && (le_status == BT_LE_DEACTIVATED)) {
268                         BT_DBG("Previous session was le enabled. Turn BT LE on automatically.");
269
270
271                         /* Enable the BT LE */
272                         _bt_enable_adapter_le();
273
274                 } else {
275                         status = _bt_adapter_get_status();
276                         le_status = _bt_adapter_get_le_status();
277                         BT_DBG("State: %d, LE State: %d", status, le_status);
278
279                         if ((status != BT_ACTIVATING && status != BT_ACTIVATED) &&
280                                         (le_status != BT_LE_ACTIVATING && le_status != BT_LE_ACTIVATED)) {
281 #ifdef TIZEN_FEATURE_BT_CONTAINER
282                                 if (_bt_service_is_container() == FALSE)
283                                         _bt_terminate_service(NULL);
284 #else
285                                 _bt_terminate_service(NULL);
286 #endif
287                         }
288                 }
289         }
290         return FALSE;
291 }
292
293 int _bt_service_initialize(void)
294 {
295         int ret;
296
297         if (is_initialized == TRUE)
298                 return BLUETOOTH_ERROR_NONE;
299
300         /* Security Initialization */
301         if (_bt_service_cynara_init() != BLUETOOTH_ERROR_NONE) {
302                 BT_ERR("Fail to init cynara");
303                 return EXIT_FAILURE;
304         }
305
306 #ifdef TIZEN_FEATURE_BT_CONTAINER
307         if (_bt_service_is_container() == TRUE) {
308                 /* Only dbus initialization is required in container to check privilege */
309                 ret = _bt_service_register_in_container();
310                 if (ret != BLUETOOTH_ERROR_NONE) {
311                         BT_ERR("Fail to register service");
312                         return ret;
313                 }
314
315                 is_initialized = TRUE;
316                 return BLUETOOTH_ERROR_NONE;
317         }
318 #endif
319
320         /* Flight mode handler */
321         _bt_service_register_vconf_handler();
322
323         /* Event sender Init */
324         ret = _bt_init_service_event_sender();
325         if (ret != BLUETOOTH_ERROR_NONE) {
326                 BT_ERR("Fail to init event sender");
327                 return ret;
328         }
329
330         ret = _bt_init_hf_local_term_event_sender();
331         if (ret != BLUETOOTH_ERROR_NONE) {
332                 BT_ERR("Fail to init core event sender");
333                 return ret;
334         }
335
336         /* Event reciever Init */
337         if (!TIZEN_PROFILE_TV) {
338                 if (_bt_init_obex_event_receiver() != BLUETOOTH_ERROR_NONE)
339                         BT_ERR("Fail to init obex event reciever");
340         }
341
342 #ifdef TIZEN_FEATURE_BT_PAN_NAP
343         /* Event reciever Init */
344         if (_bt_init_pan_nap_event_receiver() != BLUETOOTH_ERROR_NONE) {
345                 BT_ERR("Fail to init PAN NAP reciever");
346                 return 0;
347         }
348 #endif
349 #ifdef TIZEN_FEATURE_BT_IPSP
350         if (_bt_init_ipsp_receiver() != BLUETOOTH_ERROR_NONE) {
351                 BT_ERR("Fail to init ipsp reciever");
352                 return 0;
353         }
354 #endif
355         ret = _bt_service_register();
356         if (ret != BLUETOOTH_ERROR_NONE) {
357                 BT_ERR("Fail to register service");
358                 return ret;
359         }
360
361         /* BT Stack Init */
362         ret = _bt_stack_init();
363         if (ret != BLUETOOTH_ERROR_NONE) {
364                 BT_ERR("Fail to init BT Stack");
365                 return ret;
366         }
367
368         ret = _bt_service_le_init();
369         if (ret != BLUETOOTH_ERROR_NONE) {
370                 BT_ERR("Fail to init le");
371                 return ret;
372         }
373
374         _bt_init_request_id();
375
376         _bt_init_request_list();
377
378         ret = _bt_audio_init_absolute_volume_control();
379         if (ret != BLUETOOTH_ERROR_NONE)
380                 BT_ERR("Fail to init AVC");
381
382         is_initialized = TRUE;
383
384         return BLUETOOTH_ERROR_NONE;
385 }
386
387 int main(void)
388 {
389         struct sigaction sa;
390         /* ELL Start */
391         struct ell_event_source *source;
392         /* ELL End */
393         BT_INFO_C("### Starting the bt-service daemon");
394
395         if (!TIZEN_FEATURE_BT_SUPPORTED) {
396                 BT_INFO_C("BT feature is not supported, terminate bt-service");
397                 return 0;
398         }
399
400         memset(&sa, 0, sizeof(sa));
401         sa.sa_sigaction = __bt_sigterm_handler;
402         sa.sa_flags = SA_SIGINFO;
403         sigaction(SIGINT, &sa, NULL);
404         sigaction(SIGTERM, &sa, NULL);
405
406         __bt_check_factory_mode();
407
408         if (_bt_service_initialize() != BLUETOOTH_ERROR_NONE)
409                 return 0;
410
411         g_timeout_add(500, (GSourceFunc)__bt_check_bt_service, NULL);
412
413         if (terminated == TRUE) {
414                 __bt_release_service();
415                 return 0;
416         }
417
418         /* ELL Start */
419         BT_INFO("ELL Main init");
420         l_main_init();
421         /* ELL End */
422
423         bluetooth_plugin_init();
424
425         g_log_set_default_handler(__on_log_glib, NULL);
426
427         main_loop = g_main_loop_new(NULL, FALSE);
428
429         /* ELL Start */
430         source = (struct ell_event_source *) g_source_new(&event_funcs,
431                         sizeof(struct ell_event_source));
432
433         source->pollfd.fd = l_main_get_epoll_fd();
434         source->pollfd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
435
436         g_source_add_poll((GSource *)source, &source->pollfd);
437         g_source_attach((GSource *) source,
438                         g_main_loop_get_context(main_loop));
439
440         /* ELL End */
441         g_main_loop_run(main_loop);
442         BT_DBG("g_main_loop_quit called!");
443
444         bluetooth_plugin_deinit();
445
446         _bt_service_unref_connection();
447
448         /* ELL Start */
449         g_source_destroy((GSource *) source);
450         /* ELL End */
451
452         if (main_loop != NULL)
453                 g_main_loop_unref(main_loop);
454
455         if (terminated == FALSE)
456                 __bt_release_service();
457
458         /* ELL Start */
459         l_main_exit();
460         /* ELL End */
461
462         return 0;
463 }
464