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