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 | |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 8 | namespace phosphor |
| 9 | { |
| 10 | namespace time |
| 11 | { |
| 12 | namespace utils |
| 13 | { |
| 14 | |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 15 | /** @brief The template function to get property from the requested dbus path |
| 16 | * |
| 17 | * @param[in] bus - The Dbus bus object |
| 18 | * @param[in] service - The Dbus service name |
| 19 | * @param[in] path - The Dbus object path |
| 20 | * @param[in] interface - The Dbus interface |
| 21 | * @param[in] propertyName - The property name to get |
| 22 | * |
| 23 | * @return The value of the property |
| 24 | */ |
| 25 | template <typename T> |
Patrick Williams | 3867926 | 2022-07-22 19:26:55 -0500 | [diff] [blame^] | 26 | T getProperty(sdbusplus::bus_t& bus, const char* service, const char* path, |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 27 | const char* interface, const char* propertyName) |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 28 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 29 | auto method = bus.new_method_call(service, path, |
| 30 | "org.freedesktop.DBus.Properties", "Get"); |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 31 | method.append(interface, propertyName); |
Lei YU | 86c83f3 | 2018-07-13 15:14:56 +0800 | [diff] [blame] | 32 | try |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 33 | { |
Patrick Williams | c09ac3f | 2020-05-13 18:01:29 -0500 | [diff] [blame] | 34 | std::variant<T> value{}; |
Lei YU | 86c83f3 | 2018-07-13 15:14:56 +0800 | [diff] [blame] | 35 | auto reply = bus.call(method); |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 36 | reply.read(value); |
Patrick Williams | 5b746c7 | 2020-05-13 11:49:35 -0500 | [diff] [blame] | 37 | return std::get<T>(value); |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 38 | } |
Patrick Williams | 3867926 | 2022-07-22 19:26:55 -0500 | [diff] [blame^] | 39 | catch (const sdbusplus::exception_t& ex) |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 40 | { |
George Liu | 947b534 | 2022-07-01 16:12:18 +0800 | [diff] [blame] | 41 | lg2::error("GetProperty call failed, path:{PATH}, interface:{INTF}, " |
| 42 | "propertyName:{NAME}, error:{ERROR}", |
| 43 | "PATH", path, "INTF", interface, "NAME", propertyName, |
| 44 | "ERROR", ex); |
Lei YU | 86c83f3 | 2018-07-13 15:14:56 +0800 | [diff] [blame] | 45 | throw std::runtime_error("GetProperty call failed"); |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 46 | } |
Lei YU | a741713 | 2017-02-23 15:24:05 +0800 | [diff] [blame] | 47 | } |
| 48 | |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 49 | /** @brief Get service name from object path and interface |
| 50 | * |
| 51 | * @param[in] bus - The Dbus bus object |
| 52 | * @param[in] path - The Dbus object path |
| 53 | * @param[in] interface - The Dbus interface |
| 54 | * |
| 55 | * @return The name of the service |
| 56 | */ |
Patrick Williams | 3867926 | 2022-07-22 19:26:55 -0500 | [diff] [blame^] | 57 | std::string getService(sdbusplus::bus_t& bus, const char* path, |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 58 | const char* interface); |
| 59 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 60 | /** @brief Convert a string to enum Mode |
| 61 | * |
| 62 | * Convert the time mode string to enum. |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 63 | * Valid strings are |
| 64 | * "xyz.openbmc_project.Time.Synchronization.Method.NTP" |
| 65 | * "xyz.openbmc_project.Time.Synchronization.Method.Manual" |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 66 | * If it's not a valid time mode string, it means something |
| 67 | * goes wrong so raise exception. |
| 68 | * |
| 69 | * @param[in] mode - The string of time mode |
| 70 | * |
| 71 | * @return The Mode enum |
| 72 | */ |
| 73 | Mode strToMode(const std::string& mode); |
| 74 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 75 | /** @brief Convert a mode enum to mode string |
| 76 | * |
| 77 | * @param[in] mode - The Mode enum |
| 78 | * |
| 79 | * @return The string of the mode |
| 80 | */ |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 81 | std::string modeToStr(Mode mode); |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 82 | |
Lei YU | ddd5442 | 2017-04-18 16:38:44 +0800 | [diff] [blame] | 83 | } // namespace utils |
| 84 | } // namespace time |
| 85 | } // namespace phosphor |