blob: aba8cecf562456bb9cf2537aef3d3f5ec105dbbf [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
Brad Bishopb9e2b072016-12-19 13:47:10 -0500278MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500279 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500280 const std::string& path,
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400281 const std::string& devPath,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500282 const char* prefix,
283 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500284 : _bus(std::move(bus)),
Brad Bishop03e87352017-03-07 00:12:22 -0500285 _manager(_bus, root),
Brad Bishopb8740fc2017-02-24 23:38:37 -0500286 _hwmonRoot(),
287 _instance(),
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400288 _devPath(devPath),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500289 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500290 _root(root),
Brad Bishop751043e2017-08-29 11:13:46 -0400291 state(),
292 ioAccess(path)
Brad Bishopd499ca62016-12-19 09:24:50 -0500293{
Matthew Bartha23babd2018-03-16 10:03:27 -0500294 if (path.find("occ") != std::string::npos)
295 {
296 _isOCC = true;
297 }
298
Brad Bishopb8740fc2017-02-24 23:38:37 -0500299 std::string p = path;
300 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500301 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500302 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500303 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500304
305 auto n = p.rfind('/');
306 if (n != std::string::npos)
307 {
308 _instance.assign(p.substr(n + 1));
309 _hwmonRoot.assign(p.substr(0, n));
310 }
311
312 assert(!_instance.empty());
313 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500314}
315
316void MainLoop::shutdown() noexcept
317{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600318 timer->state(phosphor::hwmon::timer::OFF);
319 sd_event_exit(loop, 0);
320 loop = nullptr;
Brad Bishopd499ca62016-12-19 09:24:50 -0500321}
322
323void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500324{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600325 init();
326
327 sd_event_default(&loop);
328
329 std::function<void()> callback(std::bind(
330 &MainLoop::read, this));
331 try
332 {
333 timer = std::make_unique<phosphor::hwmon::Timer>(
334 loop, callback,
335 std::chrono::microseconds(_interval),
336 phosphor::hwmon::timer::ON);
337
338 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
339
340 // TODO: Issue#7 - Should probably periodically check the SensorSet
341 // for new entries.
342
343 _bus.attach_event(loop, SD_EVENT_PRIORITY_IMPORTANT);
344 sd_event_loop(loop);
345 }
346 catch (const std::system_error& e)
347 {
348 log<level::ERR>("Error in sysfs polling loop",
349 entry("ERROR=%s", e.what()));
350 throw;
351 }
352}
353
354void MainLoop::init()
355{
Matt Spinlera7e2c1e2018-02-23 12:18:19 -0600356 //If this device supports target speeds,
357 //check which type to use.
358 targetType fanTargetType = targetType::DEFAULT;
359 auto targetMode = getenv("TARGET_MODE");
360 if (targetMode)
361 {
362 std::string type{targetMode};
363 std::transform(type.begin(), type.end(), type.begin(), toupper);
364
365 if (type == RPM_TARGET)
366 {
367 fanTargetType = targetType::RPM;
368 }
369 else if (type == PWM_TARGET)
370 {
371 fanTargetType = targetType::PWM;
372 }
373 else
374 {
375 log<level::ERR>(
376 "Invalid TARGET_MODE env var found",
377 entry("TARGET_MODE=%s", targetMode),
378 entry("DEVPATH=%s", _devPath.c_str()));
379 }
380 }
381
Matthew Barthd26e2772018-03-22 14:24:06 -0500382 // Get list of return codes for removing sensors on device
383 std::string deviceRmRCs;
384 auto devRmRCs = getenv("REMOVERCS");
385 if (devRmRCs)
386 {
387 deviceRmRCs.assign(devRmRCs);
388 }
389
Brad Bishope55ef3d2016-12-19 09:12:40 -0500390 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500391 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500392
Brad Bishop75b4ab82017-01-06 09:33:50 -0500393 for (auto& i : *sensors)
394 {
Tom Joseph1f8a9582017-06-12 20:10:59 +0530395 std::string label;
Matt Spinler4b68c4e2017-10-12 16:44:53 -0500396 std::string id;
Brad Bishopf3df6b42017-01-06 10:14:09 -0500397
Tom Joseph1f8a9582017-06-12 20:10:59 +0530398 /*
399 * Check if the value of the MODE_<item><X> env variable for the sensor
400 * is "label", then read the sensor number from the <item><X>_label
401 * file. The name of the DBUS object would be the value of the env
402 * variable LABEL_<item><sensorNum>. If the MODE_<item><X> env variable
Matt Spinler4b68c4e2017-10-12 16:44:53 -0500403 * doesn't exist, then the name of DBUS object is the value of the env
Tom Joseph1f8a9582017-06-12 20:10:59 +0530404 * variable LABEL_<item><X>.
405 */
406 auto mode = getEnv("MODE", i.first);
407 if (!mode.compare(hwmon::entry::label))
Brad Bishop73831cd2017-01-06 09:37:22 -0500408 {
Matt Spinler4b68c4e2017-10-12 16:44:53 -0500409 id = getIndirectID(
410 _hwmonRoot + '/' + _instance + '/', i.first);
411
412 if (id.empty())
Tom Joseph1f8a9582017-06-12 20:10:59 +0530413 {
414 continue;
415 }
416 }
Matt Spinler4b68c4e2017-10-12 16:44:53 -0500417
418 //In this loop, use the ID we looked up above if
419 //there was one, otherwise use the standard one.
420 id = (id.empty()) ? i.first.second : id;
421
422 // Ignore inputs without a label.
423 label = getEnv("LABEL", i.first.first, id);
424 if (label.empty())
Tom Joseph1f8a9582017-06-12 20:10:59 +0530425 {
Matt Spinler4b68c4e2017-10-12 16:44:53 -0500426 continue;
Brad Bishop73831cd2017-01-06 09:37:22 -0500427 }
428
Brad Bishopadd98512017-01-06 22:01:19 -0500429 Attributes attrs;
430 if (!getAttributes(i.first.first, attrs))
431 {
432 continue;
433 }
434
Matthew Barthd26e2772018-03-22 14:24:06 -0500435 if (!deviceRmRCs.empty())
436 {
437 // Add sensor removal return codes defined at the device level
438 addRemoveRCs(i.first, deviceRmRCs);
439 }
440
Brad Bishop075f7a22017-01-06 09:45:08 -0500441 std::string objectPath{_root};
Brad Bishopb8740fc2017-02-24 23:38:37 -0500442 objectPath.append(1, '/');
Brad Bishopadd98512017-01-06 22:01:19 -0500443 objectPath.append(getNamespace(attrs));
Brad Bishopb8740fc2017-02-24 23:38:37 -0500444 objectPath.append(1, '/');
Brad Bishop075f7a22017-01-06 09:45:08 -0500445 objectPath.append(label);
446
Brad Bishopf7426cf2017-01-06 15:36:43 -0500447 ObjectInfo info(&_bus, std::move(objectPath), Object());
Matthew Barth8772ce32018-03-22 16:03:06 -0500448 auto valueInterface = static_cast<
449 std::shared_ptr<ValueObject>>(nullptr);
450 try
Patrick Venture1e6324f2017-06-01 14:07:05 -0700451 {
Matthew Barth8772ce32018-03-22 16:03:06 -0500452 valueInterface = addValue(i.first, _devPath, ioAccess, info,
453 _isOCC);
454 }
455 catch (const std::system_error& e)
456 {
457#ifndef REMOVE_ON_FAIL
458 // Check sensorAdjusts for sensor removal RCs
459 const auto& it = sensorAdjusts.find(i.first);
460 if (it != sensorAdjusts.end())
461 {
462 auto rmRCit = it->second.rmRCs.find(e.code().value());
463 if (rmRCit != std::end(it->second.rmRCs))
464 {
465 // Return code found in sensor removal list
466 // Skip adding this sensor for now
467 continue;
468 }
469 }
470#endif
471 using namespace sdbusplus::xyz::openbmc_project::
472 Sensor::Device::Error;
473 report<ReadFailure>(
474 xyz::openbmc_project::Sensor::Device::
475 ReadFailure::CALLOUT_ERRNO(e.code().value()),
476 xyz::openbmc_project::Sensor::Device::
477 ReadFailure::CALLOUT_DEVICE_PATH(_devPath.c_str()));
478
479 auto file = sysfs::make_sysfs_path(
480 ioAccess.path(),
481 i.first.first,
482 i.first.second,
483 hwmon::entry::cinput);
484
485 log<level::INFO>("Logging failing sysfs file",
486 entry("FILE=%s", file.c_str()));
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500487#ifdef REMOVE_ON_FAIL
Patrick Venture1e6324f2017-06-01 14:07:05 -0700488 continue; /* skip adding this sensor for now. */
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500489#else
490 exit(EXIT_FAILURE);
491#endif
Patrick Venture1e6324f2017-06-01 14:07:05 -0700492 }
Brad Bishope0b7d052017-01-06 15:30:23 -0500493 auto sensorValue = valueInterface->value();
Matt Spinlerccfc77b2017-10-12 16:49:24 -0500494 addThreshold<WarningObject>(i.first.first, id, sensorValue, info);
495 addThreshold<CriticalObject>(i.first.first, id, sensorValue, info);
Matt Spinlera3eb8e32017-10-12 16:42:49 -0500496
Matt Spinlera7e2c1e2018-02-23 12:18:19 -0600497 if ((fanTargetType == targetType::RPM) ||
498 (fanTargetType == targetType::DEFAULT))
Matt Spinler0a8de642017-05-11 10:59:39 -0500499 {
Matt Spinlera7e2c1e2018-02-23 12:18:19 -0600500 auto target = addTarget<hwmon::FanSpeed>(
501 i.first, ioAccess, _devPath, info);
502
503 if (target)
504 {
505 target->enable();
506 }
Matt Spinler0a8de642017-05-11 10:59:39 -0500507 }
Brad Bishop075f7a22017-01-06 09:45:08 -0500508
Matt Spinlera7e2c1e2018-02-23 12:18:19 -0600509 if ((fanTargetType == targetType::PWM) ||
510 (fanTargetType == targetType::DEFAULT))
511 {
512 addTarget<hwmon::FanPwm>(i.first, ioAccess, _devPath, info);
513 }
Patrick Venture9331ab72018-01-29 09:48:47 -0800514
Brad Bishop30dbcee2017-01-18 07:55:42 -0500515 // All the interfaces have been created. Go ahead
516 // and emit InterfacesAdded.
517 valueInterface->emit_object_added();
518
Brad Bishop075f7a22017-01-06 09:45:08 -0500519 auto value = std::make_tuple(
520 std::move(i.second),
521 std::move(label),
Brad Bishopf7426cf2017-01-06 15:36:43 -0500522 std::move(info));
Brad Bishop73831cd2017-01-06 09:37:22 -0500523
Brad Bishop75b4ab82017-01-06 09:33:50 -0500524 state[std::move(i.first)] = std::move(value);
525 }
526
Patrick Venture62503a42017-05-23 07:30:29 -0700527 /* If there are no sensors specified by labels, exit. */
528 if (0 == state.size())
529 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600530 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700531 }
532
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500533 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500534 std::string busname{_prefix};
Brad Bishop4d9a35b2017-11-14 22:33:03 -0500535 busname.append(1, '-');
536 busname.append(
537 std::to_string(std::hash<decltype(_devPath)>{}(_devPath)));
538 busname.append(".Hwmon1");
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500539 _bus.request_name(busname.c_str());
540 }
541
Patrick Ventureab10f162017-05-22 09:44:50 -0700542 {
543 auto interval = getenv("INTERVAL");
544 if (interval)
545 {
546 _interval = strtoull(interval, NULL, 10);
547 }
548 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600549}
Patrick Ventureab10f162017-05-22 09:44:50 -0700550
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600551void MainLoop::read()
552{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500553 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
554 // ensure the objects all exist?
555
Matthew Barth8772ce32018-03-22 16:03:06 -0500556 // Used in marking a sensor for removal from dbus
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600557 std::vector<SensorSet::key_type> destroy;
Matthew Barth8772ce32018-03-22 16:03:06 -0500558
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600559 // Iterate through all the sensors.
560 for (auto& i : state)
561 {
562 auto& attrs = std::get<0>(i.second);
563 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500564 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600565 // Read value from sensor.
566 int64_t value;
567 std::string input = hwmon::entry::cinput;
568 if (i.first.first == "pwm") {
569 input = "";
570 }
571
572 try
Brad Bishope55ef3d2016-12-19 09:12:40 -0500573 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600574 // Retry for up to a second if device is busy
575 // or has a transient error.
Patrick Venture9331ab72018-01-29 09:48:47 -0800576
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600577 value = ioAccess.read(
578 i.first.first,
579 i.first.second,
580 input,
581 sysfs::hwmonio::retries,
582 sysfs::hwmonio::delay,
583 _isOCC);
584
585 value = adjustValue(i.first, value);
586
587 auto& objInfo = std::get<ObjectInfo>(i.second);
588 auto& obj = std::get<Object>(objInfo);
589
590 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500591 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600592 auto valueIface = std::shared_ptr<ValueObject>();
593 auto warnIface = std::shared_ptr<WarningObject>();
594 auto critIface = std::shared_ptr<CriticalObject>();
Brad Bishop754d38c2017-09-08 00:46:58 -0400595
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600596 switch (iface.first)
Brad Bishope0b7d052017-01-06 15:30:23 -0500597 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600598 case InterfaceType::VALUE:
599 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
600 (iface.second);
601 valueIface->value(value);
602 break;
603 case InterfaceType::WARN:
604 checkThresholds<WarningObject>(iface.second, value);
605 break;
606 case InterfaceType::CRIT:
607 checkThresholds<CriticalObject>(iface.second, value);
608 break;
609 default:
610 break;
Brad Bishope0b7d052017-01-06 15:30:23 -0500611 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500612 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600613 }
614 catch (const std::system_error& e)
615 {
Matthew Barth8772ce32018-03-22 16:03:06 -0500616#ifndef REMOVE_ON_FAIL
617 // Check sensorAdjusts for sensor removal RCs
618 const auto& it = sensorAdjusts.find(i.first);
619 if (it != sensorAdjusts.end())
620 {
621 auto rmRCit = it->second.rmRCs.find(e.code().value());
622 if (rmRCit != std::end(it->second.rmRCs))
623 {
624 // Return code found in sensor removal list
625 // Mark this sensor to be removed from dbus
626 destroy.push_back(i.first);
627 continue;
628 }
629 }
630#endif
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600631 using namespace sdbusplus::xyz::openbmc_project::
632 Sensor::Device::Error;
633 report<ReadFailure>(
634 xyz::openbmc_project::Sensor::Device::
635 ReadFailure::CALLOUT_ERRNO(e.code().value()),
636 xyz::openbmc_project::Sensor::Device::
637 ReadFailure::CALLOUT_DEVICE_PATH(
638 _devPath.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500639
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600640 auto file = sysfs::make_sysfs_path(
641 ioAccess.path(),
642 i.first.first,
643 i.first.second,
644 hwmon::entry::cinput);
Matt Spinler9b65f762017-10-05 10:36:22 -0500645
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600646 log<level::INFO>("Logging failing sysfs file",
647 entry("FILE=%s", file.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500648
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500649#ifdef REMOVE_ON_FAIL
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600650 destroy.push_back(i.first);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500651#else
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600652 exit(EXIT_FAILURE);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500653#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500654 }
655 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600656 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500657
Matthew Barth8772ce32018-03-22 16:03:06 -0500658 // Remove any sensors marked for removal
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600659 for (auto& i : destroy)
660 {
661 state.erase(i);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500662 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500663}
664
665// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4