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