blob: 95d52b2614c499df89111f0305628b5618b57637 [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>
Matthew Barth7c23a042021-01-26 16:21:45 -060029#include <utility>
Matthew Barth177fe982020-05-26 11:05:19 -050030
Matt Spinlerabf8da32017-04-27 14:08:45 -050031namespace phosphor
32{
33namespace fan
34{
35namespace monitor
36{
37
Matt Spinlerebaae612017-04-27 14:21:48 -050038constexpr auto FAN_SENSOR_VALUE_INTF = "xyz.openbmc_project.Sensor.Value";
39constexpr auto FAN_TARGET_PROPERTY = "Target";
40constexpr auto FAN_VALUE_PROPERTY = "Value";
41
Matthew Barth4d982852017-11-17 09:37:13 -060042using namespace std::experimental::filesystem;
Matthew Barth177fe982020-05-26 11:05:19 -050043using InternalFailure =
44 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Matt Spinlerebaae612017-04-27 14:21:48 -050045
46/**
47 * @brief Helper function to read a property
48 *
49 * @param[in] interface - the interface the property is on
50 * @param[in] propertName - the name of the property
51 * @param[in] path - the dbus path
Matt Spinlerebaae612017-04-27 14:21:48 -050052 * @param[in] bus - the dbus object
53 * @param[out] value - filled in with the property value
54 */
Matthew Barth177fe982020-05-26 11:05:19 -050055template <typename T>
56static void
57 readProperty(const std::string& interface, const std::string& propertyName,
58 const std::string& path, sdbusplus::bus::bus& bus, T& value)
Matt Spinlerebaae612017-04-27 14:21:48 -050059{
Matt Spinlerebaae612017-04-27 14:21:48 -050060 try
61 {
Matthew Barth177fe982020-05-26 11:05:19 -050062 value =
63 util::SDBusPlus::getProperty<T>(bus, path, interface, propertyName);
Matt Spinlerebaae612017-04-27 14:21:48 -050064 }
65 catch (std::exception& e)
66 {
67 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
68 }
69}
Matt Spinlerabf8da32017-04-27 14:08:45 -050070
Matthew Barth177fe982020-05-26 11:05:19 -050071TachSensor::TachSensor(Mode mode, sdbusplus::bus::bus& bus, Fan& fan,
72 const std::string& id, bool hasTarget, size_t funcDelay,
73 const std::string& interface, double factor,
Jolie Ku69f2f482020-10-21 09:59:43 +080074 int64_t offset, size_t method, size_t threshold,
75 size_t timeout, const std::optional<size_t>& errorDelay,
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070076 const sdeventplus::Event& event) :
Matt Spinlerabf8da32017-04-27 14:08:45 -050077 _bus(bus),
Matthew Barth177fe982020-05-26 11:05:19 -050078 _fan(fan), _name(FAN_SENSOR_PATH + id), _invName(path(fan.getName()) / id),
79 _hasTarget(hasTarget), _funcDelay(funcDelay), _interface(interface),
Jolie Ku69f2f482020-10-21 09:59:43 +080080 _factor(factor), _offset(offset), _method(method), _threshold(threshold),
81 _timeout(timeout), _timerMode(TimerMode::func),
82 _timer(event, std::bind(&Fan::updateState, &fan, std::ref(*this))),
Matt Spinlerf13b42e2020-10-26 15:29:49 -050083 _errorDelay(errorDelay)
Matt Spinlerabf8da32017-04-27 14:08:45 -050084{
Matthew Barthd199dcd2017-11-20 10:36:41 -060085 // Start from a known state of functional
Matthew Barth7f7df312018-01-15 16:27:50 -060086 setFunctional(true);
Brad Bishopedaeb312017-07-30 19:38:20 -040087
Matthew Barth0a9fe162018-01-26 12:53:15 -060088 // Load in current Target and Input values when entering monitor mode
Matt Spinlerb0412d02020-10-12 16:53:52 -050089#ifndef MONITOR_USE_JSON
Matthew Barth0a9fe162018-01-26 12:53:15 -060090 if (mode != Mode::init)
Brad Bishopedaeb312017-07-30 19:38:20 -040091 {
Matt Spinlerb0412d02020-10-12 16:53:52 -050092#endif
Matthew Barth0a9fe162018-01-26 12:53:15 -060093 try
94 {
95 // Use getProperty directly to allow a missing sensor object
96 // to abort construction.
97 _tachInput = util::SDBusPlus::getProperty<decltype(_tachInput)>(
Matthew Barth177fe982020-05-26 11:05:19 -050098 _bus, _name, FAN_SENSOR_VALUE_INTF, FAN_VALUE_PROPERTY);
Matthew Barth0a9fe162018-01-26 12:53:15 -060099 }
100 catch (std::exception& e)
101 {
Jolie Ku4c3c24f2020-09-09 11:01:46 +0800102 log<level::ERR>(
103 fmt::format("Failed to retrieve tach sensor {}", _name)
104 .c_str());
Jolie Ku5d564a92020-10-23 09:04:28 +0800105 // Mark tach sensor as nonfunctional
Jolie Ku4c3c24f2020-09-09 11:01:46 +0800106 setFunctional(false);
Matthew Barth0a9fe162018-01-26 12:53:15 -0600107 throw InvalidSensorError();
108 }
109
110 if (_hasTarget)
111 {
Matthew Barth177fe982020-05-26 11:05:19 -0500112 readProperty(_interface, FAN_TARGET_PROPERTY, _name, _bus,
Matthew Barth0a9fe162018-01-26 12:53:15 -0600113 _tachTarget);
114 }
115
116 auto match = getMatchString(FAN_SENSOR_VALUE_INTF);
117
118 tachSignal = std::make_unique<sdbusplus::server::match::match>(
Matthew Barth177fe982020-05-26 11:05:19 -0500119 _bus, match.c_str(),
120 [this](auto& msg) { this->handleTachChange(msg); });
Matthew Barth0a9fe162018-01-26 12:53:15 -0600121
122 if (_hasTarget)
123 {
Lei YU80f271b2018-01-31 15:24:46 +0800124 match = getMatchString(_interface);
Matthew Barth0a9fe162018-01-26 12:53:15 -0600125
126 targetSignal = std::make_unique<sdbusplus::server::match::match>(
Matthew Barth177fe982020-05-26 11:05:19 -0500127 _bus, match.c_str(),
128 [this](auto& msg) { this->handleTargetChange(msg); });
Matthew Barth0a9fe162018-01-26 12:53:15 -0600129 }
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500130
131 if (_errorDelay)
132 {
133 _errorTimer = std::make_unique<
134 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
135 event, std::bind(&Fan::sensorErrorTimerExpired, &fan,
136 std::ref(*this)));
137 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500138#ifndef MONITOR_USE_JSON
Brad Bishopedaeb312017-07-30 19:38:20 -0400139 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500140#endif
Matt Spinlerebaae612017-04-27 14:21:48 -0500141}
142
Matt Spinlerebaae612017-04-27 14:21:48 -0500143std::string TachSensor::getMatchString(const std::string& interface)
144{
Matthew Barth177fe982020-05-26 11:05:19 -0500145 return sdbusplus::bus::match::rules::propertiesChanged(_name, interface);
Matt Spinlerebaae612017-04-27 14:21:48 -0500146}
147
Matthew Barthf552ea52018-01-15 16:22:04 -0600148uint64_t TachSensor::getTarget() const
149{
150 if (!_hasTarget)
151 {
152 return _fan.findTargetSpeed();
153 }
154 return _tachTarget;
155}
156
Matthew Barth7c23a042021-01-26 16:21:45 -0600157std::pair<uint64_t, uint64_t> TachSensor::getRange(const size_t deviation) const
158{
159 // Determine min/max range applying the deviation
160 uint64_t min = getTarget() * (100 - deviation) / 100;
161 uint64_t max = getTarget() * (100 + deviation) / 100;
162
163 // Adjust the min/max range by applying the factor & offset
164 min = min * _factor + _offset;
165 max = max * _factor + _offset;
166
167 return std::make_pair(min, max);
168}
169
Matthew Barthfcb0dbc2021-02-10 14:23:39 -0600170void TachSensor::processState()
171{
172 _fan.process(*this);
173}
174
175void TachSensor::resetMethod()
176{
177 switch (_method)
178 {
179 case MethodMode::timebased:
180 if (timerRunning())
181 {
182 stopTimer();
183 }
184 break;
185 case MethodMode::count:
186 if (_functional)
187 {
188 _counter = 0;
189 }
190 else
191 {
192 _counter = _threshold;
193 }
194 break;
195 }
196}
197
Matthew Barthd199dcd2017-11-20 10:36:41 -0600198void TachSensor::setFunctional(bool functional)
199{
200 _functional = functional;
201 updateInventory(_functional);
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500202
203 if (!_errorTimer)
204 {
205 return;
206 }
207
208 if (!_functional)
209 {
210 if (_fan.present())
211 {
212 _errorTimer->restartOnce(std::chrono::seconds(*_errorDelay));
213 }
214 }
215 else if (_errorTimer->isEnabled())
216 {
217 _errorTimer->setEnabled(false);
218 }
Matthew Barthd199dcd2017-11-20 10:36:41 -0600219}
Matt Spinlerebaae612017-04-27 14:21:48 -0500220
Brad Bishop771659f2017-07-30 19:52:21 -0400221void TachSensor::handleTargetChange(sdbusplus::message::message& msg)
Matt Spinlerebaae612017-04-27 14:21:48 -0500222{
Matthew Barth177fe982020-05-26 11:05:19 -0500223 readPropertyFromMessage(msg, _interface, FAN_TARGET_PROPERTY, _tachTarget);
Matt Spinlerebaae612017-04-27 14:21:48 -0500224
Matthew Barth177fe982020-05-26 11:05:19 -0500225 // Check all tach sensors on the fan against the target
Matt Spinlerebaae612017-04-27 14:21:48 -0500226 _fan.tachChanged();
227}
228
Brad Bishop771659f2017-07-30 19:52:21 -0400229void TachSensor::handleTachChange(sdbusplus::message::message& msg)
Matt Spinlerebaae612017-04-27 14:21:48 -0500230{
Matthew Barth177fe982020-05-26 11:05:19 -0500231 readPropertyFromMessage(msg, FAN_SENSOR_VALUE_INTF, FAN_VALUE_PROPERTY,
232 _tachInput);
Matt Spinlerebaae612017-04-27 14:21:48 -0500233
Matthew Barth177fe982020-05-26 11:05:19 -0500234 // Check just this sensor against the target
235 _fan.tachChanged(*this);
Matt Spinlerabf8da32017-04-27 14:08:45 -0500236}
237
Matthew Barth3800ae72018-02-19 16:08:04 -0600238void TachSensor::startTimer(TimerMode mode)
239{
Matthew Barth0d0e3552021-01-27 12:31:28 -0600240 using namespace std::chrono;
241
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700242 if (!timerRunning() || mode != _timerMode)
Matthew Barth3800ae72018-02-19 16:08:04 -0600243 {
Matthew Barth0d0e3552021-01-27 12:31:28 -0600244 log<level::INFO>(
245 fmt::format("Start timer({}) on tach sensor {}. [delay = {}s]",
246 mode, _name,
247 duration_cast<seconds>(getDelay(mode)).count())
248 .c_str());
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700249 _timer.restartOnce(getDelay(mode));
Matthew Barth3800ae72018-02-19 16:08:04 -0600250 _timerMode = mode;
251 }
Matthew Barth3800ae72018-02-19 16:08:04 -0600252}
Matt Spinlerabf8da32017-04-27 14:08:45 -0500253
Matthew Barth3800ae72018-02-19 16:08:04 -0600254std::chrono::microseconds TachSensor::getDelay(TimerMode mode)
Matt Spinlera9406a72017-04-27 14:29:24 -0500255{
256 using namespace std::chrono;
257
Matthew Barth177fe982020-05-26 11:05:19 -0500258 switch (mode)
Matthew Barth3800ae72018-02-19 16:08:04 -0600259 {
Matthew Barth177fe982020-05-26 11:05:19 -0500260 case TimerMode::nonfunc:
261 return duration_cast<microseconds>(seconds(_timeout));
262 case TimerMode::func:
263 return duration_cast<microseconds>(seconds(_funcDelay));
264 default:
265 // Log an internal error for undefined timer mode
266 log<level::ERR>("Undefined timer mode",
267 entry("TIMER_MODE=%u", mode));
268 elog<InternalFailure>();
269 return duration_cast<microseconds>(seconds(0));
Matthew Barth3800ae72018-02-19 16:08:04 -0600270 }
Matt Spinlera9406a72017-04-27 14:29:24 -0500271}
272
Jolie Ku69f2f482020-10-21 09:59:43 +0800273void TachSensor::setCounter(bool count)
274{
275 if (count)
276 {
277 if (_counter < _threshold)
278 {
279 ++_counter;
280 }
281 }
282 else
283 {
284 if (_counter > 0)
285 {
286 --_counter;
287 }
288 }
289}
290
Matthew Barth4d982852017-11-17 09:37:13 -0600291void TachSensor::updateInventory(bool functional)
292{
Matthew Barth177fe982020-05-26 11:05:19 -0500293 auto objectMap =
294 util::getObjMap<bool>(_invName, util::OPERATIONAL_STATUS_INTF,
295 util::FUNCTIONAL_PROPERTY, functional);
Matthew Barth4d982852017-11-17 09:37:13 -0600296 auto response = util::SDBusPlus::lookupAndCallMethod(
Matthew Barth177fe982020-05-26 11:05:19 -0500297 _bus, util::INVENTORY_PATH, util::INVENTORY_INTF, "Notify", objectMap);
Matthew Barth4d982852017-11-17 09:37:13 -0600298 if (response.is_method_error())
299 {
300 log<level::ERR>("Error in notify update of tach sensor inventory");
301 }
302}
Matt Spinlera9406a72017-04-27 14:29:24 -0500303
Matthew Barth177fe982020-05-26 11:05:19 -0500304} // namespace monitor
305} // namespace fan
306} // namespace phosphor