1 #pragma once 2 3 #include "types.hpp" 4 5 #include <phosphor-logging/lg2.hpp> 6 #include <sdbusplus/bus.hpp> 7 8 namespace phosphor 9 { 10 namespace time 11 { 12 namespace utils 13 { 14 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> 26 T getProperty(sdbusplus::bus_t& bus, const char* service, const char* path, 27 const char* interface, const char* propertyName) 28 { 29 auto method = bus.new_method_call(service, path, 30 "org.freedesktop.DBus.Properties", "Get"); 31 method.append(interface, propertyName); 32 try 33 { 34 std::variant<T> value{}; 35 auto reply = bus.call(method); 36 reply.read(value); 37 return std::get<T>(value); 38 } 39 catch (const sdbusplus::exception_t& ex) 40 { 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); 45 throw std::runtime_error("GetProperty call failed"); 46 } 47 } 48 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 */ 57 std::string getService(sdbusplus::bus_t& bus, const char* path, 58 const char* interface); 59 60 /** @brief Convert a string to enum Mode 61 * 62 * Convert the time mode string to enum. 63 * Valid strings are 64 * "xyz.openbmc_project.Time.Synchronization.Method.NTP" 65 * "xyz.openbmc_project.Time.Synchronization.Method.Manual" 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 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 */ 81 std::string modeToStr(Mode mode); 82 83 } // namespace utils 84 } // namespace time 85 } // namespace phosphor 86