blob: 25e57396f7b0b93882d138a799c1054211d737df [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
Ed Tanous8a57ec02020-10-09 12:46:52 -070017#include <PwmSensor.hpp>
18#include <TachSensor.hpp>
19#include <Utils.hpp>
20#include <VariantVisitors.hpp>
James Feist6714a252018-09-10 15:26:18 -070021#include <boost/algorithm/string/predicate.hpp>
22#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070023#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070024#include <boost/container/flat_set.hpp>
25#include <boost/lexical_cast.hpp>
James Feist38fb5982020-05-28 10:09:54 -070026#include <sdbusplus/asio/connection.hpp>
27#include <sdbusplus/asio/object_server.hpp>
28#include <sdbusplus/bus/match.hpp>
29
30#include <array>
James Feist24f02f22019-04-15 11:05:39 -070031#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070032#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <functional>
34#include <memory>
35#include <optional>
James Feist6714a252018-09-10 15:26:18 -070036#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070037#include <string>
38#include <utility>
39#include <variant>
40#include <vector>
James Feist6714a252018-09-10 15:26:18 -070041
Ed Tanous8a57ec02020-10-09 12:46:52 -070042static constexpr bool debug = false;
James Feist6714a252018-09-10 15:26:18 -070043
James Feistcf3bce62019-01-08 10:07:19 -080044namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080045
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +000046// The following two structures need to be consistent
Peter Lundgren8843b622019-09-12 10:33:41 -070047static constexpr std::array<const char*, 3> sensorTypes = {
James Feist95b079b2018-11-21 09:28:00 -080048 "xyz.openbmc_project.Configuration.AspeedFan",
Peter Lundgren8843b622019-09-12 10:33:41 -070049 "xyz.openbmc_project.Configuration.I2CFan",
50 "xyz.openbmc_project.Configuration.NuvotonFan"};
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +000051
52enum FanTypes
53{
54 aspeed = 0,
55 i2c,
56 nuvoton,
57 max,
58};
59
60static_assert(std::tuple_size<decltype(sensorTypes)>::value == FanTypes::max,
61 "sensorTypes element number is not equal to FanTypes number");
62
James Feistdc6c55f2018-10-31 12:53:20 -070063constexpr const char* redundancyConfiguration =
64 "xyz.openbmc_project.Configuration.FanRedundancy";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070065static std::regex inputRegex(R"(fan(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070066
James Feistdc6c55f2018-10-31 12:53:20 -070067// todo: power supply fan redundancy
James Feist7b18b1e2019-05-14 13:42:09 -070068std::optional<RedundancySensor> systemRedundancy;
James Feist95b079b2018-11-21 09:28:00 -080069
70FanTypes getFanType(const fs::path& parentPath)
71{
72 fs::path linkPath = parentPath / "device";
73 std::string canonical = fs::read_symlink(linkPath);
Jae Hyun Yoo241356e2020-02-20 12:48:04 -080074 if (boost::ends_with(canonical, "1e786000.pwm-tacho-controller") ||
75 boost::ends_with(canonical, "1e610000.pwm-tacho-controller"))
James Feist95b079b2018-11-21 09:28:00 -080076 {
77 return FanTypes::aspeed;
78 }
Ed Tanous8a57ec02020-10-09 12:46:52 -070079 if (boost::ends_with(canonical, "f0103000.pwm-fan-controller"))
Peter Lundgren8843b622019-09-12 10:33:41 -070080 {
81 return FanTypes::nuvoton;
82 }
James Feist95b079b2018-11-21 09:28:00 -080083 // todo: will we need to support other types?
84 return FanTypes::i2c;
85}
James Feistdc6c55f2018-10-31 12:53:20 -070086
Kuiying Wangd5407412020-09-09 16:06:56 +080087void createRedundancySensor(
88 const boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
89 sensors,
Ed Tanous8a57ec02020-10-09 12:46:52 -070090 const std::shared_ptr<sdbusplus::asio::connection>& conn,
Kuiying Wangd5407412020-09-09 16:06:56 +080091 sdbusplus::asio::object_server& objectServer)
92{
93
94 conn->async_method_call(
95 [&objectServer, &sensors](boost::system::error_code& ec,
Ed Tanous8a57ec02020-10-09 12:46:52 -070096 const ManagedObjectType& managedObj) {
Kuiying Wangd5407412020-09-09 16:06:56 +080097 if (ec)
98 {
99 std::cerr << "Error calling entity manager \n";
100 return;
101 }
102 for (const auto& pathPair : managedObj)
103 {
104 for (const auto& interfacePair : pathPair.second)
105 {
106 if (interfacePair.first == redundancyConfiguration)
107 {
108 // currently only support one
109 auto findCount =
110 interfacePair.second.find("AllowedFailures");
111 if (findCount == interfacePair.second.end())
112 {
113 std::cerr << "Malformed redundancy record \n";
114 return;
115 }
116 std::vector<std::string> sensorList;
117
118 for (const auto& sensor : sensors)
119 {
120 sensorList.push_back(
121 "/xyz/openbmc_project/sensors/fan_tach/" +
122 sensor.second->name);
123 }
124 systemRedundancy.reset();
125 systemRedundancy.emplace(RedundancySensor(
126 std::get<uint64_t>(findCount->second), sensorList,
127 objectServer, pathPair.first));
128
129 return;
130 }
131 }
132 }
133 },
134 "xyz.openbmc_project.EntityManager", "/",
135 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
136}
137
James Feist6714a252018-09-10 15:26:18 -0700138void createSensors(
139 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
140 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
141 tachSensors,
142 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
143 pwmSensors,
144 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -0700145 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feistf27a55c2020-08-04 14:27:30 -0700146 sensorsChanged,
147 size_t retries = 0)
James Feist6714a252018-09-10 15:26:18 -0700148{
James Feistde5e9702019-09-18 16:13:02 -0700149 auto getter = std::make_shared<GetSensorConfiguration>(
150 dbusConnection,
151 std::move([&io, &objectServer, &tachSensors, &pwmSensors,
James Feist5591cf082020-07-15 16:44:54 -0700152 &dbusConnection, sensorsChanged](
James Feistde5e9702019-09-18 16:13:02 -0700153 const ManagedObjectType& sensorConfigurations) {
154 bool firstScan = sensorsChanged == nullptr;
155 std::vector<fs::path> paths;
156 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)",
157 paths))
James Feist95b079b2018-11-21 09:28:00 -0800158 {
Yong Zhao77b3add2021-01-09 04:25:18 +0000159 std::cerr << "No fan sensors in system\n";
James Feistde5e9702019-09-18 16:13:02 -0700160 return;
James Feist95b079b2018-11-21 09:28:00 -0800161 }
James Feist6714a252018-09-10 15:26:18 -0700162
James Feistde5e9702019-09-18 16:13:02 -0700163 // iterate through all found fan sensors, and try to match them with
164 // configuration
165 for (const auto& path : paths)
James Feist6714a252018-09-10 15:26:18 -0700166 {
James Feistde5e9702019-09-18 16:13:02 -0700167 std::smatch match;
168 std::string pathStr = path.string();
169
170 std::regex_search(pathStr, match, inputRegex);
171 std::string indexStr = *(match.begin() + 1);
172
Yong Zhao77b3add2021-01-09 04:25:18 +0000173 fs::path directory = path.parent_path();
174 fs::path pwmPath = directory / ("pwm" + indexStr);
James Feistde5e9702019-09-18 16:13:02 -0700175 FanTypes fanType = getFanType(directory);
Yong Zhao77b3add2021-01-09 04:25:18 +0000176
James Feistde5e9702019-09-18 16:13:02 -0700177 size_t bus = 0;
178 size_t address = 0;
179 if (fanType == FanTypes::i2c)
James Feist6714a252018-09-10 15:26:18 -0700180 {
James Feistde5e9702019-09-18 16:13:02 -0700181 std::string link =
182 fs::read_symlink(directory / "device").filename();
183
Ed Tanous8a57ec02020-10-09 12:46:52 -0700184 size_t findDash = link.find('-');
James Feistde5e9702019-09-18 16:13:02 -0700185 if (findDash == std::string::npos ||
186 link.size() <= findDash + 1)
187 {
188 std::cerr << "Error finding device from symlink";
189 }
190 bus = std::stoi(link.substr(0, findDash));
191 address = std::stoi(link.substr(findDash + 1), nullptr, 16);
James Feist6714a252018-09-10 15:26:18 -0700192 }
James Feistde5e9702019-09-18 16:13:02 -0700193 // convert to 0 based
194 size_t index = std::stoul(indexStr) - 1;
195
196 const char* baseType;
197 const SensorData* sensorData = nullptr;
198 const std::string* interfacePath = nullptr;
199 const SensorBaseConfiguration* baseConfiguration = nullptr;
200 for (const std::pair<sdbusplus::message::object_path,
201 SensorData>& sensor : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800202 {
James Feistde5e9702019-09-18 16:13:02 -0700203 // find the base of the configuration to see if indexes
204 // match
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +0000205 auto sensorBaseFind =
206 sensor.second.find(sensorTypes[fanType]);
207 if (sensorBaseFind == sensor.second.end())
James Feistde5e9702019-09-18 16:13:02 -0700208 {
209 continue;
210 }
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800211
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +0000212 baseConfiguration = &(*sensorBaseFind);
213 interfacePath = &(sensor.first.str);
214 baseType = sensorTypes[fanType];
215
James Feistde5e9702019-09-18 16:13:02 -0700216 auto findIndex = baseConfiguration->second.find("Index");
217 if (findIndex == baseConfiguration->second.end())
218 {
219 std::cerr << baseConfiguration->first
220 << " missing index\n";
221 continue;
222 }
223 unsigned int configIndex = std::visit(
224 VariantToUnsignedIntVisitor(), findIndex->second);
225 if (configIndex != index)
226 {
227 continue;
228 }
229 if (fanType == FanTypes::aspeed ||
230 fanType == FanTypes::nuvoton)
231 {
232 // there will be only 1 aspeed or nuvoton sensor object
233 // in sysfs, we found the fan
234 sensorData = &(sensor.second);
235 break;
236 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700237 if (fanType == FanTypes::i2c)
James Feistde5e9702019-09-18 16:13:02 -0700238 {
239 auto findBus = baseConfiguration->second.find("Bus");
240 auto findAddress =
241 baseConfiguration->second.find("Address");
242 if (findBus == baseConfiguration->second.end() ||
243 findAddress == baseConfiguration->second.end())
244 {
245 std::cerr << baseConfiguration->first
246 << " missing bus or address\n";
247 continue;
248 }
249 unsigned int configBus = std::visit(
250 VariantToUnsignedIntVisitor(), findBus->second);
251 unsigned int configAddress = std::visit(
252 VariantToUnsignedIntVisitor(), findAddress->second);
253
254 if (configBus == bus && configAddress == address)
255 {
256 sensorData = &(sensor.second);
257 break;
258 }
259 }
260 }
261 if (sensorData == nullptr)
262 {
263 std::cerr << "failed to find match for " << path.string()
264 << "\n";
James Feist95b079b2018-11-21 09:28:00 -0800265 continue;
266 }
James Feist95b079b2018-11-21 09:28:00 -0800267
James Feistde5e9702019-09-18 16:13:02 -0700268 auto findSensorName = baseConfiguration->second.find("Name");
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800269
James Feistde5e9702019-09-18 16:13:02 -0700270 if (findSensorName == baseConfiguration->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800271 {
James Feistde5e9702019-09-18 16:13:02 -0700272 std::cerr << "could not determine configuration name for "
273 << path.string() << "\n";
274 continue;
275 }
276 std::string sensorName =
277 std::get<std::string>(findSensorName->second);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800278
James Feistde5e9702019-09-18 16:13:02 -0700279 // on rescans, only update sensors we were signaled by
280 auto findSensor = tachSensors.find(sensorName);
281 if (!firstScan && findSensor != tachSensors.end())
282 {
283 bool found = false;
284 for (auto it = sensorsChanged->begin();
285 it != sensorsChanged->end(); it++)
286 {
287 if (boost::ends_with(*it, findSensor->second->name))
288 {
289 sensorsChanged->erase(it);
290 findSensor->second = nullptr;
291 found = true;
292 break;
293 }
294 }
295 if (!found)
296 {
297 continue;
298 }
299 }
300 std::vector<thresholds::Threshold> sensorThresholds;
301 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
302 {
303 std::cerr << "error populating thresholds for "
304 << sensorName << "\n";
305 }
306
307 auto presenceConfig =
308 sensorData->find(baseType + std::string(".Presence"));
309
310 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
311
312 // presence sensors are optional
313 if (presenceConfig != sensorData->end())
314 {
James Feistde5e9702019-09-18 16:13:02 -0700315 auto findPolarity = presenceConfig->second.find("Polarity");
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800316 auto findPinName = presenceConfig->second.find("PinName");
James Feistde5e9702019-09-18 16:13:02 -0700317
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800318 if (findPinName == presenceConfig->second.end() ||
James Feistde5e9702019-09-18 16:13:02 -0700319 findPolarity == presenceConfig->second.end())
320 {
321 std::cerr << "Malformed Presence Configuration\n";
322 }
323 else
324 {
James Feistde5e9702019-09-18 16:13:02 -0700325 bool inverted = std::get<std::string>(
326 findPolarity->second) == "Low";
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800327 if (auto pinName =
328 std::get_if<std::string>(&findPinName->second))
329 {
330 presenceSensor = std::make_unique<PresenceSensor>(
331 *pinName, inverted, io, sensorName);
332 }
333 else
334 {
335 std::cerr
336 << "Malformed Presence pinName for sensor "
337 << sensorName << " \n";
338 }
James Feistde5e9702019-09-18 16:13:02 -0700339 }
340 }
341 std::optional<RedundancySensor>* redundancy = nullptr;
342 if (fanType == FanTypes::aspeed)
343 {
344 redundancy = &systemRedundancy;
345 }
346
Josh Lehanf920e092020-08-07 00:12:54 -0700347 PowerState powerState = PowerState::on;
348 auto findPower = baseConfiguration->second.find("PowerState");
349 if (findPower != baseConfiguration->second.end())
350 {
351 auto ptrPower =
352 std::get_if<std::string>(&(findPower->second));
353 if (ptrPower)
354 {
355 setReadState(*ptrPower, powerState);
356 }
357 }
358
James Feistde5e9702019-09-18 16:13:02 -0700359 constexpr double defaultMaxReading = 25000;
360 constexpr double defaultMinReading = 0;
361 auto limits =
362 std::make_pair(defaultMinReading, defaultMaxReading);
363
James Feist49a8ccd2020-09-16 16:09:52 -0700364 auto connector =
365 sensorData->find(baseType + std::string(".Connector"));
366
367 std::optional<std::string> led;
Yong Zhao77b3add2021-01-09 04:25:18 +0000368 std::string pwmName;
James Feist49a8ccd2020-09-16 16:09:52 -0700369
370 if (connector != sensorData->end())
371 {
372 auto findPwm = connector->second.find("Pwm");
373 if (findPwm != connector->second.end())
374 {
375
376 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
377 findPwm->second);
378 /* use pwm name override if found in configuration else
379 * use default */
380 auto findOverride = connector->second.find("PwmName");
James Feist49a8ccd2020-09-16 16:09:52 -0700381 if (findOverride != connector->second.end())
382 {
383 pwmName = std::visit(VariantToStringVisitor(),
384 findOverride->second);
385 }
386 else
387 {
388 pwmName = "Pwm_" + std::to_string(pwm + 1);
389 }
James Feist49a8ccd2020-09-16 16:09:52 -0700390 }
391 else
392 {
393 std::cerr << "Connector for " << sensorName
394 << " missing pwm!\n";
395 }
396
397 auto findLED = connector->second.find("LED");
398 if (findLED != connector->second.end())
399 {
400 auto ledName =
401 std::get_if<std::string>(&(findLED->second));
402 if (ledName == nullptr)
403 {
404 std::cerr << "Wrong format for LED of "
405 << sensorName << "\n";
406 }
407 else
408 {
409 led = *ledName;
410 }
411 }
412 }
413
James Feistde5e9702019-09-18 16:13:02 -0700414 findLimits(limits, baseConfiguration);
415 tachSensors[sensorName] = std::make_unique<TachSensor>(
416 path.string(), baseType, objectServer, dbusConnection,
417 std::move(presenceSensor), redundancy, io, sensorName,
Josh Lehanf920e092020-08-07 00:12:54 -0700418 std::move(sensorThresholds), *interfacePath, limits,
James Feist49a8ccd2020-09-16 16:09:52 -0700419 powerState, led);
Yong Zhao77b3add2021-01-09 04:25:18 +0000420
421 if (fs::exists(pwmPath) && !pwmSensors.count(pwmPath))
422 {
423 pwmSensors[pwmPath] = std::make_unique<PwmSensor>(
424 pwmName, pwmPath, dbusConnection, objectServer,
425 *interfacePath, "Fan");
426 }
James Feist95b079b2018-11-21 09:28:00 -0800427 }
Yong Zhao77b3add2021-01-09 04:25:18 +0000428
Kuiying Wangd5407412020-09-09 16:06:56 +0800429 createRedundancySensor(tachSensors, dbusConnection, objectServer);
James Feistde5e9702019-09-18 16:13:02 -0700430 }));
431 getter->getConfiguration(
James Feistf27a55c2020-08-04 14:27:30 -0700432 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()},
433 retries);
James Feist6714a252018-09-10 15:26:18 -0700434}
435
James Feistb6c0b912019-07-09 12:21:44 -0700436int main()
James Feist6714a252018-09-10 15:26:18 -0700437{
438 boost::asio::io_service io;
439 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
440 systemBus->request_name("xyz.openbmc_project.FanSensor");
441 sdbusplus::asio::object_server objectServer(systemBus);
442 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>
443 tachSensors;
444 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
445 pwmSensors;
446 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700447 auto sensorsChanged =
448 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700449
450 io.post([&]() {
451 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
452 nullptr);
453 });
454
455 boost::asio::deadline_timer filterTimer(io);
456 std::function<void(sdbusplus::message::message&)> eventHandler =
457 [&](sdbusplus::message::message& message) {
458 if (message.is_method_error())
459 {
460 std::cerr << "callback method error\n";
461 return;
462 }
463 sensorsChanged->insert(message.get_path());
464 // this implicitly cancels the timer
465 filterTimer.expires_from_now(boost::posix_time::seconds(1));
466
467 filterTimer.async_wait([&](const boost::system::error_code& ec) {
468 if (ec == boost::asio::error::operation_aborted)
469 {
470 /* we were canceled*/
471 return;
472 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700473 if (ec)
James Feist6714a252018-09-10 15:26:18 -0700474 {
475 std::cerr << "timer error\n";
476 return;
477 }
478 createSensors(io, objectServer, tachSensors, pwmSensors,
James Feistf27a55c2020-08-04 14:27:30 -0700479 systemBus, sensorsChanged, 5);
James Feist6714a252018-09-10 15:26:18 -0700480 });
481 };
482
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700483 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700484 {
485 auto match = std::make_unique<sdbusplus::bus::match::match>(
486 static_cast<sdbusplus::bus::bus&>(*systemBus),
487 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700488 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700489 eventHandler);
490 matches.emplace_back(std::move(match));
491 }
492
James Feistdc6c55f2018-10-31 12:53:20 -0700493 // redundancy sensor
494 std::function<void(sdbusplus::message::message&)> redundancyHandler =
495 [&tachSensors, &systemBus,
James Feistb6c0b912019-07-09 12:21:44 -0700496 &objectServer](sdbusplus::message::message&) {
James Feistdc6c55f2018-10-31 12:53:20 -0700497 createRedundancySensor(tachSensors, systemBus, objectServer);
498 };
499 auto match = std::make_unique<sdbusplus::bus::match::match>(
500 static_cast<sdbusplus::bus::bus&>(*systemBus),
501 "type='signal',member='PropertiesChanged',path_namespace='" +
502 std::string(inventoryPath) + "',arg0namespace='" +
503 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700504 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700505 matches.emplace_back(std::move(match));
506
James Feist6714a252018-09-10 15:26:18 -0700507 io.run();
508}