blob: 5ae0f169a7bb95053c5dc0bb148466ff36ec2d20 [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 {
115 case InterfaceType::VALUE:
116 {
117 auto& valueIface =
118 std::any_cast<std::shared_ptr<ValueObject>&>(iface.second);
119 valueIface->value(value);
120 }
121 break;
122 case InterfaceType::WARN:
123 checkThresholds<WarningObject>(iface.second, value);
124 break;
125 case InterfaceType::CRIT:
126 checkThresholds<CriticalObject>(iface.second, value);
127 break;
128 default:
129 break;
130 }
131 }
132}
133
Matthew Barth979c8062018-04-17 11:37:15 -0500134std::string MainLoop::getID(SensorSet::container_t::const_reference sensor)
Matthew Barth31d214c2018-03-26 09:54:27 -0500135{
Matthew Barth31d214c2018-03-26 09:54:27 -0500136 std::string id;
137
138 /*
139 * Check if the value of the MODE_<item><X> env variable for the sensor
Matt Spinler7c424802018-05-04 10:52:40 -0500140 * is set. If it is, then read the from the <item><X>_<mode>
Matthew Barth31d214c2018-03-26 09:54:27 -0500141 * file. The name of the DBUS object would be the value of the env
Matt Spinler7c424802018-05-04 10:52:40 -0500142 * variable LABEL_<item><mode value>. If the MODE_<item><X> env variable
Matthew Barth31d214c2018-03-26 09:54:27 -0500143 * doesn't exist, then the name of DBUS object is the value of the env
144 * variable LABEL_<item><X>.
Matt Spinler7c424802018-05-04 10:52:40 -0500145 *
146 * For example, if MODE_temp1 = "label", then code reads the temp1_label
147 * file. If it has a 5 in it, then it will use the following entry to
148 * name the object: LABEL_temp5 = "My DBus object name".
149 *
Matthew Barth31d214c2018-03-26 09:54:27 -0500150 */
Patrick Venture7a5285d2018-04-17 19:15:05 -0700151 auto mode = env::getEnv("MODE", sensor.first);
Matt Spinler7c424802018-05-04 10:52:40 -0500152 if (!mode.empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500153 {
Patrick Venture043d3232018-08-31 10:10:53 -0700154 id = env::getIndirectID(_hwmonRoot + '/' + _instance + '/', mode,
155 sensor.first);
Matthew Barth31d214c2018-03-26 09:54:27 -0500156
157 if (id.empty())
158 {
Matthew Barth979c8062018-04-17 11:37:15 -0500159 return id;
Matthew Barth31d214c2018-03-26 09:54:27 -0500160 }
161 }
162
163 // Use the ID we looked up above if there was one,
164 // otherwise use the standard one.
165 id = (id.empty()) ? sensor.first.second : id;
166
Matthew Barth979c8062018-04-17 11:37:15 -0500167 return id;
168}
169
Patrick Williams94539af2025-02-01 08:22:58 -0500170SensorIdentifiers MainLoop::getIdentifiers(
171 SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500172{
173 std::string id = getID(sensor);
174 std::string label;
George Liuc9d61612022-10-12 14:31:39 +0800175 std::string accuracy;
Lakshmi Yadlapati47fb49a2023-10-19 14:47:08 -0500176 std::string priority;
Matthew Barth979c8062018-04-17 11:37:15 -0500177
178 if (!id.empty())
179 {
180 // Ignore inputs without a label.
181 label = env::getEnv("LABEL", sensor.first.first, id);
George Liuc9d61612022-10-12 14:31:39 +0800182 accuracy = env::getEnv("ACCURACY", sensor.first.first, id);
Lakshmi Yadlapati47fb49a2023-10-19 14:47:08 -0500183 priority = env::getEnv("PRIORITY", sensor.first.first, id);
Matthew Barth979c8062018-04-17 11:37:15 -0500184 }
185
Lakshmi Yadlapati47fb49a2023-10-19 14:47:08 -0500186 return std::make_tuple(std::move(id), std::move(label), std::move(accuracy),
187 std::move(priority));
Matthew Barth979c8062018-04-17 11:37:15 -0500188}
189
190/**
191 * Reads the environment parameters of a sensor and creates an object with
Manojkiran Eda30cab622024-06-17 14:50:58 +0530192 * at least the `Value` interface, otherwise returns without creating the
193 * object. If the `Value` interface is successfully created, by reading the
194 * sensor's corresponding sysfs file's value, the additional interfaces for
195 * the sensor are created and the InterfacesAdded signal is emitted. The
196 * object's state data is then returned for sensor state monitoring within
197 * the main loop.
Matthew Barth979c8062018-04-17 11:37:15 -0500198 */
Patrick Williams94539af2025-02-01 08:22:58 -0500199std::optional<ObjectStateData> MainLoop::getObject(
200 SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500201{
202 auto properties = getIdentifiers(sensor);
203 if (std::get<sensorID>(properties).empty() ||
204 std::get<sensorLabel>(properties).empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500205 {
Matthew Barthd238e232018-04-17 12:01:50 -0500206 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500207 }
208
Patrick Venture09791852018-04-17 17:40:00 -0700209 hwmon::Attributes attrs;
210 if (!hwmon::getAttributes(sensor.first.first, attrs))
Matthew Barth31d214c2018-03-26 09:54:27 -0500211 {
Matthew Barthd238e232018-04-17 12:01:50 -0500212 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500213 }
214
Kun Yi501ade22019-07-15 15:00:32 -0700215 const auto& [sensorSetKey, sensorAttrs] = sensor;
216 const auto& [sensorSysfsType, sensorSysfsNum] = sensorSetKey;
217
Patrick Venture2864b062018-12-19 08:13:41 -0800218 /* Note: The sensor objects all share the same ioAccess object. */
Patrick Williams02e598a2024-08-16 15:21:23 -0400219 auto sensorObj =
220 std::make_unique<sensor::Sensor>(sensorSetKey, _ioAccess, _devPath);
Matthew Barth9c431062018-05-07 13:55:29 -0500221
Matthew Barthb7985272018-04-17 10:50:36 -0500222 // Get list of return codes for removing sensors on device
223 auto devRmRCs = env::getEnv("REMOVERCS");
224 // Add sensor removal return codes defined at the device level
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500225 sensorObj->addRemoveRCs(devRmRCs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500226
227 std::string objectPath{_root};
228 objectPath.append(1, '/');
Patrick Venture09791852018-04-17 17:40:00 -0700229 objectPath.append(hwmon::getNamespace(attrs));
Matthew Barth31d214c2018-03-26 09:54:27 -0500230 objectPath.append(1, '/');
Matthew Barth979c8062018-04-17 11:37:15 -0500231 objectPath.append(std::get<sensorLabel>(properties));
Matthew Barth31d214c2018-03-26 09:54:27 -0500232
Patrick Venture62067232019-06-19 17:39:33 -0700233 ObjectInfo info(&_bus, std::move(objectPath), InterfaceMap());
Patrick Venture75e56c62018-04-20 18:10:15 -0700234 RetryIO retryIO(hwmonio::retries, hwmonio::delay);
Kun Yi501ade22019-07-15 15:00:32 -0700235 if (_rmSensors.find(sensorSetKey) != _rmSensors.end())
Matthew Barthd4beecf2018-04-03 15:50:22 -0500236 {
237 // When adding a sensor that was purposely removed,
238 // don't retry on errors when reading its value
239 std::get<size_t>(retryIO) = 0;
240 }
Patrick Venture043d3232018-08-31 10:10:53 -0700241 auto valueInterface = static_cast<std::shared_ptr<ValueObject>>(nullptr);
Matthew Barth31d214c2018-03-26 09:54:27 -0500242 try
243 {
George Liuc9d61612022-10-12 14:31:39 +0800244 // Add accuracy interface
245 auto accuracyStr = std::get<sensorAccuracy>(properties);
246 try
247 {
248 if (!accuracyStr.empty())
249 {
250 auto accuracy = stod(accuracyStr);
251 sensorObj->addAccuracy(info, accuracy);
252 }
253 }
254 catch (const std::invalid_argument&)
Patrick Williamse8771fd2023-05-10 07:51:06 -0500255 {}
George Liuc9d61612022-10-12 14:31:39 +0800256
Lakshmi Yadlapati47fb49a2023-10-19 14:47:08 -0500257 // Add priority interface
258 auto priorityStr = std::get<sensorPriority>(properties);
259 try
260 {
261 if (!priorityStr.empty())
262 {
263 auto priority = std::stoul(priorityStr);
264 sensorObj->addPriority(info, priority);
265 }
266 }
267 catch (const std::invalid_argument&)
268 {}
269
Matthew Barthca44c2e2018-04-24 15:33:25 -0500270 // Add status interface based on _fault file being present
Matthew Barth2e41b132018-05-07 14:15:45 -0500271 sensorObj->addStatus(info);
Brandon Kim6d50c3e2019-08-09 15:38:53 -0700272 valueInterface = sensorObj->addValue(retryIO, info, _timedoutMap);
Matthew Barth31d214c2018-03-26 09:54:27 -0500273 }
274 catch (const std::system_error& e)
275 {
Patrick Williams02e598a2024-08-16 15:21:23 -0400276 auto file =
277 sysfs::make_sysfs_path(_ioAccess->path(), sensorSysfsType,
278 sensorSysfsNum, hwmon::entry::cinput);
Patrick Venture0892c3f2019-06-27 14:24:03 -0700279
Matthew Barth31d214c2018-03-26 09:54:27 -0500280 // Check sensorAdjusts for sensor removal RCs
Matthew Barthac473092018-05-07 14:41:46 -0500281 auto& sAdjusts = sensorObj->getAdjusts();
282 if (sAdjusts.rmRCs.count(e.code().value()) > 0)
Matthew Barth31d214c2018-03-26 09:54:27 -0500283 {
Matthew Barthac473092018-05-07 14:41:46 -0500284 // Return code found in sensor return code removal list
Kun Yi501ade22019-07-15 15:00:32 -0700285 if (_rmSensors.find(sensorSetKey) == _rmSensors.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500286 {
Matthew Barthac473092018-05-07 14:41:46 -0500287 // Trace for sensor not already removed from dbus
288 log<level::INFO>("Sensor not added to dbus for read fail",
Patrick Venture043d3232018-08-31 10:10:53 -0700289 entry("FILE=%s", file.c_str()),
290 entry("RC=%d", e.code().value()));
Kun Yi501ade22019-07-15 15:00:32 -0700291 _rmSensors[std::move(sensorSetKey)] = std::move(sensorAttrs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500292 }
Matthew Barthac473092018-05-07 14:41:46 -0500293 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500294 }
Patrick Venture0892c3f2019-06-27 14:24:03 -0700295
Patrick Venture043d3232018-08-31 10:10:53 -0700296 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error;
Matthew Barth31d214c2018-03-26 09:54:27 -0500297 report<ReadFailure>(
Patrick Venture043d3232018-08-31 10:10:53 -0700298 xyz::openbmc_project::Sensor::Device::ReadFailure::CALLOUT_ERRNO(
299 e.code().value()),
300 xyz::openbmc_project::Sensor::Device::ReadFailure::
301 CALLOUT_DEVICE_PATH(_devPath.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500302
Patrick Williams64129932024-02-13 21:10:17 -0600303 log<level::INFO>(std::format("Failing sysfs file: {} errno: {}", file,
Matt Spinler6a391de2020-07-08 13:03:10 -0500304 e.code().value())
305 .c_str());
Matthew Barth31d214c2018-03-26 09:54:27 -0500306 exit(EXIT_FAILURE);
Matthew Barth31d214c2018-03-26 09:54:27 -0500307 }
308 auto sensorValue = valueInterface->value();
Matt Spinlerecac0ae2020-07-08 13:09:08 -0500309 int64_t scale = sensorObj->getScale();
310
Kun Yi501ade22019-07-15 15:00:32 -0700311 addThreshold<WarningObject>(sensorSysfsType, std::get<sensorID>(properties),
312 sensorValue, info, scale);
313 addThreshold<CriticalObject>(sensorSysfsType,
James Feistee73f5b2018-08-01 16:31:42 -0700314 std::get<sensorID>(properties), sensorValue,
315 info, scale);
Matthew Barth31d214c2018-03-26 09:54:27 -0500316
Patrick Williams02e598a2024-08-16 15:21:23 -0400317 auto target =
318 addTarget<hwmon::FanSpeed>(sensorSetKey, _ioAccess, _devPath, info);
Matthew Barth28f8e662018-03-26 16:57:36 -0500319 if (target)
Matthew Barth31d214c2018-03-26 09:54:27 -0500320 {
Matthew Barth28f8e662018-03-26 16:57:36 -0500321 target->enable();
Matthew Barth31d214c2018-03-26 09:54:27 -0500322 }
Kun Yi501ade22019-07-15 15:00:32 -0700323 addTarget<hwmon::FanPwm>(sensorSetKey, _ioAccess, _devPath, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500324
325 // All the interfaces have been created. Go ahead
326 // and emit InterfacesAdded.
327 valueInterface->emit_object_added();
328
Matthew Barth9c431062018-05-07 13:55:29 -0500329 // Save sensor object specifications
Kun Yi501ade22019-07-15 15:00:32 -0700330 _sensorObjects[sensorSetKey] = std::move(sensorObj);
Matthew Barth9c431062018-05-07 13:55:29 -0500331
Matthew Barthd238e232018-04-17 12:01:50 -0500332 return std::make_pair(std::move(std::get<sensorLabel>(properties)),
333 std::move(info));
Matthew Barth31d214c2018-03-26 09:54:27 -0500334}
335
Patrick Williamsad6043f2022-07-22 19:26:56 -0500336MainLoop::MainLoop(sdbusplus::bus_t&& bus, const std::string& param,
Patrick Venture043d3232018-08-31 10:10:53 -0700337 const std::string& path, const std::string& devPath,
Patrick Venture16805a62019-06-21 14:19:33 -0700338 const char* prefix, const char* root,
Anton D. Kachalovd46d0812021-02-03 23:25:37 +0100339 const std::string& instanceId,
Patrick Venture16805a62019-06-21 14:19:33 -0700340 const hwmonio::HwmonIOInterface* ioIntf) :
Patrick Williams02e598a2024-08-16 15:21:23 -0400341 _bus(std::move(bus)), _manager(_bus, root), _pathParam(param), _hwmonRoot(),
342 _instance(), _devPath(devPath), _prefix(prefix), _root(root), _state(),
Anton D. Kachalovd46d0812021-02-03 23:25:37 +0100343 _instanceId(instanceId), _ioAccess(ioIntf),
344 _event(sdeventplus::Event::get_default()),
Patrick Venture52b40612018-12-19 13:36:41 -0800345 _timer(_event, std::bind(&MainLoop::read, this))
Brad Bishopd499ca62016-12-19 09:24:50 -0500346{
Patrick Venture73a50c72018-04-17 15:19:03 -0700347 // Strip off any trailing slashes.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500348 std::string p = path;
349 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500350 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500351 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500352 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500353
Patrick Venture73a50c72018-04-17 15:19:03 -0700354 // Given the furthest right /, set instance to
355 // the basename, and hwmonRoot to the leading path.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500356 auto n = p.rfind('/');
357 if (n != std::string::npos)
358 {
359 _instance.assign(p.substr(n + 1));
360 _hwmonRoot.assign(p.substr(0, n));
361 }
362
363 assert(!_instance.empty());
364 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500365}
366
367void MainLoop::shutdown() noexcept
368{
Patrick Venture52b40612018-12-19 13:36:41 -0800369 _event.exit(0);
Brad Bishopd499ca62016-12-19 09:24:50 -0500370}
371
372void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500373{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600374 init();
375
Patrick Venture043d3232018-08-31 10:10:53 -0700376 std::function<void()> callback(std::bind(&MainLoop::read, this));
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600377 try
378 {
Patrick Venture52b40612018-12-19 13:36:41 -0800379 _timer.restart(std::chrono::microseconds(_interval));
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600380
381 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
382
383 // TODO: Issue#7 - Should probably periodically check the SensorSet
384 // for new entries.
385
Patrick Venture52b40612018-12-19 13:36:41 -0800386 _bus.attach_event(_event.get(), SD_EVENT_PRIORITY_IMPORTANT);
387 _event.loop();
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600388 }
William A. Kennington III0fe4cb32018-10-18 19:19:58 -0700389 catch (const std::exception& e)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600390 {
391 log<level::ERR>("Error in sysfs polling loop",
392 entry("ERROR=%s", e.what()));
393 throw;
394 }
395}
396
397void MainLoop::init()
398{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500399 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500400 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500401
Patrick Venturedb7ecb62018-10-23 19:42:23 -0700402 for (const auto& i : *sensors)
Brad Bishop75b4ab82017-01-06 09:33:50 -0500403 {
Matthew Barthd238e232018-04-17 12:01:50 -0500404 auto object = getObject(i);
405 if (object)
406 {
407 // Construct the SensorSet value
408 // std::tuple<SensorSet::mapped_type,
409 // std::string(Sensor Label),
410 // ObjectInfo>
Patrick Williams02e598a2024-08-16 15:21:23 -0400411 auto value =
412 std::make_tuple(std::move(i.second), std::move((*object).first),
413 std::move((*object).second));
Matthew Barthd238e232018-04-17 12:01:50 -0500414
Patrick Venture52b40612018-12-19 13:36:41 -0800415 _state[std::move(i.first)] = std::move(value);
Matthew Barthd238e232018-04-17 12:01:50 -0500416 }
Carol Wang9bbe6022019-08-01 17:31:30 +0800417
418 // Initialize _averageMap of sensor. e.g. <<power, 1>, <0, 0>>
419 if ((i.first.first == hwmon::type::power) &&
420 (phosphor::utility::isAverageEnvSet(i.first)))
421 {
422 _average.setAverageValue(i.first, std::make_pair(0, 0));
423 }
Brad Bishop75b4ab82017-01-06 09:33:50 -0500424 }
425
Patrick Venture62503a42017-05-23 07:30:29 -0700426 /* If there are no sensors specified by labels, exit. */
Patrick Venture52b40612018-12-19 13:36:41 -0800427 if (0 == _state.size())
Patrick Venture62503a42017-05-23 07:30:29 -0700428 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600429 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700430 }
431
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500432 {
Patrick Venturec897d8b2018-04-23 19:01:56 -0700433 std::stringstream ss;
Anton D. Kachalovd46d0812021-02-03 23:25:37 +0100434 std::string id = _instanceId;
435 if (id.empty())
436 {
437 id =
438 std::to_string(std::hash<std::string>{}(_devPath + _pathParam));
439 }
440 ss << _prefix << "-" << id << ".Hwmon1";
Patrick Venturec897d8b2018-04-23 19:01:56 -0700441
442 _bus.request_name(ss.str().c_str());
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500443 }
444
Patrick Ventureab10f162017-05-22 09:44:50 -0700445 {
Patrick Venturea24c8802018-04-17 19:38:06 -0700446 auto interval = env::getEnv("INTERVAL");
447 if (!interval.empty())
Patrick Ventureab10f162017-05-22 09:44:50 -0700448 {
Patrick Venture50cf1c52018-04-18 09:21:41 -0700449 _interval = std::strtoull(interval.c_str(), NULL, 10);
Patrick Ventureab10f162017-05-22 09:44:50 -0700450 }
451 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600452}
Patrick Ventureab10f162017-05-22 09:44:50 -0700453
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600454void MainLoop::read()
455{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500456 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
457 // ensure the objects all exist?
458
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600459 // Iterate through all the sensors.
Kun Yi501ade22019-07-15 15:00:32 -0700460 for (auto& [sensorSetKey, sensorStateTuple] : _state)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600461 {
Kun Yi501ade22019-07-15 15:00:32 -0700462 const auto& [sensorSysfsType, sensorSysfsNum] = sensorSetKey;
463 auto& [attrs, unused, objInfo] = sensorStateTuple;
464
Kun Yi553552c2019-07-15 22:14:21 -0700465 if (attrs.find(hwmon::entry::input) == attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500466 {
Kun Yi553552c2019-07-15 22:14:21 -0700467 continue;
468 }
Kun Yi501ade22019-07-15 15:00:32 -0700469
Kun Yi553552c2019-07-15 22:14:21 -0700470 // Read value from sensor.
Carol Wang9bbe6022019-08-01 17:31:30 +0800471 std::string input = hwmon::entry::input;
472 if (sensorSysfsType == hwmon::type::pwm)
Kun Yi553552c2019-07-15 22:14:21 -0700473 {
474 input = "";
475 }
Carol Wang9bbe6022019-08-01 17:31:30 +0800476 // If type is power and AVERAGE_power* is true in env, use average
477 // instead of input
478 else if ((sensorSysfsType == hwmon::type::power) &&
479 (phosphor::utility::isAverageEnvSet(sensorSetKey)))
480 {
481 input = hwmon::entry::average;
482 }
Kun Yi553552c2019-07-15 22:14:21 -0700483
Matt Spinlerecac0ae2020-07-08 13:09:08 -0500484 SensorValueType value;
Kun Yi553552c2019-07-15 22:14:21 -0700485 auto& obj = std::get<InterfaceMap>(objInfo);
Kun Yi501ade22019-07-15 15:00:32 -0700486 std::unique_ptr<sensor::Sensor>& sensor = _sensorObjects[sensorSetKey];
Kun Yi553552c2019-07-15 22:14:21 -0700487
488 auto& statusIface = std::any_cast<std::shared_ptr<StatusObject>&>(
489 obj[InterfaceType::STATUS]);
490 // As long as addStatus is called before addValue, statusIface
491 // should never be nullptr.
492 assert(statusIface);
493
494 try
495 {
496 if (sensor->hasFaultFile())
Patrick Venture043d3232018-08-31 10:10:53 -0700497 {
Kun Yi501ade22019-07-15 15:00:32 -0700498 auto fault = _ioAccess->read(sensorSysfsType, sensorSysfsNum,
Kun Yi553552c2019-07-15 22:14:21 -0700499 hwmon::entry::fault,
500 hwmonio::retries, hwmonio::delay);
501 // Skip reading from a sensor with a valid fault file
502 // and set the functional property accordingly
503 if (!statusIface->functional((fault == 0) ? true : false))
Matthew Barth27c4a392018-04-25 14:38:51 -0500504 {
Matthew Barthac473092018-05-07 14:41:46 -0500505 continue;
Matthew Barth8772ce32018-03-22 16:03:06 -0500506 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500507 }
Kun Yi553552c2019-07-15 22:14:21 -0700508
509 {
510 // RAII object for GPIO unlock / lock
William A. Kennington III2227bd52019-06-19 11:32:22 -0700511 auto locker = sensor::gpioUnlock(sensor->getGpio());
Kun Yi553552c2019-07-15 22:14:21 -0700512
Brandon Kim6d50c3e2019-08-09 15:38:53 -0700513 // For sensors with attribute ASYNC_READ_TIMEOUT,
514 // spawn a thread with timeout
Patrick Williams02e598a2024-08-16 15:21:23 -0400515 auto asyncReadTimeout =
516 env::getEnv("ASYNC_READ_TIMEOUT", sensorSetKey);
Brandon Kim6d50c3e2019-08-09 15:38:53 -0700517 if (!asyncReadTimeout.empty())
518 {
519 std::chrono::milliseconds asyncTimeout{
520 std::stoi(asyncReadTimeout)};
521 value = sensor::asyncRead(
522 sensorSetKey, _ioAccess, asyncTimeout, _timedoutMap,
523 sensorSysfsType, sensorSysfsNum, input,
524 hwmonio::retries, hwmonio::delay);
525 }
526 else
527 {
528 // Retry for up to a second if device is busy
529 // or has a transient error.
Patrick Williams02e598a2024-08-16 15:21:23 -0400530 value =
531 _ioAccess->read(sensorSysfsType, sensorSysfsNum, input,
532 hwmonio::retries, hwmonio::delay);
Brandon Kim6d50c3e2019-08-09 15:38:53 -0700533 }
534
Kun Yi553552c2019-07-15 22:14:21 -0700535 // Set functional property to true if we could read sensor
536 statusIface->functional(true);
537
538 value = sensor->adjustValue(value);
Carol Wang9bbe6022019-08-01 17:31:30 +0800539
540 if (input == hwmon::entry::average)
541 {
542 // Calculate the values of averageMap based on current
543 // average value, current average_interval value, previous
544 // average value, previous average_interval value
545 int64_t interval =
546 _ioAccess->read(sensorSysfsType, sensorSysfsNum,
547 hwmon::entry::caverage_interval,
548 hwmonio::retries, hwmonio::delay);
549 auto ret = _average.getAverageValue(sensorSetKey);
550 assert(ret);
551
552 const auto& [preAverage, preInterval] = *ret;
553
554 auto calValue = Average::calcAverage(
555 preAverage, preInterval, value, interval);
556 if (calValue)
557 {
558 // Update previous values in averageMap before the
559 // variable value is changed next
560 _average.setAverageValue(
561 sensorSetKey, std::make_pair(value, interval));
562 // Update value to be calculated average
563 value = calValue.value();
564 }
565 else
566 {
567 // the value of
568 // power*_average_interval is not changed yet, use the
569 // previous calculated average instead. So skip dbus
570 // update.
571 continue;
572 }
573 }
Kun Yi553552c2019-07-15 22:14:21 -0700574 }
575
576 updateSensorInterfaces(obj, value);
577 }
578 catch (const std::system_error& e)
579 {
Matt Spinlerd8cacfd2021-04-26 09:56:21 -0500580#if UPDATE_FUNCTIONAL_ON_FAIL
Kun Yi553552c2019-07-15 22:14:21 -0700581 // If UPDATE_FUNCTIONAL_ON_FAIL is defined and an exception was
582 // thrown, set the functional property to false.
583 // We cannot set this with the 'continue' in the lower block
584 // as the code may exit before reaching it.
585 statusIface->functional(false);
586#endif
Carol Wang9bbe6022019-08-01 17:31:30 +0800587 auto file = sysfs::make_sysfs_path(
588 _ioAccess->path(), sensorSysfsType, sensorSysfsNum, input);
Kun Yi553552c2019-07-15 22:14:21 -0700589
590 // Check sensorAdjusts for sensor removal RCs
Kun Yi501ade22019-07-15 15:00:32 -0700591 auto& sAdjusts = _sensorObjects[sensorSetKey]->getAdjusts();
Kun Yi553552c2019-07-15 22:14:21 -0700592 if (sAdjusts.rmRCs.count(e.code().value()) > 0)
593 {
594 // Return code found in sensor return code removal list
Kun Yi501ade22019-07-15 15:00:32 -0700595 if (_rmSensors.find(sensorSetKey) == _rmSensors.end())
Kun Yi553552c2019-07-15 22:14:21 -0700596 {
597 // Trace for sensor not already removed from dbus
598 log<level::INFO>("Remove sensor from dbus for read fail",
599 entry("FILE=%s", file.c_str()),
600 entry("RC=%d", e.code().value()));
601 // Mark this sensor to be removed from dbus
Kun Yi501ade22019-07-15 15:00:32 -0700602 _rmSensors[sensorSetKey] = attrs;
Kun Yi553552c2019-07-15 22:14:21 -0700603 }
604 continue;
605 }
Matt Spinlerd8cacfd2021-04-26 09:56:21 -0500606#if UPDATE_FUNCTIONAL_ON_FAIL
Kun Yi553552c2019-07-15 22:14:21 -0700607 // Do not exit with failure if UPDATE_FUNCTIONAL_ON_FAIL is set
608 continue;
609#endif
610 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::
611 Error;
612 report<ReadFailure>(
613 xyz::openbmc_project::Sensor::Device::ReadFailure::
614 CALLOUT_ERRNO(e.code().value()),
615 xyz::openbmc_project::Sensor::Device::ReadFailure::
616 CALLOUT_DEVICE_PATH(_devPath.c_str()));
617
Patrick Williams64129932024-02-13 21:10:17 -0600618 log<level::INFO>(std::format("Failing sysfs file: {} errno: {}",
Matt Spinler6a391de2020-07-08 13:03:10 -0500619 file, e.code().value())
620 .c_str());
Kun Yi553552c2019-07-15 22:14:21 -0700621
622 exit(EXIT_FAILURE);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500623 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600624 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500625
Patrick Venture0cd4f692019-06-21 13:39:40 -0700626 removeSensors();
627
Patrick Venture0cd4f692019-06-21 13:39:40 -0700628 addDroppedSensors();
Patrick Venture0cd4f692019-06-21 13:39:40 -0700629}
630
631void MainLoop::removeSensors()
632{
Matthew Barth8772ce32018-03-22 16:03:06 -0500633 // Remove any sensors marked for removal
Patrick Venture52b40612018-12-19 13:36:41 -0800634 for (const auto& i : _rmSensors)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600635 {
Matthew Barthd0ce7922019-06-06 09:23:37 -0500636 // Remove sensor object from dbus using emit_object_removed()
637 auto& objInfo = std::get<ObjectInfo>(_state[i.first]);
638 auto& objPath = std::get<std::string>(objInfo);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700639
Matthew Barthd0ce7922019-06-06 09:23:37 -0500640 _bus.emit_object_removed(objPath.c_str());
Patrick Venture0cd4f692019-06-21 13:39:40 -0700641
Matthew Barthd0ce7922019-06-06 09:23:37 -0500642 // Erase sensor object info
Patrick Venture52b40612018-12-19 13:36:41 -0800643 _state.erase(i.first);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500644 }
Patrick Venture0cd4f692019-06-21 13:39:40 -0700645}
Matthew Barth31d214c2018-03-26 09:54:27 -0500646
Patrick Venture0cd4f692019-06-21 13:39:40 -0700647void MainLoop::addDroppedSensors()
648{
Matthew Barth31d214c2018-03-26 09:54:27 -0500649 // Attempt to add any sensors that were removed
Patrick Venture52b40612018-12-19 13:36:41 -0800650 auto it = _rmSensors.begin();
651 while (it != _rmSensors.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500652 {
Patrick Venture52b40612018-12-19 13:36:41 -0800653 if (_state.find(it->first) == _state.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500654 {
655 SensorSet::container_t::value_type ssValueType =
Patrick Venture043d3232018-08-31 10:10:53 -0700656 std::make_pair(it->first, it->second);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700657
Matthew Barthd238e232018-04-17 12:01:50 -0500658 auto object = getObject(ssValueType);
659 if (object)
Matthew Barth31d214c2018-03-26 09:54:27 -0500660 {
Matthew Barthd238e232018-04-17 12:01:50 -0500661 // Construct the SensorSet value
662 // std::tuple<SensorSet::mapped_type,
663 // std::string(Sensor Label),
664 // ObjectInfo>
665 auto value = std::make_tuple(std::move(ssValueType.second),
666 std::move((*object).first),
667 std::move((*object).second));
668
Patrick Venture52b40612018-12-19 13:36:41 -0800669 _state[std::move(ssValueType.first)] = std::move(value);
Matthew Barthd238e232018-04-17 12:01:50 -0500670
Carol Wang9bbe6022019-08-01 17:31:30 +0800671 std::string input = hwmon::entry::input;
672 // If type is power and AVERAGE_power* is true in env, use
673 // average instead of input
674 if ((it->first.first == hwmon::type::power) &&
675 (phosphor::utility::isAverageEnvSet(it->first)))
676 {
677 input = hwmon::entry::average;
678 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500679 // Sensor object added, erase entry from removal list
Patrick Williams02e598a2024-08-16 15:21:23 -0400680 auto file =
681 sysfs::make_sysfs_path(_ioAccess->path(), it->first.first,
682 it->first.second, input);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700683
Patrick Venture043d3232018-08-31 10:10:53 -0700684 log<level::INFO>("Added sensor to dbus after successful read",
685 entry("FILE=%s", file.c_str()));
Patrick Venture0cd4f692019-06-21 13:39:40 -0700686
Patrick Venture52b40612018-12-19 13:36:41 -0800687 it = _rmSensors.erase(it);
Matthew Barth31d214c2018-03-26 09:54:27 -0500688 }
689 else
690 {
691 ++it;
692 }
693 }
694 else
695 {
696 // Sanity check to remove sensors that were re-added
Patrick Venture52b40612018-12-19 13:36:41 -0800697 it = _rmSensors.erase(it);
Matthew Barth31d214c2018-03-26 09:54:27 -0500698 }
699 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500700}