Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
21 changes: 21 additions & 0 deletions crates/xtce-flight-e2e/tests/against_interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
60 changes: 60 additions & 0 deletions crates/xtce-flight/src/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1527,10 +1527,12 @@ fn helpers(layout: &Layout) -> TokenStream {
quote!()
};

let crc = crc_helpers();
let errors = errors();

quote! {
#errors
#crc
#half
#find
#powi
Expand All @@ -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<u16> {
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
Expand Down
12 changes: 6 additions & 6 deletions probes/nostd/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions probes/nostd/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand Down
10 changes: 10 additions & 0 deletions probes/nostd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion scripts/check-no-panic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading