blob: 595432dbea7068a927356ba41ecf554747abd0a0 [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
Chiabing Leec923ce92017-09-06 15:58:26 +080070// The gain and offset to adjust a value
71struct valueAdjust
72{
73 double gain = 1.0;
74 int offset = 0;
Matthew Barthd26e2772018-03-22 14:24:06 -050075 std::unordered_set<int> rmRCs;
Chiabing Leec923ce92017-09-06 15:58:26 +080076};
77
78// Store the valueAdjust for sensors
79std::map<SensorSet::key_type, valueAdjust> sensorAdjusts;
80
Matthew Barth979c8062018-04-17 11:37:15 -050081std::string MainLoop::getID(SensorSet::container_t::const_reference sensor)
Matthew Barth31d214c2018-03-26 09:54:27 -050082{
Matthew Barth31d214c2018-03-26 09:54:27 -050083 std::string id;
84
85 /*
86 * Check if the value of the MODE_<item><X> env variable for the sensor
Matt Spinler7c424802018-05-04 10:52:40 -050087 * is set. If it is, then read the from the <item><X>_<mode>
Matthew Barth31d214c2018-03-26 09:54:27 -050088 * file. The name of the DBUS object would be the value of the env
Matt Spinler7c424802018-05-04 10:52:40 -050089 * variable LABEL_<item><mode value>. If the MODE_<item><X> env variable
Matthew Barth31d214c2018-03-26 09:54:27 -050090 * doesn't exist, then the name of DBUS object is the value of the env
91 * variable LABEL_<item><X>.
Matt Spinler7c424802018-05-04 10:52:40 -050092 *
93 * For example, if MODE_temp1 = "label", then code reads the temp1_label
94 * file. If it has a 5 in it, then it will use the following entry to
95 * name the object: LABEL_temp5 = "My DBus object name".
96 *
Matthew Barth31d214c2018-03-26 09:54:27 -050097 */
Patrick Venture7a5285d2018-04-17 19:15:05 -070098 auto mode = env::getEnv("MODE", sensor.first);
Matt Spinler7c424802018-05-04 10:52:40 -050099 if (!mode.empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500100 {
Patrick Venture7a5285d2018-04-17 19:15:05 -0700101 id = env::getIndirectID(
Matt Spinler7c424802018-05-04 10:52:40 -0500102 _hwmonRoot + '/' + _instance + '/',
103 mode,
104 sensor.first);
Matthew Barth31d214c2018-03-26 09:54:27 -0500105
106 if (id.empty())
107 {
Matthew Barth979c8062018-04-17 11:37:15 -0500108 return id;
Matthew Barth31d214c2018-03-26 09:54:27 -0500109 }
110 }
111
112 // Use the ID we looked up above if there was one,
113 // otherwise use the standard one.
114 id = (id.empty()) ? sensor.first.second : id;
115
Matthew Barth979c8062018-04-17 11:37:15 -0500116 return id;
117}
118
119SensorIdentifiers MainLoop::getIdentifiers(
120 SensorSet::container_t::const_reference sensor)
121{
122 std::string id = getID(sensor);
123 std::string label;
124
125 if (!id.empty())
126 {
127 // Ignore inputs without a label.
128 label = env::getEnv("LABEL", sensor.first.first, id);
129 }
130
131 return std::make_tuple(std::move(id),
132 std::move(label));
133}
134
135/**
136 * Reads the environment parameters of a sensor and creates an object with
137 * atleast the `Value` interface, otherwise returns without creating the object.
138 * If the `Value` interface is successfully created, by reading the sensor's
139 * corresponding sysfs file's value, the additional interfaces for the sensor
Matthew Barthd238e232018-04-17 12:01:50 -0500140 * are created and the InterfacesAdded signal is emitted. The object's state
141 * data is then returned for sensor state monitoring within the main loop.
Matthew Barth979c8062018-04-17 11:37:15 -0500142 */
Matthew Barthd238e232018-04-17 12:01:50 -0500143optional_ns::optional<ObjectStateData> MainLoop::getObject(
144 SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500145{
146 auto properties = getIdentifiers(sensor);
147 if (std::get<sensorID>(properties).empty() ||
148 std::get<sensorLabel>(properties).empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500149 {
Matthew Barthd238e232018-04-17 12:01:50 -0500150 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500151 }
152
Patrick Venture09791852018-04-17 17:40:00 -0700153 hwmon::Attributes attrs;
154 if (!hwmon::getAttributes(sensor.first.first, attrs))
Matthew Barth31d214c2018-03-26 09:54:27 -0500155 {
Matthew Barthd238e232018-04-17 12:01:50 -0500156 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500157 }
158
Matthew Barth2e41b132018-05-07 14:15:45 -0500159 auto sensorObj = std::make_unique<sensor::Sensor>(sensor.first,
160 ioAccess,
161 _devPath);
Matthew Barth9c431062018-05-07 13:55:29 -0500162
Matthew Barthb7985272018-04-17 10:50:36 -0500163 // Get list of return codes for removing sensors on device
164 auto devRmRCs = env::getEnv("REMOVERCS");
165 // Add sensor removal return codes defined at the device level
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500166 sensorObj->addRemoveRCs(devRmRCs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500167
168 std::string objectPath{_root};
169 objectPath.append(1, '/');
Patrick Venture09791852018-04-17 17:40:00 -0700170 objectPath.append(hwmon::getNamespace(attrs));
Matthew Barth31d214c2018-03-26 09:54:27 -0500171 objectPath.append(1, '/');
Matthew Barth979c8062018-04-17 11:37:15 -0500172 objectPath.append(std::get<sensorLabel>(properties));
Matthew Barth31d214c2018-03-26 09:54:27 -0500173
174 ObjectInfo info(&_bus, std::move(objectPath), Object());
Patrick Venture75e56c62018-04-20 18:10:15 -0700175 RetryIO retryIO(hwmonio::retries, hwmonio::delay);
Matthew Barthd4beecf2018-04-03 15:50:22 -0500176 if (rmSensors.find(sensor.first) != rmSensors.end())
177 {
178 // When adding a sensor that was purposely removed,
179 // don't retry on errors when reading its value
180 std::get<size_t>(retryIO) = 0;
181 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500182 auto valueInterface = static_cast<
183 std::shared_ptr<ValueObject>>(nullptr);
184 try
185 {
Matthew Barthca44c2e2018-04-24 15:33:25 -0500186 // Add status interface based on _fault file being present
Matthew Barth2e41b132018-05-07 14:15:45 -0500187 sensorObj->addStatus(info);
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500188 valueInterface = sensorObj->addValue(retryIO, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500189 }
190 catch (const std::system_error& e)
191 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500192 auto file = sysfs::make_sysfs_path(
193 ioAccess.path(),
194 sensor.first.first,
195 sensor.first.second,
196 hwmon::entry::cinput);
Matthew Barth31d214c2018-03-26 09:54:27 -0500197#ifndef REMOVE_ON_FAIL
198 // Check sensorAdjusts for sensor removal RCs
199 const auto& it = sensorAdjusts.find(sensor.first);
200 if (it != sensorAdjusts.end())
201 {
202 auto rmRCit = it->second.rmRCs.find(e.code().value());
203 if (rmRCit != std::end(it->second.rmRCs))
204 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500205 // Return code found in sensor return code removal list
206 if (rmSensors.find(sensor.first) == rmSensors.end())
207 {
208 // Trace for sensor not already removed from dbus
209 log<level::INFO>("Sensor not added to dbus for read fail",
210 entry("FILE=%s", file.c_str()),
211 entry("RC=%d", e.code().value()));
212 rmSensors[std::move(sensor.first)] =
213 std::move(sensor.second);
214 }
Matthew Barthd238e232018-04-17 12:01:50 -0500215 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500216 }
217 }
218#endif
219 using namespace sdbusplus::xyz::openbmc_project::
220 Sensor::Device::Error;
221 report<ReadFailure>(
222 xyz::openbmc_project::Sensor::Device::
223 ReadFailure::CALLOUT_ERRNO(e.code().value()),
224 xyz::openbmc_project::Sensor::Device::
225 ReadFailure::CALLOUT_DEVICE_PATH(_devPath.c_str()));
226
Matthew Barth31d214c2018-03-26 09:54:27 -0500227 log<level::INFO>("Logging failing sysfs file",
228 entry("FILE=%s", file.c_str()));
229#ifdef REMOVE_ON_FAIL
Matthew Barthd238e232018-04-17 12:01:50 -0500230 return {}; /* skip adding this sensor for now. */
Matthew Barth31d214c2018-03-26 09:54:27 -0500231#else
232 exit(EXIT_FAILURE);
233#endif
234 }
235 auto sensorValue = valueInterface->value();
Matthew Barth979c8062018-04-17 11:37:15 -0500236 addThreshold<WarningObject>(sensor.first.first,
237 std::get<sensorID>(properties),
238 sensorValue,
239 info);
240 addThreshold<CriticalObject>(sensor.first.first,
241 std::get<sensorID>(properties),
242 sensorValue,
243 info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500244
Matthew Barth28f8e662018-03-26 16:57:36 -0500245 auto target = addTarget<hwmon::FanSpeed>(
246 sensor.first, ioAccess, _devPath, info);
247 if (target)
Matthew Barth31d214c2018-03-26 09:54:27 -0500248 {
Matthew Barth28f8e662018-03-26 16:57:36 -0500249 target->enable();
Matthew Barth31d214c2018-03-26 09:54:27 -0500250 }
Matthew Barth28f8e662018-03-26 16:57:36 -0500251 addTarget<hwmon::FanPwm>(sensor.first, ioAccess, _devPath, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500252
253 // All the interfaces have been created. Go ahead
254 // and emit InterfacesAdded.
255 valueInterface->emit_object_added();
256
Matthew Barth9c431062018-05-07 13:55:29 -0500257 // Save sensor object specifications
258 sensorObjects[sensor.first] = std::move(sensorObj);
259
Matthew Barthd238e232018-04-17 12:01:50 -0500260 return std::make_pair(std::move(std::get<sensorLabel>(properties)),
261 std::move(info));
Matthew Barth31d214c2018-03-26 09:54:27 -0500262}
263
Brad Bishopb9e2b072016-12-19 13:47:10 -0500264MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500265 sdbusplus::bus::bus&& bus,
Patrick Venturec897d8b2018-04-23 19:01:56 -0700266 const std::string& param,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500267 const std::string& path,
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400268 const std::string& devPath,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500269 const char* prefix,
270 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500271 : _bus(std::move(bus)),
Brad Bishop03e87352017-03-07 00:12:22 -0500272 _manager(_bus, root),
Patrick Venturec897d8b2018-04-23 19:01:56 -0700273 _pathParam(param),
Brad Bishopb8740fc2017-02-24 23:38:37 -0500274 _hwmonRoot(),
275 _instance(),
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400276 _devPath(devPath),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500277 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500278 _root(root),
Brad Bishop751043e2017-08-29 11:13:46 -0400279 state(),
280 ioAccess(path)
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{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600304 timer->state(phosphor::hwmon::timer::OFF);
305 sd_event_exit(loop, 0);
306 loop = nullptr;
Brad Bishopd499ca62016-12-19 09:24:50 -0500307}
308
309void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500310{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600311 init();
312
313 sd_event_default(&loop);
314
315 std::function<void()> callback(std::bind(
316 &MainLoop::read, this));
317 try
318 {
319 timer = std::make_unique<phosphor::hwmon::Timer>(
320 loop, callback,
321 std::chrono::microseconds(_interval),
322 phosphor::hwmon::timer::ON);
323
324 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
325
326 // TODO: Issue#7 - Should probably periodically check the SensorSet
327 // for new entries.
328
329 _bus.attach_event(loop, SD_EVENT_PRIORITY_IMPORTANT);
330 sd_event_loop(loop);
331 }
332 catch (const std::system_error& e)
333 {
334 log<level::ERR>("Error in sysfs polling loop",
335 entry("ERROR=%s", e.what()));
336 throw;
337 }
338}
339
340void MainLoop::init()
341{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500342 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500343 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500344
Brad Bishop75b4ab82017-01-06 09:33:50 -0500345 for (auto& i : *sensors)
346 {
Matthew Barthd238e232018-04-17 12:01:50 -0500347 auto object = getObject(i);
348 if (object)
349 {
350 // Construct the SensorSet value
351 // std::tuple<SensorSet::mapped_type,
352 // std::string(Sensor Label),
353 // ObjectInfo>
354 auto value = std::make_tuple(std::move(i.second),
355 std::move((*object).first),
356 std::move((*object).second));
357
358 state[std::move(i.first)] = std::move(value);
359 }
Brad Bishop75b4ab82017-01-06 09:33:50 -0500360 }
361
Patrick Venture62503a42017-05-23 07:30:29 -0700362 /* If there are no sensors specified by labels, exit. */
363 if (0 == state.size())
364 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600365 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700366 }
367
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500368 {
Patrick Venturec897d8b2018-04-23 19:01:56 -0700369 std::stringstream ss;
370 ss << _prefix
371 << "-"
372 << std::to_string(std::hash<std::string>{}(_devPath + _pathParam))
373 << ".Hwmon1";
374
375 _bus.request_name(ss.str().c_str());
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500376 }
377
Patrick Ventureab10f162017-05-22 09:44:50 -0700378 {
Patrick Venturea24c8802018-04-17 19:38:06 -0700379 auto interval = env::getEnv("INTERVAL");
380 if (!interval.empty())
Patrick Ventureab10f162017-05-22 09:44:50 -0700381 {
Patrick Venture50cf1c52018-04-18 09:21:41 -0700382 _interval = std::strtoull(interval.c_str(), NULL, 10);
Patrick Ventureab10f162017-05-22 09:44:50 -0700383 }
384 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600385}
Patrick Ventureab10f162017-05-22 09:44:50 -0700386
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600387void MainLoop::read()
388{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500389 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
390 // ensure the objects all exist?
391
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600392 // Iterate through all the sensors.
393 for (auto& i : state)
394 {
395 auto& attrs = std::get<0>(i.second);
396 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500397 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600398 // Read value from sensor.
399 int64_t value;
400 std::string input = hwmon::entry::cinput;
401 if (i.first.first == "pwm") {
402 input = "";
403 }
404
405 try
Brad Bishope55ef3d2016-12-19 09:12:40 -0500406 {
Matthew Barth27c4a392018-04-25 14:38:51 -0500407 auto& objInfo = std::get<ObjectInfo>(i.second);
408 auto& obj = std::get<Object>(objInfo);
409
410 auto it = obj.find(InterfaceType::STATUS);
411 if (it != obj.end())
412 {
Matthew Barthbfcaf3d2018-04-25 15:05:58 -0500413 auto fault = ioAccess.read(
414 i.first.first,
415 i.first.second,
416 hwmon::entry::fault,
417 hwmonio::retries,
418 hwmonio::delay);
Matthew Barth27c4a392018-04-25 14:38:51 -0500419 auto statusIface = std::experimental::any_cast<
420 std::shared_ptr<StatusObject>>(it->second);
Matthew Barthbfcaf3d2018-04-25 15:05:58 -0500421 if (!statusIface->functional((fault == 0) ? true : false))
Matthew Barth27c4a392018-04-25 14:38:51 -0500422 {
423 continue;
424 }
425 }
426
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600427 // Retry for up to a second if device is busy
428 // or has a transient error.
Patrick Venture9331ab72018-01-29 09:48:47 -0800429
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600430 value = ioAccess.read(
431 i.first.first,
432 i.first.second,
433 input,
Patrick Venture75e56c62018-04-20 18:10:15 -0700434 hwmonio::retries,
Matthew Barth0b305052018-04-26 09:46:49 -0500435 hwmonio::delay);
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600436
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500437 value = sensorObjects[i.first]->adjustValue(value);
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600438
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600439 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500440 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600441 auto valueIface = std::shared_ptr<ValueObject>();
442 auto warnIface = std::shared_ptr<WarningObject>();
443 auto critIface = std::shared_ptr<CriticalObject>();
Brad Bishop754d38c2017-09-08 00:46:58 -0400444
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600445 switch (iface.first)
Brad Bishope0b7d052017-01-06 15:30:23 -0500446 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600447 case InterfaceType::VALUE:
448 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
449 (iface.second);
450 valueIface->value(value);
451 break;
452 case InterfaceType::WARN:
453 checkThresholds<WarningObject>(iface.second, value);
454 break;
455 case InterfaceType::CRIT:
456 checkThresholds<CriticalObject>(iface.second, value);
457 break;
458 default:
459 break;
Brad Bishope0b7d052017-01-06 15:30:23 -0500460 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500461 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600462 }
463 catch (const std::system_error& e)
464 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500465 auto file = sysfs::make_sysfs_path(
466 ioAccess.path(),
467 i.first.first,
468 i.first.second,
469 hwmon::entry::cinput);
Matthew Barth8772ce32018-03-22 16:03:06 -0500470#ifndef REMOVE_ON_FAIL
471 // Check sensorAdjusts for sensor removal RCs
472 const auto& it = sensorAdjusts.find(i.first);
473 if (it != sensorAdjusts.end())
474 {
475 auto rmRCit = it->second.rmRCs.find(e.code().value());
476 if (rmRCit != std::end(it->second.rmRCs))
477 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500478 // Return code found in sensor return code removal list
479 if (rmSensors.find(i.first) == rmSensors.end())
480 {
481 // Trace for sensor not already removed from dbus
482 log<level::INFO>(
483 "Remove sensor from dbus for read fail",
484 entry("FILE=%s", file.c_str()),
485 entry("RC=%d", e.code().value()));
486 // Mark this sensor to be removed from dbus
487 rmSensors[i.first] = std::get<0>(i.second);
488 }
Matthew Barth8772ce32018-03-22 16:03:06 -0500489 continue;
490 }
491 }
492#endif
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600493 using namespace sdbusplus::xyz::openbmc_project::
494 Sensor::Device::Error;
495 report<ReadFailure>(
496 xyz::openbmc_project::Sensor::Device::
497 ReadFailure::CALLOUT_ERRNO(e.code().value()),
498 xyz::openbmc_project::Sensor::Device::
499 ReadFailure::CALLOUT_DEVICE_PATH(
500 _devPath.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500501
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600502 log<level::INFO>("Logging failing sysfs file",
503 entry("FILE=%s", file.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500504
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500505#ifdef REMOVE_ON_FAIL
Matthew Barth33db92b2018-03-26 09:55:19 -0500506 rmSensors[i.first] = std::get<0>(i.second);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500507#else
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600508 exit(EXIT_FAILURE);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500509#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500510 }
511 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600512 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500513
Matthew Barth8772ce32018-03-22 16:03:06 -0500514 // Remove any sensors marked for removal
Matthew Barth33db92b2018-03-26 09:55:19 -0500515 for (auto& i : rmSensors)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600516 {
Matthew Barth33db92b2018-03-26 09:55:19 -0500517 state.erase(i.first);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500518 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500519
520#ifndef REMOVE_ON_FAIL
521 // Attempt to add any sensors that were removed
522 auto it = rmSensors.begin();
523 while (it != rmSensors.end())
524 {
525 if (state.find(it->first) == state.end())
526 {
527 SensorSet::container_t::value_type ssValueType =
528 std::make_pair(it->first, it->second);
Matthew Barthd238e232018-04-17 12:01:50 -0500529 auto object = getObject(ssValueType);
530 if (object)
Matthew Barth31d214c2018-03-26 09:54:27 -0500531 {
Matthew Barthd238e232018-04-17 12:01:50 -0500532 // Construct the SensorSet value
533 // std::tuple<SensorSet::mapped_type,
534 // std::string(Sensor Label),
535 // ObjectInfo>
536 auto value = std::make_tuple(std::move(ssValueType.second),
537 std::move((*object).first),
538 std::move((*object).second));
539
540 state[std::move(ssValueType.first)] = std::move(value);
541
Matthew Barth31d214c2018-03-26 09:54:27 -0500542 // Sensor object added, erase entry from removal list
Matthew Barth38c74e72018-04-02 12:41:26 -0500543 auto file = sysfs::make_sysfs_path(
544 ioAccess.path(),
545 it->first.first,
546 it->first.second,
547 hwmon::entry::cinput);
548 log<level::INFO>(
549 "Added sensor to dbus after successful read",
550 entry("FILE=%s", file.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500551 it = rmSensors.erase(it);
552 }
553 else
554 {
555 ++it;
556 }
557 }
558 else
559 {
560 // Sanity check to remove sensors that were re-added
561 it = rmSensors.erase(it);
562 }
563 }
564#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500565}
566
567// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4