Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 1 | #include "config.h" |
Adriana Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 2 | |
Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 3 | #include <sdbusplus/server.hpp> |
Adriana Kobylak | 58aa750 | 2020-06-08 11:12:11 -0500 | [diff] [blame] | 4 | |
George Liu | 0a06e97 | 2020-12-17 09:17:04 +0800 | [diff] [blame] | 5 | #include <fstream> |
Adriana Kobylak | 58aa750 | 2020-06-08 11:12:11 -0500 | [diff] [blame] | 6 | #include <map> |
Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 7 | #include <string> |
Adriana Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 8 | |
Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 9 | namespace utils |
| 10 | { |
Adriana Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 11 | |
George Liu | fc025e1 | 2021-11-09 19:29:12 +0800 | [diff] [blame] | 12 | using PropertyValue = std::variant<std::string>; |
| 13 | |
Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 14 | /** |
| 15 | * @brief Get the bus service |
| 16 | * |
| 17 | * @return the bus service as a string |
| 18 | **/ |
| 19 | std::string getService(sdbusplus::bus::bus& bus, const std::string& path, |
| 20 | const std::string& interface); |
Adriana Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 21 | |
George Liu | fc025e1 | 2021-11-09 19:29:12 +0800 | [diff] [blame] | 22 | /** @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 | */ |
| 33 | const 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 | */ |
| 48 | void setProperty(sdbusplus::bus::bus& bus, const std::string& objectPath, |
| 49 | const std::string& interface, const std::string& propertyName, |
| 50 | const PropertyValue& value); |
| 51 | |
George Liu | 0a06e97 | 2020-12-17 09:17:04 +0800 | [diff] [blame] | 52 | /** |
| 53 | * @brief Merge more files |
| 54 | * |
| 55 | * @param[in] srcFiles - source files |
| 56 | * @param[out] dstFile - destination file |
| 57 | * @return |
| 58 | **/ |
Lei YU | 0cd6d84 | 2021-12-27 11:56:02 +0800 | [diff] [blame] | 59 | void mergeFiles(const std::vector<std::string>& srcFiles, |
| 60 | const std::string& dstFile); |
Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 61 | |
| 62 | namespace 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 | */ |
| 73 | template <typename... Arg> |
| 74 | constexpr 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 | */ |
| 87 | int 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 | */ |
| 97 | template <typename... Arg> |
| 98 | int 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 Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 105 | } // namespace utils |