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