mqtt5_protocol/
constants.rs

1//! MQTT Protocol Constants
2//!
3//! This module defines constants for MQTT packet types and flags to avoid magic numbers
4//! throughout the codebase.
5
6use crate::PacketType;
7
8/// Fixed header byte 1 values (packet type << 4 | flags)
9pub mod fixed_header {
10    /// CONNECT packet fixed header (0x10)
11    pub const CONNECT: u8 = (super::PacketType::Connect as u8) << 4;
12
13    /// CONNACK packet fixed header (0x20)
14    pub const CONNACK: u8 = (super::PacketType::ConnAck as u8) << 4;
15
16    /// PUBLISH packet fixed header base (0x30) - flags vary
17    pub const PUBLISH_BASE: u8 = (super::PacketType::Publish as u8) << 4;
18
19    /// PUBACK packet fixed header (0x40)
20    pub const PUBACK: u8 = (super::PacketType::PubAck as u8) << 4;
21
22    /// PUBREC packet fixed header (0x50)
23    pub const PUBREC: u8 = (super::PacketType::PubRec as u8) << 4;
24
25    /// PUBREL packet fixed header (0x62) - has required flags
26    pub const PUBREL: u8 = (super::PacketType::PubRel as u8) << 4 | 0x02;
27
28    /// PUBCOMP packet fixed header (0x70)
29    pub const PUBCOMP: u8 = (super::PacketType::PubComp as u8) << 4;
30
31    /// SUBSCRIBE packet fixed header (0x82) - has required flags
32    pub const SUBSCRIBE: u8 = (super::PacketType::Subscribe as u8) << 4 | 0x02;
33
34    /// SUBACK packet fixed header (0x90)
35    pub const SUBACK: u8 = (super::PacketType::SubAck as u8) << 4;
36
37    /// UNSUBSCRIBE packet fixed header (0xA2) - has required flags
38    pub const UNSUBSCRIBE: u8 = (super::PacketType::Unsubscribe as u8) << 4 | 0x02;
39
40    /// UNSUBACK packet fixed header (0xB0)
41    pub const UNSUBACK: u8 = (super::PacketType::UnsubAck as u8) << 4;
42
43    /// PINGREQ packet fixed header (0xC0)
44    pub const PINGREQ: u8 = (super::PacketType::PingReq as u8) << 4;
45
46    /// PINGRESP packet fixed header (0xD0)
47    pub const PINGRESP: u8 = (super::PacketType::PingResp as u8) << 4;
48
49    /// DISCONNECT packet fixed header (0xE0)
50    pub const DISCONNECT: u8 = (super::PacketType::Disconnect as u8) << 4;
51
52    /// AUTH packet fixed header (0xF0)
53    pub const AUTH: u8 = (super::PacketType::Auth as u8) << 4;
54}
55
56/// Masks for extracting fields from fixed header
57pub mod masks {
58    /// Mask for extracting packet type from fixed header byte 1 (0xF0)
59    pub const PACKET_TYPE: u8 = 0xF0;
60
61    /// Mask for extracting flags from fixed header byte 1 (0x0F)
62    pub const FLAGS: u8 = 0x0F;
63
64    /// Mask for checking continuation bit in variable byte integer (0x80)
65    pub const CONTINUATION_BIT: u8 = 0x80;
66
67    /// Mask for extracting value from variable byte integer (0x7F)
68    pub const VARIABLE_BYTE_VALUE: u8 = 0x7F;
69}
70
71/// Common packet payloads
72pub mod packets {
73    /// PINGREQ packet as bytes
74    pub const PINGREQ_BYTES: [u8; 2] = [super::fixed_header::PINGREQ, 0x00];
75
76    /// PINGRESP packet as bytes
77    pub const PINGRESP_BYTES: [u8; 2] = [super::fixed_header::PINGRESP, 0x00];
78}
79
80/// Subscription option masks
81pub mod subscription {
82    /// Mask for `QoS` bits (bits 0-1)
83    pub const QOS_MASK: u8 = 0x03;
84
85    /// Mask for No Local flag (bit 2)
86    pub const NO_LOCAL_MASK: u8 = 0x04;
87
88    /// Mask for Retain As Published flag (bit 3)
89    pub const RETAIN_AS_PUBLISHED_MASK: u8 = 0x08;
90
91    /// Mask for Retain Handling (bits 4-5)
92    pub const RETAIN_HANDLING_MASK: u8 = 0x30;
93
94    /// Shift for Retain Handling
95    pub const RETAIN_HANDLING_SHIFT: u8 = 4;
96
97    /// Mask for reserved bits (bits 6-7)
98    pub const RESERVED_BITS_MASK: u8 = 0xC0;
99}
100
101/// CONNECT flags masks
102pub mod connect_flags {
103    /// Mask for clearing Will `QoS` bits (bits 3-4)
104    pub const WILL_QOS_CLEAR_MASK: u8 = !0x18;
105    /// Mask for extracting Will `QoS` (bits 3-4 shifted)
106    pub const WILL_QOS_MASK: u8 = 0x03;
107    /// Shift for Will `QoS`
108    pub const WILL_QOS_SHIFT: u8 = 3;
109}
110
111/// String and binary data limits
112pub mod limits {
113    /// Maximum string length in MQTT (65535)
114    pub const MAX_STRING_LENGTH: u16 = u16::MAX;
115
116    /// Maximum client ID length (128 characters)
117    pub const MAX_CLIENT_ID_LENGTH: usize = 128;
118
119    /// Maximum packet size (256 MB)
120    pub const MAX_PACKET_SIZE: u32 = 268_435_456;
121
122    /// Maximum binary data length (65536)
123    pub const MAX_BINARY_LENGTH: u32 = 65_536;
124}
125
126/// Time-related constants
127pub mod time {
128    use crate::time::Duration;
129
130    /// Default session expiry interval (1 hour)
131    pub const DEFAULT_SESSION_EXPIRY: Duration = Duration::from_secs(3600);
132
133    /// Default keep alive interval (60 seconds)
134    pub const DEFAULT_KEEP_ALIVE: Duration = Duration::from_secs(60);
135}
136
137/// Buffer and capacity constants
138pub mod buffer {
139    /// Default buffer capacity (1024 bytes)
140    pub const DEFAULT_CAPACITY: usize = 1024;
141
142    /// Default buffer size for encoding/decoding (2048 bytes)
143    pub const DEFAULT_BUFFER_SIZE: usize = 2048;
144
145    /// Large buffer size for bulk operations (4096 bytes)
146    pub const LARGE_BUFFER_SIZE: usize = 4096;
147
148    /// Maximum buffer size (8192 bytes)
149    pub const MAX_BUFFER_SIZE: usize = 8192;
150
151    /// Very large buffer for high-throughput scenarios (16384 bytes)
152    pub const VERY_LARGE_BUFFER_SIZE: usize = 16384;
153
154    /// Huge buffer for maximum performance (32768 bytes)
155    pub const HUGE_BUFFER_SIZE: usize = 32768;
156}
157
158/// Variable byte integer constants
159pub mod variable_byte {
160    /// Maximum value for single byte (127)
161    pub const SINGLE_BYTE_MAX: u8 = 127;
162
163    /// Maximum value for variable byte integer (268435455)
164    pub const MAX_VALUE: u32 = 268_435_455;
165}
166
167/// Protocol version constants
168pub mod version {
169    /// MQTT protocol version 5.0
170    pub const MQTT_V5: u8 = 5;
171}
172
173/// PUBLISH flags masks
174pub mod publish_flags {
175    /// Mask for clearing `QoS` bits (bits 1-2)
176    pub const QOS_CLEAR_MASK: u8 = !0x06;
177    /// Mask for extracting `QoS` (bits 1-2 shifted)
178    pub const QOS_MASK: u8 = 0x03;
179    /// Shift for `QoS`
180    pub const QOS_SHIFT: u8 = 1;
181}
182
183#[cfg(test)]
184mod tests {
185    use super::*;
186
187    #[test]
188    fn test_fixed_header_values() {
189        assert_eq!(fixed_header::CONNECT, 0x10);
190        assert_eq!(fixed_header::CONNACK, 0x20);
191        assert_eq!(fixed_header::PUBLISH_BASE, 0x30);
192        assert_eq!(fixed_header::PINGREQ, 0xC0);
193        assert_eq!(fixed_header::PINGRESP, 0xD0);
194    }
195
196    #[test]
197    fn test_packets() {
198        assert_eq!(packets::PINGREQ_BYTES, [0xC0, 0x00]);
199        assert_eq!(packets::PINGRESP_BYTES, [0xD0, 0x00]);
200    }
201}