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