Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 1 | #include "utils.hpp" |
| 2 | |
Lei YU | 86d8041 | 2017-07-12 13:12:12 +0800 | [diff] [blame] | 3 | #include <phosphor-logging/elog.hpp> |
| 4 | #include <phosphor-logging/elog-errors.hpp> |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 5 | #include <phosphor-logging/log.hpp> |
Lei YU | 86d8041 | 2017-07-12 13:12:12 +0800 | [diff] [blame] | 6 | #include <xyz/openbmc_project/Common/error.hpp> |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 7 | |
| 8 | |
| 9 | namespace phosphor |
| 10 | { |
| 11 | namespace time |
| 12 | { |
| 13 | |
| 14 | namespace // anonymous |
| 15 | { |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 16 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 17 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 18 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | namespace utils |
| 22 | { |
| 23 | |
Lei YU | 86d8041 | 2017-07-12 13:12:12 +0800 | [diff] [blame] | 24 | using InvalidArgumentError = |
| 25 | sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument; |
| 26 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 27 | using namespace phosphor::logging; |
| 28 | |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 29 | std::string getService(sdbusplus::bus::bus& bus, |
| 30 | const char* path, |
| 31 | const char* interface) |
| 32 | { |
| 33 | auto mapper = bus.new_method_call(MAPPER_BUSNAME, |
| 34 | MAPPER_PATH, |
| 35 | MAPPER_INTERFACE, |
| 36 | "GetObject"); |
| 37 | |
| 38 | mapper.append(path, std::vector<std::string>({interface})); |
| 39 | auto mapperResponseMsg = bus.call(mapper); |
| 40 | |
| 41 | if (mapperResponseMsg.is_method_error()) |
| 42 | { |
Lei YU | 86d8041 | 2017-07-12 13:12:12 +0800 | [diff] [blame] | 43 | using namespace xyz::openbmc_project::Time::Internal; |
| 44 | elog<MethodErr>(MethodError::METHOD_NAME("GetObject"), |
| 45 | MethodError::PATH(path), |
| 46 | MethodError::INTERFACE(interface), |
| 47 | MethodError::MISC({})); |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 48 | } |
| 49 | |
Ed Tanous | 7aa715b | 2018-05-09 17:28:05 -0700 | [diff] [blame] | 50 | std::vector<std::pair<std::string, std::vector<std::string>>> |
| 51 | mapperResponse; |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 52 | mapperResponseMsg.read(mapperResponse); |
| 53 | if (mapperResponse.empty()) |
| 54 | { |
Lei YU | 86d8041 | 2017-07-12 13:12:12 +0800 | [diff] [blame] | 55 | using namespace xyz::openbmc_project::Time::Internal; |
| 56 | elog<MethodErr>(MethodError::METHOD_NAME("GetObject"), |
| 57 | MethodError::PATH(path), |
| 58 | MethodError::INTERFACE(interface), |
| 59 | MethodError::MISC("Error reading mapper response")); |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 60 | } |
Ed Tanous | 7aa715b | 2018-05-09 17:28:05 -0700 | [diff] [blame] | 61 | if (mapperResponse.size() < 1){ |
| 62 | return ""; |
| 63 | } |
| 64 | return mapperResponse[0].first; |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 65 | } |
| 66 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 67 | Mode strToMode(const std::string& mode) |
| 68 | { |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 69 | return ModeSetting::convertMethodFromString(mode); |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | Owner strToOwner(const std::string& owner) |
| 73 | { |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 74 | return OwnerSetting::convertOwnersFromString(owner); |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 75 | } |
| 76 | |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 77 | std::string modeToStr(Mode mode) |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 78 | { |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 79 | return sdbusplus::xyz::openbmc_project::Time::server::convertForMessage(mode); |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 80 | } |
| 81 | |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 82 | std::string ownerToStr(Owner owner) |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 83 | { |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 84 | return sdbusplus::xyz::openbmc_project::Time::server::convertForMessage(owner); |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | } // namespace utils |
| 88 | } // namespace time |
| 89 | } // namespace phosphor |