blob: fc8144c68d31c09424bd480a75481baf23e5fd1c [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 */
33const PropertyValue getProperty(sdbusplus::bus::bus& bus,
34 const std::string& objectPath,
35 const std::string& interface,
36 const std::string& propertyName);
37
38/** @brief Set D-Bus property
39 *
40 * @param[in] bus - bus handler
41 * @param[in] objectPath - D-Bus object path
42 * @param[in] interface - D-Bus interface
43 * @param[in] propertyName - D-Bus property name
44 * @param[in] value - The value to be set
45 *
46 * @throw sdbusplus::exception::exception when it fails
47 */
48void setProperty(sdbusplus::bus::bus& bus, const std::string& objectPath,
49 const std::string& interface, const std::string& propertyName,
50 const PropertyValue& value);
51
George Liu0a06e972020-12-17 09:17:04 +080052/**
53 * @brief Merge more files
54 *
55 * @param[in] srcFiles - source files
56 * @param[out] dstFile - destination file
57 * @return
58 **/
59void mergeFiles(std::vector<std::string>& srcFiles, std::string& dstFile);
Adriana Kobylak8a5ccbb2021-01-20 10:57:05 -060060
61namespace internal
62{
63
64/**
65 * @brief Construct an argument vector to be used with an exec command, which
66 * requires the name of the executable to be the first argument, and a
67 * null terminator to be the last.
68 * @param[in] name - Name of the executable
69 * @param[in] args - Optional arguments
70 * @return char* vector
71 */
72template <typename... Arg>
73constexpr auto constructArgv(const char* name, Arg&&... args)
74{
75 std::vector<char*> argV{
76 {const_cast<char*>(name), const_cast<char*>(args)..., nullptr}};
77 return argV;
78}
79
80/**
81 * @brief Helper function to execute command in child process
82 * @param[in] path - Fully qualified name of the executable to run
83 * @param[in] args - Optional arguments
84 * @return 0 on success
85 */
86int executeCmd(const char* path, char** args);
87
88} // namespace internal
89
90/**
91 * @brief Execute command in child process
92 * @param[in] path - Fully qualified name of the executable to run
93 * @param[in] args - Optional arguments
94 * @return 0 on success
95 */
96template <typename... Arg>
97int execute(const char* path, Arg&&... args)
98{
99 auto argArray = internal::constructArgv(path, std::forward<Arg>(args)...);
100
101 return internal::executeCmd(path, argArray.data());
102}
103
Jayashankar Padatha0135602019-04-22 16:22:58 +0530104} // namespace utils