blob: dd81314499af1a2ccfba8bfdf901725df6e641a9 [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
George Liudc746c02022-09-02 11:10:55 +08008#include <vector>
9
Lei YU7f4fca52017-02-23 15:15:51 +080010namespace phosphor
11{
12namespace time
13{
14namespace utils
15{
16
George Liudc746c02022-09-02 11:10:55 +080017using Path = std::string;
18using Service = std::string;
19using Interface = std::string;
20using Interfaces = std::vector<Interface>;
21using MapperResponse =
22 std::vector<std::pair<Path, std::vector<std::pair<Service, Interfaces>>>>;
23
Lei YUa7417132017-02-23 15:24:05 +080024/** @brief The template function to get property from the requested dbus path
25 *
26 * @param[in] bus - The Dbus bus object
27 * @param[in] service - The Dbus service name
28 * @param[in] path - The Dbus object path
29 * @param[in] interface - The Dbus interface
30 * @param[in] propertyName - The property name to get
31 *
32 * @return The value of the property
33 */
34template <typename T>
Patrick Williams38679262022-07-22 19:26:55 -050035T getProperty(sdbusplus::bus_t& bus, const char* service, const char* path,
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050036 const char* interface, const char* propertyName)
Lei YUa7417132017-02-23 15:24:05 +080037{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050038 auto method = bus.new_method_call(service, path,
39 "org.freedesktop.DBus.Properties", "Get");
Lei YUa7417132017-02-23 15:24:05 +080040 method.append(interface, propertyName);
Lei YU86c83f32018-07-13 15:14:56 +080041 try
Lei YUa7417132017-02-23 15:24:05 +080042 {
Patrick Williamsc09ac3f2020-05-13 18:01:29 -050043 std::variant<T> value{};
Lei YU86c83f32018-07-13 15:14:56 +080044 auto reply = bus.call(method);
Lei YUa7417132017-02-23 15:24:05 +080045 reply.read(value);
Patrick Williams5b746c72020-05-13 11:49:35 -050046 return std::get<T>(value);
Lei YUa7417132017-02-23 15:24:05 +080047 }
Patrick Williams38679262022-07-22 19:26:55 -050048 catch (const sdbusplus::exception_t& ex)
Lei YUa7417132017-02-23 15:24:05 +080049 {
George Liu947b5342022-07-01 16:12:18 +080050 lg2::error("GetProperty call failed, path:{PATH}, interface:{INTF}, "
51 "propertyName:{NAME}, error:{ERROR}",
52 "PATH", path, "INTF", interface, "NAME", propertyName,
53 "ERROR", ex);
Lei YU86c83f32018-07-13 15:14:56 +080054 throw std::runtime_error("GetProperty call failed");
Lei YUa7417132017-02-23 15:24:05 +080055 }
Lei YUa7417132017-02-23 15:24:05 +080056}
57
Lei YUdd8e9e42017-04-19 17:46:58 +080058/** @brief Get service name from object path and interface
59 *
60 * @param[in] bus - The Dbus bus object
61 * @param[in] path - The Dbus object path
62 * @param[in] interface - The Dbus interface
63 *
64 * @return The name of the service
65 */
Patrick Williams38679262022-07-22 19:26:55 -050066std::string getService(sdbusplus::bus_t& bus, const char* path,
Lei YUdd8e9e42017-04-19 17:46:58 +080067 const char* interface);
68
George Liudc746c02022-09-02 11:10:55 +080069/** @brief Get sub tree from root, depth and interfaces
70 *
71 * @param[in] bus - The Dbus bus object
72 * @param[in] root - The root of the tree to search
73 * @param[in] interfaces - All interfaces in the subtree to search for
74 * @param[in] depth - The number of path elements to descend
75 *
76 * @return The name of the service
77 *
78 * @throw sdbusplus::exception_t when it fails
79 */
80MapperResponse getSubTree(sdbusplus::bus_t& bus, const std::string& root,
81 const Interfaces& interfaces, int32_t depth);
82
Lei YUddd54422017-04-18 16:38:44 +080083/** @brief Convert a string to enum Mode
84 *
85 * Convert the time mode string to enum.
Lei YUad143542017-07-25 14:27:07 +080086 * Valid strings are
87 * "xyz.openbmc_project.Time.Synchronization.Method.NTP"
88 * "xyz.openbmc_project.Time.Synchronization.Method.Manual"
Lei YUddd54422017-04-18 16:38:44 +080089 * If it's not a valid time mode string, it means something
90 * goes wrong so raise exception.
91 *
92 * @param[in] mode - The string of time mode
93 *
94 * @return The Mode enum
95 */
96Mode strToMode(const std::string& mode);
97
Lei YUddd54422017-04-18 16:38:44 +080098/** @brief Convert a mode enum to mode string
99 *
100 * @param[in] mode - The Mode enum
101 *
102 * @return The string of the mode
103 */
Lei YUad143542017-07-25 14:27:07 +0800104std::string modeToStr(Mode mode);
Lei YUddd54422017-04-18 16:38:44 +0800105
Lei YUddd54422017-04-18 16:38:44 +0800106} // namespace utils
107} // namespace time
108} // namespace phosphor