Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 1 | /** |
| 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 Bishop | 4d9a35b | 2017-11-14 22:33:03 -0500 | [diff] [blame] | 16 | #include <functional> |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 17 | #include <iostream> |
| 18 | #include <memory> |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 19 | #include <cstdlib> |
Chiabing Lee | c923ce9 | 2017-09-06 15:58:26 +0800 | [diff] [blame] | 20 | #include <string> |
Matthew Barth | d26e277 | 2018-03-22 14:24:06 -0500 | [diff] [blame] | 21 | #include <unordered_set> |
Patrick Venture | c897d8b | 2018-04-23 19:01:56 -0700 | [diff] [blame] | 22 | #include <sstream> |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 23 | |
| 24 | #include <phosphor-logging/elog-errors.hpp> |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 25 | #include "config.h" |
Patrick Venture | 0979185 | 2018-04-17 17:40:00 -0700 | [diff] [blame] | 26 | #include "env.hpp" |
| 27 | #include "fan_pwm.hpp" |
| 28 | #include "fan_speed.hpp" |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 29 | #include "hwmon.hpp" |
Patrick Venture | 75e56c6 | 2018-04-20 18:10:15 -0700 | [diff] [blame] | 30 | #include "hwmonio.hpp" |
Patrick Venture | 0979185 | 2018-04-17 17:40:00 -0700 | [diff] [blame] | 31 | #include "sensorset.hpp" |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 32 | #include "sysfs.hpp" |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 33 | #include "mainloop.hpp" |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 34 | #include "targets.hpp" |
Patrick Venture | 0979185 | 2018-04-17 17:40:00 -0700 | [diff] [blame] | 35 | #include "thresholds.hpp" |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 36 | #include "sensor.hpp" |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 37 | |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 38 | #include <xyz/openbmc_project/Sensor/Device/error.hpp> |
| 39 | |
| 40 | using namespace phosphor::logging; |
| 41 | |
Saqib Khan | 973886d | 2017-03-15 14:01:16 -0500 | [diff] [blame] | 42 | // Initialization for Warning Objects |
| 43 | decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo = |
| 44 | &WarningObject::warningLow; |
| 45 | decltype(Thresholds<WarningObject>::setHi) Thresholds<WarningObject>::setHi = |
| 46 | &WarningObject::warningHigh; |
| 47 | decltype(Thresholds<WarningObject>::getLo) Thresholds<WarningObject>::getLo = |
| 48 | &WarningObject::warningLow; |
| 49 | decltype(Thresholds<WarningObject>::getHi) Thresholds<WarningObject>::getHi = |
| 50 | &WarningObject::warningHigh; |
| 51 | decltype(Thresholds<WarningObject>::alarmLo) Thresholds<WarningObject>::alarmLo = |
| 52 | &WarningObject::warningAlarmLow; |
| 53 | decltype(Thresholds<WarningObject>::alarmHi) Thresholds<WarningObject>::alarmHi = |
| 54 | &WarningObject::warningAlarmHigh; |
| 55 | |
| 56 | // Initialization for Critical Objects |
| 57 | decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo = |
| 58 | &CriticalObject::criticalLow; |
| 59 | decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi = |
| 60 | &CriticalObject::criticalHigh; |
| 61 | decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo = |
| 62 | &CriticalObject::criticalLow; |
| 63 | decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi = |
| 64 | &CriticalObject::criticalHigh; |
| 65 | decltype(Thresholds<CriticalObject>::alarmLo) Thresholds<CriticalObject>::alarmLo = |
| 66 | &CriticalObject::criticalAlarmLow; |
| 67 | decltype(Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi = |
| 68 | &CriticalObject::criticalAlarmHigh; |
| 69 | |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 70 | std::string MainLoop::getID(SensorSet::container_t::const_reference sensor) |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 71 | { |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 72 | std::string id; |
| 73 | |
| 74 | /* |
| 75 | * Check if the value of the MODE_<item><X> env variable for the sensor |
Matt Spinler | 7c42480 | 2018-05-04 10:52:40 -0500 | [diff] [blame] | 76 | * is set. If it is, then read the from the <item><X>_<mode> |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 77 | * file. The name of the DBUS object would be the value of the env |
Matt Spinler | 7c42480 | 2018-05-04 10:52:40 -0500 | [diff] [blame] | 78 | * variable LABEL_<item><mode value>. If the MODE_<item><X> env variable |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 79 | * doesn't exist, then the name of DBUS object is the value of the env |
| 80 | * variable LABEL_<item><X>. |
Matt Spinler | 7c42480 | 2018-05-04 10:52:40 -0500 | [diff] [blame] | 81 | * |
| 82 | * For example, if MODE_temp1 = "label", then code reads the temp1_label |
| 83 | * file. If it has a 5 in it, then it will use the following entry to |
| 84 | * name the object: LABEL_temp5 = "My DBus object name". |
| 85 | * |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 86 | */ |
Patrick Venture | 7a5285d | 2018-04-17 19:15:05 -0700 | [diff] [blame] | 87 | auto mode = env::getEnv("MODE", sensor.first); |
Matt Spinler | 7c42480 | 2018-05-04 10:52:40 -0500 | [diff] [blame] | 88 | if (!mode.empty()) |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 89 | { |
Patrick Venture | 7a5285d | 2018-04-17 19:15:05 -0700 | [diff] [blame] | 90 | id = env::getIndirectID( |
Matt Spinler | 7c42480 | 2018-05-04 10:52:40 -0500 | [diff] [blame] | 91 | _hwmonRoot + '/' + _instance + '/', |
| 92 | mode, |
| 93 | sensor.first); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 94 | |
| 95 | if (id.empty()) |
| 96 | { |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 97 | return id; |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | // Use the ID we looked up above if there was one, |
| 102 | // otherwise use the standard one. |
| 103 | id = (id.empty()) ? sensor.first.second : id; |
| 104 | |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 105 | return id; |
| 106 | } |
| 107 | |
| 108 | SensorIdentifiers MainLoop::getIdentifiers( |
| 109 | SensorSet::container_t::const_reference sensor) |
| 110 | { |
| 111 | std::string id = getID(sensor); |
| 112 | std::string label; |
| 113 | |
| 114 | if (!id.empty()) |
| 115 | { |
| 116 | // Ignore inputs without a label. |
| 117 | label = env::getEnv("LABEL", sensor.first.first, id); |
| 118 | } |
| 119 | |
| 120 | return std::make_tuple(std::move(id), |
| 121 | std::move(label)); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Reads the environment parameters of a sensor and creates an object with |
| 126 | * atleast the `Value` interface, otherwise returns without creating the object. |
| 127 | * If the `Value` interface is successfully created, by reading the sensor's |
| 128 | * corresponding sysfs file's value, the additional interfaces for the sensor |
Matthew Barth | d238e23 | 2018-04-17 12:01:50 -0500 | [diff] [blame] | 129 | * are created and the InterfacesAdded signal is emitted. The object's state |
| 130 | * data is then returned for sensor state monitoring within the main loop. |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 131 | */ |
Matthew Barth | d238e23 | 2018-04-17 12:01:50 -0500 | [diff] [blame] | 132 | optional_ns::optional<ObjectStateData> MainLoop::getObject( |
| 133 | SensorSet::container_t::const_reference sensor) |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 134 | { |
| 135 | auto properties = getIdentifiers(sensor); |
| 136 | if (std::get<sensorID>(properties).empty() || |
| 137 | std::get<sensorLabel>(properties).empty()) |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 138 | { |
Matthew Barth | d238e23 | 2018-04-17 12:01:50 -0500 | [diff] [blame] | 139 | return {}; |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 140 | } |
| 141 | |
Patrick Venture | 0979185 | 2018-04-17 17:40:00 -0700 | [diff] [blame] | 142 | hwmon::Attributes attrs; |
| 143 | if (!hwmon::getAttributes(sensor.first.first, attrs)) |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 144 | { |
Matthew Barth | d238e23 | 2018-04-17 12:01:50 -0500 | [diff] [blame] | 145 | return {}; |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 146 | } |
| 147 | |
Matthew Barth | 2e41b13 | 2018-05-07 14:15:45 -0500 | [diff] [blame] | 148 | auto sensorObj = std::make_unique<sensor::Sensor>(sensor.first, |
| 149 | ioAccess, |
| 150 | _devPath); |
Matthew Barth | 9c43106 | 2018-05-07 13:55:29 -0500 | [diff] [blame] | 151 | |
Matthew Barth | b798527 | 2018-04-17 10:50:36 -0500 | [diff] [blame] | 152 | // Get list of return codes for removing sensors on device |
| 153 | auto devRmRCs = env::getEnv("REMOVERCS"); |
| 154 | // Add sensor removal return codes defined at the device level |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 155 | sensorObj->addRemoveRCs(devRmRCs); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 156 | |
| 157 | std::string objectPath{_root}; |
| 158 | objectPath.append(1, '/'); |
Patrick Venture | 0979185 | 2018-04-17 17:40:00 -0700 | [diff] [blame] | 159 | objectPath.append(hwmon::getNamespace(attrs)); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 160 | objectPath.append(1, '/'); |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 161 | objectPath.append(std::get<sensorLabel>(properties)); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 162 | |
| 163 | ObjectInfo info(&_bus, std::move(objectPath), Object()); |
Patrick Venture | 75e56c6 | 2018-04-20 18:10:15 -0700 | [diff] [blame] | 164 | RetryIO retryIO(hwmonio::retries, hwmonio::delay); |
Matthew Barth | d4beecf | 2018-04-03 15:50:22 -0500 | [diff] [blame] | 165 | if (rmSensors.find(sensor.first) != rmSensors.end()) |
| 166 | { |
| 167 | // When adding a sensor that was purposely removed, |
| 168 | // don't retry on errors when reading its value |
| 169 | std::get<size_t>(retryIO) = 0; |
| 170 | } |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 171 | auto valueInterface = static_cast< |
| 172 | std::shared_ptr<ValueObject>>(nullptr); |
| 173 | try |
| 174 | { |
Matthew Barth | ca44c2e | 2018-04-24 15:33:25 -0500 | [diff] [blame] | 175 | // Add status interface based on _fault file being present |
Matthew Barth | 2e41b13 | 2018-05-07 14:15:45 -0500 | [diff] [blame] | 176 | sensorObj->addStatus(info); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 177 | valueInterface = sensorObj->addValue(retryIO, info); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 178 | } |
| 179 | catch (const std::system_error& e) |
| 180 | { |
Matthew Barth | 38c74e7 | 2018-04-02 12:41:26 -0500 | [diff] [blame] | 181 | auto file = sysfs::make_sysfs_path( |
| 182 | ioAccess.path(), |
| 183 | sensor.first.first, |
| 184 | sensor.first.second, |
| 185 | hwmon::entry::cinput); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 186 | #ifndef REMOVE_ON_FAIL |
| 187 | // Check sensorAdjusts for sensor removal RCs |
Matthew Barth | ac47309 | 2018-05-07 14:41:46 -0500 | [diff] [blame] | 188 | auto& sAdjusts = sensorObj->getAdjusts(); |
| 189 | if (sAdjusts.rmRCs.count(e.code().value()) > 0) |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 190 | { |
Matthew Barth | ac47309 | 2018-05-07 14:41:46 -0500 | [diff] [blame] | 191 | // Return code found in sensor return code removal list |
| 192 | if (rmSensors.find(sensor.first) == rmSensors.end()) |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 193 | { |
Matthew Barth | ac47309 | 2018-05-07 14:41:46 -0500 | [diff] [blame] | 194 | // Trace for sensor not already removed from dbus |
| 195 | log<level::INFO>("Sensor not added to dbus for read fail", |
| 196 | entry("FILE=%s", file.c_str()), |
| 197 | entry("RC=%d", e.code().value())); |
| 198 | rmSensors[std::move(sensor.first)] = |
| 199 | std::move(sensor.second); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 200 | } |
Matthew Barth | ac47309 | 2018-05-07 14:41:46 -0500 | [diff] [blame] | 201 | return {}; |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 202 | } |
| 203 | #endif |
| 204 | using namespace sdbusplus::xyz::openbmc_project:: |
| 205 | Sensor::Device::Error; |
| 206 | report<ReadFailure>( |
| 207 | xyz::openbmc_project::Sensor::Device:: |
| 208 | ReadFailure::CALLOUT_ERRNO(e.code().value()), |
| 209 | xyz::openbmc_project::Sensor::Device:: |
| 210 | ReadFailure::CALLOUT_DEVICE_PATH(_devPath.c_str())); |
| 211 | |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 212 | log<level::INFO>("Logging failing sysfs file", |
| 213 | entry("FILE=%s", file.c_str())); |
| 214 | #ifdef REMOVE_ON_FAIL |
Matthew Barth | d238e23 | 2018-04-17 12:01:50 -0500 | [diff] [blame] | 215 | return {}; /* skip adding this sensor for now. */ |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 216 | #else |
| 217 | exit(EXIT_FAILURE); |
| 218 | #endif |
| 219 | } |
| 220 | auto sensorValue = valueInterface->value(); |
Matthew Barth | 979c806 | 2018-04-17 11:37:15 -0500 | [diff] [blame] | 221 | addThreshold<WarningObject>(sensor.first.first, |
| 222 | std::get<sensorID>(properties), |
| 223 | sensorValue, |
| 224 | info); |
| 225 | addThreshold<CriticalObject>(sensor.first.first, |
| 226 | std::get<sensorID>(properties), |
| 227 | sensorValue, |
| 228 | info); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 229 | |
Matthew Barth | 28f8e66 | 2018-03-26 16:57:36 -0500 | [diff] [blame] | 230 | auto target = addTarget<hwmon::FanSpeed>( |
| 231 | sensor.first, ioAccess, _devPath, info); |
| 232 | if (target) |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 233 | { |
Matthew Barth | 28f8e66 | 2018-03-26 16:57:36 -0500 | [diff] [blame] | 234 | target->enable(); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 235 | } |
Matthew Barth | 28f8e66 | 2018-03-26 16:57:36 -0500 | [diff] [blame] | 236 | addTarget<hwmon::FanPwm>(sensor.first, ioAccess, _devPath, info); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 237 | |
| 238 | // All the interfaces have been created. Go ahead |
| 239 | // and emit InterfacesAdded. |
| 240 | valueInterface->emit_object_added(); |
| 241 | |
Matthew Barth | 9c43106 | 2018-05-07 13:55:29 -0500 | [diff] [blame] | 242 | // Save sensor object specifications |
| 243 | sensorObjects[sensor.first] = std::move(sensorObj); |
| 244 | |
Matthew Barth | d238e23 | 2018-04-17 12:01:50 -0500 | [diff] [blame] | 245 | return std::make_pair(std::move(std::get<sensorLabel>(properties)), |
| 246 | std::move(info)); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 247 | } |
| 248 | |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 249 | MainLoop::MainLoop( |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 250 | sdbusplus::bus::bus&& bus, |
Patrick Venture | c897d8b | 2018-04-23 19:01:56 -0700 | [diff] [blame] | 251 | const std::string& param, |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 252 | const std::string& path, |
Brad Bishop | f3aa9ae | 2017-08-25 09:56:02 -0400 | [diff] [blame] | 253 | const std::string& devPath, |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 254 | const char* prefix, |
| 255 | const char* root) |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 256 | : _bus(std::move(bus)), |
Brad Bishop | 03e8735 | 2017-03-07 00:12:22 -0500 | [diff] [blame] | 257 | _manager(_bus, root), |
Patrick Venture | c897d8b | 2018-04-23 19:01:56 -0700 | [diff] [blame] | 258 | _pathParam(param), |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 259 | _hwmonRoot(), |
| 260 | _instance(), |
Brad Bishop | f3aa9ae | 2017-08-25 09:56:02 -0400 | [diff] [blame] | 261 | _devPath(devPath), |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 262 | _prefix(prefix), |
Brad Bishop | 3c344d3 | 2017-01-05 11:48:39 -0500 | [diff] [blame] | 263 | _root(root), |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 264 | state(), |
| 265 | ioAccess(path) |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 266 | { |
Patrick Venture | 73a50c7 | 2018-04-17 15:19:03 -0700 | [diff] [blame] | 267 | // Strip off any trailing slashes. |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 268 | std::string p = path; |
| 269 | while (!p.empty() && p.back() == '/') |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 270 | { |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 271 | p.pop_back(); |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 272 | } |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 273 | |
Patrick Venture | 73a50c7 | 2018-04-17 15:19:03 -0700 | [diff] [blame] | 274 | // Given the furthest right /, set instance to |
| 275 | // the basename, and hwmonRoot to the leading path. |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 276 | auto n = p.rfind('/'); |
| 277 | if (n != std::string::npos) |
| 278 | { |
| 279 | _instance.assign(p.substr(n + 1)); |
| 280 | _hwmonRoot.assign(p.substr(0, n)); |
| 281 | } |
| 282 | |
| 283 | assert(!_instance.empty()); |
| 284 | assert(!_hwmonRoot.empty()); |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | void MainLoop::shutdown() noexcept |
| 288 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 289 | timer->state(phosphor::hwmon::timer::OFF); |
| 290 | sd_event_exit(loop, 0); |
| 291 | loop = nullptr; |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | void MainLoop::run() |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 295 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 296 | init(); |
| 297 | |
| 298 | sd_event_default(&loop); |
| 299 | |
| 300 | std::function<void()> callback(std::bind( |
| 301 | &MainLoop::read, this)); |
| 302 | try |
| 303 | { |
| 304 | timer = std::make_unique<phosphor::hwmon::Timer>( |
| 305 | loop, callback, |
| 306 | std::chrono::microseconds(_interval), |
| 307 | phosphor::hwmon::timer::ON); |
| 308 | |
| 309 | // TODO: Issue#6 - Optionally look at polling interval sysfs entry. |
| 310 | |
| 311 | // TODO: Issue#7 - Should probably periodically check the SensorSet |
| 312 | // for new entries. |
| 313 | |
| 314 | _bus.attach_event(loop, SD_EVENT_PRIORITY_IMPORTANT); |
| 315 | sd_event_loop(loop); |
| 316 | } |
| 317 | catch (const std::system_error& e) |
| 318 | { |
| 319 | log<level::ERR>("Error in sysfs polling loop", |
| 320 | entry("ERROR=%s", e.what())); |
| 321 | throw; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | void MainLoop::init() |
| 326 | { |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 327 | // Check sysfs for available sensors. |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 328 | auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance); |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 329 | |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 330 | for (auto& i : *sensors) |
| 331 | { |
Matthew Barth | d238e23 | 2018-04-17 12:01:50 -0500 | [diff] [blame] | 332 | auto object = getObject(i); |
| 333 | if (object) |
| 334 | { |
| 335 | // Construct the SensorSet value |
| 336 | // std::tuple<SensorSet::mapped_type, |
| 337 | // std::string(Sensor Label), |
| 338 | // ObjectInfo> |
| 339 | auto value = std::make_tuple(std::move(i.second), |
| 340 | std::move((*object).first), |
| 341 | std::move((*object).second)); |
| 342 | |
| 343 | state[std::move(i.first)] = std::move(value); |
| 344 | } |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 345 | } |
| 346 | |
Patrick Venture | 62503a4 | 2017-05-23 07:30:29 -0700 | [diff] [blame] | 347 | /* If there are no sensors specified by labels, exit. */ |
| 348 | if (0 == state.size()) |
| 349 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 350 | exit(0); |
Patrick Venture | 62503a4 | 2017-05-23 07:30:29 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 353 | { |
Patrick Venture | c897d8b | 2018-04-23 19:01:56 -0700 | [diff] [blame] | 354 | std::stringstream ss; |
| 355 | ss << _prefix |
| 356 | << "-" |
| 357 | << std::to_string(std::hash<std::string>{}(_devPath + _pathParam)) |
| 358 | << ".Hwmon1"; |
| 359 | |
| 360 | _bus.request_name(ss.str().c_str()); |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 361 | } |
| 362 | |
Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame] | 363 | { |
Patrick Venture | a24c880 | 2018-04-17 19:38:06 -0700 | [diff] [blame] | 364 | auto interval = env::getEnv("INTERVAL"); |
| 365 | if (!interval.empty()) |
Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame] | 366 | { |
Patrick Venture | 50cf1c5 | 2018-04-18 09:21:41 -0700 | [diff] [blame] | 367 | _interval = std::strtoull(interval.c_str(), NULL, 10); |
Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame] | 368 | } |
| 369 | } |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 370 | } |
Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame] | 371 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 372 | void MainLoop::read() |
| 373 | { |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 374 | // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to |
| 375 | // ensure the objects all exist? |
| 376 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 377 | // Iterate through all the sensors. |
| 378 | for (auto& i : state) |
| 379 | { |
| 380 | auto& attrs = std::get<0>(i.second); |
| 381 | if (attrs.find(hwmon::entry::input) != attrs.end()) |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 382 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 383 | // Read value from sensor. |
| 384 | int64_t value; |
| 385 | std::string input = hwmon::entry::cinput; |
| 386 | if (i.first.first == "pwm") { |
| 387 | input = ""; |
| 388 | } |
| 389 | |
| 390 | try |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 391 | { |
Matthew Barth | 27c4a39 | 2018-04-25 14:38:51 -0500 | [diff] [blame] | 392 | auto& objInfo = std::get<ObjectInfo>(i.second); |
| 393 | auto& obj = std::get<Object>(objInfo); |
| 394 | |
| 395 | auto it = obj.find(InterfaceType::STATUS); |
| 396 | if (it != obj.end()) |
| 397 | { |
Matthew Barth | bfcaf3d | 2018-04-25 15:05:58 -0500 | [diff] [blame] | 398 | auto fault = ioAccess.read( |
| 399 | i.first.first, |
| 400 | i.first.second, |
| 401 | hwmon::entry::fault, |
| 402 | hwmonio::retries, |
| 403 | hwmonio::delay); |
Matthew Barth | 27c4a39 | 2018-04-25 14:38:51 -0500 | [diff] [blame] | 404 | auto statusIface = std::experimental::any_cast< |
| 405 | std::shared_ptr<StatusObject>>(it->second); |
Matthew Barth | bfcaf3d | 2018-04-25 15:05:58 -0500 | [diff] [blame] | 406 | if (!statusIface->functional((fault == 0) ? true : false)) |
Matthew Barth | 27c4a39 | 2018-04-25 14:38:51 -0500 | [diff] [blame] | 407 | { |
| 408 | continue; |
| 409 | } |
| 410 | } |
| 411 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 412 | // Retry for up to a second if device is busy |
| 413 | // or has a transient error. |
Patrick Venture | 9331ab7 | 2018-01-29 09:48:47 -0800 | [diff] [blame] | 414 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 415 | value = ioAccess.read( |
| 416 | i.first.first, |
| 417 | i.first.second, |
| 418 | input, |
Patrick Venture | 75e56c6 | 2018-04-20 18:10:15 -0700 | [diff] [blame] | 419 | hwmonio::retries, |
Matthew Barth | 0b30505 | 2018-04-26 09:46:49 -0500 | [diff] [blame] | 420 | hwmonio::delay); |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 421 | |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 422 | value = sensorObjects[i.first]->adjustValue(value); |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 423 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 424 | for (auto& iface : obj) |
Brad Bishop | dddb715 | 2017-01-06 09:54:23 -0500 | [diff] [blame] | 425 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 426 | auto valueIface = std::shared_ptr<ValueObject>(); |
| 427 | auto warnIface = std::shared_ptr<WarningObject>(); |
| 428 | auto critIface = std::shared_ptr<CriticalObject>(); |
Brad Bishop | 754d38c | 2017-09-08 00:46:58 -0400 | [diff] [blame] | 429 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 430 | switch (iface.first) |
Brad Bishop | e0b7d05 | 2017-01-06 15:30:23 -0500 | [diff] [blame] | 431 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 432 | case InterfaceType::VALUE: |
| 433 | valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>> |
| 434 | (iface.second); |
| 435 | valueIface->value(value); |
| 436 | break; |
| 437 | case InterfaceType::WARN: |
| 438 | checkThresholds<WarningObject>(iface.second, value); |
| 439 | break; |
| 440 | case InterfaceType::CRIT: |
| 441 | checkThresholds<CriticalObject>(iface.second, value); |
| 442 | break; |
| 443 | default: |
| 444 | break; |
Brad Bishop | e0b7d05 | 2017-01-06 15:30:23 -0500 | [diff] [blame] | 445 | } |
Brad Bishop | dddb715 | 2017-01-06 09:54:23 -0500 | [diff] [blame] | 446 | } |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 447 | } |
| 448 | catch (const std::system_error& e) |
| 449 | { |
Matthew Barth | 38c74e7 | 2018-04-02 12:41:26 -0500 | [diff] [blame] | 450 | auto file = sysfs::make_sysfs_path( |
| 451 | ioAccess.path(), |
| 452 | i.first.first, |
| 453 | i.first.second, |
| 454 | hwmon::entry::cinput); |
Matthew Barth | 8772ce3 | 2018-03-22 16:03:06 -0500 | [diff] [blame] | 455 | #ifndef REMOVE_ON_FAIL |
| 456 | // Check sensorAdjusts for sensor removal RCs |
Matthew Barth | ac47309 | 2018-05-07 14:41:46 -0500 | [diff] [blame] | 457 | auto& sAdjusts = sensorObjects[i.first]->getAdjusts(); |
| 458 | if (sAdjusts.rmRCs.count(e.code().value()) > 0) |
Matthew Barth | 8772ce3 | 2018-03-22 16:03:06 -0500 | [diff] [blame] | 459 | { |
Matthew Barth | ac47309 | 2018-05-07 14:41:46 -0500 | [diff] [blame] | 460 | // Return code found in sensor return code removal list |
| 461 | if (rmSensors.find(i.first) == rmSensors.end()) |
Matthew Barth | 8772ce3 | 2018-03-22 16:03:06 -0500 | [diff] [blame] | 462 | { |
Matthew Barth | ac47309 | 2018-05-07 14:41:46 -0500 | [diff] [blame] | 463 | // Trace for sensor not already removed from dbus |
| 464 | log<level::INFO>( |
| 465 | "Remove sensor from dbus for read fail", |
| 466 | entry("FILE=%s", file.c_str()), |
| 467 | entry("RC=%d", e.code().value())); |
| 468 | // Mark this sensor to be removed from dbus |
| 469 | rmSensors[i.first] = std::get<0>(i.second); |
Matthew Barth | 8772ce3 | 2018-03-22 16:03:06 -0500 | [diff] [blame] | 470 | } |
Matthew Barth | ac47309 | 2018-05-07 14:41:46 -0500 | [diff] [blame] | 471 | continue; |
Matthew Barth | 8772ce3 | 2018-03-22 16:03:06 -0500 | [diff] [blame] | 472 | } |
| 473 | #endif |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 474 | using namespace sdbusplus::xyz::openbmc_project:: |
| 475 | Sensor::Device::Error; |
| 476 | report<ReadFailure>( |
| 477 | xyz::openbmc_project::Sensor::Device:: |
| 478 | ReadFailure::CALLOUT_ERRNO(e.code().value()), |
| 479 | xyz::openbmc_project::Sensor::Device:: |
| 480 | ReadFailure::CALLOUT_DEVICE_PATH( |
| 481 | _devPath.c_str())); |
Matt Spinler | 9b65f76 | 2017-10-05 10:36:22 -0500 | [diff] [blame] | 482 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 483 | log<level::INFO>("Logging failing sysfs file", |
| 484 | entry("FILE=%s", file.c_str())); |
Matt Spinler | 9b65f76 | 2017-10-05 10:36:22 -0500 | [diff] [blame] | 485 | |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 486 | #ifdef REMOVE_ON_FAIL |
Matthew Barth | 33db92b | 2018-03-26 09:55:19 -0500 | [diff] [blame] | 487 | rmSensors[i.first] = std::get<0>(i.second); |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 488 | #else |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 489 | exit(EXIT_FAILURE); |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 490 | #endif |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 491 | } |
| 492 | } |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 493 | } |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 494 | |
Matthew Barth | 8772ce3 | 2018-03-22 16:03:06 -0500 | [diff] [blame] | 495 | // Remove any sensors marked for removal |
Matthew Barth | 33db92b | 2018-03-26 09:55:19 -0500 | [diff] [blame] | 496 | for (auto& i : rmSensors) |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 497 | { |
Matthew Barth | 33db92b | 2018-03-26 09:55:19 -0500 | [diff] [blame] | 498 | state.erase(i.first); |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 499 | } |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 500 | |
| 501 | #ifndef REMOVE_ON_FAIL |
| 502 | // Attempt to add any sensors that were removed |
| 503 | auto it = rmSensors.begin(); |
| 504 | while (it != rmSensors.end()) |
| 505 | { |
| 506 | if (state.find(it->first) == state.end()) |
| 507 | { |
| 508 | SensorSet::container_t::value_type ssValueType = |
| 509 | std::make_pair(it->first, it->second); |
Matthew Barth | d238e23 | 2018-04-17 12:01:50 -0500 | [diff] [blame] | 510 | auto object = getObject(ssValueType); |
| 511 | if (object) |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 512 | { |
Matthew Barth | d238e23 | 2018-04-17 12:01:50 -0500 | [diff] [blame] | 513 | // Construct the SensorSet value |
| 514 | // std::tuple<SensorSet::mapped_type, |
| 515 | // std::string(Sensor Label), |
| 516 | // ObjectInfo> |
| 517 | auto value = std::make_tuple(std::move(ssValueType.second), |
| 518 | std::move((*object).first), |
| 519 | std::move((*object).second)); |
| 520 | |
| 521 | state[std::move(ssValueType.first)] = std::move(value); |
| 522 | |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 523 | // Sensor object added, erase entry from removal list |
Matthew Barth | 38c74e7 | 2018-04-02 12:41:26 -0500 | [diff] [blame] | 524 | auto file = sysfs::make_sysfs_path( |
| 525 | ioAccess.path(), |
| 526 | it->first.first, |
| 527 | it->first.second, |
| 528 | hwmon::entry::cinput); |
| 529 | log<level::INFO>( |
| 530 | "Added sensor to dbus after successful read", |
| 531 | entry("FILE=%s", file.c_str())); |
Matthew Barth | 31d214c | 2018-03-26 09:54:27 -0500 | [diff] [blame] | 532 | it = rmSensors.erase(it); |
| 533 | } |
| 534 | else |
| 535 | { |
| 536 | ++it; |
| 537 | } |
| 538 | } |
| 539 | else |
| 540 | { |
| 541 | // Sanity check to remove sensors that were re-added |
| 542 | it = rmSensors.erase(it); |
| 543 | } |
| 544 | } |
| 545 | #endif |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |