blob: 05895932f9aa96eafc5615330d1301f86fa240c9 [file] [log] [blame]
Matt Spinlerabf8da32017-04-27 14:08:45 -05001/**
Mike Capps7b34ee02022-05-04 14:16:12 -04002 * Copyright © 2022 IBM Corporation
Matt Spinlerabf8da32017-04-27 14:08:45 -05003 *
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 */
Matt Spinlerabf8da32017-04-27 14:08:45 -050016#include "fan.hpp"
Matthew Barth177fe982020-05-26 11:05:19 -050017
Matt Spinlerae1f8ef2020-10-14 16:15:51 -050018#include "logging.hpp"
Matthew Barth177fe982020-05-26 11:05:19 -050019#include "sdbusplus.hpp"
Matt Spinlerb0412d02020-10-12 16:53:52 -050020#include "system.hpp"
Matt Spinlerabf8da32017-04-27 14:08:45 -050021#include "types.hpp"
Matt Spinlerb1e18512017-04-27 14:42:33 -050022#include "utility.hpp"
Matthew Barth177fe982020-05-26 11:05:19 -050023
24#include <phosphor-logging/log.hpp>
25
Patrick Williamsfbf47032023-07-17 12:27:34 -050026#include <format>
27
Matt Spinlerabf8da32017-04-27 14:08:45 -050028namespace phosphor
29{
30namespace fan
31{
32namespace monitor
33{
34
35using namespace phosphor::logging;
Matt Spinlerb0412d02020-10-12 16:53:52 -050036using namespace sdbusplus::bus::match;
Matt Spinlerabf8da32017-04-27 14:08:45 -050037
Patrick Williamscb356d42022-07-22 19:26:53 -050038Fan::Fan(Mode mode, sdbusplus::bus_t& bus, const sdeventplus::Event& event,
Matt Spinlerb0412d02020-10-12 16:53:52 -050039 std::unique_ptr<trust::Manager>& trust, const FanDefinition& def,
40 System& system) :
Matt Spinlerabf8da32017-04-27 14:08:45 -050041 _bus(bus),
Matt Spinler18fb12b2023-05-09 11:17:42 -050042 _name(def.name), _deviation(def.deviation),
Matt Spinlerf724c162023-05-10 11:14:37 -050043 _upperDeviation(def.upperDeviation),
Matt Spinler18fb12b2023-05-09 11:17:42 -050044 _numSensorFailsForNonFunc(def.numSensorFailsForNonfunc),
Matt Spinlerb0412d02020-10-12 16:53:52 -050045 _trustManager(trust),
46#ifdef MONITOR_USE_JSON
Matt Spinler18fb12b2023-05-09 11:17:42 -050047 _monitorDelay(def.monitorStartDelay),
Matt Spinlerb0412d02020-10-12 16:53:52 -050048 _monitorTimer(event, std::bind(std::mem_fn(&Fan::startMonitor), this)),
49#endif
Matt Spinlerb63aa092020-10-14 09:45:11 -050050 _system(system),
51 _presenceMatch(bus,
52 rules::propertiesChanged(util::INVENTORY_PATH + _name,
53 util::INV_ITEM_IFACE),
54 std::bind(std::mem_fn(&Fan::presenceChanged), this,
Matt Spinler27f6b682020-10-27 08:43:37 -050055 std::placeholders::_1)),
Matt Spinler7d135642021-02-04 12:44:17 -060056 _presenceIfaceAddedMatch(
57 bus,
58 rules::interfacesAdded() +
59 rules::argNpath(0, util::INVENTORY_PATH + _name),
60 std::bind(std::mem_fn(&Fan::presenceIfaceAdded), this,
61 std::placeholders::_1)),
Matt Spinler18fb12b2023-05-09 11:17:42 -050062 _fanMissingErrorDelay(def.fanMissingErrDelay),
63 _setFuncOnPresent(def.funcOnPresent)
Matt Spinlerabf8da32017-04-27 14:08:45 -050064{
Matthew Barth0a9fe162018-01-26 12:53:15 -060065 // Setup tach sensors for monitoring
Matt Spinler18fb12b2023-05-09 11:17:42 -050066 for (const auto& s : def.sensorList)
Matthew Barth0a9fe162018-01-26 12:53:15 -060067 {
Matt Spinler4283c5d2021-03-01 15:56:00 -060068 _sensors.emplace_back(std::make_shared<TachSensor>(
Matt Spinler18fb12b2023-05-09 11:17:42 -050069 mode, bus, *this, s.name, s.hasTarget, def.funcDelay,
70 s.targetInterface, s.targetPath, s.factor, s.offset, def.method,
71 s.threshold, s.ignoreAboveMax, def.timeout,
72 def.nonfuncRotorErrDelay, def.countInterval, event));
Matthew Barth0a9fe162018-01-26 12:53:15 -060073
Matt Spinler4283c5d2021-03-01 15:56:00 -060074 _trustManager->registerSensor(_sensors.back());
Matthew Barth0a9fe162018-01-26 12:53:15 -060075 }
76
Mike Cappsce6820a2021-05-26 10:40:19 -040077 bool functionalState =
78 (_numSensorFailsForNonFunc == 0) ||
79 (countNonFunctionalSensors() < _numSensorFailsForNonFunc);
80
Mike Capps9ff48772021-07-19 14:49:43 -040081 if (updateInventory(functionalState) && !functionalState)
82 {
83 // the inventory update threw an exception, possibly because D-Bus
84 // wasn't ready. Try to update sensors back to functional to avoid a
85 // false-alarm. They will be updated again from subscribing to the
86 // properties-changed event
87
88 for (auto& sensor : _sensors)
89 sensor->setFunctional(true);
90 }
Mike Cappsce6820a2021-05-26 10:40:19 -040091
Matt Spinlerb0412d02020-10-12 16:53:52 -050092#ifndef MONITOR_USE_JSON
Matthew Barth0a9fe162018-01-26 12:53:15 -060093 // Check current tach state when entering monitor mode
Matthew Barth6ad28432017-08-22 11:18:19 -050094 if (mode != Mode::init)
95 {
Matt Spinlerb0412d02020-10-12 16:53:52 -050096 _monitorReady = true;
97
Matthew Barth177fe982020-05-26 11:05:19 -050098 // The TachSensors will now have already read the input
99 // and target values, so check them.
Matthew Barth6ad28432017-08-22 11:18:19 -0500100 tachChanged();
101 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500102#else
Matt Spinler7d135642021-02-04 12:44:17 -0600103 if (_system.isPowerOn())
104 {
105 _monitorTimer.restartOnce(std::chrono::seconds(_monitorDelay));
106 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500107#endif
Matt Spinlerb63aa092020-10-14 09:45:11 -0500108
Matt Spinler27f6b682020-10-27 08:43:37 -0500109 if (_fanMissingErrorDelay)
110 {
111 _fanMissingErrorTimer = std::make_unique<
112 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
113 event, std::bind(&System::fanMissingErrorTimerExpired, &system,
114 std::ref(*this)));
Matt Spinler7d135642021-02-04 12:44:17 -0600115 }
Matt Spinler27f6b682020-10-27 08:43:37 -0500116
Matt Spinler7d135642021-02-04 12:44:17 -0600117 try
118 {
119 _present = util::SDBusPlus::getProperty<bool>(
120 util::INVENTORY_PATH + _name, util::INV_ITEM_IFACE, "Present");
121
122 if (!_present)
Matt Spinler27f6b682020-10-27 08:43:37 -0500123 {
Matt Spinlerac372972021-01-25 15:11:22 -0600124 getLogger().log(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500125 std::format("On startup, fan {} is missing", _name));
Matt Spinler7d135642021-02-04 12:44:17 -0600126 if (_system.isPowerOn() && _fanMissingErrorTimer)
127 {
128 _fanMissingErrorTimer->restartOnce(
129 std::chrono::seconds{*_fanMissingErrorDelay});
130 }
131 }
132 }
133 catch (const util::DBusServiceError& e)
134 {
135 // This could happen on the first BMC boot if the presence
136 // detect app hasn't started yet and there isn't an inventory
137 // cache yet.
138 }
139}
140
Patrick Williamscb356d42022-07-22 19:26:53 -0500141void Fan::presenceIfaceAdded(sdbusplus::message_t& msg)
Matt Spinler7d135642021-02-04 12:44:17 -0600142{
143 sdbusplus::message::object_path path;
144 std::map<std::string, std::map<std::string, std::variant<bool>>> interfaces;
145
146 msg.read(path, interfaces);
147
148 auto properties = interfaces.find(util::INV_ITEM_IFACE);
149 if (properties == interfaces.end())
150 {
151 return;
152 }
153
154 auto property = properties->second.find("Present");
155 if (property == properties->second.end())
156 {
157 return;
158 }
159
160 _present = std::get<bool>(property->second);
161
162 if (!_present)
163 {
Patrick Williamsfbf47032023-07-17 12:27:34 -0500164 getLogger().log(std::format(
Matt Spinler7d135642021-02-04 12:44:17 -0600165 "New fan {} interface added and fan is not present", _name));
166 if (_system.isPowerOn() && _fanMissingErrorTimer)
167 {
Matt Spinler27f6b682020-10-27 08:43:37 -0500168 _fanMissingErrorTimer->restartOnce(
169 std::chrono::seconds{*_fanMissingErrorDelay});
170 }
171 }
Matt Spinler7d135642021-02-04 12:44:17 -0600172
173 _system.fanStatusChange(*this);
Matt Spinlerb0412d02020-10-12 16:53:52 -0500174}
175
176void Fan::startMonitor()
177{
178 _monitorReady = true;
179
Matt Spinler4283c5d2021-03-01 15:56:00 -0600180 std::for_each(_sensors.begin(), _sensors.end(), [this](auto& sensor) {
Matt Spinler3494a572023-11-28 12:45:32 -0600181 try
Matt Spinler4283c5d2021-03-01 15:56:00 -0600182 {
Matt Spinler3494a572023-11-28 12:45:32 -0600183 // Force a getProperty call to check if the tach sensor is
184 // on D-Bus. If it isn't, now set it to nonfunctional.
185 // This isn't done earlier so that code watching for
186 // nonfunctional tach sensors doesn't take actions before
187 // those sensors show up on D-Bus.
188 sensor->updateTachAndTarget();
189 tachChanged(*sensor);
190 }
191 catch (const util::DBusServiceError& e)
192 {
193 // The tach property still isn't on D-Bus. Ensure
194 // sensor is nonfunctional, but skip creating an
195 // error for it since it isn't a fan problem.
196 getLogger().log(std::format(
197 "Monitoring starting but {} sensor value not on D-Bus",
198 sensor->name()));
Matt Spinler4283c5d2021-03-01 15:56:00 -0600199
Matt Spinler3494a572023-11-28 12:45:32 -0600200 sensor->setFunctional(false, true);
Matt Spinler4283c5d2021-03-01 15:56:00 -0600201
Matt Spinler3494a572023-11-28 12:45:32 -0600202 if (_numSensorFailsForNonFunc)
203 {
204 if (_functional &&
205 (countNonFunctionalSensors() >= _numSensorFailsForNonFunc))
Matt Spinler4283c5d2021-03-01 15:56:00 -0600206 {
Matt Spinler3494a572023-11-28 12:45:32 -0600207 updateInventory(false);
Matt Spinler4283c5d2021-03-01 15:56:00 -0600208 }
Matt Spinler4283c5d2021-03-01 15:56:00 -0600209 }
Matt Spinler3494a572023-11-28 12:45:32 -0600210
211 // At this point, don't start any power off actions due
212 // to missing sensors. Let something else handle that
213 // policy.
214 _system.fanStatusChange(*this, true);
Matt Spinler4283c5d2021-03-01 15:56:00 -0600215 }
216 });
Matt Spinlerabf8da32017-04-27 14:08:45 -0500217}
218
Matt Spinlerebaae612017-04-27 14:21:48 -0500219void Fan::tachChanged()
220{
Matt Spinlerb0412d02020-10-12 16:53:52 -0500221 if (_monitorReady)
Matt Spinlerebaae612017-04-27 14:21:48 -0500222 {
Matt Spinlerb0412d02020-10-12 16:53:52 -0500223 for (auto& s : _sensors)
224 {
225 tachChanged(*s);
226 }
Matt Spinlerebaae612017-04-27 14:21:48 -0500227 }
228}
229
Matt Spinlerebaae612017-04-27 14:21:48 -0500230void Fan::tachChanged(TachSensor& sensor)
231{
Matt Spinler7d135642021-02-04 12:44:17 -0600232 if (!_system.isPowerOn() || !_monitorReady)
233 {
234 return;
235 }
236
Matt Spinlerc39e8592017-09-28 13:13:08 -0500237 if (_trustManager->active())
238 {
239 if (!_trustManager->checkTrust(sensor))
240 {
241 return;
242 }
243 }
244
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600245 // If the error checking method is 'count', if a tach change leads
246 // to an out of range sensor the count timer will take over in calling
247 // process() until the sensor is healthy again.
248 if (!sensor.countTimerRunning())
Matt Spinler623635c2021-03-29 13:13:59 -0500249 {
250 process(sensor);
251 }
252}
253
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600254void Fan::countTimerExpired(TachSensor& sensor)
Matt Spinler623635c2021-03-29 13:13:59 -0500255{
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600256 if (_trustManager->active() && !_trustManager->checkTrust(sensor))
Matt Spinler623635c2021-03-29 13:13:59 -0500257 {
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600258 return;
Matt Spinler623635c2021-03-29 13:13:59 -0500259 }
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600260 process(sensor);
Matthew Barthfcb0dbc2021-02-10 14:23:39 -0600261}
262
263void Fan::process(TachSensor& sensor)
264{
Matthew Barth177fe982020-05-26 11:05:19 -0500265 // If this sensor is out of range at this moment, start
266 // its timer, at the end of which the inventory
267 // for the fan may get updated to not functional.
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500268
Matthew Barth177fe982020-05-26 11:05:19 -0500269 // If this sensor is OK, put everything back into a good state.
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500270
271 if (outOfRange(sensor))
272 {
Matthew Barthe11cbc62018-02-20 12:11:07 -0600273 if (sensor.functional())
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500274 {
Jolie Ku69f2f482020-10-21 09:59:43 +0800275 switch (sensor.getMethod())
276 {
277 case MethodMode::timebased:
278 // Start nonfunctional timer if not already running
279 sensor.startTimer(TimerMode::nonfunc);
280 break;
281 case MethodMode::count:
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600282
283 if (!sensor.countTimerRunning())
284 {
285 sensor.startCountTimer();
286 }
Jolie Ku69f2f482020-10-21 09:59:43 +0800287 sensor.setCounter(true);
288 if (sensor.getCounter() >= sensor.getThreshold())
289 {
290 updateState(sensor);
291 }
292 break;
293 }
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500294 }
295 }
296 else
297 {
Jolie Ku69f2f482020-10-21 09:59:43 +0800298 switch (sensor.getMethod())
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500299 {
Jolie Ku69f2f482020-10-21 09:59:43 +0800300 case MethodMode::timebased:
301 if (sensor.functional())
302 {
Matthew Barth11b5d8f2021-01-28 14:04:09 -0600303 if (sensor.timerRunning())
304 {
305 sensor.stopTimer();
306 }
Jolie Ku69f2f482020-10-21 09:59:43 +0800307 }
308 else
309 {
310 // Start functional timer if not already running
311 sensor.startTimer(TimerMode::func);
312 }
313 break;
314 case MethodMode::count:
315 sensor.setCounter(false);
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600316 if (sensor.getCounter() == 0)
Jolie Ku69f2f482020-10-21 09:59:43 +0800317 {
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600318 if (!sensor.functional())
319 {
320 updateState(sensor);
321 }
322
323 sensor.stopCountTimer();
Jolie Ku69f2f482020-10-21 09:59:43 +0800324 }
325 break;
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500326 }
327 }
Matt Spinlerebaae612017-04-27 14:21:48 -0500328}
329
Matthew Barthf552ea52018-01-15 16:22:04 -0600330uint64_t Fan::findTargetSpeed()
Matt Spinlerabf8da32017-04-27 14:08:45 -0500331{
332 uint64_t target = 0;
Matthew Barth177fe982020-05-26 11:05:19 -0500333 // The sensor doesn't support a target,
334 // so get it from another sensor.
Matthew Barthf552ea52018-01-15 16:22:04 -0600335 auto s = std::find_if(_sensors.begin(), _sensors.end(),
Matthew Barth177fe982020-05-26 11:05:19 -0500336 [](const auto& s) { return s->hasTarget(); });
Matt Spinlerabf8da32017-04-27 14:08:45 -0500337
Matthew Barthf552ea52018-01-15 16:22:04 -0600338 if (s != _sensors.end())
Matt Spinlerabf8da32017-04-27 14:08:45 -0500339 {
Matthew Barthf552ea52018-01-15 16:22:04 -0600340 target = (*s)->getTarget();
Matt Spinlerabf8da32017-04-27 14:08:45 -0500341 }
342
343 return target;
344}
345
Mike Cappsce6820a2021-05-26 10:40:19 -0400346size_t Fan::countNonFunctionalSensors() const
Matt Spinlerabf8da32017-04-27 14:08:45 -0500347{
Matthew Barth7c23a042021-01-26 16:21:45 -0600348 return std::count_if(_sensors.begin(), _sensors.end(),
349 [](const auto& s) { return !s->functional(); });
Matt Spinlerabf8da32017-04-27 14:08:45 -0500350}
351
Matt Spinlerabf8da32017-04-27 14:08:45 -0500352bool Fan::outOfRange(const TachSensor& sensor)
353{
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400354 if (!sensor.hasOwner())
Matt Spinlerabf8da32017-04-27 14:08:45 -0500355 {
356 return true;
357 }
358
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400359 auto actual = static_cast<uint64_t>(sensor.getInput());
Matt Spinlerf724c162023-05-10 11:14:37 -0500360 auto range = sensor.getRange(_deviation, _upperDeviation);
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400361
Matthew Barth8a8aa442021-11-19 14:13:13 -0600362 return ((actual < range.first) ||
363 (range.second && actual > range.second.value()));
Matt Spinlerabf8da32017-04-27 14:08:45 -0500364}
365
Jolie Ku69f2f482020-10-21 09:59:43 +0800366void Fan::updateState(TachSensor& sensor)
Matt Spinlera9406a72017-04-27 14:29:24 -0500367{
Matt Spinler7d135642021-02-04 12:44:17 -0600368 if (!_system.isPowerOn())
369 {
370 return;
371 }
372
Matt Spinlerf724c162023-05-10 11:14:37 -0500373 auto range = sensor.getRange(_deviation, _upperDeviation);
Matthew Barth8a8aa442021-11-19 14:13:13 -0600374 std::string rangeMax = "NoMax";
375 if (range.second)
376 {
377 rangeMax = std::to_string(range.second.value());
378 }
379
Matt Spinlerae01b5f2022-07-06 16:49:04 -0500380 // Skip starting the error timer if the sensor
381 // isn't on D-Bus as this isn't a fan hardware problem.
382 sensor.setFunctional(!sensor.functional(), !sensor.hasOwner());
383
Patrick Williamsfbf47032023-07-17 12:27:34 -0500384 getLogger().log(std::format(
Matt Spinlerae01b5f2022-07-06 16:49:04 -0500385 "Setting tach sensor {} functional state to {}. "
Matt Spinler466bd222023-01-25 16:15:52 -0600386 "[target = {}, actual = {}, allowed range = ({} - {}) "
Matt Spinlerae01b5f2022-07-06 16:49:04 -0500387 "owned = {}]",
388 sensor.name(), sensor.functional(), sensor.getTarget(),
389 sensor.getInput(), range.first, rangeMax, sensor.hasOwner()));
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500390
391 // A zero value for _numSensorFailsForNonFunc means we aren't dealing
392 // with fan FRU functional status, only sensor functional status.
393 if (_numSensorFailsForNonFunc)
Matthew Barthe11cbc62018-02-20 12:11:07 -0600394 {
Matthew Barth7c23a042021-01-26 16:21:45 -0600395 auto numNonFuncSensors = countNonFunctionalSensors();
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500396 // If the fan was nonfunctional and enough sensors are now OK,
Matthew Barthcceffdd2021-05-20 12:17:21 -0500397 // the fan can be set to functional as long as `set_func_on_present` was
398 // not set
399 if (!_setFuncOnPresent && !_functional &&
400 !(numNonFuncSensors >= _numSensorFailsForNonFunc))
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500401 {
Patrick Williamsfbf47032023-07-17 12:27:34 -0500402 getLogger().log(std::format("Setting fan {} to functional, number "
Matthew Barth7c23a042021-01-26 16:21:45 -0600403 "of nonfunctional sensors = {}",
404 _name, numNonFuncSensors));
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500405 updateInventory(true);
406 }
Matt Spinlera9406a72017-04-27 14:29:24 -0500407
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500408 // If the fan is currently functional, but too many
409 // contained sensors are now nonfunctional, update
Matthew Barth7c23a042021-01-26 16:21:45 -0600410 // the fan to nonfunctional.
411 if (_functional && (numNonFuncSensors >= _numSensorFailsForNonFunc))
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500412 {
Patrick Williamsfbf47032023-07-17 12:27:34 -0500413 getLogger().log(std::format("Setting fan {} to nonfunctional, "
Matthew Barth7c23a042021-01-26 16:21:45 -0600414 "number of nonfunctional sensors = {}",
415 _name, numNonFuncSensors));
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500416 updateInventory(false);
417 }
Matt Spinlerb1e18512017-04-27 14:42:33 -0500418 }
Matt Spinlerb63aa092020-10-14 09:45:11 -0500419
Matt Spinlerae01b5f2022-07-06 16:49:04 -0500420 // Skip the power off rule checks if the sensor isn't
421 // on D-Bus so a running system isn't shutdown.
422 _system.fanStatusChange(*this, !sensor.hasOwner());
Matt Spinlera9406a72017-04-27 14:29:24 -0500423}
424
Mike Capps9ff48772021-07-19 14:49:43 -0400425bool Fan::updateInventory(bool functional)
Matt Spinlerb1e18512017-04-27 14:42:33 -0500426{
Mike Capps9ff48772021-07-19 14:49:43 -0400427 bool dbusError = false;
428
429 try
Matt Spinlerb1e18512017-04-27 14:42:33 -0500430 {
Mike Capps9ff48772021-07-19 14:49:43 -0400431 auto objectMap =
432 util::getObjMap<bool>(_name, util::OPERATIONAL_STATUS_INTF,
433 util::FUNCTIONAL_PROPERTY, functional);
434
Mike Capps8af8a622022-02-04 16:13:33 -0500435 auto response = util::SDBusPlus::callMethod(
436 _bus, util::INVENTORY_SVC, util::INVENTORY_PATH,
437 util::INVENTORY_INTF, "Notify", objectMap);
Mike Capps9ff48772021-07-19 14:49:43 -0400438
439 if (response.is_method_error())
440 {
441 log<level::ERR>("Error in Notify call to update inventory");
442
443 dbusError = true;
444 }
445 }
446 catch (const util::DBusError& e)
447 {
448 dbusError = true;
449
450 getLogger().log(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500451 std::format("D-Bus Exception reading/updating inventory : {}",
Mike Capps9ff48772021-07-19 14:49:43 -0400452 e.what()),
453 Logger::error);
Matt Spinlerb1e18512017-04-27 14:42:33 -0500454 }
455
Matthew Barth177fe982020-05-26 11:05:19 -0500456 // This will always track the current state of the inventory.
Matt Spinlerb1e18512017-04-27 14:42:33 -0500457 _functional = functional;
Mike Capps9ff48772021-07-19 14:49:43 -0400458
459 return dbusError;
Matt Spinlerb1e18512017-04-27 14:42:33 -0500460}
461
Patrick Williamscb356d42022-07-22 19:26:53 -0500462void Fan::presenceChanged(sdbusplus::message_t& msg)
Matt Spinlerb63aa092020-10-14 09:45:11 -0500463{
464 std::string interface;
465 std::map<std::string, std::variant<bool>> properties;
466
467 msg.read(interface, properties);
468
469 auto presentProp = properties.find("Present");
470 if (presentProp != properties.end())
471 {
472 _present = std::get<bool>(presentProp->second);
473
Matt Spinler27f6b682020-10-27 08:43:37 -0500474 getLogger().log(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500475 std::format("Fan {} presence state change to {}", _name, _present));
Matt Spinler27f6b682020-10-27 08:43:37 -0500476
Matt Spinlera3584bd2021-03-29 15:48:30 -0500477 if (_present && _setFuncOnPresent)
478 {
479 updateInventory(true);
480 std::for_each(_sensors.begin(), _sensors.end(), [](auto& sensor) {
481 sensor->setFunctional(true);
482 sensor->resetMethod();
483 });
484 }
485
Matthew Barth43b4cde2022-02-15 16:30:11 -0600486 _system.fanStatusChange(*this);
487
Matt Spinler27f6b682020-10-27 08:43:37 -0500488 if (_fanMissingErrorDelay)
489 {
Matt Spinler7d135642021-02-04 12:44:17 -0600490 if (!_present && _system.isPowerOn())
Matt Spinler27f6b682020-10-27 08:43:37 -0500491 {
492 _fanMissingErrorTimer->restartOnce(
493 std::chrono::seconds{*_fanMissingErrorDelay});
494 }
Matt Spinler7d135642021-02-04 12:44:17 -0600495 else if (_present && _fanMissingErrorTimer->isEnabled())
Matt Spinler27f6b682020-10-27 08:43:37 -0500496 {
497 _fanMissingErrorTimer->setEnabled(false);
498 }
499 }
Matt Spinlerb63aa092020-10-14 09:45:11 -0500500 }
501}
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500502
503void Fan::sensorErrorTimerExpired(const TachSensor& sensor)
504{
Matt Spinler7d135642021-02-04 12:44:17 -0600505 if (_present && _system.isPowerOn())
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500506 {
507 _system.sensorErrorTimerExpired(*this, sensor);
508 }
509}
510
Mike Capps808d7fe2022-06-13 10:12:16 -0400511void Fan::powerStateChanged([[maybe_unused]] bool powerStateOn)
Matt Spinler7d135642021-02-04 12:44:17 -0600512{
513#ifdef MONITOR_USE_JSON
514 if (powerStateOn)
515 {
Matt Spinler7d135642021-02-04 12:44:17 -0600516 _monitorTimer.restartOnce(std::chrono::seconds(_monitorDelay));
517
Matt Spinlerbb449c12021-06-14 11:45:28 -0600518 _numSensorsOnDBusAtPowerOn = 0;
519
520 std::for_each(_sensors.begin(), _sensors.end(), [this](auto& sensor) {
521 try
522 {
523 // Force a getProperty call. If sensor is on D-Bus,
524 // then make sure it's functional.
525 sensor->updateTachAndTarget();
526
527 _numSensorsOnDBusAtPowerOn++;
528
529 if (_present)
530 {
531 // If not functional, set it back to functional.
532 if (!sensor->functional())
533 {
534 sensor->setFunctional(true);
535 _system.fanStatusChange(*this, true);
536 }
537
538 // Set the counters back to zero
539 if (sensor->getMethod() == MethodMode::count)
540 {
541 sensor->resetMethod();
542 }
543 }
544 }
545 catch (const util::DBusError& e)
546 {
547 // Properties still aren't on D-Bus. Let startMonitor()
548 // deal with it, or maybe System::powerStateChanged() if
549 // there aren't any sensors at all on D-Bus.
Patrick Williamsfbf47032023-07-17 12:27:34 -0500550 getLogger().log(std::format(
Matt Spinlerbb449c12021-06-14 11:45:28 -0600551 "At power on, tach sensor {} value not on D-Bus",
552 sensor->name()));
553 }
554 });
555
Matt Spinler4283c5d2021-03-01 15:56:00 -0600556 if (_present)
557 {
Matt Spinler4283c5d2021-03-01 15:56:00 -0600558 // If configured to change functional state on the fan itself,
559 // Set it back to true now if necessary.
560 if (_numSensorFailsForNonFunc)
561 {
562 if (!_functional &&
563 (countNonFunctionalSensors() < _numSensorFailsForNonFunc))
564 {
565 updateInventory(true);
566 }
567 }
568 }
569 else
Matt Spinler7d135642021-02-04 12:44:17 -0600570 {
571 getLogger().log(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500572 std::format("At power on, fan {} is missing", _name));
Matt Spinler7d135642021-02-04 12:44:17 -0600573
574 if (_fanMissingErrorTimer)
575 {
576 _fanMissingErrorTimer->restartOnce(
577 std::chrono::seconds{*_fanMissingErrorDelay});
578 }
579 }
580 }
581 else
582 {
583 _monitorReady = false;
584
585 if (_monitorTimer.isEnabled())
586 {
587 _monitorTimer.setEnabled(false);
588 }
589
590 if (_fanMissingErrorTimer && _fanMissingErrorTimer->isEnabled())
591 {
592 _fanMissingErrorTimer->setEnabled(false);
593 }
594
595 std::for_each(_sensors.begin(), _sensors.end(), [](auto& sensor) {
596 if (sensor->timerRunning())
597 {
598 sensor->stopTimer();
599 }
Matt Spinler623635c2021-03-29 13:13:59 -0500600
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600601 sensor->stopCountTimer();
602 });
Matt Spinler7d135642021-02-04 12:44:17 -0600603 }
604#endif
605}
606
Matthew Barth177fe982020-05-26 11:05:19 -0500607} // namespace monitor
608} // namespace fan
609} // namespace phosphor