blob: f989f0d4ab2d75b49d1568e6119852eae7f275f4 [file] [log] [blame]
Matthew Barthccc77702017-07-28 13:43:04 -05001#pragma once
2
Matthew Barth604329e2017-08-04 11:18:28 -05003#include <algorithm>
4
Matthew Barthccc77702017-07-28 13:43:04 -05005namespace phosphor
6{
7namespace fan
8{
9namespace control
10{
11namespace precondition
12{
13
14/**
15 * @brief A precondition to compare a group of property values and
16 * subscribe/unsubscribe a set speed event group
17 * @details Compares each entry within the precondition group to a given value
18 * that when each entry's property value matches the given value, the set speed
19 * event is then initialized. At any point a precondition entry's value no
20 * longer matches, the set speed event is removed from being active and fans
21 * are set to full speed.
22 *
23 * @param[in] pg - Precondition property group of property values
24 * @param[in] sse - Set speed event definition
25 *
26 * @return Lambda function
27 * A lambda function to compare precondition property value states
28 * and either subscribe or unsubscribe a set speed event group.
29 */
30auto property_states_match(std::vector<PrecondGroup>&& pg,
Matthew Barthf9201ab2017-09-11 16:07:58 -050031 std::vector<SetSpeedEvent>&& sse)
Matthew Barthccc77702017-07-28 13:43:04 -050032{
33 return [pg = std::move(pg),
34 sse = std::move(sse)](auto& zone, auto& group)
35 {
Matthew Barth604329e2017-08-04 11:18:28 -050036 // Compare given precondition entries
37 size_t precondState = std::count_if(
38 pg.begin(),
39 pg.end(),
40 [&zone](auto const& entry)
41 {
42 try
43 {
44 return zone.getPropValueVariant(
45 std::get<pcPathPos>(entry),
46 std::get<pcIntfPos>(entry),
47 std::get<pcPropPos>(entry)) ==
48 std::get<pcValuePos>(entry);
49 }
50 catch (const std::out_of_range& oore)
51 {
52 // Default to property variants not equal when not found
53 return false;
54 }
55 });
56
Matthew Barth604329e2017-08-04 11:18:28 -050057 if (precondState == pg.size())
58 {
Matthew Barthf9201ab2017-09-11 16:07:58 -050059 // Init the events when all the precondition(s) are true
60 std::for_each(
61 sse.begin(),
62 sse.end(),
63 [&zone](auto const& entry)
64 {
65 zone.initEvent(entry);
66 });
Matthew Barth604329e2017-08-04 11:18:28 -050067 }
68 else
69 {
Matthew Barthf9201ab2017-09-11 16:07:58 -050070 // Unsubscribe the events' signals when any precondition is false
71 std::for_each(
72 sse.begin(),
73 sse.end(),
74 [&zone](auto const& entry)
75 {
76 zone.removeEvent(entry);
77 });
Matthew Barth60b00762017-08-15 13:39:06 -050078 zone.setFullSpeed();
Matthew Barth604329e2017-08-04 11:18:28 -050079 }
Matthew Barth60b00762017-08-15 13:39:06 -050080 // Update group's fan control active allowed
81 zone.setActiveAllow(&group, (precondState == pg.size()));
Matthew Barthccc77702017-07-28 13:43:04 -050082 };
83}
84
85} // namespace precondition
86} // namespace control
87} // namespace fan
88} // namespace phosphor