blob: 8c48f35860b9212495a0ec1de47b078c1c9b2dc9 [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 Barth3174e722020-09-15 15:13:40 -050021
22#include <nlohmann/json.hpp>
23#include <sdbusplus/bus.hpp>
24
Matthew Barth44ab7692021-03-26 11:40:10 -050025#include <memory>
Matthew Barth12bae962021-01-15 16:18:11 -060026#include <optional>
Matthew Barth44ab7692021-03-26 11:40:10 -050027#include <tuple>
Matthew Barth12bae962021-01-15 16:18:11 -060028
Matthew Barth3174e722020-09-15 15:13:40 -050029namespace phosphor::fan::control::json
30{
31
32using 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 Barth44ab7692021-03-26 11:40:10 -050048 * zone's `full_speed` value.
Matthew Barth3174e722020-09-15 15:13:40 -050049 */
50class Event : public ConfigBase
51{
Matthew Barth12bae962021-01-15 16:18:11 -060052 static constexpr auto pathPos = 0;
53 static constexpr auto intfPos = 1;
54 static constexpr auto propPos = 2;
55 static constexpr auto typePos = 3;
56 static constexpr auto valuePos = 4;
57 using eGroup = std::vector<std::tuple<std::string, std::string, std::string,
58 std::optional<std::string>,
59 std::optional<PropertyVariantType>>>;
60
Matthew Barth3174e722020-09-15 15:13:40 -050061 static constexpr auto precondName = 0;
62 static constexpr auto precondGroups = 1;
63 static constexpr auto precondEvents = 2;
64 using Precondition =
Matthew Barth44ab7692021-03-26 11:40:10 -050065 std::tuple<std::string,
66 std::vector<std::tuple<std::string, std::string, std::string,
67 PropertyVariantType>>,
68 std::vector<Event>>;
Matthew Barth3174e722020-09-15 15:13:40 -050069
70 public:
71 /* JSON file name for events */
72 static constexpr auto confFileName = "events.json";
73
74 Event() = delete;
75 Event(const Event&) = delete;
76 Event(Event&&) = delete;
77 Event& operator=(const Event&) = delete;
78 Event& operator=(Event&&) = delete;
79 ~Event() = default;
80
81 /**
82 * Constructor
83 * Parses and populates a configuration event from JSON object data
84 *
Matthew Barth3174e722020-09-15 15:13:40 -050085 * @param[in] jsonObj - JSON object
Matthew Barth44ab7692021-03-26 11:40:10 -050086 * @param[in] bus - sdbusplus bus object
87 * @param[in] groups - Available groups that can be used
Matthew Barth3174e722020-09-15 15:13:40 -050088 */
Matthew Barth44ab7692021-03-26 11:40:10 -050089 Event(const json& jsonObj, sdbusplus::bus::bus& bus,
90 std::map<configKey, std::unique_ptr<Group>>& groups);
Matthew Barth3174e722020-09-15 15:13:40 -050091
92 /**
93 * @brief Get the precondition
94 *
95 * @return The precondition details of the event
96 */
97 inline const auto& getPrecond() const
98 {
99 return _precond;
100 }
101
102 /**
103 * @brief Get the groups
104 *
105 * @return List of groups associated with the event
106 */
107 inline const auto& getGroups() const
108 {
109 return _groups;
110 }
111
112 /**
Matthew Barth3174e722020-09-15 15:13:40 -0500113 * @brief Get the actions
114 *
115 * @return List of actions to perform for the event
116 */
117 inline const auto& getActions() const
118 {
119 return _actions;
120 }
121
122 private:
Matthew Barth391ade02021-01-15 14:33:21 -0600123 /* The sdbusplus bus object */
124 sdbusplus::bus::bus& _bus;
125
Matthew Barth3174e722020-09-15 15:13:40 -0500126 /* A precondition the event has in order to be enabled */
127 Precondition _precond;
128
129 /* List of groups associated with the event */
Matthew Barth12bae962021-01-15 16:18:11 -0600130 std::vector<eGroup> _groups;
Matthew Barth3174e722020-09-15 15:13:40 -0500131
Matthew Barth3174e722020-09-15 15:13:40 -0500132 /* List of actions for this event */
Matthew Barth44ab7692021-03-26 11:40:10 -0500133 std::vector<std::unique_ptr<ActionBase>> _actions;
Matthew Barth3174e722020-09-15 15:13:40 -0500134
135 /**
136 * @brief Parse and set the event's precondition(OPTIONAL)
137 *
138 * @param[in] jsonObj - JSON object for the event
Matthew Barth44ab7692021-03-26 11:40:10 -0500139 * @param[in] groups - Available groups that can be used
Matthew Barth3174e722020-09-15 15:13:40 -0500140 *
141 * Sets the precondition of the event in order to be enabled
142 */
Matthew Barth44ab7692021-03-26 11:40:10 -0500143 void setPrecond(const json& jsonObj,
144 std::map<configKey, std::unique_ptr<Group>>& groups);
Matthew Barth3174e722020-09-15 15:13:40 -0500145
146 /**
147 * @brief Parse and set the event's groups(OPTIONAL)
148 *
149 * @param[in] jsonObj - JSON object for the event
Matthew Barth44ab7692021-03-26 11:40:10 -0500150 * @param[in] groups - Available groups that can be used
Matthew Barth3174e722020-09-15 15:13:40 -0500151 *
152 * Sets the list of groups associated with the event
153 */
Matthew Barth44ab7692021-03-26 11:40:10 -0500154 void setGroups(const json& jsonObj,
155 std::map<configKey, std::unique_ptr<Group>>& groups);
Matthew Barth3174e722020-09-15 15:13:40 -0500156
157 /**
158 * @brief Parse and set the event's triggers
159 *
160 * @param[in] jsonObj - JSON object for the event
161 *
162 * Sets the list of triggers for the event
163 */
164 void setTriggers(const json& jsonObj);
165
166 /**
167 * @brief Parse and set the event's actions(OPTIONAL)
168 *
169 * @param[in] jsonObj - JSON object for the event
170 *
171 * Sets the list of actions to perform for the event
172 */
173 void setActions(const json& jsonObj);
174};
175
176} // namespace phosphor::fan::control::json