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 | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 22 | |
| 23 | #include <phosphor-logging/elog-errors.hpp> |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 24 | #include "config.h" |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 25 | #include "sensorset.hpp" |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 26 | #include "hwmon.hpp" |
| 27 | #include "sysfs.hpp" |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 28 | #include "mainloop.hpp" |
Brad Bishop | f3df6b4 | 2017-01-06 10:14:09 -0500 | [diff] [blame] | 29 | #include "env.hpp" |
Brad Bishop | e0b7d05 | 2017-01-06 15:30:23 -0500 | [diff] [blame] | 30 | #include "thresholds.hpp" |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 31 | #include "targets.hpp" |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame] | 32 | #include "fan_speed.hpp" |
Patrick Venture | 9331ab7 | 2018-01-29 09:48:47 -0800 | [diff] [blame] | 33 | #include "fan_pwm.hpp" |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 34 | |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 35 | #include <xyz/openbmc_project/Sensor/Device/error.hpp> |
| 36 | |
| 37 | using namespace phosphor::logging; |
| 38 | |
Saqib Khan | 973886d | 2017-03-15 14:01:16 -0500 | [diff] [blame] | 39 | // Initialization for Warning Objects |
| 40 | decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo = |
| 41 | &WarningObject::warningLow; |
| 42 | decltype(Thresholds<WarningObject>::setHi) Thresholds<WarningObject>::setHi = |
| 43 | &WarningObject::warningHigh; |
| 44 | decltype(Thresholds<WarningObject>::getLo) Thresholds<WarningObject>::getLo = |
| 45 | &WarningObject::warningLow; |
| 46 | decltype(Thresholds<WarningObject>::getHi) Thresholds<WarningObject>::getHi = |
| 47 | &WarningObject::warningHigh; |
| 48 | decltype(Thresholds<WarningObject>::alarmLo) Thresholds<WarningObject>::alarmLo = |
| 49 | &WarningObject::warningAlarmLow; |
| 50 | decltype(Thresholds<WarningObject>::alarmHi) Thresholds<WarningObject>::alarmHi = |
| 51 | &WarningObject::warningAlarmHigh; |
| 52 | |
| 53 | // Initialization for Critical Objects |
| 54 | decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo = |
| 55 | &CriticalObject::criticalLow; |
| 56 | decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi = |
| 57 | &CriticalObject::criticalHigh; |
| 58 | decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo = |
| 59 | &CriticalObject::criticalLow; |
| 60 | decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi = |
| 61 | &CriticalObject::criticalHigh; |
| 62 | decltype(Thresholds<CriticalObject>::alarmLo) Thresholds<CriticalObject>::alarmLo = |
| 63 | &CriticalObject::criticalAlarmLow; |
| 64 | decltype(Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi = |
| 65 | &CriticalObject::criticalAlarmHigh; |
| 66 | |
Chiabing Lee | c923ce9 | 2017-09-06 15:58:26 +0800 | [diff] [blame] | 67 | // The gain and offset to adjust a value |
| 68 | struct valueAdjust |
| 69 | { |
| 70 | double gain = 1.0; |
| 71 | int offset = 0; |
Matthew Barth | d26e277 | 2018-03-22 14:24:06 -0500 | [diff] [blame^] | 72 | std::unordered_set<int> rmRCs; |
Chiabing Lee | c923ce9 | 2017-09-06 15:58:26 +0800 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | // Store the valueAdjust for sensors |
| 76 | std::map<SensorSet::key_type, valueAdjust> sensorAdjusts; |
| 77 | |
Brad Bishop | 74aa4dd | 2017-01-06 09:50:31 -0500 | [diff] [blame] | 78 | static 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 Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 86 | -3, |
| 87 | "temperature"), |
Brad Bishop | 74aa4dd | 2017-01-06 09:50:31 -0500 | [diff] [blame] | 88 | std::make_tuple( |
| 89 | hwmon::type::cfan, |
| 90 | ValueInterface::Unit::RPMS, |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 91 | 0, |
| 92 | "fan_tach"), |
Brad Bishop | 74aa4dd | 2017-01-06 09:50:31 -0500 | [diff] [blame] | 93 | std::make_tuple( |
| 94 | hwmon::type::cvolt, |
| 95 | ValueInterface::Unit::Volts, |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 96 | -3, |
| 97 | "voltage"), |
Brad Bishop | 5afe21a | 2017-01-06 20:44:05 -0500 | [diff] [blame] | 98 | std::make_tuple( |
| 99 | hwmon::type::ccurr, |
| 100 | ValueInterface::Unit::Amperes, |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 101 | -3, |
| 102 | "current"), |
Brad Bishop | 5afe21a | 2017-01-06 20:44:05 -0500 | [diff] [blame] | 103 | std::make_tuple( |
| 104 | hwmon::type::cenergy, |
| 105 | ValueInterface::Unit::Joules, |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 106 | -6, |
| 107 | "energy"), |
Brad Bishop | 5afe21a | 2017-01-06 20:44:05 -0500 | [diff] [blame] | 108 | std::make_tuple( |
| 109 | hwmon::type::cpower, |
| 110 | ValueInterface::Unit::Watts, |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 111 | -6, |
| 112 | "power"), |
Brad Bishop | 74aa4dd | 2017-01-06 09:50:31 -0500 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | auto getHwmonType(decltype(typeAttrMap)::const_reference attrs) |
| 116 | { |
| 117 | return std::get<0>(attrs); |
| 118 | } |
| 119 | |
| 120 | auto getUnit(decltype(typeAttrMap)::const_reference attrs) |
| 121 | { |
| 122 | return std::get<1>(attrs); |
| 123 | } |
| 124 | |
| 125 | auto getScale(decltype(typeAttrMap)::const_reference attrs) |
| 126 | { |
| 127 | return std::get<2>(attrs); |
| 128 | } |
| 129 | |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 130 | auto getNamespace(decltype(typeAttrMap)::const_reference attrs) |
| 131 | { |
| 132 | return std::get<3>(attrs); |
| 133 | } |
| 134 | |
Brad Bishop | 951a79e | 2017-01-06 21:55:11 -0500 | [diff] [blame] | 135 | using AttributeIterator = decltype(*typeAttrMap.begin()); |
| 136 | using Attributes |
| 137 | = std::remove_cv<std::remove_reference<AttributeIterator>::type>::type; |
| 138 | |
| 139 | auto 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 Barth | d26e277 | 2018-03-22 14:24:06 -0500 | [diff] [blame^] | 160 | void 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 Spinler | fee106b | 2017-11-29 15:18:05 -0600 | [diff] [blame] | 186 | int64_t adjustValue(const SensorSet::key_type& sensor, int64_t value) |
Chiabing Lee | c923ce9 | 2017-09-06 15:58:26 +0800 | [diff] [blame] | 187 | { |
Patrick Venture | c1cece7 | 2017-11-07 12:09:49 -0800 | [diff] [blame] | 188 | // 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 Lee | c923ce9 | 2017-09-06 15:58:26 +0800 | [diff] [blame] | 198 | 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 Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 209 | auto addValue(const SensorSet::key_type& sensor, |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 210 | const std::string& devPath, |
| 211 | sysfs::hwmonio::HwmonIO& ioAccess, |
Matthew Barth | a23babd | 2018-03-16 10:03:27 -0500 | [diff] [blame] | 212 | ObjectInfo& info, |
| 213 | bool isOCC = false) |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 214 | { |
Brad Bishop | 30dbcee | 2017-01-18 07:55:42 -0500 | [diff] [blame] | 215 | static constexpr bool deferSignals = true; |
| 216 | |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 217 | // 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 Barth | d26e277 | 2018-03-22 14:24:06 -0500 | [diff] [blame^] | 222 | 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 Spinler | fee106b | 2017-11-29 15:18:05 -0600 | [diff] [blame] | 229 | int64_t val = 0; |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 230 | try |
| 231 | { |
| 232 | // Retry for up to a second if device is busy |
Brad Bishop | 754d38c | 2017-09-08 00:46:58 -0400 | [diff] [blame] | 233 | // or has a transient error. |
| 234 | val = ioAccess.read( |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 235 | sensor.first, |
| 236 | sensor.second, |
Brad Bishop | 754d38c | 2017-09-08 00:46:58 -0400 | [diff] [blame] | 237 | hwmon::entry::cinput, |
| 238 | sysfs::hwmonio::retries, |
Matthew Barth | a23babd | 2018-03-16 10:03:27 -0500 | [diff] [blame] | 239 | sysfs::hwmonio::delay, |
| 240 | isOCC); |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 241 | } |
| 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 Spinler | 9b65f76 | 2017-10-05 10:36:22 -0500 | [diff] [blame] | 251 | 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 Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 260 | return static_cast<std::shared_ptr<ValueObject>>(nullptr); |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Chiabing Lee | c923ce9 | 2017-09-06 15:58:26 +0800 | [diff] [blame] | 263 | 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 Bishop | 30dbcee | 2017-01-18 07:55:42 -0500 | [diff] [blame] | 277 | auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals); |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 278 | iface->value(val); |
| 279 | |
Brad Bishop | 951a79e | 2017-01-06 21:55:11 -0500 | [diff] [blame] | 280 | Attributes attrs; |
| 281 | if (getAttributes(sensor.first, attrs)) |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 282 | { |
Brad Bishop | 951a79e | 2017-01-06 21:55:11 -0500 | [diff] [blame] | 283 | iface->unit(getUnit(attrs)); |
| 284 | iface->scale(getScale(attrs)); |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 285 | } |
| 286 | |
James Feist | 7bd5fcf | 2018-01-22 16:14:28 -0800 | [diff] [blame] | 287 | 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 Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 298 | obj[InterfaceType::VALUE] = iface; |
| 299 | return iface; |
| 300 | } |
| 301 | |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 302 | MainLoop::MainLoop( |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 303 | sdbusplus::bus::bus&& bus, |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 304 | const std::string& path, |
Brad Bishop | f3aa9ae | 2017-08-25 09:56:02 -0400 | [diff] [blame] | 305 | const std::string& devPath, |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 306 | const char* prefix, |
| 307 | const char* root) |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 308 | : _bus(std::move(bus)), |
Brad Bishop | 03e8735 | 2017-03-07 00:12:22 -0500 | [diff] [blame] | 309 | _manager(_bus, root), |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 310 | _hwmonRoot(), |
| 311 | _instance(), |
Brad Bishop | f3aa9ae | 2017-08-25 09:56:02 -0400 | [diff] [blame] | 312 | _devPath(devPath), |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 313 | _prefix(prefix), |
Brad Bishop | 3c344d3 | 2017-01-05 11:48:39 -0500 | [diff] [blame] | 314 | _root(root), |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 315 | state(), |
| 316 | ioAccess(path) |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 317 | { |
Matthew Barth | a23babd | 2018-03-16 10:03:27 -0500 | [diff] [blame] | 318 | if (path.find("occ") != std::string::npos) |
| 319 | { |
| 320 | _isOCC = true; |
| 321 | } |
| 322 | |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 323 | std::string p = path; |
| 324 | while (!p.empty() && p.back() == '/') |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 325 | { |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 326 | p.pop_back(); |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 327 | } |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 328 | |
| 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 Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | void MainLoop::shutdown() noexcept |
| 341 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 342 | timer->state(phosphor::hwmon::timer::OFF); |
| 343 | sd_event_exit(loop, 0); |
| 344 | loop = nullptr; |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | void MainLoop::run() |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 348 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 349 | 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 | |
| 378 | void MainLoop::init() |
| 379 | { |
Matt Spinler | a7e2c1e | 2018-02-23 12:18:19 -0600 | [diff] [blame] | 380 | //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 Barth | d26e277 | 2018-03-22 14:24:06 -0500 | [diff] [blame^] | 406 | // 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 Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 414 | // Check sysfs for available sensors. |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 415 | auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance); |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 416 | |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 417 | for (auto& i : *sensors) |
| 418 | { |
Tom Joseph | 1f8a958 | 2017-06-12 20:10:59 +0530 | [diff] [blame] | 419 | std::string label; |
Matt Spinler | 4b68c4e | 2017-10-12 16:44:53 -0500 | [diff] [blame] | 420 | std::string id; |
Brad Bishop | f3df6b4 | 2017-01-06 10:14:09 -0500 | [diff] [blame] | 421 | |
Tom Joseph | 1f8a958 | 2017-06-12 20:10:59 +0530 | [diff] [blame] | 422 | /* |
| 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 Spinler | 4b68c4e | 2017-10-12 16:44:53 -0500 | [diff] [blame] | 427 | * doesn't exist, then the name of DBUS object is the value of the env |
Tom Joseph | 1f8a958 | 2017-06-12 20:10:59 +0530 | [diff] [blame] | 428 | * variable LABEL_<item><X>. |
| 429 | */ |
| 430 | auto mode = getEnv("MODE", i.first); |
| 431 | if (!mode.compare(hwmon::entry::label)) |
Brad Bishop | 73831cd | 2017-01-06 09:37:22 -0500 | [diff] [blame] | 432 | { |
Matt Spinler | 4b68c4e | 2017-10-12 16:44:53 -0500 | [diff] [blame] | 433 | id = getIndirectID( |
| 434 | _hwmonRoot + '/' + _instance + '/', i.first); |
| 435 | |
| 436 | if (id.empty()) |
Tom Joseph | 1f8a958 | 2017-06-12 20:10:59 +0530 | [diff] [blame] | 437 | { |
| 438 | continue; |
| 439 | } |
| 440 | } |
Matt Spinler | 4b68c4e | 2017-10-12 16:44:53 -0500 | [diff] [blame] | 441 | |
| 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 Joseph | 1f8a958 | 2017-06-12 20:10:59 +0530 | [diff] [blame] | 449 | { |
Matt Spinler | 4b68c4e | 2017-10-12 16:44:53 -0500 | [diff] [blame] | 450 | continue; |
Brad Bishop | 73831cd | 2017-01-06 09:37:22 -0500 | [diff] [blame] | 451 | } |
| 452 | |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 453 | Attributes attrs; |
| 454 | if (!getAttributes(i.first.first, attrs)) |
| 455 | { |
| 456 | continue; |
| 457 | } |
| 458 | |
Matthew Barth | d26e277 | 2018-03-22 14:24:06 -0500 | [diff] [blame^] | 459 | if (!deviceRmRCs.empty()) |
| 460 | { |
| 461 | // Add sensor removal return codes defined at the device level |
| 462 | addRemoveRCs(i.first, deviceRmRCs); |
| 463 | } |
| 464 | |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 465 | std::string objectPath{_root}; |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 466 | objectPath.append(1, '/'); |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 467 | objectPath.append(getNamespace(attrs)); |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 468 | objectPath.append(1, '/'); |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 469 | objectPath.append(label); |
| 470 | |
Brad Bishop | f7426cf | 2017-01-06 15:36:43 -0500 | [diff] [blame] | 471 | ObjectInfo info(&_bus, std::move(objectPath), Object()); |
Matthew Barth | a23babd | 2018-03-16 10:03:27 -0500 | [diff] [blame] | 472 | auto valueInterface = addValue(i.first, _devPath, ioAccess, info, |
| 473 | _isOCC); |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 474 | if (!valueInterface) |
| 475 | { |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 476 | #ifdef REMOVE_ON_FAIL |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 477 | continue; /* skip adding this sensor for now. */ |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 478 | #else |
| 479 | exit(EXIT_FAILURE); |
| 480 | #endif |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 481 | } |
Brad Bishop | e0b7d05 | 2017-01-06 15:30:23 -0500 | [diff] [blame] | 482 | auto sensorValue = valueInterface->value(); |
Matt Spinler | ccfc77b | 2017-10-12 16:49:24 -0500 | [diff] [blame] | 483 | addThreshold<WarningObject>(i.first.first, id, sensorValue, info); |
| 484 | addThreshold<CriticalObject>(i.first.first, id, sensorValue, info); |
Matt Spinler | a3eb8e3 | 2017-10-12 16:42:49 -0500 | [diff] [blame] | 485 | |
Matt Spinler | a7e2c1e | 2018-02-23 12:18:19 -0600 | [diff] [blame] | 486 | if ((fanTargetType == targetType::RPM) || |
| 487 | (fanTargetType == targetType::DEFAULT)) |
Matt Spinler | 0a8de64 | 2017-05-11 10:59:39 -0500 | [diff] [blame] | 488 | { |
Matt Spinler | a7e2c1e | 2018-02-23 12:18:19 -0600 | [diff] [blame] | 489 | auto target = addTarget<hwmon::FanSpeed>( |
| 490 | i.first, ioAccess, _devPath, info); |
| 491 | |
| 492 | if (target) |
| 493 | { |
| 494 | target->enable(); |
| 495 | } |
Matt Spinler | 0a8de64 | 2017-05-11 10:59:39 -0500 | [diff] [blame] | 496 | } |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 497 | |
Matt Spinler | a7e2c1e | 2018-02-23 12:18:19 -0600 | [diff] [blame] | 498 | if ((fanTargetType == targetType::PWM) || |
| 499 | (fanTargetType == targetType::DEFAULT)) |
| 500 | { |
| 501 | addTarget<hwmon::FanPwm>(i.first, ioAccess, _devPath, info); |
| 502 | } |
Patrick Venture | 9331ab7 | 2018-01-29 09:48:47 -0800 | [diff] [blame] | 503 | |
Brad Bishop | 30dbcee | 2017-01-18 07:55:42 -0500 | [diff] [blame] | 504 | // All the interfaces have been created. Go ahead |
| 505 | // and emit InterfacesAdded. |
| 506 | valueInterface->emit_object_added(); |
| 507 | |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 508 | auto value = std::make_tuple( |
| 509 | std::move(i.second), |
| 510 | std::move(label), |
Brad Bishop | f7426cf | 2017-01-06 15:36:43 -0500 | [diff] [blame] | 511 | std::move(info)); |
Brad Bishop | 73831cd | 2017-01-06 09:37:22 -0500 | [diff] [blame] | 512 | |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 513 | state[std::move(i.first)] = std::move(value); |
| 514 | } |
| 515 | |
Patrick Venture | 62503a4 | 2017-05-23 07:30:29 -0700 | [diff] [blame] | 516 | /* If there are no sensors specified by labels, exit. */ |
| 517 | if (0 == state.size()) |
| 518 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 519 | exit(0); |
Patrick Venture | 62503a4 | 2017-05-23 07:30:29 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 522 | { |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 523 | std::string busname{_prefix}; |
Brad Bishop | 4d9a35b | 2017-11-14 22:33:03 -0500 | [diff] [blame] | 524 | busname.append(1, '-'); |
| 525 | busname.append( |
| 526 | std::to_string(std::hash<decltype(_devPath)>{}(_devPath))); |
| 527 | busname.append(".Hwmon1"); |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 528 | _bus.request_name(busname.c_str()); |
| 529 | } |
| 530 | |
Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame] | 531 | { |
| 532 | auto interval = getenv("INTERVAL"); |
| 533 | if (interval) |
| 534 | { |
| 535 | _interval = strtoull(interval, NULL, 10); |
| 536 | } |
| 537 | } |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 538 | } |
Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame] | 539 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 540 | void MainLoop::read() |
| 541 | { |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 542 | // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to |
| 543 | // ensure the objects all exist? |
| 544 | |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 545 | #ifdef REMOVE_ON_FAIL |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 546 | std::vector<SensorSet::key_type> destroy; |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 547 | #endif |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 548 | // 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 Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 553 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 554 | // 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 Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 562 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 563 | // Retry for up to a second if device is busy |
| 564 | // or has a transient error. |
Patrick Venture | 9331ab7 | 2018-01-29 09:48:47 -0800 | [diff] [blame] | 565 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 566 | 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 Bishop | dddb715 | 2017-01-06 09:54:23 -0500 | [diff] [blame] | 580 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 581 | auto valueIface = std::shared_ptr<ValueObject>(); |
| 582 | auto warnIface = std::shared_ptr<WarningObject>(); |
| 583 | auto critIface = std::shared_ptr<CriticalObject>(); |
Brad Bishop | 754d38c | 2017-09-08 00:46:58 -0400 | [diff] [blame] | 584 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 585 | switch (iface.first) |
Brad Bishop | e0b7d05 | 2017-01-06 15:30:23 -0500 | [diff] [blame] | 586 | { |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 587 | 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 Bishop | e0b7d05 | 2017-01-06 15:30:23 -0500 | [diff] [blame] | 600 | } |
Brad Bishop | dddb715 | 2017-01-06 09:54:23 -0500 | [diff] [blame] | 601 | } |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 602 | } |
| 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 Spinler | 9b65f76 | 2017-10-05 10:36:22 -0500 | [diff] [blame] | 613 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 614 | auto file = sysfs::make_sysfs_path( |
| 615 | ioAccess.path(), |
| 616 | i.first.first, |
| 617 | i.first.second, |
| 618 | hwmon::entry::cinput); |
Matt Spinler | 9b65f76 | 2017-10-05 10:36:22 -0500 | [diff] [blame] | 619 | |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 620 | log<level::INFO>("Logging failing sysfs file", |
| 621 | entry("FILE=%s", file.c_str())); |
Matt Spinler | 9b65f76 | 2017-10-05 10:36:22 -0500 | [diff] [blame] | 622 | |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 623 | #ifdef REMOVE_ON_FAIL |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 624 | destroy.push_back(i.first); |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 625 | #else |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 626 | exit(EXIT_FAILURE); |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 627 | #endif |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 628 | } |
| 629 | } |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 630 | } |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 631 | |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 632 | #ifdef REMOVE_ON_FAIL |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 633 | for (auto& i : destroy) |
| 634 | { |
| 635 | state.erase(i); |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 636 | } |
Deepak Kodihalli | 2a51a9c | 2018-03-07 02:39:40 -0600 | [diff] [blame] | 637 | #endif |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |