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