-
Notifications
You must be signed in to change notification settings - Fork 1
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 #25 from SamClercky/configurable_constants
Configurable constants
- Loading branch information
Showing
6 changed files
with
146 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use std::collections::HashMap; | ||
use std::env; | ||
use std::fmt::Write; | ||
use std::path::PathBuf; | ||
|
||
fn main() { | ||
// (Variable, Type, Default value) | ||
let mut configs: HashMap<&str, (&str, &str)> = HashMap::from([ | ||
("MAC_MIN_BE", ("u16", "0")), | ||
("MAC_MAX_BE", ("u16", "8")), | ||
("MAC_MAX_CSMA_BACKOFFS", ("u16", "16")), | ||
( | ||
"MAC_UNIT_BACKOFF_DURATION", | ||
( | ||
"Duration", | ||
"Duration::from_us((UNIT_BACKOFF_PERIOD * SYMBOL_RATE_INV_US) as i64)", | ||
), | ||
), | ||
("MAC_MAX_FRAME_RETIES", ("u16", "3")), | ||
( | ||
"CSMA_INTER_FRAME_TIME", | ||
("Duration", "Duration::from_us(1000)"), | ||
), | ||
("MAC_AIFS_PERIOD", ("Duration", "Duration::from_us(1000)")), | ||
("MAC_SIFS_PERIOD", ("Duration", "Duration::from_us(1000)")), | ||
("MAC_LIFS_PERIOD", ("Duration", "Duration::from_us(10_000)")), | ||
]); | ||
|
||
// Make sure we get rerun if needed | ||
println!("cargo:rerun-if-changed=build.rs"); | ||
for name in configs.keys() { | ||
println!("cargo:rerun-if-env-changed=DOT15D4_{name}"); | ||
} | ||
|
||
// Collect environment variables | ||
let mut data = String::new(); | ||
// Write preamble | ||
writeln!(data, "use crate::time::Duration;").unwrap(); | ||
writeln!( | ||
data, | ||
"use crate::csma::{{SYMBOL_RATE_INV_US, UNIT_BACKOFF_PERIOD}};" | ||
) | ||
.unwrap(); | ||
|
||
for (var, value) in std::env::vars() { | ||
if let Some(name) = var.strip_prefix("DOT15D4_") { | ||
// discard from hashmap as a way of consuming the setting | ||
let Some((ty, _)) = configs.remove_entry(name) else { | ||
panic!("Wrong configuration name {name}"); | ||
}; | ||
|
||
// write to file | ||
writeln!(data, "pub const {name}: {ty} = {value};").unwrap(); | ||
} | ||
} | ||
|
||
// Take the remaining configs and write the default value to the file | ||
for (name, (ty, value)) in configs.iter() { | ||
writeln!(data, "pub const {name}: {ty} = {value};").unwrap(); | ||
} | ||
|
||
// Now that we have the code of the configuration, actually write it to a file | ||
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); | ||
let out_file = out_dir.join("config.rs"); | ||
std::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
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 |
---|---|---|
@@ -1,19 +1,29 @@ | ||
#![allow(dead_code)] | ||
/// Export all user definable constants | ||
pub use constants::*; | ||
|
||
use crate::time::Duration; | ||
#[cfg(test)] | ||
mod constants { | ||
#![allow(dead_code)] | ||
use crate::csma::{SYMBOL_RATE_INV_US, UNIT_BACKOFF_PERIOD}; | ||
use crate::time::Duration; | ||
|
||
use super::constants::{SYMBOL_RATE_INV_US, UNIT_BACKOFF_PERIOD}; | ||
// XXX These are just random numbers I picked by fair dice roll; what should | ||
// they be? | ||
pub const MAC_MIN_BE: u16 = 0; | ||
pub const MAC_MAX_BE: u16 = 8; | ||
pub const MAC_MAX_CSMA_BACKOFFS: u16 = 16; | ||
pub const MAC_UNIT_BACKOFF_DURATION: Duration = | ||
Duration::from_us((UNIT_BACKOFF_PERIOD * SYMBOL_RATE_INV_US) as i64); | ||
pub const MAC_MAX_FRAME_RETIES: u16 = 3; // 0-7 | ||
pub const MAC_INTER_FRAME_TIME: Duration = Duration::from_us(1000); // TODO: XXX | ||
/// AIFS=1ms, for SUN PHY, LECIM PHY, TVWS PHY | ||
pub const MAC_AIFS_PERIOD: Duration = Duration::from_us(1000); | ||
pub const MAC_SIFS_PERIOD: Duration = Duration::from_us(1000); // TODO: SIFS=XXX | ||
pub const MAC_LIFS_PERIOD: Duration = Duration::from_us(10_000); // TODO: LIFS=XXX | ||
} | ||
|
||
// XXX These are just random numbers I picked by fair dice roll; what should | ||
// they be? | ||
pub const MAC_MIN_BE: u16 = 0; | ||
pub const MAC_MAX_BE: u16 = 8; | ||
pub const MAC_MAX_CSMA_BACKOFFS: u16 = 16; | ||
pub const MAC_UNIT_BACKOFF_DURATION: Duration = | ||
Duration::from_us((UNIT_BACKOFF_PERIOD * SYMBOL_RATE_INV_US) as i64); | ||
pub const MAC_MAX_FRAME_RETIES: u16 = 3; // 0-7 | ||
pub const _MAC_INTER_FRAME_TIME: Duration = Duration::from_us(1000); // TODO: XXX | ||
/// AIFS=1ms, for SUN PHY, LECIM PHY, TVWS PHY | ||
pub const ACKNOWLEDGEMENT_INTERFRAME_SPACING: Duration = Duration::from_us(1000); | ||
pub const MAC_SIFT_PERIOD: Duration = Duration::from_us(1000); // TODO: SIFS=XXX | ||
pub const MAC_LIFS_PERIOD: Duration = Duration::from_us(10_000); // TODO: LIFS=XXX | ||
#[cfg(not(test))] | ||
mod constants { | ||
#![allow(unused)] | ||
include!(concat!(env!("OUT_DIR"), "/config.rs")); | ||
} |
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