Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <ctype.h> |
| 4 | #include <stdio.h> |
| 5 | |
Matt Spinler | 81bc561 | 2023-06-01 16:48:19 -0500 | [diff] [blame] | 6 | #include <cstdint> |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 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. |
Matt Spinler | 4220a15 | 2020-03-26 10:18:09 -0500 | [diff] [blame] | 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 |
Harisuddin Mohamed Isa | ed32c8d | 2020-10-01 18:12:39 +0800 | [diff] [blame] | 31 | * @param[in] bool toJson - if true, output lines as JSON array, else print |
| 32 | * output as plain text |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 33 | * @return char * - the Hex dump |
| 34 | */ |
Harisuddin Mohamed Isa | ed32c8d | 2020-10-01 18:12:39 +0800 | [diff] [blame] | 35 | char* dumpHex(const void* data, size_t size, size_t indentCount, |
| 36 | bool toJson = true); |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 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, |
Matt Spinler | 45796e8 | 2022-07-01 11:25:27 -0500 | [diff] [blame] | 47 | const std::string& fieldValue, uint8_t indentCount); |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 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, |
Matt Spinler | 45796e8 | 2022-07-01 11:25:27 -0500 | [diff] [blame] | 58 | const std::vector<std::string>& values, |
| 59 | uint8_t indentCount); |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 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); |
Patrick Williams | 23867bc | 2021-04-22 09:26:47 -0500 | [diff] [blame] | 76 | if (len >= 0) |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 77 | { |
| 78 | numString = value; |
| 79 | } |
Patrick Williams | 23867bc | 2021-04-22 09:26:47 -0500 | [diff] [blame] | 80 | else |
| 81 | { |
| 82 | throw std::invalid_argument( |
| 83 | std::string("getNumberString: invalid format string: ") + format); |
| 84 | } |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 85 | free(value); |
| 86 | |
| 87 | return numString; |
| 88 | } |
| 89 | |
Harisuddin Mohamed Isa | e2d1bf3 | 2020-02-06 17:32:38 +0800 | [diff] [blame] | 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 | |
Matt Spinler | b832aa5 | 2023-03-21 15:32:34 -0500 | [diff] [blame] | 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 | |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 111 | } // namespace pels |
| 112 | } // namespace openpower |