blob: 866fe4ffbb2088dec12509fac3d118c15dd5253e [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 Feist95b079b2018-11-21 09:28:00 -080049std::shared_ptr<RedundancySensor> systemRedundancy = nullptr;
50
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 Feist95b079b2018-11-21 09:28:00 -0800168 else if (baseType == "xyz.openbmc_project.Configuration.I2CFan")
169 {
170 auto findBus = baseConfiguration->second.find("Bus");
171 auto findAddress = baseConfiguration->second.find("Address");
172 if (findBus == baseConfiguration->second.end() ||
173 findAddress == baseConfiguration->second.end())
174 {
175 std::cerr << baseConfiguration->first
176 << " missing bus or address\n";
177 continue;
178 }
James Feist3eb82622019-02-08 13:10:22 -0800179 unsigned int configBus =
180 std::visit(VariantToUnsignedIntVisitor(), findBus->second);
181 unsigned int configAddress = std::visit(
James Feist95b079b2018-11-21 09:28:00 -0800182 VariantToUnsignedIntVisitor(), findAddress->second);
183
Jae Hyun Yoo84e9e662019-04-22 13:30:42 -0700184 if (configBus == bus && configAddress == address)
James Feist95b079b2018-11-21 09:28:00 -0800185 {
186 sensorData = &(sensor.second);
187 break;
188 }
189 }
James Feist6714a252018-09-10 15:26:18 -0700190 }
191 if (sensorData == nullptr)
192 {
193 std::cerr << "failed to find match for " << path.string() << "\n";
194 continue;
195 }
196
197 auto findSensorName = baseConfiguration->second.find("Name");
198 if (findSensorName == baseConfiguration->second.end())
199 {
200 std::cerr << "could not determine configuration name for "
201 << path.string() << "\n";
202 continue;
203 }
James Feist3eb82622019-02-08 13:10:22 -0800204 std::string sensorName = std::get<std::string>(findSensorName->second);
James Feist6714a252018-09-10 15:26:18 -0700205 // on rescans, only update sensors we were signaled by
206 auto findSensor = tachSensors.find(sensorName);
207 if (!firstScan && findSensor != tachSensors.end())
208 {
209 bool found = false;
210 for (auto it = sensorsChanged->begin(); it != sensorsChanged->end();
211 it++)
212 {
213 if (boost::ends_with(*it, findSensor->second->name))
214 {
215 sensorsChanged->erase(it);
216 findSensor->second = nullptr;
217 found = true;
218 break;
219 }
220 }
221 if (!found)
222 {
223 continue;
224 }
225 }
226 std::vector<thresholds::Threshold> sensorThresholds;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700227 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
James Feist6714a252018-09-10 15:26:18 -0700228 {
229 std::cerr << "error populating thresholds for " << sensorName
230 << "\n";
231 }
232
James Feist7bc2bab2018-10-26 14:09:45 -0700233 auto presenceConfig =
234 sensorData->find(baseType + std::string(".Presence"));
235
236 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
237
238 // presence sensors are optional
239 if (presenceConfig != sensorData->end())
240 {
241 auto findIndex = presenceConfig->second.find("Index");
242 auto findPolarity = presenceConfig->second.find("Polarity");
243
244 if (findIndex == presenceConfig->second.end() ||
245 findPolarity == presenceConfig->second.end())
246 {
247 std::cerr << "Malformed Presence Configuration\n";
248 }
249 else
250 {
James Feist3eb82622019-02-08 13:10:22 -0800251 size_t index = std::get<uint64_t>(findIndex->second);
James Feist7bc2bab2018-10-26 14:09:45 -0700252 bool inverted =
James Feist3eb82622019-02-08 13:10:22 -0800253 std::get<std::string>(findPolarity->second) == "Low";
James Feist7bc2bab2018-10-26 14:09:45 -0700254 presenceSensor =
255 std::make_unique<PresenceSensor>(index, inverted, io);
256 }
257 }
James Feist95b079b2018-11-21 09:28:00 -0800258 std::shared_ptr<RedundancySensor> redundancy;
259 if (fanType == FanTypes::aspeed)
260 {
261 redundancy = systemRedundancy;
262 }
James Feist7bc2bab2018-10-26 14:09:45 -0700263
James Feist87d713a2018-12-06 16:06:24 -0800264 constexpr double defaultMaxReading = 25000;
265 constexpr double defaultMinReading = 0;
266 auto limits = std::make_pair(defaultMinReading, defaultMaxReading);
267
268 findLimits(limits, baseConfiguration);
James Feist6714a252018-09-10 15:26:18 -0700269 tachSensors[sensorName] = std::make_unique<TachSensor>(
James Feistce3fca42018-11-21 12:58:24 -0800270 path.string(), baseType, objectServer, dbusConnection,
James Feist95b079b2018-11-21 09:28:00 -0800271 std::move(presenceSensor), redundancy, io, sensorName,
James Feist87d713a2018-12-06 16:06:24 -0800272 std::move(sensorThresholds), *interfacePath, limits);
James Feist8e94c202019-02-12 10:09:23 -0800273
274 auto connector = sensorData->find(baseType + std::string(".Connector"));
275 if (connector != sensorData->end())
276 {
277 auto findPwm = connector->second.find("Pwm");
278 if (findPwm == connector->second.end())
279 {
280 std::cerr << "Connector Missing PWM!\n";
281 continue;
282 }
283
284 size_t pwm =
285 std::visit(VariantToUnsignedIntVisitor(), findPwm->second);
James Feist82bac4c2019-03-11 11:16:53 -0700286 pwmNumbers.emplace_back(pwm, *interfacePath);
James Feist8e94c202019-02-12 10:09:23 -0800287 }
James Feist6714a252018-09-10 15:26:18 -0700288 }
289 std::vector<fs::path> pwms;
James Feist8e94c202019-02-12 10:09:23 -0800290 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+$)", pwms))
James Feist6714a252018-09-10 15:26:18 -0700291 {
292 std::cerr << "No pwm in system\n";
293 return;
294 }
295 for (const fs::path& pwm : pwms)
296 {
James Feist95b079b2018-11-21 09:28:00 -0800297 if (pwmSensors.find(pwm) != pwmSensors.end())
298 {
299 continue;
300 }
James Feist82bac4c2019-03-11 11:16:53 -0700301 const std::string* path = nullptr;
302 for (const auto& [index, configPath] : pwmNumbers)
James Feist8e94c202019-02-12 10:09:23 -0800303 {
304 if (boost::ends_with(pwm.string(), std::to_string(index + 1)))
305 {
James Feist82bac4c2019-03-11 11:16:53 -0700306 path = &configPath;
James Feist8e94c202019-02-12 10:09:23 -0800307 break;
308 }
309 }
310
James Feist82bac4c2019-03-11 11:16:53 -0700311 if (path == nullptr)
James Feist8e94c202019-02-12 10:09:23 -0800312 {
313 continue;
314 }
315
James Feist6714a252018-09-10 15:26:18 -0700316 // only add new elements
317 pwmSensors.insert(std::pair<std::string, std::unique_ptr<PwmSensor>>(
318 pwm.string(),
James Feist82bac4c2019-03-11 11:16:53 -0700319 std::make_unique<PwmSensor>(pwm.string(), objectServer, *path)));
James Feist6714a252018-09-10 15:26:18 -0700320 }
321}
322
James Feistdc6c55f2018-10-31 12:53:20 -0700323void createRedundancySensor(
324 const boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
325 sensors,
326 std::shared_ptr<sdbusplus::asio::connection> conn,
327 sdbusplus::asio::object_server& objectServer)
328{
329
330 conn->async_method_call(
331 [&objectServer, &sensors](boost::system::error_code& ec,
332 const ManagedObjectType managedObj) {
333 if (ec)
334 {
335 std::cerr << "Error calling entity manager \n";
336 return;
337 }
338 for (const auto& pathPair : managedObj)
339 {
340 for (const auto& interfacePair : pathPair.second)
341 {
342 if (interfacePair.first == redundancyConfiguration)
343 {
344 // currently only support one
345 auto findCount =
346 interfacePair.second.find("AllowedFailures");
347 if (findCount == interfacePair.second.end())
348 {
349 std::cerr << "Malformed redundancy record \n";
350 return;
351 }
352 std::vector<std::string> sensorList;
353
354 for (const auto& sensor : sensors)
355 {
356 sensorList.push_back(
357 "/xyz/openbmc_project/sensors/fan_tach/" +
358 sensor.second->name);
359 }
James Feist3a1807e2019-03-15 11:09:39 -0700360 systemRedundancy = nullptr;
361 systemRedundancy = std::make_shared<RedundancySensor>(
James Feist3eb82622019-02-08 13:10:22 -0800362 std::get<uint64_t>(findCount->second), sensorList,
James Feist9bb67462019-03-15 15:09:50 -0700363 objectServer, pathPair.first);
James Feistdc6c55f2018-10-31 12:53:20 -0700364
365 return;
366 }
367 }
368 }
369 },
370 "xyz.openbmc_project.EntityManager", "/",
371 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
372}
373
James Feist6714a252018-09-10 15:26:18 -0700374int main(int argc, char** argv)
375{
376 boost::asio::io_service io;
377 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
378 systemBus->request_name("xyz.openbmc_project.FanSensor");
379 sdbusplus::asio::object_server objectServer(systemBus);
380 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>
381 tachSensors;
382 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
383 pwmSensors;
384 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
385 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
386 std::make_unique<boost::container::flat_set<std::string>>();
387
388 io.post([&]() {
389 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
390 nullptr);
James Feistdc6c55f2018-10-31 12:53:20 -0700391 createRedundancySensor(tachSensors, systemBus, objectServer);
James Feist6714a252018-09-10 15:26:18 -0700392 });
393
394 boost::asio::deadline_timer filterTimer(io);
395 std::function<void(sdbusplus::message::message&)> eventHandler =
396 [&](sdbusplus::message::message& message) {
397 if (message.is_method_error())
398 {
399 std::cerr << "callback method error\n";
400 return;
401 }
402 sensorsChanged->insert(message.get_path());
403 // this implicitly cancels the timer
404 filterTimer.expires_from_now(boost::posix_time::seconds(1));
405
406 filterTimer.async_wait([&](const boost::system::error_code& ec) {
407 if (ec == boost::asio::error::operation_aborted)
408 {
409 /* we were canceled*/
410 return;
411 }
412 else if (ec)
413 {
414 std::cerr << "timer error\n";
415 return;
416 }
417 createSensors(io, objectServer, tachSensors, pwmSensors,
418 systemBus, sensorsChanged);
419 });
420 };
421
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700422 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700423 {
424 auto match = std::make_unique<sdbusplus::bus::match::match>(
425 static_cast<sdbusplus::bus::bus&>(*systemBus),
426 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700427 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700428 eventHandler);
429 matches.emplace_back(std::move(match));
430 }
431
James Feistdc6c55f2018-10-31 12:53:20 -0700432 // redundancy sensor
433 std::function<void(sdbusplus::message::message&)> redundancyHandler =
434 [&tachSensors, &systemBus,
435 &objectServer](sdbusplus::message::message& message) {
436 createRedundancySensor(tachSensors, systemBus, objectServer);
437 };
438 auto match = std::make_unique<sdbusplus::bus::match::match>(
439 static_cast<sdbusplus::bus::bus&>(*systemBus),
440 "type='signal',member='PropertiesChanged',path_namespace='" +
441 std::string(inventoryPath) + "',arg0namespace='" +
442 redundancyConfiguration + "'",
443 redundancyHandler);
444 matches.emplace_back(std::move(match));
445
James Feist6714a252018-09-10 15:26:18 -0700446 io.run();
447}