blob: 51a439aaa83c10f9886a5bad0644b9673f5da7cb [file] [log] [blame]
Josh Lehan2a40e932020-09-02 11:48:14 -07001#include "ExternalSensor.hpp"
2
3#include "SensorPaths.hpp"
Ed Tanouseacbfdd2024-04-04 12:00:24 -07004#include "Thresholds.hpp"
5#include "Utils.hpp"
6#include "sensor.hpp"
Josh Lehan2a40e932020-09-02 11:48:14 -07007
George Liud630b3a2025-02-20 11:02:49 +08008#include <phosphor-logging/lg2.hpp>
Josh Lehan2a40e932020-09-02 11:48:14 -07009#include <sdbusplus/asio/connection.hpp>
10#include <sdbusplus/asio/object_server.hpp>
11
Josh Lehan72432172021-03-17 13:35:43 -070012#include <chrono>
Potin Lai02c43662025-05-01 00:03:12 +080013#include <cmath>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070014#include <cstddef>
15#include <functional>
Josh Lehan2a40e932020-09-02 11:48:14 -070016#include <limits>
17#include <memory>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070018#include <stdexcept>
Josh Lehan2a40e932020-09-02 11:48:14 -070019#include <string>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070020#include <utility>
Josh Lehan2a40e932020-09-02 11:48:14 -070021#include <vector>
22
Josh Lehan72432172021-03-17 13:35:43 -070023static constexpr bool debug = false;
24
Josh Lehan2a40e932020-09-02 11:48:14 -070025ExternalSensor::ExternalSensor(
26 const std::string& objectType, sdbusplus::asio::object_server& objectServer,
27 std::shared_ptr<sdbusplus::asio::connection>& conn,
28 const std::string& sensorName, const std::string& sensorUnits,
Jeff Lin7b7a9de2021-02-22 11:16:27 +080029 std::vector<thresholds::Threshold>&& thresholdsIn,
Josh Lehan72432172021-03-17 13:35:43 -070030 const std::string& sensorConfiguration, double maxReading,
Josh Lehan03627382021-03-17 13:35:43 -070031 double minReading, double timeoutSecs, const PowerState& powerState) :
Zhikui Renda98f092021-11-01 09:41:08 -070032 Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration,
33 objectType, true, true, maxReading, minReading, conn, powerState),
Ed Tanous2049bd22022-07-09 07:20:26 -070034 objServer(objectServer), writeLast(std::chrono::steady_clock::now()),
Josh Lehan72432172021-03-17 13:35:43 -070035 writeTimeout(
36 std::chrono::duration_cast<std::chrono::steady_clock::duration>(
37 std::chrono::duration<double>(timeoutSecs))),
Ed Tanousb429f312022-06-27 16:09:53 -070038 writePerishable(timeoutSecs > 0.0)
Josh Lehan2a40e932020-09-02 11:48:14 -070039{
40 // The caller must specify what physical characteristic
41 // an external sensor is expected to be measuring, such as temperature,
42 // as, unlike others, this is not implied by device type name.
Ed Tanous6cb732a2021-02-18 15:33:51 -080043 std::string dbusPath = sensor_paths::getPathForUnits(sensorUnits);
Josh Lehan2a40e932020-09-02 11:48:14 -070044 if (dbusPath.empty())
45 {
46 throw std::runtime_error("Units not in allow list");
47 }
Ed Tanous6cb732a2021-02-18 15:33:51 -080048 std::string objectPath = "/xyz/openbmc_project/sensors/";
Josh Lehan2a40e932020-09-02 11:48:14 -070049 objectPath += dbusPath;
50 objectPath += '/';
Potin Laidb26db22025-02-11 17:00:47 +080051 objectPath += name;
Josh Lehan2a40e932020-09-02 11:48:14 -070052
53 sensorInterface = objectServer.add_interface(
54 objectPath, "xyz.openbmc_project.Sensor.Value");
55
Jayashree Dhanapal56678082022-01-04 17:27:20 +053056 for (const auto& threshold : thresholds)
Josh Lehan2a40e932020-09-02 11:48:14 -070057 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053058 std::string interface = thresholds::getInterface(threshold.level);
59 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
60 objectServer.add_interface(objectPath, interface);
Josh Lehan2a40e932020-09-02 11:48:14 -070061 }
62
Patrick Williams2aaf7172024-08-16 15:20:40 -040063 association =
64 objectServer.add_interface(objectPath, association::interface);
Andrei Kartashev39287412022-02-04 16:04:47 +030065 setInitialProperties(sensorUnits);
Josh Lehan72432172021-03-17 13:35:43 -070066
Josh Lehan72432172021-03-17 13:35:43 -070067 if constexpr (debug)
68 {
George Liud630b3a2025-02-20 11:02:49 +080069 lg2::error(
70 "ExternalSensor '{NAME}' constructed: path '{PATH}', type '{TYPE}', "
71 "min '{MIN}', max '{MAX}', timeout '{TIMEOUT}' us",
72 "NAME", name, "PATH", objectPath, "TYPE", objectType, "MIN",
73 minReading, "MAX", maxReading, "TIMEOUT",
74 std::chrono::duration_cast<std::chrono::microseconds>(writeTimeout)
75 .count());
Josh Lehan72432172021-03-17 13:35:43 -070076 }
Josh Lehan2a40e932020-09-02 11:48:14 -070077}
78
Josh Lehan03627382021-03-17 13:35:43 -070079// Separate function from constructor, because of a gotcha: can't use the
80// enable_shared_from_this() API until after the constructor has completed.
81void ExternalSensor::initWriteHook(
82 std::function<void(std::chrono::steady_clock::time_point now)>&&
83 writeHookIn)
84{
85 // Connect ExternalSensorMain with ExternalSensor
86 writeHook = std::move(writeHookIn);
87
88 // Connect ExternalSensor with Sensor
89 auto weakThis = weak_from_this();
Ed Tanous8a17c302021-09-02 15:07:11 -070090 externalSetHook = [weakThis]() {
Josh Lehan03627382021-03-17 13:35:43 -070091 auto lockThis = weakThis.lock();
92 if (lockThis)
93 {
94 lockThis->externalSetTrigger();
95 return;
96 }
97 if constexpr (debug)
98 {
George Liud630b3a2025-02-20 11:02:49 +080099 lg2::error("ExternalSensor receive ignored, sensor gone");
Josh Lehan03627382021-03-17 13:35:43 -0700100 }
Ed Tanous8a17c302021-09-02 15:07:11 -0700101 };
Josh Lehan03627382021-03-17 13:35:43 -0700102}
103
Josh Lehan2a40e932020-09-02 11:48:14 -0700104ExternalSensor::~ExternalSensor()
105{
Josh Lehan72432172021-03-17 13:35:43 -0700106 // Make sure the write hook does not reference this object anymore
107 externalSetHook = nullptr;
108
Josh Lehan2a40e932020-09-02 11:48:14 -0700109 objServer.remove_interface(association);
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530110 for (const auto& iface : thresholdInterfaces)
111 {
112 objServer.remove_interface(iface);
113 }
Josh Lehan2a40e932020-09-02 11:48:14 -0700114 objServer.remove_interface(sensorInterface);
Josh Lehan72432172021-03-17 13:35:43 -0700115
116 if constexpr (debug)
117 {
George Liud630b3a2025-02-20 11:02:49 +0800118 lg2::error("ExternalSensor '{NAME}' destructed", "NAME", name);
Josh Lehan72432172021-03-17 13:35:43 -0700119 }
Josh Lehan2a40e932020-09-02 11:48:14 -0700120}
121
Ed Tanous201a1012024-04-03 18:07:28 -0700122void ExternalSensor::checkThresholds()
Josh Lehan2a40e932020-09-02 11:48:14 -0700123{
124 thresholds::checkThresholds(this);
125}
Josh Lehan72432172021-03-17 13:35:43 -0700126
Ed Tanous201a1012024-04-03 18:07:28 -0700127bool ExternalSensor::isAliveAndPerishable() const
Josh Lehan72432172021-03-17 13:35:43 -0700128{
129 return (writeAlive && writePerishable);
130}
131
132bool ExternalSensor::isAliveAndFresh(
133 const std::chrono::steady_clock::time_point& now) const
134{
135 // Must be alive and perishable, to have possibility of being fresh
136 if (!isAliveAndPerishable())
137 {
138 return false;
139 }
140
141 // If age, as of now, is less than timeout, it is deemed fresh
Andrew Jeffery92b96292021-05-27 16:41:31 +0930142 // NOLINTNEXTLINE
Josh Lehan72432172021-03-17 13:35:43 -0700143 return (ageElapsed(now) < writeTimeout);
144}
145
Patrick Williams556e04b2025-02-01 08:22:22 -0500146void ExternalSensor::writeBegin(
147 const std::chrono::steady_clock::time_point& now)
Josh Lehan72432172021-03-17 13:35:43 -0700148{
149 if (!writeAlive)
150 {
George Liud630b3a2025-02-20 11:02:49 +0800151 lg2::error(
152 "ExternalSensor '{NAME}' online, receiving first value '{VALUE}'",
153 "NAME", name, "VALUE", value);
Josh Lehan72432172021-03-17 13:35:43 -0700154 }
155
156 writeLast = now;
157 writeAlive = true;
158}
159
Ed Tanous201a1012024-04-03 18:07:28 -0700160void ExternalSensor::writeInvalidate()
Josh Lehan72432172021-03-17 13:35:43 -0700161{
162 writeAlive = false;
163
George Liud630b3a2025-02-20 11:02:49 +0800164 lg2::error("ExternalSensor '{NAME}' offline, timed out", "NAME", name);
Josh Lehan72432172021-03-17 13:35:43 -0700165
166 // Take back control of this sensor from the external override,
167 // as the external source has timed out.
168 // This allows sensor::updateValue() to work normally,
169 // as it would do for internal sensors with values from hardware.
170 overriddenState = false;
171
172 // Invalidate the existing Value, similar to what internal sensors do,
173 // when they encounter errors trying to read from hardware.
174 updateValue(std::numeric_limits<double>::quiet_NaN());
175}
176
177std::chrono::steady_clock::duration ExternalSensor::ageElapsed(
178 const std::chrono::steady_clock::time_point& now) const
179{
180 // Comparing 2 time_point will return duration
181 return (now - writeLast);
182}
183
184std::chrono::steady_clock::duration ExternalSensor::ageRemaining(
185 const std::chrono::steady_clock::time_point& now) const
186{
187 // Comparing duration will return another duration
188 return (writeTimeout - ageElapsed(now));
189}
190
Ed Tanous201a1012024-04-03 18:07:28 -0700191void ExternalSensor::externalSetTrigger()
Josh Lehan72432172021-03-17 13:35:43 -0700192{
193 if constexpr (debug)
194 {
George Liud630b3a2025-02-20 11:02:49 +0800195 lg2::error("ExternalSensor '{NAME}' received '{VALUE}'", "NAME", name,
196 "VALUE", value);
Josh Lehan72432172021-03-17 13:35:43 -0700197 }
198
Potin Lai02c43662025-05-01 00:03:12 +0800199 if (std::isfinite(value))
200 {
201 markAvailable(true);
202 }
203
Josh Lehan72432172021-03-17 13:35:43 -0700204 auto now = std::chrono::steady_clock::now();
205
206 writeBegin(now);
207
208 // Tell the owner to recalculate the expiration timer
209 writeHook(now);
210}