blob: 4c74f536c916e18ca9e865c51a50b089a2f07e00 [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>
Patrick Venture50cf1c52018-04-18 09:21:41 -070020#include <cstring>
Chiabing Leec923ce92017-09-06 15:58:26 +080021#include <string>
Matthew Barthd26e2772018-03-22 14:24:06 -050022#include <unordered_set>
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 Venture09791852018-04-17 17:40:00 -070030#include "sensorset.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050031#include "sysfs.hpp"
Brad Bishopd499ca62016-12-19 09:24:50 -050032#include "mainloop.hpp"
Matthew Barthbf7b7b12017-03-07 15:46:59 -060033#include "targets.hpp"
Patrick Venture09791852018-04-17 17:40:00 -070034#include "thresholds.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050035
Patrick Venture1e6324f2017-06-01 14:07:05 -070036#include <xyz/openbmc_project/Sensor/Device/error.hpp>
37
38using namespace phosphor::logging;
39
Saqib Khan973886d2017-03-15 14:01:16 -050040// Initialization for Warning Objects
41decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo =
42 &WarningObject::warningLow;
43decltype(Thresholds<WarningObject>::setHi) Thresholds<WarningObject>::setHi =
44 &WarningObject::warningHigh;
45decltype(Thresholds<WarningObject>::getLo) Thresholds<WarningObject>::getLo =
46 &WarningObject::warningLow;
47decltype(Thresholds<WarningObject>::getHi) Thresholds<WarningObject>::getHi =
48 &WarningObject::warningHigh;
49decltype(Thresholds<WarningObject>::alarmLo) Thresholds<WarningObject>::alarmLo =
50 &WarningObject::warningAlarmLow;
51decltype(Thresholds<WarningObject>::alarmHi) Thresholds<WarningObject>::alarmHi =
52 &WarningObject::warningAlarmHigh;
53
54// Initialization for Critical Objects
55decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo =
56 &CriticalObject::criticalLow;
57decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi =
58 &CriticalObject::criticalHigh;
59decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo =
60 &CriticalObject::criticalLow;
61decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi =
62 &CriticalObject::criticalHigh;
63decltype(Thresholds<CriticalObject>::alarmLo) Thresholds<CriticalObject>::alarmLo =
64 &CriticalObject::criticalAlarmLow;
65decltype(Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi =
66 &CriticalObject::criticalAlarmHigh;
67
Chiabing Leec923ce92017-09-06 15:58:26 +080068// The gain and offset to adjust a value
69struct valueAdjust
70{
71 double gain = 1.0;
72 int offset = 0;
Matthew Barthd26e2772018-03-22 14:24:06 -050073 std::unordered_set<int> rmRCs;
Chiabing Leec923ce92017-09-06 15:58:26 +080074};
75
76// Store the valueAdjust for sensors
77std::map<SensorSet::key_type, valueAdjust> sensorAdjusts;
78
Matthew Barthd26e2772018-03-22 14:24:06 -050079void addRemoveRCs(const SensorSet::key_type& sensor,
80 const std::string& rcList)
81{
Matthew Barthb7985272018-04-17 10:50:36 -050082 if (rcList.empty())
83 {
84 return;
85 }
86
Matthew Barthd26e2772018-03-22 14:24:06 -050087 // Convert to a char* for strtok
88 std::vector<char> rmRCs(rcList.c_str(),
89 rcList.c_str() + rcList.size() + 1);
Patrick Venture50cf1c52018-04-18 09:21:41 -070090 auto rmRC = std::strtok(&rmRCs[0], ", ");
Matthew Barthd26e2772018-03-22 14:24:06 -050091 while (rmRC != nullptr)
92 {
93 try
94 {
95 sensorAdjusts[sensor].rmRCs.insert(std::stoi(rmRC));
96 }
97 catch (const std::logic_error& le)
98 {
99 // Unable to convert to int, continue to next token
100 std::string name = sensor.first + "_" + sensor.second;
101 log<level::INFO>("Unable to convert sensor removal return code",
102 entry("SENSOR=%s", name.c_str()),
103 entry("RC=%s", rmRC),
104 entry("EXCEPTION=%s", le.what()));
105 }
Patrick Venture50cf1c52018-04-18 09:21:41 -0700106 rmRC = std::strtok(nullptr, ", ");
Matthew Barthd26e2772018-03-22 14:24:06 -0500107 }
108}
109
Matt Spinlerfee106b2017-11-29 15:18:05 -0600110int64_t adjustValue(const SensorSet::key_type& sensor, int64_t value)
Chiabing Leec923ce92017-09-06 15:58:26 +0800111{
Patrick Venturec1cece72017-11-07 12:09:49 -0800112// Because read doesn't have an out pointer to store errors.
113// let's assume negative values are errors if they have this
114// set.
115#ifdef NEGATIVE_ERRNO_ON_FAIL
116 if (value < 0)
117 {
118 return value;
119 }
120#endif
121
Chiabing Leec923ce92017-09-06 15:58:26 +0800122 const auto& it = sensorAdjusts.find(sensor);
123 if (it != sensorAdjusts.end())
124 {
125 // Adjust based on gain and offset
126 value = static_cast<decltype(value)>(
127 static_cast<double>(value) * it->second.gain
128 + it->second.offset);
129 }
130 return value;
131}
132
Brad Bishope9fdee02017-01-06 10:43:29 -0500133auto addValue(const SensorSet::key_type& sensor,
Matthew Barthd4beecf2018-04-03 15:50:22 -0500134 const RetryIO& retryIO,
Brad Bishop751043e2017-08-29 11:13:46 -0400135 sysfs::hwmonio::HwmonIO& ioAccess,
Matthew Bartha23babd2018-03-16 10:03:27 -0500136 ObjectInfo& info,
137 bool isOCC = false)
Brad Bishope9fdee02017-01-06 10:43:29 -0500138{
Brad Bishop30dbcee2017-01-18 07:55:42 -0500139 static constexpr bool deferSignals = true;
140
Brad Bishope9fdee02017-01-06 10:43:29 -0500141 // Get the initial value for the value interface.
142 auto& bus = *std::get<sdbusplus::bus::bus*>(info);
143 auto& obj = std::get<Object>(info);
144 auto& objPath = std::get<std::string>(info);
145
Patrick Venture7a5285d2018-04-17 19:15:05 -0700146 auto senRmRCs = env::getEnv("REMOVERCS", sensor);
Matthew Barthb7985272018-04-17 10:50:36 -0500147 // Add sensor removal return codes defined per sensor
148 addRemoveRCs(sensor, senRmRCs);
Matthew Barthd26e2772018-03-22 14:24:06 -0500149
Matthew Barth8772ce32018-03-22 16:03:06 -0500150 // Retry for up to a second if device is busy
151 // or has a transient error.
152 int64_t val = ioAccess.read(
153 sensor.first,
154 sensor.second,
155 hwmon::entry::cinput,
Matthew Barthd4beecf2018-04-03 15:50:22 -0500156 std::get<size_t>(retryIO),
157 std::get<std::chrono::milliseconds>(retryIO),
Matthew Barth8772ce32018-03-22 16:03:06 -0500158 isOCC);
Patrick Venture1e6324f2017-06-01 14:07:05 -0700159
Patrick Venture7a5285d2018-04-17 19:15:05 -0700160 auto gain = env::getEnv("GAIN", sensor);
Chiabing Leec923ce92017-09-06 15:58:26 +0800161 if (!gain.empty())
162 {
163 sensorAdjusts[sensor].gain = std::stod(gain);
164 }
165
Patrick Venture7a5285d2018-04-17 19:15:05 -0700166 auto offset = env::getEnv("OFFSET", sensor);
Chiabing Leec923ce92017-09-06 15:58:26 +0800167 if (!offset.empty())
168 {
169 sensorAdjusts[sensor].offset = std::stoi(offset);
170 }
171
172 val = adjustValue(sensor, val);
173
Brad Bishop30dbcee2017-01-18 07:55:42 -0500174 auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals);
Brad Bishope9fdee02017-01-06 10:43:29 -0500175 iface->value(val);
176
Patrick Venture09791852018-04-17 17:40:00 -0700177 hwmon::Attributes attrs;
178 if (hwmon::getAttributes(sensor.first, attrs))
Brad Bishope9fdee02017-01-06 10:43:29 -0500179 {
Patrick Venture09791852018-04-17 17:40:00 -0700180 iface->unit(hwmon::getUnit(attrs));
181 iface->scale(hwmon::getScale(attrs));
Brad Bishope9fdee02017-01-06 10:43:29 -0500182 }
183
Patrick Venture7a5285d2018-04-17 19:15:05 -0700184 auto maxValue = env::getEnv("MAXVALUE", sensor);
James Feist7bd5fcf2018-01-22 16:14:28 -0800185 if(!maxValue.empty())
186 {
187 iface->maxValue(std::stoll(maxValue));
188 }
Patrick Venture7a5285d2018-04-17 19:15:05 -0700189 auto minValue = env::getEnv("MINVALUE", sensor);
James Feist7bd5fcf2018-01-22 16:14:28 -0800190 if(!minValue.empty())
191 {
192 iface->minValue(std::stoll(minValue));
193 }
194
Brad Bishope9fdee02017-01-06 10:43:29 -0500195 obj[InterfaceType::VALUE] = iface;
196 return iface;
197}
198
Matthew Barth979c8062018-04-17 11:37:15 -0500199std::string MainLoop::getID(SensorSet::container_t::const_reference sensor)
Matthew Barth31d214c2018-03-26 09:54:27 -0500200{
Matthew Barth31d214c2018-03-26 09:54:27 -0500201 std::string id;
202
203 /*
204 * Check if the value of the MODE_<item><X> env variable for the sensor
205 * is "label", then read the sensor number from the <item><X>_label
206 * file. The name of the DBUS object would be the value of the env
207 * variable LABEL_<item><sensorNum>. If the MODE_<item><X> env variable
208 * doesn't exist, then the name of DBUS object is the value of the env
209 * variable LABEL_<item><X>.
210 */
Patrick Venture7a5285d2018-04-17 19:15:05 -0700211 auto mode = env::getEnv("MODE", sensor.first);
Matthew Barth31d214c2018-03-26 09:54:27 -0500212 if (!mode.compare(hwmon::entry::label))
213 {
Patrick Venture7a5285d2018-04-17 19:15:05 -0700214 id = env::getIndirectID(
Matthew Barth31d214c2018-03-26 09:54:27 -0500215 _hwmonRoot + '/' + _instance + '/', sensor.first);
216
217 if (id.empty())
218 {
Matthew Barth979c8062018-04-17 11:37:15 -0500219 return id;
Matthew Barth31d214c2018-03-26 09:54:27 -0500220 }
221 }
222
223 // Use the ID we looked up above if there was one,
224 // otherwise use the standard one.
225 id = (id.empty()) ? sensor.first.second : id;
226
Matthew Barth979c8062018-04-17 11:37:15 -0500227 return id;
228}
229
230SensorIdentifiers MainLoop::getIdentifiers(
231 SensorSet::container_t::const_reference sensor)
232{
233 std::string id = getID(sensor);
234 std::string label;
235
236 if (!id.empty())
237 {
238 // Ignore inputs without a label.
239 label = env::getEnv("LABEL", sensor.first.first, id);
240 }
241
242 return std::make_tuple(std::move(id),
243 std::move(label));
244}
245
246/**
247 * Reads the environment parameters of a sensor and creates an object with
248 * atleast the `Value` interface, otherwise returns without creating the object.
249 * If the `Value` interface is successfully created, by reading the sensor's
250 * corresponding sysfs file's value, the additional interfaces for the sensor
251 * are created and the InterfacesAdded signal is emitted. The sensor is then
252 * moved to the list for sensor state monitoring within the main loop.
253 */
254void MainLoop::getObject(SensorSet::container_t::const_reference sensor)
255{
256 auto properties = getIdentifiers(sensor);
257 if (std::get<sensorID>(properties).empty() ||
258 std::get<sensorLabel>(properties).empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500259 {
260 return;
261 }
262
Patrick Venture09791852018-04-17 17:40:00 -0700263 hwmon::Attributes attrs;
264 if (!hwmon::getAttributes(sensor.first.first, attrs))
Matthew Barth31d214c2018-03-26 09:54:27 -0500265 {
266 return;
267 }
268
Matthew Barthb7985272018-04-17 10:50:36 -0500269 // Get list of return codes for removing sensors on device
270 auto devRmRCs = env::getEnv("REMOVERCS");
271 // Add sensor removal return codes defined at the device level
272 addRemoveRCs(sensor.first, devRmRCs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500273
274 std::string objectPath{_root};
275 objectPath.append(1, '/');
Patrick Venture09791852018-04-17 17:40:00 -0700276 objectPath.append(hwmon::getNamespace(attrs));
Matthew Barth31d214c2018-03-26 09:54:27 -0500277 objectPath.append(1, '/');
Matthew Barth979c8062018-04-17 11:37:15 -0500278 objectPath.append(std::get<sensorLabel>(properties));
Matthew Barth31d214c2018-03-26 09:54:27 -0500279
280 ObjectInfo info(&_bus, std::move(objectPath), Object());
Matthew Barthd4beecf2018-04-03 15:50:22 -0500281 RetryIO retryIO(sysfs::hwmonio::retries, sysfs::hwmonio::delay);
282 if (rmSensors.find(sensor.first) != rmSensors.end())
283 {
284 // When adding a sensor that was purposely removed,
285 // don't retry on errors when reading its value
286 std::get<size_t>(retryIO) = 0;
287 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500288 auto valueInterface = static_cast<
289 std::shared_ptr<ValueObject>>(nullptr);
290 try
291 {
Matthew Barthd4beecf2018-04-03 15:50:22 -0500292 valueInterface = addValue(sensor.first, retryIO, ioAccess, info,
Matthew Barth31d214c2018-03-26 09:54:27 -0500293 _isOCC);
294 }
295 catch (const std::system_error& e)
296 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500297 auto file = sysfs::make_sysfs_path(
298 ioAccess.path(),
299 sensor.first.first,
300 sensor.first.second,
301 hwmon::entry::cinput);
Matthew Barth31d214c2018-03-26 09:54:27 -0500302#ifndef REMOVE_ON_FAIL
303 // Check sensorAdjusts for sensor removal RCs
304 const auto& it = sensorAdjusts.find(sensor.first);
305 if (it != sensorAdjusts.end())
306 {
307 auto rmRCit = it->second.rmRCs.find(e.code().value());
308 if (rmRCit != std::end(it->second.rmRCs))
309 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500310 // Return code found in sensor return code removal list
311 if (rmSensors.find(sensor.first) == rmSensors.end())
312 {
313 // Trace for sensor not already removed from dbus
314 log<level::INFO>("Sensor not added to dbus for read fail",
315 entry("FILE=%s", file.c_str()),
316 entry("RC=%d", e.code().value()));
317 rmSensors[std::move(sensor.first)] =
318 std::move(sensor.second);
319 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500320 return;
321 }
322 }
323#endif
324 using namespace sdbusplus::xyz::openbmc_project::
325 Sensor::Device::Error;
326 report<ReadFailure>(
327 xyz::openbmc_project::Sensor::Device::
328 ReadFailure::CALLOUT_ERRNO(e.code().value()),
329 xyz::openbmc_project::Sensor::Device::
330 ReadFailure::CALLOUT_DEVICE_PATH(_devPath.c_str()));
331
Matthew Barth31d214c2018-03-26 09:54:27 -0500332 log<level::INFO>("Logging failing sysfs file",
333 entry("FILE=%s", file.c_str()));
334#ifdef REMOVE_ON_FAIL
335 return; /* skip adding this sensor for now. */
336#else
337 exit(EXIT_FAILURE);
338#endif
339 }
340 auto sensorValue = valueInterface->value();
Matthew Barth979c8062018-04-17 11:37:15 -0500341 addThreshold<WarningObject>(sensor.first.first,
342 std::get<sensorID>(properties),
343 sensorValue,
344 info);
345 addThreshold<CriticalObject>(sensor.first.first,
346 std::get<sensorID>(properties),
347 sensorValue,
348 info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500349
Matthew Barth28f8e662018-03-26 16:57:36 -0500350 auto target = addTarget<hwmon::FanSpeed>(
351 sensor.first, ioAccess, _devPath, info);
352 if (target)
Matthew Barth31d214c2018-03-26 09:54:27 -0500353 {
Matthew Barth28f8e662018-03-26 16:57:36 -0500354 target->enable();
Matthew Barth31d214c2018-03-26 09:54:27 -0500355 }
Matthew Barth28f8e662018-03-26 16:57:36 -0500356 addTarget<hwmon::FanPwm>(sensor.first, ioAccess, _devPath, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500357
358 // All the interfaces have been created. Go ahead
359 // and emit InterfacesAdded.
360 valueInterface->emit_object_added();
361
362 auto value = std::make_tuple(
363 std::move(sensor.second),
Matthew Barth979c8062018-04-17 11:37:15 -0500364 std::move(std::get<sensorLabel>(properties)),
Matthew Barth31d214c2018-03-26 09:54:27 -0500365 std::move(info));
366
367 state[std::move(sensor.first)] = std::move(value);
368}
369
Brad Bishopb9e2b072016-12-19 13:47:10 -0500370MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500371 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500372 const std::string& path,
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400373 const std::string& devPath,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500374 const char* prefix,
375 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500376 : _bus(std::move(bus)),
Brad Bishop03e87352017-03-07 00:12:22 -0500377 _manager(_bus, root),
Brad Bishopb8740fc2017-02-24 23:38:37 -0500378 _hwmonRoot(),
379 _instance(),
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400380 _devPath(devPath),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500381 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500382 _root(root),
Brad Bishop751043e2017-08-29 11:13:46 -0400383 state(),
384 ioAccess(path)
Brad Bishopd499ca62016-12-19 09:24:50 -0500385{
Matthew Bartha23babd2018-03-16 10:03:27 -0500386 if (path.find("occ") != std::string::npos)
387 {
388 _isOCC = true;
389 }
390
Patrick Venture73a50c72018-04-17 15:19:03 -0700391 // Strip off any trailing slashes.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500392 std::string p = path;
393 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500394 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500395 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500396 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500397
Patrick Venture73a50c72018-04-17 15:19:03 -0700398 // Given the furthest right /, set instance to
399 // the basename, and hwmonRoot to the leading path.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500400 auto n = p.rfind('/');
401 if (n != std::string::npos)
402 {
403 _instance.assign(p.substr(n + 1));
404 _hwmonRoot.assign(p.substr(0, n));
405 }
406
407 assert(!_instance.empty());
408 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500409}
410
411void MainLoop::shutdown() noexcept
412{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600413 timer->state(phosphor::hwmon::timer::OFF);
414 sd_event_exit(loop, 0);
415 loop = nullptr;
Brad Bishopd499ca62016-12-19 09:24:50 -0500416}
417
418void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500419{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600420 init();
421
422 sd_event_default(&loop);
423
424 std::function<void()> callback(std::bind(
425 &MainLoop::read, this));
426 try
427 {
428 timer = std::make_unique<phosphor::hwmon::Timer>(
429 loop, callback,
430 std::chrono::microseconds(_interval),
431 phosphor::hwmon::timer::ON);
432
433 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
434
435 // TODO: Issue#7 - Should probably periodically check the SensorSet
436 // for new entries.
437
438 _bus.attach_event(loop, SD_EVENT_PRIORITY_IMPORTANT);
439 sd_event_loop(loop);
440 }
441 catch (const std::system_error& e)
442 {
443 log<level::ERR>("Error in sysfs polling loop",
444 entry("ERROR=%s", e.what()));
445 throw;
446 }
447}
448
449void MainLoop::init()
450{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500451 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500452 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500453
Brad Bishop75b4ab82017-01-06 09:33:50 -0500454 for (auto& i : *sensors)
455 {
Matthew Barth31d214c2018-03-26 09:54:27 -0500456 getObject(i);
Brad Bishop75b4ab82017-01-06 09:33:50 -0500457 }
458
Patrick Venture62503a42017-05-23 07:30:29 -0700459 /* If there are no sensors specified by labels, exit. */
460 if (0 == state.size())
461 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600462 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700463 }
464
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500465 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500466 std::string busname{_prefix};
Brad Bishop4d9a35b2017-11-14 22:33:03 -0500467 busname.append(1, '-');
468 busname.append(
469 std::to_string(std::hash<decltype(_devPath)>{}(_devPath)));
470 busname.append(".Hwmon1");
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500471 _bus.request_name(busname.c_str());
472 }
473
Patrick Ventureab10f162017-05-22 09:44:50 -0700474 {
Patrick Venturea24c8802018-04-17 19:38:06 -0700475 auto interval = env::getEnv("INTERVAL");
476 if (!interval.empty())
Patrick Ventureab10f162017-05-22 09:44:50 -0700477 {
Patrick Venture50cf1c52018-04-18 09:21:41 -0700478 _interval = std::strtoull(interval.c_str(), NULL, 10);
Patrick Ventureab10f162017-05-22 09:44:50 -0700479 }
480 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600481}
Patrick Ventureab10f162017-05-22 09:44:50 -0700482
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600483void MainLoop::read()
484{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500485 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
486 // ensure the objects all exist?
487
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600488 // Iterate through all the sensors.
489 for (auto& i : state)
490 {
491 auto& attrs = std::get<0>(i.second);
492 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500493 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600494 // Read value from sensor.
495 int64_t value;
496 std::string input = hwmon::entry::cinput;
497 if (i.first.first == "pwm") {
498 input = "";
499 }
500
501 try
Brad Bishope55ef3d2016-12-19 09:12:40 -0500502 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600503 // Retry for up to a second if device is busy
504 // or has a transient error.
Patrick Venture9331ab72018-01-29 09:48:47 -0800505
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600506 value = ioAccess.read(
507 i.first.first,
508 i.first.second,
509 input,
510 sysfs::hwmonio::retries,
511 sysfs::hwmonio::delay,
512 _isOCC);
513
514 value = adjustValue(i.first, value);
515
516 auto& objInfo = std::get<ObjectInfo>(i.second);
517 auto& obj = std::get<Object>(objInfo);
518
519 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500520 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600521 auto valueIface = std::shared_ptr<ValueObject>();
522 auto warnIface = std::shared_ptr<WarningObject>();
523 auto critIface = std::shared_ptr<CriticalObject>();
Brad Bishop754d38c2017-09-08 00:46:58 -0400524
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600525 switch (iface.first)
Brad Bishope0b7d052017-01-06 15:30:23 -0500526 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600527 case InterfaceType::VALUE:
528 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
529 (iface.second);
530 valueIface->value(value);
531 break;
532 case InterfaceType::WARN:
533 checkThresholds<WarningObject>(iface.second, value);
534 break;
535 case InterfaceType::CRIT:
536 checkThresholds<CriticalObject>(iface.second, value);
537 break;
538 default:
539 break;
Brad Bishope0b7d052017-01-06 15:30:23 -0500540 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500541 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600542 }
543 catch (const std::system_error& e)
544 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500545 auto file = sysfs::make_sysfs_path(
546 ioAccess.path(),
547 i.first.first,
548 i.first.second,
549 hwmon::entry::cinput);
Matthew Barth8772ce32018-03-22 16:03:06 -0500550#ifndef REMOVE_ON_FAIL
551 // Check sensorAdjusts for sensor removal RCs
552 const auto& it = sensorAdjusts.find(i.first);
553 if (it != sensorAdjusts.end())
554 {
555 auto rmRCit = it->second.rmRCs.find(e.code().value());
556 if (rmRCit != std::end(it->second.rmRCs))
557 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500558 // Return code found in sensor return code removal list
559 if (rmSensors.find(i.first) == rmSensors.end())
560 {
561 // Trace for sensor not already removed from dbus
562 log<level::INFO>(
563 "Remove sensor from dbus for read fail",
564 entry("FILE=%s", file.c_str()),
565 entry("RC=%d", e.code().value()));
566 // Mark this sensor to be removed from dbus
567 rmSensors[i.first] = std::get<0>(i.second);
568 }
Matthew Barth8772ce32018-03-22 16:03:06 -0500569 continue;
570 }
571 }
572#endif
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600573 using namespace sdbusplus::xyz::openbmc_project::
574 Sensor::Device::Error;
575 report<ReadFailure>(
576 xyz::openbmc_project::Sensor::Device::
577 ReadFailure::CALLOUT_ERRNO(e.code().value()),
578 xyz::openbmc_project::Sensor::Device::
579 ReadFailure::CALLOUT_DEVICE_PATH(
580 _devPath.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500581
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600582 log<level::INFO>("Logging failing sysfs file",
583 entry("FILE=%s", file.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500584
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500585#ifdef REMOVE_ON_FAIL
Matthew Barth33db92b2018-03-26 09:55:19 -0500586 rmSensors[i.first] = std::get<0>(i.second);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500587#else
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600588 exit(EXIT_FAILURE);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500589#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500590 }
591 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600592 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500593
Matthew Barth8772ce32018-03-22 16:03:06 -0500594 // Remove any sensors marked for removal
Matthew Barth33db92b2018-03-26 09:55:19 -0500595 for (auto& i : rmSensors)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600596 {
Matthew Barth33db92b2018-03-26 09:55:19 -0500597 state.erase(i.first);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500598 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500599
600#ifndef REMOVE_ON_FAIL
601 // Attempt to add any sensors that were removed
602 auto it = rmSensors.begin();
603 while (it != rmSensors.end())
604 {
605 if (state.find(it->first) == state.end())
606 {
607 SensorSet::container_t::value_type ssValueType =
608 std::make_pair(it->first, it->second);
609 getObject(ssValueType);
610 if (state.find(it->first) != state.end())
611 {
612 // Sensor object added, erase entry from removal list
Matthew Barth38c74e72018-04-02 12:41:26 -0500613 auto file = sysfs::make_sysfs_path(
614 ioAccess.path(),
615 it->first.first,
616 it->first.second,
617 hwmon::entry::cinput);
618 log<level::INFO>(
619 "Added sensor to dbus after successful read",
620 entry("FILE=%s", file.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500621 it = rmSensors.erase(it);
622 }
623 else
624 {
625 ++it;
626 }
627 }
628 else
629 {
630 // Sanity check to remove sensors that were re-added
631 it = rmSensors.erase(it);
632 }
633 }
634#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500635}
636
637// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4