Add timer expiration feature to ExternalSensor
ExternalSensor now functions as intended, wholly within dbus-sensors,
without requiring any modification to the IPMI or Redfish servers,
to provide the feature of timeout expiration of external data, so that
stale/lost external connections can be properly indicated as such.
A "Timeout" parameter is added, in decimal seconds, providing a
watchdog for the arrival of external data. The expectation is that the
external source will provide D-Bus updates, to the sensor Value
property, at regular intervals, repeating indefinitely.
If this external source stops doing this, the watchdog barks, and
the Value of this Sensor will become set to "NaN". This provides
an indication to consumers of this Sensor, to realize that the Value
of this sensor has became stale/disconnected.
A practical application of this is fan control. Upon loss of external
temperature notification, the fans could be thrown into failsafe
mode, instead of risking the system overheating by wrongly continuing
to believe an old temperature value that has become stale.
Tested: Works for me. I started an external data source, data arrived
into the Value of the sensor. I stopped that external data source,
after the Timeout period, the Value became "NaN". I started the
external source again, the Value became correct again, as soon as
external data started to arrive again. I repeated this stop and start
procedure a few times, verifying that it operated as intended.
Signed-off-by: Josh Lehan <krellan@google.com>
Change-Id: I53b9ff4c0aa771aff4aaf3449fcab23c07afa296
diff --git a/include/ExternalSensor.hpp b/include/ExternalSensor.hpp
index fd36bb0..02110fe 100644
--- a/include/ExternalSensor.hpp
+++ b/include/ExternalSensor.hpp
@@ -5,6 +5,7 @@
#include <sdbusplus/asio/object_server.hpp>
+#include <chrono>
#include <string>
#include <vector>
@@ -13,19 +14,49 @@
public std::enable_shared_from_this<ExternalSensor>
{
public:
- ExternalSensor(const std::string& objectType,
- sdbusplus::asio::object_server& objectServer,
- std::shared_ptr<sdbusplus::asio::connection>& conn,
- const std::string& sensorName,
- const std::string& sensorMeasure,
- std::vector<thresholds::Threshold>&& thresholds,
- const std::string& sensorConfiguration,
- const double& maxReading, const double& minReading,
- const PowerState& powerState);
+ ExternalSensor(
+ const std::string& objectType,
+ sdbusplus::asio::object_server& objectServer,
+ std::shared_ptr<sdbusplus::asio::connection>& conn,
+ const std::string& sensorName, const std::string& sensorMeasure,
+ std::vector<thresholds::Threshold>&& thresholdsIn,
+ const std::string& sensorConfiguration, double maxReading,
+ double minReading, double timeoutSecs, const PowerState& powerState,
+ std::function<void(std::chrono::steady_clock::time_point now)>&&
+ writeHookIn);
virtual ~ExternalSensor();
+ // Returns true if sensor has external Value that is subject to timeout
+ bool isAliveAndPerishable(void) const;
+
+ // Returns true if AliveAndPerishable and timeout has not yet happened
+ bool
+ isAliveAndFresh(const std::chrono::steady_clock::time_point& now) const;
+
+ // Marks the time when Value successfully received from external source
+ void writeBegin(const std::chrono::steady_clock::time_point& now);
+
+ // Marks sensor as timed out, replacing Value with floating-point "NaN"
+ void writeInvalidate(void);
+
+ // Returns amount of time elapsed since last writeBegin() happened
+ std::chrono::steady_clock::duration
+ ageElapsed(const std::chrono::steady_clock::time_point& now) const;
+
+ // Returns amount of time remaining until sensor timeout will happen
+ std::chrono::steady_clock::duration
+ ageRemaining(const std::chrono::steady_clock::time_point& now) const;
+
private:
sdbusplus::asio::object_server& objServer;
+ std::chrono::steady_clock::time_point writeLast;
+ std::chrono::steady_clock::duration writeTimeout;
+ bool writeAlive;
+ bool writePerishable;
+ std::function<void(const std::chrono::steady_clock::time_point& now)>
+ writeHook;
+
void checkThresholds(void) override;
+ void externalSetTrigger(void);
};