diff --git a/src/utils.h b/src/utils.h --- a/src/utils.h +++ b/src/utils.h @@ -82,6 +82,8 @@ NETWORKMANAGERQT_EXPORT QList > getBFreqs(); NETWORKMANAGERQT_EXPORT QList > getAFreqs(); + + NETWORKMANAGERQT_EXPORT QDateTime clockBootTimeToDateTime(qlonglong clockBootime); } #endif // NETWORKMANAGERQT_UTILS_H diff --git a/src/utils.cpp b/src/utils.cpp --- a/src/utils.cpp +++ b/src/utils.cpp @@ -20,6 +20,7 @@ */ #include "utils.h" +#include "time.h" QHostAddress NetworkManager::ipv6AddressAsHostAddress(const QByteArray &address) { @@ -521,3 +522,34 @@ return freqs; } + +QDateTime NetworkManager::clockBootTimeToDateTime(qlonglong clockBootime) { + + clockid_t clk_id = CLOCK_BOOTTIME; + struct timespec tp; + int r; + + // now is used as a point of reference + // with the timespec that contains the number of msec since boot + QDateTime now = QDateTime::currentDateTime(); + r = clock_gettime (clk_id, &tp); + if (r == -1 && errno == EINVAL) { + clk_id = CLOCK_MONOTONIC; + r = clock_gettime (clk_id, &tp); + } + + // convert to msecs + long now_msecs = tp.tv_sec * 1000 + tp.tv_nsec / 1000000; + + // diff the msecs and construct a QDateTime based on the offset + QDateTime res; + if (clockBootime > now_msecs) { + qlonglong offset = clockBootime - now_msecs; + res = QDateTime::fromMSecsSinceEpoch(now.toMSecsSinceEpoch() + offset); + } else { + qlonglong offset = now_msecs - clockBootime; + res = QDateTime::fromMSecsSinceEpoch(now.toMSecsSinceEpoch() - offset); + } + + return res; +} diff --git a/src/wirelessdevice.h b/src/wirelessdevice.h --- a/src/wirelessdevice.h +++ b/src/wirelessdevice.h @@ -128,6 +128,19 @@ * @return the bitrate in Kbit/s */ int bitRate() const; + /** + * The LastScan property value, converted to QDateTime + * @since 5.62.0 + * @note will always return invalid QDateTime when runtime NM < 1.12 + * @return + */ + QDateTime lastScan() const; + /** + * The time the last RequestScan function was called + * @since 5.62.0 + * @return + */ + QDateTime lastRequestScanTime() const; /** * Retrieves the capabilities of this wifi network. * @@ -210,6 +223,13 @@ * A wireless network disappeared */ void networkDisappeared(const QString &ssid); + /** + * The LastScan property has changed, meaning a scan has just finished + * @since 5.62.0 + * @note will never be emitted when runtime NM < 1.12 + * @see lastScanTime + */ + void lastScanChanged(const QDateTime &dateTime); private: Q_DECLARE_PRIVATE(WirelessDevice) diff --git a/src/wirelessdevice.cpp b/src/wirelessdevice.cpp --- a/src/wirelessdevice.cpp +++ b/src/wirelessdevice.cpp @@ -31,6 +31,7 @@ #include "manager_p.h" #include "nmdebug.h" +#include "utils.h" NetworkManager::WirelessDevicePrivate::WirelessDevicePrivate(const QString &path, WirelessDevice *q) : DevicePrivate(path, q) @@ -94,6 +95,7 @@ QDBusPendingReply<> NetworkManager::WirelessDevice::requestScan(const QVariantMap &options) { Q_D(WirelessDevice); + d->lastRequestScanTime = QDateTime::currentDateTime(); return d->wirelessIface.RequestScan(options); } @@ -127,6 +129,18 @@ return d->bitRate; } +QDateTime NetworkManager::WirelessDevice::lastScan() const +{ + Q_D(const WirelessDevice); + return d->lastScan; +} + +QDateTime NetworkManager::WirelessDevice::lastRequestScanTime() const +{ + Q_D(const WirelessDevice); + return d->lastRequestScanTime; +} + NetworkManager::WirelessDevice::Capabilities NetworkManager::WirelessDevice::wirelessCapabilities() const { Q_D(const WirelessDevice); @@ -233,6 +247,9 @@ } else if (property == QLatin1String("WirelessCapabilities")) { wirelessCapabilities = q->convertCapabilities(value.toUInt()); Q_EMIT q->wirelessCapabilitiesChanged(wirelessCapabilities); + } else if (property == QLatin1String("LastScan")) { + lastScan = NetworkManager::clockBootTimeToDateTime(value.toLongLong()); + Q_EMIT q->lastScanChanged(lastScan); } else if (property == QLatin1String("AccessPoints")) { // TODO use this instead AccessPointAdded/Removed signals? } else { diff --git a/src/wirelessdevice_p.h b/src/wirelessdevice_p.h --- a/src/wirelessdevice_p.h +++ b/src/wirelessdevice_p.h @@ -43,6 +43,8 @@ WirelessDevice::OperationMode mode; uint bitRate; WirelessDevice::Capabilities wirelessCapabilities; + QDateTime lastScan; + QDateTime lastRequestScanTime; Q_DECLARE_PUBLIC(WirelessDevice) protected: