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 "action.hpp"
19 #include "config_base.hpp"
20 #include "group.hpp"
21 #include "trigger_aliases.hpp"
22 
23 #include <nlohmann/json.hpp>
24 #include <sdbusplus/bus.hpp>
25 
26 #include <memory>
27 #include <optional>
28 #include <tuple>
29 
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
43  * to be run when a trigger occurs. The triggers and actions configured must be
44  * available within the fan 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 {
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      *
66      * @param[in] jsonObj - JSON object
67      * @param[in] mgr - Manager of this event
68      * @param[in] zones - Reference to the configured zones
69      */
70     Event(const json& jsonObj, Manager* mgr,
71           std::map<configKey, std::unique_ptr<Zone>>& zones);
72 
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 
81     /**
82      * @brief Parse group parameters and configure a group object
83      *
84      * @param[in] group - Group object to get configured
85      * @param[in] jsonObj - JSON object for the group
86      *
87      * Configures a given group from a set of JSON configuration attributes
88      */
89     static void configGroup(Group& group, const json& jsonObj);
90 
91     /**
92      * @brief Parse and set the event's groups(OPTIONAL)
93      *
94      * @param[in] jsonObj - JSON object for the event
95      * @param[in] profiles - List of profiles to validate groups against
96      * @param[out] groups - List of groups to be configured
97      *
98      * Sets the list of groups associated with the event
99      */
100     static void setGroups(const json& jsonObj,
101                           const std::vector<std::string>& profiles,
102                           std::vector<Group>& groups);
103 
104   private:
105     /* The sdbusplus bus object */
106     sdbusplus::bus::bus& _bus;
107 
108     /* The event's manager */
109     Manager* _manager;
110 
111     /* List of groups associated with the event */
112     std::vector<Group> _groups;
113 
114     /* Reference to the configured zones */
115     std::map<configKey, std::unique_ptr<Zone>>& _zones;
116 
117     /* List of actions for this event */
118     std::vector<std::unique_ptr<ActionBase>> _actions;
119 
120     /* List of trigger enablement functions for this event */
121     std::vector<trigger::enableTrigger> _triggers;
122 
123     /**
124      * @brief Load the groups available to be configured on events
125      *
126      * @return Groups available to be configured on events from `groups.json`
127      */
128     static auto& getAvailGroups() __attribute__((pure));
129 
130     /**
131      * @brief Parse and set the event's actions(OPTIONAL)
132      *
133      * @param[in] jsonObj - JSON object for the event
134      *
135      * Sets the list of actions to perform for the event
136      */
137     void setActions(const json& jsonObj);
138 
139     /**
140      * @brief Parse and set the event's triggers
141      *
142      * @param[in] jsonObj - JSON object for the event
143      *
144      * Sets the list of triggers for the event
145      */
146     void setTriggers(const json& jsonObj);
147 };
148 
149 } // namespace phosphor::fan::control::json
150