Add the default deny rule for org.projectx.bt.mesh
[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 #ifdef TIZEN_FEATURE_BUSACT
75                         _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
76 #endif
77                         if (!TIZEN_FEATURE_BT_USB_DONGLE)
78                                 _bt_disable_adapter();
79                 }
80                 if (adapter_status_le == BT_LE_ACTIVATED) {
81                         int bt_le_status_before_mode = 0;
82
83                         if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
84                                 _bt_core_set_bt_le_status(BT_POWER_SAVING_MODE, bt_le_status_before_mode);
85
86 #ifdef TIZEN_FEATURE_BUSACT
87                         /* Disable the BT LE */
88                         _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
89 #endif
90                         if (!TIZEN_FEATURE_BT_USB_DONGLE)
91                                 _bt_disable_adapter_le();
92                 }
93         } else {
94                 int ps_mode_value = 0;
95
96                 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &ps_mode_value))
97                         BT_ERR("Fail get power saving mode value");
98
99                 if (ps_mode_value == 0)
100                         return;
101
102                 BT_DBG("Activate Bluetooth");
103                 if (vconf_set_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, 0))
104                         BT_ERR("Set vconf failed");
105
106                 ret_if(__bt_off_cause_conflict_check());
107
108                 BT_DBG("BT status before Emergency mode() :%d",
109                         _bt_core_get_bt_status(BT_POWER_SAVING_MODE));
110
111                 if (adapter_status == BT_DEACTIVATED && (_bt_core_get_bt_status(BT_POWER_SAVING_MODE) != 0)) {
112                         _bt_core_set_bt_status(BT_POWER_SAVING_MODE, 0);
113                         _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
114                         if (!TIZEN_FEATURE_BT_USB_DONGLE)
115                                 _bt_enable_adapter();
116                 }
117                 BT_DBG("BT LE status before Emergency mode() :%d", _bt_core_get_bt_le_status(BT_POWER_SAVING_MODE));
118                 if (adapter_status_le == BT_LE_DEACTIVATED &&  _bt_core_get_bt_le_status(BT_POWER_SAVING_MODE) != 0) {
119                         _bt_core_set_bt_le_status(BT_POWER_SAVING_MODE, 0);
120                         /* Enable the BT LE */
121                         _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
122                         if (!TIZEN_FEATURE_BT_USB_DONGLE)
123                                 _bt_enable_adapter_le();
124                 }
125         }
126 }
127
128 #ifdef ENABLE_TIZEN_2_4
129 static void __bt_core_power_saving_mode_cb(keynode_t *node, void *data)
130 {
131         int power_saving_mode = 0;
132
133         DBG_SECURE("key=%s", vconf_keynode_get_name(node));
134
135         if (vconf_keynode_get_type(node) != VCONF_TYPE_INT) {
136                 BT_ERR("Wrong vconf type");
137                 return;
138         }
139
140         power_saving_mode = vconf_keynode_get_int(node);
141
142         BT_DBG("value=%d", power_saving_mode);
143
144         __bt_core_handle_adapter_with_power_saving_mode(power_saving_mode);
145 }
146 #endif
147
148 void _bt_core_init_vconf_value(void)
149 {
150         gboolean flight_mode = FALSE;
151 #ifndef ENABLE_TIZEN_2_4
152         int power_saving_mode = 0;
153 #endif
154         int bt_flight_mode = 0;
155         int bt_ps_mode = 0;
156
157         //_bt_core_handle_flight_mode_noti();
158         _bt_core_handle_power_saving_mode_noti();
159
160         flight_mode = _bt_core_is_flight_mode_enabled();
161
162 #ifndef ENABLE_TIZEN_2_4
163         if (!TIZEN_PROFILE_WEARABLE) {
164                 if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &power_saving_mode) != 0)
165                         BT_ERR("Fail to get the power_saving_mode status value");
166                 BT_DBG("flight_mode = %d, power_saving_mode = %d", flight_mode, power_saving_mode);
167         }
168 #endif
169         BT_DBG("flight_mode = %d, power_saving_mode = %d", flight_mode, power_saving_mode);
170
171         if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &bt_flight_mode))
172                 BT_ERR("Fail get flight mode value");
173         _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_flight_mode);
174
175         if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &bt_ps_mode))
176                 BT_ERR("Fail get power saving mode value");
177         _bt_core_set_bt_status(BT_POWER_SAVING_MODE, bt_ps_mode);
178 #ifndef ENABLE_TIZEN_2_4
179         if (power_saving_mode > 0)
180                 __bt_core_handle_adapter_with_power_saving_mode(power_saving_mode);
181         else
182                 BT_ERR("");
183 #endif
184 }
185
186 void _bt_core_handle_power_saving_mode_noti(void)
187 {
188         if (TIZEN_PROFILE_WEARABLE)
189                 return;
190
191         BT_DBG("+");
192 #ifdef ENABLE_TIZEN_2_4
193         int ret = 0;
194
195         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
196                         (vconf_callback_fn)__bt_core_power_saving_mode_cb, NULL);
197         if (ret < 0)
198                 BT_ERR("Unable to register key handler");
199 #endif
200 }
201
202 void _bt_core_unregister_vconf_handler(void)
203 {
204 #ifdef ENABLE_TIZEN_2_4
205         if (!TIZEN_PROFILE_WEARABLE) {
206                 vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
207                                 (vconf_callback_fn)__bt_core_power_saving_mode_cb);
208         }
209 #endif
210 }
211