blob: 2030a5dcb20368d1e0c86834b8b3435a957ec12c [file] [log] [blame]
Matthew Barthbaeeb8f2021-05-13 16:03:54 -05001/**
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"
Matthew Barth54b5a242021-05-21 11:02:52 -050020#include "trigger_aliases.hpp"
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050021
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
30namespace phosphor::fan::control::json::trigger::signal
31{
32
33using 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 */
42void 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
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050049 * @param[in] action - Action to be run when signal is received
50 */
Matthew Barth737f11c2021-09-27 14:23:08 -050051void propertiesChanged(Manager* mgr, std::unique_ptr<ActionBase>& action,
52 const json&);
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050053
Matthew Barth599afce2021-05-13 16:15:22 -050054/**
55 * @brief Subscribes to an interfacesAdded signal
56 *
57 * @param[in] mgr - Pointer to manager of the trigger
Matthew Barth599afce2021-05-13 16:15:22 -050058 * @param[in] action - Action to be run when signal is received
59 */
Matthew Barth737f11c2021-09-27 14:23:08 -050060void interfacesAdded(Manager* mgr, std::unique_ptr<ActionBase>& action,
61 const json&);
Matthew Barth599afce2021-05-13 16:15:22 -050062
Matthew Barthc2691402021-05-13 16:20:32 -050063/**
64 * @brief Subscribes to an interfacesRemoved signal
65 *
66 * @param[in] mgr - Pointer to manager of the trigger
Matthew Barthc2691402021-05-13 16:20:32 -050067 * @param[in] action - Action to be run when signal is received
68 */
Matthew Barth737f11c2021-09-27 14:23:08 -050069void interfacesRemoved(Manager* mgr, std::unique_ptr<ActionBase>& action,
70 const json&);
Matthew Barthc2691402021-05-13 16:20:32 -050071
Matthew Barthb03f6bb2021-05-13 16:23:10 -050072/**
73 * @brief Subscribes to a nameOwnerChanged signal
74 *
75 * @param[in] mgr - Pointer to manager of the trigger
Matthew Barthb03f6bb2021-05-13 16:23:10 -050076 * @param[in] action - Action to be run when signal is received
77 */
Matthew Barth737f11c2021-09-27 14:23:08 -050078void nameOwnerChanged(Manager* mgr, std::unique_ptr<ActionBase>& action,
79 const json&);
Matthew Barthb03f6bb2021-05-13 16:23:10 -050080
Matthew Barth16861792021-09-27 15:00:27 -050081/**
82 * @brief Subscribes to a dbus member signal
83 *
84 * @param[in] mgr - Pointer to manager of the trigger
85 * @param[in] action - Action to be run when signal is received
86 * @param[in] jsonObj - JSON object for the trigger
87 */
88void member(Manager* mgr, std::unique_ptr<ActionBase>& action,
89 const json& jsonObj);
90
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050091// Match setup function for signals
Matthew Barth737f11c2021-09-27 14:23:08 -050092using SignalMatch = std::function<void(
93 Manager*, std::unique_ptr<ActionBase>& action, const json&)>;
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050094
95/* Supported signals to their corresponding match setup functions */
96static const std::unordered_map<std::string, SignalMatch> signals = {
Matthew Barth599afce2021-05-13 16:15:22 -050097 {"properties_changed", propertiesChanged},
Matthew Barthc2691402021-05-13 16:20:32 -050098 {"interfaces_added", interfacesAdded},
Matthew Barthb03f6bb2021-05-13 16:23:10 -050099 {"interfaces_removed", interfacesRemoved},
Matthew Barth16861792021-09-27 15:00:27 -0500100 {"name_owner_changed", nameOwnerChanged},
101 {"member", member}};
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500102
103/**
104 * @brief Trigger to process an event after a signal is received
105 *
106 * @param[in] jsonObj - JSON object for the trigger
107 * @param[in] eventName - Name of event associated to the signal
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500108 * @param[in] actions - Actions associated with the trigger
109 *
110 * When fan control starts (or restarts), all events with 'signal' triggers are
111 * subscribed to run its corresponding actions when a signal, per its
112 * configuration, is received.
113 */
Matthew Barth54b5a242021-05-21 11:02:52 -0500114enableTrigger triggerSignal(const json& jsonObj, const std::string& eventName,
Matthew Barth54b5a242021-05-21 11:02:52 -0500115 std::vector<std::unique_ptr<ActionBase>>& actions);
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500116
117} // namespace phosphor::fan::control::json::trigger::signal