blob: 6908d78971c46a9ae0f7a9c7459f605459c64a77 [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
William A. Kennington III0e749752018-11-06 15:25:41 -080031#include <cassert>
Patrick Venture043d3232018-08-31 10:10:53 -070032#include <cstdlib>
33#include <functional>
34#include <iostream>
35#include <memory>
36#include <phosphor-logging/elog-errors.hpp>
37#include <sstream>
38#include <string>
39#include <unordered_set>
Patrick Venture1e6324f2017-06-01 14:07:05 -070040#include <xyz/openbmc_project/Sensor/Device/error.hpp>
41
42using namespace phosphor::logging;
43
Saqib Khan973886d2017-03-15 14:01:16 -050044// Initialization for Warning Objects
45decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo =
46 &WarningObject::warningLow;
47decltype(Thresholds<WarningObject>::setHi) Thresholds<WarningObject>::setHi =
48 &WarningObject::warningHigh;
49decltype(Thresholds<WarningObject>::getLo) Thresholds<WarningObject>::getLo =
50 &WarningObject::warningLow;
51decltype(Thresholds<WarningObject>::getHi) Thresholds<WarningObject>::getHi =
52 &WarningObject::warningHigh;
Patrick Venture043d3232018-08-31 10:10:53 -070053decltype(
54 Thresholds<WarningObject>::alarmLo) Thresholds<WarningObject>::alarmLo =
Saqib Khan973886d2017-03-15 14:01:16 -050055 &WarningObject::warningAlarmLow;
Patrick Venture043d3232018-08-31 10:10:53 -070056decltype(
57 Thresholds<WarningObject>::alarmHi) Thresholds<WarningObject>::alarmHi =
Saqib Khan973886d2017-03-15 14:01:16 -050058 &WarningObject::warningAlarmHigh;
59
60// Initialization for Critical Objects
61decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo =
62 &CriticalObject::criticalLow;
63decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi =
64 &CriticalObject::criticalHigh;
65decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo =
66 &CriticalObject::criticalLow;
67decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi =
68 &CriticalObject::criticalHigh;
Patrick Venture043d3232018-08-31 10:10:53 -070069decltype(
70 Thresholds<CriticalObject>::alarmLo) Thresholds<CriticalObject>::alarmLo =
Saqib Khan973886d2017-03-15 14:01:16 -050071 &CriticalObject::criticalAlarmLow;
Patrick Venture043d3232018-08-31 10:10:53 -070072decltype(
73 Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi =
Saqib Khan973886d2017-03-15 14:01:16 -050074 &CriticalObject::criticalAlarmHigh;
75
Matthew Barth979c8062018-04-17 11:37:15 -050076std::string MainLoop::getID(SensorSet::container_t::const_reference sensor)
Matthew Barth31d214c2018-03-26 09:54:27 -050077{
Matthew Barth31d214c2018-03-26 09:54:27 -050078 std::string id;
79
80 /*
81 * Check if the value of the MODE_<item><X> env variable for the sensor
Matt Spinler7c424802018-05-04 10:52:40 -050082 * is set. If it is, then read the from the <item><X>_<mode>
Matthew Barth31d214c2018-03-26 09:54:27 -050083 * file. The name of the DBUS object would be the value of the env
Matt Spinler7c424802018-05-04 10:52:40 -050084 * variable LABEL_<item><mode value>. If the MODE_<item><X> env variable
Matthew Barth31d214c2018-03-26 09:54:27 -050085 * doesn't exist, then the name of DBUS object is the value of the env
86 * variable LABEL_<item><X>.
Matt Spinler7c424802018-05-04 10:52:40 -050087 *
88 * For example, if MODE_temp1 = "label", then code reads the temp1_label
89 * file. If it has a 5 in it, then it will use the following entry to
90 * name the object: LABEL_temp5 = "My DBus object name".
91 *
Matthew Barth31d214c2018-03-26 09:54:27 -050092 */
Patrick Venture7a5285d2018-04-17 19:15:05 -070093 auto mode = env::getEnv("MODE", sensor.first);
Matt Spinler7c424802018-05-04 10:52:40 -050094 if (!mode.empty())
Matthew Barth31d214c2018-03-26 09:54:27 -050095 {
Patrick Venture043d3232018-08-31 10:10:53 -070096 id = env::getIndirectID(_hwmonRoot + '/' + _instance + '/', mode,
97 sensor.first);
Matthew Barth31d214c2018-03-26 09:54:27 -050098
99 if (id.empty())
100 {
Matthew Barth979c8062018-04-17 11:37:15 -0500101 return id;
Matthew Barth31d214c2018-03-26 09:54:27 -0500102 }
103 }
104
105 // Use the ID we looked up above if there was one,
106 // otherwise use the standard one.
107 id = (id.empty()) ? sensor.first.second : id;
108
Matthew Barth979c8062018-04-17 11:37:15 -0500109 return id;
110}
111
Patrick Venture043d3232018-08-31 10:10:53 -0700112SensorIdentifiers
113 MainLoop::getIdentifiers(SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500114{
115 std::string id = getID(sensor);
116 std::string label;
117
118 if (!id.empty())
119 {
120 // Ignore inputs without a label.
121 label = env::getEnv("LABEL", sensor.first.first, id);
122 }
123
Patrick Venture043d3232018-08-31 10:10:53 -0700124 return std::make_tuple(std::move(id), std::move(label));
Matthew Barth979c8062018-04-17 11:37:15 -0500125}
126
127/**
128 * Reads the environment parameters of a sensor and creates an object with
129 * atleast the `Value` interface, otherwise returns without creating the object.
130 * If the `Value` interface is successfully created, by reading the sensor's
131 * corresponding sysfs file's value, the additional interfaces for the sensor
Matthew Barthd238e232018-04-17 12:01:50 -0500132 * are created and the InterfacesAdded signal is emitted. The object's state
133 * data is then returned for sensor state monitoring within the main loop.
Matthew Barth979c8062018-04-17 11:37:15 -0500134 */
William A. Kennington III4cbdfef2018-10-18 19:19:51 -0700135std::optional<ObjectStateData>
Patrick Venture043d3232018-08-31 10:10:53 -0700136 MainLoop::getObject(SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500137{
138 auto properties = getIdentifiers(sensor);
139 if (std::get<sensorID>(properties).empty() ||
140 std::get<sensorLabel>(properties).empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500141 {
Matthew Barthd238e232018-04-17 12:01:50 -0500142 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500143 }
144
Patrick Venture09791852018-04-17 17:40:00 -0700145 hwmon::Attributes attrs;
146 if (!hwmon::getAttributes(sensor.first.first, attrs))
Matthew Barth31d214c2018-03-26 09:54:27 -0500147 {
Matthew Barthd238e232018-04-17 12:01:50 -0500148 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500149 }
150
Patrick Venture2864b062018-12-19 08:13:41 -0800151 /* Note: The sensor objects all share the same ioAccess object. */
Patrick Venture043d3232018-08-31 10:10:53 -0700152 auto sensorObj =
Patrick Venture16805a62019-06-21 14:19:33 -0700153 std::make_unique<sensor::Sensor>(sensor.first, _ioAccess, _devPath);
Matthew Barth9c431062018-05-07 13:55:29 -0500154
Matthew Barthb7985272018-04-17 10:50:36 -0500155 // Get list of return codes for removing sensors on device
156 auto devRmRCs = env::getEnv("REMOVERCS");
157 // Add sensor removal return codes defined at the device level
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500158 sensorObj->addRemoveRCs(devRmRCs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500159
160 std::string objectPath{_root};
161 objectPath.append(1, '/');
Patrick Venture09791852018-04-17 17:40:00 -0700162 objectPath.append(hwmon::getNamespace(attrs));
Matthew Barth31d214c2018-03-26 09:54:27 -0500163 objectPath.append(1, '/');
Matthew Barth979c8062018-04-17 11:37:15 -0500164 objectPath.append(std::get<sensorLabel>(properties));
Matthew Barth31d214c2018-03-26 09:54:27 -0500165
Patrick Venture62067232019-06-19 17:39:33 -0700166 ObjectInfo info(&_bus, std::move(objectPath), InterfaceMap());
Patrick Venture75e56c62018-04-20 18:10:15 -0700167 RetryIO retryIO(hwmonio::retries, hwmonio::delay);
Patrick Venture52b40612018-12-19 13:36:41 -0800168 if (_rmSensors.find(sensor.first) != _rmSensors.end())
Matthew Barthd4beecf2018-04-03 15:50:22 -0500169 {
170 // When adding a sensor that was purposely removed,
171 // don't retry on errors when reading its value
172 std::get<size_t>(retryIO) = 0;
173 }
Patrick Venture043d3232018-08-31 10:10:53 -0700174 auto valueInterface = static_cast<std::shared_ptr<ValueObject>>(nullptr);
Matthew Barth31d214c2018-03-26 09:54:27 -0500175 try
176 {
Matthew Barthca44c2e2018-04-24 15:33:25 -0500177 // Add status interface based on _fault file being present
Matthew Barth2e41b132018-05-07 14:15:45 -0500178 sensorObj->addStatus(info);
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500179 valueInterface = sensorObj->addValue(retryIO, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500180 }
181 catch (const std::system_error& e)
182 {
Patrick Venture043d3232018-08-31 10:10:53 -0700183 auto file =
Patrick Venture16805a62019-06-21 14:19:33 -0700184 sysfs::make_sysfs_path(_ioAccess->path(), sensor.first.first,
Patrick Venture043d3232018-08-31 10:10:53 -0700185 sensor.first.second, hwmon::entry::cinput);
Matthew Barth31d214c2018-03-26 09:54:27 -0500186#ifndef REMOVE_ON_FAIL
187 // Check sensorAdjusts for sensor removal RCs
Matthew Barthac473092018-05-07 14:41:46 -0500188 auto& sAdjusts = sensorObj->getAdjusts();
189 if (sAdjusts.rmRCs.count(e.code().value()) > 0)
Matthew Barth31d214c2018-03-26 09:54:27 -0500190 {
Matthew Barthac473092018-05-07 14:41:46 -0500191 // Return code found in sensor return code removal list
Patrick Venture52b40612018-12-19 13:36:41 -0800192 if (_rmSensors.find(sensor.first) == _rmSensors.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500193 {
Matthew Barthac473092018-05-07 14:41:46 -0500194 // Trace for sensor not already removed from dbus
195 log<level::INFO>("Sensor not added to dbus for read fail",
Patrick Venture043d3232018-08-31 10:10:53 -0700196 entry("FILE=%s", file.c_str()),
197 entry("RC=%d", e.code().value()));
Patrick Venture52b40612018-12-19 13:36:41 -0800198 _rmSensors[std::move(sensor.first)] = std::move(sensor.second);
Matthew Barth31d214c2018-03-26 09:54:27 -0500199 }
Matthew Barthac473092018-05-07 14:41:46 -0500200 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500201 }
202#endif
Patrick Venture043d3232018-08-31 10:10:53 -0700203 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error;
Matthew Barth31d214c2018-03-26 09:54:27 -0500204 report<ReadFailure>(
Patrick Venture043d3232018-08-31 10:10:53 -0700205 xyz::openbmc_project::Sensor::Device::ReadFailure::CALLOUT_ERRNO(
206 e.code().value()),
207 xyz::openbmc_project::Sensor::Device::ReadFailure::
208 CALLOUT_DEVICE_PATH(_devPath.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500209
Matthew Barth31d214c2018-03-26 09:54:27 -0500210 log<level::INFO>("Logging failing sysfs file",
Patrick Venture043d3232018-08-31 10:10:53 -0700211 entry("FILE=%s", file.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500212#ifdef REMOVE_ON_FAIL
Matthew Barthd238e232018-04-17 12:01:50 -0500213 return {}; /* skip adding this sensor for now. */
Matthew Barth31d214c2018-03-26 09:54:27 -0500214#else
215 exit(EXIT_FAILURE);
216#endif
217 }
218 auto sensorValue = valueInterface->value();
James Feistee73f5b2018-08-01 16:31:42 -0700219 int64_t scale = 0;
220 // scale the thresholds only if we're using doubles
221 if constexpr (std::is_same<SensorValueType, double>::value)
222 {
223 scale = sensorObj->getScale();
224 }
225 addThreshold<WarningObject>(sensor.first.first,
226 std::get<sensorID>(properties), sensorValue,
227 info, scale);
228 addThreshold<CriticalObject>(sensor.first.first,
229 std::get<sensorID>(properties), sensorValue,
230 info, scale);
Matthew Barth31d214c2018-03-26 09:54:27 -0500231
Patrick Venture043d3232018-08-31 10:10:53 -0700232 auto target =
Patrick Venture52b40612018-12-19 13:36:41 -0800233 addTarget<hwmon::FanSpeed>(sensor.first, _ioAccess, _devPath, info);
Matthew Barth28f8e662018-03-26 16:57:36 -0500234 if (target)
Matthew Barth31d214c2018-03-26 09:54:27 -0500235 {
Matthew Barth28f8e662018-03-26 16:57:36 -0500236 target->enable();
Matthew Barth31d214c2018-03-26 09:54:27 -0500237 }
Patrick Venture52b40612018-12-19 13:36:41 -0800238 addTarget<hwmon::FanPwm>(sensor.first, _ioAccess, _devPath, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500239
240 // All the interfaces have been created. Go ahead
241 // and emit InterfacesAdded.
242 valueInterface->emit_object_added();
243
Matthew Barth9c431062018-05-07 13:55:29 -0500244 // Save sensor object specifications
Patrick Venture52b40612018-12-19 13:36:41 -0800245 _sensorObjects[sensor.first] = std::move(sensorObj);
Matthew Barth9c431062018-05-07 13:55:29 -0500246
Matthew Barthd238e232018-04-17 12:01:50 -0500247 return std::make_pair(std::move(std::get<sensorLabel>(properties)),
248 std::move(info));
Matthew Barth31d214c2018-03-26 09:54:27 -0500249}
250
Patrick Venture043d3232018-08-31 10:10:53 -0700251MainLoop::MainLoop(sdbusplus::bus::bus&& bus, const std::string& param,
252 const std::string& path, const std::string& devPath,
Patrick Venture16805a62019-06-21 14:19:33 -0700253 const char* prefix, const char* root,
254 const hwmonio::HwmonIOInterface* ioIntf) :
Patrick Venture043d3232018-08-31 10:10:53 -0700255 _bus(std::move(bus)),
256 _manager(_bus, root), _pathParam(param), _hwmonRoot(), _instance(),
Patrick Venture16805a62019-06-21 14:19:33 -0700257 _devPath(devPath), _prefix(prefix), _root(root), _state(),
258 _ioAccess(ioIntf), _event(sdeventplus::Event::get_default()),
Patrick Venture52b40612018-12-19 13:36:41 -0800259 _timer(_event, std::bind(&MainLoop::read, this))
Brad Bishopd499ca62016-12-19 09:24:50 -0500260{
Patrick Venture73a50c72018-04-17 15:19:03 -0700261 // Strip off any trailing slashes.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500262 std::string p = path;
263 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500264 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500265 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500266 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500267
Patrick Venture73a50c72018-04-17 15:19:03 -0700268 // Given the furthest right /, set instance to
269 // the basename, and hwmonRoot to the leading path.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500270 auto n = p.rfind('/');
271 if (n != std::string::npos)
272 {
273 _instance.assign(p.substr(n + 1));
274 _hwmonRoot.assign(p.substr(0, n));
275 }
276
277 assert(!_instance.empty());
278 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500279}
280
281void MainLoop::shutdown() noexcept
282{
Patrick Venture52b40612018-12-19 13:36:41 -0800283 _event.exit(0);
Brad Bishopd499ca62016-12-19 09:24:50 -0500284}
285
286void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500287{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600288 init();
289
Patrick Venture043d3232018-08-31 10:10:53 -0700290 std::function<void()> callback(std::bind(&MainLoop::read, this));
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600291 try
292 {
Patrick Venture52b40612018-12-19 13:36:41 -0800293 _timer.restart(std::chrono::microseconds(_interval));
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600294
295 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
296
297 // TODO: Issue#7 - Should probably periodically check the SensorSet
298 // for new entries.
299
Patrick Venture52b40612018-12-19 13:36:41 -0800300 _bus.attach_event(_event.get(), SD_EVENT_PRIORITY_IMPORTANT);
301 _event.loop();
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600302 }
William A. Kennington III0fe4cb32018-10-18 19:19:58 -0700303 catch (const std::exception& e)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600304 {
305 log<level::ERR>("Error in sysfs polling loop",
306 entry("ERROR=%s", e.what()));
307 throw;
308 }
309}
310
311void MainLoop::init()
312{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500313 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500314 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500315
Patrick Venturedb7ecb62018-10-23 19:42:23 -0700316 for (const auto& i : *sensors)
Brad Bishop75b4ab82017-01-06 09:33:50 -0500317 {
Matthew Barthd238e232018-04-17 12:01:50 -0500318 auto object = getObject(i);
319 if (object)
320 {
321 // Construct the SensorSet value
322 // std::tuple<SensorSet::mapped_type,
323 // std::string(Sensor Label),
324 // ObjectInfo>
Patrick Venture043d3232018-08-31 10:10:53 -0700325 auto value =
326 std::make_tuple(std::move(i.second), std::move((*object).first),
327 std::move((*object).second));
Matthew Barthd238e232018-04-17 12:01:50 -0500328
Patrick Venture52b40612018-12-19 13:36:41 -0800329 _state[std::move(i.first)] = std::move(value);
Matthew Barthd238e232018-04-17 12:01:50 -0500330 }
Brad Bishop75b4ab82017-01-06 09:33:50 -0500331 }
332
Patrick Venture62503a42017-05-23 07:30:29 -0700333 /* If there are no sensors specified by labels, exit. */
Patrick Venture52b40612018-12-19 13:36:41 -0800334 if (0 == _state.size())
Patrick Venture62503a42017-05-23 07:30:29 -0700335 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600336 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700337 }
338
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500339 {
Patrick Venturec897d8b2018-04-23 19:01:56 -0700340 std::stringstream ss;
Patrick Venture043d3232018-08-31 10:10:53 -0700341 ss << _prefix << "-"
Patrick Venturec897d8b2018-04-23 19:01:56 -0700342 << std::to_string(std::hash<std::string>{}(_devPath + _pathParam))
343 << ".Hwmon1";
344
345 _bus.request_name(ss.str().c_str());
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500346 }
347
Patrick Ventureab10f162017-05-22 09:44:50 -0700348 {
Patrick Venturea24c8802018-04-17 19:38:06 -0700349 auto interval = env::getEnv("INTERVAL");
350 if (!interval.empty())
Patrick Ventureab10f162017-05-22 09:44:50 -0700351 {
Patrick Venture50cf1c52018-04-18 09:21:41 -0700352 _interval = std::strtoull(interval.c_str(), NULL, 10);
Patrick Ventureab10f162017-05-22 09:44:50 -0700353 }
354 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600355}
Patrick Ventureab10f162017-05-22 09:44:50 -0700356
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600357void MainLoop::read()
358{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500359 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
360 // ensure the objects all exist?
361
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600362 // Iterate through all the sensors.
Patrick Venture52b40612018-12-19 13:36:41 -0800363 for (auto& i : _state)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600364 {
365 auto& attrs = std::get<0>(i.second);
366 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500367 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600368 // Read value from sensor.
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600369 std::string input = hwmon::entry::cinput;
Patrick Venture043d3232018-08-31 10:10:53 -0700370 if (i.first.first == "pwm")
371 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600372 input = "";
373 }
374
375 try
Brad Bishope55ef3d2016-12-19 09:12:40 -0500376 {
Patrick Venture685efa12018-10-12 18:00:13 -0700377 int64_t value;
Matthew Barth27c4a392018-04-25 14:38:51 -0500378 auto& objInfo = std::get<ObjectInfo>(i.second);
Patrick Venture62067232019-06-19 17:39:33 -0700379 auto& obj = std::get<InterfaceMap>(objInfo);
Brandon Kim86dcac82019-06-18 17:48:51 -0700380 std::unique_ptr<sensor::Sensor>& sensor =
381 _sensorObjects[i.first];
Matthew Barth27c4a392018-04-25 14:38:51 -0500382
Brandon Kim86dcac82019-06-18 17:48:51 -0700383 auto& statusIface =
384 std::any_cast<std::shared_ptr<StatusObject>&>(
385 obj[InterfaceType::STATUS]);
386 // As long as addStatus is called before addValue, statusIface
387 // should never be nullptr.
388 assert(statusIface);
389
390 if (sensor->hasFaultFile())
Matthew Barth27c4a392018-04-25 14:38:51 -0500391 {
Patrick Venture16805a62019-06-21 14:19:33 -0700392 auto fault = _ioAccess->read(
Patrick Venture043d3232018-08-31 10:10:53 -0700393 i.first.first, i.first.second, hwmon::entry::fault,
394 hwmonio::retries, hwmonio::delay);
Brandon Kim86dcac82019-06-18 17:48:51 -0700395 // Skip reading from a sensor with a valid fault file
396 // and set the functional property accordingly
Matthew Barthbfcaf3d2018-04-25 15:05:58 -0500397 if (!statusIface->functional((fault == 0) ? true : false))
Matthew Barth27c4a392018-04-25 14:38:51 -0500398 {
399 continue;
400 }
401 }
402
Brandon Kimdb76d492019-06-17 11:53:04 -0700403 {
404 // RAII object for GPIO unlock / lock
405 sensor::GpioLock gpioLock(sensor->getGpio());
Patrick Venture9331ab72018-01-29 09:48:47 -0800406
Brandon Kim86dcac82019-06-18 17:48:51 -0700407 // Retry for up to a second if device is busy
408 // or has a transient error.
Patrick Venture16805a62019-06-21 14:19:33 -0700409 value =
410 _ioAccess->read(i.first.first, i.first.second, input,
411 hwmonio::retries, hwmonio::delay);
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600412
Brandon Kimdb76d492019-06-17 11:53:04 -0700413 value = sensor->adjustValue(value);
414 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600415
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600416 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500417 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600418 auto valueIface = std::shared_ptr<ValueObject>();
419 auto warnIface = std::shared_ptr<WarningObject>();
420 auto critIface = std::shared_ptr<CriticalObject>();
Brad Bishop754d38c2017-09-08 00:46:58 -0400421
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600422 switch (iface.first)
Brad Bishope0b7d052017-01-06 15:30:23 -0500423 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600424 case InterfaceType::VALUE:
William A. Kennington III4cbdfef2018-10-18 19:19:51 -0700425 valueIface =
426 std::any_cast<std::shared_ptr<ValueObject>>(
427 iface.second);
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600428 valueIface->value(value);
429 break;
430 case InterfaceType::WARN:
431 checkThresholds<WarningObject>(iface.second, value);
432 break;
433 case InterfaceType::CRIT:
Patrick Venture043d3232018-08-31 10:10:53 -0700434 checkThresholds<CriticalObject>(iface.second,
435 value);
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600436 break;
437 default:
438 break;
Brad Bishope0b7d052017-01-06 15:30:23 -0500439 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500440 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600441 }
442 catch (const std::system_error& e)
443 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500444 auto file = sysfs::make_sysfs_path(
Patrick Venture16805a62019-06-21 14:19:33 -0700445 _ioAccess->path(), i.first.first, i.first.second,
Patrick Venture043d3232018-08-31 10:10:53 -0700446 hwmon::entry::cinput);
Matthew Barth8772ce32018-03-22 16:03:06 -0500447#ifndef REMOVE_ON_FAIL
448 // Check sensorAdjusts for sensor removal RCs
Patrick Venture52b40612018-12-19 13:36:41 -0800449 auto& sAdjusts = _sensorObjects[i.first]->getAdjusts();
Matthew Barthac473092018-05-07 14:41:46 -0500450 if (sAdjusts.rmRCs.count(e.code().value()) > 0)
Matthew Barth8772ce32018-03-22 16:03:06 -0500451 {
Matthew Barthac473092018-05-07 14:41:46 -0500452 // Return code found in sensor return code removal list
Patrick Venture52b40612018-12-19 13:36:41 -0800453 if (_rmSensors.find(i.first) == _rmSensors.end())
Matthew Barth8772ce32018-03-22 16:03:06 -0500454 {
Matthew Barthac473092018-05-07 14:41:46 -0500455 // Trace for sensor not already removed from dbus
456 log<level::INFO>(
Patrick Venture043d3232018-08-31 10:10:53 -0700457 "Remove sensor from dbus for read fail",
458 entry("FILE=%s", file.c_str()),
459 entry("RC=%d", e.code().value()));
Matthew Barthac473092018-05-07 14:41:46 -0500460 // Mark this sensor to be removed from dbus
Patrick Venture52b40612018-12-19 13:36:41 -0800461 _rmSensors[i.first] = std::get<0>(i.second);
Matthew Barth8772ce32018-03-22 16:03:06 -0500462 }
Matthew Barthac473092018-05-07 14:41:46 -0500463 continue;
Matthew Barth8772ce32018-03-22 16:03:06 -0500464 }
465#endif
Patrick Venture043d3232018-08-31 10:10:53 -0700466 using namespace sdbusplus::xyz::openbmc_project::Sensor::
467 Device::Error;
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600468 report<ReadFailure>(
Patrick Venture043d3232018-08-31 10:10:53 -0700469 xyz::openbmc_project::Sensor::Device::ReadFailure::
470 CALLOUT_ERRNO(e.code().value()),
471 xyz::openbmc_project::Sensor::Device::ReadFailure::
472 CALLOUT_DEVICE_PATH(_devPath.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500473
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600474 log<level::INFO>("Logging failing sysfs file",
Patrick Venture043d3232018-08-31 10:10:53 -0700475 entry("FILE=%s", file.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500476
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500477#ifdef REMOVE_ON_FAIL
Patrick Venture52b40612018-12-19 13:36:41 -0800478 _rmSensors[i.first] = std::get<0>(i.second);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500479#else
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600480 exit(EXIT_FAILURE);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500481#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500482 }
483 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600484 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500485
Patrick Venture0cd4f692019-06-21 13:39:40 -0700486 removeSensors();
487
488#ifndef REMOVE_ON_FAIL
489 addDroppedSensors();
490#endif
491}
492
493void MainLoop::removeSensors()
494{
Matthew Barth8772ce32018-03-22 16:03:06 -0500495 // Remove any sensors marked for removal
Patrick Venture52b40612018-12-19 13:36:41 -0800496 for (const auto& i : _rmSensors)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600497 {
Matthew Barthd0ce7922019-06-06 09:23:37 -0500498 // Remove sensor object from dbus using emit_object_removed()
499 auto& objInfo = std::get<ObjectInfo>(_state[i.first]);
500 auto& objPath = std::get<std::string>(objInfo);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700501
Matthew Barthd0ce7922019-06-06 09:23:37 -0500502 _bus.emit_object_removed(objPath.c_str());
Patrick Venture0cd4f692019-06-21 13:39:40 -0700503
Matthew Barthd0ce7922019-06-06 09:23:37 -0500504 // Erase sensor object info
Patrick Venture52b40612018-12-19 13:36:41 -0800505 _state.erase(i.first);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500506 }
Patrick Venture0cd4f692019-06-21 13:39:40 -0700507}
Matthew Barth31d214c2018-03-26 09:54:27 -0500508
Patrick Venture0cd4f692019-06-21 13:39:40 -0700509void MainLoop::addDroppedSensors()
510{
Matthew Barth31d214c2018-03-26 09:54:27 -0500511 // Attempt to add any sensors that were removed
Patrick Venture52b40612018-12-19 13:36:41 -0800512 auto it = _rmSensors.begin();
513 while (it != _rmSensors.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500514 {
Patrick Venture52b40612018-12-19 13:36:41 -0800515 if (_state.find(it->first) == _state.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500516 {
517 SensorSet::container_t::value_type ssValueType =
Patrick Venture043d3232018-08-31 10:10:53 -0700518 std::make_pair(it->first, it->second);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700519
Matthew Barthd238e232018-04-17 12:01:50 -0500520 auto object = getObject(ssValueType);
521 if (object)
Matthew Barth31d214c2018-03-26 09:54:27 -0500522 {
Matthew Barthd238e232018-04-17 12:01:50 -0500523 // Construct the SensorSet value
524 // std::tuple<SensorSet::mapped_type,
525 // std::string(Sensor Label),
526 // ObjectInfo>
527 auto value = std::make_tuple(std::move(ssValueType.second),
528 std::move((*object).first),
529 std::move((*object).second));
530
Patrick Venture52b40612018-12-19 13:36:41 -0800531 _state[std::move(ssValueType.first)] = std::move(value);
Matthew Barthd238e232018-04-17 12:01:50 -0500532
Matthew Barth31d214c2018-03-26 09:54:27 -0500533 // Sensor object added, erase entry from removal list
Matthew Barth38c74e72018-04-02 12:41:26 -0500534 auto file = sysfs::make_sysfs_path(
Patrick Venture16805a62019-06-21 14:19:33 -0700535 _ioAccess->path(), it->first.first, it->first.second,
Patrick Venture043d3232018-08-31 10:10:53 -0700536 hwmon::entry::cinput);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700537
Patrick Venture043d3232018-08-31 10:10:53 -0700538 log<level::INFO>("Added sensor to dbus after successful read",
539 entry("FILE=%s", file.c_str()));
Patrick Venture0cd4f692019-06-21 13:39:40 -0700540
Patrick Venture52b40612018-12-19 13:36:41 -0800541 it = _rmSensors.erase(it);
Matthew Barth31d214c2018-03-26 09:54:27 -0500542 }
543 else
544 {
545 ++it;
546 }
547 }
548 else
549 {
550 // Sanity check to remove sensors that were re-added
Patrick Venture52b40612018-12-19 13:36:41 -0800551 it = _rmSensors.erase(it);
Matthew Barth31d214c2018-03-26 09:54:27 -0500552 }
553 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500554}
555
556// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4