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 | |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 17 | #include "PwmSensor.hpp" |
| 18 | #include "TachSensor.hpp" |
| 19 | #include "Utils.hpp" |
| 20 | #include "VariantVisitors.hpp" |
| 21 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 22 | #include <boost/algorithm/string/replace.hpp> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 23 | #include <boost/container/flat_map.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 24 | #include <boost/container/flat_set.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 25 | #include <sdbusplus/asio/connection.hpp> |
| 26 | #include <sdbusplus/asio/object_server.hpp> |
| 27 | #include <sdbusplus/bus/match.hpp> |
| 28 | |
| 29 | #include <array> |
James Feist | 24f02f2 | 2019-04-15 11:05:39 -0700 | [diff] [blame] | 30 | #include <filesystem> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 31 | #include <fstream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 32 | #include <functional> |
| 33 | #include <memory> |
| 34 | #include <optional> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 35 | #include <regex> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 36 | #include <string> |
| 37 | #include <utility> |
| 38 | #include <variant> |
| 39 | #include <vector> |
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 | |
Yong Zhao | a3e8f2a | 2021-01-09 02:22:43 +0000 | [diff] [blame] | 43 | // The following two structures need to be consistent |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 44 | static auto sensorTypes{ |
| 45 | std::to_array<const char*>({"AspeedFan", "I2CFan", "NuvotonFan"})}; |
Yong Zhao | a3e8f2a | 2021-01-09 02:22:43 +0000 | [diff] [blame] | 46 | |
| 47 | enum FanTypes |
| 48 | { |
| 49 | aspeed = 0, |
| 50 | i2c, |
| 51 | nuvoton, |
| 52 | max, |
| 53 | }; |
| 54 | |
| 55 | static_assert(std::tuple_size<decltype(sensorTypes)>::value == FanTypes::max, |
| 56 | "sensorTypes element number is not equal to FanTypes number"); |
| 57 | |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 58 | constexpr const char* redundancyConfiguration = |
| 59 | "xyz.openbmc_project.Configuration.FanRedundancy"; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 60 | static std::regex inputRegex(R"(fan(\d+)_input)"); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 61 | |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 62 | // todo: power supply fan redundancy |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 63 | std::optional<RedundancySensor> systemRedundancy; |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 64 | |
| 65 | FanTypes getFanType(const fs::path& parentPath) |
| 66 | { |
Chris Sides | 9a472e8 | 2023-04-03 15:13:37 -0500 | [diff] [blame^] | 67 | fs::path linkPath = parentPath / "of_node"; |
| 68 | std::string canonical = fs::canonical(linkPath); |
| 69 | |
| 70 | std::string compatiblePath = canonical + "/compatible"; |
| 71 | std::ifstream compatibleStream(compatiblePath); |
| 72 | |
| 73 | if (!compatibleStream.is_open()) |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 74 | { |
Chris Sides | 9a472e8 | 2023-04-03 15:13:37 -0500 | [diff] [blame^] | 75 | std::cerr << "Error opening " << compatiblePath << "\n"; |
| 76 | return FanTypes::i2c; // Should this throw an exception instead? |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 77 | } |
Chris Sides | 9a472e8 | 2023-04-03 15:13:37 -0500 | [diff] [blame^] | 78 | |
| 79 | std::string compatibleString; |
| 80 | while (compatibleStream.peek() != EOF) |
Peter Lundgren | 8843b62 | 2019-09-12 10:33:41 -0700 | [diff] [blame] | 81 | { |
Chris Sides | 9a472e8 | 2023-04-03 15:13:37 -0500 | [diff] [blame^] | 82 | std::getline(compatibleStream, compatibleString); |
| 83 | compatibleString.pop_back(); // trim EOL before comparisons |
| 84 | |
| 85 | if (compatibleString == "aspeed,ast2400-pwm-tacho" || |
| 86 | compatibleString == "aspeed,ast2500-pwm-tacho") |
| 87 | { |
| 88 | return FanTypes::aspeed; |
| 89 | } |
| 90 | if (compatibleString == "nuvoton,npcm750-pwm-fan") |
| 91 | { |
| 92 | return FanTypes::nuvoton; |
| 93 | } |
Peter Lundgren | 8843b62 | 2019-09-12 10:33:41 -0700 | [diff] [blame] | 94 | } |
Chris Sides | 9a472e8 | 2023-04-03 15:13:37 -0500 | [diff] [blame^] | 95 | |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 96 | // todo: will we need to support other types? |
| 97 | return FanTypes::i2c; |
| 98 | } |
Jeff Lin | abf91de | 2020-12-23 10:55:42 +0800 | [diff] [blame] | 99 | void enablePwm(const fs::path& filePath) |
| 100 | { |
| 101 | std::fstream enableFile(filePath, std::ios::in | std::ios::out); |
| 102 | if (!enableFile.good()) |
| 103 | { |
| 104 | std::cerr << "Error read/write " << filePath << "\n"; |
| 105 | return; |
| 106 | } |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 107 | |
Jeff Lin | abf91de | 2020-12-23 10:55:42 +0800 | [diff] [blame] | 108 | std::string regulateMode; |
| 109 | std::getline(enableFile, regulateMode); |
| 110 | if (regulateMode == "0") |
| 111 | { |
| 112 | enableFile << 1; |
| 113 | } |
| 114 | } |
Howard Chiu | ddf25d1 | 2021-12-02 15:14:44 +0800 | [diff] [blame] | 115 | bool findPwmfanPath(unsigned int configPwmfanIndex, fs::path& pwmPath) |
| 116 | { |
| 117 | /* Search PWM since pwm-fan had separated |
| 118 | * PWM from tach directory and 1 channel only*/ |
| 119 | std::vector<fs::path> pwmfanPaths; |
| 120 | std::string pwnfanDevName("pwm-fan"); |
| 121 | |
| 122 | pwnfanDevName += std::to_string(configPwmfanIndex); |
| 123 | |
| 124 | if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+)", pwmfanPaths)) |
| 125 | { |
| 126 | std::cerr << "No PWMs are found!\n"; |
| 127 | return false; |
| 128 | } |
| 129 | for (const auto& path : pwmfanPaths) |
| 130 | { |
| 131 | std::error_code ec; |
| 132 | fs::path link = fs::read_symlink(path.parent_path() / "device", ec); |
| 133 | |
| 134 | if (ec) |
| 135 | { |
| 136 | std::cerr << "read_symlink() failed: " << ec.message() << " (" |
| 137 | << ec.value() << ")\n"; |
| 138 | continue; |
| 139 | } |
| 140 | |
| 141 | if (link.filename().string() == pwnfanDevName) |
| 142 | { |
| 143 | pwmPath = path; |
| 144 | return true; |
| 145 | } |
| 146 | } |
| 147 | return false; |
| 148 | } |
| 149 | bool findPwmPath(const fs::path& directory, unsigned int pwm, fs::path& pwmPath) |
| 150 | { |
| 151 | std::error_code ec; |
| 152 | |
| 153 | /* Assuming PWM file is appeared in the same directory as fanX_input */ |
| 154 | auto path = directory / ("pwm" + std::to_string(pwm + 1)); |
| 155 | bool exists = fs::exists(path, ec); |
| 156 | |
| 157 | if (ec || !exists) |
| 158 | { |
| 159 | /* PWM file not exist or error happened */ |
| 160 | if (ec) |
| 161 | { |
| 162 | std::cerr << "exists() failed: " << ec.message() << " (" |
| 163 | << ec.value() << ")\n"; |
| 164 | } |
| 165 | /* try search form pwm-fanX directory */ |
| 166 | return findPwmfanPath(pwm, pwmPath); |
| 167 | } |
| 168 | |
| 169 | pwmPath = path; |
| 170 | return true; |
| 171 | } |
Justin Ledford | 9c47bd7 | 2022-08-27 01:02:09 +0000 | [diff] [blame] | 172 | |
| 173 | // The argument to this function should be the fanN_input file that we want to |
| 174 | // enable. The function will locate the corresponding fanN_enable file if it |
| 175 | // exists. Note that some drivers don't provide this file if the sensors are |
| 176 | // always enabled. |
| 177 | void enableFanInput(const fs::path& fanInputPath) |
| 178 | { |
| 179 | std::error_code ec; |
| 180 | std::string path(fanInputPath.string()); |
| 181 | boost::replace_last(path, "input", "enable"); |
| 182 | |
| 183 | bool exists = fs::exists(path, ec); |
| 184 | if (ec || !exists) |
| 185 | { |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | std::fstream enableFile(path, std::ios::out); |
| 190 | if (!enableFile.good()) |
| 191 | { |
| 192 | return; |
| 193 | } |
| 194 | enableFile << 1; |
| 195 | } |
| 196 | |
Kuiying Wang | d540741 | 2020-09-09 16:06:56 +0800 | [diff] [blame] | 197 | void createRedundancySensor( |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 198 | const boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>& |
Kuiying Wang | d540741 | 2020-09-09 16:06:56 +0800 | [diff] [blame] | 199 | sensors, |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 200 | const std::shared_ptr<sdbusplus::asio::connection>& conn, |
Kuiying Wang | d540741 | 2020-09-09 16:06:56 +0800 | [diff] [blame] | 201 | sdbusplus::asio::object_server& objectServer) |
| 202 | { |
| 203 | |
| 204 | conn->async_method_call( |
| 205 | [&objectServer, &sensors](boost::system::error_code& ec, |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 206 | const ManagedObjectType& managedObj) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 207 | if (ec) |
| 208 | { |
| 209 | std::cerr << "Error calling entity manager \n"; |
| 210 | return; |
| 211 | } |
Zev Weiss | 77636ec | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 212 | for (const auto& [path, interfaces] : managedObj) |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 213 | { |
Zev Weiss | 77636ec | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 214 | for (const auto& [intf, cfg] : interfaces) |
Kuiying Wang | d540741 | 2020-09-09 16:06:56 +0800 | [diff] [blame] | 215 | { |
Zev Weiss | 77636ec | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 216 | if (intf == redundancyConfiguration) |
Kuiying Wang | d540741 | 2020-09-09 16:06:56 +0800 | [diff] [blame] | 217 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 218 | // currently only support one |
Zev Weiss | 77636ec | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 219 | auto findCount = cfg.find("AllowedFailures"); |
| 220 | if (findCount == cfg.end()) |
Kuiying Wang | d540741 | 2020-09-09 16:06:56 +0800 | [diff] [blame] | 221 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 222 | std::cerr << "Malformed redundancy record \n"; |
Kuiying Wang | d540741 | 2020-09-09 16:06:56 +0800 | [diff] [blame] | 223 | return; |
| 224 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 225 | std::vector<std::string> sensorList; |
| 226 | |
Zev Weiss | 77636ec | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 227 | for (const auto& [name, sensor] : sensors) |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 228 | { |
| 229 | sensorList.push_back( |
| 230 | "/xyz/openbmc_project/sensors/fan_tach/" + |
Zev Weiss | 77636ec | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 231 | sensor->name); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 232 | } |
| 233 | systemRedundancy.reset(); |
Zev Weiss | 77636ec | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 234 | systemRedundancy.emplace( |
| 235 | RedundancySensor(std::get<uint64_t>(findCount->second), |
| 236 | sensorList, objectServer, path)); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 237 | |
| 238 | return; |
Kuiying Wang | d540741 | 2020-09-09 16:06:56 +0800 | [diff] [blame] | 239 | } |
| 240 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 241 | } |
Kuiying Wang | d540741 | 2020-09-09 16:06:56 +0800 | [diff] [blame] | 242 | }, |
Nan Zhou | 3e620af | 2022-09-20 22:28:31 +0000 | [diff] [blame] | 243 | "xyz.openbmc_project.EntityManager", "/xyz/openbmc_project/inventory", |
Kuiying Wang | d540741 | 2020-09-09 16:06:56 +0800 | [diff] [blame] | 244 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
| 245 | } |
| 246 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 247 | void createSensors( |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 248 | boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 249 | boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>& |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 250 | tachSensors, |
| 251 | boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>& |
| 252 | pwmSensors, |
| 253 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
James Feist | 5591cf08 | 2020-07-15 16:44:54 -0700 | [diff] [blame] | 254 | const std::shared_ptr<boost::container::flat_set<std::string>>& |
James Feist | f27a55c | 2020-08-04 14:27:30 -0700 | [diff] [blame] | 255 | sensorsChanged, |
| 256 | size_t retries = 0) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 257 | { |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 258 | auto getter = std::make_shared<GetSensorConfiguration>( |
| 259 | dbusConnection, |
Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 260 | [&io, &objectServer, &tachSensors, &pwmSensors, &dbusConnection, |
| 261 | sensorsChanged](const ManagedObjectType& sensorConfigurations) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 262 | bool firstScan = sensorsChanged == nullptr; |
| 263 | std::vector<fs::path> paths; |
| 264 | if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)", paths)) |
| 265 | { |
| 266 | std::cerr << "No fan sensors in system\n"; |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | // iterate through all found fan sensors, and try to match them with |
| 271 | // configuration |
| 272 | for (const auto& path : paths) |
| 273 | { |
| 274 | std::smatch match; |
| 275 | std::string pathStr = path.string(); |
| 276 | |
| 277 | std::regex_search(pathStr, match, inputRegex); |
| 278 | std::string indexStr = *(match.begin() + 1); |
| 279 | |
| 280 | fs::path directory = path.parent_path(); |
| 281 | FanTypes fanType = getFanType(directory); |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 282 | std::string cfgIntf = configInterfaceName(sensorTypes[fanType]); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 283 | |
| 284 | // convert to 0 based |
| 285 | size_t index = std::stoul(indexStr) - 1; |
| 286 | |
| 287 | const char* baseType = nullptr; |
| 288 | const SensorData* sensorData = nullptr; |
| 289 | const std::string* interfacePath = nullptr; |
| 290 | const SensorBaseConfiguration* baseConfiguration = nullptr; |
Zev Weiss | 77636ec | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 291 | for (const auto& [path, cfgData] : sensorConfigurations) |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 292 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 293 | // find the base of the configuration to see if indexes |
| 294 | // match |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 295 | auto sensorBaseFind = cfgData.find(cfgIntf); |
Zev Weiss | 77636ec | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 296 | if (sensorBaseFind == cfgData.end()) |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 297 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 298 | continue; |
| 299 | } |
| 300 | |
| 301 | baseConfiguration = &(*sensorBaseFind); |
Zev Weiss | 77636ec | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 302 | interfacePath = &path.str; |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 303 | baseType = sensorTypes[fanType]; |
| 304 | |
| 305 | auto findIndex = baseConfiguration->second.find("Index"); |
| 306 | if (findIndex == baseConfiguration->second.end()) |
| 307 | { |
| 308 | std::cerr << baseConfiguration->first << " missing index\n"; |
| 309 | continue; |
| 310 | } |
| 311 | unsigned int configIndex = std::visit( |
| 312 | VariantToUnsignedIntVisitor(), findIndex->second); |
| 313 | if (configIndex != index) |
| 314 | { |
| 315 | continue; |
| 316 | } |
| 317 | if (fanType == FanTypes::aspeed || fanType == FanTypes::nuvoton) |
| 318 | { |
| 319 | // there will be only 1 aspeed or nuvoton sensor object |
| 320 | // in sysfs, we found the fan |
Zev Weiss | 77636ec | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 321 | sensorData = &cfgData; |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 322 | break; |
| 323 | } |
| 324 | if (fanType == FanTypes::i2c) |
| 325 | { |
| 326 | size_t bus = 0; |
| 327 | size_t address = 0; |
| 328 | |
| 329 | std::string link = |
| 330 | fs::read_symlink(directory / "device").filename(); |
| 331 | |
| 332 | size_t findDash = link.find('-'); |
| 333 | if (findDash == std::string::npos || |
| 334 | link.size() <= findDash + 1) |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 335 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 336 | std::cerr << "Error finding device from symlink"; |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 337 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 338 | bus = std::stoi(link.substr(0, findDash)); |
| 339 | address = std::stoi(link.substr(findDash + 1), nullptr, 16); |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 340 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 341 | auto findBus = baseConfiguration->second.find("Bus"); |
| 342 | auto findAddress = |
| 343 | baseConfiguration->second.find("Address"); |
| 344 | if (findBus == baseConfiguration->second.end() || |
| 345 | findAddress == baseConfiguration->second.end()) |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 346 | { |
| 347 | std::cerr << baseConfiguration->first |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 348 | << " missing bus or address\n"; |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 349 | continue; |
| 350 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 351 | unsigned int configBus = std::visit( |
| 352 | VariantToUnsignedIntVisitor(), findBus->second); |
| 353 | unsigned int configAddress = std::visit( |
| 354 | VariantToUnsignedIntVisitor(), findAddress->second); |
| 355 | |
| 356 | if (configBus == bus && configAddress == address) |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 357 | { |
Zev Weiss | 77636ec | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 358 | sensorData = &cfgData; |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 359 | break; |
| 360 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | if (sensorData == nullptr) |
| 364 | { |
| 365 | std::cerr << "failed to find match for " << path.string() |
| 366 | << "\n"; |
| 367 | continue; |
| 368 | } |
| 369 | |
| 370 | auto findSensorName = baseConfiguration->second.find("Name"); |
| 371 | |
| 372 | if (findSensorName == baseConfiguration->second.end()) |
| 373 | { |
| 374 | std::cerr << "could not determine configuration name for " |
| 375 | << path.string() << "\n"; |
| 376 | continue; |
| 377 | } |
| 378 | std::string sensorName = |
| 379 | std::get<std::string>(findSensorName->second); |
| 380 | |
| 381 | // on rescans, only update sensors we were signaled by |
| 382 | auto findSensor = tachSensors.find(sensorName); |
| 383 | if (!firstScan && findSensor != tachSensors.end()) |
| 384 | { |
| 385 | bool found = false; |
| 386 | for (auto it = sensorsChanged->begin(); |
| 387 | it != sensorsChanged->end(); it++) |
| 388 | { |
Zev Weiss | 6c106d6 | 2022-08-17 20:50:00 -0700 | [diff] [blame] | 389 | if (it->ends_with(findSensor->second->name)) |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 390 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 391 | sensorsChanged->erase(it); |
| 392 | findSensor->second = nullptr; |
| 393 | found = true; |
| 394 | break; |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 395 | } |
| 396 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 397 | if (!found) |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 398 | { |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 399 | continue; |
| 400 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 401 | } |
| 402 | std::vector<thresholds::Threshold> sensorThresholds; |
| 403 | if (!parseThresholdsFromConfig(*sensorData, sensorThresholds)) |
| 404 | { |
| 405 | std::cerr << "error populating thresholds for " << sensorName |
| 406 | << "\n"; |
| 407 | } |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 408 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 409 | auto presenceConfig = |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 410 | sensorData->find(cfgIntf + std::string(".Presence")); |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 411 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 412 | std::unique_ptr<PresenceSensor> presenceSensor(nullptr); |
| 413 | |
| 414 | // presence sensors are optional |
| 415 | if (presenceConfig != sensorData->end()) |
| 416 | { |
| 417 | auto findPolarity = presenceConfig->second.find("Polarity"); |
| 418 | auto findPinName = presenceConfig->second.find("PinName"); |
| 419 | |
| 420 | if (findPinName == presenceConfig->second.end() || |
| 421 | findPolarity == presenceConfig->second.end()) |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 422 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 423 | std::cerr << "Malformed Presence Configuration\n"; |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 424 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 425 | else |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 426 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 427 | bool inverted = |
| 428 | std::get<std::string>(findPolarity->second) == "Low"; |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 429 | if (const auto* pinName = |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 430 | std::get_if<std::string>(&findPinName->second)) |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 431 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 432 | presenceSensor = std::make_unique<PresenceSensor>( |
| 433 | *pinName, inverted, io, sensorName); |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 434 | } |
| 435 | else |
| 436 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 437 | std::cerr << "Malformed Presence pinName for sensor " |
| 438 | << sensorName << " \n"; |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 439 | } |
| 440 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 441 | } |
| 442 | std::optional<RedundancySensor>* redundancy = nullptr; |
| 443 | if (fanType == FanTypes::aspeed) |
| 444 | { |
| 445 | redundancy = &systemRedundancy; |
| 446 | } |
| 447 | |
Zev Weiss | a4d2768 | 2022-07-19 15:30:36 -0700 | [diff] [blame] | 448 | PowerState powerState = getPowerState(baseConfiguration->second); |
Yong Zhao | 77b3add | 2021-01-09 04:25:18 +0000 | [diff] [blame] | 449 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 450 | constexpr double defaultMaxReading = 25000; |
| 451 | constexpr double defaultMinReading = 0; |
| 452 | std::pair<double, double> limits = |
| 453 | std::make_pair(defaultMinReading, defaultMaxReading); |
| 454 | |
| 455 | auto connector = |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 456 | sensorData->find(cfgIntf + std::string(".Connector")); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 457 | |
| 458 | std::optional<std::string> led; |
| 459 | std::string pwmName; |
| 460 | fs::path pwmPath; |
| 461 | |
| 462 | // The Mutable parameter is optional, defaulting to false |
| 463 | bool isValueMutable = false; |
| 464 | if (connector != sensorData->end()) |
| 465 | { |
| 466 | auto findPwm = connector->second.find("Pwm"); |
| 467 | if (findPwm != connector->second.end()) |
| 468 | { |
| 469 | size_t pwm = std::visit(VariantToUnsignedIntVisitor(), |
| 470 | findPwm->second); |
| 471 | if (!findPwmPath(directory, pwm, pwmPath)) |
| 472 | { |
| 473 | std::cerr << "Connector for " << sensorName |
| 474 | << " no pwm channel found!\n"; |
| 475 | continue; |
| 476 | } |
| 477 | |
| 478 | fs::path pwmEnableFile = |
| 479 | "pwm" + std::to_string(pwm + 1) + "_enable"; |
| 480 | fs::path enablePath = pwmPath.parent_path() / pwmEnableFile; |
| 481 | enablePwm(enablePath); |
| 482 | |
| 483 | /* use pwm name override if found in configuration else |
| 484 | * use default */ |
| 485 | auto findOverride = connector->second.find("PwmName"); |
| 486 | if (findOverride != connector->second.end()) |
| 487 | { |
| 488 | pwmName = std::visit(VariantToStringVisitor(), |
| 489 | findOverride->second); |
| 490 | } |
| 491 | else |
| 492 | { |
| 493 | pwmName = "Pwm_" + std::to_string(pwm + 1); |
| 494 | } |
| 495 | |
| 496 | // Check PWM sensor mutability |
| 497 | auto findMutable = connector->second.find("Mutable"); |
| 498 | if (findMutable != connector->second.end()) |
| 499 | { |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 500 | const auto* ptrMutable = |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 501 | std::get_if<bool>(&(findMutable->second)); |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 502 | if (ptrMutable != nullptr) |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 503 | { |
| 504 | isValueMutable = *ptrMutable; |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | else |
| 509 | { |
| 510 | std::cerr << "Connector for " << sensorName |
| 511 | << " missing pwm!\n"; |
| 512 | } |
| 513 | |
| 514 | auto findLED = connector->second.find("LED"); |
| 515 | if (findLED != connector->second.end()) |
| 516 | { |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 517 | const auto* ledName = |
| 518 | std::get_if<std::string>(&(findLED->second)); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 519 | if (ledName == nullptr) |
| 520 | { |
| 521 | std::cerr << "Wrong format for LED of " << sensorName |
| 522 | << "\n"; |
| 523 | } |
| 524 | else |
| 525 | { |
| 526 | led = *ledName; |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | findLimits(limits, baseConfiguration); |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 532 | |
Justin Ledford | 9c47bd7 | 2022-08-27 01:02:09 +0000 | [diff] [blame] | 533 | enableFanInput(path); |
| 534 | |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 535 | auto& tachSensor = tachSensors[sensorName]; |
| 536 | tachSensor = nullptr; |
| 537 | tachSensor = std::make_shared<TachSensor>( |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 538 | path.string(), baseType, objectServer, dbusConnection, |
| 539 | std::move(presenceSensor), redundancy, io, sensorName, |
| 540 | std::move(sensorThresholds), *interfacePath, limits, powerState, |
| 541 | led); |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 542 | tachSensor->setupRead(); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 543 | |
| 544 | if (!pwmPath.empty() && fs::exists(pwmPath) && |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 545 | (pwmSensors.count(pwmPath) == 0U)) |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 546 | { |
| 547 | pwmSensors[pwmPath] = std::make_unique<PwmSensor>( |
| 548 | pwmName, pwmPath, dbusConnection, objectServer, |
| 549 | *interfacePath, "Fan", isValueMutable); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | createRedundancySensor(tachSensors, dbusConnection, objectServer); |
Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 554 | }); |
James Feist | de5e970 | 2019-09-18 16:13:02 -0700 | [diff] [blame] | 555 | getter->getConfiguration( |
James Feist | f27a55c | 2020-08-04 14:27:30 -0700 | [diff] [blame] | 556 | std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()}, |
| 557 | retries); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 558 | } |
| 559 | |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 560 | int main() |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 561 | { |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 562 | boost::asio::io_context io; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 563 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
Ed Tanous | 14ed5e9 | 2022-07-12 15:50:23 -0700 | [diff] [blame] | 564 | sdbusplus::asio::object_server objectServer(systemBus, true); |
| 565 | |
| 566 | objectServer.add_manager("/xyz/openbmc_project/sensors"); |
Ed Tanous | d906725 | 2022-10-22 15:35:52 -0700 | [diff] [blame] | 567 | objectServer.add_manager("/xyz/openbmc_project/control"); |
Lei YU | c2f83fe | 2022-12-02 14:49:47 +0800 | [diff] [blame] | 568 | objectServer.add_manager("/xyz/openbmc_project/inventory"); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 569 | systemBus->request_name("xyz.openbmc_project.FanSensor"); |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 570 | boost::container::flat_map<std::string, std::shared_ptr<TachSensor>> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 571 | tachSensors; |
| 572 | boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>> |
| 573 | pwmSensors; |
James Feist | 5591cf08 | 2020-07-15 16:44:54 -0700 | [diff] [blame] | 574 | auto sensorsChanged = |
| 575 | std::make_shared<boost::container::flat_set<std::string>>(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 576 | |
Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 577 | boost::asio::post(io, [&]() { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 578 | createSensors(io, objectServer, tachSensors, pwmSensors, systemBus, |
| 579 | nullptr); |
| 580 | }); |
| 581 | |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 582 | boost::asio::steady_timer filterTimer(io); |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 583 | std::function<void(sdbusplus::message_t&)> eventHandler = |
| 584 | [&](sdbusplus::message_t& message) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 585 | if (message.is_method_error()) |
| 586 | { |
| 587 | std::cerr << "callback method error\n"; |
| 588 | return; |
| 589 | } |
| 590 | sensorsChanged->insert(message.get_path()); |
| 591 | // this implicitly cancels the timer |
Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 592 | filterTimer.expires_after(std::chrono::seconds(1)); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 593 | |
| 594 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 595 | if (ec == boost::asio::error::operation_aborted) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 596 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 597 | /* we were canceled*/ |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 598 | return; |
| 599 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 600 | if (ec) |
| 601 | { |
| 602 | std::cerr << "timer error\n"; |
| 603 | return; |
| 604 | } |
| 605 | createSensors(io, objectServer, tachSensors, pwmSensors, systemBus, |
| 606 | sensorsChanged, 5); |
| 607 | }); |
| 608 | }; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 609 | |
Zev Weiss | 214d971 | 2022-08-12 12:54:31 -0700 | [diff] [blame] | 610 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches = |
| 611 | setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 612 | |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 613 | // redundancy sensor |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 614 | std::function<void(sdbusplus::message_t&)> redundancyHandler = |
| 615 | [&tachSensors, &systemBus, &objectServer](sdbusplus::message_t&) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 616 | createRedundancySensor(tachSensors, systemBus, objectServer); |
| 617 | }; |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 618 | auto match = std::make_unique<sdbusplus::bus::match_t>( |
| 619 | static_cast<sdbusplus::bus_t&>(*systemBus), |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 620 | "type='signal',member='PropertiesChanged',path_namespace='" + |
| 621 | std::string(inventoryPath) + "',arg0namespace='" + |
| 622 | redundancyConfiguration + "'", |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 623 | std::move(redundancyHandler)); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 624 | matches.emplace_back(std::move(match)); |
| 625 | |
Bruce Lee | 1263c3d | 2021-06-04 15:16:33 +0800 | [diff] [blame] | 626 | setupManufacturingModeMatch(*systemBus); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 627 | io.run(); |
Zhikui Ren | 8685b17 | 2021-06-29 15:16:52 -0700 | [diff] [blame] | 628 | return 0; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 629 | } |