Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 1 | #include "ExternalSensor.hpp" |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 2 | #include "Thresholds.hpp" |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 3 | #include "Utils.hpp" |
| 4 | #include "VariantVisitors.hpp" |
| 5 | |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 6 | #include <boost/asio/error.hpp> |
| 7 | #include <boost/asio/io_context.hpp> |
| 8 | #include <boost/asio/post.hpp> |
| 9 | #include <boost/asio/steady_timer.hpp> |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 10 | #include <boost/container/flat_map.hpp> |
| 11 | #include <boost/container/flat_set.hpp> |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 12 | #include <phosphor-logging/lg2.hpp> |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 13 | #include <sdbusplus/asio/connection.hpp> |
| 14 | #include <sdbusplus/asio/object_server.hpp> |
| 15 | #include <sdbusplus/bus/match.hpp> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 16 | #include <sdbusplus/message.hpp> |
| 17 | #include <sdbusplus/message/native_types.hpp> |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 18 | |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 19 | #include <algorithm> |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 20 | #include <array> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 21 | #include <chrono> |
| 22 | #include <cmath> |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 23 | #include <functional> |
| 24 | #include <memory> |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 25 | #include <string> |
| 26 | #include <utility> |
| 27 | #include <variant> |
| 28 | #include <vector> |
| 29 | |
| 30 | // Copied from HwmonTempSensor and inspired by |
| 31 | // https://gerrit.openbmc-project.xyz/c/openbmc/dbus-sensors/+/35476 |
| 32 | |
| 33 | // The ExternalSensor is a sensor whose value is intended to be writable |
| 34 | // by something external to the BMC, so that the host (or something else) |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 35 | // can write to it, perhaps by using an IPMI or Redfish connection. |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 36 | |
| 37 | // Unlike most other sensors, an external sensor does not correspond |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 38 | // to a hwmon file or any other kernel/hardware interface, |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 39 | // so, after initialization, this module does not have much to do, |
| 40 | // but it handles reinitialization and thresholds, similar to the others. |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 41 | // The main work of this module is to provide backing storage for a |
| 42 | // sensor that exists only virtually, and to provide an optional |
| 43 | // timeout service for detecting loss of timely updates. |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 44 | |
| 45 | // As there is no corresponding driver or hardware to support, |
| 46 | // all configuration of this sensor comes from the JSON parameters: |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 47 | // MinValue, MaxValue, Timeout, PowerState, Units, Name |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 48 | |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 49 | // The purpose of "Units" is to specify the physical characteristic |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 50 | // the external sensor is measuring, because with an external sensor |
| 51 | // there is no other way to tell, and it will be used for the object path |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 52 | // here: /xyz/openbmc_project/sensors/<Units>/<Name> |
| 53 | |
| 54 | // For more information, see external-sensor.md design document: |
| 55 | // https://gerrit.openbmc-project.xyz/c/openbmc/docs/+/41452 |
| 56 | // https://github.com/openbmc/docs/tree/master/designs/ |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 57 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 58 | static constexpr bool debug = false; |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 59 | |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 60 | static const char* sensorType = "ExternalSensor"; |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 61 | |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 62 | void updateReaper( |
| 63 | boost::container::flat_map<std::string, std::shared_ptr<ExternalSensor>>& |
| 64 | sensors, |
| 65 | boost::asio::steady_timer& timer, |
| 66 | const std::chrono::steady_clock::time_point& now) |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 67 | { |
| 68 | // First pass, reap all stale sensors |
Zev Weiss | 08cb50c | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 69 | for (const auto& [name, sensor] : sensors) |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 70 | { |
Zev Weiss | 08cb50c | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 71 | if (!sensor) |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 72 | { |
| 73 | continue; |
| 74 | } |
| 75 | |
Zev Weiss | 08cb50c | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 76 | if (!sensor->isAliveAndPerishable()) |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 77 | { |
| 78 | continue; |
| 79 | } |
| 80 | |
Zev Weiss | 08cb50c | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 81 | if (!sensor->isAliveAndFresh(now)) |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 82 | { |
| 83 | // Mark sensor as dead, no longer alive |
Zev Weiss | 08cb50c | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 84 | sensor->writeInvalidate(); |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | std::chrono::steady_clock::duration nextCheck; |
| 89 | bool needCheck = false; |
| 90 | |
| 91 | // Second pass, determine timer interval to next check |
Zev Weiss | 08cb50c | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 92 | for (const auto& [name, sensor] : sensors) |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 93 | { |
Zev Weiss | 08cb50c | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 94 | if (!sensor) |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 95 | { |
| 96 | continue; |
| 97 | } |
| 98 | |
Zev Weiss | 08cb50c | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 99 | if (!sensor->isAliveAndPerishable()) |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 100 | { |
| 101 | continue; |
| 102 | } |
| 103 | |
Zev Weiss | 08cb50c | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 104 | auto expiration = sensor->ageRemaining(now); |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 105 | |
| 106 | if (needCheck) |
| 107 | { |
| 108 | nextCheck = std::min(nextCheck, expiration); |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | // Initialization |
| 113 | nextCheck = expiration; |
| 114 | needCheck = true; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (!needCheck) |
| 119 | { |
| 120 | if constexpr (debug) |
| 121 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 122 | lg2::error("Next ExternalSensor timer idle"); |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | timer.expires_at(now + nextCheck); |
| 129 | |
| 130 | timer.async_wait([&sensors, &timer](const boost::system::error_code& err) { |
| 131 | if (err != boost::system::errc::success) |
| 132 | { |
| 133 | // Cancellation is normal, as timer is dynamically rescheduled |
Josh Lehan | 0362738 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 134 | if (err != boost::asio::error::operation_aborted) |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 135 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 136 | lg2::error( |
| 137 | "ExternalSensor timer scheduling problem: {ERROR_MESSAGE}", |
| 138 | "ERROR_MESSAGE", err.message()); |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 139 | } |
| 140 | return; |
| 141 | } |
Josh Lehan | 0362738 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 142 | |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 143 | updateReaper(sensors, timer, std::chrono::steady_clock::now()); |
| 144 | }); |
| 145 | |
| 146 | if constexpr (debug) |
| 147 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 148 | lg2::error( |
| 149 | "Next ExternalSensor timer '{VALUE}' us", "VALUE", |
| 150 | std::chrono::duration_cast<std::chrono::microseconds>(nextCheck) |
| 151 | .count()); |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 155 | void createSensors( |
Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 156 | sdbusplus::asio::object_server& objectServer, |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 157 | boost::container::flat_map<std::string, std::shared_ptr<ExternalSensor>>& |
| 158 | sensors, |
| 159 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
| 160 | const std::shared_ptr<boost::container::flat_set<std::string>>& |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 161 | sensorsChanged, |
| 162 | boost::asio::steady_timer& reaperTimer) |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 163 | { |
Josh Lehan | 0362738 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 164 | if constexpr (debug) |
| 165 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 166 | lg2::error("ExternalSensor considering creating sensors"); |
Josh Lehan | 0362738 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 169 | auto getter = std::make_shared<GetSensorConfiguration>( |
| 170 | dbusConnection, |
Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 171 | [&objectServer, &sensors, &dbusConnection, sensorsChanged, |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 172 | &reaperTimer](const ManagedObjectType& sensorConfigurations) { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 173 | bool firstScan = (sensorsChanged == nullptr); |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 174 | |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 175 | for (const std::pair<sdbusplus::message::object_path, SensorData>& |
| 176 | sensor : sensorConfigurations) |
| 177 | { |
| 178 | const std::string& interfacePath = sensor.first.str; |
| 179 | const SensorData& sensorData = sensor.second; |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 180 | |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 181 | auto sensorBase = |
| 182 | sensorData.find(configInterfaceName(sensorType)); |
| 183 | if (sensorBase == sensorData.end()) |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 184 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 185 | lg2::error("Base configuration not found for '{PATH}'", |
| 186 | "PATH", interfacePath); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 187 | continue; |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 188 | } |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 189 | |
| 190 | const SensorBaseConfiguration& baseConfiguration = *sensorBase; |
| 191 | const SensorBaseConfigMap& baseConfigMap = |
| 192 | baseConfiguration.second; |
| 193 | |
| 194 | // MinValue and MinValue are mandatory numeric parameters |
| 195 | auto minFound = baseConfigMap.find("MinValue"); |
| 196 | if (minFound == baseConfigMap.end()) |
| 197 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 198 | lg2::error("MinValue parameter not found for '{PATH}'", |
| 199 | "PATH", interfacePath); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 200 | continue; |
| 201 | } |
| 202 | double minValue = |
| 203 | std::visit(VariantToDoubleVisitor(), minFound->second); |
| 204 | if (!std::isfinite(minValue)) |
| 205 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 206 | lg2::error("MinValue parameter not parsed for '{PATH}'", |
| 207 | "PATH", interfacePath); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 208 | continue; |
| 209 | } |
| 210 | |
| 211 | auto maxFound = baseConfigMap.find("MaxValue"); |
| 212 | if (maxFound == baseConfigMap.end()) |
| 213 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 214 | lg2::error("MaxValue parameter not found for '{PATH}'", |
| 215 | "PATH", interfacePath); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 216 | continue; |
| 217 | } |
| 218 | double maxValue = |
| 219 | std::visit(VariantToDoubleVisitor(), maxFound->second); |
| 220 | if (!std::isfinite(maxValue)) |
| 221 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 222 | lg2::error("MaxValue parameter not parsed for '{PATH}'", |
| 223 | "PATH", interfacePath); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 224 | continue; |
| 225 | } |
| 226 | |
| 227 | double timeoutSecs = 0.0; |
| 228 | |
| 229 | // Timeout is an optional numeric parameter |
| 230 | auto timeoutFound = baseConfigMap.find("Timeout"); |
| 231 | if (timeoutFound != baseConfigMap.end()) |
| 232 | { |
| 233 | timeoutSecs = std::visit(VariantToDoubleVisitor(), |
| 234 | timeoutFound->second); |
| 235 | } |
| 236 | if (!std::isfinite(timeoutSecs) || (timeoutSecs < 0.0)) |
| 237 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 238 | lg2::error("Timeout parameter not parsed for '{PATH}'", |
| 239 | "PATH", interfacePath); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 240 | continue; |
| 241 | } |
| 242 | |
| 243 | std::string sensorName; |
| 244 | std::string sensorUnits; |
| 245 | |
| 246 | // Name and Units are mandatory string parameters |
| 247 | auto nameFound = baseConfigMap.find("Name"); |
| 248 | if (nameFound == baseConfigMap.end()) |
| 249 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 250 | lg2::error("Name parameter not found for '{PATH}'", "PATH", |
| 251 | interfacePath); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 252 | continue; |
| 253 | } |
| 254 | sensorName = |
| 255 | std::visit(VariantToStringVisitor(), nameFound->second); |
| 256 | if (sensorName.empty()) |
| 257 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 258 | lg2::error("Name parameter not parsed for '{PATH}'", "PATH", |
| 259 | interfacePath); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 260 | continue; |
| 261 | } |
| 262 | |
| 263 | auto unitsFound = baseConfigMap.find("Units"); |
| 264 | if (unitsFound == baseConfigMap.end()) |
| 265 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 266 | lg2::error("Units parameter not found for '{PATH}'", "PATH", |
| 267 | interfacePath); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 268 | continue; |
| 269 | } |
| 270 | sensorUnits = |
| 271 | std::visit(VariantToStringVisitor(), unitsFound->second); |
| 272 | if (sensorUnits.empty()) |
| 273 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 274 | lg2::error("Units parameter not parsed for '{PATH}'", |
| 275 | "PATH", interfacePath); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 276 | continue; |
| 277 | } |
| 278 | |
| 279 | // on rescans, only update sensors we were signaled by |
| 280 | auto findSensor = sensors.find(sensorName); |
| 281 | if (!firstScan && (findSensor != sensors.end())) |
| 282 | { |
| 283 | std::string suffixName = "/"; |
| 284 | suffixName += findSensor->second->name; |
| 285 | bool found = false; |
| 286 | for (auto it = sensorsChanged->begin(); |
| 287 | it != sensorsChanged->end(); it++) |
| 288 | { |
| 289 | std::string suffixIt = "/"; |
| 290 | suffixIt += *it; |
| 291 | if (suffixIt.ends_with(suffixName)) |
| 292 | { |
| 293 | sensorsChanged->erase(it); |
| 294 | findSensor->second = nullptr; |
| 295 | found = true; |
| 296 | if constexpr (debug) |
| 297 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 298 | lg2::error( |
| 299 | "ExternalSensor '{NAME}' change found", |
| 300 | "NAME", sensorName); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 301 | } |
| 302 | break; |
| 303 | } |
| 304 | } |
| 305 | if (!found) |
| 306 | { |
| 307 | continue; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | std::vector<thresholds::Threshold> sensorThresholds; |
| 312 | if (!parseThresholdsFromConfig(sensorData, sensorThresholds)) |
| 313 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 314 | lg2::error("error populating thresholds for '{NAME}'", |
| 315 | "NAME", sensorName); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | PowerState readState = getPowerState(baseConfigMap); |
| 319 | |
| 320 | auto& sensorEntry = sensors[sensorName]; |
| 321 | sensorEntry = nullptr; |
| 322 | |
| 323 | sensorEntry = std::make_shared<ExternalSensor>( |
| 324 | sensorType, objectServer, dbusConnection, sensorName, |
| 325 | sensorUnits, std::move(sensorThresholds), interfacePath, |
| 326 | maxValue, minValue, timeoutSecs, readState); |
| 327 | sensorEntry->initWriteHook( |
| 328 | [&sensors, &reaperTimer]( |
| 329 | const std::chrono::steady_clock::time_point& now) { |
| 330 | updateReaper(sensors, reaperTimer, now); |
| 331 | }); |
| 332 | |
| 333 | if constexpr (debug) |
| 334 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 335 | lg2::error("ExternalSensor '{NAME}' created", "NAME", |
| 336 | sensorName); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 337 | } |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 338 | } |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 339 | }); |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 340 | |
| 341 | getter->getConfiguration(std::vector<std::string>{sensorType}); |
| 342 | } |
| 343 | |
| 344 | int main() |
| 345 | { |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 346 | if constexpr (debug) |
| 347 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 348 | lg2::error("ExternalSensor service starting up"); |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 351 | boost::asio::io_context io; |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 352 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
Ed Tanous | 14ed5e9 | 2022-07-12 15:50:23 -0700 | [diff] [blame] | 353 | sdbusplus::asio::object_server objectServer(systemBus, true); |
| 354 | |
| 355 | objectServer.add_manager("/xyz/openbmc_project/sensors"); |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 356 | systemBus->request_name("xyz.openbmc_project.ExternalSensor"); |
Ed Tanous | 14ed5e9 | 2022-07-12 15:50:23 -0700 | [diff] [blame] | 357 | |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 358 | boost::container::flat_map<std::string, std::shared_ptr<ExternalSensor>> |
| 359 | sensors; |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 360 | auto sensorsChanged = |
| 361 | std::make_shared<boost::container::flat_set<std::string>>(); |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 362 | boost::asio::steady_timer reaperTimer(io); |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 363 | |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 364 | boost::asio::post(io, [&objectServer, &sensors, &systemBus, |
| 365 | &reaperTimer]() { |
Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 366 | createSensors(objectServer, sensors, systemBus, nullptr, reaperTimer); |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 367 | }); |
| 368 | |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 369 | boost::asio::steady_timer filterTimer(io); |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 370 | std::function<void(sdbusplus::message_t&)> eventHandler = |
Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 371 | [&objectServer, &sensors, &systemBus, &sensorsChanged, &filterTimer, |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 372 | &reaperTimer](sdbusplus::message_t& message) mutable { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 373 | if (message.is_method_error()) |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 374 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 375 | lg2::error("callback method error"); |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 376 | return; |
| 377 | } |
Josh Lehan | 0362738 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 378 | |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 379 | const auto* messagePath = message.get_path(); |
| 380 | sensorsChanged->insert(messagePath); |
| 381 | if constexpr (debug) |
| 382 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 383 | lg2::error("ExternalSensor change event received: '{PATH}'", |
| 384 | "PATH", messagePath); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | // this implicitly cancels the timer |
| 388 | filterTimer.expires_after(std::chrono::seconds(1)); |
| 389 | |
| 390 | filterTimer.async_wait( |
| 391 | [&objectServer, &sensors, &systemBus, &sensorsChanged, |
| 392 | &reaperTimer](const boost::system::error_code& ec) mutable { |
| 393 | if (ec != boost::system::errc::success) |
| 394 | { |
| 395 | if (ec != boost::asio::error::operation_aborted) |
| 396 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 397 | lg2::error("callback error: '{ERROR_MESSAGE}'", |
| 398 | "ERROR_MESSAGE", ec.message()); |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 399 | } |
| 400 | return; |
| 401 | } |
| 402 | |
| 403 | createSensors(objectServer, sensors, systemBus, |
| 404 | sensorsChanged, reaperTimer); |
| 405 | }); |
| 406 | }; |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 407 | |
Zev Weiss | 214d971 | 2022-08-12 12:54:31 -0700 | [diff] [blame] | 408 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches = |
| 409 | setupPropertiesChangedMatches( |
| 410 | *systemBus, std::to_array<const char*>({sensorType}), eventHandler); |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 411 | |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 412 | if constexpr (debug) |
| 413 | { |
George Liu | d630b3a | 2025-02-20 11:02:49 +0800 | [diff] [blame] | 414 | lg2::error("ExternalSensor service entering main loop"); |
Josh Lehan | 7243217 | 2021-03-17 13:35:43 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Josh Lehan | 2a40e93 | 2020-09-02 11:48:14 -0700 | [diff] [blame] | 417 | io.run(); |
| 418 | } |