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);
94 _bt_disable_adapter();
97 if (adapter_status_le == BT_LE_ACTIVATED) {
98 int bt_le_status_before_mode = 0;
100 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
101 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
103 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
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 _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 _bt_enable_adapter_le();
148 static void __bt_core_handle_adapter_with_power_saving_mode(int power_saving_mode)
150 bt_status_t adapter_status;
151 bt_le_status_t adapter_status_le;
153 adapter_status = _bt_core_get_status();
154 adapter_status_le = _bt_core_get_le_status();
156 if (power_saving_mode == 2) {
157 BT_DBG("Deactivate Bluetooth Service");
158 if (vconf_set_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, 1) != 0)
159 BT_ERR("Set vconf failed");
161 if (adapter_status == BT_ACTIVATED) {
162 int bt_status_before_mode = 0;
163 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
164 _bt_core_set_bt_status(BT_POWER_SAVING_MODE, bt_status_before_mode);
166 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
167 _bt_disable_adapter();
169 if (adapter_status_le == BT_LE_ACTIVATED) {
170 int bt_le_status_before_mode = 0;
171 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
172 _bt_core_set_bt_le_status(BT_POWER_SAVING_MODE, bt_le_status_before_mode);
174 /* Disable the BT LE */
175 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
176 _bt_disable_adapter_le();
179 int ps_mode_value = 0;
181 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_value))
182 BT_ERR("Fail get power saving mode value");
184 if (ps_mode_value == 0)
187 BT_DBG("Activate Bluetooth");
188 if (vconf_set_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, 0))
189 BT_ERR("Set vconf failed");
191 ret_if(__bt_off_cause_conflict_check());
193 BT_DBG("BT status before Emergency mode() :%d",
194 _bt_core_get_bt_status(BT_POWER_SAVING_MODE));
196 if (adapter_status == BT_DEACTIVATED && (_bt_core_get_bt_status(BT_POWER_SAVING_MODE) != 0)) {
197 _bt_core_set_bt_status(BT_POWER_SAVING_MODE, 0);
198 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
199 _bt_enable_adapter();
201 BT_DBG("BT LE status before Emergency mode() :%d", _bt_core_get_bt_le_status(BT_POWER_SAVING_MODE));
202 if (adapter_status_le == BT_LE_DEACTIVATED && _bt_core_get_bt_le_status(BT_POWER_SAVING_MODE) != 0) {
203 _bt_core_set_bt_le_status(BT_POWER_SAVING_MODE, 0);
204 /* Enable the BT LE */
205 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
206 _bt_enable_adapter_le();
210 #ifdef TIZEN_BT_FLIGHTMODE_ENABLED
211 static void __bt_core_flight_mode_cb(keynode_t *node, void *data)
213 gboolean flight_mode = FALSE;
216 BT_DBG("key = %s", vconf_keynode_get_name(node));
218 type = vconf_keynode_get_type(node);
219 if (type != VCONF_TYPE_BOOL) {
220 BT_ERR("Invaild vconf key type : %d", type);
224 flight_mode = vconf_keynode_get_bool(node);
226 __bt_core_handle_adapter_with_flight_mode(flight_mode);
230 #ifndef TIZEN_WEARABLE
231 static void __bt_core_power_saving_mode_cb(keynode_t *node, void *data)
233 int power_saving_mode = 0;
235 DBG_SECURE("key=%s", vconf_keynode_get_name(node));
237 if (vconf_keynode_get_type(node) != VCONF_TYPE_INT) {
238 BT_ERR("Wrong vconf type");
242 power_saving_mode = vconf_keynode_get_int(node);
244 BT_DBG("value=%d", power_saving_mode);
246 __bt_core_handle_adapter_with_power_saving_mode(power_saving_mode);
250 void _bt_core_init_vconf_value(void)
252 gboolean flight_mode = FALSE;
253 int power_saving_mode = 0;
255 _bt_core_handle_flight_mode_noti();
256 _bt_core_handle_power_saving_mode_noti();
258 flight_mode = _bt_core_is_flight_mode_enabled();
260 #ifndef TIZEN_WEARABLE
261 if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &power_saving_mode) != 0)
262 BT_ERR("Fail to get the power_saving_mode status value");
264 BT_DBG("flight_mode = %d, power_saving_mode = %d", flight_mode, power_saving_mode);
266 if (flight_mode == TRUE)
267 __bt_core_handle_adapter_with_flight_mode(flight_mode);
268 else if (power_saving_mode > 0)
269 __bt_core_handle_adapter_with_power_saving_mode(power_saving_mode);
274 void _bt_core_handle_flight_mode_noti(void)
276 #ifdef TIZEN_BT_FLIGHTMODE_ENABLED
281 ret = vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
282 (vconf_callback_fn)__bt_core_flight_mode_cb, NULL);
284 BT_ERR("Unable to register key handler");
286 BT_DBG("Telephony is disabled");
290 void _bt_core_handle_power_saving_mode_noti(void)
292 #ifndef TIZEN_WEARABLE
296 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
297 (vconf_callback_fn)__bt_core_power_saving_mode_cb, NULL);
300 BT_ERR("Unable to register key handler");
304 void _bt_core_unregister_vconf_handler(void)
306 #ifdef TIZEN_BT_FLIGHTMODE_ENABLED
307 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
308 (vconf_callback_fn)__bt_core_flight_mode_cb);
311 #ifndef TIZEN_WEARABLE
312 vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
313 (vconf_callback_fn)__bt_core_power_saving_mode_cb);