2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <eventsystem.h>
25 #include "bt-internal-types.h"
26 #include "bt-service-common.h"
27 #include "bt-service-event.h"
28 #include "bt-service-main.h"
29 #include "bt-service-util.h"
30 #include "bt-request-handler.h"
31 #include "bt-service-adapter.h"
32 #include "bt-service-adapter-le.h"
34 static GMainLoop *main_loop;
35 static gboolean terminated = FALSE;
37 static void __bt_release_service(void)
39 _bt_service_unregister_vconf_handler();
41 _bt_deinit_service_event_sender();
43 _bt_service_unregister();
47 _bt_clear_request_list();
49 _bt_service_cynara_deinit();
51 BT_DBG("Terminating the bt-service daemon");
54 static void __bt_sigterm_handler(int signo, siginfo_t *info, void *data)
56 BT_INFO("signal [%d] is sent by [%d]", signo, info->si_pid);
61 gboolean _bt_terminate_service(gpointer user_data)
63 int bt_status = VCONFKEY_BT_STATUS_OFF;
65 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0) {
66 BT_ERR("no bluetooth device info, so BT was disabled at previous session");
68 if (bt_status != VCONFKEY_BT_STATUS_OFF) {
69 if (vconf_set_int(VCONFKEY_BT_STATUS,
70 VCONFKEY_BT_STATUS_OFF) != 0)
71 BT_ERR("Set vconf failed\n");
73 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
74 EVT_VAL_BT_OFF) != ES_R_OK)
75 BT_ERR("Fail to set value");
79 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_status) < 0) {
80 BT_ERR("no bluetooth device info, so BT was disabled at previous session");
82 if (bt_status != VCONFKEY_BT_LE_STATUS_OFF) {
83 if (vconf_set_int(VCONFKEY_BT_LE_STATUS,
84 VCONFKEY_BT_LE_STATUS_OFF) != 0)
85 BT_ERR("Set vconf failed\n");
86 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
87 EVT_VAL_BT_LE_OFF) != ES_R_OK)
88 BT_ERR("Fail to set value");
92 if (main_loop != NULL) {
93 g_main_loop_quit(main_loop);
95 BT_ERR("main_loop == NULL");
96 __bt_release_service();
104 gboolean _bt_reliable_terminate_service(gpointer user_data)
108 _bt_clear_request_list();
110 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
112 _bt_deinit_service_event_sender();
114 _bt_service_unregister();
118 BT_INFO_C("Terminating the bt-service daemon");
120 if (main_loop != NULL) {
121 g_main_loop_quit(main_loop);
129 static gboolean __bt_check_bt_service(void *data)
131 bt_status_t status = BT_DEACTIVATED;
132 bt_le_status_t le_status = BT_LE_DEACTIVATED;
134 int bt_status = VCONFKEY_BT_STATUS_OFF;
135 int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
138 status = _bt_adapter_get_status();
139 le_status = _bt_adapter_get_le_status();
140 BT_DBG("State: %d, LE State: %d", status, le_status);
143 _bt_enable_adapter();
145 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0) {
146 BT_DBG("no bluetooth device info, so BT was disabled at previous session");
149 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0) {
150 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
153 if ((bt_status != VCONFKEY_BT_STATUS_OFF) &&
154 (status == BT_DEACTIVATED)) {
155 BT_DBG("Previous session was enabled.");
158 _bt_enable_adapter();
161 if ((bt_le_status == VCONFKEY_BT_LE_STATUS_ON) && (le_status == BT_LE_DEACTIVATED)) {
162 BT_DBG("Previous session was le enabled. Turn BT LE on automatically.");
164 /* Enable the BT LE */
165 _bt_enable_adapter_le();
167 status = _bt_adapter_get_status();
168 le_status = _bt_adapter_get_le_status();
169 BT_DBG("State: %d, LE State: %d", status, le_status);
171 if ((status != BT_ACTIVATING && status != BT_ACTIVATED) &&
172 (le_status != BT_LE_ACTIVATING && le_status != BT_LE_ACTIVATED)) {
173 _bt_terminate_service(NULL);
183 BT_INFO_C("Starting the bt-service daemon");
185 memset(&sa, 0, sizeof(sa));
186 sa.sa_sigaction = __bt_sigterm_handler;
187 sa.sa_flags = SA_SIGINFO;
188 sigaction(SIGINT, &sa, NULL);
189 sigaction(SIGTERM, &sa, NULL);
191 /* Security Initialization */
192 if (_bt_service_cynara_init() != BLUETOOTH_ERROR_NONE) {
193 BT_ERR("Fail to init cynara");
197 /* Event sender Init */
198 if (_bt_init_service_event_sender() != BLUETOOTH_ERROR_NONE) {
199 BT_ERR("Fail to init event sender");
203 if (_bt_service_register() != BLUETOOTH_ERROR_NONE) {
204 BT_ERR("Fail to register service");
208 _bt_init_request_id();
210 _bt_init_request_list();
212 g_timeout_add(500, (GSourceFunc)__bt_check_bt_service, NULL);
214 if (terminated == TRUE) {
215 __bt_release_service();
219 main_loop = g_main_loop_new(NULL, FALSE);
221 g_main_loop_run(main_loop);
222 BT_DBG("g_main_loop_quit called!");
224 if (main_loop != NULL) {
225 g_main_loop_unref(main_loop);
228 if (terminated == FALSE)
229 __bt_release_service();