blob: 62467fd42a57d9e9fe75c5d33950860603fb2999 [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>
Chiabing Leec923ce92017-09-06 15:58:26 +080020#include <string>
Matthew Barthd26e2772018-03-22 14:24:06 -050021#include <unordered_set>
Patrick Venture1e6324f2017-06-01 14:07:05 -070022
23#include <phosphor-logging/elog-errors.hpp>
Matt Spinlerf9c83c42017-08-10 08:51:45 -050024#include "config.h"
Brad Bishope55ef3d2016-12-19 09:12:40 -050025#include "sensorset.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050026#include "hwmon.hpp"
27#include "sysfs.hpp"
Brad Bishopd499ca62016-12-19 09:24:50 -050028#include "mainloop.hpp"
Brad Bishopf3df6b42017-01-06 10:14:09 -050029#include "env.hpp"
Brad Bishope0b7d052017-01-06 15:30:23 -050030#include "thresholds.hpp"
Matthew Barthbf7b7b12017-03-07 15:46:59 -060031#include "targets.hpp"
Matthew Barth048ac872017-03-09 14:36:08 -060032#include "fan_speed.hpp"
Patrick Venture9331ab72018-01-29 09:48:47 -080033#include "fan_pwm.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050034
Patrick Venture1e6324f2017-06-01 14:07:05 -070035#include <xyz/openbmc_project/Sensor/Device/error.hpp>
36
37using namespace phosphor::logging;
38
Saqib Khan973886d2017-03-15 14:01:16 -050039// Initialization for Warning Objects
40decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo =
41 &WarningObject::warningLow;
42decltype(Thresholds<WarningObject>::setHi) Thresholds<WarningObject>::setHi =
43 &WarningObject::warningHigh;
44decltype(Thresholds<WarningObject>::getLo) Thresholds<WarningObject>::getLo =
45 &WarningObject::warningLow;
46decltype(Thresholds<WarningObject>::getHi) Thresholds<WarningObject>::getHi =
47 &WarningObject::warningHigh;
48decltype(Thresholds<WarningObject>::alarmLo) Thresholds<WarningObject>::alarmLo =
49 &WarningObject::warningAlarmLow;
50decltype(Thresholds<WarningObject>::alarmHi) Thresholds<WarningObject>::alarmHi =
51 &WarningObject::warningAlarmHigh;
52
53// Initialization for Critical Objects
54decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo =
55 &CriticalObject::criticalLow;
56decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi =
57 &CriticalObject::criticalHigh;
58decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo =
59 &CriticalObject::criticalLow;
60decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi =
61 &CriticalObject::criticalHigh;
62decltype(Thresholds<CriticalObject>::alarmLo) Thresholds<CriticalObject>::alarmLo =
63 &CriticalObject::criticalAlarmLow;
64decltype(Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi =
65 &CriticalObject::criticalAlarmHigh;
66
Chiabing Leec923ce92017-09-06 15:58:26 +080067// The gain and offset to adjust a value
68struct valueAdjust
69{
70 double gain = 1.0;
71 int offset = 0;
Matthew Barthd26e2772018-03-22 14:24:06 -050072 std::unordered_set<int> rmRCs;
Chiabing Leec923ce92017-09-06 15:58:26 +080073};
74
75// Store the valueAdjust for sensors
76std::map<SensorSet::key_type, valueAdjust> sensorAdjusts;
77
Brad Bishop74aa4dd2017-01-06 09:50:31 -050078static constexpr auto typeAttrMap =
79{
80 // 1 - hwmon class
81 // 2 - unit
82 // 3 - sysfs scaling factor
83 std::make_tuple(
84 hwmon::type::ctemp,
85 ValueInterface::Unit::DegreesC,
Brad Bishopadd98512017-01-06 22:01:19 -050086 -3,
87 "temperature"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050088 std::make_tuple(
89 hwmon::type::cfan,
90 ValueInterface::Unit::RPMS,
Brad Bishopadd98512017-01-06 22:01:19 -050091 0,
92 "fan_tach"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -050093 std::make_tuple(
94 hwmon::type::cvolt,
95 ValueInterface::Unit::Volts,
Brad Bishopadd98512017-01-06 22:01:19 -050096 -3,
97 "voltage"),
Brad Bishop5afe21a2017-01-06 20:44:05 -050098 std::make_tuple(
99 hwmon::type::ccurr,
100 ValueInterface::Unit::Amperes,
Brad Bishopadd98512017-01-06 22:01:19 -0500101 -3,
102 "current"),
Brad Bishop5afe21a2017-01-06 20:44:05 -0500103 std::make_tuple(
104 hwmon::type::cenergy,
105 ValueInterface::Unit::Joules,
Brad Bishopadd98512017-01-06 22:01:19 -0500106 -6,
107 "energy"),
Brad Bishop5afe21a2017-01-06 20:44:05 -0500108 std::make_tuple(
109 hwmon::type::cpower,
110 ValueInterface::Unit::Watts,
Brad Bishopadd98512017-01-06 22:01:19 -0500111 -6,
112 "power"),
Brad Bishop74aa4dd2017-01-06 09:50:31 -0500113};
114
115auto getHwmonType(decltype(typeAttrMap)::const_reference attrs)
116{
117 return std::get<0>(attrs);
118}
119
120auto getUnit(decltype(typeAttrMap)::const_reference attrs)
121{
122 return std::get<1>(attrs);
123}
124
125auto getScale(decltype(typeAttrMap)::const_reference attrs)
126{
127 return std::get<2>(attrs);
128}
129
Brad Bishopadd98512017-01-06 22:01:19 -0500130auto getNamespace(decltype(typeAttrMap)::const_reference attrs)
131{
132 return std::get<3>(attrs);
133}
134
Brad Bishop951a79e2017-01-06 21:55:11 -0500135using AttributeIterator = decltype(*typeAttrMap.begin());
136using Attributes
137 = std::remove_cv<std::remove_reference<AttributeIterator>::type>::type;
138
139auto getAttributes(const std::string& type, Attributes& attributes)
140{
141 // *INDENT-OFF*
142 auto a = std::find_if(
143 typeAttrMap.begin(),
144 typeAttrMap.end(),
145 [&](const auto & e)
146 {
147 return type == getHwmonType(e);
148 });
149 // *INDENT-ON*
150
151 if (a == typeAttrMap.end())
152 {
153 return false;
154 }
155
156 attributes = *a;
157 return true;
158}
159
Matthew Barthd26e2772018-03-22 14:24:06 -0500160void addRemoveRCs(const SensorSet::key_type& sensor,
161 const std::string& rcList)
162{
163 // Convert to a char* for strtok
164 std::vector<char> rmRCs(rcList.c_str(),
165 rcList.c_str() + rcList.size() + 1);
166 auto rmRC = strtok(&rmRCs[0], ", ");
167 while (rmRC != nullptr)
168 {
169 try
170 {
171 sensorAdjusts[sensor].rmRCs.insert(std::stoi(rmRC));
172 }
173 catch (const std::logic_error& le)
174 {
175 // Unable to convert to int, continue to next token
176 std::string name = sensor.first + "_" + sensor.second;
177 log<level::INFO>("Unable to convert sensor removal return code",
178 entry("SENSOR=%s", name.c_str()),
179 entry("RC=%s", rmRC),
180 entry("EXCEPTION=%s", le.what()));
181 }
182 rmRC = strtok(nullptr, ", ");
183 }
184}
185
Matt Spinlerfee106b2017-11-29 15:18:05 -0600186int64_t adjustValue(const SensorSet::key_type& sensor, int64_t value)
Chiabing Leec923ce92017-09-06 15:58:26 +0800187{
Patrick Venturec1cece72017-11-07 12:09:49 -0800188// Because read doesn't have an out pointer to store errors.
189// let's assume negative values are errors if they have this
190// set.
191#ifdef NEGATIVE_ERRNO_ON_FAIL
192 if (value < 0)
193 {
194 return value;
195 }
196#endif
197
Chiabing Leec923ce92017-09-06 15:58:26 +0800198 const auto& it = sensorAdjusts.find(sensor);
199 if (it != sensorAdjusts.end())
200 {
201 // Adjust based on gain and offset
202 value = static_cast<decltype(value)>(
203 static_cast<double>(value) * it->second.gain
204 + it->second.offset);
205 }
206 return value;
207}
208
Brad Bishope9fdee02017-01-06 10:43:29 -0500209auto addValue(const SensorSet::key_type& sensor,
Matthew Barthd4beecf2018-04-03 15:50:22 -0500210 const RetryIO& retryIO,
Brad Bishop751043e2017-08-29 11:13:46 -0400211 sysfs::hwmonio::HwmonIO& ioAccess,
Matthew Bartha23babd2018-03-16 10:03:27 -0500212 ObjectInfo& info,
213 bool isOCC = false)
Brad Bishope9fdee02017-01-06 10:43:29 -0500214{
Brad Bishop30dbcee2017-01-18 07:55:42 -0500215 static constexpr bool deferSignals = true;
216
Brad Bishope9fdee02017-01-06 10:43:29 -0500217 // Get the initial value for the value interface.
218 auto& bus = *std::get<sdbusplus::bus::bus*>(info);
219 auto& obj = std::get<Object>(info);
220 auto& objPath = std::get<std::string>(info);
221
Matthew Barthd26e2772018-03-22 14:24:06 -0500222 auto senRmRCs = getEnv("REMOVERCS", sensor);
223 if (!senRmRCs.empty())
224 {
225 // Add sensor removal return codes defined per sensor
226 addRemoveRCs(sensor, senRmRCs);
227 }
228
Matthew Barth8772ce32018-03-22 16:03:06 -0500229 // Retry for up to a second if device is busy
230 // or has a transient error.
231 int64_t val = ioAccess.read(
232 sensor.first,
233 sensor.second,
234 hwmon::entry::cinput,
Matthew Barthd4beecf2018-04-03 15:50:22 -0500235 std::get<size_t>(retryIO),
236 std::get<std::chrono::milliseconds>(retryIO),
Matthew Barth8772ce32018-03-22 16:03:06 -0500237 isOCC);
Patrick Venture1e6324f2017-06-01 14:07:05 -0700238
Chiabing Leec923ce92017-09-06 15:58:26 +0800239 auto gain = getEnv("GAIN", sensor);
240 if (!gain.empty())
241 {
242 sensorAdjusts[sensor].gain = std::stod(gain);
243 }
244
245 auto offset = getEnv("OFFSET", sensor);
246 if (!offset.empty())
247 {
248 sensorAdjusts[sensor].offset = std::stoi(offset);
249 }
250
251 val = adjustValue(sensor, val);
252
Brad Bishop30dbcee2017-01-18 07:55:42 -0500253 auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals);
Brad Bishope9fdee02017-01-06 10:43:29 -0500254 iface->value(val);
255
Brad Bishop951a79e2017-01-06 21:55:11 -0500256 Attributes attrs;
257 if (getAttributes(sensor.first, attrs))
Brad Bishope9fdee02017-01-06 10:43:29 -0500258 {
Brad Bishop951a79e2017-01-06 21:55:11 -0500259 iface->unit(getUnit(attrs));
260 iface->scale(getScale(attrs));
Brad Bishope9fdee02017-01-06 10:43:29 -0500261 }
262
James Feist7bd5fcf2018-01-22 16:14:28 -0800263 auto maxValue = getEnv("MAXVALUE", sensor);
264 if(!maxValue.empty())
265 {
266 iface->maxValue(std::stoll(maxValue));
267 }
268 auto minValue = getEnv("MINVALUE", sensor);
269 if(!minValue.empty())
270 {
271 iface->minValue(std::stoll(minValue));
272 }
273
Brad Bishope9fdee02017-01-06 10:43:29 -0500274 obj[InterfaceType::VALUE] = iface;
275 return iface;
276}
277
Matthew Barth31d214c2018-03-26 09:54:27 -0500278/**
279 * Reads the environment parameters of a sensor and creates an object with
280 * atleast the `Value` interface, otherwise returns without creating the object.
281 * If the `Value` interface is successfully created, by reading the sensor's
282 * corresponding sysfs file's value, the additional interfaces for the sensor
283 * are created and the InterfacesAdded signal is emitted. The sensor is then
284 * moved to the list for sensor state monitoring within the main loop.
285 */
286void MainLoop::getObject(SensorSet::container_t::const_reference sensor)
287{
Matthew Barth31d214c2018-03-26 09:54:27 -0500288 // Get list of return codes for removing sensors on device
289 std::string deviceRmRCs;
290 auto devRmRCs = getenv("REMOVERCS");
291 if (devRmRCs)
292 {
293 deviceRmRCs.assign(devRmRCs);
294 }
295
296 std::string label;
297 std::string id;
298
299 /*
300 * Check if the value of the MODE_<item><X> env variable for the sensor
301 * is "label", then read the sensor number from the <item><X>_label
302 * file. The name of the DBUS object would be the value of the env
303 * variable LABEL_<item><sensorNum>. If the MODE_<item><X> env variable
304 * doesn't exist, then the name of DBUS object is the value of the env
305 * variable LABEL_<item><X>.
306 */
307 auto mode = getEnv("MODE", sensor.first);
308 if (!mode.compare(hwmon::entry::label))
309 {
310 id = getIndirectID(
311 _hwmonRoot + '/' + _instance + '/', sensor.first);
312
313 if (id.empty())
314 {
315 return;
316 }
317 }
318
319 // Use the ID we looked up above if there was one,
320 // otherwise use the standard one.
321 id = (id.empty()) ? sensor.first.second : id;
322
323 // Ignore inputs without a label.
324 label = getEnv("LABEL", sensor.first.first, id);
325 if (label.empty())
326 {
327 return;
328 }
329
330 Attributes attrs;
331 if (!getAttributes(sensor.first.first, attrs))
332 {
333 return;
334 }
335
336 if (!deviceRmRCs.empty())
337 {
338 // Add sensor removal return codes defined at the device level
339 addRemoveRCs(sensor.first, deviceRmRCs);
340 }
341
342 std::string objectPath{_root};
343 objectPath.append(1, '/');
344 objectPath.append(getNamespace(attrs));
345 objectPath.append(1, '/');
346 objectPath.append(label);
347
348 ObjectInfo info(&_bus, std::move(objectPath), Object());
Matthew Barthd4beecf2018-04-03 15:50:22 -0500349 RetryIO retryIO(sysfs::hwmonio::retries, sysfs::hwmonio::delay);
350 if (rmSensors.find(sensor.first) != rmSensors.end())
351 {
352 // When adding a sensor that was purposely removed,
353 // don't retry on errors when reading its value
354 std::get<size_t>(retryIO) = 0;
355 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500356 auto valueInterface = static_cast<
357 std::shared_ptr<ValueObject>>(nullptr);
358 try
359 {
Matthew Barthd4beecf2018-04-03 15:50:22 -0500360 valueInterface = addValue(sensor.first, retryIO, ioAccess, info,
Matthew Barth31d214c2018-03-26 09:54:27 -0500361 _isOCC);
362 }
363 catch (const std::system_error& e)
364 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500365 auto file = sysfs::make_sysfs_path(
366 ioAccess.path(),
367 sensor.first.first,
368 sensor.first.second,
369 hwmon::entry::cinput);
Matthew Barth31d214c2018-03-26 09:54:27 -0500370#ifndef REMOVE_ON_FAIL
371 // Check sensorAdjusts for sensor removal RCs
372 const auto& it = sensorAdjusts.find(sensor.first);
373 if (it != sensorAdjusts.end())
374 {
375 auto rmRCit = it->second.rmRCs.find(e.code().value());
376 if (rmRCit != std::end(it->second.rmRCs))
377 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500378 // Return code found in sensor return code removal list
379 if (rmSensors.find(sensor.first) == rmSensors.end())
380 {
381 // Trace for sensor not already removed from dbus
382 log<level::INFO>("Sensor not added to dbus for read fail",
383 entry("FILE=%s", file.c_str()),
384 entry("RC=%d", e.code().value()));
385 rmSensors[std::move(sensor.first)] =
386 std::move(sensor.second);
387 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500388 return;
389 }
390 }
391#endif
392 using namespace sdbusplus::xyz::openbmc_project::
393 Sensor::Device::Error;
394 report<ReadFailure>(
395 xyz::openbmc_project::Sensor::Device::
396 ReadFailure::CALLOUT_ERRNO(e.code().value()),
397 xyz::openbmc_project::Sensor::Device::
398 ReadFailure::CALLOUT_DEVICE_PATH(_devPath.c_str()));
399
Matthew Barth31d214c2018-03-26 09:54:27 -0500400 log<level::INFO>("Logging failing sysfs file",
401 entry("FILE=%s", file.c_str()));
402#ifdef REMOVE_ON_FAIL
403 return; /* skip adding this sensor for now. */
404#else
405 exit(EXIT_FAILURE);
406#endif
407 }
408 auto sensorValue = valueInterface->value();
409 addThreshold<WarningObject>(sensor.first.first, id, sensorValue, info);
410 addThreshold<CriticalObject>(sensor.first.first, id, sensorValue, info);
411
Matthew Barth28f8e662018-03-26 16:57:36 -0500412 auto target = addTarget<hwmon::FanSpeed>(
413 sensor.first, ioAccess, _devPath, info);
414 if (target)
Matthew Barth31d214c2018-03-26 09:54:27 -0500415 {
Matthew Barth28f8e662018-03-26 16:57:36 -0500416 target->enable();
Matthew Barth31d214c2018-03-26 09:54:27 -0500417 }
Matthew Barth28f8e662018-03-26 16:57:36 -0500418 addTarget<hwmon::FanPwm>(sensor.first, ioAccess, _devPath, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500419
420 // All the interfaces have been created. Go ahead
421 // and emit InterfacesAdded.
422 valueInterface->emit_object_added();
423
424 auto value = std::make_tuple(
425 std::move(sensor.second),
426 std::move(label),
427 std::move(info));
428
429 state[std::move(sensor.first)] = std::move(value);
430}
431
Brad Bishopb9e2b072016-12-19 13:47:10 -0500432MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500433 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500434 const std::string& path,
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400435 const std::string& devPath,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500436 const char* prefix,
437 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500438 : _bus(std::move(bus)),
Brad Bishop03e87352017-03-07 00:12:22 -0500439 _manager(_bus, root),
Brad Bishopb8740fc2017-02-24 23:38:37 -0500440 _hwmonRoot(),
441 _instance(),
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400442 _devPath(devPath),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500443 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500444 _root(root),
Brad Bishop751043e2017-08-29 11:13:46 -0400445 state(),
446 ioAccess(path)
Brad Bishopd499ca62016-12-19 09:24:50 -0500447{
Matthew Bartha23babd2018-03-16 10:03:27 -0500448 if (path.find("occ") != std::string::npos)
449 {
450 _isOCC = true;
451 }
452
Patrick Venture73a50c72018-04-17 15:19:03 -0700453 // Strip off any trailing slashes.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500454 std::string p = path;
455 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500456 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500457 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500458 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500459
Patrick Venture73a50c72018-04-17 15:19:03 -0700460 // Given the furthest right /, set instance to
461 // the basename, and hwmonRoot to the leading path.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500462 auto n = p.rfind('/');
463 if (n != std::string::npos)
464 {
465 _instance.assign(p.substr(n + 1));
466 _hwmonRoot.assign(p.substr(0, n));
467 }
468
469 assert(!_instance.empty());
470 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500471}
472
473void MainLoop::shutdown() noexcept
474{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600475 timer->state(phosphor::hwmon::timer::OFF);
476 sd_event_exit(loop, 0);
477 loop = nullptr;
Brad Bishopd499ca62016-12-19 09:24:50 -0500478}
479
480void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500481{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600482 init();
483
484 sd_event_default(&loop);
485
486 std::function<void()> callback(std::bind(
487 &MainLoop::read, this));
488 try
489 {
490 timer = std::make_unique<phosphor::hwmon::Timer>(
491 loop, callback,
492 std::chrono::microseconds(_interval),
493 phosphor::hwmon::timer::ON);
494
495 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
496
497 // TODO: Issue#7 - Should probably periodically check the SensorSet
498 // for new entries.
499
500 _bus.attach_event(loop, SD_EVENT_PRIORITY_IMPORTANT);
501 sd_event_loop(loop);
502 }
503 catch (const std::system_error& e)
504 {
505 log<level::ERR>("Error in sysfs polling loop",
506 entry("ERROR=%s", e.what()));
507 throw;
508 }
509}
510
511void MainLoop::init()
512{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500513 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500514 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500515
Brad Bishop75b4ab82017-01-06 09:33:50 -0500516 for (auto& i : *sensors)
517 {
Matthew Barth31d214c2018-03-26 09:54:27 -0500518 getObject(i);
Brad Bishop75b4ab82017-01-06 09:33:50 -0500519 }
520
Patrick Venture62503a42017-05-23 07:30:29 -0700521 /* If there are no sensors specified by labels, exit. */
522 if (0 == state.size())
523 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600524 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700525 }
526
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500527 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500528 std::string busname{_prefix};
Brad Bishop4d9a35b2017-11-14 22:33:03 -0500529 busname.append(1, '-');
530 busname.append(
531 std::to_string(std::hash<decltype(_devPath)>{}(_devPath)));
532 busname.append(".Hwmon1");
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500533 _bus.request_name(busname.c_str());
534 }
535
Patrick Ventureab10f162017-05-22 09:44:50 -0700536 {
537 auto interval = getenv("INTERVAL");
538 if (interval)
539 {
540 _interval = strtoull(interval, NULL, 10);
541 }
542 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600543}
Patrick Ventureab10f162017-05-22 09:44:50 -0700544
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600545void MainLoop::read()
546{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500547 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
548 // ensure the objects all exist?
549
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600550 // Iterate through all the sensors.
551 for (auto& i : state)
552 {
553 auto& attrs = std::get<0>(i.second);
554 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500555 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600556 // Read value from sensor.
557 int64_t value;
558 std::string input = hwmon::entry::cinput;
559 if (i.first.first == "pwm") {
560 input = "";
561 }
562
563 try
Brad Bishope55ef3d2016-12-19 09:12:40 -0500564 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600565 // Retry for up to a second if device is busy
566 // or has a transient error.
Patrick Venture9331ab72018-01-29 09:48:47 -0800567
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600568 value = ioAccess.read(
569 i.first.first,
570 i.first.second,
571 input,
572 sysfs::hwmonio::retries,
573 sysfs::hwmonio::delay,
574 _isOCC);
575
576 value = adjustValue(i.first, value);
577
578 auto& objInfo = std::get<ObjectInfo>(i.second);
579 auto& obj = std::get<Object>(objInfo);
580
581 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500582 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600583 auto valueIface = std::shared_ptr<ValueObject>();
584 auto warnIface = std::shared_ptr<WarningObject>();
585 auto critIface = std::shared_ptr<CriticalObject>();
Brad Bishop754d38c2017-09-08 00:46:58 -0400586
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600587 switch (iface.first)
Brad Bishope0b7d052017-01-06 15:30:23 -0500588 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600589 case InterfaceType::VALUE:
590 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
591 (iface.second);
592 valueIface->value(value);
593 break;
594 case InterfaceType::WARN:
595 checkThresholds<WarningObject>(iface.second, value);
596 break;
597 case InterfaceType::CRIT:
598 checkThresholds<CriticalObject>(iface.second, value);
599 break;
600 default:
601 break;
Brad Bishope0b7d052017-01-06 15:30:23 -0500602 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500603 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600604 }
605 catch (const std::system_error& e)
606 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500607 auto file = sysfs::make_sysfs_path(
608 ioAccess.path(),
609 i.first.first,
610 i.first.second,
611 hwmon::entry::cinput);
Matthew Barth8772ce32018-03-22 16:03:06 -0500612#ifndef REMOVE_ON_FAIL
613 // Check sensorAdjusts for sensor removal RCs
614 const auto& it = sensorAdjusts.find(i.first);
615 if (it != sensorAdjusts.end())
616 {
617 auto rmRCit = it->second.rmRCs.find(e.code().value());
618 if (rmRCit != std::end(it->second.rmRCs))
619 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500620 // Return code found in sensor return code removal list
621 if (rmSensors.find(i.first) == rmSensors.end())
622 {
623 // Trace for sensor not already removed from dbus
624 log<level::INFO>(
625 "Remove sensor from dbus for read fail",
626 entry("FILE=%s", file.c_str()),
627 entry("RC=%d", e.code().value()));
628 // Mark this sensor to be removed from dbus
629 rmSensors[i.first] = std::get<0>(i.second);
630 }
Matthew Barth8772ce32018-03-22 16:03:06 -0500631 continue;
632 }
633 }
634#endif
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600635 using namespace sdbusplus::xyz::openbmc_project::
636 Sensor::Device::Error;
637 report<ReadFailure>(
638 xyz::openbmc_project::Sensor::Device::
639 ReadFailure::CALLOUT_ERRNO(e.code().value()),
640 xyz::openbmc_project::Sensor::Device::
641 ReadFailure::CALLOUT_DEVICE_PATH(
642 _devPath.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500643
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600644 log<level::INFO>("Logging failing sysfs file",
645 entry("FILE=%s", file.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500646
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500647#ifdef REMOVE_ON_FAIL
Matthew Barth33db92b2018-03-26 09:55:19 -0500648 rmSensors[i.first] = std::get<0>(i.second);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500649#else
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600650 exit(EXIT_FAILURE);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500651#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500652 }
653 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600654 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500655
Matthew Barth8772ce32018-03-22 16:03:06 -0500656 // Remove any sensors marked for removal
Matthew Barth33db92b2018-03-26 09:55:19 -0500657 for (auto& i : rmSensors)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600658 {
Matthew Barth33db92b2018-03-26 09:55:19 -0500659 state.erase(i.first);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500660 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500661
662#ifndef REMOVE_ON_FAIL
663 // Attempt to add any sensors that were removed
664 auto it = rmSensors.begin();
665 while (it != rmSensors.end())
666 {
667 if (state.find(it->first) == state.end())
668 {
669 SensorSet::container_t::value_type ssValueType =
670 std::make_pair(it->first, it->second);
671 getObject(ssValueType);
672 if (state.find(it->first) != state.end())
673 {
674 // Sensor object added, erase entry from removal list
Matthew Barth38c74e72018-04-02 12:41:26 -0500675 auto file = sysfs::make_sysfs_path(
676 ioAccess.path(),
677 it->first.first,
678 it->first.second,
679 hwmon::entry::cinput);
680 log<level::INFO>(
681 "Added sensor to dbus after successful read",
682 entry("FILE=%s", file.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500683 it = rmSensors.erase(it);
684 }
685 else
686 {
687 ++it;
688 }
689 }
690 else
691 {
692 // Sanity check to remove sensors that were re-added
693 it = rmSensors.erase(it);
694 }
695 }
696#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500697}
698
699// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4