Skip to main content

SpacePacket

Struct SpacePacket 

Source
#[repr(C)]
pub struct SpacePacket { pub primary_header: PrimaryHeader, pub data_field: [u8], }
Expand description

A zero-copy view over a CCSDS Space Packet in a raw byte buffer.

This struct provides a low-level, safe view over a byte slice that is known to contain a valid Space Packet. It can be created via SpacePacket::parse() or through the ergonomic SpacePacket::builder().

The data_field is an unsized [u8] slice, allowing this struct to represent packets of any valid length without needing different types.

+------------------------------------+---------+
| Field Name                         | Size    |
+------------------------------------+---------+
+ -- Primary Header (6 bytes) ------ | ------- |
|                                    |         |
| Packet Version Number              | 3 bits  |
| Packet Identification Field        | 13 bits |
|   - Packet Type                    | 1 bit   |
|   - Secondary Header Flag          | 1 bit   |
|   - APID (Application Process ID)  | 11 bits |
| Packet Sequence Control            | 16 bits |
|   - Sequence Flags                 | 2 bits  |
|   - Sequence Count                 | 14 bits |
| Packet Data Length                 | 16 bits |
|                                    |         |
| -- Packet Data Field (Variable) -- | ------- |
|                                    |         |
| Secondary Header (if present)      |         |
| User Data Field                    | 1-65536 |
|                                    | bytes   |
+------------------------------------+---------+

Fields§

§primary_header: PrimaryHeader

The 6-byte fixed primary header.

§data_field: [u8]

The variable-length data field (secondary header + user data).

Implementations§

Source§

impl SpacePacket

Source

pub fn parse(bytes: &[u8]) -> Result<&Self, ParseError>

Parses (zero-copy) a raw byte slice into a SpacePacket.

This function reads the packet header to determine the packet’s total length and returns a view over that exact portion of the provided slice.

Source

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.

Source

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.

Source

pub fn data_field(&self) -> &[u8]

Returns an immutable slice of the packet’s data field.

Source

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.

Source

pub fn builder<'a, 'b>() -> SpacePacketBuilder<'a, 'b>

Constructs a new SpacePacket in the provided buffer.

Methods from Deref<Target = PrimaryHeader>§

Source

pub fn version(&self) -> PacketVersion

Returns the 3-bit packet version number.

Source

pub fn set_version(&mut self, version: PacketVersion)

Sets the 3-bit packet version number.

Source

pub fn packet_type(&self) -> PacketType

Returns the PacketType (Telemetry or Telecommand).

Source

pub fn set_packet_type(&mut self, packet_type: PacketType)

Sets the PacketType (Telemetry or Telecommand).

Source

pub fn secondary_header_flag(&self) -> SecondaryHeaderFlag

Returns the SecondaryHeader flag (Present or Absent).

Source

pub fn set_secondary_header_flag(&mut self, flag: SecondaryHeaderFlag)

Sets the SecondaryHeader flag (Present or Absent).

Source

pub fn apid(&self) -> Apid

Returns the 11-bit Application Process Identifier (Apid).

Source

pub fn set_apid(&mut self, apid: Apid)

Sets the 11-bit Application Process Identifier (Apid).

Source

pub fn sequence_flag(&self) -> SequenceFlag

Returns the 2-bit SequenceFlag.

Source

pub fn set_sequence_flag(&mut self, flag: SequenceFlag)

Sets the 2-bit SequenceFlag.

Source

pub fn sequence_count(&self) -> SequenceCount

Returns the 14-bit packet sequence count.

Source

pub fn set_sequence_count(&mut self, count: SequenceCount)

Sets the 14-bit SequenceCount.

Source

pub fn data_field_len(&self) -> usize

Returns the length of the data field in bytes as specified by the header.

Source

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.

Source

pub fn packet_len(&self) -> usize

Returns the total length of the packet (header + data field) in bytes.

Source

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.                 |
+-----------------+------+-----------------------------------------+

Trait Implementations§

Source§

impl Debug for SpacePacket

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for SpacePacket

Source§

type Target = PrimaryHeader

Dereferences to the PrimaryHeader to allow direct access to header fields.

Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl DerefMut for SpacePacket

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl FromBytes for SpacePacket
where PrimaryHeader: FromBytes, [u8]: FromBytes,

§

fn ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, Infallible>>
where Self: KnownLayout + Immutable,

Interprets the given source as a &Self. Read more
§

fn ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, Infallible>>
where Self: KnownLayout + Immutable,

Interprets the prefix of the given source as a &Self without copying. Read more
§

fn ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, Infallible>>
where Self: Immutable + KnownLayout,

Interprets the suffix of the given bytes as a &Self. Read more
§

fn mut_from_bytes( source: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, Infallible>>
where Self: IntoBytes + KnownLayout,

Interprets the given source as a &mut Self. Read more
§

fn mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, Infallible>>
where Self: IntoBytes + KnownLayout,

Interprets the prefix of the given source as a &mut Self without copying. Read more
§

fn mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, Infallible>>
where Self: IntoBytes + KnownLayout,

Interprets the suffix of the given source as a &mut Self without copying. Read more
§

fn ref_from_bytes_with_elems( source: &[u8], count: usize, ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, Infallible>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the given source as a &Self with a DST length equal to count. Read more
§

fn ref_from_prefix_with_elems( source: &[u8], count: usize, ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, Infallible>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the prefix of the given source as a DST &Self with length equal to count. Read more
§

fn ref_from_suffix_with_elems( source: &[u8], count: usize, ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, Infallible>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the suffix of the given source as a DST &Self with length equal to count. Read more
§

fn mut_from_bytes_with_elems( source: &mut [u8], count: usize, ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, Infallible>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the given source as a &mut Self with a DST length equal to count. Read more
§

fn mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, Infallible>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize>,

Interprets the prefix of the given source as a &mut Self with DST length equal to count. Read more
§

fn mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, Infallible>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize>,

Interprets the suffix of the given source as a &mut Self with DST length equal to count. Read more
Source§

impl FromZeros for SpacePacket
where PrimaryHeader: FromZeros, [u8]: FromZeros,

§

fn zero(&mut self)

Overwrites self with zeros. Read more
Source§

impl IntoBytes for SpacePacket
where PrimaryHeader: IntoBytes, [u8]: IntoBytes, (): DynamicPaddingFree<Self, { _ }>,

§

fn as_bytes(&self) -> &[u8]
where Self: Immutable,

Gets the bytes of this value. Read more
§

fn as_mut_bytes(&mut self) -> &mut [u8]
where Self: FromBytes,

Gets the bytes of this value mutably. Read more
§

fn write_to(&self, dst: &mut [u8]) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to dst. Read more
§

fn write_to_prefix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the prefix of dst. Read more
§

fn write_to_suffix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the suffix of dst. Read more
Source§

impl KnownLayout for SpacePacket
where [u8]: KnownLayout,

Source§

type PointerMetadata = <[u8] as KnownLayout>::PointerMetadata

The type of metadata stored in a pointer to Self. Read more
§

fn size_for_metadata(meta: Self::PointerMetadata) -> Option<usize>

Computes the size of an object of type Self with the given pointer metadata. Read more
Source§

impl PartialEq for SpacePacket
where Self: IntoBytes + Immutable,

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> TryFrom<&'a SpacePacket> for &'a Telecommand

Source§

type Error = TelecommandError

The type returned in the event of a conversion error.
Source§

fn try_from(sp: &'a SpacePacket) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a> TryFrom<&'a SpacePacket> for &'a Telemetry

Source§

type Error = TelemetryError

The type returned in the event of a conversion error.
Source§

fn try_from(sp: &'a SpacePacket) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFromBytes for SpacePacket
where PrimaryHeader: TryFromBytes, [u8]: TryFromBytes,

§

fn try_ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the given source as a &Self. Read more
§

fn try_ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the prefix of the given source as a &Self. Read more
§

fn try_ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the suffix of the given source as a &Self. Read more
§

fn try_mut_from_bytes( bytes: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the given source as a &mut Self without copying. Read more
§

fn try_mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self. Read more
§

fn try_mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self. Read more
§

fn try_ref_from_bytes_with_elems( source: &[u8], count: usize, ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the given source as a &Self with a DST length equal to count. Read more
§

fn try_ref_from_prefix_with_elems( source: &[u8], count: usize, ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the prefix of the given source as a &Self with a DST length equal to count. Read more
§

fn try_ref_from_suffix_with_elems( source: &[u8], count: usize, ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the suffix of the given source as a &Self with a DST length equal to count. Read more
§

fn try_mut_from_bytes_with_elems( source: &mut [u8], count: usize, ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the given source as a &mut Self with a DST length equal to count. Read more
§

fn try_mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self with a DST length equal to count. Read more
§

fn try_mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self with a DST length equal to count. Read more
Source§

impl Eq for SpacePacket
where Self: IntoBytes + Immutable,

Source§

impl Immutable for SpacePacket
where PrimaryHeader: Immutable, [u8]: Immutable,

Source§

impl Unaligned for SpacePacket
where PrimaryHeader: Unaligned, [u8]: Unaligned,

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.