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

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