blob: cf5baafcb7f7b665fdbd7ad583c94469dce3da56 [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 */
Patrick Venture043d3232018-08-31 10:10:53 -0700134optional_ns::optional<ObjectStateData>
135 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();
Patrick Venture043d3232018-08-31 10:10:53 -0700217 addThreshold<WarningObject>(
218 sensor.first.first, std::get<sensorID>(properties), sensorValue, info);
219 addThreshold<CriticalObject>(
220 sensor.first.first, std::get<sensorID>(properties), sensorValue, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500221
Patrick Venture043d3232018-08-31 10:10:53 -0700222 auto target =
223 addTarget<hwmon::FanSpeed>(sensor.first, ioAccess, _devPath, info);
Matthew Barth28f8e662018-03-26 16:57:36 -0500224 if (target)
Matthew Barth31d214c2018-03-26 09:54:27 -0500225 {
Matthew Barth28f8e662018-03-26 16:57:36 -0500226 target->enable();
Matthew Barth31d214c2018-03-26 09:54:27 -0500227 }
Matthew Barth28f8e662018-03-26 16:57:36 -0500228 addTarget<hwmon::FanPwm>(sensor.first, ioAccess, _devPath, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500229
230 // All the interfaces have been created. Go ahead
231 // and emit InterfacesAdded.
232 valueInterface->emit_object_added();
233
Matthew Barth9c431062018-05-07 13:55:29 -0500234 // Save sensor object specifications
235 sensorObjects[sensor.first] = std::move(sensorObj);
236
Matthew Barthd238e232018-04-17 12:01:50 -0500237 return std::make_pair(std::move(std::get<sensorLabel>(properties)),
238 std::move(info));
Matthew Barth31d214c2018-03-26 09:54:27 -0500239}
240
Patrick Venture043d3232018-08-31 10:10:53 -0700241MainLoop::MainLoop(sdbusplus::bus::bus&& bus, const std::string& param,
242 const std::string& path, const std::string& devPath,
243 const char* prefix, const char* root) :
244 _bus(std::move(bus)),
245 _manager(_bus, root), _pathParam(param), _hwmonRoot(), _instance(),
246 _devPath(devPath), _prefix(prefix), _root(root), state(), ioAccess(path)
Brad Bishopd499ca62016-12-19 09:24:50 -0500247{
Patrick Venture73a50c72018-04-17 15:19:03 -0700248 // Strip off any trailing slashes.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500249 std::string p = path;
250 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500251 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500252 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500253 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500254
Patrick Venture73a50c72018-04-17 15:19:03 -0700255 // Given the furthest right /, set instance to
256 // the basename, and hwmonRoot to the leading path.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500257 auto n = p.rfind('/');
258 if (n != std::string::npos)
259 {
260 _instance.assign(p.substr(n + 1));
261 _hwmonRoot.assign(p.substr(0, n));
262 }
263
264 assert(!_instance.empty());
265 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500266}
267
268void MainLoop::shutdown() noexcept
269{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600270 timer->state(phosphor::hwmon::timer::OFF);
271 sd_event_exit(loop, 0);
272 loop = nullptr;
Brad Bishopd499ca62016-12-19 09:24:50 -0500273}
274
275void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500276{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600277 init();
278
279 sd_event_default(&loop);
280
Patrick Venture043d3232018-08-31 10:10:53 -0700281 std::function<void()> callback(std::bind(&MainLoop::read, this));
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600282 try
283 {
284 timer = std::make_unique<phosphor::hwmon::Timer>(
Patrick Venture043d3232018-08-31 10:10:53 -0700285 loop, callback, std::chrono::microseconds(_interval),
286 phosphor::hwmon::timer::ON);
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600287
288 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
289
290 // TODO: Issue#7 - Should probably periodically check the SensorSet
291 // for new entries.
292
293 _bus.attach_event(loop, SD_EVENT_PRIORITY_IMPORTANT);
294 sd_event_loop(loop);
295 }
296 catch (const std::system_error& e)
297 {
298 log<level::ERR>("Error in sysfs polling loop",
299 entry("ERROR=%s", e.what()));
300 throw;
301 }
302}
303
304void MainLoop::init()
305{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500306 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500307 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500308
Brad Bishop75b4ab82017-01-06 09:33:50 -0500309 for (auto& i : *sensors)
310 {
Matthew Barthd238e232018-04-17 12:01:50 -0500311 auto object = getObject(i);
312 if (object)
313 {
314 // Construct the SensorSet value
315 // std::tuple<SensorSet::mapped_type,
316 // std::string(Sensor Label),
317 // ObjectInfo>
Patrick Venture043d3232018-08-31 10:10:53 -0700318 auto value =
319 std::make_tuple(std::move(i.second), std::move((*object).first),
320 std::move((*object).second));
Matthew Barthd238e232018-04-17 12:01:50 -0500321
322 state[std::move(i.first)] = std::move(value);
323 }
Brad Bishop75b4ab82017-01-06 09:33:50 -0500324 }
325
Patrick Venture62503a42017-05-23 07:30:29 -0700326 /* If there are no sensors specified by labels, exit. */
327 if (0 == state.size())
328 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600329 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700330 }
331
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500332 {
Patrick Venturec897d8b2018-04-23 19:01:56 -0700333 std::stringstream ss;
Patrick Venture043d3232018-08-31 10:10:53 -0700334 ss << _prefix << "-"
Patrick Venturec897d8b2018-04-23 19:01:56 -0700335 << std::to_string(std::hash<std::string>{}(_devPath + _pathParam))
336 << ".Hwmon1";
337
338 _bus.request_name(ss.str().c_str());
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500339 }
340
Patrick Ventureab10f162017-05-22 09:44:50 -0700341 {
Patrick Venturea24c8802018-04-17 19:38:06 -0700342 auto interval = env::getEnv("INTERVAL");
343 if (!interval.empty())
Patrick Ventureab10f162017-05-22 09:44:50 -0700344 {
Patrick Venture50cf1c52018-04-18 09:21:41 -0700345 _interval = std::strtoull(interval.c_str(), NULL, 10);
Patrick Ventureab10f162017-05-22 09:44:50 -0700346 }
347 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600348}
Patrick Ventureab10f162017-05-22 09:44:50 -0700349
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600350void MainLoop::read()
351{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500352 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
353 // ensure the objects all exist?
354
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600355 // Iterate through all the sensors.
356 for (auto& i : state)
357 {
358 auto& attrs = std::get<0>(i.second);
359 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500360 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600361 // Read value from sensor.
362 int64_t value;
363 std::string input = hwmon::entry::cinput;
Patrick Venture043d3232018-08-31 10:10:53 -0700364 if (i.first.first == "pwm")
365 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600366 input = "";
367 }
368
369 try
Brad Bishope55ef3d2016-12-19 09:12:40 -0500370 {
Matthew Barth27c4a392018-04-25 14:38:51 -0500371 auto& objInfo = std::get<ObjectInfo>(i.second);
372 auto& obj = std::get<Object>(objInfo);
373
374 auto it = obj.find(InterfaceType::STATUS);
375 if (it != obj.end())
376 {
Matthew Barthbfcaf3d2018-04-25 15:05:58 -0500377 auto fault = ioAccess.read(
Patrick Venture043d3232018-08-31 10:10:53 -0700378 i.first.first, i.first.second, hwmon::entry::fault,
379 hwmonio::retries, hwmonio::delay);
Matthew Barth27c4a392018-04-25 14:38:51 -0500380 auto statusIface = std::experimental::any_cast<
Patrick Venture043d3232018-08-31 10:10:53 -0700381 std::shared_ptr<StatusObject>>(it->second);
Matthew Barthbfcaf3d2018-04-25 15:05:58 -0500382 if (!statusIface->functional((fault == 0) ? true : false))
Matthew Barth27c4a392018-04-25 14:38:51 -0500383 {
384 continue;
385 }
386 }
387
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600388 // Retry for up to a second if device is busy
389 // or has a transient error.
Patrick Venture9331ab72018-01-29 09:48:47 -0800390
Patrick Venture043d3232018-08-31 10:10:53 -0700391 value = ioAccess.read(i.first.first, i.first.second, input,
392 hwmonio::retries, hwmonio::delay);
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600393
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500394 value = sensorObjects[i.first]->adjustValue(value);
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600395
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600396 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500397 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600398 auto valueIface = std::shared_ptr<ValueObject>();
399 auto warnIface = std::shared_ptr<WarningObject>();
400 auto critIface = std::shared_ptr<CriticalObject>();
Brad Bishop754d38c2017-09-08 00:46:58 -0400401
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600402 switch (iface.first)
Brad Bishope0b7d052017-01-06 15:30:23 -0500403 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600404 case InterfaceType::VALUE:
Patrick Venture043d3232018-08-31 10:10:53 -0700405 valueIface = std::experimental::any_cast<
406 std::shared_ptr<ValueObject>>(iface.second);
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600407 valueIface->value(value);
408 break;
409 case InterfaceType::WARN:
410 checkThresholds<WarningObject>(iface.second, value);
411 break;
412 case InterfaceType::CRIT:
Patrick Venture043d3232018-08-31 10:10:53 -0700413 checkThresholds<CriticalObject>(iface.second,
414 value);
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600415 break;
416 default:
417 break;
Brad Bishope0b7d052017-01-06 15:30:23 -0500418 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500419 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600420 }
421 catch (const std::system_error& e)
422 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500423 auto file = sysfs::make_sysfs_path(
Patrick Venture043d3232018-08-31 10:10:53 -0700424 ioAccess.path(), i.first.first, i.first.second,
425 hwmon::entry::cinput);
Matthew Barth8772ce32018-03-22 16:03:06 -0500426#ifndef REMOVE_ON_FAIL
427 // Check sensorAdjusts for sensor removal RCs
Matthew Barthac473092018-05-07 14:41:46 -0500428 auto& sAdjusts = sensorObjects[i.first]->getAdjusts();
429 if (sAdjusts.rmRCs.count(e.code().value()) > 0)
Matthew Barth8772ce32018-03-22 16:03:06 -0500430 {
Matthew Barthac473092018-05-07 14:41:46 -0500431 // Return code found in sensor return code removal list
432 if (rmSensors.find(i.first) == rmSensors.end())
Matthew Barth8772ce32018-03-22 16:03:06 -0500433 {
Matthew Barthac473092018-05-07 14:41:46 -0500434 // Trace for sensor not already removed from dbus
435 log<level::INFO>(
Patrick Venture043d3232018-08-31 10:10:53 -0700436 "Remove sensor from dbus for read fail",
437 entry("FILE=%s", file.c_str()),
438 entry("RC=%d", e.code().value()));
Matthew Barthac473092018-05-07 14:41:46 -0500439 // Mark this sensor to be removed from dbus
440 rmSensors[i.first] = std::get<0>(i.second);
Matthew Barth8772ce32018-03-22 16:03:06 -0500441 }
Matthew Barthac473092018-05-07 14:41:46 -0500442 continue;
Matthew Barth8772ce32018-03-22 16:03:06 -0500443 }
444#endif
Patrick Venture043d3232018-08-31 10:10:53 -0700445 using namespace sdbusplus::xyz::openbmc_project::Sensor::
446 Device::Error;
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600447 report<ReadFailure>(
Patrick Venture043d3232018-08-31 10:10:53 -0700448 xyz::openbmc_project::Sensor::Device::ReadFailure::
449 CALLOUT_ERRNO(e.code().value()),
450 xyz::openbmc_project::Sensor::Device::ReadFailure::
451 CALLOUT_DEVICE_PATH(_devPath.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500452
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600453 log<level::INFO>("Logging failing sysfs file",
Patrick Venture043d3232018-08-31 10:10:53 -0700454 entry("FILE=%s", file.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500455
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500456#ifdef REMOVE_ON_FAIL
Matthew Barth33db92b2018-03-26 09:55:19 -0500457 rmSensors[i.first] = std::get<0>(i.second);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500458#else
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600459 exit(EXIT_FAILURE);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500460#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500461 }
462 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600463 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500464
Matthew Barth8772ce32018-03-22 16:03:06 -0500465 // Remove any sensors marked for removal
Matthew Barth33db92b2018-03-26 09:55:19 -0500466 for (auto& i : rmSensors)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600467 {
Matthew Barth33db92b2018-03-26 09:55:19 -0500468 state.erase(i.first);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500469 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500470
471#ifndef REMOVE_ON_FAIL
472 // Attempt to add any sensors that were removed
473 auto it = rmSensors.begin();
474 while (it != rmSensors.end())
475 {
476 if (state.find(it->first) == state.end())
477 {
478 SensorSet::container_t::value_type ssValueType =
Patrick Venture043d3232018-08-31 10:10:53 -0700479 std::make_pair(it->first, it->second);
Matthew Barthd238e232018-04-17 12:01:50 -0500480 auto object = getObject(ssValueType);
481 if (object)
Matthew Barth31d214c2018-03-26 09:54:27 -0500482 {
Matthew Barthd238e232018-04-17 12:01:50 -0500483 // Construct the SensorSet value
484 // std::tuple<SensorSet::mapped_type,
485 // std::string(Sensor Label),
486 // ObjectInfo>
487 auto value = std::make_tuple(std::move(ssValueType.second),
488 std::move((*object).first),
489 std::move((*object).second));
490
491 state[std::move(ssValueType.first)] = std::move(value);
492
Matthew Barth31d214c2018-03-26 09:54:27 -0500493 // Sensor object added, erase entry from removal list
Matthew Barth38c74e72018-04-02 12:41:26 -0500494 auto file = sysfs::make_sysfs_path(
Patrick Venture043d3232018-08-31 10:10:53 -0700495 ioAccess.path(), it->first.first, it->first.second,
496 hwmon::entry::cinput);
497 log<level::INFO>("Added sensor to dbus after successful read",
498 entry("FILE=%s", file.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500499 it = rmSensors.erase(it);
500 }
501 else
502 {
503 ++it;
504 }
505 }
506 else
507 {
508 // Sanity check to remove sensors that were re-added
509 it = rmSensors.erase(it);
510 }
511 }
512#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500513}
514
515// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4