Skip to main content

peat_lite/protocol/
message_type.rs

1//! Peat-Lite message type identifiers.
2
3/// Message types for the gossip protocol.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5#[repr(u8)]
6pub enum MessageType {
7    /// Announce presence and capabilities.
8    Announce = 0x01,
9    /// Heartbeat / keep-alive.
10    Heartbeat = 0x02,
11    /// Data update (CRDT state).
12    Data = 0x03,
13    /// Query for specific state.
14    Query = 0x04,
15    /// Acknowledge receipt.
16    Ack = 0x05,
17    /// Leave notification.
18    Leave = 0x06,
19    /// OTA firmware offer (Full -> Lite).
20    OtaOffer = 0x10,
21    /// OTA accept (Lite -> Full).
22    OtaAccept = 0x11,
23    /// OTA data chunk (Full -> Lite).
24    OtaData = 0x12,
25    /// OTA chunk acknowledgement (Lite -> Full).
26    OtaAck = 0x13,
27    /// OTA transfer complete (Full -> Lite).
28    OtaComplete = 0x14,
29    /// OTA result (Lite -> Full).
30    OtaResult = 0x15,
31    /// OTA abort (either direction).
32    OtaAbort = 0x16,
33}
34
35impl MessageType {
36    /// Convert a raw byte to a `MessageType`, if valid.
37    pub fn from_u8(v: u8) -> Option<Self> {
38        match v {
39            0x01 => Some(Self::Announce),
40            0x02 => Some(Self::Heartbeat),
41            0x03 => Some(Self::Data),
42            0x04 => Some(Self::Query),
43            0x05 => Some(Self::Ack),
44            0x06 => Some(Self::Leave),
45            0x10 => Some(Self::OtaOffer),
46            0x11 => Some(Self::OtaAccept),
47            0x12 => Some(Self::OtaData),
48            0x13 => Some(Self::OtaAck),
49            0x14 => Some(Self::OtaComplete),
50            0x15 => Some(Self::OtaResult),
51            0x16 => Some(Self::OtaAbort),
52            _ => None,
53        }
54    }
55}