Fix LE discovery state miss-matching issue
[platform/core/connectivity/bluetooth-frwk.git] / bt-core / bt-core-noti-handler.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <vconf.h>
19 #include <vconf-keys.h>
20
21 #include "bt-core-adapter.h"
22 #include "bt-core-common.h"
23 #include "bt-core-noti-handler.h"
24
25 static gboolean flightmode_request = FALSE;
26
27 void _bt_set_flightmode_request(gboolean value)
28 {
29         flightmode_request = value;
30 }
31
32 gboolean _bt_is_flightmode_request(void)
33 {
34         return flightmode_request;
35 }
36
37 static gboolean __bt_off_cause_conflict_check(void)
38 {
39         int flight_mode_value = 0;
40         int ps_mode_value = 0;
41         gboolean ret = FALSE;
42
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);
47
48         if (flight_mode_value == 1 || ps_mode_value > 0) {
49                 BT_DBG("Bt should not turn on");
50                 ret = TRUE;
51         }
52
53         return ret;
54 }
55
56 static void __bt_core_handle_adapter_with_power_saving_mode(int power_saving_mode)
57 {
58         bt_status_t adapter_status;
59         bt_le_status_t adapter_status_le;
60
61         adapter_status = _bt_core_get_status();
62         adapter_status_le = _bt_core_get_le_status();
63
64         if (power_saving_mode == 2) {
65                 BT_DBG("Deactivate Bluetooth Service");
66                 if (vconf_set_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, 1) != 0)
67                         BT_ERR("Set vconf failed");
68
69                 if (adapter_status == BT_ACTIVATED) {
70                         int bt_status_before_mode = 0;
71                         if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
72                                 _bt_core_set_bt_status(BT_POWER_SAVING_MODE, bt_status_before_mode);
73
74                         if (!TIZEN_FEATURE_BT_USB_DONGLE)
75                                 _bt_disable_adapter();
76                 }
77                 if (adapter_status_le == BT_LE_ACTIVATED) {
78                         int bt_le_status_before_mode = 0;
79
80                         if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
81                                 _bt_core_set_bt_le_status(BT_POWER_SAVING_MODE, bt_le_status_before_mode);
82
83                         if (!TIZEN_FEATURE_BT_USB_DONGLE)
84                                 _bt_disable_adapter_le();
85                 }
86         } else {
87                 int ps_mode_value = 0;
88
89                 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_value))
90                         BT_ERR("Fail get power saving mode value");
91
92                 if (ps_mode_value == 0)
93                         return;
94
95                 BT_DBG("Activate Bluetooth");
96                 if (vconf_set_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, 0))
97                         BT_ERR("Set vconf failed");
98
99                 ret_if(__bt_off_cause_conflict_check());
100
101                 BT_DBG("BT status before Emergency mode() :%d",
102                         _bt_core_get_bt_status(BT_POWER_SAVING_MODE));
103
104                 if (adapter_status == BT_DEACTIVATED && (_bt_core_get_bt_status(BT_POWER_SAVING_MODE) != 0)) {
105                         _bt_core_set_bt_status(BT_POWER_SAVING_MODE, 0);
106                         _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
107                         if (!TIZEN_FEATURE_BT_USB_DONGLE)
108                                 _bt_enable_adapter();
109                 }
110                 BT_DBG("BT LE status before Emergency mode() :%d", _bt_core_get_bt_le_status(BT_POWER_SAVING_MODE));
111                 if (adapter_status_le == BT_LE_DEACTIVATED &&  _bt_core_get_bt_le_status(BT_POWER_SAVING_MODE) != 0) {
112                         _bt_core_set_bt_le_status(BT_POWER_SAVING_MODE, 0);
113                         /* Enable the BT LE */
114                         _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
115                         if (!TIZEN_FEATURE_BT_USB_DONGLE)
116                                 _bt_enable_adapter_le();
117                 }
118         }
119 }
120
121 void _bt_core_init_vconf_value(void)
122 {
123         gboolean flight_mode = FALSE;
124         int power_saving_mode = 0;
125         int bt_flight_mode = 0;
126         int bt_ps_mode = 0;
127
128         //_bt_core_handle_flight_mode_noti();
129         flight_mode = _bt_core_is_flight_mode_enabled();
130
131         if (!TIZEN_PROFILE_WEARABLE) {
132                 if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &power_saving_mode) != 0)
133                         BT_ERR("Fail to get the power_saving_mode status value");
134                 BT_DBG("flight_mode = %d, power_saving_mode = %d", flight_mode, power_saving_mode);
135         }
136
137         BT_DBG("flight_mode = %d, power_saving_mode = %d", flight_mode, power_saving_mode);
138
139         if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &bt_flight_mode))
140                 BT_ERR("Fail get flight mode value");
141         _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_flight_mode);
142
143         if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &bt_ps_mode))
144                 BT_ERR("Fail get power saving mode value");
145         _bt_core_set_bt_status(BT_POWER_SAVING_MODE, bt_ps_mode);
146
147         if (power_saving_mode > 0)
148                 __bt_core_handle_adapter_with_power_saving_mode(power_saving_mode);
149         else
150                 BT_ERR("");
151 }
152
153 void _bt_core_unregister_vconf_handler(void)
154 {
155
156 }
157