Brad Bishop | 00b5208 | 2017-07-25 19:52:22 -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 | 00b5208 | 2017-07-25 19:52:22 -0400 | [diff] [blame] | 16 | #include "anyof.hpp" |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 17 | |
Brad Bishop | 00b5208 | 2017-07-25 19:52:22 -0400 | [diff] [blame] | 18 | #include "fan.hpp" |
| 19 | #include "psensor.hpp" |
| 20 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 21 | #include <phosphor-logging/log.hpp> |
| 22 | |
| 23 | #include <algorithm> |
| 24 | |
Brad Bishop | 00b5208 | 2017-07-25 19:52:22 -0400 | [diff] [blame] | 25 | namespace phosphor |
| 26 | { |
| 27 | namespace fan |
| 28 | { |
| 29 | namespace presence |
| 30 | { |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 31 | AnyOf::AnyOf(const Fan& fan, |
| 32 | const std::vector<std::reference_wrapper<PresenceSensor>>& s) : |
| 33 | RedundancyPolicy(fan), |
| 34 | state() |
Brad Bishop | 00b5208 | 2017-07-25 19:52:22 -0400 | [diff] [blame] | 35 | { |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 36 | for (auto& sensor : s) |
Brad Bishop | 00b5208 | 2017-07-25 19:52:22 -0400 | [diff] [blame] | 37 | { |
| 38 | state.emplace_back(sensor, false); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | void AnyOf::stateChanged(bool present, PresenceSensor& sensor) |
| 43 | { |
| 44 | // Find the sensor that changed state. |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 45 | auto sit = |
| 46 | std::find_if(state.begin(), state.end(), [&sensor](const auto& s) { |
| 47 | return std::get<0>(s).get() == sensor; |
| 48 | }); |
Brad Bishop | 00b5208 | 2017-07-25 19:52:22 -0400 | [diff] [blame] | 49 | if (sit != state.end()) |
| 50 | { |
| 51 | // Update our cache of the sensors state and re-evaluate. |
| 52 | std::get<bool>(*sit) = present; |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 53 | auto newState = |
| 54 | std::any_of(state.begin(), state.end(), |
| 55 | [](const auto& s) { return std::get<bool>(s); }); |
Brad Bishop | 00b5208 | 2017-07-25 19:52:22 -0400 | [diff] [blame] | 56 | setPresence(fan, newState); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void AnyOf::monitor() |
| 61 | { |
| 62 | // Start all sensors in the anyof redundancy set. |
| 63 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 64 | for (auto& s : state) |
Brad Bishop | 00b5208 | 2017-07-25 19:52:22 -0400 | [diff] [blame] | 65 | { |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 66 | auto& sensor = std::get<0>(s); |
Brad Bishop | 00b5208 | 2017-07-25 19:52:22 -0400 | [diff] [blame] | 67 | std::get<bool>(s) = sensor.get().start(); |
| 68 | } |
| 69 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 70 | auto present = std::any_of(state.begin(), state.end(), |
| 71 | [](const auto& s) { return std::get<bool>(s); }); |
Brad Bishop | 00b5208 | 2017-07-25 19:52:22 -0400 | [diff] [blame] | 72 | setPresence(fan, present); |
| 73 | } |
| 74 | |
| 75 | } // namespace presence |
| 76 | } // namespace fan |
| 77 | } // namespace phosphor |