Mesh:Fix Svace issue in bluetooth-frwk
[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                         _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
75
76                         if (!TIZEN_FEATURE_BT_USB_DONGLE)
77                                 _bt_disable_adapter();
78                 }
79                 if (adapter_status_le == BT_LE_ACTIVATED) {
80                         int bt_le_status_before_mode = 0;
81
82                         if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
83                                 _bt_core_set_bt_le_status(BT_POWER_SAVING_MODE, bt_le_status_before_mode);
84
85                         /* Disable the BT LE */
86                         _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
87
88                         if (!TIZEN_FEATURE_BT_USB_DONGLE)
89                                 _bt_disable_adapter_le();
90                 }
91         } else {
92                 int ps_mode_value = 0;
93
94                 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_value))
95                         BT_ERR("Fail get power saving mode value");
96
97                 if (ps_mode_value == 0)
98                         return;
99
100                 BT_DBG("Activate Bluetooth");
101                 if (vconf_set_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, 0))
102                         BT_ERR("Set vconf failed");
103
104                 ret_if(__bt_off_cause_conflict_check());
105
106                 BT_DBG("BT status before Emergency mode() :%d",
107                         _bt_core_get_bt_status(BT_POWER_SAVING_MODE));
108
109                 if (adapter_status == BT_DEACTIVATED && (_bt_core_get_bt_status(BT_POWER_SAVING_MODE) != 0)) {
110                         _bt_core_set_bt_status(BT_POWER_SAVING_MODE, 0);
111                         _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
112                         if (!TIZEN_FEATURE_BT_USB_DONGLE)
113                                 _bt_enable_adapter();
114                 }
115                 BT_DBG("BT LE status before Emergency mode() :%d", _bt_core_get_bt_le_status(BT_POWER_SAVING_MODE));
116                 if (adapter_status_le == BT_LE_DEACTIVATED &&  _bt_core_get_bt_le_status(BT_POWER_SAVING_MODE) != 0) {
117                         _bt_core_set_bt_le_status(BT_POWER_SAVING_MODE, 0);
118                         /* Enable the BT LE */
119                         _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
120                         if (!TIZEN_FEATURE_BT_USB_DONGLE)
121                                 _bt_enable_adapter_le();
122                 }
123         }
124 }
125
126 void _bt_core_init_vconf_value(void)
127 {
128         gboolean flight_mode = FALSE;
129         int power_saving_mode = 0;
130         int bt_flight_mode = 0;
131         int bt_ps_mode = 0;
132
133         //_bt_core_handle_flight_mode_noti();
134         flight_mode = _bt_core_is_flight_mode_enabled();
135
136         if (!TIZEN_PROFILE_WEARABLE) {
137                 if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &power_saving_mode) != 0)
138                         BT_ERR("Fail to get the power_saving_mode status value");
139                 BT_DBG("flight_mode = %d, power_saving_mode = %d", flight_mode, power_saving_mode);
140         }
141
142         BT_DBG("flight_mode = %d, power_saving_mode = %d", flight_mode, power_saving_mode);
143
144         if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &bt_flight_mode))
145                 BT_ERR("Fail get flight mode value");
146         _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_flight_mode);
147
148         if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &bt_ps_mode))
149                 BT_ERR("Fail get power saving mode value");
150         _bt_core_set_bt_status(BT_POWER_SAVING_MODE, bt_ps_mode);
151
152         if (power_saving_mode > 0)
153                 __bt_core_handle_adapter_with_power_saving_mode(power_saving_mode);
154         else
155                 BT_ERR("");
156 }
157
158 void _bt_core_unregister_vconf_handler(void)
159 {
160
161 }
162