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 | |
| 54 | // Match setup function for signals |
| 55 | using SignalMatch = std::function<void(Manager*, const std::string&, |
| 56 | std::unique_ptr<ActionBase>& action)>; |
| 57 | |
| 58 | /* Supported signals to their corresponding match setup functions */ |
| 59 | static const std::unordered_map<std::string, SignalMatch> signals = { |
| 60 | {"properties_changed", propertiesChanged}}; |
| 61 | |
| 62 | /** |
| 63 | * @brief Trigger to process an event after a signal is received |
| 64 | * |
| 65 | * @param[in] jsonObj - JSON object for the trigger |
| 66 | * @param[in] eventName - Name of event associated to the signal |
| 67 | * @param[in] mgr - Pointer to manager of the trigger |
| 68 | * @param[in] actions - Actions associated with the trigger |
| 69 | * |
| 70 | * When fan control starts (or restarts), all events with 'signal' triggers are |
| 71 | * subscribed to run its corresponding actions when a signal, per its |
| 72 | * configuration, is received. |
| 73 | */ |
| 74 | void triggerSignal(const json& jsonObj, const std::string& eventName, |
| 75 | Manager* mgr, |
| 76 | std::vector<std::unique_ptr<ActionBase>>& actions); |
| 77 | |
| 78 | } // namespace phosphor::fan::control::json::trigger::signal |