blob: 5324fcd0fafbcf427656f851fd1bf23c2c502af4 [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"
Brad Bishope55ef3d2016-12-19 09:12:40 -050036
Patrick Venture1e6324f2017-06-01 14:07:05 -070037#include <xyz/openbmc_project/Sensor/Device/error.hpp>
38
39using namespace phosphor::logging;
40
Saqib Khan973886d2017-03-15 14:01:16 -050041// Initialization for Warning Objects
42decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo =
43 &WarningObject::warningLow;
44decltype(Thresholds<WarningObject>::setHi) Thresholds<WarningObject>::setHi =
45 &WarningObject::warningHigh;
46decltype(Thresholds<WarningObject>::getLo) Thresholds<WarningObject>::getLo =
47 &WarningObject::warningLow;
48decltype(Thresholds<WarningObject>::getHi) Thresholds<WarningObject>::getHi =
49 &WarningObject::warningHigh;
50decltype(Thresholds<WarningObject>::alarmLo) Thresholds<WarningObject>::alarmLo =
51 &WarningObject::warningAlarmLow;
52decltype(Thresholds<WarningObject>::alarmHi) Thresholds<WarningObject>::alarmHi =
53 &WarningObject::warningAlarmHigh;
54
55// Initialization for Critical Objects
56decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo =
57 &CriticalObject::criticalLow;
58decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi =
59 &CriticalObject::criticalHigh;
60decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo =
61 &CriticalObject::criticalLow;
62decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi =
63 &CriticalObject::criticalHigh;
64decltype(Thresholds<CriticalObject>::alarmLo) Thresholds<CriticalObject>::alarmLo =
65 &CriticalObject::criticalAlarmLow;
66decltype(Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi =
67 &CriticalObject::criticalAlarmHigh;
68
Chiabing Leec923ce92017-09-06 15:58:26 +080069// The gain and offset to adjust a value
70struct valueAdjust
71{
72 double gain = 1.0;
73 int offset = 0;
Matthew Barthd26e2772018-03-22 14:24:06 -050074 std::unordered_set<int> rmRCs;
Chiabing Leec923ce92017-09-06 15:58:26 +080075};
76
77// Store the valueAdjust for sensors
78std::map<SensorSet::key_type, valueAdjust> sensorAdjusts;
79
Matthew Barthd26e2772018-03-22 14:24:06 -050080void addRemoveRCs(const SensorSet::key_type& sensor,
81 const std::string& rcList)
82{
Matthew Barthb7985272018-04-17 10:50:36 -050083 if (rcList.empty())
84 {
85 return;
86 }
87
Matthew Barthd26e2772018-03-22 14:24:06 -050088 // Convert to a char* for strtok
89 std::vector<char> rmRCs(rcList.c_str(),
90 rcList.c_str() + rcList.size() + 1);
Patrick Venture50cf1c52018-04-18 09:21:41 -070091 auto rmRC = std::strtok(&rmRCs[0], ", ");
Matthew Barthd26e2772018-03-22 14:24:06 -050092 while (rmRC != nullptr)
93 {
94 try
95 {
96 sensorAdjusts[sensor].rmRCs.insert(std::stoi(rmRC));
97 }
98 catch (const std::logic_error& le)
99 {
100 // Unable to convert to int, continue to next token
101 std::string name = sensor.first + "_" + sensor.second;
102 log<level::INFO>("Unable to convert sensor removal return code",
103 entry("SENSOR=%s", name.c_str()),
104 entry("RC=%s", rmRC),
105 entry("EXCEPTION=%s", le.what()));
106 }
Patrick Venture50cf1c52018-04-18 09:21:41 -0700107 rmRC = std::strtok(nullptr, ", ");
Matthew Barthd26e2772018-03-22 14:24:06 -0500108 }
109}
110
Matt Spinlerfee106b2017-11-29 15:18:05 -0600111int64_t adjustValue(const SensorSet::key_type& sensor, int64_t value)
Chiabing Leec923ce92017-09-06 15:58:26 +0800112{
Patrick Venturec1cece72017-11-07 12:09:49 -0800113// Because read doesn't have an out pointer to store errors.
114// let's assume negative values are errors if they have this
115// set.
116#ifdef NEGATIVE_ERRNO_ON_FAIL
117 if (value < 0)
118 {
119 return value;
120 }
121#endif
122
Chiabing Leec923ce92017-09-06 15:58:26 +0800123 const auto& it = sensorAdjusts.find(sensor);
124 if (it != sensorAdjusts.end())
125 {
126 // Adjust based on gain and offset
127 value = static_cast<decltype(value)>(
128 static_cast<double>(value) * it->second.gain
129 + it->second.offset);
130 }
131 return value;
132}
133
Brad Bishope9fdee02017-01-06 10:43:29 -0500134auto addValue(const SensorSet::key_type& sensor,
Matthew Barthd4beecf2018-04-03 15:50:22 -0500135 const RetryIO& retryIO,
Patrick Venture75e56c62018-04-20 18:10:15 -0700136 hwmonio::HwmonIO& ioAccess,
Matthew Bartha23babd2018-03-16 10:03:27 -0500137 ObjectInfo& info,
138 bool isOCC = false)
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
Matthew Barth8772ce32018-03-22 16:03:06 -0500151 // Retry for up to a second if device is busy
152 // or has a transient error.
153 int64_t val = ioAccess.read(
154 sensor.first,
155 sensor.second,
156 hwmon::entry::cinput,
Matthew Barthd4beecf2018-04-03 15:50:22 -0500157 std::get<size_t>(retryIO),
158 std::get<std::chrono::milliseconds>(retryIO),
Matthew Barth8772ce32018-03-22 16:03:06 -0500159 isOCC);
Patrick Venture1e6324f2017-06-01 14:07:05 -0700160
Patrick Venture7a5285d2018-04-17 19:15:05 -0700161 auto gain = env::getEnv("GAIN", sensor);
Chiabing Leec923ce92017-09-06 15:58:26 +0800162 if (!gain.empty())
163 {
164 sensorAdjusts[sensor].gain = std::stod(gain);
165 }
166
Patrick Venture7a5285d2018-04-17 19:15:05 -0700167 auto offset = env::getEnv("OFFSET", sensor);
Chiabing Leec923ce92017-09-06 15:58:26 +0800168 if (!offset.empty())
169 {
170 sensorAdjusts[sensor].offset = std::stoi(offset);
171 }
172
173 val = adjustValue(sensor, val);
174
Brad Bishop30dbcee2017-01-18 07:55:42 -0500175 auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals);
Brad Bishope9fdee02017-01-06 10:43:29 -0500176 iface->value(val);
177
Patrick Venture09791852018-04-17 17:40:00 -0700178 hwmon::Attributes attrs;
179 if (hwmon::getAttributes(sensor.first, attrs))
Brad Bishope9fdee02017-01-06 10:43:29 -0500180 {
Patrick Venture09791852018-04-17 17:40:00 -0700181 iface->unit(hwmon::getUnit(attrs));
182 iface->scale(hwmon::getScale(attrs));
Brad Bishope9fdee02017-01-06 10:43:29 -0500183 }
184
Patrick Venture7a5285d2018-04-17 19:15:05 -0700185 auto maxValue = env::getEnv("MAXVALUE", sensor);
James Feist7bd5fcf2018-01-22 16:14:28 -0800186 if(!maxValue.empty())
187 {
188 iface->maxValue(std::stoll(maxValue));
189 }
Patrick Venture7a5285d2018-04-17 19:15:05 -0700190 auto minValue = env::getEnv("MINVALUE", sensor);
James Feist7bd5fcf2018-01-22 16:14:28 -0800191 if(!minValue.empty())
192 {
193 iface->minValue(std::stoll(minValue));
194 }
195
Brad Bishope9fdee02017-01-06 10:43:29 -0500196 obj[InterfaceType::VALUE] = iface;
197 return iface;
198}
199
Matthew Barth979c8062018-04-17 11:37:15 -0500200std::string MainLoop::getID(SensorSet::container_t::const_reference sensor)
Matthew Barth31d214c2018-03-26 09:54:27 -0500201{
Matthew Barth31d214c2018-03-26 09:54:27 -0500202 std::string id;
203
204 /*
205 * Check if the value of the MODE_<item><X> env variable for the sensor
206 * is "label", then read the sensor number from the <item><X>_label
207 * file. The name of the DBUS object would be the value of the env
208 * variable LABEL_<item><sensorNum>. If the MODE_<item><X> env variable
209 * doesn't exist, then the name of DBUS object is the value of the env
210 * variable LABEL_<item><X>.
211 */
Patrick Venture7a5285d2018-04-17 19:15:05 -0700212 auto mode = env::getEnv("MODE", sensor.first);
Matthew Barth31d214c2018-03-26 09:54:27 -0500213 if (!mode.compare(hwmon::entry::label))
214 {
Patrick Venture7a5285d2018-04-17 19:15:05 -0700215 id = env::getIndirectID(
Matthew Barth31d214c2018-03-26 09:54:27 -0500216 _hwmonRoot + '/' + _instance + '/', sensor.first);
217
218 if (id.empty())
219 {
Matthew Barth979c8062018-04-17 11:37:15 -0500220 return id;
Matthew Barth31d214c2018-03-26 09:54:27 -0500221 }
222 }
223
224 // Use the ID we looked up above if there was one,
225 // otherwise use the standard one.
226 id = (id.empty()) ? sensor.first.second : id;
227
Matthew Barth979c8062018-04-17 11:37:15 -0500228 return id;
229}
230
231SensorIdentifiers MainLoop::getIdentifiers(
232 SensorSet::container_t::const_reference sensor)
233{
234 std::string id = getID(sensor);
235 std::string label;
236
237 if (!id.empty())
238 {
239 // Ignore inputs without a label.
240 label = env::getEnv("LABEL", sensor.first.first, id);
241 }
242
243 return std::make_tuple(std::move(id),
244 std::move(label));
245}
246
247/**
248 * Reads the environment parameters of a sensor and creates an object with
249 * atleast the `Value` interface, otherwise returns without creating the object.
250 * If the `Value` interface is successfully created, by reading the sensor's
251 * corresponding sysfs file's value, the additional interfaces for the sensor
252 * are created and the InterfacesAdded signal is emitted. The sensor is then
253 * moved to the list for sensor state monitoring within the main loop.
254 */
255void MainLoop::getObject(SensorSet::container_t::const_reference sensor)
256{
257 auto properties = getIdentifiers(sensor);
258 if (std::get<sensorID>(properties).empty() ||
259 std::get<sensorLabel>(properties).empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500260 {
261 return;
262 }
263
Patrick Venture09791852018-04-17 17:40:00 -0700264 hwmon::Attributes attrs;
265 if (!hwmon::getAttributes(sensor.first.first, attrs))
Matthew Barth31d214c2018-03-26 09:54:27 -0500266 {
267 return;
268 }
269
Matthew Barthb7985272018-04-17 10:50:36 -0500270 // Get list of return codes for removing sensors on device
271 auto devRmRCs = env::getEnv("REMOVERCS");
272 // Add sensor removal return codes defined at the device level
273 addRemoveRCs(sensor.first, devRmRCs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500274
275 std::string objectPath{_root};
276 objectPath.append(1, '/');
Patrick Venture09791852018-04-17 17:40:00 -0700277 objectPath.append(hwmon::getNamespace(attrs));
Matthew Barth31d214c2018-03-26 09:54:27 -0500278 objectPath.append(1, '/');
Matthew Barth979c8062018-04-17 11:37:15 -0500279 objectPath.append(std::get<sensorLabel>(properties));
Matthew Barth31d214c2018-03-26 09:54:27 -0500280
281 ObjectInfo info(&_bus, std::move(objectPath), Object());
Patrick Venture75e56c62018-04-20 18:10:15 -0700282 RetryIO retryIO(hwmonio::retries, hwmonio::delay);
Matthew Barthd4beecf2018-04-03 15:50:22 -0500283 if (rmSensors.find(sensor.first) != rmSensors.end())
284 {
285 // When adding a sensor that was purposely removed,
286 // don't retry on errors when reading its value
287 std::get<size_t>(retryIO) = 0;
288 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500289 auto valueInterface = static_cast<
290 std::shared_ptr<ValueObject>>(nullptr);
291 try
292 {
Matthew Barthd4beecf2018-04-03 15:50:22 -0500293 valueInterface = addValue(sensor.first, retryIO, ioAccess, info,
Matthew Barth31d214c2018-03-26 09:54:27 -0500294 _isOCC);
295 }
296 catch (const std::system_error& e)
297 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500298 auto file = sysfs::make_sysfs_path(
299 ioAccess.path(),
300 sensor.first.first,
301 sensor.first.second,
302 hwmon::entry::cinput);
Matthew Barth31d214c2018-03-26 09:54:27 -0500303#ifndef REMOVE_ON_FAIL
304 // Check sensorAdjusts for sensor removal RCs
305 const auto& it = sensorAdjusts.find(sensor.first);
306 if (it != sensorAdjusts.end())
307 {
308 auto rmRCit = it->second.rmRCs.find(e.code().value());
309 if (rmRCit != std::end(it->second.rmRCs))
310 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500311 // Return code found in sensor return code removal list
312 if (rmSensors.find(sensor.first) == rmSensors.end())
313 {
314 // Trace for sensor not already removed from dbus
315 log<level::INFO>("Sensor not added to dbus for read fail",
316 entry("FILE=%s", file.c_str()),
317 entry("RC=%d", e.code().value()));
318 rmSensors[std::move(sensor.first)] =
319 std::move(sensor.second);
320 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500321 return;
322 }
323 }
324#endif
325 using namespace sdbusplus::xyz::openbmc_project::
326 Sensor::Device::Error;
327 report<ReadFailure>(
328 xyz::openbmc_project::Sensor::Device::
329 ReadFailure::CALLOUT_ERRNO(e.code().value()),
330 xyz::openbmc_project::Sensor::Device::
331 ReadFailure::CALLOUT_DEVICE_PATH(_devPath.c_str()));
332
Matthew Barth31d214c2018-03-26 09:54:27 -0500333 log<level::INFO>("Logging failing sysfs file",
334 entry("FILE=%s", file.c_str()));
335#ifdef REMOVE_ON_FAIL
336 return; /* skip adding this sensor for now. */
337#else
338 exit(EXIT_FAILURE);
339#endif
340 }
341 auto sensorValue = valueInterface->value();
Matthew Barth979c8062018-04-17 11:37:15 -0500342 addThreshold<WarningObject>(sensor.first.first,
343 std::get<sensorID>(properties),
344 sensorValue,
345 info);
346 addThreshold<CriticalObject>(sensor.first.first,
347 std::get<sensorID>(properties),
348 sensorValue,
349 info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500350
Matthew Barth28f8e662018-03-26 16:57:36 -0500351 auto target = addTarget<hwmon::FanSpeed>(
352 sensor.first, ioAccess, _devPath, info);
353 if (target)
Matthew Barth31d214c2018-03-26 09:54:27 -0500354 {
Matthew Barth28f8e662018-03-26 16:57:36 -0500355 target->enable();
Matthew Barth31d214c2018-03-26 09:54:27 -0500356 }
Matthew Barth28f8e662018-03-26 16:57:36 -0500357 addTarget<hwmon::FanPwm>(sensor.first, ioAccess, _devPath, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500358
359 // All the interfaces have been created. Go ahead
360 // and emit InterfacesAdded.
361 valueInterface->emit_object_added();
362
363 auto value = std::make_tuple(
364 std::move(sensor.second),
Matthew Barth979c8062018-04-17 11:37:15 -0500365 std::move(std::get<sensorLabel>(properties)),
Matthew Barth31d214c2018-03-26 09:54:27 -0500366 std::move(info));
367
368 state[std::move(sensor.first)] = std::move(value);
369}
370
Brad Bishopb9e2b072016-12-19 13:47:10 -0500371MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500372 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500373 const std::string& path,
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400374 const std::string& devPath,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500375 const char* prefix,
376 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500377 : _bus(std::move(bus)),
Brad Bishop03e87352017-03-07 00:12:22 -0500378 _manager(_bus, root),
Brad Bishopb8740fc2017-02-24 23:38:37 -0500379 _hwmonRoot(),
380 _instance(),
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400381 _devPath(devPath),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500382 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500383 _root(root),
Brad Bishop751043e2017-08-29 11:13:46 -0400384 state(),
385 ioAccess(path)
Brad Bishopd499ca62016-12-19 09:24:50 -0500386{
Matthew Bartha23babd2018-03-16 10:03:27 -0500387 if (path.find("occ") != std::string::npos)
388 {
389 _isOCC = true;
390 }
391
Patrick Venture73a50c72018-04-17 15:19:03 -0700392 // Strip off any trailing slashes.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500393 std::string p = path;
394 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500395 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500396 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500397 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500398
Patrick Venture73a50c72018-04-17 15:19:03 -0700399 // Given the furthest right /, set instance to
400 // the basename, and hwmonRoot to the leading path.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500401 auto n = p.rfind('/');
402 if (n != std::string::npos)
403 {
404 _instance.assign(p.substr(n + 1));
405 _hwmonRoot.assign(p.substr(0, n));
406 }
407
408 assert(!_instance.empty());
409 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500410}
411
412void MainLoop::shutdown() noexcept
413{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600414 timer->state(phosphor::hwmon::timer::OFF);
415 sd_event_exit(loop, 0);
416 loop = nullptr;
Brad Bishopd499ca62016-12-19 09:24:50 -0500417}
418
419void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500420{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600421 init();
422
423 sd_event_default(&loop);
424
425 std::function<void()> callback(std::bind(
426 &MainLoop::read, this));
427 try
428 {
429 timer = std::make_unique<phosphor::hwmon::Timer>(
430 loop, callback,
431 std::chrono::microseconds(_interval),
432 phosphor::hwmon::timer::ON);
433
434 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
435
436 // TODO: Issue#7 - Should probably periodically check the SensorSet
437 // for new entries.
438
439 _bus.attach_event(loop, SD_EVENT_PRIORITY_IMPORTANT);
440 sd_event_loop(loop);
441 }
442 catch (const std::system_error& e)
443 {
444 log<level::ERR>("Error in sysfs polling loop",
445 entry("ERROR=%s", e.what()));
446 throw;
447 }
448}
449
450void MainLoop::init()
451{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500452 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500453 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500454
Brad Bishop75b4ab82017-01-06 09:33:50 -0500455 for (auto& i : *sensors)
456 {
Matthew Barth31d214c2018-03-26 09:54:27 -0500457 getObject(i);
Brad Bishop75b4ab82017-01-06 09:33:50 -0500458 }
459
Patrick Venture62503a42017-05-23 07:30:29 -0700460 /* If there are no sensors specified by labels, exit. */
461 if (0 == state.size())
462 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600463 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700464 }
465
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500466 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500467 std::string busname{_prefix};
Brad Bishop4d9a35b2017-11-14 22:33:03 -0500468 busname.append(1, '-');
469 busname.append(
470 std::to_string(std::hash<decltype(_devPath)>{}(_devPath)));
471 busname.append(".Hwmon1");
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500472 _bus.request_name(busname.c_str());
473 }
474
Patrick Ventureab10f162017-05-22 09:44:50 -0700475 {
Patrick Venturea24c8802018-04-17 19:38:06 -0700476 auto interval = env::getEnv("INTERVAL");
477 if (!interval.empty())
Patrick Ventureab10f162017-05-22 09:44:50 -0700478 {
Patrick Venture50cf1c52018-04-18 09:21:41 -0700479 _interval = std::strtoull(interval.c_str(), NULL, 10);
Patrick Ventureab10f162017-05-22 09:44:50 -0700480 }
481 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600482}
Patrick Ventureab10f162017-05-22 09:44:50 -0700483
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600484void MainLoop::read()
485{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500486 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
487 // ensure the objects all exist?
488
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600489 // Iterate through all the sensors.
490 for (auto& i : state)
491 {
492 auto& attrs = std::get<0>(i.second);
493 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500494 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600495 // Read value from sensor.
496 int64_t value;
497 std::string input = hwmon::entry::cinput;
498 if (i.first.first == "pwm") {
499 input = "";
500 }
501
502 try
Brad Bishope55ef3d2016-12-19 09:12:40 -0500503 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600504 // Retry for up to a second if device is busy
505 // or has a transient error.
Patrick Venture9331ab72018-01-29 09:48:47 -0800506
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600507 value = ioAccess.read(
508 i.first.first,
509 i.first.second,
510 input,
Patrick Venture75e56c62018-04-20 18:10:15 -0700511 hwmonio::retries,
512 hwmonio::delay,
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600513 _isOCC);
514
515 value = adjustValue(i.first, value);
516
517 auto& objInfo = std::get<ObjectInfo>(i.second);
518 auto& obj = std::get<Object>(objInfo);
519
520 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500521 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600522 auto valueIface = std::shared_ptr<ValueObject>();
523 auto warnIface = std::shared_ptr<WarningObject>();
524 auto critIface = std::shared_ptr<CriticalObject>();
Brad Bishop754d38c2017-09-08 00:46:58 -0400525
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600526 switch (iface.first)
Brad Bishope0b7d052017-01-06 15:30:23 -0500527 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600528 case InterfaceType::VALUE:
529 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
530 (iface.second);
531 valueIface->value(value);
532 break;
533 case InterfaceType::WARN:
534 checkThresholds<WarningObject>(iface.second, value);
535 break;
536 case InterfaceType::CRIT:
537 checkThresholds<CriticalObject>(iface.second, value);
538 break;
539 default:
540 break;
Brad Bishope0b7d052017-01-06 15:30:23 -0500541 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500542 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600543 }
544 catch (const std::system_error& e)
545 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500546 auto file = sysfs::make_sysfs_path(
547 ioAccess.path(),
548 i.first.first,
549 i.first.second,
550 hwmon::entry::cinput);
Matthew Barth8772ce32018-03-22 16:03:06 -0500551#ifndef REMOVE_ON_FAIL
552 // Check sensorAdjusts for sensor removal RCs
553 const auto& it = sensorAdjusts.find(i.first);
554 if (it != sensorAdjusts.end())
555 {
556 auto rmRCit = it->second.rmRCs.find(e.code().value());
557 if (rmRCit != std::end(it->second.rmRCs))
558 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500559 // Return code found in sensor return code removal list
560 if (rmSensors.find(i.first) == rmSensors.end())
561 {
562 // Trace for sensor not already removed from dbus
563 log<level::INFO>(
564 "Remove sensor from dbus for read fail",
565 entry("FILE=%s", file.c_str()),
566 entry("RC=%d", e.code().value()));
567 // Mark this sensor to be removed from dbus
568 rmSensors[i.first] = std::get<0>(i.second);
569 }
Matthew Barth8772ce32018-03-22 16:03:06 -0500570 continue;
571 }
572 }
573#endif
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600574 using namespace sdbusplus::xyz::openbmc_project::
575 Sensor::Device::Error;
576 report<ReadFailure>(
577 xyz::openbmc_project::Sensor::Device::
578 ReadFailure::CALLOUT_ERRNO(e.code().value()),
579 xyz::openbmc_project::Sensor::Device::
580 ReadFailure::CALLOUT_DEVICE_PATH(
581 _devPath.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500582
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600583 log<level::INFO>("Logging failing sysfs file",
584 entry("FILE=%s", file.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500585
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500586#ifdef REMOVE_ON_FAIL
Matthew Barth33db92b2018-03-26 09:55:19 -0500587 rmSensors[i.first] = std::get<0>(i.second);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500588#else
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600589 exit(EXIT_FAILURE);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500590#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500591 }
592 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600593 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500594
Matthew Barth8772ce32018-03-22 16:03:06 -0500595 // Remove any sensors marked for removal
Matthew Barth33db92b2018-03-26 09:55:19 -0500596 for (auto& i : rmSensors)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600597 {
Matthew Barth33db92b2018-03-26 09:55:19 -0500598 state.erase(i.first);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500599 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500600
601#ifndef REMOVE_ON_FAIL
602 // Attempt to add any sensors that were removed
603 auto it = rmSensors.begin();
604 while (it != rmSensors.end())
605 {
606 if (state.find(it->first) == state.end())
607 {
608 SensorSet::container_t::value_type ssValueType =
609 std::make_pair(it->first, it->second);
610 getObject(ssValueType);
611 if (state.find(it->first) != state.end())
612 {
613 // Sensor object added, erase entry from removal list
Matthew Barth38c74e72018-04-02 12:41:26 -0500614 auto file = sysfs::make_sysfs_path(
615 ioAccess.path(),
616 it->first.first,
617 it->first.second,
618 hwmon::entry::cinput);
619 log<level::INFO>(
620 "Added sensor to dbus after successful read",
621 entry("FILE=%s", file.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500622 it = rmSensors.erase(it);
623 }
624 else
625 {
626 ++it;
627 }
628 }
629 else
630 {
631 // Sanity check to remove sensors that were re-added
632 it = rmSensors.erase(it);
633 }
634 }
635#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500636}
637
638// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4