blob: 78bc633e16d0f3df1362328b883358cf580e6481 [file] [log] [blame]
Matthew Barth3174e722020-09-15 15:13:40 -05001/**
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 Barth44ab7692021-03-26 11:40:10 -050018#include "action.hpp"
Matthew Barth3174e722020-09-15 15:13:40 -050019#include "config_base.hpp"
Matthew Barth391ade02021-01-15 14:33:21 -060020#include "group.hpp"
Matthew Barth54b5a242021-05-21 11:02:52 -050021#include "trigger_aliases.hpp"
Matthew Barth3174e722020-09-15 15:13:40 -050022
23#include <nlohmann/json.hpp>
24#include <sdbusplus/bus.hpp>
25
Matthew Barth44ab7692021-03-26 11:40:10 -050026#include <memory>
Matthew Barth12bae962021-01-15 16:18:11 -060027#include <optional>
Matthew Barth44ab7692021-03-26 11:40:10 -050028#include <tuple>
Matthew Barth12bae962021-01-15 16:18:11 -060029
Matthew Barth3174e722020-09-15 15:13:40 -050030namespace phosphor::fan::control::json
31{
32
33using 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 Barth619dc0f2021-05-20 09:27:02 -050043 * to be run when a trigger occurs. The triggers and actions configured must be
44 * available within the fan control application source.
Matthew Barth3174e722020-09-15 15:13:40 -050045 *
46 * When no events exist, the configured fans are set to their corresponding
Matthew Barth44ab7692021-03-26 11:40:10 -050047 * zone's `full_speed` value.
Matthew Barth3174e722020-09-15 15:13:40 -050048 */
49class Event : public ConfigBase
50{
Matthew Barth3174e722020-09-15 15:13:40 -050051 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 Barth3174e722020-09-15 15:13:40 -050066 * @param[in] jsonObj - JSON object
Matthew Barthd4bd0ae2021-04-14 12:42:03 -050067 * @param[in] mgr - Manager of this event
Matthew Barth9f1632e2021-03-31 15:51:50 -050068 * @param[in] zones - Reference to the configured zones
Matthew Barth3174e722020-09-15 15:13:40 -050069 */
Matthew Barth9403a212021-05-17 09:31:50 -050070 Event(const json& jsonObj, Manager* mgr,
Matthew Barth9f1632e2021-03-31 15:51:50 -050071 std::map<configKey, std::unique_ptr<Zone>>& zones);
Matthew Barth3174e722020-09-15 15:13:40 -050072
Matthew Barth54b5a242021-05-21 11:02:52 -050073 /**
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 Barthe386fbb2021-06-30 14:33:55 -050081 /**
Matthew Barth3695ac32021-10-06 14:55:30 -050082 * @brief Clear all groups available for events
83 */
84 static void clearAllGroups()
85 {
86 allGroups.clear();
87 }
88
89 /**
90 * @brief Set the groups that are available for events
91 *
92 * @param[in] groups - All groups available for events
93 */
94 static void
95 setAllGroups(std::map<configKey, std::unique_ptr<Group>>&& groups)
96 {
97 allGroups = std::move(groups);
98 }
99
100 /**
101 * @brief Load and/or return all groups available to be configured on events
102 *
103 * @param[in] loadGroups - Whether to load the groups or not
104 * (default is to load the groups if not already loaded)
105 *
106 * @return Groups available to be configured on events from `groups.json`
107 */
108 static std::map<configKey, std::unique_ptr<Group>>&
109 getAllGroups(bool loadGroups = true);
110
111 /**
Matthew Barthe386fbb2021-06-30 14:33:55 -0500112 * @brief Parse group parameters and configure a group object
113 *
114 * @param[in] group - Group object to get configured
115 * @param[in] jsonObj - JSON object for the group
116 *
117 * Configures a given group from a set of JSON configuration attributes
118 */
119 static void configGroup(Group& group, const json& jsonObj);
120
121 /**
122 * @brief Parse and set the event's groups(OPTIONAL)
123 *
124 * @param[in] jsonObj - JSON object for the event
125 * @param[in] profiles - List of profiles to validate groups against
126 * @param[out] groups - List of groups to be configured
127 *
128 * Sets the list of groups associated with the event
129 */
130 static void setGroups(const json& jsonObj,
131 const std::vector<std::string>& profiles,
132 std::vector<Group>& groups);
133
Matthew Barth3174e722020-09-15 15:13:40 -0500134 private:
Matthew Barth391ade02021-01-15 14:33:21 -0600135 /* The sdbusplus bus object */
136 sdbusplus::bus::bus& _bus;
137
Matthew Barthd4bd0ae2021-04-14 12:42:03 -0500138 /* The event's manager */
139 Manager* _manager;
140
Matthew Barth3174e722020-09-15 15:13:40 -0500141 /* List of groups associated with the event */
Matthew Barthe5578602021-03-30 12:53:24 -0500142 std::vector<Group> _groups;
Matthew Barth3174e722020-09-15 15:13:40 -0500143
Matthew Barth9f1632e2021-03-31 15:51:50 -0500144 /* Reference to the configured zones */
145 std::map<configKey, std::unique_ptr<Zone>>& _zones;
146
Matthew Barth3174e722020-09-15 15:13:40 -0500147 /* List of actions for this event */
Matthew Barth44ab7692021-03-26 11:40:10 -0500148 std::vector<std::unique_ptr<ActionBase>> _actions;
Matthew Barth3174e722020-09-15 15:13:40 -0500149
Matthew Barth54b5a242021-05-21 11:02:52 -0500150 /* List of trigger enablement functions for this event */
151 std::vector<trigger::enableTrigger> _triggers;
152
Matthew Barth3695ac32021-10-06 14:55:30 -0500153 /* All groups available to be configred on events */
154 static std::map<configKey, std::unique_ptr<Group>> allGroups;
Matthew Barthc8bde4a2021-05-19 15:34:49 -0500155
156 /**
Matthew Barth9f1632e2021-03-31 15:51:50 -0500157 * @brief Parse and set the event's actions(OPTIONAL)
158 *
159 * @param[in] jsonObj - JSON object for the event
Matthew Barth9f1632e2021-03-31 15:51:50 -0500160 *
161 * Sets the list of actions to perform for the event
162 */
Matthew Barthc8bde4a2021-05-19 15:34:49 -0500163 void setActions(const json& jsonObj);
Matthew Barth9f1632e2021-03-31 15:51:50 -0500164
165 /**
Matthew Barth3174e722020-09-15 15:13:40 -0500166 * @brief Parse and set the event's triggers
167 *
168 * @param[in] jsonObj - JSON object for the event
169 *
170 * Sets the list of triggers for the event
171 */
172 void setTriggers(const json& jsonObj);
Matthew Barth3174e722020-09-15 15:13:40 -0500173};
174
175} // namespace phosphor::fan::control::json