/*! * * Copyright (c) 2026 Diality Inc. - All Rights Reserved. * \copyright * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * \file MqttPacket.h * \author (original) Stephen Quong * \date (original) 30-Jul-2026 * */ #pragma once #include #include /*! * \brief MQTT packet encoding and decoding. */ namespace Mqtt { enum class PacketType : quint8 { Connect = 1, ConnAck = 2, Publish = 3, PubAck = 4, PingReq = 12, PingResp = 13, Disconnect = 14, }; enum class ConnAckCode : quint8 { Accepted = 0, UnacceptableProtocol = 1, IdentifierRejected = 2, ServerUnavailable = 3, BadCredentials = 4, NotAuthorized = 5, }; /// Outcome of one readPacket() call. enum class ReadState { Incomplete, ///< Need more bytes; nothing was consumed. Complete, ///< One packet was decoded and consumed. Error, ///< Malformed; the caller should drop the connection. }; /*! * \brief One decoded MQTT packet. */ struct Packet { PacketType type = PacketType::Disconnect; QString topic; quint16 packetId = 0; quint8 qos = 0; QByteArray payload; bool dup = false; bool retain = false; QString clientId; quint16 keepAliveSecs = 0; bool cleanSession = true; ConnAckCode returnCode = ConnAckCode::Accepted; bool sessionPresent = false; }; QByteArray buildConnect(const QString &clientId, quint16 keepAliveSecs, bool cleanSession); QByteArray buildConnAck(ConnAckCode code, bool sessionPresent = false); QByteArray buildPublish(const QString &topic, const QByteArray &payload, quint8 qos, quint16 packetId); QByteArray buildPubAck(quint16 packetId); QByteArray buildPingReq(); QByteArray buildPingResp(); QByteArray buildDisconnect(); ReadState readPacket(QByteArray &bytes, Packet &packet); QString typeName(PacketType type); } // namespace Mqtt