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