blob: b3fc54a86d3b1488dfa53215d53a265f787cb0c9 [file] [log] [blame]
Brad Bishopbfb81602017-06-14 21:14:32 -04001/**
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 */
Brad Bishopbfb81602017-06-14 21:14:32 -040016#include "fallback.hpp"
Matthew Barth2d2caa32020-05-26 11:07:24 -050017
18#include "fan.hpp"
Brad Bishopbfb81602017-06-14 21:14:32 -040019#include "psensor.hpp"
20
Matthew Barth2d2caa32020-05-26 11:07:24 -050021#include <phosphor-logging/log.hpp>
22
23#include <algorithm>
Patrick Williamsfbf47032023-07-17 12:27:34 -050024#include <format>
Matthew Barth2d2caa32020-05-26 11:07:24 -050025
Brad Bishopbfb81602017-06-14 21:14:32 -040026namespace phosphor
27{
28namespace fan
29{
30namespace presence
31{
32
Mike Capps808d7fe2022-06-13 10:12:16 -040033void Fallback::stateChanged(bool present, PresenceSensor& /*sensor*/)
Brad Bishopbfb81602017-06-14 21:14:32 -040034{
35 if (!present)
36 {
37 // Starting with the first backup, find the first
38 // sensor that reports the fan as present, if any.
Matthew Barth2d2caa32020-05-26 11:07:24 -050039 auto it = std::find_if(std::next(activeSensor), sensors.end(),
40 [](auto& s) { return s.get().present(); });
Brad Bishopbfb81602017-06-14 21:14:32 -040041
42 if (it != sensors.end())
43 {
44 // A backup sensor disagrees with the active sensor.
45 // Switch to the backup.
46 activeSensor->get().stop();
47 present = it->get().start();
48
49 while (activeSensor != it)
50 {
51 // Callout the broken sensors.
52 activeSensor->get().fail();
53 ++activeSensor;
54 }
55 phosphor::logging::log<phosphor::logging::level::INFO>(
Patrick Williamsfbf47032023-07-17 12:27:34 -050056 std::format("Using backup presence sensor for fan {}",
Jay Meyerae226192020-10-15 15:33:17 -050057 std::get<1>(fan))
58 .c_str());
Brad Bishopbfb81602017-06-14 21:14:32 -040059 activeSensor = it;
60 }
61 }
62
63 setPresence(fan, present);
Matt Spinlerbc4179e2022-10-04 15:15:06 -050064
65 if (eepromDevice)
66 {
67 if (present)
68 {
69 eepromDevice->bind();
70 }
71 else
72 {
73 eepromDevice->unbind();
74 }
75 }
Brad Bishopbfb81602017-06-14 21:14:32 -040076}
77
78void Fallback::monitor()
79{
80 // Find the first sensor that says the fan is present
81 // and set it as the active sensor.
Patrick Williamsdfddd642024-08-16 15:21:51 -040082 activeSensor = std::find_if(sensors.begin(), sensors.end(), [](auto& s) {
83 return s.get().present();
84 });
Brad Bishopbfb81602017-06-14 21:14:32 -040085 if (activeSensor == sensors.end())
86 {
87 // The first sensor is working or all sensors
88 // agree the fan isn't present. Use the first sensor.
89 activeSensor = sensors.begin();
90 }
91
92 if (activeSensor != sensors.begin())
93 {
94 phosphor::logging::log<phosphor::logging::level::INFO>(
Patrick Williamsfbf47032023-07-17 12:27:34 -050095 std::format("Using backup presence sensor for fan {}",
Jay Meyerae226192020-10-15 15:33:17 -050096 std::get<1>(fan))
97 .c_str());
Brad Bishopbfb81602017-06-14 21:14:32 -040098 }
99
100 // Callout the broken sensors.
101 auto it = sensors.begin();
102 while (it != activeSensor)
103 {
104 it->get().fail();
105 ++it;
106 }
107
108 // Start the active sensor and set the initial state.
109 setPresence(fan, activeSensor->get().start());
110}
111
112} // namespace presence
113} // namespace fan
114} // namespace phosphor