1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qmqttconnectionproperties.h"
5
6#include "qmqttconnectionproperties_p.h"
7
8#include <QtCore/QLoggingCategory>
9
10#include <limits>
11
12QT_BEGIN_NAMESPACE
13
14Q_DECLARE_LOGGING_CATEGORY(lcMqttConnection)
15
16/*!
17 \class QMqttConnectionProperties
18
19 \inmodule QtMqtt
20 \since 5.12
21
22 \brief The QMqttConnectionProperties class represents configuration
23 options a QMqttClient can pass to the server when invoking
24 QMqttClient::connectToHost().
25
26 \note Connection properties are part of the MQTT 5.0 specification and
27 cannot be used when connecting with a lower protocol level. See
28 QMqttClient::ProtocolVersion for more information.
29*/
30
31/*!
32 \class QMqttServerConnectionProperties
33
34 \inmodule QtMqtt
35 \since 5.12
36
37 \brief The QMqttServerConnectionProperties class represents configuration
38 options of a server a QMqttClient is connected to.
39
40 When a connection has been established the server might send additional
41 details about the connection properties. Use availableProperties() to
42 identify properties set by the server. If a property is not set by the
43 server, default values are assumed and can be obtained by invoking access
44 functions of this instance.
45
46 \note Connection properties are part of the MQTT 5.0 specification and
47 cannot be used when connecting with a lower protocol level. See
48 QMqttClient::ProtocolVersion for more information.
49*/
50
51/*!
52 \class QMqttLastWillProperties
53
54 \inmodule QtMqtt
55 \since 5.12
56
57 \brief The QMqttLastWillProperties class represents configuration
58 options a QMqttClient can pass to the server when specifying the last will
59 during connecting to a server.
60
61 \note Last Will properties are part of the MQTT 5.0 specification and
62 cannot be used when connecting with a lower protocol level. See
63 QMqttClient::ProtocolVersion for more information.
64*/
65
66/*!
67 \enum QMqttServerConnectionProperties::ServerPropertyDetail
68
69 This enum type specifies the available properties set by the
70 server or the client after establishing a connection.
71
72 \value None
73 No property has been specified.
74 \value SessionExpiryInterval
75 The number of seconds the server keeps the session after
76 a disconnect.
77 \value MaximumReceive
78 The maximum number of QoS 1 and 2 message the server is
79 capable of managing concurrently.
80 \value MaximumQoS
81 The maximum QoS level the server can understand.
82 \value RetainAvailable
83 Specifies whether retained messages are supported.
84 \value MaximumPacketSize
85 Specifies the maximum packet size including the message header
86 and properties.
87 \value AssignedClientId
88 Specifies whether the server assigned a client identifier.
89 \value MaximumTopicAlias
90 Specifies the maximum amount of topic aliases.
91 \value ReasonString
92 Specifies a string providing more details on connection state.
93 \value UserProperty
94 Specifies additional user properties.
95 \value WildCardSupported
96 Specifies whether the server supports wildcard subscriptions.
97 \value SubscriptionIdentifierSupport
98 Specifies whether the server supports subscription identifiers.
99 \value SharedSubscriptionSupport
100 Specifies whether the server supports shared subscriptions.
101 \value ServerKeepAlive
102 Specifies the number of seconds the server expects a keep alive
103 packet from the client.
104 \value ResponseInformation
105 Specifies the response information.
106 \value ServerReference
107 Specifies an alternative server address for the client to
108 connect to.
109 \value AuthenticationMethod
110 Specifies the authentication method.
111 \value AuthenticationData
112 Specifies the authentication data.
113*/
114
115/*!
116 \internal
117*/
118QMqttLastWillProperties::QMqttLastWillProperties() : data(new QMqttLastWillPropertiesData)
119{
120}
121
122/*!
123 \internal
124*/
125QMqttLastWillProperties::QMqttLastWillProperties(const QMqttLastWillProperties &) = default;
126
127QMqttLastWillProperties &QMqttLastWillProperties::operator=(const QMqttLastWillProperties &rhs)
128{
129 if (this != &rhs)
130 data.operator=(o: rhs.data);
131 return *this;
132}
133
134QMqttLastWillProperties::~QMqttLastWillProperties() = default;
135
136/*!
137 Returns the delay in seconds a last will message will be sent after
138 disconnecting from the server.
139*/
140quint32 QMqttLastWillProperties::willDelayInterval() const
141{
142 return data->willDelayInterval;
143}
144
145/*!
146 Returns the payload format indicator.
147*/
148QMqtt::PayloadFormatIndicator QMqttLastWillProperties::payloadFormatIndicator() const
149{
150 return data->formatIndicator;
151}
152
153/*!
154 Returns the lifetime of the last will message in seconds, starting from
155 the will delay interval.
156*/
157quint32 QMqttLastWillProperties::messageExpiryInterval() const
158{
159 return data->messageExpiryInterval;
160}
161
162/*!
163 Returns the content type of the last will message.
164*/
165QString QMqttLastWillProperties::contentType() const
166{
167 return data->contentType;
168}
169
170/*!
171 Returns the topic that subscribers to the last will message should respond
172 to.
173*/
174QString QMqttLastWillProperties::responseTopic() const
175{
176 return data->responseTopic;
177}
178
179/*!
180 Returns the correlation data to identify the request.
181*/
182QByteArray QMqttLastWillProperties::correlationData() const
183{
184 return data->correlationData;
185}
186
187/*!
188 Returns the user properties.
189*/
190QMqttUserProperties QMqttLastWillProperties::userProperties() const
191{
192 return data->userProperties;
193}
194
195/*!
196 Sets the will delay interval to \a delay.
197*/
198void QMqttLastWillProperties::setWillDelayInterval(quint32 delay)
199{
200 data->willDelayInterval = delay;
201}
202
203/*!
204 Sets the payload format indicator to \a p.
205*/
206void QMqttLastWillProperties::setPayloadFormatIndicator(QMqtt::PayloadFormatIndicator p)
207{
208 data->formatIndicator = p;
209}
210
211/*!
212 Sets the message expiry interval to \a expiry.
213*/
214void QMqttLastWillProperties::setMessageExpiryInterval(quint32 expiry)
215{
216 data->messageExpiryInterval = expiry;
217}
218
219/*!
220 Sets the content type to \a content.
221*/
222void QMqttLastWillProperties::setContentType(const QString &content)
223{
224 data->contentType = content;
225}
226
227/*!
228 Sets the response topic to \a response.
229*/
230void QMqttLastWillProperties::setResponseTopic(const QString &response)
231{
232 data->responseTopic = response;
233}
234
235/*!
236 Sets the correlation data to \a correlation.
237*/
238void QMqttLastWillProperties::setCorrelationData(const QByteArray &correlation)
239{
240 data->correlationData = correlation;
241}
242
243/*!
244 Sets the user properties to \a properties.
245*/
246void QMqttLastWillProperties::setUserProperties(const QMqttUserProperties &properties)
247{
248 data->userProperties = properties;
249}
250
251/*!
252 \internal
253*/
254QMqttConnectionProperties::QMqttConnectionProperties() : data(new QMqttConnectionPropertiesData)
255{
256
257}
258
259/*!
260 \internal
261*/
262QMqttConnectionProperties::QMqttConnectionProperties(const QMqttConnectionProperties &) = default;
263
264QMqttConnectionProperties &QMqttConnectionProperties::operator=(const QMqttConnectionProperties &rhs)
265{
266 if (this != &rhs)
267 data.operator=(o: rhs.data);
268 return *this;
269}
270
271QMqttConnectionProperties::~QMqttConnectionProperties() = default;
272
273/*!
274 Sets the session expiry interval to \a expiry. The session expiry interval
275 specifies the number of seconds a server holds information on the client
276 state after a connection has been closed.
277
278 The default value is 0, which specifies that the session is closed when
279 the network connection ends. If the value is specified as maximum of
280 quint32, then the session does not expire.
281*/
282void QMqttConnectionProperties::setSessionExpiryInterval(quint32 expiry)
283{
284 data->sessionExpiryInterval = expiry;
285}
286
287/*!
288 Sets the maximum amount of QoS 1 and QoS 2 publications
289 that the client is willing to process concurrently for this session
290 to \a maximumReceive.
291
292 A maximum receive value of 0 is not allowed.
293*/
294void QMqttConnectionProperties::setMaximumReceive(quint16 maximumReceive)
295{
296 if (maximumReceive == 0) {
297 qCDebug(lcMqttConnection) << "Maximum Receive is not allowed to be 0.";
298 return;
299 }
300 data->maximumReceive = maximumReceive;
301}
302
303/*!
304 Sets the maximum packet size to \a packetSize. The maximum packet size
305 specifies the maximum size one packet can contain. This includes the
306 packet header and its properties.
307
308 If no maximum packet size is specified, no limit is imposed beyond the
309 limitations of the protocol itself.
310*/
311void QMqttConnectionProperties::setMaximumPacketSize(quint32 packetSize)
312{
313 if (packetSize == 0) {
314 qCDebug(lcMqttConnection) << "Packet size is not allowed to be 0.";
315 return;
316 }
317 data->maximumPacketSize = packetSize;
318}
319
320/*!
321 Sets the maximum topic alias to \a alias. The maximum topic alias specifies
322 the highest value that the client will accept from the server. The client
323 uses this value to limit the number of topic aliases it is willing to hold
324 for the connection.
325
326 The default value is 0. 0 indicates that the client does not accept any
327 topic aliases on this connection.
328*/
329void QMqttConnectionProperties::setMaximumTopicAlias(quint16 alias)
330{
331 data->maximumTopicAlias = alias;
332}
333
334/*!
335 Sets the request response information to \a response. A client uses this
336 to request the server to return response information after the connection
337 request has been handled.
338
339 The default value is \c false, which indicates that the client must not
340 return any response information. If the value is \c true, the server
341 may return response information, but is not enforced to do so.
342*/
343void QMqttConnectionProperties::setRequestResponseInformation(bool response)
344{
345 data->requestResponseInformation = response;
346}
347
348/*!
349 Sets the request problem information to \a problem. A client uses this
350 to request the server to return additional information in case of failure.
351 Types of failure include connection and message management on the server
352 side.
353
354 The default value is \c false, which indicates that the client must not
355 receive any problem information for anything but connection management.
356 The server still may send problem information for connection handling.
357 If the value is \c true, the server may return problem information.
358
359 Problem information is available in user properties or reason strings
360 of the property classes.
361*/
362void QMqttConnectionProperties::setRequestProblemInformation(bool problem)
363{
364 data->requestProblemInformation = problem;
365}
366
367/*!
368 Sets the user properties of the connection to \a properties.
369
370 The default value is to not send any user information.
371*/
372void QMqttConnectionProperties::setUserProperties(const QMqttUserProperties &properties)
373{
374 data->userProperties = properties;
375}
376
377/*!
378 Sets the authentication method to \a authMethod.
379
380 \sa authenticationData()
381*/
382void QMqttConnectionProperties::setAuthenticationMethod(const QString &authMethod)
383{
384 data->authenticationMethod = authMethod;
385}
386
387/*!
388 Sets the authentication data to \a authData.
389
390 Authentication data can only be used if an authentication method has
391 been specified.
392
393 \sa authenticationMethod()
394*/
395void QMqttConnectionProperties::setAuthenticationData(const QByteArray &authData)
396{
397 data->authenticationData = authData;
398}
399
400/*!
401 Returns the session expiry interval.
402*/
403quint32 QMqttConnectionProperties::sessionExpiryInterval() const
404{
405 return data->sessionExpiryInterval;
406}
407
408/*!
409 Returns the maximum amount of QoS 1 and QoS 2 publications
410 that the client (when obtained from \l QMqttClient::connectionProperties())
411 or the server (when obtained from \l QMqttClient::serverConnectionProperties())
412 is willing to process concurrently for this session.
413*/
414quint16 QMqttConnectionProperties::maximumReceive() const
415{
416 return data->maximumReceive;
417}
418
419/*!
420 Returns the maximum packet size the client can receive.
421*/
422quint32 QMqttConnectionProperties::maximumPacketSize() const
423{
424 return data->maximumPacketSize;
425}
426
427/*!
428 Returns the maximum topic alias ID the client can use.
429*/
430quint16 QMqttConnectionProperties::maximumTopicAlias() const
431{
432 return data->maximumTopicAlias;
433}
434
435/*!
436 Returns whether the client should receive response information.
437*/
438bool QMqttConnectionProperties::requestResponseInformation() const
439{
440 return data->requestResponseInformation;
441}
442
443/*!
444 Returns whether the client should receive problem information.
445*/
446bool QMqttConnectionProperties::requestProblemInformation() const
447{
448 return data->requestProblemInformation;
449}
450
451/*!
452 Returns the user properties for the connection.
453*/
454QMqttUserProperties QMqttConnectionProperties::userProperties() const
455{
456 return data->userProperties;
457}
458
459/*!
460 Returns the authentication method.
461*/
462QString QMqttConnectionProperties::authenticationMethod() const
463{
464 return data->authenticationMethod;
465}
466
467/*!
468 Returns the authentication data.
469*/
470QByteArray QMqttConnectionProperties::authenticationData() const
471{
472 return data->authenticationData;
473}
474
475/*!
476 \internal
477*/
478QMqttServerConnectionProperties::QMqttServerConnectionProperties()
479 : QMqttConnectionProperties()
480 , serverData(new QMqttServerConnectionPropertiesData)
481{
482
483}
484
485/*!
486 \internal
487*/
488QMqttServerConnectionProperties::QMqttServerConnectionProperties(const QMqttServerConnectionProperties &rhs)
489 : QMqttConnectionProperties(rhs)
490 , serverData(rhs.serverData)
491{
492
493}
494
495QMqttServerConnectionProperties &QMqttServerConnectionProperties::operator=(const QMqttServerConnectionProperties &rhs)
496{
497 if (this != &rhs) {
498 serverData.operator=(o: rhs.serverData);
499 QMqttConnectionProperties::operator=(rhs);
500 }
501 return *this;
502}
503
504QMqttServerConnectionProperties::~QMqttServerConnectionProperties() = default;
505
506/*!
507 Returns the available properties specified by the server.
508*/
509QMqttServerConnectionProperties::ServerPropertyDetails QMqttServerConnectionProperties::availableProperties() const
510{
511 return serverData->details;
512}
513
514/*!
515 Returns \c true if the server provided properties as part of the connection
516 aknowledgment. Returns \c false if no properties have been provided.
517*/
518bool QMqttServerConnectionProperties::isValid() const
519{
520 return serverData->valid;
521}
522
523/*!
524 Returns the maximum QoS level the server supports for publishing messages.
525 Publishing messages with QoS level exceeding the maximum QoS level reported by the server
526 is a protocol violation.
527
528 If the client does not need to support QoS 1 or QoS 2, it should restrict the maximum QoS level
529 in any subscription it does to a value it can support; the server would then publish messages
530 with the maximum of supported and restricted QoS levels.
531
532 The default value is \c 2.
533
534 \sa QMqttClient::publish(), QMqttClient::subscribe()
535*/
536quint8 QMqttServerConnectionProperties::maximumQoS() const
537{
538 return serverData->maximumQoS;
539}
540
541/*!
542 Returns \c true if the server accepts retained messages.
543 The default value is \c true.
544*/
545bool QMqttServerConnectionProperties::retainAvailable() const
546{
547 return serverData->retainAvailable;
548}
549
550/*!
551 Returns \c true if the server assigned a new client identifier to
552 the client.
553
554 \sa QMqttClient::clientId()
555*/
556bool QMqttServerConnectionProperties::clientIdAssigned() const
557{
558 return serverData->details & QMqttServerConnectionProperties::AssignedClientId;
559}
560
561/*!
562 Returns the reason string associated with this response.
563*/
564QString QMqttServerConnectionProperties::reason() const
565{
566 return serverData->reasonString;
567}
568
569/*!
570 Returns the reason code associated with this response.
571*/
572QMqtt::ReasonCode QMqttServerConnectionProperties::reasonCode() const
573{
574 return serverData->reasonCode;
575}
576
577/*!
578 Returns \c true if the server accepts subscriptions including wildcards.
579 The default value is \c true.
580*/
581bool QMqttServerConnectionProperties::wildcardSupported() const
582{
583 return serverData->wildcardSupported;
584}
585
586/*!
587 Returns \c true if the server accepts subscription identifiers.
588 Subscription identifiers can be passed to the server when creating
589 a new subscription.
590
591 The default value is \c true.
592
593 \sa QMqttSubscriptionProperties::setSubscriptionIdentifier()
594*/
595bool QMqttServerConnectionProperties::subscriptionIdentifierSupported() const
596{
597 return serverData->subscriptionIdentifierSupported;
598}
599
600/*!
601 Returns \c true if the server accepts shared subscriptions.
602 The default value is \c true.
603*/
604bool QMqttServerConnectionProperties::sharedSubscriptionSupported() const
605{
606 return serverData->sharedSubscriptionSupported;
607}
608
609
610/*!
611 Returns the number of seconds the server requested as keep alive. This
612 overwrites the keep alive being set from the client side.
613
614 \sa QMqttClient::setKeepAlive()
615*/
616quint16 QMqttServerConnectionProperties::serverKeepAlive() const
617{
618 return serverData->serverKeepAlive;
619}
620
621/*!
622 Returns the response information.
623*/
624QString QMqttServerConnectionProperties::responseInformation() const
625{
626 return serverData->responseInformation;
627}
628
629/*!
630 Returns a server address which can be used by the client alternatively
631 to connect to. Typically, this is used together with the reason
632 code \c 0x9c (Use another server) or \c 0x9c (Server moved).
633*/
634QString QMqttServerConnectionProperties::serverReference() const
635{
636 return serverData->serverReference;
637}
638
639QT_END_NAMESPACE
640

source code of qtmqtt/src/mqtt/qmqttconnectionproperties.cpp