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