leodos_libcfs/cfe/es/util.rs
1//! Miscellaneous utility wrappers for CFE Executive Services APIs.
2
3use crate::ffi;
4
5/// Specifies the CRC algorithm to use.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7#[repr(u32)]
8pub enum CrcType {
9 /// 16-bit CRC algorithm (CRC-16/ARC). This is the default for cFE.
10 Crc16 = ffi::CFE_ES_CrcType_Enum_CFE_ES_CrcType_16_ARC,
11 // Add other types like Crc8 and Crc32 when they are fully supported and needed.
12}
13
14/// Calculates a cyclic redundancy check (CRC) on a block of memory.
15///
16/// This routine can be used to calculate a CRC on contiguous or non-contiguous blocks of memory.
17///
18/// # Arguments
19/// * `data`: A slice of bytes to calculate the CRC over.
20/// * `input_crc`: A starting value for the CRC calculation. For non-contiguous blocks,
21/// this should be the result of a previous call to this function. For a new calculation,
22/// this should typically be 0.
23/// * `crc_type`: The CRC algorithm to use.
24pub fn calculate_crc(data: &[u8], input_crc: u32, crc_type: CrcType) -> u32 {
25 unsafe {
26 ffi::CFE_ES_CalculateCRC(
27 data.as_ptr() as *const _,
28 data.len(),
29 input_crc,
30 crc_type as ffi::CFE_ES_CrcType_Enum_t,
31 )
32 }
33}