Skip to main content

Frame

Struct Frame 

Source
pub struct Frame { /* private fields */ }
Expand description

An addressed mesh packet.

A frame names where it came from and where it is going, carries a sequence number its origin assigns, counts down a hop limit as it is relayed, and ends with a checksum. The byte layout is fixed and big-endian:

0       version
1..=4   source node       (u32)
5..=8   destination node  (u32, BROADCAST for every node)
9..=10  sequence id       (u16)
11      hop limit
12..    payload
last 2  checksum          (u16)

The checksum covers every byte except the hop limit, which changes at each relay. So the check is end to end: a node can confirm a flooded packet’s payload is intact no matter how many relays forwarded it, and a relay spends a hop without recomputing it. The whole frame lives in a fixed buffer, so neither building nor parsing allocates.

§Examples

use pamoja_mesh::Frame;

let frame = Frame::new(0x0A, 0x0B, 7, b"hello").unwrap();
assert_eq!(frame.src(), 0x0A);
assert_eq!(frame.dst(), 0x0B);
assert_eq!(frame.id(), 7);
assert_eq!(frame.payload(), b"hello");

let received = Frame::parse(frame.as_bytes()).unwrap();
assert_eq!(received.payload(), b"hello");

Implementations§

Source§

impl Frame

Source

pub const MAX_LEN: usize = 250

The largest a mesh frame may be, in bytes, sized to the payload of a connectionless ESP-NOW frame.

Source

pub const HEADER_LEN: usize = 12

The fixed header length in bytes: version, source, destination, sequence id, and hop limit.

Source

pub const OVERHEAD: usize

The non-payload bytes of a frame: the header plus the trailing checksum.

Source

pub const MAX_PAYLOAD: usize

The largest payload a single frame can carry.

Source

pub const VERSION: u8 = 1

The protocol version this build writes and accepts.

Source

pub const DEFAULT_HOP_LIMIT: u8 = 3

The hop limit a newly built frame starts with, enough for a small local mesh.

Source

pub fn new( src: u32, dst: u32, id: u16, payload: &[u8], ) -> Result<Frame, MeshError>

Builds a frame from a source, a destination, a sequence id, and a payload, starting at DEFAULT_HOP_LIMIT.

§Arguments
  • src - the origin node’s address.
  • dst - the destination node’s address, or BROADCAST for every node.
  • id - the sequence number the origin assigns, increasing per message; with the source it identifies a packet as it floods, for dedup_key.
  • payload - the bytes to carry.
§Returns

The frame, ready to send.

§Errors

Returns MeshError::PayloadTooLong if payload is longer than MAX_PAYLOAD.

Source

pub fn broadcast(src: u32, id: u16, payload: &[u8]) -> Result<Frame, MeshError>

Builds a frame addressed to every node, for flooding the whole mesh.

§Arguments
  • src - the origin node’s address.
  • id - the sequence number the origin assigns.
  • payload - the bytes to carry.
§Returns

The broadcast frame, ready to send.

§Errors

Returns MeshError::PayloadTooLong if payload is longer than MAX_PAYLOAD.

Source

pub fn with_hop_limit(self, hop_limit: u8) -> Frame

Sets the hop limit, the number of further relays the frame is allowed.

The checksum does not cover the hop limit, so this needs no recomputation and leaves a parsed frame still valid.

§Arguments
  • hop_limit - the new hop limit. 0 means no node should relay the frame further.
§Returns

The frame with the hop limit set, for chaining.

Source

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

Parses a received frame, verifying its version and checksum.

§Arguments
  • bytes - the raw frame as it came off the radio.
§Returns

The validated frame.

§Errors

Returns MeshError::FrameTooShort or MeshError::FrameTooLong if the length is outside a frame’s bounds, MeshError::UnsupportedVersion if the version byte is not VERSION, or MeshError::CrcMismatch if the checksum does not match the contents.

Source

pub fn version(&self) -> u8

Returns the protocol version.

§Returns

The version byte.

Source

pub fn src(&self) -> u32

Returns the source node’s address.

§Returns

The address of the node that originated the frame.

Source

pub fn dst(&self) -> u32

Returns the destination node’s address.

§Returns

The address of the destination node, or BROADCAST for every node.

Source

pub fn id(&self) -> u16

Returns the sequence id the origin assigned.

§Returns

The sequence number.

Source

pub fn hop_limit(&self) -> u8

Returns the remaining hop limit.

§Returns

The number of further relays the frame is allowed.

Source

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

Returns the payload.

§Returns

The carried bytes, without the header or checksum.

Source

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

Returns the whole frame, checksum included, ready for the radio.

§Returns

The frame as a byte slice.

Source

pub fn is_broadcast(&self) -> bool

Reports whether the frame is addressed to every node.

§Returns

true if the destination is BROADCAST.

Source

pub fn dedup_key(&self) -> (u32, u16)

Returns the key that identifies this packet as it floods: its source and sequence id.

§Returns

The (source, id) pair, for a SeenCache.

Source

pub fn relayed(&self) -> Option<Frame>

Returns the frame to forward one hop further, with a hop spent.

§Returns

The same frame with its hop limit reduced by one, or None if the hop limit is already 0 and the frame must not be relayed further.

Trait Implementations§

Source§

impl Clone for Frame

Source§

fn clone(&self) -> Frame

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Frame

Source§

impl Debug for Frame

Source§

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

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

impl Eq for Frame

Source§

impl PartialEq for Frame

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 StructuralPartialEq for Frame

Auto Trait Implementations§

§

impl Freeze for Frame

§

impl RefUnwindSafe for Frame

§

impl Send for Frame

§

impl Sync for Frame

§

impl Unpin for Frame

§

impl UnsafeUnpin for Frame

§

impl UnwindSafe for Frame

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

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

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.