Skip to main content

Crate rm_frame

Crate rm_frame 

Source
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

  • Validator Defines CRC algorithms used to validate frames.

  • DjiValidator A concrete validator using DJI-compatible CRC8 and CRC16.

  • Marshaler Describes how a typed payload is serialized into bytes and deserialized from raw payload data.

  • Messager Implements frame packing and unpacking, combining framing, validation, and payload marshaling.

  • RawFrame A validated, zero-copy view of a decoded frame.

  • RemoteControl (optional) A helper for encoding and decoding remote control messages.

§Typical Usage

  1. Implement Marshaler for your message types
  2. Create a Messager with a chosen Validator
  3. Use pack to encode frames
  4. Use unpack to decode frames into RawFrame
  5. Use Marshaler to convert RawFrame payloads to typed messages
  6. Optionally, use RemoteControl for 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: Derives defmt::Format on 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.
RemoteControlremote
Decoded view of the remote-control state.

Enums§

MarshalerError
Errors returned by Marshaler::marshal, Marshaler::unmarshal, and RawFrame::unmarshal.
PackError
Errors returned by Messager::pack.
Switchremote
UnPackError
Errors returned by Messager::unpack and Messager::unmarshal.

Traits§

ImplCommandMsg
Provides the command ID and payload size constants for a message type.
ImplMarshal
Payload serialization half of Marshaler.
ImplUnMarshal
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