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 | |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 8 | #include <sdbusplus/asio/connection.hpp> |
| 9 | #include <sdbusplus/asio/object_server.hpp> |
| 10 | |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 11 | #include <chrono> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 12 | #include <cstddef> |
| 13 | #include <functional> |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 14 | #include <iostream> |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 15 | #include <limits> |
| 16 | #include <memory> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 17 | #include <stdexcept> |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 18 | #include <string> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 19 | #include <utility> |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 22 | static constexpr bool debug = false; |
| 23 | |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 24 | ExternalSensor::ExternalSensor( |
| 25 | const std::string& objectType, sdbusplus::asio::object_server& objectServer, |
| 26 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 27 | const std::string& sensorName, const std::string& sensorUnits, |
Jeff Lin | 7b7a9de | 2021-02-22 11:16:27 +0800 | [diff] [blame] | 28 | std::vector<thresholds::Threshold>&& thresholdsIn, |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 29 | const std::string& sensorConfiguration, double maxReading, |
Josh Lehan | 0362738 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 30 | double minReading, double timeoutSecs, const PowerState& powerState) : |
Zhikui Ren | da98f09 | 2021-11-01 09:41:08 -0700 | [diff] [blame] | 31 | Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration, |
| 32 | objectType, true, true, maxReading, minReading, conn, powerState), |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 33 | objServer(objectServer), writeLast(std::chrono::steady_clock::now()), |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 34 | writeTimeout( |
| 35 | std::chrono::duration_cast<std::chrono::steady_clock::duration>( |
| 36 | std::chrono::duration<double>(timeoutSecs))), |
Ed Tanous | b429f31 | 2022-06-27 16:09:53 -0700 | [diff] [blame] | 37 | writePerishable(timeoutSecs > 0.0) |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 38 | { |
| 39 | // The caller must specify what physical characteristic |
| 40 | // an external sensor is expected to be measuring, such as temperature, |
| 41 | // as, unlike others, this is not implied by device type name. |
Ed Tanous | 6cb732a | 2021-02-18 15:33:51 -0800 | [diff] [blame] | 42 | std::string dbusPath = sensor_paths::getPathForUnits(sensorUnits); |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 43 | if (dbusPath.empty()) |
| 44 | { |
| 45 | throw std::runtime_error("Units not in allow list"); |
| 46 | } |
Ed Tanous | 6cb732a | 2021-02-18 15:33:51 -0800 | [diff] [blame] | 47 | std::string objectPath = "/xyz/openbmc_project/sensors/"; |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 48 | objectPath += dbusPath; |
| 49 | objectPath += '/'; |
| 50 | objectPath += sensorName; |
| 51 | |
| 52 | sensorInterface = objectServer.add_interface( |
| 53 | objectPath, "xyz.openbmc_project.Sensor.Value"); |
| 54 | |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 55 | for (const auto& threshold : thresholds) |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 56 | { |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 57 | std::string interface = thresholds::getInterface(threshold.level); |
| 58 | thresholdInterfaces[static_cast<size_t>(threshold.level)] = |
| 59 | objectServer.add_interface(objectPath, interface); |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 62 | association = |
| 63 | objectServer.add_interface(objectPath, association::interface); |
Andrei Kartashev | 3928741 | 2022-02-04 16:04:47 +0300 | [diff] [blame] | 64 | setInitialProperties(sensorUnits); |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 65 | |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 66 | if constexpr (debug) |
| 67 | { |
| 68 | std::cerr << "ExternalSensor " << name << " constructed: path " |
| 69 | << configurationPath << ", type " << objectType << ", min " |
| 70 | << minReading << ", max " << maxReading << ", timeout " |
| 71 | << std::chrono::duration_cast<std::chrono::microseconds>( |
| 72 | writeTimeout) |
| 73 | .count() |
| 74 | << " us\n"; |
| 75 | } |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Josh Lehan | 0362738 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 78 | // Separate function from constructor, because of a gotcha: can't use the |
| 79 | // enable_shared_from_this() API until after the constructor has completed. |
| 80 | void ExternalSensor::initWriteHook( |
| 81 | std::function<void(std::chrono::steady_clock::time_point now)>&& |
| 82 | writeHookIn) |
| 83 | { |
| 84 | // Connect ExternalSensorMain with ExternalSensor |
| 85 | writeHook = std::move(writeHookIn); |
| 86 | |
| 87 | // Connect ExternalSensor with Sensor |
| 88 | auto weakThis = weak_from_this(); |
Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 89 | externalSetHook = [weakThis]() { |
Josh Lehan | 0362738 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 90 | auto lockThis = weakThis.lock(); |
| 91 | if (lockThis) |
| 92 | { |
| 93 | lockThis->externalSetTrigger(); |
| 94 | return; |
| 95 | } |
| 96 | if constexpr (debug) |
| 97 | { |
| 98 | std::cerr << "ExternalSensor receive ignored, sensor gone\n"; |
| 99 | } |
Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 100 | }; |
Josh Lehan | 0362738 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 103 | ExternalSensor::~ExternalSensor() |
| 104 | { |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 105 | // Make sure the write hook does not reference this object anymore |
| 106 | externalSetHook = nullptr; |
| 107 | |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 108 | objServer.remove_interface(association); |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 109 | for (const auto& iface : thresholdInterfaces) |
| 110 | { |
| 111 | objServer.remove_interface(iface); |
| 112 | } |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 113 | objServer.remove_interface(sensorInterface); |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 114 | |
| 115 | if constexpr (debug) |
| 116 | { |
| 117 | std::cerr << "ExternalSensor " << name << " destructed\n"; |
| 118 | } |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 121 | void ExternalSensor::checkThresholds() |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 122 | { |
| 123 | thresholds::checkThresholds(this); |
| 124 | } |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 125 | |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 126 | bool ExternalSensor::isAliveAndPerishable() const |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 127 | { |
| 128 | return (writeAlive && writePerishable); |
| 129 | } |
| 130 | |
| 131 | bool ExternalSensor::isAliveAndFresh( |
| 132 | const std::chrono::steady_clock::time_point& now) const |
| 133 | { |
| 134 | // Must be alive and perishable, to have possibility of being fresh |
| 135 | if (!isAliveAndPerishable()) |
| 136 | { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | // 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] | 141 | // NOLINTNEXTLINE |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 142 | return (ageElapsed(now) < writeTimeout); |
| 143 | } |
| 144 | |
| 145 | void ExternalSensor::writeBegin( |
| 146 | const std::chrono::steady_clock::time_point& now) |
| 147 | { |
| 148 | if (!writeAlive) |
| 149 | { |
| 150 | std::cerr << "ExternalSensor " << name |
| 151 | << " online, receiving first value " << value << "\n"; |
| 152 | } |
| 153 | |
| 154 | writeLast = now; |
| 155 | writeAlive = true; |
| 156 | } |
| 157 | |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 158 | void ExternalSensor::writeInvalidate() |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 159 | { |
| 160 | writeAlive = false; |
| 161 | |
| 162 | std::cerr << "ExternalSensor " << name << " offline, timed out\n"; |
| 163 | |
| 164 | // Take back control of this sensor from the external override, |
| 165 | // as the external source has timed out. |
| 166 | // This allows sensor::updateValue() to work normally, |
| 167 | // as it would do for internal sensors with values from hardware. |
| 168 | overriddenState = false; |
| 169 | |
| 170 | // Invalidate the existing Value, similar to what internal sensors do, |
| 171 | // when they encounter errors trying to read from hardware. |
| 172 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
| 173 | } |
| 174 | |
| 175 | std::chrono::steady_clock::duration ExternalSensor::ageElapsed( |
| 176 | const std::chrono::steady_clock::time_point& now) const |
| 177 | { |
| 178 | // Comparing 2 time_point will return duration |
| 179 | return (now - writeLast); |
| 180 | } |
| 181 | |
| 182 | std::chrono::steady_clock::duration ExternalSensor::ageRemaining( |
| 183 | const std::chrono::steady_clock::time_point& now) const |
| 184 | { |
| 185 | // Comparing duration will return another duration |
| 186 | return (writeTimeout - ageElapsed(now)); |
| 187 | } |
| 188 | |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 189 | void ExternalSensor::externalSetTrigger() |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 190 | { |
| 191 | if constexpr (debug) |
| 192 | { |
| 193 | std::cerr << "ExternalSensor " << name << " received " << value << "\n"; |
| 194 | } |
| 195 | |
| 196 | auto now = std::chrono::steady_clock::now(); |
| 197 | |
| 198 | writeBegin(now); |
| 199 | |
| 200 | // Tell the owner to recalculate the expiration timer |
| 201 | writeHook(now); |
| 202 | } |