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 #ifdef TIZEN_FEATURE_BUSACT
88 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
90 #ifndef TIZEN_FEATURE_BT_USB_DONGLE
91 _bt_disable_adapter();
95 if (adapter_status_le == BT_LE_ACTIVATED) {
96 int bt_le_status_before_mode = 0;
98 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
99 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
101 #ifdef TIZEN_FEATURE_BUSACT
102 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
104 #ifndef TIZEN_FEATURE_BT_USB_DONGLE
105 _bt_disable_adapter_le();
109 int flight_mode_value = 0;
111 BT_INFO_C("Flight mode off. Turn on BT");
113 if (adapter_status == BT_DEACTIVATING || adapter_status_le == BT_LE_DEACTIVATING) {
114 BT_INFO("BT adapter is activating. Turn off BT after activation");
115 _bt_set_flightmode_request(TRUE);
118 if (adapter_status != BT_DEACTIVATED && adapter_status_le != BT_LE_DEACTIVATED) {
119 BT_INFO("No need to control bt status");
123 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value))
124 BT_ERR("Fail get flight mode value");
126 if (flight_mode_value == 0)
129 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
130 BT_ERR("Set vconf failed");
132 ret_if(__bt_off_cause_conflict_check());
134 if (adapter_status == BT_DEACTIVATED &&
135 _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
136 _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
137 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
138 #ifndef TIZEN_FEATURE_BT_USB_DONGLE
139 _bt_enable_adapter();
143 if (adapter_status_le == BT_LE_DEACTIVATED &&
144 _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
145 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
146 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
147 #ifndef TIZEN_FEATURE_BT_USB_DONGLE
148 _bt_enable_adapter_le();
154 static void __bt_core_handle_adapter_with_power_saving_mode(int power_saving_mode)
156 bt_status_t adapter_status;
157 bt_le_status_t adapter_status_le;
159 adapter_status = _bt_core_get_status();
160 adapter_status_le = _bt_core_get_le_status();
162 if (power_saving_mode == 2) {
163 BT_DBG("Deactivate Bluetooth Service");
164 if (vconf_set_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, 1) != 0)
165 BT_ERR("Set vconf failed");
167 if (adapter_status == BT_ACTIVATED) {
168 int bt_status_before_mode = 0;
169 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
170 _bt_core_set_bt_status(BT_POWER_SAVING_MODE, bt_status_before_mode);
172 #ifdef TIZEN_FEATURE_BUSACT
173 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
175 #ifndef TIZEN_FEATURE_BT_USB_DONGLE
176 _bt_disable_adapter();
179 if (adapter_status_le == BT_LE_ACTIVATED) {
180 int bt_le_status_before_mode = 0;
182 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
183 _bt_core_set_bt_le_status(BT_POWER_SAVING_MODE, bt_le_status_before_mode);
185 #ifdef TIZEN_FEATURE_BUSACT
186 /* Disable the BT LE */
187 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
189 #ifndef TIZEN_FEATURE_BT_USB_DONGLE
190 _bt_disable_adapter_le();
194 int ps_mode_value = 0;
196 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_value))
197 BT_ERR("Fail get power saving mode value");
199 if (ps_mode_value == 0)
202 BT_DBG("Activate Bluetooth");
203 if (vconf_set_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, 0))
204 BT_ERR("Set vconf failed");
206 ret_if(__bt_off_cause_conflict_check());
208 BT_DBG("BT status before Emergency mode() :%d",
209 _bt_core_get_bt_status(BT_POWER_SAVING_MODE));
211 if (adapter_status == BT_DEACTIVATED && (_bt_core_get_bt_status(BT_POWER_SAVING_MODE) != 0)) {
212 _bt_core_set_bt_status(BT_POWER_SAVING_MODE, 0);
213 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
214 #ifndef TIZEN_FEATURE_BT_USB_DONGLE
215 _bt_enable_adapter();
218 BT_DBG("BT LE status before Emergency mode() :%d", _bt_core_get_bt_le_status(BT_POWER_SAVING_MODE));
219 if (adapter_status_le == BT_LE_DEACTIVATED && _bt_core_get_bt_le_status(BT_POWER_SAVING_MODE) != 0) {
220 _bt_core_set_bt_le_status(BT_POWER_SAVING_MODE, 0);
221 /* Enable the BT LE */
222 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
223 #ifndef TIZEN_FEATURE_BT_USB_DONGLE
224 _bt_enable_adapter_le();
229 #ifdef TIZEN_FEATURE_FLIGHTMODE_ENABLED
230 static void __bt_core_flight_mode_cb(keynode_t *node, void *data)
232 gboolean flight_mode = FALSE;
235 BT_DBG("key = %s", vconf_keynode_get_name(node));
237 type = vconf_keynode_get_type(node);
238 if (type != VCONF_TYPE_BOOL) {
239 BT_ERR("Invaild vconf key type : %d", type);
243 flight_mode = vconf_keynode_get_bool(node);
245 __bt_core_handle_adapter_with_flight_mode(flight_mode);
249 #ifndef TIZEN_PROFILE_WEARABLE
250 #ifdef ENABLE_TIZEN_2_4
251 static void __bt_core_power_saving_mode_cb(keynode_t *node, void *data)
253 int power_saving_mode = 0;
255 DBG_SECURE("key=%s", vconf_keynode_get_name(node));
257 if (vconf_keynode_get_type(node) != VCONF_TYPE_INT) {
258 BT_ERR("Wrong vconf type");
262 power_saving_mode = vconf_keynode_get_int(node);
264 BT_DBG("value=%d", power_saving_mode);
266 __bt_core_handle_adapter_with_power_saving_mode(power_saving_mode);
271 void _bt_core_init_vconf_value(void)
273 gboolean flight_mode = FALSE;
274 #ifndef ENABLE_TIZEN_2_4
275 int power_saving_mode = 0;
277 int bt_flight_mode = 0;
280 _bt_core_handle_flight_mode_noti();
281 _bt_core_handle_power_saving_mode_noti();
283 flight_mode = _bt_core_is_flight_mode_enabled();
285 #ifndef TIZEN_PROFILE_WEARABLE
286 #ifndef ENABLE_TIZEN_2_4
287 if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &power_saving_mode) != 0)
288 BT_ERR("Fail to get the power_saving_mode status value");
289 BT_DBG("flight_mode = %d, power_saving_mode = %d", flight_mode, power_saving_mode);
292 BT_DBG("flight_mode = %d, power_saving_mode = %d", flight_mode, power_saving_mode);
294 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &bt_flight_mode))
295 BT_ERR("Fail get flight mode value");
296 _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_flight_mode);
298 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &bt_ps_mode))
299 BT_ERR("Fail get power saving mode value");
300 _bt_core_set_bt_status(BT_POWER_SAVING_MODE, bt_ps_mode);
302 if (flight_mode == TRUE)
303 __bt_core_handle_adapter_with_flight_mode(flight_mode);
304 #ifndef ENABLE_TIZEN_2_4
305 else if (power_saving_mode > 0)
306 __bt_core_handle_adapter_with_power_saving_mode(power_saving_mode);
312 void _bt_core_handle_flight_mode_noti(void)
314 #ifdef TIZEN_FEATURE_FLIGHTMODE_ENABLED
319 ret = vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
320 (vconf_callback_fn)__bt_core_flight_mode_cb, NULL);
322 BT_ERR("Unable to register key handler");
324 BT_DBG("Telephony is disabled");
328 void _bt_core_handle_power_saving_mode_noti(void)
330 #ifndef TIZEN_PROFILE_WEARABLE
334 #ifdef ENABLE_TIZEN_2_4
335 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
336 (vconf_callback_fn)__bt_core_power_saving_mode_cb, NULL);
339 BT_ERR("Unable to register key handler");
343 void _bt_core_unregister_vconf_handler(void)
345 #ifdef TIZEN_FEATURE_FLIGHTMODE_ENABLED
346 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
347 (vconf_callback_fn)__bt_core_flight_mode_cb);
350 #ifndef TIZEN_PROFILE_WEARABLE
351 #ifdef ENABLE_TIZEN_2_4
352 vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
353 (vconf_callback_fn)__bt_core_power_saving_mode_cb);