1 /******************************************************************************
2  *
3  *  Copyright 2014 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #define LOG_TAG "bt_stack_config"
20 
21 #include "internal_include/stack_config.h"
22 
23 #include <bluetooth/log.h>
24 
25 #include "os/log.h"
26 #include "osi/include/future.h"
27 
28 using namespace bluetooth;
29 
30 namespace {
31 const char* PTS_AVRCP_TEST = "PTS_AvrcpTest";
32 const char* PTS_SECURE_ONLY_MODE = "PTS_SecurePairOnly";
33 const char* PTS_LE_CONN_UPDATED_DISABLED = "PTS_DisableConnUpdates";
34 const char* PTS_DISABLE_SDP_LE_PAIR = "PTS_DisableSDPOnLEPair";
35 const char* PTS_SMP_PAIRING_OPTIONS_KEY = "PTS_SmpOptions";
36 const char* PTS_SMP_FAILURE_CASE_KEY = "PTS_SmpFailureCase";
37 const char* PTS_FORCE_EATT_FOR_NOTIFICATIONS = "PTS_ForceEattForNotifications";
38 const char* PTS_CONNECT_EATT_UNCONDITIONALLY =
39     "PTS_ConnectEattUncondictionally";
40 const char* PTS_CONNECT_EATT_UNENCRYPTED = "PTS_ConnectEattUnencrypted";
41 const char* PTS_BROADCAST_UNENCRYPTED = "PTS_BroadcastUnencrypted";
42 const char* PTS_FORCE_LE_AUDIO_MULTIPLE_CONTEXTS_METADATA =
43     "PTS_ForceLeAudioMultipleContextsMetadata";
44 const char* PTS_EATT_PERIPHERAL_COLLISION_SUPPORT =
45     "PTS_EattPeripheralCollionSupport";
46 const char* PTS_EATT_USE_FOR_ALL_SERVICES = "PTS_UseEattForAllServices";
47 const char* PTS_L2CAP_ECOC_UPPER_TESTER = "PTS_L2capEcocUpperTester";
48 const char* PTS_L2CAP_ECOC_MIN_KEY_SIZE = "PTS_L2capEcocMinKeySize";
49 const char* PTS_L2CAP_ECOC_INITIAL_CHAN_CNT = "PTS_L2capEcocInitialChanCnt";
50 const char* PTS_L2CAP_ECOC_CONNECT_REMAINING = "PTS_L2capEcocConnectRemaining";
51 const char* PTS_L2CAP_ECOC_SEND_NUM_OF_SDU = "PTS_L2capEcocSendNumOfSdu";
52 const char* PTS_L2CAP_ECOC_RECONFIGURE = "PTS_L2capEcocReconfigure";
53 const char* PTS_BROADCAST_AUDIO_CONFIG_OPTION =
54     "PTS_BroadcastAudioConfigOption";
55 const char* PTS_LE_AUDIO_SUSPEND_STREAMING = "PTS_LeAudioSuspendStreaming";
56 
57 static std::unique_ptr<config_t> config;
58 }  // namespace
59 
60 // Module lifecycle functions
61 
init()62 static future_t* init() {
63 // TODO(armansito): Find a better way than searching by a hardcoded path.
64 #if defined(TARGET_FLOSS)
65   const char* path = "/etc/bluetooth/bt_stack.conf";
66 #elif defined(__ANDROID__)
67   const char* path = "/apex/com.android.btservices/etc/bluetooth/bt_stack.conf";
68 #else   // !defined(__ANDROID__)
69   const char* path = "bt_stack.conf";
70 #endif  // defined(__ANDROID__)
71   log::assert_that(path != NULL, "assert failed: path != NULL");
72 
73   log::info("attempt to load stack conf from {}", path);
74 
75   config = config_new(path);
76   if (!config) {
77     log::info("file >{}< not found", path);
78     config = config_new_empty();
79   }
80 
81   return future_new_immediate(FUTURE_SUCCESS);
82 }
83 
clean_up()84 static future_t* clean_up() {
85   config.reset();
86   return future_new_immediate(FUTURE_SUCCESS);
87 }
88 
89 EXPORT_SYMBOL extern const module_t stack_config_module = {
90     .name = STACK_CONFIG_MODULE,
91     .init = init,
92     .start_up = NULL,
93     .shut_down = NULL,
94     .clean_up = clean_up,
95     .dependencies = {NULL}};
96 
97 // Interface functions
get_pts_avrcp_test(void)98 static bool get_pts_avrcp_test(void) {
99   return config_get_bool(*config, CONFIG_DEFAULT_SECTION, PTS_AVRCP_TEST,
100                          false);
101 }
102 
get_pts_secure_only_mode(void)103 static bool get_pts_secure_only_mode(void) {
104   return config_get_bool(*config, CONFIG_DEFAULT_SECTION, PTS_SECURE_ONLY_MODE,
105                          false);
106 }
107 
get_pts_conn_updates_disabled(void)108 static bool get_pts_conn_updates_disabled(void) {
109   return config_get_bool(*config, CONFIG_DEFAULT_SECTION,
110                          PTS_LE_CONN_UPDATED_DISABLED, false);
111 }
112 
get_pts_crosskey_sdp_disable(void)113 static bool get_pts_crosskey_sdp_disable(void) {
114   return config_get_bool(*config, CONFIG_DEFAULT_SECTION,
115                          PTS_DISABLE_SDP_LE_PAIR, false);
116 }
117 
get_pts_smp_options(void)118 static const std::string* get_pts_smp_options(void) {
119   return config_get_string(*config, CONFIG_DEFAULT_SECTION,
120                            PTS_SMP_PAIRING_OPTIONS_KEY, NULL);
121 }
122 
get_pts_smp_failure_case(void)123 static int get_pts_smp_failure_case(void) {
124   return config_get_int(*config, CONFIG_DEFAULT_SECTION,
125                         PTS_SMP_FAILURE_CASE_KEY, 0);
126 }
127 
get_pts_force_eatt_for_notifications(void)128 static bool get_pts_force_eatt_for_notifications(void) {
129   return config_get_bool(*config, CONFIG_DEFAULT_SECTION,
130                          PTS_FORCE_EATT_FOR_NOTIFICATIONS, false);
131 }
132 
get_pts_connect_eatt_unconditionally(void)133 static bool get_pts_connect_eatt_unconditionally(void) {
134   return config_get_bool(*config, CONFIG_DEFAULT_SECTION,
135                          PTS_CONNECT_EATT_UNCONDITIONALLY, false);
136 }
137 
get_pts_connect_eatt_before_encryption(void)138 static bool get_pts_connect_eatt_before_encryption(void) {
139   return config_get_bool(*config, CONFIG_DEFAULT_SECTION,
140                          PTS_CONNECT_EATT_UNENCRYPTED, false);
141 }
142 
get_pts_unencrypt_broadcast(void)143 static bool get_pts_unencrypt_broadcast(void) {
144   return config_get_bool(*config, CONFIG_DEFAULT_SECTION,
145                          PTS_BROADCAST_UNENCRYPTED, false);
146 }
147 
get_pts_eatt_peripheral_collision_support(void)148 static bool get_pts_eatt_peripheral_collision_support(void) {
149   return config_get_bool(*config, CONFIG_DEFAULT_SECTION,
150                          PTS_EATT_PERIPHERAL_COLLISION_SUPPORT, false);
151 }
152 
get_pts_use_eatt_for_all_services(void)153 static bool get_pts_use_eatt_for_all_services(void) {
154   return config_get_bool(*config, CONFIG_DEFAULT_SECTION,
155                          PTS_EATT_USE_FOR_ALL_SERVICES, false);
156 }
157 
get_pts_force_le_audio_multiple_contexts_metadata(void)158 static bool get_pts_force_le_audio_multiple_contexts_metadata(void) {
159   return config_get_bool(*config, CONFIG_DEFAULT_SECTION,
160                          PTS_FORCE_LE_AUDIO_MULTIPLE_CONTEXTS_METADATA, false);
161 }
162 
get_pts_l2cap_ecoc_upper_tester(void)163 static bool get_pts_l2cap_ecoc_upper_tester(void) {
164   return config_get_bool(*config, CONFIG_DEFAULT_SECTION,
165                          PTS_L2CAP_ECOC_UPPER_TESTER, false);
166 }
167 
get_pts_l2cap_ecoc_min_key_size(void)168 static int get_pts_l2cap_ecoc_min_key_size(void) {
169   return config_get_int(*config, CONFIG_DEFAULT_SECTION,
170                         PTS_L2CAP_ECOC_MIN_KEY_SIZE, -1);
171 }
172 
get_pts_l2cap_ecoc_initial_chan_cnt(void)173 static int get_pts_l2cap_ecoc_initial_chan_cnt(void) {
174   return config_get_int(*config, CONFIG_DEFAULT_SECTION,
175                         PTS_L2CAP_ECOC_INITIAL_CHAN_CNT, -1);
176 }
177 
get_pts_l2cap_ecoc_connect_remaining(void)178 static bool get_pts_l2cap_ecoc_connect_remaining(void) {
179   return config_get_bool(*config, CONFIG_DEFAULT_SECTION,
180                          PTS_L2CAP_ECOC_CONNECT_REMAINING, false);
181 }
182 
get_pts_l2cap_ecoc_send_num_of_sdu(void)183 static int get_pts_l2cap_ecoc_send_num_of_sdu(void) {
184   return config_get_int(*config, CONFIG_DEFAULT_SECTION,
185                         PTS_L2CAP_ECOC_SEND_NUM_OF_SDU, -1);
186 }
187 
get_pts_l2cap_ecoc_reconfigure(void)188 static bool get_pts_l2cap_ecoc_reconfigure(void) {
189   return config_get_bool(*config, CONFIG_DEFAULT_SECTION,
190                          PTS_L2CAP_ECOC_RECONFIGURE, false);
191 }
192 
get_pts_broadcast_audio_config_options(void)193 static const std::string* get_pts_broadcast_audio_config_options(void) {
194   if (!config) {
195     log::info("Config isn't ready, use default option");
196     return NULL;
197   }
198   return config_get_string(*config, CONFIG_DEFAULT_SECTION,
199                            PTS_BROADCAST_AUDIO_CONFIG_OPTION, NULL);
200 }
201 
get_pts_le_audio_disable_ases_before_stopping(void)202 static bool get_pts_le_audio_disable_ases_before_stopping(void) {
203   return config_get_bool(*config, CONFIG_DEFAULT_SECTION,
204                          PTS_LE_AUDIO_SUSPEND_STREAMING, false);
205 }
206 
get_all(void)207 static config_t* get_all(void) { return config.get(); }
208 
209 const stack_config_t interface = {
210     get_pts_avrcp_test,
211     get_pts_secure_only_mode,
212     get_pts_conn_updates_disabled,
213     get_pts_crosskey_sdp_disable,
214     get_pts_smp_options,
215     get_pts_smp_failure_case,
216     get_pts_force_eatt_for_notifications,
217     get_pts_connect_eatt_unconditionally,
218     get_pts_connect_eatt_before_encryption,
219     get_pts_unencrypt_broadcast,
220     get_pts_eatt_peripheral_collision_support,
221     get_pts_use_eatt_for_all_services,
222     get_pts_force_le_audio_multiple_contexts_metadata,
223     get_pts_l2cap_ecoc_upper_tester,
224     get_pts_l2cap_ecoc_min_key_size,
225     get_pts_l2cap_ecoc_initial_chan_cnt,
226     get_pts_l2cap_ecoc_connect_remaining,
227     get_pts_l2cap_ecoc_send_num_of_sdu,
228     get_pts_l2cap_ecoc_reconfigure,
229     get_pts_broadcast_audio_config_options,
230     get_pts_le_audio_disable_ases_before_stopping,
231     get_all};
232 
stack_config_get_interface(void)233 const stack_config_t* stack_config_get_interface(void) { return &interface; }
234