xref: /openbmc/phosphor-fan-presence/control/json/triggers/signal.hpp (revision 599afcefea4a65fb437d8600b7f5b7ee49066969)
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 
21 #include <nlohmann/json.hpp>
22 #include <sdbusplus/message.hpp>
23 
24 #include <functional>
25 #include <memory>
26 #include <unordered_map>
27 #include <vector>
28 
29 namespace phosphor::fan::control::json::trigger::signal
30 {
31 
32 using json = nlohmann::json;
33 
34 /**
35  * @brief Subscribe to a signal
36  *
37  * @param[in] match - Signal match string to subscribe to
38  * @param[in] pkg - Data package to attach to signal
39  * @param[in] mgr - Pointer to manager of the trigger
40  */
41 void subscribe(const std::string& match, SignalPkg&& pkg,
42                std::function<bool(SignalPkg&)> isSameSig, Manager* mgr);
43 
44 /**
45  * @brief Subscribes to a propertiesChanged signal
46  *
47  * @param[in] mgr - Pointer to manager of the trigger
48  * @param[in] eventName - Name of event associated to the signal
49  * @param[in] action - Action to be run when signal is received
50  */
51 void propertiesChanged(Manager* mgr, const std::string& eventName,
52                        std::unique_ptr<ActionBase>& action);
53 
54 /**
55  * @brief Subscribes to an interfacesAdded signal
56  *
57  * @param[in] mgr - Pointer to manager of the trigger
58  * @param[in] eventName - Name of event associated to the signal
59  * @param[in] action - Action to be run when signal is received
60  */
61 void interfacesAdded(Manager* mgr, const std::string& eventName,
62                      std::unique_ptr<ActionBase>& action);
63 
64 // Match setup function for signals
65 using SignalMatch = std::function<void(Manager*, const std::string&,
66                                        std::unique_ptr<ActionBase>& action)>;
67 
68 /* Supported signals to their corresponding match setup functions */
69 static const std::unordered_map<std::string, SignalMatch> signals = {
70     {"properties_changed", propertiesChanged},
71     {"interfaces_added", interfacesAdded}};
72 
73 /**
74  * @brief Trigger to process an event after a signal is received
75  *
76  * @param[in] jsonObj - JSON object for the trigger
77  * @param[in] eventName - Name of event associated to the signal
78  * @param[in] mgr - Pointer to manager of the trigger
79  * @param[in] actions - Actions associated with the trigger
80  *
81  * When fan control starts (or restarts), all events with 'signal' triggers are
82  * subscribed to run its corresponding actions when a signal, per its
83  * configuration, is received.
84  */
85 void triggerSignal(const json& jsonObj, const std::string& eventName,
86                    Manager* mgr,
87                    std::vector<std::unique_ptr<ActionBase>>& actions);
88 
89 } // namespace phosphor::fan::control::json::trigger::signal
90