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