blob: bfc010eafb0f69ba8f24366c1cad041012e2e74d [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 Barth0b305052018-04-26 09:46:49 -0500138 ObjectInfo& info)
Brad Bishope9fdee02017-01-06 10:43:29 -0500139{
Brad Bishop30dbcee2017-01-18 07:55:42 -0500140 static constexpr bool deferSignals = true;
141
Brad Bishope9fdee02017-01-06 10:43:29 -0500142 // Get the initial value for the value interface.
143 auto& bus = *std::get<sdbusplus::bus::bus*>(info);
144 auto& obj = std::get<Object>(info);
145 auto& objPath = std::get<std::string>(info);
146
Patrick Venture7a5285d2018-04-17 19:15:05 -0700147 auto senRmRCs = env::getEnv("REMOVERCS", sensor);
Matthew Barthb7985272018-04-17 10:50:36 -0500148 // Add sensor removal return codes defined per sensor
149 addRemoveRCs(sensor, senRmRCs);
Matthew Barthd26e2772018-03-22 14:24:06 -0500150
Patrick Venture7a5285d2018-04-17 19:15:05 -0700151 auto gain = env::getEnv("GAIN", sensor);
Chiabing Leec923ce92017-09-06 15:58:26 +0800152 if (!gain.empty())
153 {
154 sensorAdjusts[sensor].gain = std::stod(gain);
155 }
156
Patrick Venture7a5285d2018-04-17 19:15:05 -0700157 auto offset = env::getEnv("OFFSET", sensor);
Chiabing Leec923ce92017-09-06 15:58:26 +0800158 if (!offset.empty())
159 {
160 sensorAdjusts[sensor].offset = std::stoi(offset);
161 }
162
Matthew Barthca44c2e2018-04-24 15:33:25 -0500163 int64_t val = 0;
164 std::shared_ptr<StatusObject> statusIface = nullptr;
165 auto it = obj.find(InterfaceType::STATUS);
166 if (it != obj.end())
167 {
168 statusIface = std::experimental::any_cast<
169 std::shared_ptr<StatusObject>>(it->second);
170 }
171
172 // If there's no fault file or the sensor has a fault file and
173 // its status is functional, read the input value.
174 if (!statusIface || (statusIface && statusIface->functional()))
175 {
176 // Retry for up to a second if device is busy
177 // or has a transient error.
178 val = ioAccess.read(
179 sensor.first,
180 sensor.second,
181 hwmon::entry::cinput,
182 std::get<size_t>(retryIO),
Matthew Barth0b305052018-04-26 09:46:49 -0500183 std::get<std::chrono::milliseconds>(retryIO));
Matthew Barthca44c2e2018-04-24 15:33:25 -0500184 val = adjustValue(sensor, val);
185 }
Chiabing Leec923ce92017-09-06 15:58:26 +0800186
Brad Bishop30dbcee2017-01-18 07:55:42 -0500187 auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals);
Brad Bishope9fdee02017-01-06 10:43:29 -0500188 iface->value(val);
189
Patrick Venture09791852018-04-17 17:40:00 -0700190 hwmon::Attributes attrs;
191 if (hwmon::getAttributes(sensor.first, attrs))
Brad Bishope9fdee02017-01-06 10:43:29 -0500192 {
Patrick Venture09791852018-04-17 17:40:00 -0700193 iface->unit(hwmon::getUnit(attrs));
194 iface->scale(hwmon::getScale(attrs));
Brad Bishope9fdee02017-01-06 10:43:29 -0500195 }
196
Patrick Venture7a5285d2018-04-17 19:15:05 -0700197 auto maxValue = env::getEnv("MAXVALUE", sensor);
James Feist7bd5fcf2018-01-22 16:14:28 -0800198 if(!maxValue.empty())
199 {
200 iface->maxValue(std::stoll(maxValue));
201 }
Patrick Venture7a5285d2018-04-17 19:15:05 -0700202 auto minValue = env::getEnv("MINVALUE", sensor);
James Feist7bd5fcf2018-01-22 16:14:28 -0800203 if(!minValue.empty())
204 {
205 iface->minValue(std::stoll(minValue));
206 }
207
Brad Bishope9fdee02017-01-06 10:43:29 -0500208 obj[InterfaceType::VALUE] = iface;
209 return iface;
210}
211
Matthew Barth979c8062018-04-17 11:37:15 -0500212std::string MainLoop::getID(SensorSet::container_t::const_reference sensor)
Matthew Barth31d214c2018-03-26 09:54:27 -0500213{
Matthew Barth31d214c2018-03-26 09:54:27 -0500214 std::string id;
215
216 /*
217 * Check if the value of the MODE_<item><X> env variable for the sensor
218 * is "label", then read the sensor number from the <item><X>_label
219 * file. The name of the DBUS object would be the value of the env
220 * variable LABEL_<item><sensorNum>. If the MODE_<item><X> env variable
221 * doesn't exist, then the name of DBUS object is the value of the env
222 * variable LABEL_<item><X>.
223 */
Patrick Venture7a5285d2018-04-17 19:15:05 -0700224 auto mode = env::getEnv("MODE", sensor.first);
Matthew Barth31d214c2018-03-26 09:54:27 -0500225 if (!mode.compare(hwmon::entry::label))
226 {
Patrick Venture7a5285d2018-04-17 19:15:05 -0700227 id = env::getIndirectID(
Matthew Barth31d214c2018-03-26 09:54:27 -0500228 _hwmonRoot + '/' + _instance + '/', sensor.first);
229
230 if (id.empty())
231 {
Matthew Barth979c8062018-04-17 11:37:15 -0500232 return id;
Matthew Barth31d214c2018-03-26 09:54:27 -0500233 }
234 }
235
236 // Use the ID we looked up above if there was one,
237 // otherwise use the standard one.
238 id = (id.empty()) ? sensor.first.second : id;
239
Matthew Barth979c8062018-04-17 11:37:15 -0500240 return id;
241}
242
243SensorIdentifiers MainLoop::getIdentifiers(
244 SensorSet::container_t::const_reference sensor)
245{
246 std::string id = getID(sensor);
247 std::string label;
248
249 if (!id.empty())
250 {
251 // Ignore inputs without a label.
252 label = env::getEnv("LABEL", sensor.first.first, id);
253 }
254
255 return std::make_tuple(std::move(id),
256 std::move(label));
257}
258
259/**
260 * Reads the environment parameters of a sensor and creates an object with
261 * atleast the `Value` interface, otherwise returns without creating the object.
262 * If the `Value` interface is successfully created, by reading the sensor's
263 * corresponding sysfs file's value, the additional interfaces for the sensor
Matthew Barthd238e232018-04-17 12:01:50 -0500264 * are created and the InterfacesAdded signal is emitted. The object's state
265 * data is then returned for sensor state monitoring within the main loop.
Matthew Barth979c8062018-04-17 11:37:15 -0500266 */
Matthew Barthd238e232018-04-17 12:01:50 -0500267optional_ns::optional<ObjectStateData> MainLoop::getObject(
268 SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500269{
270 auto properties = getIdentifiers(sensor);
271 if (std::get<sensorID>(properties).empty() ||
272 std::get<sensorLabel>(properties).empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500273 {
Matthew Barthd238e232018-04-17 12:01:50 -0500274 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500275 }
276
Patrick Venture09791852018-04-17 17:40:00 -0700277 hwmon::Attributes attrs;
278 if (!hwmon::getAttributes(sensor.first.first, attrs))
Matthew Barth31d214c2018-03-26 09:54:27 -0500279 {
Matthew Barthd238e232018-04-17 12:01:50 -0500280 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500281 }
282
Matthew Barthb7985272018-04-17 10:50:36 -0500283 // Get list of return codes for removing sensors on device
284 auto devRmRCs = env::getEnv("REMOVERCS");
285 // Add sensor removal return codes defined at the device level
286 addRemoveRCs(sensor.first, devRmRCs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500287
288 std::string objectPath{_root};
289 objectPath.append(1, '/');
Patrick Venture09791852018-04-17 17:40:00 -0700290 objectPath.append(hwmon::getNamespace(attrs));
Matthew Barth31d214c2018-03-26 09:54:27 -0500291 objectPath.append(1, '/');
Matthew Barth979c8062018-04-17 11:37:15 -0500292 objectPath.append(std::get<sensorLabel>(properties));
Matthew Barth31d214c2018-03-26 09:54:27 -0500293
294 ObjectInfo info(&_bus, std::move(objectPath), Object());
Patrick Venture75e56c62018-04-20 18:10:15 -0700295 RetryIO retryIO(hwmonio::retries, hwmonio::delay);
Matthew Barthd4beecf2018-04-03 15:50:22 -0500296 if (rmSensors.find(sensor.first) != rmSensors.end())
297 {
298 // When adding a sensor that was purposely removed,
299 // don't retry on errors when reading its value
300 std::get<size_t>(retryIO) = 0;
301 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500302 auto valueInterface = static_cast<
303 std::shared_ptr<ValueObject>>(nullptr);
304 try
305 {
Matthew Barthca44c2e2018-04-24 15:33:25 -0500306 // Add status interface based on _fault file being present
307 sensor::addStatus(sensor.first, ioAccess, _devPath, info);
Matthew Barth0b305052018-04-26 09:46:49 -0500308 valueInterface = addValue(sensor.first, retryIO, ioAccess, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500309 }
310 catch (const std::system_error& e)
311 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500312 auto file = sysfs::make_sysfs_path(
313 ioAccess.path(),
314 sensor.first.first,
315 sensor.first.second,
316 hwmon::entry::cinput);
Matthew Barth31d214c2018-03-26 09:54:27 -0500317#ifndef REMOVE_ON_FAIL
318 // Check sensorAdjusts for sensor removal RCs
319 const auto& it = sensorAdjusts.find(sensor.first);
320 if (it != sensorAdjusts.end())
321 {
322 auto rmRCit = it->second.rmRCs.find(e.code().value());
323 if (rmRCit != std::end(it->second.rmRCs))
324 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500325 // Return code found in sensor return code removal list
326 if (rmSensors.find(sensor.first) == rmSensors.end())
327 {
328 // Trace for sensor not already removed from dbus
329 log<level::INFO>("Sensor not added to dbus for read fail",
330 entry("FILE=%s", file.c_str()),
331 entry("RC=%d", e.code().value()));
332 rmSensors[std::move(sensor.first)] =
333 std::move(sensor.second);
334 }
Matthew Barthd238e232018-04-17 12:01:50 -0500335 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500336 }
337 }
338#endif
339 using namespace sdbusplus::xyz::openbmc_project::
340 Sensor::Device::Error;
341 report<ReadFailure>(
342 xyz::openbmc_project::Sensor::Device::
343 ReadFailure::CALLOUT_ERRNO(e.code().value()),
344 xyz::openbmc_project::Sensor::Device::
345 ReadFailure::CALLOUT_DEVICE_PATH(_devPath.c_str()));
346
Matthew Barth31d214c2018-03-26 09:54:27 -0500347 log<level::INFO>("Logging failing sysfs file",
348 entry("FILE=%s", file.c_str()));
349#ifdef REMOVE_ON_FAIL
Matthew Barthd238e232018-04-17 12:01:50 -0500350 return {}; /* skip adding this sensor for now. */
Matthew Barth31d214c2018-03-26 09:54:27 -0500351#else
352 exit(EXIT_FAILURE);
353#endif
354 }
355 auto sensorValue = valueInterface->value();
Matthew Barth979c8062018-04-17 11:37:15 -0500356 addThreshold<WarningObject>(sensor.first.first,
357 std::get<sensorID>(properties),
358 sensorValue,
359 info);
360 addThreshold<CriticalObject>(sensor.first.first,
361 std::get<sensorID>(properties),
362 sensorValue,
363 info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500364
Matthew Barth28f8e662018-03-26 16:57:36 -0500365 auto target = addTarget<hwmon::FanSpeed>(
366 sensor.first, ioAccess, _devPath, info);
367 if (target)
Matthew Barth31d214c2018-03-26 09:54:27 -0500368 {
Matthew Barth28f8e662018-03-26 16:57:36 -0500369 target->enable();
Matthew Barth31d214c2018-03-26 09:54:27 -0500370 }
Matthew Barth28f8e662018-03-26 16:57:36 -0500371 addTarget<hwmon::FanPwm>(sensor.first, ioAccess, _devPath, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500372
373 // All the interfaces have been created. Go ahead
374 // and emit InterfacesAdded.
375 valueInterface->emit_object_added();
376
Matthew Barthd238e232018-04-17 12:01:50 -0500377 return std::make_pair(std::move(std::get<sensorLabel>(properties)),
378 std::move(info));
Matthew Barth31d214c2018-03-26 09:54:27 -0500379}
380
Brad Bishopb9e2b072016-12-19 13:47:10 -0500381MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500382 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500383 const std::string& path,
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400384 const std::string& devPath,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500385 const char* prefix,
386 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500387 : _bus(std::move(bus)),
Brad Bishop03e87352017-03-07 00:12:22 -0500388 _manager(_bus, root),
Brad Bishopb8740fc2017-02-24 23:38:37 -0500389 _hwmonRoot(),
390 _instance(),
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400391 _devPath(devPath),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500392 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500393 _root(root),
Brad Bishop751043e2017-08-29 11:13:46 -0400394 state(),
395 ioAccess(path)
Brad Bishopd499ca62016-12-19 09:24:50 -0500396{
Patrick Venture73a50c72018-04-17 15:19:03 -0700397 // Strip off any trailing slashes.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500398 std::string p = path;
399 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500400 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500401 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500402 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500403
Patrick Venture73a50c72018-04-17 15:19:03 -0700404 // Given the furthest right /, set instance to
405 // the basename, and hwmonRoot to the leading path.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500406 auto n = p.rfind('/');
407 if (n != std::string::npos)
408 {
409 _instance.assign(p.substr(n + 1));
410 _hwmonRoot.assign(p.substr(0, n));
411 }
412
413 assert(!_instance.empty());
414 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500415}
416
417void MainLoop::shutdown() noexcept
418{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600419 timer->state(phosphor::hwmon::timer::OFF);
420 sd_event_exit(loop, 0);
421 loop = nullptr;
Brad Bishopd499ca62016-12-19 09:24:50 -0500422}
423
424void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500425{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600426 init();
427
428 sd_event_default(&loop);
429
430 std::function<void()> callback(std::bind(
431 &MainLoop::read, this));
432 try
433 {
434 timer = std::make_unique<phosphor::hwmon::Timer>(
435 loop, callback,
436 std::chrono::microseconds(_interval),
437 phosphor::hwmon::timer::ON);
438
439 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
440
441 // TODO: Issue#7 - Should probably periodically check the SensorSet
442 // for new entries.
443
444 _bus.attach_event(loop, SD_EVENT_PRIORITY_IMPORTANT);
445 sd_event_loop(loop);
446 }
447 catch (const std::system_error& e)
448 {
449 log<level::ERR>("Error in sysfs polling loop",
450 entry("ERROR=%s", e.what()));
451 throw;
452 }
453}
454
455void MainLoop::init()
456{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500457 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500458 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500459
Brad Bishop75b4ab82017-01-06 09:33:50 -0500460 for (auto& i : *sensors)
461 {
Matthew Barthd238e232018-04-17 12:01:50 -0500462 auto object = getObject(i);
463 if (object)
464 {
465 // Construct the SensorSet value
466 // std::tuple<SensorSet::mapped_type,
467 // std::string(Sensor Label),
468 // ObjectInfo>
469 auto value = std::make_tuple(std::move(i.second),
470 std::move((*object).first),
471 std::move((*object).second));
472
473 state[std::move(i.first)] = std::move(value);
474 }
Brad Bishop75b4ab82017-01-06 09:33:50 -0500475 }
476
Patrick Venture62503a42017-05-23 07:30:29 -0700477 /* If there are no sensors specified by labels, exit. */
478 if (0 == state.size())
479 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600480 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700481 }
482
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500483 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500484 std::string busname{_prefix};
Brad Bishop4d9a35b2017-11-14 22:33:03 -0500485 busname.append(1, '-');
486 busname.append(
487 std::to_string(std::hash<decltype(_devPath)>{}(_devPath)));
488 busname.append(".Hwmon1");
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500489 _bus.request_name(busname.c_str());
490 }
491
Patrick Ventureab10f162017-05-22 09:44:50 -0700492 {
Patrick Venturea24c8802018-04-17 19:38:06 -0700493 auto interval = env::getEnv("INTERVAL");
494 if (!interval.empty())
Patrick Ventureab10f162017-05-22 09:44:50 -0700495 {
Patrick Venture50cf1c52018-04-18 09:21:41 -0700496 _interval = std::strtoull(interval.c_str(), NULL, 10);
Patrick Ventureab10f162017-05-22 09:44:50 -0700497 }
498 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600499}
Patrick Ventureab10f162017-05-22 09:44:50 -0700500
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600501void MainLoop::read()
502{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500503 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
504 // ensure the objects all exist?
505
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600506 // Iterate through all the sensors.
507 for (auto& i : state)
508 {
509 auto& attrs = std::get<0>(i.second);
510 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500511 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600512 // Read value from sensor.
513 int64_t value;
514 std::string input = hwmon::entry::cinput;
515 if (i.first.first == "pwm") {
516 input = "";
517 }
518
519 try
Brad Bishope55ef3d2016-12-19 09:12:40 -0500520 {
Matthew Barth27c4a392018-04-25 14:38:51 -0500521 auto& objInfo = std::get<ObjectInfo>(i.second);
522 auto& obj = std::get<Object>(objInfo);
523
524 auto it = obj.find(InterfaceType::STATUS);
525 if (it != obj.end())
526 {
Matthew Barthbfcaf3d2018-04-25 15:05:58 -0500527 auto fault = ioAccess.read(
528 i.first.first,
529 i.first.second,
530 hwmon::entry::fault,
531 hwmonio::retries,
532 hwmonio::delay);
Matthew Barth27c4a392018-04-25 14:38:51 -0500533 auto statusIface = std::experimental::any_cast<
534 std::shared_ptr<StatusObject>>(it->second);
Matthew Barthbfcaf3d2018-04-25 15:05:58 -0500535 if (!statusIface->functional((fault == 0) ? true : false))
Matthew Barth27c4a392018-04-25 14:38:51 -0500536 {
537 continue;
538 }
539 }
540
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600541 // Retry for up to a second if device is busy
542 // or has a transient error.
Patrick Venture9331ab72018-01-29 09:48:47 -0800543
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600544 value = ioAccess.read(
545 i.first.first,
546 i.first.second,
547 input,
Patrick Venture75e56c62018-04-20 18:10:15 -0700548 hwmonio::retries,
Matthew Barth0b305052018-04-26 09:46:49 -0500549 hwmonio::delay);
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600550
551 value = adjustValue(i.first, value);
552
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600553 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500554 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600555 auto valueIface = std::shared_ptr<ValueObject>();
556 auto warnIface = std::shared_ptr<WarningObject>();
557 auto critIface = std::shared_ptr<CriticalObject>();
Brad Bishop754d38c2017-09-08 00:46:58 -0400558
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600559 switch (iface.first)
Brad Bishope0b7d052017-01-06 15:30:23 -0500560 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600561 case InterfaceType::VALUE:
562 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
563 (iface.second);
564 valueIface->value(value);
565 break;
566 case InterfaceType::WARN:
567 checkThresholds<WarningObject>(iface.second, value);
568 break;
569 case InterfaceType::CRIT:
570 checkThresholds<CriticalObject>(iface.second, value);
571 break;
572 default:
573 break;
Brad Bishope0b7d052017-01-06 15:30:23 -0500574 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500575 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600576 }
577 catch (const std::system_error& e)
578 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500579 auto file = sysfs::make_sysfs_path(
580 ioAccess.path(),
581 i.first.first,
582 i.first.second,
583 hwmon::entry::cinput);
Matthew Barth8772ce32018-03-22 16:03:06 -0500584#ifndef REMOVE_ON_FAIL
585 // Check sensorAdjusts for sensor removal RCs
586 const auto& it = sensorAdjusts.find(i.first);
587 if (it != sensorAdjusts.end())
588 {
589 auto rmRCit = it->second.rmRCs.find(e.code().value());
590 if (rmRCit != std::end(it->second.rmRCs))
591 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500592 // Return code found in sensor return code removal list
593 if (rmSensors.find(i.first) == rmSensors.end())
594 {
595 // Trace for sensor not already removed from dbus
596 log<level::INFO>(
597 "Remove sensor from dbus for read fail",
598 entry("FILE=%s", file.c_str()),
599 entry("RC=%d", e.code().value()));
600 // Mark this sensor to be removed from dbus
601 rmSensors[i.first] = std::get<0>(i.second);
602 }
Matthew Barth8772ce32018-03-22 16:03:06 -0500603 continue;
604 }
605 }
606#endif
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600607 using namespace sdbusplus::xyz::openbmc_project::
608 Sensor::Device::Error;
609 report<ReadFailure>(
610 xyz::openbmc_project::Sensor::Device::
611 ReadFailure::CALLOUT_ERRNO(e.code().value()),
612 xyz::openbmc_project::Sensor::Device::
613 ReadFailure::CALLOUT_DEVICE_PATH(
614 _devPath.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500615
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600616 log<level::INFO>("Logging failing sysfs file",
617 entry("FILE=%s", file.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500618
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500619#ifdef REMOVE_ON_FAIL
Matthew Barth33db92b2018-03-26 09:55:19 -0500620 rmSensors[i.first] = std::get<0>(i.second);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500621#else
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600622 exit(EXIT_FAILURE);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500623#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500624 }
625 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600626 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500627
Matthew Barth8772ce32018-03-22 16:03:06 -0500628 // Remove any sensors marked for removal
Matthew Barth33db92b2018-03-26 09:55:19 -0500629 for (auto& i : rmSensors)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600630 {
Matthew Barth33db92b2018-03-26 09:55:19 -0500631 state.erase(i.first);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500632 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500633
634#ifndef REMOVE_ON_FAIL
635 // Attempt to add any sensors that were removed
636 auto it = rmSensors.begin();
637 while (it != rmSensors.end())
638 {
639 if (state.find(it->first) == state.end())
640 {
641 SensorSet::container_t::value_type ssValueType =
642 std::make_pair(it->first, it->second);
Matthew Barthd238e232018-04-17 12:01:50 -0500643 auto object = getObject(ssValueType);
644 if (object)
Matthew Barth31d214c2018-03-26 09:54:27 -0500645 {
Matthew Barthd238e232018-04-17 12:01:50 -0500646 // Construct the SensorSet value
647 // std::tuple<SensorSet::mapped_type,
648 // std::string(Sensor Label),
649 // ObjectInfo>
650 auto value = std::make_tuple(std::move(ssValueType.second),
651 std::move((*object).first),
652 std::move((*object).second));
653
654 state[std::move(ssValueType.first)] = std::move(value);
655
Matthew Barth31d214c2018-03-26 09:54:27 -0500656 // Sensor object added, erase entry from removal list
Matthew Barth38c74e72018-04-02 12:41:26 -0500657 auto file = sysfs::make_sysfs_path(
658 ioAccess.path(),
659 it->first.first,
660 it->first.second,
661 hwmon::entry::cinput);
662 log<level::INFO>(
663 "Added sensor to dbus after successful read",
664 entry("FILE=%s", file.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500665 it = rmSensors.erase(it);
666 }
667 else
668 {
669 ++it;
670 }
671 }
672 else
673 {
674 // Sanity check to remove sensors that were re-added
675 it = rmSensors.erase(it);
676 }
677 }
678#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500679}
680
681// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4