1 #pragma once 2 3 #include <stdio.h> 4 #include <string.h> 5 6 #include <cstdint> 7 #include <memory> 8 #include <string> 9 #include <vector> 10 11 namespace openpower 12 { 13 namespace pels 14 { 15 const uint8_t indentLevel = 4; 16 const uint8_t colAlign = 32; 17 /** 18 * @brief escape json - use it for PEL hex dumps. 19 * @param[in] std::string - the unescaped JSON as a string literal 20 * @return std::string - escaped JSON string literal 21 */ 22 std::string escapeJSON(const std::string& input); 23 24 /** 25 * @brief get hex dump for PEL section in json format. 26 * @param[in] const void* data - Raw PEL data 27 * @param[i] size_t size - size of Raw PEL 28 * @param[in] size_t indentCount - The number of indent levels to indent 29 * @param[in] bool toJson - if true, output lines as JSON array, else print 30 * output as plain text 31 * @return std::unique_ptr<char[]> - the Hex dump 32 */ 33 std::unique_ptr<char[]> dumpHex(const void* data, size_t size, 34 size_t indentCount, bool toJson = true); 35 36 /** 37 * @brief Inserts key-value into a JSON string 38 * 39 * @param[in] jsonStr - The JSON string 40 * @param[in] fieldName - The JSON key to insert 41 * @param[in] fieldValue - The JSON value to insert 42 * @param[in] indentCount - Indent count for the line 43 */ 44 void jsonInsert(std::string& jsonStr, const std::string& fieldName, 45 const std::string& fieldValue, uint8_t indentCount); 46 47 /** 48 * @brief Inserts key-value array into a JSON string 49 * 50 * @param[in] jsonStr - The JSON string 51 * @param[in] fieldName - The JSON key to insert 52 * @param[in] values - The JSON array to insert 53 * @param[in] indentCount - Indent count for the line 54 */ 55 void jsonInsertArray(std::string& jsonStr, const std::string& fieldName, 56 const std::vector<std::string>& values, 57 uint8_t indentCount); 58 59 /** 60 * @brief Converts an integer to a formatted string 61 * @param[in] format - the format of output string 62 * @param[in] number - the integer to convert 63 * @return std::string - the formatted string 64 */ 65 template <typename T> 66 std::string getNumberString(const char* format, T number) 67 { 68 constexpr size_t valueSize = 100; 69 char value[valueSize]; 70 std::string numString; 71 72 static_assert(std::is_integral<T>::value, "Integral required."); 73 74 int len = snprintf(value, valueSize, format, number); 75 if (len >= 0) 76 { 77 numString = value; 78 } 79 else 80 { 81 throw std::invalid_argument( 82 std::string("getNumberString: invalid format string: ") + format); 83 } 84 85 return numString; 86 } 87 88 /** 89 * @brief helper function to trim trailing whitespaces 90 * @return std::string - trimmed string 91 * @param[in] std::string - string to trim 92 */ 93 std::string trimEnd(std::string s); 94 95 /** 96 * @brief Returns the component name for the component ID. 97 * 98 * It will try to look up the name to use in JSON files based on 99 * the creator ID. If PHYP, will convert the component ID to 100 * two characters. 101 * 102 * If nothing else, it will just return the name as a string like 103 * "0x1234". 104 * 105 * @return std::string - The component name 106 */ 107 std::string getComponentName(uint16_t compID, uint8_t creatorID); 108 109 } // namespace pels 110 } // namespace openpower 111