Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 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" |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 20 | #include "trigger_aliases.hpp" |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 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 |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 40 | * @param[in] isSameSig - Function to determine if same signal being subscribed |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 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 |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 50 | * @param[in] group - Group to subscribe signal against |
| 51 | * @param[in] actions - Actions to be run when signal is received |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 52 | */ |
Matt Spinler | d0ba86a | 2021-11-09 10:09:13 -0600 | [diff] [blame^] | 53 | void propertiesChanged(Manager* mgr, const Group& group, |
| 54 | TriggerActions& actions, const json&); |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 55 | |
Matthew Barth | 599afce | 2021-05-13 16:15:22 -0500 | [diff] [blame] | 56 | /** |
| 57 | * @brief Subscribes to an interfacesAdded signal |
| 58 | * |
| 59 | * @param[in] mgr - Pointer to manager of the trigger |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 60 | * @param[in] group - Group to subscribe signal against |
| 61 | * @param[in] actions - Actions to be run when signal is received |
Matthew Barth | 599afce | 2021-05-13 16:15:22 -0500 | [diff] [blame] | 62 | */ |
Matt Spinler | d0ba86a | 2021-11-09 10:09:13 -0600 | [diff] [blame^] | 63 | void interfacesAdded(Manager* mgr, const Group& group, TriggerActions& actions, |
Matthew Barth | 737f11c | 2021-09-27 14:23:08 -0500 | [diff] [blame] | 64 | const json&); |
Matthew Barth | 599afce | 2021-05-13 16:15:22 -0500 | [diff] [blame] | 65 | |
Matthew Barth | c269140 | 2021-05-13 16:20:32 -0500 | [diff] [blame] | 66 | /** |
| 67 | * @brief Subscribes to an interfacesRemoved signal |
| 68 | * |
| 69 | * @param[in] mgr - Pointer to manager of the trigger |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 70 | * @param[in] group - Group to subscribe signal against |
| 71 | * @param[in] actions - Actions to be run when signal is received |
Matthew Barth | c269140 | 2021-05-13 16:20:32 -0500 | [diff] [blame] | 72 | */ |
Matt Spinler | d0ba86a | 2021-11-09 10:09:13 -0600 | [diff] [blame^] | 73 | void interfacesRemoved(Manager* mgr, const Group& group, |
| 74 | TriggerActions& actions, const json&); |
Matthew Barth | c269140 | 2021-05-13 16:20:32 -0500 | [diff] [blame] | 75 | |
Matthew Barth | b03f6bb | 2021-05-13 16:23:10 -0500 | [diff] [blame] | 76 | /** |
| 77 | * @brief Subscribes to a nameOwnerChanged signal |
| 78 | * |
| 79 | * @param[in] mgr - Pointer to manager of the trigger |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 80 | * @param[in] group - Group to subscribe signal against |
| 81 | * @param[in] actions - Actions to be run when signal is received |
Matthew Barth | b03f6bb | 2021-05-13 16:23:10 -0500 | [diff] [blame] | 82 | */ |
Matt Spinler | d0ba86a | 2021-11-09 10:09:13 -0600 | [diff] [blame^] | 83 | void nameOwnerChanged(Manager* mgr, const Group& group, TriggerActions& actions, |
Matthew Barth | 737f11c | 2021-09-27 14:23:08 -0500 | [diff] [blame] | 84 | const json&); |
Matthew Barth | b03f6bb | 2021-05-13 16:23:10 -0500 | [diff] [blame] | 85 | |
Matthew Barth | 1686179 | 2021-09-27 15:00:27 -0500 | [diff] [blame] | 86 | /** |
| 87 | * @brief Subscribes to a dbus member signal |
| 88 | * |
| 89 | * @param[in] mgr - Pointer to manager of the trigger |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 90 | * @param[in] group - Group to subscribe signal against |
| 91 | * @param[in] actions - Actions to be run when signal is received |
Matthew Barth | 1686179 | 2021-09-27 15:00:27 -0500 | [diff] [blame] | 92 | */ |
Matt Spinler | d0ba86a | 2021-11-09 10:09:13 -0600 | [diff] [blame^] | 93 | void member(Manager* mgr, const Group& group, TriggerActions& actions, |
Matthew Barth | 93a10ab | 2021-10-15 14:45:42 -0500 | [diff] [blame] | 94 | const json&); |
Matthew Barth | 1686179 | 2021-09-27 15:00:27 -0500 | [diff] [blame] | 95 | |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 96 | // Match setup function for signals |
Matthew Barth | c3a6908 | 2021-11-15 14:32:48 -0600 | [diff] [blame] | 97 | using SignalMatch = |
Matt Spinler | d0ba86a | 2021-11-09 10:09:13 -0600 | [diff] [blame^] | 98 | std::function<void(Manager*, const Group&, TriggerActions&, const json&)>; |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 99 | |
| 100 | /* Supported signals to their corresponding match setup functions */ |
| 101 | static const std::unordered_map<std::string, SignalMatch> signals = { |
Matthew Barth | 599afce | 2021-05-13 16:15:22 -0500 | [diff] [blame] | 102 | {"properties_changed", propertiesChanged}, |
Matthew Barth | c269140 | 2021-05-13 16:20:32 -0500 | [diff] [blame] | 103 | {"interfaces_added", interfacesAdded}, |
Matthew Barth | b03f6bb | 2021-05-13 16:23:10 -0500 | [diff] [blame] | 104 | {"interfaces_removed", interfacesRemoved}, |
Matthew Barth | 1686179 | 2021-09-27 15:00:27 -0500 | [diff] [blame] | 105 | {"name_owner_changed", nameOwnerChanged}, |
| 106 | {"member", member}}; |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 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 |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 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 | */ |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 119 | enableTrigger triggerSignal(const json& jsonObj, const std::string& eventName, |
Matthew Barth | c3a6908 | 2021-11-15 14:32:48 -0600 | [diff] [blame] | 120 | std::vector<std::unique_ptr<ActionBase>>& actions); |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 121 | |
| 122 | } // namespace phosphor::fan::control::json::trigger::signal |