blob: 9a4875509b10ed55bb71ff595ca13916a3095138 [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 Venture1e6324f2017-06-01 14:07:05 -070022
23#include <phosphor-logging/elog-errors.hpp>
Matt Spinlerf9c83c42017-08-10 08:51:45 -050024#include "config.h"
Patrick Venture09791852018-04-17 17:40:00 -070025#include "env.hpp"
26#include "fan_pwm.hpp"
27#include "fan_speed.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050028#include "hwmon.hpp"
Patrick Venture09791852018-04-17 17:40:00 -070029#include "sensorset.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050030#include "sysfs.hpp"
Brad Bishopd499ca62016-12-19 09:24:50 -050031#include "mainloop.hpp"
Matthew Barthbf7b7b12017-03-07 15:46:59 -060032#include "targets.hpp"
Patrick Venture09791852018-04-17 17:40:00 -070033#include "thresholds.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050034
Patrick Venture1e6324f2017-06-01 14:07:05 -070035#include <xyz/openbmc_project/Sensor/Device/error.hpp>
36
37using namespace phosphor::logging;
38
Saqib Khan973886d2017-03-15 14:01:16 -050039// Initialization for Warning Objects
40decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo =
41 &WarningObject::warningLow;
42decltype(Thresholds<WarningObject>::setHi) Thresholds<WarningObject>::setHi =
43 &WarningObject::warningHigh;
44decltype(Thresholds<WarningObject>::getLo) Thresholds<WarningObject>::getLo =
45 &WarningObject::warningLow;
46decltype(Thresholds<WarningObject>::getHi) Thresholds<WarningObject>::getHi =
47 &WarningObject::warningHigh;
48decltype(Thresholds<WarningObject>::alarmLo) Thresholds<WarningObject>::alarmLo =
49 &WarningObject::warningAlarmLow;
50decltype(Thresholds<WarningObject>::alarmHi) Thresholds<WarningObject>::alarmHi =
51 &WarningObject::warningAlarmHigh;
52
53// Initialization for Critical Objects
54decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo =
55 &CriticalObject::criticalLow;
56decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi =
57 &CriticalObject::criticalHigh;
58decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo =
59 &CriticalObject::criticalLow;
60decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi =
61 &CriticalObject::criticalHigh;
62decltype(Thresholds<CriticalObject>::alarmLo) Thresholds<CriticalObject>::alarmLo =
63 &CriticalObject::criticalAlarmLow;
64decltype(Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi =
65 &CriticalObject::criticalAlarmHigh;
66
Chiabing Leec923ce92017-09-06 15:58:26 +080067// The gain and offset to adjust a value
68struct valueAdjust
69{
70 double gain = 1.0;
71 int offset = 0;
Matthew Barthd26e2772018-03-22 14:24:06 -050072 std::unordered_set<int> rmRCs;
Chiabing Leec923ce92017-09-06 15:58:26 +080073};
74
75// Store the valueAdjust for sensors
76std::map<SensorSet::key_type, valueAdjust> sensorAdjusts;
77
Matthew Barthd26e2772018-03-22 14:24:06 -050078void addRemoveRCs(const SensorSet::key_type& sensor,
79 const std::string& rcList)
80{
Matthew Barthb7985272018-04-17 10:50:36 -050081 if (rcList.empty())
82 {
83 return;
84 }
85
Matthew Barthd26e2772018-03-22 14:24:06 -050086 // Convert to a char* for strtok
87 std::vector<char> rmRCs(rcList.c_str(),
88 rcList.c_str() + rcList.size() + 1);
89 auto rmRC = strtok(&rmRCs[0], ", ");
90 while (rmRC != nullptr)
91 {
92 try
93 {
94 sensorAdjusts[sensor].rmRCs.insert(std::stoi(rmRC));
95 }
96 catch (const std::logic_error& le)
97 {
98 // Unable to convert to int, continue to next token
99 std::string name = sensor.first + "_" + sensor.second;
100 log<level::INFO>("Unable to convert sensor removal return code",
101 entry("SENSOR=%s", name.c_str()),
102 entry("RC=%s", rmRC),
103 entry("EXCEPTION=%s", le.what()));
104 }
105 rmRC = strtok(nullptr, ", ");
106 }
107}
108
Matt Spinlerfee106b2017-11-29 15:18:05 -0600109int64_t adjustValue(const SensorSet::key_type& sensor, int64_t value)
Chiabing Leec923ce92017-09-06 15:58:26 +0800110{
Patrick Venturec1cece72017-11-07 12:09:49 -0800111// Because read doesn't have an out pointer to store errors.
112// let's assume negative values are errors if they have this
113// set.
114#ifdef NEGATIVE_ERRNO_ON_FAIL
115 if (value < 0)
116 {
117 return value;
118 }
119#endif
120
Chiabing Leec923ce92017-09-06 15:58:26 +0800121 const auto& it = sensorAdjusts.find(sensor);
122 if (it != sensorAdjusts.end())
123 {
124 // Adjust based on gain and offset
125 value = static_cast<decltype(value)>(
126 static_cast<double>(value) * it->second.gain
127 + it->second.offset);
128 }
129 return value;
130}
131
Brad Bishope9fdee02017-01-06 10:43:29 -0500132auto addValue(const SensorSet::key_type& sensor,
Matthew Barthd4beecf2018-04-03 15:50:22 -0500133 const RetryIO& retryIO,
Brad Bishop751043e2017-08-29 11:13:46 -0400134 sysfs::hwmonio::HwmonIO& ioAccess,
Matthew Bartha23babd2018-03-16 10:03:27 -0500135 ObjectInfo& info,
136 bool isOCC = false)
Brad Bishope9fdee02017-01-06 10:43:29 -0500137{
Brad Bishop30dbcee2017-01-18 07:55:42 -0500138 static constexpr bool deferSignals = true;
139
Brad Bishope9fdee02017-01-06 10:43:29 -0500140 // Get the initial value for the value interface.
141 auto& bus = *std::get<sdbusplus::bus::bus*>(info);
142 auto& obj = std::get<Object>(info);
143 auto& objPath = std::get<std::string>(info);
144
Patrick Venture7a5285d2018-04-17 19:15:05 -0700145 auto senRmRCs = env::getEnv("REMOVERCS", sensor);
Matthew Barthb7985272018-04-17 10:50:36 -0500146 // Add sensor removal return codes defined per sensor
147 addRemoveRCs(sensor, senRmRCs);
Matthew Barthd26e2772018-03-22 14:24:06 -0500148
Matthew Barth8772ce32018-03-22 16:03:06 -0500149 // Retry for up to a second if device is busy
150 // or has a transient error.
151 int64_t val = ioAccess.read(
152 sensor.first,
153 sensor.second,
154 hwmon::entry::cinput,
Matthew Barthd4beecf2018-04-03 15:50:22 -0500155 std::get<size_t>(retryIO),
156 std::get<std::chrono::milliseconds>(retryIO),
Matthew Barth8772ce32018-03-22 16:03:06 -0500157 isOCC);
Patrick Venture1e6324f2017-06-01 14:07:05 -0700158
Patrick Venture7a5285d2018-04-17 19:15:05 -0700159 auto gain = env::getEnv("GAIN", sensor);
Chiabing Leec923ce92017-09-06 15:58:26 +0800160 if (!gain.empty())
161 {
162 sensorAdjusts[sensor].gain = std::stod(gain);
163 }
164
Patrick Venture7a5285d2018-04-17 19:15:05 -0700165 auto offset = env::getEnv("OFFSET", sensor);
Chiabing Leec923ce92017-09-06 15:58:26 +0800166 if (!offset.empty())
167 {
168 sensorAdjusts[sensor].offset = std::stoi(offset);
169 }
170
171 val = adjustValue(sensor, val);
172
Brad Bishop30dbcee2017-01-18 07:55:42 -0500173 auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals);
Brad Bishope9fdee02017-01-06 10:43:29 -0500174 iface->value(val);
175
Patrick Venture09791852018-04-17 17:40:00 -0700176 hwmon::Attributes attrs;
177 if (hwmon::getAttributes(sensor.first, attrs))
Brad Bishope9fdee02017-01-06 10:43:29 -0500178 {
Patrick Venture09791852018-04-17 17:40:00 -0700179 iface->unit(hwmon::getUnit(attrs));
180 iface->scale(hwmon::getScale(attrs));
Brad Bishope9fdee02017-01-06 10:43:29 -0500181 }
182
Patrick Venture7a5285d2018-04-17 19:15:05 -0700183 auto maxValue = env::getEnv("MAXVALUE", sensor);
James Feist7bd5fcf2018-01-22 16:14:28 -0800184 if(!maxValue.empty())
185 {
186 iface->maxValue(std::stoll(maxValue));
187 }
Patrick Venture7a5285d2018-04-17 19:15:05 -0700188 auto minValue = env::getEnv("MINVALUE", sensor);
James Feist7bd5fcf2018-01-22 16:14:28 -0800189 if(!minValue.empty())
190 {
191 iface->minValue(std::stoll(minValue));
192 }
193
Brad Bishope9fdee02017-01-06 10:43:29 -0500194 obj[InterfaceType::VALUE] = iface;
195 return iface;
196}
197
Matthew Barth979c8062018-04-17 11:37:15 -0500198std::string MainLoop::getID(SensorSet::container_t::const_reference sensor)
Matthew Barth31d214c2018-03-26 09:54:27 -0500199{
Matthew Barth31d214c2018-03-26 09:54:27 -0500200 std::string id;
201
202 /*
203 * Check if the value of the MODE_<item><X> env variable for the sensor
204 * is "label", then read the sensor number from the <item><X>_label
205 * file. The name of the DBUS object would be the value of the env
206 * variable LABEL_<item><sensorNum>. If the MODE_<item><X> env variable
207 * doesn't exist, then the name of DBUS object is the value of the env
208 * variable LABEL_<item><X>.
209 */
Patrick Venture7a5285d2018-04-17 19:15:05 -0700210 auto mode = env::getEnv("MODE", sensor.first);
Matthew Barth31d214c2018-03-26 09:54:27 -0500211 if (!mode.compare(hwmon::entry::label))
212 {
Patrick Venture7a5285d2018-04-17 19:15:05 -0700213 id = env::getIndirectID(
Matthew Barth31d214c2018-03-26 09:54:27 -0500214 _hwmonRoot + '/' + _instance + '/', sensor.first);
215
216 if (id.empty())
217 {
Matthew Barth979c8062018-04-17 11:37:15 -0500218 return id;
Matthew Barth31d214c2018-03-26 09:54:27 -0500219 }
220 }
221
222 // Use the ID we looked up above if there was one,
223 // otherwise use the standard one.
224 id = (id.empty()) ? sensor.first.second : id;
225
Matthew Barth979c8062018-04-17 11:37:15 -0500226 return id;
227}
228
229SensorIdentifiers MainLoop::getIdentifiers(
230 SensorSet::container_t::const_reference sensor)
231{
232 std::string id = getID(sensor);
233 std::string label;
234
235 if (!id.empty())
236 {
237 // Ignore inputs without a label.
238 label = env::getEnv("LABEL", sensor.first.first, id);
239 }
240
241 return std::make_tuple(std::move(id),
242 std::move(label));
243}
244
245/**
246 * Reads the environment parameters of a sensor and creates an object with
247 * atleast the `Value` interface, otherwise returns without creating the object.
248 * If the `Value` interface is successfully created, by reading the sensor's
249 * corresponding sysfs file's value, the additional interfaces for the sensor
250 * are created and the InterfacesAdded signal is emitted. The sensor is then
251 * moved to the list for sensor state monitoring within the main loop.
252 */
253void MainLoop::getObject(SensorSet::container_t::const_reference sensor)
254{
255 auto properties = getIdentifiers(sensor);
256 if (std::get<sensorID>(properties).empty() ||
257 std::get<sensorLabel>(properties).empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500258 {
259 return;
260 }
261
Patrick Venture09791852018-04-17 17:40:00 -0700262 hwmon::Attributes attrs;
263 if (!hwmon::getAttributes(sensor.first.first, attrs))
Matthew Barth31d214c2018-03-26 09:54:27 -0500264 {
265 return;
266 }
267
Matthew Barthb7985272018-04-17 10:50:36 -0500268 // Get list of return codes for removing sensors on device
269 auto devRmRCs = env::getEnv("REMOVERCS");
270 // Add sensor removal return codes defined at the device level
271 addRemoveRCs(sensor.first, devRmRCs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500272
273 std::string objectPath{_root};
274 objectPath.append(1, '/');
Patrick Venture09791852018-04-17 17:40:00 -0700275 objectPath.append(hwmon::getNamespace(attrs));
Matthew Barth31d214c2018-03-26 09:54:27 -0500276 objectPath.append(1, '/');
Matthew Barth979c8062018-04-17 11:37:15 -0500277 objectPath.append(std::get<sensorLabel>(properties));
Matthew Barth31d214c2018-03-26 09:54:27 -0500278
279 ObjectInfo info(&_bus, std::move(objectPath), Object());
Matthew Barthd4beecf2018-04-03 15:50:22 -0500280 RetryIO retryIO(sysfs::hwmonio::retries, sysfs::hwmonio::delay);
281 if (rmSensors.find(sensor.first) != rmSensors.end())
282 {
283 // When adding a sensor that was purposely removed,
284 // don't retry on errors when reading its value
285 std::get<size_t>(retryIO) = 0;
286 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500287 auto valueInterface = static_cast<
288 std::shared_ptr<ValueObject>>(nullptr);
289 try
290 {
Matthew Barthd4beecf2018-04-03 15:50:22 -0500291 valueInterface = addValue(sensor.first, retryIO, ioAccess, info,
Matthew Barth31d214c2018-03-26 09:54:27 -0500292 _isOCC);
293 }
294 catch (const std::system_error& e)
295 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500296 auto file = sysfs::make_sysfs_path(
297 ioAccess.path(),
298 sensor.first.first,
299 sensor.first.second,
300 hwmon::entry::cinput);
Matthew Barth31d214c2018-03-26 09:54:27 -0500301#ifndef REMOVE_ON_FAIL
302 // Check sensorAdjusts for sensor removal RCs
303 const auto& it = sensorAdjusts.find(sensor.first);
304 if (it != sensorAdjusts.end())
305 {
306 auto rmRCit = it->second.rmRCs.find(e.code().value());
307 if (rmRCit != std::end(it->second.rmRCs))
308 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500309 // Return code found in sensor return code removal list
310 if (rmSensors.find(sensor.first) == rmSensors.end())
311 {
312 // Trace for sensor not already removed from dbus
313 log<level::INFO>("Sensor not added to dbus for read fail",
314 entry("FILE=%s", file.c_str()),
315 entry("RC=%d", e.code().value()));
316 rmSensors[std::move(sensor.first)] =
317 std::move(sensor.second);
318 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500319 return;
320 }
321 }
322#endif
323 using namespace sdbusplus::xyz::openbmc_project::
324 Sensor::Device::Error;
325 report<ReadFailure>(
326 xyz::openbmc_project::Sensor::Device::
327 ReadFailure::CALLOUT_ERRNO(e.code().value()),
328 xyz::openbmc_project::Sensor::Device::
329 ReadFailure::CALLOUT_DEVICE_PATH(_devPath.c_str()));
330
Matthew Barth31d214c2018-03-26 09:54:27 -0500331 log<level::INFO>("Logging failing sysfs file",
332 entry("FILE=%s", file.c_str()));
333#ifdef REMOVE_ON_FAIL
334 return; /* skip adding this sensor for now. */
335#else
336 exit(EXIT_FAILURE);
337#endif
338 }
339 auto sensorValue = valueInterface->value();
Matthew Barth979c8062018-04-17 11:37:15 -0500340 addThreshold<WarningObject>(sensor.first.first,
341 std::get<sensorID>(properties),
342 sensorValue,
343 info);
344 addThreshold<CriticalObject>(sensor.first.first,
345 std::get<sensorID>(properties),
346 sensorValue,
347 info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500348
Matthew Barth28f8e662018-03-26 16:57:36 -0500349 auto target = addTarget<hwmon::FanSpeed>(
350 sensor.first, ioAccess, _devPath, info);
351 if (target)
Matthew Barth31d214c2018-03-26 09:54:27 -0500352 {
Matthew Barth28f8e662018-03-26 16:57:36 -0500353 target->enable();
Matthew Barth31d214c2018-03-26 09:54:27 -0500354 }
Matthew Barth28f8e662018-03-26 16:57:36 -0500355 addTarget<hwmon::FanPwm>(sensor.first, ioAccess, _devPath, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500356
357 // All the interfaces have been created. Go ahead
358 // and emit InterfacesAdded.
359 valueInterface->emit_object_added();
360
361 auto value = std::make_tuple(
362 std::move(sensor.second),
Matthew Barth979c8062018-04-17 11:37:15 -0500363 std::move(std::get<sensorLabel>(properties)),
Matthew Barth31d214c2018-03-26 09:54:27 -0500364 std::move(info));
365
366 state[std::move(sensor.first)] = std::move(value);
367}
368
Brad Bishopb9e2b072016-12-19 13:47:10 -0500369MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500370 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500371 const std::string& path,
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400372 const std::string& devPath,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500373 const char* prefix,
374 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500375 : _bus(std::move(bus)),
Brad Bishop03e87352017-03-07 00:12:22 -0500376 _manager(_bus, root),
Brad Bishopb8740fc2017-02-24 23:38:37 -0500377 _hwmonRoot(),
378 _instance(),
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400379 _devPath(devPath),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500380 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500381 _root(root),
Brad Bishop751043e2017-08-29 11:13:46 -0400382 state(),
383 ioAccess(path)
Brad Bishopd499ca62016-12-19 09:24:50 -0500384{
Matthew Bartha23babd2018-03-16 10:03:27 -0500385 if (path.find("occ") != std::string::npos)
386 {
387 _isOCC = true;
388 }
389
Patrick Venture73a50c72018-04-17 15:19:03 -0700390 // Strip off any trailing slashes.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500391 std::string p = path;
392 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500393 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500394 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500395 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500396
Patrick Venture73a50c72018-04-17 15:19:03 -0700397 // Given the furthest right /, set instance to
398 // the basename, and hwmonRoot to the leading path.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500399 auto n = p.rfind('/');
400 if (n != std::string::npos)
401 {
402 _instance.assign(p.substr(n + 1));
403 _hwmonRoot.assign(p.substr(0, n));
404 }
405
406 assert(!_instance.empty());
407 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500408}
409
410void MainLoop::shutdown() noexcept
411{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600412 timer->state(phosphor::hwmon::timer::OFF);
413 sd_event_exit(loop, 0);
414 loop = nullptr;
Brad Bishopd499ca62016-12-19 09:24:50 -0500415}
416
417void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500418{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600419 init();
420
421 sd_event_default(&loop);
422
423 std::function<void()> callback(std::bind(
424 &MainLoop::read, this));
425 try
426 {
427 timer = std::make_unique<phosphor::hwmon::Timer>(
428 loop, callback,
429 std::chrono::microseconds(_interval),
430 phosphor::hwmon::timer::ON);
431
432 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
433
434 // TODO: Issue#7 - Should probably periodically check the SensorSet
435 // for new entries.
436
437 _bus.attach_event(loop, SD_EVENT_PRIORITY_IMPORTANT);
438 sd_event_loop(loop);
439 }
440 catch (const std::system_error& e)
441 {
442 log<level::ERR>("Error in sysfs polling loop",
443 entry("ERROR=%s", e.what()));
444 throw;
445 }
446}
447
448void MainLoop::init()
449{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500450 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500451 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500452
Brad Bishop75b4ab82017-01-06 09:33:50 -0500453 for (auto& i : *sensors)
454 {
Matthew Barth31d214c2018-03-26 09:54:27 -0500455 getObject(i);
Brad Bishop75b4ab82017-01-06 09:33:50 -0500456 }
457
Patrick Venture62503a42017-05-23 07:30:29 -0700458 /* If there are no sensors specified by labels, exit. */
459 if (0 == state.size())
460 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600461 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700462 }
463
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500464 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500465 std::string busname{_prefix};
Brad Bishop4d9a35b2017-11-14 22:33:03 -0500466 busname.append(1, '-');
467 busname.append(
468 std::to_string(std::hash<decltype(_devPath)>{}(_devPath)));
469 busname.append(".Hwmon1");
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500470 _bus.request_name(busname.c_str());
471 }
472
Patrick Ventureab10f162017-05-22 09:44:50 -0700473 {
Patrick Venturea24c8802018-04-17 19:38:06 -0700474 auto interval = env::getEnv("INTERVAL");
475 if (!interval.empty())
Patrick Ventureab10f162017-05-22 09:44:50 -0700476 {
Patrick Venturea24c8802018-04-17 19:38:06 -0700477 _interval = strtoull(interval.c_str(), NULL, 10);
Patrick Ventureab10f162017-05-22 09:44:50 -0700478 }
479 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600480}
Patrick Ventureab10f162017-05-22 09:44:50 -0700481
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600482void MainLoop::read()
483{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500484 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
485 // ensure the objects all exist?
486
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600487 // Iterate through all the sensors.
488 for (auto& i : state)
489 {
490 auto& attrs = std::get<0>(i.second);
491 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500492 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600493 // Read value from sensor.
494 int64_t value;
495 std::string input = hwmon::entry::cinput;
496 if (i.first.first == "pwm") {
497 input = "";
498 }
499
500 try
Brad Bishope55ef3d2016-12-19 09:12:40 -0500501 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600502 // Retry for up to a second if device is busy
503 // or has a transient error.
Patrick Venture9331ab72018-01-29 09:48:47 -0800504
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600505 value = ioAccess.read(
506 i.first.first,
507 i.first.second,
508 input,
509 sysfs::hwmonio::retries,
510 sysfs::hwmonio::delay,
511 _isOCC);
512
513 value = adjustValue(i.first, value);
514
515 auto& objInfo = std::get<ObjectInfo>(i.second);
516 auto& obj = std::get<Object>(objInfo);
517
518 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500519 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600520 auto valueIface = std::shared_ptr<ValueObject>();
521 auto warnIface = std::shared_ptr<WarningObject>();
522 auto critIface = std::shared_ptr<CriticalObject>();
Brad Bishop754d38c2017-09-08 00:46:58 -0400523
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600524 switch (iface.first)
Brad Bishope0b7d052017-01-06 15:30:23 -0500525 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600526 case InterfaceType::VALUE:
527 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
528 (iface.second);
529 valueIface->value(value);
530 break;
531 case InterfaceType::WARN:
532 checkThresholds<WarningObject>(iface.second, value);
533 break;
534 case InterfaceType::CRIT:
535 checkThresholds<CriticalObject>(iface.second, value);
536 break;
537 default:
538 break;
Brad Bishope0b7d052017-01-06 15:30:23 -0500539 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500540 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600541 }
542 catch (const std::system_error& e)
543 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500544 auto file = sysfs::make_sysfs_path(
545 ioAccess.path(),
546 i.first.first,
547 i.first.second,
548 hwmon::entry::cinput);
Matthew Barth8772ce32018-03-22 16:03:06 -0500549#ifndef REMOVE_ON_FAIL
550 // Check sensorAdjusts for sensor removal RCs
551 const auto& it = sensorAdjusts.find(i.first);
552 if (it != sensorAdjusts.end())
553 {
554 auto rmRCit = it->second.rmRCs.find(e.code().value());
555 if (rmRCit != std::end(it->second.rmRCs))
556 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500557 // Return code found in sensor return code removal list
558 if (rmSensors.find(i.first) == rmSensors.end())
559 {
560 // Trace for sensor not already removed from dbus
561 log<level::INFO>(
562 "Remove sensor from dbus for read fail",
563 entry("FILE=%s", file.c_str()),
564 entry("RC=%d", e.code().value()));
565 // Mark this sensor to be removed from dbus
566 rmSensors[i.first] = std::get<0>(i.second);
567 }
Matthew Barth8772ce32018-03-22 16:03:06 -0500568 continue;
569 }
570 }
571#endif
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600572 using namespace sdbusplus::xyz::openbmc_project::
573 Sensor::Device::Error;
574 report<ReadFailure>(
575 xyz::openbmc_project::Sensor::Device::
576 ReadFailure::CALLOUT_ERRNO(e.code().value()),
577 xyz::openbmc_project::Sensor::Device::
578 ReadFailure::CALLOUT_DEVICE_PATH(
579 _devPath.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500580
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600581 log<level::INFO>("Logging failing sysfs file",
582 entry("FILE=%s", file.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500583
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500584#ifdef REMOVE_ON_FAIL
Matthew Barth33db92b2018-03-26 09:55:19 -0500585 rmSensors[i.first] = std::get<0>(i.second);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500586#else
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600587 exit(EXIT_FAILURE);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500588#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500589 }
590 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600591 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500592
Matthew Barth8772ce32018-03-22 16:03:06 -0500593 // Remove any sensors marked for removal
Matthew Barth33db92b2018-03-26 09:55:19 -0500594 for (auto& i : rmSensors)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600595 {
Matthew Barth33db92b2018-03-26 09:55:19 -0500596 state.erase(i.first);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500597 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500598
599#ifndef REMOVE_ON_FAIL
600 // Attempt to add any sensors that were removed
601 auto it = rmSensors.begin();
602 while (it != rmSensors.end())
603 {
604 if (state.find(it->first) == state.end())
605 {
606 SensorSet::container_t::value_type ssValueType =
607 std::make_pair(it->first, it->second);
608 getObject(ssValueType);
609 if (state.find(it->first) != state.end())
610 {
611 // Sensor object added, erase entry from removal list
Matthew Barth38c74e72018-04-02 12:41:26 -0500612 auto file = sysfs::make_sysfs_path(
613 ioAccess.path(),
614 it->first.first,
615 it->first.second,
616 hwmon::entry::cinput);
617 log<level::INFO>(
618 "Added sensor to dbus after successful read",
619 entry("FILE=%s", file.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500620 it = rmSensors.erase(it);
621 }
622 else
623 {
624 ++it;
625 }
626 }
627 else
628 {
629 // Sanity check to remove sensors that were re-added
630 it = rmSensors.erase(it);
631 }
632 }
633#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500634}
635
636// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4