pub struct CrcSpacePacket<'a, 'b> { /* private fields */ }Expand description
A wrapper around a SpacePacket that automatically manages
a trailing CRC-16 checksum.
Implementations§
Source§impl<'a, 'b> CrcSpacePacket<'a, 'b>
impl<'a, 'b> CrcSpacePacket<'a, 'b>
Sourcepub fn new(
buffer: &'a mut [u8],
apid: Apid,
packet_type: PacketType,
sequence_count: SequenceCount,
secondary_header_flag: SecondaryHeaderFlag,
sequence_flag: SequenceFlag,
data_field_len: u16,
crc_alg: &'b Crc<u16>,
) -> Result<CrcSpacePacket<'a, 'b>, CrcError>
pub fn new( buffer: &'a mut [u8], apid: Apid, packet_type: PacketType, sequence_count: SequenceCount, secondary_header_flag: SecondaryHeaderFlag, sequence_flag: SequenceFlag, data_field_len: u16, crc_alg: &'b Crc<u16>, ) -> Result<CrcSpacePacket<'a, 'b>, CrcError>
Creates a new CRC-protected Space Packet in the provided buffer.
Sourcepub fn set_data<T: SpacePacketData>(
&mut self,
data: &T,
) -> Result<(), DataFieldError>
pub fn set_data<T: SpacePacketData>( &mut self, data: &T, ) -> Result<(), DataFieldError>
Writes data to the packet’s data field and automatically updates the CRC.
This is the safe, CRC-aware way to set the packet’s payload.
Sourcepub fn data_as<T: SpacePacketData>(&self) -> Result<&T, CrcError>
pub fn data_as<T: SpacePacketData>(&self) -> Result<&T, CrcError>
Validates the CRC and returns a typed, zero-copy view of the data field.
Sourcepub fn data(&self) -> Result<&[u8], CrcError>
pub fn data(&self) -> Result<&[u8], CrcError>
Validates the CRC and returns an immutable slice of the data field.
Sourcepub fn validate(&self) -> Result<(), CrcError>
pub fn validate(&self) -> Result<(), CrcError>
Validates the current CRC against the packet’s contents.
Sourcepub fn update_crc(&mut self)
pub fn update_crc(&mut self)
Forces a recalculation and update of the CRC value.
This is called automatically by set_data_field.
Methods from Deref<Target = SpacePacket>§
Sourcepub fn set_data_field<T: SpacePacketData>(
&mut self,
data: &T,
) -> Result<(), DataFieldError>
pub fn set_data_field<T: SpacePacketData>( &mut self, data: &T, ) -> Result<(), DataFieldError>
Copies a user-defined data structure into the packet’s data field.
The size of T must exactly match the length of the data field.
Sourcepub fn data_as<T: SpacePacketData>(&self) -> Result<&T, DataFieldError>
pub fn data_as<T: SpacePacketData>(&self) -> Result<&T, DataFieldError>
Returns a zero-copy, typed view of the packet’s data field.
This is the primary method for interpreting the packet’s payload as a
specific data structure. It will fail if the size of T does not match
the data field’s length.
Sourcepub fn data_field(&self) -> &[u8] ⓘ
pub fn data_field(&self) -> &[u8] ⓘ
Returns an immutable slice of the packet’s data field.
Sourcepub fn data_field_mut(&mut self) -> &mut [u8] ⓘ
pub fn data_field_mut(&mut self) -> &mut [u8] ⓘ
Returns a mutable slice of the packet’s data field.
Warning: Modifying the data field directly will invalidate any
CRC checksum. If using a CrcSpacePacket, prefer the safe set_data()
method instead.
Methods from Deref<Target = PrimaryHeader>§
Sourcepub fn version(&self) -> PacketVersion
pub fn version(&self) -> PacketVersion
Returns the 3-bit packet version number.
Sourcepub fn set_version(&mut self, version: PacketVersion)
pub fn set_version(&mut self, version: PacketVersion)
Sets the 3-bit packet version number.
Sourcepub fn packet_type(&self) -> PacketType
pub fn packet_type(&self) -> PacketType
Returns the PacketType (Telemetry or Telecommand).
Sourcepub fn set_packet_type(&mut self, packet_type: PacketType)
pub fn set_packet_type(&mut self, packet_type: PacketType)
Sets the PacketType (Telemetry or Telecommand).
Sourcepub fn secondary_header_flag(&self) -> SecondaryHeaderFlag
pub fn secondary_header_flag(&self) -> SecondaryHeaderFlag
Returns the SecondaryHeader flag (Present or Absent).
Sourcepub fn set_secondary_header_flag(&mut self, flag: SecondaryHeaderFlag)
pub fn set_secondary_header_flag(&mut self, flag: SecondaryHeaderFlag)
Sets the SecondaryHeader flag (Present or Absent).
Sourcepub fn sequence_flag(&self) -> SequenceFlag
pub fn sequence_flag(&self) -> SequenceFlag
Returns the 2-bit SequenceFlag.
Sourcepub fn set_sequence_flag(&mut self, flag: SequenceFlag)
pub fn set_sequence_flag(&mut self, flag: SequenceFlag)
Sets the 2-bit SequenceFlag.
Sourcepub fn sequence_count(&self) -> SequenceCount
pub fn sequence_count(&self) -> SequenceCount
Returns the 14-bit packet sequence count.
Sourcepub fn set_sequence_count(&mut self, count: SequenceCount)
pub fn set_sequence_count(&mut self, count: SequenceCount)
Sets the 14-bit SequenceCount.
Sourcepub fn data_field_len(&self) -> usize
pub fn data_field_len(&self) -> usize
Returns the length of the data field in bytes as specified by the header.
Sourcepub fn set_data_field_len(&mut self, len: u16)
pub fn set_data_field_len(&mut self, len: u16)
Sets the length of the data field in bytes. The value written to the header
will be len - 1 as per the CCSDS standard.
Sourcepub fn packet_len(&self) -> usize
pub fn packet_len(&self) -> usize
Returns the total length of the packet (header + data field) in bytes.
Sourcepub fn cfe_msg_id(&self) -> u16
pub fn cfe_msg_id(&self) -> u16
Reconstructs the CFE-style Message ID (MsgId) from the primary header fields.
This is a crucial convenience function for systems that interact with the cFE
Software Bus (SB). The SB uses a single integer MsgId for routing, which is
a composite value created from several fields in the CCSDS header.
A cFE MsgId is 16-bit integer with the following structure:
+-----------------+------+-----------------------------------------+
| Field | Size | Description |
+-----------------+------+-----------------------------------------+
| APID | 11 | The 11-bit Application Process ID. |
| SB Flag | 1 | 1 indicates a Software Bus message. |
| Type | 1 | 0 for Telemetry, 1 for Telecommand. |
| Reserved | 3 | Unused, should be zero. |
+-----------------+------+-----------------------------------------+