2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <vconf-keys.h>
21 #include "bt-core-adapter.h"
22 #include "bt-core-common.h"
23 #include "bt-core-noti-handler.h"
25 static gboolean flightmode_request = FALSE;
27 void _bt_set_flightmode_request(gboolean value)
29 flightmode_request = value;
32 gboolean _bt_is_flightmode_request(void)
34 return flightmode_request;
37 static gboolean __bt_off_cause_conflict_check(void)
39 int flight_mode_value = 0;
40 int ps_mode_value = 0;
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);
48 if (flight_mode_value == 1 || ps_mode_value > 0) {
49 BT_DBG("Bt should not turn on");
56 static void __bt_core_handle_adapter_with_flight_mode(gboolean flight_mode)
58 bt_status_t adapter_status;
59 bt_le_status_t adapter_status_le;
61 adapter_status = _bt_core_get_status();
62 adapter_status_le = _bt_core_get_le_status();
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");
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);
73 if (adapter_status != BT_ACTIVATED && adapter_status_le != BT_LE_ACTIVATED) {
74 BT_INFO("No need to control bt status");
78 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
79 BT_ERR("Set vconf failed");
81 if (adapter_status == BT_ACTIVATED) {
82 int bt_status_before_mode = 0;
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);
87 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
89 _bt_disable_adapter();
93 if (adapter_status_le == BT_LE_ACTIVATED) {
94 int bt_le_status_before_mode = 0;
96 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
97 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
99 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
100 #ifndef USB_BLUETOOTH
101 _bt_disable_adapter_le();
105 int flight_mode_value = 0;
107 BT_INFO_C("Flight mode off. Turn on BT");
109 if (adapter_status == BT_DEACTIVATING || adapter_status_le == BT_LE_DEACTIVATING) {
110 BT_INFO("BT adapter is activating. Turn off BT after activation");
111 _bt_set_flightmode_request(TRUE);
114 if (adapter_status != BT_DEACTIVATED && adapter_status_le != BT_LE_DEACTIVATED) {
115 BT_INFO("No need to control bt status");
119 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value))
120 BT_ERR("Fail get flight mode value");
122 if (flight_mode_value == 0)
125 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
126 BT_ERR("Set vconf failed");
128 ret_if(__bt_off_cause_conflict_check());
130 if (adapter_status == BT_DEACTIVATED &&
131 _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
132 _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
133 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
134 #ifndef USB_BLUETOOTH
135 _bt_enable_adapter();
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 #ifndef USB_BLUETOOTH
144 _bt_enable_adapter_le();
150 static void __bt_core_handle_adapter_with_power_saving_mode(int power_saving_mode)
152 bt_status_t adapter_status;
153 bt_le_status_t adapter_status_le;
155 adapter_status = _bt_core_get_status();
156 adapter_status_le = _bt_core_get_le_status();
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");
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);
168 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
169 #ifndef USB_BLUETOOTH
170 _bt_disable_adapter();
173 if (adapter_status_le == BT_LE_ACTIVATED) {
174 int bt_le_status_before_mode = 0;
176 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
177 _bt_core_set_bt_le_status(BT_POWER_SAVING_MODE, bt_le_status_before_mode);
179 /* Disable the BT LE */
180 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
181 #ifndef USB_BLUETOOTH
182 _bt_disable_adapter_le();
186 int ps_mode_value = 0;
188 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_value))
189 BT_ERR("Fail get power saving mode value");
191 if (ps_mode_value == 0)
194 BT_DBG("Activate Bluetooth");
195 if (vconf_set_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, 0))
196 BT_ERR("Set vconf failed");
198 ret_if(__bt_off_cause_conflict_check());
200 BT_DBG("BT status before Emergency mode() :%d",
201 _bt_core_get_bt_status(BT_POWER_SAVING_MODE));
203 if (adapter_status == BT_DEACTIVATED && (_bt_core_get_bt_status(BT_POWER_SAVING_MODE) != 0)) {
204 _bt_core_set_bt_status(BT_POWER_SAVING_MODE, 0);
205 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
206 #ifndef USB_BLUETOOTH
207 _bt_enable_adapter();
210 BT_DBG("BT LE status before Emergency mode() :%d", _bt_core_get_bt_le_status(BT_POWER_SAVING_MODE));
211 if (adapter_status_le == BT_LE_DEACTIVATED && _bt_core_get_bt_le_status(BT_POWER_SAVING_MODE) != 0) {
212 _bt_core_set_bt_le_status(BT_POWER_SAVING_MODE, 0);
213 /* Enable the BT LE */
214 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
215 #ifndef USB_BLUETOOTH
216 _bt_enable_adapter_le();
221 #ifdef TIZEN_BT_FLIGHTMODE_ENABLED
222 static void __bt_core_flight_mode_cb(keynode_t *node, void *data)
224 gboolean flight_mode = FALSE;
227 BT_DBG("key = %s", vconf_keynode_get_name(node));
229 type = vconf_keynode_get_type(node);
230 if (type != VCONF_TYPE_BOOL) {
231 BT_ERR("Invaild vconf key type : %d", type);
235 flight_mode = vconf_keynode_get_bool(node);
237 __bt_core_handle_adapter_with_flight_mode(flight_mode);
241 #ifndef TIZEN_WEARABLE
242 static void __bt_core_power_saving_mode_cb(keynode_t *node, void *data)
244 int power_saving_mode = 0;
246 DBG_SECURE("key=%s", vconf_keynode_get_name(node));
248 if (vconf_keynode_get_type(node) != VCONF_TYPE_INT) {
249 BT_ERR("Wrong vconf type");
253 power_saving_mode = vconf_keynode_get_int(node);
255 BT_DBG("value=%d", power_saving_mode);
257 __bt_core_handle_adapter_with_power_saving_mode(power_saving_mode);
261 void _bt_core_init_vconf_value(void)
263 gboolean flight_mode = FALSE;
264 #ifndef ENABLE_TIZEN_2_4
265 int power_saving_mode = 0;
267 int bt_flight_mode = 0;
270 _bt_core_handle_flight_mode_noti();
271 _bt_core_handle_power_saving_mode_noti();
273 flight_mode = _bt_core_is_flight_mode_enabled();
275 #ifndef TIZEN_WEARABLE
276 #ifndef ENABLE_TIZEN_2_4
277 if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &power_saving_mode) != 0)
278 BT_ERR("Fail to get the power_saving_mode status value");
279 BT_DBG("flight_mode = %d, power_saving_mode = %d", flight_mode, power_saving_mode);
282 BT_DBG("flight_mode = %d, power_saving_mode = %d", flight_mode, power_saving_mode);
284 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &bt_flight_mode))
285 BT_ERR("Fail get flight mode value");
286 _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_flight_mode);
288 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &bt_ps_mode))
289 BT_ERR("Fail get power saving mode value");
290 _bt_core_set_bt_status(BT_POWER_SAVING_MODE, bt_ps_mode);
292 if (flight_mode == TRUE)
293 __bt_core_handle_adapter_with_flight_mode(flight_mode);
294 #ifndef ENABLE_TIZEN_2_4
295 else if (power_saving_mode > 0)
296 __bt_core_handle_adapter_with_power_saving_mode(power_saving_mode);
302 void _bt_core_handle_flight_mode_noti(void)
304 #ifdef TIZEN_BT_FLIGHTMODE_ENABLED
309 ret = vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
310 (vconf_callback_fn)__bt_core_flight_mode_cb, NULL);
312 BT_ERR("Unable to register key handler");
314 BT_DBG("Telephony is disabled");
318 void _bt_core_handle_power_saving_mode_noti(void)
320 #ifndef TIZEN_WEARABLE
324 #ifdef ENABLE_TIZEN_2_4
325 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
326 (vconf_callback_fn)__bt_core_power_saving_mode_cb, NULL);
329 BT_ERR("Unable to register key handler");
333 void _bt_core_unregister_vconf_handler(void)
335 #ifdef TIZEN_BT_FLIGHTMODE_ENABLED
336 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
337 (vconf_callback_fn)__bt_core_flight_mode_cb);
340 #ifndef TIZEN_WEARABLE
341 #ifdef ENABLE_TIZEN_2_4
342 vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
343 (vconf_callback_fn)__bt_core_power_saving_mode_cb);