blob: b0237c20fdf4fc8cb3f93c54a764a1c21b8f9db6 [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
17#include <PwmSensor.hpp>
18#include <TachSensor.hpp>
19#include <Utils.hpp>
20#include <VariantVisitors.hpp>
21#include <boost/algorithm/string/predicate.hpp>
22#include <boost/algorithm/string/replace.hpp>
23#include <boost/container/flat_set.hpp>
24#include <boost/lexical_cast.hpp>
James Feist24f02f22019-04-15 11:05:39 -070025#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070026#include <fstream>
27#include <regex>
28#include <sdbusplus/asio/connection.hpp>
29#include <sdbusplus/asio/object_server.hpp>
30
31static constexpr bool DEBUG = false;
32
James Feistcf3bce62019-01-08 10:07:19 -080033namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080034
James Feist95b079b2018-11-21 09:28:00 -080035static constexpr std::array<const char*, 2> sensorTypes = {
36 "xyz.openbmc_project.Configuration.AspeedFan",
37 "xyz.openbmc_project.Configuration.I2CFan"};
James Feistdc6c55f2018-10-31 12:53:20 -070038constexpr const char* redundancyConfiguration =
39 "xyz.openbmc_project.Configuration.FanRedundancy";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070040static std::regex inputRegex(R"(fan(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070041
James Feist95b079b2018-11-21 09:28:00 -080042enum class FanTypes
43{
44 aspeed,
45 i2c
46};
47
James Feistdc6c55f2018-10-31 12:53:20 -070048// todo: power supply fan redundancy
James Feist7b18b1e2019-05-14 13:42:09 -070049std::optional<RedundancySensor> systemRedundancy;
James Feist95b079b2018-11-21 09:28:00 -080050
51FanTypes getFanType(const fs::path& parentPath)
52{
53 fs::path linkPath = parentPath / "device";
54 std::string canonical = fs::read_symlink(linkPath);
55 if (boost::ends_with(canonical, "1e786000.pwm-tacho-controller"))
56 {
57 return FanTypes::aspeed;
58 }
59 // todo: will we need to support other types?
60 return FanTypes::i2c;
61}
James Feistdc6c55f2018-10-31 12:53:20 -070062
James Feist6714a252018-09-10 15:26:18 -070063void createSensors(
64 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
65 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
66 tachSensors,
67 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
68 pwmSensors,
69 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
70 const std::unique_ptr<boost::container::flat_set<std::string>>&
71 sensorsChanged)
72{
73 bool firstScan = sensorsChanged == nullptr;
74 // use new data the first time, then refresh
75 ManagedObjectType sensorConfigurations;
76 bool useCache = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070077 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -070078 {
79 if (!getSensorConfiguration(type, dbusConnection, sensorConfigurations,
80 useCache))
81 {
82 std::cerr << "error communicating to entity manager\n";
83 return;
84 }
85 useCache = true;
86 }
87 std::vector<fs::path> paths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070088 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)", paths))
James Feist6714a252018-09-10 15:26:18 -070089 {
90 std::cerr << "No temperature sensors in system\n";
91 return;
92 }
93
James Feist82bac4c2019-03-11 11:16:53 -070094 std::vector<std::pair<uint8_t, std::string>> pwmNumbers;
James Feist8e94c202019-02-12 10:09:23 -080095
James Feist6714a252018-09-10 15:26:18 -070096 // iterate through all found fan sensors, and try to match them with
97 // configuration
James Feist95b079b2018-11-21 09:28:00 -080098 for (const auto& path : paths)
James Feist6714a252018-09-10 15:26:18 -070099 {
100 std::smatch match;
101 std::string pathStr = path.string();
102
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700103 std::regex_search(pathStr, match, inputRegex);
James Feist6714a252018-09-10 15:26:18 -0700104 std::string indexStr = *(match.begin() + 1);
105
106 auto directory = path.parent_path();
James Feist95b079b2018-11-21 09:28:00 -0800107 FanTypes fanType = getFanType(directory);
108 size_t bus = 0;
109 size_t address = 0;
110 if (fanType == FanTypes::i2c)
111 {
112 std::string link =
113 fs::read_symlink(directory / "device").filename();
114
115 size_t findDash = link.find("-");
116 if (findDash == std::string::npos || link.size() <= findDash + 1)
117 {
118 std::cerr << "Error finding device from symlink";
119 }
120 bus = std::stoi(link.substr(0, findDash));
121 address = std::stoi(link.substr(findDash + 1), nullptr, 16);
122 }
James Feist6714a252018-09-10 15:26:18 -0700123 // convert to 0 based
124 size_t index = std::stoul(indexStr) - 1;
125
126 const char* baseType;
127 const SensorData* sensorData = nullptr;
128 const std::string* interfacePath = nullptr;
James Feist87d713a2018-12-06 16:06:24 -0800129 const SensorBaseConfiguration* baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700130 for (const std::pair<sdbusplus::message::object_path, SensorData>&
131 sensor : sensorConfigurations)
132 {
133 // find the base of the configuration to see if indexes match
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700134 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700135 {
136 auto sensorBaseFind = sensor.second.find(type);
137 if (sensorBaseFind != sensor.second.end())
138 {
139 baseConfiguration = &(*sensorBaseFind);
140 interfacePath = &(sensor.first.str);
141 baseType = type;
142 break;
143 }
144 }
145 if (baseConfiguration == nullptr)
146 {
147 continue;
148 }
James Feist6714a252018-09-10 15:26:18 -0700149 auto findIndex = baseConfiguration->second.find("Index");
150 if (findIndex == baseConfiguration->second.end())
151 {
152 std::cerr << baseConfiguration->first << " missing index\n";
153 continue;
154 }
James Feist3eb82622019-02-08 13:10:22 -0800155 unsigned int configIndex =
156 std::visit(VariantToUnsignedIntVisitor(), findIndex->second);
James Feist6714a252018-09-10 15:26:18 -0700157 if (configIndex != index)
158 {
159 continue;
160 }
James Feist95b079b2018-11-21 09:28:00 -0800161 if (fanType == FanTypes::aspeed)
James Feist6714a252018-09-10 15:26:18 -0700162 {
James Feist95b079b2018-11-21 09:28:00 -0800163 // there will be only 1 aspeed sensor object in sysfs, we found
164 // the fan
James Feist6714a252018-09-10 15:26:18 -0700165 sensorData = &(sensor.second);
166 break;
167 }
James Feistb6c0b912019-07-09 12:21:44 -0700168 else if (baseType ==
169 std::string("xyz.openbmc_project.Configuration.I2CFan"))
James Feist95b079b2018-11-21 09:28:00 -0800170 {
171 auto findBus = baseConfiguration->second.find("Bus");
172 auto findAddress = baseConfiguration->second.find("Address");
173 if (findBus == baseConfiguration->second.end() ||
174 findAddress == baseConfiguration->second.end())
175 {
176 std::cerr << baseConfiguration->first
177 << " missing bus or address\n";
178 continue;
179 }
James Feist3eb82622019-02-08 13:10:22 -0800180 unsigned int configBus =
181 std::visit(VariantToUnsignedIntVisitor(), findBus->second);
182 unsigned int configAddress = std::visit(
James Feist95b079b2018-11-21 09:28:00 -0800183 VariantToUnsignedIntVisitor(), findAddress->second);
184
Jae Hyun Yoo84e9e662019-04-22 13:30:42 -0700185 if (configBus == bus && configAddress == address)
James Feist95b079b2018-11-21 09:28:00 -0800186 {
187 sensorData = &(sensor.second);
188 break;
189 }
190 }
James Feist6714a252018-09-10 15:26:18 -0700191 }
192 if (sensorData == nullptr)
193 {
194 std::cerr << "failed to find match for " << path.string() << "\n";
195 continue;
196 }
197
198 auto findSensorName = baseConfiguration->second.find("Name");
199 if (findSensorName == baseConfiguration->second.end())
200 {
201 std::cerr << "could not determine configuration name for "
202 << path.string() << "\n";
203 continue;
204 }
James Feist3eb82622019-02-08 13:10:22 -0800205 std::string sensorName = std::get<std::string>(findSensorName->second);
James Feist6714a252018-09-10 15:26:18 -0700206 // on rescans, only update sensors we were signaled by
207 auto findSensor = tachSensors.find(sensorName);
208 if (!firstScan && findSensor != tachSensors.end())
209 {
210 bool found = false;
211 for (auto it = sensorsChanged->begin(); it != sensorsChanged->end();
212 it++)
213 {
214 if (boost::ends_with(*it, findSensor->second->name))
215 {
216 sensorsChanged->erase(it);
217 findSensor->second = nullptr;
218 found = true;
219 break;
220 }
221 }
222 if (!found)
223 {
224 continue;
225 }
226 }
227 std::vector<thresholds::Threshold> sensorThresholds;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700228 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
James Feist6714a252018-09-10 15:26:18 -0700229 {
230 std::cerr << "error populating thresholds for " << sensorName
231 << "\n";
232 }
233
James Feist7bc2bab2018-10-26 14:09:45 -0700234 auto presenceConfig =
235 sensorData->find(baseType + std::string(".Presence"));
236
237 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
238
239 // presence sensors are optional
240 if (presenceConfig != sensorData->end())
241 {
242 auto findIndex = presenceConfig->second.find("Index");
243 auto findPolarity = presenceConfig->second.find("Polarity");
244
245 if (findIndex == presenceConfig->second.end() ||
246 findPolarity == presenceConfig->second.end())
247 {
248 std::cerr << "Malformed Presence Configuration\n";
249 }
250 else
251 {
James Feist3eb82622019-02-08 13:10:22 -0800252 size_t index = std::get<uint64_t>(findIndex->second);
James Feist7bc2bab2018-10-26 14:09:45 -0700253 bool inverted =
James Feist3eb82622019-02-08 13:10:22 -0800254 std::get<std::string>(findPolarity->second) == "Low";
James Feist7b18b1e2019-05-14 13:42:09 -0700255 presenceSensor = std::make_unique<PresenceSensor>(
256 index, inverted, io, sensorName);
James Feist7bc2bab2018-10-26 14:09:45 -0700257 }
258 }
James Feist7b18b1e2019-05-14 13:42:09 -0700259 std::optional<RedundancySensor>* redundancy = nullptr;
James Feist95b079b2018-11-21 09:28:00 -0800260 if (fanType == FanTypes::aspeed)
261 {
James Feist7b18b1e2019-05-14 13:42:09 -0700262 redundancy = &systemRedundancy;
James Feist95b079b2018-11-21 09:28:00 -0800263 }
James Feist7bc2bab2018-10-26 14:09:45 -0700264
James Feist87d713a2018-12-06 16:06:24 -0800265 constexpr double defaultMaxReading = 25000;
266 constexpr double defaultMinReading = 0;
267 auto limits = std::make_pair(defaultMinReading, defaultMaxReading);
268
269 findLimits(limits, baseConfiguration);
James Feist6714a252018-09-10 15:26:18 -0700270 tachSensors[sensorName] = std::make_unique<TachSensor>(
James Feistce3fca42018-11-21 12:58:24 -0800271 path.string(), baseType, objectServer, dbusConnection,
James Feist95b079b2018-11-21 09:28:00 -0800272 std::move(presenceSensor), redundancy, io, sensorName,
James Feist87d713a2018-12-06 16:06:24 -0800273 std::move(sensorThresholds), *interfacePath, limits);
James Feist8e94c202019-02-12 10:09:23 -0800274
275 auto connector = sensorData->find(baseType + std::string(".Connector"));
276 if (connector != sensorData->end())
277 {
278 auto findPwm = connector->second.find("Pwm");
279 if (findPwm == connector->second.end())
280 {
281 std::cerr << "Connector Missing PWM!\n";
282 continue;
283 }
284
285 size_t pwm =
286 std::visit(VariantToUnsignedIntVisitor(), findPwm->second);
James Feist82bac4c2019-03-11 11:16:53 -0700287 pwmNumbers.emplace_back(pwm, *interfacePath);
James Feist8e94c202019-02-12 10:09:23 -0800288 }
James Feist6714a252018-09-10 15:26:18 -0700289 }
290 std::vector<fs::path> pwms;
James Feist8e94c202019-02-12 10:09:23 -0800291 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+$)", pwms))
James Feist6714a252018-09-10 15:26:18 -0700292 {
293 std::cerr << "No pwm in system\n";
294 return;
295 }
296 for (const fs::path& pwm : pwms)
297 {
James Feist95b079b2018-11-21 09:28:00 -0800298 if (pwmSensors.find(pwm) != pwmSensors.end())
299 {
300 continue;
301 }
James Feist82bac4c2019-03-11 11:16:53 -0700302 const std::string* path = nullptr;
303 for (const auto& [index, configPath] : pwmNumbers)
James Feist8e94c202019-02-12 10:09:23 -0800304 {
305 if (boost::ends_with(pwm.string(), std::to_string(index + 1)))
306 {
James Feist82bac4c2019-03-11 11:16:53 -0700307 path = &configPath;
James Feist8e94c202019-02-12 10:09:23 -0800308 break;
309 }
310 }
311
James Feist82bac4c2019-03-11 11:16:53 -0700312 if (path == nullptr)
James Feist8e94c202019-02-12 10:09:23 -0800313 {
314 continue;
315 }
316
James Feist6714a252018-09-10 15:26:18 -0700317 // only add new elements
Cheng C Yang916360b2019-05-07 18:47:16 +0800318 const std::string& sysPath = pwm.string();
319 const std::string& pwmName =
320 "Pwm_" + sysPath.substr(sysPath.find_last_of("pwm") + 1);
James Feist6714a252018-09-10 15:26:18 -0700321 pwmSensors.insert(std::pair<std::string, std::unique_ptr<PwmSensor>>(
Cheng C Yang916360b2019-05-07 18:47:16 +0800322 sysPath, std::make_unique<PwmSensor>(pwmName, sysPath, objectServer,
323 *path)));
James Feist6714a252018-09-10 15:26:18 -0700324 }
325}
326
James Feistdc6c55f2018-10-31 12:53:20 -0700327void createRedundancySensor(
328 const boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
329 sensors,
330 std::shared_ptr<sdbusplus::asio::connection> conn,
331 sdbusplus::asio::object_server& objectServer)
332{
333
334 conn->async_method_call(
335 [&objectServer, &sensors](boost::system::error_code& ec,
336 const ManagedObjectType managedObj) {
337 if (ec)
338 {
339 std::cerr << "Error calling entity manager \n";
340 return;
341 }
342 for (const auto& pathPair : managedObj)
343 {
344 for (const auto& interfacePair : pathPair.second)
345 {
346 if (interfacePair.first == redundancyConfiguration)
347 {
348 // currently only support one
349 auto findCount =
350 interfacePair.second.find("AllowedFailures");
351 if (findCount == interfacePair.second.end())
352 {
353 std::cerr << "Malformed redundancy record \n";
354 return;
355 }
356 std::vector<std::string> sensorList;
357
358 for (const auto& sensor : sensors)
359 {
360 sensorList.push_back(
361 "/xyz/openbmc_project/sensors/fan_tach/" +
362 sensor.second->name);
363 }
James Feist7b18b1e2019-05-14 13:42:09 -0700364 systemRedundancy.reset();
365 systemRedundancy.emplace(RedundancySensor(
James Feist3eb82622019-02-08 13:10:22 -0800366 std::get<uint64_t>(findCount->second), sensorList,
James Feist7b18b1e2019-05-14 13:42:09 -0700367 objectServer, pathPair.first));
James Feistdc6c55f2018-10-31 12:53:20 -0700368
369 return;
370 }
371 }
372 }
373 },
374 "xyz.openbmc_project.EntityManager", "/",
375 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
376}
377
James Feistb6c0b912019-07-09 12:21:44 -0700378int main()
James Feist6714a252018-09-10 15:26:18 -0700379{
380 boost::asio::io_service io;
381 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
382 systemBus->request_name("xyz.openbmc_project.FanSensor");
383 sdbusplus::asio::object_server objectServer(systemBus);
384 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>
385 tachSensors;
386 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
387 pwmSensors;
388 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
389 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
390 std::make_unique<boost::container::flat_set<std::string>>();
391
392 io.post([&]() {
393 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
394 nullptr);
James Feistdc6c55f2018-10-31 12:53:20 -0700395 createRedundancySensor(tachSensors, systemBus, objectServer);
James Feist6714a252018-09-10 15:26:18 -0700396 });
397
398 boost::asio::deadline_timer filterTimer(io);
399 std::function<void(sdbusplus::message::message&)> eventHandler =
400 [&](sdbusplus::message::message& message) {
401 if (message.is_method_error())
402 {
403 std::cerr << "callback method error\n";
404 return;
405 }
406 sensorsChanged->insert(message.get_path());
407 // this implicitly cancels the timer
408 filterTimer.expires_from_now(boost::posix_time::seconds(1));
409
410 filterTimer.async_wait([&](const boost::system::error_code& ec) {
411 if (ec == boost::asio::error::operation_aborted)
412 {
413 /* we were canceled*/
414 return;
415 }
416 else if (ec)
417 {
418 std::cerr << "timer error\n";
419 return;
420 }
421 createSensors(io, objectServer, tachSensors, pwmSensors,
422 systemBus, sensorsChanged);
423 });
424 };
425
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700426 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700427 {
428 auto match = std::make_unique<sdbusplus::bus::match::match>(
429 static_cast<sdbusplus::bus::bus&>(*systemBus),
430 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700431 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700432 eventHandler);
433 matches.emplace_back(std::move(match));
434 }
435
James Feistdc6c55f2018-10-31 12:53:20 -0700436 // redundancy sensor
437 std::function<void(sdbusplus::message::message&)> redundancyHandler =
438 [&tachSensors, &systemBus,
James Feistb6c0b912019-07-09 12:21:44 -0700439 &objectServer](sdbusplus::message::message&) {
James Feistdc6c55f2018-10-31 12:53:20 -0700440 createRedundancySensor(tachSensors, systemBus, objectServer);
441 };
442 auto match = std::make_unique<sdbusplus::bus::match::match>(
443 static_cast<sdbusplus::bus::bus&>(*systemBus),
444 "type='signal',member='PropertiesChanged',path_namespace='" +
445 std::string(inventoryPath) + "',arg0namespace='" +
446 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700447 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700448 matches.emplace_back(std::move(match));
449
James Feist6714a252018-09-10 15:26:18 -0700450 io.run();
451}