blob: 3a3175fc4654a4280a9c2b6912e45a3afc2d24d9 [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);
68 if (boost::ends_with(canonical, "1e786000.pwm-tacho-controller"))
69 {
70 return FanTypes::aspeed;
71 }
Peter Lundgren8843b622019-09-12 10:33:41 -070072 else if (boost::ends_with(canonical, "f0103000.pwm-fan-controller"))
73 {
74 return FanTypes::nuvoton;
75 }
James Feist95b079b2018-11-21 09:28:00 -080076 // todo: will we need to support other types?
77 return FanTypes::i2c;
78}
James Feistdc6c55f2018-10-31 12:53:20 -070079
James Feist6714a252018-09-10 15:26:18 -070080void createSensors(
81 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
82 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
83 tachSensors,
84 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
85 pwmSensors,
86 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
87 const std::unique_ptr<boost::container::flat_set<std::string>>&
88 sensorsChanged)
89{
James Feist6714a252018-09-10 15:26:18 -070090
James Feistde5e9702019-09-18 16:13:02 -070091 auto getter = std::make_shared<GetSensorConfiguration>(
92 dbusConnection,
93 std::move([&io, &objectServer, &tachSensors, &pwmSensors,
94 &dbusConnection, &sensorsChanged](
95 const ManagedObjectType& sensorConfigurations) {
96 bool firstScan = sensorsChanged == nullptr;
97 std::vector<fs::path> paths;
98 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)",
99 paths))
James Feist95b079b2018-11-21 09:28:00 -0800100 {
James Feistde5e9702019-09-18 16:13:02 -0700101 std::cerr << "No temperature sensors in system\n";
102 return;
James Feist95b079b2018-11-21 09:28:00 -0800103 }
James Feist6714a252018-09-10 15:26:18 -0700104
James Feistde5e9702019-09-18 16:13:02 -0700105 std::vector<std::pair<uint8_t, std::string>> pwmNumbers;
106
107 // iterate through all found fan sensors, and try to match them with
108 // configuration
109 for (const auto& path : paths)
James Feist6714a252018-09-10 15:26:18 -0700110 {
James Feistde5e9702019-09-18 16:13:02 -0700111 std::smatch match;
112 std::string pathStr = path.string();
113
114 std::regex_search(pathStr, match, inputRegex);
115 std::string indexStr = *(match.begin() + 1);
116
117 auto directory = path.parent_path();
118 FanTypes fanType = getFanType(directory);
119 size_t bus = 0;
120 size_t address = 0;
121 if (fanType == FanTypes::i2c)
James Feist6714a252018-09-10 15:26:18 -0700122 {
James Feistde5e9702019-09-18 16:13:02 -0700123 std::string link =
124 fs::read_symlink(directory / "device").filename();
125
126 size_t findDash = link.find("-");
127 if (findDash == std::string::npos ||
128 link.size() <= findDash + 1)
129 {
130 std::cerr << "Error finding device from symlink";
131 }
132 bus = std::stoi(link.substr(0, findDash));
133 address = std::stoi(link.substr(findDash + 1), nullptr, 16);
James Feist6714a252018-09-10 15:26:18 -0700134 }
James Feistde5e9702019-09-18 16:13:02 -0700135 // convert to 0 based
136 size_t index = std::stoul(indexStr) - 1;
137
138 const char* baseType;
139 const SensorData* sensorData = nullptr;
140 const std::string* interfacePath = nullptr;
141 const SensorBaseConfiguration* baseConfiguration = nullptr;
142 for (const std::pair<sdbusplus::message::object_path,
143 SensorData>& sensor : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800144 {
James Feistde5e9702019-09-18 16:13:02 -0700145 // find the base of the configuration to see if indexes
146 // match
147 for (const char* type : sensorTypes)
148 {
149 auto sensorBaseFind = sensor.second.find(type);
150 if (sensorBaseFind != sensor.second.end())
151 {
152 baseConfiguration = &(*sensorBaseFind);
153 interfacePath = &(sensor.first.str);
154 baseType = type;
155 break;
156 }
157 }
158 if (baseConfiguration == nullptr)
159 {
160 continue;
161 }
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800162
James Feistde5e9702019-09-18 16:13:02 -0700163 auto findIndex = baseConfiguration->second.find("Index");
164 if (findIndex == baseConfiguration->second.end())
165 {
166 std::cerr << baseConfiguration->first
167 << " missing index\n";
168 continue;
169 }
170 unsigned int configIndex = std::visit(
171 VariantToUnsignedIntVisitor(), findIndex->second);
172 if (configIndex != index)
173 {
174 continue;
175 }
176 if (fanType == FanTypes::aspeed ||
177 fanType == FanTypes::nuvoton)
178 {
179 // there will be only 1 aspeed or nuvoton sensor object
180 // in sysfs, we found the fan
181 sensorData = &(sensor.second);
182 break;
183 }
184 else if (baseType ==
185 std::string(
186 "xyz.openbmc_project.Configuration.I2CFan"))
187 {
188 auto findBus = baseConfiguration->second.find("Bus");
189 auto findAddress =
190 baseConfiguration->second.find("Address");
191 if (findBus == baseConfiguration->second.end() ||
192 findAddress == baseConfiguration->second.end())
193 {
194 std::cerr << baseConfiguration->first
195 << " missing bus or address\n";
196 continue;
197 }
198 unsigned int configBus = std::visit(
199 VariantToUnsignedIntVisitor(), findBus->second);
200 unsigned int configAddress = std::visit(
201 VariantToUnsignedIntVisitor(), findAddress->second);
202
203 if (configBus == bus && configAddress == address)
204 {
205 sensorData = &(sensor.second);
206 break;
207 }
208 }
209 }
210 if (sensorData == nullptr)
211 {
212 std::cerr << "failed to find match for " << path.string()
213 << "\n";
James Feist95b079b2018-11-21 09:28:00 -0800214 continue;
215 }
James Feist95b079b2018-11-21 09:28:00 -0800216
James Feistde5e9702019-09-18 16:13:02 -0700217 auto findSensorName = baseConfiguration->second.find("Name");
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800218
James Feistde5e9702019-09-18 16:13:02 -0700219 if (findSensorName == baseConfiguration->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800220 {
James Feistde5e9702019-09-18 16:13:02 -0700221 std::cerr << "could not determine configuration name for "
222 << path.string() << "\n";
223 continue;
224 }
225 std::string sensorName =
226 std::get<std::string>(findSensorName->second);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800227
James Feistde5e9702019-09-18 16:13:02 -0700228 // on rescans, only update sensors we were signaled by
229 auto findSensor = tachSensors.find(sensorName);
230 if (!firstScan && findSensor != tachSensors.end())
231 {
232 bool found = false;
233 for (auto it = sensorsChanged->begin();
234 it != sensorsChanged->end(); it++)
235 {
236 if (boost::ends_with(*it, findSensor->second->name))
237 {
238 sensorsChanged->erase(it);
239 findSensor->second = nullptr;
240 found = true;
241 break;
242 }
243 }
244 if (!found)
245 {
246 continue;
247 }
248 }
249 std::vector<thresholds::Threshold> sensorThresholds;
250 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
251 {
252 std::cerr << "error populating thresholds for "
253 << sensorName << "\n";
254 }
255
256 auto presenceConfig =
257 sensorData->find(baseType + std::string(".Presence"));
258
259 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
260
261 // presence sensors are optional
262 if (presenceConfig != sensorData->end())
263 {
James Feistde5e9702019-09-18 16:13:02 -0700264 auto findPolarity = presenceConfig->second.find("Polarity");
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800265 auto findPinName = presenceConfig->second.find("PinName");
James Feistde5e9702019-09-18 16:13:02 -0700266
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800267 if (findPinName == presenceConfig->second.end() ||
James Feistde5e9702019-09-18 16:13:02 -0700268 findPolarity == presenceConfig->second.end())
269 {
270 std::cerr << "Malformed Presence Configuration\n";
271 }
272 else
273 {
James Feistde5e9702019-09-18 16:13:02 -0700274 bool inverted = std::get<std::string>(
275 findPolarity->second) == "Low";
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800276 if (auto pinName =
277 std::get_if<std::string>(&findPinName->second))
278 {
279 presenceSensor = std::make_unique<PresenceSensor>(
280 *pinName, inverted, io, sensorName);
281 }
282 else
283 {
284 std::cerr
285 << "Malformed Presence pinName for sensor "
286 << sensorName << " \n";
287 }
James Feistde5e9702019-09-18 16:13:02 -0700288 }
289 }
290 std::optional<RedundancySensor>* redundancy = nullptr;
291 if (fanType == FanTypes::aspeed)
292 {
293 redundancy = &systemRedundancy;
294 }
295
296 constexpr double defaultMaxReading = 25000;
297 constexpr double defaultMinReading = 0;
298 auto limits =
299 std::make_pair(defaultMinReading, defaultMaxReading);
300
301 findLimits(limits, baseConfiguration);
302 tachSensors[sensorName] = std::make_unique<TachSensor>(
303 path.string(), baseType, objectServer, dbusConnection,
304 std::move(presenceSensor), redundancy, io, sensorName,
305 std::move(sensorThresholds), *interfacePath, limits);
306
307 auto connector =
308 sensorData->find(baseType + std::string(".Connector"));
309 if (connector != sensorData->end())
310 {
311 auto findPwm = connector->second.find("Pwm");
312 if (findPwm == connector->second.end())
313 {
314 std::cerr << "Connector Missing PWM!\n";
315 continue;
316 }
317
318 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
319 findPwm->second);
320 pwmNumbers.emplace_back(pwm, *interfacePath);
James Feist95b079b2018-11-21 09:28:00 -0800321 }
322 }
James Feistde5e9702019-09-18 16:13:02 -0700323 std::vector<fs::path> pwms;
324 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+$)", pwms))
James Feist6714a252018-09-10 15:26:18 -0700325 {
James Feistde5e9702019-09-18 16:13:02 -0700326 std::cerr << "No pwm in system\n";
327 return;
328 }
329 for (const fs::path& pwm : pwms)
330 {
331 if (pwmSensors.find(pwm) != pwmSensors.end())
James Feist6714a252018-09-10 15:26:18 -0700332 {
James Feistde5e9702019-09-18 16:13:02 -0700333 continue;
James Feist6714a252018-09-10 15:26:18 -0700334 }
James Feistde5e9702019-09-18 16:13:02 -0700335 const std::string* path = nullptr;
336 for (const auto& [index, configPath] : pwmNumbers)
337 {
338 if (boost::ends_with(pwm.string(),
339 std::to_string(index + 1)))
340 {
341 path = &configPath;
342 break;
343 }
344 }
345
346 if (path == nullptr)
347 {
348 continue;
349 }
350
351 // only add new elements
352 const std::string& sysPath = pwm.string();
353 const std::string& pwmName =
354 "Pwm_" + sysPath.substr(sysPath.find_last_of("pwm") + 1);
355 pwmSensors.insert(
356 std::pair<std::string, std::unique_ptr<PwmSensor>>(
357 sysPath, std::make_unique<PwmSensor>(
358 pwmName, sysPath, objectServer, *path)));
James Feist6714a252018-09-10 15:26:18 -0700359 }
James Feistde5e9702019-09-18 16:13:02 -0700360 }));
361 getter->getConfiguration(
362 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()});
James Feist6714a252018-09-10 15:26:18 -0700363}
364
James Feistdc6c55f2018-10-31 12:53:20 -0700365void createRedundancySensor(
366 const boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
367 sensors,
368 std::shared_ptr<sdbusplus::asio::connection> conn,
369 sdbusplus::asio::object_server& objectServer)
370{
371
372 conn->async_method_call(
373 [&objectServer, &sensors](boost::system::error_code& ec,
374 const ManagedObjectType managedObj) {
375 if (ec)
376 {
377 std::cerr << "Error calling entity manager \n";
378 return;
379 }
380 for (const auto& pathPair : managedObj)
381 {
382 for (const auto& interfacePair : pathPair.second)
383 {
384 if (interfacePair.first == redundancyConfiguration)
385 {
386 // currently only support one
387 auto findCount =
388 interfacePair.second.find("AllowedFailures");
389 if (findCount == interfacePair.second.end())
390 {
391 std::cerr << "Malformed redundancy record \n";
392 return;
393 }
394 std::vector<std::string> sensorList;
395
396 for (const auto& sensor : sensors)
397 {
398 sensorList.push_back(
399 "/xyz/openbmc_project/sensors/fan_tach/" +
400 sensor.second->name);
401 }
James Feist7b18b1e2019-05-14 13:42:09 -0700402 systemRedundancy.reset();
403 systemRedundancy.emplace(RedundancySensor(
James Feist3eb82622019-02-08 13:10:22 -0800404 std::get<uint64_t>(findCount->second), sensorList,
James Feist7b18b1e2019-05-14 13:42:09 -0700405 objectServer, pathPair.first));
James Feistdc6c55f2018-10-31 12:53:20 -0700406
407 return;
408 }
409 }
410 }
411 },
412 "xyz.openbmc_project.EntityManager", "/",
413 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
414}
415
James Feistb6c0b912019-07-09 12:21:44 -0700416int main()
James Feist6714a252018-09-10 15:26:18 -0700417{
418 boost::asio::io_service io;
419 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
420 systemBus->request_name("xyz.openbmc_project.FanSensor");
421 sdbusplus::asio::object_server objectServer(systemBus);
422 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>
423 tachSensors;
424 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
425 pwmSensors;
426 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
427 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
428 std::make_unique<boost::container::flat_set<std::string>>();
429
430 io.post([&]() {
431 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
432 nullptr);
James Feistdc6c55f2018-10-31 12:53:20 -0700433 createRedundancySensor(tachSensors, systemBus, objectServer);
James Feist6714a252018-09-10 15:26:18 -0700434 });
435
436 boost::asio::deadline_timer filterTimer(io);
437 std::function<void(sdbusplus::message::message&)> eventHandler =
438 [&](sdbusplus::message::message& message) {
439 if (message.is_method_error())
440 {
441 std::cerr << "callback method error\n";
442 return;
443 }
444 sensorsChanged->insert(message.get_path());
445 // this implicitly cancels the timer
446 filterTimer.expires_from_now(boost::posix_time::seconds(1));
447
448 filterTimer.async_wait([&](const boost::system::error_code& ec) {
449 if (ec == boost::asio::error::operation_aborted)
450 {
451 /* we were canceled*/
452 return;
453 }
454 else if (ec)
455 {
456 std::cerr << "timer error\n";
457 return;
458 }
459 createSensors(io, objectServer, tachSensors, pwmSensors,
460 systemBus, sensorsChanged);
461 });
462 };
463
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700464 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700465 {
466 auto match = std::make_unique<sdbusplus::bus::match::match>(
467 static_cast<sdbusplus::bus::bus&>(*systemBus),
468 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700469 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700470 eventHandler);
471 matches.emplace_back(std::move(match));
472 }
473
James Feistdc6c55f2018-10-31 12:53:20 -0700474 // redundancy sensor
475 std::function<void(sdbusplus::message::message&)> redundancyHandler =
476 [&tachSensors, &systemBus,
James Feistb6c0b912019-07-09 12:21:44 -0700477 &objectServer](sdbusplus::message::message&) {
James Feistdc6c55f2018-10-31 12:53:20 -0700478 createRedundancySensor(tachSensors, systemBus, objectServer);
479 };
480 auto match = std::make_unique<sdbusplus::bus::match::match>(
481 static_cast<sdbusplus::bus::bus&>(*systemBus),
482 "type='signal',member='PropertiesChanged',path_namespace='" +
483 std::string(inventoryPath) + "',arg0namespace='" +
484 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700485 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700486 matches.emplace_back(std::move(match));
487
James Feist6714a252018-09-10 15:26:18 -0700488 io.run();
489}