summaryrefslogtreecommitdiffstats
path: root/src/knx/qknxlinklayerframebuilder.cpp
blob: 70bbeb6a1eb987ab7464a2ec1d7cf0d12dd3e7c7 (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
/******************************************************************************
**
** 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 "qknxlinklayerframebuilder.h"

// TODO: introduce QKnx::MediumType dependency. The LinkLayer Frame look different
// depending on the chosen medium. For the moment only netIp Tunneling is taken care of.
// But GroupValue service can be send via L_Data (netIp) or L_Data_Standard / L_Data_Extended
// if medium is TP of PL. Maybe transform this factory into a builder, somehow "linked" to
// the LinkLayerFrame, so that the MediumType is known.

QT_BEGIN_NAMESPACE

/*!
    \class QKnxLinkLayerFrame::Builder

    \inmodule QtKnx
    \inheaderfile QKnxLinkLayerFrameBuilder
    \ingroup qtknx-tunneling

    \brief The QKnxLinkLayerFrame::Builder class provides the means to create a
    KNX link layer frame.

    A KNX link layer frame contains several fields: message code, additional
    information, control field, maybe an extended control field, a TPDU, as
    well as a source address and destination address. The fields must be valid
    for the builder to be able to create a frame.

    The builder is set up with default values to create a common external
    message interface (cEMI) group value read frame, except for the destination
    address, which needs to be specified during the creation of the frame.

    The following code sample illustrates how to establish a KNXnet/IP tunnel
    connection, to create group value read and write frames, and to send the
    frames over the connection:

    \code
        QKnxNetIpTunnel tunnel(...);
        tunnel.connectToHost(...);

        auto frame = QKnxLinkLayerFrame::builder()
            .setDestinationAddress({ QKnxAddress::Type::Group, QLatin1String("2/1/4") })
            .create();

        tunnel.sendFrame(frame);

        frame = QKnxLinkLayerFrame::builder()
            .setDestinationAddress({ QKnxAddress::Type::Group, QLatin1String("2/1/4") })
            .setTpdu({
                QKnxTpdu::TransportControlField::DataGroup,
                QKnxTpdu::ApplicationControlField::GroupValueWrite,
                QKnxSwitch(QKnxSwitch::State::On).bytes()
            }).create();

        tunnel.sendFrame(frame);
    \endcode

    \sa QKnxLinkLayerFrame, {Qt KNX Tunneling Classes}
*/

/*!
    Sets the source address to be used by the builder to \a source and returns
    a reference to the builder.

    \sa QKnxAddress
*/
QKnxLinkLayerFrame::Builder &
    QKnxLinkLayerFrame::Builder::setSourceAddress(const QKnxAddress &source)
{
    m_src = source;
    return *this;
}

/*!
    Sets the destination address to be used by the builder to \a dest and
    returns a reference to the builder.

    \sa QKnxAddress
*/
QKnxLinkLayerFrame::Builder &
    QKnxLinkLayerFrame::Builder::setDestinationAddress(const QKnxAddress &dest)
{
    m_dest = dest;
    return *this;
}

/*!
    Sets the control field to be used by the builder to \a ctrl and returns a
    reference to the builder.

    \sa QKnxControlField
*/
QKnxLinkLayerFrame::Builder &
    QKnxLinkLayerFrame::Builder::setControlField(const QKnxControlField &ctrl)
{
    m_ctrl = ctrl;
    return *this;
}

/*!
    Sets the extended control field to be used by the builder to \a extCtrl and
    returns a reference to the builder.

    \sa QKnxExtendedControlField
*/
QKnxLinkLayerFrame::Builder &
    QKnxLinkLayerFrame::Builder::setExtendedControlField(const QKnxExtendedControlField &extCtrl)
{
    m_extCtrl = extCtrl;
    return *this;
}

/*!
    Sets the TPDU to be used by the builder to \a tpdu and returns a reference
    to the builder.

    \sa QKnxTpdu
*/
QKnxLinkLayerFrame::Builder &QKnxLinkLayerFrame::Builder::setTpdu(const QKnxTpdu &tpdu)
{
    m_tpdu = tpdu;
    return *this;
}

/*!
    Sets the medium that will determine the format and order of fields to
    \a type and returns a reference to the builder.

    \sa QKnx::MediumType
*/
QKnxLinkLayerFrame::Builder &QKnxLinkLayerFrame::Builder::setMedium(QKnx::MediumType type)
{
    m_mediumType = type;
    return *this;
}

/*!
    Sets an array of bytes specified by \a data with all the fields encoded into
    a byte array starting from the position \a offset to avoid having to use
    each of the field setters separately.

    \sa createFrame(), QKnxByteArray
*/
QKnxLinkLayerFrame::Builder &
    QKnxLinkLayerFrame::Builder::setData(const QKnxByteArray &data, quint16 offset)
{
    m_data = data;
    m_dataOffset = offset;
    return *this;
}

/*!
    Sets the link layer frame message code to be used by the builder to \a code
    and returns a reference to the builder.

    \sa QKnxLinkLayerFrame::MessageCode
*/
QKnxLinkLayerFrame::Builder &
    QKnxLinkLayerFrame::Builder::setMessageCode(QKnxLinkLayerFrame::MessageCode code)
{
    m_code = code;
    return *this;
}

/*!
    Sets the additional information to be used by the builder to \a infos
    and returns a reference to the builder.

    \sa QKnxAdditionalInfo
*/
QKnxLinkLayerFrame::Builder &
    QKnxLinkLayerFrame::Builder::setAdditionalInfos(const QVector<QKnxAdditionalInfo> &infos)
{
    m_additionalInfos = infos;
    return *this;
}

/*!
    Creates a frame from the values set for the link layer frame message code,
    additional information, control field, extended control field, TPDU, source
    address, and destination address. Alternatively, uses a byte array with all
    the fields already encoded into it.

    \sa QKnxLinkLayerFrame, QKnxLinkLayerFrame::MessageCode, QKnxAdditionalInfo,
    QKnxControlField, QKnxExtendedControlField, QKnxTpdu, QKnxAddress,
    QKnxByteArray
*/
QKnxLinkLayerFrame QKnxLinkLayerFrame::Builder::createFrame() const
{
    if (m_mediumType == QKnx::MediumType::Unknown)
        return {};

    if (m_data.size() > 0) // ignore member variables and create frame from byte representation
        return QKnxLinkLayerFrame::fromBytes(m_data, m_dataOffset, m_data.size(), m_mediumType);

    if (m_dest.type() != m_extCtrl.destinationAddressType()
        || !m_src.isValid()
        || !m_dest.isValid()
        || !m_tpdu.isValid()
        || m_extCtrl.hopCount() >= 7
        || m_tpdu.sequenceNumber() > 15)
        return {};

    QKnxLinkLayerFrame frame;
    frame.setDestinationAddress(m_dest);
    frame.setSourceAddress(m_src);
    // if data transmitted is over 15 bytes an extended frame is needed
    if (m_tpdu.dataSize() > 15) {
        m_ctrl.setFrameFormat(QKnxControlField::FrameFormat::Extended);
        m_ctrl.setPriority(QKnxControlField::Priority::Low);
    }
    frame.setControlField(m_ctrl);
    frame.setExtendedControlField(m_extCtrl);
    frame.setTpdu(m_tpdu);
    frame.setMediumType(m_mediumType);
    frame.setMessageCode(m_code);
    for (const auto &info : m_additionalInfos)
        frame.addAdditionalInfo(info);
    return frame;
}

QT_END_NAMESPACE