blob: 9944e094ca312739ac44ac480abe13404b66a182 [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
Patrick Venture7a5285d2018-04-17 19:15:05 -0700152 auto gain = env::getEnv("GAIN", sensor);
Chiabing Leec923ce92017-09-06 15:58:26 +0800153 if (!gain.empty())
154 {
155 sensorAdjusts[sensor].gain = std::stod(gain);
156 }
157
Patrick Venture7a5285d2018-04-17 19:15:05 -0700158 auto offset = env::getEnv("OFFSET", sensor);
Chiabing Leec923ce92017-09-06 15:58:26 +0800159 if (!offset.empty())
160 {
161 sensorAdjusts[sensor].offset = std::stoi(offset);
162 }
163
Matthew Barthca44c2e2018-04-24 15:33:25 -0500164 int64_t val = 0;
165 std::shared_ptr<StatusObject> statusIface = nullptr;
166 auto it = obj.find(InterfaceType::STATUS);
167 if (it != obj.end())
168 {
169 statusIface = std::experimental::any_cast<
170 std::shared_ptr<StatusObject>>(it->second);
171 }
172
173 // If there's no fault file or the sensor has a fault file and
174 // its status is functional, read the input value.
175 if (!statusIface || (statusIface && statusIface->functional()))
176 {
177 // Retry for up to a second if device is busy
178 // or has a transient error.
179 val = ioAccess.read(
180 sensor.first,
181 sensor.second,
182 hwmon::entry::cinput,
183 std::get<size_t>(retryIO),
184 std::get<std::chrono::milliseconds>(retryIO),
185 isOCC);
186 val = adjustValue(sensor, val);
187 }
Chiabing Leec923ce92017-09-06 15:58:26 +0800188
Brad Bishop30dbcee2017-01-18 07:55:42 -0500189 auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals);
Brad Bishope9fdee02017-01-06 10:43:29 -0500190 iface->value(val);
191
Patrick Venture09791852018-04-17 17:40:00 -0700192 hwmon::Attributes attrs;
193 if (hwmon::getAttributes(sensor.first, attrs))
Brad Bishope9fdee02017-01-06 10:43:29 -0500194 {
Patrick Venture09791852018-04-17 17:40:00 -0700195 iface->unit(hwmon::getUnit(attrs));
196 iface->scale(hwmon::getScale(attrs));
Brad Bishope9fdee02017-01-06 10:43:29 -0500197 }
198
Patrick Venture7a5285d2018-04-17 19:15:05 -0700199 auto maxValue = env::getEnv("MAXVALUE", sensor);
James Feist7bd5fcf2018-01-22 16:14:28 -0800200 if(!maxValue.empty())
201 {
202 iface->maxValue(std::stoll(maxValue));
203 }
Patrick Venture7a5285d2018-04-17 19:15:05 -0700204 auto minValue = env::getEnv("MINVALUE", sensor);
James Feist7bd5fcf2018-01-22 16:14:28 -0800205 if(!minValue.empty())
206 {
207 iface->minValue(std::stoll(minValue));
208 }
209
Brad Bishope9fdee02017-01-06 10:43:29 -0500210 obj[InterfaceType::VALUE] = iface;
211 return iface;
212}
213
Matthew Barth979c8062018-04-17 11:37:15 -0500214std::string MainLoop::getID(SensorSet::container_t::const_reference sensor)
Matthew Barth31d214c2018-03-26 09:54:27 -0500215{
Matthew Barth31d214c2018-03-26 09:54:27 -0500216 std::string id;
217
218 /*
219 * Check if the value of the MODE_<item><X> env variable for the sensor
220 * is "label", then read the sensor number from the <item><X>_label
221 * file. The name of the DBUS object would be the value of the env
222 * variable LABEL_<item><sensorNum>. If the MODE_<item><X> env variable
223 * doesn't exist, then the name of DBUS object is the value of the env
224 * variable LABEL_<item><X>.
225 */
Patrick Venture7a5285d2018-04-17 19:15:05 -0700226 auto mode = env::getEnv("MODE", sensor.first);
Matthew Barth31d214c2018-03-26 09:54:27 -0500227 if (!mode.compare(hwmon::entry::label))
228 {
Patrick Venture7a5285d2018-04-17 19:15:05 -0700229 id = env::getIndirectID(
Matthew Barth31d214c2018-03-26 09:54:27 -0500230 _hwmonRoot + '/' + _instance + '/', sensor.first);
231
232 if (id.empty())
233 {
Matthew Barth979c8062018-04-17 11:37:15 -0500234 return id;
Matthew Barth31d214c2018-03-26 09:54:27 -0500235 }
236 }
237
238 // Use the ID we looked up above if there was one,
239 // otherwise use the standard one.
240 id = (id.empty()) ? sensor.first.second : id;
241
Matthew Barth979c8062018-04-17 11:37:15 -0500242 return id;
243}
244
245SensorIdentifiers MainLoop::getIdentifiers(
246 SensorSet::container_t::const_reference sensor)
247{
248 std::string id = getID(sensor);
249 std::string label;
250
251 if (!id.empty())
252 {
253 // Ignore inputs without a label.
254 label = env::getEnv("LABEL", sensor.first.first, id);
255 }
256
257 return std::make_tuple(std::move(id),
258 std::move(label));
259}
260
261/**
262 * Reads the environment parameters of a sensor and creates an object with
263 * atleast the `Value` interface, otherwise returns without creating the object.
264 * If the `Value` interface is successfully created, by reading the sensor's
265 * corresponding sysfs file's value, the additional interfaces for the sensor
Matthew Barthd238e232018-04-17 12:01:50 -0500266 * are created and the InterfacesAdded signal is emitted. The object's state
267 * data is then returned for sensor state monitoring within the main loop.
Matthew Barth979c8062018-04-17 11:37:15 -0500268 */
Matthew Barthd238e232018-04-17 12:01:50 -0500269optional_ns::optional<ObjectStateData> MainLoop::getObject(
270 SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500271{
272 auto properties = getIdentifiers(sensor);
273 if (std::get<sensorID>(properties).empty() ||
274 std::get<sensorLabel>(properties).empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500275 {
Matthew Barthd238e232018-04-17 12:01:50 -0500276 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500277 }
278
Patrick Venture09791852018-04-17 17:40:00 -0700279 hwmon::Attributes attrs;
280 if (!hwmon::getAttributes(sensor.first.first, attrs))
Matthew Barth31d214c2018-03-26 09:54:27 -0500281 {
Matthew Barthd238e232018-04-17 12:01:50 -0500282 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500283 }
284
Matthew Barthb7985272018-04-17 10:50:36 -0500285 // Get list of return codes for removing sensors on device
286 auto devRmRCs = env::getEnv("REMOVERCS");
287 // Add sensor removal return codes defined at the device level
288 addRemoveRCs(sensor.first, devRmRCs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500289
290 std::string objectPath{_root};
291 objectPath.append(1, '/');
Patrick Venture09791852018-04-17 17:40:00 -0700292 objectPath.append(hwmon::getNamespace(attrs));
Matthew Barth31d214c2018-03-26 09:54:27 -0500293 objectPath.append(1, '/');
Matthew Barth979c8062018-04-17 11:37:15 -0500294 objectPath.append(std::get<sensorLabel>(properties));
Matthew Barth31d214c2018-03-26 09:54:27 -0500295
296 ObjectInfo info(&_bus, std::move(objectPath), Object());
Patrick Venture75e56c62018-04-20 18:10:15 -0700297 RetryIO retryIO(hwmonio::retries, hwmonio::delay);
Matthew Barthd4beecf2018-04-03 15:50:22 -0500298 if (rmSensors.find(sensor.first) != rmSensors.end())
299 {
300 // When adding a sensor that was purposely removed,
301 // don't retry on errors when reading its value
302 std::get<size_t>(retryIO) = 0;
303 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500304 auto valueInterface = static_cast<
305 std::shared_ptr<ValueObject>>(nullptr);
306 try
307 {
Matthew Barthca44c2e2018-04-24 15:33:25 -0500308 // Add status interface based on _fault file being present
309 sensor::addStatus(sensor.first, ioAccess, _devPath, info);
Matthew Barthd4beecf2018-04-03 15:50:22 -0500310 valueInterface = addValue(sensor.first, retryIO, ioAccess, info,
Matthew Barth31d214c2018-03-26 09:54:27 -0500311 _isOCC);
312 }
313 catch (const std::system_error& e)
314 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500315 auto file = sysfs::make_sysfs_path(
316 ioAccess.path(),
317 sensor.first.first,
318 sensor.first.second,
319 hwmon::entry::cinput);
Matthew Barth31d214c2018-03-26 09:54:27 -0500320#ifndef REMOVE_ON_FAIL
321 // Check sensorAdjusts for sensor removal RCs
322 const auto& it = sensorAdjusts.find(sensor.first);
323 if (it != sensorAdjusts.end())
324 {
325 auto rmRCit = it->second.rmRCs.find(e.code().value());
326 if (rmRCit != std::end(it->second.rmRCs))
327 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500328 // Return code found in sensor return code removal list
329 if (rmSensors.find(sensor.first) == rmSensors.end())
330 {
331 // Trace for sensor not already removed from dbus
332 log<level::INFO>("Sensor not added to dbus for read fail",
333 entry("FILE=%s", file.c_str()),
334 entry("RC=%d", e.code().value()));
335 rmSensors[std::move(sensor.first)] =
336 std::move(sensor.second);
337 }
Matthew Barthd238e232018-04-17 12:01:50 -0500338 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500339 }
340 }
341#endif
342 using namespace sdbusplus::xyz::openbmc_project::
343 Sensor::Device::Error;
344 report<ReadFailure>(
345 xyz::openbmc_project::Sensor::Device::
346 ReadFailure::CALLOUT_ERRNO(e.code().value()),
347 xyz::openbmc_project::Sensor::Device::
348 ReadFailure::CALLOUT_DEVICE_PATH(_devPath.c_str()));
349
Matthew Barth31d214c2018-03-26 09:54:27 -0500350 log<level::INFO>("Logging failing sysfs file",
351 entry("FILE=%s", file.c_str()));
352#ifdef REMOVE_ON_FAIL
Matthew Barthd238e232018-04-17 12:01:50 -0500353 return {}; /* skip adding this sensor for now. */
Matthew Barth31d214c2018-03-26 09:54:27 -0500354#else
355 exit(EXIT_FAILURE);
356#endif
357 }
358 auto sensorValue = valueInterface->value();
Matthew Barth979c8062018-04-17 11:37:15 -0500359 addThreshold<WarningObject>(sensor.first.first,
360 std::get<sensorID>(properties),
361 sensorValue,
362 info);
363 addThreshold<CriticalObject>(sensor.first.first,
364 std::get<sensorID>(properties),
365 sensorValue,
366 info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500367
Matthew Barth28f8e662018-03-26 16:57:36 -0500368 auto target = addTarget<hwmon::FanSpeed>(
369 sensor.first, ioAccess, _devPath, info);
370 if (target)
Matthew Barth31d214c2018-03-26 09:54:27 -0500371 {
Matthew Barth28f8e662018-03-26 16:57:36 -0500372 target->enable();
Matthew Barth31d214c2018-03-26 09:54:27 -0500373 }
Matthew Barth28f8e662018-03-26 16:57:36 -0500374 addTarget<hwmon::FanPwm>(sensor.first, ioAccess, _devPath, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500375
376 // All the interfaces have been created. Go ahead
377 // and emit InterfacesAdded.
378 valueInterface->emit_object_added();
379
Matthew Barthd238e232018-04-17 12:01:50 -0500380 return std::make_pair(std::move(std::get<sensorLabel>(properties)),
381 std::move(info));
Matthew Barth31d214c2018-03-26 09:54:27 -0500382}
383
Brad Bishopb9e2b072016-12-19 13:47:10 -0500384MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500385 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500386 const std::string& path,
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400387 const std::string& devPath,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500388 const char* prefix,
389 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500390 : _bus(std::move(bus)),
Brad Bishop03e87352017-03-07 00:12:22 -0500391 _manager(_bus, root),
Brad Bishopb8740fc2017-02-24 23:38:37 -0500392 _hwmonRoot(),
393 _instance(),
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400394 _devPath(devPath),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500395 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500396 _root(root),
Brad Bishop751043e2017-08-29 11:13:46 -0400397 state(),
398 ioAccess(path)
Brad Bishopd499ca62016-12-19 09:24:50 -0500399{
Matthew Bartha23babd2018-03-16 10:03:27 -0500400 if (path.find("occ") != std::string::npos)
401 {
402 _isOCC = true;
403 }
404
Patrick Venture73a50c72018-04-17 15:19:03 -0700405 // Strip off any trailing slashes.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500406 std::string p = path;
407 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500408 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500409 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500410 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500411
Patrick Venture73a50c72018-04-17 15:19:03 -0700412 // Given the furthest right /, set instance to
413 // the basename, and hwmonRoot to the leading path.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500414 auto n = p.rfind('/');
415 if (n != std::string::npos)
416 {
417 _instance.assign(p.substr(n + 1));
418 _hwmonRoot.assign(p.substr(0, n));
419 }
420
421 assert(!_instance.empty());
422 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500423}
424
425void MainLoop::shutdown() noexcept
426{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600427 timer->state(phosphor::hwmon::timer::OFF);
428 sd_event_exit(loop, 0);
429 loop = nullptr;
Brad Bishopd499ca62016-12-19 09:24:50 -0500430}
431
432void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500433{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600434 init();
435
436 sd_event_default(&loop);
437
438 std::function<void()> callback(std::bind(
439 &MainLoop::read, this));
440 try
441 {
442 timer = std::make_unique<phosphor::hwmon::Timer>(
443 loop, callback,
444 std::chrono::microseconds(_interval),
445 phosphor::hwmon::timer::ON);
446
447 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
448
449 // TODO: Issue#7 - Should probably periodically check the SensorSet
450 // for new entries.
451
452 _bus.attach_event(loop, SD_EVENT_PRIORITY_IMPORTANT);
453 sd_event_loop(loop);
454 }
455 catch (const std::system_error& e)
456 {
457 log<level::ERR>("Error in sysfs polling loop",
458 entry("ERROR=%s", e.what()));
459 throw;
460 }
461}
462
463void MainLoop::init()
464{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500465 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500466 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500467
Brad Bishop75b4ab82017-01-06 09:33:50 -0500468 for (auto& i : *sensors)
469 {
Matthew Barthd238e232018-04-17 12:01:50 -0500470 auto object = getObject(i);
471 if (object)
472 {
473 // Construct the SensorSet value
474 // std::tuple<SensorSet::mapped_type,
475 // std::string(Sensor Label),
476 // ObjectInfo>
477 auto value = std::make_tuple(std::move(i.second),
478 std::move((*object).first),
479 std::move((*object).second));
480
481 state[std::move(i.first)] = std::move(value);
482 }
Brad Bishop75b4ab82017-01-06 09:33:50 -0500483 }
484
Patrick Venture62503a42017-05-23 07:30:29 -0700485 /* If there are no sensors specified by labels, exit. */
486 if (0 == state.size())
487 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600488 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700489 }
490
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500491 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500492 std::string busname{_prefix};
Brad Bishop4d9a35b2017-11-14 22:33:03 -0500493 busname.append(1, '-');
494 busname.append(
495 std::to_string(std::hash<decltype(_devPath)>{}(_devPath)));
496 busname.append(".Hwmon1");
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500497 _bus.request_name(busname.c_str());
498 }
499
Patrick Ventureab10f162017-05-22 09:44:50 -0700500 {
Patrick Venturea24c8802018-04-17 19:38:06 -0700501 auto interval = env::getEnv("INTERVAL");
502 if (!interval.empty())
Patrick Ventureab10f162017-05-22 09:44:50 -0700503 {
Patrick Venture50cf1c52018-04-18 09:21:41 -0700504 _interval = std::strtoull(interval.c_str(), NULL, 10);
Patrick Ventureab10f162017-05-22 09:44:50 -0700505 }
506 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600507}
Patrick Ventureab10f162017-05-22 09:44:50 -0700508
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600509void MainLoop::read()
510{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500511 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
512 // ensure the objects all exist?
513
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600514 // Iterate through all the sensors.
515 for (auto& i : state)
516 {
517 auto& attrs = std::get<0>(i.second);
518 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500519 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600520 // Read value from sensor.
521 int64_t value;
522 std::string input = hwmon::entry::cinput;
523 if (i.first.first == "pwm") {
524 input = "";
525 }
526
527 try
Brad Bishope55ef3d2016-12-19 09:12:40 -0500528 {
Matthew Barth27c4a392018-04-25 14:38:51 -0500529 auto& objInfo = std::get<ObjectInfo>(i.second);
530 auto& obj = std::get<Object>(objInfo);
531
532 auto it = obj.find(InterfaceType::STATUS);
533 if (it != obj.end())
534 {
535 auto statusIface = std::experimental::any_cast<
536 std::shared_ptr<StatusObject>>(it->second);
537 if (!statusIface->functional())
538 {
539 continue;
540 }
541 }
542
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600543 // Retry for up to a second if device is busy
544 // or has a transient error.
Patrick Venture9331ab72018-01-29 09:48:47 -0800545
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600546 value = ioAccess.read(
547 i.first.first,
548 i.first.second,
549 input,
Patrick Venture75e56c62018-04-20 18:10:15 -0700550 hwmonio::retries,
551 hwmonio::delay,
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600552 _isOCC);
553
554 value = adjustValue(i.first, value);
555
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600556 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500557 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600558 auto valueIface = std::shared_ptr<ValueObject>();
559 auto warnIface = std::shared_ptr<WarningObject>();
560 auto critIface = std::shared_ptr<CriticalObject>();
Brad Bishop754d38c2017-09-08 00:46:58 -0400561
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600562 switch (iface.first)
Brad Bishope0b7d052017-01-06 15:30:23 -0500563 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600564 case InterfaceType::VALUE:
565 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
566 (iface.second);
567 valueIface->value(value);
568 break;
569 case InterfaceType::WARN:
570 checkThresholds<WarningObject>(iface.second, value);
571 break;
572 case InterfaceType::CRIT:
573 checkThresholds<CriticalObject>(iface.second, value);
574 break;
575 default:
576 break;
Brad Bishope0b7d052017-01-06 15:30:23 -0500577 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500578 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600579 }
580 catch (const std::system_error& e)
581 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500582 auto file = sysfs::make_sysfs_path(
583 ioAccess.path(),
584 i.first.first,
585 i.first.second,
586 hwmon::entry::cinput);
Matthew Barth8772ce32018-03-22 16:03:06 -0500587#ifndef REMOVE_ON_FAIL
588 // Check sensorAdjusts for sensor removal RCs
589 const auto& it = sensorAdjusts.find(i.first);
590 if (it != sensorAdjusts.end())
591 {
592 auto rmRCit = it->second.rmRCs.find(e.code().value());
593 if (rmRCit != std::end(it->second.rmRCs))
594 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500595 // Return code found in sensor return code removal list
596 if (rmSensors.find(i.first) == rmSensors.end())
597 {
598 // Trace for sensor not already removed from dbus
599 log<level::INFO>(
600 "Remove sensor from dbus for read fail",
601 entry("FILE=%s", file.c_str()),
602 entry("RC=%d", e.code().value()));
603 // Mark this sensor to be removed from dbus
604 rmSensors[i.first] = std::get<0>(i.second);
605 }
Matthew Barth8772ce32018-03-22 16:03:06 -0500606 continue;
607 }
608 }
609#endif
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600610 using namespace sdbusplus::xyz::openbmc_project::
611 Sensor::Device::Error;
612 report<ReadFailure>(
613 xyz::openbmc_project::Sensor::Device::
614 ReadFailure::CALLOUT_ERRNO(e.code().value()),
615 xyz::openbmc_project::Sensor::Device::
616 ReadFailure::CALLOUT_DEVICE_PATH(
617 _devPath.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500618
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600619 log<level::INFO>("Logging failing sysfs file",
620 entry("FILE=%s", file.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500621
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500622#ifdef REMOVE_ON_FAIL
Matthew Barth33db92b2018-03-26 09:55:19 -0500623 rmSensors[i.first] = std::get<0>(i.second);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500624#else
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600625 exit(EXIT_FAILURE);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500626#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500627 }
628 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600629 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500630
Matthew Barth8772ce32018-03-22 16:03:06 -0500631 // Remove any sensors marked for removal
Matthew Barth33db92b2018-03-26 09:55:19 -0500632 for (auto& i : rmSensors)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600633 {
Matthew Barth33db92b2018-03-26 09:55:19 -0500634 state.erase(i.first);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500635 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500636
637#ifndef REMOVE_ON_FAIL
638 // Attempt to add any sensors that were removed
639 auto it = rmSensors.begin();
640 while (it != rmSensors.end())
641 {
642 if (state.find(it->first) == state.end())
643 {
644 SensorSet::container_t::value_type ssValueType =
645 std::make_pair(it->first, it->second);
Matthew Barthd238e232018-04-17 12:01:50 -0500646 auto object = getObject(ssValueType);
647 if (object)
Matthew Barth31d214c2018-03-26 09:54:27 -0500648 {
Matthew Barthd238e232018-04-17 12:01:50 -0500649 // Construct the SensorSet value
650 // std::tuple<SensorSet::mapped_type,
651 // std::string(Sensor Label),
652 // ObjectInfo>
653 auto value = std::make_tuple(std::move(ssValueType.second),
654 std::move((*object).first),
655 std::move((*object).second));
656
657 state[std::move(ssValueType.first)] = std::move(value);
658
Matthew Barth31d214c2018-03-26 09:54:27 -0500659 // Sensor object added, erase entry from removal list
Matthew Barth38c74e72018-04-02 12:41:26 -0500660 auto file = sysfs::make_sysfs_path(
661 ioAccess.path(),
662 it->first.first,
663 it->first.second,
664 hwmon::entry::cinput);
665 log<level::INFO>(
666 "Added sensor to dbus after successful read",
667 entry("FILE=%s", file.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500668 it = rmSensors.erase(it);
669 }
670 else
671 {
672 ++it;
673 }
674 }
675 else
676 {
677 // Sanity check to remove sensors that were re-added
678 it = rmSensors.erase(it);
679 }
680 }
681#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500682}
683
684// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4