blob: 09284a891c369529529eaa69dbe0b6357a1e9e0e [file] [log] [blame]
Matthew Barth03aff082018-12-12 15:20:22 -06001#include "preconditions.hpp"
Matthew Barth3e1bb272020-05-26 11:09:21 -05002
Matthew Barth03aff082018-12-12 15:20:22 -06003#include "zone.hpp"
4
Matthew Barth3e1bb272020-05-26 11:09:21 -05005#include <phosphor-logging/log.hpp>
6
7#include <algorithm>
8
Matthew Barth03aff082018-12-12 15:20:22 -06009namespace phosphor
10{
11namespace fan
12{
13namespace control
14{
15namespace precondition
16{
17
18using namespace phosphor::fan;
Matthew Barth3e1bb272020-05-26 11:09:21 -050019using namespace phosphor::logging;
Matthew Barth03aff082018-12-12 15:20:22 -060020
21Action property_states_match(std::vector<PrecondGroup>&& pg,
22 std::vector<SetSpeedEvent>&& sse)
23{
Matthew Barth3e1bb272020-05-26 11:09:21 -050024 return [pg = std::move(pg), sse = std::move(sse)](auto& zone, auto& group) {
Matthew Barth03aff082018-12-12 15:20:22 -060025 // Compare given precondition entries
Matthew Barth3e1bb272020-05-26 11:09:21 -050026 auto precondState =
27 std::all_of(pg.begin(), pg.end(), [&zone](auto const& entry) {
Matthew Barth03aff082018-12-12 15:20:22 -060028 try
29 {
30 return zone.getPropValueVariant(
Matthew Barth3e1bb272020-05-26 11:09:21 -050031 std::get<pcPathPos>(entry),
32 std::get<pcIntfPos>(entry),
33 std::get<pcPropPos>(entry)) ==
34 std::get<pcValuePos>(entry);
Matthew Barth03aff082018-12-12 15:20:22 -060035 }
36 catch (const std::out_of_range& oore)
37 {
38 // Default to property variants not equal when not found
39 return false;
40 }
41 });
42
43 if (precondState)
44 {
45 log<level::DEBUG>(
46 "Preconditions passed, init the associated events",
47 entry("EVENT_COUNT=%u", sse.size()));
48 // Init the events when all the precondition(s) are true
Matthew Barth3e1bb272020-05-26 11:09:21 -050049 std::for_each(sse.begin(), sse.end(), [&zone](auto const& entry) {
50 zone.initEvent(entry);
51 });
Matthew Barth03aff082018-12-12 15:20:22 -060052 }
53 else
54 {
55 log<level::DEBUG>(
56 "Preconditions not met for events, events removed if present",
57 entry("EVENT_COUNT=%u", sse.size()));
58 // Unsubscribe the events' signals when any precondition is false
Matthew Barth3e1bb272020-05-26 11:09:21 -050059 std::for_each(sse.begin(), sse.end(), [&zone](auto const& entry) {
60 zone.removeEvent(entry);
61 });
Matthew Barth03aff082018-12-12 15:20:22 -060062 zone.setFullSpeed();
63 }
64 // Update group's fan control active allowed
65 zone.setActiveAllow(&group, precondState);
66 };
67}
68
Matthew Barth36cfcec2018-12-14 10:18:53 -060069Action services_missing_owner(std::vector<SetSpeedEvent>&& sse)
70{
Matthew Barth3e1bb272020-05-26 11:09:21 -050071 return [sse = std::move(sse)](auto& zone, auto& group) {
Matthew Barth36cfcec2018-12-14 10:18:53 -060072 // Set/update the services of the group
73 zone.setServices(&group);
74 const auto& services = zone.getGroupServices(&group);
Matthew Barth3e1bb272020-05-26 11:09:21 -050075 auto precondState =
76 std::any_of(services.begin(), services.end(), [](const auto& s) {
Matthew Barth36cfcec2018-12-14 10:18:53 -060077 return !std::get<hasOwnerPos>(s);
78 });
79
80 if (precondState)
81 {
82 // Init the events when all the precondition(s) are true
Matthew Barth3e1bb272020-05-26 11:09:21 -050083 std::for_each(sse.begin(), sse.end(), [&zone](auto const& entry) {
84 zone.initEvent(entry);
85 });
Matthew Barth36cfcec2018-12-14 10:18:53 -060086 }
87 else
88 {
89 // Unsubscribe the events' signals when any precondition is false
Matthew Barth3e1bb272020-05-26 11:09:21 -050090 std::for_each(sse.begin(), sse.end(), [&zone](auto const& entry) {
91 zone.removeEvent(entry);
92 });
Matthew Barth36cfcec2018-12-14 10:18:53 -060093 }
94 };
95}
96
Matthew Barth03aff082018-12-12 15:20:22 -060097} // namespace precondition
98} // namespace control
99} // namespace fan
100} // namespace phosphor