summaryrefslogtreecommitdiffstats
path: root/src/knx/netip/qknxnetiptunnelconnection.cpp
blob: fe7c0e652bfdeacd9dcb4ce287f418b30cb4e0ed (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
/******************************************************************************
**
** 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$
**
******************************************************************************/

#include "qknxnetipconnectresponse.h"
#include "qknxnetipendpointconnection_p.h"
#include "qknxnetiptunnelconnection.h"
#include "qknxnetiptunnelingrequest.h"

QT_BEGIN_NAMESPACE

/*!
    \class QKnxNetIpTunnelConnection

    \inmodule QtKnx
    \brief The QKnxNetIpTunnelConnection class enables the opening and handling
    of a client connection to a KNXnet/IP server.

    The QKnxNetIpTunnelConnection is a data connection between a client and a
    KNXnet/IP server endpoint. This is used to access functionalities of devices
    on a KNX bus. The IP address of the client must be set. It is then possible
    to connect to a chosen KNXnet/IP server and to send \l QKnxTunnelFrame
    frames to the KNXnet/IP server.

    The class takes care of connecting to the server, asking for a data
    connection, and monitoring the connection:

    \code
        QKnxNetIpTunnelConnection tunnel;
        QHostAddress clientLocalAddress = ...
        tunnel.setLocalAddress(clientLocalAddress);

        QHostAddress knxNetIpServerAddress = ...
        quint16 knxNetIpServerDataEndPointPort = ...
        tunnel.connectToHost(knxNetIpServerAddress, knxNetIpServerDataEndPointPort);

        QKnxTunnelFrame frame = ...
        tunnel.sendTunnelFrame(frame);
    \endcode
*/

class QKnxNetIpTunnelConnectionPrivate : public QKnxNetIpEndpointConnectionPrivate
{
    Q_DECLARE_PUBLIC(QKnxNetIpTunnelConnection)

public:
    QKnxNetIpTunnelConnectionPrivate(const QHostAddress &a, quint16 p, QKnxNetIp::TunnelingLayer l)
        : QKnxNetIpEndpointConnectionPrivate(a, p, QKnxNetIpCri(l), 1, QKnxNetIp::TunnelingRequestTimeout)
        , m_layer(l)
    {}

    void process(const QKnxCemiFrame &frame) override
    {
        Q_Q(QKnxNetIpTunnelConnection);
        emit q->receivedTunnelFrame(frame);
    }

    void process(const QKnxNetIpConnectResponse &response, const QNetworkDatagram &dg) override
    {
        if (response.status() == QKnxNetIp::Error::NoMoreUniqueConnections) {
            Q_ASSERT_X(false, "QKnxNetIpTunnelConnectionPrivate::process", "NoMoreUniqueConnections "
                "error handling not implemented yet.");
            // TODO: Maybe implement 03_08_04 Tunneling v01.05.03 AS.pdf, paragraph 3.3
        }

        Q_Q(QKnxNetIpTunnelConnection);
        if (q->state() != QKnxNetIpTunnelConnection::Connected)
            m_address = response.responseData().individualAddress();
        QKnxNetIpEndpointConnectionPrivate::process(response, dg);
    }

private:
    QKnxAddress m_address;
    QKnxNetIp::TunnelingLayer m_layer { QKnxNetIp::TunnelingLayer::Unknown };
};

QKnxNetIpTunnelConnection::QKnxNetIpTunnelConnection(QObject *parent)
    : QKnxNetIpTunnelConnection({ QHostAddress::LocalHost }, 0, QKnxNetIp::TunnelingLayer::Link,
        parent)
{}

QKnxNetIpTunnelConnection::QKnxNetIpTunnelConnection(const QHostAddress &localAddress,
        QObject *parent)
    : QKnxNetIpTunnelConnection(localAddress, 0, QKnxNetIp::TunnelingLayer::Link, parent)
{}

QKnxNetIpTunnelConnection::QKnxNetIpTunnelConnection(const QHostAddress &localAddress,
        quint16 localPort, QObject *parent)
    : QKnxNetIpTunnelConnection(localAddress, localPort, QKnxNetIp::TunnelingLayer::Link, parent)
{}

QKnxNetIpTunnelConnection::QKnxNetIpTunnelConnection(const QHostAddress &localAddress,
        quint16 localPort, QKnxNetIp::TunnelingLayer layer, QObject *parent)
    : QKnxNetIpEndpointConnection(*new QKnxNetIpTunnelConnectionPrivate(localAddress, localPort,
        layer), parent)
{}

QKnxAddress QKnxNetIpTunnelConnection::individualAddress() const
{
    return d_func()->m_address;
}

void QKnxNetIpTunnelConnection::setIndividualAddress(const QKnxAddress &address)
{
    Q_ASSERT_X(false, "QKnxNetIpTunnelClient::setIndividualAddress", "Setting the individual "
        "address used for tunnel connections not implemented yet.");

    Q_UNUSED(address) // TODO: Maybe implement 03_08_04 Tunneling v01.05.03 AS.pdf, paragraph 3.2
}

QKnxNetIp::TunnelingLayer QKnxNetIpTunnelConnection::layer() const
{
    return d_func()->m_layer;
}

void QKnxNetIpTunnelConnection::setTunnelingLayer(QKnxNetIp::TunnelingLayer layer)
{
    if (state() != State::Disconnected)
        return;
    d_func()->m_layer = layer;
}

bool QKnxNetIpTunnelConnection::sendTunnelFrame(const QKnxTunnelFrame &frame)
{
    if (state() != State::Connected)
        return false;

    Q_D(QKnxNetIpTunnelConnection);
    if (d->m_layer == QKnxNetIp::TunnelingLayer::Busmonitor)
        return false; // 03_08_04 Tunneling v01.05.03, paragraph 2.4

    return d->sendTunnelingRequest(frame);
}

QT_END_NAMESPACE