#pragma once #include "constants.hpp" #include "logger.hpp" #include #include #include #include /** * @brief Namespace to host common utility methods. * * A method qualifies as a common utility function if, * A)It is being used by the utility namespace at the same level as well as * other files directly. * B) The utility should be a leaf node and should not be dependent on any other * utility. * ******************* * | Commmon Utility | - - - - - - - * ******************* | * /\ | * / \ | * **************** **************** | * | json utility | | dbus utility | | * **************** **************** | * \ / | * \ / | * ************************ | * | Vpd specific Utility | - - - - - - - * ************************ */ namespace vpd { namespace commonUtility { /** @brief Return the hex representation of the incoming byte. * * @param [in] i_aByte - The input byte. * @returns Hex representation of the byte as a character. */ constexpr auto toHex(size_t i_aByte) { constexpr auto l_map = "0123456789abcdef"; return l_map[i_aByte]; } /** * @brief API to return null at the end of variadic template args. * * @return empty string. */ inline std::string getCommand() { return ""; } /** * @brief API to arrange create command. * * @param[in] arguments to create the command * @return Command string */ template inline std::string getCommand(T i_arg1, Types... i_args) { std::string l_cmd = " " + i_arg1 + getCommand(i_args...); return l_cmd; } /** * @brief API to create shell command and execute. * * @throw std::runtime_error. * * @param[in] arguments for command * @returns output of that command */ template inline std::vector executeCmd(T&& i_path, Types... i_args) { std::vector l_cmdOutput; std::array l_buffer; std::string l_cmd = i_path + getCommand(i_args...); std::unique_ptr l_cmdPipe( popen(l_cmd.c_str(), "r"), pclose); if (!l_cmdPipe) { logging::logMessage( "popen failed with error " + std::string(strerror(errno))); throw std::runtime_error("popen failed!"); } while (fgets(l_buffer.data(), l_buffer.size(), l_cmdPipe.get()) != nullptr) { l_cmdOutput.emplace_back(l_buffer.data()); } return l_cmdOutput; } /** @brief Converts string to lower case. * * @param [in] i_string - Input string. */ inline void toLower(std::string& i_string) { std::transform(i_string.begin(), i_string.end(), i_string.begin(), [](unsigned char l_char) { return std::tolower(l_char); }); } /** * @brief An API to get hex representation of the incoming bytes. * * The API returns the hex represented value of the given input in string format * with 0x prefix. * * @param[in] i_keywordValue - Vector of input byte. * * @return - Returns the converted string value. */ inline std::string convertByteVectorToHex( const types::BinaryVector& i_keywordValue) { std::ostringstream l_oss; l_oss << "0x"; for (const auto& l_byte : i_keywordValue) { l_oss << std::setfill('0') << std::setw(2) << std::hex << static_cast(l_byte); } return l_oss.str(); } } // namespace commonUtility } // namespace vpd