blob: 6365d41f0b9bf3cb576615baeb1be2ee0d0bf504 [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
Patrick Williams61b73292023-05-10 07:50:12 -050026 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 });
Matthew Barth03aff082018-12-12 15:20:22 -060041
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
Matthew Barth3e1bb272020-05-26 11:09:21 -050048 std::for_each(sse.begin(), sse.end(), [&zone](auto const& entry) {
49 zone.initEvent(entry);
50 });
Matthew Barth03aff082018-12-12 15:20:22 -060051 }
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
Matthew Barth3e1bb272020-05-26 11:09:21 -050058 std::for_each(sse.begin(), sse.end(), [&zone](auto const& entry) {
59 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 Williams5e15c3b2023-10-20 11:18:11 -050074 auto precondState = std::any_of(
75 services.begin(), services.end(),
76 [](const auto& s) { return !std::get<hasOwnerPos>(s); });
Matthew Barth36cfcec2018-12-14 10:18:53 -060077
78 if (precondState)
79 {
80 // Init the events when all the precondition(s) are true
Matthew Barth3e1bb272020-05-26 11:09:21 -050081 std::for_each(sse.begin(), sse.end(), [&zone](auto const& entry) {
82 zone.initEvent(entry);
83 });
Matthew Barth36cfcec2018-12-14 10:18:53 -060084 }
85 else
86 {
87 // Unsubscribe the events' signals when any precondition is false
Matthew Barth3e1bb272020-05-26 11:09:21 -050088 std::for_each(sse.begin(), sse.end(), [&zone](auto const& entry) {
89 zone.removeEvent(entry);
90 });
Matthew Barth36cfcec2018-12-14 10:18:53 -060091 }
92 };
93}
94
Matthew Barth03aff082018-12-12 15:20:22 -060095} // namespace precondition
96} // namespace control
97} // namespace fan
98} // namespace phosphor