Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2017 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 | */ |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 16 | #include "tach_sensor.hpp" |
| 17 | |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 18 | #include "fan.hpp" |
Brad Bishop | 2a58e2c | 2017-07-30 13:49:09 -0400 | [diff] [blame] | 19 | #include "sdbusplus.hpp" |
Matthew Barth | 4d98285 | 2017-11-17 09:37:13 -0600 | [diff] [blame] | 20 | #include "utility.hpp" |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 21 | |
Jolie Ku | 4c3c24f | 2020-09-09 11:01:46 +0800 | [diff] [blame] | 22 | #include <fmt/format.h> |
| 23 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 24 | #include <phosphor-logging/elog.hpp> |
| 25 | #include <phosphor-logging/log.hpp> |
| 26 | |
Matt Spinler | 2ea9a59 | 2022-04-08 14:36:22 -0500 | [diff] [blame] | 27 | #include <filesystem> |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 28 | #include <functional> |
Matthew Barth | 8a8aa44 | 2021-11-19 14:13:13 -0600 | [diff] [blame] | 29 | #include <optional> |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 30 | #include <utility> |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 31 | |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 32 | namespace phosphor |
| 33 | { |
| 34 | namespace fan |
| 35 | { |
| 36 | namespace monitor |
| 37 | { |
| 38 | |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 39 | constexpr auto FAN_TARGET_PROPERTY = "Target"; |
| 40 | constexpr auto FAN_VALUE_PROPERTY = "Value"; |
Mike Capps | 7b34ee0 | 2022-05-04 14:16:12 -0400 | [diff] [blame^] | 41 | constexpr auto MAX_PREV_TACHS = 8; |
| 42 | constexpr auto MAX_PREV_TARGETS = 8; |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 43 | |
Matt Spinler | 2ea9a59 | 2022-04-08 14:36:22 -0500 | [diff] [blame] | 44 | namespace fs = std::filesystem; |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 45 | using InternalFailure = |
| 46 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * @brief Helper function to read a property |
| 50 | * |
| 51 | * @param[in] interface - the interface the property is on |
| 52 | * @param[in] propertName - the name of the property |
| 53 | * @param[in] path - the dbus path |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 54 | * @param[in] bus - the dbus object |
| 55 | * @param[out] value - filled in with the property value |
| 56 | */ |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 57 | template <typename T> |
| 58 | static void |
| 59 | readProperty(const std::string& interface, const std::string& propertyName, |
| 60 | const std::string& path, sdbusplus::bus::bus& bus, T& value) |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 61 | { |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 62 | try |
| 63 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 64 | value = |
| 65 | util::SDBusPlus::getProperty<T>(bus, path, interface, propertyName); |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 66 | } |
Patrick Williams | ddb773b | 2021-10-06 11:24:49 -0500 | [diff] [blame] | 67 | catch (const std::exception& e) |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 68 | { |
| 69 | phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); |
| 70 | } |
| 71 | } |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 72 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 73 | TachSensor::TachSensor(Mode mode, sdbusplus::bus::bus& bus, Fan& fan, |
| 74 | const std::string& id, bool hasTarget, size_t funcDelay, |
| 75 | const std::string& interface, double factor, |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 76 | int64_t offset, size_t method, size_t threshold, |
Matthew Barth | 8a8aa44 | 2021-11-19 14:13:13 -0600 | [diff] [blame] | 77 | bool ignoreAboveMax, size_t timeout, |
| 78 | const std::optional<size_t>& errorDelay, |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 79 | size_t countInterval, const sdeventplus::Event& event) : |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 80 | _bus(bus), |
Matt Spinler | 2ea9a59 | 2022-04-08 14:36:22 -0500 | [diff] [blame] | 81 | _fan(fan), _name(FAN_SENSOR_PATH + id), |
| 82 | _invName(fs::path(fan.getName()) / id), _hasTarget(hasTarget), |
| 83 | _funcDelay(funcDelay), _interface(interface), _factor(factor), |
| 84 | _offset(offset), _method(method), _threshold(threshold), |
Matthew Barth | 8a8aa44 | 2021-11-19 14:13:13 -0600 | [diff] [blame] | 85 | _ignoreAboveMax(ignoreAboveMax), _timeout(timeout), |
| 86 | _timerMode(TimerMode::func), |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 87 | _timer(event, std::bind(&Fan::updateState, &fan, std::ref(*this))), |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 88 | _errorDelay(errorDelay), _countInterval(countInterval) |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 89 | { |
Mike Capps | ce6820a | 2021-05-26 10:40:19 -0400 | [diff] [blame] | 90 | // Query functional state from inventory |
| 91 | // TODO - phosphor-fan-presence/issues/25 |
Mike Capps | ce6820a | 2021-05-26 10:40:19 -0400 | [diff] [blame] | 92 | |
Mike Capps | 7b34ee0 | 2022-05-04 14:16:12 -0400 | [diff] [blame^] | 93 | _prevTachs.resize(MAX_PREV_TACHS); |
| 94 | |
| 95 | if (_hasTarget) |
| 96 | { |
| 97 | _prevTargets.resize(MAX_PREV_TARGETS); |
| 98 | } |
| 99 | |
Mike Capps | 3edb065 | 2021-09-01 18:30:21 -0400 | [diff] [blame] | 100 | _functional = true; |
| 101 | |
Mike Capps | ce6820a | 2021-05-26 10:40:19 -0400 | [diff] [blame] | 102 | try |
| 103 | { |
Mike Capps | 3edb065 | 2021-09-01 18:30:21 -0400 | [diff] [blame] | 104 | // see if any object paths in the inventory have the |
| 105 | // OperationalStatus interface. |
| 106 | auto subtree = util::SDBusPlus::getSubTreeRaw( |
| 107 | _bus, util::INVENTORY_PATH, util::OPERATIONAL_STATUS_INTF, 0); |
Mike Capps | 9ff4877 | 2021-07-19 14:49:43 -0400 | [diff] [blame] | 108 | |
Mike Capps | 3edb065 | 2021-09-01 18:30:21 -0400 | [diff] [blame] | 109 | // if the tach sensor's entry already exists, we for sure can |
| 110 | // read its functional state from the inventory |
| 111 | if (subtree.end() != subtree.find(util::INVENTORY_PATH + _invName)) |
Mike Capps | ce6820a | 2021-05-26 10:40:19 -0400 | [diff] [blame] | 112 | { |
| 113 | _functional = util::SDBusPlus::getProperty<bool>( |
Mike Capps | 3edb065 | 2021-09-01 18:30:21 -0400 | [diff] [blame] | 114 | _bus, util::INVENTORY_PATH + _invName, |
Mike Capps | ce6820a | 2021-05-26 10:40:19 -0400 | [diff] [blame] | 115 | util::OPERATIONAL_STATUS_INTF, util::FUNCTIONAL_PROPERTY); |
| 116 | } |
Mike Capps | ce6820a | 2021-05-26 10:40:19 -0400 | [diff] [blame] | 117 | } |
Patrick Williams | ddb773b | 2021-10-06 11:24:49 -0500 | [diff] [blame] | 118 | catch (const util::DBusError& e) |
Mike Capps | ce6820a | 2021-05-26 10:40:19 -0400 | [diff] [blame] | 119 | { |
| 120 | log<level::DEBUG>(e.what()); |
Mike Capps | ce6820a | 2021-05-26 10:40:19 -0400 | [diff] [blame] | 121 | } |
| 122 | |
Mike Capps | 3edb065 | 2021-09-01 18:30:21 -0400 | [diff] [blame] | 123 | updateInventory(_functional); |
| 124 | |
Mike Capps | ce6820a | 2021-05-26 10:40:19 -0400 | [diff] [blame] | 125 | if (!_functional && MethodMode::count == _method) |
| 126 | { |
| 127 | // force continual nonfunctional state |
| 128 | _counter = _threshold; |
| 129 | } |
Brad Bishop | edaeb31 | 2017-07-30 19:38:20 -0400 | [diff] [blame] | 130 | |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 131 | // Load in current Target and Input values when entering monitor mode |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 132 | #ifndef MONITOR_USE_JSON |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 133 | if (mode != Mode::init) |
Brad Bishop | edaeb31 | 2017-07-30 19:38:20 -0400 | [diff] [blame] | 134 | { |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 135 | #endif |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 136 | try |
| 137 | { |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 138 | updateTachAndTarget(); |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 139 | } |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 140 | catch (const std::exception& e) |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 141 | { |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 142 | // Until the parent Fan's monitor-ready timer expires, the |
| 143 | // object can be functional with a missing D-bus sensor. |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 144 | } |
| 145 | |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 146 | auto match = getMatchString(util::FAN_SENSOR_VALUE_INTF); |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 147 | |
Patrick Williams | 3ea9ec2 | 2021-11-19 12:21:08 -0600 | [diff] [blame] | 148 | tachSignal = std::make_unique<sdbusplus::bus::match_t>( |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 149 | _bus, match.c_str(), |
| 150 | [this](auto& msg) { this->handleTachChange(msg); }); |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 151 | |
| 152 | if (_hasTarget) |
| 153 | { |
Lei YU | 80f271b | 2018-01-31 15:24:46 +0800 | [diff] [blame] | 154 | match = getMatchString(_interface); |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 155 | |
Patrick Williams | 3ea9ec2 | 2021-11-19 12:21:08 -0600 | [diff] [blame] | 156 | targetSignal = std::make_unique<sdbusplus::bus::match_t>( |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 157 | _bus, match.c_str(), |
| 158 | [this](auto& msg) { this->handleTargetChange(msg); }); |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 159 | } |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 160 | |
| 161 | if (_errorDelay) |
| 162 | { |
| 163 | _errorTimer = std::make_unique< |
| 164 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>( |
| 165 | event, std::bind(&Fan::sensorErrorTimerExpired, &fan, |
| 166 | std::ref(*this))); |
| 167 | } |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 168 | |
| 169 | if (_method == MethodMode::count) |
| 170 | { |
| 171 | _countTimer = std::make_unique< |
| 172 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>( |
| 173 | event, |
| 174 | std::bind(&Fan::countTimerExpired, &fan, std::ref(*this))); |
| 175 | } |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 176 | #ifndef MONITOR_USE_JSON |
Brad Bishop | edaeb31 | 2017-07-30 19:38:20 -0400 | [diff] [blame] | 177 | } |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 178 | #endif |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 179 | } |
| 180 | |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 181 | void TachSensor::updateTachAndTarget() |
| 182 | { |
| 183 | _tachInput = util::SDBusPlus::getProperty<decltype(_tachInput)>( |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 184 | _bus, _name, util::FAN_SENSOR_VALUE_INTF, FAN_VALUE_PROPERTY); |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 185 | |
| 186 | if (_hasTarget) |
| 187 | { |
| 188 | readProperty(_interface, FAN_TARGET_PROPERTY, _name, _bus, _tachTarget); |
Mike Capps | 7b34ee0 | 2022-05-04 14:16:12 -0400 | [diff] [blame^] | 189 | |
| 190 | // record previous target value |
| 191 | if (_prevTargets.front() != _tachTarget) |
| 192 | { |
| 193 | _prevTargets.push_front(_tachTarget); |
| 194 | |
| 195 | _prevTargets.pop_back(); |
| 196 | } |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 197 | } |
Mike Capps | 7b34ee0 | 2022-05-04 14:16:12 -0400 | [diff] [blame^] | 198 | |
| 199 | // record previous tach value |
| 200 | _prevTachs.push_front(_tachInput); |
| 201 | |
| 202 | _prevTachs.pop_back(); |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 203 | } |
| 204 | |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 205 | std::string TachSensor::getMatchString(const std::string& interface) |
| 206 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 207 | return sdbusplus::bus::match::rules::propertiesChanged(_name, interface); |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 208 | } |
| 209 | |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 210 | uint64_t TachSensor::getTarget() const |
| 211 | { |
| 212 | if (!_hasTarget) |
| 213 | { |
| 214 | return _fan.findTargetSpeed(); |
| 215 | } |
| 216 | return _tachTarget; |
| 217 | } |
| 218 | |
Matthew Barth | 8a8aa44 | 2021-11-19 14:13:13 -0600 | [diff] [blame] | 219 | std::pair<uint64_t, std::optional<uint64_t>> |
| 220 | TachSensor::getRange(const size_t deviation) const |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 221 | { |
| 222 | // Determine min/max range applying the deviation |
| 223 | uint64_t min = getTarget() * (100 - deviation) / 100; |
Matthew Barth | 8a8aa44 | 2021-11-19 14:13:13 -0600 | [diff] [blame] | 224 | std::optional<uint64_t> max = getTarget() * (100 + deviation) / 100; |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 225 | |
| 226 | // Adjust the min/max range by applying the factor & offset |
| 227 | min = min * _factor + _offset; |
Matthew Barth | 8a8aa44 | 2021-11-19 14:13:13 -0600 | [diff] [blame] | 228 | max = max.value() * _factor + _offset; |
| 229 | |
| 230 | if (_ignoreAboveMax) |
| 231 | { |
| 232 | max = std::nullopt; |
| 233 | } |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 234 | |
| 235 | return std::make_pair(min, max); |
| 236 | } |
| 237 | |
Matthew Barth | fcb0dbc | 2021-02-10 14:23:39 -0600 | [diff] [blame] | 238 | void TachSensor::processState() |
| 239 | { |
Matt Spinler | 623635c | 2021-03-29 13:13:59 -0500 | [diff] [blame] | 240 | // This function runs from inside trust::Manager::checkTrust(), which, |
| 241 | // for sensors using the count method, runs right before process() |
| 242 | // is called anyway inside Fan::countTimerExpired() so don't call |
| 243 | // it now if using that method. |
| 244 | if (_method == MethodMode::timebased) |
| 245 | { |
| 246 | _fan.process(*this); |
| 247 | } |
Matthew Barth | fcb0dbc | 2021-02-10 14:23:39 -0600 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | void TachSensor::resetMethod() |
| 251 | { |
| 252 | switch (_method) |
| 253 | { |
| 254 | case MethodMode::timebased: |
| 255 | if (timerRunning()) |
| 256 | { |
| 257 | stopTimer(); |
| 258 | } |
| 259 | break; |
| 260 | case MethodMode::count: |
| 261 | if (_functional) |
| 262 | { |
| 263 | _counter = 0; |
| 264 | } |
| 265 | else |
| 266 | { |
| 267 | _counter = _threshold; |
| 268 | } |
| 269 | break; |
| 270 | } |
| 271 | } |
| 272 | |
Matthew Barth | d199dcd | 2017-11-20 10:36:41 -0600 | [diff] [blame] | 273 | void TachSensor::setFunctional(bool functional) |
| 274 | { |
| 275 | _functional = functional; |
| 276 | updateInventory(_functional); |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 277 | |
| 278 | if (!_errorTimer) |
| 279 | { |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | if (!_functional) |
| 284 | { |
| 285 | if (_fan.present()) |
| 286 | { |
| 287 | _errorTimer->restartOnce(std::chrono::seconds(*_errorDelay)); |
| 288 | } |
| 289 | } |
| 290 | else if (_errorTimer->isEnabled()) |
| 291 | { |
| 292 | _errorTimer->setEnabled(false); |
| 293 | } |
Matthew Barth | d199dcd | 2017-11-20 10:36:41 -0600 | [diff] [blame] | 294 | } |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 295 | |
Brad Bishop | 771659f | 2017-07-30 19:52:21 -0400 | [diff] [blame] | 296 | void TachSensor::handleTargetChange(sdbusplus::message::message& msg) |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 297 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 298 | readPropertyFromMessage(msg, _interface, FAN_TARGET_PROPERTY, _tachTarget); |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 299 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 300 | // Check all tach sensors on the fan against the target |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 301 | _fan.tachChanged(); |
Mike Capps | 7b34ee0 | 2022-05-04 14:16:12 -0400 | [diff] [blame^] | 302 | |
| 303 | // record previous target value |
| 304 | if (_prevTargets.front() != _tachTarget) |
| 305 | { |
| 306 | _prevTargets.push_front(_tachTarget); |
| 307 | |
| 308 | _prevTargets.pop_back(); |
| 309 | } |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 310 | } |
| 311 | |
Brad Bishop | 771659f | 2017-07-30 19:52:21 -0400 | [diff] [blame] | 312 | void TachSensor::handleTachChange(sdbusplus::message::message& msg) |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 313 | { |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 314 | readPropertyFromMessage(msg, util::FAN_SENSOR_VALUE_INTF, |
| 315 | FAN_VALUE_PROPERTY, _tachInput); |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 316 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 317 | // Check just this sensor against the target |
| 318 | _fan.tachChanged(*this); |
Mike Capps | 7b34ee0 | 2022-05-04 14:16:12 -0400 | [diff] [blame^] | 319 | |
| 320 | // record previous tach value |
| 321 | _prevTachs.push_front(_tachInput); |
| 322 | |
| 323 | _prevTachs.pop_back(); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 324 | } |
| 325 | |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 326 | void TachSensor::startTimer(TimerMode mode) |
| 327 | { |
Matthew Barth | 0d0e355 | 2021-01-27 12:31:28 -0600 | [diff] [blame] | 328 | using namespace std::chrono; |
| 329 | |
William A. Kennington III | 8fd879f | 2018-10-30 19:49:29 -0700 | [diff] [blame] | 330 | if (!timerRunning() || mode != _timerMode) |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 331 | { |
Matt Spinler | 1d7379e | 2021-03-01 16:16:17 -0600 | [diff] [blame] | 332 | log<level::DEBUG>( |
Matthew Barth | 0d0e355 | 2021-01-27 12:31:28 -0600 | [diff] [blame] | 333 | fmt::format("Start timer({}) on tach sensor {}. [delay = {}s]", |
Ed Tanous | cf8847e | 2022-02-01 16:26:38 -0800 | [diff] [blame] | 334 | static_cast<int>(mode), _name, |
Matthew Barth | 0d0e355 | 2021-01-27 12:31:28 -0600 | [diff] [blame] | 335 | duration_cast<seconds>(getDelay(mode)).count()) |
| 336 | .c_str()); |
William A. Kennington III | 8fd879f | 2018-10-30 19:49:29 -0700 | [diff] [blame] | 337 | _timer.restartOnce(getDelay(mode)); |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 338 | _timerMode = mode; |
| 339 | } |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 340 | } |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 341 | |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 342 | std::chrono::microseconds TachSensor::getDelay(TimerMode mode) |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 343 | { |
| 344 | using namespace std::chrono; |
| 345 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 346 | switch (mode) |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 347 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 348 | case TimerMode::nonfunc: |
| 349 | return duration_cast<microseconds>(seconds(_timeout)); |
| 350 | case TimerMode::func: |
| 351 | return duration_cast<microseconds>(seconds(_funcDelay)); |
| 352 | default: |
| 353 | // Log an internal error for undefined timer mode |
| 354 | log<level::ERR>("Undefined timer mode", |
| 355 | entry("TIMER_MODE=%u", mode)); |
| 356 | elog<InternalFailure>(); |
| 357 | return duration_cast<microseconds>(seconds(0)); |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 358 | } |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 359 | } |
| 360 | |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 361 | void TachSensor::setCounter(bool count) |
| 362 | { |
| 363 | if (count) |
| 364 | { |
| 365 | if (_counter < _threshold) |
| 366 | { |
| 367 | ++_counter; |
Matt Spinler | 623635c | 2021-03-29 13:13:59 -0500 | [diff] [blame] | 368 | log<level::DEBUG>( |
| 369 | fmt::format( |
| 370 | "Incremented error counter on {} to {} (threshold {})", |
| 371 | _name, _counter, _threshold) |
| 372 | .c_str()); |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | else |
| 376 | { |
| 377 | if (_counter > 0) |
| 378 | { |
| 379 | --_counter; |
Matt Spinler | 623635c | 2021-03-29 13:13:59 -0500 | [diff] [blame] | 380 | log<level::DEBUG>( |
| 381 | fmt::format( |
| 382 | "Decremented error counter on {} to {} (threshold {})", |
| 383 | _name, _counter, _threshold) |
| 384 | .c_str()); |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 389 | void TachSensor::startCountTimer() |
| 390 | { |
| 391 | if (_countTimer) |
| 392 | { |
| 393 | log<level::DEBUG>( |
| 394 | fmt::format("Starting count timer on sensor {}", _name).c_str()); |
| 395 | _countTimer->restart(std::chrono::seconds(_countInterval)); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | void TachSensor::stopCountTimer() |
| 400 | { |
| 401 | if (_countTimer && _countTimer->isEnabled()) |
| 402 | { |
| 403 | log<level::DEBUG>( |
| 404 | fmt::format("Stopping count timer on tach sensor {}.", _name) |
| 405 | .c_str()); |
| 406 | _countTimer->setEnabled(false); |
| 407 | } |
| 408 | } |
| 409 | |
Matthew Barth | 4d98285 | 2017-11-17 09:37:13 -0600 | [diff] [blame] | 410 | void TachSensor::updateInventory(bool functional) |
| 411 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 412 | auto objectMap = |
| 413 | util::getObjMap<bool>(_invName, util::OPERATIONAL_STATUS_INTF, |
| 414 | util::FUNCTIONAL_PROPERTY, functional); |
Mike Capps | 8af8a62 | 2022-02-04 16:13:33 -0500 | [diff] [blame] | 415 | |
| 416 | auto response = util::SDBusPlus::callMethod( |
| 417 | _bus, util::INVENTORY_SVC, util::INVENTORY_PATH, util::INVENTORY_INTF, |
| 418 | "Notify", objectMap); |
| 419 | |
Matthew Barth | 4d98285 | 2017-11-17 09:37:13 -0600 | [diff] [blame] | 420 | if (response.is_method_error()) |
| 421 | { |
| 422 | log<level::ERR>("Error in notify update of tach sensor inventory"); |
| 423 | } |
| 424 | } |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 425 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 426 | } // namespace monitor |
| 427 | } // namespace fan |
| 428 | } // namespace phosphor |