blob: 40c429aa7e4c8d66b64363f2e0281caf86d1625b [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>
37#include <iostream>
38#include <memory>
39#include <phosphor-logging/elog-errors.hpp>
40#include <sstream>
41#include <string>
42#include <unordered_set>
Patrick Venture1e6324f2017-06-01 14:07:05 -070043#include <xyz/openbmc_project/Sensor/Device/error.hpp>
44
45using namespace phosphor::logging;
46
Saqib Khan973886d2017-03-15 14:01:16 -050047// Initialization for Warning Objects
48decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo =
49 &WarningObject::warningLow;
50decltype(Thresholds<WarningObject>::setHi) Thresholds<WarningObject>::setHi =
51 &WarningObject::warningHigh;
52decltype(Thresholds<WarningObject>::getLo) Thresholds<WarningObject>::getLo =
53 &WarningObject::warningLow;
54decltype(Thresholds<WarningObject>::getHi) Thresholds<WarningObject>::getHi =
55 &WarningObject::warningHigh;
Patrick Venture043d3232018-08-31 10:10:53 -070056decltype(
57 Thresholds<WarningObject>::alarmLo) Thresholds<WarningObject>::alarmLo =
Saqib Khan973886d2017-03-15 14:01:16 -050058 &WarningObject::warningAlarmLow;
Patrick Venture043d3232018-08-31 10:10:53 -070059decltype(
60 Thresholds<WarningObject>::alarmHi) Thresholds<WarningObject>::alarmHi =
Saqib Khan973886d2017-03-15 14:01:16 -050061 &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 Venture043d3232018-08-31 10:10:53 -070088decltype(
89 Thresholds<CriticalObject>::alarmLo) Thresholds<CriticalObject>::alarmLo =
Saqib Khan973886d2017-03-15 14:01:16 -050090 &CriticalObject::criticalAlarmLow;
Patrick Venture043d3232018-08-31 10:10:53 -070091decltype(
92 Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi =
Saqib Khan973886d2017-03-15 14:01:16 -050093 &CriticalObject::criticalAlarmHigh;
Duke Du73769092021-04-14 15:35:21 +080094decltype(Thresholds<CriticalObject>::getAlarmLow)
95 Thresholds<CriticalObject>::getAlarmLow = &CriticalObject::criticalAlarmLow;
96decltype(Thresholds<CriticalObject>::getAlarmHigh)
97 Thresholds<CriticalObject>::getAlarmHigh =
98 &CriticalObject::criticalAlarmHigh;
99decltype(Thresholds<CriticalObject>::assertLowSignal)
100 Thresholds<CriticalObject>::assertLowSignal =
101 &CriticalObject::criticalLowAlarmAsserted;
102decltype(Thresholds<CriticalObject>::assertHighSignal)
103 Thresholds<CriticalObject>::assertHighSignal =
104 &CriticalObject::criticalHighAlarmAsserted;
105decltype(Thresholds<CriticalObject>::deassertLowSignal)
106 Thresholds<CriticalObject>::deassertLowSignal =
107 &CriticalObject::criticalLowAlarmDeasserted;
108decltype(Thresholds<CriticalObject>::deassertHighSignal)
109 Thresholds<CriticalObject>::deassertHighSignal =
110 &CriticalObject::criticalHighAlarmDeasserted;
Saqib Khan973886d2017-03-15 14:01:16 -0500111
Matt Spinlerecac0ae2020-07-08 13:09:08 -0500112void updateSensorInterfaces(InterfaceMap& ifaces, SensorValueType value)
Patrick Venturefeb744a2019-06-26 19:07:48 -0700113{
114 for (auto& iface : ifaces)
115 {
116 switch (iface.first)
117 {
Kun Yi94a04c42019-08-21 09:43:20 -0700118 // clang-format off
Patrick Venturefeb744a2019-06-26 19:07:48 -0700119 case InterfaceType::VALUE:
120 {
121 auto& valueIface =
122 std::any_cast<std::shared_ptr<ValueObject>&>(iface.second);
123 valueIface->value(value);
124 }
125 break;
Kun Yi94a04c42019-08-21 09:43:20 -0700126 // clang-format on
Patrick Venturefeb744a2019-06-26 19:07:48 -0700127 case InterfaceType::WARN:
128 checkThresholds<WarningObject>(iface.second, value);
129 break;
130 case InterfaceType::CRIT:
131 checkThresholds<CriticalObject>(iface.second, value);
132 break;
133 default:
134 break;
135 }
136 }
137}
138
Matthew Barth979c8062018-04-17 11:37:15 -0500139std::string MainLoop::getID(SensorSet::container_t::const_reference sensor)
Matthew Barth31d214c2018-03-26 09:54:27 -0500140{
Matthew Barth31d214c2018-03-26 09:54:27 -0500141 std::string id;
142
143 /*
144 * Check if the value of the MODE_<item><X> env variable for the sensor
Matt Spinler7c424802018-05-04 10:52:40 -0500145 * is set. If it is, then read the from the <item><X>_<mode>
Matthew Barth31d214c2018-03-26 09:54:27 -0500146 * file. The name of the DBUS object would be the value of the env
Matt Spinler7c424802018-05-04 10:52:40 -0500147 * variable LABEL_<item><mode value>. If the MODE_<item><X> env variable
Matthew Barth31d214c2018-03-26 09:54:27 -0500148 * doesn't exist, then the name of DBUS object is the value of the env
149 * variable LABEL_<item><X>.
Matt Spinler7c424802018-05-04 10:52:40 -0500150 *
151 * For example, if MODE_temp1 = "label", then code reads the temp1_label
152 * file. If it has a 5 in it, then it will use the following entry to
153 * name the object: LABEL_temp5 = "My DBus object name".
154 *
Matthew Barth31d214c2018-03-26 09:54:27 -0500155 */
Patrick Venture7a5285d2018-04-17 19:15:05 -0700156 auto mode = env::getEnv("MODE", sensor.first);
Matt Spinler7c424802018-05-04 10:52:40 -0500157 if (!mode.empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500158 {
Patrick Venture043d3232018-08-31 10:10:53 -0700159 id = env::getIndirectID(_hwmonRoot + '/' + _instance + '/', mode,
160 sensor.first);
Matthew Barth31d214c2018-03-26 09:54:27 -0500161
162 if (id.empty())
163 {
Matthew Barth979c8062018-04-17 11:37:15 -0500164 return id;
Matthew Barth31d214c2018-03-26 09:54:27 -0500165 }
166 }
167
168 // Use the ID we looked up above if there was one,
169 // otherwise use the standard one.
170 id = (id.empty()) ? sensor.first.second : id;
171
Matthew Barth979c8062018-04-17 11:37:15 -0500172 return id;
173}
174
Patrick Venture043d3232018-08-31 10:10:53 -0700175SensorIdentifiers
176 MainLoop::getIdentifiers(SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500177{
178 std::string id = getID(sensor);
179 std::string label;
180
181 if (!id.empty())
182 {
183 // Ignore inputs without a label.
184 label = env::getEnv("LABEL", sensor.first.first, id);
185 }
186
Patrick Venture043d3232018-08-31 10:10:53 -0700187 return std::make_tuple(std::move(id), std::move(label));
Matthew Barth979c8062018-04-17 11:37:15 -0500188}
189
190/**
191 * Reads the environment parameters of a sensor and creates an object with
192 * atleast the `Value` interface, otherwise returns without creating the object.
193 * If the `Value` interface is successfully created, by reading the sensor's
194 * corresponding sysfs file's value, the additional interfaces for the sensor
Matthew Barthd238e232018-04-17 12:01:50 -0500195 * are created and the InterfacesAdded signal is emitted. The object's state
196 * data is then returned for sensor state monitoring within the main loop.
Matthew Barth979c8062018-04-17 11:37:15 -0500197 */
William A. Kennington III4cbdfef2018-10-18 19:19:51 -0700198std::optional<ObjectStateData>
Patrick Venture043d3232018-08-31 10:10:53 -0700199 MainLoop::getObject(SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500200{
201 auto properties = getIdentifiers(sensor);
202 if (std::get<sensorID>(properties).empty() ||
203 std::get<sensorLabel>(properties).empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500204 {
Matthew Barthd238e232018-04-17 12:01:50 -0500205 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500206 }
207
Patrick Venture09791852018-04-17 17:40:00 -0700208 hwmon::Attributes attrs;
209 if (!hwmon::getAttributes(sensor.first.first, attrs))
Matthew Barth31d214c2018-03-26 09:54:27 -0500210 {
Matthew Barthd238e232018-04-17 12:01:50 -0500211 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500212 }
213
Kun Yi501ade22019-07-15 15:00:32 -0700214 const auto& [sensorSetKey, sensorAttrs] = sensor;
215 const auto& [sensorSysfsType, sensorSysfsNum] = sensorSetKey;
216
Patrick Venture2864b062018-12-19 08:13:41 -0800217 /* Note: The sensor objects all share the same ioAccess object. */
Patrick Venture043d3232018-08-31 10:10:53 -0700218 auto sensorObj =
Kun Yi501ade22019-07-15 15:00:32 -0700219 std::make_unique<sensor::Sensor>(sensorSetKey, _ioAccess, _devPath);
Matthew Barth9c431062018-05-07 13:55:29 -0500220
Matthew Barthb7985272018-04-17 10:50:36 -0500221 // Get list of return codes for removing sensors on device
222 auto devRmRCs = env::getEnv("REMOVERCS");
223 // Add sensor removal return codes defined at the device level
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500224 sensorObj->addRemoveRCs(devRmRCs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500225
226 std::string objectPath{_root};
227 objectPath.append(1, '/');
Patrick Venture09791852018-04-17 17:40:00 -0700228 objectPath.append(hwmon::getNamespace(attrs));
Matthew Barth31d214c2018-03-26 09:54:27 -0500229 objectPath.append(1, '/');
Matthew Barth979c8062018-04-17 11:37:15 -0500230 objectPath.append(std::get<sensorLabel>(properties));
Matthew Barth31d214c2018-03-26 09:54:27 -0500231
Patrick Venture62067232019-06-19 17:39:33 -0700232 ObjectInfo info(&_bus, std::move(objectPath), InterfaceMap());
Patrick Venture75e56c62018-04-20 18:10:15 -0700233 RetryIO retryIO(hwmonio::retries, hwmonio::delay);
Kun Yi501ade22019-07-15 15:00:32 -0700234 if (_rmSensors.find(sensorSetKey) != _rmSensors.end())
Matthew Barthd4beecf2018-04-03 15:50:22 -0500235 {
236 // When adding a sensor that was purposely removed,
237 // don't retry on errors when reading its value
238 std::get<size_t>(retryIO) = 0;
239 }
Patrick Venture043d3232018-08-31 10:10:53 -0700240 auto valueInterface = static_cast<std::shared_ptr<ValueObject>>(nullptr);
Matthew Barth31d214c2018-03-26 09:54:27 -0500241 try
242 {
Matthew Barthca44c2e2018-04-24 15:33:25 -0500243 // Add status interface based on _fault file being present
Matthew Barth2e41b132018-05-07 14:15:45 -0500244 sensorObj->addStatus(info);
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500245 valueInterface = sensorObj->addValue(retryIO, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500246 }
247 catch (const std::system_error& e)
248 {
Patrick Venture043d3232018-08-31 10:10:53 -0700249 auto file =
Kun Yi501ade22019-07-15 15:00:32 -0700250 sysfs::make_sysfs_path(_ioAccess->path(), sensorSysfsType,
251 sensorSysfsNum, hwmon::entry::cinput);
Patrick Venture0892c3f2019-06-27 14:24:03 -0700252
Matthew Barth31d214c2018-03-26 09:54:27 -0500253 // Check sensorAdjusts for sensor removal RCs
Matthew Barthac473092018-05-07 14:41:46 -0500254 auto& sAdjusts = sensorObj->getAdjusts();
255 if (sAdjusts.rmRCs.count(e.code().value()) > 0)
Matthew Barth31d214c2018-03-26 09:54:27 -0500256 {
Matthew Barthac473092018-05-07 14:41:46 -0500257 // Return code found in sensor return code removal list
Kun Yi501ade22019-07-15 15:00:32 -0700258 if (_rmSensors.find(sensorSetKey) == _rmSensors.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500259 {
Matthew Barthac473092018-05-07 14:41:46 -0500260 // Trace for sensor not already removed from dbus
261 log<level::INFO>("Sensor not added to dbus for read fail",
Patrick Venture043d3232018-08-31 10:10:53 -0700262 entry("FILE=%s", file.c_str()),
263 entry("RC=%d", e.code().value()));
Kun Yi501ade22019-07-15 15:00:32 -0700264 _rmSensors[std::move(sensorSetKey)] = std::move(sensorAttrs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500265 }
Matthew Barthac473092018-05-07 14:41:46 -0500266 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500267 }
Patrick Venture0892c3f2019-06-27 14:24:03 -0700268
Patrick Venture043d3232018-08-31 10:10:53 -0700269 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error;
Matthew Barth31d214c2018-03-26 09:54:27 -0500270 report<ReadFailure>(
Patrick Venture043d3232018-08-31 10:10:53 -0700271 xyz::openbmc_project::Sensor::Device::ReadFailure::CALLOUT_ERRNO(
272 e.code().value()),
273 xyz::openbmc_project::Sensor::Device::ReadFailure::
274 CALLOUT_DEVICE_PATH(_devPath.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500275
Matt Spinler6a391de2020-07-08 13:03:10 -0500276 log<level::INFO>(fmt::format("Failing sysfs file: {} errno: {}", file,
277 e.code().value())
278 .c_str());
Matthew Barth31d214c2018-03-26 09:54:27 -0500279 exit(EXIT_FAILURE);
Matthew Barth31d214c2018-03-26 09:54:27 -0500280 }
281 auto sensorValue = valueInterface->value();
Matt Spinlerecac0ae2020-07-08 13:09:08 -0500282 int64_t scale = sensorObj->getScale();
283
Kun Yi501ade22019-07-15 15:00:32 -0700284 addThreshold<WarningObject>(sensorSysfsType, std::get<sensorID>(properties),
285 sensorValue, info, scale);
286 addThreshold<CriticalObject>(sensorSysfsType,
James Feistee73f5b2018-08-01 16:31:42 -0700287 std::get<sensorID>(properties), sensorValue,
288 info, scale);
Matthew Barth31d214c2018-03-26 09:54:27 -0500289
Patrick Venture043d3232018-08-31 10:10:53 -0700290 auto target =
Kun Yi501ade22019-07-15 15:00:32 -0700291 addTarget<hwmon::FanSpeed>(sensorSetKey, _ioAccess, _devPath, info);
Matthew Barth28f8e662018-03-26 16:57:36 -0500292 if (target)
Matthew Barth31d214c2018-03-26 09:54:27 -0500293 {
Matthew Barth28f8e662018-03-26 16:57:36 -0500294 target->enable();
Matthew Barth31d214c2018-03-26 09:54:27 -0500295 }
Kun Yi501ade22019-07-15 15:00:32 -0700296 addTarget<hwmon::FanPwm>(sensorSetKey, _ioAccess, _devPath, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500297
298 // All the interfaces have been created. Go ahead
299 // and emit InterfacesAdded.
300 valueInterface->emit_object_added();
301
Matthew Barth9c431062018-05-07 13:55:29 -0500302 // Save sensor object specifications
Kun Yi501ade22019-07-15 15:00:32 -0700303 _sensorObjects[sensorSetKey] = std::move(sensorObj);
Matthew Barth9c431062018-05-07 13:55:29 -0500304
Matthew Barthd238e232018-04-17 12:01:50 -0500305 return std::make_pair(std::move(std::get<sensorLabel>(properties)),
306 std::move(info));
Matthew Barth31d214c2018-03-26 09:54:27 -0500307}
308
Patrick Venture043d3232018-08-31 10:10:53 -0700309MainLoop::MainLoop(sdbusplus::bus::bus&& bus, const std::string& param,
310 const std::string& path, const std::string& devPath,
Patrick Venture16805a62019-06-21 14:19:33 -0700311 const char* prefix, const char* root,
312 const hwmonio::HwmonIOInterface* ioIntf) :
Patrick Venture043d3232018-08-31 10:10:53 -0700313 _bus(std::move(bus)),
314 _manager(_bus, root), _pathParam(param), _hwmonRoot(), _instance(),
Patrick Venture16805a62019-06-21 14:19:33 -0700315 _devPath(devPath), _prefix(prefix), _root(root), _state(),
316 _ioAccess(ioIntf), _event(sdeventplus::Event::get_default()),
Patrick Venture52b40612018-12-19 13:36:41 -0800317 _timer(_event, std::bind(&MainLoop::read, this))
Brad Bishopd499ca62016-12-19 09:24:50 -0500318{
Patrick Venture73a50c72018-04-17 15:19:03 -0700319 // Strip off any trailing slashes.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500320 std::string p = path;
321 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500322 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500323 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500324 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500325
Patrick Venture73a50c72018-04-17 15:19:03 -0700326 // Given the furthest right /, set instance to
327 // the basename, and hwmonRoot to the leading path.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500328 auto n = p.rfind('/');
329 if (n != std::string::npos)
330 {
331 _instance.assign(p.substr(n + 1));
332 _hwmonRoot.assign(p.substr(0, n));
333 }
334
335 assert(!_instance.empty());
336 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500337}
338
339void MainLoop::shutdown() noexcept
340{
Patrick Venture52b40612018-12-19 13:36:41 -0800341 _event.exit(0);
Brad Bishopd499ca62016-12-19 09:24:50 -0500342}
343
344void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500345{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600346 init();
347
Patrick Venture043d3232018-08-31 10:10:53 -0700348 std::function<void()> callback(std::bind(&MainLoop::read, this));
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600349 try
350 {
Patrick Venture52b40612018-12-19 13:36:41 -0800351 _timer.restart(std::chrono::microseconds(_interval));
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600352
353 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
354
355 // TODO: Issue#7 - Should probably periodically check the SensorSet
356 // for new entries.
357
Patrick Venture52b40612018-12-19 13:36:41 -0800358 _bus.attach_event(_event.get(), SD_EVENT_PRIORITY_IMPORTANT);
359 _event.loop();
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600360 }
William A. Kennington III0fe4cb32018-10-18 19:19:58 -0700361 catch (const std::exception& e)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600362 {
363 log<level::ERR>("Error in sysfs polling loop",
364 entry("ERROR=%s", e.what()));
365 throw;
366 }
367}
368
369void MainLoop::init()
370{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500371 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500372 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500373
Patrick Venturedb7ecb62018-10-23 19:42:23 -0700374 for (const auto& i : *sensors)
Brad Bishop75b4ab82017-01-06 09:33:50 -0500375 {
Matthew Barthd238e232018-04-17 12:01:50 -0500376 auto object = getObject(i);
377 if (object)
378 {
379 // Construct the SensorSet value
380 // std::tuple<SensorSet::mapped_type,
381 // std::string(Sensor Label),
382 // ObjectInfo>
Patrick Venture043d3232018-08-31 10:10:53 -0700383 auto value =
384 std::make_tuple(std::move(i.second), std::move((*object).first),
385 std::move((*object).second));
Matthew Barthd238e232018-04-17 12:01:50 -0500386
Patrick Venture52b40612018-12-19 13:36:41 -0800387 _state[std::move(i.first)] = std::move(value);
Matthew Barthd238e232018-04-17 12:01:50 -0500388 }
Carol Wang9bbe6022019-08-01 17:31:30 +0800389
390 // Initialize _averageMap of sensor. e.g. <<power, 1>, <0, 0>>
391 if ((i.first.first == hwmon::type::power) &&
392 (phosphor::utility::isAverageEnvSet(i.first)))
393 {
394 _average.setAverageValue(i.first, std::make_pair(0, 0));
395 }
Brad Bishop75b4ab82017-01-06 09:33:50 -0500396 }
397
Patrick Venture62503a42017-05-23 07:30:29 -0700398 /* If there are no sensors specified by labels, exit. */
Patrick Venture52b40612018-12-19 13:36:41 -0800399 if (0 == _state.size())
Patrick Venture62503a42017-05-23 07:30:29 -0700400 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600401 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700402 }
403
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500404 {
Patrick Venturec897d8b2018-04-23 19:01:56 -0700405 std::stringstream ss;
Patrick Venture043d3232018-08-31 10:10:53 -0700406 ss << _prefix << "-"
Patrick Venturec897d8b2018-04-23 19:01:56 -0700407 << std::to_string(std::hash<std::string>{}(_devPath + _pathParam))
408 << ".Hwmon1";
409
410 _bus.request_name(ss.str().c_str());
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500411 }
412
Patrick Ventureab10f162017-05-22 09:44:50 -0700413 {
Patrick Venturea24c8802018-04-17 19:38:06 -0700414 auto interval = env::getEnv("INTERVAL");
415 if (!interval.empty())
Patrick Ventureab10f162017-05-22 09:44:50 -0700416 {
Patrick Venture50cf1c52018-04-18 09:21:41 -0700417 _interval = std::strtoull(interval.c_str(), NULL, 10);
Patrick Ventureab10f162017-05-22 09:44:50 -0700418 }
419 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600420}
Patrick Ventureab10f162017-05-22 09:44:50 -0700421
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600422void MainLoop::read()
423{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500424 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
425 // ensure the objects all exist?
426
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600427 // Iterate through all the sensors.
Kun Yi501ade22019-07-15 15:00:32 -0700428 for (auto& [sensorSetKey, sensorStateTuple] : _state)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600429 {
Kun Yi501ade22019-07-15 15:00:32 -0700430 const auto& [sensorSysfsType, sensorSysfsNum] = sensorSetKey;
431 auto& [attrs, unused, objInfo] = sensorStateTuple;
432
Kun Yi553552c2019-07-15 22:14:21 -0700433 if (attrs.find(hwmon::entry::input) == attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500434 {
Kun Yi553552c2019-07-15 22:14:21 -0700435 continue;
436 }
Kun Yi501ade22019-07-15 15:00:32 -0700437
Kun Yi553552c2019-07-15 22:14:21 -0700438 // Read value from sensor.
Carol Wang9bbe6022019-08-01 17:31:30 +0800439 std::string input = hwmon::entry::input;
440 if (sensorSysfsType == hwmon::type::pwm)
Kun Yi553552c2019-07-15 22:14:21 -0700441 {
442 input = "";
443 }
Carol Wang9bbe6022019-08-01 17:31:30 +0800444 // If type is power and AVERAGE_power* is true in env, use average
445 // instead of input
446 else if ((sensorSysfsType == hwmon::type::power) &&
447 (phosphor::utility::isAverageEnvSet(sensorSetKey)))
448 {
449 input = hwmon::entry::average;
450 }
Kun Yi553552c2019-07-15 22:14:21 -0700451
Matt Spinlerecac0ae2020-07-08 13:09:08 -0500452 SensorValueType value;
Kun Yi553552c2019-07-15 22:14:21 -0700453 auto& obj = std::get<InterfaceMap>(objInfo);
Kun Yi501ade22019-07-15 15:00:32 -0700454 std::unique_ptr<sensor::Sensor>& sensor = _sensorObjects[sensorSetKey];
Kun Yi553552c2019-07-15 22:14:21 -0700455
456 auto& statusIface = std::any_cast<std::shared_ptr<StatusObject>&>(
457 obj[InterfaceType::STATUS]);
458 // As long as addStatus is called before addValue, statusIface
459 // should never be nullptr.
460 assert(statusIface);
461
462 try
463 {
464 if (sensor->hasFaultFile())
Patrick Venture043d3232018-08-31 10:10:53 -0700465 {
Kun Yi501ade22019-07-15 15:00:32 -0700466 auto fault = _ioAccess->read(sensorSysfsType, sensorSysfsNum,
Kun Yi553552c2019-07-15 22:14:21 -0700467 hwmon::entry::fault,
468 hwmonio::retries, hwmonio::delay);
469 // Skip reading from a sensor with a valid fault file
470 // and set the functional property accordingly
471 if (!statusIface->functional((fault == 0) ? true : false))
Matthew Barth27c4a392018-04-25 14:38:51 -0500472 {
Matthew Barthac473092018-05-07 14:41:46 -0500473 continue;
Matthew Barth8772ce32018-03-22 16:03:06 -0500474 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500475 }
Kun Yi553552c2019-07-15 22:14:21 -0700476
477 {
478 // RAII object for GPIO unlock / lock
William A. Kennington III2227bd52019-06-19 11:32:22 -0700479 auto locker = sensor::gpioUnlock(sensor->getGpio());
Kun Yi553552c2019-07-15 22:14:21 -0700480
481 // Retry for up to a second if device is busy
482 // or has a transient error.
Kun Yi501ade22019-07-15 15:00:32 -0700483 value = _ioAccess->read(sensorSysfsType, sensorSysfsNum, input,
Kun Yi553552c2019-07-15 22:14:21 -0700484 hwmonio::retries, hwmonio::delay);
485 // Set functional property to true if we could read sensor
486 statusIface->functional(true);
487
488 value = sensor->adjustValue(value);
Carol Wang9bbe6022019-08-01 17:31:30 +0800489
490 if (input == hwmon::entry::average)
491 {
492 // Calculate the values of averageMap based on current
493 // average value, current average_interval value, previous
494 // average value, previous average_interval value
495 int64_t interval =
496 _ioAccess->read(sensorSysfsType, sensorSysfsNum,
497 hwmon::entry::caverage_interval,
498 hwmonio::retries, hwmonio::delay);
499 auto ret = _average.getAverageValue(sensorSetKey);
500 assert(ret);
501
502 const auto& [preAverage, preInterval] = *ret;
503
504 auto calValue = Average::calcAverage(
505 preAverage, preInterval, value, interval);
506 if (calValue)
507 {
508 // Update previous values in averageMap before the
509 // variable value is changed next
510 _average.setAverageValue(
511 sensorSetKey, std::make_pair(value, interval));
512 // Update value to be calculated average
513 value = calValue.value();
514 }
515 else
516 {
517 // the value of
518 // power*_average_interval is not changed yet, use the
519 // previous calculated average instead. So skip dbus
520 // update.
521 continue;
522 }
523 }
Kun Yi553552c2019-07-15 22:14:21 -0700524 }
525
526 updateSensorInterfaces(obj, value);
527 }
528 catch (const std::system_error& e)
529 {
Matt Spinlerd8cacfd2021-04-26 09:56:21 -0500530#if UPDATE_FUNCTIONAL_ON_FAIL
Kun Yi553552c2019-07-15 22:14:21 -0700531 // If UPDATE_FUNCTIONAL_ON_FAIL is defined and an exception was
532 // thrown, set the functional property to false.
533 // We cannot set this with the 'continue' in the lower block
534 // as the code may exit before reaching it.
535 statusIface->functional(false);
536#endif
Carol Wang9bbe6022019-08-01 17:31:30 +0800537 auto file = sysfs::make_sysfs_path(
538 _ioAccess->path(), sensorSysfsType, sensorSysfsNum, input);
Kun Yi553552c2019-07-15 22:14:21 -0700539
540 // Check sensorAdjusts for sensor removal RCs
Kun Yi501ade22019-07-15 15:00:32 -0700541 auto& sAdjusts = _sensorObjects[sensorSetKey]->getAdjusts();
Kun Yi553552c2019-07-15 22:14:21 -0700542 if (sAdjusts.rmRCs.count(e.code().value()) > 0)
543 {
544 // Return code found in sensor return code removal list
Kun Yi501ade22019-07-15 15:00:32 -0700545 if (_rmSensors.find(sensorSetKey) == _rmSensors.end())
Kun Yi553552c2019-07-15 22:14:21 -0700546 {
547 // Trace for sensor not already removed from dbus
548 log<level::INFO>("Remove sensor from dbus for read fail",
549 entry("FILE=%s", file.c_str()),
550 entry("RC=%d", e.code().value()));
551 // Mark this sensor to be removed from dbus
Kun Yi501ade22019-07-15 15:00:32 -0700552 _rmSensors[sensorSetKey] = attrs;
Kun Yi553552c2019-07-15 22:14:21 -0700553 }
554 continue;
555 }
Matt Spinlerd8cacfd2021-04-26 09:56:21 -0500556#if UPDATE_FUNCTIONAL_ON_FAIL
Kun Yi553552c2019-07-15 22:14:21 -0700557 // Do not exit with failure if UPDATE_FUNCTIONAL_ON_FAIL is set
558 continue;
559#endif
560 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::
561 Error;
562 report<ReadFailure>(
563 xyz::openbmc_project::Sensor::Device::ReadFailure::
564 CALLOUT_ERRNO(e.code().value()),
565 xyz::openbmc_project::Sensor::Device::ReadFailure::
566 CALLOUT_DEVICE_PATH(_devPath.c_str()));
567
Matt Spinler6a391de2020-07-08 13:03:10 -0500568 log<level::INFO>(fmt::format("Failing sysfs file: {} errno: {}",
569 file, e.code().value())
570 .c_str());
Kun Yi553552c2019-07-15 22:14:21 -0700571
572 exit(EXIT_FAILURE);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500573 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600574 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500575
Patrick Venture0cd4f692019-06-21 13:39:40 -0700576 removeSensors();
577
Patrick Venture0cd4f692019-06-21 13:39:40 -0700578 addDroppedSensors();
Patrick Venture0cd4f692019-06-21 13:39:40 -0700579}
580
581void MainLoop::removeSensors()
582{
Matthew Barth8772ce32018-03-22 16:03:06 -0500583 // Remove any sensors marked for removal
Patrick Venture52b40612018-12-19 13:36:41 -0800584 for (const auto& i : _rmSensors)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600585 {
Matthew Barthd0ce7922019-06-06 09:23:37 -0500586 // Remove sensor object from dbus using emit_object_removed()
587 auto& objInfo = std::get<ObjectInfo>(_state[i.first]);
588 auto& objPath = std::get<std::string>(objInfo);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700589
Matthew Barthd0ce7922019-06-06 09:23:37 -0500590 _bus.emit_object_removed(objPath.c_str());
Patrick Venture0cd4f692019-06-21 13:39:40 -0700591
Matthew Barthd0ce7922019-06-06 09:23:37 -0500592 // Erase sensor object info
Patrick Venture52b40612018-12-19 13:36:41 -0800593 _state.erase(i.first);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500594 }
Patrick Venture0cd4f692019-06-21 13:39:40 -0700595}
Matthew Barth31d214c2018-03-26 09:54:27 -0500596
Patrick Venture0cd4f692019-06-21 13:39:40 -0700597void MainLoop::addDroppedSensors()
598{
Matthew Barth31d214c2018-03-26 09:54:27 -0500599 // Attempt to add any sensors that were removed
Patrick Venture52b40612018-12-19 13:36:41 -0800600 auto it = _rmSensors.begin();
601 while (it != _rmSensors.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500602 {
Patrick Venture52b40612018-12-19 13:36:41 -0800603 if (_state.find(it->first) == _state.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500604 {
605 SensorSet::container_t::value_type ssValueType =
Patrick Venture043d3232018-08-31 10:10:53 -0700606 std::make_pair(it->first, it->second);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700607
Matthew Barthd238e232018-04-17 12:01:50 -0500608 auto object = getObject(ssValueType);
609 if (object)
Matthew Barth31d214c2018-03-26 09:54:27 -0500610 {
Matthew Barthd238e232018-04-17 12:01:50 -0500611 // Construct the SensorSet value
612 // std::tuple<SensorSet::mapped_type,
613 // std::string(Sensor Label),
614 // ObjectInfo>
615 auto value = std::make_tuple(std::move(ssValueType.second),
616 std::move((*object).first),
617 std::move((*object).second));
618
Patrick Venture52b40612018-12-19 13:36:41 -0800619 _state[std::move(ssValueType.first)] = std::move(value);
Matthew Barthd238e232018-04-17 12:01:50 -0500620
Carol Wang9bbe6022019-08-01 17:31:30 +0800621 std::string input = hwmon::entry::input;
622 // If type is power and AVERAGE_power* is true in env, use
623 // average instead of input
624 if ((it->first.first == hwmon::type::power) &&
625 (phosphor::utility::isAverageEnvSet(it->first)))
626 {
627 input = hwmon::entry::average;
628 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500629 // Sensor object added, erase entry from removal list
Carol Wang9bbe6022019-08-01 17:31:30 +0800630 auto file =
631 sysfs::make_sysfs_path(_ioAccess->path(), it->first.first,
632 it->first.second, input);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700633
Patrick Venture043d3232018-08-31 10:10:53 -0700634 log<level::INFO>("Added sensor to dbus after successful read",
635 entry("FILE=%s", file.c_str()));
Patrick Venture0cd4f692019-06-21 13:39:40 -0700636
Patrick Venture52b40612018-12-19 13:36:41 -0800637 it = _rmSensors.erase(it);
Matthew Barth31d214c2018-03-26 09:54:27 -0500638 }
639 else
640 {
641 ++it;
642 }
643 }
644 else
645 {
646 // Sanity check to remove sensors that were re-added
Patrick Venture52b40612018-12-19 13:36:41 -0800647 it = _rmSensors.erase(it);
Matthew Barth31d214c2018-03-26 09:54:27 -0500648 }
649 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500650}
651
652// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4