blob: d3987cc7816e290c5e77b67b69936bf7bf27fd4c [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,
Brad Bishop751043e2017-08-29 11:13:46 -0400210 const std::string& devPath,
211 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,
235 sysfs::hwmonio::retries,
236 sysfs::hwmonio::delay,
237 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{
288 //If this device supports target speeds,
289 //check which type to use.
290 targetType fanTargetType = targetType::DEFAULT;
291 auto targetMode = getenv("TARGET_MODE");
292 if (targetMode)
293 {
294 std::string type{targetMode};
295 std::transform(type.begin(), type.end(), type.begin(), toupper);
296
297 if (type == RPM_TARGET)
298 {
299 fanTargetType = targetType::RPM;
300 }
301 else if (type == PWM_TARGET)
302 {
303 fanTargetType = targetType::PWM;
304 }
305 else
306 {
307 log<level::ERR>(
308 "Invalid TARGET_MODE env var found",
309 entry("TARGET_MODE=%s", targetMode),
310 entry("DEVPATH=%s", _devPath.c_str()));
311 }
312 }
313
314 // Get list of return codes for removing sensors on device
315 std::string deviceRmRCs;
316 auto devRmRCs = getenv("REMOVERCS");
317 if (devRmRCs)
318 {
319 deviceRmRCs.assign(devRmRCs);
320 }
321
322 std::string label;
323 std::string id;
324
325 /*
326 * Check if the value of the MODE_<item><X> env variable for the sensor
327 * is "label", then read the sensor number from the <item><X>_label
328 * file. The name of the DBUS object would be the value of the env
329 * variable LABEL_<item><sensorNum>. If the MODE_<item><X> env variable
330 * doesn't exist, then the name of DBUS object is the value of the env
331 * variable LABEL_<item><X>.
332 */
333 auto mode = getEnv("MODE", sensor.first);
334 if (!mode.compare(hwmon::entry::label))
335 {
336 id = getIndirectID(
337 _hwmonRoot + '/' + _instance + '/', sensor.first);
338
339 if (id.empty())
340 {
341 return;
342 }
343 }
344
345 // Use the ID we looked up above if there was one,
346 // otherwise use the standard one.
347 id = (id.empty()) ? sensor.first.second : id;
348
349 // Ignore inputs without a label.
350 label = getEnv("LABEL", sensor.first.first, id);
351 if (label.empty())
352 {
353 return;
354 }
355
356 Attributes attrs;
357 if (!getAttributes(sensor.first.first, attrs))
358 {
359 return;
360 }
361
362 if (!deviceRmRCs.empty())
363 {
364 // Add sensor removal return codes defined at the device level
365 addRemoveRCs(sensor.first, deviceRmRCs);
366 }
367
368 std::string objectPath{_root};
369 objectPath.append(1, '/');
370 objectPath.append(getNamespace(attrs));
371 objectPath.append(1, '/');
372 objectPath.append(label);
373
374 ObjectInfo info(&_bus, std::move(objectPath), Object());
375 auto valueInterface = static_cast<
376 std::shared_ptr<ValueObject>>(nullptr);
377 try
378 {
379 valueInterface = addValue(sensor.first, _devPath, ioAccess, info,
380 _isOCC);
381 }
382 catch (const std::system_error& e)
383 {
384#ifndef REMOVE_ON_FAIL
385 // Check sensorAdjusts for sensor removal RCs
386 const auto& it = sensorAdjusts.find(sensor.first);
387 if (it != sensorAdjusts.end())
388 {
389 auto rmRCit = it->second.rmRCs.find(e.code().value());
390 if (rmRCit != std::end(it->second.rmRCs))
391 {
392 // Return code found in sensor removal list
393 // Skip adding this sensor for now
394 rmSensors[std::move(sensor.first)] = std::move(sensor.second);
395 return;
396 }
397 }
398#endif
399 using namespace sdbusplus::xyz::openbmc_project::
400 Sensor::Device::Error;
401 report<ReadFailure>(
402 xyz::openbmc_project::Sensor::Device::
403 ReadFailure::CALLOUT_ERRNO(e.code().value()),
404 xyz::openbmc_project::Sensor::Device::
405 ReadFailure::CALLOUT_DEVICE_PATH(_devPath.c_str()));
406
407 auto file = sysfs::make_sysfs_path(
408 ioAccess.path(),
409 sensor.first.first,
410 sensor.first.second,
411 hwmon::entry::cinput);
412
413 log<level::INFO>("Logging failing sysfs file",
414 entry("FILE=%s", file.c_str()));
415#ifdef REMOVE_ON_FAIL
416 return; /* skip adding this sensor for now. */
417#else
418 exit(EXIT_FAILURE);
419#endif
420 }
421 auto sensorValue = valueInterface->value();
422 addThreshold<WarningObject>(sensor.first.first, id, sensorValue, info);
423 addThreshold<CriticalObject>(sensor.first.first, id, sensorValue, info);
424
425 if ((fanTargetType == targetType::RPM) ||
426 (fanTargetType == targetType::DEFAULT))
427 {
428 auto target = addTarget<hwmon::FanSpeed>(
429 sensor.first, ioAccess, _devPath, info);
430
431 if (target)
432 {
433 target->enable();
434 }
435 }
436
437 if ((fanTargetType == targetType::PWM) ||
438 (fanTargetType == targetType::DEFAULT))
439 {
440 addTarget<hwmon::FanPwm>(sensor.first, ioAccess, _devPath, info);
441 }
442
443 // All the interfaces have been created. Go ahead
444 // and emit InterfacesAdded.
445 valueInterface->emit_object_added();
446
447 auto value = std::make_tuple(
448 std::move(sensor.second),
449 std::move(label),
450 std::move(info));
451
452 state[std::move(sensor.first)] = std::move(value);
453}
454
Brad Bishopb9e2b072016-12-19 13:47:10 -0500455MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500456 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500457 const std::string& path,
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400458 const std::string& devPath,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500459 const char* prefix,
460 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500461 : _bus(std::move(bus)),
Brad Bishop03e87352017-03-07 00:12:22 -0500462 _manager(_bus, root),
Brad Bishopb8740fc2017-02-24 23:38:37 -0500463 _hwmonRoot(),
464 _instance(),
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400465 _devPath(devPath),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500466 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500467 _root(root),
Brad Bishop751043e2017-08-29 11:13:46 -0400468 state(),
469 ioAccess(path)
Brad Bishopd499ca62016-12-19 09:24:50 -0500470{
Matthew Bartha23babd2018-03-16 10:03:27 -0500471 if (path.find("occ") != std::string::npos)
472 {
473 _isOCC = true;
474 }
475
Brad Bishopb8740fc2017-02-24 23:38:37 -0500476 std::string p = path;
477 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500478 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500479 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500480 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500481
482 auto n = p.rfind('/');
483 if (n != std::string::npos)
484 {
485 _instance.assign(p.substr(n + 1));
486 _hwmonRoot.assign(p.substr(0, n));
487 }
488
489 assert(!_instance.empty());
490 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500491}
492
493void MainLoop::shutdown() noexcept
494{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600495 timer->state(phosphor::hwmon::timer::OFF);
496 sd_event_exit(loop, 0);
497 loop = nullptr;
Brad Bishopd499ca62016-12-19 09:24:50 -0500498}
499
500void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500501{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600502 init();
503
504 sd_event_default(&loop);
505
506 std::function<void()> callback(std::bind(
507 &MainLoop::read, this));
508 try
509 {
510 timer = std::make_unique<phosphor::hwmon::Timer>(
511 loop, callback,
512 std::chrono::microseconds(_interval),
513 phosphor::hwmon::timer::ON);
514
515 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
516
517 // TODO: Issue#7 - Should probably periodically check the SensorSet
518 // for new entries.
519
520 _bus.attach_event(loop, SD_EVENT_PRIORITY_IMPORTANT);
521 sd_event_loop(loop);
522 }
523 catch (const std::system_error& e)
524 {
525 log<level::ERR>("Error in sysfs polling loop",
526 entry("ERROR=%s", e.what()));
527 throw;
528 }
529}
530
531void MainLoop::init()
532{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500533 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500534 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500535
Brad Bishop75b4ab82017-01-06 09:33:50 -0500536 for (auto& i : *sensors)
537 {
Matthew Barth31d214c2018-03-26 09:54:27 -0500538 getObject(i);
Brad Bishop75b4ab82017-01-06 09:33:50 -0500539 }
540
Patrick Venture62503a42017-05-23 07:30:29 -0700541 /* If there are no sensors specified by labels, exit. */
542 if (0 == state.size())
543 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600544 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700545 }
546
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500547 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500548 std::string busname{_prefix};
Brad Bishop4d9a35b2017-11-14 22:33:03 -0500549 busname.append(1, '-');
550 busname.append(
551 std::to_string(std::hash<decltype(_devPath)>{}(_devPath)));
552 busname.append(".Hwmon1");
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500553 _bus.request_name(busname.c_str());
554 }
555
Patrick Ventureab10f162017-05-22 09:44:50 -0700556 {
557 auto interval = getenv("INTERVAL");
558 if (interval)
559 {
560 _interval = strtoull(interval, NULL, 10);
561 }
562 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600563}
Patrick Ventureab10f162017-05-22 09:44:50 -0700564
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600565void MainLoop::read()
566{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500567 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
568 // ensure the objects all exist?
569
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600570 // Iterate through all the sensors.
571 for (auto& i : state)
572 {
573 auto& attrs = std::get<0>(i.second);
574 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500575 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600576 // Read value from sensor.
577 int64_t value;
578 std::string input = hwmon::entry::cinput;
579 if (i.first.first == "pwm") {
580 input = "";
581 }
582
583 try
Brad Bishope55ef3d2016-12-19 09:12:40 -0500584 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600585 // Retry for up to a second if device is busy
586 // or has a transient error.
Patrick Venture9331ab72018-01-29 09:48:47 -0800587
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600588 value = ioAccess.read(
589 i.first.first,
590 i.first.second,
591 input,
592 sysfs::hwmonio::retries,
593 sysfs::hwmonio::delay,
594 _isOCC);
595
596 value = adjustValue(i.first, value);
597
598 auto& objInfo = std::get<ObjectInfo>(i.second);
599 auto& obj = std::get<Object>(objInfo);
600
601 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500602 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600603 auto valueIface = std::shared_ptr<ValueObject>();
604 auto warnIface = std::shared_ptr<WarningObject>();
605 auto critIface = std::shared_ptr<CriticalObject>();
Brad Bishop754d38c2017-09-08 00:46:58 -0400606
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600607 switch (iface.first)
Brad Bishope0b7d052017-01-06 15:30:23 -0500608 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600609 case InterfaceType::VALUE:
610 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
611 (iface.second);
612 valueIface->value(value);
613 break;
614 case InterfaceType::WARN:
615 checkThresholds<WarningObject>(iface.second, value);
616 break;
617 case InterfaceType::CRIT:
618 checkThresholds<CriticalObject>(iface.second, value);
619 break;
620 default:
621 break;
Brad Bishope0b7d052017-01-06 15:30:23 -0500622 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500623 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600624 }
625 catch (const std::system_error& e)
626 {
Matthew Barth8772ce32018-03-22 16:03:06 -0500627#ifndef REMOVE_ON_FAIL
628 // Check sensorAdjusts for sensor removal RCs
629 const auto& it = sensorAdjusts.find(i.first);
630 if (it != sensorAdjusts.end())
631 {
632 auto rmRCit = it->second.rmRCs.find(e.code().value());
633 if (rmRCit != std::end(it->second.rmRCs))
634 {
635 // Return code found in sensor removal list
636 // Mark this sensor to be removed from dbus
Matthew Barth33db92b2018-03-26 09:55:19 -0500637 rmSensors[i.first] = std::get<0>(i.second);
Matthew Barth8772ce32018-03-22 16:03:06 -0500638 continue;
639 }
640 }
641#endif
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600642 using namespace sdbusplus::xyz::openbmc_project::
643 Sensor::Device::Error;
644 report<ReadFailure>(
645 xyz::openbmc_project::Sensor::Device::
646 ReadFailure::CALLOUT_ERRNO(e.code().value()),
647 xyz::openbmc_project::Sensor::Device::
648 ReadFailure::CALLOUT_DEVICE_PATH(
649 _devPath.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500650
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600651 auto file = sysfs::make_sysfs_path(
652 ioAccess.path(),
653 i.first.first,
654 i.first.second,
655 hwmon::entry::cinput);
Matt Spinler9b65f762017-10-05 10:36:22 -0500656
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600657 log<level::INFO>("Logging failing sysfs file",
658 entry("FILE=%s", file.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500659
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500660#ifdef REMOVE_ON_FAIL
Matthew Barth33db92b2018-03-26 09:55:19 -0500661 rmSensors[i.first] = std::get<0>(i.second);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500662#else
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600663 exit(EXIT_FAILURE);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500664#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500665 }
666 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600667 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500668
Matthew Barth8772ce32018-03-22 16:03:06 -0500669 // Remove any sensors marked for removal
Matthew Barth33db92b2018-03-26 09:55:19 -0500670 for (auto& i : rmSensors)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600671 {
Matthew Barth33db92b2018-03-26 09:55:19 -0500672 state.erase(i.first);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500673 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500674
675#ifndef REMOVE_ON_FAIL
676 // Attempt to add any sensors that were removed
677 auto it = rmSensors.begin();
678 while (it != rmSensors.end())
679 {
680 if (state.find(it->first) == state.end())
681 {
682 SensorSet::container_t::value_type ssValueType =
683 std::make_pair(it->first, it->second);
684 getObject(ssValueType);
685 if (state.find(it->first) != state.end())
686 {
687 // Sensor object added, erase entry from removal list
688 it = rmSensors.erase(it);
689 }
690 else
691 {
692 ++it;
693 }
694 }
695 else
696 {
697 // Sanity check to remove sensors that were re-added
698 it = rmSensors.erase(it);
699 }
700 }
701#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500702}
703
704// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4