blob: 02c3ebb9b3866f2da2e41bd75d6a97e8aff4d694 [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 */
Brad Bishop4d9a35b2017-11-14 22:33:03 -050016#include <functional>
Brad Bishope55ef3d2016-12-19 09:12:40 -050017#include <iostream>
18#include <memory>
Brad Bishop9c7b6e02016-12-19 12:43:36 -050019#include <cstdlib>
Chiabing Leec923ce92017-09-06 15:58:26 +080020#include <string>
Matthew Barthd26e2772018-03-22 14:24:06 -050021#include <unordered_set>
Patrick Venturec897d8b2018-04-23 19:01:56 -070022#include <sstream>
Patrick Venture1e6324f2017-06-01 14:07:05 -070023
24#include <phosphor-logging/elog-errors.hpp>
Matt Spinlerf9c83c42017-08-10 08:51:45 -050025#include "config.h"
Patrick Venture09791852018-04-17 17:40:00 -070026#include "env.hpp"
27#include "fan_pwm.hpp"
28#include "fan_speed.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050029#include "hwmon.hpp"
Patrick Venture75e56c62018-04-20 18:10:15 -070030#include "hwmonio.hpp"
Patrick Venture09791852018-04-17 17:40:00 -070031#include "sensorset.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050032#include "sysfs.hpp"
Brad Bishopd499ca62016-12-19 09:24:50 -050033#include "mainloop.hpp"
Matthew Barthbf7b7b12017-03-07 15:46:59 -060034#include "targets.hpp"
Patrick Venture09791852018-04-17 17:40:00 -070035#include "thresholds.hpp"
Matthew Barth35819382018-04-18 14:53:01 -050036#include "sensor.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050037
Patrick Venture1e6324f2017-06-01 14:07:05 -070038#include <xyz/openbmc_project/Sensor/Device/error.hpp>
39
40using namespace phosphor::logging;
41
Saqib Khan973886d2017-03-15 14:01:16 -050042// Initialization for Warning Objects
43decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo =
44 &WarningObject::warningLow;
45decltype(Thresholds<WarningObject>::setHi) Thresholds<WarningObject>::setHi =
46 &WarningObject::warningHigh;
47decltype(Thresholds<WarningObject>::getLo) Thresholds<WarningObject>::getLo =
48 &WarningObject::warningLow;
49decltype(Thresholds<WarningObject>::getHi) Thresholds<WarningObject>::getHi =
50 &WarningObject::warningHigh;
51decltype(Thresholds<WarningObject>::alarmLo) Thresholds<WarningObject>::alarmLo =
52 &WarningObject::warningAlarmLow;
53decltype(Thresholds<WarningObject>::alarmHi) Thresholds<WarningObject>::alarmHi =
54 &WarningObject::warningAlarmHigh;
55
56// Initialization for Critical Objects
57decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo =
58 &CriticalObject::criticalLow;
59decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi =
60 &CriticalObject::criticalHigh;
61decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo =
62 &CriticalObject::criticalLow;
63decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi =
64 &CriticalObject::criticalHigh;
65decltype(Thresholds<CriticalObject>::alarmLo) Thresholds<CriticalObject>::alarmLo =
66 &CriticalObject::criticalAlarmLow;
67decltype(Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi =
68 &CriticalObject::criticalAlarmHigh;
69
Matthew Barth979c8062018-04-17 11:37:15 -050070std::string MainLoop::getID(SensorSet::container_t::const_reference sensor)
Matthew Barth31d214c2018-03-26 09:54:27 -050071{
Matthew Barth31d214c2018-03-26 09:54:27 -050072 std::string id;
73
74 /*
75 * Check if the value of the MODE_<item><X> env variable for the sensor
Matt Spinler7c424802018-05-04 10:52:40 -050076 * is set. If it is, then read the from the <item><X>_<mode>
Matthew Barth31d214c2018-03-26 09:54:27 -050077 * file. The name of the DBUS object would be the value of the env
Matt Spinler7c424802018-05-04 10:52:40 -050078 * variable LABEL_<item><mode value>. If the MODE_<item><X> env variable
Matthew Barth31d214c2018-03-26 09:54:27 -050079 * doesn't exist, then the name of DBUS object is the value of the env
80 * variable LABEL_<item><X>.
Matt Spinler7c424802018-05-04 10:52:40 -050081 *
82 * For example, if MODE_temp1 = "label", then code reads the temp1_label
83 * file. If it has a 5 in it, then it will use the following entry to
84 * name the object: LABEL_temp5 = "My DBus object name".
85 *
Matthew Barth31d214c2018-03-26 09:54:27 -050086 */
Patrick Venture7a5285d2018-04-17 19:15:05 -070087 auto mode = env::getEnv("MODE", sensor.first);
Matt Spinler7c424802018-05-04 10:52:40 -050088 if (!mode.empty())
Matthew Barth31d214c2018-03-26 09:54:27 -050089 {
Patrick Venture7a5285d2018-04-17 19:15:05 -070090 id = env::getIndirectID(
Matt Spinler7c424802018-05-04 10:52:40 -050091 _hwmonRoot + '/' + _instance + '/',
92 mode,
93 sensor.first);
Matthew Barth31d214c2018-03-26 09:54:27 -050094
95 if (id.empty())
96 {
Matthew Barth979c8062018-04-17 11:37:15 -050097 return id;
Matthew Barth31d214c2018-03-26 09:54:27 -050098 }
99 }
100
101 // Use the ID we looked up above if there was one,
102 // otherwise use the standard one.
103 id = (id.empty()) ? sensor.first.second : id;
104
Matthew Barth979c8062018-04-17 11:37:15 -0500105 return id;
106}
107
108SensorIdentifiers MainLoop::getIdentifiers(
109 SensorSet::container_t::const_reference sensor)
110{
111 std::string id = getID(sensor);
112 std::string label;
113
114 if (!id.empty())
115 {
116 // Ignore inputs without a label.
117 label = env::getEnv("LABEL", sensor.first.first, id);
118 }
119
120 return std::make_tuple(std::move(id),
121 std::move(label));
122}
123
124/**
125 * Reads the environment parameters of a sensor and creates an object with
126 * atleast the `Value` interface, otherwise returns without creating the object.
127 * If the `Value` interface is successfully created, by reading the sensor's
128 * corresponding sysfs file's value, the additional interfaces for the sensor
Matthew Barthd238e232018-04-17 12:01:50 -0500129 * are created and the InterfacesAdded signal is emitted. The object's state
130 * data is then returned for sensor state monitoring within the main loop.
Matthew Barth979c8062018-04-17 11:37:15 -0500131 */
Matthew Barthd238e232018-04-17 12:01:50 -0500132optional_ns::optional<ObjectStateData> MainLoop::getObject(
133 SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500134{
135 auto properties = getIdentifiers(sensor);
136 if (std::get<sensorID>(properties).empty() ||
137 std::get<sensorLabel>(properties).empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500138 {
Matthew Barthd238e232018-04-17 12:01:50 -0500139 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500140 }
141
Patrick Venture09791852018-04-17 17:40:00 -0700142 hwmon::Attributes attrs;
143 if (!hwmon::getAttributes(sensor.first.first, attrs))
Matthew Barth31d214c2018-03-26 09:54:27 -0500144 {
Matthew Barthd238e232018-04-17 12:01:50 -0500145 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500146 }
147
Matthew Barth2e41b132018-05-07 14:15:45 -0500148 auto sensorObj = std::make_unique<sensor::Sensor>(sensor.first,
149 ioAccess,
150 _devPath);
Matthew Barth9c431062018-05-07 13:55:29 -0500151
Matthew Barthb7985272018-04-17 10:50:36 -0500152 // Get list of return codes for removing sensors on device
153 auto devRmRCs = env::getEnv("REMOVERCS");
154 // Add sensor removal return codes defined at the device level
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500155 sensorObj->addRemoveRCs(devRmRCs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500156
157 std::string objectPath{_root};
158 objectPath.append(1, '/');
Patrick Venture09791852018-04-17 17:40:00 -0700159 objectPath.append(hwmon::getNamespace(attrs));
Matthew Barth31d214c2018-03-26 09:54:27 -0500160 objectPath.append(1, '/');
Matthew Barth979c8062018-04-17 11:37:15 -0500161 objectPath.append(std::get<sensorLabel>(properties));
Matthew Barth31d214c2018-03-26 09:54:27 -0500162
163 ObjectInfo info(&_bus, std::move(objectPath), Object());
Patrick Venture75e56c62018-04-20 18:10:15 -0700164 RetryIO retryIO(hwmonio::retries, hwmonio::delay);
Matthew Barthd4beecf2018-04-03 15:50:22 -0500165 if (rmSensors.find(sensor.first) != rmSensors.end())
166 {
167 // When adding a sensor that was purposely removed,
168 // don't retry on errors when reading its value
169 std::get<size_t>(retryIO) = 0;
170 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500171 auto valueInterface = static_cast<
172 std::shared_ptr<ValueObject>>(nullptr);
173 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 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500181 auto file = sysfs::make_sysfs_path(
182 ioAccess.path(),
183 sensor.first.first,
184 sensor.first.second,
185 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
192 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",
196 entry("FILE=%s", file.c_str()),
197 entry("RC=%d", e.code().value()));
198 rmSensors[std::move(sensor.first)] =
199 std::move(sensor.second);
Matthew Barth31d214c2018-03-26 09:54:27 -0500200 }
Matthew Barthac473092018-05-07 14:41:46 -0500201 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500202 }
203#endif
204 using namespace sdbusplus::xyz::openbmc_project::
205 Sensor::Device::Error;
206 report<ReadFailure>(
207 xyz::openbmc_project::Sensor::Device::
208 ReadFailure::CALLOUT_ERRNO(e.code().value()),
209 xyz::openbmc_project::Sensor::Device::
210 ReadFailure::CALLOUT_DEVICE_PATH(_devPath.c_str()));
211
Matthew Barth31d214c2018-03-26 09:54:27 -0500212 log<level::INFO>("Logging failing sysfs file",
213 entry("FILE=%s", file.c_str()));
214#ifdef REMOVE_ON_FAIL
Matthew Barthd238e232018-04-17 12:01:50 -0500215 return {}; /* skip adding this sensor for now. */
Matthew Barth31d214c2018-03-26 09:54:27 -0500216#else
217 exit(EXIT_FAILURE);
218#endif
219 }
220 auto sensorValue = valueInterface->value();
Matthew Barth979c8062018-04-17 11:37:15 -0500221 addThreshold<WarningObject>(sensor.first.first,
222 std::get<sensorID>(properties),
223 sensorValue,
224 info);
225 addThreshold<CriticalObject>(sensor.first.first,
226 std::get<sensorID>(properties),
227 sensorValue,
228 info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500229
Matthew Barth28f8e662018-03-26 16:57:36 -0500230 auto target = addTarget<hwmon::FanSpeed>(
231 sensor.first, ioAccess, _devPath, info);
232 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
Brad Bishopb9e2b072016-12-19 13:47:10 -0500249MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500250 sdbusplus::bus::bus&& bus,
Patrick Venturec897d8b2018-04-23 19:01:56 -0700251 const std::string& param,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500252 const std::string& path,
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400253 const std::string& devPath,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500254 const char* prefix,
255 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500256 : _bus(std::move(bus)),
Brad Bishop03e87352017-03-07 00:12:22 -0500257 _manager(_bus, root),
Patrick Venturec897d8b2018-04-23 19:01:56 -0700258 _pathParam(param),
Brad Bishopb8740fc2017-02-24 23:38:37 -0500259 _hwmonRoot(),
260 _instance(),
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400261 _devPath(devPath),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500262 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500263 _root(root),
Brad Bishop751043e2017-08-29 11:13:46 -0400264 state(),
265 ioAccess(path)
Brad Bishopd499ca62016-12-19 09:24:50 -0500266{
Patrick Venture73a50c72018-04-17 15:19:03 -0700267 // Strip off any trailing slashes.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500268 std::string p = path;
269 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500270 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500271 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500272 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500273
Patrick Venture73a50c72018-04-17 15:19:03 -0700274 // Given the furthest right /, set instance to
275 // the basename, and hwmonRoot to the leading path.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500276 auto n = p.rfind('/');
277 if (n != std::string::npos)
278 {
279 _instance.assign(p.substr(n + 1));
280 _hwmonRoot.assign(p.substr(0, n));
281 }
282
283 assert(!_instance.empty());
284 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500285}
286
287void MainLoop::shutdown() noexcept
288{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600289 timer->state(phosphor::hwmon::timer::OFF);
290 sd_event_exit(loop, 0);
291 loop = nullptr;
Brad Bishopd499ca62016-12-19 09:24:50 -0500292}
293
294void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500295{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600296 init();
297
298 sd_event_default(&loop);
299
300 std::function<void()> callback(std::bind(
301 &MainLoop::read, this));
302 try
303 {
304 timer = std::make_unique<phosphor::hwmon::Timer>(
305 loop, callback,
306 std::chrono::microseconds(_interval),
307 phosphor::hwmon::timer::ON);
308
309 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
310
311 // TODO: Issue#7 - Should probably periodically check the SensorSet
312 // for new entries.
313
314 _bus.attach_event(loop, SD_EVENT_PRIORITY_IMPORTANT);
315 sd_event_loop(loop);
316 }
317 catch (const std::system_error& e)
318 {
319 log<level::ERR>("Error in sysfs polling loop",
320 entry("ERROR=%s", e.what()));
321 throw;
322 }
323}
324
325void MainLoop::init()
326{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500327 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500328 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500329
Brad Bishop75b4ab82017-01-06 09:33:50 -0500330 for (auto& i : *sensors)
331 {
Matthew Barthd238e232018-04-17 12:01:50 -0500332 auto object = getObject(i);
333 if (object)
334 {
335 // Construct the SensorSet value
336 // std::tuple<SensorSet::mapped_type,
337 // std::string(Sensor Label),
338 // ObjectInfo>
339 auto value = std::make_tuple(std::move(i.second),
340 std::move((*object).first),
341 std::move((*object).second));
342
343 state[std::move(i.first)] = std::move(value);
344 }
Brad Bishop75b4ab82017-01-06 09:33:50 -0500345 }
346
Patrick Venture62503a42017-05-23 07:30:29 -0700347 /* If there are no sensors specified by labels, exit. */
348 if (0 == state.size())
349 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600350 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700351 }
352
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500353 {
Patrick Venturec897d8b2018-04-23 19:01:56 -0700354 std::stringstream ss;
355 ss << _prefix
356 << "-"
357 << std::to_string(std::hash<std::string>{}(_devPath + _pathParam))
358 << ".Hwmon1";
359
360 _bus.request_name(ss.str().c_str());
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500361 }
362
Patrick Ventureab10f162017-05-22 09:44:50 -0700363 {
Patrick Venturea24c8802018-04-17 19:38:06 -0700364 auto interval = env::getEnv("INTERVAL");
365 if (!interval.empty())
Patrick Ventureab10f162017-05-22 09:44:50 -0700366 {
Patrick Venture50cf1c52018-04-18 09:21:41 -0700367 _interval = std::strtoull(interval.c_str(), NULL, 10);
Patrick Ventureab10f162017-05-22 09:44:50 -0700368 }
369 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600370}
Patrick Ventureab10f162017-05-22 09:44:50 -0700371
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600372void MainLoop::read()
373{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500374 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
375 // ensure the objects all exist?
376
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600377 // Iterate through all the sensors.
378 for (auto& i : state)
379 {
380 auto& attrs = std::get<0>(i.second);
381 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500382 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600383 // Read value from sensor.
384 int64_t value;
385 std::string input = hwmon::entry::cinput;
386 if (i.first.first == "pwm") {
387 input = "";
388 }
389
390 try
Brad Bishope55ef3d2016-12-19 09:12:40 -0500391 {
Matthew Barth27c4a392018-04-25 14:38:51 -0500392 auto& objInfo = std::get<ObjectInfo>(i.second);
393 auto& obj = std::get<Object>(objInfo);
394
395 auto it = obj.find(InterfaceType::STATUS);
396 if (it != obj.end())
397 {
Matthew Barthbfcaf3d2018-04-25 15:05:58 -0500398 auto fault = ioAccess.read(
399 i.first.first,
400 i.first.second,
401 hwmon::entry::fault,
402 hwmonio::retries,
403 hwmonio::delay);
Matthew Barth27c4a392018-04-25 14:38:51 -0500404 auto statusIface = std::experimental::any_cast<
405 std::shared_ptr<StatusObject>>(it->second);
Matthew Barthbfcaf3d2018-04-25 15:05:58 -0500406 if (!statusIface->functional((fault == 0) ? true : false))
Matthew Barth27c4a392018-04-25 14:38:51 -0500407 {
408 continue;
409 }
410 }
411
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600412 // Retry for up to a second if device is busy
413 // or has a transient error.
Patrick Venture9331ab72018-01-29 09:48:47 -0800414
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600415 value = ioAccess.read(
416 i.first.first,
417 i.first.second,
418 input,
Patrick Venture75e56c62018-04-20 18:10:15 -0700419 hwmonio::retries,
Matthew Barth0b305052018-04-26 09:46:49 -0500420 hwmonio::delay);
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600421
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500422 value = sensorObjects[i.first]->adjustValue(value);
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600423
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600424 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500425 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600426 auto valueIface = std::shared_ptr<ValueObject>();
427 auto warnIface = std::shared_ptr<WarningObject>();
428 auto critIface = std::shared_ptr<CriticalObject>();
Brad Bishop754d38c2017-09-08 00:46:58 -0400429
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600430 switch (iface.first)
Brad Bishope0b7d052017-01-06 15:30:23 -0500431 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600432 case InterfaceType::VALUE:
433 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
434 (iface.second);
435 valueIface->value(value);
436 break;
437 case InterfaceType::WARN:
438 checkThresholds<WarningObject>(iface.second, value);
439 break;
440 case InterfaceType::CRIT:
441 checkThresholds<CriticalObject>(iface.second, value);
442 break;
443 default:
444 break;
Brad Bishope0b7d052017-01-06 15:30:23 -0500445 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500446 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600447 }
448 catch (const std::system_error& e)
449 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500450 auto file = sysfs::make_sysfs_path(
451 ioAccess.path(),
452 i.first.first,
453 i.first.second,
454 hwmon::entry::cinput);
Matthew Barth8772ce32018-03-22 16:03:06 -0500455#ifndef REMOVE_ON_FAIL
456 // Check sensorAdjusts for sensor removal RCs
Matthew Barthac473092018-05-07 14:41:46 -0500457 auto& sAdjusts = sensorObjects[i.first]->getAdjusts();
458 if (sAdjusts.rmRCs.count(e.code().value()) > 0)
Matthew Barth8772ce32018-03-22 16:03:06 -0500459 {
Matthew Barthac473092018-05-07 14:41:46 -0500460 // Return code found in sensor return code removal list
461 if (rmSensors.find(i.first) == rmSensors.end())
Matthew Barth8772ce32018-03-22 16:03:06 -0500462 {
Matthew Barthac473092018-05-07 14:41:46 -0500463 // Trace for sensor not already removed from dbus
464 log<level::INFO>(
465 "Remove sensor from dbus for read fail",
466 entry("FILE=%s", file.c_str()),
467 entry("RC=%d", e.code().value()));
468 // Mark this sensor to be removed from dbus
469 rmSensors[i.first] = std::get<0>(i.second);
Matthew Barth8772ce32018-03-22 16:03:06 -0500470 }
Matthew Barthac473092018-05-07 14:41:46 -0500471 continue;
Matthew Barth8772ce32018-03-22 16:03:06 -0500472 }
473#endif
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600474 using namespace sdbusplus::xyz::openbmc_project::
475 Sensor::Device::Error;
476 report<ReadFailure>(
477 xyz::openbmc_project::Sensor::Device::
478 ReadFailure::CALLOUT_ERRNO(e.code().value()),
479 xyz::openbmc_project::Sensor::Device::
480 ReadFailure::CALLOUT_DEVICE_PATH(
481 _devPath.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500482
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600483 log<level::INFO>("Logging failing sysfs file",
484 entry("FILE=%s", file.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500485
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500486#ifdef REMOVE_ON_FAIL
Matthew Barth33db92b2018-03-26 09:55:19 -0500487 rmSensors[i.first] = std::get<0>(i.second);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500488#else
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600489 exit(EXIT_FAILURE);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500490#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500491 }
492 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600493 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500494
Matthew Barth8772ce32018-03-22 16:03:06 -0500495 // Remove any sensors marked for removal
Matthew Barth33db92b2018-03-26 09:55:19 -0500496 for (auto& i : rmSensors)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600497 {
Matthew Barth33db92b2018-03-26 09:55:19 -0500498 state.erase(i.first);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500499 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500500
501#ifndef REMOVE_ON_FAIL
502 // Attempt to add any sensors that were removed
503 auto it = rmSensors.begin();
504 while (it != rmSensors.end())
505 {
506 if (state.find(it->first) == state.end())
507 {
508 SensorSet::container_t::value_type ssValueType =
509 std::make_pair(it->first, it->second);
Matthew Barthd238e232018-04-17 12:01:50 -0500510 auto object = getObject(ssValueType);
511 if (object)
Matthew Barth31d214c2018-03-26 09:54:27 -0500512 {
Matthew Barthd238e232018-04-17 12:01:50 -0500513 // Construct the SensorSet value
514 // std::tuple<SensorSet::mapped_type,
515 // std::string(Sensor Label),
516 // ObjectInfo>
517 auto value = std::make_tuple(std::move(ssValueType.second),
518 std::move((*object).first),
519 std::move((*object).second));
520
521 state[std::move(ssValueType.first)] = std::move(value);
522
Matthew Barth31d214c2018-03-26 09:54:27 -0500523 // Sensor object added, erase entry from removal list
Matthew Barth38c74e72018-04-02 12:41:26 -0500524 auto file = sysfs::make_sysfs_path(
525 ioAccess.path(),
526 it->first.first,
527 it->first.second,
528 hwmon::entry::cinput);
529 log<level::INFO>(
530 "Added sensor to dbus after successful read",
531 entry("FILE=%s", file.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500532 it = rmSensors.erase(it);
533 }
534 else
535 {
536 ++it;
537 }
538 }
539 else
540 {
541 // Sanity check to remove sensors that were re-added
542 it = rmSensors.erase(it);
543 }
544 }
545#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500546}
547
548// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4