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 |
| 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] eventName - Name of event associated to the signal |
| 50 | * @param[in] action - Action to be run when signal is received |
| 51 | */ |
| 52 | void propertiesChanged(Manager* mgr, const std::string& eventName, |
| 53 | std::unique_ptr<ActionBase>& action); |
| 54 | |
Matthew Barth | 599afce | 2021-05-13 16:15:22 -0500 | [diff] [blame] | 55 | /** |
| 56 | * @brief Subscribes to an interfacesAdded signal |
| 57 | * |
| 58 | * @param[in] mgr - Pointer to manager of the trigger |
| 59 | * @param[in] eventName - Name of event associated to the signal |
| 60 | * @param[in] action - Action to be run when signal is received |
| 61 | */ |
| 62 | void interfacesAdded(Manager* mgr, const std::string& eventName, |
| 63 | std::unique_ptr<ActionBase>& action); |
| 64 | |
Matthew Barth | c269140 | 2021-05-13 16:20:32 -0500 | [diff] [blame] | 65 | /** |
| 66 | * @brief Subscribes to an interfacesRemoved signal |
| 67 | * |
| 68 | * @param[in] mgr - Pointer to manager of the trigger |
| 69 | * @param[in] eventName - Name of event associated to the signal |
| 70 | * @param[in] action - Action to be run when signal is received |
| 71 | */ |
| 72 | void interfacesRemoved(Manager* mgr, const std::string& eventName, |
| 73 | std::unique_ptr<ActionBase>& action); |
| 74 | |
Matthew Barth | b03f6bb | 2021-05-13 16:23:10 -0500 | [diff] [blame] | 75 | /** |
| 76 | * @brief Subscribes to a nameOwnerChanged signal |
| 77 | * |
| 78 | * @param[in] mgr - Pointer to manager of the trigger |
| 79 | * @param[in] eventName - Name of event associated to the signal |
| 80 | * @param[in] action - Action to be run when signal is received |
| 81 | */ |
| 82 | void nameOwnerChanged(Manager* mgr, const std::string& eventName, |
| 83 | std::unique_ptr<ActionBase>& actions); |
| 84 | |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 85 | // Match setup function for signals |
| 86 | using SignalMatch = std::function<void(Manager*, const std::string&, |
| 87 | std::unique_ptr<ActionBase>& action)>; |
| 88 | |
| 89 | /* Supported signals to their corresponding match setup functions */ |
| 90 | static const std::unordered_map<std::string, SignalMatch> signals = { |
Matthew Barth | 599afce | 2021-05-13 16:15:22 -0500 | [diff] [blame] | 91 | {"properties_changed", propertiesChanged}, |
Matthew Barth | c269140 | 2021-05-13 16:20:32 -0500 | [diff] [blame] | 92 | {"interfaces_added", interfacesAdded}, |
Matthew Barth | b03f6bb | 2021-05-13 16:23:10 -0500 | [diff] [blame] | 93 | {"interfaces_removed", interfacesRemoved}, |
| 94 | {"name_owner_changed", nameOwnerChanged}}; |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * @brief Trigger to process an event after a signal is received |
| 98 | * |
| 99 | * @param[in] jsonObj - JSON object for the trigger |
| 100 | * @param[in] eventName - Name of event associated to the signal |
| 101 | * @param[in] mgr - Pointer to manager of the trigger |
| 102 | * @param[in] actions - Actions associated with the trigger |
| 103 | * |
| 104 | * When fan control starts (or restarts), all events with 'signal' triggers are |
| 105 | * subscribed to run its corresponding actions when a signal, per its |
| 106 | * configuration, is received. |
| 107 | */ |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame^] | 108 | enableTrigger triggerSignal(const json& jsonObj, const std::string& eventName, |
| 109 | Manager* mgr, |
| 110 | std::vector<std::unique_ptr<ActionBase>>& actions); |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 111 | |
| 112 | } // namespace phosphor::fan::control::json::trigger::signal |