4 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #include <dbus/dbus-glib.h>
24 #include <privilege-control.h>
27 #include "bt-internal-types.h"
28 #include "bt-service-common.h"
29 #include "bt-service-event.h"
30 #include "bt-service-main.h"
31 #include "bt-service-util.h"
32 #include "bt-request-handler.h"
33 #include "bt-service-adapter.h"
35 static GMainLoop *main_loop;
36 static gboolean terminated;
38 static void __bt_release_service(void)
40 _bt_deinit_service_event_sender();
41 _bt_deinit_service_event_reciever();
43 _bt_service_unregister();
47 _bt_clear_request_list();
49 BT_DBG("Terminating the bt-service daemon");
52 static void __bt_sigterm_handler(int signo)
54 BT_DBG("Get the signal: %d", signo);
56 _bt_terminate_service(NULL);
59 gboolean _bt_terminate_service(gpointer user_data)
63 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &value) != 0)
64 BT_ERR("Fail to get the flight_mode_deactivated value");
67 BT_DBG("Bt service not terminated");
69 _bt_deinit_bluez_proxy();
74 if (main_loop != NULL) {
75 g_main_loop_quit(main_loop);
77 BT_DBG("main_loop == NULL");
78 __bt_release_service();
86 /* align bt-service state (adapter_status and vconf keys) on BlueZ state */
87 static gboolean __bt_check_bt_service(void *data)
89 int bt_status = VCONFKEY_BT_STATUS_OFF;
90 int flight_mode_deactivation = 0;
92 int adapter_enabled = 0;
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");
98 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_deactivation) != 0)
99 BT_ERR("Fail to get the flight_mode_deactivated value");
101 _bt_check_adapter(&adapter_enabled);
103 BT_DBG("get bt adapter status: %d when starting bt-service ", adapter_enabled);
105 _bt_adapter_set_status(adapter_enabled);
107 if (adapter_enabled != bt_status) {
108 BT_DBG("align vconf bt status key with real bluetooth status");
109 if (vconf_set_int(VCONFKEY_BT_STATUS, adapter_enabled) != 0)
110 BT_ERR("Set vconf key %s failed", VCONFKEY_BT_STATUS);
112 bt_status = adapter_enabled;
115 if (bt_status == VCONFKEY_BT_STATUS_OFF && flight_mode_deactivation == 1) {
116 BT_ERR("call _bt_handle_flight_mode_noti()");
117 _bt_handle_flight_mode_noti();
121 if (adapter_enabled == TRUE) {
123 _bt_handle_adapter_added();
132 BT_DBG("Starting the bt-service daemon");
134 memset(&sa, 0, sizeof(sa));
135 sa.sa_handler = __bt_sigterm_handler;
136 sigaction(SIGINT, &sa, NULL);
137 sigaction(SIGTERM, &sa, NULL);
139 if (perm_app_set_privilege("bluetooth-frwk-service", NULL, NULL) !=
140 PC_OPERATION_SUCCESS)
141 BT_ERR("Failed to set app privilege.\n");
143 /* Event reciever Init */
144 if (_bt_init_service_event_receiver() != BLUETOOTH_ERROR_NONE) {
145 BT_ERR("Fail to init event reciever");
149 /* Event sender Init */
150 if (_bt_init_service_event_sender() != BLUETOOTH_ERROR_NONE) {
151 BT_ERR("Fail to init event sender");
155 if (_bt_service_register() != BLUETOOTH_ERROR_NONE) {
156 BT_ERR("Fail to register service");
160 _bt_init_request_id();
162 _bt_init_request_list();
164 g_idle_add((GSourceFunc)__bt_check_bt_service, NULL);
166 if (terminated == TRUE) {
167 __bt_release_service();
171 main_loop = g_main_loop_new(NULL, FALSE);
173 g_main_loop_run(main_loop);
175 if (main_loop != NULL) {
176 g_main_loop_unref(main_loop);
179 __bt_release_service();