blob: 2e7442d59d9118ab70fe62ea4fcf5df22a5ccd9c [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
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +000047// The following two structures need to be consistent
Peter Lundgren8843b622019-09-12 10:33:41 -070048static constexpr std::array<const char*, 3> sensorTypes = {
James Feist95b079b2018-11-21 09:28:00 -080049 "xyz.openbmc_project.Configuration.AspeedFan",
Peter Lundgren8843b622019-09-12 10:33:41 -070050 "xyz.openbmc_project.Configuration.I2CFan",
51 "xyz.openbmc_project.Configuration.NuvotonFan"};
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +000052
53enum FanTypes
54{
55 aspeed = 0,
56 i2c,
57 nuvoton,
58 max,
59};
60
61static_assert(std::tuple_size<decltype(sensorTypes)>::value == FanTypes::max,
62 "sensorTypes element number is not equal to FanTypes number");
63
James Feistdc6c55f2018-10-31 12:53:20 -070064constexpr const char* redundancyConfiguration =
65 "xyz.openbmc_project.Configuration.FanRedundancy";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070066static std::regex inputRegex(R"(fan(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070067
James Feistdc6c55f2018-10-31 12:53:20 -070068// todo: power supply fan redundancy
James Feist7b18b1e2019-05-14 13:42:09 -070069std::optional<RedundancySensor> systemRedundancy;
James Feist95b079b2018-11-21 09:28:00 -080070
71FanTypes getFanType(const fs::path& parentPath)
72{
73 fs::path linkPath = parentPath / "device";
74 std::string canonical = fs::read_symlink(linkPath);
Jae Hyun Yoo241356e2020-02-20 12:48:04 -080075 if (boost::ends_with(canonical, "1e786000.pwm-tacho-controller") ||
76 boost::ends_with(canonical, "1e610000.pwm-tacho-controller"))
James Feist95b079b2018-11-21 09:28:00 -080077 {
78 return FanTypes::aspeed;
79 }
Peter Lundgren8843b622019-09-12 10:33:41 -070080 else if (boost::ends_with(canonical, "f0103000.pwm-fan-controller"))
81 {
82 return FanTypes::nuvoton;
83 }
James Feist95b079b2018-11-21 09:28:00 -080084 // todo: will we need to support other types?
85 return FanTypes::i2c;
86}
James Feistdc6c55f2018-10-31 12:53:20 -070087
Kuiying Wangd5407412020-09-09 16:06:56 +080088void createRedundancySensor(
89 const boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
90 sensors,
91 std::shared_ptr<sdbusplus::asio::connection> conn,
92 sdbusplus::asio::object_server& objectServer)
93{
94
95 conn->async_method_call(
96 [&objectServer, &sensors](boost::system::error_code& ec,
97 const ManagedObjectType managedObj) {
98 if (ec)
99 {
100 std::cerr << "Error calling entity manager \n";
101 return;
102 }
103 for (const auto& pathPair : managedObj)
104 {
105 for (const auto& interfacePair : pathPair.second)
106 {
107 if (interfacePair.first == redundancyConfiguration)
108 {
109 // currently only support one
110 auto findCount =
111 interfacePair.second.find("AllowedFailures");
112 if (findCount == interfacePair.second.end())
113 {
114 std::cerr << "Malformed redundancy record \n";
115 return;
116 }
117 std::vector<std::string> sensorList;
118
119 for (const auto& sensor : sensors)
120 {
121 sensorList.push_back(
122 "/xyz/openbmc_project/sensors/fan_tach/" +
123 sensor.second->name);
124 }
125 systemRedundancy.reset();
126 systemRedundancy.emplace(RedundancySensor(
127 std::get<uint64_t>(findCount->second), sensorList,
128 objectServer, pathPair.first));
129
130 return;
131 }
132 }
133 }
134 },
135 "xyz.openbmc_project.EntityManager", "/",
136 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
137}
138
James Feist6714a252018-09-10 15:26:18 -0700139void createSensors(
140 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
141 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
142 tachSensors,
143 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
144 pwmSensors,
145 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700146 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feistf27a55c2020-08-04 14:27:30 -0700147 sensorsChanged,
148 size_t retries = 0)
James Feist6714a252018-09-10 15:26:18 -0700149{
James Feistde5e9702019-09-18 16:13:02 -0700150 auto getter = std::make_shared<GetSensorConfiguration>(
151 dbusConnection,
152 std::move([&io, &objectServer, &tachSensors, &pwmSensors,
James Feist5591cf082020-07-15 16:44:54 -0700153 &dbusConnection, sensorsChanged](
James Feistde5e9702019-09-18 16:13:02 -0700154 const ManagedObjectType& sensorConfigurations) {
155 bool firstScan = sensorsChanged == nullptr;
156 std::vector<fs::path> paths;
157 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)",
158 paths))
James Feist95b079b2018-11-21 09:28:00 -0800159 {
Yong Zhao77b3add2021-01-09 04:25:18 +0000160 std::cerr << "No fan sensors in system\n";
James Feistde5e9702019-09-18 16:13:02 -0700161 return;
James Feist95b079b2018-11-21 09:28:00 -0800162 }
James Feist6714a252018-09-10 15:26:18 -0700163
James Feistde5e9702019-09-18 16:13:02 -0700164 // iterate through all found fan sensors, and try to match them with
165 // configuration
166 for (const auto& path : paths)
James Feist6714a252018-09-10 15:26:18 -0700167 {
James Feistde5e9702019-09-18 16:13:02 -0700168 std::smatch match;
169 std::string pathStr = path.string();
170
171 std::regex_search(pathStr, match, inputRegex);
172 std::string indexStr = *(match.begin() + 1);
173
Yong Zhao77b3add2021-01-09 04:25:18 +0000174 fs::path directory = path.parent_path();
175 fs::path pwmPath = directory / ("pwm" + indexStr);
James Feistde5e9702019-09-18 16:13:02 -0700176 FanTypes fanType = getFanType(directory);
Yong Zhao77b3add2021-01-09 04:25:18 +0000177
James Feistde5e9702019-09-18 16:13:02 -0700178 size_t bus = 0;
179 size_t address = 0;
180 if (fanType == FanTypes::i2c)
James Feist6714a252018-09-10 15:26:18 -0700181 {
James Feistde5e9702019-09-18 16:13:02 -0700182 std::string link =
183 fs::read_symlink(directory / "device").filename();
184
185 size_t findDash = link.find("-");
186 if (findDash == std::string::npos ||
187 link.size() <= findDash + 1)
188 {
189 std::cerr << "Error finding device from symlink";
190 }
191 bus = std::stoi(link.substr(0, findDash));
192 address = std::stoi(link.substr(findDash + 1), nullptr, 16);
James Feist6714a252018-09-10 15:26:18 -0700193 }
James Feistde5e9702019-09-18 16:13:02 -0700194 // convert to 0 based
195 size_t index = std::stoul(indexStr) - 1;
196
197 const char* baseType;
198 const SensorData* sensorData = nullptr;
199 const std::string* interfacePath = nullptr;
200 const SensorBaseConfiguration* baseConfiguration = nullptr;
201 for (const std::pair<sdbusplus::message::object_path,
202 SensorData>& sensor : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800203 {
James Feistde5e9702019-09-18 16:13:02 -0700204 // find the base of the configuration to see if indexes
205 // match
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +0000206 auto sensorBaseFind =
207 sensor.second.find(sensorTypes[fanType]);
208 if (sensorBaseFind == sensor.second.end())
James Feistde5e9702019-09-18 16:13:02 -0700209 {
210 continue;
211 }
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800212
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +0000213 baseConfiguration = &(*sensorBaseFind);
214 interfacePath = &(sensor.first.str);
215 baseType = sensorTypes[fanType];
216
James Feistde5e9702019-09-18 16:13:02 -0700217 auto findIndex = baseConfiguration->second.find("Index");
218 if (findIndex == baseConfiguration->second.end())
219 {
220 std::cerr << baseConfiguration->first
221 << " missing index\n";
222 continue;
223 }
224 unsigned int configIndex = std::visit(
225 VariantToUnsignedIntVisitor(), findIndex->second);
226 if (configIndex != index)
227 {
228 continue;
229 }
230 if (fanType == FanTypes::aspeed ||
231 fanType == FanTypes::nuvoton)
232 {
233 // there will be only 1 aspeed or nuvoton sensor object
234 // in sysfs, we found the fan
235 sensorData = &(sensor.second);
236 break;
237 }
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +0000238 else if (fanType == FanTypes::i2c)
James Feistde5e9702019-09-18 16:13:02 -0700239 {
240 auto findBus = baseConfiguration->second.find("Bus");
241 auto findAddress =
242 baseConfiguration->second.find("Address");
243 if (findBus == baseConfiguration->second.end() ||
244 findAddress == baseConfiguration->second.end())
245 {
246 std::cerr << baseConfiguration->first
247 << " missing bus or address\n";
248 continue;
249 }
250 unsigned int configBus = std::visit(
251 VariantToUnsignedIntVisitor(), findBus->second);
252 unsigned int configAddress = std::visit(
253 VariantToUnsignedIntVisitor(), findAddress->second);
254
255 if (configBus == bus && configAddress == address)
256 {
257 sensorData = &(sensor.second);
258 break;
259 }
260 }
261 }
262 if (sensorData == nullptr)
263 {
264 std::cerr << "failed to find match for " << path.string()
265 << "\n";
James Feist95b079b2018-11-21 09:28:00 -0800266 continue;
267 }
James Feist95b079b2018-11-21 09:28:00 -0800268
James Feistde5e9702019-09-18 16:13:02 -0700269 auto findSensorName = baseConfiguration->second.find("Name");
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800270
James Feistde5e9702019-09-18 16:13:02 -0700271 if (findSensorName == baseConfiguration->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800272 {
James Feistde5e9702019-09-18 16:13:02 -0700273 std::cerr << "could not determine configuration name for "
274 << path.string() << "\n";
275 continue;
276 }
277 std::string sensorName =
278 std::get<std::string>(findSensorName->second);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800279
James Feistde5e9702019-09-18 16:13:02 -0700280 // on rescans, only update sensors we were signaled by
281 auto findSensor = tachSensors.find(sensorName);
282 if (!firstScan && findSensor != tachSensors.end())
283 {
284 bool found = false;
285 for (auto it = sensorsChanged->begin();
286 it != sensorsChanged->end(); it++)
287 {
288 if (boost::ends_with(*it, findSensor->second->name))
289 {
290 sensorsChanged->erase(it);
291 findSensor->second = nullptr;
292 found = true;
293 break;
294 }
295 }
296 if (!found)
297 {
298 continue;
299 }
300 }
301 std::vector<thresholds::Threshold> sensorThresholds;
302 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
303 {
304 std::cerr << "error populating thresholds for "
305 << sensorName << "\n";
306 }
307
308 auto presenceConfig =
309 sensorData->find(baseType + std::string(".Presence"));
310
311 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
312
313 // presence sensors are optional
314 if (presenceConfig != sensorData->end())
315 {
James Feistde5e9702019-09-18 16:13:02 -0700316 auto findPolarity = presenceConfig->second.find("Polarity");
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800317 auto findPinName = presenceConfig->second.find("PinName");
James Feistde5e9702019-09-18 16:13:02 -0700318
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800319 if (findPinName == presenceConfig->second.end() ||
James Feistde5e9702019-09-18 16:13:02 -0700320 findPolarity == presenceConfig->second.end())
321 {
322 std::cerr << "Malformed Presence Configuration\n";
323 }
324 else
325 {
James Feistde5e9702019-09-18 16:13:02 -0700326 bool inverted = std::get<std::string>(
327 findPolarity->second) == "Low";
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800328 if (auto pinName =
329 std::get_if<std::string>(&findPinName->second))
330 {
331 presenceSensor = std::make_unique<PresenceSensor>(
332 *pinName, inverted, io, sensorName);
333 }
334 else
335 {
336 std::cerr
337 << "Malformed Presence pinName for sensor "
338 << sensorName << " \n";
339 }
James Feistde5e9702019-09-18 16:13:02 -0700340 }
341 }
342 std::optional<RedundancySensor>* redundancy = nullptr;
343 if (fanType == FanTypes::aspeed)
344 {
345 redundancy = &systemRedundancy;
346 }
347
Josh Lehanf920e092020-08-07 00:12:54 -0700348 PowerState powerState = PowerState::on;
349 auto findPower = baseConfiguration->second.find("PowerState");
350 if (findPower != baseConfiguration->second.end())
351 {
352 auto ptrPower =
353 std::get_if<std::string>(&(findPower->second));
354 if (ptrPower)
355 {
356 setReadState(*ptrPower, powerState);
357 }
358 }
359
James Feistde5e9702019-09-18 16:13:02 -0700360 constexpr double defaultMaxReading = 25000;
361 constexpr double defaultMinReading = 0;
362 auto limits =
363 std::make_pair(defaultMinReading, defaultMaxReading);
364
James Feist49a8ccd2020-09-16 16:09:52 -0700365 auto connector =
366 sensorData->find(baseType + std::string(".Connector"));
367
368 std::optional<std::string> led;
Yong Zhao77b3add2021-01-09 04:25:18 +0000369 std::string pwmName;
James Feist49a8ccd2020-09-16 16:09:52 -0700370
371 if (connector != sensorData->end())
372 {
373 auto findPwm = connector->second.find("Pwm");
374 if (findPwm != connector->second.end())
375 {
376
377 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
378 findPwm->second);
379 /* use pwm name override if found in configuration else
380 * use default */
381 auto findOverride = connector->second.find("PwmName");
James Feist49a8ccd2020-09-16 16:09:52 -0700382 if (findOverride != connector->second.end())
383 {
384 pwmName = std::visit(VariantToStringVisitor(),
385 findOverride->second);
386 }
387 else
388 {
389 pwmName = "Pwm_" + std::to_string(pwm + 1);
390 }
James Feist49a8ccd2020-09-16 16:09:52 -0700391 }
392 else
393 {
394 std::cerr << "Connector for " << sensorName
395 << " missing pwm!\n";
396 }
397
398 auto findLED = connector->second.find("LED");
399 if (findLED != connector->second.end())
400 {
401 auto ledName =
402 std::get_if<std::string>(&(findLED->second));
403 if (ledName == nullptr)
404 {
405 std::cerr << "Wrong format for LED of "
406 << sensorName << "\n";
407 }
408 else
409 {
410 led = *ledName;
411 }
412 }
413 }
414
James Feistde5e9702019-09-18 16:13:02 -0700415 findLimits(limits, baseConfiguration);
416 tachSensors[sensorName] = std::make_unique<TachSensor>(
417 path.string(), baseType, objectServer, dbusConnection,
418 std::move(presenceSensor), redundancy, io, sensorName,
Josh Lehanf920e092020-08-07 00:12:54 -0700419 std::move(sensorThresholds), *interfacePath, limits,
James Feist49a8ccd2020-09-16 16:09:52 -0700420 powerState, led);
Yong Zhao77b3add2021-01-09 04:25:18 +0000421
422 if (fs::exists(pwmPath) && !pwmSensors.count(pwmPath))
423 {
424 pwmSensors[pwmPath] = std::make_unique<PwmSensor>(
425 pwmName, pwmPath, dbusConnection, objectServer,
426 *interfacePath, "Fan");
427 }
James Feist95b079b2018-11-21 09:28:00 -0800428 }
Yong Zhao77b3add2021-01-09 04:25:18 +0000429
Kuiying Wangd5407412020-09-09 16:06:56 +0800430 createRedundancySensor(tachSensors, dbusConnection, objectServer);
James Feistde5e9702019-09-18 16:13:02 -0700431 }));
432 getter->getConfiguration(
James Feistf27a55c2020-08-04 14:27:30 -0700433 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()},
434 retries);
James Feist6714a252018-09-10 15:26:18 -0700435}
436
James Feistb6c0b912019-07-09 12:21:44 -0700437int main()
James Feist6714a252018-09-10 15:26:18 -0700438{
439 boost::asio::io_service io;
440 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
441 systemBus->request_name("xyz.openbmc_project.FanSensor");
442 sdbusplus::asio::object_server objectServer(systemBus);
443 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>
444 tachSensors;
445 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
446 pwmSensors;
447 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700448 auto sensorsChanged =
449 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700450
451 io.post([&]() {
452 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
453 nullptr);
454 });
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}