blob: 3e7e0bfd7fab8f61db390b7c58e4bd521300ed7f [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;
62
63// Initialization for Critical Objects
64decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo =
65 &CriticalObject::criticalLow;
66decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi =
67 &CriticalObject::criticalHigh;
68decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo =
69 &CriticalObject::criticalLow;
70decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi =
71 &CriticalObject::criticalHigh;
Patrick Venture043d3232018-08-31 10:10:53 -070072decltype(
73 Thresholds<CriticalObject>::alarmLo) Thresholds<CriticalObject>::alarmLo =
Saqib Khan973886d2017-03-15 14:01:16 -050074 &CriticalObject::criticalAlarmLow;
Patrick Venture043d3232018-08-31 10:10:53 -070075decltype(
76 Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi =
Saqib Khan973886d2017-03-15 14:01:16 -050077 &CriticalObject::criticalAlarmHigh;
78
Matt Spinlerecac0ae2020-07-08 13:09:08 -050079void updateSensorInterfaces(InterfaceMap& ifaces, SensorValueType value)
Patrick Venturefeb744a2019-06-26 19:07:48 -070080{
81 for (auto& iface : ifaces)
82 {
83 switch (iface.first)
84 {
Kun Yi94a04c42019-08-21 09:43:20 -070085 // clang-format off
Patrick Venturefeb744a2019-06-26 19:07:48 -070086 case InterfaceType::VALUE:
87 {
88 auto& valueIface =
89 std::any_cast<std::shared_ptr<ValueObject>&>(iface.second);
90 valueIface->value(value);
91 }
92 break;
Kun Yi94a04c42019-08-21 09:43:20 -070093 // clang-format on
Patrick Venturefeb744a2019-06-26 19:07:48 -070094 case InterfaceType::WARN:
95 checkThresholds<WarningObject>(iface.second, value);
96 break;
97 case InterfaceType::CRIT:
98 checkThresholds<CriticalObject>(iface.second, value);
99 break;
100 default:
101 break;
102 }
103 }
104}
105
Matthew Barth979c8062018-04-17 11:37:15 -0500106std::string MainLoop::getID(SensorSet::container_t::const_reference sensor)
Matthew Barth31d214c2018-03-26 09:54:27 -0500107{
Matthew Barth31d214c2018-03-26 09:54:27 -0500108 std::string id;
109
110 /*
111 * Check if the value of the MODE_<item><X> env variable for the sensor
Matt Spinler7c424802018-05-04 10:52:40 -0500112 * is set. If it is, then read the from the <item><X>_<mode>
Matthew Barth31d214c2018-03-26 09:54:27 -0500113 * file. The name of the DBUS object would be the value of the env
Matt Spinler7c424802018-05-04 10:52:40 -0500114 * variable LABEL_<item><mode value>. If the MODE_<item><X> env variable
Matthew Barth31d214c2018-03-26 09:54:27 -0500115 * doesn't exist, then the name of DBUS object is the value of the env
116 * variable LABEL_<item><X>.
Matt Spinler7c424802018-05-04 10:52:40 -0500117 *
118 * For example, if MODE_temp1 = "label", then code reads the temp1_label
119 * file. If it has a 5 in it, then it will use the following entry to
120 * name the object: LABEL_temp5 = "My DBus object name".
121 *
Matthew Barth31d214c2018-03-26 09:54:27 -0500122 */
Patrick Venture7a5285d2018-04-17 19:15:05 -0700123 auto mode = env::getEnv("MODE", sensor.first);
Matt Spinler7c424802018-05-04 10:52:40 -0500124 if (!mode.empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500125 {
Patrick Venture043d3232018-08-31 10:10:53 -0700126 id = env::getIndirectID(_hwmonRoot + '/' + _instance + '/', mode,
127 sensor.first);
Matthew Barth31d214c2018-03-26 09:54:27 -0500128
129 if (id.empty())
130 {
Matthew Barth979c8062018-04-17 11:37:15 -0500131 return id;
Matthew Barth31d214c2018-03-26 09:54:27 -0500132 }
133 }
134
135 // Use the ID we looked up above if there was one,
136 // otherwise use the standard one.
137 id = (id.empty()) ? sensor.first.second : id;
138
Matthew Barth979c8062018-04-17 11:37:15 -0500139 return id;
140}
141
Patrick Venture043d3232018-08-31 10:10:53 -0700142SensorIdentifiers
143 MainLoop::getIdentifiers(SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500144{
145 std::string id = getID(sensor);
146 std::string label;
147
148 if (!id.empty())
149 {
150 // Ignore inputs without a label.
151 label = env::getEnv("LABEL", sensor.first.first, id);
152 }
153
Patrick Venture043d3232018-08-31 10:10:53 -0700154 return std::make_tuple(std::move(id), std::move(label));
Matthew Barth979c8062018-04-17 11:37:15 -0500155}
156
157/**
158 * Reads the environment parameters of a sensor and creates an object with
159 * atleast the `Value` interface, otherwise returns without creating the object.
160 * If the `Value` interface is successfully created, by reading the sensor's
161 * corresponding sysfs file's value, the additional interfaces for the sensor
Matthew Barthd238e232018-04-17 12:01:50 -0500162 * are created and the InterfacesAdded signal is emitted. The object's state
163 * data is then returned for sensor state monitoring within the main loop.
Matthew Barth979c8062018-04-17 11:37:15 -0500164 */
William A. Kennington III4cbdfef2018-10-18 19:19:51 -0700165std::optional<ObjectStateData>
Patrick Venture043d3232018-08-31 10:10:53 -0700166 MainLoop::getObject(SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500167{
168 auto properties = getIdentifiers(sensor);
169 if (std::get<sensorID>(properties).empty() ||
170 std::get<sensorLabel>(properties).empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500171 {
Matthew Barthd238e232018-04-17 12:01:50 -0500172 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500173 }
174
Patrick Venture09791852018-04-17 17:40:00 -0700175 hwmon::Attributes attrs;
176 if (!hwmon::getAttributes(sensor.first.first, attrs))
Matthew Barth31d214c2018-03-26 09:54:27 -0500177 {
Matthew Barthd238e232018-04-17 12:01:50 -0500178 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500179 }
180
Kun Yi501ade22019-07-15 15:00:32 -0700181 const auto& [sensorSetKey, sensorAttrs] = sensor;
182 const auto& [sensorSysfsType, sensorSysfsNum] = sensorSetKey;
183
Patrick Venture2864b062018-12-19 08:13:41 -0800184 /* Note: The sensor objects all share the same ioAccess object. */
Patrick Venture043d3232018-08-31 10:10:53 -0700185 auto sensorObj =
Kun Yi501ade22019-07-15 15:00:32 -0700186 std::make_unique<sensor::Sensor>(sensorSetKey, _ioAccess, _devPath);
Matthew Barth9c431062018-05-07 13:55:29 -0500187
Matthew Barthb7985272018-04-17 10:50:36 -0500188 // Get list of return codes for removing sensors on device
189 auto devRmRCs = env::getEnv("REMOVERCS");
190 // Add sensor removal return codes defined at the device level
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500191 sensorObj->addRemoveRCs(devRmRCs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500192
193 std::string objectPath{_root};
194 objectPath.append(1, '/');
Patrick Venture09791852018-04-17 17:40:00 -0700195 objectPath.append(hwmon::getNamespace(attrs));
Matthew Barth31d214c2018-03-26 09:54:27 -0500196 objectPath.append(1, '/');
Matthew Barth979c8062018-04-17 11:37:15 -0500197 objectPath.append(std::get<sensorLabel>(properties));
Matthew Barth31d214c2018-03-26 09:54:27 -0500198
Patrick Venture62067232019-06-19 17:39:33 -0700199 ObjectInfo info(&_bus, std::move(objectPath), InterfaceMap());
Patrick Venture75e56c62018-04-20 18:10:15 -0700200 RetryIO retryIO(hwmonio::retries, hwmonio::delay);
Kun Yi501ade22019-07-15 15:00:32 -0700201 if (_rmSensors.find(sensorSetKey) != _rmSensors.end())
Matthew Barthd4beecf2018-04-03 15:50:22 -0500202 {
203 // When adding a sensor that was purposely removed,
204 // don't retry on errors when reading its value
205 std::get<size_t>(retryIO) = 0;
206 }
Patrick Venture043d3232018-08-31 10:10:53 -0700207 auto valueInterface = static_cast<std::shared_ptr<ValueObject>>(nullptr);
Matthew Barth31d214c2018-03-26 09:54:27 -0500208 try
209 {
Matthew Barthca44c2e2018-04-24 15:33:25 -0500210 // Add status interface based on _fault file being present
Matthew Barth2e41b132018-05-07 14:15:45 -0500211 sensorObj->addStatus(info);
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500212 valueInterface = sensorObj->addValue(retryIO, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500213 }
214 catch (const std::system_error& e)
215 {
Patrick Venture043d3232018-08-31 10:10:53 -0700216 auto file =
Kun Yi501ade22019-07-15 15:00:32 -0700217 sysfs::make_sysfs_path(_ioAccess->path(), sensorSysfsType,
218 sensorSysfsNum, hwmon::entry::cinput);
Patrick Venture0892c3f2019-06-27 14:24:03 -0700219
Matthew Barth31d214c2018-03-26 09:54:27 -0500220 // Check sensorAdjusts for sensor removal RCs
Matthew Barthac473092018-05-07 14:41:46 -0500221 auto& sAdjusts = sensorObj->getAdjusts();
222 if (sAdjusts.rmRCs.count(e.code().value()) > 0)
Matthew Barth31d214c2018-03-26 09:54:27 -0500223 {
Matthew Barthac473092018-05-07 14:41:46 -0500224 // Return code found in sensor return code removal list
Kun Yi501ade22019-07-15 15:00:32 -0700225 if (_rmSensors.find(sensorSetKey) == _rmSensors.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500226 {
Matthew Barthac473092018-05-07 14:41:46 -0500227 // Trace for sensor not already removed from dbus
228 log<level::INFO>("Sensor not added to dbus for read fail",
Patrick Venture043d3232018-08-31 10:10:53 -0700229 entry("FILE=%s", file.c_str()),
230 entry("RC=%d", e.code().value()));
Kun Yi501ade22019-07-15 15:00:32 -0700231 _rmSensors[std::move(sensorSetKey)] = std::move(sensorAttrs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500232 }
Matthew Barthac473092018-05-07 14:41:46 -0500233 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500234 }
Patrick Venture0892c3f2019-06-27 14:24:03 -0700235
Patrick Venture043d3232018-08-31 10:10:53 -0700236 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error;
Matthew Barth31d214c2018-03-26 09:54:27 -0500237 report<ReadFailure>(
Patrick Venture043d3232018-08-31 10:10:53 -0700238 xyz::openbmc_project::Sensor::Device::ReadFailure::CALLOUT_ERRNO(
239 e.code().value()),
240 xyz::openbmc_project::Sensor::Device::ReadFailure::
241 CALLOUT_DEVICE_PATH(_devPath.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500242
Matt Spinler6a391de2020-07-08 13:03:10 -0500243 log<level::INFO>(fmt::format("Failing sysfs file: {} errno: {}", file,
244 e.code().value())
245 .c_str());
Matthew Barth31d214c2018-03-26 09:54:27 -0500246 exit(EXIT_FAILURE);
Matthew Barth31d214c2018-03-26 09:54:27 -0500247 }
248 auto sensorValue = valueInterface->value();
Matt Spinlerecac0ae2020-07-08 13:09:08 -0500249 int64_t scale = sensorObj->getScale();
250
Kun Yi501ade22019-07-15 15:00:32 -0700251 addThreshold<WarningObject>(sensorSysfsType, std::get<sensorID>(properties),
252 sensorValue, info, scale);
253 addThreshold<CriticalObject>(sensorSysfsType,
James Feistee73f5b2018-08-01 16:31:42 -0700254 std::get<sensorID>(properties), sensorValue,
255 info, scale);
Matthew Barth31d214c2018-03-26 09:54:27 -0500256
Patrick Venture043d3232018-08-31 10:10:53 -0700257 auto target =
Kun Yi501ade22019-07-15 15:00:32 -0700258 addTarget<hwmon::FanSpeed>(sensorSetKey, _ioAccess, _devPath, info);
Matthew Barth28f8e662018-03-26 16:57:36 -0500259 if (target)
Matthew Barth31d214c2018-03-26 09:54:27 -0500260 {
Matthew Barth28f8e662018-03-26 16:57:36 -0500261 target->enable();
Matthew Barth31d214c2018-03-26 09:54:27 -0500262 }
Kun Yi501ade22019-07-15 15:00:32 -0700263 addTarget<hwmon::FanPwm>(sensorSetKey, _ioAccess, _devPath, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500264
265 // All the interfaces have been created. Go ahead
266 // and emit InterfacesAdded.
267 valueInterface->emit_object_added();
268
Matthew Barth9c431062018-05-07 13:55:29 -0500269 // Save sensor object specifications
Kun Yi501ade22019-07-15 15:00:32 -0700270 _sensorObjects[sensorSetKey] = std::move(sensorObj);
Matthew Barth9c431062018-05-07 13:55:29 -0500271
Matthew Barthd238e232018-04-17 12:01:50 -0500272 return std::make_pair(std::move(std::get<sensorLabel>(properties)),
273 std::move(info));
Matthew Barth31d214c2018-03-26 09:54:27 -0500274}
275
Patrick Venture043d3232018-08-31 10:10:53 -0700276MainLoop::MainLoop(sdbusplus::bus::bus&& bus, const std::string& param,
277 const std::string& path, const std::string& devPath,
Patrick Venture16805a62019-06-21 14:19:33 -0700278 const char* prefix, const char* root,
279 const hwmonio::HwmonIOInterface* ioIntf) :
Patrick Venture043d3232018-08-31 10:10:53 -0700280 _bus(std::move(bus)),
281 _manager(_bus, root), _pathParam(param), _hwmonRoot(), _instance(),
Patrick Venture16805a62019-06-21 14:19:33 -0700282 _devPath(devPath), _prefix(prefix), _root(root), _state(),
283 _ioAccess(ioIntf), _event(sdeventplus::Event::get_default()),
Patrick Venture52b40612018-12-19 13:36:41 -0800284 _timer(_event, std::bind(&MainLoop::read, this))
Brad Bishopd499ca62016-12-19 09:24:50 -0500285{
Patrick Venture73a50c72018-04-17 15:19:03 -0700286 // Strip off any trailing slashes.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500287 std::string p = path;
288 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500289 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500290 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500291 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500292
Patrick Venture73a50c72018-04-17 15:19:03 -0700293 // Given the furthest right /, set instance to
294 // the basename, and hwmonRoot to the leading path.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500295 auto n = p.rfind('/');
296 if (n != std::string::npos)
297 {
298 _instance.assign(p.substr(n + 1));
299 _hwmonRoot.assign(p.substr(0, n));
300 }
301
302 assert(!_instance.empty());
303 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500304}
305
306void MainLoop::shutdown() noexcept
307{
Patrick Venture52b40612018-12-19 13:36:41 -0800308 _event.exit(0);
Brad Bishopd499ca62016-12-19 09:24:50 -0500309}
310
311void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500312{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600313 init();
314
Patrick Venture043d3232018-08-31 10:10:53 -0700315 std::function<void()> callback(std::bind(&MainLoop::read, this));
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600316 try
317 {
Patrick Venture52b40612018-12-19 13:36:41 -0800318 _timer.restart(std::chrono::microseconds(_interval));
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600319
320 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
321
322 // TODO: Issue#7 - Should probably periodically check the SensorSet
323 // for new entries.
324
Patrick Venture52b40612018-12-19 13:36:41 -0800325 _bus.attach_event(_event.get(), SD_EVENT_PRIORITY_IMPORTANT);
326 _event.loop();
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600327 }
William A. Kennington III0fe4cb32018-10-18 19:19:58 -0700328 catch (const std::exception& e)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600329 {
330 log<level::ERR>("Error in sysfs polling loop",
331 entry("ERROR=%s", e.what()));
332 throw;
333 }
334}
335
336void MainLoop::init()
337{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500338 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500339 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500340
Patrick Venturedb7ecb62018-10-23 19:42:23 -0700341 for (const auto& i : *sensors)
Brad Bishop75b4ab82017-01-06 09:33:50 -0500342 {
Matthew Barthd238e232018-04-17 12:01:50 -0500343 auto object = getObject(i);
344 if (object)
345 {
346 // Construct the SensorSet value
347 // std::tuple<SensorSet::mapped_type,
348 // std::string(Sensor Label),
349 // ObjectInfo>
Patrick Venture043d3232018-08-31 10:10:53 -0700350 auto value =
351 std::make_tuple(std::move(i.second), std::move((*object).first),
352 std::move((*object).second));
Matthew Barthd238e232018-04-17 12:01:50 -0500353
Patrick Venture52b40612018-12-19 13:36:41 -0800354 _state[std::move(i.first)] = std::move(value);
Matthew Barthd238e232018-04-17 12:01:50 -0500355 }
Carol Wang9bbe6022019-08-01 17:31:30 +0800356
357 // Initialize _averageMap of sensor. e.g. <<power, 1>, <0, 0>>
358 if ((i.first.first == hwmon::type::power) &&
359 (phosphor::utility::isAverageEnvSet(i.first)))
360 {
361 _average.setAverageValue(i.first, std::make_pair(0, 0));
362 }
Brad Bishop75b4ab82017-01-06 09:33:50 -0500363 }
364
Patrick Venture62503a42017-05-23 07:30:29 -0700365 /* If there are no sensors specified by labels, exit. */
Patrick Venture52b40612018-12-19 13:36:41 -0800366 if (0 == _state.size())
Patrick Venture62503a42017-05-23 07:30:29 -0700367 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600368 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700369 }
370
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500371 {
Patrick Venturec897d8b2018-04-23 19:01:56 -0700372 std::stringstream ss;
Patrick Venture043d3232018-08-31 10:10:53 -0700373 ss << _prefix << "-"
Patrick Venturec897d8b2018-04-23 19:01:56 -0700374 << std::to_string(std::hash<std::string>{}(_devPath + _pathParam))
375 << ".Hwmon1";
376
377 _bus.request_name(ss.str().c_str());
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500378 }
379
Patrick Ventureab10f162017-05-22 09:44:50 -0700380 {
Patrick Venturea24c8802018-04-17 19:38:06 -0700381 auto interval = env::getEnv("INTERVAL");
382 if (!interval.empty())
Patrick Ventureab10f162017-05-22 09:44:50 -0700383 {
Patrick Venture50cf1c52018-04-18 09:21:41 -0700384 _interval = std::strtoull(interval.c_str(), NULL, 10);
Patrick Ventureab10f162017-05-22 09:44:50 -0700385 }
386 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600387}
Patrick Ventureab10f162017-05-22 09:44:50 -0700388
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600389void MainLoop::read()
390{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500391 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
392 // ensure the objects all exist?
393
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600394 // Iterate through all the sensors.
Kun Yi501ade22019-07-15 15:00:32 -0700395 for (auto& [sensorSetKey, sensorStateTuple] : _state)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600396 {
Kun Yi501ade22019-07-15 15:00:32 -0700397 const auto& [sensorSysfsType, sensorSysfsNum] = sensorSetKey;
398 auto& [attrs, unused, objInfo] = sensorStateTuple;
399
Kun Yi553552c2019-07-15 22:14:21 -0700400 if (attrs.find(hwmon::entry::input) == attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500401 {
Kun Yi553552c2019-07-15 22:14:21 -0700402 continue;
403 }
Kun Yi501ade22019-07-15 15:00:32 -0700404
Kun Yi553552c2019-07-15 22:14:21 -0700405 // Read value from sensor.
Carol Wang9bbe6022019-08-01 17:31:30 +0800406 std::string input = hwmon::entry::input;
407 if (sensorSysfsType == hwmon::type::pwm)
Kun Yi553552c2019-07-15 22:14:21 -0700408 {
409 input = "";
410 }
Carol Wang9bbe6022019-08-01 17:31:30 +0800411 // If type is power and AVERAGE_power* is true in env, use average
412 // instead of input
413 else if ((sensorSysfsType == hwmon::type::power) &&
414 (phosphor::utility::isAverageEnvSet(sensorSetKey)))
415 {
416 input = hwmon::entry::average;
417 }
Kun Yi553552c2019-07-15 22:14:21 -0700418
Matt Spinlerecac0ae2020-07-08 13:09:08 -0500419 SensorValueType value;
Kun Yi553552c2019-07-15 22:14:21 -0700420 auto& obj = std::get<InterfaceMap>(objInfo);
Kun Yi501ade22019-07-15 15:00:32 -0700421 std::unique_ptr<sensor::Sensor>& sensor = _sensorObjects[sensorSetKey];
Kun Yi553552c2019-07-15 22:14:21 -0700422
423 auto& statusIface = std::any_cast<std::shared_ptr<StatusObject>&>(
424 obj[InterfaceType::STATUS]);
425 // As long as addStatus is called before addValue, statusIface
426 // should never be nullptr.
427 assert(statusIface);
428
429 try
430 {
431 if (sensor->hasFaultFile())
Patrick Venture043d3232018-08-31 10:10:53 -0700432 {
Kun Yi501ade22019-07-15 15:00:32 -0700433 auto fault = _ioAccess->read(sensorSysfsType, sensorSysfsNum,
Kun Yi553552c2019-07-15 22:14:21 -0700434 hwmon::entry::fault,
435 hwmonio::retries, hwmonio::delay);
436 // Skip reading from a sensor with a valid fault file
437 // and set the functional property accordingly
438 if (!statusIface->functional((fault == 0) ? true : false))
Matthew Barth27c4a392018-04-25 14:38:51 -0500439 {
Matthew Barthac473092018-05-07 14:41:46 -0500440 continue;
Matthew Barth8772ce32018-03-22 16:03:06 -0500441 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500442 }
Kun Yi553552c2019-07-15 22:14:21 -0700443
444 {
445 // RAII object for GPIO unlock / lock
William A. Kennington III2227bd52019-06-19 11:32:22 -0700446 auto locker = sensor::gpioUnlock(sensor->getGpio());
Kun Yi553552c2019-07-15 22:14:21 -0700447
448 // Retry for up to a second if device is busy
449 // or has a transient error.
Kun Yi501ade22019-07-15 15:00:32 -0700450 value = _ioAccess->read(sensorSysfsType, sensorSysfsNum, input,
Kun Yi553552c2019-07-15 22:14:21 -0700451 hwmonio::retries, hwmonio::delay);
452 // Set functional property to true if we could read sensor
453 statusIface->functional(true);
454
455 value = sensor->adjustValue(value);
Carol Wang9bbe6022019-08-01 17:31:30 +0800456
457 if (input == hwmon::entry::average)
458 {
459 // Calculate the values of averageMap based on current
460 // average value, current average_interval value, previous
461 // average value, previous average_interval value
462 int64_t interval =
463 _ioAccess->read(sensorSysfsType, sensorSysfsNum,
464 hwmon::entry::caverage_interval,
465 hwmonio::retries, hwmonio::delay);
466 auto ret = _average.getAverageValue(sensorSetKey);
467 assert(ret);
468
469 const auto& [preAverage, preInterval] = *ret;
470
471 auto calValue = Average::calcAverage(
472 preAverage, preInterval, value, interval);
473 if (calValue)
474 {
475 // Update previous values in averageMap before the
476 // variable value is changed next
477 _average.setAverageValue(
478 sensorSetKey, std::make_pair(value, interval));
479 // Update value to be calculated average
480 value = calValue.value();
481 }
482 else
483 {
484 // the value of
485 // power*_average_interval is not changed yet, use the
486 // previous calculated average instead. So skip dbus
487 // update.
488 continue;
489 }
490 }
Kun Yi553552c2019-07-15 22:14:21 -0700491 }
492
493 updateSensorInterfaces(obj, value);
494 }
495 catch (const std::system_error& e)
496 {
497#ifdef UPDATE_FUNCTIONAL_ON_FAIL
498 // If UPDATE_FUNCTIONAL_ON_FAIL is defined and an exception was
499 // thrown, set the functional property to false.
500 // We cannot set this with the 'continue' in the lower block
501 // as the code may exit before reaching it.
502 statusIface->functional(false);
503#endif
Carol Wang9bbe6022019-08-01 17:31:30 +0800504 auto file = sysfs::make_sysfs_path(
505 _ioAccess->path(), sensorSysfsType, sensorSysfsNum, input);
Kun Yi553552c2019-07-15 22:14:21 -0700506
507 // Check sensorAdjusts for sensor removal RCs
Kun Yi501ade22019-07-15 15:00:32 -0700508 auto& sAdjusts = _sensorObjects[sensorSetKey]->getAdjusts();
Kun Yi553552c2019-07-15 22:14:21 -0700509 if (sAdjusts.rmRCs.count(e.code().value()) > 0)
510 {
511 // Return code found in sensor return code removal list
Kun Yi501ade22019-07-15 15:00:32 -0700512 if (_rmSensors.find(sensorSetKey) == _rmSensors.end())
Kun Yi553552c2019-07-15 22:14:21 -0700513 {
514 // Trace for sensor not already removed from dbus
515 log<level::INFO>("Remove sensor from dbus for read fail",
516 entry("FILE=%s", file.c_str()),
517 entry("RC=%d", e.code().value()));
518 // Mark this sensor to be removed from dbus
Kun Yi501ade22019-07-15 15:00:32 -0700519 _rmSensors[sensorSetKey] = attrs;
Kun Yi553552c2019-07-15 22:14:21 -0700520 }
521 continue;
522 }
523#ifdef UPDATE_FUNCTIONAL_ON_FAIL
524 // Do not exit with failure if UPDATE_FUNCTIONAL_ON_FAIL is set
525 continue;
526#endif
527 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::
528 Error;
529 report<ReadFailure>(
530 xyz::openbmc_project::Sensor::Device::ReadFailure::
531 CALLOUT_ERRNO(e.code().value()),
532 xyz::openbmc_project::Sensor::Device::ReadFailure::
533 CALLOUT_DEVICE_PATH(_devPath.c_str()));
534
Matt Spinler6a391de2020-07-08 13:03:10 -0500535 log<level::INFO>(fmt::format("Failing sysfs file: {} errno: {}",
536 file, e.code().value())
537 .c_str());
Kun Yi553552c2019-07-15 22:14:21 -0700538
539 exit(EXIT_FAILURE);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500540 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600541 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500542
Patrick Venture0cd4f692019-06-21 13:39:40 -0700543 removeSensors();
544
Patrick Venture0cd4f692019-06-21 13:39:40 -0700545 addDroppedSensors();
Patrick Venture0cd4f692019-06-21 13:39:40 -0700546}
547
548void MainLoop::removeSensors()
549{
Matthew Barth8772ce32018-03-22 16:03:06 -0500550 // Remove any sensors marked for removal
Patrick Venture52b40612018-12-19 13:36:41 -0800551 for (const auto& i : _rmSensors)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600552 {
Matthew Barthd0ce7922019-06-06 09:23:37 -0500553 // Remove sensor object from dbus using emit_object_removed()
554 auto& objInfo = std::get<ObjectInfo>(_state[i.first]);
555 auto& objPath = std::get<std::string>(objInfo);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700556
Matthew Barthd0ce7922019-06-06 09:23:37 -0500557 _bus.emit_object_removed(objPath.c_str());
Patrick Venture0cd4f692019-06-21 13:39:40 -0700558
Matthew Barthd0ce7922019-06-06 09:23:37 -0500559 // Erase sensor object info
Patrick Venture52b40612018-12-19 13:36:41 -0800560 _state.erase(i.first);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500561 }
Patrick Venture0cd4f692019-06-21 13:39:40 -0700562}
Matthew Barth31d214c2018-03-26 09:54:27 -0500563
Patrick Venture0cd4f692019-06-21 13:39:40 -0700564void MainLoop::addDroppedSensors()
565{
Matthew Barth31d214c2018-03-26 09:54:27 -0500566 // Attempt to add any sensors that were removed
Patrick Venture52b40612018-12-19 13:36:41 -0800567 auto it = _rmSensors.begin();
568 while (it != _rmSensors.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500569 {
Patrick Venture52b40612018-12-19 13:36:41 -0800570 if (_state.find(it->first) == _state.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500571 {
572 SensorSet::container_t::value_type ssValueType =
Patrick Venture043d3232018-08-31 10:10:53 -0700573 std::make_pair(it->first, it->second);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700574
Matthew Barthd238e232018-04-17 12:01:50 -0500575 auto object = getObject(ssValueType);
576 if (object)
Matthew Barth31d214c2018-03-26 09:54:27 -0500577 {
Matthew Barthd238e232018-04-17 12:01:50 -0500578 // Construct the SensorSet value
579 // std::tuple<SensorSet::mapped_type,
580 // std::string(Sensor Label),
581 // ObjectInfo>
582 auto value = std::make_tuple(std::move(ssValueType.second),
583 std::move((*object).first),
584 std::move((*object).second));
585
Patrick Venture52b40612018-12-19 13:36:41 -0800586 _state[std::move(ssValueType.first)] = std::move(value);
Matthew Barthd238e232018-04-17 12:01:50 -0500587
Carol Wang9bbe6022019-08-01 17:31:30 +0800588 std::string input = hwmon::entry::input;
589 // If type is power and AVERAGE_power* is true in env, use
590 // average instead of input
591 if ((it->first.first == hwmon::type::power) &&
592 (phosphor::utility::isAverageEnvSet(it->first)))
593 {
594 input = hwmon::entry::average;
595 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500596 // Sensor object added, erase entry from removal list
Carol Wang9bbe6022019-08-01 17:31:30 +0800597 auto file =
598 sysfs::make_sysfs_path(_ioAccess->path(), it->first.first,
599 it->first.second, input);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700600
Patrick Venture043d3232018-08-31 10:10:53 -0700601 log<level::INFO>("Added sensor to dbus after successful read",
602 entry("FILE=%s", file.c_str()));
Patrick Venture0cd4f692019-06-21 13:39:40 -0700603
Patrick Venture52b40612018-12-19 13:36:41 -0800604 it = _rmSensors.erase(it);
Matthew Barth31d214c2018-03-26 09:54:27 -0500605 }
606 else
607 {
608 ++it;
609 }
610 }
611 else
612 {
613 // Sanity check to remove sensors that were re-added
Patrick Venture52b40612018-12-19 13:36:41 -0800614 it = _rmSensors.erase(it);
Matthew Barth31d214c2018-03-26 09:54:27 -0500615 }
616 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500617}
618
619// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4