17f4fca55SLei YU #pragma once
27f4fca55SLei YU
3ddd54428SLei YU #include "types.hpp"
4ddd54428SLei YU
5947b5346SGeorge Liu #include <phosphor-logging/lg2.hpp>
6ddd54428SLei YU #include <sdbusplus/bus.hpp>
7a741713cSLei YU
8dd42c7faSPavithra Barithaya #include <string_view>
9dc746c0bSGeorge Liu #include <vector>
10dc746c0bSGeorge Liu
117f4fca55SLei YU namespace phosphor
127f4fca55SLei YU {
137f4fca55SLei YU namespace time
147f4fca55SLei YU {
157f4fca55SLei YU namespace utils
167f4fca55SLei YU {
177f4fca55SLei YU
18dc746c0bSGeorge Liu using Path = std::string;
19dc746c0bSGeorge Liu using Service = std::string;
20dc746c0bSGeorge Liu using Interface = std::string;
21dc746c0bSGeorge Liu using Interfaces = std::vector<Interface>;
22dc746c0bSGeorge Liu using MapperResponse =
23dc746c0bSGeorge Liu std::vector<std::pair<Path, std::vector<std::pair<Service, Interfaces>>>>;
24dc746c0bSGeorge Liu
25dd42c7faSPavithra Barithaya PHOSPHOR_LOG2_USING;
26dd42c7faSPavithra Barithaya
27a741713cSLei YU /** @brief The template function to get property from the requested dbus path
28a741713cSLei YU *
29a741713cSLei YU * @param[in] bus - The Dbus bus object
30a741713cSLei YU * @param[in] service - The Dbus service name
31a741713cSLei YU * @param[in] path - The Dbus object path
32a741713cSLei YU * @param[in] interface - The Dbus interface
33a741713cSLei YU * @param[in] propertyName - The property name to get
34a741713cSLei YU *
35a741713cSLei YU * @return The value of the property
36a741713cSLei YU */
37a741713cSLei YU template <typename T>
getProperty(sdbusplus::bus_t & bus,const char * service,const char * path,const char * interface,const char * propertyName)3838679266SPatrick Williams T getProperty(sdbusplus::bus_t& bus, const char* service, const char* path,
39ab4cc6a5SGunnar Mills const char* interface, const char* propertyName)
40a741713cSLei YU {
41ab4cc6a5SGunnar Mills auto method = bus.new_method_call(service, path,
42ab4cc6a5SGunnar Mills "org.freedesktop.DBus.Properties", "Get");
43a741713cSLei YU method.append(interface, propertyName);
4486c83f38SLei YU try
4586c83f38SLei YU {
46c09ac3faSPatrick Williams std::variant<T> value{};
47a741713cSLei YU auto reply = bus.call(method);
48a741713cSLei YU reply.read(value);
495b746c79SPatrick Williams return std::get<T>(value);
50a741713cSLei YU }
5138679266SPatrick Williams catch (const sdbusplus::exception_t& ex)
5286c83f38SLei YU {
53dd42c7faSPavithra Barithaya error("GetProperty call failed, path:{PATH}, interface:{INTF}, "
54947b5346SGeorge Liu "propertyName:{NAME}, error:{ERROR}",
55dd42c7faSPavithra Barithaya "PATH", path, "INTF", interface, "NAME", propertyName, "ERROR",
56dd42c7faSPavithra Barithaya ex);
5786c83f38SLei YU throw std::runtime_error("GetProperty call failed");
5886c83f38SLei YU }
5986c83f38SLei YU }
60a741713cSLei YU
61*e101030bSJason Zhu /** @brief The template function to set property to the requested dbus path
62*e101030bSJason Zhu *
63*e101030bSJason Zhu * @param[in] bus - The Dbus bus object
64*e101030bSJason Zhu * @param[in] service - The Dbus service name
65*e101030bSJason Zhu * @param[in] path - The Dbus object path
66*e101030bSJason Zhu * @param[in] interface - The Dbus interface
67*e101030bSJason Zhu * @param[in] propertyName - The property name to set
68*e101030bSJason Zhu * @param[in] value - the value to set the property to
69*e101030bSJason Zhu *
70*e101030bSJason Zhu */
71*e101030bSJason Zhu template <typename T>
setProperty(sdbusplus::bus_t & bus,const std::string & service,const std::string & path,const std::string & interface,const std::string & propertyName,T & value)72*e101030bSJason Zhu void setProperty(sdbusplus::bus_t& bus, const std::string& service,
73*e101030bSJason Zhu const std::string& path, const std::string& interface,
74*e101030bSJason Zhu const std::string& propertyName, T& value)
75*e101030bSJason Zhu {
76*e101030bSJason Zhu std::variant<T> propertyValue(value);
77*e101030bSJason Zhu
78*e101030bSJason Zhu auto method = bus.new_method_call(service.c_str(), path.c_str(),
79*e101030bSJason Zhu "org.freedesktop.DBus.Properties", "Set");
80*e101030bSJason Zhu
81*e101030bSJason Zhu method.append(interface, propertyName, propertyValue);
82*e101030bSJason Zhu
83*e101030bSJason Zhu try
84*e101030bSJason Zhu {
85*e101030bSJason Zhu auto reply = bus.call(method);
86*e101030bSJason Zhu }
87*e101030bSJason Zhu catch (const sdbusplus::exception_t& ex)
88*e101030bSJason Zhu {
89*e101030bSJason Zhu error("SetProperty call failed, path:{PATH}, interface:{INTF}, "
90*e101030bSJason Zhu "propertyName:{NAME}, error:{ERROR}",
91*e101030bSJason Zhu "PATH", path, "INTF", interface, "NAME", propertyName, "ERROR",
92*e101030bSJason Zhu ex);
93*e101030bSJason Zhu throw std::runtime_error("SetProperty call failed");
94*e101030bSJason Zhu }
95*e101030bSJason Zhu }
96*e101030bSJason Zhu
97dd8e9e40SLei YU /** @brief Get service name from object path and interface
98dd8e9e40SLei YU *
99dd8e9e40SLei YU * @param[in] bus - The Dbus bus object
100dd8e9e40SLei YU * @param[in] path - The Dbus object path
101dd8e9e40SLei YU * @param[in] interface - The Dbus interface
102dd8e9e40SLei YU *
103dd8e9e40SLei YU * @return The name of the service
104dd8e9e40SLei YU */
10538679266SPatrick Williams std::string getService(sdbusplus::bus_t& bus, const char* path,
106dd8e9e40SLei YU const char* interface);
107dd8e9e40SLei YU
108dc746c0bSGeorge Liu /** @brief Get sub tree from root, depth and interfaces
109dc746c0bSGeorge Liu *
110dc746c0bSGeorge Liu * @param[in] bus - The Dbus bus object
111dc746c0bSGeorge Liu * @param[in] root - The root of the tree to search
112dc746c0bSGeorge Liu * @param[in] interfaces - All interfaces in the subtree to search for
113dc746c0bSGeorge Liu * @param[in] depth - The number of path elements to descend
114dc746c0bSGeorge Liu *
115dc746c0bSGeorge Liu * @return The name of the service
116dc746c0bSGeorge Liu *
117dc746c0bSGeorge Liu * @throw sdbusplus::exception_t when it fails
118dc746c0bSGeorge Liu */
119dc746c0bSGeorge Liu MapperResponse getSubTree(sdbusplus::bus_t& bus, const std::string& root,
120dc746c0bSGeorge Liu const Interfaces& interfaces, int32_t depth);
121dc746c0bSGeorge Liu
122ddd54428SLei YU /** @brief Convert a string to enum Mode
123ddd54428SLei YU *
124ddd54428SLei YU * Convert the time mode string to enum.
125ad14354fSLei YU * Valid strings are
126ad14354fSLei YU * "xyz.openbmc_project.Time.Synchronization.Method.NTP"
127ad14354fSLei YU * "xyz.openbmc_project.Time.Synchronization.Method.Manual"
128ddd54428SLei YU * If it's not a valid time mode string, it means something
129ddd54428SLei YU * goes wrong so raise exception.
130ddd54428SLei YU *
131ddd54428SLei YU * @param[in] mode - The string of time mode
132ddd54428SLei YU *
133ddd54428SLei YU * @return The Mode enum
134ddd54428SLei YU */
135ddd54428SLei YU Mode strToMode(const std::string& mode);
136ddd54428SLei YU
137ddd54428SLei YU /** @brief Convert a mode enum to mode string
138ddd54428SLei YU *
139ddd54428SLei YU * @param[in] mode - The Mode enum
140ddd54428SLei YU *
141ddd54428SLei YU * @return The string of the mode
142ddd54428SLei YU */
143ad14354fSLei YU std::string modeToStr(Mode mode);
144ddd54428SLei YU
145ddd54428SLei YU } // namespace utils
146ddd54428SLei YU } // namespace time
147ddd54428SLei YU } // namespace phosphor
148