blob: 34a67e82674c888b7c489d07c0dbb9fda364baf5 [file] [log] [blame]
Lei YU7f4fca52017-02-23 15:15:51 +08001#pragma once
2
Lei YUddd54422017-04-18 16:38:44 +08003#include "types.hpp"
4
George Liu947b5342022-07-01 16:12:18 +08005#include <phosphor-logging/lg2.hpp>
Lei YUddd54422017-04-18 16:38:44 +08006#include <sdbusplus/bus.hpp>
Lei YUa7417132017-02-23 15:24:05 +08007
Pavithra Barithayadd42c7f2022-08-11 05:09:02 -05008#include <string_view>
George Liudc746c02022-09-02 11:10:55 +08009#include <vector>
10
Lei YU7f4fca52017-02-23 15:15:51 +080011namespace phosphor
12{
13namespace time
14{
15namespace utils
16{
17
George Liudc746c02022-09-02 11:10:55 +080018using Path = std::string;
19using Service = std::string;
20using Interface = std::string;
21using Interfaces = std::vector<Interface>;
22using MapperResponse =
23 std::vector<std::pair<Path, std::vector<std::pair<Service, Interfaces>>>>;
24
Pavithra Barithayadd42c7f2022-08-11 05:09:02 -050025PHOSPHOR_LOG2_USING;
26
Lei YUa7417132017-02-23 15:24:05 +080027/** @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 */
37template <typename T>
Patrick Williams38679262022-07-22 19:26:55 -050038T getProperty(sdbusplus::bus_t& bus, const char* service, const char* path,
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050039 const char* interface, const char* propertyName)
Lei YUa7417132017-02-23 15:24:05 +080040{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050041 auto method = bus.new_method_call(service, path,
42 "org.freedesktop.DBus.Properties", "Get");
Lei YUa7417132017-02-23 15:24:05 +080043 method.append(interface, propertyName);
Lei YU86c83f32018-07-13 15:14:56 +080044 try
Lei YUa7417132017-02-23 15:24:05 +080045 {
Patrick Williamsc09ac3f2020-05-13 18:01:29 -050046 std::variant<T> value{};
Lei YU86c83f32018-07-13 15:14:56 +080047 auto reply = bus.call(method);
Lei YUa7417132017-02-23 15:24:05 +080048 reply.read(value);
Patrick Williams5b746c72020-05-13 11:49:35 -050049 return std::get<T>(value);
Lei YUa7417132017-02-23 15:24:05 +080050 }
Patrick Williams38679262022-07-22 19:26:55 -050051 catch (const sdbusplus::exception_t& ex)
Lei YUa7417132017-02-23 15:24:05 +080052 {
Pavithra Barithayadd42c7f2022-08-11 05:09:02 -050053 error("GetProperty call failed, path:{PATH}, interface:{INTF}, "
54 "propertyName:{NAME}, error:{ERROR}",
55 "PATH", path, "INTF", interface, "NAME", propertyName, "ERROR",
56 ex);
Lei YU86c83f32018-07-13 15:14:56 +080057 throw std::runtime_error("GetProperty call failed");
Lei YUa7417132017-02-23 15:24:05 +080058 }
Lei YUa7417132017-02-23 15:24:05 +080059}
60
Lei YUdd8e9e42017-04-19 17:46:58 +080061/** @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 Williams38679262022-07-22 19:26:55 -050069std::string getService(sdbusplus::bus_t& bus, const char* path,
Lei YUdd8e9e42017-04-19 17:46:58 +080070 const char* interface);
71
George Liudc746c02022-09-02 11:10:55 +080072/** @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 */
83MapperResponse getSubTree(sdbusplus::bus_t& bus, const std::string& root,
84 const Interfaces& interfaces, int32_t depth);
85
Lei YUddd54422017-04-18 16:38:44 +080086/** @brief Convert a string to enum Mode
87 *
88 * Convert the time mode string to enum.
Lei YUad143542017-07-25 14:27:07 +080089 * Valid strings are
90 * "xyz.openbmc_project.Time.Synchronization.Method.NTP"
91 * "xyz.openbmc_project.Time.Synchronization.Method.Manual"
Lei YUddd54422017-04-18 16:38:44 +080092 * 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 */
99Mode strToMode(const std::string& mode);
100
Lei YUddd54422017-04-18 16:38:44 +0800101/** @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 YUad143542017-07-25 14:27:07 +0800107std::string modeToStr(Mode mode);
Lei YUddd54422017-04-18 16:38:44 +0800108
Lei YUddd54422017-04-18 16:38:44 +0800109} // namespace utils
110} // namespace time
111} // namespace phosphor