diff --git a/Cargo.lock b/Cargo.lock index 5b846b6..cf44245 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -236,8 +236,8 @@ dependencies = [ [[package]] name = "xtce-codegen" -version = "0.1.1" -source = "git+https://github.com/RustSpaceLab/xtce-rs?rev=17671eaf4ea8aa9fb17c096001b39ecd88e77059#17671eaf4ea8aa9fb17c096001b39ecd88e77059" +version = "0.2.1" +source = "git+https://github.com/RustSpaceLab/xtce-rs?rev=158e120d4e2bbe74425fb5c4d09187233cd85143#158e120d4e2bbe74425fb5c4d09187233cd85143" dependencies = [ "prettyplease", "proc-macro2", @@ -249,8 +249,8 @@ dependencies = [ [[package]] name = "xtce-decode" -version = "0.1.1" -source = "git+https://github.com/RustSpaceLab/xtce-rs?rev=17671eaf4ea8aa9fb17c096001b39ecd88e77059#17671eaf4ea8aa9fb17c096001b39ecd88e77059" +version = "0.2.1" +source = "git+https://github.com/RustSpaceLab/xtce-rs?rev=158e120d4e2bbe74425fb5c4d09187233cd85143#158e120d4e2bbe74425fb5c4d09187233cd85143" dependencies = [ "thiserror", "xtce-model", @@ -258,7 +258,7 @@ dependencies = [ [[package]] name = "xtce-flight" -version = "0.1.1" +version = "0.2.1" dependencies = [ "clap", "prettyplease", @@ -272,7 +272,7 @@ dependencies = [ [[package]] name = "xtce-flight-e2e" -version = "0.1.1" +version = "0.2.1" dependencies = [ "xtce-decode", "xtce-flight", @@ -281,8 +281,8 @@ dependencies = [ [[package]] name = "xtce-model" -version = "0.1.1" -source = "git+https://github.com/RustSpaceLab/xtce-rs?rev=17671eaf4ea8aa9fb17c096001b39ecd88e77059#17671eaf4ea8aa9fb17c096001b39ecd88e77059" +version = "0.2.1" +source = "git+https://github.com/RustSpaceLab/xtce-rs?rev=158e120d4e2bbe74425fb5c4d09187233cd85143#158e120d4e2bbe74425fb5c4d09187233cd85143" dependencies = [ "quick-xml", "thiserror", diff --git a/Cargo.toml b/Cargo.toml index 16d7890..fd62f76 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ members = ["crates/*"] exclude = ["probes/nostd"] [workspace.package] -version = "0.1.1" +version = "0.2.1" edition = "2024" rust-version = "1.85" license = "MIT OR Apache-2.0" @@ -15,13 +15,13 @@ authors = ["Maciej Myszkiewicz"] [workspace.dependencies] # Without the CLI: a build script that generates code has no use for an argument parser. -xtce-flight = { path = "crates/xtce-flight", version = "0.1.1", default-features = false } +xtce-flight = { path = "crates/xtce-flight", version = "0.2.1", default-features = false } # Pinned to a revision, not a branch. `xtce_codegen::plan` is this project's input format; # a moving dependency would mean an unrelated commit there breaks a build here. -xtce-model = { git = "https://github.com/RustSpaceLab/xtce-rs", rev = "17671eaf4ea8aa9fb17c096001b39ecd88e77059" } -xtce-codegen = { git = "https://github.com/RustSpaceLab/xtce-rs", rev = "17671eaf4ea8aa9fb17c096001b39ecd88e77059" } -xtce-decode = { git = "https://github.com/RustSpaceLab/xtce-rs", rev = "17671eaf4ea8aa9fb17c096001b39ecd88e77059" } +xtce-model = { git = "https://github.com/RustSpaceLab/xtce-rs", rev = "158e120d4e2bbe74425fb5c4d09187233cd85143" } +xtce-codegen = { git = "https://github.com/RustSpaceLab/xtce-rs", rev = "158e120d4e2bbe74425fb5c4d09187233cd85143" } +xtce-decode = { git = "https://github.com/RustSpaceLab/xtce-rs", rev = "158e120d4e2bbe74425fb5c4d09187233cd85143" } clap = { version = "4", features = ["derive"] } prettyplease = "0.2" diff --git a/crates/xtce-flight-e2e/tests/against_interpreter.rs b/crates/xtce-flight-e2e/tests/against_interpreter.rs index 4c41e12..d51518a 100644 --- a/crates/xtce-flight-e2e/tests/against_interpreter.rs +++ b/crates/xtce-flight-e2e/tests/against_interpreter.rs @@ -992,3 +992,24 @@ fn aggregates_survive_the_interpreter() { let checked = check!(aggregates, "aggregates.xml", None::<&str>, 256u64); assert!(checked > 4_000, "only {checked} field(s) compared"); } + +#[test] +fn emitted_ecss_crc16_ccitt_stamps_and_verifies_packets() { + use flight_shapes::flight::{crc16_ccitt, stamp_crc16_ccitt, verify_crc16_ccitt}; + + assert_eq!(crc16_ccitt(b""), 0xFFFF); + assert_eq!(crc16_ccitt(b"123456789"), 0x29B1); + assert!(!verify_crc16_ccitt(&[])); + assert!(!verify_crc16_ccitt(&[0xFF])); + assert_eq!(stamp_crc16_ccitt(&mut []), None); + assert_eq!(stamp_crc16_ccitt(&mut [0x00]), None); + + let mut frame = [b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', 0, 0]; + let crc = stamp_crc16_ccitt(&mut frame).expect("11-byte frame has room for 2-byte PEC"); + assert_eq!(crc, 0x29B1); + assert_eq!(&frame[9..11], &[0x29, 0xB1]); + assert!(verify_crc16_ccitt(&frame)); + + frame[3] ^= 0x01; + assert!(!verify_crc16_ccitt(&frame)); +} diff --git a/crates/xtce-flight/src/emit.rs b/crates/xtce-flight/src/emit.rs index bc9094c..ec83af1 100644 --- a/crates/xtce-flight/src/emit.rs +++ b/crates/xtce-flight/src/emit.rs @@ -1527,10 +1527,12 @@ fn helpers(layout: &Layout) -> TokenStream { quote!() }; + let crc = crc_helpers(); let errors = errors(); quote! { #errors + #crc #half #find #powi @@ -1539,6 +1541,64 @@ fn helpers(layout: &Layout) -> TokenStream { } } +/// ECSS-E-ST-70-41C Annex B / CCSDS 131.0-B CRC-16-CCITT (`0x1021`, initial `0xFFFF`) helpers. +/// +/// Written with slice pattern matching and a fixed 8-step shift register so no runtime bounds +/// check — and therefore no `core::panicking` branch — can survive into bare-metal LLVM IR. +fn crc_helpers() -> TokenStream { + quote! { + /// Computes the 16-bit ECSS-E-ST-70-41C / CCSDS 131.0-B CRC-16-CCITT syndrome + /// (`0x1021`, initial value `0xFFFF`, no reflection, no final XOR) over `bytes`. + /// + /// Over the ASCII check string `b"123456789"` this returns `0x29B1`. + #[must_use] + pub const fn crc16_ccitt(bytes: &[u8]) -> u16 { + let mut crc: u16 = 0xFFFF; + let mut rest = bytes; + while let [byte, tail @ ..] = rest { + crc ^= (*byte as u16) << 8; + let mut bit = 0u8; + while bit < 8 { + if (crc & 0x8000) != 0 { + crc = (crc << 1) ^ 0x1021; + } else { + crc <<= 1; + } + bit += 1; + } + rest = tail; + } + crc + } + + /// Computes the ECSS-E-ST-70-41C CRC-16-CCITT syndrome over all bytes of `packet` + /// preceding the final two octets, stamps the big-endian syndrome into those final two + /// octets, and returns the stamped CRC. + /// + /// Returns `None` if `packet` is shorter than 2 bytes. + pub fn stamp_crc16_ccitt(packet: &mut [u8]) -> Option { + let [body @ .., hi, lo] = packet else { + return None; + }; + let crc = crc16_ccitt(body); + let [msb, lsb] = crc.to_be_bytes(); + *hi = msb; + *lo = lsb; + Some(crc) + } + + /// Returns `true` when `packet` is at least 2 bytes long and its trailing big-endian + /// CRC-16-CCITT syndrome verifies to a zero residue (`0x0000`). + #[must_use] + pub const fn verify_crc16_ccitt(packet: &[u8]) -> bool { + if packet.len() < 2 { + return false; + } + crc16_ccitt(packet) == 0 + } + } +} + /// `base^exponent` as `xtce-decode` computes it for an integral raw value. /// /// Exactly, in `i128`, converted once — not by repeated multiplication, which rounds at every diff --git a/probes/nostd/Cargo.lock b/probes/nostd/Cargo.lock index 579f20a..6249ddb 100644 --- a/probes/nostd/Cargo.lock +++ b/probes/nostd/Cargo.lock @@ -95,8 +95,8 @@ checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" [[package]] name = "xtce-codegen" -version = "0.1.1" -source = "git+https://github.com/RustSpaceLab/xtce-rs?rev=17671eaf4ea8aa9fb17c096001b39ecd88e77059#17671eaf4ea8aa9fb17c096001b39ecd88e77059" +version = "0.2.1" +source = "git+https://github.com/RustSpaceLab/xtce-rs?rev=158e120d4e2bbe74425fb5c4d09187233cd85143#158e120d4e2bbe74425fb5c4d09187233cd85143" dependencies = [ "prettyplease", "proc-macro2", @@ -108,7 +108,7 @@ dependencies = [ [[package]] name = "xtce-flight" -version = "0.1.1" +version = "0.2.1" dependencies = [ "prettyplease", "proc-macro2", @@ -121,7 +121,7 @@ dependencies = [ [[package]] name = "xtce-flight-nostd-probe" -version = "0.1.1" +version = "0.2.1" dependencies = [ "xtce-flight", "xtce-model", @@ -129,8 +129,8 @@ dependencies = [ [[package]] name = "xtce-model" -version = "0.1.1" -source = "git+https://github.com/RustSpaceLab/xtce-rs?rev=17671eaf4ea8aa9fb17c096001b39ecd88e77059#17671eaf4ea8aa9fb17c096001b39ecd88e77059" +version = "0.2.1" +source = "git+https://github.com/RustSpaceLab/xtce-rs?rev=158e120d4e2bbe74425fb5c4d09187233cd85143#158e120d4e2bbe74425fb5c4d09187233cd85143" dependencies = [ "quick-xml", "thiserror", diff --git a/probes/nostd/Cargo.toml b/probes/nostd/Cargo.toml index 5891ff9..336d3b2 100644 --- a/probes/nostd/Cargo.toml +++ b/probes/nostd/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "xtce-flight-nostd-probe" description = "Builds the generated flight code for a bare-metal target, with no std and no panic path" -version = "0.1.1" +version = "0.2.1" edition = "2024" rust-version = "1.85" license = "MIT OR Apache-2.0" @@ -16,7 +16,7 @@ path = "src/lib.rs" [build-dependencies] xtce-flight = { path = "../../crates/xtce-flight", default-features = false } -xtce-model = { git = "https://github.com/RustSpaceLab/xtce-rs", rev = "17671eaf4ea8aa9fb17c096001b39ecd88e77059" } +xtce-model = { git = "https://github.com/RustSpaceLab/xtce-rs", rev = "158e120d4e2bbe74425fb5c4d09187233cd85143" } [profile.release] opt-level = "z" diff --git a/probes/nostd/src/lib.rs b/probes/nostd/src/lib.rs index a1b1dd3..9a0bb31 100644 --- a/probes/nostd/src/lib.rs +++ b/probes/nostd/src/lib.rs @@ -372,3 +372,13 @@ pub fn round_trip_aggregates(out: &mut [u8]) -> bool { Err(_) => false, } } + +/// Stamps and verifies an ECSS-E-ST-70-41C CRC-16-CCITT packet error control syndrome. +#[inline(never)] +#[must_use] +pub fn stamp_and_verify_ecss_crc16(out: &mut [u8]) -> bool { + let Some(crc) = flight_shapes::stamp_crc16_ccitt(out) else { + return false; + }; + crc != 0xFFFF || flight_shapes::verify_crc16_ccitt(out) +} diff --git a/scripts/check-no-panic.sh b/scripts/check-no-panic.sh index b7dd20f..3b84ad9 100755 --- a/scripts/check-no-panic.sh +++ b/scripts/check-no-panic.sh @@ -44,7 +44,7 @@ ir="$(find "${deps}" -name '*.ll' -print -quit)" # The probe's own entry points have to be in there, or the optimiser removed the very code # this is meant to inspect and a clean result would mean nothing. -for symbol in encode_numeric_edges encode_status_report encode_beacon decode_jpss calibrate_all calibrate_in_context round_trip_command round_trip_little_endian round_trip_arrays round_trip_aggregates; do +for symbol in encode_numeric_edges encode_status_report encode_beacon decode_jpss calibrate_all calibrate_in_context round_trip_command round_trip_little_endian round_trip_arrays round_trip_aggregates stamp_and_verify_ecss_crc16; do if ! grep -q "${symbol}" "${ir}"; then echo "${symbol} is not in the emitted IR; the check would pass vacuously" >&2 exit 1