Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2020 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
| 17 | |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 18 | #include "action.hpp" |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 19 | #include "config_base.hpp" |
Matthew Barth | 391ade0 | 2021-01-15 14:33:21 -0600 | [diff] [blame] | 20 | #include "group.hpp" |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 21 | |
| 22 | #include <nlohmann/json.hpp> |
| 23 | #include <sdbusplus/bus.hpp> |
| 24 | |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 25 | #include <memory> |
Matthew Barth | 12bae96 | 2021-01-15 16:18:11 -0600 | [diff] [blame] | 26 | #include <optional> |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 27 | #include <tuple> |
Matthew Barth | 12bae96 | 2021-01-15 16:18:11 -0600 | [diff] [blame] | 28 | |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 29 | namespace phosphor::fan::control::json |
| 30 | { |
| 31 | |
| 32 | using json = nlohmann::json; |
| 33 | |
| 34 | /** |
| 35 | * @class Event - Represents a configured fan control event |
| 36 | * |
| 37 | * Fan control events are optional, therefore the "events.json" file is |
| 38 | * also optional. An event object can be used to enable a specific change to |
| 39 | * how fan control should function. These events contain the configured |
| 40 | * attributes that result in how fans are controlled within a system. Events |
| 41 | * are made up of groups of sensors, triggers from those sensors, and actions |
| 42 | * to be run when a trigger occurs. Events may also have a precondition that |
| 43 | * must exist before the event is loaded into fan control. The triggers, |
| 44 | * actions, and preconditions configured must be available within the fan |
| 45 | * control application source. |
| 46 | * |
| 47 | * When no events exist, the configured fans are set to their corresponding |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 48 | * zone's `full_speed` value. |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 49 | */ |
| 50 | class Event : public ConfigBase |
| 51 | { |
| 52 | static constexpr auto precondName = 0; |
| 53 | static constexpr auto precondGroups = 1; |
| 54 | static constexpr auto precondEvents = 2; |
| 55 | using Precondition = |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 56 | std::tuple<std::string, |
| 57 | std::vector<std::tuple<std::string, std::string, std::string, |
| 58 | PropertyVariantType>>, |
| 59 | std::vector<Event>>; |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 60 | |
| 61 | public: |
| 62 | /* JSON file name for events */ |
| 63 | static constexpr auto confFileName = "events.json"; |
| 64 | |
| 65 | Event() = delete; |
| 66 | Event(const Event&) = delete; |
| 67 | Event(Event&&) = delete; |
| 68 | Event& operator=(const Event&) = delete; |
| 69 | Event& operator=(Event&&) = delete; |
| 70 | ~Event() = default; |
| 71 | |
| 72 | /** |
| 73 | * Constructor |
| 74 | * Parses and populates a configuration event from JSON object data |
| 75 | * |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 76 | * @param[in] jsonObj - JSON object |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 77 | * @param[in] bus - sdbusplus bus object |
| 78 | * @param[in] groups - Available groups that can be used |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 79 | */ |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 80 | Event(const json& jsonObj, sdbusplus::bus::bus& bus, |
| 81 | std::map<configKey, std::unique_ptr<Group>>& groups); |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * @brief Get the precondition |
| 85 | * |
| 86 | * @return The precondition details of the event |
| 87 | */ |
| 88 | inline const auto& getPrecond() const |
| 89 | { |
| 90 | return _precond; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @brief Get the groups |
| 95 | * |
| 96 | * @return List of groups associated with the event |
| 97 | */ |
| 98 | inline const auto& getGroups() const |
| 99 | { |
| 100 | return _groups; |
| 101 | } |
| 102 | |
| 103 | /** |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 104 | * @brief Get the actions |
| 105 | * |
| 106 | * @return List of actions to perform for the event |
| 107 | */ |
| 108 | inline const auto& getActions() const |
| 109 | { |
| 110 | return _actions; |
| 111 | } |
| 112 | |
| 113 | private: |
Matthew Barth | 391ade0 | 2021-01-15 14:33:21 -0600 | [diff] [blame] | 114 | /* The sdbusplus bus object */ |
| 115 | sdbusplus::bus::bus& _bus; |
| 116 | |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 117 | /* A precondition the event has in order to be enabled */ |
| 118 | Precondition _precond; |
| 119 | |
| 120 | /* List of groups associated with the event */ |
Matthew Barth | e557860 | 2021-03-30 12:53:24 -0500 | [diff] [blame] | 121 | std::vector<Group> _groups; |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 122 | |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 123 | /* List of actions for this event */ |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 124 | std::vector<std::unique_ptr<ActionBase>> _actions; |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 125 | |
| 126 | /** |
| 127 | * @brief Parse and set the event's precondition(OPTIONAL) |
| 128 | * |
| 129 | * @param[in] jsonObj - JSON object for the event |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 130 | * @param[in] groups - Available groups that can be used |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 131 | * |
| 132 | * Sets the precondition of the event in order to be enabled |
| 133 | */ |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 134 | void setPrecond(const json& jsonObj, |
| 135 | std::map<configKey, std::unique_ptr<Group>>& groups); |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 136 | |
| 137 | /** |
| 138 | * @brief Parse and set the event's groups(OPTIONAL) |
| 139 | * |
| 140 | * @param[in] jsonObj - JSON object for the event |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 141 | * @param[in] groups - Available groups that can be used |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 142 | * |
| 143 | * Sets the list of groups associated with the event |
| 144 | */ |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 145 | void setGroups(const json& jsonObj, |
| 146 | std::map<configKey, std::unique_ptr<Group>>& groups); |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 147 | |
| 148 | /** |
| 149 | * @brief Parse and set the event's triggers |
| 150 | * |
| 151 | * @param[in] jsonObj - JSON object for the event |
| 152 | * |
| 153 | * Sets the list of triggers for the event |
| 154 | */ |
| 155 | void setTriggers(const json& jsonObj); |
| 156 | |
| 157 | /** |
| 158 | * @brief Parse and set the event's actions(OPTIONAL) |
| 159 | * |
| 160 | * @param[in] jsonObj - JSON object for the event |
| 161 | * |
| 162 | * Sets the list of actions to perform for the event |
| 163 | */ |
| 164 | void setActions(const json& jsonObj); |
| 165 | }; |
| 166 | |
| 167 | } // namespace phosphor::fan::control::json |