summaryrefslogtreecommitdiffstats
path: root/src/knx/netip/qknxnetipcri.cpp
blob: 633ffcf9e679c347ec7f1578ca2fe807ae9a55a5 (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
/******************************************************************************
**
** Copyright (C) 2017 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 "qknxnetipcri.h"

QT_BEGIN_NAMESPACE

/*!
    \class QKnxNetIpCriProxy

    \inmodule QtKnx
    \ingroup qtknx-netip

    \brief The QKnxNetIpCriProxy class provides the means to read the connection
    request information (CRI) from the generic \l QKnxNetIpCri class and to
    create a KNXnet/IP CRI structure based on the information.

    A KNXnet/IP CRI structure contains additional information needed for
    different types of communication channels to fulfill a connection request.

    The structure may contain host protocol dependent and independent data,
    but the current KNX specification foresees additional data only in the
    case of tunneling.

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

    Reading the connection type and tunneling layer can be achieved like this:
    \code
        auto cri = QKnxNetIpCri::fromBytes(...);

        QKnxNetIpCriProxy proxy(cri);
        if (!proxy.isValid())
            return;

        if (proxy.connectionType() != QKnxNetIp::ConnectionType::Tunnel)
            return;
        auto layer = proxy.tunnelLayer(); // read the requested tunneling layer
    \endcode

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

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

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

/*!
    \fn QKnxNetIpCriProxy::QKnxNetIpCriProxy(const QKnxNetIpCri &&)
    \internal
*/

/*!
    Constructs a proxy object with the specified a KNXnet/IP structure
    \a cri to read the connection request information (CRI).
*/
QKnxNetIpCriProxy::QKnxNetIpCriProxy(const QKnxNetIpCri &cri)
    : m_cri(cri)
{}

/*!
    Returns \c true if the KNXnet/IP structure to create the object is a valid
    KNXnet/IP CRI structure; otherwise returns \c false.
*/
bool QKnxNetIpCriProxy::isValid() const
{
    switch (m_cri.code()) {
        case QKnxNetIp::ConnectionType::Tunnel: {
            auto tmp = m_cri.constData().value(0);
            return m_cri.isValid() && QKnxNetIp::isTunnelLayer(QKnxNetIp::TunnelLayer(tmp))
                && (m_cri.size() == 4 || m_cri.size() == 6);
        }
        case QKnxNetIp::ConnectionType::DeviceManagement:
        case QKnxNetIp::ConnectionType::RemoteLogging:
        case QKnxNetIp::ConnectionType::RemoteConfiguration:
        case QKnxNetIp::ConnectionType::ObjectServer:
            return m_cri.isValid() && m_cri.size() == 2;
        case QKnxNetIp::ConnectionType::Unknown:
            break;
    }
    return false;
}

/*!
    \since 5.12

    Returns \c true if this is an extended connection request information (CRI)
    frame; otherwise returns \c false.

    \note No validation checks are performed on the given KNXnet/IP frame
    that was passed during construction.

    \sa isValid()
*/
bool QKnxNetIpCriProxy::isExtended() const
{
    return (m_cri.code() == QKnxNetIp::ConnectionType::Tunnel) && (m_cri.size() == 6);
}

/*!
    Returns the connection type of this KNXnet/IP structure if the object
    that was passed during construction was valid; otherwise returns
    \l QKnx::NetIp::Unknown.
*/
QKnxNetIp::ConnectionType QKnxNetIpCriProxy::connectionType() const
{
    if (isValid())
        return m_cri.code();
    return QKnxNetIp::ConnectionType::Unknown;
}

/*!
    Returns the tunneling layer of this KNXnet/IP structure if the object that
    was passed during construction was valid and the connection type
    is \l QKnx::NetIp::Tunnel, otherwise returns \l QKnx::NetIp::Unknown.

    \sa additionalData(), individualAddress()
*/
QKnxNetIp::TunnelLayer QKnxNetIpCriProxy::tunnelLayer() const
{
    if (isValid())
        return QKnxNetIp::TunnelLayer(m_cri.constData().value(0));
    return QKnxNetIp::TunnelLayer::Unknown;
}

/*!
    \since 5.12

    Returns the individual address of the extended connection request information
    structure if the object that was passed during construction was valid and an
    extended structure; otherwise returns \l QKnx::NetIp::Unknown.

    \sa isExtended(), tunnelLayer(), additionalData()
*/
QKnxAddress QKnxNetIpCriProxy::individualAddress() const
{
    if (isExtended() && isValid())
        return { QKnxAddress::Type::Individual, m_cri.constData().mid(2, 2) };
    return {};
}

/*!
    Returns the additional data of this KNXnet/IP structure.

    The current KNX specification foresees additional data only in the case of
    tunneling.

    \sa tunnelLayer(), individualAddress()
*/
QKnxByteArray QKnxNetIpCriProxy::additionalData() const
{
    return m_cri.data();
}

/*!
    Returns a builder object to create a KNXnet/IP connection request
    information structure.
*/
QKnxNetIpCriProxy::Builder QKnxNetIpCriProxy::builder()
{
    return QKnxNetIpCriProxy::Builder();
}


/*!
    \class QKnxNetIpCriProxy::Builder

    \inmodule QtKnx
    \inheaderfile QKnxNetIpCriProxy

    \brief The QKnxNetIpCriProxy::Builder class creates a KNXnet/IP connection
    request information structure (CRI).

    The connection request information structure contains additional
    information needed for different types of communication channels to
    fulfill a connection request.

    The common way to create such a CRI structure is:
    \code
        auto cri = QKnxNetIpCriProxy::builder()
            .setConnectionType(QKnxNetIp::ConnectionType::Tunnel)
            .setTunnelLayer(QKnxNetIp::TunnelLayer::Link)
            .create();
    \endcode
*/

/*!
    Sets the connection type to \a type and returns a reference to the builder.

    Does nothing if \a type is not a \l QKnx::NetIp::ConnectionType.
*/
QKnxNetIpCriProxy::Builder &
    QKnxNetIpCriProxy::Builder::setConnectionType(QKnxNetIp::ConnectionType type)
{
    if (QKnxNetIp::isStructType(type))
        m_cType = type;
    return *this;
}

/*!
    Sets the additional data to the requested KNX tunneling layer \a layer and
    returns a reference to the builder.

    Does nothing if \a layer is not a \l QKnx::NetIp::TunnelLayer value.

    \sa setAdditionalData(), setIndividualAddress()
*/
QKnxNetIpCriProxy::Builder &
    QKnxNetIpCriProxy::Builder::setTunnelLayer(QKnxNetIp::TunnelLayer layer)
{
    if (QKnxNetIp::isTunnelLayer(layer))
        m_additionalData.replace(0, 2, { quint8(layer), 0x00 /* reserved byte */ });
    return *this;
}

/*!
    \since 5.12

    Sets the individual address of the extended connection request information
    to \a address and returns a reference to the builder.

    Does nothing if \a address is not of type
    \l {QKnxAddress::Type}{QKnxAddress::Type::Individual}.

    \note The current KNX specification foresees setting an individual address
    only in the case of extended tunneling connection requests.

    \sa setTunnelLayer(), setAdditionalData()
*/
QKnxNetIpCriProxy::Builder &
    QKnxNetIpCriProxy::Builder::setIndividualAddress(const QKnxAddress &address)
{
    if (address.type() == QKnxAddress::Type::Individual) {
        m_additionalData.resize(2);
        m_additionalData.append(address.bytes());
    }
    return *this;
}

/*!
    Sets the additional data to \a additionalData and returns a reference to
    the builder.

    The current KNX specification foresees additional data only in the case of
    tunneling.

    The common way to use the function is:
    \code
        QKnxAddress ia { QKnxAddress::Type::Individual, 2013 };
        auto cri = QKnxNetIpCriProxy::builder()
            .setConnectionType(QKnxNetIp::ConnectionType::Tunnel)
            .setAdditionalData(QKnxByteArray {
                   quint8(QKnxNetIp::TunnelLayer::Link), // tunnel layer
                   0x00                                  // reserved byte
                } + ia.bytes() // address for extended connection request
            ).create();
    \endcode

    The above code is equivalent to the more expressive one shown here:
    \code
        auto cri = QKnxNetIpCriProxy::builder()
            .setConnectionType(QKnxNetIp::ConnectionType::Tunnel)
            .setTunnelLayer(QKnxNetIp::TunnelLayer::Link)
            .setIndividualAddress({ QKnxAddress::Type::Individual, 2013 })
            .create();
    \endcode

    \sa setTunnelLayer(), setIndividualAddress()
*/
QKnxNetIpCriProxy::Builder &
    QKnxNetIpCriProxy::Builder::setAdditionalData(const QKnxByteArray &additionalData)
{
    m_additionalData = additionalData;
    return *this;
}

/*!
    Creates and returns a \l QKnxNetIpCri structure.

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

    \sa isValid()
*/
QKnxNetIpCri QKnxNetIpCriProxy::Builder::create() const
{
    return { m_cType, m_additionalData };
}

QT_END_NAMESPACE