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