summaryrefslogtreecommitdiffstats
path: root/src/knx/netip/qknxnetiptunnelinginfodib.cpp
blob: 6fee0548643f79adeb9de1edec5fa1d7c31c3411 (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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/******************************************************************************
**
** 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 "qknxnetiptunnelinginfodib.h"

QT_BEGIN_NAMESPACE


/*!
    \inmodule QtKnx
    \class QKnxNetIpTunnelingSlotInfo

    \since 5.12
    \ingroup qtknx-netip
    \ingroup qtknx-tunneling

    \brief The QKnxNetIpTunnelingSlotInfo class contains information about the
    individual address that is currently assigned to the tunneling slot as well
    as the current state of the tunneling slot.

    If a KNXnet/IP server supports tunneling, it can send more detailed
    information about the supported tunnel connections. The tunneling slot
    info class can be used to represent this information. Usually it includes
    the individual addresses used for the connection once it has been
    established and whether the connection slot is available at all.

    \sa QKnxNetIpTunnelingInfoDibProxy, {Qt KNX Tunneling Classes},
        {Qt KNXnet/IP Connection Classes}
*/

class QKnxNetIpTunnelingSlotInfoPrivate : public QSharedData
{
public:
    QKnxNetIpTunnelingSlotInfoPrivate() = default;
    ~QKnxNetIpTunnelingSlotInfoPrivate() = default;

    QKnxNetIpTunnelingSlotInfoPrivate(const QKnxAddress &ia, QKnxNetIpTunnelingSlotInfo::Status s)
        : m_ia(ia)
        , m_status(s)
    {}

    QKnxAddress m_ia;
    QKnxNetIpTunnelingSlotInfo::Status m_status { QKnxNetIpTunnelingSlotInfo::State::Invalid };
};

/*!
    \enum QKnxNetIpTunnelingSlotInfo::State

    This enumeration describes the current state of the tunneling slot. Only if
    all values are set, the slot is available for a KNXnet/IP client connection.

    \value NotAvailable
            Indicates that the slot is not available for a KNXnet/IP client
            connection.
    \value Free
            Indicates whether this tunneling slot is currently free or not.
    \value Authorized
             Indications about the authorization of this tunneling slot.
    \value Usable
            Indicates whether this tunneling slot is currently usable or not.
    \value Available
            Indicates that the slot is available for a KNXnet/IP client connection.
    \omitvalue Invalid
*/

/*!
    Creates a new empty invalid tunneling slot information object.
*/
QKnxNetIpTunnelingSlotInfo::QKnxNetIpTunnelingSlotInfo()
    : d_ptr(new QKnxNetIpTunnelingSlotInfoPrivate)
{}

/*!
    Destroys the object and frees any allocated resources.
*/
QKnxNetIpTunnelingSlotInfo::~QKnxNetIpTunnelingSlotInfo() = default;

/*!
    Creates a tunneling slot information object with the individual address set
    to \a ia and the status field set to \l NotAvailable.
*/
QKnxNetIpTunnelingSlotInfo::QKnxNetIpTunnelingSlotInfo(const QKnxAddress &ia)
    : d_ptr(new QKnxNetIpTunnelingSlotInfoPrivate(ia, State::NotAvailable))
{}

/*!
    Creates a tunneling slot information object with the individual address set
    to \a ia and the status field set to \a status.
*/
QKnxNetIpTunnelingSlotInfo::QKnxNetIpTunnelingSlotInfo(const QKnxAddress &ia, Status status)
    : d_ptr(new QKnxNetIpTunnelingSlotInfoPrivate(ia, status))
{}

/*!
    Returns \c true if the tunneling slot information is valid; otherwise
    returns \c false. The object is considered valid if the individual address
    is valid and the status field is properly set.
*/
bool QKnxNetIpTunnelingSlotInfo::isValid() const
{
    return d_ptr->m_ia.isValid() && (d_ptr->m_ia.type() == QKnxAddress::Type::Individual)
        && (quint32(d_ptr->m_status) <= 0x07); // only the first 3 bits can be set as of now
}

/*!
    Returns the individual address of the tunneling information slot object.
*/
QKnxAddress QKnxNetIpTunnelingSlotInfo::individualAddress() const
{
    return d_ptr->m_ia;
}

/*!
    Sets the individual address of the tunneling information slot object
    to \a ia if the passed argument is a valid \l QKnxAddress and of type
    \l {QKnxAddress::Type} {QKnxAddress::Individual}.
*/
void QKnxNetIpTunnelingSlotInfo::setIndividualAddress(const QKnxAddress &ia)
{
    if (ia.isValid() && (ia.type() == QKnxAddress::Type::Individual))
        d_ptr->m_ia = ia;
}

/*!
    Returns the status of the tunneling information slot object.
*/
QKnxNetIpTunnelingSlotInfo::Status QKnxNetIpTunnelingSlotInfo::status() const
{
    return d_ptr->m_status;
}

/*!
    Sets the status of the tunneling information slot object to \a status if
    the passed argument is a valid.
*/
void QKnxNetIpTunnelingSlotInfo::setStatus(QKnxNetIpTunnelingSlotInfo::Status status)
{
    if (quint32(status) <= 0x07) // only the first 3 bits can be set as of now
        d_ptr->m_status = status;
}

/*!
    Returns an array of data that represents the tunneling information slot
    object.
*/
QKnxByteArray QKnxNetIpTunnelingSlotInfo::bytes() const
{
    if (!isValid())
        return {};
    return d_ptr->m_ia.bytes() + QKnxUtils::QUint16::bytes(0xfff8 | quint16(d_ptr->m_status));
}

/*!
    Constructs the tunneling information slot object from the byte array \a data
    starting at the position \a index inside the array.
*/
QKnxNetIpTunnelingSlotInfo
    QKnxNetIpTunnelingSlotInfo::fromBytes(const QKnxByteArray &data, quint16 index)
{
    const qint32 availableSize = data.size() - index;
    if (availableSize < 4) // 2 bytes for address and 2 bytes for the status
        return {}; // not enough data ...

    auto status = QKnxUtils::QUint16::fromBytes(data.mid(index + 2));
    return { { QKnxAddress::Type::Individual, data.mid(index, 2) },
        QKnxNetIpTunnelingSlotInfo::Status(quint32(status) & 0x00000007) };
}

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

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

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

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

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

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

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

/*!
    \class QKnxNetIpTunnelingInfoDibProxy

    \since 5.12
    \inmodule QtKnx
    \ingroup qtknx-netip
    \ingroup qtknx-tunneling

    \brief The QKnxNetIpTunnelingInfoDibProxy class provides the means to read
    the maximum ADPU length supported by a KNXnet/IP tunneling interface and
    tunneling slot information from the generic \l QKnxNetIpDib class and to
    create a KNXnet/IP tunneling information block (DIB) structure.

    If a KNXnet/IP server supports tunneling, it can send more detailed
    information about the supported tunnel connections. The tunneling slot
    info class can be used to represent this information. Usually it includes
    the individual addresses used for the connection once it has been
    established and whether the connection slot is available at all.

    In most programs, this class will not be used directly. Instead, the
    \l QKnxNetIpServerDiscoveryAgent and \l QKnxNetIpServerInfo are provided.

    \note When using QKnxNetIpTunnelingInfoDibProxy, care must be taken to
    ensure that the referenced KNXnet/IP DIB structure outlives the proxy on
    all code paths, lest the proxy ends up referencing deleted data.

    Reading the application layer protocol data unit (APDU) length and default
    tunneling slot information can be achieved like this:
    \code
        auto dib = QKnxNetIpDib::fromBytes(...);

        QKnxNetIpTunnelingInfoDibProxy proxy(dib);
        if (!proxy.isValid())
            return;

        quint16 apduLength = proxy.maximumInterfaceApduLength();
        auto tunnelingSlotInfo = proxy.tunnelingSlotInfo(); // mandatory
    \endcode

    \sa builder(), QKnxNetIpTunnelingSlotInfo, {Qt KNX Tunneling Classes},
        {Qt KNXnet/IP Connection Classes}
*/

/*!
    \internal
    \fn QKnxNetIpTunnelingInfoDibProxy::QKnxNetIpTunnelingInfoDibProxy()
*/

/*!
    \internal
    \fn QKnxNetIpTunnelingInfoDibProxy::~QKnxNetIpTunnelingInfoDibProxy()
*/

/*!
    \internal
    \fn QKnxNetIpTunnelingInfoDibProxy::QKnxNetIpTunnelingInfoDibProxy(const QKnxNetIpDib &&)
*/

/*!
    Constructs a proxy object with the specified KNXnet/IP DIB structure
    \a dib to read the maximum APDU length and tunneling slot information.
*/
QKnxNetIpTunnelingInfoDibProxy::QKnxNetIpTunnelingInfoDibProxy(const QKnxNetIpDib &dib)
    : m_dib(dib)
{}

/*!
    Returns \c true if the KNXnet/IP structure to create the object is a valid
    KNXnet/IP DIB structure; otherwise returns \c false.

    A KNXnet/IP tunneling information DIB structure is considered valid if it
    contains the maximum APDU length and at least one tunneling slot information
    object.
*/
bool QKnxNetIpTunnelingInfoDibProxy::isValid() const
{
    return m_dib.isValid() && (m_dib.code() == QKnxNetIp::DescriptionType::TunnelingInfo)
        && (m_dib.size() >= 8) && (m_dib.size() % 4 == 0);
}

/*!
    Returns the description type of this KNXnet/IP structure if the object
    that was passed during construction was valid; otherwise returns
    \l QKnx::NetIp::Unknown.
*/
QKnxNetIp::DescriptionType QKnxNetIpTunnelingInfoDibProxy::descriptionType() const
{
    if (isValid())
        return m_dib.code();
    return QKnxNetIp::DescriptionType::Unknown;
}

/*!
    Returns the \l {QKnxInterfaceObjectProperty::MaxInterfaceApduLength}
    {maximum APDU} length carried by this KNXnet/IP DIB structure.

    \note If the object passed during construction was invalid, the function
    returns a \l {default-constructed value} which can be \c 0.

    \sa isValid()
*/
quint16 QKnxNetIpTunnelingInfoDibProxy::maximumInterfaceApduLength() const
{
    return QKnxUtils::QUint16::fromBytes(m_dib.constData());
}

/*!
    Returns the mandatory tunneling slot information carried by this KNXnet/IP
    DIB structure or a \l {default-constructed value} in case of an error.
*/
QKnxNetIpTunnelingSlotInfo QKnxNetIpTunnelingInfoDibProxy::tunnelingSlotInfo() const
{
    return QKnxNetIpTunnelingSlotInfo::fromBytes(m_dib.constData(), 2);
}

/*!
    Returns a vector of \l QKnxNetIpTunnelingSlotInfo objects carried by this
    KNXnet/IP DIB structure if the object that was passed during construction
    was valid; otherwise returns an empty vector.
*/
QVector<QKnxNetIpTunnelingSlotInfo> QKnxNetIpTunnelingInfoDibProxy::optionalSlotInfos() const
{
    QVector<QKnxNetIpTunnelingSlotInfo> infos;
    if (!isValid())
        return infos;

    const auto &data = m_dib.constData();
    // 6 bytes max APDU + mandatory slot info, advance by 4 -> expected info size
    for (quint16 i = 6 ; i < data.size(); i += 4) {
        auto info = QKnxNetIpTunnelingSlotInfo::fromBytes(data, i);
        if (!info.isValid())
            return {};
        infos.append(info);
    }
    return infos;
}

/*!
    Returns a builder object to create a KNXnet/IP tunneling info DIB structure.
*/
QKnxNetIpTunnelingInfoDibProxy::Builder QKnxNetIpTunnelingInfoDibProxy::builder()
{
    return QKnxNetIpTunnelingInfoDibProxy::Builder();
}


/*!
    \class QKnxNetIpTunnelingInfoDibProxy::Builder

    \since 5.12
    \inmodule QtKnx
    \inheaderfile QKnxNetIpTunnelingInfoDibProxy

    \brief The QKnxNetIpTunnelingInfoDibProxy::Builder class creates a
    KNXnet/IP tunneling info DIB structure.

    A KNXnet/IP tunneling info DIB structure contains the maximum application
    layer protocol data unit (APDU) length supported by a KNXnet/IP tunneling
    interface for bus access and one or more tunneling slot information objects.

    The common way to create such a DIB structure is:
    \code
        auto dib = QKnxNetIpTunnelingInfoDibProxy::builder()
            .setMaximumInterfaceApduLength(0x1000)
            .setTunnelingSlotInfo({ { QKnxAddress::Type::Individual, 1976 },
                QKnxNetIpTunnelingSlotInfo::Available
            }).setOptionalSlotInfos({
                { { QKnxAddress::Type::Individual, 2013 },
                    QKnxNetIpTunnelingSlotInfo::Usable
                        | QKnxNetIpTunnelingSlotInfo::Authorized
                        | QKnxNetIpTunnelingSlotInfo::Free },
                { { QKnxAddress::Type::Individual, 1978 }, QKnxNetIpTunnelingSlotInfo::NotAvailable },
                { QKnxNetIpTunnelingSlotInfo({ QKnxAddress::Type::Individual, 2002 }) }
            }).create();
    \endcode
*/

class QKnxNetIpTunnelingInfoDibPrivate : public QSharedData
{
public:
    QKnxNetIpTunnelingInfoDibPrivate() = default;
    ~QKnxNetIpTunnelingInfoDibPrivate() = default;

    quint16 maxApduLength { 0 };
    QKnxNetIpTunnelingSlotInfo m_info; // mandatory
    QVector<QKnxNetIpTunnelingSlotInfo> m_infos;
};

/*!
    Creates a new empty tunneling info DIB structure builder object.
*/
QKnxNetIpTunnelingInfoDibProxy::Builder::Builder()
    : d_ptr(new QKnxNetIpTunnelingInfoDibPrivate)
{}

/*!
    Destroys the object and frees any allocated resources.
*/
QKnxNetIpTunnelingInfoDibProxy::Builder::~Builder() = default;

/*!
    Sets the maximum APDU length of the KNXnet/IP DIB structure to \a length
    and returns a reference to the builder.
*/
QKnxNetIpTunnelingInfoDibProxy::Builder &
    QKnxNetIpTunnelingInfoDibProxy::Builder::setMaximumInterfaceApduLength(quint16 length)
{
    d_ptr->maxApduLength = length;
    return *this;
}

/*!
    Sets the mandatory tunneling slot information of the KNXnet/IP DIB structure
    to \a info and returns a reference to the builder.
*/
QKnxNetIpTunnelingInfoDibProxy::Builder &QKnxNetIpTunnelingInfoDibProxy::Builder
    ::setTunnelingSlotInfo(const QKnxNetIpTunnelingSlotInfo &info)
{
    d_ptr->m_info = info;
    return *this;
}

/*!
    Sets the optional tunneling slot information of the KNXnet/IP DIB structure
    to \a infos and returns a reference to the builder.
*/
QKnxNetIpTunnelingInfoDibProxy::Builder &QKnxNetIpTunnelingInfoDibProxy::Builder
    ::setOptionalSlotInfos(const QVector<QKnxNetIpTunnelingSlotInfo> &infos)
{
    d_ptr->m_infos = infos;
    return *this;
}

/*!
    Creates and returns a tunneling info QKnxNetIpDib.

    \note The returned structure may be invalid depending on the values used
    during setup.

    \sa isValid()

    \note Invalid tunneling slot information objects are not taken into account
    when creating the tunneling information QKnxNetIpDib.
*/
QKnxNetIpDib QKnxNetIpTunnelingInfoDibProxy::Builder::create() const
{
    if (!d_ptr->m_info.isValid())
        return { QKnxNetIp::DescriptionType::TunnelingInfo };

    auto bytes = QKnxUtils::QUint16::bytes(d_ptr->maxApduLength) + d_ptr->m_info.bytes();
    for (const auto &info : qAsConst(d_ptr->m_infos)) {
        if (info.isValid())
            bytes += info.bytes();
    }
    return { QKnxNetIp::DescriptionType::TunnelingInfo, bytes };
}

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

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

QT_END_NAMESPACE