From 546c8348aa071a46ee580f0dbfa1752fd4398623 Mon Sep 17 00:00:00 2001 From: Wiebe Cazemier Date: Mon, 27 Jun 2022 12:53:12 +0200 Subject: [PATCH] Add parseSubAckData() --- mqttpacket.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ mqttpacket.h | 1 + packetdatatypes.h | 7 +++++++ 3 files changed, 54 insertions(+), 0 deletions(-) diff --git a/mqttpacket.cpp b/mqttpacket.cpp index 946edcf..d87e72b 100644 --- a/mqttpacket.cpp +++ b/mqttpacket.cpp @@ -1275,6 +1275,52 @@ void MqttPacket::handlePubComp() sender->getSession()->removeOutgoingQoS2MessageId(packet_id); } +SubAckData MqttPacket::parseSubAckData() +{ + if (this->packetType != PacketType::SUBACK) + throw std::runtime_error("Packet must be suback packet."); + + setPosToDataStart(); + + SubAckData result; + + result.packet_id = readTwoBytesToUInt16(); + this->packet_id = result.packet_id; + + if (this->protocolVersion >= ProtocolVersion::Mqtt5 ) + { + const size_t proplen = decodeVariableByteIntAtPos(); + const size_t prop_end_at = pos + proplen; + + while (pos < prop_end_at) + { + const Mqtt5Properties prop = static_cast(readByte()); + + switch (prop) + { + case Mqtt5Properties::ReasonString: + result.reasonString = readBytesToString(); + break; + case Mqtt5Properties::UserProperty: + readUserProperty(); + break; + default: + throw ProtocolError("Invalid property in suback.", ReasonCodes::ProtocolError); + } + } + } + + // payload starts here + + while (!atEnd()) + { + uint8_t code = readByte(); + result.subAckCodes.push_back(code); + } + + return result; +} + void MqttPacket::calculateRemainingLength() { assert(fixed_header_length == 0); // because you're not supposed to call this on packet that we already know the length of. diff --git a/mqttpacket.h b/mqttpacket.h index e6ba3a6..0cd093d 100644 --- a/mqttpacket.h +++ b/mqttpacket.h @@ -124,6 +124,7 @@ public: void handlePubRec(); void handlePubRel(); void handlePubComp(); + SubAckData parseSubAckData(); uint8_t getFixedHeaderLength() const; size_t getSizeIncludingNonPresentHeader() const; diff --git a/packetdatatypes.h b/packetdatatypes.h index adce03f..1b1925e 100644 --- a/packetdatatypes.h +++ b/packetdatatypes.h @@ -47,5 +47,12 @@ struct DisconnectData uint32_t session_expiry_interval = 0; }; +struct SubAckData +{ + uint16_t packet_id; + std::string reasonString; + std::vector subAckCodes; +}; + #endif // PACKETDATATYPES_H -- libgit2 0.21.4