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