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> |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 21 | |
| 22 | #include <phosphor-logging/elog-errors.hpp> |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 23 | #include "config.h" |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 24 | #include "sensorset.hpp" |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 25 | #include "hwmon.hpp" |
| 26 | #include "sysfs.hpp" |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 27 | #include "mainloop.hpp" |
Brad Bishop | f3df6b4 | 2017-01-06 10:14:09 -0500 | [diff] [blame] | 28 | #include "env.hpp" |
Brad Bishop | e0b7d05 | 2017-01-06 15:30:23 -0500 | [diff] [blame] | 29 | #include "thresholds.hpp" |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 30 | #include "targets.hpp" |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame] | 31 | #include "fan_speed.hpp" |
Patrick Venture | 9331ab7 | 2018-01-29 09:48:47 -0800 | [diff] [blame^] | 32 | #include "fan_pwm.hpp" |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 33 | |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 34 | #include <xyz/openbmc_project/Sensor/Device/error.hpp> |
| 35 | |
| 36 | using namespace phosphor::logging; |
| 37 | |
Saqib Khan | 973886d | 2017-03-15 14:01:16 -0500 | [diff] [blame] | 38 | // Initialization for Warning Objects |
| 39 | decltype(Thresholds<WarningObject>::setLo) Thresholds<WarningObject>::setLo = |
| 40 | &WarningObject::warningLow; |
| 41 | decltype(Thresholds<WarningObject>::setHi) Thresholds<WarningObject>::setHi = |
| 42 | &WarningObject::warningHigh; |
| 43 | decltype(Thresholds<WarningObject>::getLo) Thresholds<WarningObject>::getLo = |
| 44 | &WarningObject::warningLow; |
| 45 | decltype(Thresholds<WarningObject>::getHi) Thresholds<WarningObject>::getHi = |
| 46 | &WarningObject::warningHigh; |
| 47 | decltype(Thresholds<WarningObject>::alarmLo) Thresholds<WarningObject>::alarmLo = |
| 48 | &WarningObject::warningAlarmLow; |
| 49 | decltype(Thresholds<WarningObject>::alarmHi) Thresholds<WarningObject>::alarmHi = |
| 50 | &WarningObject::warningAlarmHigh; |
| 51 | |
| 52 | // Initialization for Critical Objects |
| 53 | decltype(Thresholds<CriticalObject>::setLo) Thresholds<CriticalObject>::setLo = |
| 54 | &CriticalObject::criticalLow; |
| 55 | decltype(Thresholds<CriticalObject>::setHi) Thresholds<CriticalObject>::setHi = |
| 56 | &CriticalObject::criticalHigh; |
| 57 | decltype(Thresholds<CriticalObject>::getLo) Thresholds<CriticalObject>::getLo = |
| 58 | &CriticalObject::criticalLow; |
| 59 | decltype(Thresholds<CriticalObject>::getHi) Thresholds<CriticalObject>::getHi = |
| 60 | &CriticalObject::criticalHigh; |
| 61 | decltype(Thresholds<CriticalObject>::alarmLo) Thresholds<CriticalObject>::alarmLo = |
| 62 | &CriticalObject::criticalAlarmLow; |
| 63 | decltype(Thresholds<CriticalObject>::alarmHi) Thresholds<CriticalObject>::alarmHi = |
| 64 | &CriticalObject::criticalAlarmHigh; |
| 65 | |
Chiabing Lee | c923ce9 | 2017-09-06 15:58:26 +0800 | [diff] [blame] | 66 | // The gain and offset to adjust a value |
| 67 | struct valueAdjust |
| 68 | { |
| 69 | double gain = 1.0; |
| 70 | int offset = 0; |
| 71 | }; |
| 72 | |
| 73 | // Store the valueAdjust for sensors |
| 74 | std::map<SensorSet::key_type, valueAdjust> sensorAdjusts; |
| 75 | |
Brad Bishop | 74aa4dd | 2017-01-06 09:50:31 -0500 | [diff] [blame] | 76 | static constexpr auto typeAttrMap = |
| 77 | { |
| 78 | // 1 - hwmon class |
| 79 | // 2 - unit |
| 80 | // 3 - sysfs scaling factor |
| 81 | std::make_tuple( |
| 82 | hwmon::type::ctemp, |
| 83 | ValueInterface::Unit::DegreesC, |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 84 | -3, |
| 85 | "temperature"), |
Brad Bishop | 74aa4dd | 2017-01-06 09:50:31 -0500 | [diff] [blame] | 86 | std::make_tuple( |
| 87 | hwmon::type::cfan, |
| 88 | ValueInterface::Unit::RPMS, |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 89 | 0, |
| 90 | "fan_tach"), |
Brad Bishop | 74aa4dd | 2017-01-06 09:50:31 -0500 | [diff] [blame] | 91 | std::make_tuple( |
| 92 | hwmon::type::cvolt, |
| 93 | ValueInterface::Unit::Volts, |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 94 | -3, |
| 95 | "voltage"), |
Brad Bishop | 5afe21a | 2017-01-06 20:44:05 -0500 | [diff] [blame] | 96 | std::make_tuple( |
| 97 | hwmon::type::ccurr, |
| 98 | ValueInterface::Unit::Amperes, |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 99 | -3, |
| 100 | "current"), |
Brad Bishop | 5afe21a | 2017-01-06 20:44:05 -0500 | [diff] [blame] | 101 | std::make_tuple( |
| 102 | hwmon::type::cenergy, |
| 103 | ValueInterface::Unit::Joules, |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 104 | -6, |
| 105 | "energy"), |
Brad Bishop | 5afe21a | 2017-01-06 20:44:05 -0500 | [diff] [blame] | 106 | std::make_tuple( |
| 107 | hwmon::type::cpower, |
| 108 | ValueInterface::Unit::Watts, |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 109 | -6, |
| 110 | "power"), |
Brad Bishop | 74aa4dd | 2017-01-06 09:50:31 -0500 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | auto getHwmonType(decltype(typeAttrMap)::const_reference attrs) |
| 114 | { |
| 115 | return std::get<0>(attrs); |
| 116 | } |
| 117 | |
| 118 | auto getUnit(decltype(typeAttrMap)::const_reference attrs) |
| 119 | { |
| 120 | return std::get<1>(attrs); |
| 121 | } |
| 122 | |
| 123 | auto getScale(decltype(typeAttrMap)::const_reference attrs) |
| 124 | { |
| 125 | return std::get<2>(attrs); |
| 126 | } |
| 127 | |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 128 | auto getNamespace(decltype(typeAttrMap)::const_reference attrs) |
| 129 | { |
| 130 | return std::get<3>(attrs); |
| 131 | } |
| 132 | |
Brad Bishop | 951a79e | 2017-01-06 21:55:11 -0500 | [diff] [blame] | 133 | using AttributeIterator = decltype(*typeAttrMap.begin()); |
| 134 | using Attributes |
| 135 | = std::remove_cv<std::remove_reference<AttributeIterator>::type>::type; |
| 136 | |
| 137 | auto getAttributes(const std::string& type, Attributes& attributes) |
| 138 | { |
| 139 | // *INDENT-OFF* |
| 140 | auto a = std::find_if( |
| 141 | typeAttrMap.begin(), |
| 142 | typeAttrMap.end(), |
| 143 | [&](const auto & e) |
| 144 | { |
| 145 | return type == getHwmonType(e); |
| 146 | }); |
| 147 | // *INDENT-ON* |
| 148 | |
| 149 | if (a == typeAttrMap.end()) |
| 150 | { |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | attributes = *a; |
| 155 | return true; |
| 156 | } |
| 157 | |
Matt Spinler | fee106b | 2017-11-29 15:18:05 -0600 | [diff] [blame] | 158 | int64_t adjustValue(const SensorSet::key_type& sensor, int64_t value) |
Chiabing Lee | c923ce9 | 2017-09-06 15:58:26 +0800 | [diff] [blame] | 159 | { |
| 160 | const auto& it = sensorAdjusts.find(sensor); |
| 161 | if (it != sensorAdjusts.end()) |
| 162 | { |
| 163 | // Adjust based on gain and offset |
| 164 | value = static_cast<decltype(value)>( |
| 165 | static_cast<double>(value) * it->second.gain |
| 166 | + it->second.offset); |
| 167 | } |
| 168 | return value; |
| 169 | } |
| 170 | |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 171 | auto addValue(const SensorSet::key_type& sensor, |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 172 | const std::string& devPath, |
| 173 | sysfs::hwmonio::HwmonIO& ioAccess, |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 174 | ObjectInfo& info) |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 175 | { |
Brad Bishop | 30dbcee | 2017-01-18 07:55:42 -0500 | [diff] [blame] | 176 | static constexpr bool deferSignals = true; |
| 177 | |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 178 | // Get the initial value for the value interface. |
| 179 | auto& bus = *std::get<sdbusplus::bus::bus*>(info); |
| 180 | auto& obj = std::get<Object>(info); |
| 181 | auto& objPath = std::get<std::string>(info); |
| 182 | |
Matt Spinler | fee106b | 2017-11-29 15:18:05 -0600 | [diff] [blame] | 183 | int64_t val = 0; |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 184 | try |
| 185 | { |
| 186 | // Retry for up to a second if device is busy |
Brad Bishop | 754d38c | 2017-09-08 00:46:58 -0400 | [diff] [blame] | 187 | // or has a transient error. |
| 188 | val = ioAccess.read( |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 189 | sensor.first, |
| 190 | sensor.second, |
Brad Bishop | 754d38c | 2017-09-08 00:46:58 -0400 | [diff] [blame] | 191 | hwmon::entry::cinput, |
| 192 | sysfs::hwmonio::retries, |
| 193 | sysfs::hwmonio::delay); |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 194 | } |
| 195 | catch (const std::system_error& e) |
| 196 | { |
| 197 | using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error; |
| 198 | report<ReadFailure>( |
| 199 | xyz::openbmc_project::Sensor::Device:: |
| 200 | ReadFailure::CALLOUT_ERRNO(e.code().value()), |
| 201 | xyz::openbmc_project::Sensor::Device:: |
| 202 | ReadFailure::CALLOUT_DEVICE_PATH(devPath.c_str())); |
| 203 | |
Matt Spinler | 9b65f76 | 2017-10-05 10:36:22 -0500 | [diff] [blame] | 204 | auto file = sysfs::make_sysfs_path( |
| 205 | ioAccess.path(), |
| 206 | sensor.first, |
| 207 | sensor.second, |
| 208 | hwmon::entry::cinput); |
| 209 | |
| 210 | log<level::INFO>("Logging failing sysfs file", |
| 211 | entry("FILE=%s", file.c_str())); |
| 212 | |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 213 | return static_cast<std::shared_ptr<ValueObject>>(nullptr); |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Chiabing Lee | c923ce9 | 2017-09-06 15:58:26 +0800 | [diff] [blame] | 216 | auto gain = getEnv("GAIN", sensor); |
| 217 | if (!gain.empty()) |
| 218 | { |
| 219 | sensorAdjusts[sensor].gain = std::stod(gain); |
| 220 | } |
| 221 | |
| 222 | auto offset = getEnv("OFFSET", sensor); |
| 223 | if (!offset.empty()) |
| 224 | { |
| 225 | sensorAdjusts[sensor].offset = std::stoi(offset); |
| 226 | } |
| 227 | |
| 228 | val = adjustValue(sensor, val); |
| 229 | |
Brad Bishop | 30dbcee | 2017-01-18 07:55:42 -0500 | [diff] [blame] | 230 | auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals); |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 231 | iface->value(val); |
| 232 | |
Brad Bishop | 951a79e | 2017-01-06 21:55:11 -0500 | [diff] [blame] | 233 | Attributes attrs; |
| 234 | if (getAttributes(sensor.first, attrs)) |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 235 | { |
Brad Bishop | 951a79e | 2017-01-06 21:55:11 -0500 | [diff] [blame] | 236 | iface->unit(getUnit(attrs)); |
| 237 | iface->scale(getScale(attrs)); |
Brad Bishop | e9fdee0 | 2017-01-06 10:43:29 -0500 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | obj[InterfaceType::VALUE] = iface; |
| 241 | return iface; |
| 242 | } |
| 243 | |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 244 | MainLoop::MainLoop( |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 245 | sdbusplus::bus::bus&& bus, |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 246 | const std::string& path, |
Brad Bishop | f3aa9ae | 2017-08-25 09:56:02 -0400 | [diff] [blame] | 247 | const std::string& devPath, |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 248 | const char* prefix, |
| 249 | const char* root) |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 250 | : _bus(std::move(bus)), |
Brad Bishop | 03e8735 | 2017-03-07 00:12:22 -0500 | [diff] [blame] | 251 | _manager(_bus, root), |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 252 | _shutdown(false), |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 253 | _hwmonRoot(), |
| 254 | _instance(), |
Brad Bishop | f3aa9ae | 2017-08-25 09:56:02 -0400 | [diff] [blame] | 255 | _devPath(devPath), |
Brad Bishop | b9e2b07 | 2016-12-19 13:47:10 -0500 | [diff] [blame] | 256 | _prefix(prefix), |
Brad Bishop | 3c344d3 | 2017-01-05 11:48:39 -0500 | [diff] [blame] | 257 | _root(root), |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 258 | state(), |
| 259 | ioAccess(path) |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 260 | { |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 261 | std::string p = path; |
| 262 | while (!p.empty() && p.back() == '/') |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 263 | { |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 264 | p.pop_back(); |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 265 | } |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 266 | |
| 267 | auto n = p.rfind('/'); |
| 268 | if (n != std::string::npos) |
| 269 | { |
| 270 | _instance.assign(p.substr(n + 1)); |
| 271 | _hwmonRoot.assign(p.substr(0, n)); |
| 272 | } |
| 273 | |
| 274 | assert(!_instance.empty()); |
| 275 | assert(!_hwmonRoot.empty()); |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | void MainLoop::shutdown() noexcept |
| 279 | { |
| 280 | _shutdown = true; |
| 281 | } |
| 282 | |
| 283 | void MainLoop::run() |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 284 | { |
| 285 | // Check sysfs for available sensors. |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 286 | auto sensors = std::make_unique<SensorSet>(_hwmonRoot + '/' + _instance); |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 287 | |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 288 | for (auto& i : *sensors) |
| 289 | { |
Tom Joseph | 1f8a958 | 2017-06-12 20:10:59 +0530 | [diff] [blame] | 290 | std::string label; |
Matt Spinler | 4b68c4e | 2017-10-12 16:44:53 -0500 | [diff] [blame] | 291 | std::string id; |
Brad Bishop | f3df6b4 | 2017-01-06 10:14:09 -0500 | [diff] [blame] | 292 | |
Tom Joseph | 1f8a958 | 2017-06-12 20:10:59 +0530 | [diff] [blame] | 293 | /* |
| 294 | * Check if the value of the MODE_<item><X> env variable for the sensor |
| 295 | * is "label", then read the sensor number from the <item><X>_label |
| 296 | * file. The name of the DBUS object would be the value of the env |
| 297 | * variable LABEL_<item><sensorNum>. If the MODE_<item><X> env variable |
Matt Spinler | 4b68c4e | 2017-10-12 16:44:53 -0500 | [diff] [blame] | 298 | * 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] | 299 | * variable LABEL_<item><X>. |
| 300 | */ |
| 301 | auto mode = getEnv("MODE", i.first); |
| 302 | if (!mode.compare(hwmon::entry::label)) |
Brad Bishop | 73831cd | 2017-01-06 09:37:22 -0500 | [diff] [blame] | 303 | { |
Matt Spinler | 4b68c4e | 2017-10-12 16:44:53 -0500 | [diff] [blame] | 304 | id = getIndirectID( |
| 305 | _hwmonRoot + '/' + _instance + '/', i.first); |
| 306 | |
| 307 | if (id.empty()) |
Tom Joseph | 1f8a958 | 2017-06-12 20:10:59 +0530 | [diff] [blame] | 308 | { |
| 309 | continue; |
| 310 | } |
| 311 | } |
Matt Spinler | 4b68c4e | 2017-10-12 16:44:53 -0500 | [diff] [blame] | 312 | |
| 313 | //In this loop, use the ID we looked up above if |
| 314 | //there was one, otherwise use the standard one. |
| 315 | id = (id.empty()) ? i.first.second : id; |
| 316 | |
| 317 | // Ignore inputs without a label. |
| 318 | label = getEnv("LABEL", i.first.first, id); |
| 319 | if (label.empty()) |
Tom Joseph | 1f8a958 | 2017-06-12 20:10:59 +0530 | [diff] [blame] | 320 | { |
Matt Spinler | 4b68c4e | 2017-10-12 16:44:53 -0500 | [diff] [blame] | 321 | continue; |
Brad Bishop | 73831cd | 2017-01-06 09:37:22 -0500 | [diff] [blame] | 322 | } |
| 323 | |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 324 | Attributes attrs; |
| 325 | if (!getAttributes(i.first.first, attrs)) |
| 326 | { |
| 327 | continue; |
| 328 | } |
| 329 | |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 330 | std::string objectPath{_root}; |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 331 | objectPath.append(1, '/'); |
Brad Bishop | add9851 | 2017-01-06 22:01:19 -0500 | [diff] [blame] | 332 | objectPath.append(getNamespace(attrs)); |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 333 | objectPath.append(1, '/'); |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 334 | objectPath.append(label); |
| 335 | |
Brad Bishop | f7426cf | 2017-01-06 15:36:43 -0500 | [diff] [blame] | 336 | ObjectInfo info(&_bus, std::move(objectPath), Object()); |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 337 | auto valueInterface = addValue(i.first, _devPath, ioAccess, info); |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 338 | if (!valueInterface) |
| 339 | { |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 340 | #ifdef REMOVE_ON_FAIL |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 341 | continue; /* skip adding this sensor for now. */ |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 342 | #else |
| 343 | exit(EXIT_FAILURE); |
| 344 | #endif |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 345 | } |
Brad Bishop | e0b7d05 | 2017-01-06 15:30:23 -0500 | [diff] [blame] | 346 | auto sensorValue = valueInterface->value(); |
Matt Spinler | ccfc77b | 2017-10-12 16:49:24 -0500 | [diff] [blame] | 347 | addThreshold<WarningObject>(i.first.first, id, sensorValue, info); |
| 348 | addThreshold<CriticalObject>(i.first.first, id, sensorValue, info); |
Matt Spinler | a3eb8e3 | 2017-10-12 16:42:49 -0500 | [diff] [blame] | 349 | |
Matt Spinler | 0a8de64 | 2017-05-11 10:59:39 -0500 | [diff] [blame] | 350 | auto target = addTarget<hwmon::FanSpeed>( |
Matt Spinler | b4e6557 | 2017-10-17 10:09:18 -0500 | [diff] [blame] | 351 | i.first, ioAccess, _devPath, info); |
Matt Spinler | 0a8de64 | 2017-05-11 10:59:39 -0500 | [diff] [blame] | 352 | |
| 353 | if (target) |
| 354 | { |
| 355 | target->enable(); |
| 356 | } |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 357 | |
Patrick Venture | 9331ab7 | 2018-01-29 09:48:47 -0800 | [diff] [blame^] | 358 | addTarget<hwmon::FanPwm>(i.first, ioAccess, _devPath, info); |
| 359 | |
Brad Bishop | 30dbcee | 2017-01-18 07:55:42 -0500 | [diff] [blame] | 360 | // All the interfaces have been created. Go ahead |
| 361 | // and emit InterfacesAdded. |
| 362 | valueInterface->emit_object_added(); |
| 363 | |
Brad Bishop | 075f7a2 | 2017-01-06 09:45:08 -0500 | [diff] [blame] | 364 | auto value = std::make_tuple( |
| 365 | std::move(i.second), |
| 366 | std::move(label), |
Brad Bishop | f7426cf | 2017-01-06 15:36:43 -0500 | [diff] [blame] | 367 | std::move(info)); |
Brad Bishop | 73831cd | 2017-01-06 09:37:22 -0500 | [diff] [blame] | 368 | |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 369 | state[std::move(i.first)] = std::move(value); |
| 370 | } |
| 371 | |
Patrick Venture | 62503a4 | 2017-05-23 07:30:29 -0700 | [diff] [blame] | 372 | /* If there are no sensors specified by labels, exit. */ |
| 373 | if (0 == state.size()) |
| 374 | { |
| 375 | return; |
| 376 | } |
| 377 | |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 378 | { |
Brad Bishop | b8740fc | 2017-02-24 23:38:37 -0500 | [diff] [blame] | 379 | std::string busname{_prefix}; |
Brad Bishop | 4d9a35b | 2017-11-14 22:33:03 -0500 | [diff] [blame] | 380 | busname.append(1, '-'); |
| 381 | busname.append( |
| 382 | std::to_string(std::hash<decltype(_devPath)>{}(_devPath))); |
| 383 | busname.append(".Hwmon1"); |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 384 | _bus.request_name(busname.c_str()); |
| 385 | } |
| 386 | |
Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame] | 387 | { |
| 388 | auto interval = getenv("INTERVAL"); |
| 389 | if (interval) |
| 390 | { |
| 391 | _interval = strtoull(interval, NULL, 10); |
| 392 | } |
| 393 | } |
| 394 | |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 395 | // TODO: Issue#3 - Need to make calls to the dbus sensor cache here to |
| 396 | // ensure the objects all exist? |
| 397 | |
| 398 | // Polling loop. |
Brad Bishop | d499ca6 | 2016-12-19 09:24:50 -0500 | [diff] [blame] | 399 | while (!_shutdown) |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 400 | { |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 401 | #ifdef REMOVE_ON_FAIL |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 402 | std::vector<SensorSet::key_type> destroy; |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 403 | #endif |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 404 | // Iterate through all the sensors. |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 405 | for (auto& i : state) |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 406 | { |
Brad Bishop | 75b4ab8 | 2017-01-06 09:33:50 -0500 | [diff] [blame] | 407 | auto& attrs = std::get<0>(i.second); |
| 408 | if (attrs.find(hwmon::entry::input) != attrs.end()) |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 409 | { |
| 410 | // Read value from sensor. |
Matt Spinler | fee106b | 2017-11-29 15:18:05 -0600 | [diff] [blame] | 411 | int64_t value; |
Patrick Venture | 9331ab7 | 2018-01-29 09:48:47 -0800 | [diff] [blame^] | 412 | std::string input = hwmon::entry::cinput; |
| 413 | if (i.first.first == "pwm") { |
| 414 | input = ""; |
| 415 | } |
| 416 | |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 417 | try |
Brad Bishop | dddb715 | 2017-01-06 09:54:23 -0500 | [diff] [blame] | 418 | { |
Brad Bishop | 754d38c | 2017-09-08 00:46:58 -0400 | [diff] [blame] | 419 | // Retry for up to a second if device is busy |
| 420 | // or has a transient error. |
| 421 | |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 422 | value = ioAccess.read( |
| 423 | i.first.first, |
| 424 | i.first.second, |
Patrick Venture | 9331ab7 | 2018-01-29 09:48:47 -0800 | [diff] [blame^] | 425 | input, |
Brad Bishop | 754d38c | 2017-09-08 00:46:58 -0400 | [diff] [blame] | 426 | sysfs::hwmonio::retries, |
| 427 | sysfs::hwmonio::delay); |
Brad Bishop | e0b7d05 | 2017-01-06 15:30:23 -0500 | [diff] [blame] | 428 | |
Chiabing Lee | c923ce9 | 2017-09-06 15:58:26 +0800 | [diff] [blame] | 429 | value = adjustValue(i.first, value); |
| 430 | |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 431 | auto& objInfo = std::get<ObjectInfo>(i.second); |
| 432 | auto& obj = std::get<Object>(objInfo); |
| 433 | |
| 434 | for (auto& iface : obj) |
Brad Bishop | e0b7d05 | 2017-01-06 15:30:23 -0500 | [diff] [blame] | 435 | { |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 436 | auto valueIface = std::shared_ptr<ValueObject>(); |
| 437 | auto warnIface = std::shared_ptr<WarningObject>(); |
| 438 | auto critIface = std::shared_ptr<CriticalObject>(); |
| 439 | |
| 440 | switch (iface.first) |
| 441 | { |
| 442 | case InterfaceType::VALUE: |
| 443 | valueIface = std::experimental::any_cast<std::shared_ptr<ValueObject>> |
| 444 | (iface.second); |
| 445 | valueIface->value(value); |
| 446 | break; |
| 447 | case InterfaceType::WARN: |
| 448 | checkThresholds<WarningObject>(iface.second, value); |
| 449 | break; |
| 450 | case InterfaceType::CRIT: |
| 451 | checkThresholds<CriticalObject>(iface.second, value); |
| 452 | break; |
| 453 | default: |
| 454 | break; |
| 455 | } |
Brad Bishop | e0b7d05 | 2017-01-06 15:30:23 -0500 | [diff] [blame] | 456 | } |
Brad Bishop | dddb715 | 2017-01-06 09:54:23 -0500 | [diff] [blame] | 457 | } |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 458 | catch (const std::system_error& e) |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 459 | { |
Brad Bishop | 751043e | 2017-08-29 11:13:46 -0400 | [diff] [blame] | 460 | using namespace sdbusplus::xyz::openbmc_project:: |
| 461 | Sensor::Device::Error; |
| 462 | report<ReadFailure>( |
| 463 | xyz::openbmc_project::Sensor::Device:: |
| 464 | ReadFailure::CALLOUT_ERRNO(e.code().value()), |
| 465 | xyz::openbmc_project::Sensor::Device:: |
| 466 | ReadFailure::CALLOUT_DEVICE_PATH( |
| 467 | _devPath.c_str())); |
Matt Spinler | 9b65f76 | 2017-10-05 10:36:22 -0500 | [diff] [blame] | 468 | |
| 469 | auto file = sysfs::make_sysfs_path( |
| 470 | ioAccess.path(), |
| 471 | i.first.first, |
| 472 | i.first.second, |
| 473 | hwmon::entry::cinput); |
| 474 | |
| 475 | log<level::INFO>("Logging failing sysfs file", |
| 476 | entry("FILE=%s", file.c_str())); |
| 477 | |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 478 | #ifdef REMOVE_ON_FAIL |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 479 | destroy.push_back(i.first); |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 480 | #else |
| 481 | exit(EXIT_FAILURE); |
| 482 | #endif |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 483 | } |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 487 | #ifdef REMOVE_ON_FAIL |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 488 | for (auto& i : destroy) |
| 489 | { |
| 490 | state.erase(i); |
| 491 | } |
Matt Spinler | f9c83c4 | 2017-08-10 08:51:45 -0500 | [diff] [blame] | 492 | #endif |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 493 | |
Brad Bishop | 9c7b6e0 | 2016-12-19 12:43:36 -0500 | [diff] [blame] | 494 | // Respond to DBus |
| 495 | _bus.process_discard(); |
| 496 | |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 497 | // Sleep until next interval. |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 498 | // TODO: Issue#6 - Optionally look at polling interval sysfs entry. |
Patrick Venture | ab10f16 | 2017-05-22 09:44:50 -0700 | [diff] [blame] | 499 | _bus.wait(_interval); |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 500 | |
| 501 | // TODO: Issue#7 - Should probably periodically check the SensorSet |
| 502 | // for new entries. |
| 503 | } |
Brad Bishop | e55ef3d | 2016-12-19 09:12:40 -0500 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |