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