blob: 078b213b8188ba2d61648e5eee8410fdcc699a2b [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
Jolie Ku4c3c24f2020-09-09 11:01:46 +080022#include <fmt/format.h>
23
Matthew Barth177fe982020-05-26 11:05:19 -050024#include <phosphor-logging/elog.hpp>
25#include <phosphor-logging/log.hpp>
26
27#include <experimental/filesystem>
28#include <functional>
29
Matt Spinlerabf8da32017-04-27 14:08:45 -050030namespace phosphor
31{
32namespace fan
33{
34namespace monitor
35{
36
Matt Spinlerebaae612017-04-27 14:21:48 -050037constexpr auto FAN_SENSOR_VALUE_INTF = "xyz.openbmc_project.Sensor.Value";
38constexpr auto FAN_TARGET_PROPERTY = "Target";
39constexpr auto FAN_VALUE_PROPERTY = "Value";
40
Matthew Barth4d982852017-11-17 09:37:13 -060041using namespace std::experimental::filesystem;
Matthew Barth177fe982020-05-26 11:05:19 -050042using InternalFailure =
43 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Matt Spinlerebaae612017-04-27 14:21:48 -050044
45/**
46 * @brief Helper function to read a property
47 *
48 * @param[in] interface - the interface the property is on
49 * @param[in] propertName - the name of the property
50 * @param[in] path - the dbus path
Matt Spinlerebaae612017-04-27 14:21:48 -050051 * @param[in] bus - the dbus object
52 * @param[out] value - filled in with the property value
53 */
Matthew Barth177fe982020-05-26 11:05:19 -050054template <typename T>
55static void
56 readProperty(const std::string& interface, const std::string& propertyName,
57 const std::string& path, sdbusplus::bus::bus& bus, T& value)
Matt Spinlerebaae612017-04-27 14:21:48 -050058{
Matt Spinlerebaae612017-04-27 14:21:48 -050059 try
60 {
Matthew Barth177fe982020-05-26 11:05:19 -050061 value =
62 util::SDBusPlus::getProperty<T>(bus, path, interface, propertyName);
Matt Spinlerebaae612017-04-27 14:21:48 -050063 }
64 catch (std::exception& e)
65 {
66 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
67 }
68}
Matt Spinlerabf8da32017-04-27 14:08:45 -050069
Matthew Barth177fe982020-05-26 11:05:19 -050070TachSensor::TachSensor(Mode mode, sdbusplus::bus::bus& bus, Fan& fan,
71 const std::string& id, bool hasTarget, size_t funcDelay,
72 const std::string& interface, double factor,
73 int64_t offset, size_t timeout,
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070074 const sdeventplus::Event& event) :
Matt Spinlerabf8da32017-04-27 14:08:45 -050075 _bus(bus),
Matthew Barth177fe982020-05-26 11:05:19 -050076 _fan(fan), _name(FAN_SENSOR_PATH + id), _invName(path(fan.getName()) / id),
77 _hasTarget(hasTarget), _funcDelay(funcDelay), _interface(interface),
78 _factor(factor), _offset(offset), _timeout(timeout),
Matthew Barth3800ae72018-02-19 16:08:04 -060079 _timerMode(TimerMode::func),
William A. Kennington III22c36ab2018-10-30 19:50:57 -070080 _timer(event, std::bind(&Fan::timerExpired, &fan, std::ref(*this)))
Matt Spinlerabf8da32017-04-27 14:08:45 -050081{
Matthew Barthd199dcd2017-11-20 10:36:41 -060082 // Start from a known state of functional
Matthew Barth7f7df312018-01-15 16:27:50 -060083 setFunctional(true);
Brad Bishopedaeb312017-07-30 19:38:20 -040084
Matthew Barth0a9fe162018-01-26 12:53:15 -060085 // Load in current Target and Input values when entering monitor mode
86 if (mode != Mode::init)
Brad Bishopedaeb312017-07-30 19:38:20 -040087 {
Matthew Barth0a9fe162018-01-26 12:53:15 -060088 try
89 {
90 // Use getProperty directly to allow a missing sensor object
91 // to abort construction.
92 _tachInput = util::SDBusPlus::getProperty<decltype(_tachInput)>(
Matthew Barth177fe982020-05-26 11:05:19 -050093 _bus, _name, FAN_SENSOR_VALUE_INTF, FAN_VALUE_PROPERTY);
Matthew Barth0a9fe162018-01-26 12:53:15 -060094 }
95 catch (std::exception& e)
96 {
Jolie Ku4c3c24f2020-09-09 11:01:46 +080097 log<level::ERR>(
98 fmt::format("Failed to retrieve tach sensor {}", _name)
99 .c_str());
100 // mark tach sensor as nonfunctional
101 setFunctional(false);
Matthew Barth0a9fe162018-01-26 12:53:15 -0600102 throw InvalidSensorError();
103 }
104
105 if (_hasTarget)
106 {
Matthew Barth177fe982020-05-26 11:05:19 -0500107 readProperty(_interface, FAN_TARGET_PROPERTY, _name, _bus,
Matthew Barth0a9fe162018-01-26 12:53:15 -0600108 _tachTarget);
109 }
110
111 auto match = getMatchString(FAN_SENSOR_VALUE_INTF);
112
113 tachSignal = std::make_unique<sdbusplus::server::match::match>(
Matthew Barth177fe982020-05-26 11:05:19 -0500114 _bus, match.c_str(),
115 [this](auto& msg) { this->handleTachChange(msg); });
Matthew Barth0a9fe162018-01-26 12:53:15 -0600116
117 if (_hasTarget)
118 {
Lei YU80f271b2018-01-31 15:24:46 +0800119 match = getMatchString(_interface);
Matthew Barth0a9fe162018-01-26 12:53:15 -0600120
121 targetSignal = std::make_unique<sdbusplus::server::match::match>(
Matthew Barth177fe982020-05-26 11:05:19 -0500122 _bus, match.c_str(),
123 [this](auto& msg) { this->handleTargetChange(msg); });
Matthew Barth0a9fe162018-01-26 12:53:15 -0600124 }
Brad Bishopedaeb312017-07-30 19:38:20 -0400125 }
Matt Spinlerebaae612017-04-27 14:21:48 -0500126}
127
Matt Spinlerebaae612017-04-27 14:21:48 -0500128std::string TachSensor::getMatchString(const std::string& interface)
129{
Matthew Barth177fe982020-05-26 11:05:19 -0500130 return sdbusplus::bus::match::rules::propertiesChanged(_name, interface);
Matt Spinlerebaae612017-04-27 14:21:48 -0500131}
132
Matthew Barthf552ea52018-01-15 16:22:04 -0600133uint64_t TachSensor::getTarget() const
134{
135 if (!_hasTarget)
136 {
137 return _fan.findTargetSpeed();
138 }
139 return _tachTarget;
140}
141
Matthew Barthd199dcd2017-11-20 10:36:41 -0600142void TachSensor::setFunctional(bool functional)
143{
144 _functional = functional;
145 updateInventory(_functional);
146}
Matt Spinlerebaae612017-04-27 14:21:48 -0500147
Matt Spinlerebaae612017-04-27 14:21:48 -0500148/**
149 * @brief Reads a property from the input message and stores it in value.
150 * T is the value type.
151 *
152 * Note: This can only be called once per message.
153 *
154 * @param[in] msg - the dbus message that contains the data
155 * @param[in] interface - the interface the property is on
156 * @param[in] propertName - the name of the property
157 * @param[out] value - the value to store the property value in
158 */
Matthew Barth177fe982020-05-26 11:05:19 -0500159template <typename T>
Matt Spinlerebaae612017-04-27 14:21:48 -0500160static void readPropertyFromMessage(sdbusplus::message::message& msg,
161 const std::string& interface,
Matthew Barth177fe982020-05-26 11:05:19 -0500162 const std::string& propertyName, T& value)
Matt Spinlerebaae612017-04-27 14:21:48 -0500163{
164 std::string sensor;
Patrick Williamsc21d0b32020-05-13 17:55:14 -0500165 std::map<std::string, std::variant<T>> data;
Matt Spinlerebaae612017-04-27 14:21:48 -0500166 msg.read(sensor, data);
167
168 if (sensor.compare(interface) == 0)
169 {
170 auto propertyMap = data.find(propertyName);
171 if (propertyMap != data.end())
172 {
Patrick Williams1f514fa2020-05-13 11:53:33 -0500173 value = std::get<T>(propertyMap->second);
Matt Spinlerebaae612017-04-27 14:21:48 -0500174 }
175 }
176}
177
Brad Bishop771659f2017-07-30 19:52:21 -0400178void TachSensor::handleTargetChange(sdbusplus::message::message& msg)
Matt Spinlerebaae612017-04-27 14:21:48 -0500179{
Matthew Barth177fe982020-05-26 11:05:19 -0500180 readPropertyFromMessage(msg, _interface, FAN_TARGET_PROPERTY, _tachTarget);
Matt Spinlerebaae612017-04-27 14:21:48 -0500181
Matthew Barth177fe982020-05-26 11:05:19 -0500182 // Check all tach sensors on the fan against the target
Matt Spinlerebaae612017-04-27 14:21:48 -0500183 _fan.tachChanged();
184}
185
Brad Bishop771659f2017-07-30 19:52:21 -0400186void TachSensor::handleTachChange(sdbusplus::message::message& msg)
Matt Spinlerebaae612017-04-27 14:21:48 -0500187{
Matthew Barth177fe982020-05-26 11:05:19 -0500188 readPropertyFromMessage(msg, FAN_SENSOR_VALUE_INTF, FAN_VALUE_PROPERTY,
189 _tachInput);
Matt Spinlerebaae612017-04-27 14:21:48 -0500190
Matthew Barth177fe982020-05-26 11:05:19 -0500191 // Check just this sensor against the target
192 _fan.tachChanged(*this);
Matt Spinlerabf8da32017-04-27 14:08:45 -0500193}
194
Matthew Barth3800ae72018-02-19 16:08:04 -0600195void TachSensor::startTimer(TimerMode mode)
196{
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700197 if (!timerRunning() || mode != _timerMode)
Matthew Barth3800ae72018-02-19 16:08:04 -0600198 {
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700199 _timer.restartOnce(getDelay(mode));
Matthew Barth3800ae72018-02-19 16:08:04 -0600200 _timerMode = mode;
201 }
Matthew Barth3800ae72018-02-19 16:08:04 -0600202}
Matt Spinlerabf8da32017-04-27 14:08:45 -0500203
Matthew Barth3800ae72018-02-19 16:08:04 -0600204std::chrono::microseconds TachSensor::getDelay(TimerMode mode)
Matt Spinlera9406a72017-04-27 14:29:24 -0500205{
206 using namespace std::chrono;
207
Matthew Barth177fe982020-05-26 11:05:19 -0500208 switch (mode)
Matthew Barth3800ae72018-02-19 16:08:04 -0600209 {
Matthew Barth177fe982020-05-26 11:05:19 -0500210 case TimerMode::nonfunc:
211 return duration_cast<microseconds>(seconds(_timeout));
212 case TimerMode::func:
213 return duration_cast<microseconds>(seconds(_funcDelay));
214 default:
215 // Log an internal error for undefined timer mode
216 log<level::ERR>("Undefined timer mode",
217 entry("TIMER_MODE=%u", mode));
218 elog<InternalFailure>();
219 return duration_cast<microseconds>(seconds(0));
Matthew Barth3800ae72018-02-19 16:08:04 -0600220 }
Matt Spinlera9406a72017-04-27 14:29:24 -0500221}
222
Matthew Barth4d982852017-11-17 09:37:13 -0600223void TachSensor::updateInventory(bool functional)
224{
Matthew Barth177fe982020-05-26 11:05:19 -0500225 auto objectMap =
226 util::getObjMap<bool>(_invName, util::OPERATIONAL_STATUS_INTF,
227 util::FUNCTIONAL_PROPERTY, functional);
Matthew Barth4d982852017-11-17 09:37:13 -0600228 auto response = util::SDBusPlus::lookupAndCallMethod(
Matthew Barth177fe982020-05-26 11:05:19 -0500229 _bus, util::INVENTORY_PATH, util::INVENTORY_INTF, "Notify", objectMap);
Matthew Barth4d982852017-11-17 09:37:13 -0600230 if (response.is_method_error())
231 {
232 log<level::ERR>("Error in notify update of tach sensor inventory");
233 }
234}
Matt Spinlera9406a72017-04-27 14:29:24 -0500235
Matthew Barth177fe982020-05-26 11:05:19 -0500236} // namespace monitor
237} // namespace fan
238} // namespace phosphor