blob: ad44f95bca47e3203ed4c84895cfcbc95afb0e4d [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
Matthew Barth039f91e2021-10-04 15:18:07 -050040 * @param[in] isSameSig - Function to determine if same signal being subscribed
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050041 * @param[in] mgr - Pointer to manager of the trigger
42 */
43void 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
Matthew Barth039f91e2021-10-04 15:18:07 -050050 * @param[in] group - Group to subscribe signal against
51 * @param[in] actions - Actions to be run when signal is received
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050052 */
Matt Spinlerd0ba86a2021-11-09 10:09:13 -060053void propertiesChanged(Manager* mgr, const Group& group,
54 TriggerActions& actions, const json&);
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050055
Matthew Barth599afce2021-05-13 16:15:22 -050056/**
57 * @brief Subscribes to an interfacesAdded signal
58 *
59 * @param[in] mgr - Pointer to manager of the trigger
Matthew Barth039f91e2021-10-04 15:18:07 -050060 * @param[in] group - Group to subscribe signal against
61 * @param[in] actions - Actions to be run when signal is received
Matthew Barth599afce2021-05-13 16:15:22 -050062 */
Matt Spinlerd0ba86a2021-11-09 10:09:13 -060063void interfacesAdded(Manager* mgr, const Group& group, TriggerActions& actions,
Matthew Barth737f11c2021-09-27 14:23:08 -050064 const json&);
Matthew Barth599afce2021-05-13 16:15:22 -050065
Matthew Barthc2691402021-05-13 16:20:32 -050066/**
67 * @brief Subscribes to an interfacesRemoved signal
68 *
69 * @param[in] mgr - Pointer to manager of the trigger
Matthew Barth039f91e2021-10-04 15:18:07 -050070 * @param[in] group - Group to subscribe signal against
71 * @param[in] actions - Actions to be run when signal is received
Matthew Barthc2691402021-05-13 16:20:32 -050072 */
Matt Spinlerd0ba86a2021-11-09 10:09:13 -060073void interfacesRemoved(Manager* mgr, const Group& group,
74 TriggerActions& actions, const json&);
Matthew Barthc2691402021-05-13 16:20:32 -050075
Matthew Barthb03f6bb2021-05-13 16:23:10 -050076/**
77 * @brief Subscribes to a nameOwnerChanged signal
78 *
79 * @param[in] mgr - Pointer to manager of the trigger
Matthew Barth039f91e2021-10-04 15:18:07 -050080 * @param[in] group - Group to subscribe signal against
81 * @param[in] actions - Actions to be run when signal is received
Matthew Barthb03f6bb2021-05-13 16:23:10 -050082 */
Matt Spinlerd0ba86a2021-11-09 10:09:13 -060083void nameOwnerChanged(Manager* mgr, const Group& group, TriggerActions& actions,
Matthew Barth737f11c2021-09-27 14:23:08 -050084 const json&);
Matthew Barthb03f6bb2021-05-13 16:23:10 -050085
Matthew Barth16861792021-09-27 15:00:27 -050086/**
87 * @brief Subscribes to a dbus member signal
88 *
89 * @param[in] mgr - Pointer to manager of the trigger
Matthew Barth039f91e2021-10-04 15:18:07 -050090 * @param[in] group - Group to subscribe signal against
91 * @param[in] actions - Actions to be run when signal is received
Matthew Barth16861792021-09-27 15:00:27 -050092 */
Matt Spinlerd0ba86a2021-11-09 10:09:13 -060093void member(Manager* mgr, const Group& group, TriggerActions& actions,
Matthew Barth93a10ab2021-10-15 14:45:42 -050094 const json&);
Matthew Barth16861792021-09-27 15:00:27 -050095
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050096// Match setup function for signals
Matthew Barthc3a69082021-11-15 14:32:48 -060097using SignalMatch =
Matt Spinlerd0ba86a2021-11-09 10:09:13 -060098 std::function<void(Manager*, const Group&, TriggerActions&, const json&)>;
Matthew Barthbaeeb8f2021-05-13 16:03:54 -050099
100/* Supported signals to their corresponding match setup functions */
101static const std::unordered_map<std::string, SignalMatch> signals = {
Matthew Barth599afce2021-05-13 16:15:22 -0500102 {"properties_changed", propertiesChanged},
Matthew Barthc2691402021-05-13 16:20:32 -0500103 {"interfaces_added", interfacesAdded},
Matthew Barthb03f6bb2021-05-13 16:23:10 -0500104 {"interfaces_removed", interfacesRemoved},
Matthew Barth16861792021-09-27 15:00:27 -0500105 {"name_owner_changed", nameOwnerChanged},
106 {"member", member}};
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500107
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
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500113 * @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 */
Matthew Barth54b5a242021-05-21 11:02:52 -0500119enableTrigger triggerSignal(const json& jsonObj, const std::string& eventName,
Matthew Barthc3a69082021-11-15 14:32:48 -0600120 std::vector<std::unique_ptr<ActionBase>>& actions);
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500121
122} // namespace phosphor::fan::control::json::trigger::signal