summaryrefslogtreecommitdiffstats
path: root/src/knx/qknxextendedcontrolfield.cpp
blob: 849aa2f5c0be850bd9a7a5f4814d3f4335a88c1d (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
/******************************************************************************
**
** 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 "qknxextendedcontrolfield.h"

QT_BEGIN_NAMESPACE

const std::bitset<8> gHopCountMask = 0x70;

static constexpr bool testBit(quint8 byteToTest, quint8 bit) noexcept
{
    return (byteToTest & (quint8(1) << bit)) != 0;
}

// ### Qt6: pass byteToSet as reference
static constexpr quint8 setBit(quint8 byteToSet, bool value, quint8 bit) noexcept
{
    return (value ? byteToSet | (quint8(1) << bit) : byteToSet & ~(quint8(1) << bit));
}

/*!
    \class QKnxExtendedControlField

    \inmodule QtKnx
    \ingroup qtknx-general-classes

    \brief The QKnxExtendedControlField class represents an 8-bit extended KNX
    control field.

    A KNX frame contains several fields, one of which might be the extended
    control field. The extended control field must specify
    \l destinationAddressType(), \l hopCount(), and \l ExtendedFrameFormat.

    The address type determines whether group addresses or individual addresses
    are used to establish a communication channel between devices.

    The hop count specifies the number of subnetworks that the frame is allowed
    to pass. It prevents the endless circulation of messages in incorrectly
    configured installations.

    The value of the \l ExtendedFrameFormat flag is mapped to the value of
    \l QKnxControlField::FrameFormat. If it is set to \c Extended, the
    extended frame format is used and the frame type is selected by the frame
    type parameter bit.

    The following is an example of how to create a control field from a
    KNX byte array:

    \code
        auto data = QKnxByteArray::fromHex("e0");
        const QKnxExtendedControlField extCtrlField(data);
    \endcode

    In addition to the default constructors a builder can be used:

    \code
        auto extCtrl = QKnxExtendedControlField::builder()
            .setDestinationAddressType(QKnxAddress::Type::Group)
            .setHopCount(6)
            .setFormat(QKnxExtendedControlField::ExtendedFrameFormat::Standard)
            .create();
    \endcode

    \sa builder(), QKnxControlField
*/

/*!
    \enum QKnxExtendedControlField::ExtendedFrameFormat

    This enum type holds the extended frame format.

    \value Standard
           The frame has standard format.
    \value Lte
           The frame has Logical Tag Extended (LTE) format.
*/

/*!
    \fn QKnxExtendedControlField::byte() const

    Returns the extended control field as a byte.
*/

/*!
    \fn QKnxExtendedControlField::size() const

    Returns the number of bytes in the control field.
*/

/*!
    \fn QKnxExtendedControlField::QKnxExtendedControlField()

    Creates an extended control field.
*/

/*!
    Creates a new extended control field from an 8-bit \a data value.
*/
QKnxExtendedControlField::QKnxExtendedControlField(quint8 data)
    : m_ctrl2(data)
{}

/*!
    Creates a new extended control field from the first byte of the \a data
    byte array.

    \note The byte array must contain at least one element.
*/
QKnxExtendedControlField::QKnxExtendedControlField(const QKnxByteArray &data)
{
    if (data.size() > 0)
        m_ctrl2 = quint8(data.at(0));
}

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

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

/*!
    Returns the destination address type.
*/
QKnxAddress::Type QKnxExtendedControlField::destinationAddressType() const
{
    // ### Qt6: Replace byte() with m_ctrl2
    return static_cast<QKnxAddress::Type> (quint8(testBit(byte(), 7)));
}

/*!
    Sets the destination address type to \a address.
*/
void QKnxExtendedControlField::setDestinationAddressType(QKnxAddress::Type address)
{
    // ### Qt6: Replace byte() with m_ctrl2
    m_ctrl2 = setBit(byte(), bool(static_cast<int> (address)), 7);
}

/*!
    Returns the hop count.
*/
quint8 QKnxExtendedControlField::hopCount() const
{
    return static_cast<quint8> ((m_ctrl2 & gHopCountMask).to_ulong() >> 4);
}

/*!
    Sets the hop count to \a hopCount.
*/
void QKnxExtendedControlField::setHopCount(quint8 hopCount)
{
    if (hopCount > 7)
        return;

    m_ctrl2 &= ~gHopCountMask; // clear
    m_ctrl2 |= (hopCount << 4); // set
}

/*!
    Returns the format of the extended control field.
*/
QKnxExtendedControlField::ExtendedFrameFormat QKnxExtendedControlField::format() const
{
    // ### Qt6: Replace byte() with m_ctrl2
    return static_cast<ExtendedFrameFormat> (quint8(testBit(byte(), 3)));
}

/*!
    Sets the format of the extended control field to \a format.
*/
void QKnxExtendedControlField::setFormat(QKnxExtendedControlField::ExtendedFrameFormat format)
{
    // ### Qt6: Replace byte() with m_ctrl2
    m_ctrl2 = setBit(byte(), format == QKnxExtendedControlField::ExtendedFrameFormat::Lte, 3);
}

/*!
    Returns a builder to create a KNX extended control field object.
*/
QKnxExtendedControlField::Builder QKnxExtendedControlField::builder()
{
    return Builder();
}

/*!
    \fn quint8 QKnxExtendedControlField::bytes() const

    Returns the extended control field as a range of bytes.
*/

/*!
    \relates QKnxExtendedControlField

    Writes the extended control field \a field to the \a debug stream.
*/
QDebug operator<<(QDebug debug, const QKnxExtendedControlField &field)
{
    QDebugStateSaver _(debug);
    debug.nospace().noquote() << "0x" << hex << qSetFieldWidth(2) << qSetPadChar(QLatin1Char('0'))
        << field.bytes();
    return debug;
}


/*!
    \class QKnxExtendedControlField::Builder

    \inmodule QtKnx
    \inheaderfile QKnxExtendedControlField

    \brief The QKnxExtendedControlField::Builder class creates a KNX extended
    control field with some default values set.

    The default values produce extended control fields that are suitable for
    multicast frames that read or write group values.

    By default, the address type is set to \l{QKnxAddress::Type}{Group} to
    specify that the frame is sent to a group address. The hop count is set
    to 6 to limit the number of subnetworks the frame is allowed to pass to
    six. This prevents the endless circulation of messages in incorrectly
    configured installations. The frame format is mapped to the value of
    \l QKnxControlField::FrameFormat, which is set to \c Standard by default.
    That is the preferred format for short frames.

    The following sample code creates an extended control field using the
    default values:

    Example:
    \code
        auto ctrl = QKnxExtendedControlField::builder.create();
    \endcode

    Some flags can be modified for more advanced use, such as setting up an
    extended control field used for transmitting a \c {L_Data_Extended} frame
    to the individual address of a device:

    \code
        auto ctrl = QKnxControlField::builder
            .setDestinationAddressType(QKnxAddress::Type::Individual)
            .create();
    \endcode
*/

/*!
    Sets the destination address type to \a address and returns a reference to
    the builder.
*/
QKnxExtendedControlField::Builder &
    QKnxExtendedControlField::Builder::setDestinationAddressType(QKnxAddress::Type address)
{
    m_address = address;
    return *this;
}

/*!
    Sets the hop count to \a hopCount and returns a reference to the builder.
*/
QKnxExtendedControlField::Builder &QKnxExtendedControlField::Builder::setHopCount(quint8 hopCount)
{
    m_hopCount = hopCount;
    return *this;
}

/*!
    Sets the extended frame format to \a format and returns a reference to the
    builder.
*/
QKnxExtendedControlField::Builder &
    QKnxExtendedControlField::Builder::setFormat(QKnxExtendedControlField::ExtendedFrameFormat format)
{
    m_format = format;
    return *this;
}

/*!
    Creates and returns a QKnxExtendedControlField.
*/
QKnxExtendedControlField QKnxExtendedControlField::Builder::create() const
{
    QKnxExtendedControlField extCtrl;
    extCtrl.setDestinationAddressType(m_address);
    extCtrl.setHopCount(m_hopCount);
    extCtrl.setFormat(m_format);
    return extCtrl;
}

QT_END_NAMESPACE