diff options
Diffstat (limited to 'examples/knx/discoverer/main.cpp')
-rw-r--r-- | examples/knx/discoverer/main.cpp | 35 |
1 files changed, 11 insertions, 24 deletions
diff --git a/examples/knx/discoverer/main.cpp b/examples/knx/discoverer/main.cpp index 0e539bc..a2c292b 100644 --- a/examples/knx/discoverer/main.cpp +++ b/examples/knx/discoverer/main.cpp @@ -56,8 +56,6 @@ #include <QtKnx/QKnxNetIpServerDiscoveryAgent> #include <QtKnx/QKnxNetIpSrpBuilder> -#include <QtNetwork/QNetworkInterface> - static QString familyToString(QKnxNetIp::ServiceFamily id) { switch (id) { @@ -83,17 +81,6 @@ static QString familyToString(QKnxNetIp::ServiceFamily id) return "Unknown"; } -QString interfaceFromAddress(const QHostAddress &address) -{ - auto interfaces = QNetworkInterface::allInterfaces(); - for (const auto &interface : qAsConst(interfaces)) { - if (interface.allAddresses().contains(address)) - return interface.humanReadableName(); - } - return QString(address == QHostAddress(QKnxNetIp::Constants::MulticastAddress) - ? "Multicast" : "Unknown"); -} - int main(int argc, char *argv[]) { QCoreApplication discoverer(argc, argv); @@ -130,10 +117,8 @@ int main(int argc, char *argv[]) agent.setLocalPort(parser.value("localPort").toUInt()); agent.setTimeout(parser.value("timeout").toInt() * 1000); - if (parser.isSet("localAddress")) { + if (parser.isSet("localAddress")) agent.setLocalAddress(QHostAddress(parser.value("localAddress"))); - agent.setResponseType(QKnxNetIpServerDiscoveryAgent::ResponseType::Unicast); - } if (parser.isSet("unicast")) agent.setResponseType(QKnxNetIpServerDiscoveryAgent::ResponseType::Unicast); @@ -189,15 +174,17 @@ int main(int argc, char *argv[]) QObject::connect(&agent, &QKnxNetIpServerDiscoveryAgent::finished, &discoverer, &QCoreApplication::quit); - agent.start(); - - if (agent.error() == QKnxNetIpServerDiscoveryAgent::Error::None) - discoverer.exec(); + if (!parser.isSet("localAddress")) { + agent.start(QKnxNetIpServerDiscoveryAgent::InterfaceType::Ethernet + | QKnxNetIpServerDiscoveryAgent::InterfaceType::Wifi); + } else { + agent.start(QVector<QHostAddress> { agent.localAddress() }); + } - qInfo().noquote() << endl << "Device used to send the search request:"; - qInfo().noquote() << QString::fromLatin1(" Network interface: %1, address: %2, port: %3") - .arg(interfaceFromAddress(agent.localAddress()), agent.localAddress().toString()) - .arg(agent.localPort()); + if (agent.error() == QKnxNetIpServerDiscoveryAgent::Error::None + && agent.state() == QKnxNetIpServerDiscoveryAgent::State::Running) { + discoverer.exec(); + } const auto servers = agent.discoveredServers(); if (servers.size() > 0) { |