Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame^] | 1 | #include "utils.hpp" |
| 2 | |
| 3 | #include <phosphor-logging/log.hpp> |
| 4 | |
| 5 | |
| 6 | namespace phosphor |
| 7 | { |
| 8 | namespace time |
| 9 | { |
| 10 | |
| 11 | namespace // anonymous |
| 12 | { |
| 13 | /** @brief The map that maps the string to Mode */ |
| 14 | const std::map<std::string, Mode> modeMap = |
| 15 | { |
| 16 | { "NTP", Mode::NTP }, |
| 17 | { "MANUAL", Mode::MANUAL }, |
| 18 | }; |
| 19 | |
| 20 | /** @brief The map that maps the string to Owner */ |
| 21 | const std::map<std::string, Owner> ownerMap = |
| 22 | { |
| 23 | { "BMC", Owner::BMC }, |
| 24 | { "HOST", Owner::HOST }, |
| 25 | { "SPLIT", Owner::SPLIT }, |
| 26 | { "BOTH", Owner::BOTH }, |
| 27 | }; |
| 28 | } |
| 29 | |
| 30 | namespace utils |
| 31 | { |
| 32 | |
| 33 | using namespace phosphor::logging; |
| 34 | |
| 35 | Mode strToMode(const std::string& mode) |
| 36 | { |
| 37 | auto it = modeMap.find(mode); |
| 38 | if (it == modeMap.end()) |
| 39 | { |
| 40 | log<level::ERR>("Unrecognized mode", |
| 41 | entry("%s", mode.c_str())); |
| 42 | // TODO: use elog to throw exceptions |
| 43 | assert(0); |
| 44 | } |
| 45 | return it->second; |
| 46 | } |
| 47 | |
| 48 | Owner strToOwner(const std::string& owner) |
| 49 | { |
| 50 | auto it = ownerMap.find(owner); |
| 51 | if (it == ownerMap.end()) |
| 52 | { |
| 53 | log<level::ERR>("Unrecognized owner", |
| 54 | entry("%s", owner.c_str())); |
| 55 | // TODO: use elog to throw exceptions |
| 56 | assert(0); |
| 57 | } |
| 58 | return it->second; |
| 59 | } |
| 60 | |
| 61 | const char* modeToStr(Mode mode) |
| 62 | { |
| 63 | const char* ret{}; |
| 64 | switch (mode) |
| 65 | { |
| 66 | case Mode::NTP: |
| 67 | ret = "NTP"; |
| 68 | break; |
| 69 | case Mode::MANUAL: |
| 70 | ret = "MANUAL"; |
| 71 | break; |
| 72 | default: |
| 73 | // TODO: use elog to throw exceptions |
| 74 | assert(0); |
| 75 | break; |
| 76 | } |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | const char* ownerToStr(Owner owner) |
| 81 | { |
| 82 | const char* ret{}; |
| 83 | switch (owner) |
| 84 | { |
| 85 | case Owner::BMC: |
| 86 | ret = "BMC"; |
| 87 | break; |
| 88 | case Owner::HOST: |
| 89 | ret = "HOST"; |
| 90 | break; |
| 91 | case Owner::SPLIT: |
| 92 | ret = "SPLIT"; |
| 93 | break; |
| 94 | case Owner::BOTH: |
| 95 | ret = "BOTH"; |
| 96 | break; |
| 97 | default: |
| 98 | // TODO: use elog to throw exceptions |
| 99 | assert(0); |
| 100 | break; |
| 101 | } |
| 102 | return ret; |
| 103 | } |
| 104 | |
| 105 | } // namespace utils |
| 106 | } // namespace time |
| 107 | } // namespace phosphor |