Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "constants.hpp" |
| 4 | #include "logger.hpp" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <cstdio> |
| 8 | #include <cstdlib> |
| 9 | #include <vector> |
| 10 | |
| 11 | /** |
| 12 | * @brief Namespace to host common utility methods. |
| 13 | * |
| 14 | * A method qualifies as a common utility function if, |
| 15 | * A)It is being used by the utility namespace at the same level as well as |
| 16 | * other files directly. |
| 17 | * B) The utility should be a leaf node and should not be dependent on any other |
| 18 | * utility. |
| 19 | * ******************* |
| 20 | * | Commmon Utility | - - - - - - - |
| 21 | * ******************* | |
| 22 | * /\ | |
| 23 | * / \ | |
| 24 | * **************** **************** | |
| 25 | * | json utility | | dbus utility | | |
| 26 | * **************** **************** | |
| 27 | * \ / | |
| 28 | * \ / | |
| 29 | * ************************ | |
| 30 | * | Vpd specific Utility | - - - - - - - |
| 31 | * ************************ |
| 32 | */ |
| 33 | |
| 34 | namespace vpd |
| 35 | { |
| 36 | |
| 37 | namespace commonUtility |
| 38 | { |
| 39 | /** @brief Return the hex representation of the incoming byte. |
| 40 | * |
| 41 | * @param [in] i_aByte - The input byte. |
| 42 | * @returns Hex representation of the byte as a character. |
| 43 | */ |
| 44 | constexpr auto toHex(size_t i_aByte) |
| 45 | { |
| 46 | constexpr auto l_map = "0123456789abcdef"; |
| 47 | return l_map[i_aByte]; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @brief API to return null at the end of variadic template args. |
| 52 | * |
| 53 | * @return empty string. |
| 54 | */ |
| 55 | inline std::string getCommand() |
| 56 | { |
| 57 | return ""; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @brief API to arrange create command. |
| 62 | * |
| 63 | * @param[in] arguments to create the command |
| 64 | * @return Command string |
| 65 | */ |
| 66 | template <typename T, typename... Types> |
| 67 | inline std::string getCommand(T i_arg1, Types... i_args) |
| 68 | { |
| 69 | std::string l_cmd = " " + i_arg1 + getCommand(i_args...); |
| 70 | |
| 71 | return l_cmd; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @brief API to create shell command and execute. |
| 76 | * |
| 77 | * @throw std::runtime_error. |
| 78 | * |
| 79 | * @param[in] arguments for command |
| 80 | * @returns output of that command |
| 81 | */ |
| 82 | template <typename T, typename... Types> |
| 83 | inline std::vector<std::string> executeCmd(T&& i_path, Types... i_args) |
| 84 | { |
| 85 | std::vector<std::string> l_cmdOutput; |
| 86 | std::array<char, constants::CMD_BUFFER_LENGTH> l_buffer; |
| 87 | |
| 88 | std::string l_cmd = i_path + getCommand(i_args...); |
| 89 | |
| 90 | std::unique_ptr<FILE, decltype(&pclose)> l_cmdPipe( |
| 91 | popen(l_cmd.c_str(), "r"), pclose); |
| 92 | |
| 93 | if (!l_cmdPipe) |
| 94 | { |
| 95 | logging::logMessage( |
| 96 | "popen failed with error " + std::string(strerror(errno))); |
| 97 | throw std::runtime_error("popen failed!"); |
| 98 | } |
| 99 | while (fgets(l_buffer.data(), l_buffer.size(), l_cmdPipe.get()) != nullptr) |
| 100 | { |
| 101 | l_cmdOutput.emplace_back(l_buffer.data()); |
| 102 | } |
| 103 | |
| 104 | return l_cmdOutput; |
| 105 | } |
| 106 | |
| 107 | /** @brief Converts string to lower case. |
| 108 | * |
| 109 | * @param [in] i_string - Input string. |
| 110 | */ |
| 111 | inline void toLower(std::string& i_string) |
| 112 | { |
| 113 | std::transform(i_string.begin(), i_string.end(), i_string.begin(), |
| 114 | [](unsigned char l_char) { return std::tolower(l_char); }); |
| 115 | } |
Anupama B R | 6f14283 | 2025-04-11 04:17:49 -0500 | [diff] [blame^] | 116 | |
| 117 | /** |
| 118 | * @brief An API to get hex representation of the incoming bytes. |
| 119 | * |
| 120 | * The API returns the hex represented value of the given input in string format |
| 121 | * with 0x prefix. |
| 122 | * |
| 123 | * @param[in] i_keywordValue - Vector of input byte. |
| 124 | * |
| 125 | * @return - Returns the converted string value. |
| 126 | */ |
| 127 | inline std::string convertByteVectorToHex( |
| 128 | const types::BinaryVector& i_keywordValue) |
| 129 | { |
| 130 | std::ostringstream l_oss; |
| 131 | l_oss << "0x"; |
| 132 | for (const auto& l_byte : i_keywordValue) |
| 133 | { |
| 134 | l_oss << std::setfill('0') << std::setw(2) << std::hex |
| 135 | << static_cast<int>(l_byte); |
| 136 | } |
| 137 | |
| 138 | return l_oss.str(); |
| 139 | } |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 140 | } // namespace commonUtility |
| 141 | } // namespace vpd |