xref: /openbmc/phosphor-fan-presence/control/preconditions.cpp (revision 3420426c93fe195e148b2ae9aefe2eed5afa2f47)
1 #include "preconditions.hpp"
2 
3 #include "zone.hpp"
4 
5 #include <phosphor-logging/log.hpp>
6 
7 #include <algorithm>
8 
9 namespace phosphor
10 {
11 namespace fan
12 {
13 namespace control
14 {
15 namespace precondition
16 {
17 
18 using namespace phosphor::fan;
19 using namespace phosphor::logging;
20 
21 Action property_states_match(std::vector<PrecondGroup>&& pg,
22                              std::vector<SetSpeedEvent>&& sse)
23 {
24     return [pg = std::move(pg), sse = std::move(sse)](auto& zone, auto& group) {
25         // Compare given precondition entries
26         auto precondState = std::all_of(pg.begin(), pg.end(),
27                                         [&zone](auto const& entry) {
28             try
29             {
30                 return zone.getPropValueVariant(std::get<pcPathPos>(entry),
31                                                 std::get<pcIntfPos>(entry),
32                                                 std::get<pcPropPos>(entry)) ==
33                        std::get<pcValuePos>(entry);
34             }
35             catch (const std::out_of_range& oore)
36             {
37                 // Default to property variants not equal when not found
38                 return false;
39             }
40         });
41 
42         if (precondState)
43         {
44             log<level::DEBUG>(
45                 "Preconditions passed, init the associated events",
46                 entry("EVENT_COUNT=%u", sse.size()));
47             // Init the events when all the precondition(s) are true
48             std::for_each(sse.begin(), sse.end(), [&zone](auto const& entry) {
49                 zone.initEvent(entry);
50             });
51         }
52         else
53         {
54             log<level::DEBUG>(
55                 "Preconditions not met for events, events removed if present",
56                 entry("EVENT_COUNT=%u", sse.size()));
57             // Unsubscribe the events' signals when any precondition is false
58             std::for_each(sse.begin(), sse.end(), [&zone](auto const& entry) {
59                 zone.removeEvent(entry);
60             });
61             zone.setFullSpeed();
62         }
63         // Update group's fan control active allowed
64         zone.setActiveAllow(&group, precondState);
65     };
66 }
67 
68 Action services_missing_owner(std::vector<SetSpeedEvent>&& sse)
69 {
70     return [sse = std::move(sse)](auto& zone, auto& group) {
71         // Set/update the services of the group
72         zone.setServices(&group);
73         const auto& services = zone.getGroupServices(&group);
74         auto precondState = std::any_of(
75             services.begin(), services.end(),
76             [](const auto& s) { return !std::get<hasOwnerPos>(s); });
77 
78         if (precondState)
79         {
80             // Init the events when all the precondition(s) are true
81             std::for_each(sse.begin(), sse.end(), [&zone](auto const& entry) {
82                 zone.initEvent(entry);
83             });
84         }
85         else
86         {
87             // Unsubscribe the events' signals when any precondition is false
88             std::for_each(sse.begin(), sse.end(), [&zone](auto const& entry) {
89                 zone.removeEvent(entry);
90             });
91         }
92     };
93 }
94 
95 } // namespace precondition
96 } // namespace control
97 } // namespace fan
98 } // namespace phosphor
99