blob: a34dd4d9a64f9f0d612beba61b8e0a5b6fa9f2d9 [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();
James Feistde5e9702019-09-18 16:13:02 -0700174 FanTypes fanType = getFanType(directory);
Yong Zhao77b3add2021-01-09 04:25:18 +0000175
James Feistde5e9702019-09-18 16:13:02 -0700176 // convert to 0 based
177 size_t index = std::stoul(indexStr) - 1;
178
179 const char* baseType;
180 const SensorData* sensorData = nullptr;
181 const std::string* interfacePath = nullptr;
182 const SensorBaseConfiguration* baseConfiguration = nullptr;
183 for (const std::pair<sdbusplus::message::object_path,
184 SensorData>& sensor : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800185 {
James Feistde5e9702019-09-18 16:13:02 -0700186 // find the base of the configuration to see if indexes
187 // match
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +0000188 auto sensorBaseFind =
189 sensor.second.find(sensorTypes[fanType]);
190 if (sensorBaseFind == sensor.second.end())
James Feistde5e9702019-09-18 16:13:02 -0700191 {
192 continue;
193 }
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800194
Yong Zhaoa3e8f2a2021-01-09 02:22:43 +0000195 baseConfiguration = &(*sensorBaseFind);
196 interfacePath = &(sensor.first.str);
197 baseType = sensorTypes[fanType];
198
James Feistde5e9702019-09-18 16:13:02 -0700199 auto findIndex = baseConfiguration->second.find("Index");
200 if (findIndex == baseConfiguration->second.end())
201 {
202 std::cerr << baseConfiguration->first
203 << " missing index\n";
204 continue;
205 }
206 unsigned int configIndex = std::visit(
207 VariantToUnsignedIntVisitor(), findIndex->second);
208 if (configIndex != index)
209 {
210 continue;
211 }
212 if (fanType == FanTypes::aspeed ||
213 fanType == FanTypes::nuvoton)
214 {
215 // there will be only 1 aspeed or nuvoton sensor object
216 // in sysfs, we found the fan
217 sensorData = &(sensor.second);
218 break;
219 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700220 if (fanType == FanTypes::i2c)
James Feistde5e9702019-09-18 16:13:02 -0700221 {
Yong Zhaoadd46822021-01-09 04:52:36 +0000222 size_t bus = 0;
223 size_t address = 0;
224
225 std::string link =
226 fs::read_symlink(directory / "device").filename();
227
228 size_t findDash = link.find('-');
229 if (findDash == std::string::npos ||
230 link.size() <= findDash + 1)
231 {
232 std::cerr << "Error finding device from symlink";
233 }
234 bus = std::stoi(link.substr(0, findDash));
235 address =
236 std::stoi(link.substr(findDash + 1), nullptr, 16);
237
James Feistde5e9702019-09-18 16:13:02 -0700238 auto findBus = baseConfiguration->second.find("Bus");
239 auto findAddress =
240 baseConfiguration->second.find("Address");
241 if (findBus == baseConfiguration->second.end() ||
242 findAddress == baseConfiguration->second.end())
243 {
244 std::cerr << baseConfiguration->first
245 << " missing bus or address\n";
246 continue;
247 }
248 unsigned int configBus = std::visit(
249 VariantToUnsignedIntVisitor(), findBus->second);
250 unsigned int configAddress = std::visit(
251 VariantToUnsignedIntVisitor(), findAddress->second);
252
253 if (configBus == bus && configAddress == address)
254 {
255 sensorData = &(sensor.second);
256 break;
257 }
258 }
259 }
260 if (sensorData == nullptr)
261 {
262 std::cerr << "failed to find match for " << path.string()
263 << "\n";
James Feist95b079b2018-11-21 09:28:00 -0800264 continue;
265 }
James Feist95b079b2018-11-21 09:28:00 -0800266
James Feistde5e9702019-09-18 16:13:02 -0700267 auto findSensorName = baseConfiguration->second.find("Name");
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800268
James Feistde5e9702019-09-18 16:13:02 -0700269 if (findSensorName == baseConfiguration->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800270 {
James Feistde5e9702019-09-18 16:13:02 -0700271 std::cerr << "could not determine configuration name for "
272 << path.string() << "\n";
273 continue;
274 }
275 std::string sensorName =
276 std::get<std::string>(findSensorName->second);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800277
James Feistde5e9702019-09-18 16:13:02 -0700278 // on rescans, only update sensors we were signaled by
279 auto findSensor = tachSensors.find(sensorName);
280 if (!firstScan && findSensor != tachSensors.end())
281 {
282 bool found = false;
283 for (auto it = sensorsChanged->begin();
284 it != sensorsChanged->end(); it++)
285 {
286 if (boost::ends_with(*it, findSensor->second->name))
287 {
288 sensorsChanged->erase(it);
289 findSensor->second = nullptr;
290 found = true;
291 break;
292 }
293 }
294 if (!found)
295 {
296 continue;
297 }
298 }
299 std::vector<thresholds::Threshold> sensorThresholds;
300 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
301 {
302 std::cerr << "error populating thresholds for "
303 << sensorName << "\n";
304 }
305
306 auto presenceConfig =
307 sensorData->find(baseType + std::string(".Presence"));
308
309 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
310
311 // presence sensors are optional
312 if (presenceConfig != sensorData->end())
313 {
James Feistde5e9702019-09-18 16:13:02 -0700314 auto findPolarity = presenceConfig->second.find("Polarity");
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800315 auto findPinName = presenceConfig->second.find("PinName");
James Feistde5e9702019-09-18 16:13:02 -0700316
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800317 if (findPinName == presenceConfig->second.end() ||
James Feistde5e9702019-09-18 16:13:02 -0700318 findPolarity == presenceConfig->second.end())
319 {
320 std::cerr << "Malformed Presence Configuration\n";
321 }
322 else
323 {
James Feistde5e9702019-09-18 16:13:02 -0700324 bool inverted = std::get<std::string>(
325 findPolarity->second) == "Low";
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800326 if (auto pinName =
327 std::get_if<std::string>(&findPinName->second))
328 {
329 presenceSensor = std::make_unique<PresenceSensor>(
330 *pinName, inverted, io, sensorName);
331 }
332 else
333 {
334 std::cerr
335 << "Malformed Presence pinName for sensor "
336 << sensorName << " \n";
337 }
James Feistde5e9702019-09-18 16:13:02 -0700338 }
339 }
340 std::optional<RedundancySensor>* redundancy = nullptr;
341 if (fanType == FanTypes::aspeed)
342 {
343 redundancy = &systemRedundancy;
344 }
345
Josh Lehanf920e092020-08-07 00:12:54 -0700346 PowerState powerState = PowerState::on;
347 auto findPower = baseConfiguration->second.find("PowerState");
348 if (findPower != baseConfiguration->second.end())
349 {
350 auto ptrPower =
351 std::get_if<std::string>(&(findPower->second));
352 if (ptrPower)
353 {
354 setReadState(*ptrPower, powerState);
355 }
356 }
357
James Feistde5e9702019-09-18 16:13:02 -0700358 constexpr double defaultMaxReading = 25000;
359 constexpr double defaultMinReading = 0;
360 auto limits =
361 std::make_pair(defaultMinReading, defaultMaxReading);
362
James Feist49a8ccd2020-09-16 16:09:52 -0700363 auto connector =
364 sensorData->find(baseType + std::string(".Connector"));
365
366 std::optional<std::string> led;
Yong Zhao77b3add2021-01-09 04:25:18 +0000367 std::string pwmName;
Zhikui Rend05867c2021-02-26 16:10:34 -0800368 fs::path pwmPath;
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);
Zhikui Rend05867c2021-02-26 16:10:34 -0800378 pwmPath = directory / ("pwm" + std::to_string(pwm + 1));
James Feist49a8ccd2020-09-16 16:09:52 -0700379 /* use pwm name override if found in configuration else
380 * use default */
381 auto findOverride = connector->second.find("PwmName");
James Feist49a8ccd2020-09-16 16:09:52 -0700382 if (findOverride != connector->second.end())
383 {
384 pwmName = std::visit(VariantToStringVisitor(),
385 findOverride->second);
386 }
387 else
388 {
389 pwmName = "Pwm_" + std::to_string(pwm + 1);
390 }
James Feist49a8ccd2020-09-16 16:09:52 -0700391 }
392 else
393 {
394 std::cerr << "Connector for " << sensorName
395 << " missing pwm!\n";
396 }
397
398 auto findLED = connector->second.find("LED");
399 if (findLED != connector->second.end())
400 {
401 auto ledName =
402 std::get_if<std::string>(&(findLED->second));
403 if (ledName == nullptr)
404 {
405 std::cerr << "Wrong format for LED of "
406 << sensorName << "\n";
407 }
408 else
409 {
410 led = *ledName;
411 }
412 }
413 }
414
James Feistde5e9702019-09-18 16:13:02 -0700415 findLimits(limits, baseConfiguration);
416 tachSensors[sensorName] = std::make_unique<TachSensor>(
417 path.string(), baseType, objectServer, dbusConnection,
418 std::move(presenceSensor), redundancy, io, sensorName,
Josh Lehanf920e092020-08-07 00:12:54 -0700419 std::move(sensorThresholds), *interfacePath, limits,
James Feist49a8ccd2020-09-16 16:09:52 -0700420 powerState, led);
Yong Zhao77b3add2021-01-09 04:25:18 +0000421
Zhikui Rend05867c2021-02-26 16:10:34 -0800422 if (!pwmPath.empty() && fs::exists(pwmPath) &&
423 !pwmSensors.count(pwmPath))
Yong Zhao77b3add2021-01-09 04:25:18 +0000424 {
425 pwmSensors[pwmPath] = std::make_unique<PwmSensor>(
426 pwmName, pwmPath, dbusConnection, objectServer,
427 *interfacePath, "Fan");
428 }
James Feist95b079b2018-11-21 09:28:00 -0800429 }
Yong Zhao77b3add2021-01-09 04:25:18 +0000430
Kuiying Wangd5407412020-09-09 16:06:56 +0800431 createRedundancySensor(tachSensors, dbusConnection, objectServer);
James Feistde5e9702019-09-18 16:13:02 -0700432 }));
433 getter->getConfiguration(
James Feistf27a55c2020-08-04 14:27:30 -0700434 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()},
435 retries);
James Feist6714a252018-09-10 15:26:18 -0700436}
437
James Feistb6c0b912019-07-09 12:21:44 -0700438int main()
James Feist6714a252018-09-10 15:26:18 -0700439{
440 boost::asio::io_service io;
441 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
442 systemBus->request_name("xyz.openbmc_project.FanSensor");
443 sdbusplus::asio::object_server objectServer(systemBus);
444 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>
445 tachSensors;
446 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
447 pwmSensors;
448 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700449 auto sensorsChanged =
450 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700451
452 io.post([&]() {
453 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
454 nullptr);
455 });
456
457 boost::asio::deadline_timer filterTimer(io);
458 std::function<void(sdbusplus::message::message&)> eventHandler =
459 [&](sdbusplus::message::message& message) {
460 if (message.is_method_error())
461 {
462 std::cerr << "callback method error\n";
463 return;
464 }
465 sensorsChanged->insert(message.get_path());
466 // this implicitly cancels the timer
467 filterTimer.expires_from_now(boost::posix_time::seconds(1));
468
469 filterTimer.async_wait([&](const boost::system::error_code& ec) {
470 if (ec == boost::asio::error::operation_aborted)
471 {
472 /* we were canceled*/
473 return;
474 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700475 if (ec)
James Feist6714a252018-09-10 15:26:18 -0700476 {
477 std::cerr << "timer error\n";
478 return;
479 }
480 createSensors(io, objectServer, tachSensors, pwmSensors,
James Feistf27a55c2020-08-04 14:27:30 -0700481 systemBus, sensorsChanged, 5);
James Feist6714a252018-09-10 15:26:18 -0700482 });
483 };
484
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700485 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700486 {
487 auto match = std::make_unique<sdbusplus::bus::match::match>(
488 static_cast<sdbusplus::bus::bus&>(*systemBus),
489 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700490 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700491 eventHandler);
492 matches.emplace_back(std::move(match));
493 }
494
James Feistdc6c55f2018-10-31 12:53:20 -0700495 // redundancy sensor
496 std::function<void(sdbusplus::message::message&)> redundancyHandler =
497 [&tachSensors, &systemBus,
James Feistb6c0b912019-07-09 12:21:44 -0700498 &objectServer](sdbusplus::message::message&) {
James Feistdc6c55f2018-10-31 12:53:20 -0700499 createRedundancySensor(tachSensors, systemBus, objectServer);
500 };
501 auto match = std::make_unique<sdbusplus::bus::match::match>(
502 static_cast<sdbusplus::bus::bus&>(*systemBus),
503 "type='signal',member='PropertiesChanged',path_namespace='" +
504 std::string(inventoryPath) + "',arg0namespace='" +
505 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700506 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700507 matches.emplace_back(std::move(match));
508
James Feist6714a252018-09-10 15:26:18 -0700509 io.run();
510}