aboutsummaryrefslogtreecommitdiffstats
path: root/src/coap/qcoapoption.cpp
blob: ddb42fd18a111c2ec163b2e335142bfae07599e9 (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
/****************************************************************************
**
** 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 "qcoapoption_p.h"

#include <QtCore/qdebug.h>
#include <QtCore/qloggingcategory.h>

QT_BEGIN_NAMESPACE

Q_LOGGING_CATEGORY(lcCoapOption, "qt.coap.option")

/*!
    \class QCoapOption
    \inmodule QtCoap

    \brief The QCoapOption class holds data about CoAP options.

    \reentrant

    CoAP defines a number of options that can be included in a message.
    Both requests and responses may include a list of one or more
    options.  For example, the URI in a request is transported in several
    options, and metadata that would be carried in an HTTP header in HTTP
    is supplied as options as well.

    An option contains a name, related to an option ID, and a value.
    The name is one of the values from the OptionName enumeration.
*/

/*!
    \enum QCoapOption::OptionName

    Indicates the name of an option.
    The value of each ID is as specified by the CoAP standard, with the
    exception of Invalid. You can refer to
    \l{https://tools.ietf.org/html/rfc7252#section-5.10}{RFC 7252} and
    \l{https://tools.ietf.org/html/rfc7959#section-2.1}{RFC 7959} for more details.

    \value Invalid                  An invalid option.
    \value IfMatch                  If-Match option.
    \value UriHost                  Uri-Host option.
    \value Etag                     Etag option.
    \value IfNoneMatch              If-None-Match option.
    \value Observe                  Observe option.
    \value UriPort                  Uri-Port option.
    \value LocationPath             Location-path option.
    \value UriPath                  Uri-Path option.
    \value ContentFormat            Content-Format option.
    \value MaxAge                   Max-Age option.
    \value UriQuery                 Uri-Query option.
    \value Accept                   Accept option.
    \value LocationQuery            Location-Query option.
    \value Block2                   Block2 option.
    \value Block1                   Block1 option.
    \value Size2                    Size2 option.
    \value ProxyUri                 Proxy-Uri option.
    \value ProxyScheme              Proxy-Scheme option.
    \value Size1                    Size1 option.
*/

/*!
    Constructs a new CoAP option with the given \a name
    and QByteArray \a value.
    If no parameters are passed, constructs an Invalid object.

    \sa isValid()
 */
QCoapOption::QCoapOption(OptionName name, const QByteArray &value) :
    d_ptr(new QCoapOptionPrivate)
{
    Q_D(QCoapOption);
    d->name = name;
    setValue(value);
}

/*!
    Constructs a new CoAP option with the given \a name
    and the QStringView \a value.

    \sa isValid()
 */
QCoapOption::QCoapOption(OptionName name, QStringView value) :
    d_ptr(new QCoapOptionPrivate)
{
    Q_D(QCoapOption);
    d->name = name;
    setValue(value);
}

/*!
    Constructs a new CoAP option with the given \a name
    and the string \a value.

    \sa isValid()
 */
QCoapOption::QCoapOption(OptionName name, const char *value) :
    d_ptr(new QCoapOptionPrivate)
{
    Q_D(QCoapOption);
    d->name = name;
    setValue(value);
}

/*!
    Constructs a new CoAP option with the given \a name
    and the unsigned integer \a value.

    \sa isValid()
 */
QCoapOption::QCoapOption(OptionName name, quint32 value) :
    d_ptr(new QCoapOptionPrivate)
{
    Q_D(QCoapOption);
    d->name = name;
    setValue(value);
}

/*!
    Constructs a new CoAP option as a copy of \a other, making the two
    options identical.

    \sa isValid()
 */
QCoapOption::QCoapOption(const QCoapOption &other) :
    d_ptr(new QCoapOptionPrivate(*other.d_ptr))
{
}

/*!
    Move-constructs a QCoapOption, making it point to the same object
    as \a other was pointing to.
 */
QCoapOption::QCoapOption(QCoapOption &&other) :
    d_ptr(other.d_ptr)
{
    other.d_ptr = nullptr;
}

/*!
    Destroys the QCoapOption object.
 */
QCoapOption::~QCoapOption()
{
    delete d_ptr;
}

/*!
    Copies \a other into this option, making the two options identical.
    Returns a reference to this QCoapOption.
 */
QCoapOption &QCoapOption::operator=(const QCoapOption &other)
{
    d_ptr->name = other.d_ptr->name;
    d_ptr->value = other.d_ptr->value;
    return *this;
}

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

/*!
    Swaps this option with \a other. This operation is very fast and never fails.
 */
void QCoapOption::swap(QCoapOption &other) Q_DECL_NOTHROW
{
    qSwap(d_ptr, other.d_ptr);
}

/*!
    Returns the value of the option.
 */
QByteArray QCoapOption::value() const
{
    Q_D(const QCoapOption);
    return d->value;
}

/*!
    Returns the integer value of the option.
 */
quint32 QCoapOption::valueToInt() const
{
    Q_D(const QCoapOption);

    quint32 intValue = 0;
    for (int i = 0; i < d->value.length(); i++)
        intValue |= static_cast<quint8>(d->value.at(i)) << (8 * i);

    return intValue;
}

/*!
    Returns the length of the value of the option.
 */
int QCoapOption::length() const
{
    Q_D(const QCoapOption);
    return d->value.length();
}

/*!
    Returns the name of the option.
 */
QCoapOption::OptionName QCoapOption::name() const
{
    Q_D(const QCoapOption);
    return d->name;
}

/*!
    Returns \c true if the option is valid.
 */
bool QCoapOption::isValid() const
{
    Q_D(const QCoapOption);
    return d->name != QCoapOption::Invalid;
}

/*!
    Returns \c true if this QCoapOption and \a other are equals.
 */
bool QCoapOption::operator==(const QCoapOption &other) const
{
    Q_D(const QCoapOption);
    return (d->name == other.d_ptr->name
            && d->value == other.d_ptr->value);
}

/*!
    Returns \c true if this QCoapOption and \a other are different.
 */
bool QCoapOption::operator!=(const QCoapOption &other) const
{
    return !(*this == other);
}

/*!
    Sets the \a value for the option.
 */
void QCoapOption::setValue(const QByteArray &value)
{
    Q_D(QCoapOption);
    bool oversized = false;

    // Check for value maximum size, according to section 5.10 of RFC 7252
    // https://tools.ietf.org/html/rfc7252#section-5.10
    switch (d_ptr->name) {
    case IfNoneMatch:
        if (value.size() > 0)
            oversized = true;
        break;

    case UriPort:
    case ContentFormat:
    case Accept:
        if (value.size() > 2)
            oversized = true;
        break;

    case MaxAge:
    case Size1:
        if (value.size() > 4)
            oversized = true;
        break;

    case IfMatch:
    case Etag:
        if (value.size() > 8)
            oversized = true;
        break;

    case UriHost:
    case LocationPath:
    case UriPath:
    case UriQuery:
    case LocationQuery:
    case ProxyScheme:
        if (value.size() > 255)
            oversized = true;
        break;

    case ProxyUri:
        if (value.size() > 1034)
            oversized = true;
        break;

    case Observe:
    case Block2:
    case Block1:
    case Size2:
    default:
        break;
    }

    if (oversized)
        qCWarning(lcCoapOption) << "Value" << value << "is probably too big for option" << d->name;

    d->value = value;
}

/*!
    \overload

    Sets the \a value for the option.
 */
void QCoapOption::setValue(QStringView value)
{
    setValue(value.toUtf8());
}

/*!
    \overload

    Sets the \a value for the option.
 */
void QCoapOption::setValue(const char *value)
{
    setValue(QByteArray(value, static_cast<int>(strlen(value))));
}

/*!
    \overload

    Sets the \a value for the option.
 */
void QCoapOption::setValue(quint32 value)
{
    QByteArray data;
    for (; value; value >>= 8)
        data.append(static_cast<qint8>(value & 0xFF));

    setValue(data);
}

/*!
    \internal

    For QSharedDataPointer.
*/
QCoapOptionPrivate *QCoapOption::d_func()
{
    return d_ptr;
}

QT_END_NAMESPACE