diff options
| author | diogo464 <[email protected]> | 2025-12-09 22:30:42 +0000 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2025-12-09 22:30:42 +0000 |
| commit | a5845673cf052b606f722be10d48c5d963958050 (patch) | |
| tree | e21bf5848163d07fce4bf8e3d7474bfeed5d1aff /src/mqtt/protocol.rs | |
| parent | 6bb6d358f39c31b5486621b49da463f97226fea5 (diff) | |
moved embedded-mqtt crate to a module
Diffstat (limited to 'src/mqtt/protocol.rs')
| -rw-r--r-- | src/mqtt/protocol.rs | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/mqtt/protocol.rs b/src/mqtt/protocol.rs new file mode 100644 index 0000000..bf77d78 --- /dev/null +++ b/src/mqtt/protocol.rs | |||
| @@ -0,0 +1,65 @@ | |||
| 1 | pub const PACKET_TYPE_CONNECT: u8 = 1; | ||
| 2 | pub const PACKET_TYPE_CONNACK: u8 = 2; | ||
| 3 | pub const PACKET_TYPE_PUBLISH: u8 = 3; | ||
| 4 | pub const PACKET_TYPE_PUBACK: u8 = 4; | ||
| 5 | pub const PACKET_TYPE_PUBREC: u8 = 5; | ||
| 6 | pub const PACKET_TYPE_PUBREL: u8 = 6; | ||
| 7 | pub const PACKET_TYPE_PUBCOMP: u8 = 7; | ||
| 8 | pub const PACKET_TYPE_SUBSCRIBE: u8 = 8; | ||
| 9 | pub const PACKET_TYPE_SUBACK: u8 = 9; | ||
| 10 | pub const PACKET_TYPE_UNSUBSCRIBE: u8 = 10; | ||
| 11 | pub const PACKET_TYPE_UNSUBACK: u8 = 11; | ||
| 12 | pub const PACKET_TYPE_PINGREQ: u8 = 12; | ||
| 13 | pub const PACKET_TYPE_PINGRESP: u8 = 13; | ||
| 14 | pub const PACKET_TYPE_DISCONNECT: u8 = 14; | ||
| 15 | |||
| 16 | pub const PROTOCOL_NAME: &str = "MQTT"; | ||
| 17 | |||
| 18 | pub const PROTOCOL_LEVEL_3_1_1: u8 = 0x04; | ||
| 19 | pub const PROTOCOL_LEVEL_5_0_0: u8 = 0x05; | ||
| 20 | |||
| 21 | pub const CONNECT_FLAG_USERNAME: u8 = 1 << 7; | ||
| 22 | pub const CONNECT_FLAG_PASSWORD: u8 = 1 << 6; | ||
| 23 | pub const CONNECT_FLAG_WILL_RETAIN: u8 = 1 << 5; | ||
| 24 | pub const CONNECT_FLAG_WILL_FLAG: u8 = 1 << 2; | ||
| 25 | pub const CONNECT_FLAG_CLEAN_SESSION: u8 = 1 << 1; | ||
| 26 | |||
| 27 | pub const SUBSCRIBE_HEADER_FLAGS: u8 = 0x02; | ||
| 28 | pub const UNSUBSCRIBE_HEADER_FLAGS: u8 = 0x02; | ||
| 29 | pub const PUBREL_HEADER_FLAGS: u8 = 0x02; | ||
| 30 | |||
| 31 | pub const CONNACK_CODE_ACCEPTED: u8 = 0; | ||
| 32 | pub const CONNACK_CODE_UNACCEPTABLE_PROTOCOL_VERSION: u8 = 1; | ||
| 33 | pub const CONNACK_CODE_IDENTIFIER_REJECTED: u8 = 2; | ||
| 34 | pub const CONNACK_CODE_SERVER_UNAVAILABLE: u8 = 3; | ||
| 35 | pub const CONNACK_CODE_BAD_USERNAME_PASSWORD: u8 = 4; | ||
| 36 | pub const CONNACK_CODE_NOT_AUTHORIZED: u8 = 5; | ||
| 37 | |||
| 38 | pub const CONNACK_FLAG_SESSION_PRESENT: u8 = 0x01; | ||
| 39 | pub const CONNACK_FLAG_RESERVED: u8 = 0xFE; | ||
| 40 | |||
| 41 | pub const SUBACK_FAILURE: u8 = 0x80; | ||
| 42 | |||
| 43 | pub const PUBLISH_FLAG_RETAIN: u8 = 0x01; | ||
| 44 | pub const PUBLISH_FLAG_QOS_MASK: u8 = 0x06; | ||
| 45 | pub const PUBLISH_FLAG_QOS_SHIFT: u8 = 1; | ||
| 46 | pub const PUBLISH_FLAG_DUP: u8 = 0x08; | ||
| 47 | |||
| 48 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 49 | pub struct HeaderControl { | ||
| 50 | pub packet_type: u8, | ||
| 51 | pub packet_flags: u8, | ||
| 52 | } | ||
| 53 | |||
| 54 | pub fn create_header_control(packet_type: u8, flags: u8) -> u8 { | ||
| 55 | assert!(packet_type & 0xF0 == 0); | ||
| 56 | assert!(flags & 0xF0 == 0); | ||
| 57 | packet_type << 4 | flags | ||
| 58 | } | ||
| 59 | |||
| 60 | pub fn split_header_control(control: u8) -> HeaderControl { | ||
| 61 | HeaderControl { | ||
| 62 | packet_type: control >> 4, | ||
| 63 | packet_flags: control & 0x0F, | ||
| 64 | } | ||
| 65 | } | ||
