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