blob: fa8b392e7e27bd238d489a58e50da64a4137aa44 [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
Jay Meyera7aed012020-10-06 14:32:22 -050024#include <fmt/format.h>
25
Matthew Barth177fe982020-05-26 11:05:19 -050026#include <phosphor-logging/log.hpp>
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
Matthew Barth177fe982020-05-26 11:05:19 -050038Fan::Fan(Mode mode, sdbusplus::bus::bus& 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),
42 _name(std::get<fanNameField>(def)),
43 _deviation(std::get<fanDeviationField>(def)),
Matt Spinlerc39e8592017-09-28 13:13:08 -050044 _numSensorFailsForNonFunc(std::get<numSensorFailsForNonfuncField>(def)),
Matt Spinlerb0412d02020-10-12 16:53:52 -050045 _trustManager(trust),
46#ifdef MONITOR_USE_JSON
47 _monitorDelay(std::get<monitorStartDelayField>(def)),
48 _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 Spinler623635c2021-03-29 13:13:59 -050062 _fanMissingErrorDelay(std::get<fanMissingErrDelayField>(def)),
Matt Spinlera3584bd2021-03-29 15:48:30 -050063 _setFuncOnPresent(std::get<funcOnPresentField>(def))
Matt Spinlerabf8da32017-04-27 14:08:45 -050064{
Matthew Barth0a9fe162018-01-26 12:53:15 -060065 // Setup tach sensors for monitoring
66 auto& sensors = std::get<sensorListField>(def);
67 for (auto& s : sensors)
68 {
Matt Spinler4283c5d2021-03-01 15:56:00 -060069 _sensors.emplace_back(std::make_shared<TachSensor>(
70 mode, bus, *this, std::get<sensorNameField>(s),
71 std::get<hasTargetField>(s), std::get<funcDelay>(def),
72 std::get<targetInterfaceField>(s), std::get<factorField>(s),
73 std::get<offsetField>(s), std::get<methodField>(def),
Matthew Barth8a8aa442021-11-19 14:13:13 -060074 std::get<thresholdField>(s), std::get<ignoreAboveMaxField>(s),
75 std::get<timeoutField>(def),
Matt Spinlerfdfcc672021-06-01 14:51:06 -060076 std::get<nonfuncRotorErrDelayField>(def),
77 std::get<countIntervalField>(def), event));
Matthew Barth0a9fe162018-01-26 12:53:15 -060078
Matt Spinler4283c5d2021-03-01 15:56:00 -060079 _trustManager->registerSensor(_sensors.back());
Matthew Barth0a9fe162018-01-26 12:53:15 -060080 }
81
Mike Cappsce6820a2021-05-26 10:40:19 -040082 bool functionalState =
83 (_numSensorFailsForNonFunc == 0) ||
84 (countNonFunctionalSensors() < _numSensorFailsForNonFunc);
85
Mike Capps9ff48772021-07-19 14:49:43 -040086 if (updateInventory(functionalState) && !functionalState)
87 {
88 // the inventory update threw an exception, possibly because D-Bus
89 // wasn't ready. Try to update sensors back to functional to avoid a
90 // false-alarm. They will be updated again from subscribing to the
91 // properties-changed event
92
93 for (auto& sensor : _sensors)
94 sensor->setFunctional(true);
95 }
Mike Cappsce6820a2021-05-26 10:40:19 -040096
Matt Spinlerb0412d02020-10-12 16:53:52 -050097#ifndef MONITOR_USE_JSON
Matthew Barth0a9fe162018-01-26 12:53:15 -060098 // Check current tach state when entering monitor mode
Matthew Barth6ad28432017-08-22 11:18:19 -050099 if (mode != Mode::init)
100 {
Matt Spinlerb0412d02020-10-12 16:53:52 -0500101 _monitorReady = true;
102
Matthew Barth177fe982020-05-26 11:05:19 -0500103 // The TachSensors will now have already read the input
104 // and target values, so check them.
Matthew Barth6ad28432017-08-22 11:18:19 -0500105 tachChanged();
106 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500107#else
Matt Spinler7d135642021-02-04 12:44:17 -0600108 if (_system.isPowerOn())
109 {
110 _monitorTimer.restartOnce(std::chrono::seconds(_monitorDelay));
111 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500112#endif
Matt Spinlerb63aa092020-10-14 09:45:11 -0500113
Matt Spinler27f6b682020-10-27 08:43:37 -0500114 if (_fanMissingErrorDelay)
115 {
116 _fanMissingErrorTimer = std::make_unique<
117 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
118 event, std::bind(&System::fanMissingErrorTimerExpired, &system,
119 std::ref(*this)));
Matt Spinler7d135642021-02-04 12:44:17 -0600120 }
Matt Spinler27f6b682020-10-27 08:43:37 -0500121
Matt Spinler7d135642021-02-04 12:44:17 -0600122 try
123 {
124 _present = util::SDBusPlus::getProperty<bool>(
125 util::INVENTORY_PATH + _name, util::INV_ITEM_IFACE, "Present");
126
127 if (!_present)
Matt Spinler27f6b682020-10-27 08:43:37 -0500128 {
Matt Spinlerac372972021-01-25 15:11:22 -0600129 getLogger().log(
130 fmt::format("On startup, fan {} is missing", _name));
Matt Spinler7d135642021-02-04 12:44:17 -0600131 if (_system.isPowerOn() && _fanMissingErrorTimer)
132 {
133 _fanMissingErrorTimer->restartOnce(
134 std::chrono::seconds{*_fanMissingErrorDelay});
135 }
136 }
137 }
138 catch (const util::DBusServiceError& e)
139 {
140 // This could happen on the first BMC boot if the presence
141 // detect app hasn't started yet and there isn't an inventory
142 // cache yet.
143 }
144}
145
146void Fan::presenceIfaceAdded(sdbusplus::message::message& msg)
147{
148 sdbusplus::message::object_path path;
149 std::map<std::string, std::map<std::string, std::variant<bool>>> interfaces;
150
151 msg.read(path, interfaces);
152
153 auto properties = interfaces.find(util::INV_ITEM_IFACE);
154 if (properties == interfaces.end())
155 {
156 return;
157 }
158
159 auto property = properties->second.find("Present");
160 if (property == properties->second.end())
161 {
162 return;
163 }
164
165 _present = std::get<bool>(property->second);
166
167 if (!_present)
168 {
169 getLogger().log(fmt::format(
170 "New fan {} interface added and fan is not present", _name));
171 if (_system.isPowerOn() && _fanMissingErrorTimer)
172 {
Matt Spinler27f6b682020-10-27 08:43:37 -0500173 _fanMissingErrorTimer->restartOnce(
174 std::chrono::seconds{*_fanMissingErrorDelay});
175 }
176 }
Matt Spinler7d135642021-02-04 12:44:17 -0600177
178 _system.fanStatusChange(*this);
Matt Spinlerb0412d02020-10-12 16:53:52 -0500179}
180
181void Fan::startMonitor()
182{
183 _monitorReady = true;
184
Matt Spinler4283c5d2021-03-01 15:56:00 -0600185 std::for_each(_sensors.begin(), _sensors.end(), [this](auto& sensor) {
186 if (_present)
187 {
188 try
189 {
190 // Force a getProperty call to check if the tach sensor is
191 // on D-Bus. If it isn't, now set it to nonfunctional.
192 // This isn't done earlier so that code watching for
193 // nonfunctional tach sensors doesn't take actions before
194 // those sensors show up on D-Bus.
195 sensor->updateTachAndTarget();
196 tachChanged(*sensor);
197 }
198 catch (const util::DBusServiceError& e)
199 {
200 // The tach property still isn't on D-Bus, ensure
201 // sensor is nonfunctional.
202 getLogger().log(fmt::format(
203 "Monitoring starting but {} sensor value not on D-Bus",
204 sensor->name()));
205
206 sensor->setFunctional(false);
207
208 if (_numSensorFailsForNonFunc)
209 {
210 if (_functional && (countNonFunctionalSensors() >=
211 _numSensorFailsForNonFunc))
212 {
213 updateInventory(false);
214 }
215 }
216
217 _system.fanStatusChange(*this);
218 }
219 }
220 });
Matt Spinlerabf8da32017-04-27 14:08:45 -0500221}
222
Matt Spinlerebaae612017-04-27 14:21:48 -0500223void Fan::tachChanged()
224{
Matt Spinlerb0412d02020-10-12 16:53:52 -0500225 if (_monitorReady)
Matt Spinlerebaae612017-04-27 14:21:48 -0500226 {
Matt Spinlerb0412d02020-10-12 16:53:52 -0500227 for (auto& s : _sensors)
228 {
229 tachChanged(*s);
230 }
Matt Spinlerebaae612017-04-27 14:21:48 -0500231 }
232}
233
Matt Spinlerebaae612017-04-27 14:21:48 -0500234void Fan::tachChanged(TachSensor& sensor)
235{
Matt Spinler7d135642021-02-04 12:44:17 -0600236 if (!_system.isPowerOn() || !_monitorReady)
237 {
238 return;
239 }
240
Matt Spinlerc39e8592017-09-28 13:13:08 -0500241 if (_trustManager->active())
242 {
243 if (!_trustManager->checkTrust(sensor))
244 {
245 return;
246 }
247 }
248
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600249 // If the error checking method is 'count', if a tach change leads
250 // to an out of range sensor the count timer will take over in calling
251 // process() until the sensor is healthy again.
252 if (!sensor.countTimerRunning())
Matt Spinler623635c2021-03-29 13:13:59 -0500253 {
254 process(sensor);
255 }
256}
257
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600258void Fan::countTimerExpired(TachSensor& sensor)
Matt Spinler623635c2021-03-29 13:13:59 -0500259{
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600260 if (_trustManager->active() && !_trustManager->checkTrust(sensor))
Matt Spinler623635c2021-03-29 13:13:59 -0500261 {
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600262 return;
Matt Spinler623635c2021-03-29 13:13:59 -0500263 }
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600264 process(sensor);
Matthew Barthfcb0dbc2021-02-10 14:23:39 -0600265}
266
267void Fan::process(TachSensor& sensor)
268{
Matthew Barth177fe982020-05-26 11:05:19 -0500269 // If this sensor is out of range at this moment, start
270 // its timer, at the end of which the inventory
271 // for the fan may get updated to not functional.
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500272
Matthew Barth177fe982020-05-26 11:05:19 -0500273 // If this sensor is OK, put everything back into a good state.
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500274
275 if (outOfRange(sensor))
276 {
Matthew Barthe11cbc62018-02-20 12:11:07 -0600277 if (sensor.functional())
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500278 {
Jolie Ku69f2f482020-10-21 09:59:43 +0800279 switch (sensor.getMethod())
280 {
281 case MethodMode::timebased:
282 // Start nonfunctional timer if not already running
283 sensor.startTimer(TimerMode::nonfunc);
284 break;
285 case MethodMode::count:
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600286
287 if (!sensor.countTimerRunning())
288 {
289 sensor.startCountTimer();
290 }
Jolie Ku69f2f482020-10-21 09:59:43 +0800291 sensor.setCounter(true);
292 if (sensor.getCounter() >= sensor.getThreshold())
293 {
294 updateState(sensor);
295 }
296 break;
297 }
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500298 }
299 }
300 else
301 {
Jolie Ku69f2f482020-10-21 09:59:43 +0800302 switch (sensor.getMethod())
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500303 {
Jolie Ku69f2f482020-10-21 09:59:43 +0800304 case MethodMode::timebased:
305 if (sensor.functional())
306 {
Matthew Barth11b5d8f2021-01-28 14:04:09 -0600307 if (sensor.timerRunning())
308 {
309 sensor.stopTimer();
310 }
Jolie Ku69f2f482020-10-21 09:59:43 +0800311 }
312 else
313 {
314 // Start functional timer if not already running
315 sensor.startTimer(TimerMode::func);
316 }
317 break;
318 case MethodMode::count:
319 sensor.setCounter(false);
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600320 if (sensor.getCounter() == 0)
Jolie Ku69f2f482020-10-21 09:59:43 +0800321 {
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600322 if (!sensor.functional())
323 {
324 updateState(sensor);
325 }
326
327 sensor.stopCountTimer();
Jolie Ku69f2f482020-10-21 09:59:43 +0800328 }
329 break;
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500330 }
331 }
Matt Spinlerebaae612017-04-27 14:21:48 -0500332}
333
Matthew Barthf552ea52018-01-15 16:22:04 -0600334uint64_t Fan::findTargetSpeed()
Matt Spinlerabf8da32017-04-27 14:08:45 -0500335{
336 uint64_t target = 0;
Matthew Barth177fe982020-05-26 11:05:19 -0500337 // The sensor doesn't support a target,
338 // so get it from another sensor.
Matthew Barthf552ea52018-01-15 16:22:04 -0600339 auto s = std::find_if(_sensors.begin(), _sensors.end(),
Matthew Barth177fe982020-05-26 11:05:19 -0500340 [](const auto& s) { return s->hasTarget(); });
Matt Spinlerabf8da32017-04-27 14:08:45 -0500341
Matthew Barthf552ea52018-01-15 16:22:04 -0600342 if (s != _sensors.end())
Matt Spinlerabf8da32017-04-27 14:08:45 -0500343 {
Matthew Barthf552ea52018-01-15 16:22:04 -0600344 target = (*s)->getTarget();
Matt Spinlerabf8da32017-04-27 14:08:45 -0500345 }
346
347 return target;
348}
349
Mike Cappsce6820a2021-05-26 10:40:19 -0400350size_t Fan::countNonFunctionalSensors() const
Matt Spinlerabf8da32017-04-27 14:08:45 -0500351{
Matthew Barth7c23a042021-01-26 16:21:45 -0600352 return std::count_if(_sensors.begin(), _sensors.end(),
353 [](const auto& s) { return !s->functional(); });
Matt Spinlerabf8da32017-04-27 14:08:45 -0500354}
355
Matt Spinlerabf8da32017-04-27 14:08:45 -0500356bool Fan::outOfRange(const TachSensor& sensor)
357{
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400358 if (!sensor.hasOwner())
Matt Spinlerabf8da32017-04-27 14:08:45 -0500359 {
360 return true;
361 }
362
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400363 auto actual = static_cast<uint64_t>(sensor.getInput());
364 auto range = sensor.getRange(_deviation);
365
Matthew Barth8a8aa442021-11-19 14:13:13 -0600366 return ((actual < range.first) ||
367 (range.second && actual > range.second.value()));
Matt Spinlerabf8da32017-04-27 14:08:45 -0500368}
369
Jolie Ku69f2f482020-10-21 09:59:43 +0800370void Fan::updateState(TachSensor& sensor)
Matt Spinlera9406a72017-04-27 14:29:24 -0500371{
Matt Spinler7d135642021-02-04 12:44:17 -0600372 if (!_system.isPowerOn())
373 {
374 return;
375 }
376
Matthew Barth8a8aa442021-11-19 14:13:13 -0600377 auto range = sensor.getRange(_deviation);
378 std::string rangeMax = "NoMax";
379 if (range.second)
380 {
381 rangeMax = std::to_string(range.second.value());
382 }
383
Matthew Barthe11cbc62018-02-20 12:11:07 -0600384 sensor.setFunctional(!sensor.functional());
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500385 getLogger().log(
386 fmt::format("Setting tach sensor {} functional state to {}. "
Matthew Barth7c23a042021-01-26 16:21:45 -0600387 "[target = {}, input = {}, allowed range = ({} - {})]",
388 sensor.name(), sensor.functional(), sensor.getTarget(),
Matthew Barth8a8aa442021-11-19 14:13:13 -0600389 sensor.getInput(), range.first, rangeMax));
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 {
Matthew Barth7c23a042021-01-26 16:21:45 -0600402 getLogger().log(fmt::format("Setting fan {} to functional, number "
403 "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 {
Matthew Barth7c23a042021-01-26 16:21:45 -0600413 getLogger().log(fmt::format("Setting fan {} to nonfunctional, "
414 "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
420 _system.fanStatusChange(*this);
Matt Spinlera9406a72017-04-27 14:29:24 -0500421}
422
Mike Capps9ff48772021-07-19 14:49:43 -0400423bool Fan::updateInventory(bool functional)
Matt Spinlerb1e18512017-04-27 14:42:33 -0500424{
Mike Capps9ff48772021-07-19 14:49:43 -0400425 bool dbusError = false;
426
427 try
Matt Spinlerb1e18512017-04-27 14:42:33 -0500428 {
Mike Capps9ff48772021-07-19 14:49:43 -0400429 auto objectMap =
430 util::getObjMap<bool>(_name, util::OPERATIONAL_STATUS_INTF,
431 util::FUNCTIONAL_PROPERTY, functional);
432
Mike Capps8af8a622022-02-04 16:13:33 -0500433 auto response = util::SDBusPlus::callMethod(
434 _bus, util::INVENTORY_SVC, util::INVENTORY_PATH,
435 util::INVENTORY_INTF, "Notify", objectMap);
Mike Capps9ff48772021-07-19 14:49:43 -0400436
437 if (response.is_method_error())
438 {
439 log<level::ERR>("Error in Notify call to update inventory");
440
441 dbusError = true;
442 }
443 }
444 catch (const util::DBusError& e)
445 {
446 dbusError = true;
447
448 getLogger().log(
449 fmt::format("D-Bus Exception reading/updating inventory : {}",
450 e.what()),
451 Logger::error);
Matt Spinlerb1e18512017-04-27 14:42:33 -0500452 }
453
Matthew Barth177fe982020-05-26 11:05:19 -0500454 // This will always track the current state of the inventory.
Matt Spinlerb1e18512017-04-27 14:42:33 -0500455 _functional = functional;
Mike Capps9ff48772021-07-19 14:49:43 -0400456
457 return dbusError;
Matt Spinlerb1e18512017-04-27 14:42:33 -0500458}
459
Matt Spinlerb63aa092020-10-14 09:45:11 -0500460void Fan::presenceChanged(sdbusplus::message::message& msg)
461{
462 std::string interface;
463 std::map<std::string, std::variant<bool>> properties;
464
465 msg.read(interface, properties);
466
467 auto presentProp = properties.find("Present");
468 if (presentProp != properties.end())
469 {
470 _present = std::get<bool>(presentProp->second);
471
Matt Spinler27f6b682020-10-27 08:43:37 -0500472 getLogger().log(
Matt Spinlerac372972021-01-25 15:11:22 -0600473 fmt::format("Fan {} presence state change to {}", _name, _present));
Matt Spinler27f6b682020-10-27 08:43:37 -0500474
Matt Spinlera3584bd2021-03-29 15:48:30 -0500475 if (_present && _setFuncOnPresent)
476 {
477 updateInventory(true);
478 std::for_each(_sensors.begin(), _sensors.end(), [](auto& sensor) {
479 sensor->setFunctional(true);
480 sensor->resetMethod();
481 });
482 }
483
Matthew Barth43b4cde2022-02-15 16:30:11 -0600484 _system.fanStatusChange(*this);
485
Matt Spinler27f6b682020-10-27 08:43:37 -0500486 if (_fanMissingErrorDelay)
487 {
Matt Spinler7d135642021-02-04 12:44:17 -0600488 if (!_present && _system.isPowerOn())
Matt Spinler27f6b682020-10-27 08:43:37 -0500489 {
490 _fanMissingErrorTimer->restartOnce(
491 std::chrono::seconds{*_fanMissingErrorDelay});
492 }
Matt Spinler7d135642021-02-04 12:44:17 -0600493 else if (_present && _fanMissingErrorTimer->isEnabled())
Matt Spinler27f6b682020-10-27 08:43:37 -0500494 {
495 _fanMissingErrorTimer->setEnabled(false);
496 }
497 }
Matt Spinlerb63aa092020-10-14 09:45:11 -0500498 }
499}
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500500
501void Fan::sensorErrorTimerExpired(const TachSensor& sensor)
502{
Matt Spinler7d135642021-02-04 12:44:17 -0600503 if (_present && _system.isPowerOn())
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500504 {
505 _system.sensorErrorTimerExpired(*this, sensor);
506 }
507}
508
Matt Spinler7d135642021-02-04 12:44:17 -0600509void Fan::powerStateChanged(bool powerStateOn)
510{
511#ifdef MONITOR_USE_JSON
512 if (powerStateOn)
513 {
Matt Spinler7d135642021-02-04 12:44:17 -0600514 _monitorTimer.restartOnce(std::chrono::seconds(_monitorDelay));
515
Matt Spinlerbb449c12021-06-14 11:45:28 -0600516 _numSensorsOnDBusAtPowerOn = 0;
517
518 std::for_each(_sensors.begin(), _sensors.end(), [this](auto& sensor) {
519 try
520 {
521 // Force a getProperty call. If sensor is on D-Bus,
522 // then make sure it's functional.
523 sensor->updateTachAndTarget();
524
525 _numSensorsOnDBusAtPowerOn++;
526
527 if (_present)
528 {
529 // If not functional, set it back to functional.
530 if (!sensor->functional())
531 {
532 sensor->setFunctional(true);
533 _system.fanStatusChange(*this, true);
534 }
535
536 // Set the counters back to zero
537 if (sensor->getMethod() == MethodMode::count)
538 {
539 sensor->resetMethod();
540 }
541 }
542 }
543 catch (const util::DBusError& e)
544 {
545 // Properties still aren't on D-Bus. Let startMonitor()
546 // deal with it, or maybe System::powerStateChanged() if
547 // there aren't any sensors at all on D-Bus.
548 getLogger().log(fmt::format(
549 "At power on, tach sensor {} value not on D-Bus",
550 sensor->name()));
551 }
552 });
553
Matt Spinler4283c5d2021-03-01 15:56:00 -0600554 if (_present)
555 {
Matt Spinler4283c5d2021-03-01 15:56:00 -0600556 // If configured to change functional state on the fan itself,
557 // Set it back to true now if necessary.
558 if (_numSensorFailsForNonFunc)
559 {
560 if (!_functional &&
561 (countNonFunctionalSensors() < _numSensorFailsForNonFunc))
562 {
563 updateInventory(true);
564 }
565 }
566 }
567 else
Matt Spinler7d135642021-02-04 12:44:17 -0600568 {
569 getLogger().log(
570 fmt::format("At power on, fan {} is missing", _name));
571
572 if (_fanMissingErrorTimer)
573 {
574 _fanMissingErrorTimer->restartOnce(
575 std::chrono::seconds{*_fanMissingErrorDelay});
576 }
577 }
578 }
579 else
580 {
581 _monitorReady = false;
582
583 if (_monitorTimer.isEnabled())
584 {
585 _monitorTimer.setEnabled(false);
586 }
587
588 if (_fanMissingErrorTimer && _fanMissingErrorTimer->isEnabled())
589 {
590 _fanMissingErrorTimer->setEnabled(false);
591 }
592
593 std::for_each(_sensors.begin(), _sensors.end(), [](auto& sensor) {
594 if (sensor->timerRunning())
595 {
596 sensor->stopTimer();
597 }
Matt Spinler623635c2021-03-29 13:13:59 -0500598
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600599 sensor->stopCountTimer();
600 });
Matt Spinler7d135642021-02-04 12:44:17 -0600601 }
602#endif
603}
604
Matthew Barth177fe982020-05-26 11:05:19 -0500605} // namespace monitor
606} // namespace fan
607} // namespace phosphor