| 1 | // Copyright (C) 2017 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QTMQTTCLIENT_H |
| 5 | #define QTMQTTCLIENT_H |
| 6 | |
| 7 | #include <QtMqtt/qmqttglobal.h> |
| 8 | #include <QtMqtt/qmqttauthenticationproperties.h> |
| 9 | #include <QtMqtt/qmqttconnectionproperties.h> |
| 10 | #include <QtMqtt/qmqttpublishproperties.h> |
| 11 | #include <QtMqtt/qmqttsubscription.h> |
| 12 | #include <QtMqtt/qmqttsubscriptionproperties.h> |
| 13 | #include <QtMqtt/qmqtttopicfilter.h> |
| 14 | |
| 15 | #include <QtCore/QIODevice> |
| 16 | #include <QtCore/QObject> |
| 17 | #include <QtCore/QSharedPointer> |
| 18 | #include <QtNetwork/QTcpSocket> |
| 19 | #ifndef QT_NO_SSL |
| 20 | #include <QtNetwork/QSslConfiguration> |
| 21 | #endif |
| 22 | |
| 23 | QT_BEGIN_NAMESPACE |
| 24 | |
| 25 | class QMqttClientPrivate; |
| 26 | |
| 27 | class Q_MQTT_EXPORT QMqttClient : public QObject |
| 28 | { |
| 29 | public: |
| 30 | enum TransportType { |
| 31 | IODevice = 0, |
| 32 | AbstractSocket, |
| 33 | SecureSocket |
| 34 | }; |
| 35 | |
| 36 | enum ClientState { |
| 37 | Disconnected = 0, |
| 38 | Connecting, |
| 39 | Connected |
| 40 | }; |
| 41 | Q_ENUM(ClientState) |
| 42 | |
| 43 | enum ClientError { |
| 44 | // Protocol states |
| 45 | NoError = 0, |
| 46 | InvalidProtocolVersion = 1, |
| 47 | IdRejected = 2, |
| 48 | ServerUnavailable = 3, |
| 49 | BadUsernameOrPassword = 4, |
| 50 | NotAuthorized = 5, |
| 51 | // Qt states |
| 52 | TransportInvalid = 256, |
| 53 | ProtocolViolation, |
| 54 | UnknownError, |
| 55 | Mqtt5SpecificError |
| 56 | }; |
| 57 | Q_ENUM(ClientError) |
| 58 | |
| 59 | enum ProtocolVersion { |
| 60 | MQTT_3_1 = 3, |
| 61 | MQTT_3_1_1 = 4, |
| 62 | MQTT_5_0 = 5 |
| 63 | }; |
| 64 | Q_ENUM(ProtocolVersion) |
| 65 | |
| 66 | private: |
| 67 | Q_OBJECT |
| 68 | Q_ENUMS(ClientState) |
| 69 | Q_ENUMS(ClientError) |
| 70 | Q_ENUMS(ProtocolVersion) |
| 71 | Q_PROPERTY(QString clientId READ clientId WRITE setClientId NOTIFY clientIdChanged) |
| 72 | Q_PROPERTY(QString hostname READ hostname WRITE setHostname NOTIFY hostnameChanged) |
| 73 | Q_PROPERTY(quint16 port READ port WRITE setPort NOTIFY portChanged) |
| 74 | Q_PROPERTY(quint16 keepAlive READ keepAlive WRITE setKeepAlive NOTIFY keepAliveChanged) |
| 75 | Q_PROPERTY(ProtocolVersion protocolVersion READ protocolVersion WRITE setProtocolVersion NOTIFY protocolVersionChanged) |
| 76 | Q_PROPERTY(ClientState state READ state WRITE setState NOTIFY stateChanged) |
| 77 | Q_PROPERTY(ClientError error READ error WRITE setError NOTIFY errorChanged) |
| 78 | Q_PROPERTY(QString username READ username WRITE setUsername NOTIFY usernameChanged) |
| 79 | Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged) |
| 80 | Q_PROPERTY(bool cleanSession READ cleanSession WRITE setCleanSession NOTIFY cleanSessionChanged) |
| 81 | Q_PROPERTY(QString willTopic READ willTopic WRITE setWillTopic NOTIFY willTopicChanged) |
| 82 | Q_PROPERTY(QByteArray willMessage READ willMessage WRITE setWillMessage NOTIFY willMessageChanged) |
| 83 | Q_PROPERTY(quint8 willQoS READ willQoS WRITE setWillQoS NOTIFY willQoSChanged) |
| 84 | Q_PROPERTY(bool willRetain READ willRetain WRITE setWillRetain NOTIFY willRetainChanged) |
| 85 | Q_PROPERTY(bool autoKeepAlive READ autoKeepAlive WRITE setAutoKeepAlive NOTIFY autoKeepAliveChanged) |
| 86 | public: |
| 87 | explicit QMqttClient(QObject *parent = nullptr); |
| 88 | ~QMqttClient() override; |
| 89 | |
| 90 | void setTransport(QIODevice *device, TransportType transport); |
| 91 | QIODevice *transport() const; |
| 92 | |
| 93 | QMqttSubscription *subscribe(const QMqttTopicFilter &topic, quint8 qos = 0); |
| 94 | QMqttSubscription *subscribe(const QMqttTopicFilter &topic, |
| 95 | const QMqttSubscriptionProperties &properties, quint8 qos = 0); |
| 96 | void unsubscribe(const QMqttTopicFilter &topic); |
| 97 | void unsubscribe(const QMqttTopicFilter &topic, const QMqttUnsubscriptionProperties &properties); |
| 98 | |
| 99 | Q_INVOKABLE qint32 publish(const QMqttTopicName &topic, const QByteArray &message = QByteArray(), |
| 100 | quint8 qos = 0, bool retain = false); |
| 101 | Q_INVOKABLE qint32 publish(const QMqttTopicName &topic, const QMqttPublishProperties &properties, |
| 102 | const QByteArray &message = QByteArray(), |
| 103 | quint8 qos = 0, |
| 104 | bool retain = false); |
| 105 | |
| 106 | bool requestPing(); |
| 107 | |
| 108 | QString hostname() const; |
| 109 | quint16 port() const; |
| 110 | QString clientId() const; |
| 111 | quint16 keepAlive() const; |
| 112 | ProtocolVersion protocolVersion() const; |
| 113 | |
| 114 | Q_INVOKABLE void connectToHost(); |
| 115 | #ifndef QT_NO_SSL |
| 116 | void connectToHostEncrypted(const QSslConfiguration &conf); |
| 117 | #endif |
| 118 | Q_INVOKABLE void disconnectFromHost(); |
| 119 | |
| 120 | ClientState state() const; |
| 121 | ClientError error() const; |
| 122 | |
| 123 | QString username() const; |
| 124 | QString password() const; |
| 125 | bool cleanSession() const; |
| 126 | |
| 127 | QString willTopic() const; |
| 128 | quint8 willQoS() const; |
| 129 | QByteArray willMessage() const; |
| 130 | bool willRetain() const; |
| 131 | bool autoKeepAlive() const; |
| 132 | |
| 133 | void setConnectionProperties(const QMqttConnectionProperties &prop); |
| 134 | QMqttConnectionProperties connectionProperties() const; |
| 135 | |
| 136 | void setLastWillProperties(const QMqttLastWillProperties &prop); |
| 137 | QMqttLastWillProperties lastWillProperties() const; |
| 138 | |
| 139 | QMqttServerConnectionProperties serverConnectionProperties() const; |
| 140 | |
| 141 | void authenticate(const QMqttAuthenticationProperties &prop); |
| 142 | Q_SIGNALS: |
| 143 | void connected(); |
| 144 | void disconnected(); |
| 145 | void messageReceived(const QByteArray &message, const QMqttTopicName &topic = QMqttTopicName()); |
| 146 | void messageStatusChanged(qint32 id, QMqtt::MessageStatus s, const QMqttMessageStatusProperties &properties); |
| 147 | void messageSent(qint32 id); |
| 148 | void pingResponseReceived(); |
| 149 | void brokerSessionRestored(); |
| 150 | |
| 151 | void hostnameChanged(QString hostname); |
| 152 | void portChanged(quint16 port); |
| 153 | void clientIdChanged(QString clientId); |
| 154 | void keepAliveChanged(quint16 keepAlive); |
| 155 | void protocolVersionChanged(ProtocolVersion protocolVersion); |
| 156 | void stateChanged(ClientState state); |
| 157 | void errorChanged(ClientError error); |
| 158 | void usernameChanged(QString username); |
| 159 | void passwordChanged(QString password); |
| 160 | void cleanSessionChanged(bool cleanSession); |
| 161 | |
| 162 | void willTopicChanged(QString willTopic); |
| 163 | void willQoSChanged(quint8 willQoS); |
| 164 | void willMessageChanged(QByteArray willMessage); |
| 165 | void willRetainChanged(bool willRetain); |
| 166 | void autoKeepAliveChanged(bool autoKeepAlive); |
| 167 | |
| 168 | void authenticationRequested(const QMqttAuthenticationProperties &p); |
| 169 | void authenticationFinished(const QMqttAuthenticationProperties &p); |
| 170 | public Q_SLOTS: |
| 171 | void setHostname(const QString &hostname); |
| 172 | void setPort(quint16 port); |
| 173 | void setClientId(const QString &clientId); |
| 174 | void setKeepAlive(quint16 keepAlive); |
| 175 | void setProtocolVersion(ProtocolVersion protocolVersion); |
| 176 | void setState(ClientState state); |
| 177 | void setError(ClientError error); |
| 178 | void setUsername(const QString &username); |
| 179 | void setPassword(const QString &password); |
| 180 | void setCleanSession(bool cleanSession); |
| 181 | |
| 182 | void setWillTopic(const QString &willTopic); |
| 183 | void setWillQoS(quint8 willQoS); |
| 184 | void setWillMessage(const QByteArray &willMessage); |
| 185 | void setWillRetain(bool willRetain); |
| 186 | void setAutoKeepAlive(bool autoKeepAlive); |
| 187 | |
| 188 | private: |
| 189 | void connectToHost(bool encrypted, const QString &sslPeerName); |
| 190 | Q_DISABLE_COPY(QMqttClient) |
| 191 | Q_DECLARE_PRIVATE(QMqttClient) |
| 192 | }; |
| 193 | |
| 194 | Q_DECLARE_TYPEINFO(QMqttClient::TransportType, Q_PRIMITIVE_TYPE); |
| 195 | Q_DECLARE_TYPEINFO(QMqttClient::ClientState, Q_PRIMITIVE_TYPE); |
| 196 | Q_DECLARE_TYPEINFO(QMqttClient::ClientError, Q_PRIMITIVE_TYPE); |
| 197 | |
| 198 | QT_END_NAMESPACE |
| 199 | |
| 200 | Q_DECLARE_METATYPE(QMqttClient::TransportType) |
| 201 | Q_DECLARE_METATYPE(QMqttClient::ClientState) |
| 202 | Q_DECLARE_METATYPE(QMqttClient::ClientError) |
| 203 | |
| 204 | #endif // QTMQTTCLIENT_H |
| 205 | |