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, |
| 73 | int64_t offset, size_t timeout, |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame] | 74 | const sdeventplus::Event& event) : |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 75 | _bus(bus), |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 76 | _fan(fan), _name(FAN_SENSOR_PATH + id), _invName(path(fan.getName()) / id), |
| 77 | _hasTarget(hasTarget), _funcDelay(funcDelay), _interface(interface), |
| 78 | _factor(factor), _offset(offset), _timeout(timeout), |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 79 | _timerMode(TimerMode::func), |
William A. Kennington III | 22c36ab | 2018-10-30 19:50:57 -0700 | [diff] [blame] | 80 | _timer(event, std::bind(&Fan::timerExpired, &fan, std::ref(*this))) |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 81 | { |
Matthew Barth | d199dcd | 2017-11-20 10:36:41 -0600 | [diff] [blame] | 82 | // Start from a known state of functional |
Matthew Barth | 7f7df31 | 2018-01-15 16:27:50 -0600 | [diff] [blame] | 83 | setFunctional(true); |
Brad Bishop | edaeb31 | 2017-07-30 19:38:20 -0400 | [diff] [blame] | 84 | |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 85 | // Load in current Target and Input values when entering monitor mode |
| 86 | if (mode != Mode::init) |
Brad Bishop | edaeb31 | 2017-07-30 19:38:20 -0400 | [diff] [blame] | 87 | { |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 88 | try |
| 89 | { |
| 90 | // Use getProperty directly to allow a missing sensor object |
| 91 | // to abort construction. |
| 92 | _tachInput = util::SDBusPlus::getProperty<decltype(_tachInput)>( |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 93 | _bus, _name, FAN_SENSOR_VALUE_INTF, FAN_VALUE_PROPERTY); |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 94 | } |
| 95 | catch (std::exception& e) |
| 96 | { |
Jolie Ku | 4c3c24f | 2020-09-09 11:01:46 +0800 | [diff] [blame^] | 97 | log<level::ERR>( |
| 98 | fmt::format("Failed to retrieve tach sensor {}", _name) |
| 99 | .c_str()); |
| 100 | // mark tach sensor as nonfunctional |
| 101 | setFunctional(false); |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 102 | throw InvalidSensorError(); |
| 103 | } |
| 104 | |
| 105 | if (_hasTarget) |
| 106 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 107 | readProperty(_interface, FAN_TARGET_PROPERTY, _name, _bus, |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 108 | _tachTarget); |
| 109 | } |
| 110 | |
| 111 | auto match = getMatchString(FAN_SENSOR_VALUE_INTF); |
| 112 | |
| 113 | tachSignal = std::make_unique<sdbusplus::server::match::match>( |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 114 | _bus, match.c_str(), |
| 115 | [this](auto& msg) { this->handleTachChange(msg); }); |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 116 | |
| 117 | if (_hasTarget) |
| 118 | { |
Lei YU | 80f271b | 2018-01-31 15:24:46 +0800 | [diff] [blame] | 119 | match = getMatchString(_interface); |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 120 | |
| 121 | targetSignal = std::make_unique<sdbusplus::server::match::match>( |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 122 | _bus, match.c_str(), |
| 123 | [this](auto& msg) { this->handleTargetChange(msg); }); |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 124 | } |
Brad Bishop | edaeb31 | 2017-07-30 19:38:20 -0400 | [diff] [blame] | 125 | } |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 126 | } |
| 127 | |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 128 | std::string TachSensor::getMatchString(const std::string& interface) |
| 129 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 130 | return sdbusplus::bus::match::rules::propertiesChanged(_name, interface); |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 131 | } |
| 132 | |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 133 | uint64_t TachSensor::getTarget() const |
| 134 | { |
| 135 | if (!_hasTarget) |
| 136 | { |
| 137 | return _fan.findTargetSpeed(); |
| 138 | } |
| 139 | return _tachTarget; |
| 140 | } |
| 141 | |
Matthew Barth | d199dcd | 2017-11-20 10:36:41 -0600 | [diff] [blame] | 142 | void TachSensor::setFunctional(bool functional) |
| 143 | { |
| 144 | _functional = functional; |
| 145 | updateInventory(_functional); |
| 146 | } |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 147 | |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 148 | /** |
| 149 | * @brief Reads a property from the input message and stores it in value. |
| 150 | * T is the value type. |
| 151 | * |
| 152 | * Note: This can only be called once per message. |
| 153 | * |
| 154 | * @param[in] msg - the dbus message that contains the data |
| 155 | * @param[in] interface - the interface the property is on |
| 156 | * @param[in] propertName - the name of the property |
| 157 | * @param[out] value - the value to store the property value in |
| 158 | */ |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 159 | template <typename T> |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 160 | static void readPropertyFromMessage(sdbusplus::message::message& msg, |
| 161 | const std::string& interface, |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 162 | const std::string& propertyName, T& value) |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 163 | { |
| 164 | std::string sensor; |
Patrick Williams | c21d0b3 | 2020-05-13 17:55:14 -0500 | [diff] [blame] | 165 | std::map<std::string, std::variant<T>> data; |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 166 | msg.read(sensor, data); |
| 167 | |
| 168 | if (sensor.compare(interface) == 0) |
| 169 | { |
| 170 | auto propertyMap = data.find(propertyName); |
| 171 | if (propertyMap != data.end()) |
| 172 | { |
Patrick Williams | 1f514fa | 2020-05-13 11:53:33 -0500 | [diff] [blame] | 173 | value = std::get<T>(propertyMap->second); |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
Brad Bishop | 771659f | 2017-07-30 19:52:21 -0400 | [diff] [blame] | 178 | void TachSensor::handleTargetChange(sdbusplus::message::message& msg) |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 179 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 180 | readPropertyFromMessage(msg, _interface, FAN_TARGET_PROPERTY, _tachTarget); |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 181 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 182 | // Check all tach sensors on the fan against the target |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 183 | _fan.tachChanged(); |
| 184 | } |
| 185 | |
Brad Bishop | 771659f | 2017-07-30 19:52:21 -0400 | [diff] [blame] | 186 | void TachSensor::handleTachChange(sdbusplus::message::message& msg) |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 187 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 188 | readPropertyFromMessage(msg, FAN_SENSOR_VALUE_INTF, FAN_VALUE_PROPERTY, |
| 189 | _tachInput); |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 190 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 191 | // Check just this sensor against the target |
| 192 | _fan.tachChanged(*this); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 193 | } |
| 194 | |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 195 | void TachSensor::startTimer(TimerMode mode) |
| 196 | { |
William A. Kennington III | 8fd879f | 2018-10-30 19:49:29 -0700 | [diff] [blame] | 197 | if (!timerRunning() || mode != _timerMode) |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 198 | { |
William A. Kennington III | 8fd879f | 2018-10-30 19:49:29 -0700 | [diff] [blame] | 199 | _timer.restartOnce(getDelay(mode)); |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 200 | _timerMode = mode; |
| 201 | } |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 202 | } |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 203 | |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 204 | std::chrono::microseconds TachSensor::getDelay(TimerMode mode) |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 205 | { |
| 206 | using namespace std::chrono; |
| 207 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 208 | switch (mode) |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 209 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 210 | case TimerMode::nonfunc: |
| 211 | return duration_cast<microseconds>(seconds(_timeout)); |
| 212 | case TimerMode::func: |
| 213 | return duration_cast<microseconds>(seconds(_funcDelay)); |
| 214 | default: |
| 215 | // Log an internal error for undefined timer mode |
| 216 | log<level::ERR>("Undefined timer mode", |
| 217 | entry("TIMER_MODE=%u", mode)); |
| 218 | elog<InternalFailure>(); |
| 219 | return duration_cast<microseconds>(seconds(0)); |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 220 | } |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 221 | } |
| 222 | |
Matthew Barth | 4d98285 | 2017-11-17 09:37:13 -0600 | [diff] [blame] | 223 | void TachSensor::updateInventory(bool functional) |
| 224 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 225 | auto objectMap = |
| 226 | util::getObjMap<bool>(_invName, util::OPERATIONAL_STATUS_INTF, |
| 227 | util::FUNCTIONAL_PROPERTY, functional); |
Matthew Barth | 4d98285 | 2017-11-17 09:37:13 -0600 | [diff] [blame] | 228 | auto response = util::SDBusPlus::lookupAndCallMethod( |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 229 | _bus, util::INVENTORY_PATH, util::INVENTORY_INTF, "Notify", objectMap); |
Matthew Barth | 4d98285 | 2017-11-17 09:37:13 -0600 | [diff] [blame] | 230 | if (response.is_method_error()) |
| 231 | { |
| 232 | log<level::ERR>("Error in notify update of tach sensor inventory"); |
| 233 | } |
| 234 | } |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 235 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 236 | } // namespace monitor |
| 237 | } // namespace fan |
| 238 | } // namespace phosphor |