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"; |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | namespace utils |
| 19 | { |
| 20 | |
| 21 | using namespace phosphor::logging; |
| 22 | |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 23 | std::string getService(sdbusplus::bus::bus& bus, |
| 24 | const char* path, |
| 25 | const char* interface) |
| 26 | { |
| 27 | auto mapper = bus.new_method_call(MAPPER_BUSNAME, |
| 28 | MAPPER_PATH, |
| 29 | MAPPER_INTERFACE, |
| 30 | "GetObject"); |
| 31 | |
| 32 | mapper.append(path, std::vector<std::string>({interface})); |
Lei YU | 86c83f3 | 2018-07-13 15:14:56 +0800 | [diff] [blame] | 33 | try |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 34 | { |
Lei YU | 86c83f3 | 2018-07-13 15:14:56 +0800 | [diff] [blame] | 35 | auto mapperResponseMsg = bus.call(mapper); |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 36 | |
Lei YU | 86c83f3 | 2018-07-13 15:14:56 +0800 | [diff] [blame] | 37 | std::vector<std::pair<std::string, std::vector<std::string>>> |
| 38 | mapperResponse; |
| 39 | mapperResponseMsg.read(mapperResponse); |
| 40 | if (mapperResponse.empty()) |
| 41 | { |
| 42 | log<level::ERR>("Error reading mapper response"); |
| 43 | throw std::runtime_error("Error reading mapper response"); |
| 44 | } |
| 45 | if (mapperResponse.size() < 1){ |
| 46 | return ""; |
| 47 | } |
| 48 | return mapperResponse[0].first; |
| 49 | } |
| 50 | catch (const sdbusplus::exception::SdBusError& ex) |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 51 | { |
Lei YU | 86c83f3 | 2018-07-13 15:14:56 +0800 | [diff] [blame] | 52 | log<level::ERR>("Mapper call failed", |
| 53 | entry("METHOD=%d", "GetObject"), |
| 54 | entry("PATH=%s", path), |
| 55 | entry("INTERFACE=%s", interface)); |
| 56 | throw std::runtime_error("Mapper call failed"); |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 57 | } |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 58 | } |
| 59 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 60 | Mode strToMode(const std::string& mode) |
| 61 | { |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 62 | return ModeSetting::convertMethodFromString(mode); |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | Owner strToOwner(const std::string& owner) |
| 66 | { |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 67 | return OwnerSetting::convertOwnersFromString(owner); |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 68 | } |
| 69 | |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 70 | std::string modeToStr(Mode mode) |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 71 | { |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 72 | return sdbusplus::xyz::openbmc_project::Time::server::convertForMessage(mode); |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 73 | } |
| 74 | |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 75 | std::string ownerToStr(Owner owner) |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 76 | { |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 77 | return sdbusplus::xyz::openbmc_project::Time::server::convertForMessage(owner); |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | } // namespace utils |
| 81 | } // namespace time |
| 82 | } // namespace phosphor |