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 #include "trigger_aliases.hpp" 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] isSameSig - Function to determine if same signal being subscribed 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 50 * @param[in] group - Group to subscribe signal against 51 * @param[in] actions - Actions to be run when signal is received 52 */ 53 void propertiesChanged(Manager* mgr, const Group& group, SignalActions actions, 54 const json&); 55 56 /** 57 * @brief Subscribes to an interfacesAdded signal 58 * 59 * @param[in] mgr - Pointer to manager of the trigger 60 * @param[in] group - Group to subscribe signal against 61 * @param[in] actions - Actions to be run when signal is received 62 */ 63 void interfacesAdded(Manager* mgr, const Group& group, SignalActions actions, 64 const json&); 65 66 /** 67 * @brief Subscribes to an interfacesRemoved signal 68 * 69 * @param[in] mgr - Pointer to manager of the trigger 70 * @param[in] group - Group to subscribe signal against 71 * @param[in] actions - Actions to be run when signal is received 72 */ 73 void interfacesRemoved(Manager* mgr, const Group& group, SignalActions actions, 74 const json&); 75 76 /** 77 * @brief Subscribes to a nameOwnerChanged signal 78 * 79 * @param[in] mgr - Pointer to manager of the trigger 80 * @param[in] group - Group to subscribe signal against 81 * @param[in] actions - Actions to be run when signal is received 82 */ 83 void nameOwnerChanged(Manager* mgr, const Group& group, SignalActions actions, 84 const json&); 85 86 /** 87 * @brief Subscribes to a dbus member signal 88 * 89 * @param[in] mgr - Pointer to manager of the trigger 90 * @param[in] group - Group to subscribe signal against 91 * @param[in] actions - Actions to be run when signal is received 92 * @param[in] jsonObj - JSON object for the trigger 93 */ 94 void member(Manager* mgr, const Group&, SignalActions actions, 95 const json& jsonObj); 96 97 // Match setup function for signals 98 using SignalMatch = std::function<void(Manager*, const Group&, 99 SignalActions actions, const json&)>; 100 101 /* Supported signals to their corresponding match setup functions */ 102 static const std::unordered_map<std::string, SignalMatch> signals = { 103 {"properties_changed", propertiesChanged}, 104 {"interfaces_added", interfacesAdded}, 105 {"interfaces_removed", interfacesRemoved}, 106 {"name_owner_changed", nameOwnerChanged}, 107 {"member", member}}; 108 109 /** 110 * @brief Trigger to process an event after a signal is received 111 * 112 * @param[in] jsonObj - JSON object for the trigger 113 * @param[in] eventName - Name of event associated to the signal 114 * @param[in] actions - Actions associated with the trigger 115 * 116 * When fan control starts (or restarts), all events with 'signal' triggers are 117 * subscribed to run its corresponding actions when a signal, per its 118 * configuration, is received. 119 */ 120 enableTrigger triggerSignal(const json& jsonObj, const std::string& eventName, 121 SignalActions actions); 122 123 } // namespace phosphor::fan::control::json::trigger::signal 124