blob: 9991f782539e84d2dc38a3edd11b8a56bc156c6d [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
Anwaar Hadi64b5ac22025-04-04 23:54:53 +00005#include <phosphor-logging/lg2.hpp>
Matthew Barth3e1bb272020-05-26 11:09:21 -05006
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;
19
20Action property_states_match(std::vector<PrecondGroup>&& pg,
21 std::vector<SetSpeedEvent>&& sse)
22{
Matthew Barth3e1bb272020-05-26 11:09:21 -050023 return [pg = std::move(pg), sse = std::move(sse)](auto& zone, auto& group) {
Matthew Barth03aff082018-12-12 15:20:22 -060024 // Compare given precondition entries
Patrick Williamsdfddd642024-08-16 15:21:51 -040025 auto precondState =
26 std::all_of(pg.begin(), pg.end(), [&zone](const auto& entry) {
27 try
28 {
29 return zone.getPropValueVariant(
30 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 });
Matthew Barth03aff082018-12-12 15:20:22 -060041
42 if (precondState)
43 {
Anwaar Hadi64b5ac22025-04-04 23:54:53 +000044 lg2::debug(
45 "Preconditions passed, init the associated events, Event_Count={EVENT_COUNT}",
46 "EVENT_COUNT", sse.size());
Matthew Barth03aff082018-12-12 15:20:22 -060047 // Init the events when all the precondition(s) are true
Patrick Williamsdfddd642024-08-16 15:21:51 -040048 std::for_each(sse.begin(), sse.end(), [&zone](const auto& entry) {
Matthew Barth3e1bb272020-05-26 11:09:21 -050049 zone.initEvent(entry);
50 });
Matthew Barth03aff082018-12-12 15:20:22 -060051 }
52 else
53 {
Anwaar Hadi64b5ac22025-04-04 23:54:53 +000054 lg2::debug(
55 "Preconditions not met for events, events removed if present, Event_Count={EVENT_COUNT}",
56 "EVENT_COUNT", sse.size());
Matthew Barth03aff082018-12-12 15:20:22 -060057 // Unsubscribe the events' signals when any precondition is false
Patrick Williamsdfddd642024-08-16 15:21:51 -040058 std::for_each(sse.begin(), sse.end(), [&zone](const auto& entry) {
Matthew Barth3e1bb272020-05-26 11:09:21 -050059 zone.removeEvent(entry);
60 });
Matthew Barth03aff082018-12-12 15:20:22 -060061 zone.setFullSpeed();
62 }
63 // Update group's fan control active allowed
64 zone.setActiveAllow(&group, precondState);
65 };
66}
67
Matthew Barth36cfcec2018-12-14 10:18:53 -060068Action services_missing_owner(std::vector<SetSpeedEvent>&& sse)
69{
Matthew Barth3e1bb272020-05-26 11:09:21 -050070 return [sse = std::move(sse)](auto& zone, auto& group) {
Matthew Barth36cfcec2018-12-14 10:18:53 -060071 // Set/update the services of the group
72 zone.setServices(&group);
73 const auto& services = zone.getGroupServices(&group);
Patrick Williamsdfddd642024-08-16 15:21:51 -040074 auto precondState =
75 std::any_of(services.begin(), services.end(), [](const auto& s) {
76 return !std::get<hasOwnerPos>(s);
77 });
Matthew Barth36cfcec2018-12-14 10:18:53 -060078
79 if (precondState)
80 {
81 // Init the events when all the precondition(s) are true
Patrick Williamsdfddd642024-08-16 15:21:51 -040082 std::for_each(sse.begin(), sse.end(), [&zone](const auto& entry) {
Matthew Barth3e1bb272020-05-26 11:09:21 -050083 zone.initEvent(entry);
84 });
Matthew Barth36cfcec2018-12-14 10:18:53 -060085 }
86 else
87 {
88 // Unsubscribe the events' signals when any precondition is false
Patrick Williamsdfddd642024-08-16 15:21:51 -040089 std::for_each(sse.begin(), sse.end(), [&zone](const auto& entry) {
Matthew Barth3e1bb272020-05-26 11:09:21 -050090 zone.removeEvent(entry);
91 });
Matthew Barth36cfcec2018-12-14 10:18:53 -060092 }
93 };
94}
95
Matthew Barth03aff082018-12-12 15:20:22 -060096} // namespace precondition
97} // namespace control
98} // namespace fan
99} // namespace phosphor