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 | |
| 27 | #include <experimental/filesystem> |
| 28 | #include <functional> |
| 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_SENSOR_VALUE_INTF = "xyz.openbmc_project.Sensor.Value"; |
| 38 | constexpr auto FAN_TARGET_PROPERTY = "Target"; |
| 39 | constexpr auto FAN_VALUE_PROPERTY = "Value"; |
| 40 | |
Matthew Barth | 4d98285 | 2017-11-17 09:37:13 -0600 | [diff] [blame] | 41 | using namespace std::experimental::filesystem; |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 42 | using InternalFailure = |
| 43 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * @brief Helper function to read a property |
| 47 | * |
| 48 | * @param[in] interface - the interface the property is on |
| 49 | * @param[in] propertName - the name of the property |
| 50 | * @param[in] path - the dbus path |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 51 | * @param[in] bus - the dbus object |
| 52 | * @param[out] value - filled in with the property value |
| 53 | */ |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 54 | template <typename T> |
| 55 | static void |
| 56 | readProperty(const std::string& interface, const std::string& propertyName, |
| 57 | const std::string& path, sdbusplus::bus::bus& bus, T& value) |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 58 | { |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 59 | try |
| 60 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 61 | value = |
| 62 | util::SDBusPlus::getProperty<T>(bus, path, interface, propertyName); |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 63 | } |
| 64 | catch (std::exception& e) |
| 65 | { |
| 66 | phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); |
| 67 | } |
| 68 | } |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 69 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 70 | TachSensor::TachSensor(Mode mode, sdbusplus::bus::bus& bus, Fan& fan, |
| 71 | const std::string& id, bool hasTarget, size_t funcDelay, |
| 72 | const std::string& interface, double factor, |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 73 | int64_t offset, size_t method, size_t threshold, |
| 74 | size_t timeout, const std::optional<size_t>& errorDelay, |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame] | 75 | const sdeventplus::Event& event) : |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 76 | _bus(bus), |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 77 | _fan(fan), _name(FAN_SENSOR_PATH + id), _invName(path(fan.getName()) / id), |
| 78 | _hasTarget(hasTarget), _funcDelay(funcDelay), _interface(interface), |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 79 | _factor(factor), _offset(offset), _method(method), _threshold(threshold), |
| 80 | _timeout(timeout), _timerMode(TimerMode::func), |
| 81 | _timer(event, std::bind(&Fan::updateState, &fan, std::ref(*this))), |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 82 | _errorDelay(errorDelay) |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 83 | { |
Matthew Barth | d199dcd | 2017-11-20 10:36:41 -0600 | [diff] [blame] | 84 | // Start from a known state of functional |
Matthew Barth | 7f7df31 | 2018-01-15 16:27:50 -0600 | [diff] [blame] | 85 | setFunctional(true); |
Brad Bishop | edaeb31 | 2017-07-30 19:38:20 -0400 | [diff] [blame] | 86 | |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 87 | // Load in current Target and Input values when entering monitor mode |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 88 | #ifndef MONITOR_USE_JSON |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 89 | if (mode != Mode::init) |
Brad Bishop | edaeb31 | 2017-07-30 19:38:20 -0400 | [diff] [blame] | 90 | { |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 91 | #endif |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 92 | try |
| 93 | { |
| 94 | // Use getProperty directly to allow a missing sensor object |
| 95 | // to abort construction. |
| 96 | _tachInput = util::SDBusPlus::getProperty<decltype(_tachInput)>( |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 97 | _bus, _name, FAN_SENSOR_VALUE_INTF, FAN_VALUE_PROPERTY); |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 98 | } |
| 99 | catch (std::exception& e) |
| 100 | { |
Jolie Ku | 4c3c24f | 2020-09-09 11:01:46 +0800 | [diff] [blame] | 101 | log<level::ERR>( |
| 102 | fmt::format("Failed to retrieve tach sensor {}", _name) |
| 103 | .c_str()); |
Jolie Ku | 5d564a9 | 2020-10-23 09:04:28 +0800 | [diff] [blame] | 104 | // Mark tach sensor as nonfunctional |
Jolie Ku | 4c3c24f | 2020-09-09 11:01:46 +0800 | [diff] [blame] | 105 | setFunctional(false); |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 106 | throw InvalidSensorError(); |
| 107 | } |
| 108 | |
| 109 | if (_hasTarget) |
| 110 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 111 | readProperty(_interface, FAN_TARGET_PROPERTY, _name, _bus, |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 112 | _tachTarget); |
| 113 | } |
| 114 | |
| 115 | auto match = getMatchString(FAN_SENSOR_VALUE_INTF); |
| 116 | |
| 117 | tachSignal = std::make_unique<sdbusplus::server::match::match>( |
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 | { |
Lei YU | 80f271b | 2018-01-31 15:24:46 +0800 | [diff] [blame] | 123 | match = getMatchString(_interface); |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 124 | |
| 125 | targetSignal = std::make_unique<sdbusplus::server::match::match>( |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 126 | _bus, match.c_str(), |
| 127 | [this](auto& msg) { this->handleTargetChange(msg); }); |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 128 | } |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 129 | |
| 130 | if (_errorDelay) |
| 131 | { |
| 132 | _errorTimer = std::make_unique< |
| 133 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>( |
| 134 | event, std::bind(&Fan::sensorErrorTimerExpired, &fan, |
| 135 | std::ref(*this))); |
| 136 | } |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 137 | #ifndef MONITOR_USE_JSON |
Brad Bishop | edaeb31 | 2017-07-30 19:38:20 -0400 | [diff] [blame] | 138 | } |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 139 | #endif |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 140 | } |
| 141 | |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 142 | std::string TachSensor::getMatchString(const std::string& interface) |
| 143 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 144 | return sdbusplus::bus::match::rules::propertiesChanged(_name, interface); |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 145 | } |
| 146 | |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 147 | uint64_t TachSensor::getTarget() const |
| 148 | { |
| 149 | if (!_hasTarget) |
| 150 | { |
| 151 | return _fan.findTargetSpeed(); |
| 152 | } |
| 153 | return _tachTarget; |
| 154 | } |
| 155 | |
Matthew Barth | d199dcd | 2017-11-20 10:36:41 -0600 | [diff] [blame] | 156 | void TachSensor::setFunctional(bool functional) |
| 157 | { |
| 158 | _functional = functional; |
| 159 | updateInventory(_functional); |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 160 | |
| 161 | if (!_errorTimer) |
| 162 | { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | if (!_functional) |
| 167 | { |
| 168 | if (_fan.present()) |
| 169 | { |
| 170 | _errorTimer->restartOnce(std::chrono::seconds(*_errorDelay)); |
| 171 | } |
| 172 | } |
| 173 | else if (_errorTimer->isEnabled()) |
| 174 | { |
| 175 | _errorTimer->setEnabled(false); |
| 176 | } |
Matthew Barth | d199dcd | 2017-11-20 10:36:41 -0600 | [diff] [blame] | 177 | } |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 178 | |
Brad Bishop | 771659f | 2017-07-30 19:52:21 -0400 | [diff] [blame] | 179 | void TachSensor::handleTargetChange(sdbusplus::message::message& msg) |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 180 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 181 | readPropertyFromMessage(msg, _interface, FAN_TARGET_PROPERTY, _tachTarget); |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 182 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 183 | // Check all tach sensors on the fan against the target |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 184 | _fan.tachChanged(); |
| 185 | } |
| 186 | |
Brad Bishop | 771659f | 2017-07-30 19:52:21 -0400 | [diff] [blame] | 187 | void TachSensor::handleTachChange(sdbusplus::message::message& msg) |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 188 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 189 | readPropertyFromMessage(msg, FAN_SENSOR_VALUE_INTF, FAN_VALUE_PROPERTY, |
| 190 | _tachInput); |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 191 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 192 | // Check just this sensor against the target |
| 193 | _fan.tachChanged(*this); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 194 | } |
| 195 | |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 196 | void TachSensor::startTimer(TimerMode mode) |
| 197 | { |
William A. Kennington III | 8fd879f | 2018-10-30 19:49:29 -0700 | [diff] [blame] | 198 | if (!timerRunning() || mode != _timerMode) |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 199 | { |
William A. Kennington III | 8fd879f | 2018-10-30 19:49:29 -0700 | [diff] [blame] | 200 | _timer.restartOnce(getDelay(mode)); |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 201 | _timerMode = mode; |
| 202 | } |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 203 | } |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 204 | |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 205 | std::chrono::microseconds TachSensor::getDelay(TimerMode mode) |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 206 | { |
| 207 | using namespace std::chrono; |
| 208 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 209 | switch (mode) |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 210 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 211 | case TimerMode::nonfunc: |
| 212 | return duration_cast<microseconds>(seconds(_timeout)); |
| 213 | case TimerMode::func: |
| 214 | return duration_cast<microseconds>(seconds(_funcDelay)); |
| 215 | default: |
| 216 | // Log an internal error for undefined timer mode |
| 217 | log<level::ERR>("Undefined timer mode", |
| 218 | entry("TIMER_MODE=%u", mode)); |
| 219 | elog<InternalFailure>(); |
| 220 | return duration_cast<microseconds>(seconds(0)); |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 221 | } |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 222 | } |
| 223 | |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 224 | void TachSensor::setCounter(bool count) |
| 225 | { |
| 226 | if (count) |
| 227 | { |
| 228 | if (_counter < _threshold) |
| 229 | { |
| 230 | ++_counter; |
| 231 | } |
| 232 | } |
| 233 | else |
| 234 | { |
| 235 | if (_counter > 0) |
| 236 | { |
| 237 | --_counter; |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
Matthew Barth | 4d98285 | 2017-11-17 09:37:13 -0600 | [diff] [blame] | 242 | void TachSensor::updateInventory(bool functional) |
| 243 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 244 | auto objectMap = |
| 245 | util::getObjMap<bool>(_invName, util::OPERATIONAL_STATUS_INTF, |
| 246 | util::FUNCTIONAL_PROPERTY, functional); |
Matthew Barth | 4d98285 | 2017-11-17 09:37:13 -0600 | [diff] [blame] | 247 | auto response = util::SDBusPlus::lookupAndCallMethod( |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 248 | _bus, util::INVENTORY_PATH, util::INVENTORY_INTF, "Notify", objectMap); |
Matthew Barth | 4d98285 | 2017-11-17 09:37:13 -0600 | [diff] [blame] | 249 | if (response.is_method_error()) |
| 250 | { |
| 251 | log<level::ERR>("Error in notify update of tach sensor inventory"); |
| 252 | } |
| 253 | } |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 254 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 255 | } // namespace monitor |
| 256 | } // namespace fan |
| 257 | } // namespace phosphor |