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