Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 3 | #include "types.hpp" |
| 4 | |
George Liu | 947b534 | 2022-07-01 16:12:18 +0800 | [diff] [blame] | 5 | #include <phosphor-logging/lg2.hpp> |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 6 | #include <sdbusplus/bus.hpp> |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 7 | |
Pavithra Barithaya | dd42c7f | 2022-08-11 05:09:02 -0500 | [diff] [blame] | 8 | #include <string_view> |
George Liu | dc746c0 | 2022-09-02 11:10:55 +0800 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 11 | namespace phosphor |
| 12 | { |
| 13 | namespace time |
| 14 | { |
| 15 | namespace utils |
| 16 | { |
| 17 | |
George Liu | dc746c0 | 2022-09-02 11:10:55 +0800 | [diff] [blame] | 18 | using Path = std::string; |
| 19 | using Service = std::string; |
| 20 | using Interface = std::string; |
| 21 | using Interfaces = std::vector<Interface>; |
| 22 | using MapperResponse = |
| 23 | std::vector<std::pair<Path, std::vector<std::pair<Service, Interfaces>>>>; |
| 24 | |
Pavithra Barithaya | dd42c7f | 2022-08-11 05:09:02 -0500 | [diff] [blame] | 25 | PHOSPHOR_LOG2_USING; |
| 26 | |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 27 | /** @brief The template function to get property from the requested dbus path |
| 28 | * |
| 29 | * @param[in] bus - The Dbus bus object |
| 30 | * @param[in] service - The Dbus service name |
| 31 | * @param[in] path - The Dbus object path |
| 32 | * @param[in] interface - The Dbus interface |
| 33 | * @param[in] propertyName - The property name to get |
| 34 | * |
| 35 | * @return The value of the property |
| 36 | */ |
| 37 | template <typename T> |
Patrick Williams | 3867926 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 38 | T getProperty(sdbusplus::bus_t& bus, const char* service, const char* path, |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 39 | const char* interface, const char* propertyName) |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 40 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 41 | auto method = bus.new_method_call(service, path, |
| 42 | "org.freedesktop.DBus.Properties", "Get"); |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 43 | method.append(interface, propertyName); |
Lei YU | 86c83f3 | 2018-07-13 15:14:56 +0800 | [diff] [blame] | 44 | try |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 45 | { |
Patrick Williams | c09ac3f | 2020-05-13 18:01:29 -0500 | [diff] [blame] | 46 | std::variant<T> value{}; |
Lei YU | 86c83f3 | 2018-07-13 15:14:56 +0800 | [diff] [blame] | 47 | auto reply = bus.call(method); |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 48 | reply.read(value); |
Patrick Williams | 5b746c7 | 2020-05-13 11:49:35 -0500 | [diff] [blame] | 49 | return std::get<T>(value); |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 50 | } |
Patrick Williams | 3867926 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 51 | catch (const sdbusplus::exception_t& ex) |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 52 | { |
Pavithra Barithaya | dd42c7f | 2022-08-11 05:09:02 -0500 | [diff] [blame] | 53 | error("GetProperty call failed, path:{PATH}, interface:{INTF}, " |
| 54 | "propertyName:{NAME}, error:{ERROR}", |
| 55 | "PATH", path, "INTF", interface, "NAME", propertyName, "ERROR", |
| 56 | ex); |
Lei YU | 86c83f3 | 2018-07-13 15:14:56 +0800 | [diff] [blame] | 57 | throw std::runtime_error("GetProperty call failed"); |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 58 | } |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 59 | } |
| 60 | |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 61 | /** @brief Get service name from object path and interface |
| 62 | * |
| 63 | * @param[in] bus - The Dbus bus object |
| 64 | * @param[in] path - The Dbus object path |
| 65 | * @param[in] interface - The Dbus interface |
| 66 | * |
| 67 | * @return The name of the service |
| 68 | */ |
Patrick Williams | 3867926 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 69 | std::string getService(sdbusplus::bus_t& bus, const char* path, |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 70 | const char* interface); |
| 71 | |
George Liu | dc746c0 | 2022-09-02 11:10:55 +0800 | [diff] [blame] | 72 | /** @brief Get sub tree from root, depth and interfaces |
| 73 | * |
| 74 | * @param[in] bus - The Dbus bus object |
| 75 | * @param[in] root - The root of the tree to search |
| 76 | * @param[in] interfaces - All interfaces in the subtree to search for |
| 77 | * @param[in] depth - The number of path elements to descend |
| 78 | * |
| 79 | * @return The name of the service |
| 80 | * |
| 81 | * @throw sdbusplus::exception_t when it fails |
| 82 | */ |
| 83 | MapperResponse getSubTree(sdbusplus::bus_t& bus, const std::string& root, |
| 84 | const Interfaces& interfaces, int32_t depth); |
| 85 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 86 | /** @brief Convert a string to enum Mode |
| 87 | * |
| 88 | * Convert the time mode string to enum. |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 89 | * Valid strings are |
| 90 | * "xyz.openbmc_project.Time.Synchronization.Method.NTP" |
| 91 | * "xyz.openbmc_project.Time.Synchronization.Method.Manual" |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 92 | * If it's not a valid time mode string, it means something |
| 93 | * goes wrong so raise exception. |
| 94 | * |
| 95 | * @param[in] mode - The string of time mode |
| 96 | * |
| 97 | * @return The Mode enum |
| 98 | */ |
| 99 | Mode strToMode(const std::string& mode); |
| 100 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 101 | /** @brief Convert a mode enum to mode string |
| 102 | * |
| 103 | * @param[in] mode - The Mode enum |
| 104 | * |
| 105 | * @return The string of the mode |
| 106 | */ |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 107 | std::string modeToStr(Mode mode); |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 108 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 109 | } // namespace utils |
| 110 | } // namespace time |
| 111 | } // namespace phosphor |