control: Add timer trigger support
When an event is configured with a "timer" trigger, the event's actions
are run after the configured type of timer expires.
Change-Id: I4a400cdae0df560ba58383916c246c10d27d3516
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/control/Makefile.am b/control/Makefile.am
index 74f28dc..8f0a11e 100644
--- a/control/Makefile.am
+++ b/control/Makefile.am
@@ -35,7 +35,8 @@
json/actions/missing_owner_target.cpp \
json/actions/count_state_target.cpp \
json/actions/net_target_increase.cpp \
- json/actions/net_target_decrease.cpp
+ json/actions/net_target_decrease.cpp \
+ json/triggers/timer.cpp
else
phosphor_fan_control_SOURCES += \
argument.cpp \
diff --git a/control/json/triggers/timer.cpp b/control/json/triggers/timer.cpp
new file mode 100644
index 0000000..641e33b
--- /dev/null
+++ b/control/json/triggers/timer.cpp
@@ -0,0 +1,85 @@
+/**
+ * Copyright © 2021 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "timer.hpp"
+
+#include "../manager.hpp"
+
+#include <fmt/format.h>
+
+#include <nlohmann/json.hpp>
+#include <phosphor-logging/log.hpp>
+
+#include <chrono>
+
+namespace phosphor::fan::control::json::trigger::timer
+{
+
+using json = nlohmann::json;
+using namespace phosphor::logging;
+
+TimerType getType(const json& jsonObj)
+{
+ if (!jsonObj.contains("type"))
+ {
+ log<level::ERR>("Missing required timer trigger type",
+ entry("JSON=%s", jsonObj.dump().c_str()));
+ throw std::runtime_error("Missing required timer trigger type");
+ }
+ auto type = jsonObj["type"].get<std::string>();
+ if (type == "oneshot")
+ {
+ return TimerType::oneshot;
+ }
+ else if (type == "repeating")
+ {
+ return TimerType::repeating;
+ }
+ else
+ {
+ log<level::ERR>(
+ fmt::format("Timer trigger type '{}' is not supported", type)
+ .c_str(),
+ entry("AVAILABLE_TYPES={oneshot, repeating}"));
+ throw std::runtime_error("Unsupported timer trigger type given");
+ }
+}
+
+std::chrono::microseconds getInterval(const json& jsonObj)
+{
+ if (!jsonObj.contains("interval"))
+ {
+ log<level::ERR>("Missing required timer trigger interval",
+ entry("JSON=%s", jsonObj.dump().c_str()));
+ throw std::runtime_error("Missing required timer trigger interval");
+ }
+ return static_cast<std::chrono::microseconds>(
+ jsonObj["interval"].get<uint64_t>());
+}
+
+void triggerTimer(const json& jsonObj, const std::string& eventName,
+ Manager* mgr,
+ std::vector<std::unique_ptr<ActionBase>>& actions)
+{
+ // Get the type and interval of this timer from the JSON
+ auto type = getType(jsonObj);
+ auto interval = getInterval(jsonObj);
+
+ // Create/add event timer
+ auto tpPtr = std::make_unique<TimerPkg>(eventName, std::ref(actions));
+ mgr->addTimer(type, interval, std::move(tpPtr));
+}
+
+} // namespace phosphor::fan::control::json::trigger::timer
diff --git a/control/json/triggers/timer.hpp b/control/json/triggers/timer.hpp
new file mode 100644
index 0000000..1be7fdd
--- /dev/null
+++ b/control/json/triggers/timer.hpp
@@ -0,0 +1,64 @@
+/**
+ * Copyright © 2021 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include "../manager.hpp"
+#include "action.hpp"
+
+#include <nlohmann/json.hpp>
+
+#include <chrono>
+
+namespace phosphor::fan::control::json::trigger::timer
+{
+
+using json = nlohmann::json;
+
+/**
+ * @brief Parse and return the timer trigger's type
+ *
+ * @param[in] jsonObj - JSON object for the timer trigger
+ *
+ * Gets the type of this timer
+ */
+TimerType getType(const json& jsonObj);
+
+/**
+ * @brief Parse and return the timer's interval
+ *
+ * @param[in] jsonObj - JSON object for the timer trigger
+ *
+ * Gets the interval of this timer
+ */
+std::chrono::microseconds getInterval(const json& jsonObj);
+
+/**
+ * @brief Trigger to run an event based on a timer
+ *
+ * @param[in] jsonObj - JSON object for the trigger
+ * @param[in] eventName - Name of event creating the trigger
+ * @param[in] mgr - Manager to setup the trigger on
+ * @param[in] actions - Actions associated with the trigger
+ *
+ * When fan control starts (or restarts), all events with 'timer' triggers
+ * have their timers started. Once the timer expires, per its configuration,
+ * the corresponding event's actions are run.
+ */
+void triggerTimer(const json& jsonObj, const std::string& eventName,
+ Manager* mgr,
+ std::vector<std::unique_ptr<ActionBase>>& actions);
+
+} // namespace phosphor::fan::control::json::trigger::timer
diff --git a/control/json/triggers/trigger.hpp b/control/json/triggers/trigger.hpp
index 7051932..b5fb902 100644
--- a/control/json/triggers/trigger.hpp
+++ b/control/json/triggers/trigger.hpp
@@ -17,6 +17,7 @@
#include "action.hpp"
#include "manager.hpp"
+#include "timer.hpp"
#include <nlohmann/json.hpp>
@@ -36,6 +37,7 @@
std::vector<std::unique_ptr<ActionBase>>&)>;
// Mapping of trigger class name to its creation function
-static const std::map<std::string, createTrigger> triggers = {};
+static const std::map<std::string, createTrigger> triggers = {
+ {"timer", timer::triggerTimer}};
} // namespace phosphor::fan::control::json::trigger