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