qmqtt_client.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * qmqtt_client.h - qmqtt client header
  3. *
  4. * Copyright (c) 2013 Ery Lee <ery.lee at gmail dot com>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * * Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * * Neither the name of mqttc nor the names of its contributors may be used
  16. * to endorse or promote products derived from this software without
  17. * specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. */
  32. #ifndef QMQTT_CLIENT_H
  33. #define QMQTT_CLIENT_H
  34. #include <qmqtt_global.h>
  35. #include <QObject>
  36. #include <QString>
  37. #include <QHostAddress>
  38. #include <QByteArray>
  39. #include <QAbstractSocket>
  40. #include <QScopedPointer>
  41. #include <QList>
  42. #ifdef QT_WEBSOCKETS_LIB
  43. #include <QWebSocket>
  44. #endif // QT_WEBSOCKETS_LIB
  45. #ifndef QT_NO_SSL
  46. #include <QSslConfiguration>
  47. QT_FORWARD_DECLARE_CLASS(QSslError)
  48. #endif // QT_NO_SSL
  49. #ifndef Q_ENUM_NS
  50. #define Q_ENUM_NS(x)
  51. #endif // Q_ENUM_NS
  52. namespace QMQTT {
  53. #if (QT_VERSION >= QT_VERSION_CHECK(5, 8, 0))
  54. Q_MQTT_EXPORT Q_NAMESPACE
  55. #endif
  56. static const quint8 LIBRARY_VERSION_MAJOR = 0;
  57. static const quint8 LIBRARY_VERSION_MINOR = 3;
  58. static const quint8 LIBRARY_VERSION_REVISION = 1;
  59. //static const char* LIBRARY_VERSION = "0.3.1";
  60. enum MQTTVersion
  61. {
  62. V3_1_0 = 3,
  63. V3_1_1 = 4
  64. };
  65. Q_ENUM_NS(MQTTVersion)
  66. enum ConnectionState
  67. {
  68. STATE_INIT = 0,
  69. STATE_CONNECTING,
  70. STATE_CONNECTED,
  71. STATE_DISCONNECTED
  72. };
  73. Q_ENUM_NS(ConnectionState)
  74. enum ClientError
  75. {
  76. UnknownError = 0,
  77. SocketConnectionRefusedError,
  78. SocketRemoteHostClosedError,
  79. SocketHostNotFoundError,
  80. SocketAccessError,
  81. SocketResourceError,
  82. SocketTimeoutError,
  83. SocketDatagramTooLargeError,
  84. SocketNetworkError,
  85. SocketAddressInUseError,
  86. SocketAddressNotAvailableError,
  87. SocketUnsupportedSocketOperationError,
  88. SocketUnfinishedSocketOperationError,
  89. SocketProxyAuthenticationRequiredError,
  90. SocketSslHandshakeFailedError,
  91. SocketProxyConnectionRefusedError,
  92. SocketProxyConnectionClosedError,
  93. SocketProxyConnectionTimeoutError,
  94. SocketProxyNotFoundError,
  95. SocketProxyProtocolError,
  96. SocketOperationError,
  97. SocketSslInternalError,
  98. SocketSslInvalidUserDataError,
  99. SocketTemporaryError,
  100. MqttUnacceptableProtocolVersionError=1<<16,
  101. MqttIdentifierRejectedError,
  102. MqttServerUnavailableError,
  103. MqttBadUserNameOrPasswordError,
  104. MqttNotAuthorizedError,
  105. MqttNoPingResponse
  106. };
  107. Q_ENUM_NS(ClientError)
  108. class ClientPrivate;
  109. class Message;
  110. class Frame;
  111. class NetworkInterface;
  112. class Q_MQTT_EXPORT Client : public QObject
  113. {
  114. Q_OBJECT
  115. Q_PROPERTY(quint16 _port READ port WRITE setPort)
  116. Q_PROPERTY(QHostAddress _host READ host WRITE setHost)
  117. Q_PROPERTY(QString _hostName READ hostName WRITE setHostName)
  118. Q_PROPERTY(QString _clientId READ clientId WRITE setClientId)
  119. Q_PROPERTY(QString _username READ username WRITE setUsername)
  120. Q_PROPERTY(QByteArray _password READ password WRITE setPassword)
  121. Q_PROPERTY(quint16 _keepAlive READ keepAlive WRITE setKeepAlive)
  122. Q_PROPERTY(MQTTVersion _version READ version WRITE setVersion)
  123. Q_PROPERTY(bool _autoReconnect READ autoReconnect WRITE setAutoReconnect)
  124. Q_PROPERTY(int _autoReconnectInterval READ autoReconnectInterval WRITE setAutoReconnectInterval)
  125. Q_PROPERTY(bool _cleanSession READ cleanSession WRITE setCleanSession)
  126. Q_PROPERTY(QString _willTopic READ willTopic WRITE setWillTopic)
  127. Q_PROPERTY(quint8 _willQos READ willQos WRITE setWillQos)
  128. Q_PROPERTY(bool _willRetain READ willRetain WRITE setWillRetain)
  129. Q_PROPERTY(QByteArray _willMessage READ willMessage WRITE setWillMessage)
  130. Q_PROPERTY(ConnectionState _connectionState READ connectionState)
  131. #ifndef QT_NO_SSL
  132. Q_PROPERTY(QSslConfiguration _sslConfiguration READ sslConfiguration WRITE setSslConfiguration)
  133. #endif // QT_NO_SSL
  134. public:
  135. Client(const QHostAddress& host = QHostAddress::LocalHost,
  136. const quint16 port = 1883,
  137. QObject* parent = nullptr);
  138. #ifndef QT_NO_SSL
  139. Client(const QString& hostName,
  140. const quint16 port,
  141. const QSslConfiguration& config,
  142. const bool ignoreSelfSigned=false,
  143. QObject* parent = nullptr);
  144. #endif // QT_NO_SSL
  145. // This function is provided for backward compatibility with older versions of QMQTT.
  146. // If the ssl parameter is true, this function will load a private key ('cert.key') and a local
  147. // certificate ('cert.crt') from the current working directory. It will also set PeerVerifyMode
  148. // to None. This may not be the safest way to set up an SSL connection.
  149. Client(const QString& hostName,
  150. const quint16 port,
  151. const bool ssl,
  152. const bool ignoreSelfSigned,
  153. QObject* parent = nullptr);
  154. #ifdef QT_WEBSOCKETS_LIB
  155. // Create a connection over websockets
  156. Client(const QString& url,
  157. const QString& origin,
  158. QWebSocketProtocol::Version version,
  159. bool ignoreSelfSigned = false,
  160. QObject* parent = nullptr);
  161. #ifndef QT_NO_SSL
  162. Client(const QString& url,
  163. const QString& origin,
  164. QWebSocketProtocol::Version version,
  165. const QSslConfiguration& config,
  166. const bool ignoreSelfSigned = false,
  167. QObject* parent = nullptr);
  168. #endif // QT_NO_SSL
  169. #endif // QT_WEBSOCKETS_LIB
  170. // for testing purposes only
  171. Client(NetworkInterface* network,
  172. const QHostAddress& host = QHostAddress::LocalHost,
  173. const quint16 port = 1883,
  174. QObject* parent = nullptr);
  175. virtual ~Client();
  176. QHostAddress host() const;
  177. QString hostName() const;
  178. quint16 port() const;
  179. QString clientId() const;
  180. QString username() const;
  181. QByteArray password() const;
  182. QMQTT::MQTTVersion version() const;
  183. quint16 keepAlive() const;
  184. bool cleanSession() const;
  185. bool autoReconnect() const;
  186. int autoReconnectInterval() const;
  187. ConnectionState connectionState() const;
  188. QString willTopic() const;
  189. quint8 willQos() const;
  190. bool willRetain() const;
  191. QByteArray willMessage() const;
  192. bool isConnectedToHost() const;
  193. #ifndef QT_NO_SSL
  194. QSslConfiguration sslConfiguration() const;
  195. void setSslConfiguration(const QSslConfiguration& config);
  196. #endif // QT_NO_SSL
  197. public Q_SLOTS:
  198. void setHost(const QHostAddress& host);
  199. void setHostName(const QString& hostName);
  200. void setPort(const quint16 port);
  201. void setClientId(const QString& clientId);
  202. void setUsername(const QString& username);
  203. void setPassword(const QByteArray& password);
  204. void setVersion(const MQTTVersion version);
  205. void setKeepAlive(const quint16 keepAlive);
  206. void setCleanSession(const bool cleanSession);
  207. void setAutoReconnect(const bool value);
  208. void setAutoReconnectInterval(const int autoReconnectInterval);
  209. void setWillTopic(const QString& willTopic);
  210. void setWillQos(const quint8 willQos);
  211. void setWillRetain(const bool willRetain);
  212. void setWillMessage(const QByteArray& willMessage);
  213. void connectToHost();
  214. void disconnectFromHost();
  215. void subscribe(const QString& topic, const quint8 qos = 0);
  216. void unsubscribe(const QString& topic);
  217. quint16 publish(const QMQTT::Message& message);
  218. #ifndef QT_NO_SSL
  219. void ignoreSslErrors();
  220. void ignoreSslErrors(const QList<QSslError>& errors);
  221. #endif // QT_NO_SSL
  222. Q_SIGNALS:
  223. void connected();
  224. void disconnected();
  225. void error(const QMQTT::ClientError error);
  226. void subscribed(const QString& topic, const quint8 qos = 0);
  227. void unsubscribed(const QString& topic);
  228. void published(const QMQTT::Message& message, quint16 msgid = 0);
  229. void received(const QMQTT::Message& message);
  230. void pingresp();
  231. #ifndef QT_NO_SSL
  232. void sslErrors(const QList<QSslError>& errors);
  233. #endif // QT_NO_SSL
  234. protected Q_SLOTS:
  235. void onNetworkConnected();
  236. void onNetworkDisconnected();
  237. void onNetworkReceived(const QMQTT::Frame& frame);
  238. void onTimerPingReq();
  239. void onPingTimeout();
  240. void onNetworkError(QAbstractSocket::SocketError error);
  241. #ifndef QT_NO_SSL
  242. void onSslErrors(const QList<QSslError>& errors);
  243. #endif // QT_NO_SSL
  244. protected:
  245. QScopedPointer<ClientPrivate> d_ptr;
  246. private:
  247. Q_DISABLE_COPY(Client)
  248. Q_DECLARE_PRIVATE(Client)
  249. };
  250. } // namespace QMQTT
  251. Q_DECLARE_METATYPE(QMQTT::ClientError)
  252. #endif // QMQTT_CLIENT_H