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