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 | **/ |
Patrick Williams | bf2bb2b | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 19 | std::string getService(sdbusplus::bus_t& bus, const std::string& path, |
Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 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 | * |
Patrick Williams | bf2bb2b | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 31 | * @throw sdbusplus::exception_t when it fails |
George Liu | fc025e1 | 2021-11-09 19:29:12 +0800 | [diff] [blame] | 32 | */ |
Andrew Geissler | 047f60e | 2022-04-06 15:38:07 -0500 | [diff] [blame] | 33 | template <typename T> |
Patrick Williams | bf2bb2b | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 34 | T getProperty(sdbusplus::bus_t& bus, const std::string& objectPath, |
Andrew Geissler | 047f60e | 2022-04-06 15:38:07 -0500 | [diff] [blame] | 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 Liu | fc025e1 | 2021-11-09 19:29:12 +0800 | [diff] [blame] | 53 | |
| 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 | * |
Patrick Williams | bf2bb2b | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 62 | * @throw sdbusplus::exception_t when it fails |
George Liu | fc025e1 | 2021-11-09 19:29:12 +0800 | [diff] [blame] | 63 | */ |
Patrick Williams | bf2bb2b | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 64 | void setProperty(sdbusplus::bus_t& bus, const std::string& objectPath, |
George Liu | fc025e1 | 2021-11-09 19:29:12 +0800 | [diff] [blame] | 65 | const std::string& interface, const std::string& propertyName, |
| 66 | const PropertyValue& value); |
| 67 | |
George Liu | 0a06e97 | 2020-12-17 09:17:04 +0800 | [diff] [blame] | 68 | /** |
| 69 | * @brief Merge more files |
| 70 | * |
| 71 | * @param[in] srcFiles - source files |
| 72 | * @param[out] dstFile - destination file |
| 73 | * @return |
| 74 | **/ |
Lei YU | 0cd6d84 | 2021-12-27 11:56:02 +0800 | [diff] [blame] | 75 | void mergeFiles(const std::vector<std::string>& srcFiles, |
| 76 | const std::string& dstFile); |
Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 77 | |
| 78 | namespace 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 | */ |
| 89 | template <typename... Arg> |
| 90 | constexpr 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 |
Isaac Kurth | deb86b4 | 2022-03-08 19:47:53 -0600 | [diff] [blame] | 99 | * @param[in] args - Executable plus optional arguments |
| 100 | * @return 0 and command output on success |
Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 101 | */ |
Isaac Kurth | deb86b4 | 2022-03-08 19:47:53 -0600 | [diff] [blame] | 102 | std::pair<int, std::string> executeCmd(char** args); |
Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 103 | |
| 104 | } // namespace internal |
| 105 | |
| 106 | /** |
| 107 | * @brief Execute command in child process |
| 108 | * @param[in] path - Fully qualified name of the executable to run |
| 109 | * @param[in] args - Optional arguments |
Isaac Kurth | deb86b4 | 2022-03-08 19:47:53 -0600 | [diff] [blame] | 110 | * @return 0 and command output on success |
Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 111 | */ |
| 112 | template <typename... Arg> |
Isaac Kurth | deb86b4 | 2022-03-08 19:47:53 -0600 | [diff] [blame] | 113 | std::pair<int, std::string> execute(const char* path, Arg&&... args) |
Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 114 | { |
| 115 | auto argArray = internal::constructArgv(path, std::forward<Arg>(args)...); |
| 116 | |
Isaac Kurth | deb86b4 | 2022-03-08 19:47:53 -0600 | [diff] [blame] | 117 | return internal::executeCmd(argArray.data()); |
Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 118 | } |
| 119 | |
Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 120 | } // namespace utils |