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