blob: c7ede1588ebff2d3901813c74c84e40d1f244230 [file] [log] [blame]
Brad Bishope55ef3d2016-12-19 09:12:40 -05001/**
2 * Copyright © 2016 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brad Bishop4d9a35b2017-11-14 22:33:03 -050016#include <functional>
Brad Bishope55ef3d2016-12-19 09:12:40 -050017#include <iostream>
18#include <memory>
Brad Bishop9c7b6e02016-12-19 12:43:36 -050019#include <cstdlib>
Patrick Venture50cf1c52018-04-18 09:21:41 -070020#include <cstring>
Chiabing Leec923ce92017-09-06 15:58:26 +080021#include <string>
Matthew Barthd26e2772018-03-22 14:24:06 -050022#include <unordered_set>
Patrick Venturec897d8b2018-04-23 19:01:56 -070023#include <sstream>
Patrick Venture1e6324f2017-06-01 14:07:05 -070024
25#include <phosphor-logging/elog-errors.hpp>
Matt Spinlerf9c83c42017-08-10 08:51:45 -050026#include "config.h"
Patrick Venture09791852018-04-17 17:40:00 -070027#include "env.hpp"
28#include "fan_pwm.hpp"
29#include "fan_speed.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050030#include "hwmon.hpp"
Patrick Venture75e56c62018-04-20 18:10:15 -070031#include "hwmonio.hpp"
Patrick Venture09791852018-04-17 17:40:00 -070032#include "sensorset.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050033#include "sysfs.hpp"
Brad Bishopd499ca62016-12-19 09:24:50 -050034#include "mainloop.hpp"
Matthew Barthbf7b7b12017-03-07 15:46:59 -060035#include "targets.hpp"
Patrick Venture09791852018-04-17 17:40:00 -070036#include "thresholds.hpp"
Matthew Barth35819382018-04-18 14:53:01 -050037#include "sensor.hpp"
Brad Bishope55ef3d2016-12-19 09:12:40 -050038
Patrick Venture1e6324f2017-06-01 14:07:05 -070039#include <xyz/openbmc_project/Sensor/Device/error.hpp>
40
41using namespace phosphor::logging;
42
Saqib Khan973886d2017-03-15 14:01:16 -050043// Initialization for Warning Objects
44decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo =
45 &WarningObject::warningLow;
46decltype(Thresholds<WarningObject>::setHi) Thresholds<WarningObject>::setHi =
47 &WarningObject::warningHigh;
48decltype(Thresholds<WarningObject>::getLo) Thresholds<WarningObject>::getLo =
49 &WarningObject::warningLow;
50decltype(Thresholds<WarningObject>::getHi) Thresholds<WarningObject>::getHi =
51 &WarningObject::warningHigh;
52decltype(Thresholds<WarningObject>::alarmLo) Thresholds<WarningObject>::alarmLo =
53 &WarningObject::warningAlarmLow;
54decltype(Thresholds<WarningObject>::alarmHi) Thresholds<WarningObject>::alarmHi =
55 &WarningObject::warningAlarmHigh;
56
57// Initialization for Critical Objects
58decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo =
59 &CriticalObject::criticalLow;
60decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi =
61 &CriticalObject::criticalHigh;
62decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo =
63 &CriticalObject::criticalLow;
64decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi =
65 &CriticalObject::criticalHigh;
66decltype(Thresholds<CriticalObject>::alarmLo) Thresholds<CriticalObject>::alarmLo =
67 &CriticalObject::criticalAlarmLow;
68decltype(Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi =
69 &CriticalObject::criticalAlarmHigh;
70
Chiabing Leec923ce92017-09-06 15:58:26 +080071// The gain and offset to adjust a value
72struct valueAdjust
73{
74 double gain = 1.0;
75 int offset = 0;
Matthew Barthd26e2772018-03-22 14:24:06 -050076 std::unordered_set<int> rmRCs;
Chiabing Leec923ce92017-09-06 15:58:26 +080077};
78
79// Store the valueAdjust for sensors
80std::map<SensorSet::key_type, valueAdjust> sensorAdjusts;
81
Matthew Barthd26e2772018-03-22 14:24:06 -050082void addRemoveRCs(const SensorSet::key_type& sensor,
83 const std::string& rcList)
84{
Matthew Barthb7985272018-04-17 10:50:36 -050085 if (rcList.empty())
86 {
87 return;
88 }
89
Matthew Barthd26e2772018-03-22 14:24:06 -050090 // Convert to a char* for strtok
91 std::vector<char> rmRCs(rcList.c_str(),
92 rcList.c_str() + rcList.size() + 1);
Patrick Venture50cf1c52018-04-18 09:21:41 -070093 auto rmRC = std::strtok(&rmRCs[0], ", ");
Matthew Barthd26e2772018-03-22 14:24:06 -050094 while (rmRC != nullptr)
95 {
96 try
97 {
98 sensorAdjusts[sensor].rmRCs.insert(std::stoi(rmRC));
99 }
100 catch (const std::logic_error& le)
101 {
102 // Unable to convert to int, continue to next token
103 std::string name = sensor.first + "_" + sensor.second;
104 log<level::INFO>("Unable to convert sensor removal return code",
105 entry("SENSOR=%s", name.c_str()),
106 entry("RC=%s", rmRC),
107 entry("EXCEPTION=%s", le.what()));
108 }
Patrick Venture50cf1c52018-04-18 09:21:41 -0700109 rmRC = std::strtok(nullptr, ", ");
Matthew Barthd26e2772018-03-22 14:24:06 -0500110 }
111}
112
Matt Spinlerfee106b2017-11-29 15:18:05 -0600113int64_t adjustValue(const SensorSet::key_type& sensor, int64_t value)
Chiabing Leec923ce92017-09-06 15:58:26 +0800114{
Patrick Venturec1cece72017-11-07 12:09:49 -0800115// Because read doesn't have an out pointer to store errors.
116// let's assume negative values are errors if they have this
117// set.
118#ifdef NEGATIVE_ERRNO_ON_FAIL
119 if (value < 0)
120 {
121 return value;
122 }
123#endif
124
Chiabing Leec923ce92017-09-06 15:58:26 +0800125 const auto& it = sensorAdjusts.find(sensor);
126 if (it != sensorAdjusts.end())
127 {
128 // Adjust based on gain and offset
129 value = static_cast<decltype(value)>(
130 static_cast<double>(value) * it->second.gain
131 + it->second.offset);
132 }
133 return value;
134}
135
Brad Bishope9fdee02017-01-06 10:43:29 -0500136auto addValue(const SensorSet::key_type& sensor,
Matthew Barthd4beecf2018-04-03 15:50:22 -0500137 const RetryIO& retryIO,
Patrick Venture75e56c62018-04-20 18:10:15 -0700138 hwmonio::HwmonIO& ioAccess,
Matthew Barth0b305052018-04-26 09:46:49 -0500139 ObjectInfo& info)
Brad Bishope9fdee02017-01-06 10:43:29 -0500140{
Brad Bishop30dbcee2017-01-18 07:55:42 -0500141 static constexpr bool deferSignals = true;
142
Brad Bishope9fdee02017-01-06 10:43:29 -0500143 // Get the initial value for the value interface.
144 auto& bus = *std::get<sdbusplus::bus::bus*>(info);
145 auto& obj = std::get<Object>(info);
146 auto& objPath = std::get<std::string>(info);
147
Patrick Venture7a5285d2018-04-17 19:15:05 -0700148 auto senRmRCs = env::getEnv("REMOVERCS", sensor);
Matthew Barthb7985272018-04-17 10:50:36 -0500149 // Add sensor removal return codes defined per sensor
150 addRemoveRCs(sensor, senRmRCs);
Matthew Barthd26e2772018-03-22 14:24:06 -0500151
Patrick Venture7a5285d2018-04-17 19:15:05 -0700152 auto gain = env::getEnv("GAIN", sensor);
Chiabing Leec923ce92017-09-06 15:58:26 +0800153 if (!gain.empty())
154 {
155 sensorAdjusts[sensor].gain = std::stod(gain);
156 }
157
Patrick Venture7a5285d2018-04-17 19:15:05 -0700158 auto offset = env::getEnv("OFFSET", sensor);
Chiabing Leec923ce92017-09-06 15:58:26 +0800159 if (!offset.empty())
160 {
161 sensorAdjusts[sensor].offset = std::stoi(offset);
162 }
163
Matthew Barthca44c2e2018-04-24 15:33:25 -0500164 int64_t val = 0;
165 std::shared_ptr<StatusObject> statusIface = nullptr;
166 auto it = obj.find(InterfaceType::STATUS);
167 if (it != obj.end())
168 {
169 statusIface = std::experimental::any_cast<
170 std::shared_ptr<StatusObject>>(it->second);
171 }
172
173 // If there's no fault file or the sensor has a fault file and
174 // its status is functional, read the input value.
175 if (!statusIface || (statusIface && statusIface->functional()))
176 {
177 // Retry for up to a second if device is busy
178 // or has a transient error.
179 val = ioAccess.read(
180 sensor.first,
181 sensor.second,
182 hwmon::entry::cinput,
183 std::get<size_t>(retryIO),
Matthew Barth0b305052018-04-26 09:46:49 -0500184 std::get<std::chrono::milliseconds>(retryIO));
Matthew Barthca44c2e2018-04-24 15:33:25 -0500185 val = adjustValue(sensor, val);
186 }
Chiabing Leec923ce92017-09-06 15:58:26 +0800187
Brad Bishop30dbcee2017-01-18 07:55:42 -0500188 auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals);
Brad Bishope9fdee02017-01-06 10:43:29 -0500189 iface->value(val);
190
Patrick Venture09791852018-04-17 17:40:00 -0700191 hwmon::Attributes attrs;
192 if (hwmon::getAttributes(sensor.first, attrs))
Brad Bishope9fdee02017-01-06 10:43:29 -0500193 {
Patrick Venture09791852018-04-17 17:40:00 -0700194 iface->unit(hwmon::getUnit(attrs));
195 iface->scale(hwmon::getScale(attrs));
Brad Bishope9fdee02017-01-06 10:43:29 -0500196 }
197
Patrick Venture7a5285d2018-04-17 19:15:05 -0700198 auto maxValue = env::getEnv("MAXVALUE", sensor);
James Feist7bd5fcf2018-01-22 16:14:28 -0800199 if(!maxValue.empty())
200 {
201 iface->maxValue(std::stoll(maxValue));
202 }
Patrick Venture7a5285d2018-04-17 19:15:05 -0700203 auto minValue = env::getEnv("MINVALUE", sensor);
James Feist7bd5fcf2018-01-22 16:14:28 -0800204 if(!minValue.empty())
205 {
206 iface->minValue(std::stoll(minValue));
207 }
208
Brad Bishope9fdee02017-01-06 10:43:29 -0500209 obj[InterfaceType::VALUE] = iface;
210 return iface;
211}
212
Matthew Barth979c8062018-04-17 11:37:15 -0500213std::string MainLoop::getID(SensorSet::container_t::const_reference sensor)
Matthew Barth31d214c2018-03-26 09:54:27 -0500214{
Matthew Barth31d214c2018-03-26 09:54:27 -0500215 std::string id;
216
217 /*
218 * Check if the value of the MODE_<item><X> env variable for the sensor
Matt Spinler7c424802018-05-04 10:52:40 -0500219 * is set. If it is, then read the from the <item><X>_<mode>
Matthew Barth31d214c2018-03-26 09:54:27 -0500220 * file. The name of the DBUS object would be the value of the env
Matt Spinler7c424802018-05-04 10:52:40 -0500221 * variable LABEL_<item><mode value>. If the MODE_<item><X> env variable
Matthew Barth31d214c2018-03-26 09:54:27 -0500222 * doesn't exist, then the name of DBUS object is the value of the env
223 * variable LABEL_<item><X>.
Matt Spinler7c424802018-05-04 10:52:40 -0500224 *
225 * For example, if MODE_temp1 = "label", then code reads the temp1_label
226 * file. If it has a 5 in it, then it will use the following entry to
227 * name the object: LABEL_temp5 = "My DBus object name".
228 *
Matthew Barth31d214c2018-03-26 09:54:27 -0500229 */
Patrick Venture7a5285d2018-04-17 19:15:05 -0700230 auto mode = env::getEnv("MODE", sensor.first);
Matt Spinler7c424802018-05-04 10:52:40 -0500231 if (!mode.empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500232 {
Patrick Venture7a5285d2018-04-17 19:15:05 -0700233 id = env::getIndirectID(
Matt Spinler7c424802018-05-04 10:52:40 -0500234 _hwmonRoot + '/' + _instance + '/',
235 mode,
236 sensor.first);
Matthew Barth31d214c2018-03-26 09:54:27 -0500237
238 if (id.empty())
239 {
Matthew Barth979c8062018-04-17 11:37:15 -0500240 return id;
Matthew Barth31d214c2018-03-26 09:54:27 -0500241 }
242 }
243
244 // Use the ID we looked up above if there was one,
245 // otherwise use the standard one.
246 id = (id.empty()) ? sensor.first.second : id;
247
Matthew Barth979c8062018-04-17 11:37:15 -0500248 return id;
249}
250
251SensorIdentifiers MainLoop::getIdentifiers(
252 SensorSet::container_t::const_reference sensor)
253{
254 std::string id = getID(sensor);
255 std::string label;
256
257 if (!id.empty())
258 {
259 // Ignore inputs without a label.
260 label = env::getEnv("LABEL", sensor.first.first, id);
261 }
262
263 return std::make_tuple(std::move(id),
264 std::move(label));
265}
266
267/**
268 * Reads the environment parameters of a sensor and creates an object with
269 * atleast the `Value` interface, otherwise returns without creating the object.
270 * If the `Value` interface is successfully created, by reading the sensor's
271 * corresponding sysfs file's value, the additional interfaces for the sensor
Matthew Barthd238e232018-04-17 12:01:50 -0500272 * are created and the InterfacesAdded signal is emitted. The object's state
273 * data is then returned for sensor state monitoring within the main loop.
Matthew Barth979c8062018-04-17 11:37:15 -0500274 */
Matthew Barthd238e232018-04-17 12:01:50 -0500275optional_ns::optional<ObjectStateData> MainLoop::getObject(
276 SensorSet::container_t::const_reference sensor)
Matthew Barth979c8062018-04-17 11:37:15 -0500277{
278 auto properties = getIdentifiers(sensor);
279 if (std::get<sensorID>(properties).empty() ||
280 std::get<sensorLabel>(properties).empty())
Matthew Barth31d214c2018-03-26 09:54:27 -0500281 {
Matthew Barthd238e232018-04-17 12:01:50 -0500282 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500283 }
284
Patrick Venture09791852018-04-17 17:40:00 -0700285 hwmon::Attributes attrs;
286 if (!hwmon::getAttributes(sensor.first.first, attrs))
Matthew Barth31d214c2018-03-26 09:54:27 -0500287 {
Matthew Barthd238e232018-04-17 12:01:50 -0500288 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500289 }
290
Matthew Barth9c431062018-05-07 13:55:29 -0500291 auto sensorObj = std::make_unique<sensor::Sensor>(sensor.first);
292
Matthew Barthb7985272018-04-17 10:50:36 -0500293 // Get list of return codes for removing sensors on device
294 auto devRmRCs = env::getEnv("REMOVERCS");
295 // Add sensor removal return codes defined at the device level
296 addRemoveRCs(sensor.first, devRmRCs);
Matthew Barth31d214c2018-03-26 09:54:27 -0500297
298 std::string objectPath{_root};
299 objectPath.append(1, '/');
Patrick Venture09791852018-04-17 17:40:00 -0700300 objectPath.append(hwmon::getNamespace(attrs));
Matthew Barth31d214c2018-03-26 09:54:27 -0500301 objectPath.append(1, '/');
Matthew Barth979c8062018-04-17 11:37:15 -0500302 objectPath.append(std::get<sensorLabel>(properties));
Matthew Barth31d214c2018-03-26 09:54:27 -0500303
304 ObjectInfo info(&_bus, std::move(objectPath), Object());
Patrick Venture75e56c62018-04-20 18:10:15 -0700305 RetryIO retryIO(hwmonio::retries, hwmonio::delay);
Matthew Barthd4beecf2018-04-03 15:50:22 -0500306 if (rmSensors.find(sensor.first) != rmSensors.end())
307 {
308 // When adding a sensor that was purposely removed,
309 // don't retry on errors when reading its value
310 std::get<size_t>(retryIO) = 0;
311 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500312 auto valueInterface = static_cast<
313 std::shared_ptr<ValueObject>>(nullptr);
314 try
315 {
Matthew Barthca44c2e2018-04-24 15:33:25 -0500316 // Add status interface based on _fault file being present
317 sensor::addStatus(sensor.first, ioAccess, _devPath, info);
Matthew Barth0b305052018-04-26 09:46:49 -0500318 valueInterface = addValue(sensor.first, retryIO, ioAccess, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500319 }
320 catch (const std::system_error& e)
321 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500322 auto file = sysfs::make_sysfs_path(
323 ioAccess.path(),
324 sensor.first.first,
325 sensor.first.second,
326 hwmon::entry::cinput);
Matthew Barth31d214c2018-03-26 09:54:27 -0500327#ifndef REMOVE_ON_FAIL
328 // Check sensorAdjusts for sensor removal RCs
329 const auto& it = sensorAdjusts.find(sensor.first);
330 if (it != sensorAdjusts.end())
331 {
332 auto rmRCit = it->second.rmRCs.find(e.code().value());
333 if (rmRCit != std::end(it->second.rmRCs))
334 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500335 // Return code found in sensor return code removal list
336 if (rmSensors.find(sensor.first) == rmSensors.end())
337 {
338 // Trace for sensor not already removed from dbus
339 log<level::INFO>("Sensor not added to dbus for read fail",
340 entry("FILE=%s", file.c_str()),
341 entry("RC=%d", e.code().value()));
342 rmSensors[std::move(sensor.first)] =
343 std::move(sensor.second);
344 }
Matthew Barthd238e232018-04-17 12:01:50 -0500345 return {};
Matthew Barth31d214c2018-03-26 09:54:27 -0500346 }
347 }
348#endif
349 using namespace sdbusplus::xyz::openbmc_project::
350 Sensor::Device::Error;
351 report<ReadFailure>(
352 xyz::openbmc_project::Sensor::Device::
353 ReadFailure::CALLOUT_ERRNO(e.code().value()),
354 xyz::openbmc_project::Sensor::Device::
355 ReadFailure::CALLOUT_DEVICE_PATH(_devPath.c_str()));
356
Matthew Barth31d214c2018-03-26 09:54:27 -0500357 log<level::INFO>("Logging failing sysfs file",
358 entry("FILE=%s", file.c_str()));
359#ifdef REMOVE_ON_FAIL
Matthew Barthd238e232018-04-17 12:01:50 -0500360 return {}; /* skip adding this sensor for now. */
Matthew Barth31d214c2018-03-26 09:54:27 -0500361#else
362 exit(EXIT_FAILURE);
363#endif
364 }
365 auto sensorValue = valueInterface->value();
Matthew Barth979c8062018-04-17 11:37:15 -0500366 addThreshold<WarningObject>(sensor.first.first,
367 std::get<sensorID>(properties),
368 sensorValue,
369 info);
370 addThreshold<CriticalObject>(sensor.first.first,
371 std::get<sensorID>(properties),
372 sensorValue,
373 info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500374
Matthew Barth28f8e662018-03-26 16:57:36 -0500375 auto target = addTarget<hwmon::FanSpeed>(
376 sensor.first, ioAccess, _devPath, info);
377 if (target)
Matthew Barth31d214c2018-03-26 09:54:27 -0500378 {
Matthew Barth28f8e662018-03-26 16:57:36 -0500379 target->enable();
Matthew Barth31d214c2018-03-26 09:54:27 -0500380 }
Matthew Barth28f8e662018-03-26 16:57:36 -0500381 addTarget<hwmon::FanPwm>(sensor.first, ioAccess, _devPath, info);
Matthew Barth31d214c2018-03-26 09:54:27 -0500382
383 // All the interfaces have been created. Go ahead
384 // and emit InterfacesAdded.
385 valueInterface->emit_object_added();
386
Matthew Barth9c431062018-05-07 13:55:29 -0500387 // Save sensor object specifications
388 sensorObjects[sensor.first] = std::move(sensorObj);
389
Matthew Barthd238e232018-04-17 12:01:50 -0500390 return std::make_pair(std::move(std::get<sensorLabel>(properties)),
391 std::move(info));
Matthew Barth31d214c2018-03-26 09:54:27 -0500392}
393
Brad Bishopb9e2b072016-12-19 13:47:10 -0500394MainLoop::MainLoop(
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500395 sdbusplus::bus::bus&& bus,
Patrick Venturec897d8b2018-04-23 19:01:56 -0700396 const std::string& param,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500397 const std::string& path,
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400398 const std::string& devPath,
Brad Bishopb9e2b072016-12-19 13:47:10 -0500399 const char* prefix,
400 const char* root)
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500401 : _bus(std::move(bus)),
Brad Bishop03e87352017-03-07 00:12:22 -0500402 _manager(_bus, root),
Patrick Venturec897d8b2018-04-23 19:01:56 -0700403 _pathParam(param),
Brad Bishopb8740fc2017-02-24 23:38:37 -0500404 _hwmonRoot(),
405 _instance(),
Brad Bishopf3aa9ae2017-08-25 09:56:02 -0400406 _devPath(devPath),
Brad Bishopb9e2b072016-12-19 13:47:10 -0500407 _prefix(prefix),
Brad Bishop3c344d32017-01-05 11:48:39 -0500408 _root(root),
Brad Bishop751043e2017-08-29 11:13:46 -0400409 state(),
410 ioAccess(path)
Brad Bishopd499ca62016-12-19 09:24:50 -0500411{
Patrick Venture73a50c72018-04-17 15:19:03 -0700412 // Strip off any trailing slashes.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500413 std::string p = path;
414 while (!p.empty() && p.back() == '/')
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500415 {
Brad Bishopb8740fc2017-02-24 23:38:37 -0500416 p.pop_back();
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500417 }
Brad Bishopb8740fc2017-02-24 23:38:37 -0500418
Patrick Venture73a50c72018-04-17 15:19:03 -0700419 // Given the furthest right /, set instance to
420 // the basename, and hwmonRoot to the leading path.
Brad Bishopb8740fc2017-02-24 23:38:37 -0500421 auto n = p.rfind('/');
422 if (n != std::string::npos)
423 {
424 _instance.assign(p.substr(n + 1));
425 _hwmonRoot.assign(p.substr(0, n));
426 }
427
428 assert(!_instance.empty());
429 assert(!_hwmonRoot.empty());
Brad Bishopd499ca62016-12-19 09:24:50 -0500430}
431
432void MainLoop::shutdown() noexcept
433{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600434 timer->state(phosphor::hwmon::timer::OFF);
435 sd_event_exit(loop, 0);
436 loop = nullptr;
Brad Bishopd499ca62016-12-19 09:24:50 -0500437}
438
439void MainLoop::run()
Brad Bishope55ef3d2016-12-19 09:12:40 -0500440{
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600441 init();
442
443 sd_event_default(&loop);
444
445 std::function<void()> callback(std::bind(
446 &MainLoop::read, this));
447 try
448 {
449 timer = std::make_unique<phosphor::hwmon::Timer>(
450 loop, callback,
451 std::chrono::microseconds(_interval),
452 phosphor::hwmon::timer::ON);
453
454 // TODO: Issue#6 - Optionally look at polling interval sysfs entry.
455
456 // TODO: Issue#7 - Should probably periodically check the SensorSet
457 // for new entries.
458
459 _bus.attach_event(loop, SD_EVENT_PRIORITY_IMPORTANT);
460 sd_event_loop(loop);
461 }
462 catch (const std::system_error& e)
463 {
464 log<level::ERR>("Error in sysfs polling loop",
465 entry("ERROR=%s", e.what()));
466 throw;
467 }
468}
469
470void MainLoop::init()
471{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500472 // Check sysfs for available sensors.
Brad Bishop4db64422017-02-16 11:33:32 -0500473 auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500474
Brad Bishop75b4ab82017-01-06 09:33:50 -0500475 for (auto& i : *sensors)
476 {
Matthew Barthd238e232018-04-17 12:01:50 -0500477 auto object = getObject(i);
478 if (object)
479 {
480 // Construct the SensorSet value
481 // std::tuple<SensorSet::mapped_type,
482 // std::string(Sensor Label),
483 // ObjectInfo>
484 auto value = std::make_tuple(std::move(i.second),
485 std::move((*object).first),
486 std::move((*object).second));
487
488 state[std::move(i.first)] = std::move(value);
489 }
Brad Bishop75b4ab82017-01-06 09:33:50 -0500490 }
491
Patrick Venture62503a42017-05-23 07:30:29 -0700492 /* If there are no sensors specified by labels, exit. */
493 if (0 == state.size())
494 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600495 exit(0);
Patrick Venture62503a42017-05-23 07:30:29 -0700496 }
497
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500498 {
Patrick Venturec897d8b2018-04-23 19:01:56 -0700499 std::stringstream ss;
500 ss << _prefix
501 << "-"
502 << std::to_string(std::hash<std::string>{}(_devPath + _pathParam))
503 << ".Hwmon1";
504
505 _bus.request_name(ss.str().c_str());
Brad Bishop9c7b6e02016-12-19 12:43:36 -0500506 }
507
Patrick Ventureab10f162017-05-22 09:44:50 -0700508 {
Patrick Venturea24c8802018-04-17 19:38:06 -0700509 auto interval = env::getEnv("INTERVAL");
510 if (!interval.empty())
Patrick Ventureab10f162017-05-22 09:44:50 -0700511 {
Patrick Venture50cf1c52018-04-18 09:21:41 -0700512 _interval = std::strtoull(interval.c_str(), NULL, 10);
Patrick Ventureab10f162017-05-22 09:44:50 -0700513 }
514 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600515}
Patrick Ventureab10f162017-05-22 09:44:50 -0700516
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600517void MainLoop::read()
518{
Brad Bishope55ef3d2016-12-19 09:12:40 -0500519 // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to
520 // ensure the objects all exist?
521
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600522 // Iterate through all the sensors.
523 for (auto& i : state)
524 {
525 auto& attrs = std::get<0>(i.second);
526 if (attrs.find(hwmon::entry::input) != attrs.end())
Brad Bishope55ef3d2016-12-19 09:12:40 -0500527 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600528 // Read value from sensor.
529 int64_t value;
530 std::string input = hwmon::entry::cinput;
531 if (i.first.first == "pwm") {
532 input = "";
533 }
534
535 try
Brad Bishope55ef3d2016-12-19 09:12:40 -0500536 {
Matthew Barth27c4a392018-04-25 14:38:51 -0500537 auto& objInfo = std::get<ObjectInfo>(i.second);
538 auto& obj = std::get<Object>(objInfo);
539
540 auto it = obj.find(InterfaceType::STATUS);
541 if (it != obj.end())
542 {
Matthew Barthbfcaf3d2018-04-25 15:05:58 -0500543 auto fault = ioAccess.read(
544 i.first.first,
545 i.first.second,
546 hwmon::entry::fault,
547 hwmonio::retries,
548 hwmonio::delay);
Matthew Barth27c4a392018-04-25 14:38:51 -0500549 auto statusIface = std::experimental::any_cast<
550 std::shared_ptr<StatusObject>>(it->second);
Matthew Barthbfcaf3d2018-04-25 15:05:58 -0500551 if (!statusIface->functional((fault == 0) ? true : false))
Matthew Barth27c4a392018-04-25 14:38:51 -0500552 {
553 continue;
554 }
555 }
556
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600557 // Retry for up to a second if device is busy
558 // or has a transient error.
Patrick Venture9331ab72018-01-29 09:48:47 -0800559
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600560 value = ioAccess.read(
561 i.first.first,
562 i.first.second,
563 input,
Patrick Venture75e56c62018-04-20 18:10:15 -0700564 hwmonio::retries,
Matthew Barth0b305052018-04-26 09:46:49 -0500565 hwmonio::delay);
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600566
567 value = adjustValue(i.first, value);
568
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600569 for (auto& iface : obj)
Brad Bishopdddb7152017-01-06 09:54:23 -0500570 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600571 auto valueIface = std::shared_ptr<ValueObject>();
572 auto warnIface = std::shared_ptr<WarningObject>();
573 auto critIface = std::shared_ptr<CriticalObject>();
Brad Bishop754d38c2017-09-08 00:46:58 -0400574
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600575 switch (iface.first)
Brad Bishope0b7d052017-01-06 15:30:23 -0500576 {
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600577 case InterfaceType::VALUE:
578 valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>>
579 (iface.second);
580 valueIface->value(value);
581 break;
582 case InterfaceType::WARN:
583 checkThresholds<WarningObject>(iface.second, value);
584 break;
585 case InterfaceType::CRIT:
586 checkThresholds<CriticalObject>(iface.second, value);
587 break;
588 default:
589 break;
Brad Bishope0b7d052017-01-06 15:30:23 -0500590 }
Brad Bishopdddb7152017-01-06 09:54:23 -0500591 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600592 }
593 catch (const std::system_error& e)
594 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500595 auto file = sysfs::make_sysfs_path(
596 ioAccess.path(),
597 i.first.first,
598 i.first.second,
599 hwmon::entry::cinput);
Matthew Barth8772ce32018-03-22 16:03:06 -0500600#ifndef REMOVE_ON_FAIL
601 // Check sensorAdjusts for sensor removal RCs
602 const auto& it = sensorAdjusts.find(i.first);
603 if (it != sensorAdjusts.end())
604 {
605 auto rmRCit = it->second.rmRCs.find(e.code().value());
606 if (rmRCit != std::end(it->second.rmRCs))
607 {
Matthew Barth38c74e72018-04-02 12:41:26 -0500608 // Return code found in sensor return code removal list
609 if (rmSensors.find(i.first) == rmSensors.end())
610 {
611 // Trace for sensor not already removed from dbus
612 log<level::INFO>(
613 "Remove sensor from dbus for read fail",
614 entry("FILE=%s", file.c_str()),
615 entry("RC=%d", e.code().value()));
616 // Mark this sensor to be removed from dbus
617 rmSensors[i.first] = std::get<0>(i.second);
618 }
Matthew Barth8772ce32018-03-22 16:03:06 -0500619 continue;
620 }
621 }
622#endif
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600623 using namespace sdbusplus::xyz::openbmc_project::
624 Sensor::Device::Error;
625 report<ReadFailure>(
626 xyz::openbmc_project::Sensor::Device::
627 ReadFailure::CALLOUT_ERRNO(e.code().value()),
628 xyz::openbmc_project::Sensor::Device::
629 ReadFailure::CALLOUT_DEVICE_PATH(
630 _devPath.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500631
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600632 log<level::INFO>("Logging failing sysfs file",
633 entry("FILE=%s", file.c_str()));
Matt Spinler9b65f762017-10-05 10:36:22 -0500634
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500635#ifdef REMOVE_ON_FAIL
Matthew Barth33db92b2018-03-26 09:55:19 -0500636 rmSensors[i.first] = std::get<0>(i.second);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500637#else
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600638 exit(EXIT_FAILURE);
Matt Spinlerf9c83c42017-08-10 08:51:45 -0500639#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500640 }
641 }
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600642 }
Brad Bishope55ef3d2016-12-19 09:12:40 -0500643
Matthew Barth8772ce32018-03-22 16:03:06 -0500644 // Remove any sensors marked for removal
Matthew Barth33db92b2018-03-26 09:55:19 -0500645 for (auto& i : rmSensors)
Deepak Kodihalli2a51a9c2018-03-07 02:39:40 -0600646 {
Matthew Barth33db92b2018-03-26 09:55:19 -0500647 state.erase(i.first);
Brad Bishope55ef3d2016-12-19 09:12:40 -0500648 }
Matthew Barth31d214c2018-03-26 09:54:27 -0500649
650#ifndef REMOVE_ON_FAIL
651 // Attempt to add any sensors that were removed
652 auto it = rmSensors.begin();
653 while (it != rmSensors.end())
654 {
655 if (state.find(it->first) == state.end())
656 {
657 SensorSet::container_t::value_type ssValueType =
658 std::make_pair(it->first, it->second);
Matthew Barthd238e232018-04-17 12:01:50 -0500659 auto object = getObject(ssValueType);
660 if (object)
Matthew Barth31d214c2018-03-26 09:54:27 -0500661 {
Matthew Barthd238e232018-04-17 12:01:50 -0500662 // Construct the SensorSet value
663 // std::tuple<SensorSet::mapped_type,
664 // std::string(Sensor Label),
665 // ObjectInfo>
666 auto value = std::make_tuple(std::move(ssValueType.second),
667 std::move((*object).first),
668 std::move((*object).second));
669
670 state[std::move(ssValueType.first)] = std::move(value);
671
Matthew Barth31d214c2018-03-26 09:54:27 -0500672 // Sensor object added, erase entry from removal list
Matthew Barth38c74e72018-04-02 12:41:26 -0500673 auto file = sysfs::make_sysfs_path(
674 ioAccess.path(),
675 it->first.first,
676 it->first.second,
677 hwmon::entry::cinput);
678 log<level::INFO>(
679 "Added sensor to dbus after successful read",
680 entry("FILE=%s", file.c_str()));
Matthew Barth31d214c2018-03-26 09:54:27 -0500681 it = rmSensors.erase(it);
682 }
683 else
684 {
685 ++it;
686 }
687 }
688 else
689 {
690 // Sanity check to remove sensors that were re-added
691 it = rmSensors.erase(it);
692 }
693 }
694#endif
Brad Bishope55ef3d2016-12-19 09:12:40 -0500695}
696
697// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4