blob: b77d6d78d5b31216bd08dffb597e171ab3ca1035 [file] [log] [blame]
Brad Bishop00b52082017-07-25 19:52:22 -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 Bishop00b52082017-07-25 19:52:22 -040016#include "anyof.hpp"
Matthew Barth2d2caa32020-05-26 11:07:24 -050017
Brad Bishop00b52082017-07-25 19:52:22 -040018#include "fan.hpp"
19#include "psensor.hpp"
20
Matthew Barth2d2caa32020-05-26 11:07:24 -050021#include <phosphor-logging/log.hpp>
22
23#include <algorithm>
24
Brad Bishop00b52082017-07-25 19:52:22 -040025namespace phosphor
26{
27namespace fan
28{
29namespace presence
30{
Matthew Barth2d2caa32020-05-26 11:07:24 -050031AnyOf::AnyOf(const Fan& fan,
32 const std::vector<std::reference_wrapper<PresenceSensor>>& s) :
33 RedundancyPolicy(fan),
34 state()
Brad Bishop00b52082017-07-25 19:52:22 -040035{
Matthew Barth2d2caa32020-05-26 11:07:24 -050036 for (auto& sensor : s)
Brad Bishop00b52082017-07-25 19:52:22 -040037 {
38 state.emplace_back(sensor, false);
39 }
40}
41
42void AnyOf::stateChanged(bool present, PresenceSensor& sensor)
43{
44 // Find the sensor that changed state.
Matthew Barth2d2caa32020-05-26 11:07:24 -050045 auto sit =
46 std::find_if(state.begin(), state.end(), [&sensor](const auto& s) {
47 return std::get<0>(s).get() == sensor;
48 });
Brad Bishop00b52082017-07-25 19:52:22 -040049 if (sit != state.end())
50 {
51 // Update our cache of the sensors state and re-evaluate.
52 std::get<bool>(*sit) = present;
Matthew Barth2d2caa32020-05-26 11:07:24 -050053 auto newState =
54 std::any_of(state.begin(), state.end(),
55 [](const auto& s) { return std::get<bool>(s); });
Brad Bishop00b52082017-07-25 19:52:22 -040056 setPresence(fan, newState);
57 }
58}
59
60void AnyOf::monitor()
61{
62 // Start all sensors in the anyof redundancy set.
63
Matthew Barth2d2caa32020-05-26 11:07:24 -050064 for (auto& s : state)
Brad Bishop00b52082017-07-25 19:52:22 -040065 {
Matthew Barth2d2caa32020-05-26 11:07:24 -050066 auto& sensor = std::get<0>(s);
Brad Bishop00b52082017-07-25 19:52:22 -040067 std::get<bool>(s) = sensor.get().start();
68 }
69
Matthew Barth2d2caa32020-05-26 11:07:24 -050070 auto present = std::any_of(state.begin(), state.end(),
71 [](const auto& s) { return std::get<bool>(s); });
Brad Bishop00b52082017-07-25 19:52:22 -040072 setPresence(fan, present);
73}
74
75} // namespace presence
76} // namespace fan
77} // namespace phosphor