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