blob: d33ea44c21f29d8ef97eb6835745b3560f1468ee [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
Matt Spinlerfee106b2017-11-29 15:18:05 -0600229 int64_t val = 0;
Brad Bishop751043e2017-08-29 11:13:46 -0400230 try
231 {
232 // Retry for up to a second if device is busy
Brad Bishop754d38c2017-09-08 00:46:58 -0400233 // or has a transient error.
234 val = ioAccess.read(
Brad Bishop751043e2017-08-29 11:13:46 -0400235 sensor.first,
236 sensor.second,
Brad Bishop754d38c2017-09-08 00:46:58 -0400237 hwmon::entry::cinput,
238 sysfs::hwmonio::retries,
Matthew Bartha23babd2018-03-16 10:03:27 -0500239 sysfs::hwmonio::delay,
240 isOCC);
Brad Bishop751043e2017-08-29 11:13:46 -0400241 }
242 catch (const std::system_error& e)
243 {
244 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error;
245 report<ReadFailure>(
246 xyz::openbmc_project::Sensor::Device::
247 ReadFailure::CALLOUT_ERRNO(e.code().value()),
248 xyz::openbmc_project::Sensor::Device::
249 ReadFailure::CALLOUT_DEVICE_PATH(devPath.c_str()));
250
Matt Spinler9b65f762017-10-05 10:36:22 -0500251 auto file = sysfs::make_sysfs_path(
252 ioAccess.path(),
253 sensor.first,
254 sensor.second,
255 hwmon::entry::cinput);
256
257 log<level::INFO>("Logging failing sysfs file",
258 entry("FILE=%s", file.c_str()));
259
Brad Bishop751043e2017-08-29 11:13:46 -0400260 return static_cast<std::shared_ptr<ValueObject>>(nullptr);
Patrick Venture1e6324f2017-06-01 14:07:05 -0700261 }
262
Chiabing Leec923ce92017-09-06 15:58:26 +0800263 auto gain = getEnv("GAIN", sensor);
264 if (!gain.empty())
265 {
266 sensorAdjusts[sensor].gain = std::stod(gain);
267 }
268
269 auto offset = getEnv("OFFSET", sensor);
270 if (!offset.empty())
271 {
272 sensorAdjusts[sensor].offset = std::stoi(offset);
273 }
274
275 val = adjustValue(sensor, val);
276
Brad Bishop30dbcee2017-01-18 07:55:42 -0500277 auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals);
Brad Bishope9fdee02017-01-06 10:43:29 -0500278 iface->value(val);
279
Brad Bishop951a79e2017-01-06 21:55:11 -0500280 Attributes attrs;
281 if (getAttributes(sensor.first, attrs))
Brad Bishope9fdee02017-01-06 10:43:29 -0500282 {
Brad Bishop951a79e2017-01-06 21:55:11 -0500283 iface->unit(getUnit(attrs));
284 iface->scale(getScale(attrs));
Brad Bishope9fdee02017-01-06 10:43:29 -0500285 }
286
James Feist7bd5fcf2018-01-22 16:14:28 -0800287 auto maxValue = getEnv("MAXVALUE", sensor);
288 if(!maxValue.empty())
289 {
290 iface->maxValue(std::stoll(maxValue));
291 }
292 auto minValue = getEnv("MINVALUE", sensor);
293 if(!minValue.empty())
294 {
295 iface->minValue(std::stoll(minValue));
296 }
297
Brad Bishope9fdee02017-01-06 10:43:29 -0500298 obj[InterfaceType::VALUE] = iface;
299 return iface;
300}
301
Brad Bishopb9e2b072016-12-19 13:47:10 -0500302MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500303 sdbusplus::bus::bus&& bus,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500304 const std::string& path,
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400305 const std::string& devPath,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500306 const char* prefix,
307 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500308 : _bus(std::move(bus)),
Brad Bishop03e87352017-03-07 00:12:22 -0500309 _manager(_bus, root),
Brad Bishopb8740fc2017-02-24 23:38:37 -0500310 _hwmonRoot(),
311 _instance(),
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400312 _devPath(devPath),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500313 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500314 _root(root),
Brad Bishop751043e2017-08-29 11:13:46 -0400315 state(),
316 ioAccess(path)
Brad Bishopd499ca62016-12-19 09:24:50 -0500317{
Matthew Bartha23babd2018-03-16 10:03:27 -0500318 if (path.find("occ") != std::string::npos)
319 {
320 _isOCC = true;
321 }
322
Brad Bishopb8740fc2017-02-24 23:38:37 -0500323 std::string p = path;
324 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500325 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500326 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500327 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500328
329 auto n = p.rfind('/');
330 if (n != std::string::npos)
331 {
332 _instance.assign(p.substr(n + 1));
333 _hwmonRoot.assign(p.substr(0, n));
334 }
335
336 assert(!_instance.empty());
337 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500338}
339
340void MainLoop::shutdown() noexcept
341{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600342 timer->state(phosphor::hwmon::timer::OFF);
343 sd_event_exit(loop, 0);
344 loop = nullptr;
Brad Bishopd499ca62016-12-19 09:24:50 -0500345}
346
347void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500348{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600349 init();
350
351 sd_event_default(&loop);
352
353 std::function<void()> callback(std::bind(
354 &MainLoop::read, this));
355 try
356 {
357 timer = std::make_unique<phosphor::hwmon::Timer>(
358 loop, callback,
359 std::chrono::microseconds(_interval),
360 phosphor::hwmon::timer::ON);
361
362 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
363
364 // TODO: Issue#7 - Should probably periodically check the SensorSet
365 // for new entries.
366
367 _bus.attach_event(loop, SD_EVENT_PRIORITY_IMPORTANT);
368 sd_event_loop(loop);
369 }
370 catch (const std::system_error& e)
371 {
372 log<level::ERR>("Error in sysfs polling loop",
373 entry("ERROR=%s", e.what()));
374 throw;
375 }
376}
377
378void MainLoop::init()
379{
Matt Spinlera7e2c1e2018-02-23 12:18:19 -0600380 //If this device supports target speeds,
381 //check which type to use.
382 targetType fanTargetType = targetType::DEFAULT;
383 auto targetMode = getenv("TARGET_MODE");
384 if (targetMode)
385 {
386 std::string type{targetMode};
387 std::transform(type.begin(), type.end(), type.begin(), toupper);
388
389 if (type == RPM_TARGET)
390 {
391 fanTargetType = targetType::RPM;
392 }
393 else if (type == PWM_TARGET)
394 {
395 fanTargetType = targetType::PWM;
396 }
397 else
398 {
399 log<level::ERR>(
400 "Invalid TARGET_MODE env var found",
401 entry("TARGET_MODE=%s", targetMode),
402 entry("DEVPATH=%s", _devPath.c_str()));
403 }
404 }
405
Matthew Barthd26e2772018-03-22 14:24:06 -0500406 // Get list of return codes for removing sensors on device
407 std::string deviceRmRCs;
408 auto devRmRCs = getenv("REMOVERCS");
409 if (devRmRCs)
410 {
411 deviceRmRCs.assign(devRmRCs);
412 }
413
Brad Bishope55ef3d2016-12-19 09:12:40 -0500414 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500415 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500416
Brad Bishop75b4ab82017-01-06 09:33:50 -0500417 for (auto& i : *sensors)
418 {
Tom Joseph1f8a9582017-06-12 20:10:59 +0530419 std::string label;
Matt Spinler4b68c4e2017-10-12 16:44:53 -0500420 std::string id;
Brad Bishopf3df6b42017-01-06 10:14:09 -0500421
Tom Joseph1f8a9582017-06-12 20:10:59 +0530422 /*
423 * Check if the value of the MODE_<item><X> env variable for the sensor
424 * is "label", then read the sensor number from the <item><X>_label
425 * file. The name of the DBUS object would be the value of the env
426 * variable LABEL_<item><sensorNum>. If the MODE_<item><X> env variable
Matt Spinler4b68c4e2017-10-12 16:44:53 -0500427 * doesn't exist, then the name of DBUS object is the value of the env
Tom Joseph1f8a9582017-06-12 20:10:59 +0530428 * variable LABEL_<item><X>.
429 */
430 auto mode = getEnv("MODE", i.first);
431 if (!mode.compare(hwmon::entry::label))
Brad Bishop73831cd2017-01-06 09:37:22 -0500432 {
Matt Spinler4b68c4e2017-10-12 16:44:53 -0500433 id = getIndirectID(
434 _hwmonRoot + '/' + _instance + '/', i.first);
435
436 if (id.empty())
Tom Joseph1f8a9582017-06-12 20:10:59 +0530437 {
438 continue;
439 }
440 }
Matt Spinler4b68c4e2017-10-12 16:44:53 -0500441
442 //In this loop, use the ID we looked up above if
443 //there was one, otherwise use the standard one.
444 id = (id.empty()) ? i.first.second : id;
445
446 // Ignore inputs without a label.
447 label = getEnv("LABEL", i.first.first, id);
448 if (label.empty())
Tom Joseph1f8a9582017-06-12 20:10:59 +0530449 {
Matt Spinler4b68c4e2017-10-12 16:44:53 -0500450 continue;
Brad Bishop73831cd2017-01-06 09:37:22 -0500451 }
452
Brad Bishopadd98512017-01-06 22:01:19 -0500453 Attributes attrs;
454 if (!getAttributes(i.first.first, attrs))
455 {
456 continue;
457 }
458
Matthew Barthd26e2772018-03-22 14:24:06 -0500459 if (!deviceRmRCs.empty())
460 {
461 // Add sensor removal return codes defined at the device level
462 addRemoveRCs(i.first, deviceRmRCs);
463 }
464
Brad Bishop075f7a22017-01-06 09:45:08 -0500465 std::string objectPath{_root};
Brad Bishopb8740fc2017-02-24 23:38:37 -0500466 objectPath.append(1, '/');
Brad Bishopadd98512017-01-06 22:01:19 -0500467 objectPath.append(getNamespace(attrs));
Brad Bishopb8740fc2017-02-24 23:38:37 -0500468 objectPath.append(1, '/');
Brad Bishop075f7a22017-01-06 09:45:08 -0500469 objectPath.append(label);
470
Brad Bishopf7426cf2017-01-06 15:36:43 -0500471 ObjectInfo info(&_bus, std::move(objectPath), Object());
Matthew Bartha23babd2018-03-16 10:03:27 -0500472 auto valueInterface = addValue(i.first, _devPath, ioAccess, info,
473 _isOCC);
Patrick Venture1e6324f2017-06-01 14:07:05 -0700474 if (!valueInterface)
475 {
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500476#ifdef REMOVE_ON_FAIL
Patrick Venture1e6324f2017-06-01 14:07:05 -0700477 continue; /* skip adding this sensor for now. */
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500478#else
479 exit(EXIT_FAILURE);
480#endif
Patrick Venture1e6324f2017-06-01 14:07:05 -0700481 }
Brad Bishope0b7d052017-01-06 15:30:23 -0500482 auto sensorValue = valueInterface->value();
Matt Spinlerccfc77b2017-10-12 16:49:24 -0500483 addThreshold<WarningObject>(i.first.first, id, sensorValue, info);
484 addThreshold<CriticalObject>(i.first.first, id, sensorValue, info);
Matt Spinlera3eb8e32017-10-12 16:42:49 -0500485
Matt Spinlera7e2c1e2018-02-23 12:18:19 -0600486 if ((fanTargetType == targetType::RPM) ||
487 (fanTargetType == targetType::DEFAULT))
Matt Spinler0a8de642017-05-11 10:59:39 -0500488 {
Matt Spinlera7e2c1e2018-02-23 12:18:19 -0600489 auto target = addTarget<hwmon::FanSpeed>(
490 i.first, ioAccess, _devPath, info);
491
492 if (target)
493 {
494 target->enable();
495 }
Matt Spinler0a8de642017-05-11 10:59:39 -0500496 }
Brad Bishop075f7a22017-01-06 09:45:08 -0500497
Matt Spinlera7e2c1e2018-02-23 12:18:19 -0600498 if ((fanTargetType == targetType::PWM) ||
499 (fanTargetType == targetType::DEFAULT))
500 {
501 addTarget<hwmon::FanPwm>(i.first, ioAccess, _devPath, info);
502 }
Patrick Venture9331ab72018-01-29 09:48:47 -0800503
Brad Bishop30dbcee2017-01-18 07:55:42 -0500504 // All the interfaces have been created. Go ahead
505 // and emit InterfacesAdded.
506 valueInterface->emit_object_added();
507
Brad Bishop075f7a22017-01-06 09:45:08 -0500508 auto value = std::make_tuple(
509 std::move(i.second),
510 std::move(label),
Brad Bishopf7426cf2017-01-06 15:36:43 -0500511 std::move(info));
Brad Bishop73831cd2017-01-06 09:37:22 -0500512
Brad Bishop75b4ab82017-01-06 09:33:50 -0500513 state[std::move(i.first)] = std::move(value);
514 }
515
Patrick Venture62503a42017-05-23 07:30:29 -0700516 /* If there are no sensors specified by labels, exit. */
517 if (0 == state.size())
518 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600519 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700520 }
521
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500522 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500523 std::string busname{_prefix};
Brad Bishop4d9a35b2017-11-14 22:33:03 -0500524 busname.append(1, '-');
525 busname.append(
526 std::to_string(std::hash<decltype(_devPath)>{}(_devPath)));
527 busname.append(".Hwmon1");
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500528 _bus.request_name(busname.c_str());
529 }
530
Patrick Ventureab10f162017-05-22 09:44:50 -0700531 {
532 auto interval = getenv("INTERVAL");
533 if (interval)
534 {
535 _interval = strtoull(interval, NULL, 10);
536 }
537 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600538}
Patrick Ventureab10f162017-05-22 09:44:50 -0700539
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600540void MainLoop::read()
541{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500542 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
543 // ensure the objects all exist?
544
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500545#ifdef REMOVE_ON_FAIL
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600546 std::vector<SensorSet::key_type> destroy;
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500547#endif
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600548 // Iterate through all the sensors.
549 for (auto& i : state)
550 {
551 auto& attrs = std::get<0>(i.second);
552 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500553 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600554 // Read value from sensor.
555 int64_t value;
556 std::string input = hwmon::entry::cinput;
557 if (i.first.first == "pwm") {
558 input = "";
559 }
560
561 try
Brad Bishope55ef3d2016-12-19 09:12:40 -0500562 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600563 // Retry for up to a second if device is busy
564 // or has a transient error.
Patrick Venture9331ab72018-01-29 09:48:47 -0800565
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600566 value = ioAccess.read(
567 i.first.first,
568 i.first.second,
569 input,
570 sysfs::hwmonio::retries,
571 sysfs::hwmonio::delay,
572 _isOCC);
573
574 value = adjustValue(i.first, value);
575
576 auto& objInfo = std::get<ObjectInfo>(i.second);
577 auto& obj = std::get<Object>(objInfo);
578
579 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500580 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600581 auto valueIface = std::shared_ptr<ValueObject>();
582 auto warnIface = std::shared_ptr<WarningObject>();
583 auto critIface = std::shared_ptr<CriticalObject>();
Brad Bishop754d38c2017-09-08 00:46:58 -0400584
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600585 switch (iface.first)
Brad Bishope0b7d052017-01-06 15:30:23 -0500586 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600587 case InterfaceType::VALUE:
588 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
589 (iface.second);
590 valueIface->value(value);
591 break;
592 case InterfaceType::WARN:
593 checkThresholds<WarningObject>(iface.second, value);
594 break;
595 case InterfaceType::CRIT:
596 checkThresholds<CriticalObject>(iface.second, value);
597 break;
598 default:
599 break;
Brad Bishope0b7d052017-01-06 15:30:23 -0500600 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500601 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600602 }
603 catch (const std::system_error& e)
604 {
605 using namespace sdbusplus::xyz::openbmc_project::
606 Sensor::Device::Error;
607 report<ReadFailure>(
608 xyz::openbmc_project::Sensor::Device::
609 ReadFailure::CALLOUT_ERRNO(e.code().value()),
610 xyz::openbmc_project::Sensor::Device::
611 ReadFailure::CALLOUT_DEVICE_PATH(
612 _devPath.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500613
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600614 auto file = sysfs::make_sysfs_path(
615 ioAccess.path(),
616 i.first.first,
617 i.first.second,
618 hwmon::entry::cinput);
Matt Spinler9b65f762017-10-05 10:36:22 -0500619
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600620 log<level::INFO>("Logging failing sysfs file",
621 entry("FILE=%s", file.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500622
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500623#ifdef REMOVE_ON_FAIL
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600624 destroy.push_back(i.first);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500625#else
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600626 exit(EXIT_FAILURE);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500627#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500628 }
629 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600630 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500631
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500632#ifdef REMOVE_ON_FAIL
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600633 for (auto& i : destroy)
634 {
635 state.erase(i);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500636 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600637#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500638}
639
640// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4