blob: 6fa74d33d74f5d40587682a47768bc63831a5076 [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
Patrick Williamse8771fd2023-05-10 07:51:06 -050032#include <phosphor-logging/elog-errors.hpp>
33#include <xyz/openbmc_project/Sensor/Device/error.hpp>
34
William A. Kennington III0e749752018-11-06 15:25:41 -080035#include <cassert>
Patrick Venture043d3232018-08-31 10:10:53 -070036#include <cstdlib>
Patrick Williams64129932024-02-13 21:10:17 -060037#include <format>
Patrick Venture043d3232018-08-31 10:10:53 -070038#include <functional>
Brandon Kim6d50c3e2019-08-09 15:38:53 -070039#include <future>
Patrick Venture043d3232018-08-31 10:10:53 -070040#include <iostream>
41#include <memory>
Patrick Venture043d3232018-08-31 10:10:53 -070042#include <sstream>
43#include <string>
44#include <unordered_set>
Patrick Venture1e6324f2017-06-01 14:07:05 -070045
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 {
Patrick Williamsc8e818d2023-10-20 11:19:30 -0500115 // 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;
George Liuc9d61612022-10-12 14:31:39 +0800177 std::string accuracy;
Lakshmi Yadlapati47fb49a2023-10-19 14:47:08 -0500178 std::string priority;
Matthew Barth979c8062018-04-17 11:37:15 -0500179
180 if (!id.empty())
181 {
182 // Ignore inputs without a label.
183 label = env::getEnv("LABEL", sensor.first.first, id);
George Liuc9d61612022-10-12 14:31:39 +0800184 accuracy = env::getEnv("ACCURACY", sensor.first.first, id);
Lakshmi Yadlapati47fb49a2023-10-19 14:47:08 -0500185 priority = env::getEnv("PRIORITY", sensor.first.first, id);
Matthew Barth979c8062018-04-17 11:37:15 -0500186 }
187
Lakshmi Yadlapati47fb49a2023-10-19 14:47:08 -0500188 return std::make_tuple(std::move(id), std::move(label), std::move(accuracy),
189 std::move(priority));
Matthew Barth979c8062018-04-17 11:37:15 -0500190}
191
192/**
193 * Reads the environment parameters of a sensor and creates an object with
194 * atleast the `Value` interface, otherwise returns without creating the object.
195 * If the `Value` interface is successfully created, by reading the sensor's
196 * corresponding sysfs file's value, the additional interfaces for the sensor
Matthew Barthd238e232018-04-17 12:01:50 -0500197 * are created and the InterfacesAdded signal is emitted. The object's state
198 * data is then returned for sensor state monitoring within the main loop.
Matthew Barth979c8062018-04-17 11:37:15 -0500199 */
William A. Kennington III4cbdfef2018-10-18 19:19:51 -0700200std::optional<ObjectStateData>
Patrick Venture043d3232018-08-31 10:10:53 -0700201 MainLoop::getObject(SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500202{
203 auto properties = getIdentifiers(sensor);
204 if (std::get<sensorID>(properties).empty() ||
205 std::get<sensorLabel>(properties).empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500206 {
Matthew Barthd238e232018-04-17 12:01:50 -0500207 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500208 }
209
Patrick Venture09791852018-04-17 17:40:00 -0700210 hwmon::Attributes attrs;
211 if (!hwmon::getAttributes(sensor.first.first, attrs))
Matthew Barth31d214c2018-03-26 09:54:27 -0500212 {
Matthew Barthd238e232018-04-17 12:01:50 -0500213 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500214 }
215
Kun Yi501ade22019-07-15 15:00:32 -0700216 const auto& [sensorSetKey, sensorAttrs] = sensor;
217 const auto& [sensorSysfsType, sensorSysfsNum] = sensorSetKey;
218
Patrick Venture2864b062018-12-19 08:13:41 -0800219 /* Note: The sensor objects all share the same ioAccess object. */
Patrick Williamse8771fd2023-05-10 07:51:06 -0500220 auto sensorObj = std::make_unique<sensor::Sensor>(sensorSetKey, _ioAccess,
221 _devPath);
Matthew Barth9c431062018-05-07 13:55:29 -0500222
Matthew Barthb7985272018-04-17 10:50:36 -0500223 // Get list of return codes for removing sensors on device
224 auto devRmRCs = env::getEnv("REMOVERCS");
225 // Add sensor removal return codes defined at the device level
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500226 sensorObj->addRemoveRCs(devRmRCs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500227
228 std::string objectPath{_root};
229 objectPath.append(1, '/');
Patrick Venture09791852018-04-17 17:40:00 -0700230 objectPath.append(hwmon::getNamespace(attrs));
Matthew Barth31d214c2018-03-26 09:54:27 -0500231 objectPath.append(1, '/');
Matthew Barth979c8062018-04-17 11:37:15 -0500232 objectPath.append(std::get<sensorLabel>(properties));
Matthew Barth31d214c2018-03-26 09:54:27 -0500233
Patrick Venture62067232019-06-19 17:39:33 -0700234 ObjectInfo info(&_bus, std::move(objectPath), InterfaceMap());
Patrick Venture75e56c62018-04-20 18:10:15 -0700235 RetryIO retryIO(hwmonio::retries, hwmonio::delay);
Kun Yi501ade22019-07-15 15:00:32 -0700236 if (_rmSensors.find(sensorSetKey) != _rmSensors.end())
Matthew Barthd4beecf2018-04-03 15:50:22 -0500237 {
238 // When adding a sensor that was purposely removed,
239 // don't retry on errors when reading its value
240 std::get<size_t>(retryIO) = 0;
241 }
Patrick Venture043d3232018-08-31 10:10:53 -0700242 auto valueInterface = static_cast<std::shared_ptr<ValueObject>>(nullptr);
Matthew Barth31d214c2018-03-26 09:54:27 -0500243 try
244 {
George Liuc9d61612022-10-12 14:31:39 +0800245 // Add accuracy interface
246 auto accuracyStr = std::get<sensorAccuracy>(properties);
247 try
248 {
249 if (!accuracyStr.empty())
250 {
251 auto accuracy = stod(accuracyStr);
252 sensorObj->addAccuracy(info, accuracy);
253 }
254 }
255 catch (const std::invalid_argument&)
Patrick Williamse8771fd2023-05-10 07:51:06 -0500256 {}
George Liuc9d61612022-10-12 14:31:39 +0800257
Lakshmi Yadlapati47fb49a2023-10-19 14:47:08 -0500258 // Add priority interface
259 auto priorityStr = std::get<sensorPriority>(properties);
260 try
261 {
262 if (!priorityStr.empty())
263 {
264 auto priority = std::stoul(priorityStr);
265 sensorObj->addPriority(info, priority);
266 }
267 }
268 catch (const std::invalid_argument&)
269 {}
270
Matthew Barthca44c2e2018-04-24 15:33:25 -0500271 // Add status interface based on _fault file being present
Matthew Barth2e41b132018-05-07 14:15:45 -0500272 sensorObj->addStatus(info);
Brandon Kim6d50c3e2019-08-09 15:38:53 -0700273 valueInterface = sensorObj->addValue(retryIO, info, _timedoutMap);
Matthew Barth31d214c2018-03-26 09:54:27 -0500274 }
275 catch (const std::system_error& e)
276 {
Patrick Williamse8771fd2023-05-10 07:51:06 -0500277 auto file = sysfs::make_sysfs_path(_ioAccess->path(), sensorSysfsType,
278 sensorSysfsNum,
279 hwmon::entry::cinput);
Patrick Venture0892c3f2019-06-27 14:24:03 -0700280
Matthew Barth31d214c2018-03-26 09:54:27 -0500281 // Check sensorAdjusts for sensor removal RCs
Matthew Barthac473092018-05-07 14:41:46 -0500282 auto& sAdjusts = sensorObj->getAdjusts();
283 if (sAdjusts.rmRCs.count(e.code().value()) > 0)
Matthew Barth31d214c2018-03-26 09:54:27 -0500284 {
Matthew Barthac473092018-05-07 14:41:46 -0500285 // Return code found in sensor return code removal list
Kun Yi501ade22019-07-15 15:00:32 -0700286 if (_rmSensors.find(sensorSetKey) == _rmSensors.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500287 {
Matthew Barthac473092018-05-07 14:41:46 -0500288 // Trace for sensor not already removed from dbus
289 log<level::INFO>("Sensor not added to dbus for read fail",
Patrick Venture043d3232018-08-31 10:10:53 -0700290 entry("FILE=%s", file.c_str()),
291 entry("RC=%d", e.code().value()));
Kun Yi501ade22019-07-15 15:00:32 -0700292 _rmSensors[std::move(sensorSetKey)] = std::move(sensorAttrs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500293 }
Matthew Barthac473092018-05-07 14:41:46 -0500294 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500295 }
Patrick Venture0892c3f2019-06-27 14:24:03 -0700296
Patrick Venture043d3232018-08-31 10:10:53 -0700297 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error;
Matthew Barth31d214c2018-03-26 09:54:27 -0500298 report<ReadFailure>(
Patrick Venture043d3232018-08-31 10:10:53 -0700299 xyz::openbmc_project::Sensor::Device::ReadFailure::CALLOUT_ERRNO(
300 e.code().value()),
301 xyz::openbmc_project::Sensor::Device::ReadFailure::
302 CALLOUT_DEVICE_PATH(_devPath.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500303
Patrick Williams64129932024-02-13 21:10:17 -0600304 log<level::INFO>(std::format("Failing sysfs file: {} errno: {}", file,
Matt Spinler6a391de2020-07-08 13:03:10 -0500305 e.code().value())
306 .c_str());
Matthew Barth31d214c2018-03-26 09:54:27 -0500307 exit(EXIT_FAILURE);
Matthew Barth31d214c2018-03-26 09:54:27 -0500308 }
309 auto sensorValue = valueInterface->value();
Matt Spinlerecac0ae2020-07-08 13:09:08 -0500310 int64_t scale = sensorObj->getScale();
311
Kun Yi501ade22019-07-15 15:00:32 -0700312 addThreshold<WarningObject>(sensorSysfsType, std::get<sensorID>(properties),
313 sensorValue, info, scale);
314 addThreshold<CriticalObject>(sensorSysfsType,
James Feistee73f5b2018-08-01 16:31:42 -0700315 std::get<sensorID>(properties), sensorValue,
316 info, scale);
Matthew Barth31d214c2018-03-26 09:54:27 -0500317
Patrick Williamse8771fd2023-05-10 07:51:06 -0500318 auto target = addTarget<hwmon::FanSpeed>(sensorSetKey, _ioAccess, _devPath,
319 info);
Matthew Barth28f8e662018-03-26 16:57:36 -0500320 if (target)
Matthew Barth31d214c2018-03-26 09:54:27 -0500321 {
Matthew Barth28f8e662018-03-26 16:57:36 -0500322 target->enable();
Matthew Barth31d214c2018-03-26 09:54:27 -0500323 }
Kun Yi501ade22019-07-15 15:00:32 -0700324 addTarget<hwmon::FanPwm>(sensorSetKey, _ioAccess, _devPath, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500325
326 // All the interfaces have been created. Go ahead
327 // and emit InterfacesAdded.
328 valueInterface->emit_object_added();
329
Matthew Barth9c431062018-05-07 13:55:29 -0500330 // Save sensor object specifications
Kun Yi501ade22019-07-15 15:00:32 -0700331 _sensorObjects[sensorSetKey] = std::move(sensorObj);
Matthew Barth9c431062018-05-07 13:55:29 -0500332
Matthew Barthd238e232018-04-17 12:01:50 -0500333 return std::make_pair(std::move(std::get<sensorLabel>(properties)),
334 std::move(info));
Matthew Barth31d214c2018-03-26 09:54:27 -0500335}
336
Patrick Williamsad6043f2022-07-22 19:26:56 -0500337MainLoop::MainLoop(sdbusplus::bus_t&& bus, const std::string& param,
Patrick Venture043d3232018-08-31 10:10:53 -0700338 const std::string& path, const std::string& devPath,
Patrick Venture16805a62019-06-21 14:19:33 -0700339 const char* prefix, const char* root,
Anton D. Kachalovd46d0812021-02-03 23:25:37 +0100340 const std::string& instanceId,
Patrick Venture16805a62019-06-21 14:19:33 -0700341 const hwmonio::HwmonIOInterface* ioIntf) :
Patrick Venture043d3232018-08-31 10:10:53 -0700342 _bus(std::move(bus)),
343 _manager(_bus, root), _pathParam(param), _hwmonRoot(), _instance(),
Patrick Venture16805a62019-06-21 14:19:33 -0700344 _devPath(devPath), _prefix(prefix), _root(root), _state(),
Anton D. Kachalovd46d0812021-02-03 23:25:37 +0100345 _instanceId(instanceId), _ioAccess(ioIntf),
346 _event(sdeventplus::Event::get_default()),
Patrick Venture52b40612018-12-19 13:36:41 -0800347 _timer(_event, std::bind(&MainLoop::read, this))
Brad Bishopd499ca62016-12-19 09:24:50 -0500348{
Patrick Venture73a50c72018-04-17 15:19:03 -0700349 // Strip off any trailing slashes.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500350 std::string p = path;
351 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500352 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500353 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500354 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500355
Patrick Venture73a50c72018-04-17 15:19:03 -0700356 // Given the furthest right /, set instance to
357 // the basename, and hwmonRoot to the leading path.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500358 auto n = p.rfind('/');
359 if (n != std::string::npos)
360 {
361 _instance.assign(p.substr(n + 1));
362 _hwmonRoot.assign(p.substr(0, n));
363 }
364
365 assert(!_instance.empty());
366 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500367}
368
369void MainLoop::shutdown() noexcept
370{
Patrick Venture52b40612018-12-19 13:36:41 -0800371 _event.exit(0);
Brad Bishopd499ca62016-12-19 09:24:50 -0500372}
373
374void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500375{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600376 init();
377
Patrick Venture043d3232018-08-31 10:10:53 -0700378 std::function<void()> callback(std::bind(&MainLoop::read, this));
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600379 try
380 {
Patrick Venture52b40612018-12-19 13:36:41 -0800381 _timer.restart(std::chrono::microseconds(_interval));
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600382
383 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
384
385 // TODO: Issue#7 - Should probably periodically check the SensorSet
386 // for new entries.
387
Patrick Venture52b40612018-12-19 13:36:41 -0800388 _bus.attach_event(_event.get(), SD_EVENT_PRIORITY_IMPORTANT);
389 _event.loop();
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600390 }
William A. Kennington III0fe4cb32018-10-18 19:19:58 -0700391 catch (const std::exception& e)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600392 {
393 log<level::ERR>("Error in sysfs polling loop",
394 entry("ERROR=%s", e.what()));
395 throw;
396 }
397}
398
399void MainLoop::init()
400{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500401 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500402 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500403
Patrick Venturedb7ecb62018-10-23 19:42:23 -0700404 for (const auto& i : *sensors)
Brad Bishop75b4ab82017-01-06 09:33:50 -0500405 {
Matthew Barthd238e232018-04-17 12:01:50 -0500406 auto object = getObject(i);
407 if (object)
408 {
409 // Construct the SensorSet value
410 // std::tuple<SensorSet::mapped_type,
411 // std::string(Sensor Label),
412 // ObjectInfo>
Patrick Williamse8771fd2023-05-10 07:51:06 -0500413 auto value = std::make_tuple(std::move(i.second),
414 std::move((*object).first),
415 std::move((*object).second));
Matthew Barthd238e232018-04-17 12:01:50 -0500416
Patrick Venture52b40612018-12-19 13:36:41 -0800417 _state[std::move(i.first)] = std::move(value);
Matthew Barthd238e232018-04-17 12:01:50 -0500418 }
Carol Wang9bbe6022019-08-01 17:31:30 +0800419
420 // Initialize _averageMap of sensor. e.g. <<power, 1>, <0, 0>>
421 if ((i.first.first == hwmon::type::power) &&
422 (phosphor::utility::isAverageEnvSet(i.first)))
423 {
424 _average.setAverageValue(i.first, std::make_pair(0, 0));
425 }
Brad Bishop75b4ab82017-01-06 09:33:50 -0500426 }
427
Patrick Venture62503a42017-05-23 07:30:29 -0700428 /* If there are no sensors specified by labels, exit. */
Patrick Venture52b40612018-12-19 13:36:41 -0800429 if (0 == _state.size())
Patrick Venture62503a42017-05-23 07:30:29 -0700430 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600431 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700432 }
433
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500434 {
Patrick Venturec897d8b2018-04-23 19:01:56 -0700435 std::stringstream ss;
Anton D. Kachalovd46d0812021-02-03 23:25:37 +0100436 std::string id = _instanceId;
437 if (id.empty())
438 {
439 id =
440 std::to_string(std::hash<std::string>{}(_devPath + _pathParam));
441 }
442 ss << _prefix << "-" << id << ".Hwmon1";
Patrick Venturec897d8b2018-04-23 19:01:56 -0700443
444 _bus.request_name(ss.str().c_str());
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500445 }
446
Patrick Ventureab10f162017-05-22 09:44:50 -0700447 {
Patrick Venturea24c8802018-04-17 19:38:06 -0700448 auto interval = env::getEnv("INTERVAL");
449 if (!interval.empty())
Patrick Ventureab10f162017-05-22 09:44:50 -0700450 {
Patrick Venture50cf1c52018-04-18 09:21:41 -0700451 _interval = std::strtoull(interval.c_str(), NULL, 10);
Patrick Ventureab10f162017-05-22 09:44:50 -0700452 }
453 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600454}
Patrick Ventureab10f162017-05-22 09:44:50 -0700455
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600456void MainLoop::read()
457{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500458 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
459 // ensure the objects all exist?
460
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600461 // Iterate through all the sensors.
Kun Yi501ade22019-07-15 15:00:32 -0700462 for (auto& [sensorSetKey, sensorStateTuple] : _state)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600463 {
Kun Yi501ade22019-07-15 15:00:32 -0700464 const auto& [sensorSysfsType, sensorSysfsNum] = sensorSetKey;
465 auto& [attrs, unused, objInfo] = sensorStateTuple;
466
Kun Yi553552c2019-07-15 22:14:21 -0700467 if (attrs.find(hwmon::entry::input) == attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500468 {
Kun Yi553552c2019-07-15 22:14:21 -0700469 continue;
470 }
Kun Yi501ade22019-07-15 15:00:32 -0700471
Kun Yi553552c2019-07-15 22:14:21 -0700472 // Read value from sensor.
Carol Wang9bbe6022019-08-01 17:31:30 +0800473 std::string input = hwmon::entry::input;
474 if (sensorSysfsType == hwmon::type::pwm)
Kun Yi553552c2019-07-15 22:14:21 -0700475 {
476 input = "";
477 }
Carol Wang9bbe6022019-08-01 17:31:30 +0800478 // If type is power and AVERAGE_power* is true in env, use average
479 // instead of input
480 else if ((sensorSysfsType == hwmon::type::power) &&
481 (phosphor::utility::isAverageEnvSet(sensorSetKey)))
482 {
483 input = hwmon::entry::average;
484 }
Kun Yi553552c2019-07-15 22:14:21 -0700485
Matt Spinlerecac0ae2020-07-08 13:09:08 -0500486 SensorValueType value;
Kun Yi553552c2019-07-15 22:14:21 -0700487 auto& obj = std::get<InterfaceMap>(objInfo);
Kun Yi501ade22019-07-15 15:00:32 -0700488 std::unique_ptr<sensor::Sensor>& sensor = _sensorObjects[sensorSetKey];
Kun Yi553552c2019-07-15 22:14:21 -0700489
490 auto& statusIface = std::any_cast<std::shared_ptr<StatusObject>&>(
491 obj[InterfaceType::STATUS]);
492 // As long as addStatus is called before addValue, statusIface
493 // should never be nullptr.
494 assert(statusIface);
495
496 try
497 {
498 if (sensor->hasFaultFile())
Patrick Venture043d3232018-08-31 10:10:53 -0700499 {
Kun Yi501ade22019-07-15 15:00:32 -0700500 auto fault = _ioAccess->read(sensorSysfsType, sensorSysfsNum,
Kun Yi553552c2019-07-15 22:14:21 -0700501 hwmon::entry::fault,
502 hwmonio::retries, hwmonio::delay);
503 // Skip reading from a sensor with a valid fault file
504 // and set the functional property accordingly
505 if (!statusIface->functional((fault == 0) ? true : false))
Matthew Barth27c4a392018-04-25 14:38:51 -0500506 {
Matthew Barthac473092018-05-07 14:41:46 -0500507 continue;
Matthew Barth8772ce32018-03-22 16:03:06 -0500508 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500509 }
Kun Yi553552c2019-07-15 22:14:21 -0700510
511 {
512 // RAII object for GPIO unlock / lock
William A. Kennington III2227bd52019-06-19 11:32:22 -0700513 auto locker = sensor::gpioUnlock(sensor->getGpio());
Kun Yi553552c2019-07-15 22:14:21 -0700514
Brandon Kim6d50c3e2019-08-09 15:38:53 -0700515 // For sensors with attribute ASYNC_READ_TIMEOUT,
516 // spawn a thread with timeout
Patrick Williamse8771fd2023-05-10 07:51:06 -0500517 auto asyncReadTimeout = env::getEnv("ASYNC_READ_TIMEOUT",
518 sensorSetKey);
Brandon Kim6d50c3e2019-08-09 15:38:53 -0700519 if (!asyncReadTimeout.empty())
520 {
521 std::chrono::milliseconds asyncTimeout{
522 std::stoi(asyncReadTimeout)};
523 value = sensor::asyncRead(
524 sensorSetKey, _ioAccess, asyncTimeout, _timedoutMap,
525 sensorSysfsType, sensorSysfsNum, input,
526 hwmonio::retries, hwmonio::delay);
527 }
528 else
529 {
530 // Retry for up to a second if device is busy
531 // or has a transient error.
Patrick Williamse8771fd2023-05-10 07:51:06 -0500532 value = _ioAccess->read(sensorSysfsType, sensorSysfsNum,
533 input, hwmonio::retries,
534 hwmonio::delay);
Brandon Kim6d50c3e2019-08-09 15:38:53 -0700535 }
536
Kun Yi553552c2019-07-15 22:14:21 -0700537 // Set functional property to true if we could read sensor
538 statusIface->functional(true);
539
540 value = sensor->adjustValue(value);
Carol Wang9bbe6022019-08-01 17:31:30 +0800541
542 if (input == hwmon::entry::average)
543 {
544 // Calculate the values of averageMap based on current
545 // average value, current average_interval value, previous
546 // average value, previous average_interval value
547 int64_t interval =
548 _ioAccess->read(sensorSysfsType, sensorSysfsNum,
549 hwmon::entry::caverage_interval,
550 hwmonio::retries, hwmonio::delay);
551 auto ret = _average.getAverageValue(sensorSetKey);
552 assert(ret);
553
554 const auto& [preAverage, preInterval] = *ret;
555
556 auto calValue = Average::calcAverage(
557 preAverage, preInterval, value, interval);
558 if (calValue)
559 {
560 // Update previous values in averageMap before the
561 // variable value is changed next
562 _average.setAverageValue(
563 sensorSetKey, std::make_pair(value, interval));
564 // Update value to be calculated average
565 value = calValue.value();
566 }
567 else
568 {
569 // the value of
570 // power*_average_interval is not changed yet, use the
571 // previous calculated average instead. So skip dbus
572 // update.
573 continue;
574 }
575 }
Kun Yi553552c2019-07-15 22:14:21 -0700576 }
577
578 updateSensorInterfaces(obj, value);
579 }
580 catch (const std::system_error& e)
581 {
Matt Spinlerd8cacfd2021-04-26 09:56:21 -0500582#if UPDATE_FUNCTIONAL_ON_FAIL
Kun Yi553552c2019-07-15 22:14:21 -0700583 // If UPDATE_FUNCTIONAL_ON_FAIL is defined and an exception was
584 // thrown, set the functional property to false.
585 // We cannot set this with the 'continue' in the lower block
586 // as the code may exit before reaching it.
587 statusIface->functional(false);
588#endif
Carol Wang9bbe6022019-08-01 17:31:30 +0800589 auto file = sysfs::make_sysfs_path(
590 _ioAccess->path(), sensorSysfsType, sensorSysfsNum, input);
Kun Yi553552c2019-07-15 22:14:21 -0700591
592 // Check sensorAdjusts for sensor removal RCs
Kun Yi501ade22019-07-15 15:00:32 -0700593 auto& sAdjusts = _sensorObjects[sensorSetKey]->getAdjusts();
Kun Yi553552c2019-07-15 22:14:21 -0700594 if (sAdjusts.rmRCs.count(e.code().value()) > 0)
595 {
596 // Return code found in sensor return code removal list
Kun Yi501ade22019-07-15 15:00:32 -0700597 if (_rmSensors.find(sensorSetKey) == _rmSensors.end())
Kun Yi553552c2019-07-15 22:14:21 -0700598 {
599 // Trace for sensor not already removed from dbus
600 log<level::INFO>("Remove sensor from dbus for read fail",
601 entry("FILE=%s", file.c_str()),
602 entry("RC=%d", e.code().value()));
603 // Mark this sensor to be removed from dbus
Kun Yi501ade22019-07-15 15:00:32 -0700604 _rmSensors[sensorSetKey] = attrs;
Kun Yi553552c2019-07-15 22:14:21 -0700605 }
606 continue;
607 }
Matt Spinlerd8cacfd2021-04-26 09:56:21 -0500608#if UPDATE_FUNCTIONAL_ON_FAIL
Kun Yi553552c2019-07-15 22:14:21 -0700609 // Do not exit with failure if UPDATE_FUNCTIONAL_ON_FAIL is set
610 continue;
611#endif
612 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::
613 Error;
614 report<ReadFailure>(
615 xyz::openbmc_project::Sensor::Device::ReadFailure::
616 CALLOUT_ERRNO(e.code().value()),
617 xyz::openbmc_project::Sensor::Device::ReadFailure::
618 CALLOUT_DEVICE_PATH(_devPath.c_str()));
619
Patrick Williams64129932024-02-13 21:10:17 -0600620 log<level::INFO>(std::format("Failing sysfs file: {} errno: {}",
Matt Spinler6a391de2020-07-08 13:03:10 -0500621 file, e.code().value())
622 .c_str());
Kun Yi553552c2019-07-15 22:14:21 -0700623
624 exit(EXIT_FAILURE);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500625 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600626 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500627
Patrick Venture0cd4f692019-06-21 13:39:40 -0700628 removeSensors();
629
Patrick Venture0cd4f692019-06-21 13:39:40 -0700630 addDroppedSensors();
Patrick Venture0cd4f692019-06-21 13:39:40 -0700631}
632
633void MainLoop::removeSensors()
634{
Matthew Barth8772ce32018-03-22 16:03:06 -0500635 // Remove any sensors marked for removal
Patrick Venture52b40612018-12-19 13:36:41 -0800636 for (const auto& i : _rmSensors)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600637 {
Matthew Barthd0ce7922019-06-06 09:23:37 -0500638 // Remove sensor object from dbus using emit_object_removed()
639 auto& objInfo = std::get<ObjectInfo>(_state[i.first]);
640 auto& objPath = std::get<std::string>(objInfo);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700641
Matthew Barthd0ce7922019-06-06 09:23:37 -0500642 _bus.emit_object_removed(objPath.c_str());
Patrick Venture0cd4f692019-06-21 13:39:40 -0700643
Matthew Barthd0ce7922019-06-06 09:23:37 -0500644 // Erase sensor object info
Patrick Venture52b40612018-12-19 13:36:41 -0800645 _state.erase(i.first);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500646 }
Patrick Venture0cd4f692019-06-21 13:39:40 -0700647}
Matthew Barth31d214c2018-03-26 09:54:27 -0500648
Patrick Venture0cd4f692019-06-21 13:39:40 -0700649void MainLoop::addDroppedSensors()
650{
Matthew Barth31d214c2018-03-26 09:54:27 -0500651 // Attempt to add any sensors that were removed
Patrick Venture52b40612018-12-19 13:36:41 -0800652 auto it = _rmSensors.begin();
653 while (it != _rmSensors.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500654 {
Patrick Venture52b40612018-12-19 13:36:41 -0800655 if (_state.find(it->first) == _state.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500656 {
657 SensorSet::container_t::value_type ssValueType =
Patrick Venture043d3232018-08-31 10:10:53 -0700658 std::make_pair(it->first, it->second);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700659
Matthew Barthd238e232018-04-17 12:01:50 -0500660 auto object = getObject(ssValueType);
661 if (object)
Matthew Barth31d214c2018-03-26 09:54:27 -0500662 {
Matthew Barthd238e232018-04-17 12:01:50 -0500663 // Construct the SensorSet value
664 // std::tuple<SensorSet::mapped_type,
665 // std::string(Sensor Label),
666 // ObjectInfo>
667 auto value = std::make_tuple(std::move(ssValueType.second),
668 std::move((*object).first),
669 std::move((*object).second));
670
Patrick Venture52b40612018-12-19 13:36:41 -0800671 _state[std::move(ssValueType.first)] = std::move(value);
Matthew Barthd238e232018-04-17 12:01:50 -0500672
Carol Wang9bbe6022019-08-01 17:31:30 +0800673 std::string input = hwmon::entry::input;
674 // If type is power and AVERAGE_power* is true in env, use
675 // average instead of input
676 if ((it->first.first == hwmon::type::power) &&
677 (phosphor::utility::isAverageEnvSet(it->first)))
678 {
679 input = hwmon::entry::average;
680 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500681 // Sensor object added, erase entry from removal list
Patrick Williamse8771fd2023-05-10 07:51:06 -0500682 auto file = sysfs::make_sysfs_path(_ioAccess->path(),
683 it->first.first,
684 it->first.second, input);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700685
Patrick Venture043d3232018-08-31 10:10:53 -0700686 log<level::INFO>("Added sensor to dbus after successful read",
687 entry("FILE=%s", file.c_str()));
Patrick Venture0cd4f692019-06-21 13:39:40 -0700688
Patrick Venture52b40612018-12-19 13:36:41 -0800689 it = _rmSensors.erase(it);
Matthew Barth31d214c2018-03-26 09:54:27 -0500690 }
691 else
692 {
693 ++it;
694 }
695 }
696 else
697 {
698 // Sanity check to remove sensors that were re-added
Patrick Venture52b40612018-12-19 13:36:41 -0800699 it = _rmSensors.erase(it);
Matthew Barth31d214c2018-03-26 09:54:27 -0500700 }
701 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500702}
703
704// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4