1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
use log::{error, info};
use paste::paste;
use std::collections::HashMap;
use std::fmt;
use std::sync::Mutex;
// Fallback to bool when type is not specified
macro_rules! type_expand {
() => {
bool
};
($type:ty) => {
$type
};
}
macro_rules! default_value {
() => {
false
};
($type:ty) => {
<$type>::default()
};
($($type:ty)? = $default:tt) => {
$default
};
}
macro_rules! test_value {
() => {
true
};
($type:ty) => {
<$type>::default()
};
}
#[cfg(test)]
macro_rules! call_getter_fn {
($flag:ident) => {
paste! {
[<$flag _is_enabled>]()
}
};
($flag:ident $type:ty) => {
paste! {
[<get_ $flag>]()
}
};
}
macro_rules! create_getter_fn {
($flag:ident) => {
paste! {
#[doc = concat!(" Return true if ", stringify!($flag), " is enabled")]
pub fn [<$flag _is_enabled>]() -> bool {
FLAGS.lock().unwrap().$flag
}
}
};
($flag:ident $type:ty) => {
paste! {
#[doc = concat!(" Return the flag value of ", stringify!($flag))]
pub fn [<get_ $flag>]() -> $type {
FLAGS.lock().unwrap().$flag
}
}
};
}
macro_rules! init_flags {
(flags: { $($flag:ident $(: $type:ty)? $(= $default:tt)?,)* }
extra_fields: { $($extra_field:ident : $extra_field_type:ty $(= $extra_default:tt)?,)* }
extra_parsed_flags: { $($extra_flag:tt => $extra_flag_fn:ident(_, _ $(,$extra_args:tt)*),)*}
dependencies: { $($parent:ident => $child:ident),* }) => {
struct InitFlags {
$($flag : type_expand!($($type)?),)*
$($extra_field : $extra_field_type,)*
}
impl Default for InitFlags {
fn default() -> Self {
Self {
$($flag : default_value!($($type)? $(= $default)?),)*
$($extra_field : default_value!($extra_field_type $(= $extra_default)?),)*
}
}
}
/// Sets all bool flags to true
/// Set all other flags and extra fields to their default type value
pub fn set_all_for_testing() {
*FLAGS.lock().unwrap() = InitFlags {
$($flag: test_value!($($type)?),)*
$($extra_field: test_value!($extra_field_type),)*
};
}
impl InitFlags {
fn parse(flags: Vec<String>) -> Self {
let mut init_flags = Self::default();
for flag in flags {
let values: Vec<&str> = flag.split("=").collect();
if values.len() != 2 {
error!("Bad flag {}, must be in <FLAG>=<VALUE> format", flag);
continue;
}
match values[0] {
$(concat!("INIT_", stringify!($flag)) =>
init_flags.$flag = values[1].parse().unwrap_or_else(|e| {
error!("Parse failure on '{}': {}", flag, e);
default_value!($($type)? $(= $default)?)}),)*
$($extra_flag => $extra_flag_fn(&mut init_flags, values $(, $extra_args)*),)*
_ => error!("Unsaved flag: {} = {}", values[0], values[1])
}
}
init_flags.reconcile()
}
fn reconcile(mut self) -> Self {
loop {
// dependencies can be specified in any order
$(if self.$parent && !self.$child {
self.$child = true;
continue;
})*
break;
}
// TODO: acl should not be off if l2cap is on, but need to reconcile legacy code
if self.gd_l2cap {
// TODO This can never be turned off self.gd_acl = false;
}
self
}
}
impl fmt::Display for InitFlags {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, concat!(
concat!($(concat!(stringify!($flag), "={}")),*),
$(concat!(stringify!($extra_field), "={}")),*),
$(self.$flag),*,
$(self.$extra_field),*)
}
}
$(create_getter_fn!($flag $($type)?);)*
#[cfg(test)]
mod tests_autogenerated {
use super::*;
$(paste! {
#[test]
pub fn [<test_get_ $flag>]() {
let _guard = tests::ASYNC_LOCK.lock().unwrap();
tests::test_load(vec![
&*format!(concat!(concat!("INIT_", stringify!($flag)), "={}"), test_value!($($type)?))
]);
let get_value = call_getter_fn!($flag $($type)?);
drop(_guard); // Prevent poisonning other tests if a panic occurs
assert_eq!(get_value, test_value!($($type)?));
}
})*
}
}
}
#[derive(Default)]
struct ExplicitTagSettings {
map: HashMap<String, bool>,
}
impl fmt::Display for ExplicitTagSettings {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self.map)
}
}
fn parse_logging_tag(flags: &mut InitFlags, values: Vec<&str>, enabled: bool) {
for tag in values[1].split(',') {
flags.logging_debug_explicit_tag_settings.map.insert(tag.to_string(), enabled);
}
}
/// Return true if `tag` is enabled in the flag
pub fn is_debug_logging_enabled_for_tag(tag: &str) -> bool {
let guard = FLAGS.lock().unwrap();
*guard
.logging_debug_explicit_tag_settings
.map
.get(tag)
.unwrap_or(&guard.logging_debug_enabled_for_all)
}
fn parse_hci_adapter(flags: &mut InitFlags, values: Vec<&str>) {
flags.hci_adapter = values[1].parse().unwrap_or(0);
}
init_flags!(
// LINT.IfChange
flags: {
always_send_services_if_gatt_disc_done = true,
asynchronously_start_l2cap_coc = true,
btaa_hci = true,
bta_dm_clear_conn_id_on_client_close = true,
btm_dm_flush_discovery_queue_on_search_cancel,
clear_hidd_interrupt_cid_on_disconnect = true,
delay_hidh_cleanup_until_hidh_ready_start = true,
finite_att_timeout = true,
gatt_robust_caching_client = true,
gatt_robust_caching_server,
gd_core,
gd_l2cap,
gd_link_policy,
gd_rust,
gd_security,
hci_adapter: i32,
irk_rotation,
leaudio_targeted_announcement_reconnection_mode,
logging_debug_enabled_for_all,
pass_phy_update_callback = true,
queue_l2cap_coc_while_encrypting = true,
sdp_serialization = true,
sdp_skip_rnr_if_known = true,
trigger_advertising_callbacks_on_first_resume_after_pause = true,
}
// extra_fields are not a 1 to 1 match with "INIT_*" flags
extra_fields: {
logging_debug_explicit_tag_settings: ExplicitTagSettings,
}
// LINT.ThenChange(/system/gd/common/init_flags.fbs)
extra_parsed_flags: {
"INIT_logging_debug_enabled_for_tags" => parse_logging_tag(_, _, true),
"INIT_logging_debug_disabled_for_tags" => parse_logging_tag(_, _, false),
"--hci" => parse_hci_adapter(_, _),
}
dependencies: {
gd_core => gd_security
}
);
lazy_static! {
static ref FLAGS: Mutex<InitFlags> = Mutex::new(InitFlags::default());
}
/// Loads the flag values from the passed-in vector of string values
pub fn load(raw_flags: Vec<String>) {
crate::init_logging();
let flags = InitFlags::parse(raw_flags);
info!("Flags loaded: {}", flags);
*FLAGS.lock().unwrap() = flags;
}
#[cfg(test)]
mod tests {
use super::*;
lazy_static! {
/// do not run concurrent tests as they all use the same global init_flag struct and
/// accessor
pub(super) static ref ASYNC_LOCK: Mutex<()> = Mutex::new(());
}
pub(super) fn test_load(raw_flags: Vec<&str>) {
let raw_flags = raw_flags.into_iter().map(|x| x.to_string()).collect();
load(raw_flags);
}
#[test]
fn simple_flag() {
let _guard = ASYNC_LOCK.lock().unwrap();
test_load(vec![
"INIT_btaa_hci=false", //override a default flag
"INIT_gatt_robust_caching_server=true",
]);
assert!(!btaa_hci_is_enabled());
assert!(gatt_robust_caching_server_is_enabled());
}
#[test]
fn parsing_failure() {
let _guard = ASYNC_LOCK.lock().unwrap();
test_load(vec![
"foo=bar=?", // vec length
"foo=bar", // flag not save
"INIT_btaa_hci=not_false", // parse error but has default value
"INIT_gatt_robust_caching_server=not_true", // parse error
]);
assert!(btaa_hci_is_enabled());
assert!(!gatt_robust_caching_server_is_enabled());
}
#[test]
fn int_flag() {
let _guard = ASYNC_LOCK.lock().unwrap();
test_load(vec!["--hci=2"]);
assert_eq!(get_hci_adapter(), 2);
}
#[test]
fn explicit_flag() {
let _guard = ASYNC_LOCK.lock().unwrap();
test_load(vec![
"INIT_logging_debug_enabled_for_all=true",
"INIT_logging_debug_enabled_for_tags=foo,bar",
"INIT_logging_debug_disabled_for_tags=foo,bar2",
"INIT_logging_debug_enabled_for_tags=bar2",
]);
assert!(!is_debug_logging_enabled_for_tag("foo"));
assert!(is_debug_logging_enabled_for_tag("bar"));
assert!(is_debug_logging_enabled_for_tag("bar2"));
assert!(is_debug_logging_enabled_for_tag("unknown_flag"));
assert!(logging_debug_enabled_for_all_is_enabled());
FLAGS.lock().unwrap().logging_debug_enabled_for_all = false;
assert!(!is_debug_logging_enabled_for_tag("foo"));
assert!(is_debug_logging_enabled_for_tag("bar"));
assert!(is_debug_logging_enabled_for_tag("bar2"));
assert!(!is_debug_logging_enabled_for_tag("unknown_flag"));
assert!(!logging_debug_enabled_for_all_is_enabled());
}
}
|