blob: 1a695f4184a31be97c404b3e901d192a17bccd64 [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 **/
Lei YU0cd6d842021-12-27 11:56:02 +080059void mergeFiles(const std::vector<std::string>& srcFiles,
60 const std::string& dstFile);
Adriana Kobylak8a5ccbb2021-01-20 10:57:05 -060061
62namespace internal
63{
64
65/**
66 * @brief Construct an argument vector to be used with an exec command, which
67 * requires the name of the executable to be the first argument, and a
68 * null terminator to be the last.
69 * @param[in] name - Name of the executable
70 * @param[in] args - Optional arguments
71 * @return char* vector
72 */
73template <typename... Arg>
74constexpr auto constructArgv(const char* name, Arg&&... args)
75{
76 std::vector<char*> argV{
77 {const_cast<char*>(name), const_cast<char*>(args)..., nullptr}};
78 return argV;
79}
80
81/**
82 * @brief Helper function to execute command in child process
83 * @param[in] path - Fully qualified name of the executable to run
84 * @param[in] args - Optional arguments
85 * @return 0 on success
86 */
87int executeCmd(const char* path, char** args);
88
89} // namespace internal
90
91/**
92 * @brief Execute command in child process
93 * @param[in] path - Fully qualified name of the executable to run
94 * @param[in] args - Optional arguments
95 * @return 0 on success
96 */
97template <typename... Arg>
98int execute(const char* path, Arg&&... args)
99{
100 auto argArray = internal::constructArgv(path, std::forward<Arg>(args)...);
101
102 return internal::executeCmd(path, argArray.data());
103}
104
Jayashankar Padatha0135602019-04-22 16:22:58 +0530105} // namespace utils