5841c3952bf8ae961ff41dc734ef8e7cad8ad6bb
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-main.c
1 /*
2  * bluetooth-frwk
3  *
4  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  *              http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include <dbus/dbus-glib.h>
21 #include <glib.h>
22 #include <dlog.h>
23 #include <string.h>
24 #include <privilege-control.h>
25 #include <vconf.h>
26
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"
34
35 static GMainLoop *main_loop;
36 static gboolean terminated;
37
38 static void __bt_release_service(void)
39 {
40         _bt_deinit_service_event_sender();
41         _bt_deinit_service_event_reciever();
42
43         _bt_service_unregister();
44
45         _bt_deinit_proxys();
46
47         _bt_clear_request_list();
48
49         BT_DBG("Terminating the bt-service daemon");
50 }
51
52 static void __bt_sigterm_handler(int signo)
53 {
54         BT_DBG("Get the signal: %d", signo);
55
56         _bt_terminate_service(NULL);
57 }
58
59 gboolean _bt_terminate_service(gpointer user_data)
60 {
61         int value;
62
63         if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &value) != 0)
64                 BT_ERR("Fail to get the flight_mode_deactivated value");
65
66         if (value == 1) {
67                 BT_DBG("Bt service not terminated");
68
69                 _bt_deinit_bluez_proxy();
70
71                 return FALSE;
72         }
73
74         if (main_loop != NULL) {
75                 g_main_loop_quit(main_loop);
76         } else {
77                 BT_DBG("main_loop == NULL");
78                 __bt_release_service();
79                 terminated = TRUE;
80                 exit(0);
81         }
82
83         return FALSE;
84 }
85
86 /* align bt-service state (adapter_status and vconf keys) on BlueZ state */
87 static gboolean __bt_check_bt_service(void *data)
88 {
89         int bt_status = VCONFKEY_BT_STATUS_OFF;
90         int flight_mode_deactivation = 0;
91
92         int adapter_enabled = 0;
93
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");
96         }
97
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");
100
101         _bt_check_adapter(&adapter_enabled);
102
103         BT_DBG("get bt adapter status: %d when starting bt-service ", adapter_enabled);
104
105         _bt_adapter_set_status(adapter_enabled);
106
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);
111
112                 bt_status = adapter_enabled;
113         }
114
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();
118                 return FALSE;
119         }
120
121         if (adapter_enabled == TRUE) {
122                 BT_DBG("");
123                 _bt_handle_adapter_added();
124         }
125
126         return FALSE;
127 }
128
129 int main(void)
130 {
131         struct sigaction sa;
132         BT_DBG("Starting the bt-service daemon");
133
134         memset(&sa, 0, sizeof(sa));
135         sa.sa_handler = __bt_sigterm_handler;
136         sigaction(SIGINT, &sa, NULL);
137         sigaction(SIGTERM, &sa, NULL);
138
139         if (perm_app_set_privilege("bluetooth-frwk-service", NULL, NULL) !=
140                                                                 PC_OPERATION_SUCCESS)
141                 BT_ERR("Failed to set app privilege.\n");
142
143         /* Event reciever Init */
144         if (_bt_init_service_event_receiver() != BLUETOOTH_ERROR_NONE) {
145                 BT_ERR("Fail to init event reciever");
146                 return 0;
147         }
148
149         /* Event sender Init */
150         if (_bt_init_service_event_sender() != BLUETOOTH_ERROR_NONE) {
151                 BT_ERR("Fail to init event sender");
152                 return 0;
153         }
154
155         if (_bt_service_register() != BLUETOOTH_ERROR_NONE) {
156                 BT_ERR("Fail to register service");
157                 return 0;
158         }
159
160         _bt_init_request_id();
161
162         _bt_init_request_list();
163
164         g_idle_add((GSourceFunc)__bt_check_bt_service, NULL);
165
166         if (terminated == TRUE) {
167                 __bt_release_service();
168                 return 0;
169         }
170
171         main_loop = g_main_loop_new(NULL, FALSE);
172
173         g_main_loop_run(main_loop);
174
175         if (main_loop != NULL) {
176                 g_main_loop_unref(main_loop);
177         }
178
179         __bt_release_service();
180
181         return 0;
182 }
183