blob: 4abfc02e32d60de19d769ac69ff6d869d5f388d7 [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
William A. Kennington III0e749752018-11-06 15:25:41 -080032#include <cassert>
Patrick Venture043d3232018-08-31 10:10:53 -070033#include <cstdlib>
34#include <functional>
35#include <iostream>
36#include <memory>
37#include <phosphor-logging/elog-errors.hpp>
38#include <sstream>
39#include <string>
40#include <unordered_set>
Patrick Venture1e6324f2017-06-01 14:07:05 -070041#include <xyz/openbmc_project/Sensor/Device/error.hpp>
42
43using namespace phosphor::logging;
44
Saqib Khan973886d2017-03-15 14:01:16 -050045// Initialization for Warning Objects
46decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo =
47 &WarningObject::warningLow;
48decltype(Thresholds<WarningObject>::setHi) Thresholds<WarningObject>::setHi =
49 &WarningObject::warningHigh;
50decltype(Thresholds<WarningObject>::getLo) Thresholds<WarningObject>::getLo =
51 &WarningObject::warningLow;
52decltype(Thresholds<WarningObject>::getHi) Thresholds<WarningObject>::getHi =
53 &WarningObject::warningHigh;
Patrick Venture043d3232018-08-31 10:10:53 -070054decltype(
55 Thresholds<WarningObject>::alarmLo) Thresholds<WarningObject>::alarmLo =
Saqib Khan973886d2017-03-15 14:01:16 -050056 &WarningObject::warningAlarmLow;
Patrick Venture043d3232018-08-31 10:10:53 -070057decltype(
58 Thresholds<WarningObject>::alarmHi) Thresholds<WarningObject>::alarmHi =
Saqib Khan973886d2017-03-15 14:01:16 -050059 &WarningObject::warningAlarmHigh;
60
61// Initialization for Critical Objects
62decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo =
63 &CriticalObject::criticalLow;
64decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi =
65 &CriticalObject::criticalHigh;
66decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo =
67 &CriticalObject::criticalLow;
68decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi =
69 &CriticalObject::criticalHigh;
Patrick Venture043d3232018-08-31 10:10:53 -070070decltype(
71 Thresholds<CriticalObject>::alarmLo) Thresholds<CriticalObject>::alarmLo =
Saqib Khan973886d2017-03-15 14:01:16 -050072 &CriticalObject::criticalAlarmLow;
Patrick Venture043d3232018-08-31 10:10:53 -070073decltype(
74 Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi =
Saqib Khan973886d2017-03-15 14:01:16 -050075 &CriticalObject::criticalAlarmHigh;
76
Patrick Venturefeb744a2019-06-26 19:07:48 -070077void updateSensorInterfaces(InterfaceMap& ifaces, int64_t value)
78{
79 for (auto& iface : ifaces)
80 {
81 switch (iface.first)
82 {
83 case InterfaceType::VALUE:
84 {
85 auto& valueIface =
86 std::any_cast<std::shared_ptr<ValueObject>&>(iface.second);
87 valueIface->value(value);
88 }
89 break;
90 case InterfaceType::WARN:
91 checkThresholds<WarningObject>(iface.second, value);
92 break;
93 case InterfaceType::CRIT:
94 checkThresholds<CriticalObject>(iface.second, value);
95 break;
96 default:
97 break;
98 }
99 }
100}
101
Matthew Barth979c8062018-04-17 11:37:15 -0500102std::string MainLoop::getID(SensorSet::container_t::const_reference sensor)
Matthew Barth31d214c2018-03-26 09:54:27 -0500103{
Matthew Barth31d214c2018-03-26 09:54:27 -0500104 std::string id;
105
106 /*
107 * Check if the value of the MODE_<item><X> env variable for the sensor
Matt Spinler7c424802018-05-04 10:52:40 -0500108 * is set. If it is, then read the from the <item><X>_<mode>
Matthew Barth31d214c2018-03-26 09:54:27 -0500109 * file. The name of the DBUS object would be the value of the env
Matt Spinler7c424802018-05-04 10:52:40 -0500110 * variable LABEL_<item><mode value>. If the MODE_<item><X> env variable
Matthew Barth31d214c2018-03-26 09:54:27 -0500111 * doesn't exist, then the name of DBUS object is the value of the env
112 * variable LABEL_<item><X>.
Matt Spinler7c424802018-05-04 10:52:40 -0500113 *
114 * For example, if MODE_temp1 = "label", then code reads the temp1_label
115 * file. If it has a 5 in it, then it will use the following entry to
116 * name the object: LABEL_temp5 = "My DBus object name".
117 *
Matthew Barth31d214c2018-03-26 09:54:27 -0500118 */
Patrick Venture7a5285d2018-04-17 19:15:05 -0700119 auto mode = env::getEnv("MODE", sensor.first);
Matt Spinler7c424802018-05-04 10:52:40 -0500120 if (!mode.empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500121 {
Patrick Venture043d3232018-08-31 10:10:53 -0700122 id = env::getIndirectID(_hwmonRoot + '/' + _instance + '/', mode,
123 sensor.first);
Matthew Barth31d214c2018-03-26 09:54:27 -0500124
125 if (id.empty())
126 {
Matthew Barth979c8062018-04-17 11:37:15 -0500127 return id;
Matthew Barth31d214c2018-03-26 09:54:27 -0500128 }
129 }
130
131 // Use the ID we looked up above if there was one,
132 // otherwise use the standard one.
133 id = (id.empty()) ? sensor.first.second : id;
134
Matthew Barth979c8062018-04-17 11:37:15 -0500135 return id;
136}
137
Patrick Venture043d3232018-08-31 10:10:53 -0700138SensorIdentifiers
139 MainLoop::getIdentifiers(SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500140{
141 std::string id = getID(sensor);
142 std::string label;
143
144 if (!id.empty())
145 {
146 // Ignore inputs without a label.
147 label = env::getEnv("LABEL", sensor.first.first, id);
148 }
149
Patrick Venture043d3232018-08-31 10:10:53 -0700150 return std::make_tuple(std::move(id), std::move(label));
Matthew Barth979c8062018-04-17 11:37:15 -0500151}
152
153/**
154 * Reads the environment parameters of a sensor and creates an object with
155 * atleast the `Value` interface, otherwise returns without creating the object.
156 * If the `Value` interface is successfully created, by reading the sensor's
157 * corresponding sysfs file's value, the additional interfaces for the sensor
Matthew Barthd238e232018-04-17 12:01:50 -0500158 * are created and the InterfacesAdded signal is emitted. The object's state
159 * data is then returned for sensor state monitoring within the main loop.
Matthew Barth979c8062018-04-17 11:37:15 -0500160 */
William A. Kennington III4cbdfef2018-10-18 19:19:51 -0700161std::optional<ObjectStateData>
Patrick Venture043d3232018-08-31 10:10:53 -0700162 MainLoop::getObject(SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500163{
164 auto properties = getIdentifiers(sensor);
165 if (std::get<sensorID>(properties).empty() ||
166 std::get<sensorLabel>(properties).empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500167 {
Matthew Barthd238e232018-04-17 12:01:50 -0500168 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500169 }
170
Patrick Venture09791852018-04-17 17:40:00 -0700171 hwmon::Attributes attrs;
172 if (!hwmon::getAttributes(sensor.first.first, attrs))
Matthew Barth31d214c2018-03-26 09:54:27 -0500173 {
Matthew Barthd238e232018-04-17 12:01:50 -0500174 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500175 }
176
Kun Yi501ade22019-07-15 15:00:32 -0700177 const auto& [sensorSetKey, sensorAttrs] = sensor;
178 const auto& [sensorSysfsType, sensorSysfsNum] = sensorSetKey;
179
Patrick Venture2864b062018-12-19 08:13:41 -0800180 /* Note: The sensor objects all share the same ioAccess object. */
Patrick Venture043d3232018-08-31 10:10:53 -0700181 auto sensorObj =
Kun Yi501ade22019-07-15 15:00:32 -0700182 std::make_unique<sensor::Sensor>(sensorSetKey, _ioAccess, _devPath);
Matthew Barth9c431062018-05-07 13:55:29 -0500183
Matthew Barthb7985272018-04-17 10:50:36 -0500184 // Get list of return codes for removing sensors on device
185 auto devRmRCs = env::getEnv("REMOVERCS");
186 // Add sensor removal return codes defined at the device level
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500187 sensorObj->addRemoveRCs(devRmRCs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500188
189 std::string objectPath{_root};
190 objectPath.append(1, '/');
Patrick Venture09791852018-04-17 17:40:00 -0700191 objectPath.append(hwmon::getNamespace(attrs));
Matthew Barth31d214c2018-03-26 09:54:27 -0500192 objectPath.append(1, '/');
Matthew Barth979c8062018-04-17 11:37:15 -0500193 objectPath.append(std::get<sensorLabel>(properties));
Matthew Barth31d214c2018-03-26 09:54:27 -0500194
Patrick Venture62067232019-06-19 17:39:33 -0700195 ObjectInfo info(&_bus, std::move(objectPath), InterfaceMap());
Patrick Venture75e56c62018-04-20 18:10:15 -0700196 RetryIO retryIO(hwmonio::retries, hwmonio::delay);
Kun Yi501ade22019-07-15 15:00:32 -0700197 if (_rmSensors.find(sensorSetKey) != _rmSensors.end())
Matthew Barthd4beecf2018-04-03 15:50:22 -0500198 {
199 // When adding a sensor that was purposely removed,
200 // don't retry on errors when reading its value
201 std::get<size_t>(retryIO) = 0;
202 }
Patrick Venture043d3232018-08-31 10:10:53 -0700203 auto valueInterface = static_cast<std::shared_ptr<ValueObject>>(nullptr);
Matthew Barth31d214c2018-03-26 09:54:27 -0500204 try
205 {
Matthew Barthca44c2e2018-04-24 15:33:25 -0500206 // Add status interface based on _fault file being present
Matthew Barth2e41b132018-05-07 14:15:45 -0500207 sensorObj->addStatus(info);
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500208 valueInterface = sensorObj->addValue(retryIO, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500209 }
210 catch (const std::system_error& e)
211 {
Patrick Venture043d3232018-08-31 10:10:53 -0700212 auto file =
Kun Yi501ade22019-07-15 15:00:32 -0700213 sysfs::make_sysfs_path(_ioAccess->path(), sensorSysfsType,
214 sensorSysfsNum, hwmon::entry::cinput);
Patrick Venture0892c3f2019-06-27 14:24:03 -0700215
Matthew Barth31d214c2018-03-26 09:54:27 -0500216 // Check sensorAdjusts for sensor removal RCs
Matthew Barthac473092018-05-07 14:41:46 -0500217 auto& sAdjusts = sensorObj->getAdjusts();
218 if (sAdjusts.rmRCs.count(e.code().value()) > 0)
Matthew Barth31d214c2018-03-26 09:54:27 -0500219 {
Matthew Barthac473092018-05-07 14:41:46 -0500220 // Return code found in sensor return code removal list
Kun Yi501ade22019-07-15 15:00:32 -0700221 if (_rmSensors.find(sensorSetKey) == _rmSensors.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500222 {
Matthew Barthac473092018-05-07 14:41:46 -0500223 // Trace for sensor not already removed from dbus
224 log<level::INFO>("Sensor not added to dbus for read fail",
Patrick Venture043d3232018-08-31 10:10:53 -0700225 entry("FILE=%s", file.c_str()),
226 entry("RC=%d", e.code().value()));
Kun Yi501ade22019-07-15 15:00:32 -0700227 _rmSensors[std::move(sensorSetKey)] = std::move(sensorAttrs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500228 }
Matthew Barthac473092018-05-07 14:41:46 -0500229 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500230 }
Patrick Venture0892c3f2019-06-27 14:24:03 -0700231
Patrick Venture043d3232018-08-31 10:10:53 -0700232 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error;
Matthew Barth31d214c2018-03-26 09:54:27 -0500233 report<ReadFailure>(
Patrick Venture043d3232018-08-31 10:10:53 -0700234 xyz::openbmc_project::Sensor::Device::ReadFailure::CALLOUT_ERRNO(
235 e.code().value()),
236 xyz::openbmc_project::Sensor::Device::ReadFailure::
237 CALLOUT_DEVICE_PATH(_devPath.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500238
Matthew Barth31d214c2018-03-26 09:54:27 -0500239 log<level::INFO>("Logging failing sysfs file",
Patrick Venture043d3232018-08-31 10:10:53 -0700240 entry("FILE=%s", file.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500241 exit(EXIT_FAILURE);
Matthew Barth31d214c2018-03-26 09:54:27 -0500242 }
243 auto sensorValue = valueInterface->value();
James Feistee73f5b2018-08-01 16:31:42 -0700244 int64_t scale = 0;
245 // scale the thresholds only if we're using doubles
246 if constexpr (std::is_same<SensorValueType, double>::value)
247 {
248 scale = sensorObj->getScale();
249 }
Kun Yi501ade22019-07-15 15:00:32 -0700250 addThreshold<WarningObject>(sensorSysfsType, std::get<sensorID>(properties),
251 sensorValue, info, scale);
252 addThreshold<CriticalObject>(sensorSysfsType,
James Feistee73f5b2018-08-01 16:31:42 -0700253 std::get<sensorID>(properties), sensorValue,
254 info, scale);
Matthew Barth31d214c2018-03-26 09:54:27 -0500255
Patrick Venture043d3232018-08-31 10:10:53 -0700256 auto target =
Kun Yi501ade22019-07-15 15:00:32 -0700257 addTarget<hwmon::FanSpeed>(sensorSetKey, _ioAccess, _devPath, info);
Matthew Barth28f8e662018-03-26 16:57:36 -0500258 if (target)
Matthew Barth31d214c2018-03-26 09:54:27 -0500259 {
Matthew Barth28f8e662018-03-26 16:57:36 -0500260 target->enable();
Matthew Barth31d214c2018-03-26 09:54:27 -0500261 }
Kun Yi501ade22019-07-15 15:00:32 -0700262 addTarget<hwmon::FanPwm>(sensorSetKey, _ioAccess, _devPath, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500263
264 // All the interfaces have been created. Go ahead
265 // and emit InterfacesAdded.
266 valueInterface->emit_object_added();
267
Matthew Barth9c431062018-05-07 13:55:29 -0500268 // Save sensor object specifications
Kun Yi501ade22019-07-15 15:00:32 -0700269 _sensorObjects[sensorSetKey] = std::move(sensorObj);
Matthew Barth9c431062018-05-07 13:55:29 -0500270
Matthew Barthd238e232018-04-17 12:01:50 -0500271 return std::make_pair(std::move(std::get<sensorLabel>(properties)),
272 std::move(info));
Matthew Barth31d214c2018-03-26 09:54:27 -0500273}
274
Patrick Venture043d3232018-08-31 10:10:53 -0700275MainLoop::MainLoop(sdbusplus::bus::bus&& bus, const std::string& param,
276 const std::string& path, const std::string& devPath,
Patrick Venture16805a62019-06-21 14:19:33 -0700277 const char* prefix, const char* root,
278 const hwmonio::HwmonIOInterface* ioIntf) :
Patrick Venture043d3232018-08-31 10:10:53 -0700279 _bus(std::move(bus)),
280 _manager(_bus, root), _pathParam(param), _hwmonRoot(), _instance(),
Patrick Venture16805a62019-06-21 14:19:33 -0700281 _devPath(devPath), _prefix(prefix), _root(root), _state(),
282 _ioAccess(ioIntf), _event(sdeventplus::Event::get_default()),
Patrick Venture52b40612018-12-19 13:36:41 -0800283 _timer(_event, std::bind(&MainLoop::read, this))
Brad Bishopd499ca62016-12-19 09:24:50 -0500284{
Patrick Venture73a50c72018-04-17 15:19:03 -0700285 // Strip off any trailing slashes.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500286 std::string p = path;
287 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500288 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500289 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500290 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500291
Patrick Venture73a50c72018-04-17 15:19:03 -0700292 // Given the furthest right /, set instance to
293 // the basename, and hwmonRoot to the leading path.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500294 auto n = p.rfind('/');
295 if (n != std::string::npos)
296 {
297 _instance.assign(p.substr(n + 1));
298 _hwmonRoot.assign(p.substr(0, n));
299 }
300
301 assert(!_instance.empty());
302 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500303}
304
305void MainLoop::shutdown() noexcept
306{
Patrick Venture52b40612018-12-19 13:36:41 -0800307 _event.exit(0);
Brad Bishopd499ca62016-12-19 09:24:50 -0500308}
309
310void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500311{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600312 init();
313
Patrick Venture043d3232018-08-31 10:10:53 -0700314 std::function<void()> callback(std::bind(&MainLoop::read, this));
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600315 try
316 {
Patrick Venture52b40612018-12-19 13:36:41 -0800317 _timer.restart(std::chrono::microseconds(_interval));
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600318
319 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
320
321 // TODO: Issue#7 - Should probably periodically check the SensorSet
322 // for new entries.
323
Patrick Venture52b40612018-12-19 13:36:41 -0800324 _bus.attach_event(_event.get(), SD_EVENT_PRIORITY_IMPORTANT);
325 _event.loop();
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600326 }
William A. Kennington III0fe4cb32018-10-18 19:19:58 -0700327 catch (const std::exception& e)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600328 {
329 log<level::ERR>("Error in sysfs polling loop",
330 entry("ERROR=%s", e.what()));
331 throw;
332 }
333}
334
335void MainLoop::init()
336{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500337 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500338 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500339
Patrick Venturedb7ecb62018-10-23 19:42:23 -0700340 for (const auto& i : *sensors)
Brad Bishop75b4ab82017-01-06 09:33:50 -0500341 {
Matthew Barthd238e232018-04-17 12:01:50 -0500342 auto object = getObject(i);
343 if (object)
344 {
345 // Construct the SensorSet value
346 // std::tuple<SensorSet::mapped_type,
347 // std::string(Sensor Label),
348 // ObjectInfo>
Patrick Venture043d3232018-08-31 10:10:53 -0700349 auto value =
350 std::make_tuple(std::move(i.second), std::move((*object).first),
351 std::move((*object).second));
Matthew Barthd238e232018-04-17 12:01:50 -0500352
Patrick Venture52b40612018-12-19 13:36:41 -0800353 _state[std::move(i.first)] = std::move(value);
Matthew Barthd238e232018-04-17 12:01:50 -0500354 }
Carol Wang9bbe6022019-08-01 17:31:30 +0800355
356 // Initialize _averageMap of sensor. e.g. <<power, 1>, <0, 0>>
357 if ((i.first.first == hwmon::type::power) &&
358 (phosphor::utility::isAverageEnvSet(i.first)))
359 {
360 _average.setAverageValue(i.first, std::make_pair(0, 0));
361 }
Brad Bishop75b4ab82017-01-06 09:33:50 -0500362 }
363
Patrick Venture62503a42017-05-23 07:30:29 -0700364 /* If there are no sensors specified by labels, exit. */
Patrick Venture52b40612018-12-19 13:36:41 -0800365 if (0 == _state.size())
Patrick Venture62503a42017-05-23 07:30:29 -0700366 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600367 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700368 }
369
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500370 {
Patrick Venturec897d8b2018-04-23 19:01:56 -0700371 std::stringstream ss;
Patrick Venture043d3232018-08-31 10:10:53 -0700372 ss << _prefix << "-"
Patrick Venturec897d8b2018-04-23 19:01:56 -0700373 << std::to_string(std::hash<std::string>{}(_devPath + _pathParam))
374 << ".Hwmon1";
375
376 _bus.request_name(ss.str().c_str());
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500377 }
378
Patrick Ventureab10f162017-05-22 09:44:50 -0700379 {
Patrick Venturea24c8802018-04-17 19:38:06 -0700380 auto interval = env::getEnv("INTERVAL");
381 if (!interval.empty())
Patrick Ventureab10f162017-05-22 09:44:50 -0700382 {
Patrick Venture50cf1c52018-04-18 09:21:41 -0700383 _interval = std::strtoull(interval.c_str(), NULL, 10);
Patrick Ventureab10f162017-05-22 09:44:50 -0700384 }
385 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600386}
Patrick Ventureab10f162017-05-22 09:44:50 -0700387
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600388void MainLoop::read()
389{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500390 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
391 // ensure the objects all exist?
392
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600393 // Iterate through all the sensors.
Kun Yi501ade22019-07-15 15:00:32 -0700394 for (auto& [sensorSetKey, sensorStateTuple] : _state)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600395 {
Kun Yi501ade22019-07-15 15:00:32 -0700396 const auto& [sensorSysfsType, sensorSysfsNum] = sensorSetKey;
397 auto& [attrs, unused, objInfo] = sensorStateTuple;
398
Kun Yi553552c2019-07-15 22:14:21 -0700399 if (attrs.find(hwmon::entry::input) == attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500400 {
Kun Yi553552c2019-07-15 22:14:21 -0700401 continue;
402 }
Kun Yi501ade22019-07-15 15:00:32 -0700403
Kun Yi553552c2019-07-15 22:14:21 -0700404 // Read value from sensor.
Carol Wang9bbe6022019-08-01 17:31:30 +0800405 std::string input = hwmon::entry::input;
406 if (sensorSysfsType == hwmon::type::pwm)
Kun Yi553552c2019-07-15 22:14:21 -0700407 {
408 input = "";
409 }
Carol Wang9bbe6022019-08-01 17:31:30 +0800410 // If type is power and AVERAGE_power* is true in env, use average
411 // instead of input
412 else if ((sensorSysfsType == hwmon::type::power) &&
413 (phosphor::utility::isAverageEnvSet(sensorSetKey)))
414 {
415 input = hwmon::entry::average;
416 }
Kun Yi553552c2019-07-15 22:14:21 -0700417
418 int64_t value;
Kun Yi553552c2019-07-15 22:14:21 -0700419 auto& obj = std::get<InterfaceMap>(objInfo);
Kun Yi501ade22019-07-15 15:00:32 -0700420 std::unique_ptr<sensor::Sensor>& sensor = _sensorObjects[sensorSetKey];
Kun Yi553552c2019-07-15 22:14:21 -0700421
422 auto& statusIface = std::any_cast<std::shared_ptr<StatusObject>&>(
423 obj[InterfaceType::STATUS]);
424 // As long as addStatus is called before addValue, statusIface
425 // should never be nullptr.
426 assert(statusIface);
427
428 try
429 {
430 if (sensor->hasFaultFile())
Patrick Venture043d3232018-08-31 10:10:53 -0700431 {
Kun Yi501ade22019-07-15 15:00:32 -0700432 auto fault = _ioAccess->read(sensorSysfsType, sensorSysfsNum,
Kun Yi553552c2019-07-15 22:14:21 -0700433 hwmon::entry::fault,
434 hwmonio::retries, hwmonio::delay);
435 // Skip reading from a sensor with a valid fault file
436 // and set the functional property accordingly
437 if (!statusIface->functional((fault == 0) ? true : false))
Matthew Barth27c4a392018-04-25 14:38:51 -0500438 {
Matthew Barthac473092018-05-07 14:41:46 -0500439 continue;
Matthew Barth8772ce32018-03-22 16:03:06 -0500440 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500441 }
Kun Yi553552c2019-07-15 22:14:21 -0700442
443 {
444 // RAII object for GPIO unlock / lock
William A. Kennington III2227bd52019-06-19 11:32:22 -0700445 auto locker = sensor::gpioUnlock(sensor->getGpio());
Kun Yi553552c2019-07-15 22:14:21 -0700446
447 // Retry for up to a second if device is busy
448 // or has a transient error.
Kun Yi501ade22019-07-15 15:00:32 -0700449 value = _ioAccess->read(sensorSysfsType, sensorSysfsNum, input,
Kun Yi553552c2019-07-15 22:14:21 -0700450 hwmonio::retries, hwmonio::delay);
451 // Set functional property to true if we could read sensor
452 statusIface->functional(true);
453
454 value = sensor->adjustValue(value);
Carol Wang9bbe6022019-08-01 17:31:30 +0800455
456 if (input == hwmon::entry::average)
457 {
458 // Calculate the values of averageMap based on current
459 // average value, current average_interval value, previous
460 // average value, previous average_interval value
461 int64_t interval =
462 _ioAccess->read(sensorSysfsType, sensorSysfsNum,
463 hwmon::entry::caverage_interval,
464 hwmonio::retries, hwmonio::delay);
465 auto ret = _average.getAverageValue(sensorSetKey);
466 assert(ret);
467
468 const auto& [preAverage, preInterval] = *ret;
469
470 auto calValue = Average::calcAverage(
471 preAverage, preInterval, value, interval);
472 if (calValue)
473 {
474 // Update previous values in averageMap before the
475 // variable value is changed next
476 _average.setAverageValue(
477 sensorSetKey, std::make_pair(value, interval));
478 // Update value to be calculated average
479 value = calValue.value();
480 }
481 else
482 {
483 // the value of
484 // power*_average_interval is not changed yet, use the
485 // previous calculated average instead. So skip dbus
486 // update.
487 continue;
488 }
489 }
Kun Yi553552c2019-07-15 22:14:21 -0700490 }
491
492 updateSensorInterfaces(obj, value);
493 }
494 catch (const std::system_error& e)
495 {
496#ifdef UPDATE_FUNCTIONAL_ON_FAIL
497 // If UPDATE_FUNCTIONAL_ON_FAIL is defined and an exception was
498 // thrown, set the functional property to false.
499 // We cannot set this with the 'continue' in the lower block
500 // as the code may exit before reaching it.
501 statusIface->functional(false);
502#endif
Carol Wang9bbe6022019-08-01 17:31:30 +0800503 auto file = sysfs::make_sysfs_path(
504 _ioAccess->path(), sensorSysfsType, sensorSysfsNum, input);
Kun Yi553552c2019-07-15 22:14:21 -0700505
506 // Check sensorAdjusts for sensor removal RCs
Kun Yi501ade22019-07-15 15:00:32 -0700507 auto& sAdjusts = _sensorObjects[sensorSetKey]->getAdjusts();
Kun Yi553552c2019-07-15 22:14:21 -0700508 if (sAdjusts.rmRCs.count(e.code().value()) > 0)
509 {
510 // Return code found in sensor return code removal list
Kun Yi501ade22019-07-15 15:00:32 -0700511 if (_rmSensors.find(sensorSetKey) == _rmSensors.end())
Kun Yi553552c2019-07-15 22:14:21 -0700512 {
513 // Trace for sensor not already removed from dbus
514 log<level::INFO>("Remove sensor from dbus for read fail",
515 entry("FILE=%s", file.c_str()),
516 entry("RC=%d", e.code().value()));
517 // Mark this sensor to be removed from dbus
Kun Yi501ade22019-07-15 15:00:32 -0700518 _rmSensors[sensorSetKey] = attrs;
Kun Yi553552c2019-07-15 22:14:21 -0700519 }
520 continue;
521 }
522#ifdef UPDATE_FUNCTIONAL_ON_FAIL
523 // Do not exit with failure if UPDATE_FUNCTIONAL_ON_FAIL is set
524 continue;
525#endif
526 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::
527 Error;
528 report<ReadFailure>(
529 xyz::openbmc_project::Sensor::Device::ReadFailure::
530 CALLOUT_ERRNO(e.code().value()),
531 xyz::openbmc_project::Sensor::Device::ReadFailure::
532 CALLOUT_DEVICE_PATH(_devPath.c_str()));
533
534 log<level::INFO>("Logging failing sysfs file",
535 entry("FILE=%s", file.c_str()));
536
537 exit(EXIT_FAILURE);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500538 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600539 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500540
Patrick Venture0cd4f692019-06-21 13:39:40 -0700541 removeSensors();
542
Patrick Venture0cd4f692019-06-21 13:39:40 -0700543 addDroppedSensors();
Patrick Venture0cd4f692019-06-21 13:39:40 -0700544}
545
546void MainLoop::removeSensors()
547{
Matthew Barth8772ce32018-03-22 16:03:06 -0500548 // Remove any sensors marked for removal
Patrick Venture52b40612018-12-19 13:36:41 -0800549 for (const auto& i : _rmSensors)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600550 {
Matthew Barthd0ce7922019-06-06 09:23:37 -0500551 // Remove sensor object from dbus using emit_object_removed()
552 auto& objInfo = std::get<ObjectInfo>(_state[i.first]);
553 auto& objPath = std::get<std::string>(objInfo);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700554
Matthew Barthd0ce7922019-06-06 09:23:37 -0500555 _bus.emit_object_removed(objPath.c_str());
Patrick Venture0cd4f692019-06-21 13:39:40 -0700556
Matthew Barthd0ce7922019-06-06 09:23:37 -0500557 // Erase sensor object info
Patrick Venture52b40612018-12-19 13:36:41 -0800558 _state.erase(i.first);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500559 }
Patrick Venture0cd4f692019-06-21 13:39:40 -0700560}
Matthew Barth31d214c2018-03-26 09:54:27 -0500561
Patrick Venture0cd4f692019-06-21 13:39:40 -0700562void MainLoop::addDroppedSensors()
563{
Matthew Barth31d214c2018-03-26 09:54:27 -0500564 // Attempt to add any sensors that were removed
Patrick Venture52b40612018-12-19 13:36:41 -0800565 auto it = _rmSensors.begin();
566 while (it != _rmSensors.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500567 {
Patrick Venture52b40612018-12-19 13:36:41 -0800568 if (_state.find(it->first) == _state.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500569 {
570 SensorSet::container_t::value_type ssValueType =
Patrick Venture043d3232018-08-31 10:10:53 -0700571 std::make_pair(it->first, it->second);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700572
Matthew Barthd238e232018-04-17 12:01:50 -0500573 auto object = getObject(ssValueType);
574 if (object)
Matthew Barth31d214c2018-03-26 09:54:27 -0500575 {
Matthew Barthd238e232018-04-17 12:01:50 -0500576 // Construct the SensorSet value
577 // std::tuple<SensorSet::mapped_type,
578 // std::string(Sensor Label),
579 // ObjectInfo>
580 auto value = std::make_tuple(std::move(ssValueType.second),
581 std::move((*object).first),
582 std::move((*object).second));
583
Patrick Venture52b40612018-12-19 13:36:41 -0800584 _state[std::move(ssValueType.first)] = std::move(value);
Matthew Barthd238e232018-04-17 12:01:50 -0500585
Carol Wang9bbe6022019-08-01 17:31:30 +0800586 std::string input = hwmon::entry::input;
587 // If type is power and AVERAGE_power* is true in env, use
588 // average instead of input
589 if ((it->first.first == hwmon::type::power) &&
590 (phosphor::utility::isAverageEnvSet(it->first)))
591 {
592 input = hwmon::entry::average;
593 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500594 // Sensor object added, erase entry from removal list
Carol Wang9bbe6022019-08-01 17:31:30 +0800595 auto file =
596 sysfs::make_sysfs_path(_ioAccess->path(), it->first.first,
597 it->first.second, input);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700598
Patrick Venture043d3232018-08-31 10:10:53 -0700599 log<level::INFO>("Added sensor to dbus after successful read",
600 entry("FILE=%s", file.c_str()));
Patrick Venture0cd4f692019-06-21 13:39:40 -0700601
Patrick Venture52b40612018-12-19 13:36:41 -0800602 it = _rmSensors.erase(it);
Matthew Barth31d214c2018-03-26 09:54:27 -0500603 }
604 else
605 {
606 ++it;
607 }
608 }
609 else
610 {
611 // Sanity check to remove sensors that were re-added
Patrick Venture52b40612018-12-19 13:36:41 -0800612 it = _rmSensors.erase(it);
Matthew Barth31d214c2018-03-26 09:54:27 -0500613 }
614 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500615}
616
617// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4