peat_lite/wire.rs
1// Copyright (c) 2025-2026 Defense Unicorns, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Wire format constants and error types.
5//!
6//! Defines markers and error handling for over-the-air message encoding.
7
8use crate::canned::MAX_CANNED_ACKS;
9
10/// Marker byte for canned message events on the wire.
11///
12/// Format: `0xAF` followed by message payload.
13/// This allows receivers to distinguish canned messages from other data.
14pub const CANNED_MESSAGE_MARKER: u8 = 0xAF;
15
16/// Size of an Ed25519 signature in bytes.
17pub const SIGNATURE_SIZE: usize = 64;
18
19/// Size of unsigned CannedMessageEvent wire format.
20///
21/// Format: `[marker:1][msg_code:1][src:4][tgt:4][timestamp:8][seq:4] = 22 bytes`
22pub const CANNED_MESSAGE_UNSIGNED_SIZE: usize = 22;
23
24/// Size of signed CannedMessageEvent wire format.
25///
26/// Format: `[marker:1][msg_code:1][src:4][tgt:4][timestamp:8][seq:4][signature:64] = 86 bytes`
27pub const CANNED_MESSAGE_SIGNED_SIZE: usize = CANNED_MESSAGE_UNSIGNED_SIZE + SIGNATURE_SIZE;
28
29/// Maximum encoded size for [`CannedMessageAckEvent`](crate::CannedMessageAckEvent).
30///
31/// 24 bytes base + (12 bytes per ACK entry × MAX_CANNED_ACKS).
32pub const CANNED_ACK_EVENT_MAX_SIZE: usize = 24 + MAX_CANNED_ACKS * 12;
33
34/// Wire format error types.
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub enum WireError {
37 /// Data too short to contain required fields.
38 TooShort,
39 /// Invalid marker byte.
40 InvalidMarker,
41 /// Unknown message code.
42 UnknownCode,
43 /// Checksum mismatch.
44 ChecksumMismatch,
45 /// Buffer capacity exceeded.
46 BufferFull,
47 /// Invalid or missing signature.
48 InvalidSignature,
49}
50
51#[cfg(feature = "std")]
52impl std::fmt::Display for WireError {
53 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54 match self {
55 Self::TooShort => write!(f, "data too short"),
56 Self::InvalidMarker => write!(f, "invalid marker byte"),
57 Self::UnknownCode => write!(f, "unknown message code"),
58 Self::ChecksumMismatch => write!(f, "checksum mismatch"),
59 Self::BufferFull => write!(f, "buffer capacity exceeded"),
60 Self::InvalidSignature => write!(f, "invalid or missing signature"),
61 }
62 }
63}
64
65#[cfg(feature = "std")]
66impl std::error::Error for WireError {}
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71
72 #[test]
73 fn test_marker_value() {
74 // Verify marker is in the "reserved" range and unlikely to collide
75 assert_eq!(CANNED_MESSAGE_MARKER, 0xAF);
76 }
77}