summaryrefslogtreecommitdiffstats
path: root/src/knx/netip/qknxnetipframe.cpp
blob: 3b24c2b8cccb41d3a0b36472959f4d62b9bb8ffe (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
/******************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtKnx 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 "qknxnetipframe.h"

QT_BEGIN_NAMESPACE

class QKnxNetIpFramePrivate : public QSharedData
{
public:
    QKnxNetIpFrameHeader m_header;
    QKnxNetIpConnectionHeader m_connectionHeader;
    QKnxByteArray m_data;
};


/*!
    \class QKnxNetIpFrame

    \inmodule QtKnx
    \ingroup qtknx-netip

    \brief The QKnxNetIpFrame class represents the base for all KNXnet/IP
    related communication.

    The communication between KNXnet/IP devices is based on KNXnet/IP frames.
    A KNXnet/IP frame is a data packet sent over the non-KNX network protocol
    that consists of a header, comparable to the IP header of an Internet
    protocol data packet, and optional data of variable length. The type of the
    KNXnet/IP frame is described by a KNXnet/IP service type identifier in the
    header.

    \sa {Qt KNXnet/IP Connection Classes}
*/

/*!
    Constructs an empty invalid KNXnet/IP frame object.
*/
QKnxNetIpFrame::QKnxNetIpFrame()
    : d_ptr(new QKnxNetIpFramePrivate)
{}

/*!
    Destroys the KNXnet/IP frame object and releases all allocated resources.
*/
QKnxNetIpFrame::~QKnxNetIpFrame() = default;

/*!
    Creates a new KNXnet/IP frame with the given service type \a type and data
    set to \a data.
*/
QKnxNetIpFrame::QKnxNetIpFrame(QKnxNetIp::ServiceType type, const QKnxByteArray &data)
    : QKnxNetIpFrame(type, {}, data)
{}

/*!
    Creates a new KNXnet/IP frame with the given service type \a type,
    connection header set to \a connectionHeader, and data set to \a data.
*/
QKnxNetIpFrame::QKnxNetIpFrame(QKnxNetIp::ServiceType type,
        const QKnxNetIpConnectionHeader &connectionHeader, const QKnxByteArray &data)
    : d_ptr(new QKnxNetIpFramePrivate)
{
    d_ptr->m_connectionHeader = connectionHeader;
    d_ptr->m_data = data;
    d_ptr->m_header = { type, quint16(connectionHeader.size() + data.size()) };
}

/*!
    Creates a new KNXnet/IP frame with the given frame header \a header,
    connection header set to \a connectionHeader, and data set to \a data.
*/
QKnxNetIpFrame::QKnxNetIpFrame(const QKnxNetIpFrameHeader &header,
        const QKnxNetIpConnectionHeader &connectionHeader, const QKnxByteArray &data)
    : d_ptr(new QKnxNetIpFramePrivate)
{
    d_ptr->m_header = header;
    d_ptr->m_connectionHeader = connectionHeader;
    d_ptr->m_data = data;
}

/*!
    Returns \c true if this is a default constructed frame, otherwise returns
    \c false. A frame is considered null if it contains no initialized values.
*/
bool QKnxNetIpFrame::isNull() const
{
    return d_ptr->m_header.isNull() && d_ptr->m_connectionHeader.isNull() && d_ptr->m_data.isNull();
}

/*!
    Returns \c true if the frame contains initialized values and is in itself
    valid, otherwise returns \c false. A valid KNXnet/IP frame consists of
    at least a valid header and a size in bytes corresponding to the KNXnet/IP
    frame header \l QKnxNetIpFrameHeader::totalSize.
*/
bool QKnxNetIpFrame::isValid() const
{
    if (isNull())
        return false;

    const auto &connectionHeader = d_ptr->m_connectionHeader;
    auto connHeaderValid = connectionHeader.isNull();
    switch (serviceType()) {
        case QKnxNetIp::ServiceType::TunnelingRequest:
        case QKnxNetIp::ServiceType::TunnelingAcknowledge:
        case QKnxNetIp::ServiceType::TunnelingFeatureGet:
        case QKnxNetIp::ServiceType::TunnelingFeatureSet:
        case QKnxNetIp::ServiceType::TunnelingFeatureInfo:
        case QKnxNetIp::ServiceType::TunnelingFeatureResponse:
        case QKnxNetIp::ServiceType::DeviceConfigurationRequest:
        case QKnxNetIp::ServiceType::DeviceConfigurationAcknowledge:
            connHeaderValid = connectionHeader.isValid();
            break;
        default:
            break;
    }

    const auto &header = d_ptr->m_header;
    auto size = header.size() + connectionHeader.size() + d_ptr->m_data.size();

    return header.isValid() && connHeaderValid && header.totalSize() == size
        && header.dataSize() == (d_ptr->m_data.size() + connectionHeader.size());
}

/*!
    Returns the size in bytes of the KNXnet/IP frame. The size includes the
    size of the KNXnet/IP frame header, the optional connection header, and
    the frame's data part.
*/
quint16 QKnxNetIpFrame::size() const
{
    return d_ptr->m_header.totalSize();
}

/*!
    Returns the size of the KNXnet/IP frame data. The data size excludes the
    size of the KNXnet/IP frame header and the optional connection header.
*/
quint16 QKnxNetIpFrame::dataSize() const
{
    return d_ptr->m_header.dataSize() - d_ptr->m_connectionHeader.size();
}

/*!
    Returns the KNXnet/IP frame header.
*/
QKnxNetIpFrameHeader QKnxNetIpFrame::header() const
{
    return d_ptr->m_header;
}

/*!
    Sets the KNXnet/IP frame header to \a header.
*/
void QKnxNetIpFrame::setHeader(const QKnxNetIpFrameHeader &header)
{
    d_ptr->m_header = header;
}

/*!
    Returns the KNXnet/IP frame protocol version.
*/
quint8 QKnxNetIpFrame::protocolVersion() const
{
    return d_ptr->m_header.protocolVersion();
}

/*!
    Returns the KNXnet/IP frame's service type or QKnxNetIp::Unknown for
    a default generated frame.
*/
QKnxNetIp::ServiceType QKnxNetIpFrame::serviceType() const
{
    return d_ptr->m_header.serviceType();
}

/*!
    Sets the KNXnet/IP frame's service type to \a type.
*/
void QKnxNetIpFrame::setServiceType(QKnxNetIp::ServiceType type)
{
    d_ptr->m_header.setServiceType(type);
}

/*!
    Returns the KNXnet/IP frame's connection header.
*/
QKnxNetIpConnectionHeader QKnxNetIpFrame::connectionHeader() const
{
    return d_ptr->m_connectionHeader;
}

/*!
    Sets the KNXnet/IP frame's connection header to \a header and updates
    the total size accordingly.
*/
void QKnxNetIpFrame::setConnectionHeader(const QKnxNetIpConnectionHeader &header)
{
    quint16 dataSize = d_ptr->m_header.dataSize() - d_ptr->m_connectionHeader.size();
    d_ptr->m_connectionHeader = header;
    d_ptr->m_header.setDataSize(dataSize + header.size());
}

/*!
    Returns the KNXnet/IP frame's channel ID if a connection header is set.

    \note By default the function returns null.
*/
quint8 QKnxNetIpFrame::channelId() const
{
    return d_ptr->m_connectionHeader.channelId();
}

/*!
    Returns the KNXnet/IP frame's sequence number if a connection header is set.

    \note By default the function returns null.
*/
quint8 QKnxNetIpFrame::sequenceNumber() const
{
    return d_ptr->m_connectionHeader.sequenceNumber();
}

/*!
    Returns the service type specific value of the KNXnet/IP frame if a
    connection header is set.

    \note By default the function returns null.
*/
quint8 QKnxNetIpFrame::serviceTypeSpecificValue() const
{
    return d_ptr->m_connectionHeader.serviceTypeSpecificValue();
}

/*!
    Returns a byte array with connection type specific header items of the
    KNXnet/IP frame if a connection header is set.

    \note The returned array can be empty.
*/
QKnxByteArray QKnxNetIpFrame::connectionTypeSpecificHeaderItems() const
{
    return d_ptr->m_connectionHeader.connectionTypeSpecificHeaderItems();
}

/*!
    Returns the data part of the KNXnet/IP frame.

    \note If a connection header is set, it is not considered part of the
    returned data.
*/
QKnxByteArray QKnxNetIpFrame::data() const
{
    return d_ptr->m_data;
}

/*!
    Returns the data part of the KNXnet/IP frame as constant reference.

    \note If a connection header is set, it is not considered part of the
    returned data.
*/
const QKnxByteArray &QKnxNetIpFrame::constData() const
{
    return d_ptr->m_data;
}

/*!
    Sets the data part of the KNXnet/IP frame to \a data and updates the
    total size accordingly.

    \note The data part passed may not contain the connection header.
*/
void QKnxNetIpFrame::setData(const QKnxByteArray &data)
{
    auto dataSize = d_ptr->m_header.dataSize() - d_ptr->m_data.size();
    d_ptr->m_data = data;
    d_ptr->m_header.setDataSize(dataSize + data.size());
}

/*!
    Returns an array of bytes that represent the KNXnet/IP frame.
*/
QKnxByteArray QKnxNetIpFrame::bytes() const
{
    return d_ptr->m_header.bytes() + d_ptr->m_connectionHeader.bytes() + d_ptr->m_data;
}

/*!
    Constructs the KNXnet/IP frame from the byte array \a bytes starting
    at position \a index inside the array.
*/
QKnxNetIpFrame QKnxNetIpFrame::fromBytes(const QKnxByteArray &bytes, quint16 index)
{
    auto header = QKnxNetIpFrameHeader::fromBytes(bytes, index);
    if (!header.isValid())
        return {};
    index += header.size();

    QKnxNetIpConnectionHeader connHeader;
    switch (header.serviceType()) {
        case QKnxNetIp::ServiceType::TunnelingRequest:
        case QKnxNetIp::ServiceType::TunnelingAcknowledge:
        case QKnxNetIp::ServiceType::TunnelingFeatureGet:
        case QKnxNetIp::ServiceType::TunnelingFeatureSet:
        case QKnxNetIp::ServiceType::TunnelingFeatureInfo:
        case QKnxNetIp::ServiceType::TunnelingFeatureResponse:
        case QKnxNetIp::ServiceType::DeviceConfigurationRequest:
        case QKnxNetIp::ServiceType::DeviceConfigurationAcknowledge: {
            connHeader = QKnxNetIpConnectionHeader::fromBytes(bytes, index);
            if (!connHeader.isValid())
                return {};
            index += connHeader.size();
        }   break;
        default:
            break;
    }

    const auto dataSize = header.totalSize() - index;
    if ((bytes.size() - index) < dataSize)
        return {};
    return { header, connHeader, bytes.mid(index, dataSize) };
}

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

/*!
    Assigns the specified \a other to this object.
*/
QKnxNetIpFrame &QKnxNetIpFrame::operator=(const QKnxNetIpFrame &other)
{
    d_ptr = other.d_ptr;
    return *this;
}

/*!
    Move-constructs an object instance, making it point to the same object that
    \a other was pointing to.
*/
QKnxNetIpFrame::QKnxNetIpFrame(QKnxNetIpFrame &&other) Q_DECL_NOTHROW
    : d_ptr(other.d_ptr)
{
    other.d_ptr = Q_NULLPTR;
}

/*!
    Move-assigns \a other to this object instance.
*/
QKnxNetIpFrame &QKnxNetIpFrame::operator=(QKnxNetIpFrame &&other) Q_DECL_NOTHROW
{
    swap(other);
    return *this;
}

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

/*!
    Returns \c true if this object and the given \a other are equal; otherwise
    returns \c false.
*/
bool QKnxNetIpFrame::operator==(const QKnxNetIpFrame &other) const
{
    return d_ptr == other.d_ptr || [&]() -> bool {
        return d_ptr->m_header == other.d_ptr->m_header
            && d_ptr->m_connectionHeader == other.d_ptr->m_connectionHeader
            && d_ptr->m_data == other.d_ptr->m_data;
    }();
}

/*!
    Returns \c true if this object and the given \a other are not equal;
    otherwise returns \c false.
*/
bool QKnxNetIpFrame::operator!=(const QKnxNetIpFrame &other) const
{
    return !operator==(other);
}

/*!
    \internal
*/
QKnxNetIpFrame::QKnxNetIpFrame(QKnxNetIpFramePrivate &dd)
    : d_ptr(new QKnxNetIpFramePrivate(dd))
{}

/*!
    \relates QKnxNetIpFrame

    Writes the KNXnet/IP \a frame to the \a debug stream.
*/
QDebug operator<<(QDebug debug, const QKnxNetIpFrame &frame)
{
    QDebugStateSaver _(debug);
    return debug.nospace().noquote() << "0x" << frame.bytes().toHex();
}

QT_END_NAMESPACE