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