-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknxserverdiscovery.cpp
154 lines (131 loc) · 6.55 KB
/
knxserverdiscovery.cpp
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright 2013 - 2020, nymea GmbH
* Contact: [email protected]
*
* This file is part of nymea.
*
* GNU Lesser General Public License Usage
* This project may be redistributed and/or modified under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; version 3. This project is distributed in the hope that
* it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this project. If not, see <https://www.gnu.org/licenses/>.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "knxserverdiscovery.h"
#include "extern-plugininfo.h"
#include <QNetworkInterface>
#include <QKnxNetIpExtendedDeviceDibProxy>
KnxServerDiscovery::KnxServerDiscovery(QObject *parent) :
QObject(parent)
{
}
QList<QKnxNetIpServerInfo> KnxServerDiscovery::discoveredServers() const
{
return m_discoveredServers;
}
QString KnxServerDiscovery::serviceFamilyToString(QKnx::NetIp::ServiceFamily id)
{
switch (id) {
case QKnxNetIp::ServiceFamily::Core:
return "Core";
case QKnxNetIp::ServiceFamily::DeviceManagement:
return "Device Management";
case QKnxNetIp::ServiceFamily::IpTunneling:
return "Tunnel";
case QKnxNetIp::ServiceFamily::IpRouting:
return "Routing";
case QKnxNetIp::ServiceFamily::RemoteLogging:
return "Remote Logging";
case QKnxNetIp::ServiceFamily::RemoteConfigAndDiagnosis:
return "Remote Configuration";
case QKnxNetIp::ServiceFamily::ObjectServer:
return "Object Server";
case QKnxNetIp::ServiceFamily::Security:
return "Security";
default:
break;
}
return "Unknown";
}
void KnxServerDiscovery::printServerInfo(const QKnxNetIpServerInfo &serverInfo)
{
qCDebug(dcKnx()) << "Server:" << serverInfo.deviceName();
qCDebug(dcKnx()) << " Individual address:" << serverInfo.individualAddress().toString();
qCDebug(dcKnx()) << " Server control endpoint:" << QString("%1:%2").arg(serverInfo.controlEndpointAddress().toString()).arg(serverInfo.controlEndpointPort());
QVector<QKnxServiceInfo> services = serverInfo.supportedServices();
qCDebug(dcKnx()) << " Supported services:";
foreach (const QKnxServiceInfo &service, services)
qCDebug(dcKnx()) << " KNXnet/IP" << serviceFamilyToString(service.ServiceFamily) << ", Version:" << service.ServiceFamilyVersion;
qCDebug(dcKnx()) << " Mask version:" << serverInfo.maskVersion();
qCDebug(dcKnx()) << " Endpoint:" << serverInfo.endpoint();
QKnxNetIpDib dib = serverInfo.hardware();
QKnxNetIpExtendedDeviceDibProxy hardware(dib);
if (hardware.isValid()) {
qCDebug(dcKnx()) << " Extended hardware information:";
qCDebug(dcKnx()).noquote() << QString::fromLatin1(" Mask version: %1").arg(hardware.deviceDescriptorType0(), 4, 16, QLatin1Char('0'));
qCDebug(dcKnx()).noquote() << QString::fromLatin1(" Max. local APDU length: %1").arg(hardware.maximumLocalApduLength());
auto status = serverInfo.mediumStatus();
if (status == QKnx::MediumStatus::CommunicationPossible)
qCDebug(dcKnx()) << " Medium status: Communication possible";
else if (status == QKnx::MediumStatus::CommunicationImpossible)
qCDebug(dcKnx()) << " Medium status: Communication impossible";
else
qCDebug(dcKnx()) << " Medium status: Unknown";
}
}
void KnxServerDiscovery::onDiscoveryAgentErrorOccured(QKnxNetIpServerDiscoveryAgent::Error error)
{
QKnxNetIpServerDiscoveryAgent *discovery = static_cast<QKnxNetIpServerDiscoveryAgent *>(sender());
qCDebug(dcKnx()) << "Discovery error occured" << discovery->localAddress().toString() << error << discovery->errorString();
}
void KnxServerDiscovery::onDiscoveryAgentFinished()
{
QKnxNetIpServerDiscoveryAgent *discovery = static_cast<QKnxNetIpServerDiscoveryAgent *>(sender());
qCDebug(dcKnx()) << "Discovery agent for" << discovery->localAddress() << "has finished";
qCDebug(dcKnx()) << "Found" << discovery->discoveredServers().count() << "servers";
foreach (const QKnxNetIpServerInfo &serverInfo, discovery->discoveredServers()) {
m_discoveredServers.append(serverInfo);
}
m_runningDiscoveryAgents.removeAll(discovery);
discovery->deleteLater();
if (m_runningDiscoveryAgents.isEmpty()) {
emit discoveryFinished();
}
}
bool KnxServerDiscovery::startDisovery()
{
if (!m_runningDiscoveryAgents.isEmpty()) {
qCWarning(dcKnx()) << "Could not start discovery. There are still discovery agents running. (" << m_runningDiscoveryAgents.count() << "discoveries )";
return false;
}
qCDebug(dcKnx()) << "Start KNX server discovery on all interfaces";
m_discoveredServers.clear();
foreach (const QNetworkInterface &interface, QNetworkInterface::allInterfaces()) {
//qCDebug(dcKnx()) << "Checking network interface" << interface.name() << interface.type();
foreach (const QNetworkAddressEntry &addressEntry, interface.addressEntries()) {
// If ipv4 and not local host
if (addressEntry.ip().protocol() == QAbstractSocket::IPv4Protocol && !addressEntry.ip().isLoopback()) {
qCDebug(dcKnx()) << "Start discovery on" << interface.name() << addressEntry.ip().toString();
// Create discovery agent
QKnxNetIpServerDiscoveryAgent *discovery = new QKnxNetIpServerDiscoveryAgent(this);
discovery->setLocalAddress(addressEntry.ip());
discovery->setLocalPort(0);
discovery->setSearchFrequency(60);
discovery->setResponseType(QKnxNetIpServerDiscoveryAgent::ResponseType::Unicast);
discovery->setDiscoveryMode(QKnxNetIpServerDiscoveryAgent::DiscoveryMode::CoreV1 | QKnxNetIpServerDiscoveryAgent::DiscoveryMode::CoreV2);
connect(discovery, &QKnxNetIpServerDiscoveryAgent::finished, this, &KnxServerDiscovery::onDiscoveryAgentFinished);
connect(discovery, &QKnxNetIpServerDiscoveryAgent::errorOccurred, this, &KnxServerDiscovery::onDiscoveryAgentErrorOccured);
m_runningDiscoveryAgents.append(discovery);
// Start the discovery
discovery->start(m_discoveryTimeout);
}
}
}
return true;
}