Disconnect rfcomm connections
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-emul / 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 #include <bundle.h>
23 #include <eventsystem.h>
24
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"
33
34 static GMainLoop *main_loop;
35 static gboolean terminated = FALSE;
36
37 static void __bt_release_service(void)
38 {
39         _bt_service_unregister_vconf_handler();
40
41         _bt_deinit_service_event_sender();
42
43         _bt_service_unregister();
44
45         _bt_deinit_proxys();
46
47         _bt_clear_request_list();
48
49         _bt_service_cynara_deinit();
50
51         BT_DBG("Terminating the bt-service daemon");
52 }
53
54 static void __bt_sigterm_handler(int signo, siginfo_t *info, void *data)
55 {
56         BT_INFO("signal [%d] is sent by [%d]", signo, info->si_pid);
57
58         return;
59 }
60
61 gboolean _bt_terminate_service(gpointer user_data)
62 {
63         int bt_status = VCONFKEY_BT_STATUS_OFF;
64
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");
67         } else {
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");
72
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");
76                 }
77         }
78
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");
81         } else {
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");
89                 }
90         }
91
92         if (main_loop != NULL) {
93                 g_main_loop_quit(main_loop);
94         } else {
95                 BT_ERR("main_loop == NULL");
96                 __bt_release_service();
97                 terminated = TRUE;
98                 exit(0);
99         }
100
101         return FALSE;
102 }
103
104 gboolean _bt_reliable_terminate_service(gpointer user_data)
105 {
106         _bt_deinit_proxys();
107
108         _bt_clear_request_list();
109
110         _bt_set_disabled(BLUETOOTH_ERROR_NONE);
111
112         _bt_deinit_service_event_sender();
113
114         _bt_service_unregister();
115
116         terminated = TRUE;
117
118         BT_INFO_C("Terminating the bt-service daemon");
119
120         if (main_loop != NULL)
121                 g_main_loop_quit(main_loop);
122         else
123                 exit(0);
124
125         return FALSE;
126 }
127
128 static gboolean __bt_check_bt_service(void *data)
129 {
130         bt_status_t status = BT_DEACTIVATED;
131         bt_le_status_t le_status = BT_LE_DEACTIVATED;
132 #ifndef TIZEN_TV
133         int bt_status = VCONFKEY_BT_STATUS_OFF;
134         int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
135 #endif
136
137         status = _bt_adapter_get_status();
138         le_status = _bt_adapter_get_le_status();
139         BT_DBG("State: %d, LE State: %d", status, le_status);
140
141 #ifdef TIZEN_TV
142         _bt_enable_adapter();
143 #else
144         if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
145                 BT_DBG("no bluetooth device info, so BT was disabled at previous session");
146
147         if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
148                 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
149
150         if ((bt_status != VCONFKEY_BT_STATUS_OFF) &&
151                 (status == BT_DEACTIVATED)) {
152                 BT_DBG("Previous session was enabled.");
153
154                 /* Enable the BT */
155                 _bt_enable_adapter();
156         }
157
158         if ((bt_le_status == VCONFKEY_BT_LE_STATUS_ON) && (le_status == BT_LE_DEACTIVATED)) {
159                 BT_DBG("Previous session was le enabled. Turn BT LE on automatically.");
160
161                 /* Enable the BT LE */
162                 _bt_enable_adapter_le();
163         } else {
164                 status = _bt_adapter_get_status();
165                 le_status = _bt_adapter_get_le_status();
166                 BT_DBG("State: %d, LE State: %d", status, le_status);
167
168                 if ((status != BT_ACTIVATING && status != BT_ACTIVATED) &&
169                                 (le_status != BT_LE_ACTIVATING && le_status != BT_LE_ACTIVATED)) {
170                         _bt_terminate_service(NULL);
171                 }
172         }
173 #endif
174         return FALSE;
175 }
176
177 int main(void)
178 {
179         struct sigaction sa;
180         BT_INFO_C("Starting the bt-service daemon");
181
182         memset(&sa, 0, sizeof(sa));
183         sa.sa_sigaction = __bt_sigterm_handler;
184         sa.sa_flags = SA_SIGINFO;
185         sigaction(SIGINT, &sa, NULL);
186         sigaction(SIGTERM, &sa, NULL);
187
188         /* Security Initialization */
189         if (_bt_service_cynara_init() != BLUETOOTH_ERROR_NONE) {
190                 BT_ERR("Fail to init cynara");
191                 return EXIT_FAILURE;
192         }
193
194         /* Event sender Init */
195         if (_bt_init_service_event_sender() != BLUETOOTH_ERROR_NONE) {
196                 BT_ERR("Fail to init event sender");
197                 return 0;
198         }
199
200         if (_bt_service_register() != BLUETOOTH_ERROR_NONE) {
201                 BT_ERR("Fail to register service");
202                 return 0;
203         }
204
205         _bt_init_request_id();
206
207         _bt_init_request_list();
208
209         g_timeout_add(500, (GSourceFunc)__bt_check_bt_service, NULL);
210
211         if (terminated == TRUE) {
212                 __bt_release_service();
213                 return 0;
214         }
215
216         main_loop = g_main_loop_new(NULL, FALSE);
217
218         g_main_loop_run(main_loop);
219         BT_DBG("g_main_loop_quit called!");
220
221         if (main_loop != NULL)
222                 g_main_loop_unref(main_loop);
223
224         if (terminated == FALSE)
225                 __bt_release_service();
226
227         return 0;
228 }
229