Fix the instance id's miss-matching issue
[platform/core/connectivity/bluetooth-frwk.git] / bt-core / bt-core-noti-handler.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 <vconf.h>
19 #include <vconf-keys.h>
20
21 #include "bt-core-adapter.h"
22 #include "bt-core-common.h"
23 #include "bt-core-noti-handler.h"
24
25 static gboolean flightmode_request = FALSE;
26
27 void _bt_set_flightmode_request(gboolean value)
28 {
29         flightmode_request = value;
30 }
31
32 gboolean _bt_is_flightmode_request(void)
33 {
34         return flightmode_request;
35 }
36
37 static gboolean __bt_off_cause_conflict_check(void)
38 {
39         int flight_mode_value = 0;
40         int ps_mode_value = 0;
41         gboolean ret = FALSE;
42
43         retv_if(vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE,
44                                         &flight_mode_value) != 0, FALSE);
45         retv_if(vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE,
46                                         &ps_mode_value) != 0, FALSE);
47
48         if (flight_mode_value == 1 || ps_mode_value > 0) {
49                 BT_DBG("Bt should not turn on");
50                 ret = TRUE;
51         }
52
53         return ret;
54 }
55
56 static void __bt_core_handle_adapter_with_flight_mode(gboolean flight_mode)
57 {
58         bt_status_t adapter_status;
59         bt_le_status_t adapter_status_le;
60
61         adapter_status = _bt_core_get_status();
62         adapter_status_le = _bt_core_get_le_status();
63
64         BT_INFO("bt status %d, le status %d", adapter_status, adapter_status_le);
65         if (flight_mode == TRUE) {
66                 BT_INFO_C("Flight mode on. Turn off BT");
67
68                 if (adapter_status == BT_ACTIVATING || adapter_status_le == BT_LE_ACTIVATING) {
69                         BT_INFO("BT adapter is activating. Turn off BT after activation");
70                         _bt_set_flightmode_request(TRUE);
71                         return;
72                 }
73                 if (adapter_status != BT_ACTIVATED && adapter_status_le != BT_LE_ACTIVATED) {
74                         BT_INFO("No need to control bt status");
75                         return;
76                 }
77
78                 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
79                         BT_ERR("Set vconf failed");
80
81                 if (adapter_status == BT_ACTIVATED) {
82                         int bt_status_before_mode = 0;
83
84                         if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
85                                 _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
86
87 #ifdef TIZEN_FEATURE_BUSACT
88                         _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
89 #endif
90                         if (!TIZEN_FEATURE_BT_USB_DONGLE)
91                                 _bt_disable_adapter();
92                 }
93
94                 if (adapter_status_le == BT_LE_ACTIVATED) {
95                         int bt_le_status_before_mode = 0;
96
97                         if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
98                                 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
99
100 #ifdef TIZEN_FEATURE_BUSACT
101                         _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
102 #endif
103                         if (!TIZEN_FEATURE_BT_USB_DONGLE)
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                         if (!TIZEN_FEATURE_BT_USB_DONGLE)
137                                 _bt_enable_adapter();
138                 }
139
140                 if (adapter_status_le == BT_LE_DEACTIVATED &&
141                     _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
142                         _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
143                         _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
144                         if (!TIZEN_FEATURE_BT_USB_DONGLE)
145                                 _bt_enable_adapter_le();
146                 }
147         }
148 }
149
150 static void __bt_core_handle_adapter_with_power_saving_mode(int power_saving_mode)
151 {
152         bt_status_t adapter_status;
153         bt_le_status_t adapter_status_le;
154
155         adapter_status = _bt_core_get_status();
156         adapter_status_le = _bt_core_get_le_status();
157
158         if (power_saving_mode == 2) {
159                 BT_DBG("Deactivate Bluetooth Service");
160                 if (vconf_set_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, 1) != 0)
161                         BT_ERR("Set vconf failed");
162
163                 if (adapter_status == BT_ACTIVATED) {
164                         int bt_status_before_mode = 0;
165                         if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
166                                 _bt_core_set_bt_status(BT_POWER_SAVING_MODE, bt_status_before_mode);
167
168 #ifdef TIZEN_FEATURE_BUSACT
169                         _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
170 #endif
171                         if (!TIZEN_FEATURE_BT_USB_DONGLE)
172                                 _bt_disable_adapter();
173                 }
174                 if (adapter_status_le == BT_LE_ACTIVATED) {
175                         int bt_le_status_before_mode = 0;
176
177                         if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
178                                 _bt_core_set_bt_le_status(BT_POWER_SAVING_MODE, bt_le_status_before_mode);
179
180 #ifdef TIZEN_FEATURE_BUSACT
181                         /* Disable the BT LE */
182                         _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
183 #endif
184                         if (!TIZEN_FEATURE_BT_USB_DONGLE)
185                                 _bt_disable_adapter_le();
186                 }
187         } else {
188                 int ps_mode_value = 0;
189
190                 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_value))
191                         BT_ERR("Fail get power saving mode value");
192
193                 if (ps_mode_value == 0)
194                         return;
195
196                 BT_DBG("Activate Bluetooth");
197                 if (vconf_set_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, 0))
198                         BT_ERR("Set vconf failed");
199
200                 ret_if(__bt_off_cause_conflict_check());
201
202                 BT_DBG("BT status before Emergency mode() :%d",
203                         _bt_core_get_bt_status(BT_POWER_SAVING_MODE));
204
205                 if (adapter_status == BT_DEACTIVATED && (_bt_core_get_bt_status(BT_POWER_SAVING_MODE) != 0)) {
206                         _bt_core_set_bt_status(BT_POWER_SAVING_MODE, 0);
207                         _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
208                         if (!TIZEN_FEATURE_BT_USB_DONGLE)
209                                 _bt_enable_adapter();
210                 }
211                 BT_DBG("BT LE status before Emergency mode() :%d", _bt_core_get_bt_le_status(BT_POWER_SAVING_MODE));
212                 if (adapter_status_le == BT_LE_DEACTIVATED &&  _bt_core_get_bt_le_status(BT_POWER_SAVING_MODE) != 0) {
213                         _bt_core_set_bt_le_status(BT_POWER_SAVING_MODE, 0);
214                         /* Enable the BT LE */
215                         _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
216                         if (!TIZEN_FEATURE_BT_USB_DONGLE)
217                                 _bt_enable_adapter_le();
218                 }
219         }
220 }
221
222 static void __bt_core_flight_mode_cb(keynode_t *node, void *data)
223 {
224         gboolean flight_mode = FALSE;
225         int type;
226
227         BT_DBG("key = %s", vconf_keynode_get_name(node));
228
229         type = vconf_keynode_get_type(node);
230         if (type != VCONF_TYPE_BOOL) {
231                 BT_ERR("Invaild vconf key type : %d", type);
232                 return;
233         }
234
235         flight_mode = vconf_keynode_get_bool(node);
236
237         __bt_core_handle_adapter_with_flight_mode(flight_mode);
238 }
239
240 #ifdef ENABLE_TIZEN_2_4
241 static void __bt_core_power_saving_mode_cb(keynode_t *node, void *data)
242 {
243         int power_saving_mode = 0;
244
245         DBG_SECURE("key=%s", vconf_keynode_get_name(node));
246
247         if (vconf_keynode_get_type(node) != VCONF_TYPE_INT) {
248                 BT_ERR("Wrong vconf type");
249                 return;
250         }
251
252         power_saving_mode = vconf_keynode_get_int(node);
253
254         BT_DBG("value=%d", power_saving_mode);
255
256         __bt_core_handle_adapter_with_power_saving_mode(power_saving_mode);
257 }
258 #endif
259
260 void _bt_core_init_vconf_value(void)
261 {
262         gboolean flight_mode = FALSE;
263 #ifndef ENABLE_TIZEN_2_4
264         int power_saving_mode = 0;
265 #endif
266         int bt_flight_mode = 0;
267         int bt_ps_mode = 0;
268
269         _bt_core_handle_flight_mode_noti();
270         _bt_core_handle_power_saving_mode_noti();
271
272         flight_mode = _bt_core_is_flight_mode_enabled();
273
274 #ifndef ENABLE_TIZEN_2_4
275         if (!TIZEN_PROFILE_WEARABLE) {
276                 if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &power_saving_mode) != 0)
277                         BT_ERR("Fail to get the power_saving_mode status value");
278                 BT_DBG("flight_mode = %d, power_saving_mode = %d", flight_mode, power_saving_mode);
279         }
280 #endif
281         BT_DBG("flight_mode = %d, power_saving_mode = %d", flight_mode, power_saving_mode);
282
283         if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &bt_flight_mode))
284                 BT_ERR("Fail get flight mode value");
285         _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_flight_mode);
286
287         if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &bt_ps_mode))
288                 BT_ERR("Fail get power saving mode value");
289         _bt_core_set_bt_status(BT_POWER_SAVING_MODE, bt_ps_mode);
290
291         if (flight_mode == TRUE)
292                 __bt_core_handle_adapter_with_flight_mode(flight_mode);
293 #ifndef ENABLE_TIZEN_2_4
294         else if (power_saving_mode > 0)
295                 __bt_core_handle_adapter_with_power_saving_mode(power_saving_mode);
296 #endif
297         else
298                 BT_ERR("");
299 }
300
301 void _bt_core_handle_flight_mode_noti(void)
302 {
303         int ret;
304
305         if (TIZEN_FEATURE_FLIGHTMODE_ENABLED) {
306                 BT_DBG("+");
307
308                 ret = vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
309                                 (vconf_callback_fn)__bt_core_flight_mode_cb, NULL);
310                 if (ret < 0)
311                         BT_ERR("Unable to register key handler");
312         } else {
313                 BT_DBG("Telephony is disabled");
314         }
315 }
316
317 void _bt_core_handle_power_saving_mode_noti(void)
318 {
319         if (TIZEN_PROFILE_WEARABLE)
320                 return;
321
322         BT_DBG("+");
323 #ifdef ENABLE_TIZEN_2_4
324         int ret = 0;
325
326         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
327                         (vconf_callback_fn)__bt_core_power_saving_mode_cb, NULL);
328         if (ret < 0)
329                 BT_ERR("Unable to register key handler");
330 #endif
331 }
332
333 void _bt_core_unregister_vconf_handler(void)
334 {
335         if (TIZEN_FEATURE_FLIGHTMODE_ENABLED) {
336                 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
337                                 (vconf_callback_fn)__bt_core_flight_mode_cb);
338         }
339
340 #ifdef ENABLE_TIZEN_2_4
341         if (!TIZEN_PROFILE_WEARABLE) {
342                 vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
343                                 (vconf_callback_fn)__bt_core_power_saving_mode_cb);
344         }
345 #endif
346
347         return;
348 }
349