Index: lib/Comms/include/MqttClient.h =================================================================== diff -u -r51e99f2578e0901d9da91a4cb60d1b8858cfe971 -raaebfee335c74b0250864a6dce0555f866adadea --- lib/Comms/include/MqttClient.h (.../MqttClient.h) (revision 51e99f2578e0901d9da91a4cb60d1b8858cfe971) +++ lib/Comms/include/MqttClient.h (.../MqttClient.h) (revision aaebfee335c74b0250864a6dce0555f866adadea) @@ -17,25 +17,14 @@ #include #include #include +#include +#include class QMqttClient; /*! - * \brief Publishes messages to the cloud over MQTT. - * \details Wraps QtMqtt's QMqttClient for a QoS 1 publishing session. - * - * The lifecycle is init() -> open() -> close(). init() only validates - * the configuration and builds the TLS configuration from it; the - * QMqttClient itself is created by open() and destroyed by close(), - * or by an unsolicited disconnect. A close()/open() pair is therefore - * a full reset, which is what allows the session to be reopened with - * rotated credentials. - * \note Connecting is asynchronous: open() only starts the attempt, and a - * true return does not mean the broker accepted the session. - * \note Must live on the thread that calls open(): the client's socket - * belongs to the thread that constructs it. - * \note The connection is always TLS. init() fails unless keyPath names a - * readable private key. + * \brief Encrypted client MQTT connection to a broker for message sending and receiving. + * Lifecycle: init() -> open() -> close(). */ class MqttClient : public QObject { @@ -62,19 +51,37 @@ ~MqttClient() override; bool init(const Config &config); - -public Q_SLOTS: bool open(); void close(); - bool publish(const QString &topic, const QByteArray &payload, qint64 messageId = -1); + bool publish(const QString &topic, const QByteArray &payload, qint32 &msgId, quint8 qos=1); + bool subscribe(const QString &topicFilter, quint8 qos=1); -private: +Q_SIGNALS: + /*! + * \brief Emitted when the state of the session changes (i.e. connect, disconnect) + * \note Subscriptions belong to the QMqttClient that gets destroyed on disconnect, + * so re-subscription is required on every new connection. + * \note Not emitted for a caller-initiated close() + */ + void didStateChanged(QMqttClient::ClientState state); + + /*! + * \brief Emitted for status change of every sent message. + */ + void didMessageStatusChanged(quint32 id, QMqtt::MessageStatus status, + const QMqttMessageStatusProperties &properties); + + /*! + * \brief Emitted for every message the broker delivers to this client. + * \details Covers all active subscriptions; the receiver demultiplexes by topic. + */ + void didMessageReceived(const QString &topic, const QByteArray &payload); + +private Q_SLOTS: void onStateChanged(); - void onMessageSent(qint32 id); - void failPending(); +private: QMqttClient *_client = nullptr; Config _config; QSslConfiguration _sslConfig; - QHash _pending; };