ExternalSensor: Further refinements
Further refinements, learned after more testing.
Moved the writeHook lambda out of the ExternalSensor constructor,
and into separate initWriteHook() function, to reduce the bloat of
an already extremely large argument list to the constructor,
and solve a nasty gotcha regarding enable_shared_from_this usage:
https://stackoverflow.com/a/63579750/3063879
Adding a few more useful debugging messages, to be printed when
debugging is enabled (set the "debug" constants to true).
Tested: Interrupted external source of data, values changed to NaN
as expected. Resumed external source, values changed from NaN to the
correctly updated values. Timer durations appear correct. Also sent
many configuration change messages, forcing sensor objects to be
reconstructed. No errors noted during processing, even when messages
sent quickly, and also with random delays, and left to run a while.
To check sensor data value:
busctl --no-pager introspect xyz.openbmc_project.ExternalSensor \
/xyz/openbmc_project/sensors/temperature/mysensor \
xyz.openbmc_project.Sensor.Value
To send a configuration change message:
dbus-send --system \
/xyz/openbmc_project/inventory/system/board/myboard/mysensor \
x.x.x.PropertiesChanged \
string:xyz.openbmc_project.Configuration.ExternalSensor
Change-Id: I7bd515fed8ddf391df3fabadab61321a446c1b9f
Signed-off-by: Josh Lehan <krellan@google.com>
diff --git a/src/ExternalSensor.cpp b/src/ExternalSensor.cpp
index 13082e3..3aba626 100644
--- a/src/ExternalSensor.cpp
+++ b/src/ExternalSensor.cpp
@@ -26,9 +26,7 @@
const std::string& sensorName, const std::string& sensorUnits,
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) :
+ double minReading, double timeoutSecs, const PowerState& powerState) :
// TODO(): When the Mutable feature is integrated,
// make sure all ExternalSensor instances are mutable,
// because that is the entire point of ExternalSensor,
@@ -41,8 +39,7 @@
writeTimeout(
std::chrono::duration_cast<std::chrono::steady_clock::duration>(
std::chrono::duration<double>(timeoutSecs))),
- writeAlive(false), writePerishable(timeoutSecs > 0.0),
- writeHook(std::move(writeHookIn))
+ writeAlive(false), writePerishable(timeoutSecs > 0.0)
{
// The caller must specify what physical characteristic
// an external sensor is expected to be measuring, such as temperature,
@@ -75,14 +72,6 @@
objectServer.add_interface(objectPath, association::interface);
setInitialProperties(conn);
- externalSetHook = [weakThis = weak_from_this()]() {
- auto lockThis = weakThis.lock();
- if (lockThis)
- {
- lockThis->externalSetTrigger();
- }
- };
-
if constexpr (debug)
{
std::cerr << "ExternalSensor " << name << " constructed: path "
@@ -95,6 +84,31 @@
}
}
+// Separate function from constructor, because of a gotcha: can't use the
+// enable_shared_from_this() API until after the constructor has completed.
+void ExternalSensor::initWriteHook(
+ std::function<void(std::chrono::steady_clock::time_point now)>&&
+ writeHookIn)
+{
+ // Connect ExternalSensorMain with ExternalSensor
+ writeHook = std::move(writeHookIn);
+
+ // Connect ExternalSensor with Sensor
+ auto weakThis = weak_from_this();
+ externalSetHook = std::move([weakThis]() {
+ auto lockThis = weakThis.lock();
+ if (lockThis)
+ {
+ lockThis->externalSetTrigger();
+ return;
+ }
+ if constexpr (debug)
+ {
+ std::cerr << "ExternalSensor receive ignored, sensor gone\n";
+ }
+ });
+}
+
ExternalSensor::~ExternalSensor()
{
// Make sure the write hook does not reference this object anymore