summaryrefslogtreecommitdiffstats
path: root/src/mqtt/qmqtttype.cpp
blob: 373707b6a0bc39210187a255963a29e4d3cb0a72 (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
/******************************************************************************
**
** Copyright (C) 2018 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 "qmqtttype.h"

QT_BEGIN_NAMESPACE

/*!
    \class QMqttStringPair

    \inmodule QtMqtt
    \since 5.12

    \brief The QMqttStringPair class represents the string pair data type
    of the MQTT 5.0 standard.

    This data type is used to hold a name-value pair.
*/

/*!
    \class QMqttUserProperties

    \inmodule QtMqtt
    \since 5.12

    \brief The QMqttUserProperties class represents a list of QMqttStringPair
    values which can be passed to the server.

    User properties allow a client to pass additional data to the server or
    other subscribers, which do not belong to the message payload.

    On the other hand it also provides a server the flexibility to provide
    further information to connected clients.

    The server might add additional user properties to the ones provided.
    However, the order of the provided properties is not changed during transfer.

    \note User properties are part of the MQTT 5.0 specification and
    cannot be used when using QtMqtt with an older protocol level. See
    QMqttClient::ProtocolVersion for more information.

    \sa QMqttAuthenticationProperties, QMqttConnectionProperties,
    QMqttLastWillProperties, QMqttPublishProperties, QMqttSubscriptionProperties,
    QMqttUnsubscriptionProperties, QMqttSubscription
*/

/*!
    \namespace QMqtt
    \inmodule QtMqtt
    \since 5.12

    \brief Contains miscellaneous identifiers used throughout the Qt MQTT module.
*/

/*!
    \enum QMqtt::PayloadFormatIndicator
    \since 5.12

    The payload format provides information on the content of a message.
    This can help other clients to handle the message faster.

    \value Unspecified
           The format is not specified.
    \value UTF8Encoded
           The payload of the message is formatted as UTF-8 Encoded
           Character Data.
*/

/*!
    \enum QMqtt::MessageStatus
    \since 5.12

    This enum type specifies the available states of a message. Depending
    on the QoS and role of the client, different message statuses are
    expected.

    \value Unknown
           The message status is unknown.
    \value Published
           The client received a message for one of its subscriptions. This
           applies to all QoS levels.
    \value Acknowledged
           A message has been acknowledged. This applies to QoS 1 and states
           that the message handling has been finished from the client side.
    \value Received
           A message has been received. This applies to QoS 2.
    \value Released
           A message has been released. This applies to QoS 2. For a publisher
           the message handling has been finished.
    \value Completed
           A message has been completed. This applies to QoS 2 and states
           that the message handling has been finished from the client side.
*/

/*!
    \enum QMqtt::ReasonCode
    \since 5.12

    This enum type specifies the available error codes.

    \value Success
           The specified action has succeeded.
    \value SubscriptionQoSLevel0
           A subscription with QoS level 0 has been created.
    \value SubscriptionQoSLevel1
           A subscription with QoS level 1 has been created.
    \value SubscriptionQoSLevel2
           A subscription with QoS level 2 has been created.
    \value NoMatchingSubscriber
           The message has been accepted by the server, but there are no
           subscribers to receive this message. A broker may send this
           reason code instead of \l Success.
    \value UnspecifiedError
           An unspecified error occurred.
    \value MalformedPacket
           The packet sent to the server is invalid.
    \value ProtocolError
           A protocol error has occurred. In most cases, this will cause
           the server to disconnect the client.
    \value ImplementationSpecificError
           The packet is valid, but the recipient rejects it.
    \value UnsupportedProtocolVersion
           The requested protocol version is not supported by the server.
    \value InvalidClientId
           The client ID is invalid.
    \value InvalidUserNameOrPassword
           The username or password specified is invalid.
    \value NotAuthorized
           The client is not authorized for the specified action.
    \value ServerNotAvailable
           The server to connect to is not available.
    \value ServerBusy
           The server to connect to is not available. The client is asked to
           try at a later time.
    \value ClientBanned
           The client has been banned from the server.
    \value InvalidAuthenticationMethod
           The authentication method specified is invalid.
    \value InvalidTopicFilter
           The topic filter specified is invalid.
    \value InvalidTopicName
           The topic name specified is invalid.
    \value MessageIdInUse
           The message ID used in the previous packet is already in use.
    \value MessageIdNotFound
           The message ID used in the previous packet has not been found.
    \value PacketTooLarge
           The packet received is too large. See also
           \l QMqttServerConnectionProperties::maximumPacketSize().
    \value QuotaExceeded
           An administratively imposed limit has been exceeded.
    \value InvalidPayloadFormat
           The payload format is invalid.
    \value RetainNotSupported
           The server does not support retained messages.
    \value QoSNotSupported
           The QoS level requested is not supported.
    \value UseAnotherServer
           The server the client tries to connect to is not available. See also
           \l QMqttServerConnectionProperties::serverReference().
    \value ServerMoved
           The server the client tries to connect to has moved to a new address.
           See also \l QMqttServerConnectionProperties::serverReference().
    \value SharedSubscriptionsNotSupported
           Shared subscriptions are not supported.
    \value ExceededConnectionRate
           The connection rate limit has been exceeded.
    \value SubscriptionIdsNotSupported
           Subscription IDs are not supported.
    \value WildCardSubscriptionsNotSupported
           Subscriptions using wildcards are not supported by the server.

    Not all values are available in every use case. Especially, some servers
    will reject a reason code not suited for a specific command. See below
    table to highlight expected reason codes for specific actions.

    \table
    \header
        \li Reason Code
        \li Connect Properties
        \li Subscription Properties
        \li Message Properties
    \row
        \li Success
        \li X
        \li X
        \li X
    \row
        \li SubscriptionQoSLevel0
        \li
        \li X
        \li
    \row
        \li SubscriptionQoSLevel1
        \li
        \li X
        \li
    \row
        \li SubscriptionQoSLevel2
        \li
        \li X
        \li
    \row
        \li NoMatchingSubscriber
        \li
        \li
        \li X
    \row
        \li UnspecifiedError
        \li X
        \li X
        \li X
    \row
        \li MalformedPacket
        \li X
        \li
        \li
    \row
        \li ProtocolError
        \li X
        \li
        \li
    \row
        \li ImplementationSpecificError
        \li X
        \li X
        \li X
    \row
        \li UnsupportedProtocolVersion
        \li X
        \li
        \li
    \row
        \li InvalidClientId
        \li X
        \li
        \li
    \row
        \li InvalidUserNameOrPassword
        \li X
        \li
        \li
    \row
        \li NotAuthorized
        \li X
        \li X
        \li X
    \row
        \li ServerNotAvailable
        \li X
        \li
        \li
    \row
        \li ServerBusy
        \li X
        \li
        \li
    \row
        \li ClientBanned
        \li X
        \li
        \li
    \row
        \li InvalidAuthenticationMethod
        \li X
        \li
        \li
    \row
        \li InvalidTopicFilter
        \li
        \li X
        \li
    \row
        \li InvalidTopicName
        \li X
        \li
        \li X
    \row
        \li MessageIdInUse
        \li
        \li X
        \li X
    \row
        \li MessageIdNotFound
        \li
        \li
        \li X
    \row
        \li PacketTooLarge
        \li X
        \li
        \li
    \row
        \li QuotaExceeded
        \li X
        \li X
        \li X
    \row
        \li InvalidPayloadFormat
        \li X
        \li
        \li X
    \row
        \li RetainNotSupported
        \li X
        \li
        \li
    \row
        \li QoSNotSupported
        \li X
        \li
        \li
    \row
        \li UseAnotherServer
        \li X
        \li
        \li
    \row
        \li ServerMoved
        \li X
        \li
        \li
    \row
        \li SharedSubscriptionsNotSupported
        \li
        \li X
        \li
    \row
        \li ExceededConnectionRate
        \li X
        \li
        \li
    \row
        \li SubscriptionIdsNotSupported
        \li
        \li X
        \li
    \row
        \li WildCardSubscriptionsNotSupported
        \li
        \li X
        \li
    \endtable
*/

class QMqttStringPairData : public QSharedData
{
public:
    QMqttStringPairData() = default;
    QMqttStringPairData(const QString &name, const QString &value);

    bool operator==(const QMqttStringPairData &rhs) const;
    QString m_name;
    QString m_value;
};

QMqttStringPairData::QMqttStringPairData(const QString &name, const QString &value)
    : m_name(name)
    , m_value(value)
{
}

bool QMqttStringPairData::operator==(const QMqttStringPairData &rhs) const
{
    return m_name == rhs.m_name && m_value == rhs.m_value;
}

QMqttStringPair::QMqttStringPair()
    : data(new QMqttStringPairData)
{

}

QMqttStringPair::QMqttStringPair(const QString &name, const QString &value)
    : data(new QMqttStringPairData(name, value))
{
}

QMqttStringPair::QMqttStringPair(const QMqttStringPair &) = default;

QMqttStringPair::~QMqttStringPair() = default;

/*!
    Returns the name of the string pair.
*/
QString QMqttStringPair::name() const
{
    return data->m_name;
}

/*!
    Sets the name to \a n.
*/
void QMqttStringPair::setName(const QString &n)
{
    data->m_name = n;
}

/*!
    Returns the value of the string pair.
*/
QString QMqttStringPair::value() const
{
    return data->m_value;
}

/*!
    Sets the value to \a v.
*/
void QMqttStringPair::setValue(const QString &v)
{
    data->m_value = v;
}

/*!
    Returns \c true if this instance matches \a other.
*/
bool QMqttStringPair::operator==(const QMqttStringPair &other) const
{
    return *data.constData() == *other.data.constData();
}

QMqttStringPair &QMqttStringPair::operator=(const QMqttStringPair &rhs)
{
    if (this != &rhs)
        data.operator=(rhs.data);
    return *this;
}

#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug d, const QMqttStringPair &s)
{
    QDebugStateSaver saver(d);
    d.nospace() << "QMqttStringPair(" << s.name() << " : " << s.value() << ')';
    return d;
}
#endif

QT_END_NAMESPACE