Standardize Action -> Target Map Type
This change refactors the watchdog header to export the type of watchdog
action to systemd target map, so it can be directly used by other files.
This makes enumerating the map type more trivial.
Also convert to an unordered_map instead of map since we don't need the
ordering guarantees.
Tested:
Builds and tests still pass
Change-Id: I77d315210ec27fde295589479c50d46dc5d1b32a
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/mainapp.cpp b/mainapp.cpp
index 72277d1..c61fa85 100644
--- a/mainapp.cpp
+++ b/mainapp.cpp
@@ -36,19 +36,18 @@
exit(EXIT_FAILURE);
}
-void printActionTargets(
- const std::map<Watchdog::Action, std::string>& actionTargets)
+void printActionTargetMap(const Watchdog::ActionTargetMap& actionTargetMap)
{
std::cerr << "Action Targets:\n";
- for (const auto& actionTarget : actionTargets)
+ for (const auto& [action, target] : actionTargetMap)
{
- std::cerr << " " << convertForMessage(actionTarget.first) << " -> "
- << actionTarget.second << "\n";
+ std::cerr << " " << convertForMessage(action) << " -> " << target
+ << "\n";
}
std::cerr << std::flush;
}
-int main(int argc, char** argv)
+int main(int argc, char* argv[])
{
using namespace phosphor::logging;
using InternalFailure =
@@ -97,13 +96,13 @@
{
exitWithError("Multiple targets specified.", argv);
}
- std::map<Watchdog::Action, Watchdog::TargetName> actionTargets;
+ Watchdog::ActionTargetMap actionTargetMap;
if (!targetParam.empty())
{
auto target = targetParam.back();
- actionTargets[Watchdog::Action::HardReset] = target;
- actionTargets[Watchdog::Action::PowerOff] = target;
- actionTargets[Watchdog::Action::PowerCycle] = target;
+ actionTargetMap[Watchdog::Action::HardReset] = target;
+ actionTargetMap[Watchdog::Action::PowerOff] = target;
+ actionTargetMap[Watchdog::Action::PowerCycle] = target;
}
// Parse out the action_target arguments. We allow one target to map
@@ -132,9 +131,9 @@
exitWithError("Bad action specified.", argv);
}
- actionTargets[action] = std::move(value);
+ actionTargetMap[action] = std::move(value);
}
- printActionTargets(actionTargets);
+ printActionTargetMap(actionTargetMap);
// Parse out the fallback settings for the watchdog. Note that we require
// both of the fallback arguments to do anything here, but having a fallback
@@ -213,7 +212,7 @@
try
{
// Create a watchdog object
- Watchdog watchdog(bus, path.c_str(), eventP, std::move(actionTargets),
+ Watchdog watchdog(bus, path.c_str(), eventP, std::move(actionTargetMap),
std::move(fallback));
// Claim the bus
diff --git a/test/watchdog_test.cpp b/test/watchdog_test.cpp
index f24d45e..49890da 100644
--- a/test/watchdog_test.cpp
+++ b/test/watchdog_test.cpp
@@ -191,9 +191,8 @@
.action = Watchdog::Action::PowerOff,
.interval = static_cast<uint64_t>(fallbackIntervalMs),
};
- std::map<Watchdog::Action, Watchdog::TargetName> emptyActionTargets;
wdog = std::make_unique<Watchdog>(bus, TEST_PATH, eventP,
- std::move(emptyActionTargets),
+ Watchdog::ActionTargetMap(),
std::move(fallback));
EXPECT_EQ(primaryInterval, milliseconds(wdog->interval(primaryIntervalMs)));
EXPECT_FALSE(wdog->enabled());
@@ -275,9 +274,8 @@
.interval = static_cast<uint64_t>(fallbackIntervalMs),
.always = false,
};
- std::map<Watchdog::Action, Watchdog::TargetName> emptyActionTargets;
wdog = std::make_unique<Watchdog>(bus, TEST_PATH, eventP,
- std::move(emptyActionTargets),
+ Watchdog::ActionTargetMap(),
std::move(fallback));
EXPECT_EQ(primaryInterval, milliseconds(wdog->interval(primaryIntervalMs)));
EXPECT_FALSE(wdog->enabled());
@@ -330,9 +328,8 @@
.interval = static_cast<uint64_t>(fallbackIntervalMs),
.always = true,
};
- std::map<Watchdog::Action, Watchdog::TargetName> emptyActionTargets;
wdog = std::make_unique<Watchdog>(bus, TEST_PATH, eventP,
- std::move(emptyActionTargets),
+ Watchdog::ActionTargetMap(),
std::move(fallback));
EXPECT_EQ(primaryInterval, milliseconds(wdog->interval(primaryIntervalMs)));
EXPECT_FALSE(wdog->enabled());
diff --git a/watchdog.cpp b/watchdog.cpp
index 9e7ec23..9301fb0 100644
--- a/watchdog.cpp
+++ b/watchdog.cpp
@@ -112,8 +112,8 @@
action = fallback->action;
}
- auto target = actionTargets.find(action);
- if (target == actionTargets.end())
+ auto target = actionTargetMap.find(action);
+ if (target == actionTargetMap.end())
{
log<level::INFO>("watchdog: Timed out with no target",
entry("ACTION=%s", convertForMessage(action).c_str()));
diff --git a/watchdog.hpp b/watchdog.hpp
index efa413a..c9821db 100644
--- a/watchdog.hpp
+++ b/watchdog.hpp
@@ -3,10 +3,10 @@
#include "timer.hpp"
#include <functional>
-#include <map>
#include <optional>
#include <sdbusplus/bus.hpp>
#include <sdbusplus/server/object.hpp>
+#include <unordered_map>
#include <utility>
#include <xyz/openbmc_project/State/Watchdog/server.hpp>
@@ -14,6 +14,7 @@
{
namespace watchdog
{
+
namespace Base = sdbusplus::xyz::openbmc_project::State::server;
using WatchdogInherits = sdbusplus::server::object::object<Base::Watchdog>;
@@ -36,6 +37,11 @@
*/
using TargetName = std::string;
+ /** @brief Type used to store the mapping of a Watchdog timeout
+ * action to a systemd target.
+ */
+ using ActionTargetMap = std::unordered_map<Action, TargetName>;
+
/** @brief Type used to specify the parameters of a fallback watchdog
*/
struct Fallback
@@ -47,18 +53,17 @@
/** @brief Constructs the Watchdog object
*
- * @param[in] bus - DBus bus to attach to.
- * @param[in] objPath - Object path to attach to.
- * @param[in] event - reference to sd_event unique pointer
- * @param[in] actionTargets - map of systemd targets called on timeout
+ * @param[in] bus - DBus bus to attach to.
+ * @param[in] objPath - Object path to attach to.
+ * @param[in] event - reference to sd_event unique pointer
+ * @param[in] actionTargetMap - map of systemd targets called on timeout
* @param[in] fallback
*/
Watchdog(sdbusplus::bus::bus& bus, const char* objPath, EventPtr& event,
- std::map<Action, TargetName>&& actionTargets =
- std::map<Action, TargetName>(),
+ ActionTargetMap&& actionTargetMap = {},
std::optional<Fallback>&& fallback = std::nullopt) :
WatchdogInherits(bus, objPath),
- bus(bus), actionTargets(std::move(actionTargets)),
+ bus(bus), actionTargetMap(std::move(actionTargetMap)),
fallback(std::move(fallback)),
timer(event, std::bind(&Watchdog::timeOutHandler, this))
{
@@ -127,7 +132,7 @@
sdbusplus::bus::bus& bus;
/** @brief Map of systemd units to be started when the timer expires */
- std::map<Action, TargetName> actionTargets;
+ ActionTargetMap actionTargetMap;
/** @brief Fallback timer options */
std::optional<Fallback> fallback;