blob: a9898b718b30bb3f908ae989a0c97038bafdbd42 [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 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 Barthd26e2772018-03-22 14:24:06 -050081void addRemoveRCs(const SensorSet::key_type& sensor,
82 const std::string& rcList)
83{
Matthew Barthb7985272018-04-17 10:50:36 -050084 if (rcList.empty())
85 {
86 return;
87 }
88
Matthew Barthd26e2772018-03-22 14:24:06 -050089 // Convert to a char* for strtok
90 std::vector<char> rmRCs(rcList.c_str(),
91 rcList.c_str() + rcList.size() + 1);
Patrick Venture50cf1c52018-04-18 09:21:41 -070092 auto rmRC = std::strtok(&rmRCs[0], ", ");
Matthew Barthd26e2772018-03-22 14:24:06 -050093 while (rmRC != nullptr)
94 {
95 try
96 {
97 sensorAdjusts[sensor].rmRCs.insert(std::stoi(rmRC));
98 }
99 catch (const std::logic_error& le)
100 {
101 // Unable to convert to int, continue to next token
102 std::string name = sensor.first + "_" + sensor.second;
103 log<level::INFO>("Unable to convert sensor removal return code",
104 entry("SENSOR=%s", name.c_str()),
105 entry("RC=%s", rmRC),
106 entry("EXCEPTION=%s", le.what()));
107 }
Patrick Venture50cf1c52018-04-18 09:21:41 -0700108 rmRC = std::strtok(nullptr, ", ");
Matthew Barthd26e2772018-03-22 14:24:06 -0500109 }
110}
111
Matt Spinlerfee106b2017-11-29 15:18:05 -0600112int64_t adjustValue(const SensorSet::key_type& sensor, int64_t value)
Chiabing Leec923ce92017-09-06 15:58:26 +0800113{
Patrick Venturec1cece72017-11-07 12:09:49 -0800114// Because read doesn't have an out pointer to store errors.
115// let's assume negative values are errors if they have this
116// set.
117#ifdef NEGATIVE_ERRNO_ON_FAIL
118 if (value < 0)
119 {
120 return value;
121 }
122#endif
123
Chiabing Leec923ce92017-09-06 15:58:26 +0800124 const auto& it = sensorAdjusts.find(sensor);
125 if (it != sensorAdjusts.end())
126 {
127 // Adjust based on gain and offset
128 value = static_cast<decltype(value)>(
129 static_cast<double>(value) * it->second.gain
130 + it->second.offset);
131 }
132 return value;
133}
134
Brad Bishope9fdee02017-01-06 10:43:29 -0500135auto addValue(const SensorSet::key_type& sensor,
Matthew Barthd4beecf2018-04-03 15:50:22 -0500136 const RetryIO& retryIO,
Patrick Venture75e56c62018-04-20 18:10:15 -0700137 hwmonio::HwmonIO& ioAccess,
Matthew Bartha23babd2018-03-16 10:03:27 -0500138 ObjectInfo& info,
139 bool isOCC = false)
Brad Bishope9fdee02017-01-06 10:43:29 -0500140{
Brad Bishop30dbcee2017-01-18 07:55:42 -0500141 static constexpr bool deferSignals = true;
142
Brad Bishope9fdee02017-01-06 10:43:29 -0500143 // Get the initial value for the value interface.
144 auto& bus = *std::get<sdbusplus::bus::bus*>(info);
145 auto& obj = std::get<Object>(info);
146 auto& objPath = std::get<std::string>(info);
147
Patrick Venture7a5285d2018-04-17 19:15:05 -0700148 auto senRmRCs = env::getEnv("REMOVERCS", sensor);
Matthew Barthb7985272018-04-17 10:50:36 -0500149 // Add sensor removal return codes defined per sensor
150 addRemoveRCs(sensor, senRmRCs);
Matthew Barthd26e2772018-03-22 14:24:06 -0500151
Matthew Barth8772ce32018-03-22 16:03:06 -0500152 // Retry for up to a second if device is busy
153 // or has a transient error.
154 int64_t val = ioAccess.read(
155 sensor.first,
156 sensor.second,
157 hwmon::entry::cinput,
Matthew Barthd4beecf2018-04-03 15:50:22 -0500158 std::get<size_t>(retryIO),
159 std::get<std::chrono::milliseconds>(retryIO),
Matthew Barth8772ce32018-03-22 16:03:06 -0500160 isOCC);
Patrick Venture1e6324f2017-06-01 14:07:05 -0700161
Patrick Venture7a5285d2018-04-17 19:15:05 -0700162 auto gain = env::getEnv("GAIN", sensor);
Chiabing Leec923ce92017-09-06 15:58:26 +0800163 if (!gain.empty())
164 {
165 sensorAdjusts[sensor].gain = std::stod(gain);
166 }
167
Patrick Venture7a5285d2018-04-17 19:15:05 -0700168 auto offset = env::getEnv("OFFSET", sensor);
Chiabing Leec923ce92017-09-06 15:58:26 +0800169 if (!offset.empty())
170 {
171 sensorAdjusts[sensor].offset = std::stoi(offset);
172 }
173
174 val = adjustValue(sensor, val);
175
Brad Bishop30dbcee2017-01-18 07:55:42 -0500176 auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals);
Brad Bishope9fdee02017-01-06 10:43:29 -0500177 iface->value(val);
178
Patrick Venture09791852018-04-17 17:40:00 -0700179 hwmon::Attributes attrs;
180 if (hwmon::getAttributes(sensor.first, attrs))
Brad Bishope9fdee02017-01-06 10:43:29 -0500181 {
Patrick Venture09791852018-04-17 17:40:00 -0700182 iface->unit(hwmon::getUnit(attrs));
183 iface->scale(hwmon::getScale(attrs));
Brad Bishope9fdee02017-01-06 10:43:29 -0500184 }
185
Patrick Venture7a5285d2018-04-17 19:15:05 -0700186 auto maxValue = env::getEnv("MAXVALUE", sensor);
James Feist7bd5fcf2018-01-22 16:14:28 -0800187 if(!maxValue.empty())
188 {
189 iface->maxValue(std::stoll(maxValue));
190 }
Patrick Venture7a5285d2018-04-17 19:15:05 -0700191 auto minValue = env::getEnv("MINVALUE", sensor);
James Feist7bd5fcf2018-01-22 16:14:28 -0800192 if(!minValue.empty())
193 {
194 iface->minValue(std::stoll(minValue));
195 }
196
Brad Bishope9fdee02017-01-06 10:43:29 -0500197 obj[InterfaceType::VALUE] = iface;
198 return iface;
199}
200
Matthew Barth979c8062018-04-17 11:37:15 -0500201std::string MainLoop::getID(SensorSet::container_t::const_reference sensor)
Matthew Barth31d214c2018-03-26 09:54:27 -0500202{
Matthew Barth31d214c2018-03-26 09:54:27 -0500203 std::string id;
204
205 /*
206 * Check if the value of the MODE_<item><X> env variable for the sensor
207 * is "label", then read the sensor number from the <item><X>_label
208 * file. The name of the DBUS object would be the value of the env
209 * variable LABEL_<item><sensorNum>. If the MODE_<item><X> env variable
210 * doesn't exist, then the name of DBUS object is the value of the env
211 * variable LABEL_<item><X>.
212 */
Patrick Venture7a5285d2018-04-17 19:15:05 -0700213 auto mode = env::getEnv("MODE", sensor.first);
Matthew Barth31d214c2018-03-26 09:54:27 -0500214 if (!mode.compare(hwmon::entry::label))
215 {
Patrick Venture7a5285d2018-04-17 19:15:05 -0700216 id = env::getIndirectID(
Matthew Barth31d214c2018-03-26 09:54:27 -0500217 _hwmonRoot + '/' + _instance + '/', sensor.first);
218
219 if (id.empty())
220 {
Matthew Barth979c8062018-04-17 11:37:15 -0500221 return id;
Matthew Barth31d214c2018-03-26 09:54:27 -0500222 }
223 }
224
225 // Use the ID we looked up above if there was one,
226 // otherwise use the standard one.
227 id = (id.empty()) ? sensor.first.second : id;
228
Matthew Barth979c8062018-04-17 11:37:15 -0500229 return id;
230}
231
232SensorIdentifiers MainLoop::getIdentifiers(
233 SensorSet::container_t::const_reference sensor)
234{
235 std::string id = getID(sensor);
236 std::string label;
237
238 if (!id.empty())
239 {
240 // Ignore inputs without a label.
241 label = env::getEnv("LABEL", sensor.first.first, id);
242 }
243
244 return std::make_tuple(std::move(id),
245 std::move(label));
246}
247
248/**
249 * Reads the environment parameters of a sensor and creates an object with
250 * atleast the `Value` interface, otherwise returns without creating the object.
251 * If the `Value` interface is successfully created, by reading the sensor's
252 * corresponding sysfs file's value, the additional interfaces for the sensor
Matthew Barthd238e232018-04-17 12:01:50 -0500253 * are created and the InterfacesAdded signal is emitted. The object's state
254 * data is then returned for sensor state monitoring within the main loop.
Matthew Barth979c8062018-04-17 11:37:15 -0500255 */
Matthew Barthd238e232018-04-17 12:01:50 -0500256optional_ns::optional<ObjectStateData> MainLoop::getObject(
257 SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500258{
259 auto properties = getIdentifiers(sensor);
260 if (std::get<sensorID>(properties).empty() ||
261 std::get<sensorLabel>(properties).empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500262 {
Matthew Barthd238e232018-04-17 12:01:50 -0500263 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500264 }
265
Patrick Venture09791852018-04-17 17:40:00 -0700266 hwmon::Attributes attrs;
267 if (!hwmon::getAttributes(sensor.first.first, attrs))
Matthew Barth31d214c2018-03-26 09:54:27 -0500268 {
Matthew Barthd238e232018-04-17 12:01:50 -0500269 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500270 }
271
Matthew Barthb7985272018-04-17 10:50:36 -0500272 // Get list of return codes for removing sensors on device
273 auto devRmRCs = env::getEnv("REMOVERCS");
274 // Add sensor removal return codes defined at the device level
275 addRemoveRCs(sensor.first, devRmRCs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500276
277 std::string objectPath{_root};
278 objectPath.append(1, '/');
Patrick Venture09791852018-04-17 17:40:00 -0700279 objectPath.append(hwmon::getNamespace(attrs));
Matthew Barth31d214c2018-03-26 09:54:27 -0500280 objectPath.append(1, '/');
Matthew Barth979c8062018-04-17 11:37:15 -0500281 objectPath.append(std::get<sensorLabel>(properties));
Matthew Barth31d214c2018-03-26 09:54:27 -0500282
283 ObjectInfo info(&_bus, std::move(objectPath), Object());
Patrick Venture75e56c62018-04-20 18:10:15 -0700284 RetryIO retryIO(hwmonio::retries, hwmonio::delay);
Matthew Barthd4beecf2018-04-03 15:50:22 -0500285 if (rmSensors.find(sensor.first) != rmSensors.end())
286 {
287 // When adding a sensor that was purposely removed,
288 // don't retry on errors when reading its value
289 std::get<size_t>(retryIO) = 0;
290 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500291 auto valueInterface = static_cast<
292 std::shared_ptr<ValueObject>>(nullptr);
293 try
294 {
Matthew Barthd4beecf2018-04-03 15:50:22 -0500295 valueInterface = addValue(sensor.first, retryIO, ioAccess, info,
Matthew Barth31d214c2018-03-26 09:54:27 -0500296 _isOCC);
297 }
298 catch (const std::system_error& e)
299 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500300 auto file = sysfs::make_sysfs_path(
301 ioAccess.path(),
302 sensor.first.first,
303 sensor.first.second,
304 hwmon::entry::cinput);
Matthew Barth31d214c2018-03-26 09:54:27 -0500305#ifndef REMOVE_ON_FAIL
306 // Check sensorAdjusts for sensor removal RCs
307 const auto& it = sensorAdjusts.find(sensor.first);
308 if (it != sensorAdjusts.end())
309 {
310 auto rmRCit = it->second.rmRCs.find(e.code().value());
311 if (rmRCit != std::end(it->second.rmRCs))
312 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500313 // Return code found in sensor return code removal list
314 if (rmSensors.find(sensor.first) == rmSensors.end())
315 {
316 // Trace for sensor not already removed from dbus
317 log<level::INFO>("Sensor not added to dbus for read fail",
318 entry("FILE=%s", file.c_str()),
319 entry("RC=%d", e.code().value()));
320 rmSensors[std::move(sensor.first)] =
321 std::move(sensor.second);
322 }
Matthew Barthd238e232018-04-17 12:01:50 -0500323 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500324 }
325 }
326#endif
327 using namespace sdbusplus::xyz::openbmc_project::
328 Sensor::Device::Error;
329 report<ReadFailure>(
330 xyz::openbmc_project::Sensor::Device::
331 ReadFailure::CALLOUT_ERRNO(e.code().value()),
332 xyz::openbmc_project::Sensor::Device::
333 ReadFailure::CALLOUT_DEVICE_PATH(_devPath.c_str()));
334
Matthew Barth31d214c2018-03-26 09:54:27 -0500335 log<level::INFO>("Logging failing sysfs file",
336 entry("FILE=%s", file.c_str()));
337#ifdef REMOVE_ON_FAIL
Matthew Barthd238e232018-04-17 12:01:50 -0500338 return {}; /* skip adding this sensor for now. */
Matthew Barth31d214c2018-03-26 09:54:27 -0500339#else
340 exit(EXIT_FAILURE);
341#endif
342 }
343 auto sensorValue = valueInterface->value();
Matthew Barth979c8062018-04-17 11:37:15 -0500344 addThreshold<WarningObject>(sensor.first.first,
345 std::get<sensorID>(properties),
346 sensorValue,
347 info);
348 addThreshold<CriticalObject>(sensor.first.first,
349 std::get<sensorID>(properties),
350 sensorValue,
351 info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500352
Matthew Barth28f8e662018-03-26 16:57:36 -0500353 auto target = addTarget<hwmon::FanSpeed>(
354 sensor.first, ioAccess, _devPath, info);
355 if (target)
Matthew Barth31d214c2018-03-26 09:54:27 -0500356 {
Matthew Barth28f8e662018-03-26 16:57:36 -0500357 target->enable();
Matthew Barth31d214c2018-03-26 09:54:27 -0500358 }
Matthew Barth28f8e662018-03-26 16:57:36 -0500359 addTarget<hwmon::FanPwm>(sensor.first, ioAccess, _devPath, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500360
Matthew Barth35819382018-04-18 14:53:01 -0500361 // Add status interface based on _fault file being present
362 sensor::addStatus(sensor.first, ioAccess, _devPath, info);
363
Matthew Barth31d214c2018-03-26 09:54:27 -0500364 // All the interfaces have been created. Go ahead
365 // and emit InterfacesAdded.
366 valueInterface->emit_object_added();
367
Matthew Barthd238e232018-04-17 12:01:50 -0500368 return std::make_pair(std::move(std::get<sensorLabel>(properties)),
369 std::move(info));
Matthew Barth31d214c2018-03-26 09:54:27 -0500370}
371
Brad Bishopb9e2b072016-12-19 13:47:10 -0500372MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500373 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500374 const std::string& path,
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400375 const std::string& devPath,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500376 const char* prefix,
377 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500378 : _bus(std::move(bus)),
Brad Bishop03e87352017-03-07 00:12:22 -0500379 _manager(_bus, root),
Brad Bishopb8740fc2017-02-24 23:38:37 -0500380 _hwmonRoot(),
381 _instance(),
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400382 _devPath(devPath),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500383 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500384 _root(root),
Brad Bishop751043e2017-08-29 11:13:46 -0400385 state(),
386 ioAccess(path)
Brad Bishopd499ca62016-12-19 09:24:50 -0500387{
Matthew Bartha23babd2018-03-16 10:03:27 -0500388 if (path.find("occ") != std::string::npos)
389 {
390 _isOCC = true;
391 }
392
Patrick Venture73a50c72018-04-17 15:19:03 -0700393 // Strip off any trailing slashes.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500394 std::string p = path;
395 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500396 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500397 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500398 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500399
Patrick Venture73a50c72018-04-17 15:19:03 -0700400 // Given the furthest right /, set instance to
401 // the basename, and hwmonRoot to the leading path.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500402 auto n = p.rfind('/');
403 if (n != std::string::npos)
404 {
405 _instance.assign(p.substr(n + 1));
406 _hwmonRoot.assign(p.substr(0, n));
407 }
408
409 assert(!_instance.empty());
410 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500411}
412
413void MainLoop::shutdown() noexcept
414{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600415 timer->state(phosphor::hwmon::timer::OFF);
416 sd_event_exit(loop, 0);
417 loop = nullptr;
Brad Bishopd499ca62016-12-19 09:24:50 -0500418}
419
420void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500421{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600422 init();
423
424 sd_event_default(&loop);
425
426 std::function<void()> callback(std::bind(
427 &MainLoop::read, this));
428 try
429 {
430 timer = std::make_unique<phosphor::hwmon::Timer>(
431 loop, callback,
432 std::chrono::microseconds(_interval),
433 phosphor::hwmon::timer::ON);
434
435 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
436
437 // TODO: Issue#7 - Should probably periodically check the SensorSet
438 // for new entries.
439
440 _bus.attach_event(loop, SD_EVENT_PRIORITY_IMPORTANT);
441 sd_event_loop(loop);
442 }
443 catch (const std::system_error& e)
444 {
445 log<level::ERR>("Error in sysfs polling loop",
446 entry("ERROR=%s", e.what()));
447 throw;
448 }
449}
450
451void MainLoop::init()
452{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500453 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500454 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500455
Brad Bishop75b4ab82017-01-06 09:33:50 -0500456 for (auto& i : *sensors)
457 {
Matthew Barthd238e232018-04-17 12:01:50 -0500458 auto object = getObject(i);
459 if (object)
460 {
461 // Construct the SensorSet value
462 // std::tuple<SensorSet::mapped_type,
463 // std::string(Sensor Label),
464 // ObjectInfo>
465 auto value = std::make_tuple(std::move(i.second),
466 std::move((*object).first),
467 std::move((*object).second));
468
469 state[std::move(i.first)] = std::move(value);
470 }
Brad Bishop75b4ab82017-01-06 09:33:50 -0500471 }
472
Patrick Venture62503a42017-05-23 07:30:29 -0700473 /* If there are no sensors specified by labels, exit. */
474 if (0 == state.size())
475 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600476 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700477 }
478
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500479 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500480 std::string busname{_prefix};
Brad Bishop4d9a35b2017-11-14 22:33:03 -0500481 busname.append(1, '-');
482 busname.append(
483 std::to_string(std::hash<decltype(_devPath)>{}(_devPath)));
484 busname.append(".Hwmon1");
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500485 _bus.request_name(busname.c_str());
486 }
487
Patrick Ventureab10f162017-05-22 09:44:50 -0700488 {
Patrick Venturea24c8802018-04-17 19:38:06 -0700489 auto interval = env::getEnv("INTERVAL");
490 if (!interval.empty())
Patrick Ventureab10f162017-05-22 09:44:50 -0700491 {
Patrick Venture50cf1c52018-04-18 09:21:41 -0700492 _interval = std::strtoull(interval.c_str(), NULL, 10);
Patrick Ventureab10f162017-05-22 09:44:50 -0700493 }
494 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600495}
Patrick Ventureab10f162017-05-22 09:44:50 -0700496
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600497void MainLoop::read()
498{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500499 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
500 // ensure the objects all exist?
501
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600502 // Iterate through all the sensors.
503 for (auto& i : state)
504 {
505 auto& attrs = std::get<0>(i.second);
506 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500507 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600508 // Read value from sensor.
509 int64_t value;
510 std::string input = hwmon::entry::cinput;
511 if (i.first.first == "pwm") {
512 input = "";
513 }
514
515 try
Brad Bishope55ef3d2016-12-19 09:12:40 -0500516 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600517 // Retry for up to a second if device is busy
518 // or has a transient error.
Patrick Venture9331ab72018-01-29 09:48:47 -0800519
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600520 value = ioAccess.read(
521 i.first.first,
522 i.first.second,
523 input,
Patrick Venture75e56c62018-04-20 18:10:15 -0700524 hwmonio::retries,
525 hwmonio::delay,
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600526 _isOCC);
527
528 value = adjustValue(i.first, value);
529
530 auto& objInfo = std::get<ObjectInfo>(i.second);
531 auto& obj = std::get<Object>(objInfo);
532
533 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500534 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600535 auto valueIface = std::shared_ptr<ValueObject>();
536 auto warnIface = std::shared_ptr<WarningObject>();
537 auto critIface = std::shared_ptr<CriticalObject>();
Brad Bishop754d38c2017-09-08 00:46:58 -0400538
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600539 switch (iface.first)
Brad Bishope0b7d052017-01-06 15:30:23 -0500540 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600541 case InterfaceType::VALUE:
542 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
543 (iface.second);
544 valueIface->value(value);
545 break;
546 case InterfaceType::WARN:
547 checkThresholds<WarningObject>(iface.second, value);
548 break;
549 case InterfaceType::CRIT:
550 checkThresholds<CriticalObject>(iface.second, value);
551 break;
552 default:
553 break;
Brad Bishope0b7d052017-01-06 15:30:23 -0500554 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500555 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600556 }
557 catch (const std::system_error& e)
558 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500559 auto file = sysfs::make_sysfs_path(
560 ioAccess.path(),
561 i.first.first,
562 i.first.second,
563 hwmon::entry::cinput);
Matthew Barth8772ce32018-03-22 16:03:06 -0500564#ifndef REMOVE_ON_FAIL
565 // Check sensorAdjusts for sensor removal RCs
566 const auto& it = sensorAdjusts.find(i.first);
567 if (it != sensorAdjusts.end())
568 {
569 auto rmRCit = it->second.rmRCs.find(e.code().value());
570 if (rmRCit != std::end(it->second.rmRCs))
571 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500572 // Return code found in sensor return code removal list
573 if (rmSensors.find(i.first) == rmSensors.end())
574 {
575 // Trace for sensor not already removed from dbus
576 log<level::INFO>(
577 "Remove sensor from dbus for read fail",
578 entry("FILE=%s", file.c_str()),
579 entry("RC=%d", e.code().value()));
580 // Mark this sensor to be removed from dbus
581 rmSensors[i.first] = std::get<0>(i.second);
582 }
Matthew Barth8772ce32018-03-22 16:03:06 -0500583 continue;
584 }
585 }
586#endif
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600587 using namespace sdbusplus::xyz::openbmc_project::
588 Sensor::Device::Error;
589 report<ReadFailure>(
590 xyz::openbmc_project::Sensor::Device::
591 ReadFailure::CALLOUT_ERRNO(e.code().value()),
592 xyz::openbmc_project::Sensor::Device::
593 ReadFailure::CALLOUT_DEVICE_PATH(
594 _devPath.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500595
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600596 log<level::INFO>("Logging failing sysfs file",
597 entry("FILE=%s", file.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500598
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500599#ifdef REMOVE_ON_FAIL
Matthew Barth33db92b2018-03-26 09:55:19 -0500600 rmSensors[i.first] = std::get<0>(i.second);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500601#else
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600602 exit(EXIT_FAILURE);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500603#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500604 }
605 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600606 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500607
Matthew Barth8772ce32018-03-22 16:03:06 -0500608 // Remove any sensors marked for removal
Matthew Barth33db92b2018-03-26 09:55:19 -0500609 for (auto& i : rmSensors)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600610 {
Matthew Barth33db92b2018-03-26 09:55:19 -0500611 state.erase(i.first);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500612 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500613
614#ifndef REMOVE_ON_FAIL
615 // Attempt to add any sensors that were removed
616 auto it = rmSensors.begin();
617 while (it != rmSensors.end())
618 {
619 if (state.find(it->first) == state.end())
620 {
621 SensorSet::container_t::value_type ssValueType =
622 std::make_pair(it->first, it->second);
Matthew Barthd238e232018-04-17 12:01:50 -0500623 auto object = getObject(ssValueType);
624 if (object)
Matthew Barth31d214c2018-03-26 09:54:27 -0500625 {
Matthew Barthd238e232018-04-17 12:01:50 -0500626 // Construct the SensorSet value
627 // std::tuple<SensorSet::mapped_type,
628 // std::string(Sensor Label),
629 // ObjectInfo>
630 auto value = std::make_tuple(std::move(ssValueType.second),
631 std::move((*object).first),
632 std::move((*object).second));
633
634 state[std::move(ssValueType.first)] = std::move(value);
635
Matthew Barth31d214c2018-03-26 09:54:27 -0500636 // Sensor object added, erase entry from removal list
Matthew Barth38c74e72018-04-02 12:41:26 -0500637 auto file = sysfs::make_sysfs_path(
638 ioAccess.path(),
639 it->first.first,
640 it->first.second,
641 hwmon::entry::cinput);
642 log<level::INFO>(
643 "Added sensor to dbus after successful read",
644 entry("FILE=%s", file.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500645 it = rmSensors.erase(it);
646 }
647 else
648 {
649 ++it;
650 }
651 }
652 else
653 {
654 // Sanity check to remove sensors that were re-added
655 it = rmSensors.erase(it);
656 }
657 }
658#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500659}
660
661// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4