blob: 6bf6c04f6bd5e9d639808c30fbb6230ebbdedd21 [file] [log] [blame]
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +08001#pragma once
2
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +08003#include <stdio.h>
Matt Spinler920c1972023-12-12 10:21:35 -06004#include <string.h>
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +08005
Matt Spinler81bc5612023-06-01 16:48:19 -05006#include <cstdint>
Arya K Padman8c8aaa02024-04-28 23:23:45 -05007#include <memory>
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +08008#include <string>
9#include <vector>
10
11namespace openpower
12{
13namespace pels
14{
15const uint8_t indentLevel = 4;
16const 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 */
22std::string escapeJSON(const std::string& input);
23
24/**
25 * @brief get hex dump for PEL section in json format.
Matt Spinler4220a152020-03-26 10:18:09 -050026 * @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
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +080029 * @param[in] bool toJson - if true, output lines as JSON array, else print
30 * output as plain text
Arya K Padman8c8aaa02024-04-28 23:23:45 -050031 * @return std::unique_ptr<char[]> - the Hex dump
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080032 */
Arya K Padman8c8aaa02024-04-28 23:23:45 -050033std::unique_ptr<char[]> dumpHex(const void* data, size_t size,
34 size_t indentCount, bool toJson = true);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080035
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 */
44void jsonInsert(std::string& jsonStr, const std::string& fieldName,
Matt Spinler45796e82022-07-01 11:25:27 -050045 const std::string& fieldValue, uint8_t indentCount);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080046
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 */
55void jsonInsertArray(std::string& jsonStr, const std::string& fieldName,
Matt Spinler45796e82022-07-01 11:25:27 -050056 const std::vector<std::string>& values,
57 uint8_t indentCount);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080058
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 */
65template <typename T>
66std::string getNumberString(const char* format, T number)
67{
Matt Spinler920c1972023-12-12 10:21:35 -060068 constexpr size_t valueSize = 100;
69 char value[valueSize];
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080070 std::string numString;
71
72 static_assert(std::is_integral<T>::value, "Integral required.");
73
Matt Spinler920c1972023-12-12 10:21:35 -060074 int len = snprintf(value, valueSize, format, number);
Patrick Williams23867bc2021-04-22 09:26:47 -050075 if (len >= 0)
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080076 {
77 numString = value;
78 }
Patrick Williams23867bc2021-04-22 09:26:47 -050079 else
80 {
81 throw std::invalid_argument(
82 std::string("getNumberString: invalid format string: ") + format);
83 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080084
85 return numString;
86}
87
Harisuddin Mohamed Isae2d1bf32020-02-06 17:32:38 +080088/**
89 * @brief helper function to trim trailing whitespaces
90 * @return std::string - trimmed string
91 * @param[in] std::string - string to trim
92 */
93std::string trimEnd(std::string s);
94
Matt Spinlerb832aa52023-03-21 15:32:34 -050095/**
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 */
107std::string getComponentName(uint16_t compID, uint8_t creatorID);
108
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800109} // namespace pels
110} // namespace openpower