Brad Bishop | bfb8160 | 2017-06-14 21:14:32 -0400 | [diff] [blame] | 1 | /** |
| 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 Bishop | bfb8160 | 2017-06-14 21:14:32 -0400 | [diff] [blame] | 16 | #include "fallback.hpp" |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 17 | |
| 18 | #include "fan.hpp" |
Brad Bishop | bfb8160 | 2017-06-14 21:14:32 -0400 | [diff] [blame] | 19 | #include "psensor.hpp" |
| 20 | |
Anwaar Hadi | ebead9a | 2025-03-19 20:35:57 +0000 | [diff] [blame^] | 21 | #include <phosphor-logging/lg2.hpp> |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 22 | |
| 23 | #include <algorithm> |
| 24 | |
Brad Bishop | bfb8160 | 2017-06-14 21:14:32 -0400 | [diff] [blame] | 25 | namespace phosphor |
| 26 | { |
| 27 | namespace fan |
| 28 | { |
| 29 | namespace presence |
| 30 | { |
| 31 | |
Mike Capps | 808d7fe | 2022-06-13 10:12:16 -0400 | [diff] [blame] | 32 | void Fallback::stateChanged(bool present, PresenceSensor& /*sensor*/) |
Brad Bishop | bfb8160 | 2017-06-14 21:14:32 -0400 | [diff] [blame] | 33 | { |
| 34 | if (!present) |
| 35 | { |
| 36 | // Starting with the first backup, find the first |
| 37 | // sensor that reports the fan as present, if any. |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 38 | auto it = std::find_if(std::next(activeSensor), sensors.end(), |
| 39 | [](auto& s) { return s.get().present(); }); |
Brad Bishop | bfb8160 | 2017-06-14 21:14:32 -0400 | [diff] [blame] | 40 | |
| 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 | } |
Anwaar Hadi | ebead9a | 2025-03-19 20:35:57 +0000 | [diff] [blame^] | 54 | lg2::info("Using backup presence sensor for fan {FAN}", "FAN", |
| 55 | std::get<1>(fan)); |
Brad Bishop | bfb8160 | 2017-06-14 21:14:32 -0400 | [diff] [blame] | 56 | activeSensor = it; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | setPresence(fan, present); |
Matt Spinler | bc4179e | 2022-10-04 15:15:06 -0500 | [diff] [blame] | 61 | |
| 62 | if (eepromDevice) |
| 63 | { |
| 64 | if (present) |
| 65 | { |
| 66 | eepromDevice->bind(); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | eepromDevice->unbind(); |
| 71 | } |
| 72 | } |
Brad Bishop | bfb8160 | 2017-06-14 21:14:32 -0400 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void Fallback::monitor() |
| 76 | { |
| 77 | // Find the first sensor that says the fan is present |
| 78 | // and set it as the active sensor. |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 79 | activeSensor = std::find_if(sensors.begin(), sensors.end(), [](auto& s) { |
| 80 | return s.get().present(); |
| 81 | }); |
Brad Bishop | bfb8160 | 2017-06-14 21:14:32 -0400 | [diff] [blame] | 82 | if (activeSensor == sensors.end()) |
| 83 | { |
| 84 | // The first sensor is working or all sensors |
| 85 | // agree the fan isn't present. Use the first sensor. |
| 86 | activeSensor = sensors.begin(); |
| 87 | } |
| 88 | |
| 89 | if (activeSensor != sensors.begin()) |
| 90 | { |
Anwaar Hadi | ebead9a | 2025-03-19 20:35:57 +0000 | [diff] [blame^] | 91 | lg2::info("Using backup presence sensor for fan {FAN}", "FAN", |
| 92 | std::get<1>(fan)); |
Brad Bishop | bfb8160 | 2017-06-14 21:14:32 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // Callout the broken sensors. |
| 96 | auto it = sensors.begin(); |
| 97 | while (it != activeSensor) |
| 98 | { |
| 99 | it->get().fail(); |
| 100 | ++it; |
| 101 | } |
| 102 | |
| 103 | // Start the active sensor and set the initial state. |
| 104 | setPresence(fan, activeSensor->get().start()); |
| 105 | } |
| 106 | |
| 107 | } // namespace presence |
| 108 | } // namespace fan |
| 109 | } // namespace phosphor |