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