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 | */ |
| 16 | #include <phosphor-logging/log.hpp> |
| 17 | #include "fan.hpp" |
Brad Bishop | 2a58e2c | 2017-07-30 13:49:09 -0400 | [diff] [blame] | 18 | #include "sdbusplus.hpp" |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 19 | #include "tach_sensor.hpp" |
| 20 | #include "../utility.hpp" |
| 21 | |
| 22 | namespace phosphor |
| 23 | { |
| 24 | namespace fan |
| 25 | { |
| 26 | namespace monitor |
| 27 | { |
| 28 | |
| 29 | constexpr auto FAN_SENSOR_PATH = "/xyz/openbmc_project/sensors/fan_tach/"; |
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 | |
| 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 | |
| 66 | TachSensor::TachSensor(sdbusplus::bus::bus& bus, |
| 67 | Fan& fan, |
| 68 | const std::string& id, |
| 69 | bool hasTarget, |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 70 | size_t timeout, |
Matt Spinler | e824f98 | 2017-05-11 10:07:55 -0500 | [diff] [blame] | 71 | phosphor::fan::event::EventPtr& events) : |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 72 | _bus(bus), |
| 73 | _fan(fan), |
| 74 | _name(FAN_SENSOR_PATH + id), |
| 75 | _hasTarget(hasTarget), |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 76 | _timeout(timeout), |
| 77 | _timer(events, [this, &fan](){ fan.timerExpired(*this); }) |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 78 | { |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 79 | //Load in starting Target and Input values |
Brad Bishop | edaeb31 | 2017-07-30 19:38:20 -0400 | [diff] [blame] | 80 | |
| 81 | try |
| 82 | { |
| 83 | // Use getProperty directly to allow a missing sensor object |
| 84 | // to abort construction. |
| 85 | _tachInput = util::SDBusPlus::getProperty<decltype(_tachInput)>( |
| 86 | _bus, |
| 87 | _name, |
| 88 | FAN_SENSOR_VALUE_INTF, |
| 89 | FAN_VALUE_PROPERTY); |
| 90 | } |
| 91 | catch (std::exception& e) |
| 92 | { |
| 93 | throw InvalidSensorError(); |
| 94 | } |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 95 | |
| 96 | if (_hasTarget) |
| 97 | { |
| 98 | readProperty(FAN_SENSOR_CONTROL_INTF, |
| 99 | FAN_TARGET_PROPERTY, |
| 100 | _name, |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 101 | _bus, |
| 102 | _tachTarget); |
| 103 | } |
| 104 | |
| 105 | auto match = getMatchString(FAN_SENSOR_VALUE_INTF); |
| 106 | |
| 107 | tachSignal = std::make_unique<sdbusplus::server::match::match>( |
| 108 | _bus, |
| 109 | match.c_str(), |
Brad Bishop | 771659f | 2017-07-30 19:52:21 -0400 | [diff] [blame] | 110 | [this](auto& msg){ this->handleTachChange(msg); }); |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 111 | |
| 112 | if (_hasTarget) |
| 113 | { |
| 114 | match = getMatchString(FAN_SENSOR_CONTROL_INTF); |
| 115 | |
| 116 | targetSignal = std::make_unique<sdbusplus::server::match::match>( |
| 117 | _bus, |
| 118 | match.c_str(), |
Brad Bishop | 771659f | 2017-07-30 19:52:21 -0400 | [diff] [blame] | 119 | [this](auto& msg){ this->handleTargetChange(msg); }); |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | } |
| 123 | |
| 124 | |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 125 | std::string TachSensor::getMatchString(const std::string& interface) |
| 126 | { |
Brad Bishop | 78b5845 | 2017-07-30 19:44:49 -0400 | [diff] [blame] | 127 | return sdbusplus::bus::match::rules::propertiesChanged( |
| 128 | _name, interface); |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 132 | /** |
| 133 | * @brief Reads a property from the input message and stores it in value. |
| 134 | * T is the value type. |
| 135 | * |
| 136 | * Note: This can only be called once per message. |
| 137 | * |
| 138 | * @param[in] msg - the dbus message that contains the data |
| 139 | * @param[in] interface - the interface the property is on |
| 140 | * @param[in] propertName - the name of the property |
| 141 | * @param[out] value - the value to store the property value in |
| 142 | */ |
| 143 | template<typename T> |
| 144 | static void readPropertyFromMessage(sdbusplus::message::message& msg, |
| 145 | const std::string& interface, |
| 146 | const std::string& propertyName, |
| 147 | T& value) |
| 148 | { |
| 149 | std::string sensor; |
| 150 | std::map<std::string, sdbusplus::message::variant<T>> data; |
| 151 | msg.read(sensor, data); |
| 152 | |
| 153 | if (sensor.compare(interface) == 0) |
| 154 | { |
| 155 | auto propertyMap = data.find(propertyName); |
| 156 | if (propertyMap != data.end()) |
| 157 | { |
| 158 | value = sdbusplus::message::variant_ns::get<T>( |
| 159 | propertyMap->second); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | |
Brad Bishop | 771659f | 2017-07-30 19:52:21 -0400 | [diff] [blame] | 165 | void TachSensor::handleTargetChange(sdbusplus::message::message& msg) |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 166 | { |
| 167 | readPropertyFromMessage(msg, |
| 168 | FAN_SENSOR_CONTROL_INTF, |
| 169 | FAN_TARGET_PROPERTY, |
| 170 | _tachTarget); |
| 171 | |
| 172 | //Check all tach sensors on the fan against the target |
| 173 | _fan.tachChanged(); |
| 174 | } |
| 175 | |
| 176 | |
Brad Bishop | 771659f | 2017-07-30 19:52:21 -0400 | [diff] [blame] | 177 | void TachSensor::handleTachChange(sdbusplus::message::message& msg) |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 178 | { |
| 179 | readPropertyFromMessage(msg, |
| 180 | FAN_SENSOR_VALUE_INTF, |
| 181 | FAN_VALUE_PROPERTY, |
| 182 | _tachInput); |
| 183 | |
| 184 | //Check just this sensor against the target |
| 185 | _fan.tachChanged(*this); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 189 | std::chrono::microseconds TachSensor::getTimeout() |
| 190 | { |
| 191 | using namespace std::chrono; |
| 192 | |
| 193 | return duration_cast<microseconds>(seconds(_timeout)); |
| 194 | } |
| 195 | |
| 196 | |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | } |