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 | |
Jay Meyer | ae22619 | 2020-10-15 15:33:17 -0500 | [diff] [blame] | 21 | #include <fmt/format.h> |
| 22 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 23 | #include <phosphor-logging/log.hpp> |
| 24 | |
| 25 | #include <algorithm> |
| 26 | |
Brad Bishop | bfb8160 | 2017-06-14 21:14:32 -0400 | [diff] [blame] | 27 | namespace phosphor |
| 28 | { |
| 29 | namespace fan |
| 30 | { |
| 31 | namespace presence |
| 32 | { |
| 33 | |
Mike Capps | 808d7fe | 2022-06-13 10:12:16 -0400 | [diff] [blame] | 34 | void Fallback::stateChanged(bool present, PresenceSensor& /*sensor*/) |
Brad Bishop | bfb8160 | 2017-06-14 21:14:32 -0400 | [diff] [blame] | 35 | { |
| 36 | if (!present) |
| 37 | { |
| 38 | // Starting with the first backup, find the first |
| 39 | // sensor that reports the fan as present, if any. |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 40 | auto it = std::find_if(std::next(activeSensor), sensors.end(), |
| 41 | [](auto& s) { return s.get().present(); }); |
Brad Bishop | bfb8160 | 2017-06-14 21:14:32 -0400 | [diff] [blame] | 42 | |
| 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 Meyer | ae22619 | 2020-10-15 15:33:17 -0500 | [diff] [blame] | 57 | fmt::format("Using backup presence sensor for fan {}", |
| 58 | std::get<1>(fan)) |
| 59 | .c_str()); |
Brad Bishop | bfb8160 | 2017-06-14 21:14:32 -0400 | [diff] [blame] | 60 | activeSensor = it; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | setPresence(fan, present); |
Matt Spinler | bc4179e | 2022-10-04 15:15:06 -0500 | [diff] [blame^] | 65 | |
| 66 | if (eepromDevice) |
| 67 | { |
| 68 | if (present) |
| 69 | { |
| 70 | eepromDevice->bind(); |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | eepromDevice->unbind(); |
| 75 | } |
| 76 | } |
Brad Bishop | bfb8160 | 2017-06-14 21:14:32 -0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void Fallback::monitor() |
| 80 | { |
| 81 | // Find the first sensor that says the fan is present |
| 82 | // and set it as the active sensor. |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 83 | activeSensor = std::find_if(sensors.begin(), sensors.end(), |
| 84 | [](auto& s) { return s.get().present(); }); |
Brad Bishop | bfb8160 | 2017-06-14 21:14:32 -0400 | [diff] [blame] | 85 | 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>( |
Jay Meyer | ae22619 | 2020-10-15 15:33:17 -0500 | [diff] [blame] | 95 | fmt::format("Using backup presence sensor for fan {}", |
| 96 | std::get<1>(fan)) |
| 97 | .c_str()); |
Brad Bishop | bfb8160 | 2017-06-14 21:14:32 -0400 | [diff] [blame] | 98 | } |
| 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 |