blob: d961a1022708cb356dff017dfe52374c2d48d87a [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
Patrick Venturefeb744a2019-06-26 19:07:48 -070076void updateSensorInterfaces(InterfaceMap& ifaces, int64_t value)
77{
78 for (auto& iface : ifaces)
79 {
80 switch (iface.first)
81 {
82 case InterfaceType::VALUE:
83 {
84 auto& valueIface =
85 std::any_cast<std::shared_ptr<ValueObject>&>(iface.second);
86 valueIface->value(value);
87 }
88 break;
89 case InterfaceType::WARN:
90 checkThresholds<WarningObject>(iface.second, value);
91 break;
92 case InterfaceType::CRIT:
93 checkThresholds<CriticalObject>(iface.second, value);
94 break;
95 default:
96 break;
97 }
98 }
99}
100
Matthew Barth979c8062018-04-17 11:37:15 -0500101std::string MainLoop::getID(SensorSet::container_t::const_reference sensor)
Matthew Barth31d214c2018-03-26 09:54:27 -0500102{
Matthew Barth31d214c2018-03-26 09:54:27 -0500103 std::string id;
104
105 /*
106 * Check if the value of the MODE_<item><X> env variable for the sensor
Matt Spinler7c424802018-05-04 10:52:40 -0500107 * is set. If it is, then read the from the <item><X>_<mode>
Matthew Barth31d214c2018-03-26 09:54:27 -0500108 * file. The name of the DBUS object would be the value of the env
Matt Spinler7c424802018-05-04 10:52:40 -0500109 * variable LABEL_<item><mode value>. If the MODE_<item><X> env variable
Matthew Barth31d214c2018-03-26 09:54:27 -0500110 * doesn't exist, then the name of DBUS object is the value of the env
111 * variable LABEL_<item><X>.
Matt Spinler7c424802018-05-04 10:52:40 -0500112 *
113 * For example, if MODE_temp1 = "label", then code reads the temp1_label
114 * file. If it has a 5 in it, then it will use the following entry to
115 * name the object: LABEL_temp5 = "My DBus object name".
116 *
Matthew Barth31d214c2018-03-26 09:54:27 -0500117 */
Patrick Venture7a5285d2018-04-17 19:15:05 -0700118 auto mode = env::getEnv("MODE", sensor.first);
Matt Spinler7c424802018-05-04 10:52:40 -0500119 if (!mode.empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500120 {
Patrick Venture043d3232018-08-31 10:10:53 -0700121 id = env::getIndirectID(_hwmonRoot + '/' + _instance + '/', mode,
122 sensor.first);
Matthew Barth31d214c2018-03-26 09:54:27 -0500123
124 if (id.empty())
125 {
Matthew Barth979c8062018-04-17 11:37:15 -0500126 return id;
Matthew Barth31d214c2018-03-26 09:54:27 -0500127 }
128 }
129
130 // Use the ID we looked up above if there was one,
131 // otherwise use the standard one.
132 id = (id.empty()) ? sensor.first.second : id;
133
Matthew Barth979c8062018-04-17 11:37:15 -0500134 return id;
135}
136
Patrick Venture043d3232018-08-31 10:10:53 -0700137SensorIdentifiers
138 MainLoop::getIdentifiers(SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500139{
140 std::string id = getID(sensor);
141 std::string label;
142
143 if (!id.empty())
144 {
145 // Ignore inputs without a label.
146 label = env::getEnv("LABEL", sensor.first.first, id);
147 }
148
Patrick Venture043d3232018-08-31 10:10:53 -0700149 return std::make_tuple(std::move(id), std::move(label));
Matthew Barth979c8062018-04-17 11:37:15 -0500150}
151
152/**
153 * Reads the environment parameters of a sensor and creates an object with
154 * atleast the `Value` interface, otherwise returns without creating the object.
155 * If the `Value` interface is successfully created, by reading the sensor's
156 * corresponding sysfs file's value, the additional interfaces for the sensor
Matthew Barthd238e232018-04-17 12:01:50 -0500157 * are created and the InterfacesAdded signal is emitted. The object's state
158 * data is then returned for sensor state monitoring within the main loop.
Matthew Barth979c8062018-04-17 11:37:15 -0500159 */
William A. Kennington III4cbdfef2018-10-18 19:19:51 -0700160std::optional<ObjectStateData>
Patrick Venture043d3232018-08-31 10:10:53 -0700161 MainLoop::getObject(SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500162{
163 auto properties = getIdentifiers(sensor);
164 if (std::get<sensorID>(properties).empty() ||
165 std::get<sensorLabel>(properties).empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500166 {
Matthew Barthd238e232018-04-17 12:01:50 -0500167 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500168 }
169
Patrick Venture09791852018-04-17 17:40:00 -0700170 hwmon::Attributes attrs;
171 if (!hwmon::getAttributes(sensor.first.first, attrs))
Matthew Barth31d214c2018-03-26 09:54:27 -0500172 {
Matthew Barthd238e232018-04-17 12:01:50 -0500173 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500174 }
175
Patrick Venture2864b062018-12-19 08:13:41 -0800176 /* Note: The sensor objects all share the same ioAccess object. */
Patrick Venture043d3232018-08-31 10:10:53 -0700177 auto sensorObj =
Patrick Venture16805a62019-06-21 14:19:33 -0700178 std::make_unique<sensor::Sensor>(sensor.first, _ioAccess, _devPath);
Matthew Barth9c431062018-05-07 13:55:29 -0500179
Matthew Barthb7985272018-04-17 10:50:36 -0500180 // Get list of return codes for removing sensors on device
181 auto devRmRCs = env::getEnv("REMOVERCS");
182 // Add sensor removal return codes defined at the device level
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500183 sensorObj->addRemoveRCs(devRmRCs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500184
185 std::string objectPath{_root};
186 objectPath.append(1, '/');
Patrick Venture09791852018-04-17 17:40:00 -0700187 objectPath.append(hwmon::getNamespace(attrs));
Matthew Barth31d214c2018-03-26 09:54:27 -0500188 objectPath.append(1, '/');
Matthew Barth979c8062018-04-17 11:37:15 -0500189 objectPath.append(std::get<sensorLabel>(properties));
Matthew Barth31d214c2018-03-26 09:54:27 -0500190
Patrick Venture62067232019-06-19 17:39:33 -0700191 ObjectInfo info(&_bus, std::move(objectPath), InterfaceMap());
Patrick Venture75e56c62018-04-20 18:10:15 -0700192 RetryIO retryIO(hwmonio::retries, hwmonio::delay);
Patrick Venture52b40612018-12-19 13:36:41 -0800193 if (_rmSensors.find(sensor.first) != _rmSensors.end())
Matthew Barthd4beecf2018-04-03 15:50:22 -0500194 {
195 // When adding a sensor that was purposely removed,
196 // don't retry on errors when reading its value
197 std::get<size_t>(retryIO) = 0;
198 }
Patrick Venture043d3232018-08-31 10:10:53 -0700199 auto valueInterface = static_cast<std::shared_ptr<ValueObject>>(nullptr);
Matthew Barth31d214c2018-03-26 09:54:27 -0500200 try
201 {
Matthew Barthca44c2e2018-04-24 15:33:25 -0500202 // Add status interface based on _fault file being present
Matthew Barth2e41b132018-05-07 14:15:45 -0500203 sensorObj->addStatus(info);
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500204 valueInterface = sensorObj->addValue(retryIO, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500205 }
206 catch (const std::system_error& e)
207 {
Patrick Venture043d3232018-08-31 10:10:53 -0700208 auto file =
Patrick Venture16805a62019-06-21 14:19:33 -0700209 sysfs::make_sysfs_path(_ioAccess->path(), sensor.first.first,
Patrick Venture043d3232018-08-31 10:10:53 -0700210 sensor.first.second, hwmon::entry::cinput);
Patrick Venture0892c3f2019-06-27 14:24:03 -0700211
Matthew Barth31d214c2018-03-26 09:54:27 -0500212 // Check sensorAdjusts for sensor removal RCs
Matthew Barthac473092018-05-07 14:41:46 -0500213 auto& sAdjusts = sensorObj->getAdjusts();
214 if (sAdjusts.rmRCs.count(e.code().value()) > 0)
Matthew Barth31d214c2018-03-26 09:54:27 -0500215 {
Matthew Barthac473092018-05-07 14:41:46 -0500216 // Return code found in sensor return code removal list
Patrick Venture52b40612018-12-19 13:36:41 -0800217 if (_rmSensors.find(sensor.first) == _rmSensors.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500218 {
Matthew Barthac473092018-05-07 14:41:46 -0500219 // Trace for sensor not already removed from dbus
220 log<level::INFO>("Sensor not added to dbus for read fail",
Patrick Venture043d3232018-08-31 10:10:53 -0700221 entry("FILE=%s", file.c_str()),
222 entry("RC=%d", e.code().value()));
Patrick Venture52b40612018-12-19 13:36:41 -0800223 _rmSensors[std::move(sensor.first)] = std::move(sensor.second);
Matthew Barth31d214c2018-03-26 09:54:27 -0500224 }
Matthew Barthac473092018-05-07 14:41:46 -0500225 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500226 }
Patrick Venture0892c3f2019-06-27 14:24:03 -0700227
Patrick Venture043d3232018-08-31 10:10:53 -0700228 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error;
Matthew Barth31d214c2018-03-26 09:54:27 -0500229 report<ReadFailure>(
Patrick Venture043d3232018-08-31 10:10:53 -0700230 xyz::openbmc_project::Sensor::Device::ReadFailure::CALLOUT_ERRNO(
231 e.code().value()),
232 xyz::openbmc_project::Sensor::Device::ReadFailure::
233 CALLOUT_DEVICE_PATH(_devPath.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500234
Matthew Barth31d214c2018-03-26 09:54:27 -0500235 log<level::INFO>("Logging failing sysfs file",
Patrick Venture043d3232018-08-31 10:10:53 -0700236 entry("FILE=%s", file.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500237 exit(EXIT_FAILURE);
Matthew Barth31d214c2018-03-26 09:54:27 -0500238 }
239 auto sensorValue = valueInterface->value();
James Feistee73f5b2018-08-01 16:31:42 -0700240 int64_t scale = 0;
241 // scale the thresholds only if we're using doubles
242 if constexpr (std::is_same<SensorValueType, double>::value)
243 {
244 scale = sensorObj->getScale();
245 }
246 addThreshold<WarningObject>(sensor.first.first,
247 std::get<sensorID>(properties), sensorValue,
248 info, scale);
249 addThreshold<CriticalObject>(sensor.first.first,
250 std::get<sensorID>(properties), sensorValue,
251 info, scale);
Matthew Barth31d214c2018-03-26 09:54:27 -0500252
Patrick Venture043d3232018-08-31 10:10:53 -0700253 auto target =
Patrick Venture52b40612018-12-19 13:36:41 -0800254 addTarget<hwmon::FanSpeed>(sensor.first, _ioAccess, _devPath, info);
Matthew Barth28f8e662018-03-26 16:57:36 -0500255 if (target)
Matthew Barth31d214c2018-03-26 09:54:27 -0500256 {
Matthew Barth28f8e662018-03-26 16:57:36 -0500257 target->enable();
Matthew Barth31d214c2018-03-26 09:54:27 -0500258 }
Patrick Venture52b40612018-12-19 13:36:41 -0800259 addTarget<hwmon::FanPwm>(sensor.first, _ioAccess, _devPath, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500260
261 // All the interfaces have been created. Go ahead
262 // and emit InterfacesAdded.
263 valueInterface->emit_object_added();
264
Matthew Barth9c431062018-05-07 13:55:29 -0500265 // Save sensor object specifications
Patrick Venture52b40612018-12-19 13:36:41 -0800266 _sensorObjects[sensor.first] = std::move(sensorObj);
Matthew Barth9c431062018-05-07 13:55:29 -0500267
Matthew Barthd238e232018-04-17 12:01:50 -0500268 return std::make_pair(std::move(std::get<sensorLabel>(properties)),
269 std::move(info));
Matthew Barth31d214c2018-03-26 09:54:27 -0500270}
271
Patrick Venture043d3232018-08-31 10:10:53 -0700272MainLoop::MainLoop(sdbusplus::bus::bus&& bus, const std::string& param,
273 const std::string& path, const std::string& devPath,
Patrick Venture16805a62019-06-21 14:19:33 -0700274 const char* prefix, const char* root,
275 const hwmonio::HwmonIOInterface* ioIntf) :
Patrick Venture043d3232018-08-31 10:10:53 -0700276 _bus(std::move(bus)),
277 _manager(_bus, root), _pathParam(param), _hwmonRoot(), _instance(),
Patrick Venture16805a62019-06-21 14:19:33 -0700278 _devPath(devPath), _prefix(prefix), _root(root), _state(),
279 _ioAccess(ioIntf), _event(sdeventplus::Event::get_default()),
Patrick Venture52b40612018-12-19 13:36:41 -0800280 _timer(_event, std::bind(&MainLoop::read, this))
Brad Bishopd499ca62016-12-19 09:24:50 -0500281{
Patrick Venture73a50c72018-04-17 15:19:03 -0700282 // Strip off any trailing slashes.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500283 std::string p = path;
284 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500285 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500286 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500287 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500288
Patrick Venture73a50c72018-04-17 15:19:03 -0700289 // Given the furthest right /, set instance to
290 // the basename, and hwmonRoot to the leading path.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500291 auto n = p.rfind('/');
292 if (n != std::string::npos)
293 {
294 _instance.assign(p.substr(n + 1));
295 _hwmonRoot.assign(p.substr(0, n));
296 }
297
298 assert(!_instance.empty());
299 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500300}
301
302void MainLoop::shutdown() noexcept
303{
Patrick Venture52b40612018-12-19 13:36:41 -0800304 _event.exit(0);
Brad Bishopd499ca62016-12-19 09:24:50 -0500305}
306
307void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500308{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600309 init();
310
Patrick Venture043d3232018-08-31 10:10:53 -0700311 std::function<void()> callback(std::bind(&MainLoop::read, this));
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600312 try
313 {
Patrick Venture52b40612018-12-19 13:36:41 -0800314 _timer.restart(std::chrono::microseconds(_interval));
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600315
316 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
317
318 // TODO: Issue#7 - Should probably periodically check the SensorSet
319 // for new entries.
320
Patrick Venture52b40612018-12-19 13:36:41 -0800321 _bus.attach_event(_event.get(), SD_EVENT_PRIORITY_IMPORTANT);
322 _event.loop();
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600323 }
William A. Kennington III0fe4cb32018-10-18 19:19:58 -0700324 catch (const std::exception& e)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600325 {
326 log<level::ERR>("Error in sysfs polling loop",
327 entry("ERROR=%s", e.what()));
328 throw;
329 }
330}
331
332void MainLoop::init()
333{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500334 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500335 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500336
Patrick Venturedb7ecb62018-10-23 19:42:23 -0700337 for (const auto& i : *sensors)
Brad Bishop75b4ab82017-01-06 09:33:50 -0500338 {
Matthew Barthd238e232018-04-17 12:01:50 -0500339 auto object = getObject(i);
340 if (object)
341 {
342 // Construct the SensorSet value
343 // std::tuple<SensorSet::mapped_type,
344 // std::string(Sensor Label),
345 // ObjectInfo>
Patrick Venture043d3232018-08-31 10:10:53 -0700346 auto value =
347 std::make_tuple(std::move(i.second), std::move((*object).first),
348 std::move((*object).second));
Matthew Barthd238e232018-04-17 12:01:50 -0500349
Patrick Venture52b40612018-12-19 13:36:41 -0800350 _state[std::move(i.first)] = std::move(value);
Matthew Barthd238e232018-04-17 12:01:50 -0500351 }
Brad Bishop75b4ab82017-01-06 09:33:50 -0500352 }
353
Patrick Venture62503a42017-05-23 07:30:29 -0700354 /* If there are no sensors specified by labels, exit. */
Patrick Venture52b40612018-12-19 13:36:41 -0800355 if (0 == _state.size())
Patrick Venture62503a42017-05-23 07:30:29 -0700356 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600357 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700358 }
359
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500360 {
Patrick Venturec897d8b2018-04-23 19:01:56 -0700361 std::stringstream ss;
Patrick Venture043d3232018-08-31 10:10:53 -0700362 ss << _prefix << "-"
Patrick Venturec897d8b2018-04-23 19:01:56 -0700363 << std::to_string(std::hash<std::string>{}(_devPath + _pathParam))
364 << ".Hwmon1";
365
366 _bus.request_name(ss.str().c_str());
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500367 }
368
Patrick Ventureab10f162017-05-22 09:44:50 -0700369 {
Patrick Venturea24c8802018-04-17 19:38:06 -0700370 auto interval = env::getEnv("INTERVAL");
371 if (!interval.empty())
Patrick Ventureab10f162017-05-22 09:44:50 -0700372 {
Patrick Venture50cf1c52018-04-18 09:21:41 -0700373 _interval = std::strtoull(interval.c_str(), NULL, 10);
Patrick Ventureab10f162017-05-22 09:44:50 -0700374 }
375 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600376}
Patrick Ventureab10f162017-05-22 09:44:50 -0700377
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600378void MainLoop::read()
379{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500380 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
381 // ensure the objects all exist?
382
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600383 // Iterate through all the sensors.
Patrick Venture52b40612018-12-19 13:36:41 -0800384 for (auto& i : _state)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600385 {
386 auto& attrs = std::get<0>(i.second);
387 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500388 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600389 // Read value from sensor.
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600390 std::string input = hwmon::entry::cinput;
Patrick Venture043d3232018-08-31 10:10:53 -0700391 if (i.first.first == "pwm")
392 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600393 input = "";
394 }
395
Brandon Kim79205b22019-06-20 12:18:24 -0700396 int64_t value;
397 auto& objInfo = std::get<ObjectInfo>(i.second);
398 auto& obj = std::get<InterfaceMap>(objInfo);
399 std::unique_ptr<sensor::Sensor>& sensor = _sensorObjects[i.first];
400
401 auto& statusIface = std::any_cast<std::shared_ptr<StatusObject>&>(
402 obj[InterfaceType::STATUS]);
403 // As long as addStatus is called before addValue, statusIface
404 // should never be nullptr.
405 assert(statusIface);
406
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600407 try
Brad Bishope55ef3d2016-12-19 09:12:40 -0500408 {
Brandon Kim86dcac82019-06-18 17:48:51 -0700409 if (sensor->hasFaultFile())
Matthew Barth27c4a392018-04-25 14:38:51 -0500410 {
Patrick Venture16805a62019-06-21 14:19:33 -0700411 auto fault = _ioAccess->read(
Patrick Venture043d3232018-08-31 10:10:53 -0700412 i.first.first, i.first.second, hwmon::entry::fault,
413 hwmonio::retries, hwmonio::delay);
Brandon Kim86dcac82019-06-18 17:48:51 -0700414 // Skip reading from a sensor with a valid fault file
415 // and set the functional property accordingly
Matthew Barthbfcaf3d2018-04-25 15:05:58 -0500416 if (!statusIface->functional((fault == 0) ? true : false))
Matthew Barth27c4a392018-04-25 14:38:51 -0500417 {
418 continue;
419 }
420 }
421
Brandon Kimdb76d492019-06-17 11:53:04 -0700422 {
423 // RAII object for GPIO unlock / lock
424 sensor::GpioLock gpioLock(sensor->getGpio());
Patrick Venture9331ab72018-01-29 09:48:47 -0800425
Brandon Kim86dcac82019-06-18 17:48:51 -0700426 // Retry for up to a second if device is busy
427 // or has a transient error.
Patrick Venture16805a62019-06-21 14:19:33 -0700428 value =
429 _ioAccess->read(i.first.first, i.first.second, input,
430 hwmonio::retries, hwmonio::delay);
Brandon Kim79205b22019-06-20 12:18:24 -0700431 // Set functional property to true if we could read sensor
432 statusIface->functional(true);
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600433
Brandon Kimdb76d492019-06-17 11:53:04 -0700434 value = sensor->adjustValue(value);
435 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600436
Patrick Venturefeb744a2019-06-26 19:07:48 -0700437 updateSensorInterfaces(obj, value);
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600438 }
439 catch (const std::system_error& e)
440 {
Brandon Kim79205b22019-06-20 12:18:24 -0700441#ifdef UPDATE_FUNCTIONAL_ON_FAIL
442 // If UPDATE_FUNCTIONAL_ON_FAIL is defined and an exception was
443 // thrown, set the functional property to false.
444 // We cannot set this with the 'continue' in the lower block
445 // as the code may exit before reaching it.
446 statusIface->functional(false);
447#endif
Matthew Barth38c74e72018-04-02 12:41:26 -0500448 auto file = sysfs::make_sysfs_path(
Patrick Venture16805a62019-06-21 14:19:33 -0700449 _ioAccess->path(), i.first.first, i.first.second,
Patrick Venture043d3232018-08-31 10:10:53 -0700450 hwmon::entry::cinput);
Patrick Venture0892c3f2019-06-27 14:24:03 -0700451
Matthew Barth8772ce32018-03-22 16:03:06 -0500452 // Check sensorAdjusts for sensor removal RCs
Patrick Venture52b40612018-12-19 13:36:41 -0800453 auto& sAdjusts = _sensorObjects[i.first]->getAdjusts();
Matthew Barthac473092018-05-07 14:41:46 -0500454 if (sAdjusts.rmRCs.count(e.code().value()) > 0)
Matthew Barth8772ce32018-03-22 16:03:06 -0500455 {
Matthew Barthac473092018-05-07 14:41:46 -0500456 // Return code found in sensor return code removal list
Patrick Venture52b40612018-12-19 13:36:41 -0800457 if (_rmSensors.find(i.first) == _rmSensors.end())
Matthew Barth8772ce32018-03-22 16:03:06 -0500458 {
Matthew Barthac473092018-05-07 14:41:46 -0500459 // Trace for sensor not already removed from dbus
460 log<level::INFO>(
Patrick Venture043d3232018-08-31 10:10:53 -0700461 "Remove sensor from dbus for read fail",
462 entry("FILE=%s", file.c_str()),
463 entry("RC=%d", e.code().value()));
Matthew Barthac473092018-05-07 14:41:46 -0500464 // Mark this sensor to be removed from dbus
Patrick Venture52b40612018-12-19 13:36:41 -0800465 _rmSensors[i.first] = std::get<0>(i.second);
Matthew Barth8772ce32018-03-22 16:03:06 -0500466 }
Matthew Barthac473092018-05-07 14:41:46 -0500467 continue;
Matthew Barth8772ce32018-03-22 16:03:06 -0500468 }
Brandon Kim79205b22019-06-20 12:18:24 -0700469#ifdef UPDATE_FUNCTIONAL_ON_FAIL
470 // Do not exit with failure if UPDATE_FUNCTIONAL_ON_FAIL is set
471 continue;
472#endif
Patrick Venture043d3232018-08-31 10:10:53 -0700473 using namespace sdbusplus::xyz::openbmc_project::Sensor::
474 Device::Error;
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600475 report<ReadFailure>(
Patrick Venture043d3232018-08-31 10:10:53 -0700476 xyz::openbmc_project::Sensor::Device::ReadFailure::
477 CALLOUT_ERRNO(e.code().value()),
478 xyz::openbmc_project::Sensor::Device::ReadFailure::
479 CALLOUT_DEVICE_PATH(_devPath.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500480
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600481 log<level::INFO>("Logging failing sysfs file",
Patrick Venture043d3232018-08-31 10:10:53 -0700482 entry("FILE=%s", file.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500483
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600484 exit(EXIT_FAILURE);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500485 }
486 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600487 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500488
Patrick Venture0cd4f692019-06-21 13:39:40 -0700489 removeSensors();
490
Patrick Venture0cd4f692019-06-21 13:39:40 -0700491 addDroppedSensors();
Patrick Venture0cd4f692019-06-21 13:39:40 -0700492}
493
494void MainLoop::removeSensors()
495{
Matthew Barth8772ce32018-03-22 16:03:06 -0500496 // Remove any sensors marked for removal
Patrick Venture52b40612018-12-19 13:36:41 -0800497 for (const auto& i : _rmSensors)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600498 {
Matthew Barthd0ce7922019-06-06 09:23:37 -0500499 // Remove sensor object from dbus using emit_object_removed()
500 auto& objInfo = std::get<ObjectInfo>(_state[i.first]);
501 auto& objPath = std::get<std::string>(objInfo);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700502
Matthew Barthd0ce7922019-06-06 09:23:37 -0500503 _bus.emit_object_removed(objPath.c_str());
Patrick Venture0cd4f692019-06-21 13:39:40 -0700504
Matthew Barthd0ce7922019-06-06 09:23:37 -0500505 // Erase sensor object info
Patrick Venture52b40612018-12-19 13:36:41 -0800506 _state.erase(i.first);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500507 }
Patrick Venture0cd4f692019-06-21 13:39:40 -0700508}
Matthew Barth31d214c2018-03-26 09:54:27 -0500509
Patrick Venture0cd4f692019-06-21 13:39:40 -0700510void MainLoop::addDroppedSensors()
511{
Matthew Barth31d214c2018-03-26 09:54:27 -0500512 // Attempt to add any sensors that were removed
Patrick Venture52b40612018-12-19 13:36:41 -0800513 auto it = _rmSensors.begin();
514 while (it != _rmSensors.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500515 {
Patrick Venture52b40612018-12-19 13:36:41 -0800516 if (_state.find(it->first) == _state.end())
Matthew Barth31d214c2018-03-26 09:54:27 -0500517 {
518 SensorSet::container_t::value_type ssValueType =
Patrick Venture043d3232018-08-31 10:10:53 -0700519 std::make_pair(it->first, it->second);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700520
Matthew Barthd238e232018-04-17 12:01:50 -0500521 auto object = getObject(ssValueType);
522 if (object)
Matthew Barth31d214c2018-03-26 09:54:27 -0500523 {
Matthew Barthd238e232018-04-17 12:01:50 -0500524 // Construct the SensorSet value
525 // std::tuple<SensorSet::mapped_type,
526 // std::string(Sensor Label),
527 // ObjectInfo>
528 auto value = std::make_tuple(std::move(ssValueType.second),
529 std::move((*object).first),
530 std::move((*object).second));
531
Patrick Venture52b40612018-12-19 13:36:41 -0800532 _state[std::move(ssValueType.first)] = std::move(value);
Matthew Barthd238e232018-04-17 12:01:50 -0500533
Matthew Barth31d214c2018-03-26 09:54:27 -0500534 // Sensor object added, erase entry from removal list
Matthew Barth38c74e72018-04-02 12:41:26 -0500535 auto file = sysfs::make_sysfs_path(
Patrick Venture16805a62019-06-21 14:19:33 -0700536 _ioAccess->path(), it->first.first, it->first.second,
Patrick Venture043d3232018-08-31 10:10:53 -0700537 hwmon::entry::cinput);
Patrick Venture0cd4f692019-06-21 13:39:40 -0700538
Patrick Venture043d3232018-08-31 10:10:53 -0700539 log<level::INFO>("Added sensor to dbus after successful read",
540 entry("FILE=%s", file.c_str()));
Patrick Venture0cd4f692019-06-21 13:39:40 -0700541
Patrick Venture52b40612018-12-19 13:36:41 -0800542 it = _rmSensors.erase(it);
Matthew Barth31d214c2018-03-26 09:54:27 -0500543 }
544 else
545 {
546 ++it;
547 }
548 }
549 else
550 {
551 // Sanity check to remove sensors that were re-added
Patrick Venture52b40612018-12-19 13:36:41 -0800552 it = _rmSensors.erase(it);
Matthew Barth31d214c2018-03-26 09:54:27 -0500553 }
554 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500555}
556
557// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4