Merge branch 'master' into tizen_2.1
[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 static gboolean __bt_check_bt_service(void *data)
87 {
88         int bt_status = VCONFKEY_BT_STATUS_OFF;
89         int flight_mode_deactivation = 0;
90
91         if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0) {
92                 BT_DBG("no bluetooth device info, so BT was disabled at previous session");
93         }
94
95         if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_deactivation) != 0)
96                         BT_ERR("Fail to get the flight_mode_deactivated value");
97
98         if (bt_status != VCONFKEY_BT_STATUS_OFF) {
99                 BT_DBG("Previous session was enabled.");
100
101                 /* Enable the BT */
102                 _bt_enable_adapter();
103         } else if (bt_status == VCONFKEY_BT_STATUS_OFF &&
104                                         flight_mode_deactivation == 1) {
105                 _bt_handle_flight_mode_noti();
106         } else {
107                 bt_status_t status = _bt_adapter_get_status();
108                 int adapter_enabled = 0;
109
110                 _bt_check_adapter(&adapter_enabled);
111
112                 BT_DBG("State: %d", status);
113                 BT_DBG("Adapter enabled: %d", adapter_enabled);
114
115                 if (adapter_enabled == 1) {
116                         _bt_handle_adapter_added();
117                         return FALSE;
118                 }
119
120                 if (status != BT_ACTIVATING && status != BT_ACTIVATED) {
121                         _bt_terminate_service(NULL);
122                 }
123         }
124
125         return FALSE;
126 }
127
128 int main(void)
129 {
130         struct sigaction sa;
131         BT_DBG("Starting the bt-service daemon");
132
133         memset(&sa, 0, sizeof(sa));
134         sa.sa_handler = __bt_sigterm_handler;
135         sigaction(SIGINT, &sa, NULL);
136         sigaction(SIGTERM, &sa, NULL);
137
138         g_type_init();
139
140         if (set_app_privilege("bluetooth-frwk-service", NULL, NULL) !=
141                                                                 PC_OPERATION_SUCCESS)
142                 BT_ERR("Failed to set app privilege.\n");
143
144         /* Event reciever Init */
145         if (_bt_init_service_event_receiver() != BLUETOOTH_ERROR_NONE) {
146                 BT_ERR("Fail to init event reciever");
147                 return 0;
148         }
149
150         /* Event sender Init */
151         if (_bt_init_service_event_sender() != BLUETOOTH_ERROR_NONE) {
152                 BT_ERR("Fail to init event sender");
153                 return 0;
154         }
155
156         if (_bt_service_register() != BLUETOOTH_ERROR_NONE) {
157                 BT_ERR("Fail to register service");
158                 return 0;
159         }
160
161         _bt_init_request_id();
162
163         _bt_init_request_list();
164
165         g_idle_add((GSourceFunc)__bt_check_bt_service, NULL);
166
167         if (terminated == TRUE) {
168                 __bt_release_service();
169                 return 0;
170         }
171
172         main_loop = g_main_loop_new(NULL, FALSE);
173
174         g_main_loop_run(main_loop);
175
176         if (main_loop != NULL) {
177                 g_main_loop_unref(main_loop);
178         }
179
180         __bt_release_service();
181
182         return 0;
183 }
184