Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 1 | #include "ExternalSensor.hpp" |
| 2 | |
| 3 | #include "SensorPaths.hpp" |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 4 | #include "Thresholds.hpp" |
| 5 | #include "Utils.hpp" |
| 6 | #include "sensor.hpp" |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 7 | |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 8 | #include <phosphor-logging/lg2.hpp> |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 9 | #include <sdbusplus/asio/connection.hpp> |
| 10 | #include <sdbusplus/asio/object_server.hpp> |
| 11 | |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 12 | #include <chrono> |
Potin Lai | 02c4366 | 2025-05-01 00:03:12 +0800 | [diff] [blame^] | 13 | #include <cmath> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 14 | #include <cstddef> |
| 15 | #include <functional> |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 16 | #include <limits> |
| 17 | #include <memory> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 18 | #include <stdexcept> |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 19 | #include <string> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 20 | #include <utility> |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 21 | #include <vector> |
| 22 | |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 23 | static constexpr bool debug = false; |
| 24 | |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 25 | ExternalSensor::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 Lin | 7b7a9de | 2021-02-22 11:16:27 +0800 | [diff] [blame] | 29 | std::vector<thresholds::Threshold>&& thresholdsIn, |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 30 | const std::string& sensorConfiguration, double maxReading, |
Josh Lehan | 0362738 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 31 | double minReading, double timeoutSecs, const PowerState& powerState) : |
Zhikui Ren | da98f09 | 2021-11-01 09:41:08 -0700 | [diff] [blame] | 32 | Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration, |
| 33 | objectType, true, true, maxReading, minReading, conn, powerState), |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 34 | objServer(objectServer), writeLast(std::chrono::steady_clock::now()), |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 35 | writeTimeout( |
| 36 | std::chrono::duration_cast<std::chrono::steady_clock::duration>( |
| 37 | std::chrono::duration<double>(timeoutSecs))), |
Ed Tanous | b429f31 | 2022-06-27 16:09:53 -0700 | [diff] [blame] | 38 | writePerishable(timeoutSecs > 0.0) |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 39 | { |
| 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 Tanous | 6cb732a | 2021-02-18 15:33:51 -0800 | [diff] [blame] | 43 | std::string dbusPath = sensor_paths::getPathForUnits(sensorUnits); |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 44 | if (dbusPath.empty()) |
| 45 | { |
| 46 | throw std::runtime_error("Units not in allow list"); |
| 47 | } |
Ed Tanous | 6cb732a | 2021-02-18 15:33:51 -0800 | [diff] [blame] | 48 | std::string objectPath = "/xyz/openbmc_project/sensors/"; |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 49 | objectPath += dbusPath; |
| 50 | objectPath += '/'; |
Potin Lai | db26db2 | 2025-02-11 17:00:47 +0800 | [diff] [blame] | 51 | objectPath += name; |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 52 | |
| 53 | sensorInterface = objectServer.add_interface( |
| 54 | objectPath, "xyz.openbmc_project.Sensor.Value"); |
| 55 | |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 56 | for (const auto& threshold : thresholds) |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 57 | { |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 58 | std::string interface = thresholds::getInterface(threshold.level); |
| 59 | thresholdInterfaces[static_cast<size_t>(threshold.level)] = |
| 60 | objectServer.add_interface(objectPath, interface); |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 63 | association = |
| 64 | objectServer.add_interface(objectPath, association::interface); |
Andrei Kartashev | 3928741 | 2022-02-04 16:04:47 +0300 | [diff] [blame] | 65 | setInitialProperties(sensorUnits); |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 66 | |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 67 | if constexpr (debug) |
| 68 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 69 | 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 Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 76 | } |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Josh Lehan | 0362738 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 79 | // Separate function from constructor, because of a gotcha: can't use the |
| 80 | // enable_shared_from_this() API until after the constructor has completed. |
| 81 | void 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 Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 90 | externalSetHook = [weakThis]() { |
Josh Lehan | 0362738 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 91 | auto lockThis = weakThis.lock(); |
| 92 | if (lockThis) |
| 93 | { |
| 94 | lockThis->externalSetTrigger(); |
| 95 | return; |
| 96 | } |
| 97 | if constexpr (debug) |
| 98 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 99 | lg2::error("ExternalSensor receive ignored, sensor gone"); |
Josh Lehan | 0362738 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 100 | } |
Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 101 | }; |
Josh Lehan | 0362738 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 104 | ExternalSensor::~ExternalSensor() |
| 105 | { |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 106 | // Make sure the write hook does not reference this object anymore |
| 107 | externalSetHook = nullptr; |
| 108 | |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 109 | objServer.remove_interface(association); |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 110 | for (const auto& iface : thresholdInterfaces) |
| 111 | { |
| 112 | objServer.remove_interface(iface); |
| 113 | } |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 114 | objServer.remove_interface(sensorInterface); |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 115 | |
| 116 | if constexpr (debug) |
| 117 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 118 | lg2::error("ExternalSensor '{NAME}' destructed", "NAME", name); |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 119 | } |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 122 | void ExternalSensor::checkThresholds() |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 123 | { |
| 124 | thresholds::checkThresholds(this); |
| 125 | } |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 126 | |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 127 | bool ExternalSensor::isAliveAndPerishable() const |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 128 | { |
| 129 | return (writeAlive && writePerishable); |
| 130 | } |
| 131 | |
| 132 | bool 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 Jeffery | 92b9629 | 2021-05-27 16:41:31 +0930 | [diff] [blame] | 142 | // NOLINTNEXTLINE |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 143 | return (ageElapsed(now) < writeTimeout); |
| 144 | } |
| 145 | |
Patrick Williams | 556e04b | 2025-02-01 08:22:22 -0500 | [diff] [blame] | 146 | void ExternalSensor::writeBegin( |
| 147 | const std::chrono::steady_clock::time_point& now) |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 148 | { |
| 149 | if (!writeAlive) |
| 150 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 151 | lg2::error( |
| 152 | "ExternalSensor '{NAME}' online, receiving first value '{VALUE}'", |
| 153 | "NAME", name, "VALUE", value); |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | writeLast = now; |
| 157 | writeAlive = true; |
| 158 | } |
| 159 | |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 160 | void ExternalSensor::writeInvalidate() |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 161 | { |
| 162 | writeAlive = false; |
| 163 | |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 164 | lg2::error("ExternalSensor '{NAME}' offline, timed out", "NAME", name); |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 165 | |
| 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 | |
| 177 | std::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 | |
| 184 | std::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 Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 191 | void ExternalSensor::externalSetTrigger() |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 192 | { |
| 193 | if constexpr (debug) |
| 194 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 195 | lg2::error("ExternalSensor '{NAME}' received '{VALUE}'", "NAME", name, |
| 196 | "VALUE", value); |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Potin Lai | 02c4366 | 2025-05-01 00:03:12 +0800 | [diff] [blame^] | 199 | if (std::isfinite(value)) |
| 200 | { |
| 201 | markAvailable(true); |
| 202 | } |
| 203 | |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 204 | 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 | } |