summaryrefslogtreecommitdiffstats
path: root/src/knx/netip/qknxnetiphpai.cpp
blob: 4f2ebf82bb62acd8e9238328d3ee9c804e36a500 (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
/******************************************************************************
**
** 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 "qknxnetiphpai.h"
#include "qknxutils.h"

QT_BEGIN_NAMESPACE

/*!
    \class QKnxNetIpHpaiProxy

    \inmodule QtKnx
    \ingroup qtknx-netip

    \brief The QKnxNetIpHpaiProxy class provides the means to read the
    KNXnet/IP host address protocol information (HPAI) from the generic
    QKnxNetIpHpai class and to create such a structure.

    A KNXnet/IP HPAI structure contains the information that is necessary to
    uniquely identify an KNXnet/IP transport connection endpoint.

    The information needed to identify an KNXnet/IP transport connection
    endpoint includes the \l{hostProtocol()}{protocol}, \l{hostAddress()}
    {IP address}, and \l port number.

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

    Reading the host address and port number can be achieved like this:
    \code
        auto hpai = QKnxNetIpHpai::fromBytes(...);

        QKnxNetIpHpaiProxy proxy(hpai);
        if (!proxy.isValid())
            return;

        if (proxy.hostProtocol() == QKnxNetIp::HostProtocol::TCP_IPv4)
            return; // TCP support not implemented yet

        // read the host protocol address information
        auto address = proxy.hostAddress();
        auto port = proxy.port();
    \endcode

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

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

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

/*!
    \internal
    \fn QKnxNetIpHpaiProxy::QKnxNetIpHpaiProxy(const QKnxNetIpHpai &&)
*/

/*!
    Constructs a proxy object with the given a KNXnet/IP structure \a hpai to
    read the host protocol address information (HPAI).
*/
QKnxNetIpHpaiProxy::QKnxNetIpHpaiProxy(const QKnxNetIpHpai &hpai)
    : m_hpai(hpai)
{}

/*!
    Returns \c true if the KNXnet/IP structure to create the object is a valid
    KNXnet/IP HPAI structure, returns \c false otherwise.
*/
bool QKnxNetIpHpaiProxy::isValid() const
{
    bool validHostProtocolCode = m_hpai.code() == QKnxNetIp::HostProtocol::UDP_IPv4
        || m_hpai.code() == QKnxNetIp::HostProtocol::TCP_IPv4;
    return m_hpai.isValid() && m_hpai.size() == 8 && validHostProtocolCode;
}

/*!
    Return the host protocol from KNXnet/IP structure if the object passed
    during construction was valid, otherwise returns QKnxNetIp::Unknown.
*/
QKnxNetIp::HostProtocol QKnxNetIpHpaiProxy::hostProtocol() const
{
    if (isValid())
        return m_hpai.code();
    return QKnxNetIp::HostProtocol::Unknown;
}

/*!
    Returns the host address from KNXnet/IP structure if the object passed
    during construction was valid, otherwise returns a \e {default-constructed}
    QHostAddress.
*/
QHostAddress QKnxNetIpHpaiProxy::hostAddress() const
{
    if (isValid())
        return QKnxUtils::HostAddress::fromBytes(m_hpai.constData());
    return {};
}

/*!
    Returns the port number carried inside the KNXnet/IP host protocol address
    information structure.
*/
quint16 QKnxNetIpHpaiProxy::port() const
{
    return QKnxUtils::QUint16::fromBytes(m_hpai.constData(), 4);
}

/*!
    Returns a builder object to create a KNXnet/IP host protocol address
    information structure.
*/
QKnxNetIpHpaiProxy::Builder QKnxNetIpHpaiProxy::builder()
{
    return QKnxNetIpHpaiProxy::Builder();
}


/*!
    \class QKnxNetIpHpaiProxy::Builder

    \inmodule QtKnx
    \inheaderfile QKnxNetIpHpaiProxy

    \brief The QKnxNetIpHpaiProxy::Builder class provides the means to create a
    KNXnet/IP host address protocol information.

    A KNXnet/IP HPAI structure contains the information that is necessary to
    uniquely identify an KNXnet/IP transport connection endpoint.

    The information needed to identify an KNXnet/IP transport connection
    endpoint includes the \l{hostProtocol()}{protocol}, \l{hostAddress()}
    {IP address}, and \l port number.

    The common way to create such a HPAI structure is:
    \code
        auto hpai = QKnxNetIpHpaiProxy::builder()
            .setHostProtocol(QKnxNetIp::HostProtocol::TCP_IPv4
            .setHostAddress(QHostAddress::AnyIPv4)
            .setPort(2013)
            .create();
    \endcode

    By default setting the host protocol can be omitted if you want to target a
    KNXnet/IP server using a UDP/IP connection.
*/

/*!
    Sets the host protocol to \a code and returns a reference to the builder.
*/
QKnxNetIpHpaiProxy::Builder &QKnxNetIpHpaiProxy::Builder::setHostProtocol(QKnxNetIp::HostProtocol code)
{
    m_code = code;
    return *this;
}

/*!
    Sets the host address to \a address and returns a reference to the builder.
*/
QKnxNetIpHpaiProxy::Builder &QKnxNetIpHpaiProxy::Builder::setHostAddress(const QHostAddress &address)
{
    m_address = address;
    return *this;
}

/*!
    Sets the host port to \a port and returns a reference to the builder.
*/
QKnxNetIpHpaiProxy::Builder &QKnxNetIpHpaiProxy::Builder::setPort(quint16 port)
{
    m_port = port;
    return *this;
}

/*!
    Creates and returns a QKnxNetIpHpai.

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

    \sa isValid()
*/
QKnxNetIpHpai QKnxNetIpHpaiProxy::Builder::create() const
{
    return { m_code, QKnxUtils::HostAddress::bytes(m_address) + QKnxUtils::QUint16::bytes(m_port) };
}

QT_END_NAMESPACE