xref: /openbmc/phosphor-fan-presence/control/json/triggers/signal.hpp (revision 737f11cd8def5854345e06404e9675881ee9dc9a)
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] mgr - Pointer to manager of the trigger
41  */
42 void subscribe(const std::string& match, SignalPkg&& pkg,
43                std::function<bool(SignalPkg&)> isSameSig, Manager* mgr);
44 
45 /**
46  * @brief Subscribes to a propertiesChanged signal
47  *
48  * @param[in] mgr - Pointer to manager of the trigger
49  * @param[in] action - Action to be run when signal is received
50  */
51 void propertiesChanged(Manager* mgr, std::unique_ptr<ActionBase>& action,
52                        const json&);
53 
54 /**
55  * @brief Subscribes to an interfacesAdded signal
56  *
57  * @param[in] mgr - Pointer to manager of the trigger
58  * @param[in] action - Action to be run when signal is received
59  */
60 void interfacesAdded(Manager* mgr, std::unique_ptr<ActionBase>& action,
61                      const json&);
62 
63 /**
64  * @brief Subscribes to an interfacesRemoved signal
65  *
66  * @param[in] mgr - Pointer to manager of the trigger
67  * @param[in] action - Action to be run when signal is received
68  */
69 void interfacesRemoved(Manager* mgr, std::unique_ptr<ActionBase>& action,
70                        const json&);
71 
72 /**
73  * @brief Subscribes to a nameOwnerChanged signal
74  *
75  * @param[in] mgr - Pointer to manager of the trigger
76  * @param[in] action - Action to be run when signal is received
77  */
78 void nameOwnerChanged(Manager* mgr, std::unique_ptr<ActionBase>& action,
79                       const json&);
80 
81 // Match setup function for signals
82 using SignalMatch = std::function<void(
83     Manager*, std::unique_ptr<ActionBase>& action, const json&)>;
84 
85 /* Supported signals to their corresponding match setup functions */
86 static const std::unordered_map<std::string, SignalMatch> signals = {
87     {"properties_changed", propertiesChanged},
88     {"interfaces_added", interfacesAdded},
89     {"interfaces_removed", interfacesRemoved},
90     {"name_owner_changed", nameOwnerChanged}};
91 
92 /**
93  * @brief Trigger to process an event after a signal is received
94  *
95  * @param[in] jsonObj - JSON object for the trigger
96  * @param[in] eventName - Name of event associated to the signal
97  * @param[in] actions - Actions associated with the trigger
98  *
99  * When fan control starts (or restarts), all events with 'signal' triggers are
100  * subscribed to run its corresponding actions when a signal, per its
101  * configuration, is received.
102  */
103 enableTrigger triggerSignal(const json& jsonObj, const std::string& eventName,
104                             std::vector<std::unique_ptr<ActionBase>>& actions);
105 
106 } // namespace phosphor::fan::control::json::trigger::signal
107