blob: b694261f0c68d996c988eb5aecf97c39cd47f571 [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 Spinler5e034af2020-06-24 15:21:53 -0500243 log<level::INFO>(fmt::format("Failing sysfs file: {}", file).c_str());
Matthew Barth31d214c2018-03-26 09:54:27 -0500244 exit(EXIT_FAILURE);
Matthew Barth31d214c2018-03-26 09:54:27 -0500245 }
246 auto sensorValue = valueInterface->value();
Matt Spinlerecac0ae2020-07-08 13:09:08 -0500247 int64_t scale = sensorObj->getScale();
248
Kun Yi501ade22019-07-15 15:00:32 -0700249 addThreshold<WarningObject>(sensorSysfsType, std::get<sensorID>(properties),
250 sensorValue, info, scale);
251 addThreshold<CriticalObject>(sensorSysfsType,
James Feistee73f5b2018-08-01 16:31:42 -0700252 std::get<sensorID>(properties), sensorValue,
253 info, scale);
Matthew Barth31d214c2018-03-26 09:54:27 -0500254
Patrick Venture043d3232018-08-31 10:10:53 -0700255 auto target =
Kun Yi501ade22019-07-15 15:00:32 -0700256 addTarget<hwmon::FanSpeed>(sensorSetKey, _ioAccess, _devPath, info);
Matthew Barth28f8e662018-03-26 16:57:36 -0500257 if (target)
Matthew Barth31d214c2018-03-26 09:54:27 -0500258 {
Matthew Barth28f8e662018-03-26 16:57:36 -0500259 target->enable();
Matthew Barth31d214c2018-03-26 09:54:27 -0500260 }
Kun Yi501ade22019-07-15 15:00:32 -0700261 addTarget<hwmon::FanPwm>(sensorSetKey, _ioAccess, _devPath, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500262
263 // All the interfaces have been created. Go ahead
264 // and emit InterfacesAdded.
265 valueInterface->emit_object_added();
266
Matthew Barth9c431062018-05-07 13:55:29 -0500267 // Save sensor object specifications
Kun Yi501ade22019-07-15 15:00:32 -0700268 _sensorObjects[sensorSetKey] = std::move(sensorObj);
Matthew Barth9c431062018-05-07 13:55:29 -0500269
Matthew Barthd238e232018-04-17 12:01:50 -0500270 return std::make_pair(std::move(std::get<sensorLabel>(properties)),
271 std::move(info));
Matthew Barth31d214c2018-03-26 09:54:27 -0500272}
273
Patrick Venture043d3232018-08-31 10:10:53 -0700274MainLoop::MainLoop(sdbusplus::bus::bus&& bus, const std::string& param,
275 const std::string& path, const std::string& devPath,
Patrick Venture16805a62019-06-21 14:19:33 -0700276 const char* prefix, const char* root,
277 const hwmonio::HwmonIOInterface* ioIntf) :
Patrick Venture043d3232018-08-31 10:10:53 -0700278 _bus(std::move(bus)),
279 _manager(_bus, root), _pathParam(param), _hwmonRoot(), _instance(),
Patrick Venture16805a62019-06-21 14:19:33 -0700280 _devPath(devPath), _prefix(prefix), _root(root), _state(),
281 _ioAccess(ioIntf), _event(sdeventplus::Event::get_default()),
Patrick Venture52b40612018-12-19 13:36:41 -0800282 _timer(_event, std::bind(&MainLoop::read, this))
Brad Bishopd499ca62016-12-19 09:24:50 -0500283{
Patrick Venture73a50c72018-04-17 15:19:03 -0700284 // Strip off any trailing slashes.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500285 std::string p = path;
286 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500287 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500288 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500289 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500290
Patrick Venture73a50c72018-04-17 15:19:03 -0700291 // Given the furthest right /, set instance to
292 // the basename, and hwmonRoot to the leading path.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500293 auto n = p.rfind('/');
294 if (n != std::string::npos)
295 {
296 _instance.assign(p.substr(n + 1));
297 _hwmonRoot.assign(p.substr(0, n));
298 }
299
300 assert(!_instance.empty());
301 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500302}
303
304void MainLoop::shutdown() noexcept
305{
Patrick Venture52b40612018-12-19 13:36:41 -0800306 _event.exit(0);
Brad Bishopd499ca62016-12-19 09:24:50 -0500307}
308
309void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500310{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600311 init();
312
Patrick Venture043d3232018-08-31 10:10:53 -0700313 std::function<void()> callback(std::bind(&MainLoop::read, this));
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600314 try
315 {
Patrick Venture52b40612018-12-19 13:36:41 -0800316 _timer.restart(std::chrono::microseconds(_interval));
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600317
318 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
319
320 // TODO: Issue#7 - Should probably periodically check the SensorSet
321 // for new entries.
322
Patrick Venture52b40612018-12-19 13:36:41 -0800323 _bus.attach_event(_event.get(), SD_EVENT_PRIORITY_IMPORTANT);
324 _event.loop();
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600325 }
William A. Kennington III0fe4cb32018-10-18 19:19:58 -0700326 catch (const std::exception& e)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600327 {
328 log<level::ERR>("Error in sysfs polling loop",
329 entry("ERROR=%s", e.what()));
330 throw;
331 }
332}
333
334void MainLoop::init()
335{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500336 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500337 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500338
Patrick Venturedb7ecb62018-10-23 19:42:23 -0700339 for (const auto& i : *sensors)
Brad Bishop75b4ab82017-01-06 09:33:50 -0500340 {
Matthew Barthd238e232018-04-17 12:01:50 -0500341 auto object = getObject(i);
342 if (object)
343 {
344 // Construct the SensorSet value
345 // std::tuple<SensorSet::mapped_type,
346 // std::string(Sensor Label),
347 // ObjectInfo>
Patrick Venture043d3232018-08-31 10:10:53 -0700348 auto value =
349 std::make_tuple(std::move(i.second), std::move((*object).first),
350 std::move((*object).second));
Matthew Barthd238e232018-04-17 12:01:50 -0500351
Patrick Venture52b40612018-12-19 13:36:41 -0800352 _state[std::move(i.first)] = std::move(value);
Matthew Barthd238e232018-04-17 12:01:50 -0500353 }
Carol Wang9bbe6022019-08-01 17:31:30 +0800354
355 // Initialize _averageMap of sensor. e.g. <<power, 1>, <0, 0>>
356 if ((i.first.first == hwmon::type::power) &&
357 (phosphor::utility::isAverageEnvSet(i.first)))
358 {
359 _average.setAverageValue(i.first, std::make_pair(0, 0));
360 }
Brad Bishop75b4ab82017-01-06 09:33:50 -0500361 }
362
Patrick Venture62503a42017-05-23 07:30:29 -0700363 /* If there are no sensors specified by labels, exit. */
Patrick Venture52b40612018-12-19 13:36:41 -0800364 if (0 == _state.size())
Patrick Venture62503a42017-05-23 07:30:29 -0700365 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600366 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700367 }
368
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500369 {
Patrick Venturec897d8b2018-04-23 19:01:56 -0700370 std::stringstream ss;
Patrick Venture043d3232018-08-31 10:10:53 -0700371 ss << _prefix << "-"
Patrick Venturec897d8b2018-04-23 19:01:56 -0700372 << std::to_string(std::hash<std::string>{}(_devPath + _pathParam))
373 << ".Hwmon1";
374
375 _bus.request_name(ss.str().c_str());
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500376 }
377
Patrick Ventureab10f162017-05-22 09:44:50 -0700378 {
Patrick Venturea24c8802018-04-17 19:38:06 -0700379 auto interval = env::getEnv("INTERVAL");
380 if (!interval.empty())
Patrick Ventureab10f162017-05-22 09:44:50 -0700381 {
Patrick Venture50cf1c52018-04-18 09:21:41 -0700382 _interval = std::strtoull(interval.c_str(), NULL, 10);
Patrick Ventureab10f162017-05-22 09:44:50 -0700383 }
384 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600385}
Patrick Ventureab10f162017-05-22 09:44:50 -0700386
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600387void MainLoop::read()
388{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500389 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
390 // ensure the objects all exist?
391
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600392 // Iterate through all the sensors.
Kun Yi501ade22019-07-15 15:00:32 -0700393 for (auto& [sensorSetKey, sensorStateTuple] : _state)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600394 {
Kun Yi501ade22019-07-15 15:00:32 -0700395 const auto& [sensorSysfsType, sensorSysfsNum] = sensorSetKey;
396 auto& [attrs, unused, objInfo] = sensorStateTuple;
397
Kun Yi553552c2019-07-15 22:14:21 -0700398 if (attrs.find(hwmon::entry::input) == attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500399 {
Kun Yi553552c2019-07-15 22:14:21 -0700400 continue;
401 }
Kun Yi501ade22019-07-15 15:00:32 -0700402
Kun Yi553552c2019-07-15 22:14:21 -0700403 // Read value from sensor.
Carol Wang9bbe6022019-08-01 17:31:30 +0800404 std::string input = hwmon::entry::input;
405 if (sensorSysfsType == hwmon::type::pwm)
Kun Yi553552c2019-07-15 22:14:21 -0700406 {
407 input = "";
408 }
Carol Wang9bbe6022019-08-01 17:31:30 +0800409 // If type is power and AVERAGE_power* is true in env, use average
410 // instead of input
411 else if ((sensorSysfsType == hwmon::type::power) &&
412 (phosphor::utility::isAverageEnvSet(sensorSetKey)))
413 {
414 input = hwmon::entry::average;
415 }
Kun Yi553552c2019-07-15 22:14:21 -0700416
Matt Spinlerecac0ae2020-07-08 13:09:08 -0500417 SensorValueType value;
Kun Yi553552c2019-07-15 22:14:21 -0700418 auto& obj = std::get<InterfaceMap>(objInfo);
Kun Yi501ade22019-07-15 15:00:32 -0700419 std::unique_ptr<sensor::Sensor>& sensor = _sensorObjects[sensorSetKey];
Kun Yi553552c2019-07-15 22:14:21 -0700420
421 auto& statusIface = std::any_cast<std::shared_ptr<StatusObject>&>(
422 obj[InterfaceType::STATUS]);
423 // As long as addStatus is called before addValue, statusIface
424 // should never be nullptr.
425 assert(statusIface);
426
427 try
428 {
429 if (sensor->hasFaultFile())
Patrick Venture043d3232018-08-31 10:10:53 -0700430 {
Kun Yi501ade22019-07-15 15:00:32 -0700431 auto fault = _ioAccess->read(sensorSysfsType, sensorSysfsNum,
Kun Yi553552c2019-07-15 22:14:21 -0700432 hwmon::entry::fault,
433 hwmonio::retries, hwmonio::delay);
434 // Skip reading from a sensor with a valid fault file
435 // and set the functional property accordingly
436 if (!statusIface->functional((fault == 0) ? true : false))
Matthew Barth27c4a392018-04-25 14:38:51 -0500437 {
Matthew Barthac473092018-05-07 14:41:46 -0500438 continue;
Matthew Barth8772ce32018-03-22 16:03:06 -0500439 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500440 }
Kun Yi553552c2019-07-15 22:14:21 -0700441
442 {
443 // RAII object for GPIO unlock / lock
William A. Kennington III2227bd52019-06-19 11:32:22 -0700444 auto locker = sensor::gpioUnlock(sensor->getGpio());
Kun Yi553552c2019-07-15 22:14:21 -0700445
446 // Retry for up to a second if device is busy
447 // or has a transient error.
Kun Yi501ade22019-07-15 15:00:32 -0700448 value = _ioAccess->read(sensorSysfsType, sensorSysfsNum, input,
Kun Yi553552c2019-07-15 22:14:21 -0700449 hwmonio::retries, hwmonio::delay);
450 // Set functional property to true if we could read sensor
451 statusIface->functional(true);
452
453 value = sensor->adjustValue(value);
Carol Wang9bbe6022019-08-01 17:31:30 +0800454
455 if (input == hwmon::entry::average)
456 {
457 // Calculate the values of averageMap based on current
458 // average value, current average_interval value, previous
459 // average value, previous average_interval value
460 int64_t interval =
461 _ioAccess->read(sensorSysfsType, sensorSysfsNum,
462 hwmon::entry::caverage_interval,
463 hwmonio::retries, hwmonio::delay);
464 auto ret = _average.getAverageValue(sensorSetKey);
465 assert(ret);
466
467 const auto& [preAverage, preInterval] = *ret;
468
469 auto calValue = Average::calcAverage(
470 preAverage, preInterval, value, interval);
471 if (calValue)
472 {
473 // Update previous values in averageMap before the
474 // variable value is changed next
475 _average.setAverageValue(
476 sensorSetKey, std::make_pair(value, interval));
477 // Update value to be calculated average
478 value = calValue.value();
479 }
480 else
481 {
482 // the value of
483 // power*_average_interval is not changed yet, use the
484 // previous calculated average instead. So skip dbus
485 // update.
486 continue;
487 }
488 }
Kun Yi553552c2019-07-15 22:14:21 -0700489 }
490
491 updateSensorInterfaces(obj, value);
492 }
493 catch (const std::system_error& e)
494 {
495#ifdef UPDATE_FUNCTIONAL_ON_FAIL
496 // If UPDATE_FUNCTIONAL_ON_FAIL is defined and an exception was
497 // thrown, set the functional property to false.
498 // We cannot set this with the 'continue' in the lower block
499 // as the code may exit before reaching it.
500 statusIface->functional(false);
501#endif
Carol Wang9bbe6022019-08-01 17:31:30 +0800502 auto file = sysfs::make_sysfs_path(
503 _ioAccess->path(), sensorSysfsType, sensorSysfsNum, input);
Kun Yi553552c2019-07-15 22:14:21 -0700504
505 // Check sensorAdjusts for sensor removal RCs
Kun Yi501ade22019-07-15 15:00:32 -0700506 auto& sAdjusts = _sensorObjects[sensorSetKey]->getAdjusts();
Kun Yi553552c2019-07-15 22:14:21 -0700507 if (sAdjusts.rmRCs.count(e.code().value()) > 0)
508 {
509 // Return code found in sensor return code removal list
Kun Yi501ade22019-07-15 15:00:32 -0700510 if (_rmSensors.find(sensorSetKey) == _rmSensors.end())
Kun Yi553552c2019-07-15 22:14:21 -0700511 {
512 // Trace for sensor not already removed from dbus
513 log<level::INFO>("Remove sensor from dbus for read fail",
514 entry("FILE=%s", file.c_str()),
515 entry("RC=%d", e.code().value()));
516 // Mark this sensor to be removed from dbus
Kun Yi501ade22019-07-15 15:00:32 -0700517 _rmSensors[sensorSetKey] = attrs;
Kun Yi553552c2019-07-15 22:14:21 -0700518 }
519 continue;
520 }
521#ifdef UPDATE_FUNCTIONAL_ON_FAIL
522 // Do not exit with failure if UPDATE_FUNCTIONAL_ON_FAIL is set
523 continue;
524#endif
525 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::
526 Error;
527 report<ReadFailure>(
528 xyz::openbmc_project::Sensor::Device::ReadFailure::
529 CALLOUT_ERRNO(e.code().value()),
530 xyz::openbmc_project::Sensor::Device::ReadFailure::
531 CALLOUT_DEVICE_PATH(_devPath.c_str()));
532
Matt Spinler5e034af2020-06-24 15:21:53 -0500533 log<level::INFO>(
534 fmt::format("Failing sysfs file: {}", file).c_str());
Kun Yi553552c2019-07-15 22:14:21 -0700535
536 exit(EXIT_FAILURE);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500537 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600538 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500539
Patrick Venture0cd4f692019-06-21 13:39:40 -0700540 removeSensors();
541
Patrick Venture0cd4f692019-06-21 13:39:40 -0700542 addDroppedSensors();
Patrick Venture0cd4f692019-06-21 13:39:40 -0700543}
544
545void MainLoop::removeSensors()
546{
Matthew Barth8772ce32018-03-22 16:03:06 -0500547 // Remove any sensors marked for removal
Patrick Venture52b40612018-12-19 13:36:41 -0800548 for (const auto& i : _rmSensors)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600549 {
Matthew Barthd0ce7922019-06-06 09:23:37 -0500550 // Remove sensor object from dbus using emit_object_removed()
551 auto& objInfo = std::get<ObjectInfo>(_state[i.first]);
552 auto& objPath = std::get<std::string>(objInfo);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700553
Matthew Barthd0ce7922019-06-06 09:23:37 -0500554 _bus.emit_object_removed(objPath.c_str());
Patrick Venture0cd4f692019-06-21 13:39:40 -0700555
Matthew Barthd0ce7922019-06-06 09:23:37 -0500556 // Erase sensor object info
Patrick Venture52b40612018-12-19 13:36:41 -0800557 _state.erase(i.first);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500558 }
Patrick Venture0cd4f692019-06-21 13:39:40 -0700559}
Matthew Barth31d214c2018-03-26 09:54:27 -0500560
Patrick Venture0cd4f692019-06-21 13:39:40 -0700561void MainLoop::addDroppedSensors()
562{
Matthew Barth31d214c2018-03-26 09:54:27 -0500563 // Attempt to add any sensors that were removed
Patrick Venture52b40612018-12-19 13:36:41 -0800564 auto it = _rmSensors.begin();
565 while (it != _rmSensors.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500566 {
Patrick Venture52b40612018-12-19 13:36:41 -0800567 if (_state.find(it->first) == _state.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500568 {
569 SensorSet::container_t::value_type ssValueType =
Patrick Venture043d3232018-08-31 10:10:53 -0700570 std::make_pair(it->first, it->second);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700571
Matthew Barthd238e232018-04-17 12:01:50 -0500572 auto object = getObject(ssValueType);
573 if (object)
Matthew Barth31d214c2018-03-26 09:54:27 -0500574 {
Matthew Barthd238e232018-04-17 12:01:50 -0500575 // Construct the SensorSet value
576 // std::tuple<SensorSet::mapped_type,
577 // std::string(Sensor Label),
578 // ObjectInfo>
579 auto value = std::make_tuple(std::move(ssValueType.second),
580 std::move((*object).first),
581 std::move((*object).second));
582
Patrick Venture52b40612018-12-19 13:36:41 -0800583 _state[std::move(ssValueType.first)] = std::move(value);
Matthew Barthd238e232018-04-17 12:01:50 -0500584
Carol Wang9bbe6022019-08-01 17:31:30 +0800585 std::string input = hwmon::entry::input;
586 // If type is power and AVERAGE_power* is true in env, use
587 // average instead of input
588 if ((it->first.first == hwmon::type::power) &&
589 (phosphor::utility::isAverageEnvSet(it->first)))
590 {
591 input = hwmon::entry::average;
592 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500593 // Sensor object added, erase entry from removal list
Carol Wang9bbe6022019-08-01 17:31:30 +0800594 auto file =
595 sysfs::make_sysfs_path(_ioAccess->path(), it->first.first,
596 it->first.second, input);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700597
Patrick Venture043d3232018-08-31 10:10:53 -0700598 log<level::INFO>("Added sensor to dbus after successful read",
599 entry("FILE=%s", file.c_str()));
Patrick Venture0cd4f692019-06-21 13:39:40 -0700600
Patrick Venture52b40612018-12-19 13:36:41 -0800601 it = _rmSensors.erase(it);
Matthew Barth31d214c2018-03-26 09:54:27 -0500602 }
603 else
604 {
605 ++it;
606 }
607 }
608 else
609 {
610 // Sanity check to remove sensors that were re-added
Patrick Venture52b40612018-12-19 13:36:41 -0800611 it = _rmSensors.erase(it);
Matthew Barth31d214c2018-03-26 09:54:27 -0500612 }
613 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500614}
615
616// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4