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