summaryrefslogtreecommitdiffstats
path: root/src/mqtt/qmqttclient.cpp
blob: 39f37015b6cc6b6878edca752ea90a942d2f1145 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
/******************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtMqtt module.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
******************************************************************************/

#include "qmqttclient.h"
#include "qmqttclient_p.h"

#include <QtCore/QLoggingCategory>
#include <QtCore/QUuid>
#include <QtCore/QtEndian>

QT_BEGIN_NAMESPACE

Q_LOGGING_CATEGORY(lcMqttClient, "qt.mqtt.client")

/*!
    \class QMqttClient

    \inmodule QtMqtt
    \brief The QMqttClient class represents the central access communicating
    with an MQTT broker.

    An MQTT client is a program or device that uses MQTT to create a network
    connection to an MQTT server, also called a \e broker. The connection
    request must contain a unique client identifier. Optionally, it can contain
    a Will Topic, Will Message, user name, and password.

    Once a connection is created, a client can send messages that other clients
    might be interested in receiving, subscribe to request notifications on
    topics, unsubscribe to remove a request for notifications, and disconnect
    from the broker.
*/

/*!
    \property QMqttClient::clientId
    \brief This property holds the client's identifier value.

    Each client needs to have a unique ID to be able to connect to an MQTT
    broker. If no client ID is specified by the user, one will be generated
    automatically when a connection is established.
*/

/*!
    \property QMqttClient::hostname
    \brief This property holds the hostname of the MQTT broker to connect to.

    If no transport is specified via setTransport(), the client will instantiate
    a socket connection to the specified hostname itself.
*/

/*!
    \property QMqttClient::port
    \brief This property holds the port to connect to the MQTT broker.

    If no transport is specified via setTransport(), the client will instantiate
    a socket connection to a host with this port number.
*/

/*!
    \property QMqttClient::keepAlive
    \brief This property holds the interval at which regular ping messages are
    sent to the broker.

    Once a connection to a broker is established, the client needs to send
    frequent updates to propagate it can still be reached. The interval between
    those updates is specified by this property.

    The interval is specified in seconds.
*/

/*!
    \property QMqttClient::protocolVersion
    \brief This property holds the MQTT standard version to use for connections.

    Specifies the version of the standard the client uses for connecting to a
    broker. Valid values are:

    \list
        \li 3: MQTT standard version 3.1.
        \li 4: MQTT standard version 3.1.1, often referred to MQTT 4.
    \endlist
*/

/*!
    \property QMqttClient::state
    \brief This property holds the current state of the client.
*/

/*!
    \property QMqttClient::error
    \brief Specifies the current error of the client.
*/

/*!
    \property QMqttClient::username
    \brief This property holds the user name for connecting to a broker.
*/

/*!
    \property QMqttClient::password
    \brief This property holds the password for connecting to a broker.
*/

/*!
    \property QMqttClient::cleanSession
    \brief This property holds the state after connecting to a broker.
*/

/*!
    \property QMqttClient::willTopic
    \brief This property holds the Will Topic.
*/

/*!
    \property QMqttClient::willMessage
    \brief This property holds the payload of a Will Message.
*/

/*!
    \property QMqttClient::willQoS
    \brief This property holds the level of QoS for sending and storing the
    Will Message.
*/

/*!
    \property QMqttClient::willRetain
    \brief This property holds whether the Will Message should be retained on
    the broker for future subscribers to receive.
*/

/*!
    \enum QMqttClient::TransportType

    This enum type specifies the connection method to be used to instantiate a
    connection to a broker.

    \value IODevice
           The transport uses a class based on a QIODevice.
    \value AbstractSocket
           The transport uses a class based on a QAbstractSocket.
    \value SecureSocket
           The transport uses a class based on a QSslSocket.
*/

/*!
    \enum QMqttClient::ClientState

    This enum type specifies the states a client can enter.

    \value Disconnected
           The client is disconnected from the broker.
    \value Connecting
           A connection request has been made, but the broker has not approved
           the connection yet.
    \value Connected
           The client is connected to the broker.
*/

/*!
    \enum QMqttClient::ClientError

    This enum type specifies the error state of a client.

    \value NoError
           No error occurred.
    \value InvalidProtocolVersion
           The broker does not accept a connection using the specified protocol
           version.
    \value IdRejected
           The client ID is malformed. This might be related to its length.
    \value ServerUnavailable
           The network connection has been established, but the service is
           unavailable on the broker side.
    \value BadUsernameOrPassword
           The data in the username or password is malformed.
    \value NotAuthorized
           The client is not authorized to connect.
    \value TransportInvalid
           The underlying transport caused an error. For example, the connection
           might have been interrupted unexpectedly.
    \value ProtocolViolation
           The client encountered a protocol violation, and therefore closed the
           connection.
    \value UnknownError
           An unknown error occurred.
    \value Mqtt5SpecificError
           The error is related to MQTT protocol level 5. A reason code might
           provide more details.
*/

/*!
    \enum QMqttClient::ProtocolVersion

    This enum specifies the protocol version of the MQTT standard to use during
    communication with a broker.

    \value MQTT_3_1
           MQTT Standard 3.1
    \value MQTT_3_1_1
           MQTT Standard 3.1.1, publicly referred to as version 4
    \value MQTT_5_0
           MQTT Standard 5.0
*/

/*!
    \fn QMqttClient::connected()

    This signal is emitted when a connection has been established.
*/

/*!
    \fn QMqttClient::disconnected()

    This signal is emitted when a connection has been closed. A connection may
    be closed when disconnectFromHost() is called or when the broker
    disconnects.
*/

/*!
    \fn QMqttClient::messageReceived(const QByteArray &message, const QMqttTopicName &topic)

    This signal is emitted when a new message has been received. The category of
    the message is specified by \a topic with the content being \a message.
*/

/*!
    \fn QMqttClient::messageStatusChanged(qint32 id, QMqtt::MessageStatus s, const QMqttMessageStatusProperties &properties);
    \since 5.12

    This signal is emitted when the status for the message identified by \a id
    changes. \a s specifies the new status of the message, and
    \a properties specify additional properties provided by the server.
*/

/*!
    \fn QMqttClient::messageSent(qint32 id)

    Indicates that a message that was sent via the publish() function has been
    received by the broker. The \a id is the same as returned by \c publish() to
    help tracking the status of the message.
*/

/*!
    \fn QMqttClient::pingResponseReceived()

    This signal is emitted after the broker responds to a requestPing() call or
    a keepAlive() ping message, and the connection is still valid.
*/

/*!
    \fn QMqttClient::brokerSessionRestored()

    This signal is emitted after a client has successfully connected to a broker
    with the cleanSession property set to \c false, and the broker has restored
    the session.

    Sessions can be restored if a client has connected previously using the same
    clientId.
*/

/*!
    \since 5.12
    \fn QMqttClient::authenticationRequested(const QMqttAuthenticationProperties &p)

    This signal is emitted after a client invoked QMqttClient::connectToHost or
    QMqttClient::connectToHostEncrypted and before the connection is
    established. In extended authentication, a broker might request additional
    details which need to be provided by invoking QMqttClient::authenticate.
    \a p specifies properties provided by the broker.

    \note Extended authentication is part of the MQTT 5.0 standard and can
    only be used when the client specifies MQTT_5_0 as ProtocolVersion.

    \sa authenticationFinished(), authenticate()
*/

/*!
    \since 5.12
    \fn QMqttClient::authenticationFinished(const QMqttAuthenticationProperties &p)

    This signal is emitted after extended authentication has finished. \a p
    specifies available details on the authentication process.

    After successful authentication QMqttClient::connected is emitted.

    \note Extended authentication is part of the MQTT 5.0 standard and can
    only be used when the client specifies MQTT_5_0 as ProtocolVersion.

    \sa authenticationRequested(), authenticate()
*/

/*
    Creates a new MQTT client instance with the specified \a parent.
 */
QMqttClient::QMqttClient(QObject *parent) : QObject(*(new QMqttClientPrivate(this)), parent)
{
    Q_D(QMqttClient);
    d->m_connection.setClientPrivate(d);
}

/*!
    Sets the transport to \a device. A transport can be either a socket type
    or derived from QIODevice and is specified by \a transport.

    \note The transport can only be exchanged if the MQTT client is in the
    \l Disconnected state.

    \note Setting a custom transport for a client does not pass over responsibility
    on connection management. The transport has to be opened for QIODevice based
    transports or connected for socket type transports before calling QMqttClient::connectToHost().
 */
void QMqttClient::setTransport(QIODevice *device, QMqttClient::TransportType transport)
{
    Q_D(QMqttClient);

    if (d->m_state != Disconnected) {
        qCDebug(lcMqttClient) << "Changing transport layer while connected is not possible.";
        return;
    }
    d->m_connection.setTransport(device, transport);
}

/*!
    Returns the transport used for communication with the broker.
 */
QIODevice *QMqttClient::transport() const
{
    Q_D(const QMqttClient);
    return d->m_connection.transport();
}

/*!
    Adds a new subscription to receive notifications on \a topic. The parameter
    \a qos specifies the level at which security messages are received. For more
    information about the available QoS levels, see \l {Quality of Service}.

    This function returns a pointer to a \l QMqttSubscription. If the same topic
    is subscribed twice, the return value points to the same subscription
    instance. The MQTT client is the owner of the subscription.
 */
QMqttSubscription *QMqttClient::subscribe(const QMqttTopicFilter &topic, quint8 qos)
{
    return subscribe(topic, QMqttSubscriptionProperties(), qos);
}

/*!
    \since 5.12

    Adds a new subscription to receive notifications on \a topic. The parameter
    \a properties specifies additional subscription properties to be validated
    by the broker. The parameter \a qos specifies the level at which security
    messages are received. For more information about the available QoS levels,
    see \l {Quality of Service}.

    This function returns a pointer to a \l QMqttSubscription. If the same topic
    is subscribed twice, the return value points to the same subscription
    instance. The MQTT client is the owner of the subscription.

    \note \a properties will only be passed to the broker when the client
    specifies MQTT_5_0 as ProtocolVersion.
*/
QMqttSubscription *QMqttClient::subscribe(const QMqttTopicFilter &topic, const QMqttSubscriptionProperties &properties, quint8 qos)
{
    Q_D(QMqttClient);

    if (d->m_state != QMqttClient::Connected)
        return nullptr;

    return d->m_connection.sendControlSubscribe(topic, qos, properties);
}

/*!
    Unsubscribes from \a topic. No notifications will be sent to any of the
    subscriptions made by calling subscribe().

    \note If a client disconnects from a broker without unsubscribing, the
    broker will store all messages and publish them on the next reconnect.
 */
void QMqttClient::unsubscribe(const QMqttTopicFilter &topic)
{
    unsubscribe(topic, QMqttUnsubscriptionProperties());
}

/*!
    \since 5.12

    Unsubscribes from \a topic. No notifications will be sent to any of the
    subscriptions made by calling subscribe(). \a properties specifies
    additional user properties to be passed to the broker.

    \note If a client disconnects from a broker without unsubscribing, the
    broker will store all messages and publish them on the next reconnect.

    \note \a properties will only be passed to the broker when the client
    specifies MQTT_5_0 as ProtocolVersion.
*/
void QMqttClient::unsubscribe(const QMqttTopicFilter &topic, const QMqttUnsubscriptionProperties &properties)
{
    Q_D(QMqttClient);
    d->m_connection.sendControlUnsubscribe(topic, properties);
}

/*!
    Publishes a \a message to the broker with the specified \a topic. \a qos
    specifies the level of security required for transferring the message.

    If \a retain is set to \c true, the message will stay on the broker for
    other clients to connect and receive the message.

    Returns an ID that is used internally to identify the message.
*/
qint32 QMqttClient::publish(const QMqttTopicName &topic, const QByteArray &message, quint8 qos, bool retain)
{
    return publish(topic, QMqttPublishProperties(), message, qos, retain);
}

/*!
    \since 5.12

    Publishes a \a message to the broker with the specified \a properties and
    \a topic. \a qos specifies the level of security required for transferring
    the message.

    If \a retain is set to \c true, the message will stay on the broker for
    other clients to connect and receive the message.

    Returns an ID that is used internally to identify the message.

    \note \a properties will only be passed to the broker when the client
    specifies MQTT_5_0 as ProtocolVersion.
*/
qint32 QMqttClient::publish(const QMqttTopicName &topic, const QMqttPublishProperties &properties,
                            const QByteArray &message, quint8 qos, bool retain)
{
    Q_D(QMqttClient);
    if (qos > 2)
        return -1;

    if (d->m_state != QMqttClient::Connected)
        return -1;

    return d->m_connection.sendControlPublish(topic, message, qos, retain, properties);
}

/*!
    Sends a ping message to the broker and expects a reply. If the connection
    is active, the MQTT client will automatically send a ping message at
    \l keepAlive intervals.

    To check whether the ping is successful, connect to the
    \l pingResponseReceived() signal.

    Returns \c true if the ping request could be sent.
 */
bool QMqttClient::requestPing()
{
    Q_D(QMqttClient);
    return d->m_connection.sendControlPingRequest();
}

QString QMqttClient::hostname() const
{
    Q_D(const QMqttClient);
    return d->m_hostname;
}

quint16 QMqttClient::port() const
{
    Q_D(const QMqttClient);
    return d->m_port;
}

/*!
    Initiates a connection to the MQTT broker.
 */
void QMqttClient::connectToHost()
{
    connectToHost(false, QString());
}

/*!
    Initiates an encrypted connection to the MQTT broker.

    \a sslPeerName specifies the peer name to be passed to the socket.
 */
#ifndef QT_NO_SSL
void QMqttClient::connectToHostEncrypted(const QString &sslPeerName)
{
    connectToHost(true, sslPeerName);
}
#endif

void QMqttClient::connectToHost(bool encrypted, const QString &sslPeerName)
{
    Q_D(QMqttClient);

    if (state() == QMqttClient::Connecting) {
        qCDebug(lcMqttClient) << "Connection request currently ongoing.";
        return;
    }

    if (state() == QMqttClient::Connected) {
        qCDebug(lcMqttClient) << "Already connected to a broker. Rejecting connection request.";
        return;
    }

    if (!d->m_connection.ensureTransport(encrypted)) {
        qCDebug(lcMqttClient) << "Could not ensure connection.";
        d->setStateAndError(Disconnected, TransportInvalid);
        return;
    }
    d->m_error = QMqttClient::NoError; // Fresh reconnect, unset error
    d->setStateAndError(Connecting);

    if (d->m_cleanSession)
        d->m_connection.cleanSubscriptions();

    if (!d->m_connection.ensureTransportOpen(sslPeerName)) {
        qCDebug(lcMqttClient) << "Could not ensure that connection is open.";
        d->setStateAndError(Disconnected, TransportInvalid);
        return;
    }

    // Once transport has connected, it will invoke
    // QMqttConnection::sendControlConnect to
    // handshake with the broker
}

/*!
    Disconnects from the MQTT broker.
 */
void QMqttClient::disconnectFromHost()
{
    Q_D(QMqttClient);

    switch (d->m_connection.internalState()) {
    case QMqttConnection::BrokerConnected:
        d->m_connection.sendControlDisconnect();
        break;
    case QMqttConnection::BrokerDisconnected:
        break;
    case QMqttConnection::BrokerConnecting:
    case QMqttConnection::BrokerWaitForConnectAck:
        d->m_connection.m_transport->close();
        break;
    }
}

QMqttClient::ClientState QMqttClient::state() const
{
    Q_D(const QMqttClient);
    return d->m_state;
}

QString QMqttClient::username() const
{
    Q_D(const QMqttClient);
    return d->m_username;
}

QString QMqttClient::password() const
{
    Q_D(const QMqttClient);
    return d->m_password;
}

bool QMqttClient::cleanSession() const
{
    Q_D(const QMqttClient);
    return d->m_cleanSession;
}

QString QMqttClient::willTopic() const
{
    Q_D(const QMqttClient);
    return d->m_willTopic;
}

quint8 QMqttClient::willQoS() const
{
    Q_D(const QMqttClient);
    return d->m_willQoS;
}

QByteArray QMqttClient::willMessage() const
{
    Q_D(const QMqttClient);
    return d->m_willMessage;
}

bool QMqttClient::willRetain() const
{
    Q_D(const QMqttClient);
    return d->m_willRetain;
}

/*!
    \since 5.12

    Sets the connection properties to \a prop. \l QMqttConnectionProperties
    can be used to ask the server to use a specific feature set. After a
    connection request the server response can be obtained by calling
    \l QMqttClient::serverConnectionProperties.

    \note The connection properties can only be set if the MQTT client is in the
    \l Disconnected state.

    \note QMqttConnectionProperties can only be used when the client specifies
    MQTT_5_0 as ProtocolVersion.
*/
void QMqttClient::setConnectionProperties(const QMqttConnectionProperties &prop)
{
    Q_D(QMqttClient);
    d->m_connectionProperties = prop;
}

/*!
    \since 5.12

    Returns the connection properties the client requests to the broker.

    \note QMqttConnectionProperties can only be used when the client specifies
    MQTT_5_0 as ProtocolVersion.
*/
QMqttConnectionProperties QMqttClient::connectionProperties() const
{
    Q_D(const QMqttClient);
    return d->m_connectionProperties;
}

/*!
    \since 5.12

    Sets the last will properties to \a prop. QMqttLastWillProperties allows
    to set additional features for the last will message stored at the broker.

    \note The connection properties can only be set if the MQTT client is in the
    \l Disconnected state.

    \note QMqttLastWillProperties can only be used when the client specifies
    MQTT_5_0 as ProtocolVersion.
*/
void QMqttClient::setLastWillProperties(const QMqttLastWillProperties &prop)
{
    Q_D(QMqttClient);
    d->m_lastWillProperties = prop;
}

/*!
    \since 5.12

    Returns the last will properties.

    \note QMqttLastWillProperties can only be used when the client specifies
    MQTT_5_0 as ProtocolVersion.
*/
QMqttLastWillProperties QMqttClient::lastWillProperties() const
{
    Q_D(const QMqttClient);
    return d->m_lastWillProperties;
}

/*!
    \since 5.12

    Returns the QMqttServerConnectionProperties the broker returned after a
    connection attempt.

    This can be used to verify that client side connection properties set by
    QMqttClient::setConnectionProperties have been accepted by the broker. Also,
    in case of a failed connection attempt, it can be used for connection
    diagnostics.

    \note QMqttServerConnectionProperties can only be used when the client
    specifies MQTT_5_0 as ProtocolVersion.

    \sa connectionProperties()
*/
QMqttServerConnectionProperties QMqttClient::serverConnectionProperties() const
{
    Q_D(const QMqttClient);
    return d->m_serverConnectionProperties;
}

/*!
    \since 5.12

    Sends an authentication request to the broker. \a prop specifies
    the required information to fulfill the authentication request.

    This function should only be called after a
    QMqttClient::authenticationRequested signal has been emitted.

    \note Extended authentication is part of the MQTT 5.0 standard and can
    only be used when the client specifies MQTT_5_0 as ProtocolVersion.

    \sa authenticationRequested(), authenticationFinished()
*/
void QMqttClient::authenticate(const QMqttAuthenticationProperties &prop)
{
    Q_D(QMqttClient);
    if (protocolVersion() != QMqttClient::MQTT_5_0) {
        qCDebug(lcMqttClient) << "Authentication is only supported on protocol level 5.";
        return;
    }
    if (state() == QMqttClient::Disconnected) {
        qCDebug(lcMqttClient) << "Cannot send authentication request while disconnected.";
        return;
    }
    d->m_connection.sendControlAuthenticate(prop);
}

QMqttClient::ClientError QMqttClient::error() const
{
    Q_D(const QMqttClient);
    return d->m_error;
}

QMqttClient::ProtocolVersion QMqttClient::protocolVersion() const
{
    Q_D(const QMqttClient);
    return d->m_protocolVersion;
}

QString QMqttClient::clientId() const
{
    Q_D(const QMqttClient);
    return d->m_clientId;
}

quint16 QMqttClient::keepAlive() const
{
    Q_D(const QMqttClient);
    return d->m_keepAlive;
}

void QMqttClient::setHostname(const QString &hostname)
{
    Q_D(QMqttClient);

    if (state() != QMqttClient::Disconnected) {
        qCDebug(lcMqttClient) << "Changing hostname while connected is not possible.";
        return;
    }

    if (d->m_hostname == hostname)
        return;

    d->m_hostname = hostname;
    emit hostnameChanged(hostname);
}

void QMqttClient::setPort(quint16 port)
{
    Q_D(QMqttClient);

    if (state() != QMqttClient::Disconnected) {
        qCDebug(lcMqttClient) << "Changing port while connected is not possible.";
        return;
    }

    if (d->m_port == port)
        return;

    d->m_port = port;
    emit portChanged(port);
}

void QMqttClient::setClientId(const QString &clientId)
{
    Q_D(QMqttClient);

    if (state() != QMqttClient::Disconnected) {
        qCDebug(lcMqttClient) << "Changing client ID while connected is not possible.";
        return;
    }
    d->setClientId(clientId);
}

void QMqttClient::setKeepAlive(quint16 keepAlive)
{
    Q_D(QMqttClient);
    if (d->m_keepAlive == keepAlive)
        return;

    if (state() != QMqttClient::Disconnected) {
        qCDebug(lcMqttClient) << "Changing keepAlive while connected is not possible.";
        return;
    }

    d->m_keepAlive = keepAlive;
    emit keepAliveChanged(keepAlive);
}

void QMqttClient::setProtocolVersion(ProtocolVersion protocolVersion)
{
    Q_D(QMqttClient);

    if (state() != QMqttClient::Disconnected) {
        qCDebug(lcMqttClient) << "Changing protocol version while connected is not possible.";
        return;
    }

    if (d->m_protocolVersion == protocolVersion)
        return;

    if (protocolVersion < 3 || protocolVersion > 5)
        return;

    d->m_protocolVersion = protocolVersion;
    emit protocolVersionChanged(protocolVersion);
}

void QMqttClient::setState(ClientState state)
{
    Q_D(QMqttClient);
    if (d->m_state == state)
        return;

    d->m_state = state;
    emit stateChanged(state);
    if (d->m_state == QMqttClient::Disconnected)
        emit disconnected();
    else if (d->m_state == QMqttClient::Connected)
        emit connected();
}

void QMqttClient::setUsername(const QString &username)
{
    Q_D(QMqttClient);

    if (state() != QMqttClient::Disconnected) {
        qCDebug(lcMqttClient) << "Changing username while connected is not possible.";
        return;
    }

    if (d->m_username == username)
        return;

    d->m_username = username;
    emit usernameChanged(username);
}

void QMqttClient::setPassword(const QString &password)
{
    Q_D(QMqttClient);

    if (state() != QMqttClient::Disconnected) {
        qCDebug(lcMqttClient) << "Changing password while connected is not possible.";
        return;
    }

    if (d->m_password == password)
        return;

    d->m_password = password;
    emit passwordChanged(password);
}

void QMqttClient::setCleanSession(bool cleanSession)
{
    Q_D(QMqttClient);

    if (state() != QMqttClient::Disconnected) {
        qCDebug(lcMqttClient) << "Changing clean session while connected is not possible.";
        return;
    }

    if (d->m_cleanSession == cleanSession)
        return;

    d->m_cleanSession = cleanSession;
    emit cleanSessionChanged(cleanSession);
}

void QMqttClient::setWillTopic(const QString &willTopic)
{
    Q_D(QMqttClient);

    if (state() != QMqttClient::Disconnected) {
        qCDebug(lcMqttClient) << "Changing will topic while connected is not possible.";
        return;
    }

    if (d->m_willTopic == willTopic)
        return;

    d->m_willTopic = willTopic;
    emit willTopicChanged(willTopic);
}

void QMqttClient::setWillQoS(quint8 willQoS)
{
    Q_D(QMqttClient);

    if (state() != QMqttClient::Disconnected) {
        qCDebug(lcMqttClient) << "Changing will qos while connected is not possible.";
        return;
    }

    if (d->m_willQoS == willQoS)
        return;

    d->m_willQoS = willQoS;
    emit willQoSChanged(willQoS);
}

void QMqttClient::setWillMessage(const QByteArray &willMessage)
{
    Q_D(QMqttClient);

    if (state() != QMqttClient::Disconnected) {
        qCDebug(lcMqttClient) << "Changing will message while connected is not possible.";
        return;
    }

    if (d->m_willMessage == willMessage)
        return;

    d->m_willMessage = willMessage;
    emit willMessageChanged(willMessage);
}

void QMqttClient::setWillRetain(bool willRetain)
{
    Q_D(QMqttClient);

    if (state() != QMqttClient::Disconnected) {
        qCDebug(lcMqttClient) << "Changing will retain while connected is not possible.";
        return;
    }

    if (d->m_willRetain == willRetain)
        return;

    d->m_willRetain = willRetain;
    emit willRetainChanged(willRetain);
}

void QMqttClient::setError(ClientError e)
{
    Q_D(QMqttClient);
    if (d->m_error == e)
        return;

    d->m_error = e;
    emit errorChanged(d->m_error);
}

QMqttClientPrivate::QMqttClientPrivate(QMqttClient *c)
    : QObjectPrivate()
{
    m_client = c;
    m_clientId = QUuid::createUuid().toString();
    m_clientId.remove(QLatin1Char('{'));
    m_clientId.remove(QLatin1Char('}'));
    m_clientId.remove(QLatin1Char('-'));
    m_clientId.resize(23);
#ifdef QT_BUILD_INTERNAL
    // Some test servers require a username token
    if (qEnvironmentVariableIsSet("QT_MQTT_TEST_USERNAME"))
        m_username = qEnvironmentVariable("QT_MQTT_TEST_USERNAME");
    if (qEnvironmentVariableIsSet("QT_MQTT_TEST_PASSWORD"))
        m_password = qEnvironmentVariable("QT_MQTT_TEST_PASSWORD");
    if (qEnvironmentVariableIsSet("QT_MQTT_TEST_CLIENTID"))
        m_clientId = qEnvironmentVariable("QT_MQTT_TEST_CLIENTID");
#endif
}

QMqttClientPrivate::~QMqttClientPrivate()
{
}

void QMqttClientPrivate::setStateAndError(QMqttClient::ClientState s, QMqttClient::ClientError e)
{
    Q_Q(QMqttClient);

    if (s != m_state)
        q->setState(s);
    if (e != QMqttClient::NoError && m_error != e)
        q->setError(e);
}

void QMqttClientPrivate::setClientId(const QString &id)
{
    Q_Q(QMqttClient);

    if (m_clientId == id)
        return;

    m_clientId = id;
    emit q->clientIdChanged(id);
}

QT_END_NAMESPACE