mirror of
https://github.com/ratspeak/rsReticulum
synced 2026-11-27 13:52:16 +00:00
rnode: centralize capability admission
This commit is contained in:
1 parent
52998569fb
commit
01fa84bf1f
2 files changed
+278
-64
No files matched your search
@@ -8,6 +8,8 @@
|
||||
|
||||
use md5::{Digest, Md5};
|
||||
|
||||
use crate::rnode::{RNodeConfigValidationError, RNodeRadioSettings};
|
||||
|
||||
const PRODUCT_ADDRESS: usize = 0x00;
|
||||
const MODEL_ADDRESS: usize = 0x01;
|
||||
const IDENTITY_CHECKSUM_INPUT_END: usize = 0x0B;
|
||||
@@ -140,6 +142,58 @@ pub enum RNodeCapabilityParseError {
|
||||
ChecksumMismatch,
|
||||
}
|
||||
|
||||
/// Model-specific classification for a validated RNode capability image.
|
||||
///
|
||||
/// `Verified` means the exact model has a reviewed capability profile.
|
||||
/// `Unverified` means the exact stored model is unknown or quarantined, so no
|
||||
/// model profile was inferred. A `Verified` classification alone does not
|
||||
/// admit radio settings; use [`admit_rnode_radio_settings`] for that.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
pub enum RNodeRadioAdmission {
|
||||
Verified { product_code: u8, model_code: u8 },
|
||||
Unverified { product_code: u8, model_code: u8 },
|
||||
}
|
||||
|
||||
impl RNodeRadioAdmission {
|
||||
/// Validated EEPROM product code associated with this admission result.
|
||||
pub const fn product_code(self) -> u8 {
|
||||
match self {
|
||||
Self::Verified { product_code, .. } | Self::Unverified { product_code, .. } => {
|
||||
product_code
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Exact, unnormalised EEPROM model code associated with this result.
|
||||
pub const fn model_code(self) -> u8 {
|
||||
match self {
|
||||
Self::Verified { model_code, .. } | Self::Unverified { model_code, .. } => model_code,
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a reviewed model-specific profile was available.
|
||||
pub const fn is_verified(self) -> bool {
|
||||
matches!(self, Self::Verified { .. })
|
||||
}
|
||||
}
|
||||
|
||||
/// Typed failure from model-specific RNode radio admission.
|
||||
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
|
||||
#[non_exhaustive]
|
||||
pub enum RNodeRadioAdmissionError {
|
||||
#[error("generic RNode radio settings are invalid: {0}")]
|
||||
GenericValidation(#[source] RNodeConfigValidationError),
|
||||
#[error("frequency {requested_hz} Hz is outside verified model range {min_hz}..={max_hz} Hz")]
|
||||
FrequencyOutOfRange {
|
||||
requested_hz: u32,
|
||||
min_hz: u32,
|
||||
max_hz: u32,
|
||||
},
|
||||
#[error("TX power {requested_dbm} dBm exceeds verified model maximum {max_dbm} dBm")]
|
||||
TxPowerExceedsMaximum { requested_dbm: u8, max_dbm: u8 },
|
||||
}
|
||||
|
||||
/// Validate an EEPROM image and retain only non-identifying capability data.
|
||||
///
|
||||
/// Validation requires bytes through the identity-information lock at `0x9b`.
|
||||
@@ -175,6 +229,61 @@ pub fn parse_rnode_capabilities(
|
||||
})
|
||||
}
|
||||
|
||||
/// Classify a validated model without admitting radio settings.
|
||||
///
|
||||
/// `capabilities` can only be obtained from a successfully validated EEPROM
|
||||
/// image. Unknown, aliased, ambiguous, and quarantined model codes return
|
||||
/// [`RNodeRadioAdmission::Unverified`] without normalisation or an inferred
|
||||
/// profile.
|
||||
pub fn classify_rnode_radio_capabilities(capabilities: RNodeCapabilities) -> RNodeRadioAdmission {
|
||||
let product_code = capabilities.product_code();
|
||||
let model_code = capabilities.model_code();
|
||||
match capabilities.radio() {
|
||||
RNodeRadioCapabilities::Known(_) => RNodeRadioAdmission::Verified {
|
||||
product_code,
|
||||
model_code,
|
||||
},
|
||||
RNodeRadioCapabilities::Unknown => RNodeRadioAdmission::Unverified {
|
||||
product_code,
|
||||
model_code,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply the sole lower-layer RF admission policy to validated capabilities.
|
||||
///
|
||||
/// Generic RNode validation runs first. Known exact model codes then enforce
|
||||
/// their inclusive frequency range and maximum TX power. Unknown, aliased,
|
||||
/// ambiguous, and quarantined model codes continue as an explicit
|
||||
/// [`RNodeRadioAdmission::Unverified`] result without normalisation or an
|
||||
/// inferred profile.
|
||||
pub fn admit_rnode_radio_settings(
|
||||
capabilities: RNodeCapabilities,
|
||||
settings: RNodeRadioSettings,
|
||||
) -> Result<RNodeRadioAdmission, RNodeRadioAdmissionError> {
|
||||
settings
|
||||
.validate()
|
||||
.map_err(RNodeRadioAdmissionError::GenericValidation)?;
|
||||
|
||||
if let RNodeRadioCapabilities::Known(radio) = capabilities.radio() {
|
||||
if !radio.supports_frequency(settings.frequency) {
|
||||
return Err(RNodeRadioAdmissionError::FrequencyOutOfRange {
|
||||
requested_hz: settings.frequency,
|
||||
min_hz: radio.min_frequency_hz(),
|
||||
max_hz: radio.max_frequency_hz(),
|
||||
});
|
||||
}
|
||||
if !radio.supports_tx_power(settings.tx_power) {
|
||||
return Err(RNodeRadioAdmissionError::TxPowerExceedsMaximum {
|
||||
requested_dbm: settings.tx_power,
|
||||
max_dbm: radio.max_tx_power_dbm(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Ok(classify_rnode_radio_capabilities(capabilities))
|
||||
}
|
||||
|
||||
/// Return the reviewed display name for an RNode product code.
|
||||
///
|
||||
/// Unknown product codes return `None`. Names are static code-table values and
|
||||
@@ -420,6 +529,10 @@ mod tests {
|
||||
bytes
|
||||
}
|
||||
|
||||
fn settings(frequency: u32, tx_power: u8) -> RNodeRadioSettings {
|
||||
RNodeRadioSettings::new(frequency, 125_000, 7, 5, tx_power)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn requires_every_byte_through_info_lock() {
|
||||
let short = &image(0xA4)[..REQUIRED_EEPROM_LEN - 1];
|
||||
@@ -521,6 +634,108 @@ mod tests {
|
||||
assert!(!radio.supports_tx_power(23));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn known_model_admission_includes_frequency_and_power_boundaries() {
|
||||
let capabilities = parse_rnode_capabilities(&image(0x11)).expect("valid known model");
|
||||
|
||||
assert_eq!(
|
||||
admit_rnode_radio_settings(capabilities, settings(430_000_000, 0)),
|
||||
Ok(RNodeRadioAdmission::Verified {
|
||||
product_code: PRODUCT,
|
||||
model_code: 0x11,
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
admit_rnode_radio_settings(capabilities, settings(510_000_000, 22)),
|
||||
Ok(RNodeRadioAdmission::Verified {
|
||||
product_code: PRODUCT,
|
||||
model_code: 0x11,
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
classify_rnode_radio_capabilities(capabilities),
|
||||
RNodeRadioAdmission::Verified {
|
||||
product_code: PRODUCT,
|
||||
model_code: 0x11,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn known_model_admission_returns_typed_frequency_and_power_mismatches() {
|
||||
let capabilities = parse_rnode_capabilities(&image(0x11)).expect("valid known model");
|
||||
|
||||
for (frequency, expected) in [
|
||||
(
|
||||
429_999_999,
|
||||
RNodeRadioAdmissionError::FrequencyOutOfRange {
|
||||
requested_hz: 429_999_999,
|
||||
min_hz: 430_000_000,
|
||||
max_hz: 510_000_000,
|
||||
},
|
||||
),
|
||||
(
|
||||
510_000_001,
|
||||
RNodeRadioAdmissionError::FrequencyOutOfRange {
|
||||
requested_hz: 510_000_001,
|
||||
min_hz: 430_000_000,
|
||||
max_hz: 510_000_000,
|
||||
},
|
||||
),
|
||||
] {
|
||||
assert_eq!(
|
||||
admit_rnode_radio_settings(capabilities, settings(frequency, 22)),
|
||||
Err(expected)
|
||||
);
|
||||
}
|
||||
|
||||
let error = admit_rnode_radio_settings(capabilities, settings(430_000_000, 23));
|
||||
assert_eq!(
|
||||
error,
|
||||
Err(RNodeRadioAdmissionError::TxPowerExceedsMaximum {
|
||||
requested_dbm: 23,
|
||||
max_dbm: 22,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_and_quarantined_admission_is_explicitly_unverified() {
|
||||
for model in std::iter::once(0x00).chain(QUARANTINED_MODELS.iter().copied()) {
|
||||
let capabilities =
|
||||
parse_rnode_capabilities(&image(model)).expect("valid identity image");
|
||||
assert_eq!(
|
||||
classify_rnode_radio_capabilities(capabilities),
|
||||
RNodeRadioAdmission::Unverified {
|
||||
product_code: PRODUCT,
|
||||
model_code: model,
|
||||
},
|
||||
"model {model:#04x} classification must remain unverified"
|
||||
);
|
||||
assert_eq!(
|
||||
admit_rnode_radio_settings(capabilities, settings(868_000_000, 37)),
|
||||
Ok(RNodeRadioAdmission::Unverified {
|
||||
product_code: PRODUCT,
|
||||
model_code: model,
|
||||
}),
|
||||
"model {model:#04x} must remain unverified"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generic_validation_precedes_known_and_unverified_model_policy() {
|
||||
for model in [0x11, 0xA6] {
|
||||
let capabilities = parse_rnode_capabilities(&image(model)).expect("valid identity");
|
||||
let error = admit_rnode_radio_settings(capabilities, settings(1, 23))
|
||||
.expect_err("model policy must not bypass generic validation");
|
||||
assert!(
|
||||
matches!(error, RNodeRadioAdmissionError::GenericValidation(_)),
|
||||
"model {model:#04x} did not fail generic validation first: {error:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn product_names_are_closed_static_lookups() {
|
||||
const PRODUCTS: &[(u8, &str)] = &[
|
||||
|
||||
@@ -14,7 +14,8 @@ use rns_interface::{
|
||||
rnode, rnode_admin,
|
||||
rnode_capabilities::{
|
||||
RNodeCapabilities, RNodeCapabilityParseError, RNodeKnownRadioCapabilities,
|
||||
RNodeRadioCapabilities, RNodeRadioFamily, parse_rnode_capabilities, rnode_product_name,
|
||||
RNodeRadioAdmission, RNodeRadioCapabilities, RNodeRadioFamily, admit_rnode_radio_settings,
|
||||
classify_rnode_radio_capabilities, parse_rnode_capabilities, rnode_product_name,
|
||||
},
|
||||
rnode_protocol::{
|
||||
FREQUENCY_TOLERANCE_HZ, RNodeFirmwareVersion, RNodeProtocolEffect, RNodeProtocolState,
|
||||
@@ -290,7 +291,11 @@ pub(crate) fn main() -> ExitCode {
|
||||
let mut session = SerialFrameSession::new(port);
|
||||
if let Some(mutation) = plan.mutation {
|
||||
match execute_mutation(&mut session, mutation) {
|
||||
Ok(outcome) => println!("{} ({})", outcome.summary(), outcome.verification.summary()),
|
||||
Ok(outcome) => println!(
|
||||
"{} ({})",
|
||||
outcome.summary(),
|
||||
model_verification_summary(outcome.verification)
|
||||
),
|
||||
Err(error) => {
|
||||
eprintln!("rnodeconf-rs: {error}");
|
||||
return ExitCode::from(1);
|
||||
@@ -339,27 +344,6 @@ impl std::fmt::Display for MutationError {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum ModelVerification {
|
||||
Verified { product: u8, model: u8 },
|
||||
Unverified { product: u8, model: u8 },
|
||||
}
|
||||
|
||||
impl ModelVerification {
|
||||
fn summary(self) -> String {
|
||||
match self {
|
||||
Self::Verified { product, model } => {
|
||||
format!(
|
||||
"model-specific limits verified for product 0x{product:02x}, model 0x{model:02x}"
|
||||
)
|
||||
}
|
||||
Self::Unverified { product, model } => format!(
|
||||
"generic RF validation only; model-specific limits unverified for product 0x{product:02x}, model 0x{model:02x}"
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum MutationKind {
|
||||
Tnc,
|
||||
@@ -369,7 +353,7 @@ enum MutationKind {
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
struct MutationOutcome {
|
||||
kind: MutationKind,
|
||||
verification: ModelVerification,
|
||||
verification: RNodeRadioAdmission,
|
||||
}
|
||||
|
||||
impl MutationOutcome {
|
||||
@@ -381,6 +365,18 @@ impl MutationOutcome {
|
||||
}
|
||||
}
|
||||
|
||||
fn model_verification_summary(admission: RNodeRadioAdmission) -> String {
|
||||
let product = admission.product_code();
|
||||
let model = admission.model_code();
|
||||
if admission.is_verified() {
|
||||
format!("model-specific limits verified for product 0x{product:02x}, model 0x{model:02x}")
|
||||
} else {
|
||||
format!(
|
||||
"generic RF validation only; model-specific limits unverified for product 0x{product:02x}, model 0x{model:02x}"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct MutationPreflight {
|
||||
firmware: RNodeFirmwareVersion,
|
||||
@@ -459,8 +455,8 @@ fn execute_tnc<S: FrameSession>(
|
||||
settings: rnode::RNodeRadioSettings,
|
||||
preflight: MutationPreflight,
|
||||
) -> Result<MutationOutcome, MutationError> {
|
||||
let verification = gate_model_capabilities(preflight.capabilities, Some(settings))
|
||||
.map_err(MutationError::Failed)?;
|
||||
let verification = admit_rnode_radio_settings(preflight.capabilities, settings)
|
||||
.map_err(|error| MutationError::Failed(error.to_string()))?;
|
||||
let target = RNodeProtocolTarget::new(
|
||||
settings.frequency,
|
||||
settings.bandwidth,
|
||||
@@ -525,8 +521,7 @@ fn execute_normal<S: FrameSession>(
|
||||
session: &mut S,
|
||||
preflight: MutationPreflight,
|
||||
) -> Result<MutationOutcome, MutationError> {
|
||||
let verification =
|
||||
gate_model_capabilities(preflight.capabilities, None).map_err(MutationError::Failed)?;
|
||||
let verification = classify_rnode_radio_capabilities(preflight.capabilities);
|
||||
let image = persist_then_read(session, rnode::CMD_CONF_DELETE)?;
|
||||
if image.radio_config().is_some() {
|
||||
return Err(MutationError::PersistenceIndeterminate(
|
||||
@@ -539,38 +534,6 @@ fn execute_normal<S: FrameSession>(
|
||||
})
|
||||
}
|
||||
|
||||
fn gate_model_capabilities(
|
||||
capabilities: RNodeCapabilities,
|
||||
settings: Option<rnode::RNodeRadioSettings>,
|
||||
) -> Result<ModelVerification, String> {
|
||||
let product = capabilities.product_code();
|
||||
let model = capabilities.model_code();
|
||||
match capabilities.radio() {
|
||||
RNodeRadioCapabilities::Known(radio) => {
|
||||
if let Some(settings) = settings {
|
||||
if !radio.supports_frequency(settings.frequency) {
|
||||
return Err(format!(
|
||||
"frequency {} Hz is outside verified model range {}..={} Hz",
|
||||
settings.frequency,
|
||||
radio.min_frequency_hz(),
|
||||
radio.max_frequency_hz()
|
||||
));
|
||||
}
|
||||
if !radio.supports_tx_power(settings.tx_power) {
|
||||
return Err(format!(
|
||||
"TX power {} dBm exceeds verified model maximum {} dBm",
|
||||
settings.tx_power,
|
||||
radio.max_tx_power_dbm()
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(ModelVerification::Verified { product, model })
|
||||
}
|
||||
RNodeRadioCapabilities::Unknown => Ok(ModelVerification::Unverified { product, model }),
|
||||
_ => Ok(ModelVerification::Unverified { product, model }),
|
||||
}
|
||||
}
|
||||
|
||||
fn persist_then_read<S: FrameSession>(
|
||||
session: &mut S,
|
||||
persist_command: u8,
|
||||
@@ -1127,8 +1090,15 @@ mod tests {
|
||||
assert_eq!(outcome.kind, MutationKind::Tnc);
|
||||
assert!(matches!(
|
||||
outcome.verification,
|
||||
ModelVerification::Verified { model: 0xB4, .. }
|
||||
RNodeRadioAdmission::Verified {
|
||||
model_code: 0xB4,
|
||||
..
|
||||
}
|
||||
));
|
||||
assert_eq!(
|
||||
model_verification_summary(outcome.verification),
|
||||
"model-specific limits verified for product 0x03, model 0xb4"
|
||||
);
|
||||
assert_eq!(
|
||||
session.commands(),
|
||||
vec![
|
||||
@@ -1389,7 +1359,30 @@ mod tests {
|
||||
let unsupported = settings(868_000_000);
|
||||
let mut known = ScriptedSession::from_frames(preflight_frames(0xB4));
|
||||
let error = execute_mutation(&mut known, MutationPlan::Tnc(unsupported)).unwrap_err();
|
||||
assert!(matches!(error, MutationError::Failed(_)));
|
||||
assert_eq!(
|
||||
error,
|
||||
MutationError::Failed(
|
||||
"frequency 868000000 Hz is outside verified model range 420000000..=520000000 Hz"
|
||||
.to_string()
|
||||
)
|
||||
);
|
||||
assert_no_save(&known);
|
||||
assert!(
|
||||
known
|
||||
.sent
|
||||
.iter()
|
||||
.all(|request| request.command != rnode::CMD_FREQUENCY)
|
||||
);
|
||||
|
||||
let excessive_power = rnode::RNodeRadioSettings::new(433_000_000, 125_000, 7, 5, 18);
|
||||
let mut known = ScriptedSession::from_frames(preflight_frames(0xB4));
|
||||
let error = execute_mutation(&mut known, MutationPlan::Tnc(excessive_power)).unwrap_err();
|
||||
assert_eq!(
|
||||
error,
|
||||
MutationError::Failed(
|
||||
"TX power 18 dBm exceeds verified model maximum 17 dBm".to_string()
|
||||
)
|
||||
);
|
||||
assert_no_save(&known);
|
||||
assert!(
|
||||
known
|
||||
@@ -1406,9 +1399,15 @@ mod tests {
|
||||
let outcome = execute_mutation(&mut quarantined, MutationPlan::Tnc(unsupported)).unwrap();
|
||||
assert!(matches!(
|
||||
outcome.verification,
|
||||
ModelVerification::Unverified { model: 0xA6, .. }
|
||||
RNodeRadioAdmission::Unverified {
|
||||
model_code: 0xA6,
|
||||
..
|
||||
}
|
||||
));
|
||||
assert!(outcome.verification.summary().contains("unverified"));
|
||||
assert_eq!(
|
||||
model_verification_summary(outcome.verification),
|
||||
"generic RF validation only; model-specific limits unverified for product 0x03, model 0xa6"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user