-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from embassy-rs/l2cap-features
add features for l2cap pool size and queue len
- Loading branch information
Showing
15 changed files
with
326 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use std::collections::HashMap; | ||
use std::fmt::Write; | ||
use std::path::PathBuf; | ||
use std::{env, fs}; | ||
|
||
static CONFIGS: &[(&str, usize)] = &[ | ||
// BEGIN AUTOGENERATED CONFIG FEATURES | ||
// Generated by gen_config.py. DO NOT EDIT. | ||
("L2CAP_RX_QUEUE_SIZE", 1), | ||
("L2CAP_RX_PACKET_POOL_SIZE", 2), | ||
// END AUTOGENERATED CONFIG FEATURES | ||
]; | ||
|
||
struct ConfigState { | ||
value: usize, | ||
seen_feature: bool, | ||
seen_env: bool, | ||
} | ||
|
||
fn main() { | ||
let crate_name = env::var("CARGO_PKG_NAME") | ||
.unwrap() | ||
.to_ascii_uppercase() | ||
.replace('-', "_"); | ||
|
||
// only rebuild if build.rs changed. Otherwise Cargo will rebuild if any | ||
// other file changed. | ||
println!("cargo:rerun-if-changed=build.rs"); | ||
|
||
// Rebuild if config envvar changed. | ||
for (name, _) in CONFIGS { | ||
println!("cargo:rerun-if-env-changed={crate_name}_{name}"); | ||
} | ||
|
||
let mut configs = HashMap::new(); | ||
for (name, default) in CONFIGS { | ||
configs.insert( | ||
*name, | ||
ConfigState { | ||
value: *default, | ||
seen_env: false, | ||
seen_feature: false, | ||
}, | ||
); | ||
} | ||
|
||
let prefix = format!("{crate_name}_"); | ||
for (var, value) in env::vars() { | ||
if let Some(name) = var.strip_prefix(&prefix) { | ||
let Some(cfg) = configs.get_mut(name) else { | ||
panic!("Unknown env var {name}") | ||
}; | ||
|
||
let Ok(value) = value.parse::<usize>() else { | ||
panic!("Invalid value for env var {name}: {value}") | ||
}; | ||
|
||
cfg.value = value; | ||
cfg.seen_env = true; | ||
} | ||
|
||
if let Some(feature) = var.strip_prefix("CARGO_FEATURE_") { | ||
if let Some(i) = feature.rfind('_') { | ||
let name = &feature[..i]; | ||
let value = &feature[i + 1..]; | ||
if let Some(cfg) = configs.get_mut(name) { | ||
let Ok(value) = value.parse::<usize>() else { | ||
panic!("Invalid value for feature {name}: {value}") | ||
}; | ||
|
||
// envvars take priority. | ||
if !cfg.seen_env { | ||
if cfg.seen_feature { | ||
panic!("multiple values set for feature {}: {} and {}", name, cfg.value, value); | ||
} | ||
|
||
cfg.value = value; | ||
cfg.seen_feature = true; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
let mut data = String::new(); | ||
|
||
for (name, cfg) in &configs { | ||
writeln!(&mut data, "pub const {}: usize = {};", name, cfg.value).unwrap(); | ||
} | ||
|
||
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); | ||
let out_file = out_dir.join("config.rs").to_string_lossy().to_string(); | ||
fs::write(out_file, data).unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import os | ||
|
||
abspath = os.path.abspath(__file__) | ||
dname = os.path.dirname(abspath) | ||
os.chdir(dname) | ||
|
||
features = [] | ||
|
||
|
||
def feature(name, default, min=None, max=None, pow2=None, vals=None, factors=[]): | ||
if vals is None: | ||
assert min is not None | ||
assert max is not None | ||
|
||
vals = set() | ||
val = min | ||
while val <= max: | ||
vals.add(val) | ||
for f in factors: | ||
if val*f <= max: | ||
vals.add(val*f) | ||
if (pow2 == True or (isinstance(pow2, int) and val >= pow2)) and val > 0: | ||
val *= 2 | ||
else: | ||
val += 1 | ||
vals.add(default) | ||
vals = sorted(list(vals)) | ||
|
||
features.append( | ||
{ | ||
"name": name, | ||
"default": default, | ||
"vals": vals, | ||
} | ||
) | ||
|
||
|
||
feature("l2cap_rx_queue_size", default=1, min=1, max=64, pow2=True) | ||
feature("l2cap_rx_packet_pool_size", default=2, min=1, max=512, pow2=True) | ||
|
||
# ========= Update Cargo.toml | ||
|
||
things = "" | ||
for f in features: | ||
name = f["name"].replace("_", "-") | ||
for val in f["vals"]: | ||
things += f"{name}-{val} = []" | ||
if val == f["default"]: | ||
things += " # Default" | ||
things += "\n" | ||
things += "\n" | ||
|
||
SEPARATOR_START = "# BEGIN AUTOGENERATED CONFIG FEATURES\n" | ||
SEPARATOR_END = "# END AUTOGENERATED CONFIG FEATURES\n" | ||
HELP = "# Generated by gen_config.py. DO NOT EDIT.\n" | ||
with open("Cargo.toml", "r") as f: | ||
data = f.read() | ||
before, data = data.split(SEPARATOR_START, maxsplit=1) | ||
_, after = data.split(SEPARATOR_END, maxsplit=1) | ||
data = before + SEPARATOR_START + HELP + things + SEPARATOR_END + after | ||
with open("Cargo.toml", "w") as f: | ||
f.write(data) | ||
|
||
|
||
# ========= Update build.rs | ||
|
||
things = "" | ||
for f in features: | ||
name = f["name"].upper() | ||
things += f' ("{name}", {f["default"]}),\n' | ||
|
||
SEPARATOR_START = "// BEGIN AUTOGENERATED CONFIG FEATURES\n" | ||
SEPARATOR_END = "// END AUTOGENERATED CONFIG FEATURES\n" | ||
HELP = " // Generated by gen_config.py. DO NOT EDIT.\n" | ||
with open("build.rs", "r") as f: | ||
data = f.read() | ||
before, data = data.split(SEPARATOR_START, maxsplit=1) | ||
_, after = data.split(SEPARATOR_END, maxsplit=1) | ||
data = before + SEPARATOR_START + HELP + \ | ||
things + " " + SEPARATOR_END + after | ||
with open("build.rs", "w") as f: | ||
f.write(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//! Compile-time configuration. | ||
//! | ||
//! `trouble` has some configuration settings that are set at compile time. | ||
//! | ||
//! They can be set in two ways: | ||
//! | ||
//! - Via Cargo features: enable a feature like `<name>-<value>`. `name` must be in lowercase and | ||
//! use dashes instead of underscores. For example. `max-page-count-1024`. Only a selection of values | ||
//! is available, check `Cargo.toml` for the list. | ||
//! - Via environment variables at build time: set the variable named `TROUBLE_HOST_<value>`. For example | ||
//! `TROUBLE_HOST_L2CAP_RX_QUEUE_SIZE=1 cargo build`. You can also set them in the `[env]` section of `.cargo/config.toml`. | ||
//! Any value can be set, unlike with Cargo features. | ||
//! | ||
//! Environment variables take precedence over Cargo features. If two Cargo features are enabled for the same setting | ||
//! with different values, compilation fails. | ||
//! | ||
//! ## Compatibility warning | ||
//! | ||
//! Changing ANY of these configuration settings changes the on-disk format of the database. If you change | ||
//! them, you won't be able to read databases written with a different configuration. | ||
//! | ||
//! Currently, mounting doesn't check the on-disk database uses the same configuration. Mounting a database | ||
//! with a different configuration might succeed and then cause fun errors later, perhaps very rarely. | ||
//! Always remember to format your flash device every time you change them. | ||
mod raw { | ||
#![allow(unused)] | ||
include!(concat!(env!("OUT_DIR"), "/config.rs")); | ||
} | ||
|
||
// ======== L2CAP parameters | ||
|
||
/// L2CAP RX queue size | ||
/// | ||
/// This is the rx queue size of every l2cap channel. Every channel have to be able | ||
/// to buffer at least 1 packet, but if the controller already does buffering this | ||
/// may be sufficient. | ||
/// | ||
/// If the controller does not support rx buffering, increasing this value will allow | ||
/// a higher throughput between the controller and host. | ||
/// | ||
/// Default: 1. | ||
pub const L2CAP_RX_QUEUE_SIZE: usize = raw::L2CAP_RX_QUEUE_SIZE; | ||
|
||
/// L2CAP RX packet pool size | ||
/// | ||
/// This is the rx packet pool size of every l2cap channel. There has to be at least | ||
/// 1 packet that can be allocated, but the pool is shared among different channels. | ||
/// | ||
/// If the rx queue size is adjusted, consider adjusting the rx packet pool size as well, | ||
/// taking the number of channels and per-channel queue size into account. | ||
/// | ||
/// Ensuring fair access to the pool is done configuring the QoS policy when creating | ||
/// the host resources. | ||
/// | ||
/// Default: 1. | ||
pub const L2CAP_RX_PACKET_POOL_SIZE: usize = raw::L2CAP_RX_PACKET_POOL_SIZE; |
Oops, something went wrong.