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