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 #ifdef ENABLE_TIZEN_2_4
101 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
102 _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 _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 _bt_enable_adapter();
141 if (adapter_status_le == BT_LE_DEACTIVATED &&
142 _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
143 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
144 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
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 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
169 _bt_disable_adapter();
171 if (adapter_status_le == BT_LE_ACTIVATED) {
172 int bt_le_status_before_mode = 0;
173 #ifdef ENABLE_TIZEN_2_4
174 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
175 _bt_core_set_bt_le_status(BT_POWER_SAVING_MODE, bt_le_status_before_mode);
178 /* Disable the BT LE */
179 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
180 _bt_disable_adapter_le();
183 int ps_mode_value = 0;
185 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_value))
186 BT_ERR("Fail get power saving mode value");
188 if (ps_mode_value == 0)
191 BT_DBG("Activate Bluetooth");
192 if (vconf_set_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, 0))
193 BT_ERR("Set vconf failed");
195 ret_if(__bt_off_cause_conflict_check());
197 BT_DBG("BT status before Emergency mode() :%d",
198 _bt_core_get_bt_status(BT_POWER_SAVING_MODE));
200 if (adapter_status == BT_DEACTIVATED && (_bt_core_get_bt_status(BT_POWER_SAVING_MODE) != 0)) {
201 _bt_core_set_bt_status(BT_POWER_SAVING_MODE, 0);
202 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
203 _bt_enable_adapter();
205 BT_DBG("BT LE status before Emergency mode() :%d", _bt_core_get_bt_le_status(BT_POWER_SAVING_MODE));
206 if (adapter_status_le == BT_LE_DEACTIVATED && _bt_core_get_bt_le_status(BT_POWER_SAVING_MODE) != 0) {
207 _bt_core_set_bt_le_status(BT_POWER_SAVING_MODE, 0);
208 /* Enable the BT LE */
209 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
210 _bt_enable_adapter_le();
214 #ifdef TIZEN_BT_FLIGHTMODE_ENABLED
215 static void __bt_core_flight_mode_cb(keynode_t *node, void *data)
217 gboolean flight_mode = FALSE;
220 BT_DBG("key = %s", vconf_keynode_get_name(node));
222 type = vconf_keynode_get_type(node);
223 if (type != VCONF_TYPE_BOOL) {
224 BT_ERR("Invaild vconf key type : %d", type);
228 flight_mode = vconf_keynode_get_bool(node);
230 __bt_core_handle_adapter_with_flight_mode(flight_mode);
234 #ifndef TIZEN_WEARABLE
235 static void __bt_core_power_saving_mode_cb(keynode_t *node, void *data)
237 int power_saving_mode = 0;
239 DBG_SECURE("key=%s", vconf_keynode_get_name(node));
241 if (vconf_keynode_get_type(node) != VCONF_TYPE_INT) {
242 BT_ERR("Wrong vconf type");
246 power_saving_mode = vconf_keynode_get_int(node);
248 BT_DBG("value=%d", power_saving_mode);
250 __bt_core_handle_adapter_with_power_saving_mode(power_saving_mode);
254 void _bt_core_init_vconf_value(void)
256 gboolean flight_mode = FALSE;
257 int power_saving_mode = 0;
259 _bt_core_handle_flight_mode_noti();
260 _bt_core_handle_power_saving_mode_noti();
262 flight_mode = _bt_core_is_flight_mode_enabled();
264 #ifndef TIZEN_WEARABLE
265 #ifdef ENABLE_TIZEN_2_4
266 if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &power_saving_mode) != 0)
267 BT_ERR("Fail to get the power_saving_mode status value");
270 BT_DBG("flight_mode = %d, power_saving_mode = %d", flight_mode, power_saving_mode);
272 if (flight_mode == TRUE)
273 __bt_core_handle_adapter_with_flight_mode(flight_mode);
274 else if (power_saving_mode > 0)
275 __bt_core_handle_adapter_with_power_saving_mode(power_saving_mode);
280 void _bt_core_handle_flight_mode_noti(void)
282 #ifdef TIZEN_BT_FLIGHTMODE_ENABLED
287 ret = vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
288 (vconf_callback_fn)__bt_core_flight_mode_cb, NULL);
290 BT_ERR("Unable to register key handler");
292 BT_DBG("Telephony is disabled");
296 void _bt_core_handle_power_saving_mode_noti(void)
298 #ifndef TIZEN_WEARABLE
302 #ifdef ENABLE_TIZEN_2_4
303 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
304 (vconf_callback_fn)__bt_core_power_saving_mode_cb, NULL);
307 BT_ERR("Unable to register key handler");
311 void _bt_core_unregister_vconf_handler(void)
313 #ifdef TIZEN_BT_FLIGHTMODE_ENABLED
314 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
315 (vconf_callback_fn)__bt_core_flight_mode_cb);
318 #ifndef TIZEN_WEARABLE
319 #ifdef ENABLE_TIZEN_2_4
320 vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
321 (vconf_callback_fn)__bt_core_power_saving_mode_cb);