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