aboutsummaryrefslogtreecommitdiffstats
path: root/src/coap/qcoapmessage.cpp
blob: d07611cd61e41c727b98e190e6bfbae6d9e8eb3c (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
/****************************************************************************
**
** Copyright (C) 2017 Witekio.
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCoap 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 "qcoapmessage_p.h"

QT_BEGIN_NAMESPACE

QCoapMessagePrivate::QCoapMessagePrivate(QCoapMessage::MessageType _type) :
    type(_type)
{
}

QCoapMessagePrivate::QCoapMessagePrivate(const QCoapMessagePrivate &other) :
    QSharedData(other),
    version(other.version), type(other.type), messageId(other.messageId),
    token(other.token), options(other.options), payload(other.payload)
{
}

QCoapMessagePrivate::~QCoapMessagePrivate()
{
}

/*!
    \class QCoapMessage
    \inmodule QtCoap

    \brief The QCoapMessage class holds information about a CoAP message that
    can be a request or a reply.

    \reentrant

    It holds information such as the message type, message id, token and other
    ancillary data.

    \sa QCoapOption, QCoapReply, QCoapRequest
*/

/*!
    \enum QCoapMessage::MessageType

    Indicates the type of the message.

    \value Confirmable                  A Confirmable message. The destination
                                        endpoint needs to acknowledge the
                                        message.
    \value NonConfirmable               A Non-Confirmable message. The
                                        destination endpoint does not need to
                                        acknowledge the message.
    \value Acknowledgment               An Acknowledgment message. A message
                                        sent or received in reply to a
                                        Confirmable message.
    \value Reset                        A Reset message. This message type is used
                                        in case of errors or to stop the ongoing
                                        transmission. (For example, it is used
                                        to cancel an observation).
*/

/*!
    \fn void QCoapMessage::swap(QCoapMessage &other)

    Swaps this message with \a other. This operation is very fast and never fails.
*/

/*!
    Constructs a new QCoapMessage.
*/
QCoapMessage::QCoapMessage() :
    d_ptr(new QCoapMessagePrivate)
{
}

/*!
    Destroys the QCoapMessage.
*/
QCoapMessage::~QCoapMessage()
{
}

/*!
    Constructs a shallow copy of \a other.
*/
QCoapMessage::QCoapMessage(const QCoapMessage &other) :
    d_ptr(other.d_ptr)
{
}

/*!
    \internal
    Constructs a new QCoapMessage with \a dd as the d_ptr.
    This constructor must be used internally when subclassing
    the QCoapMessage class.
*/
QCoapMessage::QCoapMessage(QCoapMessagePrivate &dd) :
    d_ptr(&dd)
{
}

/*!
    \overload

    Adds the CoAP option with the given \a name and \a value.
*/
void QCoapMessage::addOption(QCoapOption::OptionName name, const QByteArray &value)
{
    QCoapOption option(name, value);
    addOption(option);
}

/*!
    Adds the given CoAP \a option.
*/
void QCoapMessage::addOption(const QCoapOption &option)
{
    Q_D(QCoapMessage);

    // Sort options by ascending order while inserting
    d->options.insert(
                std::upper_bound(d->options.begin(), d->options.end(), option,
                                 [](const QCoapOption &a, const QCoapOption &b) -> bool {
                                     return a.name() < b.name();
                                 }),
                option
            );
}

/*!
    Removes the given \a option.
*/
void QCoapMessage::removeOption(const QCoapOption &option)
{
    Q_D(QCoapMessage);
    d->options.removeOne(option);
}

/*!
    Removes all options with the given \a name.
    The CoAP protocol allows for the same option to repeat.
*/
void QCoapMessage::removeOption(QCoapOption::OptionName name)
{
    Q_D(QCoapMessage);
    auto namesMatch = [name](const QCoapOption &option) {
        return option.name() == name;
    };

    auto &options = d->options;
    options.erase(std::remove_if(options.begin(), options.end(), namesMatch),
                  options.end());
}

/*!
    Removes all options.
*/
void QCoapMessage::removeAllOptions()
{
    Q_D(QCoapMessage);
    d->options.clear();
}

/*!
    Returns the CoAP version.

    \sa setVersion()
*/
quint8 QCoapMessage::version() const
{
    Q_D(const QCoapMessage);
    return d->version;
}

/*!
    Returns the message type.

    \sa setType()
*/
QCoapMessage::MessageType QCoapMessage::type() const
{
    Q_D(const QCoapMessage);
    return d->type;
}

/*!
    Returns the message token.

    \sa setToken()
*/
QByteArray QCoapMessage::token() const
{
    Q_D(const QCoapMessage);
    return d->token;
}

/*!
    Returns the token length.
*/
quint8 QCoapMessage::tokenLength() const
{
    Q_D(const QCoapMessage);
    return static_cast<quint8>(d->token.length());
}

/*!
    Returns the message id.

    \sa setMessageId()
*/
quint16 QCoapMessage::messageId() const
{
    Q_D(const QCoapMessage);
    return d->messageId;
}

/*!
    Returns the payload.

    \sa setPayload()
*/
QByteArray QCoapMessage::payload() const
{
    Q_D(const QCoapMessage);
    return d->payload;
}

/*!
    Returns the option at \a index position.
*/
QCoapOption QCoapMessage::option(int index) const
{
    Q_D(const QCoapMessage);
    return d->options.at(index);
}

/*!
    Finds and returns the first option with the given \a name.
    If there is no such option, returns an Invalid CoapOption with an empty value.
*/
QCoapOption QCoapMessage::option(QCoapOption::OptionName name) const
{
    Q_D(const QCoapMessage);

    auto it = findOption(name);
    return it != d->options.end() ? *it : QCoapOption();
}

/*!
    Finds and returns a constant iterator to the first option
    with the given \a name.
    If there is no such option, returns \c d->options.end().
*/
QVector<QCoapOption>::const_iterator QCoapMessage::findOption(QCoapOption::OptionName name) const
{
    Q_D(const QCoapMessage);
    return std::find_if(d->options.begin(), d->options.end(), [name](const QCoapOption &option) {
        return option.name() == name;
    });
}

/*!
    Returns \c true if the message contains at last one option
    with \a name.
*/
bool QCoapMessage::hasOption(QCoapOption::OptionName name) const
{
    Q_D(const QCoapMessage);
    return findOption(name) != d->options.end();
}

/*!
    Returns the list of options.
*/
const QVector<QCoapOption> &QCoapMessage::options() const
{
    Q_D(const QCoapMessage);
    return d->options;
}

/*!
    Finds and returns the list of options with the given \a name.
*/
QVector<QCoapOption> QCoapMessage::options(QCoapOption::OptionName name) const
{
    Q_D(const QCoapMessage);

    QVector<QCoapOption> result;
    std::copy_if(d->options.cbegin(), d->options.cend(), std::back_inserter(result),
                 [name](const QCoapOption &option) {
                            return option.name() == name;
                 });
    return result;
}

/*!
    Returns the number of options.
*/
int QCoapMessage::optionCount() const
{
    Q_D(const QCoapMessage);
    return d->options.length();
}

/*!
    Sets the CoAP version to \a version.

    \sa version()
*/
void QCoapMessage::setVersion(quint8 version)
{
    Q_D(QCoapMessage);
    d->version = version;
}

/*!
    Sets the message type to \a type.

    \sa type()
*/
void QCoapMessage::setType(const MessageType &type)
{
    Q_D(QCoapMessage);
    d->type = type;
}

/*!
    Sets the message token to \a token.

    \sa token()
*/
void QCoapMessage::setToken(const QByteArray &token)
{
    Q_D(QCoapMessage);
    d->token = token;
}

/*!
    Sets the message ID to \a id.

    \sa messageId()
*/
void QCoapMessage::setMessageId(quint16 id)
{
    Q_D(QCoapMessage);
    d->messageId = id;
}

/*!
    Sets the message payload to \a payload. The payload can be represented in
    one of the content formats defined in \l {CoAP Content-Formats Registry}.

    \note CoAP supports common content formats such as XML, JSON, and so on, but
    these are text based and consequently heavy both in payload and in processing.
    One of the recommended content formats to use with CoAP is CBOR, which is
    designed to be used in such contexts.

    \sa payload(), QCborStreamWriter, QCborStreamReader
*/
void QCoapMessage::setPayload(const QByteArray &payload)
{
    Q_D(QCoapMessage);
    d->payload = payload;
}

void QCoapMessage::swap(QCoapMessage &other) Q_DECL_NOTHROW
{
    qSwap(d_ptr, other.d_ptr);
}

/*!
    Move-assignment operator.
 */
QCoapMessage &QCoapMessage::operator=(QCoapMessage &&other) Q_DECL_NOTHROW
{
    swap(other);
    return *this;
}

/*!
    Copies the contents of \a other into this message.
    Returns a reference to this QCoapMessage.
 */
QCoapMessage &QCoapMessage::operator=(const QCoapMessage &other)
{
    d_ptr = other.d_ptr;
    return *this;
}

/*!
    \internal

    For QSharedDataPointer.
*/
QCoapMessagePrivate* QCoapMessage::d_func()
{
    return d_ptr.data();
}

QT_END_NAMESPACE