| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | // Copyright (c) 2017 Intel Corporation | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
| Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 17 | #include <ADCSensor.hpp> | 
|  | 18 | #include <Utils.hpp> | 
|  | 19 | #include <VariantVisitors.hpp> | 
| James Feist | b83bb2b | 2019-05-31 12:31:23 -0700 | [diff] [blame] | 20 | #include <boost/algorithm/string/case_conv.hpp> | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 21 | #include <boost/algorithm/string/predicate.hpp> | 
|  | 22 | #include <boost/algorithm/string/replace.hpp> | 
|  | 23 | #include <boost/container/flat_set.hpp> | 
| James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 24 | #include <sdbusplus/asio/connection.hpp> | 
|  | 25 | #include <sdbusplus/asio/object_server.hpp> | 
|  | 26 | #include <sdbusplus/bus/match.hpp> | 
|  | 27 |  | 
| James Feist | 24f02f2 | 2019-04-15 11:05:39 -0700 | [diff] [blame] | 28 | #include <filesystem> | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 29 | #include <fstream> | 
| Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 30 | #include <functional> | 
|  | 31 | #include <memory> | 
| Zhu, Yunge | a5b1bbc | 2019-04-09 19:49:36 -0400 | [diff] [blame] | 32 | #include <optional> | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 33 | #include <regex> | 
| Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 34 | #include <string> | 
|  | 35 | #include <variant> | 
|  | 36 | #include <vector> | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 37 |  | 
| Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 38 | static constexpr float pollRateDefault = 0.5; | 
| Zev Weiss | f72eb83 | 2021-06-25 05:55:08 +0000 | [diff] [blame] | 39 | static constexpr float gpioBridgeSetupTimeDefault = 0.02; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 40 |  | 
| James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 41 | namespace fs = std::filesystem; | 
| James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 42 |  | 
| Brandon Kim | 6655823 | 2021-11-09 16:53:08 -0800 | [diff] [blame] | 43 | static constexpr auto sensorTypes{ | 
|  | 44 | std::to_array<const char*>({"xyz.openbmc_project.Configuration.ADC"})}; | 
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 45 | static std::regex inputRegex(R"(in(\d+)_input)"); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 46 |  | 
| James Feist | b83bb2b | 2019-05-31 12:31:23 -0700 | [diff] [blame] | 47 | static boost::container::flat_map<size_t, bool> cpuPresence; | 
|  | 48 |  | 
| Matt Spinler | 6ad74d9 | 2022-03-01 10:56:46 -0600 | [diff] [blame^] | 49 | enum class UpdateType | 
|  | 50 | { | 
|  | 51 | init, | 
|  | 52 | cpuPresenceChange | 
|  | 53 | }; | 
|  | 54 |  | 
| James Feist | 08aec6f | 2019-01-30 16:17:04 -0800 | [diff] [blame] | 55 | // filter out adc from any other voltage sensor | 
|  | 56 | bool isAdc(const fs::path& parentPath) | 
|  | 57 | { | 
|  | 58 | fs::path namePath = parentPath / "name"; | 
|  | 59 |  | 
|  | 60 | std::ifstream nameFile(namePath); | 
|  | 61 | if (!nameFile.good()) | 
|  | 62 | { | 
|  | 63 | std::cerr << "Failure reading " << namePath.string() << "\n"; | 
|  | 64 | return false; | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | std::string name; | 
|  | 68 | std::getline(nameFile, name); | 
|  | 69 |  | 
|  | 70 | return name == "iio_hwmon"; | 
|  | 71 | } | 
|  | 72 |  | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 73 | void createSensors( | 
|  | 74 | boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer, | 
| Yong Li | 1afda6b | 2020-04-09 19:24:13 +0800 | [diff] [blame] | 75 | boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>>& | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 76 | sensors, | 
|  | 77 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, | 
| James Feist | 5591cf08 | 2020-07-15 16:44:54 -0700 | [diff] [blame] | 78 | const std::shared_ptr<boost::container::flat_set<std::string>>& | 
| Matt Spinler | 6ad74d9 | 2022-03-01 10:56:46 -0600 | [diff] [blame^] | 79 | sensorsChanged, | 
|  | 80 | UpdateType updateType) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 81 | { | 
| James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 82 | auto getter = std::make_shared<GetSensorConfiguration>( | 
|  | 83 | dbusConnection, | 
| Matt Spinler | 6ad74d9 | 2022-03-01 10:56:46 -0600 | [diff] [blame^] | 84 | [&io, &objectServer, &sensors, &dbusConnection, sensorsChanged, | 
|  | 85 | updateType](const ManagedObjectType& sensorConfigurations) { | 
| James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 86 | bool firstScan = sensorsChanged == nullptr; | 
|  | 87 | std::vector<fs::path> paths; | 
|  | 88 | if (!findFiles(fs::path("/sys/class/hwmon"), R"(in\d+_input)", | 
|  | 89 | paths)) | 
| James Feist | 08aec6f | 2019-01-30 16:17:04 -0800 | [diff] [blame] | 90 | { | 
| krishnar4 | a040ec4 | 2021-10-06 16:44:12 +0530 | [diff] [blame] | 91 | std::cerr << "No adc sensors in system\n"; | 
| James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 92 | return; | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | // iterate through all found adc sensors, and try to match them with | 
|  | 96 | // configuration | 
|  | 97 | for (auto& path : paths) | 
|  | 98 | { | 
|  | 99 | if (!isAdc(path.parent_path())) | 
| James Feist | 08aec6f | 2019-01-30 16:17:04 -0800 | [diff] [blame] | 100 | { | 
| James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 101 | continue; | 
| James Feist | 08aec6f | 2019-01-30 16:17:04 -0800 | [diff] [blame] | 102 | } | 
| James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 103 | std::smatch match; | 
|  | 104 | std::string pathStr = path.string(); | 
| James Feist | 08aec6f | 2019-01-30 16:17:04 -0800 | [diff] [blame] | 105 |  | 
| James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 106 | std::regex_search(pathStr, match, inputRegex); | 
|  | 107 | std::string indexStr = *(match.begin() + 1); | 
| James Feist | 08aec6f | 2019-01-30 16:17:04 -0800 | [diff] [blame] | 108 |  | 
| James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 109 | auto directory = path.parent_path(); | 
|  | 110 | // convert to 0 based | 
|  | 111 | size_t index = std::stoul(indexStr) - 1; | 
| James Feist | 08aec6f | 2019-01-30 16:17:04 -0800 | [diff] [blame] | 112 |  | 
| James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 113 | const SensorData* sensorData = nullptr; | 
|  | 114 | const std::string* interfacePath = nullptr; | 
|  | 115 | const std::pair< | 
|  | 116 | std::string, | 
|  | 117 | boost::container::flat_map<std::string, BasicVariantType>>* | 
| Ed Tanous | a771f6a | 2022-01-14 09:36:51 -0800 | [diff] [blame] | 118 | baseConfiguration = nullptr; | 
| James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 119 | for (const std::pair<sdbusplus::message::object_path, | 
|  | 120 | SensorData>& sensor : sensorConfigurations) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 121 | { | 
| James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 122 | // clear it out each loop | 
|  | 123 | baseConfiguration = nullptr; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 124 |  | 
| James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 125 | // find base configuration | 
|  | 126 | for (const char* type : sensorTypes) | 
| Jae Hyun Yoo | 12bbae0 | 2019-07-22 17:24:09 -0700 | [diff] [blame] | 127 | { | 
| James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 128 | auto sensorBase = sensor.second.find(type); | 
|  | 129 | if (sensorBase != sensor.second.end()) | 
| Jae Hyun Yoo | 12bbae0 | 2019-07-22 17:24:09 -0700 | [diff] [blame] | 130 | { | 
| James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 131 | baseConfiguration = &(*sensorBase); | 
|  | 132 | break; | 
| Jae Hyun Yoo | 12bbae0 | 2019-07-22 17:24:09 -0700 | [diff] [blame] | 133 | } | 
|  | 134 | } | 
| James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 135 | if (baseConfiguration == nullptr) | 
|  | 136 | { | 
|  | 137 | continue; | 
|  | 138 | } | 
|  | 139 | auto findIndex = baseConfiguration->second.find("Index"); | 
|  | 140 | if (findIndex == baseConfiguration->second.end()) | 
|  | 141 | { | 
|  | 142 | std::cerr << "Base configuration missing Index" | 
|  | 143 | << baseConfiguration->first << "\n"; | 
|  | 144 | continue; | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | unsigned int number = std::visit( | 
|  | 148 | VariantToUnsignedIntVisitor(), findIndex->second); | 
|  | 149 |  | 
|  | 150 | if (number != index) | 
|  | 151 | { | 
|  | 152 | continue; | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 | sensorData = &(sensor.second); | 
|  | 156 | interfacePath = &(sensor.first.str); | 
|  | 157 | break; | 
|  | 158 | } | 
|  | 159 | if (sensorData == nullptr) | 
|  | 160 | { | 
|  | 161 | std::cerr << "failed to find match for " << path.string() | 
|  | 162 | << "\n"; | 
|  | 163 | continue; | 
| Jae Hyun Yoo | 12bbae0 | 2019-07-22 17:24:09 -0700 | [diff] [blame] | 164 | } | 
|  | 165 |  | 
| James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 166 | if (baseConfiguration == nullptr) | 
|  | 167 | { | 
|  | 168 | std::cerr << "error finding base configuration for" | 
|  | 169 | << path.string() << "\n"; | 
|  | 170 | continue; | 
|  | 171 | } | 
| Jae Hyun Yoo | 12bbae0 | 2019-07-22 17:24:09 -0700 | [diff] [blame] | 172 |  | 
| James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 173 | auto findSensorName = baseConfiguration->second.find("Name"); | 
|  | 174 | if (findSensorName == baseConfiguration->second.end()) | 
|  | 175 | { | 
|  | 176 | std::cerr << "could not determine configuration name for " | 
|  | 177 | << path.string() << "\n"; | 
|  | 178 | continue; | 
|  | 179 | } | 
|  | 180 | std::string sensorName = | 
|  | 181 | std::get<std::string>(findSensorName->second); | 
|  | 182 |  | 
|  | 183 | // on rescans, only update sensors we were signaled by | 
|  | 184 | auto findSensor = sensors.find(sensorName); | 
|  | 185 | if (!firstScan && findSensor != sensors.end()) | 
|  | 186 | { | 
|  | 187 | bool found = false; | 
|  | 188 | for (auto it = sensorsChanged->begin(); | 
|  | 189 | it != sensorsChanged->end(); it++) | 
|  | 190 | { | 
| Wludzik, Jozef | 9a44547 | 2020-06-24 12:11:09 +0200 | [diff] [blame] | 191 | if (findSensor->second && | 
|  | 192 | boost::ends_with(*it, findSensor->second->name)) | 
| James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 193 | { | 
|  | 194 | sensorsChanged->erase(it); | 
|  | 195 | findSensor->second = nullptr; | 
|  | 196 | found = true; | 
|  | 197 | break; | 
|  | 198 | } | 
|  | 199 | } | 
|  | 200 | if (!found) | 
|  | 201 | { | 
|  | 202 | continue; | 
|  | 203 | } | 
|  | 204 | } | 
| Matt Spinler | 6ad74d9 | 2022-03-01 10:56:46 -0600 | [diff] [blame^] | 205 |  | 
|  | 206 | auto findCPU = baseConfiguration->second.find("CPURequired"); | 
|  | 207 | if (findCPU != baseConfiguration->second.end()) | 
|  | 208 | { | 
|  | 209 | size_t index = | 
|  | 210 | std::visit(VariantToIntVisitor(), findCPU->second); | 
|  | 211 | auto presenceFind = cpuPresence.find(index); | 
|  | 212 | if (presenceFind == cpuPresence.end()) | 
|  | 213 | { | 
|  | 214 | continue; // no such cpu | 
|  | 215 | } | 
|  | 216 | if (!presenceFind->second) | 
|  | 217 | { | 
|  | 218 | continue; // cpu not installed | 
|  | 219 | } | 
|  | 220 | } | 
|  | 221 | else if (updateType == UpdateType::cpuPresenceChange) | 
|  | 222 | { | 
|  | 223 | continue; | 
|  | 224 | } | 
|  | 225 |  | 
| James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 226 | std::vector<thresholds::Threshold> sensorThresholds; | 
|  | 227 | if (!parseThresholdsFromConfig(*sensorData, sensorThresholds)) | 
|  | 228 | { | 
|  | 229 | std::cerr << "error populating thresholds for " | 
|  | 230 | << sensorName << "\n"; | 
|  | 231 | } | 
|  | 232 |  | 
|  | 233 | auto findScaleFactor = | 
|  | 234 | baseConfiguration->second.find("ScaleFactor"); | 
|  | 235 | float scaleFactor = 1.0; | 
|  | 236 | if (findScaleFactor != baseConfiguration->second.end()) | 
|  | 237 | { | 
|  | 238 | scaleFactor = std::visit(VariantToFloatVisitor(), | 
|  | 239 | findScaleFactor->second); | 
| Zhikui Ren | 937eb54 | 2020-10-12 13:42:08 -0700 | [diff] [blame] | 240 | // scaleFactor is used in division | 
|  | 241 | if (scaleFactor == 0.0f) | 
|  | 242 | { | 
|  | 243 | scaleFactor = 1.0; | 
|  | 244 | } | 
| James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 245 | } | 
|  | 246 |  | 
| Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 247 | auto findPollRate = baseConfiguration->second.find("PollRate"); | 
|  | 248 | float pollRate = pollRateDefault; | 
|  | 249 | if (findPollRate != baseConfiguration->second.end()) | 
|  | 250 | { | 
|  | 251 | pollRate = std::visit(VariantToFloatVisitor(), | 
|  | 252 | findPollRate->second); | 
|  | 253 | if (pollRate <= 0.0f) | 
|  | 254 | { | 
|  | 255 | pollRate = pollRateDefault; // polling time too short | 
|  | 256 | } | 
|  | 257 | } | 
|  | 258 |  | 
| James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 259 | auto findPowerOn = baseConfiguration->second.find("PowerState"); | 
|  | 260 | PowerState readState = PowerState::always; | 
|  | 261 | if (findPowerOn != baseConfiguration->second.end()) | 
|  | 262 | { | 
|  | 263 | std::string powerState = std::visit( | 
|  | 264 | VariantToStringVisitor(), findPowerOn->second); | 
|  | 265 | setReadState(powerState, readState); | 
|  | 266 | } | 
|  | 267 |  | 
| James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 268 | auto& sensor = sensors[sensorName]; | 
|  | 269 | sensor = nullptr; | 
|  | 270 |  | 
|  | 271 | std::optional<BridgeGpio> bridgeGpio; | 
|  | 272 | for (const SensorBaseConfiguration& suppConfig : *sensorData) | 
|  | 273 | { | 
|  | 274 | if (suppConfig.first.find("BridgeGpio") != | 
|  | 275 | std::string::npos) | 
|  | 276 | { | 
|  | 277 | auto findName = suppConfig.second.find("Name"); | 
|  | 278 | if (findName != suppConfig.second.end()) | 
|  | 279 | { | 
|  | 280 | std::string gpioName = std::visit( | 
|  | 281 | VariantToStringVisitor(), findName->second); | 
|  | 282 |  | 
|  | 283 | int polarity = gpiod::line::ACTIVE_HIGH; | 
|  | 284 | auto findPolarity = | 
|  | 285 | suppConfig.second.find("Polarity"); | 
|  | 286 | if (findPolarity != suppConfig.second.end()) | 
|  | 287 | { | 
|  | 288 | if (std::string("Low") == | 
|  | 289 | std::visit(VariantToStringVisitor(), | 
|  | 290 | findPolarity->second)) | 
|  | 291 | { | 
|  | 292 | polarity = gpiod::line::ACTIVE_LOW; | 
|  | 293 | } | 
|  | 294 | } | 
| Zev Weiss | f72eb83 | 2021-06-25 05:55:08 +0000 | [diff] [blame] | 295 |  | 
|  | 296 | float setupTime = gpioBridgeSetupTimeDefault; | 
|  | 297 | auto findSetupTime = | 
|  | 298 | suppConfig.second.find("SetupTime"); | 
|  | 299 | if (findSetupTime != suppConfig.second.end()) | 
|  | 300 | { | 
|  | 301 | setupTime = std::visit(VariantToFloatVisitor(), | 
|  | 302 | findSetupTime->second); | 
|  | 303 | } | 
|  | 304 |  | 
|  | 305 | bridgeGpio = | 
|  | 306 | BridgeGpio(gpioName, polarity, setupTime); | 
| James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 307 | } | 
|  | 308 |  | 
|  | 309 | break; | 
|  | 310 | } | 
|  | 311 | } | 
|  | 312 |  | 
| Yong Li | 1afda6b | 2020-04-09 19:24:13 +0800 | [diff] [blame] | 313 | sensor = std::make_shared<ADCSensor>( | 
| James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 314 | path.string(), objectServer, dbusConnection, io, sensorName, | 
| Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 315 | std::move(sensorThresholds), scaleFactor, pollRate, | 
|  | 316 | readState, *interfacePath, std::move(bridgeGpio)); | 
| Yong Li | 1afda6b | 2020-04-09 19:24:13 +0800 | [diff] [blame] | 317 | sensor->setupRead(); | 
| James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 318 | } | 
| Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 319 | }); | 
| James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 320 |  | 
|  | 321 | getter->getConfiguration( | 
|  | 322 | std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()}); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 323 | } | 
|  | 324 |  | 
| James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 325 | int main() | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 326 | { | 
|  | 327 | boost::asio::io_service io; | 
|  | 328 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); | 
|  | 329 | systemBus->request_name("xyz.openbmc_project.ADCSensor"); | 
|  | 330 | sdbusplus::asio::object_server objectServer(systemBus); | 
| Yong Li | 1afda6b | 2020-04-09 19:24:13 +0800 | [diff] [blame] | 331 | boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>> sensors; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 332 | std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches; | 
| James Feist | 5591cf08 | 2020-07-15 16:44:54 -0700 | [diff] [blame] | 333 | auto sensorsChanged = | 
|  | 334 | std::make_shared<boost::container::flat_set<std::string>>(); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 335 |  | 
|  | 336 | io.post([&]() { | 
| Matt Spinler | 6ad74d9 | 2022-03-01 10:56:46 -0600 | [diff] [blame^] | 337 | createSensors(io, objectServer, sensors, systemBus, nullptr, | 
|  | 338 | UpdateType::init); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 339 | }); | 
|  | 340 |  | 
|  | 341 | boost::asio::deadline_timer filterTimer(io); | 
|  | 342 | std::function<void(sdbusplus::message::message&)> eventHandler = | 
|  | 343 | [&](sdbusplus::message::message& message) { | 
|  | 344 | if (message.is_method_error()) | 
|  | 345 | { | 
|  | 346 | std::cerr << "callback method error\n"; | 
|  | 347 | return; | 
|  | 348 | } | 
|  | 349 | sensorsChanged->insert(message.get_path()); | 
|  | 350 | // this implicitly cancels the timer | 
|  | 351 | filterTimer.expires_from_now(boost::posix_time::seconds(1)); | 
|  | 352 |  | 
|  | 353 | filterTimer.async_wait([&](const boost::system::error_code& ec) { | 
|  | 354 | if (ec == boost::asio::error::operation_aborted) | 
|  | 355 | { | 
|  | 356 | /* we were canceled*/ | 
|  | 357 | return; | 
|  | 358 | } | 
| Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 359 | if (ec) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 360 | { | 
|  | 361 | std::cerr << "timer error\n"; | 
|  | 362 | return; | 
|  | 363 | } | 
|  | 364 | createSensors(io, objectServer, sensors, systemBus, | 
| Matt Spinler | 6ad74d9 | 2022-03-01 10:56:46 -0600 | [diff] [blame^] | 365 | sensorsChanged, UpdateType::init); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 366 | }); | 
|  | 367 | }; | 
|  | 368 |  | 
| James Feist | b83bb2b | 2019-05-31 12:31:23 -0700 | [diff] [blame] | 369 | std::function<void(sdbusplus::message::message&)> cpuPresenceHandler = | 
|  | 370 | [&](sdbusplus::message::message& message) { | 
|  | 371 | std::string path = message.get_path(); | 
|  | 372 | boost::to_lower(path); | 
|  | 373 |  | 
|  | 374 | if (path.rfind("cpu") == std::string::npos) | 
|  | 375 | { | 
|  | 376 | return; // not interested | 
|  | 377 | } | 
|  | 378 | size_t index = 0; | 
|  | 379 | try | 
|  | 380 | { | 
|  | 381 | index = std::stoi(path.substr(path.size() - 1)); | 
|  | 382 | } | 
| Patrick Williams | 26601e8 | 2021-10-06 12:43:25 -0500 | [diff] [blame] | 383 | catch (const std::invalid_argument&) | 
| James Feist | b83bb2b | 2019-05-31 12:31:23 -0700 | [diff] [blame] | 384 | { | 
|  | 385 | std::cerr << "Found invalid path " << path << "\n"; | 
|  | 386 | return; | 
|  | 387 | } | 
|  | 388 |  | 
|  | 389 | std::string objectName; | 
|  | 390 | boost::container::flat_map<std::string, std::variant<bool>> values; | 
|  | 391 | message.read(objectName, values); | 
|  | 392 | auto findPresence = values.find("Present"); | 
|  | 393 | if (findPresence != values.end()) | 
|  | 394 | { | 
|  | 395 | cpuPresence[index] = std::get<bool>(findPresence->second); | 
|  | 396 | } | 
|  | 397 |  | 
|  | 398 | // this implicitly cancels the timer | 
|  | 399 | filterTimer.expires_from_now(boost::posix_time::seconds(1)); | 
|  | 400 |  | 
|  | 401 | filterTimer.async_wait([&](const boost::system::error_code& ec) { | 
|  | 402 | if (ec == boost::asio::error::operation_aborted) | 
|  | 403 | { | 
|  | 404 | /* we were canceled*/ | 
|  | 405 | return; | 
|  | 406 | } | 
| Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 407 | if (ec) | 
| James Feist | b83bb2b | 2019-05-31 12:31:23 -0700 | [diff] [blame] | 408 | { | 
|  | 409 | std::cerr << "timer error\n"; | 
|  | 410 | return; | 
|  | 411 | } | 
| Matt Spinler | 6ad74d9 | 2022-03-01 10:56:46 -0600 | [diff] [blame^] | 412 | createSensors(io, objectServer, sensors, systemBus, nullptr, | 
|  | 413 | UpdateType::cpuPresenceChange); | 
| James Feist | b83bb2b | 2019-05-31 12:31:23 -0700 | [diff] [blame] | 414 | }); | 
|  | 415 | }; | 
|  | 416 |  | 
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 417 | for (const char* type : sensorTypes) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 418 | { | 
|  | 419 | auto match = std::make_unique<sdbusplus::bus::match::match>( | 
|  | 420 | static_cast<sdbusplus::bus::bus&>(*systemBus), | 
|  | 421 | "type='signal',member='PropertiesChanged',path_namespace='" + | 
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 422 | std::string(inventoryPath) + "',arg0namespace='" + type + "'", | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 423 | eventHandler); | 
|  | 424 | matches.emplace_back(std::move(match)); | 
|  | 425 | } | 
| James Feist | b83bb2b | 2019-05-31 12:31:23 -0700 | [diff] [blame] | 426 | matches.emplace_back(std::make_unique<sdbusplus::bus::match::match>( | 
|  | 427 | static_cast<sdbusplus::bus::bus&>(*systemBus), | 
|  | 428 | "type='signal',member='PropertiesChanged',path_namespace='" + | 
|  | 429 | std::string(cpuInventoryPath) + | 
|  | 430 | "',arg0namespace='xyz.openbmc_project.Inventory.Item'", | 
|  | 431 | cpuPresenceHandler)); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 432 |  | 
| Bruce Lee | 1263c3d | 2021-06-04 15:16:33 +0800 | [diff] [blame] | 433 | setupManufacturingModeMatch(*systemBus); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 434 | io.run(); | 
|  | 435 | } |