blob: 73937ccd5113985212ea0381e5afe241fc932046 [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 Feist6714a252018-09-10 15:26:18 -070090 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,
James Feist5591cf082020-07-15 16:44:54 -070096 &dbusConnection, sensorsChanged](
James Feistde5e9702019-09-18 16:13:02 -070097 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
Jason Lingd320a2e2020-07-11 14:08:14 -0700107 // pwm index, sysfs path, pwm name
108 std::vector<std::tuple<uint8_t, std::string, std::string>>
109 pwmNumbers;
James Feistde5e9702019-09-18 16:13:02 -0700110
111 // iterate through all found fan sensors, and try to match them with
112 // configuration
113 for (const auto& path : paths)
James Feist6714a252018-09-10 15:26:18 -0700114 {
James Feistde5e9702019-09-18 16:13:02 -0700115 std::smatch match;
116 std::string pathStr = path.string();
117
118 std::regex_search(pathStr, match, inputRegex);
119 std::string indexStr = *(match.begin() + 1);
120
121 auto directory = path.parent_path();
122 FanTypes fanType = getFanType(directory);
123 size_t bus = 0;
124 size_t address = 0;
125 if (fanType == FanTypes::i2c)
James Feist6714a252018-09-10 15:26:18 -0700126 {
James Feistde5e9702019-09-18 16:13:02 -0700127 std::string link =
128 fs::read_symlink(directory / "device").filename();
129
130 size_t findDash = link.find("-");
131 if (findDash == std::string::npos ||
132 link.size() <= findDash + 1)
133 {
134 std::cerr << "Error finding device from symlink";
135 }
136 bus = std::stoi(link.substr(0, findDash));
137 address = std::stoi(link.substr(findDash + 1), nullptr, 16);
James Feist6714a252018-09-10 15:26:18 -0700138 }
James Feistde5e9702019-09-18 16:13:02 -0700139 // convert to 0 based
140 size_t index = std::stoul(indexStr) - 1;
141
142 const char* baseType;
143 const SensorData* sensorData = nullptr;
144 const std::string* interfacePath = nullptr;
145 const SensorBaseConfiguration* baseConfiguration = nullptr;
146 for (const std::pair<sdbusplus::message::object_path,
147 SensorData>& sensor : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800148 {
James Feistde5e9702019-09-18 16:13:02 -0700149 // find the base of the configuration to see if indexes
150 // match
151 for (const char* type : sensorTypes)
152 {
153 auto sensorBaseFind = sensor.second.find(type);
154 if (sensorBaseFind != sensor.second.end())
155 {
156 baseConfiguration = &(*sensorBaseFind);
157 interfacePath = &(sensor.first.str);
158 baseType = type;
159 break;
160 }
161 }
162 if (baseConfiguration == nullptr)
163 {
164 continue;
165 }
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800166
James Feistde5e9702019-09-18 16:13:02 -0700167 auto findIndex = baseConfiguration->second.find("Index");
168 if (findIndex == baseConfiguration->second.end())
169 {
170 std::cerr << baseConfiguration->first
171 << " missing index\n";
172 continue;
173 }
174 unsigned int configIndex = std::visit(
175 VariantToUnsignedIntVisitor(), findIndex->second);
176 if (configIndex != index)
177 {
178 continue;
179 }
180 if (fanType == FanTypes::aspeed ||
181 fanType == FanTypes::nuvoton)
182 {
183 // there will be only 1 aspeed or nuvoton sensor object
184 // in sysfs, we found the fan
185 sensorData = &(sensor.second);
186 break;
187 }
188 else if (baseType ==
189 std::string(
190 "xyz.openbmc_project.Configuration.I2CFan"))
191 {
192 auto findBus = baseConfiguration->second.find("Bus");
193 auto findAddress =
194 baseConfiguration->second.find("Address");
195 if (findBus == baseConfiguration->second.end() ||
196 findAddress == baseConfiguration->second.end())
197 {
198 std::cerr << baseConfiguration->first
199 << " missing bus or address\n";
200 continue;
201 }
202 unsigned int configBus = std::visit(
203 VariantToUnsignedIntVisitor(), findBus->second);
204 unsigned int configAddress = std::visit(
205 VariantToUnsignedIntVisitor(), findAddress->second);
206
207 if (configBus == bus && configAddress == address)
208 {
209 sensorData = &(sensor.second);
210 break;
211 }
212 }
213 }
214 if (sensorData == nullptr)
215 {
216 std::cerr << "failed to find match for " << path.string()
217 << "\n";
James Feist95b079b2018-11-21 09:28:00 -0800218 continue;
219 }
James Feist95b079b2018-11-21 09:28:00 -0800220
James Feistde5e9702019-09-18 16:13:02 -0700221 auto findSensorName = baseConfiguration->second.find("Name");
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800222
James Feistde5e9702019-09-18 16:13:02 -0700223 if (findSensorName == baseConfiguration->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800224 {
James Feistde5e9702019-09-18 16:13:02 -0700225 std::cerr << "could not determine configuration name for "
226 << path.string() << "\n";
227 continue;
228 }
229 std::string sensorName =
230 std::get<std::string>(findSensorName->second);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800231
James Feistde5e9702019-09-18 16:13:02 -0700232 // on rescans, only update sensors we were signaled by
233 auto findSensor = tachSensors.find(sensorName);
234 if (!firstScan && findSensor != tachSensors.end())
235 {
236 bool found = false;
237 for (auto it = sensorsChanged->begin();
238 it != sensorsChanged->end(); it++)
239 {
240 if (boost::ends_with(*it, findSensor->second->name))
241 {
242 sensorsChanged->erase(it);
243 findSensor->second = nullptr;
244 found = true;
245 break;
246 }
247 }
248 if (!found)
249 {
250 continue;
251 }
252 }
253 std::vector<thresholds::Threshold> sensorThresholds;
254 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
255 {
256 std::cerr << "error populating thresholds for "
257 << sensorName << "\n";
258 }
259
260 auto presenceConfig =
261 sensorData->find(baseType + std::string(".Presence"));
262
263 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
264
265 // presence sensors are optional
266 if (presenceConfig != sensorData->end())
267 {
James Feistde5e9702019-09-18 16:13:02 -0700268 auto findPolarity = presenceConfig->second.find("Polarity");
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800269 auto findPinName = presenceConfig->second.find("PinName");
James Feistde5e9702019-09-18 16:13:02 -0700270
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800271 if (findPinName == presenceConfig->second.end() ||
James Feistde5e9702019-09-18 16:13:02 -0700272 findPolarity == presenceConfig->second.end())
273 {
274 std::cerr << "Malformed Presence Configuration\n";
275 }
276 else
277 {
James Feistde5e9702019-09-18 16:13:02 -0700278 bool inverted = std::get<std::string>(
279 findPolarity->second) == "Low";
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800280 if (auto pinName =
281 std::get_if<std::string>(&findPinName->second))
282 {
283 presenceSensor = std::make_unique<PresenceSensor>(
284 *pinName, inverted, io, sensorName);
285 }
286 else
287 {
288 std::cerr
289 << "Malformed Presence pinName for sensor "
290 << sensorName << " \n";
291 }
James Feistde5e9702019-09-18 16:13:02 -0700292 }
293 }
294 std::optional<RedundancySensor>* redundancy = nullptr;
295 if (fanType == FanTypes::aspeed)
296 {
297 redundancy = &systemRedundancy;
298 }
299
300 constexpr double defaultMaxReading = 25000;
301 constexpr double defaultMinReading = 0;
302 auto limits =
303 std::make_pair(defaultMinReading, defaultMaxReading);
304
305 findLimits(limits, baseConfiguration);
306 tachSensors[sensorName] = std::make_unique<TachSensor>(
307 path.string(), baseType, objectServer, dbusConnection,
308 std::move(presenceSensor), redundancy, io, sensorName,
309 std::move(sensorThresholds), *interfacePath, limits);
310
311 auto connector =
312 sensorData->find(baseType + std::string(".Connector"));
313 if (connector != sensorData->end())
314 {
315 auto findPwm = connector->second.find("Pwm");
316 if (findPwm == connector->second.end())
317 {
318 std::cerr << "Connector Missing PWM!\n";
319 continue;
320 }
James Feistde5e9702019-09-18 16:13:02 -0700321 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
322 findPwm->second);
Jason Lingd320a2e2020-07-11 14:08:14 -0700323 /* use pwm name override if found in configuration else use
324 * default */
325 auto findOverride = connector->second.find("PwmName");
326 std::string pwmName;
327 if (findOverride != connector->second.end())
328 {
329 pwmName = std::visit(VariantToStringVisitor(),
330 findOverride->second);
331 }
332 else
333 {
334 pwmName = "Pwm_" + std::to_string(pwm + 1);
335 }
336 pwmNumbers.emplace_back(pwm, *interfacePath, pwmName);
James Feist95b079b2018-11-21 09:28:00 -0800337 }
338 }
James Feistde5e9702019-09-18 16:13:02 -0700339 std::vector<fs::path> pwms;
340 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+$)", pwms))
James Feist6714a252018-09-10 15:26:18 -0700341 {
James Feistde5e9702019-09-18 16:13:02 -0700342 std::cerr << "No pwm in system\n";
343 return;
344 }
345 for (const fs::path& pwm : pwms)
346 {
347 if (pwmSensors.find(pwm) != pwmSensors.end())
James Feist6714a252018-09-10 15:26:18 -0700348 {
James Feistde5e9702019-09-18 16:13:02 -0700349 continue;
James Feist6714a252018-09-10 15:26:18 -0700350 }
James Feistde5e9702019-09-18 16:13:02 -0700351 const std::string* path = nullptr;
Jason Lingd320a2e2020-07-11 14:08:14 -0700352 const std::string* pwmName = nullptr;
353
354 for (const auto& [index, configPath, name] : pwmNumbers)
James Feistde5e9702019-09-18 16:13:02 -0700355 {
356 if (boost::ends_with(pwm.string(),
357 std::to_string(index + 1)))
358 {
359 path = &configPath;
Jason Lingd320a2e2020-07-11 14:08:14 -0700360 pwmName = &name;
James Feistde5e9702019-09-18 16:13:02 -0700361 break;
362 }
363 }
364
365 if (path == nullptr)
366 {
367 continue;
368 }
369
370 // only add new elements
371 const std::string& sysPath = pwm.string();
James Feistde5e9702019-09-18 16:13:02 -0700372 pwmSensors.insert(
373 std::pair<std::string, std::unique_ptr<PwmSensor>>(
374 sysPath, std::make_unique<PwmSensor>(
Jason Lingd320a2e2020-07-11 14:08:14 -0700375 *pwmName, sysPath, dbusConnection,
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530376 objectServer, *path, "Fan")));
James Feist6714a252018-09-10 15:26:18 -0700377 }
James Feistde5e9702019-09-18 16:13:02 -0700378 }));
379 getter->getConfiguration(
380 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()});
James Feist6714a252018-09-10 15:26:18 -0700381}
382
James Feistdc6c55f2018-10-31 12:53:20 -0700383void createRedundancySensor(
384 const boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
385 sensors,
386 std::shared_ptr<sdbusplus::asio::connection> conn,
387 sdbusplus::asio::object_server& objectServer)
388{
389
390 conn->async_method_call(
391 [&objectServer, &sensors](boost::system::error_code& ec,
392 const ManagedObjectType managedObj) {
393 if (ec)
394 {
395 std::cerr << "Error calling entity manager \n";
396 return;
397 }
398 for (const auto& pathPair : managedObj)
399 {
400 for (const auto& interfacePair : pathPair.second)
401 {
402 if (interfacePair.first == redundancyConfiguration)
403 {
404 // currently only support one
405 auto findCount =
406 interfacePair.second.find("AllowedFailures");
407 if (findCount == interfacePair.second.end())
408 {
409 std::cerr << "Malformed redundancy record \n";
410 return;
411 }
412 std::vector<std::string> sensorList;
413
414 for (const auto& sensor : sensors)
415 {
416 sensorList.push_back(
417 "/xyz/openbmc_project/sensors/fan_tach/" +
418 sensor.second->name);
419 }
James Feist7b18b1e2019-05-14 13:42:09 -0700420 systemRedundancy.reset();
421 systemRedundancy.emplace(RedundancySensor(
James Feist3eb82622019-02-08 13:10:22 -0800422 std::get<uint64_t>(findCount->second), sensorList,
James Feist7b18b1e2019-05-14 13:42:09 -0700423 objectServer, pathPair.first));
James Feistdc6c55f2018-10-31 12:53:20 -0700424
425 return;
426 }
427 }
428 }
429 },
430 "xyz.openbmc_project.EntityManager", "/",
431 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
432}
433
James Feistb6c0b912019-07-09 12:21:44 -0700434int main()
James Feist6714a252018-09-10 15:26:18 -0700435{
436 boost::asio::io_service io;
437 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
438 systemBus->request_name("xyz.openbmc_project.FanSensor");
439 sdbusplus::asio::object_server objectServer(systemBus);
440 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>
441 tachSensors;
442 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
443 pwmSensors;
444 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700445 auto sensorsChanged =
446 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700447
448 io.post([&]() {
449 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
450 nullptr);
James Feistdc6c55f2018-10-31 12:53:20 -0700451 createRedundancySensor(tachSensors, systemBus, objectServer);
James Feist6714a252018-09-10 15:26:18 -0700452 });
453
454 boost::asio::deadline_timer filterTimer(io);
455 std::function<void(sdbusplus::message::message&)> eventHandler =
456 [&](sdbusplus::message::message& message) {
457 if (message.is_method_error())
458 {
459 std::cerr << "callback method error\n";
460 return;
461 }
462 sensorsChanged->insert(message.get_path());
463 // this implicitly cancels the timer
464 filterTimer.expires_from_now(boost::posix_time::seconds(1));
465
466 filterTimer.async_wait([&](const boost::system::error_code& ec) {
467 if (ec == boost::asio::error::operation_aborted)
468 {
469 /* we were canceled*/
470 return;
471 }
472 else if (ec)
473 {
474 std::cerr << "timer error\n";
475 return;
476 }
477 createSensors(io, objectServer, tachSensors, pwmSensors,
478 systemBus, sensorsChanged);
479 });
480 };
481
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700482 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700483 {
484 auto match = std::make_unique<sdbusplus::bus::match::match>(
485 static_cast<sdbusplus::bus::bus&>(*systemBus),
486 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700487 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700488 eventHandler);
489 matches.emplace_back(std::move(match));
490 }
491
James Feistdc6c55f2018-10-31 12:53:20 -0700492 // redundancy sensor
493 std::function<void(sdbusplus::message::message&)> redundancyHandler =
494 [&tachSensors, &systemBus,
James Feistb6c0b912019-07-09 12:21:44 -0700495 &objectServer](sdbusplus::message::message&) {
James Feistdc6c55f2018-10-31 12:53:20 -0700496 createRedundancySensor(tachSensors, systemBus, objectServer);
497 };
498 auto match = std::make_unique<sdbusplus::bus::match::match>(
499 static_cast<sdbusplus::bus::bus&>(*systemBus),
500 "type='signal',member='PropertiesChanged',path_namespace='" +
501 std::string(inventoryPath) + "',arg0namespace='" +
502 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700503 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700504 matches.emplace_back(std::move(match));
505
James Feist6714a252018-09-10 15:26:18 -0700506 io.run();
507}