blob: 42b826f64da3b0bfa0aab56d6f567e9da201ac42 [file] [log] [blame]
Matt Spinlerabf8da32017-04-27 14:08:45 -05001/**
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 Barth177fe982020-05-26 11:05:19 -050016#include "tach_sensor.hpp"
17
Matt Spinlerabf8da32017-04-27 14:08:45 -050018#include "fan.hpp"
Brad Bishop2a58e2c2017-07-30 13:49:09 -040019#include "sdbusplus.hpp"
Matthew Barth4d982852017-11-17 09:37:13 -060020#include "utility.hpp"
Matt Spinlerabf8da32017-04-27 14:08:45 -050021
Matthew Barth177fe982020-05-26 11:05:19 -050022#include <phosphor-logging/elog.hpp>
23#include <phosphor-logging/log.hpp>
24
25#include <experimental/filesystem>
26#include <functional>
27
Matt Spinlerabf8da32017-04-27 14:08:45 -050028namespace phosphor
29{
30namespace fan
31{
32namespace monitor
33{
34
Matt Spinlerebaae612017-04-27 14:21:48 -050035constexpr auto FAN_SENSOR_VALUE_INTF = "xyz.openbmc_project.Sensor.Value";
36constexpr auto FAN_TARGET_PROPERTY = "Target";
37constexpr auto FAN_VALUE_PROPERTY = "Value";
38
Matthew Barth4d982852017-11-17 09:37:13 -060039using namespace std::experimental::filesystem;
Matthew Barth177fe982020-05-26 11:05:19 -050040using InternalFailure =
41 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Matt Spinlerebaae612017-04-27 14:21:48 -050042
43/**
44 * @brief Helper function to read a property
45 *
46 * @param[in] interface - the interface the property is on
47 * @param[in] propertName - the name of the property
48 * @param[in] path - the dbus path
Matt Spinlerebaae612017-04-27 14:21:48 -050049 * @param[in] bus - the dbus object
50 * @param[out] value - filled in with the property value
51 */
Matthew Barth177fe982020-05-26 11:05:19 -050052template <typename T>
53static void
54 readProperty(const std::string& interface, const std::string& propertyName,
55 const std::string& path, sdbusplus::bus::bus& bus, T& value)
Matt Spinlerebaae612017-04-27 14:21:48 -050056{
Matt Spinlerebaae612017-04-27 14:21:48 -050057 try
58 {
Matthew Barth177fe982020-05-26 11:05:19 -050059 value =
60 util::SDBusPlus::getProperty<T>(bus, path, interface, propertyName);
Matt Spinlerebaae612017-04-27 14:21:48 -050061 }
62 catch (std::exception& e)
63 {
64 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
65 }
66}
Matt Spinlerabf8da32017-04-27 14:08:45 -050067
Matthew Barth177fe982020-05-26 11:05:19 -050068TachSensor::TachSensor(Mode mode, sdbusplus::bus::bus& bus, Fan& fan,
69 const std::string& id, bool hasTarget, size_t funcDelay,
70 const std::string& interface, double factor,
71 int64_t offset, size_t timeout,
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070072 const sdeventplus::Event& event) :
Matt Spinlerabf8da32017-04-27 14:08:45 -050073 _bus(bus),
Matthew Barth177fe982020-05-26 11:05:19 -050074 _fan(fan), _name(FAN_SENSOR_PATH + id), _invName(path(fan.getName()) / id),
75 _hasTarget(hasTarget), _funcDelay(funcDelay), _interface(interface),
76 _factor(factor), _offset(offset), _timeout(timeout),
Matthew Barth3800ae72018-02-19 16:08:04 -060077 _timerMode(TimerMode::func),
William A. Kennington III22c36ab2018-10-30 19:50:57 -070078 _timer(event, std::bind(&Fan::timerExpired, &fan, std::ref(*this)))
Matt Spinlerabf8da32017-04-27 14:08:45 -050079{
Matthew Barthd199dcd2017-11-20 10:36:41 -060080 // Start from a known state of functional
Matthew Barth7f7df312018-01-15 16:27:50 -060081 setFunctional(true);
Brad Bishopedaeb312017-07-30 19:38:20 -040082
Matthew Barth0a9fe162018-01-26 12:53:15 -060083 // Load in current Target and Input values when entering monitor mode
84 if (mode != Mode::init)
Brad Bishopedaeb312017-07-30 19:38:20 -040085 {
Matthew Barth0a9fe162018-01-26 12:53:15 -060086 try
87 {
88 // Use getProperty directly to allow a missing sensor object
89 // to abort construction.
90 _tachInput = util::SDBusPlus::getProperty<decltype(_tachInput)>(
Matthew Barth177fe982020-05-26 11:05:19 -050091 _bus, _name, FAN_SENSOR_VALUE_INTF, FAN_VALUE_PROPERTY);
Matthew Barth0a9fe162018-01-26 12:53:15 -060092 }
93 catch (std::exception& e)
94 {
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050095 log<level::INFO>("Not monitoring a tach sensor",
Matthew Barth177fe982020-05-26 11:05:19 -050096 entry("SENSOR=%s", _name.c_str()));
Matthew Barth0a9fe162018-01-26 12:53:15 -060097 throw InvalidSensorError();
98 }
99
100 if (_hasTarget)
101 {
Matthew Barth177fe982020-05-26 11:05:19 -0500102 readProperty(_interface, FAN_TARGET_PROPERTY, _name, _bus,
Matthew Barth0a9fe162018-01-26 12:53:15 -0600103 _tachTarget);
104 }
105
106 auto match = getMatchString(FAN_SENSOR_VALUE_INTF);
107
108 tachSignal = std::make_unique<sdbusplus::server::match::match>(
Matthew Barth177fe982020-05-26 11:05:19 -0500109 _bus, match.c_str(),
110 [this](auto& msg) { this->handleTachChange(msg); });
Matthew Barth0a9fe162018-01-26 12:53:15 -0600111
112 if (_hasTarget)
113 {
Lei YU80f271b2018-01-31 15:24:46 +0800114 match = getMatchString(_interface);
Matthew Barth0a9fe162018-01-26 12:53:15 -0600115
116 targetSignal = std::make_unique<sdbusplus::server::match::match>(
Matthew Barth177fe982020-05-26 11:05:19 -0500117 _bus, match.c_str(),
118 [this](auto& msg) { this->handleTargetChange(msg); });
Matthew Barth0a9fe162018-01-26 12:53:15 -0600119 }
Brad Bishopedaeb312017-07-30 19:38:20 -0400120 }
Matt Spinlerebaae612017-04-27 14:21:48 -0500121}
122
Matt Spinlerebaae612017-04-27 14:21:48 -0500123std::string TachSensor::getMatchString(const std::string& interface)
124{
Matthew Barth177fe982020-05-26 11:05:19 -0500125 return sdbusplus::bus::match::rules::propertiesChanged(_name, interface);
Matt Spinlerebaae612017-04-27 14:21:48 -0500126}
127
Matthew Barthf552ea52018-01-15 16:22:04 -0600128uint64_t TachSensor::getTarget() const
129{
130 if (!_hasTarget)
131 {
132 return _fan.findTargetSpeed();
133 }
134 return _tachTarget;
135}
136
Matthew Barthd199dcd2017-11-20 10:36:41 -0600137void TachSensor::setFunctional(bool functional)
138{
139 _functional = functional;
140 updateInventory(_functional);
141}
Matt Spinlerebaae612017-04-27 14:21:48 -0500142
Matt Spinlerebaae612017-04-27 14:21:48 -0500143/**
144 * @brief Reads a property from the input message and stores it in value.
145 * T is the value type.
146 *
147 * Note: This can only be called once per message.
148 *
149 * @param[in] msg - the dbus message that contains the data
150 * @param[in] interface - the interface the property is on
151 * @param[in] propertName - the name of the property
152 * @param[out] value - the value to store the property value in
153 */
Matthew Barth177fe982020-05-26 11:05:19 -0500154template <typename T>
Matt Spinlerebaae612017-04-27 14:21:48 -0500155static void readPropertyFromMessage(sdbusplus::message::message& msg,
156 const std::string& interface,
Matthew Barth177fe982020-05-26 11:05:19 -0500157 const std::string& propertyName, T& value)
Matt Spinlerebaae612017-04-27 14:21:48 -0500158{
159 std::string sensor;
Patrick Williamsc21d0b32020-05-13 17:55:14 -0500160 std::map<std::string, std::variant<T>> data;
Matt Spinlerebaae612017-04-27 14:21:48 -0500161 msg.read(sensor, data);
162
163 if (sensor.compare(interface) == 0)
164 {
165 auto propertyMap = data.find(propertyName);
166 if (propertyMap != data.end())
167 {
Patrick Williams1f514fa2020-05-13 11:53:33 -0500168 value = std::get<T>(propertyMap->second);
Matt Spinlerebaae612017-04-27 14:21:48 -0500169 }
170 }
171}
172
Brad Bishop771659f2017-07-30 19:52:21 -0400173void TachSensor::handleTargetChange(sdbusplus::message::message& msg)
Matt Spinlerebaae612017-04-27 14:21:48 -0500174{
Matthew Barth177fe982020-05-26 11:05:19 -0500175 readPropertyFromMessage(msg, _interface, FAN_TARGET_PROPERTY, _tachTarget);
Matt Spinlerebaae612017-04-27 14:21:48 -0500176
Matthew Barth177fe982020-05-26 11:05:19 -0500177 // Check all tach sensors on the fan against the target
Matt Spinlerebaae612017-04-27 14:21:48 -0500178 _fan.tachChanged();
179}
180
Brad Bishop771659f2017-07-30 19:52:21 -0400181void TachSensor::handleTachChange(sdbusplus::message::message& msg)
Matt Spinlerebaae612017-04-27 14:21:48 -0500182{
Matthew Barth177fe982020-05-26 11:05:19 -0500183 readPropertyFromMessage(msg, FAN_SENSOR_VALUE_INTF, FAN_VALUE_PROPERTY,
184 _tachInput);
Matt Spinlerebaae612017-04-27 14:21:48 -0500185
Matthew Barth177fe982020-05-26 11:05:19 -0500186 // Check just this sensor against the target
187 _fan.tachChanged(*this);
Matt Spinlerabf8da32017-04-27 14:08:45 -0500188}
189
Matthew Barth3800ae72018-02-19 16:08:04 -0600190void TachSensor::startTimer(TimerMode mode)
191{
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700192 if (!timerRunning() || mode != _timerMode)
Matthew Barth3800ae72018-02-19 16:08:04 -0600193 {
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700194 _timer.restartOnce(getDelay(mode));
Matthew Barth3800ae72018-02-19 16:08:04 -0600195 _timerMode = mode;
196 }
Matthew Barth3800ae72018-02-19 16:08:04 -0600197}
Matt Spinlerabf8da32017-04-27 14:08:45 -0500198
Matthew Barth3800ae72018-02-19 16:08:04 -0600199std::chrono::microseconds TachSensor::getDelay(TimerMode mode)
Matt Spinlera9406a72017-04-27 14:29:24 -0500200{
201 using namespace std::chrono;
202
Matthew Barth177fe982020-05-26 11:05:19 -0500203 switch (mode)
Matthew Barth3800ae72018-02-19 16:08:04 -0600204 {
Matthew Barth177fe982020-05-26 11:05:19 -0500205 case TimerMode::nonfunc:
206 return duration_cast<microseconds>(seconds(_timeout));
207 case TimerMode::func:
208 return duration_cast<microseconds>(seconds(_funcDelay));
209 default:
210 // Log an internal error for undefined timer mode
211 log<level::ERR>("Undefined timer mode",
212 entry("TIMER_MODE=%u", mode));
213 elog<InternalFailure>();
214 return duration_cast<microseconds>(seconds(0));
Matthew Barth3800ae72018-02-19 16:08:04 -0600215 }
Matt Spinlera9406a72017-04-27 14:29:24 -0500216}
217
Matthew Barth4d982852017-11-17 09:37:13 -0600218void TachSensor::updateInventory(bool functional)
219{
Matthew Barth177fe982020-05-26 11:05:19 -0500220 auto objectMap =
221 util::getObjMap<bool>(_invName, util::OPERATIONAL_STATUS_INTF,
222 util::FUNCTIONAL_PROPERTY, functional);
Matthew Barth4d982852017-11-17 09:37:13 -0600223 auto response = util::SDBusPlus::lookupAndCallMethod(
Matthew Barth177fe982020-05-26 11:05:19 -0500224 _bus, util::INVENTORY_PATH, util::INVENTORY_INTF, "Notify", objectMap);
Matthew Barth4d982852017-11-17 09:37:13 -0600225 if (response.is_method_error())
226 {
227 log<level::ERR>("Error in notify update of tach sensor inventory");
228 }
229}
Matt Spinlera9406a72017-04-27 14:29:24 -0500230
Matthew Barth177fe982020-05-26 11:05:19 -0500231} // namespace monitor
232} // namespace fan
233} // namespace phosphor