blob: cab1c8066ae0b20addabac2b2d6438fa22e1e6ca [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
James Feistcf3bce62019-01-08 10:07:19 -080017#include "filesystem.hpp"
18
James Feist6714a252018-09-10 15:26:18 -070019#include <PwmSensor.hpp>
20#include <TachSensor.hpp>
21#include <Utils.hpp>
22#include <VariantVisitors.hpp>
23#include <boost/algorithm/string/predicate.hpp>
24#include <boost/algorithm/string/replace.hpp>
25#include <boost/container/flat_set.hpp>
26#include <boost/lexical_cast.hpp>
James Feist6714a252018-09-10 15:26:18 -070027#include <fstream>
28#include <regex>
29#include <sdbusplus/asio/connection.hpp>
30#include <sdbusplus/asio/object_server.hpp>
31
32static constexpr bool DEBUG = false;
33
James Feistcf3bce62019-01-08 10:07:19 -080034namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080035
James Feist95b079b2018-11-21 09:28:00 -080036static constexpr std::array<const char*, 2> sensorTypes = {
37 "xyz.openbmc_project.Configuration.AspeedFan",
38 "xyz.openbmc_project.Configuration.I2CFan"};
James Feistdc6c55f2018-10-31 12:53:20 -070039constexpr const char* redundancyConfiguration =
40 "xyz.openbmc_project.Configuration.FanRedundancy";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070041static std::regex inputRegex(R"(fan(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070042
James Feist95b079b2018-11-21 09:28:00 -080043enum class FanTypes
44{
45 aspeed,
46 i2c
47};
48
James Feistdc6c55f2018-10-31 12:53:20 -070049// todo: power supply fan redundancy
James Feist95b079b2018-11-21 09:28:00 -080050std::shared_ptr<RedundancySensor> systemRedundancy = nullptr;
51
52FanTypes getFanType(const fs::path& parentPath)
53{
54 fs::path linkPath = parentPath / "device";
55 std::string canonical = fs::read_symlink(linkPath);
56 if (boost::ends_with(canonical, "1e786000.pwm-tacho-controller"))
57 {
58 return FanTypes::aspeed;
59 }
60 // todo: will we need to support other types?
61 return FanTypes::i2c;
62}
James Feistdc6c55f2018-10-31 12:53:20 -070063
James Feist6714a252018-09-10 15:26:18 -070064void createSensors(
65 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
66 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
67 tachSensors,
68 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
69 pwmSensors,
70 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
71 const std::unique_ptr<boost::container::flat_set<std::string>>&
72 sensorsChanged)
73{
74 bool firstScan = sensorsChanged == nullptr;
75 // use new data the first time, then refresh
76 ManagedObjectType sensorConfigurations;
77 bool useCache = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070078 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -070079 {
80 if (!getSensorConfiguration(type, dbusConnection, sensorConfigurations,
81 useCache))
82 {
83 std::cerr << "error communicating to entity manager\n";
84 return;
85 }
86 useCache = true;
87 }
88 std::vector<fs::path> paths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070089 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)", paths))
James Feist6714a252018-09-10 15:26:18 -070090 {
91 std::cerr << "No temperature sensors in system\n";
92 return;
93 }
94
James Feist82bac4c2019-03-11 11:16:53 -070095 std::vector<std::pair<uint8_t, std::string>> pwmNumbers;
James Feist8e94c202019-02-12 10:09:23 -080096
James Feist6714a252018-09-10 15:26:18 -070097 // iterate through all found fan sensors, and try to match them with
98 // configuration
James Feist95b079b2018-11-21 09:28:00 -080099 for (const auto& path : paths)
James Feist6714a252018-09-10 15:26:18 -0700100 {
101 std::smatch match;
102 std::string pathStr = path.string();
103
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700104 std::regex_search(pathStr, match, inputRegex);
James Feist6714a252018-09-10 15:26:18 -0700105 std::string indexStr = *(match.begin() + 1);
106
107 auto directory = path.parent_path();
James Feist95b079b2018-11-21 09:28:00 -0800108 FanTypes fanType = getFanType(directory);
109 size_t bus = 0;
110 size_t address = 0;
111 if (fanType == FanTypes::i2c)
112 {
113 std::string link =
114 fs::read_symlink(directory / "device").filename();
115
116 size_t findDash = link.find("-");
117 if (findDash == std::string::npos || link.size() <= findDash + 1)
118 {
119 std::cerr << "Error finding device from symlink";
120 }
121 bus = std::stoi(link.substr(0, findDash));
122 address = std::stoi(link.substr(findDash + 1), nullptr, 16);
123 }
James Feist6714a252018-09-10 15:26:18 -0700124 // convert to 0 based
125 size_t index = std::stoul(indexStr) - 1;
126
127 const char* baseType;
128 const SensorData* sensorData = nullptr;
129 const std::string* interfacePath = nullptr;
James Feist87d713a2018-12-06 16:06:24 -0800130 const SensorBaseConfiguration* baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700131 for (const std::pair<sdbusplus::message::object_path, SensorData>&
132 sensor : sensorConfigurations)
133 {
134 // find the base of the configuration to see if indexes match
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700135 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700136 {
137 auto sensorBaseFind = sensor.second.find(type);
138 if (sensorBaseFind != sensor.second.end())
139 {
140 baseConfiguration = &(*sensorBaseFind);
141 interfacePath = &(sensor.first.str);
142 baseType = type;
143 break;
144 }
145 }
146 if (baseConfiguration == nullptr)
147 {
148 continue;
149 }
James Feist6714a252018-09-10 15:26:18 -0700150 auto findIndex = baseConfiguration->second.find("Index");
151 if (findIndex == baseConfiguration->second.end())
152 {
153 std::cerr << baseConfiguration->first << " missing index\n";
154 continue;
155 }
James Feist3eb82622019-02-08 13:10:22 -0800156 unsigned int configIndex =
157 std::visit(VariantToUnsignedIntVisitor(), findIndex->second);
James Feist6714a252018-09-10 15:26:18 -0700158 if (configIndex != index)
159 {
160 continue;
161 }
James Feist95b079b2018-11-21 09:28:00 -0800162 if (fanType == FanTypes::aspeed)
James Feist6714a252018-09-10 15:26:18 -0700163 {
James Feist95b079b2018-11-21 09:28:00 -0800164 // there will be only 1 aspeed sensor object in sysfs, we found
165 // the fan
James Feist6714a252018-09-10 15:26:18 -0700166 sensorData = &(sensor.second);
167 break;
168 }
James Feist95b079b2018-11-21 09:28:00 -0800169 else if (baseType == "xyz.openbmc_project.Configuration.I2CFan")
170 {
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
185 if (configBus == bus && configAddress == configAddress)
186 {
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 Feist7bc2bab2018-10-26 14:09:45 -0700255 presenceSensor =
256 std::make_unique<PresenceSensor>(index, inverted, io);
257 }
258 }
James Feist95b079b2018-11-21 09:28:00 -0800259 std::shared_ptr<RedundancySensor> redundancy;
260 if (fanType == FanTypes::aspeed)
261 {
262 redundancy = systemRedundancy;
263 }
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
318 pwmSensors.insert(std::pair<std::string, std::unique_ptr<PwmSensor>>(
319 pwm.string(),
James Feist82bac4c2019-03-11 11:16:53 -0700320 std::make_unique<PwmSensor>(pwm.string(), objectServer, *path)));
James Feist6714a252018-09-10 15:26:18 -0700321 }
322}
323
James Feistdc6c55f2018-10-31 12:53:20 -0700324void createRedundancySensor(
325 const boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
326 sensors,
327 std::shared_ptr<sdbusplus::asio::connection> conn,
328 sdbusplus::asio::object_server& objectServer)
329{
330
331 conn->async_method_call(
332 [&objectServer, &sensors](boost::system::error_code& ec,
333 const ManagedObjectType managedObj) {
334 if (ec)
335 {
336 std::cerr << "Error calling entity manager \n";
337 return;
338 }
339 for (const auto& pathPair : managedObj)
340 {
341 for (const auto& interfacePair : pathPair.second)
342 {
343 if (interfacePair.first == redundancyConfiguration)
344 {
345 // currently only support one
346 auto findCount =
347 interfacePair.second.find("AllowedFailures");
348 if (findCount == interfacePair.second.end())
349 {
350 std::cerr << "Malformed redundancy record \n";
351 return;
352 }
353 std::vector<std::string> sensorList;
354
355 for (const auto& sensor : sensors)
356 {
357 sensorList.push_back(
358 "/xyz/openbmc_project/sensors/fan_tach/" +
359 sensor.second->name);
360 }
James Feist3a1807e2019-03-15 11:09:39 -0700361 systemRedundancy = nullptr;
362 systemRedundancy = std::make_shared<RedundancySensor>(
James Feist3eb82622019-02-08 13:10:22 -0800363 std::get<uint64_t>(findCount->second), sensorList,
James Feist9bb67462019-03-15 15:09:50 -0700364 objectServer, pathPair.first);
James Feistdc6c55f2018-10-31 12:53:20 -0700365
366 return;
367 }
368 }
369 }
370 },
371 "xyz.openbmc_project.EntityManager", "/",
372 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
373}
374
James Feist6714a252018-09-10 15:26:18 -0700375int main(int argc, char** argv)
376{
377 boost::asio::io_service io;
378 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
379 systemBus->request_name("xyz.openbmc_project.FanSensor");
380 sdbusplus::asio::object_server objectServer(systemBus);
381 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>
382 tachSensors;
383 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
384 pwmSensors;
385 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
386 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
387 std::make_unique<boost::container::flat_set<std::string>>();
388
389 io.post([&]() {
390 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
391 nullptr);
James Feistdc6c55f2018-10-31 12:53:20 -0700392 createRedundancySensor(tachSensors, systemBus, objectServer);
James Feist6714a252018-09-10 15:26:18 -0700393 });
394
395 boost::asio::deadline_timer filterTimer(io);
396 std::function<void(sdbusplus::message::message&)> eventHandler =
397 [&](sdbusplus::message::message& message) {
398 if (message.is_method_error())
399 {
400 std::cerr << "callback method error\n";
401 return;
402 }
403 sensorsChanged->insert(message.get_path());
404 // this implicitly cancels the timer
405 filterTimer.expires_from_now(boost::posix_time::seconds(1));
406
407 filterTimer.async_wait([&](const boost::system::error_code& ec) {
408 if (ec == boost::asio::error::operation_aborted)
409 {
410 /* we were canceled*/
411 return;
412 }
413 else if (ec)
414 {
415 std::cerr << "timer error\n";
416 return;
417 }
418 createSensors(io, objectServer, tachSensors, pwmSensors,
419 systemBus, sensorsChanged);
420 });
421 };
422
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700423 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700424 {
425 auto match = std::make_unique<sdbusplus::bus::match::match>(
426 static_cast<sdbusplus::bus::bus&>(*systemBus),
427 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700428 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700429 eventHandler);
430 matches.emplace_back(std::move(match));
431 }
432
James Feistdc6c55f2018-10-31 12:53:20 -0700433 // redundancy sensor
434 std::function<void(sdbusplus::message::message&)> redundancyHandler =
435 [&tachSensors, &systemBus,
436 &objectServer](sdbusplus::message::message& message) {
437 createRedundancySensor(tachSensors, systemBus, objectServer);
438 };
439 auto match = std::make_unique<sdbusplus::bus::match::match>(
440 static_cast<sdbusplus::bus::bus&>(*systemBus),
441 "type='signal',member='PropertiesChanged',path_namespace='" +
442 std::string(inventoryPath) + "',arg0namespace='" +
443 redundancyConfiguration + "'",
444 redundancyHandler);
445 matches.emplace_back(std::move(match));
446
James Feist6714a252018-09-10 15:26:18 -0700447 io.run();
448}