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