blob: e8f1fff17c43abc2c064aeb6fef4b96c6c88750e [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001/*
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
Patrick Ventureca44b2f2019-10-31 11:02:26 -070017#include "PwmSensor.hpp"
18#include "TachSensor.hpp"
19#include "Utils.hpp"
20#include "VariantVisitors.hpp"
21
James Feist6714a252018-09-10 15:26:18 -070022#include <boost/algorithm/string/predicate.hpp>
23#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070024#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070025#include <boost/container/flat_set.hpp>
26#include <boost/lexical_cast.hpp>
James Feist38fb5982020-05-28 10:09:54 -070027#include <sdbusplus/asio/connection.hpp>
28#include <sdbusplus/asio/object_server.hpp>
29#include <sdbusplus/bus/match.hpp>
30
31#include <array>
James Feist24f02f22019-04-15 11:05:39 -070032#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070033#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070034#include <functional>
35#include <memory>
36#include <optional>
James Feist6714a252018-09-10 15:26:18 -070037#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070038#include <string>
39#include <utility>
40#include <variant>
41#include <vector>
James Feist6714a252018-09-10 15:26:18 -070042
43static constexpr bool DEBUG = false;
44
James Feistcf3bce62019-01-08 10:07:19 -080045namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080046
Peter Lundgren8843b622019-09-12 10:33:41 -070047static constexpr std::array<const char*, 3> sensorTypes = {
James Feist95b079b2018-11-21 09:28:00 -080048 "xyz.openbmc_project.Configuration.AspeedFan",
Peter Lundgren8843b622019-09-12 10:33:41 -070049 "xyz.openbmc_project.Configuration.I2CFan",
50 "xyz.openbmc_project.Configuration.NuvotonFan"};
James Feistdc6c55f2018-10-31 12:53:20 -070051constexpr const char* redundancyConfiguration =
52 "xyz.openbmc_project.Configuration.FanRedundancy";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070053static std::regex inputRegex(R"(fan(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070054
James Feist95b079b2018-11-21 09:28:00 -080055enum class FanTypes
56{
57 aspeed,
Peter Lundgren8843b622019-09-12 10:33:41 -070058 i2c,
59 nuvoton
James Feist95b079b2018-11-21 09:28:00 -080060};
61
James Feistdc6c55f2018-10-31 12:53:20 -070062// todo: power supply fan redundancy
James Feist7b18b1e2019-05-14 13:42:09 -070063std::optional<RedundancySensor> systemRedundancy;
James Feist95b079b2018-11-21 09:28:00 -080064
65FanTypes getFanType(const fs::path& parentPath)
66{
67 fs::path linkPath = parentPath / "device";
68 std::string canonical = fs::read_symlink(linkPath);
Jae Hyun Yoo241356e2020-02-20 12:48:04 -080069 if (boost::ends_with(canonical, "1e786000.pwm-tacho-controller") ||
70 boost::ends_with(canonical, "1e610000.pwm-tacho-controller"))
James Feist95b079b2018-11-21 09:28:00 -080071 {
72 return FanTypes::aspeed;
73 }
Peter Lundgren8843b622019-09-12 10:33:41 -070074 else if (boost::ends_with(canonical, "f0103000.pwm-fan-controller"))
75 {
76 return FanTypes::nuvoton;
77 }
James Feist95b079b2018-11-21 09:28:00 -080078 // todo: will we need to support other types?
79 return FanTypes::i2c;
80}
James Feistdc6c55f2018-10-31 12:53:20 -070081
James Feist6714a252018-09-10 15:26:18 -070082void createSensors(
83 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
84 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
85 tachSensors,
86 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
87 pwmSensors,
88 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -070089 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feistf27a55c2020-08-04 14:27:30 -070090 sensorsChanged,
91 size_t retries = 0)
James Feist6714a252018-09-10 15:26:18 -070092{
James Feist6714a252018-09-10 15:26:18 -070093
James Feistde5e9702019-09-18 16:13:02 -070094 auto getter = std::make_shared<GetSensorConfiguration>(
95 dbusConnection,
96 std::move([&io, &objectServer, &tachSensors, &pwmSensors,
James Feist5591cf082020-07-15 16:44:54 -070097 &dbusConnection, sensorsChanged](
James Feistde5e9702019-09-18 16:13:02 -070098 const ManagedObjectType& sensorConfigurations) {
99 bool firstScan = sensorsChanged == nullptr;
100 std::vector<fs::path> paths;
101 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)",
102 paths))
James Feist95b079b2018-11-21 09:28:00 -0800103 {
James Feistde5e9702019-09-18 16:13:02 -0700104 std::cerr << "No temperature sensors in system\n";
105 return;
James Feist95b079b2018-11-21 09:28:00 -0800106 }
James Feist6714a252018-09-10 15:26:18 -0700107
Jason Lingd320a2e2020-07-11 14:08:14 -0700108 // pwm index, sysfs path, pwm name
109 std::vector<std::tuple<uint8_t, std::string, std::string>>
110 pwmNumbers;
James Feistde5e9702019-09-18 16:13:02 -0700111
112 // iterate through all found fan sensors, and try to match them with
113 // configuration
114 for (const auto& path : paths)
James Feist6714a252018-09-10 15:26:18 -0700115 {
James Feistde5e9702019-09-18 16:13:02 -0700116 std::smatch match;
117 std::string pathStr = path.string();
118
119 std::regex_search(pathStr, match, inputRegex);
120 std::string indexStr = *(match.begin() + 1);
121
122 auto directory = path.parent_path();
123 FanTypes fanType = getFanType(directory);
124 size_t bus = 0;
125 size_t address = 0;
126 if (fanType == FanTypes::i2c)
James Feist6714a252018-09-10 15:26:18 -0700127 {
James Feistde5e9702019-09-18 16:13:02 -0700128 std::string link =
129 fs::read_symlink(directory / "device").filename();
130
131 size_t findDash = link.find("-");
132 if (findDash == std::string::npos ||
133 link.size() <= findDash + 1)
134 {
135 std::cerr << "Error finding device from symlink";
136 }
137 bus = std::stoi(link.substr(0, findDash));
138 address = std::stoi(link.substr(findDash + 1), nullptr, 16);
James Feist6714a252018-09-10 15:26:18 -0700139 }
James Feistde5e9702019-09-18 16:13:02 -0700140 // convert to 0 based
141 size_t index = std::stoul(indexStr) - 1;
142
143 const char* baseType;
144 const SensorData* sensorData = nullptr;
145 const std::string* interfacePath = nullptr;
146 const SensorBaseConfiguration* baseConfiguration = nullptr;
147 for (const std::pair<sdbusplus::message::object_path,
148 SensorData>& sensor : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800149 {
James Feistde5e9702019-09-18 16:13:02 -0700150 // find the base of the configuration to see if indexes
151 // match
152 for (const char* type : sensorTypes)
153 {
154 auto sensorBaseFind = sensor.second.find(type);
155 if (sensorBaseFind != sensor.second.end())
156 {
157 baseConfiguration = &(*sensorBaseFind);
158 interfacePath = &(sensor.first.str);
159 baseType = type;
160 break;
161 }
162 }
163 if (baseConfiguration == nullptr)
164 {
165 continue;
166 }
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800167
James Feistde5e9702019-09-18 16:13:02 -0700168 auto findIndex = baseConfiguration->second.find("Index");
169 if (findIndex == baseConfiguration->second.end())
170 {
171 std::cerr << baseConfiguration->first
172 << " missing index\n";
173 continue;
174 }
175 unsigned int configIndex = std::visit(
176 VariantToUnsignedIntVisitor(), findIndex->second);
177 if (configIndex != index)
178 {
179 continue;
180 }
181 if (fanType == FanTypes::aspeed ||
182 fanType == FanTypes::nuvoton)
183 {
184 // there will be only 1 aspeed or nuvoton sensor object
185 // in sysfs, we found the fan
186 sensorData = &(sensor.second);
187 break;
188 }
189 else if (baseType ==
190 std::string(
191 "xyz.openbmc_project.Configuration.I2CFan"))
192 {
193 auto findBus = baseConfiguration->second.find("Bus");
194 auto findAddress =
195 baseConfiguration->second.find("Address");
196 if (findBus == baseConfiguration->second.end() ||
197 findAddress == baseConfiguration->second.end())
198 {
199 std::cerr << baseConfiguration->first
200 << " missing bus or address\n";
201 continue;
202 }
203 unsigned int configBus = std::visit(
204 VariantToUnsignedIntVisitor(), findBus->second);
205 unsigned int configAddress = std::visit(
206 VariantToUnsignedIntVisitor(), findAddress->second);
207
208 if (configBus == bus && configAddress == address)
209 {
210 sensorData = &(sensor.second);
211 break;
212 }
213 }
214 }
215 if (sensorData == nullptr)
216 {
217 std::cerr << "failed to find match for " << path.string()
218 << "\n";
James Feist95b079b2018-11-21 09:28:00 -0800219 continue;
220 }
James Feist95b079b2018-11-21 09:28:00 -0800221
James Feistde5e9702019-09-18 16:13:02 -0700222 auto findSensorName = baseConfiguration->second.find("Name");
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800223
James Feistde5e9702019-09-18 16:13:02 -0700224 if (findSensorName == baseConfiguration->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800225 {
James Feistde5e9702019-09-18 16:13:02 -0700226 std::cerr << "could not determine configuration name for "
227 << path.string() << "\n";
228 continue;
229 }
230 std::string sensorName =
231 std::get<std::string>(findSensorName->second);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800232
James Feistde5e9702019-09-18 16:13:02 -0700233 // on rescans, only update sensors we were signaled by
234 auto findSensor = tachSensors.find(sensorName);
235 if (!firstScan && findSensor != tachSensors.end())
236 {
237 bool found = false;
238 for (auto it = sensorsChanged->begin();
239 it != sensorsChanged->end(); it++)
240 {
241 if (boost::ends_with(*it, findSensor->second->name))
242 {
243 sensorsChanged->erase(it);
244 findSensor->second = nullptr;
245 found = true;
246 break;
247 }
248 }
249 if (!found)
250 {
251 continue;
252 }
253 }
254 std::vector<thresholds::Threshold> sensorThresholds;
255 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
256 {
257 std::cerr << "error populating thresholds for "
258 << sensorName << "\n";
259 }
260
261 auto presenceConfig =
262 sensorData->find(baseType + std::string(".Presence"));
263
264 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
265
266 // presence sensors are optional
267 if (presenceConfig != sensorData->end())
268 {
James Feistde5e9702019-09-18 16:13:02 -0700269 auto findPolarity = presenceConfig->second.find("Polarity");
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800270 auto findPinName = presenceConfig->second.find("PinName");
James Feistde5e9702019-09-18 16:13:02 -0700271
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800272 if (findPinName == presenceConfig->second.end() ||
James Feistde5e9702019-09-18 16:13:02 -0700273 findPolarity == presenceConfig->second.end())
274 {
275 std::cerr << "Malformed Presence Configuration\n";
276 }
277 else
278 {
James Feistde5e9702019-09-18 16:13:02 -0700279 bool inverted = std::get<std::string>(
280 findPolarity->second) == "Low";
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800281 if (auto pinName =
282 std::get_if<std::string>(&findPinName->second))
283 {
284 presenceSensor = std::make_unique<PresenceSensor>(
285 *pinName, inverted, io, sensorName);
286 }
287 else
288 {
289 std::cerr
290 << "Malformed Presence pinName for sensor "
291 << sensorName << " \n";
292 }
James Feistde5e9702019-09-18 16:13:02 -0700293 }
294 }
295 std::optional<RedundancySensor>* redundancy = nullptr;
296 if (fanType == FanTypes::aspeed)
297 {
298 redundancy = &systemRedundancy;
299 }
300
301 constexpr double defaultMaxReading = 25000;
302 constexpr double defaultMinReading = 0;
303 auto limits =
304 std::make_pair(defaultMinReading, defaultMaxReading);
305
306 findLimits(limits, baseConfiguration);
307 tachSensors[sensorName] = std::make_unique<TachSensor>(
308 path.string(), baseType, objectServer, dbusConnection,
309 std::move(presenceSensor), redundancy, io, sensorName,
310 std::move(sensorThresholds), *interfacePath, limits);
311
312 auto connector =
313 sensorData->find(baseType + std::string(".Connector"));
314 if (connector != sensorData->end())
315 {
316 auto findPwm = connector->second.find("Pwm");
317 if (findPwm == connector->second.end())
318 {
319 std::cerr << "Connector Missing PWM!\n";
320 continue;
321 }
James Feistde5e9702019-09-18 16:13:02 -0700322 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
323 findPwm->second);
Jason Lingd320a2e2020-07-11 14:08:14 -0700324 /* use pwm name override if found in configuration else use
325 * default */
326 auto findOverride = connector->second.find("PwmName");
327 std::string pwmName;
328 if (findOverride != connector->second.end())
329 {
330 pwmName = std::visit(VariantToStringVisitor(),
331 findOverride->second);
332 }
333 else
334 {
335 pwmName = "Pwm_" + std::to_string(pwm + 1);
336 }
337 pwmNumbers.emplace_back(pwm, *interfacePath, pwmName);
James Feist95b079b2018-11-21 09:28:00 -0800338 }
339 }
James Feistde5e9702019-09-18 16:13:02 -0700340 std::vector<fs::path> pwms;
341 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+$)", pwms))
James Feist6714a252018-09-10 15:26:18 -0700342 {
James Feistde5e9702019-09-18 16:13:02 -0700343 std::cerr << "No pwm in system\n";
344 return;
345 }
346 for (const fs::path& pwm : pwms)
347 {
348 if (pwmSensors.find(pwm) != pwmSensors.end())
James Feist6714a252018-09-10 15:26:18 -0700349 {
James Feistde5e9702019-09-18 16:13:02 -0700350 continue;
James Feist6714a252018-09-10 15:26:18 -0700351 }
James Feistde5e9702019-09-18 16:13:02 -0700352 const std::string* path = nullptr;
Jason Lingd320a2e2020-07-11 14:08:14 -0700353 const std::string* pwmName = nullptr;
354
355 for (const auto& [index, configPath, name] : pwmNumbers)
James Feistde5e9702019-09-18 16:13:02 -0700356 {
357 if (boost::ends_with(pwm.string(),
358 std::to_string(index + 1)))
359 {
360 path = &configPath;
Jason Lingd320a2e2020-07-11 14:08:14 -0700361 pwmName = &name;
James Feistde5e9702019-09-18 16:13:02 -0700362 break;
363 }
364 }
365
366 if (path == nullptr)
367 {
368 continue;
369 }
370
371 // only add new elements
372 const std::string& sysPath = pwm.string();
James Feistde5e9702019-09-18 16:13:02 -0700373 pwmSensors.insert(
374 std::pair<std::string, std::unique_ptr<PwmSensor>>(
375 sysPath, std::make_unique<PwmSensor>(
Jason Lingd320a2e2020-07-11 14:08:14 -0700376 *pwmName, sysPath, dbusConnection,
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530377 objectServer, *path, "Fan")));
James Feist6714a252018-09-10 15:26:18 -0700378 }
James Feistde5e9702019-09-18 16:13:02 -0700379 }));
380 getter->getConfiguration(
James Feistf27a55c2020-08-04 14:27:30 -0700381 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()},
382 retries);
James Feist6714a252018-09-10 15:26:18 -0700383}
384
James Feistdc6c55f2018-10-31 12:53:20 -0700385void createRedundancySensor(
386 const boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
387 sensors,
388 std::shared_ptr<sdbusplus::asio::connection> conn,
389 sdbusplus::asio::object_server& objectServer)
390{
391
392 conn->async_method_call(
393 [&objectServer, &sensors](boost::system::error_code& ec,
394 const ManagedObjectType managedObj) {
395 if (ec)
396 {
397 std::cerr << "Error calling entity manager \n";
398 return;
399 }
400 for (const auto& pathPair : managedObj)
401 {
402 for (const auto& interfacePair : pathPair.second)
403 {
404 if (interfacePair.first == redundancyConfiguration)
405 {
406 // currently only support one
407 auto findCount =
408 interfacePair.second.find("AllowedFailures");
409 if (findCount == interfacePair.second.end())
410 {
411 std::cerr << "Malformed redundancy record \n";
412 return;
413 }
414 std::vector<std::string> sensorList;
415
416 for (const auto& sensor : sensors)
417 {
418 sensorList.push_back(
419 "/xyz/openbmc_project/sensors/fan_tach/" +
420 sensor.second->name);
421 }
James Feist7b18b1e2019-05-14 13:42:09 -0700422 systemRedundancy.reset();
423 systemRedundancy.emplace(RedundancySensor(
James Feist3eb82622019-02-08 13:10:22 -0800424 std::get<uint64_t>(findCount->second), sensorList,
James Feist7b18b1e2019-05-14 13:42:09 -0700425 objectServer, pathPair.first));
James Feistdc6c55f2018-10-31 12:53:20 -0700426
427 return;
428 }
429 }
430 }
431 },
432 "xyz.openbmc_project.EntityManager", "/",
433 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
434}
435
James Feistb6c0b912019-07-09 12:21:44 -0700436int main()
James Feist6714a252018-09-10 15:26:18 -0700437{
438 boost::asio::io_service io;
439 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
440 systemBus->request_name("xyz.openbmc_project.FanSensor");
441 sdbusplus::asio::object_server objectServer(systemBus);
442 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>
443 tachSensors;
444 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
445 pwmSensors;
446 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700447 auto sensorsChanged =
448 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700449
450 io.post([&]() {
451 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
452 nullptr);
James Feistdc6c55f2018-10-31 12:53:20 -0700453 createRedundancySensor(tachSensors, systemBus, objectServer);
James Feist6714a252018-09-10 15:26:18 -0700454 });
455
456 boost::asio::deadline_timer filterTimer(io);
457 std::function<void(sdbusplus::message::message&)> eventHandler =
458 [&](sdbusplus::message::message& message) {
459 if (message.is_method_error())
460 {
461 std::cerr << "callback method error\n";
462 return;
463 }
464 sensorsChanged->insert(message.get_path());
465 // this implicitly cancels the timer
466 filterTimer.expires_from_now(boost::posix_time::seconds(1));
467
468 filterTimer.async_wait([&](const boost::system::error_code& ec) {
469 if (ec == boost::asio::error::operation_aborted)
470 {
471 /* we were canceled*/
472 return;
473 }
474 else if (ec)
475 {
476 std::cerr << "timer error\n";
477 return;
478 }
479 createSensors(io, objectServer, tachSensors, pwmSensors,
James Feistf27a55c2020-08-04 14:27:30 -0700480 systemBus, sensorsChanged, 5);
James Feist6714a252018-09-10 15:26:18 -0700481 });
482 };
483
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700484 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700485 {
486 auto match = std::make_unique<sdbusplus::bus::match::match>(
487 static_cast<sdbusplus::bus::bus&>(*systemBus),
488 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700489 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700490 eventHandler);
491 matches.emplace_back(std::move(match));
492 }
493
James Feistdc6c55f2018-10-31 12:53:20 -0700494 // redundancy sensor
495 std::function<void(sdbusplus::message::message&)> redundancyHandler =
496 [&tachSensors, &systemBus,
James Feistb6c0b912019-07-09 12:21:44 -0700497 &objectServer](sdbusplus::message::message&) {
James Feistdc6c55f2018-10-31 12:53:20 -0700498 createRedundancySensor(tachSensors, systemBus, objectServer);
499 };
500 auto match = std::make_unique<sdbusplus::bus::match::match>(
501 static_cast<sdbusplus::bus::bus&>(*systemBus),
502 "type='signal',member='PropertiesChanged',path_namespace='" +
503 std::string(inventoryPath) + "',arg0namespace='" +
504 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700505 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700506 matches.emplace_back(std::move(match));
507
James Feist6714a252018-09-10 15:26:18 -0700508 io.run();
509}