Matthew Barth | ccc7770 | 2017-07-28 13:43:04 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Matthew Barth | 604329e | 2017-08-04 11:18:28 -0500 | [diff] [blame] | 3 | #include <algorithm> |
Matthew Barth | 572d406 | 2018-05-08 14:16:04 -0500 | [diff] [blame] | 4 | #include <phosphor-logging/log.hpp> |
Matthew Barth | 604329e | 2017-08-04 11:18:28 -0500 | [diff] [blame] | 5 | |
Matthew Barth | ccc7770 | 2017-07-28 13:43:04 -0500 | [diff] [blame] | 6 | namespace phosphor |
| 7 | { |
| 8 | namespace fan |
| 9 | { |
| 10 | namespace control |
| 11 | { |
| 12 | namespace precondition |
| 13 | { |
| 14 | |
Matthew Barth | 572d406 | 2018-05-08 14:16:04 -0500 | [diff] [blame] | 15 | using namespace phosphor::logging; |
| 16 | |
Matthew Barth | ccc7770 | 2017-07-28 13:43:04 -0500 | [diff] [blame] | 17 | /** |
| 18 | * @brief A precondition to compare a group of property values and |
| 19 | * subscribe/unsubscribe a set speed event group |
| 20 | * @details Compares each entry within the precondition group to a given value |
| 21 | * that when each entry's property value matches the given value, the set speed |
| 22 | * event is then initialized. At any point a precondition entry's value no |
| 23 | * longer matches, the set speed event is removed from being active and fans |
| 24 | * are set to full speed. |
| 25 | * |
| 26 | * @param[in] pg - Precondition property group of property values |
| 27 | * @param[in] sse - Set speed event definition |
| 28 | * |
| 29 | * @return Lambda function |
| 30 | * A lambda function to compare precondition property value states |
| 31 | * and either subscribe or unsubscribe a set speed event group. |
| 32 | */ |
| 33 | auto property_states_match(std::vector<PrecondGroup>&& pg, |
Matthew Barth | f9201ab | 2017-09-11 16:07:58 -0500 | [diff] [blame] | 34 | std::vector<SetSpeedEvent>&& sse) |
Matthew Barth | ccc7770 | 2017-07-28 13:43:04 -0500 | [diff] [blame] | 35 | { |
| 36 | return [pg = std::move(pg), |
| 37 | sse = std::move(sse)](auto& zone, auto& group) |
| 38 | { |
Matthew Barth | 604329e | 2017-08-04 11:18:28 -0500 | [diff] [blame] | 39 | // Compare given precondition entries |
Matthew Barth | 572d406 | 2018-05-08 14:16:04 -0500 | [diff] [blame] | 40 | auto precondState = std::all_of( |
Matthew Barth | 604329e | 2017-08-04 11:18:28 -0500 | [diff] [blame] | 41 | pg.begin(), |
| 42 | pg.end(), |
| 43 | [&zone](auto const& entry) |
| 44 | { |
| 45 | try |
| 46 | { |
| 47 | return zone.getPropValueVariant( |
| 48 | std::get<pcPathPos>(entry), |
| 49 | std::get<pcIntfPos>(entry), |
| 50 | std::get<pcPropPos>(entry)) == |
| 51 | std::get<pcValuePos>(entry); |
| 52 | } |
| 53 | catch (const std::out_of_range& oore) |
| 54 | { |
| 55 | // Default to property variants not equal when not found |
| 56 | return false; |
| 57 | } |
| 58 | }); |
| 59 | |
Matthew Barth | 572d406 | 2018-05-08 14:16:04 -0500 | [diff] [blame] | 60 | if (precondState) |
Matthew Barth | 604329e | 2017-08-04 11:18:28 -0500 | [diff] [blame] | 61 | { |
Matthew Barth | 572d406 | 2018-05-08 14:16:04 -0500 | [diff] [blame] | 62 | log<level::DEBUG>( |
| 63 | "Preconditions passed, init the associated events", |
| 64 | entry("EVENT_COUNT=%u", sse.size())); |
Matthew Barth | f9201ab | 2017-09-11 16:07:58 -0500 | [diff] [blame] | 65 | // Init the events when all the precondition(s) are true |
| 66 | std::for_each( |
| 67 | sse.begin(), |
| 68 | sse.end(), |
| 69 | [&zone](auto const& entry) |
| 70 | { |
| 71 | zone.initEvent(entry); |
| 72 | }); |
Matthew Barth | 604329e | 2017-08-04 11:18:28 -0500 | [diff] [blame] | 73 | } |
| 74 | else |
| 75 | { |
Matthew Barth | 572d406 | 2018-05-08 14:16:04 -0500 | [diff] [blame] | 76 | log<level::DEBUG>( |
| 77 | "Preconditions not met for events, events removed if present", |
| 78 | entry("EVENT_COUNT=%u", sse.size())); |
Matthew Barth | f9201ab | 2017-09-11 16:07:58 -0500 | [diff] [blame] | 79 | // Unsubscribe the events' signals when any precondition is false |
| 80 | std::for_each( |
| 81 | sse.begin(), |
| 82 | sse.end(), |
| 83 | [&zone](auto const& entry) |
| 84 | { |
| 85 | zone.removeEvent(entry); |
| 86 | }); |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 87 | zone.setFullSpeed(); |
Matthew Barth | 604329e | 2017-08-04 11:18:28 -0500 | [diff] [blame] | 88 | } |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 89 | // Update group's fan control active allowed |
Matthew Barth | 572d406 | 2018-05-08 14:16:04 -0500 | [diff] [blame] | 90 | zone.setActiveAllow(&group, precondState); |
Matthew Barth | ccc7770 | 2017-07-28 13:43:04 -0500 | [diff] [blame] | 91 | }; |
| 92 | } |
| 93 | |
| 94 | } // namespace precondition |
| 95 | } // namespace control |
| 96 | } // namespace fan |
| 97 | } // namespace phosphor |