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 | |
| 18 | #include "config_base.hpp" |
Matthew Barth | 391ade0 | 2021-01-15 14:33:21 -0600 | [diff] [blame] | 19 | #include "group.hpp" |
| 20 | #include "json_parser.hpp" |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 21 | #include "types.hpp" |
| 22 | |
| 23 | #include <nlohmann/json.hpp> |
| 24 | #include <sdbusplus/bus.hpp> |
| 25 | |
Matthew Barth | 12bae96 | 2021-01-15 16:18:11 -0600 | [diff] [blame] | 26 | #include <optional> |
| 27 | |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 28 | namespace phosphor::fan::control::json |
| 29 | { |
| 30 | |
| 31 | using json = nlohmann::json; |
| 32 | |
| 33 | /** |
| 34 | * @class Event - Represents a configured fan control event |
| 35 | * |
| 36 | * Fan control events are optional, therefore the "events.json" file is |
| 37 | * also optional. An event object can be used to enable a specific change to |
| 38 | * how fan control should function. These events contain the configured |
| 39 | * attributes that result in how fans are controlled within a system. Events |
| 40 | * are made up of groups of sensors, triggers from those sensors, and actions |
| 41 | * to be run when a trigger occurs. Events may also have a precondition that |
| 42 | * must exist before the event is loaded into fan control. The triggers, |
| 43 | * actions, and preconditions configured must be available within the fan |
| 44 | * control application source. |
| 45 | * |
| 46 | * When no events exist, the configured fans are set to their corresponding |
| 47 | * zone's full_speed value. |
| 48 | */ |
| 49 | class Event : public ConfigBase |
| 50 | { |
Matthew Barth | 12bae96 | 2021-01-15 16:18:11 -0600 | [diff] [blame] | 51 | static constexpr auto pathPos = 0; |
| 52 | static constexpr auto intfPos = 1; |
| 53 | static constexpr auto propPos = 2; |
| 54 | static constexpr auto typePos = 3; |
| 55 | static constexpr auto valuePos = 4; |
| 56 | using eGroup = std::vector<std::tuple<std::string, std::string, std::string, |
| 57 | std::optional<std::string>, |
| 58 | std::optional<PropertyVariantType>>>; |
| 59 | |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 60 | static constexpr auto precondName = 0; |
| 61 | static constexpr auto precondGroups = 1; |
| 62 | static constexpr auto precondEvents = 2; |
| 63 | using Precondition = |
| 64 | std::tuple<std::string, std::vector<PrecondGroup>, std::vector<Event>>; |
| 65 | |
| 66 | public: |
| 67 | /* JSON file name for events */ |
| 68 | static constexpr auto confFileName = "events.json"; |
| 69 | |
| 70 | Event() = delete; |
| 71 | Event(const Event&) = delete; |
| 72 | Event(Event&&) = delete; |
| 73 | Event& operator=(const Event&) = delete; |
| 74 | Event& operator=(Event&&) = delete; |
| 75 | ~Event() = default; |
| 76 | |
| 77 | /** |
| 78 | * Constructor |
| 79 | * Parses and populates a configuration event from JSON object data |
| 80 | * |
| 81 | * @param[in] bus - sdbusplus bus object |
| 82 | * @param[in] jsonObj - JSON object |
| 83 | */ |
| 84 | Event(sdbusplus::bus::bus& bus, const json& jsonObj); |
| 85 | |
| 86 | /** |
| 87 | * @brief Get the precondition |
| 88 | * |
| 89 | * @return The precondition details of the event |
| 90 | */ |
| 91 | inline const auto& getPrecond() const |
| 92 | { |
| 93 | return _precond; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @brief Get the groups |
| 98 | * |
| 99 | * @return List of groups associated with the event |
| 100 | */ |
| 101 | inline const auto& getGroups() const |
| 102 | { |
| 103 | return _groups; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @brief Get the triggers |
| 108 | * |
| 109 | * @return List of triggers for this event |
| 110 | */ |
| 111 | inline const auto& getTriggers() const |
| 112 | { |
| 113 | return _triggers; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @brief Get the actions |
| 118 | * |
| 119 | * @return List of actions to perform for the event |
| 120 | */ |
| 121 | inline const auto& getActions() const |
| 122 | { |
| 123 | return _actions; |
| 124 | } |
| 125 | |
| 126 | private: |
Matthew Barth | 391ade0 | 2021-01-15 14:33:21 -0600 | [diff] [blame] | 127 | /* Mapping of available group names & profiles to their group object */ |
| 128 | static const std::map<configKey, std::unique_ptr<Group>> _availGrps; |
| 129 | |
| 130 | /* The sdbusplus bus object */ |
| 131 | sdbusplus::bus::bus& _bus; |
| 132 | |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 133 | /* A precondition the event has in order to be enabled */ |
| 134 | Precondition _precond; |
| 135 | |
| 136 | /* List of groups associated with the event */ |
Matthew Barth | 12bae96 | 2021-01-15 16:18:11 -0600 | [diff] [blame] | 137 | std::vector<eGroup> _groups; |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 138 | |
| 139 | /* List of triggers for this event */ |
| 140 | std::vector<Trigger> _triggers; |
| 141 | |
| 142 | /* List of actions for this event */ |
| 143 | std::vector<Action> _actions; |
| 144 | |
| 145 | /** |
| 146 | * @brief Parse and set the event's precondition(OPTIONAL) |
| 147 | * |
| 148 | * @param[in] jsonObj - JSON object for the event |
| 149 | * |
| 150 | * Sets the precondition of the event in order to be enabled |
| 151 | */ |
| 152 | void setPrecond(const json& jsonObj); |
| 153 | |
| 154 | /** |
| 155 | * @brief Parse and set the event's groups(OPTIONAL) |
| 156 | * |
| 157 | * @param[in] jsonObj - JSON object for the event |
| 158 | * |
| 159 | * Sets the list of groups associated with the event |
| 160 | */ |
| 161 | void setGroups(const json& jsonObj); |
| 162 | |
| 163 | /** |
| 164 | * @brief Parse and set the event's triggers |
| 165 | * |
| 166 | * @param[in] jsonObj - JSON object for the event |
| 167 | * |
| 168 | * Sets the list of triggers for the event |
| 169 | */ |
| 170 | void setTriggers(const json& jsonObj); |
| 171 | |
| 172 | /** |
| 173 | * @brief Parse and set the event's actions(OPTIONAL) |
| 174 | * |
| 175 | * @param[in] jsonObj - JSON object for the event |
| 176 | * |
| 177 | * Sets the list of actions to perform for the event |
| 178 | */ |
| 179 | void setActions(const json& jsonObj); |
| 180 | }; |
| 181 | |
| 182 | } // namespace phosphor::fan::control::json |