summaryrefslogtreecommitdiffstats
path: root/src/knx/qknxcontrolfield.h
blob: 02a0ddbb528dc9528e7840e6c2ce96bb50624f77 (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
/******************************************************************************
**
** 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$
**
******************************************************************************/

#ifndef QKNXCONTROLFIELD_H
#define QKNXCONTROLFIELD_H

#include <QtCore/qdebug.h>
#include <QtKnx/qknxbytearray.h>
#include <QtKnx/qtknxglobal.h>

#include <bitset>

QT_BEGIN_NAMESPACE

class Q_KNX_EXPORT QKnxControlField final
{
    Q_GADGET

    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));
    }

public:
    QKnxControlField() = default;
    explicit QKnxControlField(quint8 data);
    explicit QKnxControlField(const QKnxByteArray &data);

    enum class FrameFormat : quint8
    {
        Extended = 0x00,
        Standard = 0x01
    };
    Q_ENUM(FrameFormat)
    QKnxControlField::FrameFormat frameFormat() const // ### Qt6: Replace byte() with m_ctrl1
        { return static_cast<FrameFormat> (quint8(testBit(byte(), 7))); }
    void setFrameFormat(QKnxControlField::FrameFormat type) // ### Qt6: Replace byte() with m_ctrl1
        { m_ctrl1 = setBit(byte(), bool(static_cast<int> (type)), 7); }

    enum class Repeat : quint8
    {
        Repeat = 0x00,
        DoNotRepeat = 0x01
    };
    Q_ENUM(Repeat)
    QKnxControlField::Repeat repeat() const // ### Qt6: Replace byte() with m_ctrl1
        { return static_cast<Repeat> (quint8(testBit(byte(), 5))); }
    void setRepeat(QKnxControlField::Repeat repeat) // ### Qt6: Replace byte() with m_ctrl1
        { m_ctrl1 = setBit(byte(), bool(static_cast<int> (repeat)), 5); }

    enum class Broadcast : quint8
    {
        System = 0x00,
        Domain = 0x01
    };
    Q_ENUM(Broadcast)
    QKnxControlField::Broadcast broadcast() const // ### Qt6: Replace byte() with m_ctrl1
        { return static_cast<Broadcast> (quint8(testBit(byte(), 4))); }
    void setBroadcast(QKnxControlField::Broadcast bcst) // ### Qt6: Replace byte() with m_ctrl1
        { m_ctrl1 = setBit(byte(), bool(static_cast<int> (bcst)), 4); }

    enum class Priority : quint8
    {
        System = 0x00,
        Normal = 0x01,
        Urgent = 0x02,
        Low = 0x03
    };
    Q_ENUM(Priority)
    QKnxControlField::Priority priority() const;
    void setPriority(QKnxControlField::Priority priority);

    enum class Acknowledge : quint8
    {
        NotRequested = 0x00,
        Requested = 0x01
    };
    Q_ENUM(Acknowledge)
    QKnxControlField::Acknowledge acknowledge() const // ### Qt6: Replace byte() with m_ctrl1
        { return Acknowledge(quint8(testBit(byte(), 1))); }
    void setAcknowledge(QKnxControlField::Acknowledge ack) // ### Qt6: Replace byte() with m_ctrl1
        { m_ctrl1 = setBit(byte(), bool(static_cast<int> (ack)), 1); }

    enum class Confirm : quint8
    {
        NoError = 0x00,
        Error = 0x01
    };
    Q_ENUM(Confirm)
    QKnxControlField::Confirm confirm() const // ### Qt6: Replace byte() with m_ctrl1
        { return static_cast<Confirm> (quint8(testBit(byte(), 0))); }
    void setConfirm(QKnxControlField::Confirm confirm) // ### Qt6: Replace byte() with m_ctrl1
        { m_ctrl1 = setBit(byte(), bool(static_cast<int> (confirm)), 0); }

    quint8 byte() const { return quint8(m_ctrl1.to_ulong()); }
    QKnxByteArray bytes() const { return { byte() }; }

    quint8 size() const { return 1; }

    bool operator==(const QKnxControlField &other) const;
    bool operator!=(const QKnxControlField &other) const;

    class Q_KNX_EXPORT Builder
    {
    public:
        Builder &setFrameFormat(QKnxControlField::FrameFormat type);
        Builder &setRepeat(QKnxControlField::Repeat repeat);
        Builder &setBroadcast(QKnxControlField::Broadcast broadcast);
        Builder &setPriority(QKnxControlField::Priority priority);
        Builder &setAcknowledge(QKnxControlField::Acknowledge acknowledge);
        Builder &setConfirm(QKnxControlField::Confirm errorStatus);

        QKnxControlField create() const;

    private:
        QKnxControlField::FrameFormat m_frameFormat { FrameFormat::Standard };
        QKnxControlField::Repeat m_repeat { Repeat::DoNotRepeat };
        QKnxControlField::Broadcast m_broad { Broadcast::Domain };
        QKnxControlField::Priority m_priority { Priority::Low };
        QKnxControlField::Acknowledge m_acknowledge { Acknowledge::NotRequested };
        QKnxControlField::Confirm m_errorStatus { Confirm::NoError };
    };
    static QKnxControlField::Builder builder();

private:
    std::bitset<8> m_ctrl1; // ### Qt6: Replace with quint8
};
Q_KNX_EXPORT QDebug operator<<(QDebug debug, const QKnxControlField &ctrl);

Q_DECLARE_TYPEINFO(QKnxControlField::FrameFormat, Q_PRIMITIVE_TYPE);
Q_DECLARE_TYPEINFO(QKnxControlField::Repeat, Q_PRIMITIVE_TYPE);
Q_DECLARE_TYPEINFO(QKnxControlField::Broadcast, Q_PRIMITIVE_TYPE);
Q_DECLARE_TYPEINFO(QKnxControlField::Priority, Q_PRIMITIVE_TYPE);
Q_DECLARE_TYPEINFO(QKnxControlField::Acknowledge, Q_PRIMITIVE_TYPE);
Q_DECLARE_TYPEINFO(QKnxControlField::Confirm, Q_PRIMITIVE_TYPE);

QT_END_NAMESPACE

#endif