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 if (!TIZEN_FEATURE_BT_USB_DONGLE)
91 _bt_disable_adapter();
94 if (adapter_status_le == BT_LE_ACTIVATED) {
95 int bt_le_status_before_mode = 0;
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);
100 #ifdef TIZEN_FEATURE_BUSACT
101 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
103 if (!TIZEN_FEATURE_BT_USB_DONGLE)
104 _bt_disable_adapter_le();
107 int flight_mode_value = 0;
109 BT_INFO_C("Flight mode off. Turn on BT");
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);
116 if (adapter_status != BT_DEACTIVATED && adapter_status_le != BT_LE_DEACTIVATED) {
117 BT_INFO("No need to control bt status");
121 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value))
122 BT_ERR("Fail get flight mode value");
124 if (flight_mode_value == 0)
127 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
128 BT_ERR("Set vconf failed");
130 ret_if(__bt_off_cause_conflict_check());
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();
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();
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 #ifdef TIZEN_FEATURE_BUSACT
169 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
171 if (!TIZEN_FEATURE_BT_USB_DONGLE)
172 _bt_disable_adapter();
174 if (adapter_status_le == BT_LE_ACTIVATED) {
175 int bt_le_status_before_mode = 0;
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);
180 #ifdef TIZEN_FEATURE_BUSACT
181 /* Disable the BT LE */
182 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
184 if (!TIZEN_FEATURE_BT_USB_DONGLE)
185 _bt_disable_adapter_le();
188 int ps_mode_value = 0;
190 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_value))
191 BT_ERR("Fail get power saving mode value");
193 if (ps_mode_value == 0)
196 BT_DBG("Activate Bluetooth");
197 if (vconf_set_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, 0))
198 BT_ERR("Set vconf failed");
200 ret_if(__bt_off_cause_conflict_check());
202 BT_DBG("BT status before Emergency mode() :%d",
203 _bt_core_get_bt_status(BT_POWER_SAVING_MODE));
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();
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();
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);
240 #ifdef ENABLE_TIZEN_2_4
241 static void __bt_core_power_saving_mode_cb(keynode_t *node, void *data)
243 int power_saving_mode = 0;
245 DBG_SECURE("key=%s", vconf_keynode_get_name(node));
247 if (vconf_keynode_get_type(node) != VCONF_TYPE_INT) {
248 BT_ERR("Wrong vconf type");
252 power_saving_mode = vconf_keynode_get_int(node);
254 BT_DBG("value=%d", power_saving_mode);
256 __bt_core_handle_adapter_with_power_saving_mode(power_saving_mode);
260 void _bt_core_init_vconf_value(void)
262 gboolean flight_mode = FALSE;
263 #ifndef ENABLE_TIZEN_2_4
264 int power_saving_mode = 0;
266 int bt_flight_mode = 0;
269 _bt_core_handle_flight_mode_noti();
270 _bt_core_handle_power_saving_mode_noti();
272 flight_mode = _bt_core_is_flight_mode_enabled();
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);
281 BT_DBG("flight_mode = %d, power_saving_mode = %d", flight_mode, power_saving_mode);
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);
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);
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);
301 void _bt_core_handle_flight_mode_noti(void)
305 if (TIZEN_FEATURE_FLIGHTMODE_ENABLED) {
308 ret = vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
309 (vconf_callback_fn)__bt_core_flight_mode_cb, NULL);
311 BT_ERR("Unable to register key handler");
313 BT_DBG("Telephony is disabled");
317 void _bt_core_handle_power_saving_mode_noti(void)
319 if (TIZEN_PROFILE_WEARABLE)
323 #ifdef ENABLE_TIZEN_2_4
326 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
327 (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 if (TIZEN_FEATURE_FLIGHTMODE_ENABLED) {
336 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
337 (vconf_callback_fn)__bt_core_flight_mode_cb);
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);