pub struct Messager<V: Validator = DjiValidator> { /* private fields */ }Expand description
Frame encoder and decoder.
Messager packs typed messages into binary frames and unpacks
validated frames from raw bytes. It performs no I/O and allocates
no memory.
Generic over Validator to allow custom CRC implementations.
The default validator is DjiValidator.
The internal sequence counter starts at the value passed to new
and increments by one after each successful pack call,
wrapping on overflow.
§Frame Layout
+--------+--------+--------+--------+--------+---------+--------+
| SOF | LEN | SEQ | CRC8 | CMD_ID | DATA | CRC16 |
+--------+--------+--------+--------+--------+---------+--------+
| 1 byte | 2 byte | 1 byte | 1 byte | 2 byte | N bytes | 2 byte |
+--------+--------+--------+--------+--------+---------+--------+SOF: start-of-frame marker (0xA5)LEN: payload length, little-endianu16SEQ: sequence number,u8CRC8: checksum over[SOF, LEN, SEQ]CMD_ID: command identifier, little-endianu16DATA: payload bytes (N =crate::Marshaler::PAYLOAD_SIZE)CRC16: checksum over the entire frame preceding this field
Implementations§
Source§impl<V: Validator> Messager<V>
impl<V: Validator> Messager<V>
Sourcepub fn pack<M: ImplMarshal>(
&mut self,
msg: &M,
dst: &mut [u8],
) -> Result<usize, PackError>
pub fn pack<M: ImplMarshal>( &mut self, msg: &M, dst: &mut [u8], ) -> Result<usize, PackError>
Encode a message into a binary frame.
Serializes msg and writes the complete frame — header, command ID,
payload, and CRC — into dst. The payload is written directly into
dst with no intermediate buffer.
On success, returns the total number of bytes written.
The sequence counter increments by one after each successful call.
§Errors
PackError::BufferTooSmall—dstis smaller than the full frame.needis the minimum required size in bytes.PackError::InvalidPayloadSize— the marshaler returned a byte count that does not equalcrate::Marshaler::PAYLOAD_SIZE.PackError::MarshalerError— the marshaler itself failed.
Sourcepub fn unpack<'t>(
&self,
src: &'t [u8],
) -> Result<(RawFrame<'t>, usize), UnPackError>
pub fn unpack<'t>( &self, src: &'t [u8], ) -> Result<(RawFrame<'t>, usize), UnPackError>
Parse and validate one frame from raw bytes.
Reads from the start of src and performs these checks in order:
- Start-of-frame marker (
0xA5) - Header CRC8
- Frame CRC16
On success, returns a RawFrame whose payload borrows from src,
and the number of bytes consumed.
The payload in the returned RawFrame is untyped. Use
RawFrame::unmarshal to decode it, or call unmarshal
instead of this method to do both steps at once.
§Errors
UnPackError::ReSync—srcdoes not start with SOF.skipis the offset of the next SOF byte; discard that many bytes and retry.UnPackError::MissingHeader— no SOF byte found anywhere insrc.skipequalssrc.len(); discard the entire buffer and wait for more data.UnPackError::UnexpectedEnd— the frame is truncated. Keep the existing bytes and append more data before retrying.UnPackError::InvalidChecksum— CRC validation failed. CallUnPackError::skipto determine how many bytes to discard.
Sourcepub fn unmarshal<M: ImplUnMarshal>(
&self,
src: &[u8],
) -> Result<(M, usize), UnPackError>
pub fn unmarshal<M: ImplUnMarshal>( &self, src: &[u8], ) -> Result<(M, usize), UnPackError>
Parse a frame and decode its payload into a typed message.
Combines unpack and RawFrame::unmarshal in one call.
On success, returns the decoded message and the number of bytes consumed.
§Errors
- Returns
UnPackErrorfor any framing or CRC failure (seeunpack). - Returns
UnPackError::MarshalerErrorif command ID or payload size does not matchM, or ifM::unmarshalfails.