1 /**
2  * Copyright © 2021 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 "../manager.hpp"
19 #include "action.hpp"
20 #include "trigger_aliases.hpp"
21 
22 #include <nlohmann/json.hpp>
23 #include <sdbusplus/message.hpp>
24 
25 #include <functional>
26 #include <memory>
27 #include <unordered_map>
28 #include <vector>
29 
30 namespace phosphor::fan::control::json::trigger::signal
31 {
32 
33 using json = nlohmann::json;
34 
35 /**
36  * @brief Subscribe to a signal
37  *
38  * @param[in] match - Signal match string to subscribe to
39  * @param[in] pkg - Data package to attach to signal
40  * @param[in] isSameSig - Function to determine if same signal being subscribed
41  * @param[in] mgr - Pointer to manager of the trigger
42  */
43 void subscribe(const std::string& match, SignalPkg&& pkg,
44                std::function<bool(SignalPkg&)> isSameSig, Manager* mgr);
45 
46 /**
47  * @brief Subscribes to a propertiesChanged signal
48  *
49  * @param[in] mgr - Pointer to manager of the trigger
50  * @param[in] group - Group to subscribe signal against
51  * @param[in] actions - Actions to be run when signal is received
52  */
53 void propertiesChanged(Manager* mgr, const Group& group,
54                        TriggerActions& actions, const json&);
55 
56 /**
57  * @brief Subscribes to an interfacesAdded signal
58  *
59  * @param[in] mgr - Pointer to manager of the trigger
60  * @param[in] group - Group to subscribe signal against
61  * @param[in] actions - Actions to be run when signal is received
62  */
63 void interfacesAdded(Manager* mgr, const Group& group, TriggerActions& actions,
64                      const json&);
65 
66 /**
67  * @brief Subscribes to an interfacesRemoved signal
68  *
69  * @param[in] mgr - Pointer to manager of the trigger
70  * @param[in] group - Group to subscribe signal against
71  * @param[in] actions - Actions to be run when signal is received
72  */
73 void interfacesRemoved(Manager* mgr, const Group& group,
74                        TriggerActions& actions, const json&);
75 
76 /**
77  * @brief Subscribes to a nameOwnerChanged signal
78  *
79  * @param[in] mgr - Pointer to manager of the trigger
80  * @param[in] group - Group to subscribe signal against
81  * @param[in] actions - Actions to be run when signal is received
82  */
83 void nameOwnerChanged(Manager* mgr, const Group& group, TriggerActions& actions,
84                       const json&);
85 
86 /**
87  * @brief Subscribes to a dbus member signal
88  *
89  * @param[in] mgr - Pointer to manager of the trigger
90  * @param[in] group - Group to subscribe signal against
91  * @param[in] actions - Actions to be run when signal is received
92  */
93 void member(Manager* mgr, const Group& group, TriggerActions& actions,
94             const json&);
95 
96 // Match setup function for signals
97 using SignalMatch =
98     std::function<void(Manager*, const Group&, TriggerActions&, const json&)>;
99 
100 /* Supported signals to their corresponding match setup functions */
101 static const std::unordered_map<std::string, SignalMatch> signals = {
102     {"properties_changed", propertiesChanged},
103     {"interfaces_added", interfacesAdded},
104     {"interfaces_removed", interfacesRemoved},
105     {"name_owner_changed", nameOwnerChanged},
106     {"member", member}};
107 
108 /**
109  * @brief Trigger to process an event after a signal is received
110  *
111  * @param[in] jsonObj - JSON object for the trigger
112  * @param[in] eventName - Name of event associated to the signal
113  * @param[in] actions - Actions associated with the trigger
114  *
115  * When fan control starts (or restarts), all events with 'signal' triggers are
116  * subscribed to run its corresponding actions when a signal, per its
117  * configuration, is received.
118  */
119 enableTrigger triggerSignal(const json& jsonObj, const std::string& eventName,
120                             std::vector<std::unique_ptr<ActionBase>>& actions);
121 
122 } // namespace phosphor::fan::control::json::trigger::signal
123