blob: 221c8c996cec6cddf32a5ffe2024678cf3ad4e0e [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 Venturec897d8b2018-04-23 19:01:56 -070023#include <sstream>
Patrick Venture1e6324f2017-06-01 14:07:05 -070024
25#include <phosphor-logging/elog-errors.hpp>
Matt Spinlerf9c83c42017-08-10 08:51:45 -050026#include "config.h"
Patrick Venture09791852018-04-17 17:40:00 -070027#include "env.hpp"
28#include "fan_pwm.hpp"
29#include "fan_speed.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050030#include "hwmon.hpp"
Patrick Venture75e56c62018-04-20 18:10:15 -070031#include "hwmonio.hpp"
Patrick Venture09791852018-04-17 17:40:00 -070032#include "sensorset.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050033#include "sysfs.hpp"
Brad Bishopd499ca62016-12-19 09:24:50 -050034#include "mainloop.hpp"
Matthew Barthbf7b7b12017-03-07 15:46:59 -060035#include "targets.hpp"
Patrick Venture09791852018-04-17 17:40:00 -070036#include "thresholds.hpp"
Matthew Barth35819382018-04-18 14:53:01 -050037#include "sensor.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050038
Patrick Venture1e6324f2017-06-01 14:07:05 -070039#include <xyz/openbmc_project/Sensor/Device/error.hpp>
40
41using namespace phosphor::logging;
42
Saqib Khan973886d2017-03-15 14:01:16 -050043// Initialization for Warning Objects
44decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo =
45 &WarningObject::warningLow;
46decltype(Thresholds<WarningObject>::setHi) Thresholds<WarningObject>::setHi =
47 &WarningObject::warningHigh;
48decltype(Thresholds<WarningObject>::getLo) Thresholds<WarningObject>::getLo =
49 &WarningObject::warningLow;
50decltype(Thresholds<WarningObject>::getHi) Thresholds<WarningObject>::getHi =
51 &WarningObject::warningHigh;
52decltype(Thresholds<WarningObject>::alarmLo) Thresholds<WarningObject>::alarmLo =
53 &WarningObject::warningAlarmLow;
54decltype(Thresholds<WarningObject>::alarmHi) Thresholds<WarningObject>::alarmHi =
55 &WarningObject::warningAlarmHigh;
56
57// Initialization for Critical Objects
58decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo =
59 &CriticalObject::criticalLow;
60decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi =
61 &CriticalObject::criticalHigh;
62decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo =
63 &CriticalObject::criticalLow;
64decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi =
65 &CriticalObject::criticalHigh;
66decltype(Thresholds<CriticalObject>::alarmLo) Thresholds<CriticalObject>::alarmLo =
67 &CriticalObject::criticalAlarmLow;
68decltype(Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi =
69 &CriticalObject::criticalAlarmHigh;
70
Chiabing Leec923ce92017-09-06 15:58:26 +080071// The gain and offset to adjust a value
72struct valueAdjust
73{
74 double gain = 1.0;
75 int offset = 0;
Matthew Barthd26e2772018-03-22 14:24:06 -050076 std::unordered_set<int> rmRCs;
Chiabing Leec923ce92017-09-06 15:58:26 +080077};
78
79// Store the valueAdjust for sensors
80std::map<SensorSet::key_type, valueAdjust> sensorAdjusts;
81
Matthew Barthd26e2772018-03-22 14:24:06 -050082void addRemoveRCs(const SensorSet::key_type& sensor,
83 const std::string& rcList)
84{
Matthew Barthb7985272018-04-17 10:50:36 -050085 if (rcList.empty())
86 {
87 return;
88 }
89
Matthew Barthd26e2772018-03-22 14:24:06 -050090 // Convert to a char* for strtok
91 std::vector<char> rmRCs(rcList.c_str(),
92 rcList.c_str() + rcList.size() + 1);
Patrick Venture50cf1c52018-04-18 09:21:41 -070093 auto rmRC = std::strtok(&rmRCs[0], ", ");
Matthew Barthd26e2772018-03-22 14:24:06 -050094 while (rmRC != nullptr)
95 {
96 try
97 {
98 sensorAdjusts[sensor].rmRCs.insert(std::stoi(rmRC));
99 }
100 catch (const std::logic_error& le)
101 {
102 // Unable to convert to int, continue to next token
103 std::string name = sensor.first + "_" + sensor.second;
104 log<level::INFO>("Unable to convert sensor removal return code",
105 entry("SENSOR=%s", name.c_str()),
106 entry("RC=%s", rmRC),
107 entry("EXCEPTION=%s", le.what()));
108 }
Patrick Venture50cf1c52018-04-18 09:21:41 -0700109 rmRC = std::strtok(nullptr, ", ");
Matthew Barthd26e2772018-03-22 14:24:06 -0500110 }
111}
112
Matt Spinlerfee106b2017-11-29 15:18:05 -0600113int64_t adjustValue(const SensorSet::key_type& sensor, int64_t value)
Chiabing Leec923ce92017-09-06 15:58:26 +0800114{
Patrick Venturec1cece72017-11-07 12:09:49 -0800115// Because read doesn't have an out pointer to store errors.
116// let's assume negative values are errors if they have this
117// set.
118#ifdef NEGATIVE_ERRNO_ON_FAIL
119 if (value < 0)
120 {
121 return value;
122 }
123#endif
124
Chiabing Leec923ce92017-09-06 15:58:26 +0800125 const auto& it = sensorAdjusts.find(sensor);
126 if (it != sensorAdjusts.end())
127 {
128 // Adjust based on gain and offset
129 value = static_cast<decltype(value)>(
130 static_cast<double>(value) * it->second.gain
131 + it->second.offset);
132 }
133 return value;
134}
135
Brad Bishope9fdee02017-01-06 10:43:29 -0500136auto addValue(const SensorSet::key_type& sensor,
Matthew Barthd4beecf2018-04-03 15:50:22 -0500137 const RetryIO& retryIO,
Patrick Venture75e56c62018-04-20 18:10:15 -0700138 hwmonio::HwmonIO& ioAccess,
Matthew Barth0b305052018-04-26 09:46:49 -0500139 ObjectInfo& info)
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),
Matthew Barth0b305052018-04-26 09:46:49 -0500184 std::get<std::chrono::milliseconds>(retryIO));
Matthew Barthca44c2e2018-04-24 15:33:25 -0500185 val = adjustValue(sensor, val);
186 }
Chiabing Leec923ce92017-09-06 15:58:26 +0800187
Brad Bishop30dbcee2017-01-18 07:55:42 -0500188 auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals);
Brad Bishope9fdee02017-01-06 10:43:29 -0500189 iface->value(val);
190
Patrick Venture09791852018-04-17 17:40:00 -0700191 hwmon::Attributes attrs;
192 if (hwmon::getAttributes(sensor.first, attrs))
Brad Bishope9fdee02017-01-06 10:43:29 -0500193 {
Patrick Venture09791852018-04-17 17:40:00 -0700194 iface->unit(hwmon::getUnit(attrs));
195 iface->scale(hwmon::getScale(attrs));
Brad Bishope9fdee02017-01-06 10:43:29 -0500196 }
197
Patrick Venture7a5285d2018-04-17 19:15:05 -0700198 auto maxValue = env::getEnv("MAXVALUE", sensor);
James Feist7bd5fcf2018-01-22 16:14:28 -0800199 if(!maxValue.empty())
200 {
201 iface->maxValue(std::stoll(maxValue));
202 }
Patrick Venture7a5285d2018-04-17 19:15:05 -0700203 auto minValue = env::getEnv("MINVALUE", sensor);
James Feist7bd5fcf2018-01-22 16:14:28 -0800204 if(!minValue.empty())
205 {
206 iface->minValue(std::stoll(minValue));
207 }
208
Brad Bishope9fdee02017-01-06 10:43:29 -0500209 obj[InterfaceType::VALUE] = iface;
210 return iface;
211}
212
Matthew Barth979c8062018-04-17 11:37:15 -0500213std::string MainLoop::getID(SensorSet::container_t::const_reference sensor)
Matthew Barth31d214c2018-03-26 09:54:27 -0500214{
Matthew Barth31d214c2018-03-26 09:54:27 -0500215 std::string id;
216
217 /*
218 * Check if the value of the MODE_<item><X> env variable for the sensor
219 * is "label", then read the sensor number from the <item><X>_label
220 * file. The name of the DBUS object would be the value of the env
221 * variable LABEL_<item><sensorNum>. If the MODE_<item><X> env variable
222 * doesn't exist, then the name of DBUS object is the value of the env
223 * variable LABEL_<item><X>.
224 */
Patrick Venture7a5285d2018-04-17 19:15:05 -0700225 auto mode = env::getEnv("MODE", sensor.first);
Matthew Barth31d214c2018-03-26 09:54:27 -0500226 if (!mode.compare(hwmon::entry::label))
227 {
Patrick Venture7a5285d2018-04-17 19:15:05 -0700228 id = env::getIndirectID(
Matthew Barth31d214c2018-03-26 09:54:27 -0500229 _hwmonRoot + '/' + _instance + '/', sensor.first);
230
231 if (id.empty())
232 {
Matthew Barth979c8062018-04-17 11:37:15 -0500233 return id;
Matthew Barth31d214c2018-03-26 09:54:27 -0500234 }
235 }
236
237 // Use the ID we looked up above if there was one,
238 // otherwise use the standard one.
239 id = (id.empty()) ? sensor.first.second : id;
240
Matthew Barth979c8062018-04-17 11:37:15 -0500241 return id;
242}
243
244SensorIdentifiers MainLoop::getIdentifiers(
245 SensorSet::container_t::const_reference sensor)
246{
247 std::string id = getID(sensor);
248 std::string label;
249
250 if (!id.empty())
251 {
252 // Ignore inputs without a label.
253 label = env::getEnv("LABEL", sensor.first.first, id);
254 }
255
256 return std::make_tuple(std::move(id),
257 std::move(label));
258}
259
260/**
261 * Reads the environment parameters of a sensor and creates an object with
262 * atleast the `Value` interface, otherwise returns without creating the object.
263 * If the `Value` interface is successfully created, by reading the sensor's
264 * corresponding sysfs file's value, the additional interfaces for the sensor
Matthew Barthd238e232018-04-17 12:01:50 -0500265 * are created and the InterfacesAdded signal is emitted. The object's state
266 * data is then returned for sensor state monitoring within the main loop.
Matthew Barth979c8062018-04-17 11:37:15 -0500267 */
Matthew Barthd238e232018-04-17 12:01:50 -0500268optional_ns::optional<ObjectStateData> MainLoop::getObject(
269 SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500270{
271 auto properties = getIdentifiers(sensor);
272 if (std::get<sensorID>(properties).empty() ||
273 std::get<sensorLabel>(properties).empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500274 {
Matthew Barthd238e232018-04-17 12:01:50 -0500275 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500276 }
277
Patrick Venture09791852018-04-17 17:40:00 -0700278 hwmon::Attributes attrs;
279 if (!hwmon::getAttributes(sensor.first.first, attrs))
Matthew Barth31d214c2018-03-26 09:54:27 -0500280 {
Matthew Barthd238e232018-04-17 12:01:50 -0500281 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500282 }
283
Matthew Barthb7985272018-04-17 10:50:36 -0500284 // Get list of return codes for removing sensors on device
285 auto devRmRCs = env::getEnv("REMOVERCS");
286 // Add sensor removal return codes defined at the device level
287 addRemoveRCs(sensor.first, devRmRCs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500288
289 std::string objectPath{_root};
290 objectPath.append(1, '/');
Patrick Venture09791852018-04-17 17:40:00 -0700291 objectPath.append(hwmon::getNamespace(attrs));
Matthew Barth31d214c2018-03-26 09:54:27 -0500292 objectPath.append(1, '/');
Matthew Barth979c8062018-04-17 11:37:15 -0500293 objectPath.append(std::get<sensorLabel>(properties));
Matthew Barth31d214c2018-03-26 09:54:27 -0500294
295 ObjectInfo info(&_bus, std::move(objectPath), Object());
Patrick Venture75e56c62018-04-20 18:10:15 -0700296 RetryIO retryIO(hwmonio::retries, hwmonio::delay);
Matthew Barthd4beecf2018-04-03 15:50:22 -0500297 if (rmSensors.find(sensor.first) != rmSensors.end())
298 {
299 // When adding a sensor that was purposely removed,
300 // don't retry on errors when reading its value
301 std::get<size_t>(retryIO) = 0;
302 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500303 auto valueInterface = static_cast<
304 std::shared_ptr<ValueObject>>(nullptr);
305 try
306 {
Matthew Barthca44c2e2018-04-24 15:33:25 -0500307 // Add status interface based on _fault file being present
308 sensor::addStatus(sensor.first, ioAccess, _devPath, info);
Matthew Barth0b305052018-04-26 09:46:49 -0500309 valueInterface = addValue(sensor.first, retryIO, ioAccess, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500310 }
311 catch (const std::system_error& e)
312 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500313 auto file = sysfs::make_sysfs_path(
314 ioAccess.path(),
315 sensor.first.first,
316 sensor.first.second,
317 hwmon::entry::cinput);
Matthew Barth31d214c2018-03-26 09:54:27 -0500318#ifndef REMOVE_ON_FAIL
319 // Check sensorAdjusts for sensor removal RCs
320 const auto& it = sensorAdjusts.find(sensor.first);
321 if (it != sensorAdjusts.end())
322 {
323 auto rmRCit = it->second.rmRCs.find(e.code().value());
324 if (rmRCit != std::end(it->second.rmRCs))
325 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500326 // Return code found in sensor return code removal list
327 if (rmSensors.find(sensor.first) == rmSensors.end())
328 {
329 // Trace for sensor not already removed from dbus
330 log<level::INFO>("Sensor not added to dbus for read fail",
331 entry("FILE=%s", file.c_str()),
332 entry("RC=%d", e.code().value()));
333 rmSensors[std::move(sensor.first)] =
334 std::move(sensor.second);
335 }
Matthew Barthd238e232018-04-17 12:01:50 -0500336 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500337 }
338 }
339#endif
340 using namespace sdbusplus::xyz::openbmc_project::
341 Sensor::Device::Error;
342 report<ReadFailure>(
343 xyz::openbmc_project::Sensor::Device::
344 ReadFailure::CALLOUT_ERRNO(e.code().value()),
345 xyz::openbmc_project::Sensor::Device::
346 ReadFailure::CALLOUT_DEVICE_PATH(_devPath.c_str()));
347
Matthew Barth31d214c2018-03-26 09:54:27 -0500348 log<level::INFO>("Logging failing sysfs file",
349 entry("FILE=%s", file.c_str()));
350#ifdef REMOVE_ON_FAIL
Matthew Barthd238e232018-04-17 12:01:50 -0500351 return {}; /* skip adding this sensor for now. */
Matthew Barth31d214c2018-03-26 09:54:27 -0500352#else
353 exit(EXIT_FAILURE);
354#endif
355 }
356 auto sensorValue = valueInterface->value();
Matthew Barth979c8062018-04-17 11:37:15 -0500357 addThreshold<WarningObject>(sensor.first.first,
358 std::get<sensorID>(properties),
359 sensorValue,
360 info);
361 addThreshold<CriticalObject>(sensor.first.first,
362 std::get<sensorID>(properties),
363 sensorValue,
364 info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500365
Matthew Barth28f8e662018-03-26 16:57:36 -0500366 auto target = addTarget<hwmon::FanSpeed>(
367 sensor.first, ioAccess, _devPath, info);
368 if (target)
Matthew Barth31d214c2018-03-26 09:54:27 -0500369 {
Matthew Barth28f8e662018-03-26 16:57:36 -0500370 target->enable();
Matthew Barth31d214c2018-03-26 09:54:27 -0500371 }
Matthew Barth28f8e662018-03-26 16:57:36 -0500372 addTarget<hwmon::FanPwm>(sensor.first, ioAccess, _devPath, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500373
374 // All the interfaces have been created. Go ahead
375 // and emit InterfacesAdded.
376 valueInterface->emit_object_added();
377
Matthew Barthd238e232018-04-17 12:01:50 -0500378 return std::make_pair(std::move(std::get<sensorLabel>(properties)),
379 std::move(info));
Matthew Barth31d214c2018-03-26 09:54:27 -0500380}
381
Brad Bishopb9e2b072016-12-19 13:47:10 -0500382MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500383 sdbusplus::bus::bus&& bus,
Patrick Venturec897d8b2018-04-23 19:01:56 -0700384 const std::string& param,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500385 const std::string& path,
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400386 const std::string& devPath,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500387 const char* prefix,
388 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500389 : _bus(std::move(bus)),
Brad Bishop03e87352017-03-07 00:12:22 -0500390 _manager(_bus, root),
Patrick Venturec897d8b2018-04-23 19:01:56 -0700391 _pathParam(param),
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{
Patrick Venture73a50c72018-04-17 15:19:03 -0700400 // Strip off any trailing slashes.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500401 std::string p = path;
402 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500403 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500404 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500405 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500406
Patrick Venture73a50c72018-04-17 15:19:03 -0700407 // Given the furthest right /, set instance to
408 // the basename, and hwmonRoot to the leading path.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500409 auto n = p.rfind('/');
410 if (n != std::string::npos)
411 {
412 _instance.assign(p.substr(n + 1));
413 _hwmonRoot.assign(p.substr(0, n));
414 }
415
416 assert(!_instance.empty());
417 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500418}
419
420void MainLoop::shutdown() noexcept
421{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600422 timer->state(phosphor::hwmon::timer::OFF);
423 sd_event_exit(loop, 0);
424 loop = nullptr;
Brad Bishopd499ca62016-12-19 09:24:50 -0500425}
426
427void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500428{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600429 init();
430
431 sd_event_default(&loop);
432
433 std::function<void()> callback(std::bind(
434 &MainLoop::read, this));
435 try
436 {
437 timer = std::make_unique<phosphor::hwmon::Timer>(
438 loop, callback,
439 std::chrono::microseconds(_interval),
440 phosphor::hwmon::timer::ON);
441
442 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
443
444 // TODO: Issue#7 - Should probably periodically check the SensorSet
445 // for new entries.
446
447 _bus.attach_event(loop, SD_EVENT_PRIORITY_IMPORTANT);
448 sd_event_loop(loop);
449 }
450 catch (const std::system_error& e)
451 {
452 log<level::ERR>("Error in sysfs polling loop",
453 entry("ERROR=%s", e.what()));
454 throw;
455 }
456}
457
458void MainLoop::init()
459{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500460 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500461 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500462
Brad Bishop75b4ab82017-01-06 09:33:50 -0500463 for (auto& i : *sensors)
464 {
Matthew Barthd238e232018-04-17 12:01:50 -0500465 auto object = getObject(i);
466 if (object)
467 {
468 // Construct the SensorSet value
469 // std::tuple<SensorSet::mapped_type,
470 // std::string(Sensor Label),
471 // ObjectInfo>
472 auto value = std::make_tuple(std::move(i.second),
473 std::move((*object).first),
474 std::move((*object).second));
475
476 state[std::move(i.first)] = std::move(value);
477 }
Brad Bishop75b4ab82017-01-06 09:33:50 -0500478 }
479
Patrick Venture62503a42017-05-23 07:30:29 -0700480 /* If there are no sensors specified by labels, exit. */
481 if (0 == state.size())
482 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600483 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700484 }
485
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500486 {
Patrick Venturec897d8b2018-04-23 19:01:56 -0700487 std::stringstream ss;
488 ss << _prefix
489 << "-"
490 << std::to_string(std::hash<std::string>{}(_devPath + _pathParam))
491 << ".Hwmon1";
492
493 _bus.request_name(ss.str().c_str());
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500494 }
495
Patrick Ventureab10f162017-05-22 09:44:50 -0700496 {
Patrick Venturea24c8802018-04-17 19:38:06 -0700497 auto interval = env::getEnv("INTERVAL");
498 if (!interval.empty())
Patrick Ventureab10f162017-05-22 09:44:50 -0700499 {
Patrick Venture50cf1c52018-04-18 09:21:41 -0700500 _interval = std::strtoull(interval.c_str(), NULL, 10);
Patrick Ventureab10f162017-05-22 09:44:50 -0700501 }
502 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600503}
Patrick Ventureab10f162017-05-22 09:44:50 -0700504
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600505void MainLoop::read()
506{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500507 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
508 // ensure the objects all exist?
509
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600510 // Iterate through all the sensors.
511 for (auto& i : state)
512 {
513 auto& attrs = std::get<0>(i.second);
514 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500515 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600516 // Read value from sensor.
517 int64_t value;
518 std::string input = hwmon::entry::cinput;
519 if (i.first.first == "pwm") {
520 input = "";
521 }
522
523 try
Brad Bishope55ef3d2016-12-19 09:12:40 -0500524 {
Matthew Barth27c4a392018-04-25 14:38:51 -0500525 auto& objInfo = std::get<ObjectInfo>(i.second);
526 auto& obj = std::get<Object>(objInfo);
527
528 auto it = obj.find(InterfaceType::STATUS);
529 if (it != obj.end())
530 {
Matthew Barthbfcaf3d2018-04-25 15:05:58 -0500531 auto fault = ioAccess.read(
532 i.first.first,
533 i.first.second,
534 hwmon::entry::fault,
535 hwmonio::retries,
536 hwmonio::delay);
Matthew Barth27c4a392018-04-25 14:38:51 -0500537 auto statusIface = std::experimental::any_cast<
538 std::shared_ptr<StatusObject>>(it->second);
Matthew Barthbfcaf3d2018-04-25 15:05:58 -0500539 if (!statusIface->functional((fault == 0) ? true : false))
Matthew Barth27c4a392018-04-25 14:38:51 -0500540 {
541 continue;
542 }
543 }
544
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600545 // Retry for up to a second if device is busy
546 // or has a transient error.
Patrick Venture9331ab72018-01-29 09:48:47 -0800547
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600548 value = ioAccess.read(
549 i.first.first,
550 i.first.second,
551 input,
Patrick Venture75e56c62018-04-20 18:10:15 -0700552 hwmonio::retries,
Matthew Barth0b305052018-04-26 09:46:49 -0500553 hwmonio::delay);
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600554
555 value = adjustValue(i.first, value);
556
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600557 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500558 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600559 auto valueIface = std::shared_ptr<ValueObject>();
560 auto warnIface = std::shared_ptr<WarningObject>();
561 auto critIface = std::shared_ptr<CriticalObject>();
Brad Bishop754d38c2017-09-08 00:46:58 -0400562
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600563 switch (iface.first)
Brad Bishope0b7d052017-01-06 15:30:23 -0500564 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600565 case InterfaceType::VALUE:
566 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
567 (iface.second);
568 valueIface->value(value);
569 break;
570 case InterfaceType::WARN:
571 checkThresholds<WarningObject>(iface.second, value);
572 break;
573 case InterfaceType::CRIT:
574 checkThresholds<CriticalObject>(iface.second, value);
575 break;
576 default:
577 break;
Brad Bishope0b7d052017-01-06 15:30:23 -0500578 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500579 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600580 }
581 catch (const std::system_error& e)
582 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500583 auto file = sysfs::make_sysfs_path(
584 ioAccess.path(),
585 i.first.first,
586 i.first.second,
587 hwmon::entry::cinput);
Matthew Barth8772ce32018-03-22 16:03:06 -0500588#ifndef REMOVE_ON_FAIL
589 // Check sensorAdjusts for sensor removal RCs
590 const auto& it = sensorAdjusts.find(i.first);
591 if (it != sensorAdjusts.end())
592 {
593 auto rmRCit = it->second.rmRCs.find(e.code().value());
594 if (rmRCit != std::end(it->second.rmRCs))
595 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500596 // Return code found in sensor return code removal list
597 if (rmSensors.find(i.first) == rmSensors.end())
598 {
599 // Trace for sensor not already removed from dbus
600 log<level::INFO>(
601 "Remove sensor from dbus for read fail",
602 entry("FILE=%s", file.c_str()),
603 entry("RC=%d", e.code().value()));
604 // Mark this sensor to be removed from dbus
605 rmSensors[i.first] = std::get<0>(i.second);
606 }
Matthew Barth8772ce32018-03-22 16:03:06 -0500607 continue;
608 }
609 }
610#endif
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600611 using namespace sdbusplus::xyz::openbmc_project::
612 Sensor::Device::Error;
613 report<ReadFailure>(
614 xyz::openbmc_project::Sensor::Device::
615 ReadFailure::CALLOUT_ERRNO(e.code().value()),
616 xyz::openbmc_project::Sensor::Device::
617 ReadFailure::CALLOUT_DEVICE_PATH(
618 _devPath.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500619
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600620 log<level::INFO>("Logging failing sysfs file",
621 entry("FILE=%s", file.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500622
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500623#ifdef REMOVE_ON_FAIL
Matthew Barth33db92b2018-03-26 09:55:19 -0500624 rmSensors[i.first] = std::get<0>(i.second);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500625#else
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600626 exit(EXIT_FAILURE);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500627#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500628 }
629 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600630 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500631
Matthew Barth8772ce32018-03-22 16:03:06 -0500632 // Remove any sensors marked for removal
Matthew Barth33db92b2018-03-26 09:55:19 -0500633 for (auto& i : rmSensors)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600634 {
Matthew Barth33db92b2018-03-26 09:55:19 -0500635 state.erase(i.first);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500636 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500637
638#ifndef REMOVE_ON_FAIL
639 // Attempt to add any sensors that were removed
640 auto it = rmSensors.begin();
641 while (it != rmSensors.end())
642 {
643 if (state.find(it->first) == state.end())
644 {
645 SensorSet::container_t::value_type ssValueType =
646 std::make_pair(it->first, it->second);
Matthew Barthd238e232018-04-17 12:01:50 -0500647 auto object = getObject(ssValueType);
648 if (object)
Matthew Barth31d214c2018-03-26 09:54:27 -0500649 {
Matthew Barthd238e232018-04-17 12:01:50 -0500650 // Construct the SensorSet value
651 // std::tuple<SensorSet::mapped_type,
652 // std::string(Sensor Label),
653 // ObjectInfo>
654 auto value = std::make_tuple(std::move(ssValueType.second),
655 std::move((*object).first),
656 std::move((*object).second));
657
658 state[std::move(ssValueType.first)] = std::move(value);
659
Matthew Barth31d214c2018-03-26 09:54:27 -0500660 // Sensor object added, erase entry from removal list
Matthew Barth38c74e72018-04-02 12:41:26 -0500661 auto file = sysfs::make_sysfs_path(
662 ioAccess.path(),
663 it->first.first,
664 it->first.second,
665 hwmon::entry::cinput);
666 log<level::INFO>(
667 "Added sensor to dbus after successful read",
668 entry("FILE=%s", file.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500669 it = rmSensors.erase(it);
670 }
671 else
672 {
673 ++it;
674 }
675 }
676 else
677 {
678 // Sanity check to remove sensors that were re-added
679 it = rmSensors.erase(it);
680 }
681 }
682#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500683}
684
685// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4