use crc::{CRC_16_IBM_SDLC, CRC_32_ISCSI, Crc};
use zerocopy::{
FromBytes, Immutable, IntoBytes, KnownLayout, Unaligned,
little_endian::{U16, U64},
};
pub const ENTRY_MARKER: u8 = 0xAA;
pub const ENTRY_MARKER_RUN: u8 = 0xAB;
pub const ENTRY_HEADER_SIZE: usize = 16;
pub const ENTRY_HEADER_FIELDS_SIZE: usize = 14;
pub const ENTRY_HEADER_CRC_SIZE: usize = 2;
pub const ENTRY_CRC_SIZE: usize = 4;
pub const ENTRY_OVERHEAD: usize = ENTRY_HEADER_SIZE + ENTRY_CRC_SIZE;
pub const FLAGS_UNCOMMITTED: u8 = 0xFF;
pub const FLAGS_COMMITTED: u8 = 0x00;
const HEADER_CRC: Crc<u16> = Crc::<u16>::new(&CRC_16_IBM_SDLC);
#[derive(FromBytes, IntoBytes, KnownLayout, Immutable, Unaligned, Clone, Copy)]
#[repr(C, packed)]
pub struct EntryHeaderWire {
pub marker: u8,
pub flags: u8,
pub sensor_id: U16,
pub timestamp: U64,
pub payload_len: U16,
pub header_crc: U16,
}
impl EntryHeaderWire {
pub fn new(sensor_id: u16, timestamp: u64, payload_len: u16) -> Self {
Self::with_marker(ENTRY_MARKER, sensor_id, timestamp, payload_len)
}
pub fn new_run(sensor_id: u16, base_timestamp: u64, payload_len: u16) -> Self {
Self::with_marker(ENTRY_MARKER_RUN, sensor_id, base_timestamp, payload_len)
}
fn with_marker(marker: u8, sensor_id: u16, timestamp: u64, payload_len: u16) -> Self {
let mut header = Self {
marker,
flags: FLAGS_UNCOMMITTED,
sensor_id: U16::new(sensor_id),
timestamp: U64::new(timestamp),
payload_len: U16::new(payload_len),
header_crc: U16::new(0),
};
header.header_crc = U16::new(header.compute_header_crc());
header
}
pub fn compute_header_crc(&self) -> u16 {
let mut bytes = [0u8; ENTRY_HEADER_FIELDS_SIZE];
bytes.copy_from_slice(&self.as_bytes()[..ENTRY_HEADER_FIELDS_SIZE]);
bytes[1] = FLAGS_UNCOMMITTED; HEADER_CRC.checksum(&bytes)
}
pub fn header_crc_valid(&self) -> bool {
self.header_crc.get() == self.compute_header_crc()
}
pub fn is_valid_marker(&self) -> bool {
is_entry_marker(self.marker)
}
pub fn is_run(&self) -> bool {
self.marker == ENTRY_MARKER_RUN
}
pub fn to_header(&self) -> EntryHeader {
EntryHeader {
sensor_id: self.sensor_id.get(),
timestamp: self.timestamp.get(),
payload_len: self.payload_len.get(),
kind: if self.is_run() {
EntryKind::Run
} else {
EntryKind::Single
},
}
}
}
pub fn is_entry_marker(byte: u8) -> bool {
byte == ENTRY_MARKER || byte == ENTRY_MARKER_RUN
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum EntryKind {
#[default]
Single,
Run,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EntryHeader {
pub sensor_id: u16,
pub timestamp: u64,
pub payload_len: u16,
pub kind: EntryKind,
}
impl EntryHeader {
pub fn is_run(&self) -> bool {
matches!(self.kind, EntryKind::Run)
}
}
pub fn compute_entry_crc(header: &EntryHeaderWire, payload: &[u8], offset_in_sector: u32) -> u32 {
const CRC: Crc<u32> = Crc::<u32>::new(&CRC_32_ISCSI);
let mut digest = CRC.digest();
digest.update(&offset_in_sector.to_le_bytes());
let mut header_bytes = [0u8; ENTRY_HEADER_FIELDS_SIZE];
header_bytes.copy_from_slice(&header.as_bytes()[..ENTRY_HEADER_FIELDS_SIZE]);
header_bytes[1] = FLAGS_UNCOMMITTED; digest.update(&header_bytes);
digest.update(payload);
digest.finalize()
}