Implement mode/owner and string conversions
Move Manager::convertToMode() to utils::strToMode();
Move Manager::convertToOwner() to utils::strToOwner();
Add utils::modeToStr() and utils::ownerToStr();
Adjust and add unit tests.
Change-Id: Ied35d0e732c477017e1b2db1a3464b0425b12387
Signed-off-by: Lei YU <mine260309@gmail.com>
diff --git a/utils.cpp b/utils.cpp
new file mode 100644
index 0000000..16cd1b4
--- /dev/null
+++ b/utils.cpp
@@ -0,0 +1,107 @@
+#include "utils.hpp"
+
+#include <phosphor-logging/log.hpp>
+
+
+namespace phosphor
+{
+namespace time
+{
+
+namespace // anonymous
+{
+/** @brief The map that maps the string to Mode */
+const std::map<std::string, Mode> modeMap =
+{
+ { "NTP", Mode::NTP },
+ { "MANUAL", Mode::MANUAL },
+};
+
+/** @brief The map that maps the string to Owner */
+const std::map<std::string, Owner> ownerMap =
+{
+ { "BMC", Owner::BMC },
+ { "HOST", Owner::HOST },
+ { "SPLIT", Owner::SPLIT },
+ { "BOTH", Owner::BOTH },
+};
+}
+
+namespace utils
+{
+
+using namespace phosphor::logging;
+
+Mode strToMode(const std::string& mode)
+{
+ auto it = modeMap.find(mode);
+ if (it == modeMap.end())
+ {
+ log<level::ERR>("Unrecognized mode",
+ entry("%s", mode.c_str()));
+ // TODO: use elog to throw exceptions
+ assert(0);
+ }
+ return it->second;
+}
+
+Owner strToOwner(const std::string& owner)
+{
+ auto it = ownerMap.find(owner);
+ if (it == ownerMap.end())
+ {
+ log<level::ERR>("Unrecognized owner",
+ entry("%s", owner.c_str()));
+ // TODO: use elog to throw exceptions
+ assert(0);
+ }
+ return it->second;
+}
+
+const char* modeToStr(Mode mode)
+{
+ const char* ret{};
+ switch (mode)
+ {
+ case Mode::NTP:
+ ret = "NTP";
+ break;
+ case Mode::MANUAL:
+ ret = "MANUAL";
+ break;
+ default:
+ // TODO: use elog to throw exceptions
+ assert(0);
+ break;
+ }
+ return ret;
+}
+
+const char* ownerToStr(Owner owner)
+{
+ const char* ret{};
+ switch (owner)
+ {
+ case Owner::BMC:
+ ret = "BMC";
+ break;
+ case Owner::HOST:
+ ret = "HOST";
+ break;
+ case Owner::SPLIT:
+ ret = "SPLIT";
+ break;
+ case Owner::BOTH:
+ ret = "BOTH";
+ break;
+ default:
+ // TODO: use elog to throw exceptions
+ assert(0);
+ break;
+ }
+ return ret;
+}
+
+} // namespace utils
+} // namespace time
+} // namespace phosphor