summaryrefslogtreecommitdiffstats
path: root/src/knx/qknxcontrolfield.cpp
blob: 6a10ad3f9730e3e98bb2e442a7bb8db105d31e3c (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
/******************************************************************************
**
** 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 "qknxcontrolfield.h"

QT_BEGIN_NAMESPACE

const std::bitset<8> gPriorityMask = 0x0c;

/*!
    \class QKnxControlField

    \inmodule QtKnx
    \ingroup qtknx-general-classes

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

    A KNX frame contains several fields, one of which might be the control
    field. The control field must specify at least the \l FrameFormat, the
    \l Repeat flag, and the frame \l Priority. In addition, it may specify
    how widely the frame is \l Broadcast, and whether acknowledgment
    (\l Acknowledge) or confirmation (\l Confirm) is requested for the
    transmission or reception of the frame.

    \section2 Selecting Frame Format

    The control field selects the standard or extended frame format for the data
    link layer and includes information for the used extended frame type. The
    standard format should be preferred and the extended format should only be
    used when the encoding capabilities of the standard format are insufficient.

    \section2 Selecting Frame Priority

    The priority of frames should be selected carefully to ensure fair bus
    access. The maximum priority that is allowed for run-time communication is
    \e normal. The \e system priority is reserved for system configuration
    and management procedures.

    The \e urgent priority is only allowed for implementations of datapoints of
    functional blocks or channels for which this is specified explicitly. In a
    network, the frame traffic using urgent priority shall not exceed five
    percent of the total traffic.

    \sa builder(), QKnxExtendedControlField
*/

/*!
    \enum QKnxControlField::FrameFormat

    This enum type selects the standard or extended frame format for the data
    link layer and includes information for the used extended frame type.

    \value Extended
           This parameter is mapped to
           \l QKnxExtendedControlField::ExtendedFrameFormat. The extended frame
           format is used and the frame type is selected by the frame type
           parameter bit.
    \value Standard
           The standard frame format is used.
*/

/*!
    \enum QKnxControlField::Repeat

    This enum type holds whether the frame transmission is repeated.
    If one of the addressed bus devices has returned a negative acknowledgment,
    the repeat flag is set to \c Repeat.

    \value Repeat
           Frame transmission is repeated.
    \value DoNotRepeat
           Frame transmission is not repeated.
*/

/*!
    \enum QKnxControlField::Broadcast

    This enum type specifies whether the frame is transmitted using system
    broadcast communication mode or broadcast communication mode.

    \value System
           The frame is transmitted using system broadcast communication mode.
    \value Domain
           The frame is transmitted using broadcast communication mode.
*/

/*!
    \enum QKnxControlField::Priority

    This enum type holds the priority used for the transmission or reception of
    the frame.

    \value System
           System priority is used. It is reserved for high priority frames, as
           well as system configuration and management procedures.
    \value Normal
           Normal priority is used. It is the default priority for short frames.
    \value Urgent
           High priority is used. It is reserved for urgent frames.
    \value Low
           Low priority is used. It is mandatory for long frames, burst traffic,
           and so on.
*/

/*!
    \enum QKnxControlField::Acknowledge

    This enum type holds whether a Layer 2 acknowledge is requested for an
    \c {L_Data.req} frame that is used in client-to-server communiction on
    the KNX data link layer. This is not valid for all media.

    \value NotRequested
           Acknowledgment is not requested.
    \value Requested
           Acknowledgment is requested.

    \sa QKnxLinkLayerFrame::MessageCode
*/

/*!
    \enum QKnxControlField::Confirm

    This enum type holds whether there is an error in the transmitted frame.

    \value NoError
           No errors found.
    \value Error
           Errors found.
*/

/*!
    \fn QKnxControlField::frameFormat() const

    Returns the frame format.
*/

/*!
    \fn QKnxControlField::repeat() const

    Returns whether frame transmission is repeated.
*/

/*!
    \fn QKnxControlField::broadcast() const

    Returns whether the frame is transmitted using system broadcast
    communication mode or broadcast communication mode.
*/

/*!
    \fn QKnxControlField::acknowledge() const

    Returns whether a Layer 2 acknowledge is requested for an \c {L_Data.req}
    frame.

    \sa QKnxLinkLayerFrame::MessageCode
*/

/*!
    \fn QKnxControlField::confirm() const

    For an \c {L_Data.con} frame, returns \c true if there is an error in the
    transmitted frame; otherwise returns \c false.

    \sa QKnxLinkLayerFrame::MessageCode
*/

/*!
    \fn QKnxControlField::setFrameFormat(QKnxControlField::FrameFormat type)

    Sets the frame type bit of the the frame format parameter to \a type.
*/

/*!
    \fn QKnxControlField::setRepeat(QKnxControlField::Repeat repeat)

    Sets the repetition flag to \a repeat.
*/

/*!
    \fn QKnxControlField::setBroadcast(QKnxControlField::Broadcast bcst)

    Sets the broadcast flag to \a bcst.
*/

/*!
    \fn QKnxControlField::setAcknowledge(QKnxControlField::Acknowledge ack)

    Sets whether acknowledgment for an \c {L_Data.req} frame is requested to
    \a ack.

    \sa QKnxLinkLayerFrame::MessageCode
*/

/*!
    \fn QKnxControlField::setConfirm(QKnxControlField::Confirm confirm)

    Sets the confirm flag to \a confirm.
*/

/*!
    \fn quint8 QKnxControlField::byte() const

    Returns the control field as single byte.
*/

/*!
    \fn quint8 QKnxControlField::size() const

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

/*!
    \fn QKnxControlField::QKnxControlField()

    Creates a KNX control field.
*/

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

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

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

/*!
    Returns the priority that is used for the transmission or reception of
    the frame.
*/
QKnxControlField::Priority QKnxControlField::priority() const
{
    return static_cast<Priority> ((m_ctrl1 & gPriorityMask).to_ulong() >> 2);
}

/*!
    Sets the \a priority that is used for the transmission or reception of
    the frame.
*/
void QKnxControlField::setPriority(QKnxControlField::Priority priority)
{
    m_ctrl1 &= ~gPriorityMask; // clear
    m_ctrl1 |= (static_cast<quint8> (priority) << 2); // set
}

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

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

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

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

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

/*!
    \relates QKnxControlField

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


/*!
    \class QKnxControlField::Builder

    \inmodule QtKnx
    \inheaderfile QKnxControlField

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

    The control field of a KNX frame must specify at least the \l FrameFormat,
    the \l Repeat flag, and the frame \l Priority. In addition, it may specify
    how widely the frame is \l Broadcast, and whether acknowledgment
    (\l Acknowledge) or confirmation (\l Confirm) is requested for the
    transmission or reception of the frame.

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

    The control field selects the standard or extended frame format for the data
    link layer and includes information for the used extended frame type. By
    default, the frame format is set to \l Standard, which is the preferred
    format for short frames.

    The repeat flag determines whether the frame transmission is repeated if the
    status of an acknowledgment frame received from a bus device indicates that
    an error has occurred. By default, the repeat flag is set to \l DoNotRepeat.

    The broadcast flag selects the communication mode used for transmitting
    the frame. By default, it is set to \l Domain to transmit frames using
    broadcast communication mode.

    The priority of frames should be selected carefully to ensure fair bus
    access. By default, the priority of a frame is set to \l Low, which is
    mandatory for long frames. The maximum priority that is allowed for
    run-time communication is \l Normal.

    The acknowledge flag determines whether a Layer 2 acknowledge is requested
    for an \c {L_Data.req} frame that is used in client-to-server communiction
    on the KNX data link layer. By default, it is set to \l NotRequested.

    The confirm flag indicates whether there is an error in the transmitted
    frame. By default, it is set to \l NoError.

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

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

    Some flags can be modified for more advanced use, such as setting up a
    control field used for unicast or broadcast with a higher priority:

    \code
        auto ctrl = QKnxControlField::builder
            .setPriority(QKnxControlField::Priority::System)
            .create();
    \endcode
*/

/*!
    Sets the frame type bit of the frame format parameter to \a type and returns
    a reference to the builder.
*/
QKnxControlField::Builder &
    QKnxControlField::Builder::setFrameFormat(QKnxControlField::FrameFormat type)
{
    m_frameFormat = type;
    return *this;
}

/*!
    Sets the repeat flag to \a repeat and returns a reference to the builder.
*/
QKnxControlField::Builder &QKnxControlField::Builder::setRepeat(QKnxControlField::Repeat repeat)
{
    m_repeat = repeat;
    return *this;
}

/*!
    Sets the broadcast flag to \a broadcast and returns a reference to the builder.
*/
QKnxControlField::Builder &
    QKnxControlField::Builder::setBroadcast(QKnxControlField::Broadcast broadcast)
{
    m_broad = broadcast;
    return *this;
}

/*!
    Sets the priority flag to \a priority and returns a reference to the builder.
*/
QKnxControlField::Builder &
    QKnxControlField::Builder::setPriority(QKnxControlField::Priority priority)
{
    m_priority = priority;
    return *this;
}

/*!
    Sets the acknowledge flag to \a acknowledge and returns a reference to the builder.
*/
QKnxControlField::Builder &
    QKnxControlField::Builder::setAcknowledge(QKnxControlField::Acknowledge acknowledge)
{
    m_acknowledge = acknowledge;
    return *this;
}

/*!
    Sets the confirm flag to \a errorStatus and returns a reference to the builder.
*/
QKnxControlField::Builder &
    QKnxControlField::Builder::setConfirm(QKnxControlField::Confirm errorStatus)
{
    m_errorStatus = errorStatus;
    return *this;
}

/*!
    Creates and returns a KNX control field.
*/
QKnxControlField QKnxControlField::Builder::create() const
{
    QKnxControlField ctrlField;
    ctrlField.setFrameFormat(m_frameFormat);
    ctrlField.setRepeat(m_repeat);
    ctrlField.setBroadcast(m_broad);
    ctrlField.setPriority(m_priority);
    ctrlField.setAcknowledge(m_acknowledge);
    ctrlField.setConfirm(m_errorStatus);
    return ctrlField;
}

QT_END_NAMESPACE