Cody sync.: merge tizen 2.4 code from spin git
[platform/core/connectivity/bluetooth-frwk.git] / bt-core / bt-core-noti-handler.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Hocheol Seo <hocheol.seo@samsung.com>
7  *               Girishashok Joshi <girish.joshi@samsung.com>
8  *               Chanyeol Park <chanyeol.park@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *              http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23
24 #include <vconf.h>
25 #include <vconf-keys.h>
26
27 #include "bt-core-adapter.h"
28 #include "bt-core-common.h"
29 #include "bt-core-noti-handler.h"
30
31 static gboolean flightmode_request = FALSE;
32
33 void _bt_set_flightmode_request(gboolean value)
34 {
35         flightmode_request = value;
36 }
37
38 gboolean _bt_is_flightmode_request(void)
39 {
40         return flightmode_request;
41 }
42
43 static gboolean __bt_off_cause_conflict_check(void)
44 {
45         int flight_mode_value = 0;
46         int ps_mode_value = 0;
47         gboolean ret = FALSE;
48
49         retv_if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE,
50                                         &flight_mode_value) != 0, FALSE);
51         retv_if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE,
52                                         &ps_mode_value) != 0, FALSE);
53
54         if (flight_mode_value == 1 || ps_mode_value > 0) {
55                 BT_DBG("Bt should not turn on");
56                 ret = TRUE;
57         }
58
59         return ret;
60 }
61
62 static void __bt_core_handle_adapter_with_flight_mode(gboolean flight_mode)
63 {
64         bt_status_t adapter_status;
65         bt_le_status_t adapter_status_le;
66
67         adapter_status = _bt_core_get_status();
68         adapter_status_le = _bt_core_get_le_status();
69
70         BT_INFO("bt status %d, le status %d", adapter_status, adapter_status_le);
71         if (flight_mode == TRUE) {
72                 BT_INFO_C("Flight mode on. Turn off BT");
73
74                 if (adapter_status == BT_ACTIVATING || adapter_status_le == BT_LE_ACTIVATING) {
75                         BT_INFO("BT adapter is activating. Turn off BT after activation");
76                         _bt_set_flightmode_request(TRUE);
77                         return;
78                 }
79                 if (adapter_status != BT_ACTIVATED && adapter_status_le != BT_LE_ACTIVATED) {
80                         BT_INFO("No need to control bt status");
81                         return;
82                 }
83
84                 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
85                         BT_ERR("Set vconf failed");
86
87                 if (adapter_status == BT_ACTIVATED) {
88                         int bt_status_before_mode = 0;
89
90                         if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
91                                 _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
92
93                         _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
94                         _bt_disable_adapter();
95                 }
96
97                 if (adapter_status_le == BT_LE_ACTIVATED) {
98                         int bt_le_status_before_mode = 0;
99
100                         if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
101                                 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
102
103                         _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
104                         _bt_disable_adapter_le();
105                 }
106         } else {
107                 int flight_mode_value = 0;
108
109                 BT_INFO_C("Flight mode off. Turn on BT");
110
111                 if (adapter_status == BT_DEACTIVATING || adapter_status_le == BT_LE_DEACTIVATING) {
112                         BT_INFO("BT adapter is activating. Turn off BT after activation");
113                         _bt_set_flightmode_request(TRUE);
114                         return;
115                 }
116                 if (adapter_status != BT_DEACTIVATED && adapter_status_le != BT_LE_DEACTIVATED) {
117                         BT_INFO("No need to control bt status");
118                         return;
119                 }
120
121                 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value))
122                         BT_ERR("Fail get flight mode value");
123
124                 if (flight_mode_value == 0)
125                         return;
126
127                 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
128                         BT_ERR("Set vconf failed");
129
130                 ret_if(__bt_off_cause_conflict_check());
131
132                 if (adapter_status == BT_DEACTIVATED &&
133                     _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
134                         _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
135                         _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
136                         _bt_enable_adapter();
137                 }
138
139                 if (adapter_status_le == BT_LE_DEACTIVATED &&
140                     _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
141                         _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
142                         _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
143                         _bt_enable_adapter_le();
144                 }
145         }
146 }
147
148 static void __bt_core_handle_adapter_with_power_saving_mode(int power_saving_mode)
149 {
150         bt_status_t adapter_status;
151         bt_le_status_t adapter_status_le;
152
153         adapter_status = _bt_core_get_status();
154         adapter_status_le = _bt_core_get_le_status();
155
156         if (power_saving_mode == 2) {
157                 BT_DBG("Deactivate Bluetooth Service");
158                 if (vconf_set_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, 1) != 0)
159                         BT_ERR("Set vconf failed");
160
161                 if (adapter_status == BT_ACTIVATED) {
162                         int bt_status_before_mode = 0;
163                         if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
164                                 _bt_core_set_bt_status(BT_POWER_SAVING_MODE, bt_status_before_mode);
165
166                         _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
167                         _bt_disable_adapter();
168                 }
169                 if (adapter_status_le == BT_LE_ACTIVATED) {
170                         int bt_le_status_before_mode = 0;
171                         if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
172                                 _bt_core_set_bt_le_status(BT_POWER_SAVING_MODE, bt_le_status_before_mode);
173
174                         /* Disable the BT LE */
175                         _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
176                         _bt_disable_adapter_le();
177                 }
178         } else {
179                 int ps_mode_value = 0;
180
181                 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_value))
182                         BT_ERR("Fail get power saving mode value");
183
184                 if (ps_mode_value == 0)
185                         return;
186
187                 BT_DBG("Activate Bluetooth");
188                 if (vconf_set_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, 0))
189                         BT_ERR("Set vconf failed");
190
191                 ret_if(__bt_off_cause_conflict_check());
192
193                 BT_DBG("BT status before Emergency mode() :%d",
194                         _bt_core_get_bt_status(BT_POWER_SAVING_MODE));
195
196                 if (adapter_status == BT_DEACTIVATED && (_bt_core_get_bt_status(BT_POWER_SAVING_MODE) != 0)) {
197                         _bt_core_set_bt_status(BT_POWER_SAVING_MODE, 0);
198                         _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
199                         _bt_enable_adapter();
200                 }
201                 BT_DBG("BT LE status before Emergency mode() :%d", _bt_core_get_bt_le_status(BT_POWER_SAVING_MODE));
202                 if (adapter_status_le == BT_LE_DEACTIVATED &&  _bt_core_get_bt_le_status(BT_POWER_SAVING_MODE) != 0) {
203                         _bt_core_set_bt_le_status(BT_POWER_SAVING_MODE, 0);
204                         /* Enable the BT LE */
205                         _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
206                         _bt_enable_adapter_le();
207                 }
208         }
209 }
210 #ifdef TIZEN_BT_FLIGHTMODE_ENABLED
211 static void __bt_core_flight_mode_cb(keynode_t *node, void *data)
212 {
213         gboolean flight_mode = FALSE;
214         int type;
215
216         BT_DBG("key = %s", vconf_keynode_get_name(node));
217
218         type = vconf_keynode_get_type(node);
219         if (type != VCONF_TYPE_BOOL) {
220                 BT_ERR("Invaild vconf key type : %d", type);
221                 return;
222         }
223
224         flight_mode = vconf_keynode_get_bool(node);
225
226         __bt_core_handle_adapter_with_flight_mode(flight_mode);
227 }
228 #endif
229
230 #ifndef TIZEN_WEARABLE
231 static void __bt_core_power_saving_mode_cb(keynode_t *node, void *data)
232 {
233         int power_saving_mode = 0;
234
235         DBG_SECURE("key=%s", vconf_keynode_get_name(node));
236
237         if (vconf_keynode_get_type(node) != VCONF_TYPE_INT) {
238                 BT_ERR("Wrong vconf type");
239                 return;
240         }
241
242         power_saving_mode = vconf_keynode_get_int(node);
243
244         BT_DBG("value=%d", power_saving_mode);
245
246         __bt_core_handle_adapter_with_power_saving_mode(power_saving_mode);
247 }
248 #endif
249
250 void _bt_core_init_vconf_value(void)
251 {
252         gboolean flight_mode = FALSE;
253         int power_saving_mode = 0;
254
255         _bt_core_handle_flight_mode_noti();
256         _bt_core_handle_power_saving_mode_noti();
257
258         flight_mode = _bt_core_is_flight_mode_enabled();
259
260 #ifndef TIZEN_WEARABLE
261         if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &power_saving_mode) != 0)
262                 BT_ERR("Fail to get the power_saving_mode status value");
263 #endif
264         BT_DBG("flight_mode = %d, power_saving_mode = %d", flight_mode, power_saving_mode);
265
266         if (flight_mode == TRUE)
267                 __bt_core_handle_adapter_with_flight_mode(flight_mode);
268         else if (power_saving_mode > 0)
269                 __bt_core_handle_adapter_with_power_saving_mode(power_saving_mode);
270         else
271                 BT_ERR("");
272 }
273
274 void _bt_core_handle_flight_mode_noti(void)
275 {
276 #ifdef TIZEN_BT_FLIGHTMODE_ENABLED
277         int ret;
278
279         BT_DBG("+");
280
281         ret = vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
282                         (vconf_callback_fn)__bt_core_flight_mode_cb, NULL);
283         if (ret < 0)
284                 BT_ERR("Unable to register key handler");
285 #else
286         BT_DBG("Telephony is disabled");
287 #endif
288 }
289
290 void _bt_core_handle_power_saving_mode_noti(void)
291 {
292 #ifndef TIZEN_WEARABLE
293         int ret;
294
295         BT_DBG("+");
296         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
297                         (vconf_callback_fn)__bt_core_power_saving_mode_cb, NULL);
298
299         if (ret < 0)
300                 BT_ERR("Unable to register key handler");
301 #endif
302 }
303
304 void _bt_core_unregister_vconf_handler(void)
305 {
306 #ifdef TIZEN_BT_FLIGHTMODE_ENABLED
307         vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
308                         (vconf_callback_fn)__bt_core_flight_mode_cb);
309 #endif
310
311 #ifndef TIZEN_WEARABLE
312         vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
313                         (vconf_callback_fn)__bt_core_power_saving_mode_cb);
314 #endif
315
316         return;
317 }
318