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