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 "fan.hpp"
19 #include "fallback.hpp"
20 #include "psensor.hpp"
21 
22 namespace phosphor
23 {
24 namespace fan
25 {
26 namespace presence
27 {
28 
29 void Fallback::stateChanged(bool present, PresenceSensor& sensor)
30 {
31     if (!present)
32     {
33         // Starting with the first backup, find the first
34         // sensor that reports the fan as present, if any.
35         auto it = std::find_if(
36                 std::next(activeSensor),
37                 sensors.end(),
38                 [](auto& s){return s.get().present();});
39 
40         if (it != sensors.end())
41         {
42             // A backup sensor disagrees with the active sensor.
43             // Switch to the backup.
44             activeSensor->get().stop();
45             present = it->get().start();
46 
47             while (activeSensor != it)
48             {
49                 // Callout the broken sensors.
50                 activeSensor->get().fail();
51                 ++activeSensor;
52             }
53             phosphor::logging::log<phosphor::logging::level::INFO>(
54                     "Using backup presence sensor.",
55                     phosphor::logging::entry(
56                         "FAN=%s", std::get<1>(fan)));
57             activeSensor = it;
58         }
59     }
60 
61     setPresence(fan, present);
62 }
63 
64 void Fallback::monitor()
65 {
66     // Find the first sensor that says the fan is present
67     // and set it as the active sensor.
68     activeSensor = std::find_if(
69             sensors.begin(),
70             sensors.end(),
71             [](auto& s){return s.get().present();});
72     if (activeSensor == sensors.end())
73     {
74         // The first sensor is working or all sensors
75         // agree the fan isn't present.  Use the first sensor.
76         activeSensor = sensors.begin();
77     }
78 
79     if (activeSensor != sensors.begin())
80     {
81         phosphor::logging::log<phosphor::logging::level::INFO>(
82                 "Using backup presence sensor.",
83                 phosphor::logging::entry(
84                     "FAN=%s", std::get<1>(fan)));
85     }
86 
87     // Callout the broken sensors.
88     auto it = sensors.begin();
89     while (it != activeSensor)
90     {
91         it->get().fail();
92         ++it;
93     }
94 
95     // Start the active sensor and set the initial state.
96     setPresence(fan, activeSensor->get().start());
97 }
98 
99 } // namespace presence
100 } // namespace fan
101 } // namespace phosphor
102