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

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