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