blob: cfc5b5e2f6ebd3d66ee44401b7e276a98dad4c02 [file] [log] [blame]
Brad Bishope55ef3d2016-12-19 09:12:40 -05001/**
2 * Copyright © 2016 IBM 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 */
Matt Spinlerf9c83c42017-08-10 08:51:45 -050016#include "config.h"
Patrick Venture043d3232018-08-31 10:10:53 -070017
18#include "mainloop.hpp"
19
Patrick Venture09791852018-04-17 17:40:00 -070020#include "env.hpp"
21#include "fan_pwm.hpp"
22#include "fan_speed.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050023#include "hwmon.hpp"
Patrick Venture75e56c62018-04-20 18:10:15 -070024#include "hwmonio.hpp"
Patrick Venture043d3232018-08-31 10:10:53 -070025#include "sensor.hpp"
Patrick Venture09791852018-04-17 17:40:00 -070026#include "sensorset.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050027#include "sysfs.hpp"
Matthew Barthbf7b7b12017-03-07 15:46:59 -060028#include "targets.hpp"
Patrick Venture09791852018-04-17 17:40:00 -070029#include "thresholds.hpp"
Carol Wang9bbe6022019-08-01 17:31:30 +080030#include "util.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050031
Matt Spinler5e034af2020-06-24 15:21:53 -050032#include <fmt/format.h>
33
Patrick Williamse8771fd2023-05-10 07:51:06 -050034#include <phosphor-logging/elog-errors.hpp>
35#include <xyz/openbmc_project/Sensor/Device/error.hpp>
36
William A. Kennington III0e749752018-11-06 15:25:41 -080037#include <cassert>
Patrick Venture043d3232018-08-31 10:10:53 -070038#include <cstdlib>
39#include <functional>
Brandon Kim6d50c3e2019-08-09 15:38:53 -070040#include <future>
Patrick Venture043d3232018-08-31 10:10:53 -070041#include <iostream>
42#include <memory>
Patrick Venture043d3232018-08-31 10:10:53 -070043#include <sstream>
44#include <string>
45#include <unordered_set>
Patrick Venture1e6324f2017-06-01 14:07:05 -070046
47using namespace phosphor::logging;
48
Saqib Khan973886d2017-03-15 14:01:16 -050049// Initialization for Warning Objects
50decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo =
51 &WarningObject::warningLow;
52decltype(Thresholds<WarningObject>::setHi) Thresholds<WarningObject>::setHi =
53 &WarningObject::warningHigh;
54decltype(Thresholds<WarningObject>::getLo) Thresholds<WarningObject>::getLo =
55 &WarningObject::warningLow;
56decltype(Thresholds<WarningObject>::getHi) Thresholds<WarningObject>::getHi =
57 &WarningObject::warningHigh;
Patrick Williams3a816142021-10-06 15:36:42 -050058decltype(Thresholds<WarningObject>::alarmLo)
59 Thresholds<WarningObject>::alarmLo = &WarningObject::warningAlarmLow;
60decltype(Thresholds<WarningObject>::alarmHi)
61 Thresholds<WarningObject>::alarmHi = &WarningObject::warningAlarmHigh;
Duke Du73769092021-04-14 15:35:21 +080062decltype(Thresholds<WarningObject>::getAlarmLow)
63 Thresholds<WarningObject>::getAlarmLow = &WarningObject::warningAlarmLow;
64decltype(Thresholds<WarningObject>::getAlarmHigh)
65 Thresholds<WarningObject>::getAlarmHigh = &WarningObject::warningAlarmHigh;
66decltype(Thresholds<WarningObject>::assertLowSignal)
67 Thresholds<WarningObject>::assertLowSignal =
68 &WarningObject::warningLowAlarmAsserted;
69decltype(Thresholds<WarningObject>::assertHighSignal)
70 Thresholds<WarningObject>::assertHighSignal =
71 &WarningObject::warningHighAlarmAsserted;
72decltype(Thresholds<WarningObject>::deassertLowSignal)
73 Thresholds<WarningObject>::deassertLowSignal =
74 &WarningObject::warningLowAlarmDeasserted;
75decltype(Thresholds<WarningObject>::deassertHighSignal)
76 Thresholds<WarningObject>::deassertHighSignal =
77 &WarningObject::warningHighAlarmDeasserted;
Saqib Khan973886d2017-03-15 14:01:16 -050078
79// Initialization for Critical Objects
80decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo =
81 &CriticalObject::criticalLow;
82decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi =
83 &CriticalObject::criticalHigh;
84decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo =
85 &CriticalObject::criticalLow;
86decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi =
87 &CriticalObject::criticalHigh;
Patrick Williams3a816142021-10-06 15:36:42 -050088decltype(Thresholds<CriticalObject>::alarmLo)
89 Thresholds<CriticalObject>::alarmLo = &CriticalObject::criticalAlarmLow;
90decltype(Thresholds<CriticalObject>::alarmHi)
91 Thresholds<CriticalObject>::alarmHi = &CriticalObject::criticalAlarmHigh;
Duke Du73769092021-04-14 15:35:21 +080092decltype(Thresholds<CriticalObject>::getAlarmLow)
93 Thresholds<CriticalObject>::getAlarmLow = &CriticalObject::criticalAlarmLow;
94decltype(Thresholds<CriticalObject>::getAlarmHigh)
95 Thresholds<CriticalObject>::getAlarmHigh =
96 &CriticalObject::criticalAlarmHigh;
97decltype(Thresholds<CriticalObject>::assertLowSignal)
98 Thresholds<CriticalObject>::assertLowSignal =
99 &CriticalObject::criticalLowAlarmAsserted;
100decltype(Thresholds<CriticalObject>::assertHighSignal)
101 Thresholds<CriticalObject>::assertHighSignal =
102 &CriticalObject::criticalHighAlarmAsserted;
103decltype(Thresholds<CriticalObject>::deassertLowSignal)
104 Thresholds<CriticalObject>::deassertLowSignal =
105 &CriticalObject::criticalLowAlarmDeasserted;
106decltype(Thresholds<CriticalObject>::deassertHighSignal)
107 Thresholds<CriticalObject>::deassertHighSignal =
108 &CriticalObject::criticalHighAlarmDeasserted;
Saqib Khan973886d2017-03-15 14:01:16 -0500109
Matt Spinlerecac0ae2020-07-08 13:09:08 -0500110void updateSensorInterfaces(InterfaceMap& ifaces, SensorValueType value)
Patrick Venturefeb744a2019-06-26 19:07:48 -0700111{
112 for (auto& iface : ifaces)
113 {
114 switch (iface.first)
115 {
Patrick Williamsc8e818d2023-10-20 11:19:30 -0500116 // clang-format off
Patrick Venturefeb744a2019-06-26 19:07:48 -0700117 case InterfaceType::VALUE:
118 {
119 auto& valueIface =
120 std::any_cast<std::shared_ptr<ValueObject>&>(iface.second);
121 valueIface->value(value);
122 }
123 break;
Kun Yi94a04c42019-08-21 09:43:20 -0700124 // clang-format on
Patrick Venturefeb744a2019-06-26 19:07:48 -0700125 case InterfaceType::WARN:
126 checkThresholds<WarningObject>(iface.second, value);
127 break;
128 case InterfaceType::CRIT:
129 checkThresholds<CriticalObject>(iface.second, value);
130 break;
131 default:
132 break;
133 }
134 }
135}
136
Matthew Barth979c8062018-04-17 11:37:15 -0500137std::string MainLoop::getID(SensorSet::container_t::const_reference sensor)
Matthew Barth31d214c2018-03-26 09:54:27 -0500138{
Matthew Barth31d214c2018-03-26 09:54:27 -0500139 std::string id;
140
141 /*
142 * Check if the value of the MODE_<item><X> env variable for the sensor
Matt Spinler7c424802018-05-04 10:52:40 -0500143 * is set. If it is, then read the from the <item><X>_<mode>
Matthew Barth31d214c2018-03-26 09:54:27 -0500144 * file. The name of the DBUS object would be the value of the env
Matt Spinler7c424802018-05-04 10:52:40 -0500145 * variable LABEL_<item><mode value>. If the MODE_<item><X> env variable
Matthew Barth31d214c2018-03-26 09:54:27 -0500146 * doesn't exist, then the name of DBUS object is the value of the env
147 * variable LABEL_<item><X>.
Matt Spinler7c424802018-05-04 10:52:40 -0500148 *
149 * For example, if MODE_temp1 = "label", then code reads the temp1_label
150 * file. If it has a 5 in it, then it will use the following entry to
151 * name the object: LABEL_temp5 = "My DBus object name".
152 *
Matthew Barth31d214c2018-03-26 09:54:27 -0500153 */
Patrick Venture7a5285d2018-04-17 19:15:05 -0700154 auto mode = env::getEnv("MODE", sensor.first);
Matt Spinler7c424802018-05-04 10:52:40 -0500155 if (!mode.empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500156 {
Patrick Venture043d3232018-08-31 10:10:53 -0700157 id = env::getIndirectID(_hwmonRoot + '/' + _instance + '/', mode,
158 sensor.first);
Matthew Barth31d214c2018-03-26 09:54:27 -0500159
160 if (id.empty())
161 {
Matthew Barth979c8062018-04-17 11:37:15 -0500162 return id;
Matthew Barth31d214c2018-03-26 09:54:27 -0500163 }
164 }
165
166 // Use the ID we looked up above if there was one,
167 // otherwise use the standard one.
168 id = (id.empty()) ? sensor.first.second : id;
169
Matthew Barth979c8062018-04-17 11:37:15 -0500170 return id;
171}
172
Patrick Venture043d3232018-08-31 10:10:53 -0700173SensorIdentifiers
174 MainLoop::getIdentifiers(SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500175{
176 std::string id = getID(sensor);
177 std::string label;
George Liuc9d61612022-10-12 14:31:39 +0800178 std::string accuracy;
Lakshmi Yadlapati47fb49a2023-10-19 14:47:08 -0500179 std::string priority;
Matthew Barth979c8062018-04-17 11:37:15 -0500180
181 if (!id.empty())
182 {
183 // Ignore inputs without a label.
184 label = env::getEnv("LABEL", sensor.first.first, id);
George Liuc9d61612022-10-12 14:31:39 +0800185 accuracy = env::getEnv("ACCURACY", sensor.first.first, id);
Lakshmi Yadlapati47fb49a2023-10-19 14:47:08 -0500186 priority = env::getEnv("PRIORITY", sensor.first.first, id);
Matthew Barth979c8062018-04-17 11:37:15 -0500187 }
188
Lakshmi Yadlapati47fb49a2023-10-19 14:47:08 -0500189 return std::make_tuple(std::move(id), std::move(label), std::move(accuracy),
190 std::move(priority));
Matthew Barth979c8062018-04-17 11:37:15 -0500191}
192
193/**
194 * Reads the environment parameters of a sensor and creates an object with
195 * atleast the `Value` interface, otherwise returns without creating the object.
196 * If the `Value` interface is successfully created, by reading the sensor's
197 * corresponding sysfs file's value, the additional interfaces for the sensor
Matthew Barthd238e232018-04-17 12:01:50 -0500198 * are created and the InterfacesAdded signal is emitted. The object's state
199 * data is then returned for sensor state monitoring within the main loop.
Matthew Barth979c8062018-04-17 11:37:15 -0500200 */
William A. Kennington III4cbdfef2018-10-18 19:19:51 -0700201std::optional<ObjectStateData>
Patrick Venture043d3232018-08-31 10:10:53 -0700202 MainLoop::getObject(SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500203{
204 auto properties = getIdentifiers(sensor);
205 if (std::get<sensorID>(properties).empty() ||
206 std::get<sensorLabel>(properties).empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500207 {
Matthew Barthd238e232018-04-17 12:01:50 -0500208 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500209 }
210
Patrick Venture09791852018-04-17 17:40:00 -0700211 hwmon::Attributes attrs;
212 if (!hwmon::getAttributes(sensor.first.first, attrs))
Matthew Barth31d214c2018-03-26 09:54:27 -0500213 {
Matthew Barthd238e232018-04-17 12:01:50 -0500214 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500215 }
216
Kun Yi501ade22019-07-15 15:00:32 -0700217 const auto& [sensorSetKey, sensorAttrs] = sensor;
218 const auto& [sensorSysfsType, sensorSysfsNum] = sensorSetKey;
219
Patrick Venture2864b062018-12-19 08:13:41 -0800220 /* Note: The sensor objects all share the same ioAccess object. */
Patrick Williamse8771fd2023-05-10 07:51:06 -0500221 auto sensorObj = std::make_unique<sensor::Sensor>(sensorSetKey, _ioAccess,
222 _devPath);
Matthew Barth9c431062018-05-07 13:55:29 -0500223
Matthew Barthb7985272018-04-17 10:50:36 -0500224 // Get list of return codes for removing sensors on device
225 auto devRmRCs = env::getEnv("REMOVERCS");
226 // Add sensor removal return codes defined at the device level
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500227 sensorObj->addRemoveRCs(devRmRCs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500228
229 std::string objectPath{_root};
230 objectPath.append(1, '/');
Patrick Venture09791852018-04-17 17:40:00 -0700231 objectPath.append(hwmon::getNamespace(attrs));
Matthew Barth31d214c2018-03-26 09:54:27 -0500232 objectPath.append(1, '/');
Matthew Barth979c8062018-04-17 11:37:15 -0500233 objectPath.append(std::get<sensorLabel>(properties));
Matthew Barth31d214c2018-03-26 09:54:27 -0500234
Patrick Venture62067232019-06-19 17:39:33 -0700235 ObjectInfo info(&_bus, std::move(objectPath), InterfaceMap());
Patrick Venture75e56c62018-04-20 18:10:15 -0700236 RetryIO retryIO(hwmonio::retries, hwmonio::delay);
Kun Yi501ade22019-07-15 15:00:32 -0700237 if (_rmSensors.find(sensorSetKey) != _rmSensors.end())
Matthew Barthd4beecf2018-04-03 15:50:22 -0500238 {
239 // When adding a sensor that was purposely removed,
240 // don't retry on errors when reading its value
241 std::get<size_t>(retryIO) = 0;
242 }
Patrick Venture043d3232018-08-31 10:10:53 -0700243 auto valueInterface = static_cast<std::shared_ptr<ValueObject>>(nullptr);
Matthew Barth31d214c2018-03-26 09:54:27 -0500244 try
245 {
George Liuc9d61612022-10-12 14:31:39 +0800246 // Add accuracy interface
247 auto accuracyStr = std::get<sensorAccuracy>(properties);
248 try
249 {
250 if (!accuracyStr.empty())
251 {
252 auto accuracy = stod(accuracyStr);
253 sensorObj->addAccuracy(info, accuracy);
254 }
255 }
256 catch (const std::invalid_argument&)
Patrick Williamse8771fd2023-05-10 07:51:06 -0500257 {}
George Liuc9d61612022-10-12 14:31:39 +0800258
Lakshmi Yadlapati47fb49a2023-10-19 14:47:08 -0500259 // Add priority interface
260 auto priorityStr = std::get<sensorPriority>(properties);
261 try
262 {
263 if (!priorityStr.empty())
264 {
265 auto priority = std::stoul(priorityStr);
266 sensorObj->addPriority(info, priority);
267 }
268 }
269 catch (const std::invalid_argument&)
270 {}
271
Matthew Barthca44c2e2018-04-24 15:33:25 -0500272 // Add status interface based on _fault file being present
Matthew Barth2e41b132018-05-07 14:15:45 -0500273 sensorObj->addStatus(info);
Brandon Kim6d50c3e2019-08-09 15:38:53 -0700274 valueInterface = sensorObj->addValue(retryIO, info, _timedoutMap);
Matthew Barth31d214c2018-03-26 09:54:27 -0500275 }
276 catch (const std::system_error& e)
277 {
Patrick Williamse8771fd2023-05-10 07:51:06 -0500278 auto file = sysfs::make_sysfs_path(_ioAccess->path(), sensorSysfsType,
279 sensorSysfsNum,
280 hwmon::entry::cinput);
Patrick Venture0892c3f2019-06-27 14:24:03 -0700281
Matthew Barth31d214c2018-03-26 09:54:27 -0500282 // Check sensorAdjusts for sensor removal RCs
Matthew Barthac473092018-05-07 14:41:46 -0500283 auto& sAdjusts = sensorObj->getAdjusts();
284 if (sAdjusts.rmRCs.count(e.code().value()) > 0)
Matthew Barth31d214c2018-03-26 09:54:27 -0500285 {
Matthew Barthac473092018-05-07 14:41:46 -0500286 // Return code found in sensor return code removal list
Kun Yi501ade22019-07-15 15:00:32 -0700287 if (_rmSensors.find(sensorSetKey) == _rmSensors.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500288 {
Matthew Barthac473092018-05-07 14:41:46 -0500289 // Trace for sensor not already removed from dbus
290 log<level::INFO>("Sensor not added to dbus for read fail",
Patrick Venture043d3232018-08-31 10:10:53 -0700291 entry("FILE=%s", file.c_str()),
292 entry("RC=%d", e.code().value()));
Kun Yi501ade22019-07-15 15:00:32 -0700293 _rmSensors[std::move(sensorSetKey)] = std::move(sensorAttrs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500294 }
Matthew Barthac473092018-05-07 14:41:46 -0500295 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500296 }
Patrick Venture0892c3f2019-06-27 14:24:03 -0700297
Patrick Venture043d3232018-08-31 10:10:53 -0700298 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error;
Matthew Barth31d214c2018-03-26 09:54:27 -0500299 report<ReadFailure>(
Patrick Venture043d3232018-08-31 10:10:53 -0700300 xyz::openbmc_project::Sensor::Device::ReadFailure::CALLOUT_ERRNO(
301 e.code().value()),
302 xyz::openbmc_project::Sensor::Device::ReadFailure::
303 CALLOUT_DEVICE_PATH(_devPath.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500304
Matt Spinler6a391de2020-07-08 13:03:10 -0500305 log<level::INFO>(fmt::format("Failing sysfs file: {} errno: {}", file,
306 e.code().value())
307 .c_str());
Matthew Barth31d214c2018-03-26 09:54:27 -0500308 exit(EXIT_FAILURE);
Matthew Barth31d214c2018-03-26 09:54:27 -0500309 }
310 auto sensorValue = valueInterface->value();
Matt Spinlerecac0ae2020-07-08 13:09:08 -0500311 int64_t scale = sensorObj->getScale();
312
Kun Yi501ade22019-07-15 15:00:32 -0700313 addThreshold<WarningObject>(sensorSysfsType, std::get<sensorID>(properties),
314 sensorValue, info, scale);
315 addThreshold<CriticalObject>(sensorSysfsType,
James Feistee73f5b2018-08-01 16:31:42 -0700316 std::get<sensorID>(properties), sensorValue,
317 info, scale);
Matthew Barth31d214c2018-03-26 09:54:27 -0500318
Patrick Williamse8771fd2023-05-10 07:51:06 -0500319 auto target = addTarget<hwmon::FanSpeed>(sensorSetKey, _ioAccess, _devPath,
320 info);
Matthew Barth28f8e662018-03-26 16:57:36 -0500321 if (target)
Matthew Barth31d214c2018-03-26 09:54:27 -0500322 {
Matthew Barth28f8e662018-03-26 16:57:36 -0500323 target->enable();
Matthew Barth31d214c2018-03-26 09:54:27 -0500324 }
Kun Yi501ade22019-07-15 15:00:32 -0700325 addTarget<hwmon::FanPwm>(sensorSetKey, _ioAccess, _devPath, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500326
327 // All the interfaces have been created. Go ahead
328 // and emit InterfacesAdded.
329 valueInterface->emit_object_added();
330
Matthew Barth9c431062018-05-07 13:55:29 -0500331 // Save sensor object specifications
Kun Yi501ade22019-07-15 15:00:32 -0700332 _sensorObjects[sensorSetKey] = std::move(sensorObj);
Matthew Barth9c431062018-05-07 13:55:29 -0500333
Matthew Barthd238e232018-04-17 12:01:50 -0500334 return std::make_pair(std::move(std::get<sensorLabel>(properties)),
335 std::move(info));
Matthew Barth31d214c2018-03-26 09:54:27 -0500336}
337
Patrick Williamsad6043f2022-07-22 19:26:56 -0500338MainLoop::MainLoop(sdbusplus::bus_t&& bus, const std::string& param,
Patrick Venture043d3232018-08-31 10:10:53 -0700339 const std::string& path, const std::string& devPath,
Patrick Venture16805a62019-06-21 14:19:33 -0700340 const char* prefix, const char* root,
Anton D. Kachalovd46d0812021-02-03 23:25:37 +0100341 const std::string& instanceId,
Patrick Venture16805a62019-06-21 14:19:33 -0700342 const hwmonio::HwmonIOInterface* ioIntf) :
Patrick Venture043d3232018-08-31 10:10:53 -0700343 _bus(std::move(bus)),
344 _manager(_bus, root), _pathParam(param), _hwmonRoot(), _instance(),
Patrick Venture16805a62019-06-21 14:19:33 -0700345 _devPath(devPath), _prefix(prefix), _root(root), _state(),
Anton D. Kachalovd46d0812021-02-03 23:25:37 +0100346 _instanceId(instanceId), _ioAccess(ioIntf),
347 _event(sdeventplus::Event::get_default()),
Patrick Venture52b40612018-12-19 13:36:41 -0800348 _timer(_event, std::bind(&MainLoop::read, this))
Brad Bishopd499ca62016-12-19 09:24:50 -0500349{
Patrick Venture73a50c72018-04-17 15:19:03 -0700350 // Strip off any trailing slashes.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500351 std::string p = path;
352 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500353 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500354 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500355 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500356
Patrick Venture73a50c72018-04-17 15:19:03 -0700357 // Given the furthest right /, set instance to
358 // the basename, and hwmonRoot to the leading path.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500359 auto n = p.rfind('/');
360 if (n != std::string::npos)
361 {
362 _instance.assign(p.substr(n + 1));
363 _hwmonRoot.assign(p.substr(0, n));
364 }
365
366 assert(!_instance.empty());
367 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500368}
369
370void MainLoop::shutdown() noexcept
371{
Patrick Venture52b40612018-12-19 13:36:41 -0800372 _event.exit(0);
Brad Bishopd499ca62016-12-19 09:24:50 -0500373}
374
375void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500376{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600377 init();
378
Patrick Venture043d3232018-08-31 10:10:53 -0700379 std::function<void()> callback(std::bind(&MainLoop::read, this));
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600380 try
381 {
Patrick Venture52b40612018-12-19 13:36:41 -0800382 _timer.restart(std::chrono::microseconds(_interval));
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600383
384 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
385
386 // TODO: Issue#7 - Should probably periodically check the SensorSet
387 // for new entries.
388
Patrick Venture52b40612018-12-19 13:36:41 -0800389 _bus.attach_event(_event.get(), SD_EVENT_PRIORITY_IMPORTANT);
390 _event.loop();
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600391 }
William A. Kennington III0fe4cb32018-10-18 19:19:58 -0700392 catch (const std::exception& e)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600393 {
394 log<level::ERR>("Error in sysfs polling loop",
395 entry("ERROR=%s", e.what()));
396 throw;
397 }
398}
399
400void MainLoop::init()
401{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500402 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500403 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500404
Patrick Venturedb7ecb62018-10-23 19:42:23 -0700405 for (const auto& i : *sensors)
Brad Bishop75b4ab82017-01-06 09:33:50 -0500406 {
Matthew Barthd238e232018-04-17 12:01:50 -0500407 auto object = getObject(i);
408 if (object)
409 {
410 // Construct the SensorSet value
411 // std::tuple<SensorSet::mapped_type,
412 // std::string(Sensor Label),
413 // ObjectInfo>
Patrick Williamse8771fd2023-05-10 07:51:06 -0500414 auto value = std::make_tuple(std::move(i.second),
415 std::move((*object).first),
416 std::move((*object).second));
Matthew Barthd238e232018-04-17 12:01:50 -0500417
Patrick Venture52b40612018-12-19 13:36:41 -0800418 _state[std::move(i.first)] = std::move(value);
Matthew Barthd238e232018-04-17 12:01:50 -0500419 }
Carol Wang9bbe6022019-08-01 17:31:30 +0800420
421 // Initialize _averageMap of sensor. e.g. <<power, 1>, <0, 0>>
422 if ((i.first.first == hwmon::type::power) &&
423 (phosphor::utility::isAverageEnvSet(i.first)))
424 {
425 _average.setAverageValue(i.first, std::make_pair(0, 0));
426 }
Brad Bishop75b4ab82017-01-06 09:33:50 -0500427 }
428
Patrick Venture62503a42017-05-23 07:30:29 -0700429 /* If there are no sensors specified by labels, exit. */
Patrick Venture52b40612018-12-19 13:36:41 -0800430 if (0 == _state.size())
Patrick Venture62503a42017-05-23 07:30:29 -0700431 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600432 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700433 }
434
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500435 {
Patrick Venturec897d8b2018-04-23 19:01:56 -0700436 std::stringstream ss;
Anton D. Kachalovd46d0812021-02-03 23:25:37 +0100437 std::string id = _instanceId;
438 if (id.empty())
439 {
440 id =
441 std::to_string(std::hash<std::string>{}(_devPath + _pathParam));
442 }
443 ss << _prefix << "-" << id << ".Hwmon1";
Patrick Venturec897d8b2018-04-23 19:01:56 -0700444
445 _bus.request_name(ss.str().c_str());
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500446 }
447
Patrick Ventureab10f162017-05-22 09:44:50 -0700448 {
Patrick Venturea24c8802018-04-17 19:38:06 -0700449 auto interval = env::getEnv("INTERVAL");
450 if (!interval.empty())
Patrick Ventureab10f162017-05-22 09:44:50 -0700451 {
Patrick Venture50cf1c52018-04-18 09:21:41 -0700452 _interval = std::strtoull(interval.c_str(), NULL, 10);
Patrick Ventureab10f162017-05-22 09:44:50 -0700453 }
454 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600455}
Patrick Ventureab10f162017-05-22 09:44:50 -0700456
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600457void MainLoop::read()
458{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500459 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
460 // ensure the objects all exist?
461
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600462 // Iterate through all the sensors.
Kun Yi501ade22019-07-15 15:00:32 -0700463 for (auto& [sensorSetKey, sensorStateTuple] : _state)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600464 {
Kun Yi501ade22019-07-15 15:00:32 -0700465 const auto& [sensorSysfsType, sensorSysfsNum] = sensorSetKey;
466 auto& [attrs, unused, objInfo] = sensorStateTuple;
467
Kun Yi553552c2019-07-15 22:14:21 -0700468 if (attrs.find(hwmon::entry::input) == attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500469 {
Kun Yi553552c2019-07-15 22:14:21 -0700470 continue;
471 }
Kun Yi501ade22019-07-15 15:00:32 -0700472
Kun Yi553552c2019-07-15 22:14:21 -0700473 // Read value from sensor.
Carol Wang9bbe6022019-08-01 17:31:30 +0800474 std::string input = hwmon::entry::input;
475 if (sensorSysfsType == hwmon::type::pwm)
Kun Yi553552c2019-07-15 22:14:21 -0700476 {
477 input = "";
478 }
Carol Wang9bbe6022019-08-01 17:31:30 +0800479 // If type is power and AVERAGE_power* is true in env, use average
480 // instead of input
481 else if ((sensorSysfsType == hwmon::type::power) &&
482 (phosphor::utility::isAverageEnvSet(sensorSetKey)))
483 {
484 input = hwmon::entry::average;
485 }
Kun Yi553552c2019-07-15 22:14:21 -0700486
Matt Spinlerecac0ae2020-07-08 13:09:08 -0500487 SensorValueType value;
Kun Yi553552c2019-07-15 22:14:21 -0700488 auto& obj = std::get<InterfaceMap>(objInfo);
Kun Yi501ade22019-07-15 15:00:32 -0700489 std::unique_ptr<sensor::Sensor>& sensor = _sensorObjects[sensorSetKey];
Kun Yi553552c2019-07-15 22:14:21 -0700490
491 auto& statusIface = std::any_cast<std::shared_ptr<StatusObject>&>(
492 obj[InterfaceType::STATUS]);
493 // As long as addStatus is called before addValue, statusIface
494 // should never be nullptr.
495 assert(statusIface);
496
497 try
498 {
499 if (sensor->hasFaultFile())
Patrick Venture043d3232018-08-31 10:10:53 -0700500 {
Kun Yi501ade22019-07-15 15:00:32 -0700501 auto fault = _ioAccess->read(sensorSysfsType, sensorSysfsNum,
Kun Yi553552c2019-07-15 22:14:21 -0700502 hwmon::entry::fault,
503 hwmonio::retries, hwmonio::delay);
504 // Skip reading from a sensor with a valid fault file
505 // and set the functional property accordingly
506 if (!statusIface->functional((fault == 0) ? true : false))
Matthew Barth27c4a392018-04-25 14:38:51 -0500507 {
Matthew Barthac473092018-05-07 14:41:46 -0500508 continue;
Matthew Barth8772ce32018-03-22 16:03:06 -0500509 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500510 }
Kun Yi553552c2019-07-15 22:14:21 -0700511
512 {
513 // RAII object for GPIO unlock / lock
William A. Kennington III2227bd52019-06-19 11:32:22 -0700514 auto locker = sensor::gpioUnlock(sensor->getGpio());
Kun Yi553552c2019-07-15 22:14:21 -0700515
Brandon Kim6d50c3e2019-08-09 15:38:53 -0700516 // For sensors with attribute ASYNC_READ_TIMEOUT,
517 // spawn a thread with timeout
Patrick Williamse8771fd2023-05-10 07:51:06 -0500518 auto asyncReadTimeout = env::getEnv("ASYNC_READ_TIMEOUT",
519 sensorSetKey);
Brandon Kim6d50c3e2019-08-09 15:38:53 -0700520 if (!asyncReadTimeout.empty())
521 {
522 std::chrono::milliseconds asyncTimeout{
523 std::stoi(asyncReadTimeout)};
524 value = sensor::asyncRead(
525 sensorSetKey, _ioAccess, asyncTimeout, _timedoutMap,
526 sensorSysfsType, sensorSysfsNum, input,
527 hwmonio::retries, hwmonio::delay);
528 }
529 else
530 {
531 // Retry for up to a second if device is busy
532 // or has a transient error.
Patrick Williamse8771fd2023-05-10 07:51:06 -0500533 value = _ioAccess->read(sensorSysfsType, sensorSysfsNum,
534 input, hwmonio::retries,
535 hwmonio::delay);
Brandon Kim6d50c3e2019-08-09 15:38:53 -0700536 }
537
Kun Yi553552c2019-07-15 22:14:21 -0700538 // Set functional property to true if we could read sensor
539 statusIface->functional(true);
540
541 value = sensor->adjustValue(value);
Carol Wang9bbe6022019-08-01 17:31:30 +0800542
543 if (input == hwmon::entry::average)
544 {
545 // Calculate the values of averageMap based on current
546 // average value, current average_interval value, previous
547 // average value, previous average_interval value
548 int64_t interval =
549 _ioAccess->read(sensorSysfsType, sensorSysfsNum,
550 hwmon::entry::caverage_interval,
551 hwmonio::retries, hwmonio::delay);
552 auto ret = _average.getAverageValue(sensorSetKey);
553 assert(ret);
554
555 const auto& [preAverage, preInterval] = *ret;
556
557 auto calValue = Average::calcAverage(
558 preAverage, preInterval, value, interval);
559 if (calValue)
560 {
561 // Update previous values in averageMap before the
562 // variable value is changed next
563 _average.setAverageValue(
564 sensorSetKey, std::make_pair(value, interval));
565 // Update value to be calculated average
566 value = calValue.value();
567 }
568 else
569 {
570 // the value of
571 // power*_average_interval is not changed yet, use the
572 // previous calculated average instead. So skip dbus
573 // update.
574 continue;
575 }
576 }
Kun Yi553552c2019-07-15 22:14:21 -0700577 }
578
579 updateSensorInterfaces(obj, value);
580 }
581 catch (const std::system_error& e)
582 {
Matt Spinlerd8cacfd2021-04-26 09:56:21 -0500583#if UPDATE_FUNCTIONAL_ON_FAIL
Kun Yi553552c2019-07-15 22:14:21 -0700584 // If UPDATE_FUNCTIONAL_ON_FAIL is defined and an exception was
585 // thrown, set the functional property to false.
586 // We cannot set this with the 'continue' in the lower block
587 // as the code may exit before reaching it.
588 statusIface->functional(false);
589#endif
Carol Wang9bbe6022019-08-01 17:31:30 +0800590 auto file = sysfs::make_sysfs_path(
591 _ioAccess->path(), sensorSysfsType, sensorSysfsNum, input);
Kun Yi553552c2019-07-15 22:14:21 -0700592
593 // Check sensorAdjusts for sensor removal RCs
Kun Yi501ade22019-07-15 15:00:32 -0700594 auto& sAdjusts = _sensorObjects[sensorSetKey]->getAdjusts();
Kun Yi553552c2019-07-15 22:14:21 -0700595 if (sAdjusts.rmRCs.count(e.code().value()) > 0)
596 {
597 // Return code found in sensor return code removal list
Kun Yi501ade22019-07-15 15:00:32 -0700598 if (_rmSensors.find(sensorSetKey) == _rmSensors.end())
Kun Yi553552c2019-07-15 22:14:21 -0700599 {
600 // Trace for sensor not already removed from dbus
601 log<level::INFO>("Remove sensor from dbus for read fail",
602 entry("FILE=%s", file.c_str()),
603 entry("RC=%d", e.code().value()));
604 // Mark this sensor to be removed from dbus
Kun Yi501ade22019-07-15 15:00:32 -0700605 _rmSensors[sensorSetKey] = attrs;
Kun Yi553552c2019-07-15 22:14:21 -0700606 }
607 continue;
608 }
Matt Spinlerd8cacfd2021-04-26 09:56:21 -0500609#if UPDATE_FUNCTIONAL_ON_FAIL
Kun Yi553552c2019-07-15 22:14:21 -0700610 // Do not exit with failure if UPDATE_FUNCTIONAL_ON_FAIL is set
611 continue;
612#endif
613 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::
614 Error;
615 report<ReadFailure>(
616 xyz::openbmc_project::Sensor::Device::ReadFailure::
617 CALLOUT_ERRNO(e.code().value()),
618 xyz::openbmc_project::Sensor::Device::ReadFailure::
619 CALLOUT_DEVICE_PATH(_devPath.c_str()));
620
Matt Spinler6a391de2020-07-08 13:03:10 -0500621 log<level::INFO>(fmt::format("Failing sysfs file: {} errno: {}",
622 file, e.code().value())
623 .c_str());
Kun Yi553552c2019-07-15 22:14:21 -0700624
625 exit(EXIT_FAILURE);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500626 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600627 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500628
Patrick Venture0cd4f692019-06-21 13:39:40 -0700629 removeSensors();
630
Patrick Venture0cd4f692019-06-21 13:39:40 -0700631 addDroppedSensors();
Patrick Venture0cd4f692019-06-21 13:39:40 -0700632}
633
634void MainLoop::removeSensors()
635{
Matthew Barth8772ce32018-03-22 16:03:06 -0500636 // Remove any sensors marked for removal
Patrick Venture52b40612018-12-19 13:36:41 -0800637 for (const auto& i : _rmSensors)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600638 {
Matthew Barthd0ce7922019-06-06 09:23:37 -0500639 // Remove sensor object from dbus using emit_object_removed()
640 auto& objInfo = std::get<ObjectInfo>(_state[i.first]);
641 auto& objPath = std::get<std::string>(objInfo);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700642
Matthew Barthd0ce7922019-06-06 09:23:37 -0500643 _bus.emit_object_removed(objPath.c_str());
Patrick Venture0cd4f692019-06-21 13:39:40 -0700644
Matthew Barthd0ce7922019-06-06 09:23:37 -0500645 // Erase sensor object info
Patrick Venture52b40612018-12-19 13:36:41 -0800646 _state.erase(i.first);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500647 }
Patrick Venture0cd4f692019-06-21 13:39:40 -0700648}
Matthew Barth31d214c2018-03-26 09:54:27 -0500649
Patrick Venture0cd4f692019-06-21 13:39:40 -0700650void MainLoop::addDroppedSensors()
651{
Matthew Barth31d214c2018-03-26 09:54:27 -0500652 // Attempt to add any sensors that were removed
Patrick Venture52b40612018-12-19 13:36:41 -0800653 auto it = _rmSensors.begin();
654 while (it != _rmSensors.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500655 {
Patrick Venture52b40612018-12-19 13:36:41 -0800656 if (_state.find(it->first) == _state.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500657 {
658 SensorSet::container_t::value_type ssValueType =
Patrick Venture043d3232018-08-31 10:10:53 -0700659 std::make_pair(it->first, it->second);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700660
Matthew Barthd238e232018-04-17 12:01:50 -0500661 auto object = getObject(ssValueType);
662 if (object)
Matthew Barth31d214c2018-03-26 09:54:27 -0500663 {
Matthew Barthd238e232018-04-17 12:01:50 -0500664 // Construct the SensorSet value
665 // std::tuple<SensorSet::mapped_type,
666 // std::string(Sensor Label),
667 // ObjectInfo>
668 auto value = std::make_tuple(std::move(ssValueType.second),
669 std::move((*object).first),
670 std::move((*object).second));
671
Patrick Venture52b40612018-12-19 13:36:41 -0800672 _state[std::move(ssValueType.first)] = std::move(value);
Matthew Barthd238e232018-04-17 12:01:50 -0500673
Carol Wang9bbe6022019-08-01 17:31:30 +0800674 std::string input = hwmon::entry::input;
675 // If type is power and AVERAGE_power* is true in env, use
676 // average instead of input
677 if ((it->first.first == hwmon::type::power) &&
678 (phosphor::utility::isAverageEnvSet(it->first)))
679 {
680 input = hwmon::entry::average;
681 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500682 // Sensor object added, erase entry from removal list
Patrick Williamse8771fd2023-05-10 07:51:06 -0500683 auto file = sysfs::make_sysfs_path(_ioAccess->path(),
684 it->first.first,
685 it->first.second, input);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700686
Patrick Venture043d3232018-08-31 10:10:53 -0700687 log<level::INFO>("Added sensor to dbus after successful read",
688 entry("FILE=%s", file.c_str()));
Patrick Venture0cd4f692019-06-21 13:39:40 -0700689
Patrick Venture52b40612018-12-19 13:36:41 -0800690 it = _rmSensors.erase(it);
Matthew Barth31d214c2018-03-26 09:54:27 -0500691 }
692 else
693 {
694 ++it;
695 }
696 }
697 else
698 {
699 // Sanity check to remove sensors that were re-added
Patrick Venture52b40612018-12-19 13:36:41 -0800700 it = _rmSensors.erase(it);
Matthew Barth31d214c2018-03-26 09:54:27 -0500701 }
702 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500703}
704
705// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4