use crc::{CRC_32_ISCSI, Crc};
use zerocopy::{
FromBytes, Immutable, IntoBytes, KnownLayout, Unaligned,
little_endian::{U32, U64},
};
pub const SECTOR_MAGIC: [u8; 4] = *b"SD05";
pub const SECTOR_FORMAT_VERSION: u8 = 5;
pub const SECTOR_HEADER_SIZE: usize = 28;
const SECTOR_HEADER_CRC_COVERAGE: usize = SECTOR_HEADER_SIZE - 4;
const SECTOR_CRC: Crc<u32> = Crc::<u32>::new(&CRC_32_ISCSI);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct LogId(core::num::NonZeroU32);
impl LogId {
pub const fn new(raw: u32) -> Option<Self> {
match core::num::NonZeroU32::new(raw) {
Some(v) => Some(Self(v)),
None => None,
}
}
pub const fn get(self) -> u32 {
self.0.get()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum SectorClass {
Valid,
Free,
Foreign {
found: u32,
},
Unsupported {
found: u8,
},
}
#[derive(FromBytes, IntoBytes, KnownLayout, Immutable, Unaligned, Clone, Copy)]
#[repr(C, packed)]
pub struct SectorHeader {
pub magic: [u8; 4],
pub version: u8,
pub reserved: [u8; 3],
pub log_id: U32,
pub epoch: U32,
pub sequence: U32,
pub erase_count: U32,
pub header_crc: U32,
}
impl SectorHeader {
pub fn new(log_id: Option<LogId>, epoch: u32, sequence: u32, erase_count: u32) -> Self {
let mut header = Self {
magic: SECTOR_MAGIC,
version: SECTOR_FORMAT_VERSION,
reserved: [0; 3],
log_id: U32::new(log_id.map_or(0, LogId::get)),
epoch: U32::new(epoch),
sequence: U32::new(sequence),
erase_count: U32::new(erase_count),
header_crc: U32::new(0),
};
header.header_crc = U32::new(header.compute_crc());
header
}
fn compute_crc(&self) -> u32 {
SECTOR_CRC.checksum(&self.as_bytes()[..SECTOR_HEADER_CRC_COVERAGE])
}
pub fn is_valid(&self) -> bool {
self.magic == SECTOR_MAGIC
&& self.version == SECTOR_FORMAT_VERSION
&& self.header_crc.get() == self.compute_crc()
}
pub fn classify(&self, expected: Option<LogId>) -> SectorClass {
if self.magic != SECTOR_MAGIC || self.header_crc.get() != self.compute_crc() {
return SectorClass::Free;
}
if self.version != SECTOR_FORMAT_VERSION {
return SectorClass::Unsupported {
found: self.version,
};
}
let found = self.log_id.get();
match expected {
None => SectorClass::Valid,
Some(id) if found == 0 || found == id.get() => SectorClass::Valid,
_ => SectorClass::Foreign { found },
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum SectorState {
#[default]
Erased,
Active,
Sealed,
}
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct SectorSummary {
pub state: SectorState,
pub sequence: u32,
pub entry_count: u32,
pub sample_count: u32,
pub write_offset: u32,
pub min_timestamp: u64,
pub max_timestamp: u64,
pub sensor_bloom: u64,
pub erase_count: u32,
}
impl Default for SectorSummary {
#[allow(clippy::derivable_impls)]
fn default() -> Self {
Self {
state: SectorState::Erased,
sequence: 0,
entry_count: 0,
sample_count: 0,
write_offset: 0,
min_timestamp: u64::MAX,
max_timestamp: 0,
sensor_bloom: 0,
erase_count: 0,
}
}
}
pub fn sensor_id_to_bit(sensor_id: u16) -> u64 {
let hash = (sensor_id as u32).wrapping_mul(0x9E37_79B9);
1u64 << ((hash >> 26) & 0x3F)
}
impl SectorSummary {
pub fn erased() -> Self {
Self::default()
}
pub fn overlaps_time_range(&self, t_start: u64, t_end: u64) -> bool {
if self.entry_count == 0 {
return false;
}
t_start <= self.max_timestamp && t_end >= self.min_timestamp
}
pub fn record_sensor(&mut self, sensor_id: u16) {
self.sensor_bloom |= sensor_id_to_bit(sensor_id);
}
pub fn might_contain_sensor(&self, sensor_id: u16) -> bool {
self.sensor_bloom & sensor_id_to_bit(sensor_id) != 0
}
pub fn record_span(&mut self, first: u64, last: u64) {
self.update_timestamps(first);
self.update_timestamps(last);
}
pub fn update_timestamps(&mut self, timestamp: u64) {
if timestamp < self.min_timestamp {
self.min_timestamp = timestamp;
}
if timestamp > self.max_timestamp {
self.max_timestamp = timestamp;
}
}
}
pub const SECTOR_FOOTER_MAGIC: [u8; 4] = *b"SDF2";
pub const SECTOR_FOOTER_VERSION: u8 = 2;
pub const SECTOR_FOOTER_SIZE: usize = 52;
const SECTOR_FOOTER_CRC_COVERAGE: usize = SECTOR_FOOTER_SIZE - 4;
const _: () = assert!(core::mem::size_of::<SectorFooter>() == SECTOR_FOOTER_SIZE);
#[derive(FromBytes, IntoBytes, KnownLayout, Immutable, Unaligned, Clone, Copy)]
#[repr(C, packed)]
pub struct SectorFooter {
pub magic: [u8; 4],
pub version: u8,
pub reserved: [u8; 3],
pub sequence: U32,
pub entry_count: U32,
pub sample_count: U32,
pub write_offset: U32,
pub min_timestamp: U64,
pub max_timestamp: U64,
pub sensor_bloom: U64,
pub footer_crc: U32,
}
impl SectorFooter {
pub fn new(summary: &SectorSummary) -> Self {
let mut footer = Self {
magic: SECTOR_FOOTER_MAGIC,
version: SECTOR_FOOTER_VERSION,
reserved: [0; 3],
sequence: U32::new(summary.sequence),
entry_count: U32::new(summary.entry_count),
sample_count: U32::new(summary.sample_count),
write_offset: U32::new(summary.write_offset),
min_timestamp: U64::new(summary.min_timestamp),
max_timestamp: U64::new(summary.max_timestamp),
sensor_bloom: U64::new(summary.sensor_bloom),
footer_crc: U32::new(0),
};
footer.footer_crc = U32::new(footer.compute_crc());
footer
}
fn compute_crc(&self) -> u32 {
SECTOR_CRC.checksum(&self.as_bytes()[..SECTOR_FOOTER_CRC_COVERAGE])
}
pub fn describes(&self, sequence: u32) -> bool {
self.magic == SECTOR_FOOTER_MAGIC
&& self.version == SECTOR_FOOTER_VERSION
&& self.sequence.get() == sequence
&& self.footer_crc.get() == self.compute_crc()
}
pub fn apply_to(&self, summary: &mut SectorSummary) {
summary.entry_count = self.entry_count.get();
summary.sample_count = self.sample_count.get();
summary.write_offset = self.write_offset.get();
summary.min_timestamp = self.min_timestamp.get();
summary.max_timestamp = self.max_timestamp.get();
summary.sensor_bloom = self.sensor_bloom.get();
}
}