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 | { |
Pavithra Barithaya | 864e173 | 2023-04-11 04:30:23 -0500 | [diff] [blame] | 10 | constexpr auto mapperBusname = "xyz.openbmc_project.ObjectMapper"; |
| 11 | constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper"; |
| 12 | constexpr auto mapperInterface = "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 | |
Pavithra Barithaya | dd42c7f | 2022-08-11 05:09:02 -0500 | [diff] [blame] | 18 | PHOSPHOR_LOG2_USING; |
| 19 | |
Patrick Williams | 3867926 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 20 | std::string getService(sdbusplus::bus_t& bus, const char* path, |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 21 | const char* interface) |
| 22 | { |
Pavithra Barithaya | 864e173 | 2023-04-11 04:30:23 -0500 | [diff] [blame] | 23 | auto mapper = bus.new_method_call(mapperBusname, mapperPath, |
| 24 | mapperInterface, "GetObject"); |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 25 | |
| 26 | mapper.append(path, std::vector<std::string>({interface})); |
Lei YU | 86c83f3 | 2018-07-13 15:14:56 +0800 | [diff] [blame] | 27 | try |
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 | auto mapperResponseMsg = bus.call(mapper); |
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 | std::vector<std::pair<std::string, std::vector<std::string>>> |
| 32 | mapperResponse; |
| 33 | mapperResponseMsg.read(mapperResponse); |
| 34 | if (mapperResponse.empty()) |
| 35 | { |
Pavithra Barithaya | dd42c7f | 2022-08-11 05:09:02 -0500 | [diff] [blame] | 36 | error("Error reading mapper response"); |
Lei YU | 86c83f3 | 2018-07-13 15:14:56 +0800 | [diff] [blame] | 37 | throw std::runtime_error("Error reading mapper response"); |
| 38 | } |
George Liu | 261525d | 2022-07-01 17:02:55 +0800 | [diff] [blame] | 39 | |
Lei YU | 86c83f3 | 2018-07-13 15:14:56 +0800 | [diff] [blame] | 40 | return mapperResponse[0].first; |
| 41 | } |
Patrick Williams | 3867926 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 42 | catch (const sdbusplus::exception_t& ex) |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 43 | { |
Pavithra Barithaya | dd42c7f | 2022-08-11 05:09:02 -0500 | [diff] [blame] | 44 | error( |
George Liu | 947b534 | 2022-07-01 16:12:18 +0800 | [diff] [blame] | 45 | "Mapper call failed: path:{PATH}, interface:{INTF}, error:{ERROR}", |
| 46 | "PATH", path, "INTF", interface, "ERROR", ex); |
Lei YU | 86c83f3 | 2018-07-13 15:14:56 +0800 | [diff] [blame] | 47 | throw std::runtime_error("Mapper call failed"); |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 48 | } |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 49 | } |
| 50 | |
George Liu | dc746c0 | 2022-09-02 11:10:55 +0800 | [diff] [blame] | 51 | MapperResponse getSubTree(sdbusplus::bus_t& bus, const std::string& root, |
| 52 | const Interfaces& interfaces, int32_t depth) |
| 53 | { |
Pavithra Barithaya | 864e173 | 2023-04-11 04:30:23 -0500 | [diff] [blame] | 54 | auto mapperCall = bus.new_method_call(mapperBusname, mapperPath, |
| 55 | mapperInterface, "GetSubTree"); |
George Liu | dc746c0 | 2022-09-02 11:10:55 +0800 | [diff] [blame] | 56 | mapperCall.append(root); |
| 57 | mapperCall.append(depth); |
| 58 | mapperCall.append(interfaces); |
| 59 | |
| 60 | auto response = bus.call(mapperCall); |
| 61 | |
| 62 | MapperResponse result; |
| 63 | response.read(result); |
| 64 | return result; |
| 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 | |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 72 | std::string modeToStr(Mode mode) |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 73 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 74 | return sdbusplus::xyz::openbmc_project::Time::server::convertForMessage( |
| 75 | mode); |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 76 | } |
| 77 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 78 | } // namespace utils |
| 79 | } // namespace time |
| 80 | } // namespace phosphor |