diff --git a/networkdetector/devicemonitor.cpp b/networkdetector/devicemonitor.cpp index 1ae8d850..7c0eaed0 100644 --- a/networkdetector/devicemonitor.cpp +++ b/networkdetector/devicemonitor.cpp @@ -2,6 +2,8 @@ #include "extern-plugininfo.h" +#include + DeviceMonitor::DeviceMonitor(const QString &macAddress, const QString &ipAddress, QObject *parent): QObject(parent) { @@ -36,13 +38,27 @@ void DeviceMonitor::lookupArpCache() void DeviceMonitor::ping() { // qCDebug(dcNetworkDetector()) << "Running:" << "ping" << "-c" << "1" << m_host->address(); - m_pingProcess->start("ping", {"-c", "1", m_host->address()}); + QNetworkInterface targetInterface; + foreach (const QNetworkInterface &interface, QNetworkInterface::allInterfaces()) { + foreach (const QNetworkAddressEntry &addressEntry, interface.addressEntries()) { + QHostAddress clientAddress(m_host->address()); + if (clientAddress.isInSubnet(addressEntry.ip(), addressEntry.prefixLength())) { + targetInterface = interface; + break; + } + } + } + if (!targetInterface.isValid()) { + qCWarning(dcNetworkDetector()) << "Could not find a suitable interface to ping for" << m_host->address(); + return; + } + m_pingProcess->start("arping", {"-I", targetInterface.name(), "-f", "-w", "5", m_host->address()}); } void DeviceMonitor::arpLookupFinished(int exitCode) { if (exitCode != 0) { - qWarning(dcNetworkDetector()) << "Error looking up ARP cache."; + qCWarning(dcNetworkDetector()) << "Error looking up ARP cache."; return; } @@ -60,7 +76,7 @@ void DeviceMonitor::arpLookupFinished(int exitCode) emit addressChanged(parts.first()); } if (parts.last() == "REACHABLE") { - qDebug(dcNetworkDetector()) << "Device" << m_host->macAddress() << "found in ARP cache and claims to be REACHABLE"; + qCDebug(dcNetworkDetector()) << "Device" << m_host->macAddress() << "found in ARP cache and claims to be REACHABLE"; m_host->seen(); if (!m_host->reachable()) { m_host->setReachable(true); @@ -80,8 +96,8 @@ void DeviceMonitor::arpLookupFinished(int exitCode) ping(); } - if (m_host->reachable() && m_host->lastSeenTime().addSecs(300) < QDateTime::currentDateTime()) { - qCDebug(dcNetworkDetector()) << "Could not reach device for > 5 mins. Marking it as gone." << m_host->address() << m_host->macAddress(); + if (m_host->reachable() && m_host->lastSeenTime().addSecs(20) < QDateTime::currentDateTime()) { + qCDebug(dcNetworkDetector()) << "Could not reach device for 20 seconds. Marking it as gone." << m_host->address() << m_host->macAddress(); m_host->setReachable(false); emit reachableChanged(false); } @@ -91,6 +107,7 @@ void DeviceMonitor::pingFinished(int exitCode) { if (exitCode == 0) { // we were able to ping the device + qCDebug(dcNetworkDetector()) << "Ping successful for" << m_host->macAddress() << m_host->address(); m_host->seen(); if (!m_host->reachable()) { m_host->setReachable(true); @@ -98,7 +115,7 @@ void DeviceMonitor::pingFinished(int exitCode) } emit seen(); } else { - qDebug(dcNetworkDetector()) << "Could not ping device" << m_host->macAddress() << m_host->address(); + qCDebug(dcNetworkDetector()) << "Could not ping device" << m_host->macAddress() << m_host->address(); } // read data to discard it from socket QString data = QString::fromLatin1(m_pingProcess->readAll()); diff --git a/networkdetector/devicepluginnetworkdetector.cpp b/networkdetector/devicepluginnetworkdetector.cpp index 4cada1ce..7556e7b0 100644 --- a/networkdetector/devicepluginnetworkdetector.cpp +++ b/networkdetector/devicepluginnetworkdetector.cpp @@ -142,9 +142,8 @@ void DevicePluginNetworkDetector::deviceReachableChanged(bool reachable) { DeviceMonitor *monitor = static_cast(sender()); Device *device = m_monitors.value(monitor); - if (device->stateValue(networkDeviceConnectedStateTypeId).toBool() != reachable) { - qCDebug(dcNetworkDetector()) << "Device" << device->paramValue(networkDeviceDeviceMacAddressParamTypeId).toString() << "reachable changed" << reachable; - device->setStateValue(networkDeviceConnectedStateTypeId, reachable); + if (device->stateValue(networkDeviceIsPresentStateTypeId).toBool() != reachable) { + qCDebug(dcNetworkDetector()) << "Device" << device->name() << device->paramValue(networkDeviceDeviceMacAddressParamTypeId).toString() << "reachable changed" << reachable; device->setStateValue(networkDeviceIsPresentStateTypeId, reachable); } } @@ -154,6 +153,7 @@ void DevicePluginNetworkDetector::deviceAddressChanged(const QString &address) DeviceMonitor *monitor = static_cast(sender()); Device *device = m_monitors.value(monitor); if (device->paramValue(networkDeviceDeviceAddressParamTypeId).toString() != address) { + qCDebug(dcNetworkDetector()) << "Device" << device->name() << device->paramValue(networkDeviceDeviceMacAddressParamTypeId).toString() << "changed IP address to" << address; device->setParamValue(networkDeviceDeviceAddressParamTypeId.toString(), address); } } diff --git a/networkdetector/devicepluginnetworkdetector.json b/networkdetector/devicepluginnetworkdetector.json index a8d303af..4768567a 100644 --- a/networkdetector/devicepluginnetworkdetector.json +++ b/networkdetector/devicepluginnetworkdetector.json @@ -17,7 +17,7 @@ "Device", "Sensor" ], - "interfaces": [ "connectable", "presencesensor" ], + "interfaces": [ "presencesensor" ], "primaryStateTypeId": "cb43e1b5-4f61-4538-bfa2-c33055c542cf", "createMethods": ["user", "discovery"], "paramTypes": [ @@ -39,15 +39,6 @@ "stateTypes": [ { "id": "cb43e1b5-4f61-4538-bfa2-c33055c542cf", - "name": "connected", - "displayName": "Device in network", - "displayNameEvent": "Device in network changed", - "type": "bool", - "defaultValue": false, - "cached": false - }, - { - "id": "0a38651c-418d-42b3-80f5-6e7adb6a25fc", "name": "isPresent", "displayName": "Device is present", "displayNameEvent": "Device is present changed",