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 "osi/include/future.h"
26 
27 using namespace bluetooth;
28 
29 namespace {
30 const char* PTS_AVRCP_TEST = "PTS_AvrcpTest";
31 const char* PTS_SECURE_ONLY_MODE = "PTS_SecurePairOnly";
32 const char* PTS_LE_CONN_UPDATED_DISABLED = "PTS_DisableConnUpdates";
33 const char* PTS_DISABLE_SDP_LE_PAIR = "PTS_DisableSDPOnLEPair";
34 const char* PTS_SMP_PAIRING_OPTIONS_KEY = "PTS_SmpOptions";
35 const char* PTS_SMP_FAILURE_CASE_KEY = "PTS_SmpFailureCase";
36 const char* PTS_FORCE_EATT_FOR_NOTIFICATIONS = "PTS_ForceEattForNotifications";
37 const char* PTS_CONNECT_EATT_UNCONDITIONALLY = "PTS_ConnectEattUncondictionally";
38 const char* PTS_CONNECT_EATT_UNENCRYPTED = "PTS_ConnectEattUnencrypted";
39 const char* PTS_BROADCAST_UNENCRYPTED = "PTS_BroadcastUnencrypted";
40 const char* PTS_FORCE_LE_AUDIO_MULTIPLE_CONTEXTS_METADATA =
41         "PTS_ForceLeAudioMultipleContextsMetadata";
42 const char* PTS_EATT_PERIPHERAL_COLLISION_SUPPORT = "PTS_EattPeripheralCollionSupport";
43 const char* PTS_EATT_USE_FOR_ALL_SERVICES = "PTS_UseEattForAllServices";
44 const char* PTS_L2CAP_ECOC_UPPER_TESTER = "PTS_L2capEcocUpperTester";
45 const char* PTS_L2CAP_ECOC_MIN_KEY_SIZE = "PTS_L2capEcocMinKeySize";
46 const char* PTS_L2CAP_ECOC_INITIAL_CHAN_CNT = "PTS_L2capEcocInitialChanCnt";
47 const char* PTS_L2CAP_ECOC_CONNECT_REMAINING = "PTS_L2capEcocConnectRemaining";
48 const char* PTS_L2CAP_ECOC_SEND_NUM_OF_SDU = "PTS_L2capEcocSendNumOfSdu";
49 const char* PTS_L2CAP_ECOC_RECONFIGURE = "PTS_L2capEcocReconfigure";
50 const char* PTS_BROADCAST_AUDIO_CONFIG_OPTION = "PTS_BroadcastAudioConfigOption";
51 const char* PTS_LE_AUDIO_SUSPEND_STREAMING = "PTS_LeAudioSuspendStreaming";
52 
53 static std::unique_ptr<config_t> config;
54 }  // namespace
55 
56 // Module lifecycle functions
57 
init()58 static future_t* init() {
59 // TODO(armansito): Find a better way than searching by a hardcoded path.
60 #if defined(TARGET_FLOSS)
61   const char* path = "/etc/bluetooth/bt_stack.conf";
62 #elif defined(__ANDROID__)
63   const char* path = "/apex/com.android.btservices/etc/bluetooth/bt_stack.conf";
64 #else   // !defined(__ANDROID__)
65   const char* path = "bt_stack.conf";
66 #endif  // defined(__ANDROID__)
67   log::assert_that(path != NULL, "assert failed: path != NULL");
68 
69   log::info("attempt to load stack conf from {}", path);
70 
71   config = config_new(path);
72   if (!config) {
73     log::info("file >{}< not found", path);
74     config = config_new_empty();
75   }
76 
77   return future_new_immediate(FUTURE_SUCCESS);
78 }
79 
clean_up()80 static future_t* clean_up() {
81   config.reset();
82   return future_new_immediate(FUTURE_SUCCESS);
83 }
84 
85 EXPORT_SYMBOL extern const module_t stack_config_module = {.name = STACK_CONFIG_MODULE,
86                                                            .init = init,
87                                                            .start_up = NULL,
88                                                            .shut_down = NULL,
89                                                            .clean_up = clean_up,
90                                                            .dependencies = {NULL}};
91 
92 // Interface functions
get_pts_avrcp_test(void)93 static bool get_pts_avrcp_test(void) {
94   return config_get_bool(*config, CONFIG_DEFAULT_SECTION, PTS_AVRCP_TEST, false);
95 }
96 
get_pts_secure_only_mode(void)97 static bool get_pts_secure_only_mode(void) {
98   return config_get_bool(*config, CONFIG_DEFAULT_SECTION, PTS_SECURE_ONLY_MODE, false);
99 }
100 
get_pts_conn_updates_disabled(void)101 static bool get_pts_conn_updates_disabled(void) {
102   return config_get_bool(*config, CONFIG_DEFAULT_SECTION, PTS_LE_CONN_UPDATED_DISABLED, false);
103 }
104 
get_pts_crosskey_sdp_disable(void)105 static bool get_pts_crosskey_sdp_disable(void) {
106   return config_get_bool(*config, CONFIG_DEFAULT_SECTION, PTS_DISABLE_SDP_LE_PAIR, false);
107 }
108 
get_pts_smp_options(void)109 static const std::string* get_pts_smp_options(void) {
110   return config_get_string(*config, CONFIG_DEFAULT_SECTION, PTS_SMP_PAIRING_OPTIONS_KEY, NULL);
111 }
112 
get_pts_smp_failure_case(void)113 static int get_pts_smp_failure_case(void) {
114   return config_get_int(*config, CONFIG_DEFAULT_SECTION, PTS_SMP_FAILURE_CASE_KEY, 0);
115 }
116 
get_pts_force_eatt_for_notifications(void)117 static bool get_pts_force_eatt_for_notifications(void) {
118   return config_get_bool(*config, CONFIG_DEFAULT_SECTION, PTS_FORCE_EATT_FOR_NOTIFICATIONS, false);
119 }
120 
get_pts_connect_eatt_unconditionally(void)121 static bool get_pts_connect_eatt_unconditionally(void) {
122   return config_get_bool(*config, CONFIG_DEFAULT_SECTION, PTS_CONNECT_EATT_UNCONDITIONALLY, false);
123 }
124 
get_pts_connect_eatt_before_encryption(void)125 static bool get_pts_connect_eatt_before_encryption(void) {
126   return config_get_bool(*config, CONFIG_DEFAULT_SECTION, PTS_CONNECT_EATT_UNENCRYPTED, false);
127 }
128 
get_pts_unencrypt_broadcast(void)129 static bool get_pts_unencrypt_broadcast(void) {
130   return config_get_bool(*config, CONFIG_DEFAULT_SECTION, PTS_BROADCAST_UNENCRYPTED, false);
131 }
132 
get_pts_eatt_peripheral_collision_support(void)133 static bool get_pts_eatt_peripheral_collision_support(void) {
134   return config_get_bool(*config, CONFIG_DEFAULT_SECTION, PTS_EATT_PERIPHERAL_COLLISION_SUPPORT,
135                          false);
136 }
137 
get_pts_use_eatt_for_all_services(void)138 static bool get_pts_use_eatt_for_all_services(void) {
139   return config_get_bool(*config, CONFIG_DEFAULT_SECTION, PTS_EATT_USE_FOR_ALL_SERVICES, false);
140 }
141 
get_pts_force_le_audio_multiple_contexts_metadata(void)142 static bool get_pts_force_le_audio_multiple_contexts_metadata(void) {
143   return config_get_bool(*config, CONFIG_DEFAULT_SECTION,
144                          PTS_FORCE_LE_AUDIO_MULTIPLE_CONTEXTS_METADATA, false);
145 }
146 
get_pts_l2cap_ecoc_upper_tester(void)147 static bool get_pts_l2cap_ecoc_upper_tester(void) {
148   return config_get_bool(*config, CONFIG_DEFAULT_SECTION, PTS_L2CAP_ECOC_UPPER_TESTER, false);
149 }
150 
get_pts_l2cap_ecoc_min_key_size(void)151 static int get_pts_l2cap_ecoc_min_key_size(void) {
152   return config_get_int(*config, CONFIG_DEFAULT_SECTION, PTS_L2CAP_ECOC_MIN_KEY_SIZE, -1);
153 }
154 
get_pts_l2cap_ecoc_initial_chan_cnt(void)155 static int get_pts_l2cap_ecoc_initial_chan_cnt(void) {
156   return config_get_int(*config, CONFIG_DEFAULT_SECTION, PTS_L2CAP_ECOC_INITIAL_CHAN_CNT, -1);
157 }
158 
get_pts_l2cap_ecoc_connect_remaining(void)159 static bool get_pts_l2cap_ecoc_connect_remaining(void) {
160   return config_get_bool(*config, CONFIG_DEFAULT_SECTION, PTS_L2CAP_ECOC_CONNECT_REMAINING, false);
161 }
162 
get_pts_l2cap_ecoc_send_num_of_sdu(void)163 static int get_pts_l2cap_ecoc_send_num_of_sdu(void) {
164   return config_get_int(*config, CONFIG_DEFAULT_SECTION, PTS_L2CAP_ECOC_SEND_NUM_OF_SDU, -1);
165 }
166 
get_pts_l2cap_ecoc_reconfigure(void)167 static bool get_pts_l2cap_ecoc_reconfigure(void) {
168   return config_get_bool(*config, CONFIG_DEFAULT_SECTION, PTS_L2CAP_ECOC_RECONFIGURE, false);
169 }
170 
get_pts_broadcast_audio_config_options(void)171 static const std::string* get_pts_broadcast_audio_config_options(void) {
172   if (!config) {
173     log::info("Config isn't ready, use default option");
174     return NULL;
175   }
176   return config_get_string(*config, CONFIG_DEFAULT_SECTION, PTS_BROADCAST_AUDIO_CONFIG_OPTION,
177                            NULL);
178 }
179 
get_pts_le_audio_disable_ases_before_stopping(void)180 static bool get_pts_le_audio_disable_ases_before_stopping(void) {
181   return config_get_bool(*config, CONFIG_DEFAULT_SECTION, PTS_LE_AUDIO_SUSPEND_STREAMING, false);
182 }
183 
get_all(void)184 static config_t* get_all(void) { return config.get(); }
185 
186 const stack_config_t interface = {get_pts_avrcp_test,
187                                   get_pts_secure_only_mode,
188                                   get_pts_conn_updates_disabled,
189                                   get_pts_crosskey_sdp_disable,
190                                   get_pts_smp_options,
191                                   get_pts_smp_failure_case,
192                                   get_pts_force_eatt_for_notifications,
193                                   get_pts_connect_eatt_unconditionally,
194                                   get_pts_connect_eatt_before_encryption,
195                                   get_pts_unencrypt_broadcast,
196                                   get_pts_eatt_peripheral_collision_support,
197                                   get_pts_use_eatt_for_all_services,
198                                   get_pts_force_le_audio_multiple_contexts_metadata,
199                                   get_pts_l2cap_ecoc_upper_tester,
200                                   get_pts_l2cap_ecoc_min_key_size,
201                                   get_pts_l2cap_ecoc_initial_chan_cnt,
202                                   get_pts_l2cap_ecoc_connect_remaining,
203                                   get_pts_l2cap_ecoc_send_num_of_sdu,
204                                   get_pts_l2cap_ecoc_reconfigure,
205                                   get_pts_broadcast_audio_config_options,
206                                   get_pts_le_audio_disable_ases_before_stopping,
207                                   get_all};
208 
stack_config_get_interface(void)209 const stack_config_t* stack_config_get_interface(void) { return &interface; }
210