blob: 21749386de038b126c5599f319d3486f46c56712 [file] [log] [blame]
Jayashankar Padatha0135602019-04-22 16:22:58 +05301#include "config.h"
Adriana Kobylak5ed9b2d2018-09-06 13:15:34 -05002
Jayashankar Padatha0135602019-04-22 16:22:58 +05303#include <sdbusplus/server.hpp>
Adriana Kobylak58aa7502020-06-08 11:12:11 -05004
George Liu0a06e972020-12-17 09:17:04 +08005#include <fstream>
Adriana Kobylak58aa7502020-06-08 11:12:11 -05006#include <map>
Jayashankar Padatha0135602019-04-22 16:22:58 +05307#include <string>
Adriana Kobylak5ed9b2d2018-09-06 13:15:34 -05008
Jayashankar Padatha0135602019-04-22 16:22:58 +05309namespace utils
10{
Adriana Kobylak5ed9b2d2018-09-06 13:15:34 -050011
George Liufc025e12021-11-09 19:29:12 +080012using PropertyValue = std::variant<std::string>;
13
Jayashankar Padatha0135602019-04-22 16:22:58 +053014/**
15 * @brief Get the bus service
16 *
17 * @return the bus service as a string
18 **/
19std::string getService(sdbusplus::bus::bus& bus, const std::string& path,
20 const std::string& interface);
Adriana Kobylak5ed9b2d2018-09-06 13:15:34 -050021
George Liufc025e12021-11-09 19:29:12 +080022/** @brief Get property(type: variant)
23 *
24 * @param[in] bus - bus handler
25 * @param[in] objectPath - D-Bus object path
26 * @param[in] interface - D-Bus interface
27 * @param[in] propertyName - D-Bus property name
28 *
29 * @return The value of the property(type: variant)
30 *
31 * @throw sdbusplus::exception::exception when it fails
32 */
Andrew Geissler047f60e2022-04-06 15:38:07 -050033template <typename T>
34T getProperty(sdbusplus::bus::bus& bus, const std::string& objectPath,
35 const std::string& interface, const std::string& propertyName)
36{
37 std::variant<T> value{};
38 auto service = getService(bus, objectPath, interface);
39 if (service.empty())
40 {
41 return std::get<T>(value);
42 }
43
44 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
45 "org.freedesktop.DBus.Properties", "Get");
46 method.append(interface, propertyName);
47
48 auto reply = bus.call(method);
49 reply.read(value);
50
51 return std::get<T>(value);
52}
George Liufc025e12021-11-09 19:29:12 +080053
54/** @brief Set D-Bus property
55 *
56 * @param[in] bus - bus handler
57 * @param[in] objectPath - D-Bus object path
58 * @param[in] interface - D-Bus interface
59 * @param[in] propertyName - D-Bus property name
60 * @param[in] value - The value to be set
61 *
62 * @throw sdbusplus::exception::exception when it fails
63 */
64void setProperty(sdbusplus::bus::bus& bus, const std::string& objectPath,
65 const std::string& interface, const std::string& propertyName,
66 const PropertyValue& value);
67
George Liu0a06e972020-12-17 09:17:04 +080068/**
69 * @brief Merge more files
70 *
71 * @param[in] srcFiles - source files
72 * @param[out] dstFile - destination file
73 * @return
74 **/
Lei YU0cd6d842021-12-27 11:56:02 +080075void mergeFiles(const std::vector<std::string>& srcFiles,
76 const std::string& dstFile);
Adriana Kobylak8a5ccbb2021-01-20 10:57:05 -060077
78namespace internal
79{
80
81/**
82 * @brief Construct an argument vector to be used with an exec command, which
83 * requires the name of the executable to be the first argument, and a
84 * null terminator to be the last.
85 * @param[in] name - Name of the executable
86 * @param[in] args - Optional arguments
87 * @return char* vector
88 */
89template <typename... Arg>
90constexpr auto constructArgv(const char* name, Arg&&... args)
91{
92 std::vector<char*> argV{
93 {const_cast<char*>(name), const_cast<char*>(args)..., nullptr}};
94 return argV;
95}
96
97/**
98 * @brief Helper function to execute command in child process
99 * @param[in] path - Fully qualified name of the executable to run
100 * @param[in] args - Optional arguments
101 * @return 0 on success
102 */
103int executeCmd(const char* path, char** args);
104
105} // namespace internal
106
107/**
108 * @brief Execute command in child process
109 * @param[in] path - Fully qualified name of the executable to run
110 * @param[in] args - Optional arguments
111 * @return 0 on success
112 */
113template <typename... Arg>
114int execute(const char* path, Arg&&... args)
115{
116 auto argArray = internal::constructArgv(path, std::forward<Arg>(args)...);
117
118 return internal::executeCmd(path, argArray.data());
119}
120
Jayashankar Padatha0135602019-04-22 16:22:58 +0530121} // namespace utils