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
|
// Copyright (C) 2017 Witekio.
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "qcoapoption_p.h"
#include <QtCore/qdebug.h>
#include <QtCore/qloggingcategory.h>
QT_BEGIN_NAMESPACE
Q_STATIC_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 opaqueValue.
If no parameters are passed, constructs an Invalid object.
\sa isValid()
*/
QCoapOption::QCoapOption(OptionName name, const QByteArray &opaqueValue) :
d_ptr(new QCoapOptionPrivate)
{
Q_D(QCoapOption);
d->name = name;
d->setValue(opaqueValue);
}
/*!
Constructs a new CoAP option with the given \a name
and the QString \a stringValue.
\sa isValid()
*/
QCoapOption::QCoapOption(OptionName name, const QString &stringValue) :
d_ptr(new QCoapOptionPrivate)
{
Q_D(QCoapOption);
d->name = name;
d->setValue(stringValue);
}
/*!
Constructs a new CoAP option with the given \a name
and the unsigned integer \a intValue.
\sa isValid()
*/
QCoapOption::QCoapOption(OptionName name, quint32 intValue) :
d_ptr(new QCoapOptionPrivate)
{
Q_D(QCoapOption);
d->name = name;
d->setValue(intValue);
}
/*!
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)
{
QCoapOption copy(other);
swap(copy);
return *this;
}
/*!
Moves \a other into this option and returns a reference to this QCoapOption.
*/
QCoapOption &QCoapOption::operator=(QCoapOption &&other) noexcept
{
swap(other);
return *this;
}
/*!
Swaps this option with \a other. This operation is very fast and never fails.
*/
void QCoapOption::swap(QCoapOption &other) noexcept
{
qSwap(d_ptr, other.d_ptr);
}
/*!
Returns the value of the option.
*/
QByteArray QCoapOption::opaqueValue() const
{
Q_D(const QCoapOption);
return d->value;
}
/*!
Returns the integer value of the option.
*/
quint32 QCoapOption::uintValue() const
{
Q_D(const QCoapOption);
quint32 intValue = 0;
for (int i = 0; i < d->value.size(); i++)
intValue |= static_cast<quint8>(d->value.at(i)) << (8 * i);
return intValue;
}
/*!
Returns the QString value of the option.
*/
QString QCoapOption::stringValue() const
{
Q_D(const QCoapOption);
return QString::fromUtf8(d->value);
}
/*!
Returns the length of the value of the option.
*/
int QCoapOption::length() const
{
Q_D(const QCoapOption);
return d->value.size();
}
/*!
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);
}
/*!
\internal
Sets the \a value for the option.
*/
void QCoapOptionPrivate::setValue(const QByteArray &opaqueValue)
{
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 (name) {
case QCoapOption::IfNoneMatch:
if (opaqueValue.size() > 0)
oversized = true;
break;
case QCoapOption::UriPort:
case QCoapOption::ContentFormat:
case QCoapOption::Accept:
if (opaqueValue.size() > 2)
oversized = true;
break;
case QCoapOption::MaxAge:
case QCoapOption::Size1:
if (opaqueValue.size() > 4)
oversized = true;
break;
case QCoapOption::IfMatch:
case QCoapOption::Etag:
if (opaqueValue.size() > 8)
oversized = true;
break;
case QCoapOption::UriHost:
case QCoapOption::LocationPath:
case QCoapOption::UriPath:
case QCoapOption::UriQuery:
case QCoapOption::LocationQuery:
case QCoapOption::ProxyScheme:
if (opaqueValue.size() > 255)
oversized = true;
break;
case QCoapOption::ProxyUri:
if (opaqueValue.size() > 1034)
oversized = true;
break;
case QCoapOption::Observe:
case QCoapOption::Block2:
case QCoapOption::Block1:
case QCoapOption::Size2:
default:
break;
}
if (oversized)
qCWarning(lcCoapOption) << "Value" << opaqueValue << "is probably too big for option" << name;
value = opaqueValue;
}
/*!
\internal
\overload
Sets the \a value for the option.
*/
void QCoapOptionPrivate::setValue(const QString &value)
{
setValue(value.toUtf8());
}
/*!
\internal
\overload
Sets the \a value for the option.
*/
void QCoapOptionPrivate::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
|