blob: 122bba0afc07d85d335290a5c06674a7b3efc60f [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
Josh Lehanf920e092020-08-07 00:12:54 -0700301 PowerState powerState = PowerState::on;
302 auto findPower = baseConfiguration->second.find("PowerState");
303 if (findPower != baseConfiguration->second.end())
304 {
305 auto ptrPower =
306 std::get_if<std::string>(&(findPower->second));
307 if (ptrPower)
308 {
309 setReadState(*ptrPower, powerState);
310 }
311 }
312
James Feistde5e9702019-09-18 16:13:02 -0700313 constexpr double defaultMaxReading = 25000;
314 constexpr double defaultMinReading = 0;
315 auto limits =
316 std::make_pair(defaultMinReading, defaultMaxReading);
317
318 findLimits(limits, baseConfiguration);
319 tachSensors[sensorName] = std::make_unique<TachSensor>(
320 path.string(), baseType, objectServer, dbusConnection,
321 std::move(presenceSensor), redundancy, io, sensorName,
Josh Lehanf920e092020-08-07 00:12:54 -0700322 std::move(sensorThresholds), *interfacePath, limits,
323 powerState);
James Feistde5e9702019-09-18 16:13:02 -0700324
325 auto connector =
326 sensorData->find(baseType + std::string(".Connector"));
327 if (connector != sensorData->end())
328 {
329 auto findPwm = connector->second.find("Pwm");
330 if (findPwm == connector->second.end())
331 {
332 std::cerr << "Connector Missing PWM!\n";
333 continue;
334 }
James Feistde5e9702019-09-18 16:13:02 -0700335 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
336 findPwm->second);
Jason Lingd320a2e2020-07-11 14:08:14 -0700337 /* use pwm name override if found in configuration else use
338 * default */
339 auto findOverride = connector->second.find("PwmName");
340 std::string pwmName;
341 if (findOverride != connector->second.end())
342 {
343 pwmName = std::visit(VariantToStringVisitor(),
344 findOverride->second);
345 }
346 else
347 {
348 pwmName = "Pwm_" + std::to_string(pwm + 1);
349 }
350 pwmNumbers.emplace_back(pwm, *interfacePath, pwmName);
James Feist95b079b2018-11-21 09:28:00 -0800351 }
352 }
James Feistde5e9702019-09-18 16:13:02 -0700353 std::vector<fs::path> pwms;
354 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+$)", pwms))
James Feist6714a252018-09-10 15:26:18 -0700355 {
James Feistde5e9702019-09-18 16:13:02 -0700356 std::cerr << "No pwm in system\n";
357 return;
358 }
359 for (const fs::path& pwm : pwms)
360 {
361 if (pwmSensors.find(pwm) != pwmSensors.end())
James Feist6714a252018-09-10 15:26:18 -0700362 {
James Feistde5e9702019-09-18 16:13:02 -0700363 continue;
James Feist6714a252018-09-10 15:26:18 -0700364 }
James Feistde5e9702019-09-18 16:13:02 -0700365 const std::string* path = nullptr;
Jason Lingd320a2e2020-07-11 14:08:14 -0700366 const std::string* pwmName = nullptr;
367
368 for (const auto& [index, configPath, name] : pwmNumbers)
James Feistde5e9702019-09-18 16:13:02 -0700369 {
370 if (boost::ends_with(pwm.string(),
371 std::to_string(index + 1)))
372 {
373 path = &configPath;
Jason Lingd320a2e2020-07-11 14:08:14 -0700374 pwmName = &name;
James Feistde5e9702019-09-18 16:13:02 -0700375 break;
376 }
377 }
378
379 if (path == nullptr)
380 {
381 continue;
382 }
383
384 // only add new elements
385 const std::string& sysPath = pwm.string();
James Feistde5e9702019-09-18 16:13:02 -0700386 pwmSensors.insert(
387 std::pair<std::string, std::unique_ptr<PwmSensor>>(
388 sysPath, std::make_unique<PwmSensor>(
Jason Lingd320a2e2020-07-11 14:08:14 -0700389 *pwmName, sysPath, dbusConnection,
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530390 objectServer, *path, "Fan")));
James Feist6714a252018-09-10 15:26:18 -0700391 }
James Feistde5e9702019-09-18 16:13:02 -0700392 }));
393 getter->getConfiguration(
James Feistf27a55c2020-08-04 14:27:30 -0700394 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()},
395 retries);
James Feist6714a252018-09-10 15:26:18 -0700396}
397
James Feistdc6c55f2018-10-31 12:53:20 -0700398void createRedundancySensor(
399 const boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
400 sensors,
401 std::shared_ptr<sdbusplus::asio::connection> conn,
402 sdbusplus::asio::object_server& objectServer)
403{
404
405 conn->async_method_call(
406 [&objectServer, &sensors](boost::system::error_code& ec,
407 const ManagedObjectType managedObj) {
408 if (ec)
409 {
410 std::cerr << "Error calling entity manager \n";
411 return;
412 }
413 for (const auto& pathPair : managedObj)
414 {
415 for (const auto& interfacePair : pathPair.second)
416 {
417 if (interfacePair.first == redundancyConfiguration)
418 {
419 // currently only support one
420 auto findCount =
421 interfacePair.second.find("AllowedFailures");
422 if (findCount == interfacePair.second.end())
423 {
424 std::cerr << "Malformed redundancy record \n";
425 return;
426 }
427 std::vector<std::string> sensorList;
428
429 for (const auto& sensor : sensors)
430 {
431 sensorList.push_back(
432 "/xyz/openbmc_project/sensors/fan_tach/" +
433 sensor.second->name);
434 }
James Feist7b18b1e2019-05-14 13:42:09 -0700435 systemRedundancy.reset();
436 systemRedundancy.emplace(RedundancySensor(
James Feist3eb82622019-02-08 13:10:22 -0800437 std::get<uint64_t>(findCount->second), sensorList,
James Feist7b18b1e2019-05-14 13:42:09 -0700438 objectServer, pathPair.first));
James Feistdc6c55f2018-10-31 12:53:20 -0700439
440 return;
441 }
442 }
443 }
444 },
445 "xyz.openbmc_project.EntityManager", "/",
446 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
447}
448
James Feistb6c0b912019-07-09 12:21:44 -0700449int main()
James Feist6714a252018-09-10 15:26:18 -0700450{
451 boost::asio::io_service io;
452 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
453 systemBus->request_name("xyz.openbmc_project.FanSensor");
454 sdbusplus::asio::object_server objectServer(systemBus);
455 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>
456 tachSensors;
457 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
458 pwmSensors;
459 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700460 auto sensorsChanged =
461 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700462
463 io.post([&]() {
464 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
465 nullptr);
James Feistdc6c55f2018-10-31 12:53:20 -0700466 createRedundancySensor(tachSensors, systemBus, objectServer);
James Feist6714a252018-09-10 15:26:18 -0700467 });
468
469 boost::asio::deadline_timer filterTimer(io);
470 std::function<void(sdbusplus::message::message&)> eventHandler =
471 [&](sdbusplus::message::message& message) {
472 if (message.is_method_error())
473 {
474 std::cerr << "callback method error\n";
475 return;
476 }
477 sensorsChanged->insert(message.get_path());
478 // this implicitly cancels the timer
479 filterTimer.expires_from_now(boost::posix_time::seconds(1));
480
481 filterTimer.async_wait([&](const boost::system::error_code& ec) {
482 if (ec == boost::asio::error::operation_aborted)
483 {
484 /* we were canceled*/
485 return;
486 }
487 else if (ec)
488 {
489 std::cerr << "timer error\n";
490 return;
491 }
492 createSensors(io, objectServer, tachSensors, pwmSensors,
James Feistf27a55c2020-08-04 14:27:30 -0700493 systemBus, sensorsChanged, 5);
James Feist6714a252018-09-10 15:26:18 -0700494 });
495 };
496
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700497 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700498 {
499 auto match = std::make_unique<sdbusplus::bus::match::match>(
500 static_cast<sdbusplus::bus::bus&>(*systemBus),
501 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700502 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700503 eventHandler);
504 matches.emplace_back(std::move(match));
505 }
506
James Feistdc6c55f2018-10-31 12:53:20 -0700507 // redundancy sensor
508 std::function<void(sdbusplus::message::message&)> redundancyHandler =
509 [&tachSensors, &systemBus,
James Feistb6c0b912019-07-09 12:21:44 -0700510 &objectServer](sdbusplus::message::message&) {
James Feistdc6c55f2018-10-31 12:53:20 -0700511 createRedundancySensor(tachSensors, systemBus, objectServer);
512 };
513 auto match = std::make_unique<sdbusplus::bus::match::match>(
514 static_cast<sdbusplus::bus::bus&>(*systemBus),
515 "type='signal',member='PropertiesChanged',path_namespace='" +
516 std::string(inventoryPath) + "',arg0namespace='" +
517 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700518 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700519 matches.emplace_back(std::move(match));
520
James Feist6714a252018-09-10 15:26:18 -0700521 io.run();
522}