blob: 0b6d070f56e478a3968854cba3e88856e3d94748 [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
William A. Kennington III0e749752018-11-06 15:25:41 -080034#include <cassert>
Patrick Venture043d3232018-08-31 10:10:53 -070035#include <cstdlib>
36#include <functional>
Brandon Kim6d50c3e2019-08-09 15:38:53 -070037#include <future>
Patrick Venture043d3232018-08-31 10:10:53 -070038#include <iostream>
39#include <memory>
40#include <phosphor-logging/elog-errors.hpp>
41#include <sstream>
42#include <string>
43#include <unordered_set>
Patrick Venture1e6324f2017-06-01 14:07:05 -070044#include <xyz/openbmc_project/Sensor/Device/error.hpp>
45
46using namespace phosphor::logging;
47
Saqib Khan973886d2017-03-15 14:01:16 -050048// Initialization for Warning Objects
49decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo =
50 &WarningObject::warningLow;
51decltype(Thresholds<WarningObject>::setHi) Thresholds<WarningObject>::setHi =
52 &WarningObject::warningHigh;
53decltype(Thresholds<WarningObject>::getLo) Thresholds<WarningObject>::getLo =
54 &WarningObject::warningLow;
55decltype(Thresholds<WarningObject>::getHi) Thresholds<WarningObject>::getHi =
56 &WarningObject::warningHigh;
Patrick Venture043d3232018-08-31 10:10:53 -070057decltype(
58 Thresholds<WarningObject>::alarmLo) Thresholds<WarningObject>::alarmLo =
Saqib Khan973886d2017-03-15 14:01:16 -050059 &WarningObject::warningAlarmLow;
Patrick Venture043d3232018-08-31 10:10:53 -070060decltype(
61 Thresholds<WarningObject>::alarmHi) Thresholds<WarningObject>::alarmHi =
Saqib Khan973886d2017-03-15 14:01:16 -050062 &WarningObject::warningAlarmHigh;
Duke Du73769092021-04-14 15:35:21 +080063decltype(Thresholds<WarningObject>::getAlarmLow)
64 Thresholds<WarningObject>::getAlarmLow = &WarningObject::warningAlarmLow;
65decltype(Thresholds<WarningObject>::getAlarmHigh)
66 Thresholds<WarningObject>::getAlarmHigh = &WarningObject::warningAlarmHigh;
67decltype(Thresholds<WarningObject>::assertLowSignal)
68 Thresholds<WarningObject>::assertLowSignal =
69 &WarningObject::warningLowAlarmAsserted;
70decltype(Thresholds<WarningObject>::assertHighSignal)
71 Thresholds<WarningObject>::assertHighSignal =
72 &WarningObject::warningHighAlarmAsserted;
73decltype(Thresholds<WarningObject>::deassertLowSignal)
74 Thresholds<WarningObject>::deassertLowSignal =
75 &WarningObject::warningLowAlarmDeasserted;
76decltype(Thresholds<WarningObject>::deassertHighSignal)
77 Thresholds<WarningObject>::deassertHighSignal =
78 &WarningObject::warningHighAlarmDeasserted;
Saqib Khan973886d2017-03-15 14:01:16 -050079
80// Initialization for Critical Objects
81decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo =
82 &CriticalObject::criticalLow;
83decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi =
84 &CriticalObject::criticalHigh;
85decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo =
86 &CriticalObject::criticalLow;
87decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi =
88 &CriticalObject::criticalHigh;
Patrick Venture043d3232018-08-31 10:10:53 -070089decltype(
90 Thresholds<CriticalObject>::alarmLo) Thresholds<CriticalObject>::alarmLo =
Saqib Khan973886d2017-03-15 14:01:16 -050091 &CriticalObject::criticalAlarmLow;
Patrick Venture043d3232018-08-31 10:10:53 -070092decltype(
93 Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi =
Saqib Khan973886d2017-03-15 14:01:16 -050094 &CriticalObject::criticalAlarmHigh;
Duke Du73769092021-04-14 15:35:21 +080095decltype(Thresholds<CriticalObject>::getAlarmLow)
96 Thresholds<CriticalObject>::getAlarmLow = &CriticalObject::criticalAlarmLow;
97decltype(Thresholds<CriticalObject>::getAlarmHigh)
98 Thresholds<CriticalObject>::getAlarmHigh =
99 &CriticalObject::criticalAlarmHigh;
100decltype(Thresholds<CriticalObject>::assertLowSignal)
101 Thresholds<CriticalObject>::assertLowSignal =
102 &CriticalObject::criticalLowAlarmAsserted;
103decltype(Thresholds<CriticalObject>::assertHighSignal)
104 Thresholds<CriticalObject>::assertHighSignal =
105 &CriticalObject::criticalHighAlarmAsserted;
106decltype(Thresholds<CriticalObject>::deassertLowSignal)
107 Thresholds<CriticalObject>::deassertLowSignal =
108 &CriticalObject::criticalLowAlarmDeasserted;
109decltype(Thresholds<CriticalObject>::deassertHighSignal)
110 Thresholds<CriticalObject>::deassertHighSignal =
111 &CriticalObject::criticalHighAlarmDeasserted;
Saqib Khan973886d2017-03-15 14:01:16 -0500112
Matt Spinlerecac0ae2020-07-08 13:09:08 -0500113void updateSensorInterfaces(InterfaceMap& ifaces, SensorValueType value)
Patrick Venturefeb744a2019-06-26 19:07:48 -0700114{
115 for (auto& iface : ifaces)
116 {
117 switch (iface.first)
118 {
Kun Yi94a04c42019-08-21 09:43:20 -0700119 // clang-format off
Patrick Venturefeb744a2019-06-26 19:07:48 -0700120 case InterfaceType::VALUE:
121 {
122 auto& valueIface =
123 std::any_cast<std::shared_ptr<ValueObject>&>(iface.second);
124 valueIface->value(value);
125 }
126 break;
Kun Yi94a04c42019-08-21 09:43:20 -0700127 // clang-format on
Patrick Venturefeb744a2019-06-26 19:07:48 -0700128 case InterfaceType::WARN:
129 checkThresholds<WarningObject>(iface.second, value);
130 break;
131 case InterfaceType::CRIT:
132 checkThresholds<CriticalObject>(iface.second, value);
133 break;
134 default:
135 break;
136 }
137 }
138}
139
Matthew Barth979c8062018-04-17 11:37:15 -0500140std::string MainLoop::getID(SensorSet::container_t::const_reference sensor)
Matthew Barth31d214c2018-03-26 09:54:27 -0500141{
Matthew Barth31d214c2018-03-26 09:54:27 -0500142 std::string id;
143
144 /*
145 * Check if the value of the MODE_<item><X> env variable for the sensor
Matt Spinler7c424802018-05-04 10:52:40 -0500146 * is set. If it is, then read the from the <item><X>_<mode>
Matthew Barth31d214c2018-03-26 09:54:27 -0500147 * file. The name of the DBUS object would be the value of the env
Matt Spinler7c424802018-05-04 10:52:40 -0500148 * variable LABEL_<item><mode value>. If the MODE_<item><X> env variable
Matthew Barth31d214c2018-03-26 09:54:27 -0500149 * doesn't exist, then the name of DBUS object is the value of the env
150 * variable LABEL_<item><X>.
Matt Spinler7c424802018-05-04 10:52:40 -0500151 *
152 * For example, if MODE_temp1 = "label", then code reads the temp1_label
153 * file. If it has a 5 in it, then it will use the following entry to
154 * name the object: LABEL_temp5 = "My DBus object name".
155 *
Matthew Barth31d214c2018-03-26 09:54:27 -0500156 */
Patrick Venture7a5285d2018-04-17 19:15:05 -0700157 auto mode = env::getEnv("MODE", sensor.first);
Matt Spinler7c424802018-05-04 10:52:40 -0500158 if (!mode.empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500159 {
Patrick Venture043d3232018-08-31 10:10:53 -0700160 id = env::getIndirectID(_hwmonRoot + '/' + _instance + '/', mode,
161 sensor.first);
Matthew Barth31d214c2018-03-26 09:54:27 -0500162
163 if (id.empty())
164 {
Matthew Barth979c8062018-04-17 11:37:15 -0500165 return id;
Matthew Barth31d214c2018-03-26 09:54:27 -0500166 }
167 }
168
169 // Use the ID we looked up above if there was one,
170 // otherwise use the standard one.
171 id = (id.empty()) ? sensor.first.second : id;
172
Matthew Barth979c8062018-04-17 11:37:15 -0500173 return id;
174}
175
Patrick Venture043d3232018-08-31 10:10:53 -0700176SensorIdentifiers
177 MainLoop::getIdentifiers(SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500178{
179 std::string id = getID(sensor);
180 std::string label;
181
182 if (!id.empty())
183 {
184 // Ignore inputs without a label.
185 label = env::getEnv("LABEL", sensor.first.first, id);
186 }
187
Patrick Venture043d3232018-08-31 10:10:53 -0700188 return std::make_tuple(std::move(id), std::move(label));
Matthew Barth979c8062018-04-17 11:37:15 -0500189}
190
191/**
192 * Reads the environment parameters of a sensor and creates an object with
193 * atleast the `Value` interface, otherwise returns without creating the object.
194 * If the `Value` interface is successfully created, by reading the sensor's
195 * corresponding sysfs file's value, the additional interfaces for the sensor
Matthew Barthd238e232018-04-17 12:01:50 -0500196 * are created and the InterfacesAdded signal is emitted. The object's state
197 * data is then returned for sensor state monitoring within the main loop.
Matthew Barth979c8062018-04-17 11:37:15 -0500198 */
William A. Kennington III4cbdfef2018-10-18 19:19:51 -0700199std::optional<ObjectStateData>
Patrick Venture043d3232018-08-31 10:10:53 -0700200 MainLoop::getObject(SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500201{
202 auto properties = getIdentifiers(sensor);
203 if (std::get<sensorID>(properties).empty() ||
204 std::get<sensorLabel>(properties).empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500205 {
Matthew Barthd238e232018-04-17 12:01:50 -0500206 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500207 }
208
Patrick Venture09791852018-04-17 17:40:00 -0700209 hwmon::Attributes attrs;
210 if (!hwmon::getAttributes(sensor.first.first, attrs))
Matthew Barth31d214c2018-03-26 09:54:27 -0500211 {
Matthew Barthd238e232018-04-17 12:01:50 -0500212 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500213 }
214
Kun Yi501ade22019-07-15 15:00:32 -0700215 const auto& [sensorSetKey, sensorAttrs] = sensor;
216 const auto& [sensorSysfsType, sensorSysfsNum] = sensorSetKey;
217
Patrick Venture2864b062018-12-19 08:13:41 -0800218 /* Note: The sensor objects all share the same ioAccess object. */
Patrick Venture043d3232018-08-31 10:10:53 -0700219 auto sensorObj =
Kun Yi501ade22019-07-15 15:00:32 -0700220 std::make_unique<sensor::Sensor>(sensorSetKey, _ioAccess, _devPath);
Matthew Barth9c431062018-05-07 13:55:29 -0500221
Matthew Barthb7985272018-04-17 10:50:36 -0500222 // Get list of return codes for removing sensors on device
223 auto devRmRCs = env::getEnv("REMOVERCS");
224 // Add sensor removal return codes defined at the device level
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500225 sensorObj->addRemoveRCs(devRmRCs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500226
227 std::string objectPath{_root};
228 objectPath.append(1, '/');
Patrick Venture09791852018-04-17 17:40:00 -0700229 objectPath.append(hwmon::getNamespace(attrs));
Matthew Barth31d214c2018-03-26 09:54:27 -0500230 objectPath.append(1, '/');
Matthew Barth979c8062018-04-17 11:37:15 -0500231 objectPath.append(std::get<sensorLabel>(properties));
Matthew Barth31d214c2018-03-26 09:54:27 -0500232
Patrick Venture62067232019-06-19 17:39:33 -0700233 ObjectInfo info(&_bus, std::move(objectPath), InterfaceMap());
Patrick Venture75e56c62018-04-20 18:10:15 -0700234 RetryIO retryIO(hwmonio::retries, hwmonio::delay);
Kun Yi501ade22019-07-15 15:00:32 -0700235 if (_rmSensors.find(sensorSetKey) != _rmSensors.end())
Matthew Barthd4beecf2018-04-03 15:50:22 -0500236 {
237 // When adding a sensor that was purposely removed,
238 // don't retry on errors when reading its value
239 std::get<size_t>(retryIO) = 0;
240 }
Patrick Venture043d3232018-08-31 10:10:53 -0700241 auto valueInterface = static_cast<std::shared_ptr<ValueObject>>(nullptr);
Matthew Barth31d214c2018-03-26 09:54:27 -0500242 try
243 {
Matthew Barthca44c2e2018-04-24 15:33:25 -0500244 // Add status interface based on _fault file being present
Matthew Barth2e41b132018-05-07 14:15:45 -0500245 sensorObj->addStatus(info);
Brandon Kim6d50c3e2019-08-09 15:38:53 -0700246 valueInterface = sensorObj->addValue(retryIO, info, _timedoutMap);
Matthew Barth31d214c2018-03-26 09:54:27 -0500247 }
248 catch (const std::system_error& e)
249 {
Patrick Venture043d3232018-08-31 10:10:53 -0700250 auto file =
Kun Yi501ade22019-07-15 15:00:32 -0700251 sysfs::make_sysfs_path(_ioAccess->path(), sensorSysfsType,
252 sensorSysfsNum, hwmon::entry::cinput);
Patrick Venture0892c3f2019-06-27 14:24:03 -0700253
Matthew Barth31d214c2018-03-26 09:54:27 -0500254 // Check sensorAdjusts for sensor removal RCs
Matthew Barthac473092018-05-07 14:41:46 -0500255 auto& sAdjusts = sensorObj->getAdjusts();
256 if (sAdjusts.rmRCs.count(e.code().value()) > 0)
Matthew Barth31d214c2018-03-26 09:54:27 -0500257 {
Matthew Barthac473092018-05-07 14:41:46 -0500258 // Return code found in sensor return code removal list
Kun Yi501ade22019-07-15 15:00:32 -0700259 if (_rmSensors.find(sensorSetKey) == _rmSensors.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500260 {
Matthew Barthac473092018-05-07 14:41:46 -0500261 // Trace for sensor not already removed from dbus
262 log<level::INFO>("Sensor not added to dbus for read fail",
Patrick Venture043d3232018-08-31 10:10:53 -0700263 entry("FILE=%s", file.c_str()),
264 entry("RC=%d", e.code().value()));
Kun Yi501ade22019-07-15 15:00:32 -0700265 _rmSensors[std::move(sensorSetKey)] = std::move(sensorAttrs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500266 }
Matthew Barthac473092018-05-07 14:41:46 -0500267 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500268 }
Patrick Venture0892c3f2019-06-27 14:24:03 -0700269
Patrick Venture043d3232018-08-31 10:10:53 -0700270 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error;
Matthew Barth31d214c2018-03-26 09:54:27 -0500271 report<ReadFailure>(
Patrick Venture043d3232018-08-31 10:10:53 -0700272 xyz::openbmc_project::Sensor::Device::ReadFailure::CALLOUT_ERRNO(
273 e.code().value()),
274 xyz::openbmc_project::Sensor::Device::ReadFailure::
275 CALLOUT_DEVICE_PATH(_devPath.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500276
Matt Spinler6a391de2020-07-08 13:03:10 -0500277 log<level::INFO>(fmt::format("Failing sysfs file: {} errno: {}", file,
278 e.code().value())
279 .c_str());
Matthew Barth31d214c2018-03-26 09:54:27 -0500280 exit(EXIT_FAILURE);
Matthew Barth31d214c2018-03-26 09:54:27 -0500281 }
282 auto sensorValue = valueInterface->value();
Matt Spinlerecac0ae2020-07-08 13:09:08 -0500283 int64_t scale = sensorObj->getScale();
284
Kun Yi501ade22019-07-15 15:00:32 -0700285 addThreshold<WarningObject>(sensorSysfsType, std::get<sensorID>(properties),
286 sensorValue, info, scale);
287 addThreshold<CriticalObject>(sensorSysfsType,
James Feistee73f5b2018-08-01 16:31:42 -0700288 std::get<sensorID>(properties), sensorValue,
289 info, scale);
Matthew Barth31d214c2018-03-26 09:54:27 -0500290
Patrick Venture043d3232018-08-31 10:10:53 -0700291 auto target =
Kun Yi501ade22019-07-15 15:00:32 -0700292 addTarget<hwmon::FanSpeed>(sensorSetKey, _ioAccess, _devPath, info);
Matthew Barth28f8e662018-03-26 16:57:36 -0500293 if (target)
Matthew Barth31d214c2018-03-26 09:54:27 -0500294 {
Matthew Barth28f8e662018-03-26 16:57:36 -0500295 target->enable();
Matthew Barth31d214c2018-03-26 09:54:27 -0500296 }
Kun Yi501ade22019-07-15 15:00:32 -0700297 addTarget<hwmon::FanPwm>(sensorSetKey, _ioAccess, _devPath, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500298
299 // All the interfaces have been created. Go ahead
300 // and emit InterfacesAdded.
301 valueInterface->emit_object_added();
302
Matthew Barth9c431062018-05-07 13:55:29 -0500303 // Save sensor object specifications
Kun Yi501ade22019-07-15 15:00:32 -0700304 _sensorObjects[sensorSetKey] = std::move(sensorObj);
Matthew Barth9c431062018-05-07 13:55:29 -0500305
Matthew Barthd238e232018-04-17 12:01:50 -0500306 return std::make_pair(std::move(std::get<sensorLabel>(properties)),
307 std::move(info));
Matthew Barth31d214c2018-03-26 09:54:27 -0500308}
309
Patrick Venture043d3232018-08-31 10:10:53 -0700310MainLoop::MainLoop(sdbusplus::bus::bus&& bus, const std::string& param,
311 const std::string& path, const std::string& devPath,
Patrick Venture16805a62019-06-21 14:19:33 -0700312 const char* prefix, const char* root,
Anton D. Kachalovd46d0812021-02-03 23:25:37 +0100313 const std::string& instanceId,
Patrick Venture16805a62019-06-21 14:19:33 -0700314 const hwmonio::HwmonIOInterface* ioIntf) :
Patrick Venture043d3232018-08-31 10:10:53 -0700315 _bus(std::move(bus)),
316 _manager(_bus, root), _pathParam(param), _hwmonRoot(), _instance(),
Patrick Venture16805a62019-06-21 14:19:33 -0700317 _devPath(devPath), _prefix(prefix), _root(root), _state(),
Anton D. Kachalovd46d0812021-02-03 23:25:37 +0100318 _instanceId(instanceId), _ioAccess(ioIntf),
319 _event(sdeventplus::Event::get_default()),
Patrick Venture52b40612018-12-19 13:36:41 -0800320 _timer(_event, std::bind(&MainLoop::read, this))
Brad Bishopd499ca62016-12-19 09:24:50 -0500321{
Patrick Venture73a50c72018-04-17 15:19:03 -0700322 // Strip off any trailing slashes.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500323 std::string p = path;
324 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500325 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500326 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500327 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500328
Patrick Venture73a50c72018-04-17 15:19:03 -0700329 // Given the furthest right /, set instance to
330 // the basename, and hwmonRoot to the leading path.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500331 auto n = p.rfind('/');
332 if (n != std::string::npos)
333 {
334 _instance.assign(p.substr(n + 1));
335 _hwmonRoot.assign(p.substr(0, n));
336 }
337
338 assert(!_instance.empty());
339 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500340}
341
342void MainLoop::shutdown() noexcept
343{
Patrick Venture52b40612018-12-19 13:36:41 -0800344 _event.exit(0);
Brad Bishopd499ca62016-12-19 09:24:50 -0500345}
346
347void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500348{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600349 init();
350
Patrick Venture043d3232018-08-31 10:10:53 -0700351 std::function<void()> callback(std::bind(&MainLoop::read, this));
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600352 try
353 {
Patrick Venture52b40612018-12-19 13:36:41 -0800354 _timer.restart(std::chrono::microseconds(_interval));
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600355
356 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
357
358 // TODO: Issue#7 - Should probably periodically check the SensorSet
359 // for new entries.
360
Patrick Venture52b40612018-12-19 13:36:41 -0800361 _bus.attach_event(_event.get(), SD_EVENT_PRIORITY_IMPORTANT);
362 _event.loop();
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600363 }
William A. Kennington III0fe4cb32018-10-18 19:19:58 -0700364 catch (const std::exception& e)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600365 {
366 log<level::ERR>("Error in sysfs polling loop",
367 entry("ERROR=%s", e.what()));
368 throw;
369 }
370}
371
372void MainLoop::init()
373{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500374 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500375 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500376
Patrick Venturedb7ecb62018-10-23 19:42:23 -0700377 for (const auto& i : *sensors)
Brad Bishop75b4ab82017-01-06 09:33:50 -0500378 {
Matthew Barthd238e232018-04-17 12:01:50 -0500379 auto object = getObject(i);
380 if (object)
381 {
382 // Construct the SensorSet value
383 // std::tuple<SensorSet::mapped_type,
384 // std::string(Sensor Label),
385 // ObjectInfo>
Patrick Venture043d3232018-08-31 10:10:53 -0700386 auto value =
387 std::make_tuple(std::move(i.second), std::move((*object).first),
388 std::move((*object).second));
Matthew Barthd238e232018-04-17 12:01:50 -0500389
Patrick Venture52b40612018-12-19 13:36:41 -0800390 _state[std::move(i.first)] = std::move(value);
Matthew Barthd238e232018-04-17 12:01:50 -0500391 }
Carol Wang9bbe6022019-08-01 17:31:30 +0800392
393 // Initialize _averageMap of sensor. e.g. <<power, 1>, <0, 0>>
394 if ((i.first.first == hwmon::type::power) &&
395 (phosphor::utility::isAverageEnvSet(i.first)))
396 {
397 _average.setAverageValue(i.first, std::make_pair(0, 0));
398 }
Brad Bishop75b4ab82017-01-06 09:33:50 -0500399 }
400
Patrick Venture62503a42017-05-23 07:30:29 -0700401 /* If there are no sensors specified by labels, exit. */
Patrick Venture52b40612018-12-19 13:36:41 -0800402 if (0 == _state.size())
Patrick Venture62503a42017-05-23 07:30:29 -0700403 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600404 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700405 }
406
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500407 {
Patrick Venturec897d8b2018-04-23 19:01:56 -0700408 std::stringstream ss;
Anton D. Kachalovd46d0812021-02-03 23:25:37 +0100409 std::string id = _instanceId;
410 if (id.empty())
411 {
412 id =
413 std::to_string(std::hash<std::string>{}(_devPath + _pathParam));
414 }
415 ss << _prefix << "-" << id << ".Hwmon1";
Patrick Venturec897d8b2018-04-23 19:01:56 -0700416
417 _bus.request_name(ss.str().c_str());
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500418 }
419
Patrick Ventureab10f162017-05-22 09:44:50 -0700420 {
Patrick Venturea24c8802018-04-17 19:38:06 -0700421 auto interval = env::getEnv("INTERVAL");
422 if (!interval.empty())
Patrick Ventureab10f162017-05-22 09:44:50 -0700423 {
Patrick Venture50cf1c52018-04-18 09:21:41 -0700424 _interval = std::strtoull(interval.c_str(), NULL, 10);
Patrick Ventureab10f162017-05-22 09:44:50 -0700425 }
426 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600427}
Patrick Ventureab10f162017-05-22 09:44:50 -0700428
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600429void MainLoop::read()
430{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500431 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
432 // ensure the objects all exist?
433
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600434 // Iterate through all the sensors.
Kun Yi501ade22019-07-15 15:00:32 -0700435 for (auto& [sensorSetKey, sensorStateTuple] : _state)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600436 {
Kun Yi501ade22019-07-15 15:00:32 -0700437 const auto& [sensorSysfsType, sensorSysfsNum] = sensorSetKey;
438 auto& [attrs, unused, objInfo] = sensorStateTuple;
439
Kun Yi553552c2019-07-15 22:14:21 -0700440 if (attrs.find(hwmon::entry::input) == attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500441 {
Kun Yi553552c2019-07-15 22:14:21 -0700442 continue;
443 }
Kun Yi501ade22019-07-15 15:00:32 -0700444
Kun Yi553552c2019-07-15 22:14:21 -0700445 // Read value from sensor.
Carol Wang9bbe6022019-08-01 17:31:30 +0800446 std::string input = hwmon::entry::input;
447 if (sensorSysfsType == hwmon::type::pwm)
Kun Yi553552c2019-07-15 22:14:21 -0700448 {
449 input = "";
450 }
Carol Wang9bbe6022019-08-01 17:31:30 +0800451 // If type is power and AVERAGE_power* is true in env, use average
452 // instead of input
453 else if ((sensorSysfsType == hwmon::type::power) &&
454 (phosphor::utility::isAverageEnvSet(sensorSetKey)))
455 {
456 input = hwmon::entry::average;
457 }
Kun Yi553552c2019-07-15 22:14:21 -0700458
Matt Spinlerecac0ae2020-07-08 13:09:08 -0500459 SensorValueType value;
Kun Yi553552c2019-07-15 22:14:21 -0700460 auto& obj = std::get<InterfaceMap>(objInfo);
Kun Yi501ade22019-07-15 15:00:32 -0700461 std::unique_ptr<sensor::Sensor>& sensor = _sensorObjects[sensorSetKey];
Kun Yi553552c2019-07-15 22:14:21 -0700462
463 auto& statusIface = std::any_cast<std::shared_ptr<StatusObject>&>(
464 obj[InterfaceType::STATUS]);
465 // As long as addStatus is called before addValue, statusIface
466 // should never be nullptr.
467 assert(statusIface);
468
469 try
470 {
471 if (sensor->hasFaultFile())
Patrick Venture043d3232018-08-31 10:10:53 -0700472 {
Kun Yi501ade22019-07-15 15:00:32 -0700473 auto fault = _ioAccess->read(sensorSysfsType, sensorSysfsNum,
Kun Yi553552c2019-07-15 22:14:21 -0700474 hwmon::entry::fault,
475 hwmonio::retries, hwmonio::delay);
476 // Skip reading from a sensor with a valid fault file
477 // and set the functional property accordingly
478 if (!statusIface->functional((fault == 0) ? true : false))
Matthew Barth27c4a392018-04-25 14:38:51 -0500479 {
Matthew Barthac473092018-05-07 14:41:46 -0500480 continue;
Matthew Barth8772ce32018-03-22 16:03:06 -0500481 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500482 }
Kun Yi553552c2019-07-15 22:14:21 -0700483
484 {
485 // RAII object for GPIO unlock / lock
William A. Kennington III2227bd52019-06-19 11:32:22 -0700486 auto locker = sensor::gpioUnlock(sensor->getGpio());
Kun Yi553552c2019-07-15 22:14:21 -0700487
Brandon Kim6d50c3e2019-08-09 15:38:53 -0700488 // For sensors with attribute ASYNC_READ_TIMEOUT,
489 // spawn a thread with timeout
490 auto asyncReadTimeout =
491 env::getEnv("ASYNC_READ_TIMEOUT", sensorSetKey);
492 if (!asyncReadTimeout.empty())
493 {
494 std::chrono::milliseconds asyncTimeout{
495 std::stoi(asyncReadTimeout)};
496 value = sensor::asyncRead(
497 sensorSetKey, _ioAccess, asyncTimeout, _timedoutMap,
498 sensorSysfsType, sensorSysfsNum, input,
499 hwmonio::retries, hwmonio::delay);
500 }
501 else
502 {
503 // Retry for up to a second if device is busy
504 // or has a transient error.
505 value =
506 _ioAccess->read(sensorSysfsType, sensorSysfsNum, input,
Kun Yi553552c2019-07-15 22:14:21 -0700507 hwmonio::retries, hwmonio::delay);
Brandon Kim6d50c3e2019-08-09 15:38:53 -0700508 }
509
Kun Yi553552c2019-07-15 22:14:21 -0700510 // Set functional property to true if we could read sensor
511 statusIface->functional(true);
512
513 value = sensor->adjustValue(value);
Carol Wang9bbe6022019-08-01 17:31:30 +0800514
515 if (input == hwmon::entry::average)
516 {
517 // Calculate the values of averageMap based on current
518 // average value, current average_interval value, previous
519 // average value, previous average_interval value
520 int64_t interval =
521 _ioAccess->read(sensorSysfsType, sensorSysfsNum,
522 hwmon::entry::caverage_interval,
523 hwmonio::retries, hwmonio::delay);
524 auto ret = _average.getAverageValue(sensorSetKey);
525 assert(ret);
526
527 const auto& [preAverage, preInterval] = *ret;
528
529 auto calValue = Average::calcAverage(
530 preAverage, preInterval, value, interval);
531 if (calValue)
532 {
533 // Update previous values in averageMap before the
534 // variable value is changed next
535 _average.setAverageValue(
536 sensorSetKey, std::make_pair(value, interval));
537 // Update value to be calculated average
538 value = calValue.value();
539 }
540 else
541 {
542 // the value of
543 // power*_average_interval is not changed yet, use the
544 // previous calculated average instead. So skip dbus
545 // update.
546 continue;
547 }
548 }
Kun Yi553552c2019-07-15 22:14:21 -0700549 }
550
551 updateSensorInterfaces(obj, value);
552 }
553 catch (const std::system_error& e)
554 {
Matt Spinlerd8cacfd2021-04-26 09:56:21 -0500555#if UPDATE_FUNCTIONAL_ON_FAIL
Kun Yi553552c2019-07-15 22:14:21 -0700556 // If UPDATE_FUNCTIONAL_ON_FAIL is defined and an exception was
557 // thrown, set the functional property to false.
558 // We cannot set this with the 'continue' in the lower block
559 // as the code may exit before reaching it.
560 statusIface->functional(false);
561#endif
Carol Wang9bbe6022019-08-01 17:31:30 +0800562 auto file = sysfs::make_sysfs_path(
563 _ioAccess->path(), sensorSysfsType, sensorSysfsNum, input);
Kun Yi553552c2019-07-15 22:14:21 -0700564
565 // Check sensorAdjusts for sensor removal RCs
Kun Yi501ade22019-07-15 15:00:32 -0700566 auto& sAdjusts = _sensorObjects[sensorSetKey]->getAdjusts();
Kun Yi553552c2019-07-15 22:14:21 -0700567 if (sAdjusts.rmRCs.count(e.code().value()) > 0)
568 {
569 // Return code found in sensor return code removal list
Kun Yi501ade22019-07-15 15:00:32 -0700570 if (_rmSensors.find(sensorSetKey) == _rmSensors.end())
Kun Yi553552c2019-07-15 22:14:21 -0700571 {
572 // Trace for sensor not already removed from dbus
573 log<level::INFO>("Remove sensor from dbus for read fail",
574 entry("FILE=%s", file.c_str()),
575 entry("RC=%d", e.code().value()));
576 // Mark this sensor to be removed from dbus
Kun Yi501ade22019-07-15 15:00:32 -0700577 _rmSensors[sensorSetKey] = attrs;
Kun Yi553552c2019-07-15 22:14:21 -0700578 }
579 continue;
580 }
Matt Spinlerd8cacfd2021-04-26 09:56:21 -0500581#if UPDATE_FUNCTIONAL_ON_FAIL
Kun Yi553552c2019-07-15 22:14:21 -0700582 // Do not exit with failure if UPDATE_FUNCTIONAL_ON_FAIL is set
583 continue;
584#endif
585 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::
586 Error;
587 report<ReadFailure>(
588 xyz::openbmc_project::Sensor::Device::ReadFailure::
589 CALLOUT_ERRNO(e.code().value()),
590 xyz::openbmc_project::Sensor::Device::ReadFailure::
591 CALLOUT_DEVICE_PATH(_devPath.c_str()));
592
Matt Spinler6a391de2020-07-08 13:03:10 -0500593 log<level::INFO>(fmt::format("Failing sysfs file: {} errno: {}",
594 file, e.code().value())
595 .c_str());
Kun Yi553552c2019-07-15 22:14:21 -0700596
597 exit(EXIT_FAILURE);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500598 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600599 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500600
Patrick Venture0cd4f692019-06-21 13:39:40 -0700601 removeSensors();
602
Patrick Venture0cd4f692019-06-21 13:39:40 -0700603 addDroppedSensors();
Patrick Venture0cd4f692019-06-21 13:39:40 -0700604}
605
606void MainLoop::removeSensors()
607{
Matthew Barth8772ce32018-03-22 16:03:06 -0500608 // Remove any sensors marked for removal
Patrick Venture52b40612018-12-19 13:36:41 -0800609 for (const auto& i : _rmSensors)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600610 {
Matthew Barthd0ce7922019-06-06 09:23:37 -0500611 // Remove sensor object from dbus using emit_object_removed()
612 auto& objInfo = std::get<ObjectInfo>(_state[i.first]);
613 auto& objPath = std::get<std::string>(objInfo);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700614
Matthew Barthd0ce7922019-06-06 09:23:37 -0500615 _bus.emit_object_removed(objPath.c_str());
Patrick Venture0cd4f692019-06-21 13:39:40 -0700616
Matthew Barthd0ce7922019-06-06 09:23:37 -0500617 // Erase sensor object info
Patrick Venture52b40612018-12-19 13:36:41 -0800618 _state.erase(i.first);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500619 }
Patrick Venture0cd4f692019-06-21 13:39:40 -0700620}
Matthew Barth31d214c2018-03-26 09:54:27 -0500621
Patrick Venture0cd4f692019-06-21 13:39:40 -0700622void MainLoop::addDroppedSensors()
623{
Matthew Barth31d214c2018-03-26 09:54:27 -0500624 // Attempt to add any sensors that were removed
Patrick Venture52b40612018-12-19 13:36:41 -0800625 auto it = _rmSensors.begin();
626 while (it != _rmSensors.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500627 {
Patrick Venture52b40612018-12-19 13:36:41 -0800628 if (_state.find(it->first) == _state.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500629 {
630 SensorSet::container_t::value_type ssValueType =
Patrick Venture043d3232018-08-31 10:10:53 -0700631 std::make_pair(it->first, it->second);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700632
Matthew Barthd238e232018-04-17 12:01:50 -0500633 auto object = getObject(ssValueType);
634 if (object)
Matthew Barth31d214c2018-03-26 09:54:27 -0500635 {
Matthew Barthd238e232018-04-17 12:01:50 -0500636 // Construct the SensorSet value
637 // std::tuple<SensorSet::mapped_type,
638 // std::string(Sensor Label),
639 // ObjectInfo>
640 auto value = std::make_tuple(std::move(ssValueType.second),
641 std::move((*object).first),
642 std::move((*object).second));
643
Patrick Venture52b40612018-12-19 13:36:41 -0800644 _state[std::move(ssValueType.first)] = std::move(value);
Matthew Barthd238e232018-04-17 12:01:50 -0500645
Carol Wang9bbe6022019-08-01 17:31:30 +0800646 std::string input = hwmon::entry::input;
647 // If type is power and AVERAGE_power* is true in env, use
648 // average instead of input
649 if ((it->first.first == hwmon::type::power) &&
650 (phosphor::utility::isAverageEnvSet(it->first)))
651 {
652 input = hwmon::entry::average;
653 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500654 // Sensor object added, erase entry from removal list
Carol Wang9bbe6022019-08-01 17:31:30 +0800655 auto file =
656 sysfs::make_sysfs_path(_ioAccess->path(), it->first.first,
657 it->first.second, input);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700658
Patrick Venture043d3232018-08-31 10:10:53 -0700659 log<level::INFO>("Added sensor to dbus after successful read",
660 entry("FILE=%s", file.c_str()));
Patrick Venture0cd4f692019-06-21 13:39:40 -0700661
Patrick Venture52b40612018-12-19 13:36:41 -0800662 it = _rmSensors.erase(it);
Matthew Barth31d214c2018-03-26 09:54:27 -0500663 }
664 else
665 {
666 ++it;
667 }
668 }
669 else
670 {
671 // Sanity check to remove sensors that were re-added
Patrick Venture52b40612018-12-19 13:36:41 -0800672 it = _rmSensors.erase(it);
Matthew Barth31d214c2018-03-26 09:54:27 -0500673 }
674 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500675}
676
677// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4