blob: a9ca01355b3cdcd274f4f02151b72f8b3a81a534 [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) :
Patrick Williamsdfddd642024-08-16 15:21:51 -040041 _bus(bus), _name(def.name), _deviation(def.deviation),
Matt Spinlerf724c162023-05-10 11:14:37 -050042 _upperDeviation(def.upperDeviation),
Matt Spinler18fb12b2023-05-09 11:17:42 -050043 _numSensorFailsForNonFunc(def.numSensorFailsForNonfunc),
Matt Spinlerb0412d02020-10-12 16:53:52 -050044 _trustManager(trust),
45#ifdef MONITOR_USE_JSON
Matt Spinler18fb12b2023-05-09 11:17:42 -050046 _monitorDelay(def.monitorStartDelay),
Matt Spinlerb0412d02020-10-12 16:53:52 -050047 _monitorTimer(event, std::bind(std::mem_fn(&Fan::startMonitor), this)),
48#endif
Matt Spinlerb63aa092020-10-14 09:45:11 -050049 _system(system),
50 _presenceMatch(bus,
51 rules::propertiesChanged(util::INVENTORY_PATH + _name,
52 util::INV_ITEM_IFACE),
53 std::bind(std::mem_fn(&Fan::presenceChanged), this,
Matt Spinler27f6b682020-10-27 08:43:37 -050054 std::placeholders::_1)),
Matt Spinler7d135642021-02-04 12:44:17 -060055 _presenceIfaceAddedMatch(
56 bus,
57 rules::interfacesAdded() +
58 rules::argNpath(0, util::INVENTORY_PATH + _name),
59 std::bind(std::mem_fn(&Fan::presenceIfaceAdded), this,
60 std::placeholders::_1)),
Matt Spinler18fb12b2023-05-09 11:17:42 -050061 _fanMissingErrorDelay(def.fanMissingErrDelay),
62 _setFuncOnPresent(def.funcOnPresent)
Matt Spinlerabf8da32017-04-27 14:08:45 -050063{
Matthew Barth0a9fe162018-01-26 12:53:15 -060064 // Setup tach sensors for monitoring
Matt Spinler18fb12b2023-05-09 11:17:42 -050065 for (const auto& s : def.sensorList)
Matthew Barth0a9fe162018-01-26 12:53:15 -060066 {
Matt Spinler4283c5d2021-03-01 15:56:00 -060067 _sensors.emplace_back(std::make_shared<TachSensor>(
Matt Spinler18fb12b2023-05-09 11:17:42 -050068 mode, bus, *this, s.name, s.hasTarget, def.funcDelay,
69 s.targetInterface, s.targetPath, s.factor, s.offset, def.method,
70 s.threshold, s.ignoreAboveMax, def.timeout,
71 def.nonfuncRotorErrDelay, def.countInterval, event));
Matthew Barth0a9fe162018-01-26 12:53:15 -060072
Matt Spinler4283c5d2021-03-01 15:56:00 -060073 _trustManager->registerSensor(_sensors.back());
Matthew Barth0a9fe162018-01-26 12:53:15 -060074 }
75
Mike Cappsce6820a2021-05-26 10:40:19 -040076 bool functionalState =
77 (_numSensorFailsForNonFunc == 0) ||
78 (countNonFunctionalSensors() < _numSensorFailsForNonFunc);
79
Mike Capps9ff48772021-07-19 14:49:43 -040080 if (updateInventory(functionalState) && !functionalState)
81 {
82 // the inventory update threw an exception, possibly because D-Bus
83 // wasn't ready. Try to update sensors back to functional to avoid a
84 // false-alarm. They will be updated again from subscribing to the
85 // properties-changed event
86
87 for (auto& sensor : _sensors)
88 sensor->setFunctional(true);
89 }
Mike Cappsce6820a2021-05-26 10:40:19 -040090
Matt Spinlerb0412d02020-10-12 16:53:52 -050091#ifndef MONITOR_USE_JSON
Matthew Barth0a9fe162018-01-26 12:53:15 -060092 // Check current tach state when entering monitor mode
Matthew Barth6ad28432017-08-22 11:18:19 -050093 if (mode != Mode::init)
94 {
Matt Spinlerb0412d02020-10-12 16:53:52 -050095 _monitorReady = true;
96
Matthew Barth177fe982020-05-26 11:05:19 -050097 // The TachSensors will now have already read the input
98 // and target values, so check them.
Matthew Barth6ad28432017-08-22 11:18:19 -050099 tachChanged();
100 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500101#else
Matt Spinler7d135642021-02-04 12:44:17 -0600102 if (_system.isPowerOn())
103 {
104 _monitorTimer.restartOnce(std::chrono::seconds(_monitorDelay));
105 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500106#endif
Matt Spinlerb63aa092020-10-14 09:45:11 -0500107
Matt Spinler27f6b682020-10-27 08:43:37 -0500108 if (_fanMissingErrorDelay)
109 {
110 _fanMissingErrorTimer = std::make_unique<
111 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
112 event, std::bind(&System::fanMissingErrorTimerExpired, &system,
113 std::ref(*this)));
Matt Spinler7d135642021-02-04 12:44:17 -0600114 }
Matt Spinler27f6b682020-10-27 08:43:37 -0500115
Matt Spinler7d135642021-02-04 12:44:17 -0600116 try
117 {
118 _present = util::SDBusPlus::getProperty<bool>(
119 util::INVENTORY_PATH + _name, util::INV_ITEM_IFACE, "Present");
120
121 if (!_present)
Matt Spinler27f6b682020-10-27 08:43:37 -0500122 {
Matt Spinlerac372972021-01-25 15:11:22 -0600123 getLogger().log(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500124 std::format("On startup, fan {} is missing", _name));
Matt Spinler7d135642021-02-04 12:44:17 -0600125 if (_system.isPowerOn() && _fanMissingErrorTimer)
126 {
127 _fanMissingErrorTimer->restartOnce(
128 std::chrono::seconds{*_fanMissingErrorDelay});
129 }
130 }
131 }
132 catch (const util::DBusServiceError& e)
133 {
134 // This could happen on the first BMC boot if the presence
135 // detect app hasn't started yet and there isn't an inventory
136 // cache yet.
137 }
138}
139
Patrick Williamscb356d42022-07-22 19:26:53 -0500140void Fan::presenceIfaceAdded(sdbusplus::message_t& msg)
Matt Spinler7d135642021-02-04 12:44:17 -0600141{
142 sdbusplus::message::object_path path;
143 std::map<std::string, std::map<std::string, std::variant<bool>>> interfaces;
144
145 msg.read(path, interfaces);
146
147 auto properties = interfaces.find(util::INV_ITEM_IFACE);
148 if (properties == interfaces.end())
149 {
150 return;
151 }
152
153 auto property = properties->second.find("Present");
154 if (property == properties->second.end())
155 {
156 return;
157 }
158
159 _present = std::get<bool>(property->second);
160
161 if (!_present)
162 {
Patrick Williamsfbf47032023-07-17 12:27:34 -0500163 getLogger().log(std::format(
Matt Spinler7d135642021-02-04 12:44:17 -0600164 "New fan {} interface added and fan is not present", _name));
165 if (_system.isPowerOn() && _fanMissingErrorTimer)
166 {
Matt Spinler27f6b682020-10-27 08:43:37 -0500167 _fanMissingErrorTimer->restartOnce(
168 std::chrono::seconds{*_fanMissingErrorDelay});
169 }
170 }
Matt Spinler7d135642021-02-04 12:44:17 -0600171
172 _system.fanStatusChange(*this);
Matt Spinlerb0412d02020-10-12 16:53:52 -0500173}
174
175void Fan::startMonitor()
176{
177 _monitorReady = true;
178
Matt Spinler4283c5d2021-03-01 15:56:00 -0600179 std::for_each(_sensors.begin(), _sensors.end(), [this](auto& sensor) {
Matt Spinler3494a572023-11-28 12:45:32 -0600180 try
Matt Spinler4283c5d2021-03-01 15:56:00 -0600181 {
Matt Spinler3494a572023-11-28 12:45:32 -0600182 // Force a getProperty call to check if the tach sensor is
183 // on D-Bus. If it isn't, now set it to nonfunctional.
184 // This isn't done earlier so that code watching for
185 // nonfunctional tach sensors doesn't take actions before
186 // those sensors show up on D-Bus.
187 sensor->updateTachAndTarget();
188 tachChanged(*sensor);
189 }
190 catch (const util::DBusServiceError& e)
191 {
192 // The tach property still isn't on D-Bus. Ensure
193 // sensor is nonfunctional, but skip creating an
194 // error for it since it isn't a fan problem.
195 getLogger().log(std::format(
196 "Monitoring starting but {} sensor value not on D-Bus",
197 sensor->name()));
Matt Spinler4283c5d2021-03-01 15:56:00 -0600198
Matt Spinler3494a572023-11-28 12:45:32 -0600199 sensor->setFunctional(false, true);
Matt Spinler4283c5d2021-03-01 15:56:00 -0600200
Matt Spinler3494a572023-11-28 12:45:32 -0600201 if (_numSensorFailsForNonFunc)
202 {
203 if (_functional &&
204 (countNonFunctionalSensors() >= _numSensorFailsForNonFunc))
Matt Spinler4283c5d2021-03-01 15:56:00 -0600205 {
Matt Spinler3494a572023-11-28 12:45:32 -0600206 updateInventory(false);
Matt Spinler4283c5d2021-03-01 15:56:00 -0600207 }
Matt Spinler4283c5d2021-03-01 15:56:00 -0600208 }
Matt Spinler3494a572023-11-28 12:45:32 -0600209
210 // At this point, don't start any power off actions due
211 // to missing sensors. Let something else handle that
212 // policy.
213 _system.fanStatusChange(*this, true);
Matt Spinler4283c5d2021-03-01 15:56:00 -0600214 }
215 });
Matt Spinlerabf8da32017-04-27 14:08:45 -0500216}
217
Matt Spinlerebaae612017-04-27 14:21:48 -0500218void Fan::tachChanged()
219{
Matt Spinlerb0412d02020-10-12 16:53:52 -0500220 if (_monitorReady)
Matt Spinlerebaae612017-04-27 14:21:48 -0500221 {
Matt Spinlerb0412d02020-10-12 16:53:52 -0500222 for (auto& s : _sensors)
223 {
224 tachChanged(*s);
225 }
Matt Spinlerebaae612017-04-27 14:21:48 -0500226 }
227}
228
Matt Spinlerebaae612017-04-27 14:21:48 -0500229void Fan::tachChanged(TachSensor& sensor)
230{
Matt Spinler7d135642021-02-04 12:44:17 -0600231 if (!_system.isPowerOn() || !_monitorReady)
232 {
233 return;
234 }
235
Matt Spinlerc39e8592017-09-28 13:13:08 -0500236 if (_trustManager->active())
237 {
238 if (!_trustManager->checkTrust(sensor))
239 {
240 return;
241 }
242 }
243
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600244 // If the error checking method is 'count', if a tach change leads
245 // to an out of range sensor the count timer will take over in calling
246 // process() until the sensor is healthy again.
247 if (!sensor.countTimerRunning())
Matt Spinler623635c2021-03-29 13:13:59 -0500248 {
249 process(sensor);
250 }
251}
252
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600253void Fan::countTimerExpired(TachSensor& sensor)
Matt Spinler623635c2021-03-29 13:13:59 -0500254{
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600255 if (_trustManager->active() && !_trustManager->checkTrust(sensor))
Matt Spinler623635c2021-03-29 13:13:59 -0500256 {
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600257 return;
Matt Spinler623635c2021-03-29 13:13:59 -0500258 }
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600259 process(sensor);
Matthew Barthfcb0dbc2021-02-10 14:23:39 -0600260}
261
262void Fan::process(TachSensor& sensor)
263{
Matthew Barth177fe982020-05-26 11:05:19 -0500264 // If this sensor is out of range at this moment, start
265 // its timer, at the end of which the inventory
266 // for the fan may get updated to not functional.
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500267
Matthew Barth177fe982020-05-26 11:05:19 -0500268 // If this sensor is OK, put everything back into a good state.
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500269
270 if (outOfRange(sensor))
271 {
Matthew Barthe11cbc62018-02-20 12:11:07 -0600272 if (sensor.functional())
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500273 {
Jolie Ku69f2f482020-10-21 09:59:43 +0800274 switch (sensor.getMethod())
275 {
276 case MethodMode::timebased:
277 // Start nonfunctional timer if not already running
278 sensor.startTimer(TimerMode::nonfunc);
279 break;
280 case MethodMode::count:
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600281
282 if (!sensor.countTimerRunning())
283 {
284 sensor.startCountTimer();
285 }
Jolie Ku69f2f482020-10-21 09:59:43 +0800286 sensor.setCounter(true);
287 if (sensor.getCounter() >= sensor.getThreshold())
288 {
289 updateState(sensor);
290 }
291 break;
292 }
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500293 }
294 }
295 else
296 {
Jolie Ku69f2f482020-10-21 09:59:43 +0800297 switch (sensor.getMethod())
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500298 {
Jolie Ku69f2f482020-10-21 09:59:43 +0800299 case MethodMode::timebased:
300 if (sensor.functional())
301 {
Matthew Barth11b5d8f2021-01-28 14:04:09 -0600302 if (sensor.timerRunning())
303 {
304 sensor.stopTimer();
305 }
Jolie Ku69f2f482020-10-21 09:59:43 +0800306 }
307 else
308 {
309 // Start functional timer if not already running
310 sensor.startTimer(TimerMode::func);
311 }
312 break;
313 case MethodMode::count:
314 sensor.setCounter(false);
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600315 if (sensor.getCounter() == 0)
Jolie Ku69f2f482020-10-21 09:59:43 +0800316 {
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600317 if (!sensor.functional())
318 {
319 updateState(sensor);
320 }
321
322 sensor.stopCountTimer();
Jolie Ku69f2f482020-10-21 09:59:43 +0800323 }
324 break;
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500325 }
326 }
Matt Spinlerebaae612017-04-27 14:21:48 -0500327}
328
Matthew Barthf552ea52018-01-15 16:22:04 -0600329uint64_t Fan::findTargetSpeed()
Matt Spinlerabf8da32017-04-27 14:08:45 -0500330{
331 uint64_t target = 0;
Matthew Barth177fe982020-05-26 11:05:19 -0500332 // The sensor doesn't support a target,
333 // so get it from another sensor.
Patrick Williamsdfddd642024-08-16 15:21:51 -0400334 auto s = std::find_if(_sensors.begin(), _sensors.end(), [](const auto& s) {
335 return s->hasTarget();
336 });
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{
Patrick Williamsdfddd642024-08-16 15:21:51 -0400348 return std::count_if(_sensors.begin(), _sensors.end(), [](const auto& s) {
349 return !s->functional();
350 });
Matt Spinlerabf8da32017-04-27 14:08:45 -0500351}
352
Matt Spinlerabf8da32017-04-27 14:08:45 -0500353bool Fan::outOfRange(const TachSensor& sensor)
354{
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400355 if (!sensor.hasOwner())
Matt Spinlerabf8da32017-04-27 14:08:45 -0500356 {
357 return true;
358 }
359
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400360 auto actual = static_cast<uint64_t>(sensor.getInput());
Matt Spinlerf724c162023-05-10 11:14:37 -0500361 auto range = sensor.getRange(_deviation, _upperDeviation);
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400362
Matthew Barth8a8aa442021-11-19 14:13:13 -0600363 return ((actual < range.first) ||
364 (range.second && actual > range.second.value()));
Matt Spinlerabf8da32017-04-27 14:08:45 -0500365}
366
Jolie Ku69f2f482020-10-21 09:59:43 +0800367void Fan::updateState(TachSensor& sensor)
Matt Spinlera9406a72017-04-27 14:29:24 -0500368{
Matt Spinler7d135642021-02-04 12:44:17 -0600369 if (!_system.isPowerOn())
370 {
371 return;
372 }
373
Matt Spinlerf724c162023-05-10 11:14:37 -0500374 auto range = sensor.getRange(_deviation, _upperDeviation);
Matthew Barth8a8aa442021-11-19 14:13:13 -0600375 std::string rangeMax = "NoMax";
376 if (range.second)
377 {
378 rangeMax = std::to_string(range.second.value());
379 }
380
Matt Spinlerae01b5f2022-07-06 16:49:04 -0500381 // Skip starting the error timer if the sensor
382 // isn't on D-Bus as this isn't a fan hardware problem.
383 sensor.setFunctional(!sensor.functional(), !sensor.hasOwner());
384
Patrick Williamsfbf47032023-07-17 12:27:34 -0500385 getLogger().log(std::format(
Matt Spinlerae01b5f2022-07-06 16:49:04 -0500386 "Setting tach sensor {} functional state to {}. "
Matt Spinler466bd222023-01-25 16:15:52 -0600387 "[target = {}, actual = {}, allowed range = ({} - {}) "
Matt Spinlerae01b5f2022-07-06 16:49:04 -0500388 "owned = {}]",
389 sensor.name(), sensor.functional(), sensor.getTarget(),
390 sensor.getInput(), range.first, rangeMax, sensor.hasOwner()));
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500391
392 // A zero value for _numSensorFailsForNonFunc means we aren't dealing
393 // with fan FRU functional status, only sensor functional status.
394 if (_numSensorFailsForNonFunc)
Matthew Barthe11cbc62018-02-20 12:11:07 -0600395 {
Matthew Barth7c23a042021-01-26 16:21:45 -0600396 auto numNonFuncSensors = countNonFunctionalSensors();
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500397 // If the fan was nonfunctional and enough sensors are now OK,
Matthew Barthcceffdd2021-05-20 12:17:21 -0500398 // the fan can be set to functional as long as `set_func_on_present` was
399 // not set
400 if (!_setFuncOnPresent && !_functional &&
401 !(numNonFuncSensors >= _numSensorFailsForNonFunc))
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500402 {
Patrick Williamsfbf47032023-07-17 12:27:34 -0500403 getLogger().log(std::format("Setting fan {} to functional, number "
Matthew Barth7c23a042021-01-26 16:21:45 -0600404 "of nonfunctional sensors = {}",
405 _name, numNonFuncSensors));
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500406 updateInventory(true);
407 }
Matt Spinlera9406a72017-04-27 14:29:24 -0500408
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500409 // If the fan is currently functional, but too many
410 // contained sensors are now nonfunctional, update
Matthew Barth7c23a042021-01-26 16:21:45 -0600411 // the fan to nonfunctional.
412 if (_functional && (numNonFuncSensors >= _numSensorFailsForNonFunc))
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500413 {
Patrick Williamsfbf47032023-07-17 12:27:34 -0500414 getLogger().log(std::format("Setting fan {} to nonfunctional, "
Matthew Barth7c23a042021-01-26 16:21:45 -0600415 "number of nonfunctional sensors = {}",
416 _name, numNonFuncSensors));
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500417 updateInventory(false);
418 }
Matt Spinlerb1e18512017-04-27 14:42:33 -0500419 }
Matt Spinlerb63aa092020-10-14 09:45:11 -0500420
Matt Spinlerae01b5f2022-07-06 16:49:04 -0500421 // Skip the power off rule checks if the sensor isn't
422 // on D-Bus so a running system isn't shutdown.
423 _system.fanStatusChange(*this, !sensor.hasOwner());
Matt Spinlera9406a72017-04-27 14:29:24 -0500424}
425
Mike Capps9ff48772021-07-19 14:49:43 -0400426bool Fan::updateInventory(bool functional)
Matt Spinlerb1e18512017-04-27 14:42:33 -0500427{
Mike Capps9ff48772021-07-19 14:49:43 -0400428 bool dbusError = false;
429
430 try
Matt Spinlerb1e18512017-04-27 14:42:33 -0500431 {
Mike Capps9ff48772021-07-19 14:49:43 -0400432 auto objectMap =
433 util::getObjMap<bool>(_name, util::OPERATIONAL_STATUS_INTF,
434 util::FUNCTIONAL_PROPERTY, functional);
435
Mike Capps8af8a622022-02-04 16:13:33 -0500436 auto response = util::SDBusPlus::callMethod(
437 _bus, util::INVENTORY_SVC, util::INVENTORY_PATH,
438 util::INVENTORY_INTF, "Notify", objectMap);
Mike Capps9ff48772021-07-19 14:49:43 -0400439
440 if (response.is_method_error())
441 {
442 log<level::ERR>("Error in Notify call to update inventory");
443
444 dbusError = true;
445 }
446 }
447 catch (const util::DBusError& e)
448 {
449 dbusError = true;
450
451 getLogger().log(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500452 std::format("D-Bus Exception reading/updating inventory : {}",
Mike Capps9ff48772021-07-19 14:49:43 -0400453 e.what()),
454 Logger::error);
Matt Spinlerb1e18512017-04-27 14:42:33 -0500455 }
456
Matthew Barth177fe982020-05-26 11:05:19 -0500457 // This will always track the current state of the inventory.
Matt Spinlerb1e18512017-04-27 14:42:33 -0500458 _functional = functional;
Mike Capps9ff48772021-07-19 14:49:43 -0400459
460 return dbusError;
Matt Spinlerb1e18512017-04-27 14:42:33 -0500461}
462
Patrick Williamscb356d42022-07-22 19:26:53 -0500463void Fan::presenceChanged(sdbusplus::message_t& msg)
Matt Spinlerb63aa092020-10-14 09:45:11 -0500464{
465 std::string interface;
466 std::map<std::string, std::variant<bool>> properties;
467
468 msg.read(interface, properties);
469
470 auto presentProp = properties.find("Present");
471 if (presentProp != properties.end())
472 {
473 _present = std::get<bool>(presentProp->second);
474
Matt Spinler27f6b682020-10-27 08:43:37 -0500475 getLogger().log(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500476 std::format("Fan {} presence state change to {}", _name, _present));
Matt Spinler27f6b682020-10-27 08:43:37 -0500477
Matt Spinlera3584bd2021-03-29 15:48:30 -0500478 if (_present && _setFuncOnPresent)
479 {
480 updateInventory(true);
481 std::for_each(_sensors.begin(), _sensors.end(), [](auto& sensor) {
482 sensor->setFunctional(true);
483 sensor->resetMethod();
484 });
485 }
486
Matthew Barth43b4cde2022-02-15 16:30:11 -0600487 _system.fanStatusChange(*this);
488
Matt Spinler27f6b682020-10-27 08:43:37 -0500489 if (_fanMissingErrorDelay)
490 {
Matt Spinler7d135642021-02-04 12:44:17 -0600491 if (!_present && _system.isPowerOn())
Matt Spinler27f6b682020-10-27 08:43:37 -0500492 {
493 _fanMissingErrorTimer->restartOnce(
494 std::chrono::seconds{*_fanMissingErrorDelay});
495 }
Matt Spinler7d135642021-02-04 12:44:17 -0600496 else if (_present && _fanMissingErrorTimer->isEnabled())
Matt Spinler27f6b682020-10-27 08:43:37 -0500497 {
498 _fanMissingErrorTimer->setEnabled(false);
499 }
500 }
Matt Spinlerb63aa092020-10-14 09:45:11 -0500501 }
502}
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500503
504void Fan::sensorErrorTimerExpired(const TachSensor& sensor)
505{
Matt Spinler7d135642021-02-04 12:44:17 -0600506 if (_present && _system.isPowerOn())
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500507 {
508 _system.sensorErrorTimerExpired(*this, sensor);
509 }
510}
511
Mike Capps808d7fe2022-06-13 10:12:16 -0400512void Fan::powerStateChanged([[maybe_unused]] bool powerStateOn)
Matt Spinler7d135642021-02-04 12:44:17 -0600513{
514#ifdef MONITOR_USE_JSON
515 if (powerStateOn)
516 {
Matt Spinler7d135642021-02-04 12:44:17 -0600517 _monitorTimer.restartOnce(std::chrono::seconds(_monitorDelay));
518
Matt Spinlerbb449c12021-06-14 11:45:28 -0600519 _numSensorsOnDBusAtPowerOn = 0;
520
521 std::for_each(_sensors.begin(), _sensors.end(), [this](auto& sensor) {
522 try
523 {
524 // Force a getProperty call. If sensor is on D-Bus,
525 // then make sure it's functional.
526 sensor->updateTachAndTarget();
527
528 _numSensorsOnDBusAtPowerOn++;
529
530 if (_present)
531 {
532 // If not functional, set it back to functional.
533 if (!sensor->functional())
534 {
535 sensor->setFunctional(true);
536 _system.fanStatusChange(*this, true);
537 }
538
539 // Set the counters back to zero
540 if (sensor->getMethod() == MethodMode::count)
541 {
542 sensor->resetMethod();
543 }
544 }
545 }
546 catch (const util::DBusError& e)
547 {
548 // Properties still aren't on D-Bus. Let startMonitor()
549 // deal with it, or maybe System::powerStateChanged() if
550 // there aren't any sensors at all on D-Bus.
Patrick Williamsfbf47032023-07-17 12:27:34 -0500551 getLogger().log(std::format(
Matt Spinlerbb449c12021-06-14 11:45:28 -0600552 "At power on, tach sensor {} value not on D-Bus",
553 sensor->name()));
554 }
555 });
556
Matt Spinler4283c5d2021-03-01 15:56:00 -0600557 if (_present)
558 {
Matt Spinler4283c5d2021-03-01 15:56:00 -0600559 // If configured to change functional state on the fan itself,
560 // Set it back to true now if necessary.
561 if (_numSensorFailsForNonFunc)
562 {
563 if (!_functional &&
564 (countNonFunctionalSensors() < _numSensorFailsForNonFunc))
565 {
566 updateInventory(true);
567 }
568 }
569 }
570 else
Matt Spinler7d135642021-02-04 12:44:17 -0600571 {
572 getLogger().log(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500573 std::format("At power on, fan {} is missing", _name));
Matt Spinler7d135642021-02-04 12:44:17 -0600574
575 if (_fanMissingErrorTimer)
576 {
577 _fanMissingErrorTimer->restartOnce(
578 std::chrono::seconds{*_fanMissingErrorDelay});
579 }
580 }
581 }
582 else
583 {
584 _monitorReady = false;
585
586 if (_monitorTimer.isEnabled())
587 {
588 _monitorTimer.setEnabled(false);
589 }
590
591 if (_fanMissingErrorTimer && _fanMissingErrorTimer->isEnabled())
592 {
593 _fanMissingErrorTimer->setEnabled(false);
594 }
595
596 std::for_each(_sensors.begin(), _sensors.end(), [](auto& sensor) {
597 if (sensor->timerRunning())
598 {
599 sensor->stopTimer();
600 }
Matt Spinler623635c2021-03-29 13:13:59 -0500601
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600602 sensor->stopCountTimer();
603 });
Matt Spinler7d135642021-02-04 12:44:17 -0600604 }
605#endif
606}
607
Matthew Barth177fe982020-05-26 11:05:19 -0500608} // namespace monitor
609} // namespace fan
610} // namespace phosphor