blob: 726c988cf57cbdd23dfb64c8f332a0b85dddd453 [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>
24
Brad Bishopbfb81602017-06-14 21:14:32 -040025namespace phosphor
26{
27namespace fan
28{
29namespace presence
30{
31
Brad Bishop11083ec2017-07-25 19:08:53 -040032void Fallback::stateChanged(bool present, PresenceSensor& sensor)
Brad Bishopbfb81602017-06-14 21:14:32 -040033{
34 if (!present)
35 {
36 // Starting with the first backup, find the first
37 // sensor that reports the fan as present, if any.
Matthew Barth2d2caa32020-05-26 11:07:24 -050038 auto it = std::find_if(std::next(activeSensor), sensors.end(),
39 [](auto& s) { return s.get().present(); });
Brad Bishopbfb81602017-06-14 21:14:32 -040040
41 if (it != sensors.end())
42 {
43 // A backup sensor disagrees with the active sensor.
44 // Switch to the backup.
45 activeSensor->get().stop();
46 present = it->get().start();
47
48 while (activeSensor != it)
49 {
50 // Callout the broken sensors.
51 activeSensor->get().fail();
52 ++activeSensor;
53 }
54 phosphor::logging::log<phosphor::logging::level::INFO>(
Matthew Barth2d2caa32020-05-26 11:07:24 -050055 "Using backup presence sensor.",
56 phosphor::logging::entry("FAN=%s", std::get<1>(fan).c_str()));
Brad Bishopbfb81602017-06-14 21:14:32 -040057 activeSensor = it;
58 }
59 }
60
61 setPresence(fan, present);
62}
63
64void Fallback::monitor()
65{
66 // Find the first sensor that says the fan is present
67 // and set it as the active sensor.
Matthew Barth2d2caa32020-05-26 11:07:24 -050068 activeSensor = std::find_if(sensors.begin(), sensors.end(),
69 [](auto& s) { return s.get().present(); });
Brad Bishopbfb81602017-06-14 21:14:32 -040070 if (activeSensor == sensors.end())
71 {
72 // The first sensor is working or all sensors
73 // agree the fan isn't present. Use the first sensor.
74 activeSensor = sensors.begin();
75 }
76
77 if (activeSensor != sensors.begin())
78 {
79 phosphor::logging::log<phosphor::logging::level::INFO>(
Matthew Barth2d2caa32020-05-26 11:07:24 -050080 "Using backup presence sensor.",
81 phosphor::logging::entry("FAN=%s", std::get<1>(fan).c_str()));
Brad Bishopbfb81602017-06-14 21:14:32 -040082 }
83
84 // Callout the broken sensors.
85 auto it = sensors.begin();
86 while (it != activeSensor)
87 {
88 it->get().fail();
89 ++it;
90 }
91
92 // Start the active sensor and set the initial state.
93 setPresence(fan, activeSensor->get().start());
94}
95
96} // namespace presence
97} // namespace fan
98} // namespace phosphor