blob: bf90d9bf50095ecf6ade4d6eb94d3121a275e693 [file] [log] [blame]
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +08001#pragma once
2
3#include <ctype.h>
4#include <stdio.h>
Matt Spinler920c1972023-12-12 10:21:35 -06005#include <string.h>
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +08006
Matt Spinler81bc5612023-06-01 16:48:19 -05007#include <cstdint>
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +08008#include <fstream>
9#include <iomanip>
10#include <iostream>
Arya K Padman8c8aaa02024-04-28 23:23:45 -050011#include <memory>
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080012#include <string>
13#include <vector>
14
15namespace openpower
16{
17namespace pels
18{
19const uint8_t indentLevel = 4;
20const 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 */
26std::string escapeJSON(const std::string& input);
27
28/**
29 * @brief get hex dump for PEL section in json format.
Matt Spinler4220a152020-03-26 10:18:09 -050030 * @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 Isaed32c8d2020-10-01 18:12:39 +080033 * @param[in] bool toJson - if true, output lines as JSON array, else print
34 * output as plain text
Arya K Padman8c8aaa02024-04-28 23:23:45 -050035 * @return std::unique_ptr<char[]> - the Hex dump
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080036 */
Arya K Padman8c8aaa02024-04-28 23:23:45 -050037std::unique_ptr<char[]> dumpHex(const void* data, size_t size,
38 size_t indentCount, bool toJson = true);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080039
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 */
48void jsonInsert(std::string& jsonStr, const std::string& fieldName,
Matt Spinler45796e82022-07-01 11:25:27 -050049 const std::string& fieldValue, uint8_t indentCount);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080050
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 */
59void jsonInsertArray(std::string& jsonStr, const std::string& fieldName,
Matt Spinler45796e82022-07-01 11:25:27 -050060 const std::vector<std::string>& values,
61 uint8_t indentCount);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080062
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 */
69template <typename T>
70std::string getNumberString(const char* format, T number)
71{
Matt Spinler920c1972023-12-12 10:21:35 -060072 constexpr size_t valueSize = 100;
73 char value[valueSize];
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080074 std::string numString;
75
76 static_assert(std::is_integral<T>::value, "Integral required.");
77
Matt Spinler920c1972023-12-12 10:21:35 -060078 int len = snprintf(value, valueSize, format, number);
Patrick Williams23867bc2021-04-22 09:26:47 -050079 if (len >= 0)
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080080 {
81 numString = value;
82 }
Patrick Williams23867bc2021-04-22 09:26:47 -050083 else
84 {
85 throw std::invalid_argument(
86 std::string("getNumberString: invalid format string: ") + format);
87 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080088
89 return numString;
90}
91
Harisuddin Mohamed Isae2d1bf32020-02-06 17:32:38 +080092/**
93 * @brief helper function to trim trailing whitespaces
94 * @return std::string - trimmed string
95 * @param[in] std::string - string to trim
96 */
97std::string trimEnd(std::string s);
98
Matt Spinlerb832aa52023-03-21 15:32:34 -050099/**
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 */
111std::string getComponentName(uint16_t compID, uint8_t creatorID);
112
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800113} // namespace pels
114} // namespace openpower