Skip to content

Commit

Permalink
Implement MQTT spec 3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
bertmelis authored Sep 23, 2024
1 parent cad93ae commit 7c33ffa
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 46 deletions.
6 changes: 4 additions & 2 deletions src/Packets/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ the LICENSE file.

namespace espMqttClientInternals {

constexpr const char PROTOCOL[] = "MQTT";
constexpr const uint8_t PROTOCOL_LEVEL = 0b00000100;
constexpr const char PROTOCOL_3_1[] = "MQIsdp";
constexpr const char PROTOCOL_3_1_1[] = "MQTT";
constexpr const uint8_t PROTOCOL_LEVEL_3_1 = 0b00000011;
constexpr const uint8_t PROTOCOL_LEVEL_3_1_1 = 0b00000100;

typedef uint8_t MQTTPacketType;

Expand Down
6 changes: 3 additions & 3 deletions src/Packets/Packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Packet::Packet(espMqttClientTypes::Error& error,

// Calculate size
size_t remainingLength =
6 + // protocol
8 + // protocol
1 + // protocol level
1 + // connect flags
2 + // keepalive
Expand All @@ -119,8 +119,8 @@ Packet::Packet(espMqttClientTypes::Error& error,
// FIXED HEADER
_data[pos++] = PacketType.CONNECT | HeaderFlag.CONNECT_RESERVED;
pos += encodeRemainingLength(remainingLength, &_data[pos]);
pos += encodeString(PROTOCOL, &_data[pos]);
_data[pos++] = PROTOCOL_LEVEL;
pos += encodeString(PROTOCOL_3_1, &_data[pos]);
_data[pos++] = PROTOCOL_LEVEL_3_1;
uint8_t connectFlags = 0;
if (cleanSession) connectFlags |= espMqttClientInternals::ConnectFlag.CLEAN_SESSION;
if (username != nullptr) connectFlags |= espMqttClientInternals::ConnectFlag.USERNAME;
Expand Down
18 changes: 9 additions & 9 deletions src/Packets/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,20 @@ ParserResult Parser::_fixedHeader(Parser* p) {
return ParserResult::protocolError;
}
} else {
switch (p->_packet.fixedHeader.packetType) {
case PacketType.CONNACK | HeaderFlag.CONNACK_RESERVED:
case PacketType.PUBACK | HeaderFlag.PUBACK_RESERVED:
case PacketType.PUBREC | HeaderFlag.PUBREC_RESERVED:
case PacketType.PUBREL | HeaderFlag.PUBREL_RESERVED:
case PacketType.PUBCOMP | HeaderFlag.PUBCOMP_RESERVED:
case PacketType.UNSUBACK | HeaderFlag.UNSUBACK_RESERVED:
switch (p->_packet.fixedHeader.packetType & 0xF0) {
case PacketType.CONNACK:
case PacketType.PUBACK:
case PacketType.PUBREC:
case PacketType.PUBREL:
case PacketType.PUBCOMP:
case PacketType.UNSUBACK:
p->_parse = _remainingLengthFixed;
break;
case PacketType.SUBACK | HeaderFlag.SUBACK_RESERVED:
case PacketType.SUBACK:
p->_parse = _remainingLengthVariable;
p->_bytePos = 0;
break;
case PacketType.PINGRESP | HeaderFlag.PINGRESP_RESERVED:
case PacketType.PINGRESP:
p->_parse = _remainingLengthNone;
break;
default:
Expand Down
64 changes: 32 additions & 32 deletions test/test_packets/test_packets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ void tearDown() {}

void test_encodeConnect0() {
const uint8_t check[] = {
0b00010000, // header
0x0F, // remaining length
0x00,0x04,'M','Q','T','T', // protocol
0b00000100, // protocol level
0b00000010, // connect flags
0x00,0x10, // keepalive (16)
0x00,0x03,'c','l','i' // client id
0b00010000, // header
0x11, // remaining length
0x00,0x06,'M','Q','I','s', 'd', 'p', // protocol
0b000000011, // protocol level
0b00000010, // connect flags
0x00,0x10, // keepalive (16)
0x00,0x03,'c','l','i' // client id
};
const uint32_t length = 17;
const uint32_t length = 19;

bool cleanSession = true;
const char* username = nullptr;
Expand Down Expand Up @@ -56,19 +56,19 @@ void test_encodeConnect0() {

void test_encodeConnect1() {
const uint8_t check[] = {
0b00010000, // header
0x20, // remaining length
0x00,0x04,'M','Q','T','T', // protocol
0b00000100, // protocol level
0b11101110, // connect flags
0x00,0x10, // keepalive (16)
0x00,0x03,'c','l','i', // client id
0x00,0x03,'t','o','p', // will topic
0x00,0x02,'p','l', // will payload
0x00,0x02,'u','n', // username
0x00,0x02,'p','a' // password
0b00010000, // header
0x22, // remaining length
0x00,0x06,'M','Q','I','s', 'd', 'p', // protocol
0b00000011, // protocol level
0b11101110, // connect flags
0x00,0x10, // keepalive (16)
0x00,0x03,'c','l','i', // client id
0x00,0x03,'t','o','p', // will topic
0x00,0x02,'p','l', // will payload
0x00,0x02,'u','n', // username
0x00,0x02,'p','a' // password
};
const uint32_t length = 34;
const uint32_t length = 36;

bool cleanSession = true;
const char* username = "un";
Expand Down Expand Up @@ -106,19 +106,19 @@ void test_encodeConnect1() {

void test_encodeConnect2() {
const uint8_t check[] = {
0b00010000, // header
0x20, // remaining length
0x00,0x04,'M','Q','T','T', // protocol
0b00000100, // protocol level
0b11110110, // connect flags
0x00,0x10, // keepalive (16)
0x00,0x03,'c','l','i', // client id
0x00,0x03,'t','o','p', // will topic
0x00,0x02,'p','l', // will payload
0x00,0x02,'u','n', // username
0x00,0x02,'p','a' // password
0b00010000, // header
0x22, // remaining length
0x00,0x06,'M','Q','I','s', 'd', 'p', // protocol
0b00000011, // protocol level
0b11110110, // connect flags
0x00,0x10, // keepalive (16)
0x00,0x03,'c','l','i', // client id
0x00,0x03,'t','o','p', // will topic
0x00,0x02,'p','l', // will payload
0x00,0x02,'u','n', // username
0x00,0x02,'p','a' // password
};
const uint32_t length = 34;
const uint32_t length = 36;

bool cleanSession = true;
const char* username = "un";
Expand Down

0 comments on commit 7c33ffa

Please sign in to comment.