| Adriana Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 1 | #include "utils.hpp" | 
|  | 2 |  | 
| Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 3 | #include <unistd.h> | 
|  | 4 |  | 
| Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 5 | #include <phosphor-logging/lg2.hpp> | 
| Adriana Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 6 |  | 
| Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 7 | namespace utils | 
| Adriana Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 8 | { | 
| Adriana Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 9 |  | 
| Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 10 | PHOSPHOR_LOG2_USING; | 
| Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 11 |  | 
| Patrick Williams | bf2bb2b | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 12 | std::string getService(sdbusplus::bus_t& bus, const std::string& path, | 
| Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 13 | const std::string& interface) | 
|  | 14 | { | 
|  | 15 | auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, | 
|  | 16 | MAPPER_BUSNAME, "GetObject"); | 
|  | 17 |  | 
|  | 18 | method.append(path); | 
|  | 19 | method.append(std::vector<std::string>({interface})); | 
|  | 20 |  | 
|  | 21 | std::vector<std::pair<std::string, std::vector<std::string>>> response; | 
|  | 22 |  | 
|  | 23 | try | 
| Adriana Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 24 | { | 
| Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 25 | auto reply = bus.call(method); | 
|  | 26 | reply.read(response); | 
|  | 27 | if (response.empty()) | 
|  | 28 | { | 
| Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 29 | error( | 
|  | 30 | "Empty response from mapper for getting service name: {PATH} {INTERFACE}", | 
|  | 31 | "PATH", path, "INTERFACE", interface); | 
| Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 32 | return std::string{}; | 
|  | 33 | } | 
| Adriana Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 34 | } | 
| Patrick Williams | bf2bb2b | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 35 | catch (const sdbusplus::exception_t& e) | 
| Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 36 | { | 
| Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 37 | error("Error in mapper method call for ({PATH}, {INTERFACE}: {ERROR}", | 
|  | 38 | "ERROR", e, "PATH", path, "INTERFACE", interface); | 
| Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 39 | return std::string{}; | 
|  | 40 | } | 
|  | 41 | return response[0].first; | 
| Adriana Kobylak | 5ed9b2d | 2018-09-06 13:15:34 -0500 | [diff] [blame] | 42 | } | 
|  | 43 |  | 
| Patrick Williams | bf2bb2b | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 44 | void setProperty(sdbusplus::bus_t& bus, const std::string& objectPath, | 
| George Liu | fc025e1 | 2021-11-09 19:29:12 +0800 | [diff] [blame] | 45 | const std::string& interface, const std::string& propertyName, | 
|  | 46 | const PropertyValue& value) | 
|  | 47 | { | 
|  | 48 | auto service = getService(bus, objectPath, interface); | 
|  | 49 | if (service.empty()) | 
|  | 50 | { | 
|  | 51 | return; | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | auto method = bus.new_method_call(service.c_str(), objectPath.c_str(), | 
|  | 55 | "org.freedesktop.DBus.Properties", "Set"); | 
|  | 56 | method.append(interface.c_str(), propertyName.c_str(), value); | 
|  | 57 |  | 
|  | 58 | bus.call_noreply(method); | 
|  | 59 | } | 
|  | 60 |  | 
| Lei YU | 0cd6d84 | 2021-12-27 11:56:02 +0800 | [diff] [blame] | 61 | void mergeFiles(const std::vector<std::string>& srcFiles, | 
|  | 62 | const std::string& dstFile) | 
| George Liu | 0a06e97 | 2020-12-17 09:17:04 +0800 | [diff] [blame] | 63 | { | 
|  | 64 | std::ofstream outFile(dstFile, std::ios::out); | 
| Lei YU | 0cd6d84 | 2021-12-27 11:56:02 +0800 | [diff] [blame] | 65 | for (const auto& file : srcFiles) | 
| George Liu | 0a06e97 | 2020-12-17 09:17:04 +0800 | [diff] [blame] | 66 | { | 
|  | 67 | std::ifstream inFile; | 
|  | 68 | inFile.open(file, std::ios_base::in); | 
|  | 69 | if (!inFile) | 
|  | 70 | { | 
|  | 71 | continue; | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | inFile.peek(); | 
|  | 75 | if (inFile.eof()) | 
|  | 76 | { | 
|  | 77 | inFile.close(); | 
|  | 78 | continue; | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | outFile << inFile.rdbuf(); | 
|  | 82 | inFile.close(); | 
|  | 83 | } | 
|  | 84 | outFile.close(); | 
|  | 85 | } | 
|  | 86 |  | 
| Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 87 | namespace internal | 
|  | 88 | { | 
|  | 89 |  | 
|  | 90 | /* @brief Helper function to build a string from command arguments */ | 
| Isaac Kurth | deb86b4 | 2022-03-08 19:47:53 -0600 | [diff] [blame] | 91 | static std::string buildCommandStr(char** args) | 
| Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 92 | { | 
| Isaac Kurth | deb86b4 | 2022-03-08 19:47:53 -0600 | [diff] [blame] | 93 | std::string command = ""; | 
| Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 94 | for (int i = 0; args[i]; i++) | 
|  | 95 | { | 
| Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 96 | command += args[i]; | 
| Isaac Kurth | deb86b4 | 2022-03-08 19:47:53 -0600 | [diff] [blame] | 97 | command += " "; | 
| Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 98 | } | 
|  | 99 | return command; | 
|  | 100 | } | 
|  | 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 | { | 
| Isaac Kurth | deb86b4 | 2022-03-08 19:47:53 -0600 | [diff] [blame] | 104 | std::array<char, 512> buffer; | 
|  | 105 | std::string cmd = buildCommandStr(args); | 
|  | 106 | std::stringstream result; | 
|  | 107 | int rc; | 
|  | 108 | FILE* pipe = popen(cmd.c_str(), "r"); | 
|  | 109 | if (!pipe) | 
| Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 110 | { | 
| Isaac Kurth | deb86b4 | 2022-03-08 19:47:53 -0600 | [diff] [blame] | 111 | error("Failed to execute command: {COMMAND}", "COMMAND", cmd); | 
|  | 112 | return {-1, std::string{}}; | 
| Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 113 | } | 
| Isaac Kurth | deb86b4 | 2022-03-08 19:47:53 -0600 | [diff] [blame] | 114 | while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) | 
| Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 115 | { | 
| Isaac Kurth | deb86b4 | 2022-03-08 19:47:53 -0600 | [diff] [blame] | 116 | result << buffer.data(); | 
| Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 117 | } | 
| Isaac Kurth | deb86b4 | 2022-03-08 19:47:53 -0600 | [diff] [blame] | 118 | rc = pclose(pipe); | 
|  | 119 | return {rc, result.str()}; | 
| Adriana Kobylak | 8a5ccbb | 2021-01-20 10:57:05 -0600 | [diff] [blame] | 120 | } | 
|  | 121 |  | 
|  | 122 | } // namespace internal | 
|  | 123 |  | 
| Jayashankar Padath | a013560 | 2019-04-22 16:22:58 +0530 | [diff] [blame] | 124 | } // namespace utils |