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);
177 /* Disable the BT LE */
178 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
179 _bt_disable_adapter_le();
182 int ps_mode_value = 0;
184 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_value))
185 BT_ERR("Fail get power saving mode value");
187 if (ps_mode_value == 0)
190 BT_DBG("Activate Bluetooth");
191 if (vconf_set_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, 0))
192 BT_ERR("Set vconf failed");
194 ret_if(__bt_off_cause_conflict_check());
196 BT_DBG("BT status before Emergency mode() :%d",
197 _bt_core_get_bt_status(BT_POWER_SAVING_MODE));
199 if (adapter_status == BT_DEACTIVATED && (_bt_core_get_bt_status(BT_POWER_SAVING_MODE) != 0)) {
200 _bt_core_set_bt_status(BT_POWER_SAVING_MODE, 0);
201 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
202 _bt_enable_adapter();
204 BT_DBG("BT LE status before Emergency mode() :%d", _bt_core_get_bt_le_status(BT_POWER_SAVING_MODE));
205 if (adapter_status_le == BT_LE_DEACTIVATED && _bt_core_get_bt_le_status(BT_POWER_SAVING_MODE) != 0) {
206 _bt_core_set_bt_le_status(BT_POWER_SAVING_MODE, 0);
207 /* Enable the BT LE */
208 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
209 _bt_enable_adapter_le();
214 static void __bt_core_flight_mode_cb(keynode_t *node, void *data)
216 gboolean flight_mode = FALSE;
219 BT_DBG("key = %s", vconf_keynode_get_name(node));
221 type = vconf_keynode_get_type(node);
222 if (type != VCONF_TYPE_BOOL) {
223 BT_ERR("Invaild vconf key type : %d", type);
227 flight_mode = vconf_keynode_get_bool(node);
229 __bt_core_handle_adapter_with_flight_mode(flight_mode);
232 #ifndef TIZEN_WEARABLE
233 static void __bt_core_power_saving_mode_cb(keynode_t *node, void *data)
235 int power_saving_mode = 0;
237 DBG_SECURE("key=%s", vconf_keynode_get_name(node));
239 if (vconf_keynode_get_type(node) != VCONF_TYPE_INT) {
240 BT_ERR("Wrong vconf type");
244 power_saving_mode = vconf_keynode_get_int(node);
246 BT_DBG("value=%d", power_saving_mode);
248 __bt_core_handle_adapter_with_power_saving_mode(power_saving_mode);
252 void _bt_core_init_vconf_value(void)
254 gboolean flight_mode = FALSE;
255 int power_saving_mode = 0;
257 _bt_core_handle_flight_mode_noti();
258 _bt_core_handle_power_saving_mode_noti();
260 if (vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &flight_mode) != 0)
261 BT_ERR("Fail to get the flight_mode status value");
262 #ifndef TIZEN_WEARABLE
263 #ifdef ENABLE_TIZEN_2_4
264 if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &power_saving_mode) != 0)
265 BT_ERR("Fail to get the power_saving_mode status value");
268 BT_DBG("flight_mode = %d, power_saving_mode = %d", flight_mode, power_saving_mode);
270 if (flight_mode == TRUE)
271 __bt_core_handle_adapter_with_flight_mode(flight_mode);
272 else if (power_saving_mode > 0)
273 __bt_core_handle_adapter_with_power_saving_mode(power_saving_mode);
278 void _bt_core_handle_flight_mode_noti(void)
280 #ifdef TIZEN_TELEPHONY_ENABLED
285 ret = vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
286 (vconf_callback_fn)__bt_core_flight_mode_cb, NULL);
288 BT_ERR("Unable to register key handler");
291 BT_DBG("Telephony is disabled");
295 void _bt_core_handle_power_saving_mode_noti(void)
297 #ifndef TIZEN_WEARABLE
301 #ifdef ENABLE_TIZEN_2_4
302 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
303 (vconf_callback_fn)__bt_core_power_saving_mode_cb, NULL);
306 BT_ERR("Unable to register key handler");
310 void _bt_core_unregister_vconf_handler(void)
312 #ifdef TIZEN_TELEPHONY_ENABLED
313 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
314 (vconf_callback_fn)__bt_core_flight_mode_cb);
317 #ifndef TIZEN_WEARABLE
318 #ifdef ENABLE_TIZEN_2_4
319 vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
320 (vconf_callback_fn)__bt_core_power_saving_mode_cb);