1baeeb8f1SMatthew Barth /** 2baeeb8f1SMatthew Barth * Copyright © 2021 IBM Corporation 3baeeb8f1SMatthew Barth * 4baeeb8f1SMatthew Barth * Licensed under the Apache License, Version 2.0 (the "License"); 5baeeb8f1SMatthew Barth * you may not use this file except in compliance with the License. 6baeeb8f1SMatthew Barth * You may obtain a copy of the License at 7baeeb8f1SMatthew Barth * 8baeeb8f1SMatthew Barth * http://www.apache.org/licenses/LICENSE-2.0 9baeeb8f1SMatthew Barth * 10baeeb8f1SMatthew Barth * Unless required by applicable law or agreed to in writing, software 11baeeb8f1SMatthew Barth * distributed under the License is distributed on an "AS IS" BASIS, 12baeeb8f1SMatthew Barth * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13baeeb8f1SMatthew Barth * See the License for the specific language governing permissions and 14baeeb8f1SMatthew Barth * limitations under the License. 15baeeb8f1SMatthew Barth */ 16baeeb8f1SMatthew Barth #pragma once 17baeeb8f1SMatthew Barth 18baeeb8f1SMatthew Barth #include "../manager.hpp" 19baeeb8f1SMatthew Barth #include "action.hpp" 2054b5a24fSMatthew Barth #include "trigger_aliases.hpp" 21baeeb8f1SMatthew Barth 22baeeb8f1SMatthew Barth #include <nlohmann/json.hpp> 23baeeb8f1SMatthew Barth #include <sdbusplus/message.hpp> 24baeeb8f1SMatthew Barth 25baeeb8f1SMatthew Barth #include <functional> 26baeeb8f1SMatthew Barth #include <memory> 27baeeb8f1SMatthew Barth #include <unordered_map> 28baeeb8f1SMatthew Barth #include <vector> 29baeeb8f1SMatthew Barth 30baeeb8f1SMatthew Barth namespace phosphor::fan::control::json::trigger::signal 31baeeb8f1SMatthew Barth { 32baeeb8f1SMatthew Barth 33baeeb8f1SMatthew Barth using json = nlohmann::json; 34baeeb8f1SMatthew Barth 35baeeb8f1SMatthew Barth /** 36baeeb8f1SMatthew Barth * @brief Subscribe to a signal 37baeeb8f1SMatthew Barth * 38baeeb8f1SMatthew Barth * @param[in] match - Signal match string to subscribe to 39baeeb8f1SMatthew Barth * @param[in] pkg - Data package to attach to signal 40039f91e5SMatthew Barth * @param[in] isSameSig - Function to determine if same signal being subscribed 41baeeb8f1SMatthew Barth * @param[in] mgr - Pointer to manager of the trigger 42baeeb8f1SMatthew Barth */ 43baeeb8f1SMatthew Barth void subscribe(const std::string& match, SignalPkg&& pkg, 44baeeb8f1SMatthew Barth std::function<bool(SignalPkg&)> isSameSig, Manager* mgr); 45baeeb8f1SMatthew Barth 46baeeb8f1SMatthew Barth /** 47baeeb8f1SMatthew Barth * @brief Subscribes to a propertiesChanged signal 48baeeb8f1SMatthew Barth * 49baeeb8f1SMatthew Barth * @param[in] mgr - Pointer to manager of the trigger 50039f91e5SMatthew Barth * @param[in] group - Group to subscribe signal against 51039f91e5SMatthew Barth * @param[in] actions - Actions to be run when signal is received 52baeeb8f1SMatthew Barth */ 53*d0ba86a3SMatt Spinler void propertiesChanged(Manager* mgr, const Group& group, 54*d0ba86a3SMatt Spinler TriggerActions& actions, const json&); 55baeeb8f1SMatthew Barth 56599afcefSMatthew Barth /** 57599afcefSMatthew Barth * @brief Subscribes to an interfacesAdded signal 58599afcefSMatthew Barth * 59599afcefSMatthew Barth * @param[in] mgr - Pointer to manager of the trigger 60039f91e5SMatthew Barth * @param[in] group - Group to subscribe signal against 61039f91e5SMatthew Barth * @param[in] actions - Actions to be run when signal is received 62599afcefSMatthew Barth */ 63*d0ba86a3SMatt Spinler void interfacesAdded(Manager* mgr, const Group& group, TriggerActions& actions, 64737f11cdSMatthew Barth const json&); 65599afcefSMatthew Barth 66c2691407SMatthew Barth /** 67c2691407SMatthew Barth * @brief Subscribes to an interfacesRemoved signal 68c2691407SMatthew Barth * 69c2691407SMatthew Barth * @param[in] mgr - Pointer to manager of the trigger 70039f91e5SMatthew Barth * @param[in] group - Group to subscribe signal against 71039f91e5SMatthew Barth * @param[in] actions - Actions to be run when signal is received 72c2691407SMatthew Barth */ 73*d0ba86a3SMatt Spinler void interfacesRemoved(Manager* mgr, const Group& group, 74*d0ba86a3SMatt Spinler TriggerActions& actions, const json&); 75c2691407SMatthew Barth 76b03f6bb0SMatthew Barth /** 77b03f6bb0SMatthew Barth * @brief Subscribes to a nameOwnerChanged signal 78b03f6bb0SMatthew Barth * 79b03f6bb0SMatthew Barth * @param[in] mgr - Pointer to manager of the trigger 80039f91e5SMatthew Barth * @param[in] group - Group to subscribe signal against 81039f91e5SMatthew Barth * @param[in] actions - Actions to be run when signal is received 82b03f6bb0SMatthew Barth */ 83*d0ba86a3SMatt Spinler void nameOwnerChanged(Manager* mgr, const Group& group, TriggerActions& actions, 84737f11cdSMatthew Barth const json&); 85b03f6bb0SMatthew Barth 8616861797SMatthew Barth /** 8716861797SMatthew Barth * @brief Subscribes to a dbus member signal 8816861797SMatthew Barth * 8916861797SMatthew Barth * @param[in] mgr - Pointer to manager of the trigger 90039f91e5SMatthew Barth * @param[in] group - Group to subscribe signal against 91039f91e5SMatthew Barth * @param[in] actions - Actions to be run when signal is received 9216861797SMatthew Barth */ 93*d0ba86a3SMatt Spinler void member(Manager* mgr, const Group& group, TriggerActions& actions, 9493a10ab6SMatthew Barth const json&); 9516861797SMatthew Barth 96baeeb8f1SMatthew Barth // Match setup function for signals 97c3a69087SMatthew Barth using SignalMatch = 98*d0ba86a3SMatt Spinler std::function<void(Manager*, const Group&, TriggerActions&, const json&)>; 99baeeb8f1SMatthew Barth 100baeeb8f1SMatthew Barth /* Supported signals to their corresponding match setup functions */ 101baeeb8f1SMatthew Barth static const std::unordered_map<std::string, SignalMatch> signals = { 102599afcefSMatthew Barth {"properties_changed", propertiesChanged}, 103c2691407SMatthew Barth {"interfaces_added", interfacesAdded}, 104b03f6bb0SMatthew Barth {"interfaces_removed", interfacesRemoved}, 10516861797SMatthew Barth {"name_owner_changed", nameOwnerChanged}, 10616861797SMatthew Barth {"member", member}}; 107baeeb8f1SMatthew Barth 108baeeb8f1SMatthew Barth /** 109baeeb8f1SMatthew Barth * @brief Trigger to process an event after a signal is received 110baeeb8f1SMatthew Barth * 111baeeb8f1SMatthew Barth * @param[in] jsonObj - JSON object for the trigger 112baeeb8f1SMatthew Barth * @param[in] eventName - Name of event associated to the signal 113baeeb8f1SMatthew Barth * @param[in] actions - Actions associated with the trigger 114baeeb8f1SMatthew Barth * 115baeeb8f1SMatthew Barth * When fan control starts (or restarts), all events with 'signal' triggers are 116baeeb8f1SMatthew Barth * subscribed to run its corresponding actions when a signal, per its 117baeeb8f1SMatthew Barth * configuration, is received. 118baeeb8f1SMatthew Barth */ 11954b5a24fSMatthew Barth enableTrigger triggerSignal(const json& jsonObj, const std::string& eventName, 120c3a69087SMatthew Barth std::vector<std::unique_ptr<ActionBase>>& actions); 121baeeb8f1SMatthew Barth 122baeeb8f1SMatthew Barth } // namespace phosphor::fan::control::json::trigger::signal 123