blob: 4a025a74c6a030c063e35ab3d53565820fb52ccf [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
Jay Meyerae226192020-10-15 15:33:17 -050021#include <fmt/format.h>
22
Matthew Barth2d2caa32020-05-26 11:07:24 -050023#include <phosphor-logging/log.hpp>
24
25#include <algorithm>
26
Brad Bishopbfb81602017-06-14 21:14:32 -040027namespace phosphor
28{
29namespace fan
30{
31namespace presence
32{
33
Brad Bishop11083ec2017-07-25 19:08:53 -040034void Fallback::stateChanged(bool present, PresenceSensor& sensor)
Brad Bishopbfb81602017-06-14 21:14:32 -040035{
36 if (!present)
37 {
38 // Starting with the first backup, find the first
39 // sensor that reports the fan as present, if any.
Matthew Barth2d2caa32020-05-26 11:07:24 -050040 auto it = std::find_if(std::next(activeSensor), sensors.end(),
41 [](auto& s) { return s.get().present(); });
Brad Bishopbfb81602017-06-14 21:14:32 -040042
43 if (it != sensors.end())
44 {
45 // A backup sensor disagrees with the active sensor.
46 // Switch to the backup.
47 activeSensor->get().stop();
48 present = it->get().start();
49
50 while (activeSensor != it)
51 {
52 // Callout the broken sensors.
53 activeSensor->get().fail();
54 ++activeSensor;
55 }
56 phosphor::logging::log<phosphor::logging::level::INFO>(
Jay Meyerae226192020-10-15 15:33:17 -050057 fmt::format("Using backup presence sensor for fan {}",
58 std::get<1>(fan))
59 .c_str());
Brad Bishopbfb81602017-06-14 21:14:32 -040060 activeSensor = it;
61 }
62 }
63
64 setPresence(fan, present);
65}
66
67void Fallback::monitor()
68{
69 // Find the first sensor that says the fan is present
70 // and set it as the active sensor.
Matthew Barth2d2caa32020-05-26 11:07:24 -050071 activeSensor = std::find_if(sensors.begin(), sensors.end(),
72 [](auto& s) { return s.get().present(); });
Brad Bishopbfb81602017-06-14 21:14:32 -040073 if (activeSensor == sensors.end())
74 {
75 // The first sensor is working or all sensors
76 // agree the fan isn't present. Use the first sensor.
77 activeSensor = sensors.begin();
78 }
79
80 if (activeSensor != sensors.begin())
81 {
82 phosphor::logging::log<phosphor::logging::level::INFO>(
Jay Meyerae226192020-10-15 15:33:17 -050083 fmt::format("Using backup presence sensor for fan {}",
84 std::get<1>(fan))
85 .c_str());
Brad Bishopbfb81602017-06-14 21:14:32 -040086 }
87
88 // Callout the broken sensors.
89 auto it = sensors.begin();
90 while (it != activeSensor)
91 {
92 it->get().fail();
93 ++it;
94 }
95
96 // Start the active sensor and set the initial state.
97 setPresence(fan, activeSensor->get().start());
98}
99
100} // namespace presence
101} // namespace fan
102} // namespace phosphor