blob: a1868e22bdc5297a21aaf854a38c606ccf4b06e8 [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
James Feist6714a252018-09-10 15:26:18 -070022#include <boost/algorithm/string/predicate.hpp>
23#include <boost/algorithm/string/replace.hpp>
24#include <boost/container/flat_set.hpp>
25#include <boost/lexical_cast.hpp>
James Feist24f02f22019-04-15 11:05:39 -070026#include <filesystem>
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
Peter Lundgren8843b622019-09-12 10:33:41 -070036static constexpr std::array<const char*, 3> sensorTypes = {
James Feist95b079b2018-11-21 09:28:00 -080037 "xyz.openbmc_project.Configuration.AspeedFan",
Peter Lundgren8843b622019-09-12 10:33:41 -070038 "xyz.openbmc_project.Configuration.I2CFan",
39 "xyz.openbmc_project.Configuration.NuvotonFan"};
James Feistdc6c55f2018-10-31 12:53:20 -070040constexpr const char* redundancyConfiguration =
41 "xyz.openbmc_project.Configuration.FanRedundancy";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070042static std::regex inputRegex(R"(fan(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070043
James Feist95b079b2018-11-21 09:28:00 -080044enum class FanTypes
45{
46 aspeed,
Peter Lundgren8843b622019-09-12 10:33:41 -070047 i2c,
48 nuvoton
James Feist95b079b2018-11-21 09:28:00 -080049};
50
James Feistdc6c55f2018-10-31 12:53:20 -070051// todo: power supply fan redundancy
James Feist7b18b1e2019-05-14 13:42:09 -070052std::optional<RedundancySensor> systemRedundancy;
James Feist95b079b2018-11-21 09:28:00 -080053
54FanTypes getFanType(const fs::path& parentPath)
55{
56 fs::path linkPath = parentPath / "device";
57 std::string canonical = fs::read_symlink(linkPath);
58 if (boost::ends_with(canonical, "1e786000.pwm-tacho-controller"))
59 {
60 return FanTypes::aspeed;
61 }
Peter Lundgren8843b622019-09-12 10:33:41 -070062 else if (boost::ends_with(canonical, "f0103000.pwm-fan-controller"))
63 {
64 return FanTypes::nuvoton;
65 }
James Feist95b079b2018-11-21 09:28:00 -080066 // todo: will we need to support other types?
67 return FanTypes::i2c;
68}
James Feistdc6c55f2018-10-31 12:53:20 -070069
James Feist6714a252018-09-10 15:26:18 -070070void createSensors(
71 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
72 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
73 tachSensors,
74 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
75 pwmSensors,
76 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
77 const std::unique_ptr<boost::container::flat_set<std::string>>&
78 sensorsChanged)
79{
James Feist6714a252018-09-10 15:26:18 -070080
James Feistde5e9702019-09-18 16:13:02 -070081 auto getter = std::make_shared<GetSensorConfiguration>(
82 dbusConnection,
83 std::move([&io, &objectServer, &tachSensors, &pwmSensors,
84 &dbusConnection, &sensorsChanged](
85 const ManagedObjectType& sensorConfigurations) {
86 bool firstScan = sensorsChanged == nullptr;
87 std::vector<fs::path> paths;
88 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)",
89 paths))
James Feist95b079b2018-11-21 09:28:00 -080090 {
James Feistde5e9702019-09-18 16:13:02 -070091 std::cerr << "No temperature sensors in system\n";
92 return;
James Feist95b079b2018-11-21 09:28:00 -080093 }
James Feist6714a252018-09-10 15:26:18 -070094
James Feistde5e9702019-09-18 16:13:02 -070095 std::vector<std::pair<uint8_t, std::string>> pwmNumbers;
96
97 // iterate through all found fan sensors, and try to match them with
98 // configuration
99 for (const auto& path : paths)
James Feist6714a252018-09-10 15:26:18 -0700100 {
James Feistde5e9702019-09-18 16:13:02 -0700101 std::smatch match;
102 std::string pathStr = path.string();
103
104 std::regex_search(pathStr, match, inputRegex);
105 std::string indexStr = *(match.begin() + 1);
106
107 auto directory = path.parent_path();
108 FanTypes fanType = getFanType(directory);
109 size_t bus = 0;
110 size_t address = 0;
111 if (fanType == FanTypes::i2c)
James Feist6714a252018-09-10 15:26:18 -0700112 {
James Feistde5e9702019-09-18 16:13:02 -0700113 std::string link =
114 fs::read_symlink(directory / "device").filename();
115
116 size_t findDash = link.find("-");
117 if (findDash == std::string::npos ||
118 link.size() <= findDash + 1)
119 {
120 std::cerr << "Error finding device from symlink";
121 }
122 bus = std::stoi(link.substr(0, findDash));
123 address = std::stoi(link.substr(findDash + 1), nullptr, 16);
James Feist6714a252018-09-10 15:26:18 -0700124 }
James Feistde5e9702019-09-18 16:13:02 -0700125 // convert to 0 based
126 size_t index = std::stoul(indexStr) - 1;
127
128 const char* baseType;
129 const SensorData* sensorData = nullptr;
130 const std::string* interfacePath = nullptr;
131 const SensorBaseConfiguration* baseConfiguration = nullptr;
132 for (const std::pair<sdbusplus::message::object_path,
133 SensorData>& sensor : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800134 {
James Feistde5e9702019-09-18 16:13:02 -0700135 // find the base of the configuration to see if indexes
136 // match
137 for (const char* type : sensorTypes)
138 {
139 auto sensorBaseFind = sensor.second.find(type);
140 if (sensorBaseFind != sensor.second.end())
141 {
142 baseConfiguration = &(*sensorBaseFind);
143 interfacePath = &(sensor.first.str);
144 baseType = type;
145 break;
146 }
147 }
148 if (baseConfiguration == nullptr)
149 {
150 continue;
151 }
152 auto findIndex = baseConfiguration->second.find("Index");
153 if (findIndex == baseConfiguration->second.end())
154 {
155 std::cerr << baseConfiguration->first
156 << " missing index\n";
157 continue;
158 }
159 unsigned int configIndex = std::visit(
160 VariantToUnsignedIntVisitor(), findIndex->second);
161 if (configIndex != index)
162 {
163 continue;
164 }
165 if (fanType == FanTypes::aspeed ||
166 fanType == FanTypes::nuvoton)
167 {
168 // there will be only 1 aspeed or nuvoton sensor object
169 // in sysfs, we found the fan
170 sensorData = &(sensor.second);
171 break;
172 }
173 else if (baseType ==
174 std::string(
175 "xyz.openbmc_project.Configuration.I2CFan"))
176 {
177 auto findBus = baseConfiguration->second.find("Bus");
178 auto findAddress =
179 baseConfiguration->second.find("Address");
180 if (findBus == baseConfiguration->second.end() ||
181 findAddress == baseConfiguration->second.end())
182 {
183 std::cerr << baseConfiguration->first
184 << " missing bus or address\n";
185 continue;
186 }
187 unsigned int configBus = std::visit(
188 VariantToUnsignedIntVisitor(), findBus->second);
189 unsigned int configAddress = std::visit(
190 VariantToUnsignedIntVisitor(), findAddress->second);
191
192 if (configBus == bus && configAddress == address)
193 {
194 sensorData = &(sensor.second);
195 break;
196 }
197 }
198 }
199 if (sensorData == nullptr)
200 {
201 std::cerr << "failed to find match for " << path.string()
202 << "\n";
James Feist95b079b2018-11-21 09:28:00 -0800203 continue;
204 }
James Feist95b079b2018-11-21 09:28:00 -0800205
James Feistde5e9702019-09-18 16:13:02 -0700206 auto findSensorName = baseConfiguration->second.find("Name");
207 if (findSensorName == baseConfiguration->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800208 {
James Feistde5e9702019-09-18 16:13:02 -0700209 std::cerr << "could not determine configuration name for "
210 << path.string() << "\n";
211 continue;
212 }
213 std::string sensorName =
214 std::get<std::string>(findSensorName->second);
215 // on rescans, only update sensors we were signaled by
216 auto findSensor = tachSensors.find(sensorName);
217 if (!firstScan && findSensor != tachSensors.end())
218 {
219 bool found = false;
220 for (auto it = sensorsChanged->begin();
221 it != sensorsChanged->end(); it++)
222 {
223 if (boost::ends_with(*it, findSensor->second->name))
224 {
225 sensorsChanged->erase(it);
226 findSensor->second = nullptr;
227 found = true;
228 break;
229 }
230 }
231 if (!found)
232 {
233 continue;
234 }
235 }
236 std::vector<thresholds::Threshold> sensorThresholds;
237 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
238 {
239 std::cerr << "error populating thresholds for "
240 << sensorName << "\n";
241 }
242
243 auto presenceConfig =
244 sensorData->find(baseType + std::string(".Presence"));
245
246 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
247
248 // presence sensors are optional
249 if (presenceConfig != sensorData->end())
250 {
251 auto findIndex = presenceConfig->second.find("Index");
252 auto findPolarity = presenceConfig->second.find("Polarity");
253
254 if (findIndex == presenceConfig->second.end() ||
255 findPolarity == presenceConfig->second.end())
256 {
257 std::cerr << "Malformed Presence Configuration\n";
258 }
259 else
260 {
261 size_t index = std::get<uint64_t>(findIndex->second);
262 bool inverted = std::get<std::string>(
263 findPolarity->second) == "Low";
264 presenceSensor = std::make_unique<PresenceSensor>(
265 index, inverted, io, sensorName);
266 }
267 }
268 std::optional<RedundancySensor>* redundancy = nullptr;
269 if (fanType == FanTypes::aspeed)
270 {
271 redundancy = &systemRedundancy;
272 }
273
274 constexpr double defaultMaxReading = 25000;
275 constexpr double defaultMinReading = 0;
276 auto limits =
277 std::make_pair(defaultMinReading, defaultMaxReading);
278
279 findLimits(limits, baseConfiguration);
280 tachSensors[sensorName] = std::make_unique<TachSensor>(
281 path.string(), baseType, objectServer, dbusConnection,
282 std::move(presenceSensor), redundancy, io, sensorName,
283 std::move(sensorThresholds), *interfacePath, limits);
284
285 auto connector =
286 sensorData->find(baseType + std::string(".Connector"));
287 if (connector != sensorData->end())
288 {
289 auto findPwm = connector->second.find("Pwm");
290 if (findPwm == connector->second.end())
291 {
292 std::cerr << "Connector Missing PWM!\n";
293 continue;
294 }
295
296 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
297 findPwm->second);
298 pwmNumbers.emplace_back(pwm, *interfacePath);
James Feist95b079b2018-11-21 09:28:00 -0800299 }
300 }
James Feistde5e9702019-09-18 16:13:02 -0700301 std::vector<fs::path> pwms;
302 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+$)", pwms))
James Feist6714a252018-09-10 15:26:18 -0700303 {
James Feistde5e9702019-09-18 16:13:02 -0700304 std::cerr << "No pwm in system\n";
305 return;
306 }
307 for (const fs::path& pwm : pwms)
308 {
309 if (pwmSensors.find(pwm) != pwmSensors.end())
James Feist6714a252018-09-10 15:26:18 -0700310 {
James Feistde5e9702019-09-18 16:13:02 -0700311 continue;
James Feist6714a252018-09-10 15:26:18 -0700312 }
James Feistde5e9702019-09-18 16:13:02 -0700313 const std::string* path = nullptr;
314 for (const auto& [index, configPath] : pwmNumbers)
315 {
316 if (boost::ends_with(pwm.string(),
317 std::to_string(index + 1)))
318 {
319 path = &configPath;
320 break;
321 }
322 }
323
324 if (path == nullptr)
325 {
326 continue;
327 }
328
329 // only add new elements
330 const std::string& sysPath = pwm.string();
331 const std::string& pwmName =
332 "Pwm_" + sysPath.substr(sysPath.find_last_of("pwm") + 1);
333 pwmSensors.insert(
334 std::pair<std::string, std::unique_ptr<PwmSensor>>(
335 sysPath, std::make_unique<PwmSensor>(
336 pwmName, sysPath, objectServer, *path)));
James Feist6714a252018-09-10 15:26:18 -0700337 }
James Feistde5e9702019-09-18 16:13:02 -0700338 }));
339 getter->getConfiguration(
340 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()});
James Feist6714a252018-09-10 15:26:18 -0700341}
342
James Feistdc6c55f2018-10-31 12:53:20 -0700343void createRedundancySensor(
344 const boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
345 sensors,
346 std::shared_ptr<sdbusplus::asio::connection> conn,
347 sdbusplus::asio::object_server& objectServer)
348{
349
350 conn->async_method_call(
351 [&objectServer, &sensors](boost::system::error_code& ec,
352 const ManagedObjectType managedObj) {
353 if (ec)
354 {
355 std::cerr << "Error calling entity manager \n";
356 return;
357 }
358 for (const auto& pathPair : managedObj)
359 {
360 for (const auto& interfacePair : pathPair.second)
361 {
362 if (interfacePair.first == redundancyConfiguration)
363 {
364 // currently only support one
365 auto findCount =
366 interfacePair.second.find("AllowedFailures");
367 if (findCount == interfacePair.second.end())
368 {
369 std::cerr << "Malformed redundancy record \n";
370 return;
371 }
372 std::vector<std::string> sensorList;
373
374 for (const auto& sensor : sensors)
375 {
376 sensorList.push_back(
377 "/xyz/openbmc_project/sensors/fan_tach/" +
378 sensor.second->name);
379 }
James Feist7b18b1e2019-05-14 13:42:09 -0700380 systemRedundancy.reset();
381 systemRedundancy.emplace(RedundancySensor(
James Feist3eb82622019-02-08 13:10:22 -0800382 std::get<uint64_t>(findCount->second), sensorList,
James Feist7b18b1e2019-05-14 13:42:09 -0700383 objectServer, pathPair.first));
James Feistdc6c55f2018-10-31 12:53:20 -0700384
385 return;
386 }
387 }
388 }
389 },
390 "xyz.openbmc_project.EntityManager", "/",
391 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
392}
393
James Feistb6c0b912019-07-09 12:21:44 -0700394int main()
James Feist6714a252018-09-10 15:26:18 -0700395{
396 boost::asio::io_service io;
397 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
398 systemBus->request_name("xyz.openbmc_project.FanSensor");
399 sdbusplus::asio::object_server objectServer(systemBus);
400 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>
401 tachSensors;
402 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
403 pwmSensors;
404 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
405 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
406 std::make_unique<boost::container::flat_set<std::string>>();
407
408 io.post([&]() {
409 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
410 nullptr);
James Feistdc6c55f2018-10-31 12:53:20 -0700411 createRedundancySensor(tachSensors, systemBus, objectServer);
James Feist6714a252018-09-10 15:26:18 -0700412 });
413
414 boost::asio::deadline_timer filterTimer(io);
415 std::function<void(sdbusplus::message::message&)> eventHandler =
416 [&](sdbusplus::message::message& message) {
417 if (message.is_method_error())
418 {
419 std::cerr << "callback method error\n";
420 return;
421 }
422 sensorsChanged->insert(message.get_path());
423 // this implicitly cancels the timer
424 filterTimer.expires_from_now(boost::posix_time::seconds(1));
425
426 filterTimer.async_wait([&](const boost::system::error_code& ec) {
427 if (ec == boost::asio::error::operation_aborted)
428 {
429 /* we were canceled*/
430 return;
431 }
432 else if (ec)
433 {
434 std::cerr << "timer error\n";
435 return;
436 }
437 createSensors(io, objectServer, tachSensors, pwmSensors,
438 systemBus, sensorsChanged);
439 });
440 };
441
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700442 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700443 {
444 auto match = std::make_unique<sdbusplus::bus::match::match>(
445 static_cast<sdbusplus::bus::bus&>(*systemBus),
446 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700447 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700448 eventHandler);
449 matches.emplace_back(std::move(match));
450 }
451
James Feistdc6c55f2018-10-31 12:53:20 -0700452 // redundancy sensor
453 std::function<void(sdbusplus::message::message&)> redundancyHandler =
454 [&tachSensors, &systemBus,
James Feistb6c0b912019-07-09 12:21:44 -0700455 &objectServer](sdbusplus::message::message&) {
James Feistdc6c55f2018-10-31 12:53:20 -0700456 createRedundancySensor(tachSensors, systemBus, objectServer);
457 };
458 auto match = std::make_unique<sdbusplus::bus::match::match>(
459 static_cast<sdbusplus::bus::bus&>(*systemBus),
460 "type='signal',member='PropertiesChanged',path_namespace='" +
461 std::string(inventoryPath) + "',arg0namespace='" +
462 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700463 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700464 matches.emplace_back(std::move(match));
465
James Feist6714a252018-09-10 15:26:18 -0700466 io.run();
467}