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 | { |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame^] | 13 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 14 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 15 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 16 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 17 | /** @brief The map that maps the string to Mode */ |
| 18 | const std::map<std::string, Mode> modeMap = |
| 19 | { |
| 20 | { "NTP", Mode::NTP }, |
| 21 | { "MANUAL", Mode::MANUAL }, |
| 22 | }; |
| 23 | |
| 24 | /** @brief The map that maps the string to Owner */ |
| 25 | const std::map<std::string, Owner> ownerMap = |
| 26 | { |
| 27 | { "BMC", Owner::BMC }, |
| 28 | { "HOST", Owner::HOST }, |
| 29 | { "SPLIT", Owner::SPLIT }, |
| 30 | { "BOTH", Owner::BOTH }, |
| 31 | }; |
| 32 | } |
| 33 | |
| 34 | namespace utils |
| 35 | { |
| 36 | |
| 37 | using namespace phosphor::logging; |
| 38 | |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame^] | 39 | std::string getService(sdbusplus::bus::bus& bus, |
| 40 | const char* path, |
| 41 | const char* interface) |
| 42 | { |
| 43 | auto mapper = bus.new_method_call(MAPPER_BUSNAME, |
| 44 | MAPPER_PATH, |
| 45 | MAPPER_INTERFACE, |
| 46 | "GetObject"); |
| 47 | |
| 48 | mapper.append(path, std::vector<std::string>({interface})); |
| 49 | auto mapperResponseMsg = bus.call(mapper); |
| 50 | |
| 51 | if (mapperResponseMsg.is_method_error()) |
| 52 | { |
| 53 | // TODO: define repo specific errors and use elog report() |
| 54 | log<level::ERR>("Error in mapper call", |
| 55 | entry("PATH=%s", path), |
| 56 | entry("INTERFACE=%s", interface)); |
| 57 | return {}; |
| 58 | } |
| 59 | |
| 60 | std::map<std::string, std::vector<std::string>> mapperResponse; |
| 61 | mapperResponseMsg.read(mapperResponse); |
| 62 | if (mapperResponse.empty()) |
| 63 | { |
| 64 | // TODO: define repo specific errors and use elog report() |
| 65 | log<level::ERR>("Error reading mapper response", |
| 66 | entry("PATH=%s", path), |
| 67 | entry("INTERFACE=%s", interface)); |
| 68 | return {}; |
| 69 | } |
| 70 | |
| 71 | return mapperResponse.begin()->first; |
| 72 | } |
| 73 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 74 | Mode strToMode(const std::string& mode) |
| 75 | { |
| 76 | auto it = modeMap.find(mode); |
| 77 | if (it == modeMap.end()) |
| 78 | { |
| 79 | log<level::ERR>("Unrecognized mode", |
| 80 | entry("%s", mode.c_str())); |
| 81 | // TODO: use elog to throw exceptions |
| 82 | assert(0); |
| 83 | } |
| 84 | return it->second; |
| 85 | } |
| 86 | |
| 87 | Owner strToOwner(const std::string& owner) |
| 88 | { |
| 89 | auto it = ownerMap.find(owner); |
| 90 | if (it == ownerMap.end()) |
| 91 | { |
| 92 | log<level::ERR>("Unrecognized owner", |
| 93 | entry("%s", owner.c_str())); |
| 94 | // TODO: use elog to throw exceptions |
| 95 | assert(0); |
| 96 | } |
| 97 | return it->second; |
| 98 | } |
| 99 | |
| 100 | const char* modeToStr(Mode mode) |
| 101 | { |
| 102 | const char* ret{}; |
| 103 | switch (mode) |
| 104 | { |
| 105 | case Mode::NTP: |
| 106 | ret = "NTP"; |
| 107 | break; |
| 108 | case Mode::MANUAL: |
| 109 | ret = "MANUAL"; |
| 110 | break; |
| 111 | default: |
| 112 | // TODO: use elog to throw exceptions |
| 113 | assert(0); |
| 114 | break; |
| 115 | } |
| 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | const char* ownerToStr(Owner owner) |
| 120 | { |
| 121 | const char* ret{}; |
| 122 | switch (owner) |
| 123 | { |
| 124 | case Owner::BMC: |
| 125 | ret = "BMC"; |
| 126 | break; |
| 127 | case Owner::HOST: |
| 128 | ret = "HOST"; |
| 129 | break; |
| 130 | case Owner::SPLIT: |
| 131 | ret = "SPLIT"; |
| 132 | break; |
| 133 | case Owner::BOTH: |
| 134 | ret = "BOTH"; |
| 135 | break; |
| 136 | default: |
| 137 | // TODO: use elog to throw exceptions |
| 138 | assert(0); |
| 139 | break; |
| 140 | } |
| 141 | return ret; |
| 142 | } |
| 143 | |
| 144 | } // namespace utils |
| 145 | } // namespace time |
| 146 | } // namespace phosphor |