blob: 4db0940e822cff8a0def51b33fe647cb8b0e1b0a [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"
Brad Bishope55ef3d2016-12-19 09:12:40 -050030
Patrick Venture043d3232018-08-31 10:10:53 -070031#include <cstdlib>
32#include <functional>
33#include <iostream>
34#include <memory>
35#include <phosphor-logging/elog-errors.hpp>
36#include <sstream>
37#include <string>
38#include <unordered_set>
Patrick Venture1e6324f2017-06-01 14:07:05 -070039#include <xyz/openbmc_project/Sensor/Device/error.hpp>
40
41using namespace phosphor::logging;
42
Saqib Khan973886d2017-03-15 14:01:16 -050043// Initialization for Warning Objects
44decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo =
45 &WarningObject::warningLow;
46decltype(Thresholds<WarningObject>::setHi) Thresholds<WarningObject>::setHi =
47 &WarningObject::warningHigh;
48decltype(Thresholds<WarningObject>::getLo) Thresholds<WarningObject>::getLo =
49 &WarningObject::warningLow;
50decltype(Thresholds<WarningObject>::getHi) Thresholds<WarningObject>::getHi =
51 &WarningObject::warningHigh;
Patrick Venture043d3232018-08-31 10:10:53 -070052decltype(
53 Thresholds<WarningObject>::alarmLo) Thresholds<WarningObject>::alarmLo =
Saqib Khan973886d2017-03-15 14:01:16 -050054 &WarningObject::warningAlarmLow;
Patrick Venture043d3232018-08-31 10:10:53 -070055decltype(
56 Thresholds<WarningObject>::alarmHi) Thresholds<WarningObject>::alarmHi =
Saqib Khan973886d2017-03-15 14:01:16 -050057 &WarningObject::warningAlarmHigh;
58
59// Initialization for Critical Objects
60decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo =
61 &CriticalObject::criticalLow;
62decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi =
63 &CriticalObject::criticalHigh;
64decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo =
65 &CriticalObject::criticalLow;
66decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi =
67 &CriticalObject::criticalHigh;
Patrick Venture043d3232018-08-31 10:10:53 -070068decltype(
69 Thresholds<CriticalObject>::alarmLo) Thresholds<CriticalObject>::alarmLo =
Saqib Khan973886d2017-03-15 14:01:16 -050070 &CriticalObject::criticalAlarmLow;
Patrick Venture043d3232018-08-31 10:10:53 -070071decltype(
72 Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi =
Saqib Khan973886d2017-03-15 14:01:16 -050073 &CriticalObject::criticalAlarmHigh;
74
Matthew Barth979c8062018-04-17 11:37:15 -050075std::string MainLoop::getID(SensorSet::container_t::const_reference sensor)
Matthew Barth31d214c2018-03-26 09:54:27 -050076{
Matthew Barth31d214c2018-03-26 09:54:27 -050077 std::string id;
78
79 /*
80 * Check if the value of the MODE_<item><X> env variable for the sensor
Matt Spinler7c424802018-05-04 10:52:40 -050081 * is set. If it is, then read the from the <item><X>_<mode>
Matthew Barth31d214c2018-03-26 09:54:27 -050082 * file. The name of the DBUS object would be the value of the env
Matt Spinler7c424802018-05-04 10:52:40 -050083 * variable LABEL_<item><mode value>. If the MODE_<item><X> env variable
Matthew Barth31d214c2018-03-26 09:54:27 -050084 * doesn't exist, then the name of DBUS object is the value of the env
85 * variable LABEL_<item><X>.
Matt Spinler7c424802018-05-04 10:52:40 -050086 *
87 * For example, if MODE_temp1 = "label", then code reads the temp1_label
88 * file. If it has a 5 in it, then it will use the following entry to
89 * name the object: LABEL_temp5 = "My DBus object name".
90 *
Matthew Barth31d214c2018-03-26 09:54:27 -050091 */
Patrick Venture7a5285d2018-04-17 19:15:05 -070092 auto mode = env::getEnv("MODE", sensor.first);
Matt Spinler7c424802018-05-04 10:52:40 -050093 if (!mode.empty())
Matthew Barth31d214c2018-03-26 09:54:27 -050094 {
Patrick Venture043d3232018-08-31 10:10:53 -070095 id = env::getIndirectID(_hwmonRoot + '/' + _instance + '/', mode,
96 sensor.first);
Matthew Barth31d214c2018-03-26 09:54:27 -050097
98 if (id.empty())
99 {
Matthew Barth979c8062018-04-17 11:37:15 -0500100 return id;
Matthew Barth31d214c2018-03-26 09:54:27 -0500101 }
102 }
103
104 // Use the ID we looked up above if there was one,
105 // otherwise use the standard one.
106 id = (id.empty()) ? sensor.first.second : id;
107
Matthew Barth979c8062018-04-17 11:37:15 -0500108 return id;
109}
110
Patrick Venture043d3232018-08-31 10:10:53 -0700111SensorIdentifiers
112 MainLoop::getIdentifiers(SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500113{
114 std::string id = getID(sensor);
115 std::string label;
116
117 if (!id.empty())
118 {
119 // Ignore inputs without a label.
120 label = env::getEnv("LABEL", sensor.first.first, id);
121 }
122
Patrick Venture043d3232018-08-31 10:10:53 -0700123 return std::make_tuple(std::move(id), std::move(label));
Matthew Barth979c8062018-04-17 11:37:15 -0500124}
125
126/**
127 * Reads the environment parameters of a sensor and creates an object with
128 * atleast the `Value` interface, otherwise returns without creating the object.
129 * If the `Value` interface is successfully created, by reading the sensor's
130 * corresponding sysfs file's value, the additional interfaces for the sensor
Matthew Barthd238e232018-04-17 12:01:50 -0500131 * are created and the InterfacesAdded signal is emitted. The object's state
132 * data is then returned for sensor state monitoring within the main loop.
Matthew Barth979c8062018-04-17 11:37:15 -0500133 */
William A. Kennington III4cbdfef2018-10-18 19:19:51 -0700134std::optional<ObjectStateData>
Patrick Venture043d3232018-08-31 10:10:53 -0700135 MainLoop::getObject(SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500136{
137 auto properties = getIdentifiers(sensor);
138 if (std::get<sensorID>(properties).empty() ||
139 std::get<sensorLabel>(properties).empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500140 {
Matthew Barthd238e232018-04-17 12:01:50 -0500141 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500142 }
143
Patrick Venture09791852018-04-17 17:40:00 -0700144 hwmon::Attributes attrs;
145 if (!hwmon::getAttributes(sensor.first.first, attrs))
Matthew Barth31d214c2018-03-26 09:54:27 -0500146 {
Matthew Barthd238e232018-04-17 12:01:50 -0500147 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500148 }
149
Patrick Venture043d3232018-08-31 10:10:53 -0700150 auto sensorObj =
151 std::make_unique<sensor::Sensor>(sensor.first, ioAccess, _devPath);
Matthew Barth9c431062018-05-07 13:55:29 -0500152
Matthew Barthb7985272018-04-17 10:50:36 -0500153 // Get list of return codes for removing sensors on device
154 auto devRmRCs = env::getEnv("REMOVERCS");
155 // Add sensor removal return codes defined at the device level
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500156 sensorObj->addRemoveRCs(devRmRCs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500157
158 std::string objectPath{_root};
159 objectPath.append(1, '/');
Patrick Venture09791852018-04-17 17:40:00 -0700160 objectPath.append(hwmon::getNamespace(attrs));
Matthew Barth31d214c2018-03-26 09:54:27 -0500161 objectPath.append(1, '/');
Matthew Barth979c8062018-04-17 11:37:15 -0500162 objectPath.append(std::get<sensorLabel>(properties));
Matthew Barth31d214c2018-03-26 09:54:27 -0500163
164 ObjectInfo info(&_bus, std::move(objectPath), Object());
Patrick Venture75e56c62018-04-20 18:10:15 -0700165 RetryIO retryIO(hwmonio::retries, hwmonio::delay);
Matthew Barthd4beecf2018-04-03 15:50:22 -0500166 if (rmSensors.find(sensor.first) != rmSensors.end())
167 {
168 // When adding a sensor that was purposely removed,
169 // don't retry on errors when reading its value
170 std::get<size_t>(retryIO) = 0;
171 }
Patrick Venture043d3232018-08-31 10:10:53 -0700172 auto valueInterface = static_cast<std::shared_ptr<ValueObject>>(nullptr);
Matthew Barth31d214c2018-03-26 09:54:27 -0500173 try
174 {
Matthew Barthca44c2e2018-04-24 15:33:25 -0500175 // Add status interface based on _fault file being present
Matthew Barth2e41b132018-05-07 14:15:45 -0500176 sensorObj->addStatus(info);
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500177 valueInterface = sensorObj->addValue(retryIO, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500178 }
179 catch (const std::system_error& e)
180 {
Patrick Venture043d3232018-08-31 10:10:53 -0700181 auto file =
182 sysfs::make_sysfs_path(ioAccess.path(), sensor.first.first,
183 sensor.first.second, hwmon::entry::cinput);
Matthew Barth31d214c2018-03-26 09:54:27 -0500184#ifndef REMOVE_ON_FAIL
185 // Check sensorAdjusts for sensor removal RCs
Matthew Barthac473092018-05-07 14:41:46 -0500186 auto& sAdjusts = sensorObj->getAdjusts();
187 if (sAdjusts.rmRCs.count(e.code().value()) > 0)
Matthew Barth31d214c2018-03-26 09:54:27 -0500188 {
Matthew Barthac473092018-05-07 14:41:46 -0500189 // Return code found in sensor return code removal list
190 if (rmSensors.find(sensor.first) == rmSensors.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500191 {
Matthew Barthac473092018-05-07 14:41:46 -0500192 // Trace for sensor not already removed from dbus
193 log<level::INFO>("Sensor not added to dbus for read fail",
Patrick Venture043d3232018-08-31 10:10:53 -0700194 entry("FILE=%s", file.c_str()),
195 entry("RC=%d", e.code().value()));
196 rmSensors[std::move(sensor.first)] = std::move(sensor.second);
Matthew Barth31d214c2018-03-26 09:54:27 -0500197 }
Matthew Barthac473092018-05-07 14:41:46 -0500198 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500199 }
200#endif
Patrick Venture043d3232018-08-31 10:10:53 -0700201 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error;
Matthew Barth31d214c2018-03-26 09:54:27 -0500202 report<ReadFailure>(
Patrick Venture043d3232018-08-31 10:10:53 -0700203 xyz::openbmc_project::Sensor::Device::ReadFailure::CALLOUT_ERRNO(
204 e.code().value()),
205 xyz::openbmc_project::Sensor::Device::ReadFailure::
206 CALLOUT_DEVICE_PATH(_devPath.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500207
Matthew Barth31d214c2018-03-26 09:54:27 -0500208 log<level::INFO>("Logging failing sysfs file",
Patrick Venture043d3232018-08-31 10:10:53 -0700209 entry("FILE=%s", file.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500210#ifdef REMOVE_ON_FAIL
Matthew Barthd238e232018-04-17 12:01:50 -0500211 return {}; /* skip adding this sensor for now. */
Matthew Barth31d214c2018-03-26 09:54:27 -0500212#else
213 exit(EXIT_FAILURE);
214#endif
215 }
216 auto sensorValue = valueInterface->value();
James Feistee73f5b2018-08-01 16:31:42 -0700217 int64_t scale = 0;
218 // scale the thresholds only if we're using doubles
219 if constexpr (std::is_same<SensorValueType, double>::value)
220 {
221 scale = sensorObj->getScale();
222 }
223 addThreshold<WarningObject>(sensor.first.first,
224 std::get<sensorID>(properties), sensorValue,
225 info, scale);
226 addThreshold<CriticalObject>(sensor.first.first,
227 std::get<sensorID>(properties), sensorValue,
228 info, scale);
Matthew Barth31d214c2018-03-26 09:54:27 -0500229
Patrick Venture043d3232018-08-31 10:10:53 -0700230 auto target =
231 addTarget<hwmon::FanSpeed>(sensor.first, ioAccess, _devPath, info);
Matthew Barth28f8e662018-03-26 16:57:36 -0500232 if (target)
Matthew Barth31d214c2018-03-26 09:54:27 -0500233 {
Matthew Barth28f8e662018-03-26 16:57:36 -0500234 target->enable();
Matthew Barth31d214c2018-03-26 09:54:27 -0500235 }
Matthew Barth28f8e662018-03-26 16:57:36 -0500236 addTarget<hwmon::FanPwm>(sensor.first, ioAccess, _devPath, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500237
238 // All the interfaces have been created. Go ahead
239 // and emit InterfacesAdded.
240 valueInterface->emit_object_added();
241
Matthew Barth9c431062018-05-07 13:55:29 -0500242 // Save sensor object specifications
243 sensorObjects[sensor.first] = std::move(sensorObj);
244
Matthew Barthd238e232018-04-17 12:01:50 -0500245 return std::make_pair(std::move(std::get<sensorLabel>(properties)),
246 std::move(info));
Matthew Barth31d214c2018-03-26 09:54:27 -0500247}
248
Patrick Venture043d3232018-08-31 10:10:53 -0700249MainLoop::MainLoop(sdbusplus::bus::bus&& bus, const std::string& param,
250 const std::string& path, const std::string& devPath,
251 const char* prefix, const char* root) :
252 _bus(std::move(bus)),
253 _manager(_bus, root), _pathParam(param), _hwmonRoot(), _instance(),
William A. Kennington III0fe4cb32018-10-18 19:19:58 -0700254 _devPath(devPath), _prefix(prefix), _root(root), state(), ioAccess(path),
William A. Kennington III0dd0c4d2018-10-18 19:20:01 -0700255 event(sdeventplus::Event::get_default()),
256 timer(event, std::bind(&MainLoop::read, this))
Brad Bishopd499ca62016-12-19 09:24:50 -0500257{
Patrick Venture73a50c72018-04-17 15:19:03 -0700258 // Strip off any trailing slashes.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500259 std::string p = path;
260 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500261 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500262 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500263 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500264
Patrick Venture73a50c72018-04-17 15:19:03 -0700265 // Given the furthest right /, set instance to
266 // the basename, and hwmonRoot to the leading path.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500267 auto n = p.rfind('/');
268 if (n != std::string::npos)
269 {
270 _instance.assign(p.substr(n + 1));
271 _hwmonRoot.assign(p.substr(0, n));
272 }
273
274 assert(!_instance.empty());
275 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500276}
277
278void MainLoop::shutdown() noexcept
279{
William A. Kennington III0fe4cb32018-10-18 19:19:58 -0700280 event.exit(0);
Brad Bishopd499ca62016-12-19 09:24:50 -0500281}
282
283void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500284{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600285 init();
286
Patrick Venture043d3232018-08-31 10:10:53 -0700287 std::function<void()> callback(std::bind(&MainLoop::read, this));
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600288 try
289 {
William A. Kennington III0dd0c4d2018-10-18 19:20:01 -0700290 timer.restart(std::chrono::microseconds(_interval));
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600291
292 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
293
294 // TODO: Issue#7 - Should probably periodically check the SensorSet
295 // for new entries.
296
William A. Kennington III0fe4cb32018-10-18 19:19:58 -0700297 _bus.attach_event(event.get(), SD_EVENT_PRIORITY_IMPORTANT);
298 event.loop();
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600299 }
William A. Kennington III0fe4cb32018-10-18 19:19:58 -0700300 catch (const std::exception& e)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600301 {
302 log<level::ERR>("Error in sysfs polling loop",
303 entry("ERROR=%s", e.what()));
304 throw;
305 }
306}
307
308void MainLoop::init()
309{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500310 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500311 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500312
Patrick Venturedb7ecb62018-10-23 19:42:23 -0700313 for (const auto& i : *sensors)
Brad Bishop75b4ab82017-01-06 09:33:50 -0500314 {
Matthew Barthd238e232018-04-17 12:01:50 -0500315 auto object = getObject(i);
316 if (object)
317 {
318 // Construct the SensorSet value
319 // std::tuple<SensorSet::mapped_type,
320 // std::string(Sensor Label),
321 // ObjectInfo>
Patrick Venture043d3232018-08-31 10:10:53 -0700322 auto value =
323 std::make_tuple(std::move(i.second), std::move((*object).first),
324 std::move((*object).second));
Matthew Barthd238e232018-04-17 12:01:50 -0500325
326 state[std::move(i.first)] = std::move(value);
327 }
Brad Bishop75b4ab82017-01-06 09:33:50 -0500328 }
329
Patrick Venture62503a42017-05-23 07:30:29 -0700330 /* If there are no sensors specified by labels, exit. */
331 if (0 == state.size())
332 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600333 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700334 }
335
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500336 {
Patrick Venturec897d8b2018-04-23 19:01:56 -0700337 std::stringstream ss;
Patrick Venture043d3232018-08-31 10:10:53 -0700338 ss << _prefix << "-"
Patrick Venturec897d8b2018-04-23 19:01:56 -0700339 << std::to_string(std::hash<std::string>{}(_devPath + _pathParam))
340 << ".Hwmon1";
341
342 _bus.request_name(ss.str().c_str());
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500343 }
344
Patrick Ventureab10f162017-05-22 09:44:50 -0700345 {
Patrick Venturea24c8802018-04-17 19:38:06 -0700346 auto interval = env::getEnv("INTERVAL");
347 if (!interval.empty())
Patrick Ventureab10f162017-05-22 09:44:50 -0700348 {
Patrick Venture50cf1c52018-04-18 09:21:41 -0700349 _interval = std::strtoull(interval.c_str(), NULL, 10);
Patrick Ventureab10f162017-05-22 09:44:50 -0700350 }
351 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600352}
Patrick Ventureab10f162017-05-22 09:44:50 -0700353
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600354void MainLoop::read()
355{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500356 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
357 // ensure the objects all exist?
358
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600359 // Iterate through all the sensors.
360 for (auto& i : state)
361 {
362 auto& attrs = std::get<0>(i.second);
363 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500364 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600365 // Read value from sensor.
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600366 std::string input = hwmon::entry::cinput;
Patrick Venture043d3232018-08-31 10:10:53 -0700367 if (i.first.first == "pwm")
368 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600369 input = "";
370 }
371
372 try
Brad Bishope55ef3d2016-12-19 09:12:40 -0500373 {
Patrick Venture685efa12018-10-12 18:00:13 -0700374 int64_t value;
Matthew Barth27c4a392018-04-25 14:38:51 -0500375 auto& objInfo = std::get<ObjectInfo>(i.second);
376 auto& obj = std::get<Object>(objInfo);
377
378 auto it = obj.find(InterfaceType::STATUS);
379 if (it != obj.end())
380 {
Matthew Barthbfcaf3d2018-04-25 15:05:58 -0500381 auto fault = ioAccess.read(
Patrick Venture043d3232018-08-31 10:10:53 -0700382 i.first.first, i.first.second, hwmon::entry::fault,
383 hwmonio::retries, hwmonio::delay);
William A. Kennington III4cbdfef2018-10-18 19:19:51 -0700384 auto statusIface =
385 std::any_cast<std::shared_ptr<StatusObject>>(
386 it->second);
Matthew Barthbfcaf3d2018-04-25 15:05:58 -0500387 if (!statusIface->functional((fault == 0) ? true : false))
Matthew Barth27c4a392018-04-25 14:38:51 -0500388 {
389 continue;
390 }
391 }
392
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600393 // Retry for up to a second if device is busy
394 // or has a transient error.
Patrick Ventureb28f4322018-09-14 10:19:14 -0700395 std::unique_ptr<sensor::Sensor>& sensor =
396 sensorObjects[i.first];
397
398 sensor->unlockGpio();
Patrick Venture9331ab72018-01-29 09:48:47 -0800399
Patrick Venture043d3232018-08-31 10:10:53 -0700400 value = ioAccess.read(i.first.first, i.first.second, input,
401 hwmonio::retries, hwmonio::delay);
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600402
Patrick Ventureb28f4322018-09-14 10:19:14 -0700403 sensor->lockGpio();
404
405 value = sensor->adjustValue(value);
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600406
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600407 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500408 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600409 auto valueIface = std::shared_ptr<ValueObject>();
410 auto warnIface = std::shared_ptr<WarningObject>();
411 auto critIface = std::shared_ptr<CriticalObject>();
Brad Bishop754d38c2017-09-08 00:46:58 -0400412
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600413 switch (iface.first)
Brad Bishope0b7d052017-01-06 15:30:23 -0500414 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600415 case InterfaceType::VALUE:
William A. Kennington III4cbdfef2018-10-18 19:19:51 -0700416 valueIface =
417 std::any_cast<std::shared_ptr<ValueObject>>(
418 iface.second);
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600419 valueIface->value(value);
420 break;
421 case InterfaceType::WARN:
422 checkThresholds<WarningObject>(iface.second, value);
423 break;
424 case InterfaceType::CRIT:
Patrick Venture043d3232018-08-31 10:10:53 -0700425 checkThresholds<CriticalObject>(iface.second,
426 value);
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600427 break;
428 default:
429 break;
Brad Bishope0b7d052017-01-06 15:30:23 -0500430 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500431 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600432 }
433 catch (const std::system_error& e)
434 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500435 auto file = sysfs::make_sysfs_path(
Patrick Venture043d3232018-08-31 10:10:53 -0700436 ioAccess.path(), i.first.first, i.first.second,
437 hwmon::entry::cinput);
Matthew Barth8772ce32018-03-22 16:03:06 -0500438#ifndef REMOVE_ON_FAIL
439 // Check sensorAdjusts for sensor removal RCs
Matthew Barthac473092018-05-07 14:41:46 -0500440 auto& sAdjusts = sensorObjects[i.first]->getAdjusts();
441 if (sAdjusts.rmRCs.count(e.code().value()) > 0)
Matthew Barth8772ce32018-03-22 16:03:06 -0500442 {
Matthew Barthac473092018-05-07 14:41:46 -0500443 // Return code found in sensor return code removal list
444 if (rmSensors.find(i.first) == rmSensors.end())
Matthew Barth8772ce32018-03-22 16:03:06 -0500445 {
Matthew Barthac473092018-05-07 14:41:46 -0500446 // Trace for sensor not already removed from dbus
447 log<level::INFO>(
Patrick Venture043d3232018-08-31 10:10:53 -0700448 "Remove sensor from dbus for read fail",
449 entry("FILE=%s", file.c_str()),
450 entry("RC=%d", e.code().value()));
Matthew Barthac473092018-05-07 14:41:46 -0500451 // Mark this sensor to be removed from dbus
452 rmSensors[i.first] = std::get<0>(i.second);
Matthew Barth8772ce32018-03-22 16:03:06 -0500453 }
Matthew Barthac473092018-05-07 14:41:46 -0500454 continue;
Matthew Barth8772ce32018-03-22 16:03:06 -0500455 }
456#endif
Patrick Venture043d3232018-08-31 10:10:53 -0700457 using namespace sdbusplus::xyz::openbmc_project::Sensor::
458 Device::Error;
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600459 report<ReadFailure>(
Patrick Venture043d3232018-08-31 10:10:53 -0700460 xyz::openbmc_project::Sensor::Device::ReadFailure::
461 CALLOUT_ERRNO(e.code().value()),
462 xyz::openbmc_project::Sensor::Device::ReadFailure::
463 CALLOUT_DEVICE_PATH(_devPath.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500464
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600465 log<level::INFO>("Logging failing sysfs file",
Patrick Venture043d3232018-08-31 10:10:53 -0700466 entry("FILE=%s", file.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500467
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500468#ifdef REMOVE_ON_FAIL
Matthew Barth33db92b2018-03-26 09:55:19 -0500469 rmSensors[i.first] = std::get<0>(i.second);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500470#else
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600471 exit(EXIT_FAILURE);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500472#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500473 }
474 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600475 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500476
Matthew Barth8772ce32018-03-22 16:03:06 -0500477 // Remove any sensors marked for removal
Patrick Venturedb7ecb62018-10-23 19:42:23 -0700478 for (const auto& i : rmSensors)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600479 {
Matthew Barth33db92b2018-03-26 09:55:19 -0500480 state.erase(i.first);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500481 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500482
483#ifndef REMOVE_ON_FAIL
484 // Attempt to add any sensors that were removed
485 auto it = rmSensors.begin();
486 while (it != rmSensors.end())
487 {
488 if (state.find(it->first) == state.end())
489 {
490 SensorSet::container_t::value_type ssValueType =
Patrick Venture043d3232018-08-31 10:10:53 -0700491 std::make_pair(it->first, it->second);
Matthew Barthd238e232018-04-17 12:01:50 -0500492 auto object = getObject(ssValueType);
493 if (object)
Matthew Barth31d214c2018-03-26 09:54:27 -0500494 {
Matthew Barthd238e232018-04-17 12:01:50 -0500495 // Construct the SensorSet value
496 // std::tuple<SensorSet::mapped_type,
497 // std::string(Sensor Label),
498 // ObjectInfo>
499 auto value = std::make_tuple(std::move(ssValueType.second),
500 std::move((*object).first),
501 std::move((*object).second));
502
503 state[std::move(ssValueType.first)] = std::move(value);
504
Matthew Barth31d214c2018-03-26 09:54:27 -0500505 // Sensor object added, erase entry from removal list
Matthew Barth38c74e72018-04-02 12:41:26 -0500506 auto file = sysfs::make_sysfs_path(
Patrick Venture043d3232018-08-31 10:10:53 -0700507 ioAccess.path(), it->first.first, it->first.second,
508 hwmon::entry::cinput);
509 log<level::INFO>("Added sensor to dbus after successful read",
510 entry("FILE=%s", file.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500511 it = rmSensors.erase(it);
512 }
513 else
514 {
515 ++it;
516 }
517 }
518 else
519 {
520 // Sanity check to remove sensors that were re-added
521 it = rmSensors.erase(it);
522 }
523 }
524#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500525}
526
527// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4