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