blob: 8668addec15a3fc2eb7193afec8b6ae986e2bbb9 [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
Patrick Venture96e97db2019-10-31 13:44:38 -070022#include <array>
James Feist6714a252018-09-10 15:26:18 -070023#include <boost/algorithm/string/predicate.hpp>
24#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070025#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070026#include <boost/container/flat_set.hpp>
27#include <boost/lexical_cast.hpp>
James Feist24f02f22019-04-15 11:05:39 -070028#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070029#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070030#include <functional>
31#include <memory>
32#include <optional>
James Feist6714a252018-09-10 15:26:18 -070033#include <regex>
34#include <sdbusplus/asio/connection.hpp>
35#include <sdbusplus/asio/object_server.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070036#include <sdbusplus/bus/match.hpp>
37#include <string>
38#include <utility>
39#include <variant>
40#include <vector>
James Feist6714a252018-09-10 15:26:18 -070041
42static constexpr bool DEBUG = false;
43
James Feistcf3bce62019-01-08 10:07:19 -080044namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080045
Peter Lundgren8843b622019-09-12 10:33:41 -070046static constexpr std::array<const char*, 3> sensorTypes = {
James Feist95b079b2018-11-21 09:28:00 -080047 "xyz.openbmc_project.Configuration.AspeedFan",
Peter Lundgren8843b622019-09-12 10:33:41 -070048 "xyz.openbmc_project.Configuration.I2CFan",
49 "xyz.openbmc_project.Configuration.NuvotonFan"};
James Feistdc6c55f2018-10-31 12:53:20 -070050constexpr const char* redundancyConfiguration =
51 "xyz.openbmc_project.Configuration.FanRedundancy";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070052static std::regex inputRegex(R"(fan(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070053
James Feist95b079b2018-11-21 09:28:00 -080054enum class FanTypes
55{
56 aspeed,
Peter Lundgren8843b622019-09-12 10:33:41 -070057 i2c,
58 nuvoton
James Feist95b079b2018-11-21 09:28:00 -080059};
60
James Feistdc6c55f2018-10-31 12:53:20 -070061// todo: power supply fan redundancy
James Feist7b18b1e2019-05-14 13:42:09 -070062std::optional<RedundancySensor> systemRedundancy;
James Feist95b079b2018-11-21 09:28:00 -080063
64FanTypes getFanType(const fs::path& parentPath)
65{
66 fs::path linkPath = parentPath / "device";
67 std::string canonical = fs::read_symlink(linkPath);
Jae Hyun Yoo241356e2020-02-20 12:48:04 -080068 if (boost::ends_with(canonical, "1e786000.pwm-tacho-controller") ||
69 boost::ends_with(canonical, "1e610000.pwm-tacho-controller"))
James Feist95b079b2018-11-21 09:28:00 -080070 {
71 return FanTypes::aspeed;
72 }
Peter Lundgren8843b622019-09-12 10:33:41 -070073 else if (boost::ends_with(canonical, "f0103000.pwm-fan-controller"))
74 {
75 return FanTypes::nuvoton;
76 }
James Feist95b079b2018-11-21 09:28:00 -080077 // todo: will we need to support other types?
78 return FanTypes::i2c;
79}
James Feistdc6c55f2018-10-31 12:53:20 -070080
James Feist6714a252018-09-10 15:26:18 -070081void createSensors(
82 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
83 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
84 tachSensors,
85 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
86 pwmSensors,
87 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
88 const std::unique_ptr<boost::container::flat_set<std::string>>&
89 sensorsChanged)
90{
James Feist6714a252018-09-10 15:26:18 -070091
James Feistde5e9702019-09-18 16:13:02 -070092 auto getter = std::make_shared<GetSensorConfiguration>(
93 dbusConnection,
94 std::move([&io, &objectServer, &tachSensors, &pwmSensors,
95 &dbusConnection, &sensorsChanged](
96 const ManagedObjectType& sensorConfigurations) {
97 bool firstScan = sensorsChanged == nullptr;
98 std::vector<fs::path> paths;
99 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)",
100 paths))
James Feist95b079b2018-11-21 09:28:00 -0800101 {
James Feistde5e9702019-09-18 16:13:02 -0700102 std::cerr << "No temperature sensors in system\n";
103 return;
James Feist95b079b2018-11-21 09:28:00 -0800104 }
James Feist6714a252018-09-10 15:26:18 -0700105
James Feistde5e9702019-09-18 16:13:02 -0700106 std::vector<std::pair<uint8_t, std::string>> pwmNumbers;
107
108 // iterate through all found fan sensors, and try to match them with
109 // configuration
110 for (const auto& path : paths)
James Feist6714a252018-09-10 15:26:18 -0700111 {
James Feistde5e9702019-09-18 16:13:02 -0700112 std::smatch match;
113 std::string pathStr = path.string();
114
115 std::regex_search(pathStr, match, inputRegex);
116 std::string indexStr = *(match.begin() + 1);
117
118 auto directory = path.parent_path();
119 FanTypes fanType = getFanType(directory);
120 size_t bus = 0;
121 size_t address = 0;
122 if (fanType == FanTypes::i2c)
James Feist6714a252018-09-10 15:26:18 -0700123 {
James Feistde5e9702019-09-18 16:13:02 -0700124 std::string link =
125 fs::read_symlink(directory / "device").filename();
126
127 size_t findDash = link.find("-");
128 if (findDash == std::string::npos ||
129 link.size() <= findDash + 1)
130 {
131 std::cerr << "Error finding device from symlink";
132 }
133 bus = std::stoi(link.substr(0, findDash));
134 address = std::stoi(link.substr(findDash + 1), nullptr, 16);
James Feist6714a252018-09-10 15:26:18 -0700135 }
James Feistde5e9702019-09-18 16:13:02 -0700136 // convert to 0 based
137 size_t index = std::stoul(indexStr) - 1;
138
139 const char* baseType;
140 const SensorData* sensorData = nullptr;
141 const std::string* interfacePath = nullptr;
142 const SensorBaseConfiguration* baseConfiguration = nullptr;
143 for (const std::pair<sdbusplus::message::object_path,
144 SensorData>& sensor : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800145 {
James Feistde5e9702019-09-18 16:13:02 -0700146 // find the base of the configuration to see if indexes
147 // match
148 for (const char* type : sensorTypes)
149 {
150 auto sensorBaseFind = sensor.second.find(type);
151 if (sensorBaseFind != sensor.second.end())
152 {
153 baseConfiguration = &(*sensorBaseFind);
154 interfacePath = &(sensor.first.str);
155 baseType = type;
156 break;
157 }
158 }
159 if (baseConfiguration == nullptr)
160 {
161 continue;
162 }
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800163
James Feistde5e9702019-09-18 16:13:02 -0700164 auto findIndex = baseConfiguration->second.find("Index");
165 if (findIndex == baseConfiguration->second.end())
166 {
167 std::cerr << baseConfiguration->first
168 << " missing index\n";
169 continue;
170 }
171 unsigned int configIndex = std::visit(
172 VariantToUnsignedIntVisitor(), findIndex->second);
173 if (configIndex != index)
174 {
175 continue;
176 }
177 if (fanType == FanTypes::aspeed ||
178 fanType == FanTypes::nuvoton)
179 {
180 // there will be only 1 aspeed or nuvoton sensor object
181 // in sysfs, we found the fan
182 sensorData = &(sensor.second);
183 break;
184 }
185 else if (baseType ==
186 std::string(
187 "xyz.openbmc_project.Configuration.I2CFan"))
188 {
189 auto findBus = baseConfiguration->second.find("Bus");
190 auto findAddress =
191 baseConfiguration->second.find("Address");
192 if (findBus == baseConfiguration->second.end() ||
193 findAddress == baseConfiguration->second.end())
194 {
195 std::cerr << baseConfiguration->first
196 << " missing bus or address\n";
197 continue;
198 }
199 unsigned int configBus = std::visit(
200 VariantToUnsignedIntVisitor(), findBus->second);
201 unsigned int configAddress = std::visit(
202 VariantToUnsignedIntVisitor(), findAddress->second);
203
204 if (configBus == bus && configAddress == address)
205 {
206 sensorData = &(sensor.second);
207 break;
208 }
209 }
210 }
211 if (sensorData == nullptr)
212 {
213 std::cerr << "failed to find match for " << path.string()
214 << "\n";
James Feist95b079b2018-11-21 09:28:00 -0800215 continue;
216 }
James Feist95b079b2018-11-21 09:28:00 -0800217
James Feistde5e9702019-09-18 16:13:02 -0700218 auto findSensorName = baseConfiguration->second.find("Name");
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800219
James Feistde5e9702019-09-18 16:13:02 -0700220 if (findSensorName == baseConfiguration->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800221 {
James Feistde5e9702019-09-18 16:13:02 -0700222 std::cerr << "could not determine configuration name for "
223 << path.string() << "\n";
224 continue;
225 }
226 std::string sensorName =
227 std::get<std::string>(findSensorName->second);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800228
James Feistde5e9702019-09-18 16:13:02 -0700229 // on rescans, only update sensors we were signaled by
230 auto findSensor = tachSensors.find(sensorName);
231 if (!firstScan && findSensor != tachSensors.end())
232 {
233 bool found = false;
234 for (auto it = sensorsChanged->begin();
235 it != sensorsChanged->end(); it++)
236 {
237 if (boost::ends_with(*it, findSensor->second->name))
238 {
239 sensorsChanged->erase(it);
240 findSensor->second = nullptr;
241 found = true;
242 break;
243 }
244 }
245 if (!found)
246 {
247 continue;
248 }
249 }
250 std::vector<thresholds::Threshold> sensorThresholds;
251 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
252 {
253 std::cerr << "error populating thresholds for "
254 << sensorName << "\n";
255 }
256
257 auto presenceConfig =
258 sensorData->find(baseType + std::string(".Presence"));
259
260 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
261
262 // presence sensors are optional
263 if (presenceConfig != sensorData->end())
264 {
James Feistde5e9702019-09-18 16:13:02 -0700265 auto findPolarity = presenceConfig->second.find("Polarity");
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800266 auto findPinName = presenceConfig->second.find("PinName");
James Feistde5e9702019-09-18 16:13:02 -0700267
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800268 if (findPinName == presenceConfig->second.end() ||
James Feistde5e9702019-09-18 16:13:02 -0700269 findPolarity == presenceConfig->second.end())
270 {
271 std::cerr << "Malformed Presence Configuration\n";
272 }
273 else
274 {
James Feistde5e9702019-09-18 16:13:02 -0700275 bool inverted = std::get<std::string>(
276 findPolarity->second) == "Low";
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800277 if (auto pinName =
278 std::get_if<std::string>(&findPinName->second))
279 {
280 presenceSensor = std::make_unique<PresenceSensor>(
281 *pinName, inverted, io, sensorName);
282 }
283 else
284 {
285 std::cerr
286 << "Malformed Presence pinName for sensor "
287 << sensorName << " \n";
288 }
James Feistde5e9702019-09-18 16:13:02 -0700289 }
290 }
291 std::optional<RedundancySensor>* redundancy = nullptr;
292 if (fanType == FanTypes::aspeed)
293 {
294 redundancy = &systemRedundancy;
295 }
296
297 constexpr double defaultMaxReading = 25000;
298 constexpr double defaultMinReading = 0;
299 auto limits =
300 std::make_pair(defaultMinReading, defaultMaxReading);
301
302 findLimits(limits, baseConfiguration);
303 tachSensors[sensorName] = std::make_unique<TachSensor>(
304 path.string(), baseType, objectServer, dbusConnection,
305 std::move(presenceSensor), redundancy, io, sensorName,
306 std::move(sensorThresholds), *interfacePath, limits);
307
308 auto connector =
309 sensorData->find(baseType + std::string(".Connector"));
310 if (connector != sensorData->end())
311 {
312 auto findPwm = connector->second.find("Pwm");
313 if (findPwm == connector->second.end())
314 {
315 std::cerr << "Connector Missing PWM!\n";
316 continue;
317 }
318
319 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
320 findPwm->second);
321 pwmNumbers.emplace_back(pwm, *interfacePath);
James Feist95b079b2018-11-21 09:28:00 -0800322 }
323 }
James Feistde5e9702019-09-18 16:13:02 -0700324 std::vector<fs::path> pwms;
325 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+$)", pwms))
James Feist6714a252018-09-10 15:26:18 -0700326 {
James Feistde5e9702019-09-18 16:13:02 -0700327 std::cerr << "No pwm in system\n";
328 return;
329 }
330 for (const fs::path& pwm : pwms)
331 {
332 if (pwmSensors.find(pwm) != pwmSensors.end())
James Feist6714a252018-09-10 15:26:18 -0700333 {
James Feistde5e9702019-09-18 16:13:02 -0700334 continue;
James Feist6714a252018-09-10 15:26:18 -0700335 }
James Feistde5e9702019-09-18 16:13:02 -0700336 const std::string* path = nullptr;
337 for (const auto& [index, configPath] : pwmNumbers)
338 {
339 if (boost::ends_with(pwm.string(),
340 std::to_string(index + 1)))
341 {
342 path = &configPath;
343 break;
344 }
345 }
346
347 if (path == nullptr)
348 {
349 continue;
350 }
351
352 // only add new elements
353 const std::string& sysPath = pwm.string();
354 const std::string& pwmName =
355 "Pwm_" + sysPath.substr(sysPath.find_last_of("pwm") + 1);
356 pwmSensors.insert(
357 std::pair<std::string, std::unique_ptr<PwmSensor>>(
358 sysPath, std::make_unique<PwmSensor>(
359 pwmName, sysPath, objectServer, *path)));
James Feist6714a252018-09-10 15:26:18 -0700360 }
James Feistde5e9702019-09-18 16:13:02 -0700361 }));
362 getter->getConfiguration(
363 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()});
James Feist6714a252018-09-10 15:26:18 -0700364}
365
James Feistdc6c55f2018-10-31 12:53:20 -0700366void createRedundancySensor(
367 const boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
368 sensors,
369 std::shared_ptr<sdbusplus::asio::connection> conn,
370 sdbusplus::asio::object_server& objectServer)
371{
372
373 conn->async_method_call(
374 [&objectServer, &sensors](boost::system::error_code& ec,
375 const ManagedObjectType managedObj) {
376 if (ec)
377 {
378 std::cerr << "Error calling entity manager \n";
379 return;
380 }
381 for (const auto& pathPair : managedObj)
382 {
383 for (const auto& interfacePair : pathPair.second)
384 {
385 if (interfacePair.first == redundancyConfiguration)
386 {
387 // currently only support one
388 auto findCount =
389 interfacePair.second.find("AllowedFailures");
390 if (findCount == interfacePair.second.end())
391 {
392 std::cerr << "Malformed redundancy record \n";
393 return;
394 }
395 std::vector<std::string> sensorList;
396
397 for (const auto& sensor : sensors)
398 {
399 sensorList.push_back(
400 "/xyz/openbmc_project/sensors/fan_tach/" +
401 sensor.second->name);
402 }
James Feist7b18b1e2019-05-14 13:42:09 -0700403 systemRedundancy.reset();
404 systemRedundancy.emplace(RedundancySensor(
James Feist3eb82622019-02-08 13:10:22 -0800405 std::get<uint64_t>(findCount->second), sensorList,
James Feist7b18b1e2019-05-14 13:42:09 -0700406 objectServer, pathPair.first));
James Feistdc6c55f2018-10-31 12:53:20 -0700407
408 return;
409 }
410 }
411 }
412 },
413 "xyz.openbmc_project.EntityManager", "/",
414 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
415}
416
James Feistb6c0b912019-07-09 12:21:44 -0700417int main()
James Feist6714a252018-09-10 15:26:18 -0700418{
419 boost::asio::io_service io;
420 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
421 systemBus->request_name("xyz.openbmc_project.FanSensor");
422 sdbusplus::asio::object_server objectServer(systemBus);
423 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>
424 tachSensors;
425 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
426 pwmSensors;
427 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
428 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
429 std::make_unique<boost::container::flat_set<std::string>>();
430
431 io.post([&]() {
432 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
433 nullptr);
James Feistdc6c55f2018-10-31 12:53:20 -0700434 createRedundancySensor(tachSensors, systemBus, objectServer);
James Feist6714a252018-09-10 15:26:18 -0700435 });
436
437 boost::asio::deadline_timer filterTimer(io);
438 std::function<void(sdbusplus::message::message&)> eventHandler =
439 [&](sdbusplus::message::message& message) {
440 if (message.is_method_error())
441 {
442 std::cerr << "callback method error\n";
443 return;
444 }
445 sensorsChanged->insert(message.get_path());
446 // this implicitly cancels the timer
447 filterTimer.expires_from_now(boost::posix_time::seconds(1));
448
449 filterTimer.async_wait([&](const boost::system::error_code& ec) {
450 if (ec == boost::asio::error::operation_aborted)
451 {
452 /* we were canceled*/
453 return;
454 }
455 else if (ec)
456 {
457 std::cerr << "timer error\n";
458 return;
459 }
460 createSensors(io, objectServer, tachSensors, pwmSensors,
461 systemBus, sensorsChanged);
462 });
463 };
464
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700465 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700466 {
467 auto match = std::make_unique<sdbusplus::bus::match::match>(
468 static_cast<sdbusplus::bus::bus&>(*systemBus),
469 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700470 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700471 eventHandler);
472 matches.emplace_back(std::move(match));
473 }
474
James Feistdc6c55f2018-10-31 12:53:20 -0700475 // redundancy sensor
476 std::function<void(sdbusplus::message::message&)> redundancyHandler =
477 [&tachSensors, &systemBus,
James Feistb6c0b912019-07-09 12:21:44 -0700478 &objectServer](sdbusplus::message::message&) {
James Feistdc6c55f2018-10-31 12:53:20 -0700479 createRedundancySensor(tachSensors, systemBus, objectServer);
480 };
481 auto match = std::make_unique<sdbusplus::bus::match::match>(
482 static_cast<sdbusplus::bus::bus&>(*systemBus),
483 "type='signal',member='PropertiesChanged',path_namespace='" +
484 std::string(inventoryPath) + "',arg0namespace='" +
485 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700486 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700487 matches.emplace_back(std::move(match));
488
James Feist6714a252018-09-10 15:26:18 -0700489 io.run();
490}