blob: 9f78fc723f196d6699815e566d24f16036ad260e [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,
89 const std::unique_ptr<boost::container::flat_set<std::string>>&
90 sensorsChanged)
91{
James Feist6714a252018-09-10 15:26:18 -070092
James Feistde5e9702019-09-18 16:13:02 -070093 auto getter = std::make_shared<GetSensorConfiguration>(
94 dbusConnection,
95 std::move([&io, &objectServer, &tachSensors, &pwmSensors,
96 &dbusConnection, &sensorsChanged](
97 const ManagedObjectType& sensorConfigurations) {
98 bool firstScan = sensorsChanged == nullptr;
99 std::vector<fs::path> paths;
100 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)",
101 paths))
James Feist95b079b2018-11-21 09:28:00 -0800102 {
James Feistde5e9702019-09-18 16:13:02 -0700103 std::cerr << "No temperature sensors in system\n";
104 return;
James Feist95b079b2018-11-21 09:28:00 -0800105 }
James Feist6714a252018-09-10 15:26:18 -0700106
James Feistde5e9702019-09-18 16:13:02 -0700107 std::vector<std::pair<uint8_t, std::string>> pwmNumbers;
108
109 // iterate through all found fan sensors, and try to match them with
110 // configuration
111 for (const auto& path : paths)
James Feist6714a252018-09-10 15:26:18 -0700112 {
James Feistde5e9702019-09-18 16:13:02 -0700113 std::smatch match;
114 std::string pathStr = path.string();
115
116 std::regex_search(pathStr, match, inputRegex);
117 std::string indexStr = *(match.begin() + 1);
118
119 auto directory = path.parent_path();
120 FanTypes fanType = getFanType(directory);
121 size_t bus = 0;
122 size_t address = 0;
123 if (fanType == FanTypes::i2c)
James Feist6714a252018-09-10 15:26:18 -0700124 {
James Feistde5e9702019-09-18 16:13:02 -0700125 std::string link =
126 fs::read_symlink(directory / "device").filename();
127
128 size_t findDash = link.find("-");
129 if (findDash == std::string::npos ||
130 link.size() <= findDash + 1)
131 {
132 std::cerr << "Error finding device from symlink";
133 }
134 bus = std::stoi(link.substr(0, findDash));
135 address = std::stoi(link.substr(findDash + 1), nullptr, 16);
James Feist6714a252018-09-10 15:26:18 -0700136 }
James Feistde5e9702019-09-18 16:13:02 -0700137 // convert to 0 based
138 size_t index = std::stoul(indexStr) - 1;
139
140 const char* baseType;
141 const SensorData* sensorData = nullptr;
142 const std::string* interfacePath = nullptr;
143 const SensorBaseConfiguration* baseConfiguration = nullptr;
144 for (const std::pair<sdbusplus::message::object_path,
145 SensorData>& sensor : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800146 {
James Feistde5e9702019-09-18 16:13:02 -0700147 // find the base of the configuration to see if indexes
148 // match
149 for (const char* type : sensorTypes)
150 {
151 auto sensorBaseFind = sensor.second.find(type);
152 if (sensorBaseFind != sensor.second.end())
153 {
154 baseConfiguration = &(*sensorBaseFind);
155 interfacePath = &(sensor.first.str);
156 baseType = type;
157 break;
158 }
159 }
160 if (baseConfiguration == nullptr)
161 {
162 continue;
163 }
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800164
James Feistde5e9702019-09-18 16:13:02 -0700165 auto findIndex = baseConfiguration->second.find("Index");
166 if (findIndex == baseConfiguration->second.end())
167 {
168 std::cerr << baseConfiguration->first
169 << " missing index\n";
170 continue;
171 }
172 unsigned int configIndex = std::visit(
173 VariantToUnsignedIntVisitor(), findIndex->second);
174 if (configIndex != index)
175 {
176 continue;
177 }
178 if (fanType == FanTypes::aspeed ||
179 fanType == FanTypes::nuvoton)
180 {
181 // there will be only 1 aspeed or nuvoton sensor object
182 // in sysfs, we found the fan
183 sensorData = &(sensor.second);
184 break;
185 }
186 else if (baseType ==
187 std::string(
188 "xyz.openbmc_project.Configuration.I2CFan"))
189 {
190 auto findBus = baseConfiguration->second.find("Bus");
191 auto findAddress =
192 baseConfiguration->second.find("Address");
193 if (findBus == baseConfiguration->second.end() ||
194 findAddress == baseConfiguration->second.end())
195 {
196 std::cerr << baseConfiguration->first
197 << " missing bus or address\n";
198 continue;
199 }
200 unsigned int configBus = std::visit(
201 VariantToUnsignedIntVisitor(), findBus->second);
202 unsigned int configAddress = std::visit(
203 VariantToUnsignedIntVisitor(), findAddress->second);
204
205 if (configBus == bus && configAddress == address)
206 {
207 sensorData = &(sensor.second);
208 break;
209 }
210 }
211 }
212 if (sensorData == nullptr)
213 {
214 std::cerr << "failed to find match for " << path.string()
215 << "\n";
James Feist95b079b2018-11-21 09:28:00 -0800216 continue;
217 }
James Feist95b079b2018-11-21 09:28:00 -0800218
James Feistde5e9702019-09-18 16:13:02 -0700219 auto findSensorName = baseConfiguration->second.find("Name");
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800220
James Feistde5e9702019-09-18 16:13:02 -0700221 if (findSensorName == baseConfiguration->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800222 {
James Feistde5e9702019-09-18 16:13:02 -0700223 std::cerr << "could not determine configuration name for "
224 << path.string() << "\n";
225 continue;
226 }
227 std::string sensorName =
228 std::get<std::string>(findSensorName->second);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800229
James Feistde5e9702019-09-18 16:13:02 -0700230 // on rescans, only update sensors we were signaled by
231 auto findSensor = tachSensors.find(sensorName);
232 if (!firstScan && findSensor != tachSensors.end())
233 {
234 bool found = false;
235 for (auto it = sensorsChanged->begin();
236 it != sensorsChanged->end(); it++)
237 {
238 if (boost::ends_with(*it, findSensor->second->name))
239 {
240 sensorsChanged->erase(it);
241 findSensor->second = nullptr;
242 found = true;
243 break;
244 }
245 }
246 if (!found)
247 {
248 continue;
249 }
250 }
251 std::vector<thresholds::Threshold> sensorThresholds;
252 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
253 {
254 std::cerr << "error populating thresholds for "
255 << sensorName << "\n";
256 }
257
258 auto presenceConfig =
259 sensorData->find(baseType + std::string(".Presence"));
260
261 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
262
263 // presence sensors are optional
264 if (presenceConfig != sensorData->end())
265 {
James Feistde5e9702019-09-18 16:13:02 -0700266 auto findPolarity = presenceConfig->second.find("Polarity");
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800267 auto findPinName = presenceConfig->second.find("PinName");
James Feistde5e9702019-09-18 16:13:02 -0700268
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800269 if (findPinName == presenceConfig->second.end() ||
James Feistde5e9702019-09-18 16:13:02 -0700270 findPolarity == presenceConfig->second.end())
271 {
272 std::cerr << "Malformed Presence Configuration\n";
273 }
274 else
275 {
James Feistde5e9702019-09-18 16:13:02 -0700276 bool inverted = std::get<std::string>(
277 findPolarity->second) == "Low";
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800278 if (auto pinName =
279 std::get_if<std::string>(&findPinName->second))
280 {
281 presenceSensor = std::make_unique<PresenceSensor>(
282 *pinName, inverted, io, sensorName);
283 }
284 else
285 {
286 std::cerr
287 << "Malformed Presence pinName for sensor "
288 << sensorName << " \n";
289 }
James Feistde5e9702019-09-18 16:13:02 -0700290 }
291 }
292 std::optional<RedundancySensor>* redundancy = nullptr;
293 if (fanType == FanTypes::aspeed)
294 {
295 redundancy = &systemRedundancy;
296 }
297
298 constexpr double defaultMaxReading = 25000;
299 constexpr double defaultMinReading = 0;
300 auto limits =
301 std::make_pair(defaultMinReading, defaultMaxReading);
302
303 findLimits(limits, baseConfiguration);
304 tachSensors[sensorName] = std::make_unique<TachSensor>(
305 path.string(), baseType, objectServer, dbusConnection,
306 std::move(presenceSensor), redundancy, io, sensorName,
307 std::move(sensorThresholds), *interfacePath, limits);
308
309 auto connector =
310 sensorData->find(baseType + std::string(".Connector"));
311 if (connector != sensorData->end())
312 {
313 auto findPwm = connector->second.find("Pwm");
314 if (findPwm == connector->second.end())
315 {
316 std::cerr << "Connector Missing PWM!\n";
317 continue;
318 }
319
320 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
321 findPwm->second);
322 pwmNumbers.emplace_back(pwm, *interfacePath);
James Feist95b079b2018-11-21 09:28:00 -0800323 }
324 }
James Feistde5e9702019-09-18 16:13:02 -0700325 std::vector<fs::path> pwms;
326 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+$)", pwms))
James Feist6714a252018-09-10 15:26:18 -0700327 {
James Feistde5e9702019-09-18 16:13:02 -0700328 std::cerr << "No pwm in system\n";
329 return;
330 }
331 for (const fs::path& pwm : pwms)
332 {
333 if (pwmSensors.find(pwm) != pwmSensors.end())
James Feist6714a252018-09-10 15:26:18 -0700334 {
James Feistde5e9702019-09-18 16:13:02 -0700335 continue;
James Feist6714a252018-09-10 15:26:18 -0700336 }
James Feistde5e9702019-09-18 16:13:02 -0700337 const std::string* path = nullptr;
338 for (const auto& [index, configPath] : pwmNumbers)
339 {
340 if (boost::ends_with(pwm.string(),
341 std::to_string(index + 1)))
342 {
343 path = &configPath;
344 break;
345 }
346 }
347
348 if (path == nullptr)
349 {
350 continue;
351 }
352
353 // only add new elements
354 const std::string& sysPath = pwm.string();
355 const std::string& pwmName =
356 "Pwm_" + sysPath.substr(sysPath.find_last_of("pwm") + 1);
357 pwmSensors.insert(
358 std::pair<std::string, std::unique_ptr<PwmSensor>>(
359 sysPath, std::make_unique<PwmSensor>(
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530360 pwmName, sysPath, dbusConnection,
361 objectServer, *path, "Fan")));
James Feist6714a252018-09-10 15:26:18 -0700362 }
James Feistde5e9702019-09-18 16:13:02 -0700363 }));
364 getter->getConfiguration(
365 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()});
James Feist6714a252018-09-10 15:26:18 -0700366}
367
James Feistdc6c55f2018-10-31 12:53:20 -0700368void createRedundancySensor(
369 const boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
370 sensors,
371 std::shared_ptr<sdbusplus::asio::connection> conn,
372 sdbusplus::asio::object_server& objectServer)
373{
374
375 conn->async_method_call(
376 [&objectServer, &sensors](boost::system::error_code& ec,
377 const ManagedObjectType managedObj) {
378 if (ec)
379 {
380 std::cerr << "Error calling entity manager \n";
381 return;
382 }
383 for (const auto& pathPair : managedObj)
384 {
385 for (const auto& interfacePair : pathPair.second)
386 {
387 if (interfacePair.first == redundancyConfiguration)
388 {
389 // currently only support one
390 auto findCount =
391 interfacePair.second.find("AllowedFailures");
392 if (findCount == interfacePair.second.end())
393 {
394 std::cerr << "Malformed redundancy record \n";
395 return;
396 }
397 std::vector<std::string> sensorList;
398
399 for (const auto& sensor : sensors)
400 {
401 sensorList.push_back(
402 "/xyz/openbmc_project/sensors/fan_tach/" +
403 sensor.second->name);
404 }
James Feist7b18b1e2019-05-14 13:42:09 -0700405 systemRedundancy.reset();
406 systemRedundancy.emplace(RedundancySensor(
James Feist3eb82622019-02-08 13:10:22 -0800407 std::get<uint64_t>(findCount->second), sensorList,
James Feist7b18b1e2019-05-14 13:42:09 -0700408 objectServer, pathPair.first));
James Feistdc6c55f2018-10-31 12:53:20 -0700409
410 return;
411 }
412 }
413 }
414 },
415 "xyz.openbmc_project.EntityManager", "/",
416 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
417}
418
James Feistb6c0b912019-07-09 12:21:44 -0700419int main()
James Feist6714a252018-09-10 15:26:18 -0700420{
421 boost::asio::io_service io;
422 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
423 systemBus->request_name("xyz.openbmc_project.FanSensor");
424 sdbusplus::asio::object_server objectServer(systemBus);
425 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>
426 tachSensors;
427 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
428 pwmSensors;
429 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
430 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
431 std::make_unique<boost::container::flat_set<std::string>>();
432
433 io.post([&]() {
434 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
435 nullptr);
James Feistdc6c55f2018-10-31 12:53:20 -0700436 createRedundancySensor(tachSensors, systemBus, objectServer);
James Feist6714a252018-09-10 15:26:18 -0700437 });
438
439 boost::asio::deadline_timer filterTimer(io);
440 std::function<void(sdbusplus::message::message&)> eventHandler =
441 [&](sdbusplus::message::message& message) {
442 if (message.is_method_error())
443 {
444 std::cerr << "callback method error\n";
445 return;
446 }
447 sensorsChanged->insert(message.get_path());
448 // this implicitly cancels the timer
449 filterTimer.expires_from_now(boost::posix_time::seconds(1));
450
451 filterTimer.async_wait([&](const boost::system::error_code& ec) {
452 if (ec == boost::asio::error::operation_aborted)
453 {
454 /* we were canceled*/
455 return;
456 }
457 else if (ec)
458 {
459 std::cerr << "timer error\n";
460 return;
461 }
462 createSensors(io, objectServer, tachSensors, pwmSensors,
463 systemBus, sensorsChanged);
464 });
465 };
466
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700467 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700468 {
469 auto match = std::make_unique<sdbusplus::bus::match::match>(
470 static_cast<sdbusplus::bus::bus&>(*systemBus),
471 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700472 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700473 eventHandler);
474 matches.emplace_back(std::move(match));
475 }
476
James Feistdc6c55f2018-10-31 12:53:20 -0700477 // redundancy sensor
478 std::function<void(sdbusplus::message::message&)> redundancyHandler =
479 [&tachSensors, &systemBus,
James Feistb6c0b912019-07-09 12:21:44 -0700480 &objectServer](sdbusplus::message::message&) {
James Feistdc6c55f2018-10-31 12:53:20 -0700481 createRedundancySensor(tachSensors, systemBus, objectServer);
482 };
483 auto match = std::make_unique<sdbusplus::bus::match::match>(
484 static_cast<sdbusplus::bus::bus&>(*systemBus),
485 "type='signal',member='PropertiesChanged',path_namespace='" +
486 std::string(inventoryPath) + "',arg0namespace='" +
487 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700488 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700489 matches.emplace_back(std::move(match));
490
James Feist6714a252018-09-10 15:26:18 -0700491 io.run();
492}