4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 * Girishashok Joshi <girish.joshi@samsung.com>
8 * Chanyeol Park <chanyeol.park@samsung.com>
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <vconf-keys.h>
27 #include "bt-core-adapter.h"
28 #include "bt-core-common.h"
29 #include "bt-core-noti-handler.h"
31 static gboolean flightmode_request = FALSE;
33 void _bt_set_flightmode_request(gboolean value)
35 flightmode_request = value;
38 gboolean _bt_is_flightmode_request(void)
40 return flightmode_request;
43 static gboolean __bt_off_cause_conflict_check(void)
45 int flight_mode_value = 0;
46 int ps_mode_value = 0;
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);
54 if (flight_mode_value == 1 || ps_mode_value > 0) {
55 BT_DBG("Bt should not turn on");
62 static void __bt_core_handle_adapter_with_flight_mode(gboolean flight_mode)
64 bt_status_t adapter_status;
65 bt_le_status_t adapter_status_le;
67 adapter_status = _bt_core_get_status();
68 adapter_status_le = _bt_core_get_le_status();
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");
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);
79 if (adapter_status != BT_ACTIVATED && adapter_status_le != BT_LE_ACTIVATED) {
80 BT_INFO("No need to control bt status");
84 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
85 BT_ERR("Set vconf failed");
87 if (adapter_status == BT_ACTIVATED) {
88 int bt_status_before_mode = 0;
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);
93 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
95 _bt_disable_adapter();
99 if (adapter_status_le == BT_LE_ACTIVATED) {
100 int bt_le_status_before_mode = 0;
102 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
103 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
105 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
106 #ifndef USB_BLUETOOTH
107 _bt_disable_adapter_le();
111 int flight_mode_value = 0;
113 BT_INFO_C("Flight mode off. Turn on BT");
115 if (adapter_status == BT_DEACTIVATING || adapter_status_le == BT_LE_DEACTIVATING) {
116 BT_INFO("BT adapter is activating. Turn off BT after activation");
117 _bt_set_flightmode_request(TRUE);
120 if (adapter_status != BT_DEACTIVATED && adapter_status_le != BT_LE_DEACTIVATED) {
121 BT_INFO("No need to control bt status");
125 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value))
126 BT_ERR("Fail get flight mode value");
128 if (flight_mode_value == 0)
131 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
132 BT_ERR("Set vconf failed");
134 ret_if(__bt_off_cause_conflict_check());
136 if (adapter_status == BT_DEACTIVATED &&
137 _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
138 _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
139 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
140 #ifndef USB_BLUETOOTH
141 _bt_enable_adapter();
145 if (adapter_status_le == BT_LE_DEACTIVATED &&
146 _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
147 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
148 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
149 #ifndef USB_BLUETOOTH
150 _bt_enable_adapter_le();
156 static void __bt_core_handle_adapter_with_power_saving_mode(int power_saving_mode)
158 bt_status_t adapter_status;
159 bt_le_status_t adapter_status_le;
161 adapter_status = _bt_core_get_status();
162 adapter_status_le = _bt_core_get_le_status();
164 if (power_saving_mode == 2) {
165 BT_DBG("Deactivate Bluetooth Service");
166 if (vconf_set_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, 1) != 0)
167 BT_ERR("Set vconf failed");
169 if (adapter_status == BT_ACTIVATED) {
170 int bt_status_before_mode = 0;
171 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
172 _bt_core_set_bt_status(BT_POWER_SAVING_MODE, bt_status_before_mode);
174 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
175 #ifndef USB_BLUETOOTH
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 /* Disable the BT LE */
186 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
187 #ifndef USB_BLUETOOTH
188 _bt_disable_adapter_le();
192 int ps_mode_value = 0;
194 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_value))
195 BT_ERR("Fail get power saving mode value");
197 if (ps_mode_value == 0)
200 BT_DBG("Activate Bluetooth");
201 if (vconf_set_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, 0))
202 BT_ERR("Set vconf failed");
204 ret_if(__bt_off_cause_conflict_check());
206 BT_DBG("BT status before Emergency mode() :%d",
207 _bt_core_get_bt_status(BT_POWER_SAVING_MODE));
209 if (adapter_status == BT_DEACTIVATED && (_bt_core_get_bt_status(BT_POWER_SAVING_MODE) != 0)) {
210 _bt_core_set_bt_status(BT_POWER_SAVING_MODE, 0);
211 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
212 #ifndef USB_BLUETOOTH
213 _bt_enable_adapter();
216 BT_DBG("BT LE status before Emergency mode() :%d", _bt_core_get_bt_le_status(BT_POWER_SAVING_MODE));
217 if (adapter_status_le == BT_LE_DEACTIVATED && _bt_core_get_bt_le_status(BT_POWER_SAVING_MODE) != 0) {
218 _bt_core_set_bt_le_status(BT_POWER_SAVING_MODE, 0);
219 /* Enable the BT LE */
220 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
221 #ifndef USB_BLUETOOTH
222 _bt_enable_adapter_le();
227 #ifdef TIZEN_BT_FLIGHTMODE_ENABLED
228 static void __bt_core_flight_mode_cb(keynode_t *node, void *data)
230 gboolean flight_mode = FALSE;
233 BT_DBG("key = %s", vconf_keynode_get_name(node));
235 type = vconf_keynode_get_type(node);
236 if (type != VCONF_TYPE_BOOL) {
237 BT_ERR("Invaild vconf key type : %d", type);
241 flight_mode = vconf_keynode_get_bool(node);
243 __bt_core_handle_adapter_with_flight_mode(flight_mode);
247 #ifndef TIZEN_WEARABLE
248 static void __bt_core_power_saving_mode_cb(keynode_t *node, void *data)
250 int power_saving_mode = 0;
252 DBG_SECURE("key=%s", vconf_keynode_get_name(node));
254 if (vconf_keynode_get_type(node) != VCONF_TYPE_INT) {
255 BT_ERR("Wrong vconf type");
259 power_saving_mode = vconf_keynode_get_int(node);
261 BT_DBG("value=%d", power_saving_mode);
263 __bt_core_handle_adapter_with_power_saving_mode(power_saving_mode);
267 void _bt_core_init_vconf_value(void)
269 gboolean flight_mode = FALSE;
270 int power_saving_mode = 0;
272 _bt_core_handle_flight_mode_noti();
273 _bt_core_handle_power_saving_mode_noti();
275 flight_mode = _bt_core_is_flight_mode_enabled();
277 #ifndef TIZEN_WEARABLE
278 #ifdef ENABLE_TIZEN_2_4
279 if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &power_saving_mode) != 0)
280 BT_ERR("Fail to get the power_saving_mode status value");
283 BT_DBG("flight_mode = %d, power_saving_mode = %d", flight_mode, power_saving_mode);
285 if (flight_mode == TRUE)
286 __bt_core_handle_adapter_with_flight_mode(flight_mode);
287 else if (power_saving_mode > 0)
288 __bt_core_handle_adapter_with_power_saving_mode(power_saving_mode);
293 void _bt_core_handle_flight_mode_noti(void)
295 #ifdef TIZEN_BT_FLIGHTMODE_ENABLED
300 ret = vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
301 (vconf_callback_fn)__bt_core_flight_mode_cb, NULL);
303 BT_ERR("Unable to register key handler");
305 BT_DBG("Telephony is disabled");
309 void _bt_core_handle_power_saving_mode_noti(void)
311 #ifndef TIZEN_WEARABLE
315 #ifdef ENABLE_TIZEN_2_4
316 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
317 (vconf_callback_fn)__bt_core_power_saving_mode_cb, NULL);
320 BT_ERR("Unable to register key handler");
324 void _bt_core_unregister_vconf_handler(void)
326 #ifdef TIZEN_BT_FLIGHTMODE_ENABLED
327 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
328 (vconf_callback_fn)__bt_core_flight_mode_cb);
331 #ifndef TIZEN_WEARABLE
332 #ifdef ENABLE_TIZEN_2_4
333 vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
334 (vconf_callback_fn)__bt_core_power_saving_mode_cb);