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