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 | |
Patrick Williams | 3867926 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 18 | std::string getService(sdbusplus::bus_t& 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 | } |
George Liu | 261525d | 2022-07-01 17:02:55 +0800 | [diff] [blame] | 37 | |
Lei YU | 86c83f3 | 2018-07-13 15:14:56 +0800 | [diff] [blame] | 38 | return mapperResponse[0].first; |
| 39 | } |
Patrick Williams | 3867926 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 40 | catch (const sdbusplus::exception_t& ex) |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 41 | { |
George Liu | 947b534 | 2022-07-01 16:12:18 +0800 | [diff] [blame] | 42 | lg2::error( |
| 43 | "Mapper call failed: path:{PATH}, interface:{INTF}, error:{ERROR}", |
| 44 | "PATH", path, "INTF", interface, "ERROR", ex); |
Lei YU | 86c83f3 | 2018-07-13 15:14:56 +0800 | [diff] [blame] | 45 | throw std::runtime_error("Mapper call failed"); |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 46 | } |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 47 | } |
| 48 | |
George Liu | dc746c0 | 2022-09-02 11:10:55 +0800 | [diff] [blame] | 49 | MapperResponse getSubTree(sdbusplus::bus_t& bus, const std::string& root, |
| 50 | const Interfaces& interfaces, int32_t depth) |
| 51 | { |
| 52 | auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 53 | MAPPER_INTERFACE, "GetSubTree"); |
| 54 | mapperCall.append(root); |
| 55 | mapperCall.append(depth); |
| 56 | mapperCall.append(interfaces); |
| 57 | |
| 58 | auto response = bus.call(mapperCall); |
| 59 | |
| 60 | MapperResponse result; |
| 61 | response.read(result); |
| 62 | return result; |
| 63 | } |
| 64 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 65 | Mode strToMode(const std::string& mode) |
| 66 | { |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 67 | return ModeSetting::convertMethodFromString(mode); |
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 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 72 | return sdbusplus::xyz::openbmc_project::Time::server::convertForMessage( |
| 73 | mode); |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 74 | } |
| 75 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 76 | } // namespace utils |
| 77 | } // namespace time |
| 78 | } // namespace phosphor |