Expand description
A lightweight binary framing protocol library.
This crate provides a minimal, allocation-free framing layer designed for embedded and resource-constrained environments. It focuses on deterministic binary encoding, checksum validation, and zero-copy decoding.
§Architecture Overview
-
ValidatorDefines CRC algorithms used to validate frames. -
DjiValidatorA concrete validator using DJI-compatible CRC8 and CRC16. -
MarshalerDescribes how a typed payload is serialized into bytes and deserialized from raw payload data. -
MessagerImplements frame packing and unpacking, combining framing, validation, and payload marshaling. -
RawFrameA validated, zero-copy view of a decoded frame. -
RemoteControl(optional) A helper for encoding and decoding remote control messages.
§Typical Usage
- Implement
Marshalerfor your message types - Create a
Messagerwith a chosenValidator - Use
packto encode frames - Use
unpackto decode frames intoRawFrame - Use
Marshalerto convertRawFramepayloads to typed messages - Optionally, use
RemoteControlfor handling remote control data
§Example
// Define a custom message type and implement Marshaler for it
struct MyMessage {
value: u32,
}
impl Marshaler for MyMessage {
const CMD_ID: u16 = 0x1234;
const PAYLOAD_SIZE: u16 = 4;
// If called manually, the caller must ensure that the destination
// buffer is large enough to hold the serialized payload.
fn marshal(&self, buf: &mut [u8]) -> Result<usize, MarshalerError> {
buf[..4].copy_from_slice(&self.value.to_le_bytes());
Ok(4)
}
fn unmarshal(buf: &[u8]) -> Result<Self, MarshalerError> {
Ok(Self {
value: u32::from_le_bytes([buf[0], buf[1], buf[2], buf[3]]),
})
}
}
let mut buffer = [0u8; 64];
let mut msger = Messager::<DjiValidator>::new( /* seq: */ 0 );
let msg = MyMessage { value: 38 };
let packed_len = msger.pack(&msg, &mut buffer)?; // seq++
println!("Packed Frame Length: {}", packed_len);
let (decoded_msg, consumed): (MyMessage, _) = msger.unmarshal(&buffer)?;
assert_eq!(consumed, packed_len);
assert_eq!(decoded_msg.value, 38);
let msg = MyMessage { value: 42 };
let packed_len = msger.pack(&msg, &mut buffer)?; // seq++
println!("Packed Frame Length: {}", packed_len);
let (raw_frame, consumed) = msger.unpack(&buffer)?;
assert_eq!(consumed, packed_len);
assert_eq!(raw_frame.cmd_id(), MyMessage::CMD_ID);
assert_eq!(raw_frame.sequence(), 1);
let decoded_msg = MyMessage::unmarshal(raw_frame.payload())?;
assert_eq!(decoded_msg.value, 42);
RemoteControl’s Example See README.md for details.
§Frame Layout
+--------+--------+--------+--------+--------+---------+--------+
| SOF | LEN | SEQ | CRC8 | CMD_ID | DATA | CRC16 |
+--------+--------+--------+--------+--------+---------+--------+
| 1 byte | 2 byte | 1 byte | 1 byte | 2 byte | N bytes | 2 byte |
+--------+--------+--------+--------+--------+---------+--------+§Protocol Source
See RoboMaster Resources Hub for official documentation and protocol details: RMU Communication Protocol
§Features
defmt: Derivesdefmt::Formaton error types and key enums for embedded structured logging.remote: Adds support for encoding and decoding remote control messages, including switch states and joystick positions.
§License
This crate is licensed under the MIT License or the Apache License (Version 2.0).
See LICENSE-MIT and LICENSE-APACHE files in the repository for details.
Structs§
- DjiValidator
- DJI protocol CRC validator.
- Messager
- Frame encoder and decoder.
- RawFrame
- A validated, undecoded frame.
- Remote
Control remote - Decoded view of the remote-control state.
Enums§
- Marshaler
Error - Errors returned by
Marshaler::marshal,Marshaler::unmarshal, andRawFrame::unmarshal. - Pack
Error - Errors returned by
Messager::pack. - Switch
remote - UnPack
Error - Errors returned by
Messager::unpackandMessager::unmarshal.
Traits§
- Impl
Command Msg - Provides the command ID and payload size constants for a message type.
- Impl
Marshal - Payload serialization half of
Marshaler. - Impl
UnMarshal - Payload deserialization half of
Marshaler. - Marshaler
- Payload marshaling interface.
- Validator
- CRC validator abstraction for the frame protocol.
Functions§
- calc_
dji8 - Calculate CRC8-DJI Checksum
- calc_
dji16 - Calculate CRC16-DJI Checksum