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