blob: 6ab8e9cc680ca20e5f71fa3d0cc30e6c0278daa6 [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 Williams3a816142021-10-06 15:36:42 -050057decltype(Thresholds<WarningObject>::alarmLo)
58 Thresholds<WarningObject>::alarmLo = &WarningObject::warningAlarmLow;
59decltype(Thresholds<WarningObject>::alarmHi)
60 Thresholds<WarningObject>::alarmHi = &WarningObject::warningAlarmHigh;
Duke Du73769092021-04-14 15:35:21 +080061decltype(Thresholds<WarningObject>::getAlarmLow)
62 Thresholds<WarningObject>::getAlarmLow = &WarningObject::warningAlarmLow;
63decltype(Thresholds<WarningObject>::getAlarmHigh)
64 Thresholds<WarningObject>::getAlarmHigh = &WarningObject::warningAlarmHigh;
65decltype(Thresholds<WarningObject>::assertLowSignal)
66 Thresholds<WarningObject>::assertLowSignal =
67 &WarningObject::warningLowAlarmAsserted;
68decltype(Thresholds<WarningObject>::assertHighSignal)
69 Thresholds<WarningObject>::assertHighSignal =
70 &WarningObject::warningHighAlarmAsserted;
71decltype(Thresholds<WarningObject>::deassertLowSignal)
72 Thresholds<WarningObject>::deassertLowSignal =
73 &WarningObject::warningLowAlarmDeasserted;
74decltype(Thresholds<WarningObject>::deassertHighSignal)
75 Thresholds<WarningObject>::deassertHighSignal =
76 &WarningObject::warningHighAlarmDeasserted;
Saqib Khan973886d2017-03-15 14:01:16 -050077
78// Initialization for Critical Objects
79decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo =
80 &CriticalObject::criticalLow;
81decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi =
82 &CriticalObject::criticalHigh;
83decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo =
84 &CriticalObject::criticalLow;
85decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi =
86 &CriticalObject::criticalHigh;
Patrick Williams3a816142021-10-06 15:36:42 -050087decltype(Thresholds<CriticalObject>::alarmLo)
88 Thresholds<CriticalObject>::alarmLo = &CriticalObject::criticalAlarmLow;
89decltype(Thresholds<CriticalObject>::alarmHi)
90 Thresholds<CriticalObject>::alarmHi = &CriticalObject::criticalAlarmHigh;
Duke Du73769092021-04-14 15:35:21 +080091decltype(Thresholds<CriticalObject>::getAlarmLow)
92 Thresholds<CriticalObject>::getAlarmLow = &CriticalObject::criticalAlarmLow;
93decltype(Thresholds<CriticalObject>::getAlarmHigh)
94 Thresholds<CriticalObject>::getAlarmHigh =
95 &CriticalObject::criticalAlarmHigh;
96decltype(Thresholds<CriticalObject>::assertLowSignal)
97 Thresholds<CriticalObject>::assertLowSignal =
98 &CriticalObject::criticalLowAlarmAsserted;
99decltype(Thresholds<CriticalObject>::assertHighSignal)
100 Thresholds<CriticalObject>::assertHighSignal =
101 &CriticalObject::criticalHighAlarmAsserted;
102decltype(Thresholds<CriticalObject>::deassertLowSignal)
103 Thresholds<CriticalObject>::deassertLowSignal =
104 &CriticalObject::criticalLowAlarmDeasserted;
105decltype(Thresholds<CriticalObject>::deassertHighSignal)
106 Thresholds<CriticalObject>::deassertHighSignal =
107 &CriticalObject::criticalHighAlarmDeasserted;
Saqib Khan973886d2017-03-15 14:01:16 -0500108
Matt Spinlerecac0ae2020-07-08 13:09:08 -0500109void updateSensorInterfaces(InterfaceMap& ifaces, SensorValueType value)
Patrick Venturefeb744a2019-06-26 19:07:48 -0700110{
111 for (auto& iface : ifaces)
112 {
113 switch (iface.first)
114 {
Kun Yi94a04c42019-08-21 09:43:20 -0700115 // clang-format off
Patrick Venturefeb744a2019-06-26 19:07:48 -0700116 case InterfaceType::VALUE:
117 {
118 auto& valueIface =
119 std::any_cast<std::shared_ptr<ValueObject>&>(iface.second);
120 valueIface->value(value);
121 }
122 break;
Kun Yi94a04c42019-08-21 09:43:20 -0700123 // clang-format on
Patrick Venturefeb744a2019-06-26 19:07:48 -0700124 case InterfaceType::WARN:
125 checkThresholds<WarningObject>(iface.second, value);
126 break;
127 case InterfaceType::CRIT:
128 checkThresholds<CriticalObject>(iface.second, value);
129 break;
130 default:
131 break;
132 }
133 }
134}
135
Matthew Barth979c8062018-04-17 11:37:15 -0500136std::string MainLoop::getID(SensorSet::container_t::const_reference sensor)
Matthew Barth31d214c2018-03-26 09:54:27 -0500137{
Matthew Barth31d214c2018-03-26 09:54:27 -0500138 std::string id;
139
140 /*
141 * Check if the value of the MODE_<item><X> env variable for the sensor
Matt Spinler7c424802018-05-04 10:52:40 -0500142 * is set. If it is, then read the from the <item><X>_<mode>
Matthew Barth31d214c2018-03-26 09:54:27 -0500143 * file. The name of the DBUS object would be the value of the env
Matt Spinler7c424802018-05-04 10:52:40 -0500144 * variable LABEL_<item><mode value>. If the MODE_<item><X> env variable
Matthew Barth31d214c2018-03-26 09:54:27 -0500145 * doesn't exist, then the name of DBUS object is the value of the env
146 * variable LABEL_<item><X>.
Matt Spinler7c424802018-05-04 10:52:40 -0500147 *
148 * For example, if MODE_temp1 = "label", then code reads the temp1_label
149 * file. If it has a 5 in it, then it will use the following entry to
150 * name the object: LABEL_temp5 = "My DBus object name".
151 *
Matthew Barth31d214c2018-03-26 09:54:27 -0500152 */
Patrick Venture7a5285d2018-04-17 19:15:05 -0700153 auto mode = env::getEnv("MODE", sensor.first);
Matt Spinler7c424802018-05-04 10:52:40 -0500154 if (!mode.empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500155 {
Patrick Venture043d3232018-08-31 10:10:53 -0700156 id = env::getIndirectID(_hwmonRoot + '/' + _instance + '/', mode,
157 sensor.first);
Matthew Barth31d214c2018-03-26 09:54:27 -0500158
159 if (id.empty())
160 {
Matthew Barth979c8062018-04-17 11:37:15 -0500161 return id;
Matthew Barth31d214c2018-03-26 09:54:27 -0500162 }
163 }
164
165 // Use the ID we looked up above if there was one,
166 // otherwise use the standard one.
167 id = (id.empty()) ? sensor.first.second : id;
168
Matthew Barth979c8062018-04-17 11:37:15 -0500169 return id;
170}
171
Patrick Venture043d3232018-08-31 10:10:53 -0700172SensorIdentifiers
173 MainLoop::getIdentifiers(SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500174{
175 std::string id = getID(sensor);
176 std::string label;
177
178 if (!id.empty())
179 {
180 // Ignore inputs without a label.
181 label = env::getEnv("LABEL", sensor.first.first, id);
182 }
183
Patrick Venture043d3232018-08-31 10:10:53 -0700184 return std::make_tuple(std::move(id), std::move(label));
Matthew Barth979c8062018-04-17 11:37:15 -0500185}
186
187/**
188 * Reads the environment parameters of a sensor and creates an object with
189 * atleast the `Value` interface, otherwise returns without creating the object.
190 * If the `Value` interface is successfully created, by reading the sensor's
191 * corresponding sysfs file's value, the additional interfaces for the sensor
Matthew Barthd238e232018-04-17 12:01:50 -0500192 * are created and the InterfacesAdded signal is emitted. The object's state
193 * data is then returned for sensor state monitoring within the main loop.
Matthew Barth979c8062018-04-17 11:37:15 -0500194 */
William A. Kennington III4cbdfef2018-10-18 19:19:51 -0700195std::optional<ObjectStateData>
Patrick Venture043d3232018-08-31 10:10:53 -0700196 MainLoop::getObject(SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500197{
198 auto properties = getIdentifiers(sensor);
199 if (std::get<sensorID>(properties).empty() ||
200 std::get<sensorLabel>(properties).empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500201 {
Matthew Barthd238e232018-04-17 12:01:50 -0500202 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500203 }
204
Patrick Venture09791852018-04-17 17:40:00 -0700205 hwmon::Attributes attrs;
206 if (!hwmon::getAttributes(sensor.first.first, attrs))
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
Kun Yi501ade22019-07-15 15:00:32 -0700211 const auto& [sensorSetKey, sensorAttrs] = sensor;
212 const auto& [sensorSysfsType, sensorSysfsNum] = sensorSetKey;
213
Patrick Venture2864b062018-12-19 08:13:41 -0800214 /* Note: The sensor objects all share the same ioAccess object. */
Patrick Venture043d3232018-08-31 10:10:53 -0700215 auto sensorObj =
Kun Yi501ade22019-07-15 15:00:32 -0700216 std::make_unique<sensor::Sensor>(sensorSetKey, _ioAccess, _devPath);
Matthew Barth9c431062018-05-07 13:55:29 -0500217
Matthew Barthb7985272018-04-17 10:50:36 -0500218 // Get list of return codes for removing sensors on device
219 auto devRmRCs = env::getEnv("REMOVERCS");
220 // Add sensor removal return codes defined at the device level
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500221 sensorObj->addRemoveRCs(devRmRCs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500222
223 std::string objectPath{_root};
224 objectPath.append(1, '/');
Patrick Venture09791852018-04-17 17:40:00 -0700225 objectPath.append(hwmon::getNamespace(attrs));
Matthew Barth31d214c2018-03-26 09:54:27 -0500226 objectPath.append(1, '/');
Matthew Barth979c8062018-04-17 11:37:15 -0500227 objectPath.append(std::get<sensorLabel>(properties));
Matthew Barth31d214c2018-03-26 09:54:27 -0500228
Patrick Venture62067232019-06-19 17:39:33 -0700229 ObjectInfo info(&_bus, std::move(objectPath), InterfaceMap());
Patrick Venture75e56c62018-04-20 18:10:15 -0700230 RetryIO retryIO(hwmonio::retries, hwmonio::delay);
Kun Yi501ade22019-07-15 15:00:32 -0700231 if (_rmSensors.find(sensorSetKey) != _rmSensors.end())
Matthew Barthd4beecf2018-04-03 15:50:22 -0500232 {
233 // When adding a sensor that was purposely removed,
234 // don't retry on errors when reading its value
235 std::get<size_t>(retryIO) = 0;
236 }
Patrick Venture043d3232018-08-31 10:10:53 -0700237 auto valueInterface = static_cast<std::shared_ptr<ValueObject>>(nullptr);
Matthew Barth31d214c2018-03-26 09:54:27 -0500238 try
239 {
Matthew Barthca44c2e2018-04-24 15:33:25 -0500240 // Add status interface based on _fault file being present
Matthew Barth2e41b132018-05-07 14:15:45 -0500241 sensorObj->addStatus(info);
Brandon Kim6d50c3e2019-08-09 15:38:53 -0700242 valueInterface = sensorObj->addValue(retryIO, info, _timedoutMap);
Matthew Barth31d214c2018-03-26 09:54:27 -0500243 }
244 catch (const std::system_error& e)
245 {
Patrick Venture043d3232018-08-31 10:10:53 -0700246 auto file =
Kun Yi501ade22019-07-15 15:00:32 -0700247 sysfs::make_sysfs_path(_ioAccess->path(), sensorSysfsType,
248 sensorSysfsNum, hwmon::entry::cinput);
Patrick Venture0892c3f2019-06-27 14:24:03 -0700249
Matthew Barth31d214c2018-03-26 09:54:27 -0500250 // Check sensorAdjusts for sensor removal RCs
Matthew Barthac473092018-05-07 14:41:46 -0500251 auto& sAdjusts = sensorObj->getAdjusts();
252 if (sAdjusts.rmRCs.count(e.code().value()) > 0)
Matthew Barth31d214c2018-03-26 09:54:27 -0500253 {
Matthew Barthac473092018-05-07 14:41:46 -0500254 // Return code found in sensor return code removal list
Kun Yi501ade22019-07-15 15:00:32 -0700255 if (_rmSensors.find(sensorSetKey) == _rmSensors.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500256 {
Matthew Barthac473092018-05-07 14:41:46 -0500257 // Trace for sensor not already removed from dbus
258 log<level::INFO>("Sensor not added to dbus for read fail",
Patrick Venture043d3232018-08-31 10:10:53 -0700259 entry("FILE=%s", file.c_str()),
260 entry("RC=%d", e.code().value()));
Kun Yi501ade22019-07-15 15:00:32 -0700261 _rmSensors[std::move(sensorSetKey)] = std::move(sensorAttrs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500262 }
Matthew Barthac473092018-05-07 14:41:46 -0500263 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500264 }
Patrick Venture0892c3f2019-06-27 14:24:03 -0700265
Patrick Venture043d3232018-08-31 10:10:53 -0700266 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error;
Matthew Barth31d214c2018-03-26 09:54:27 -0500267 report<ReadFailure>(
Patrick Venture043d3232018-08-31 10:10:53 -0700268 xyz::openbmc_project::Sensor::Device::ReadFailure::CALLOUT_ERRNO(
269 e.code().value()),
270 xyz::openbmc_project::Sensor::Device::ReadFailure::
271 CALLOUT_DEVICE_PATH(_devPath.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500272
Matt Spinler6a391de2020-07-08 13:03:10 -0500273 log<level::INFO>(fmt::format("Failing sysfs file: {} errno: {}", file,
274 e.code().value())
275 .c_str());
Matthew Barth31d214c2018-03-26 09:54:27 -0500276 exit(EXIT_FAILURE);
Matthew Barth31d214c2018-03-26 09:54:27 -0500277 }
278 auto sensorValue = valueInterface->value();
Matt Spinlerecac0ae2020-07-08 13:09:08 -0500279 int64_t scale = sensorObj->getScale();
280
Kun Yi501ade22019-07-15 15:00:32 -0700281 addThreshold<WarningObject>(sensorSysfsType, std::get<sensorID>(properties),
282 sensorValue, info, scale);
283 addThreshold<CriticalObject>(sensorSysfsType,
James Feistee73f5b2018-08-01 16:31:42 -0700284 std::get<sensorID>(properties), sensorValue,
285 info, scale);
Matthew Barth31d214c2018-03-26 09:54:27 -0500286
Patrick Venture043d3232018-08-31 10:10:53 -0700287 auto target =
Kun Yi501ade22019-07-15 15:00:32 -0700288 addTarget<hwmon::FanSpeed>(sensorSetKey, _ioAccess, _devPath, info);
Matthew Barth28f8e662018-03-26 16:57:36 -0500289 if (target)
Matthew Barth31d214c2018-03-26 09:54:27 -0500290 {
Matthew Barth28f8e662018-03-26 16:57:36 -0500291 target->enable();
Matthew Barth31d214c2018-03-26 09:54:27 -0500292 }
Kun Yi501ade22019-07-15 15:00:32 -0700293 addTarget<hwmon::FanPwm>(sensorSetKey, _ioAccess, _devPath, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500294
295 // All the interfaces have been created. Go ahead
296 // and emit InterfacesAdded.
297 valueInterface->emit_object_added();
298
Matthew Barth9c431062018-05-07 13:55:29 -0500299 // Save sensor object specifications
Kun Yi501ade22019-07-15 15:00:32 -0700300 _sensorObjects[sensorSetKey] = std::move(sensorObj);
Matthew Barth9c431062018-05-07 13:55:29 -0500301
Matthew Barthd238e232018-04-17 12:01:50 -0500302 return std::make_pair(std::move(std::get<sensorLabel>(properties)),
303 std::move(info));
Matthew Barth31d214c2018-03-26 09:54:27 -0500304}
305
Patrick Venture043d3232018-08-31 10:10:53 -0700306MainLoop::MainLoop(sdbusplus::bus::bus&& bus, const std::string& param,
307 const std::string& path, const std::string& devPath,
Patrick Venture16805a62019-06-21 14:19:33 -0700308 const char* prefix, const char* root,
Anton D. Kachalovd46d0812021-02-03 23:25:37 +0100309 const std::string& instanceId,
Patrick Venture16805a62019-06-21 14:19:33 -0700310 const hwmonio::HwmonIOInterface* ioIntf) :
Patrick Venture043d3232018-08-31 10:10:53 -0700311 _bus(std::move(bus)),
312 _manager(_bus, root), _pathParam(param), _hwmonRoot(), _instance(),
Patrick Venture16805a62019-06-21 14:19:33 -0700313 _devPath(devPath), _prefix(prefix), _root(root), _state(),
Anton D. Kachalovd46d0812021-02-03 23:25:37 +0100314 _instanceId(instanceId), _ioAccess(ioIntf),
315 _event(sdeventplus::Event::get_default()),
Patrick Venture52b40612018-12-19 13:36:41 -0800316 _timer(_event, std::bind(&MainLoop::read, this))
Brad Bishopd499ca62016-12-19 09:24:50 -0500317{
Patrick Venture73a50c72018-04-17 15:19:03 -0700318 // Strip off any trailing slashes.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500319 std::string p = path;
320 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500321 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500322 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500323 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500324
Patrick Venture73a50c72018-04-17 15:19:03 -0700325 // Given the furthest right /, set instance to
326 // the basename, and hwmonRoot to the leading path.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500327 auto n = p.rfind('/');
328 if (n != std::string::npos)
329 {
330 _instance.assign(p.substr(n + 1));
331 _hwmonRoot.assign(p.substr(0, n));
332 }
333
334 assert(!_instance.empty());
335 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500336}
337
338void MainLoop::shutdown() noexcept
339{
Patrick Venture52b40612018-12-19 13:36:41 -0800340 _event.exit(0);
Brad Bishopd499ca62016-12-19 09:24:50 -0500341}
342
343void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500344{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600345 init();
346
Patrick Venture043d3232018-08-31 10:10:53 -0700347 std::function<void()> callback(std::bind(&MainLoop::read, this));
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600348 try
349 {
Patrick Venture52b40612018-12-19 13:36:41 -0800350 _timer.restart(std::chrono::microseconds(_interval));
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600351
352 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
353
354 // TODO: Issue#7 - Should probably periodically check the SensorSet
355 // for new entries.
356
Patrick Venture52b40612018-12-19 13:36:41 -0800357 _bus.attach_event(_event.get(), SD_EVENT_PRIORITY_IMPORTANT);
358 _event.loop();
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600359 }
William A. Kennington III0fe4cb32018-10-18 19:19:58 -0700360 catch (const std::exception& e)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600361 {
362 log<level::ERR>("Error in sysfs polling loop",
363 entry("ERROR=%s", e.what()));
364 throw;
365 }
366}
367
368void MainLoop::init()
369{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500370 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500371 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500372
Patrick Venturedb7ecb62018-10-23 19:42:23 -0700373 for (const auto& i : *sensors)
Brad Bishop75b4ab82017-01-06 09:33:50 -0500374 {
Matthew Barthd238e232018-04-17 12:01:50 -0500375 auto object = getObject(i);
376 if (object)
377 {
378 // Construct the SensorSet value
379 // std::tuple<SensorSet::mapped_type,
380 // std::string(Sensor Label),
381 // ObjectInfo>
Patrick Venture043d3232018-08-31 10:10:53 -0700382 auto value =
383 std::make_tuple(std::move(i.second), std::move((*object).first),
384 std::move((*object).second));
Matthew Barthd238e232018-04-17 12:01:50 -0500385
Patrick Venture52b40612018-12-19 13:36:41 -0800386 _state[std::move(i.first)] = std::move(value);
Matthew Barthd238e232018-04-17 12:01:50 -0500387 }
Carol Wang9bbe6022019-08-01 17:31:30 +0800388
389 // Initialize _averageMap of sensor. e.g. <<power, 1>, <0, 0>>
390 if ((i.first.first == hwmon::type::power) &&
391 (phosphor::utility::isAverageEnvSet(i.first)))
392 {
393 _average.setAverageValue(i.first, std::make_pair(0, 0));
394 }
Brad Bishop75b4ab82017-01-06 09:33:50 -0500395 }
396
Patrick Venture62503a42017-05-23 07:30:29 -0700397 /* If there are no sensors specified by labels, exit. */
Patrick Venture52b40612018-12-19 13:36:41 -0800398 if (0 == _state.size())
Patrick Venture62503a42017-05-23 07:30:29 -0700399 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600400 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700401 }
402
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500403 {
Patrick Venturec897d8b2018-04-23 19:01:56 -0700404 std::stringstream ss;
Anton D. Kachalovd46d0812021-02-03 23:25:37 +0100405 std::string id = _instanceId;
406 if (id.empty())
407 {
408 id =
409 std::to_string(std::hash<std::string>{}(_devPath + _pathParam));
410 }
411 ss << _prefix << "-" << id << ".Hwmon1";
Patrick Venturec897d8b2018-04-23 19:01:56 -0700412
413 _bus.request_name(ss.str().c_str());
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500414 }
415
Patrick Ventureab10f162017-05-22 09:44:50 -0700416 {
Patrick Venturea24c8802018-04-17 19:38:06 -0700417 auto interval = env::getEnv("INTERVAL");
418 if (!interval.empty())
Patrick Ventureab10f162017-05-22 09:44:50 -0700419 {
Patrick Venture50cf1c52018-04-18 09:21:41 -0700420 _interval = std::strtoull(interval.c_str(), NULL, 10);
Patrick Ventureab10f162017-05-22 09:44:50 -0700421 }
422 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600423}
Patrick Ventureab10f162017-05-22 09:44:50 -0700424
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600425void MainLoop::read()
426{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500427 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
428 // ensure the objects all exist?
429
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600430 // Iterate through all the sensors.
Kun Yi501ade22019-07-15 15:00:32 -0700431 for (auto& [sensorSetKey, sensorStateTuple] : _state)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600432 {
Kun Yi501ade22019-07-15 15:00:32 -0700433 const auto& [sensorSysfsType, sensorSysfsNum] = sensorSetKey;
434 auto& [attrs, unused, objInfo] = sensorStateTuple;
435
Kun Yi553552c2019-07-15 22:14:21 -0700436 if (attrs.find(hwmon::entry::input) == attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500437 {
Kun Yi553552c2019-07-15 22:14:21 -0700438 continue;
439 }
Kun Yi501ade22019-07-15 15:00:32 -0700440
Kun Yi553552c2019-07-15 22:14:21 -0700441 // Read value from sensor.
Carol Wang9bbe6022019-08-01 17:31:30 +0800442 std::string input = hwmon::entry::input;
443 if (sensorSysfsType == hwmon::type::pwm)
Kun Yi553552c2019-07-15 22:14:21 -0700444 {
445 input = "";
446 }
Carol Wang9bbe6022019-08-01 17:31:30 +0800447 // If type is power and AVERAGE_power* is true in env, use average
448 // instead of input
449 else if ((sensorSysfsType == hwmon::type::power) &&
450 (phosphor::utility::isAverageEnvSet(sensorSetKey)))
451 {
452 input = hwmon::entry::average;
453 }
Kun Yi553552c2019-07-15 22:14:21 -0700454
Matt Spinlerecac0ae2020-07-08 13:09:08 -0500455 SensorValueType value;
Kun Yi553552c2019-07-15 22:14:21 -0700456 auto& obj = std::get<InterfaceMap>(objInfo);
Kun Yi501ade22019-07-15 15:00:32 -0700457 std::unique_ptr<sensor::Sensor>& sensor = _sensorObjects[sensorSetKey];
Kun Yi553552c2019-07-15 22:14:21 -0700458
459 auto& statusIface = std::any_cast<std::shared_ptr<StatusObject>&>(
460 obj[InterfaceType::STATUS]);
461 // As long as addStatus is called before addValue, statusIface
462 // should never be nullptr.
463 assert(statusIface);
464
465 try
466 {
467 if (sensor->hasFaultFile())
Patrick Venture043d3232018-08-31 10:10:53 -0700468 {
Kun Yi501ade22019-07-15 15:00:32 -0700469 auto fault = _ioAccess->read(sensorSysfsType, sensorSysfsNum,
Kun Yi553552c2019-07-15 22:14:21 -0700470 hwmon::entry::fault,
471 hwmonio::retries, hwmonio::delay);
472 // Skip reading from a sensor with a valid fault file
473 // and set the functional property accordingly
474 if (!statusIface->functional((fault == 0) ? true : false))
Matthew Barth27c4a392018-04-25 14:38:51 -0500475 {
Matthew Barthac473092018-05-07 14:41:46 -0500476 continue;
Matthew Barth8772ce32018-03-22 16:03:06 -0500477 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500478 }
Kun Yi553552c2019-07-15 22:14:21 -0700479
480 {
481 // RAII object for GPIO unlock / lock
William A. Kennington III2227bd52019-06-19 11:32:22 -0700482 auto locker = sensor::gpioUnlock(sensor->getGpio());
Kun Yi553552c2019-07-15 22:14:21 -0700483
Brandon Kim6d50c3e2019-08-09 15:38:53 -0700484 // For sensors with attribute ASYNC_READ_TIMEOUT,
485 // spawn a thread with timeout
486 auto asyncReadTimeout =
487 env::getEnv("ASYNC_READ_TIMEOUT", sensorSetKey);
488 if (!asyncReadTimeout.empty())
489 {
490 std::chrono::milliseconds asyncTimeout{
491 std::stoi(asyncReadTimeout)};
492 value = sensor::asyncRead(
493 sensorSetKey, _ioAccess, asyncTimeout, _timedoutMap,
494 sensorSysfsType, sensorSysfsNum, input,
495 hwmonio::retries, hwmonio::delay);
496 }
497 else
498 {
499 // Retry for up to a second if device is busy
500 // or has a transient error.
501 value =
502 _ioAccess->read(sensorSysfsType, sensorSysfsNum, input,
Kun Yi553552c2019-07-15 22:14:21 -0700503 hwmonio::retries, hwmonio::delay);
Brandon Kim6d50c3e2019-08-09 15:38:53 -0700504 }
505
Kun Yi553552c2019-07-15 22:14:21 -0700506 // Set functional property to true if we could read sensor
507 statusIface->functional(true);
508
509 value = sensor->adjustValue(value);
Carol Wang9bbe6022019-08-01 17:31:30 +0800510
511 if (input == hwmon::entry::average)
512 {
513 // Calculate the values of averageMap based on current
514 // average value, current average_interval value, previous
515 // average value, previous average_interval value
516 int64_t interval =
517 _ioAccess->read(sensorSysfsType, sensorSysfsNum,
518 hwmon::entry::caverage_interval,
519 hwmonio::retries, hwmonio::delay);
520 auto ret = _average.getAverageValue(sensorSetKey);
521 assert(ret);
522
523 const auto& [preAverage, preInterval] = *ret;
524
525 auto calValue = Average::calcAverage(
526 preAverage, preInterval, value, interval);
527 if (calValue)
528 {
529 // Update previous values in averageMap before the
530 // variable value is changed next
531 _average.setAverageValue(
532 sensorSetKey, std::make_pair(value, interval));
533 // Update value to be calculated average
534 value = calValue.value();
535 }
536 else
537 {
538 // the value of
539 // power*_average_interval is not changed yet, use the
540 // previous calculated average instead. So skip dbus
541 // update.
542 continue;
543 }
544 }
Kun Yi553552c2019-07-15 22:14:21 -0700545 }
546
547 updateSensorInterfaces(obj, value);
548 }
549 catch (const std::system_error& e)
550 {
Matt Spinlerd8cacfd2021-04-26 09:56:21 -0500551#if UPDATE_FUNCTIONAL_ON_FAIL
Kun Yi553552c2019-07-15 22:14:21 -0700552 // If UPDATE_FUNCTIONAL_ON_FAIL is defined and an exception was
553 // thrown, set the functional property to false.
554 // We cannot set this with the 'continue' in the lower block
555 // as the code may exit before reaching it.
556 statusIface->functional(false);
557#endif
Carol Wang9bbe6022019-08-01 17:31:30 +0800558 auto file = sysfs::make_sysfs_path(
559 _ioAccess->path(), sensorSysfsType, sensorSysfsNum, input);
Kun Yi553552c2019-07-15 22:14:21 -0700560
561 // Check sensorAdjusts for sensor removal RCs
Kun Yi501ade22019-07-15 15:00:32 -0700562 auto& sAdjusts = _sensorObjects[sensorSetKey]->getAdjusts();
Kun Yi553552c2019-07-15 22:14:21 -0700563 if (sAdjusts.rmRCs.count(e.code().value()) > 0)
564 {
565 // Return code found in sensor return code removal list
Kun Yi501ade22019-07-15 15:00:32 -0700566 if (_rmSensors.find(sensorSetKey) == _rmSensors.end())
Kun Yi553552c2019-07-15 22:14:21 -0700567 {
568 // Trace for sensor not already removed from dbus
569 log<level::INFO>("Remove sensor from dbus for read fail",
570 entry("FILE=%s", file.c_str()),
571 entry("RC=%d", e.code().value()));
572 // Mark this sensor to be removed from dbus
Kun Yi501ade22019-07-15 15:00:32 -0700573 _rmSensors[sensorSetKey] = attrs;
Kun Yi553552c2019-07-15 22:14:21 -0700574 }
575 continue;
576 }
Matt Spinlerd8cacfd2021-04-26 09:56:21 -0500577#if UPDATE_FUNCTIONAL_ON_FAIL
Kun Yi553552c2019-07-15 22:14:21 -0700578 // Do not exit with failure if UPDATE_FUNCTIONAL_ON_FAIL is set
579 continue;
580#endif
581 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::
582 Error;
583 report<ReadFailure>(
584 xyz::openbmc_project::Sensor::Device::ReadFailure::
585 CALLOUT_ERRNO(e.code().value()),
586 xyz::openbmc_project::Sensor::Device::ReadFailure::
587 CALLOUT_DEVICE_PATH(_devPath.c_str()));
588
Matt Spinler6a391de2020-07-08 13:03:10 -0500589 log<level::INFO>(fmt::format("Failing sysfs file: {} errno: {}",
590 file, e.code().value())
591 .c_str());
Kun Yi553552c2019-07-15 22:14:21 -0700592
593 exit(EXIT_FAILURE);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500594 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600595 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500596
Patrick Venture0cd4f692019-06-21 13:39:40 -0700597 removeSensors();
598
Patrick Venture0cd4f692019-06-21 13:39:40 -0700599 addDroppedSensors();
Patrick Venture0cd4f692019-06-21 13:39:40 -0700600}
601
602void MainLoop::removeSensors()
603{
Matthew Barth8772ce32018-03-22 16:03:06 -0500604 // Remove any sensors marked for removal
Patrick Venture52b40612018-12-19 13:36:41 -0800605 for (const auto& i : _rmSensors)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600606 {
Matthew Barthd0ce7922019-06-06 09:23:37 -0500607 // Remove sensor object from dbus using emit_object_removed()
608 auto& objInfo = std::get<ObjectInfo>(_state[i.first]);
609 auto& objPath = std::get<std::string>(objInfo);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700610
Matthew Barthd0ce7922019-06-06 09:23:37 -0500611 _bus.emit_object_removed(objPath.c_str());
Patrick Venture0cd4f692019-06-21 13:39:40 -0700612
Matthew Barthd0ce7922019-06-06 09:23:37 -0500613 // Erase sensor object info
Patrick Venture52b40612018-12-19 13:36:41 -0800614 _state.erase(i.first);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500615 }
Patrick Venture0cd4f692019-06-21 13:39:40 -0700616}
Matthew Barth31d214c2018-03-26 09:54:27 -0500617
Patrick Venture0cd4f692019-06-21 13:39:40 -0700618void MainLoop::addDroppedSensors()
619{
Matthew Barth31d214c2018-03-26 09:54:27 -0500620 // Attempt to add any sensors that were removed
Patrick Venture52b40612018-12-19 13:36:41 -0800621 auto it = _rmSensors.begin();
622 while (it != _rmSensors.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500623 {
Patrick Venture52b40612018-12-19 13:36:41 -0800624 if (_state.find(it->first) == _state.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500625 {
626 SensorSet::container_t::value_type ssValueType =
Patrick Venture043d3232018-08-31 10:10:53 -0700627 std::make_pair(it->first, it->second);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700628
Matthew Barthd238e232018-04-17 12:01:50 -0500629 auto object = getObject(ssValueType);
630 if (object)
Matthew Barth31d214c2018-03-26 09:54:27 -0500631 {
Matthew Barthd238e232018-04-17 12:01:50 -0500632 // Construct the SensorSet value
633 // std::tuple<SensorSet::mapped_type,
634 // std::string(Sensor Label),
635 // ObjectInfo>
636 auto value = std::make_tuple(std::move(ssValueType.second),
637 std::move((*object).first),
638 std::move((*object).second));
639
Patrick Venture52b40612018-12-19 13:36:41 -0800640 _state[std::move(ssValueType.first)] = std::move(value);
Matthew Barthd238e232018-04-17 12:01:50 -0500641
Carol Wang9bbe6022019-08-01 17:31:30 +0800642 std::string input = hwmon::entry::input;
643 // If type is power and AVERAGE_power* is true in env, use
644 // average instead of input
645 if ((it->first.first == hwmon::type::power) &&
646 (phosphor::utility::isAverageEnvSet(it->first)))
647 {
648 input = hwmon::entry::average;
649 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500650 // Sensor object added, erase entry from removal list
Carol Wang9bbe6022019-08-01 17:31:30 +0800651 auto file =
652 sysfs::make_sysfs_path(_ioAccess->path(), it->first.first,
653 it->first.second, input);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700654
Patrick Venture043d3232018-08-31 10:10:53 -0700655 log<level::INFO>("Added sensor to dbus after successful read",
656 entry("FILE=%s", file.c_str()));
Patrick Venture0cd4f692019-06-21 13:39:40 -0700657
Patrick Venture52b40612018-12-19 13:36:41 -0800658 it = _rmSensors.erase(it);
Matthew Barth31d214c2018-03-26 09:54:27 -0500659 }
660 else
661 {
662 ++it;
663 }
664 }
665 else
666 {
667 // Sanity check to remove sensors that were re-added
Patrick Venture52b40612018-12-19 13:36:41 -0800668 it = _rmSensors.erase(it);
Matthew Barth31d214c2018-03-26 09:54:27 -0500669 }
670 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500671}
672
673// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4