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 | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 21 | #include "trigger_aliases.hpp" |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 22 | |
| 23 | #include <nlohmann/json.hpp> |
| 24 | #include <sdbusplus/bus.hpp> |
| 25 | |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 26 | #include <memory> |
Matthew Barth | 12bae96 | 2021-01-15 16:18:11 -0600 | [diff] [blame] | 27 | #include <optional> |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 28 | #include <tuple> |
Matthew Barth | 12bae96 | 2021-01-15 16:18:11 -0600 | [diff] [blame] | 29 | |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 30 | namespace phosphor::fan::control::json |
| 31 | { |
| 32 | |
| 33 | using json = nlohmann::json; |
| 34 | |
| 35 | /** |
| 36 | * @class Event - Represents a configured fan control event |
| 37 | * |
| 38 | * Fan control events are optional, therefore the "events.json" file is |
| 39 | * also optional. An event object can be used to enable a specific change to |
| 40 | * how fan control should function. These events contain the configured |
| 41 | * attributes that result in how fans are controlled within a system. Events |
| 42 | * are made up of groups of sensors, triggers from those sensors, and actions |
Matthew Barth | 619dc0f | 2021-05-20 09:27:02 -0500 | [diff] [blame] | 43 | * to be run when a trigger occurs. The triggers and actions configured must be |
| 44 | * available within the fan control application source. |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 45 | * |
| 46 | * When no events exist, the configured fans are set to their corresponding |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 47 | * zone's `full_speed` value. |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 48 | */ |
| 49 | class Event : public ConfigBase |
| 50 | { |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 51 | public: |
| 52 | /* JSON file name for events */ |
| 53 | static constexpr auto confFileName = "events.json"; |
| 54 | |
| 55 | Event() = delete; |
| 56 | Event(const Event&) = delete; |
| 57 | Event(Event&&) = delete; |
| 58 | Event& operator=(const Event&) = delete; |
| 59 | Event& operator=(Event&&) = delete; |
| 60 | ~Event() = default; |
| 61 | |
| 62 | /** |
| 63 | * Constructor |
| 64 | * Parses and populates a configuration event from JSON object data |
| 65 | * |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 66 | * @param[in] jsonObj - JSON object |
Matthew Barth | d4bd0ae | 2021-04-14 12:42:03 -0500 | [diff] [blame] | 67 | * @param[in] mgr - Manager of this event |
Matthew Barth | 9f1632e | 2021-03-31 15:51:50 -0500 | [diff] [blame] | 68 | * @param[in] zones - Reference to the configured zones |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 69 | */ |
Matthew Barth | 9403a21 | 2021-05-17 09:31:50 -0500 | [diff] [blame] | 70 | Event(const json& jsonObj, Manager* mgr, |
Matthew Barth | 9f1632e | 2021-03-31 15:51:50 -0500 | [diff] [blame] | 71 | std::map<configKey, std::unique_ptr<Zone>>& zones); |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 72 | |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 73 | /** |
| 74 | * @brief Enable the event |
| 75 | * |
| 76 | * Performs the necessary tasks to enable the event such as enabling all the |
| 77 | * event triggers, etc... |
| 78 | */ |
| 79 | void enable(); |
| 80 | |
Matthew Barth | e386fbb | 2021-06-30 14:33:55 -0500 | [diff] [blame] | 81 | /** |
Matt Spinler | d1f97f4 | 2021-10-29 16:19:24 -0500 | [diff] [blame] | 82 | * @brief Call any power on triggers |
| 83 | */ |
| 84 | void powerOn(); |
| 85 | |
| 86 | /** |
| 87 | * @brief Call any power off triggers |
| 88 | */ |
| 89 | void powerOff(); |
| 90 | |
| 91 | /** |
Matthew Barth | 3695ac3 | 2021-10-06 14:55:30 -0500 | [diff] [blame] | 92 | * @brief Clear all groups available for events |
| 93 | */ |
| 94 | static void clearAllGroups() |
| 95 | { |
| 96 | allGroups.clear(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @brief Set the groups that are available for events |
| 101 | * |
| 102 | * @param[in] groups - All groups available for events |
| 103 | */ |
| 104 | static void |
| 105 | setAllGroups(std::map<configKey, std::unique_ptr<Group>>&& groups) |
| 106 | { |
| 107 | allGroups = std::move(groups); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @brief Load and/or return all groups available to be configured on events |
| 112 | * |
| 113 | * @param[in] loadGroups - Whether to load the groups or not |
| 114 | * (default is to load the groups if not already loaded) |
| 115 | * |
| 116 | * @return Groups available to be configured on events from `groups.json` |
| 117 | */ |
| 118 | static std::map<configKey, std::unique_ptr<Group>>& |
| 119 | getAllGroups(bool loadGroups = true); |
| 120 | |
| 121 | /** |
Matthew Barth | e386fbb | 2021-06-30 14:33:55 -0500 | [diff] [blame] | 122 | * @brief Parse group parameters and configure a group object |
| 123 | * |
| 124 | * @param[in] group - Group object to get configured |
| 125 | * @param[in] jsonObj - JSON object for the group |
| 126 | * |
| 127 | * Configures a given group from a set of JSON configuration attributes |
| 128 | */ |
| 129 | static void configGroup(Group& group, const json& jsonObj); |
| 130 | |
| 131 | /** |
| 132 | * @brief Parse and set the event's groups(OPTIONAL) |
| 133 | * |
| 134 | * @param[in] jsonObj - JSON object for the event |
| 135 | * @param[in] profiles - List of profiles to validate groups against |
| 136 | * @param[out] groups - List of groups to be configured |
| 137 | * |
| 138 | * Sets the list of groups associated with the event |
| 139 | */ |
| 140 | static void setGroups(const json& jsonObj, |
| 141 | const std::vector<std::string>& profiles, |
| 142 | std::vector<Group>& groups); |
| 143 | |
Matt Spinler | c3eb7b3 | 2022-04-25 15:44:16 -0500 | [diff] [blame] | 144 | /** |
| 145 | * @brief Return the contained groups and actions as JSON |
| 146 | * |
| 147 | * @return json - A JSON object of groups and actions |
| 148 | */ |
| 149 | json dump() const; |
| 150 | |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 151 | private: |
Matthew Barth | 391ade0 | 2021-01-15 14:33:21 -0600 | [diff] [blame] | 152 | /* The sdbusplus bus object */ |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 153 | sdbusplus::bus_t& _bus; |
Matthew Barth | 391ade0 | 2021-01-15 14:33:21 -0600 | [diff] [blame] | 154 | |
Matthew Barth | d4bd0ae | 2021-04-14 12:42:03 -0500 | [diff] [blame] | 155 | /* The event's manager */ |
| 156 | Manager* _manager; |
| 157 | |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 158 | /* List of groups associated with the event */ |
Matthew Barth | e557860 | 2021-03-30 12:53:24 -0500 | [diff] [blame] | 159 | std::vector<Group> _groups; |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 160 | |
Matthew Barth | 9f1632e | 2021-03-31 15:51:50 -0500 | [diff] [blame] | 161 | /* Reference to the configured zones */ |
| 162 | std::map<configKey, std::unique_ptr<Zone>>& _zones; |
| 163 | |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 164 | /* List of actions for this event */ |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 165 | std::vector<std::unique_ptr<ActionBase>> _actions; |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 166 | |
Matt Spinler | d1f97f4 | 2021-10-29 16:19:24 -0500 | [diff] [blame] | 167 | /* List of trigger type and enablement functions for this event */ |
| 168 | std::vector<std::tuple<std::string, trigger::enableTrigger>> _triggers; |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 169 | |
Matthew Barth | 3695ac3 | 2021-10-06 14:55:30 -0500 | [diff] [blame] | 170 | /* All groups available to be configred on events */ |
| 171 | static std::map<configKey, std::unique_ptr<Group>> allGroups; |
Matthew Barth | c8bde4a | 2021-05-19 15:34:49 -0500 | [diff] [blame] | 172 | |
| 173 | /** |
Matthew Barth | 9f1632e | 2021-03-31 15:51:50 -0500 | [diff] [blame] | 174 | * @brief Parse and set the event's actions(OPTIONAL) |
| 175 | * |
| 176 | * @param[in] jsonObj - JSON object for the event |
Matthew Barth | 9f1632e | 2021-03-31 15:51:50 -0500 | [diff] [blame] | 177 | * |
| 178 | * Sets the list of actions to perform for the event |
| 179 | */ |
Matthew Barth | c8bde4a | 2021-05-19 15:34:49 -0500 | [diff] [blame] | 180 | void setActions(const json& jsonObj); |
Matthew Barth | 9f1632e | 2021-03-31 15:51:50 -0500 | [diff] [blame] | 181 | |
| 182 | /** |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 183 | * @brief Parse and set the event's triggers |
| 184 | * |
| 185 | * @param[in] jsonObj - JSON object for the event |
| 186 | * |
| 187 | * Sets the list of triggers for the event |
| 188 | */ |
| 189 | void setTriggers(const json& jsonObj); |
Matthew Barth | 3174e72 | 2020-09-15 15:13:40 -0500 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | } // namespace phosphor::fan::control::json |