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 "qmqtttype.h"
6
7QT_BEGIN_NAMESPACE
8
9/*!
10 \class QMqttStringPair
11
12 \inmodule QtMqtt
13 \since 5.12
14
15 \brief The QMqttStringPair class represents the string pair data type
16 of the MQTT 5.0 standard.
17
18 This data type is used to hold a name-value pair.
19*/
20
21/*!
22 \class QMqttUserProperties
23
24 \inmodule QtMqtt
25 \since 5.12
26
27 \brief The QMqttUserProperties class represents a list of QMqttStringPair
28 values which can be passed to the server.
29
30 User properties allow a client to pass additional data to the server or
31 other subscribers, which do not belong to the message payload.
32
33 On the other hand it also provides a server the flexibility to provide
34 further information to connected clients.
35
36 The server might add additional user properties to the ones provided.
37 However, the order of the provided properties is not changed during transfer.
38
39 \note User properties are part of the MQTT 5.0 specification and
40 cannot be used when using QtMqtt with an older protocol level. See
41 QMqttClient::ProtocolVersion for more information.
42
43 \sa QMqttAuthenticationProperties, QMqttConnectionProperties,
44 QMqttLastWillProperties, QMqttPublishProperties, QMqttSubscriptionProperties,
45 QMqttUnsubscriptionProperties, QMqttSubscription
46*/
47
48/*!
49 \namespace QMqtt
50 \inmodule QtMqtt
51 \since 5.12
52
53 \brief Contains miscellaneous identifiers used throughout the Qt MQTT module.
54*/
55
56/*!
57 \enum QMqtt::PayloadFormatIndicator
58 \since 5.12
59
60 The payload format provides information on the content of a message.
61 This can help other clients to handle the message faster.
62
63 \value Unspecified
64 The format is not specified.
65 \value UTF8Encoded
66 The payload of the message is formatted as UTF-8 Encoded
67 Character Data.
68*/
69
70/*!
71 \enum QMqtt::MessageStatus
72 \since 5.12
73
74 This enum type specifies the available states of a message. Depending
75 on the QoS and role of the client, different message statuses are
76 expected.
77
78 \value Unknown
79 The message status is unknown.
80 \value Published
81 The client received a message for one of its subscriptions. This
82 applies to QoS levels 1 and 2.
83 \value Acknowledged
84 A message has been acknowledged. This applies to QoS 1 and states
85 that the message handling has been finished from the client side.
86 \value Received
87 A message has been received. This applies to QoS 2.
88 \value Released
89 A message has been released. This applies to QoS 2. For a publisher
90 the message handling has been finished.
91 \value Completed
92 A message has been completed. This applies to QoS 2 and states
93 that the message handling has been finished from the client side.
94*/
95
96/*!
97 \enum QMqtt::ReasonCode
98 \since 5.12
99
100 This enum type specifies the available error codes.
101
102 \value Success
103 The specified action has succeeded.
104 \value SubscriptionQoSLevel0
105 A subscription with QoS level 0 has been created.
106 \value SubscriptionQoSLevel1
107 A subscription with QoS level 1 has been created.
108 \value SubscriptionQoSLevel2
109 A subscription with QoS level 2 has been created.
110 \value NoMatchingSubscriber
111 The message has been accepted by the server, but there are no
112 subscribers to receive this message. A broker may send this
113 reason code instead of \l Success.
114 \value [since 5.15] NoSubscriptionExisted
115 No matching Topic Filter is being used by the Client.
116 \value [since 5.15] ContinueAuthentication
117 Continue the authentication with another step.
118 \value [since 5.15] ReAuthenticate
119 Initiate a re-authentication.
120 \value UnspecifiedError
121 An unspecified error occurred.
122 \value MalformedPacket
123 The packet sent to the server is invalid.
124 \value ProtocolError
125 A protocol error has occurred. In most cases, this will cause
126 the server to disconnect the client.
127 \value ImplementationSpecificError
128 The packet is valid, but the recipient rejects it.
129 \value UnsupportedProtocolVersion
130 The requested protocol version is not supported by the server.
131 \value InvalidClientId
132 The client ID is invalid.
133 \value InvalidUserNameOrPassword
134 The username or password specified is invalid.
135 \value NotAuthorized
136 The client is not authorized for the specified action.
137 \value ServerNotAvailable
138 The server to connect to is not available.
139 \value ServerBusy
140 The server to connect to is not available. The client is asked to
141 try at a later time.
142 \value ClientBanned
143 The client has been banned from the server.
144 \value InvalidAuthenticationMethod
145 The authentication method specified is invalid.
146 \value InvalidTopicFilter
147 The topic filter specified is invalid.
148 \value InvalidTopicName
149 The topic name specified is invalid.
150 \value MessageIdInUse
151 The message ID used in the previous packet is already in use.
152 \value MessageIdNotFound
153 The message ID used in the previous packet has not been found.
154 \value PacketTooLarge
155 The packet received is too large. See also
156 \l QMqttServerConnectionProperties::maximumPacketSize().
157 \value QuotaExceeded
158 An administratively imposed limit has been exceeded.
159 \value InvalidPayloadFormat
160 The payload format is invalid.
161 See also \l QMqttPublishProperties::payloadFormatIndicator().
162 \value RetainNotSupported
163 The server does not support retained messages.
164 See also \l QMqttServerConnectionProperties::retainAvailable().
165 \value QoSNotSupported
166 The QoS level requested is not supported.
167 See also \l QMqttServerConnectionProperties::maximumQoS().
168 \value UseAnotherServer
169 The server the client tries to connect to is not available. See also
170 \l QMqttServerConnectionProperties::serverReference().
171 \value ServerMoved
172 The server the client tries to connect to has moved to a new address.
173 See also \l QMqttServerConnectionProperties::serverReference().
174 \value SharedSubscriptionsNotSupported
175 Shared subscriptions are not supported.
176 See also \l QMqttServerConnectionProperties::sharedSubscriptionSupported().
177 \value ExceededConnectionRate
178 The connection rate limit has been exceeded.
179 \value SubscriptionIdsNotSupported
180 Subscription IDs are not supported.
181 See also \l QMqttServerConnectionProperties::subscriptionIdentifierSupported().
182 \value WildCardSubscriptionsNotSupported
183 Subscriptions using wildcards are not supported by the server.
184 See also \l QMqttServerConnectionProperties::wildcardSupported().
185
186 Not all values are available in every use case. Especially, some servers
187 will reject a reason code not suited for a specific command. See below
188 table to highlight expected reason codes for specific actions.
189
190 \table
191 \header
192 \li Reason Code
193 \li Connect Properties
194 \li Subscription Properties
195 \li Message Properties
196 \row
197 \li Success
198 \li X
199 \li X
200 \li X
201 \row
202 \li SubscriptionQoSLevel0
203 \li
204 \li X
205 \li
206 \row
207 \li SubscriptionQoSLevel1
208 \li
209 \li X
210 \li
211 \row
212 \li SubscriptionQoSLevel2
213 \li
214 \li X
215 \li
216 \row
217 \li NoMatchingSubscriber
218 \li
219 \li
220 \li X
221 \row
222 \li UnspecifiedError
223 \li X
224 \li X
225 \li X
226 \row
227 \li MalformedPacket
228 \li X
229 \li
230 \li
231 \row
232 \li ProtocolError
233 \li X
234 \li
235 \li
236 \row
237 \li ImplementationSpecificError
238 \li X
239 \li X
240 \li X
241 \row
242 \li UnsupportedProtocolVersion
243 \li X
244 \li
245 \li
246 \row
247 \li InvalidClientId
248 \li X
249 \li
250 \li
251 \row
252 \li InvalidUserNameOrPassword
253 \li X
254 \li
255 \li
256 \row
257 \li NotAuthorized
258 \li X
259 \li X
260 \li X
261 \row
262 \li ServerNotAvailable
263 \li X
264 \li
265 \li
266 \row
267 \li ServerBusy
268 \li X
269 \li
270 \li
271 \row
272 \li ClientBanned
273 \li X
274 \li
275 \li
276 \row
277 \li InvalidAuthenticationMethod
278 \li X
279 \li
280 \li
281 \row
282 \li InvalidTopicFilter
283 \li
284 \li X
285 \li
286 \row
287 \li InvalidTopicName
288 \li X
289 \li
290 \li X
291 \row
292 \li MessageIdInUse
293 \li
294 \li X
295 \li X
296 \row
297 \li MessageIdNotFound
298 \li
299 \li
300 \li X
301 \row
302 \li PacketTooLarge
303 \li X
304 \li
305 \li
306 \row
307 \li QuotaExceeded
308 \li X
309 \li X
310 \li X
311 \row
312 \li InvalidPayloadFormat
313 \li X
314 \li
315 \li X
316 \row
317 \li RetainNotSupported
318 \li X
319 \li
320 \li
321 \row
322 \li QoSNotSupported
323 \li X
324 \li
325 \li
326 \row
327 \li UseAnotherServer
328 \li X
329 \li
330 \li
331 \row
332 \li ServerMoved
333 \li X
334 \li
335 \li
336 \row
337 \li SharedSubscriptionsNotSupported
338 \li
339 \li X
340 \li
341 \row
342 \li ExceededConnectionRate
343 \li X
344 \li
345 \li
346 \row
347 \li SubscriptionIdsNotSupported
348 \li
349 \li X
350 \li
351 \row
352 \li WildCardSubscriptionsNotSupported
353 \li
354 \li X
355 \li
356 \endtable
357*/
358
359class QMqttStringPairData : public QSharedData
360{
361public:
362 QMqttStringPairData() = default;
363 QMqttStringPairData(const QString &name, const QString &value);
364
365 bool operator==(const QMqttStringPairData &rhs) const;
366 QString m_name;
367 QString m_value;
368};
369
370QMqttStringPairData::QMqttStringPairData(const QString &name, const QString &value)
371 : m_name(name)
372 , m_value(value)
373{
374}
375
376bool QMqttStringPairData::operator==(const QMqttStringPairData &rhs) const
377{
378 return m_name == rhs.m_name && m_value == rhs.m_value;
379}
380
381QMqttStringPair::QMqttStringPair()
382 : data(new QMqttStringPairData)
383{
384
385}
386
387QMqttStringPair::QMqttStringPair(const QString &name, const QString &value)
388 : data(new QMqttStringPairData(name, value))
389{
390}
391
392QMqttStringPair::QMqttStringPair(const QMqttStringPair &) = default;
393
394QMqttStringPair::~QMqttStringPair() = default;
395
396/*!
397 Returns the name of the string pair.
398*/
399QString QMqttStringPair::name() const
400{
401 return data->m_name;
402}
403
404/*!
405 Sets the name to \a n.
406*/
407void QMqttStringPair::setName(const QString &n)
408{
409 data->m_name = n;
410}
411
412/*!
413 Returns the value of the string pair.
414*/
415QString QMqttStringPair::value() const
416{
417 return data->m_value;
418}
419
420/*!
421 Sets the value to \a v.
422*/
423void QMqttStringPair::setValue(const QString &v)
424{
425 data->m_value = v;
426}
427
428/*!
429 Returns \c true if this instance matches \a other.
430*/
431bool QMqttStringPair::operator==(const QMqttStringPair &other) const
432{
433 return *data.constData() == *other.data.constData();
434}
435
436/*!
437 Returns \c true if this instance does not match \a other.
438*/
439bool QMqttStringPair::operator!=(const QMqttStringPair &other) const
440{
441 return !operator==(other);
442}
443
444QMqttStringPair &QMqttStringPair::operator=(const QMqttStringPair &rhs)
445{
446 if (this != &rhs)
447 data.operator=(o: rhs.data);
448 return *this;
449}
450
451#ifndef QT_NO_DEBUG_STREAM
452QDebug operator<<(QDebug d, const QMqttStringPair &s)
453{
454 QDebugStateSaver saver(d);
455 d.nospace() << "QMqttStringPair(" << s.name() << " : " << s.value() << ')';
456 return d;
457}
458#endif
459
460QT_END_NAMESPACE
461

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