Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 1 | #include "utils.hpp" |
| 2 | |
| 3 | #include <phosphor-logging/log.hpp> |
| 4 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 5 | namespace phosphor |
| 6 | { |
| 7 | namespace time |
| 8 | { |
| 9 | |
| 10 | namespace // anonymous |
| 11 | { |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 12 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 13 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 14 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 15 | } // namespace |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 16 | |
| 17 | namespace utils |
| 18 | { |
| 19 | |
| 20 | using namespace phosphor::logging; |
| 21 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 22 | std::string getService(sdbusplus::bus::bus& bus, const char* path, |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 23 | const char* interface) |
| 24 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 25 | auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 26 | MAPPER_INTERFACE, "GetObject"); |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 27 | |
| 28 | mapper.append(path, std::vector<std::string>({interface})); |
Lei YU | 86c83f3 | 2018-07-13 15:14:56 +0800 | [diff] [blame] | 29 | try |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 30 | { |
Lei YU | 86c83f3 | 2018-07-13 15:14:56 +0800 | [diff] [blame] | 31 | auto mapperResponseMsg = bus.call(mapper); |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 32 | |
Lei YU | 86c83f3 | 2018-07-13 15:14:56 +0800 | [diff] [blame] | 33 | std::vector<std::pair<std::string, std::vector<std::string>>> |
| 34 | mapperResponse; |
| 35 | mapperResponseMsg.read(mapperResponse); |
| 36 | if (mapperResponse.empty()) |
| 37 | { |
| 38 | log<level::ERR>("Error reading mapper response"); |
| 39 | throw std::runtime_error("Error reading mapper response"); |
| 40 | } |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 41 | if (mapperResponse.size() < 1) |
| 42 | { |
Lei YU | 86c83f3 | 2018-07-13 15:14:56 +0800 | [diff] [blame] | 43 | return ""; |
| 44 | } |
| 45 | return mapperResponse[0].first; |
| 46 | } |
| 47 | catch (const sdbusplus::exception::SdBusError& ex) |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 48 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 49 | log<level::ERR>("Mapper call failed", entry("METHOD=%d", "GetObject"), |
Lei YU | 86c83f3 | 2018-07-13 15:14:56 +0800 | [diff] [blame] | 50 | entry("PATH=%s", path), |
| 51 | entry("INTERFACE=%s", interface)); |
| 52 | throw std::runtime_error("Mapper call failed"); |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 53 | } |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 54 | } |
| 55 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 56 | Mode strToMode(const std::string& mode) |
| 57 | { |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 58 | return ModeSetting::convertMethodFromString(mode); |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 59 | } |
| 60 | |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 61 | std::string modeToStr(Mode mode) |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 62 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 63 | return sdbusplus::xyz::openbmc_project::Time::server::convertForMessage( |
| 64 | mode); |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 65 | } |
| 66 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 67 | } // namespace utils |
| 68 | } // namespace time |
| 69 | } // namespace phosphor |