blob: 4ee32f4b7e287fcb315615855951c1dc2efe27d9 [file] [log] [blame]
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +08001#pragma once
2
3#include <ctype.h>
4#include <stdio.h>
5
6#include <fstream>
7#include <iomanip>
8#include <iostream>
9#include <string>
10#include <vector>
11
12namespace openpower
13{
14namespace pels
15{
16const uint8_t indentLevel = 4;
17const uint8_t colAlign = 32;
18/**
19 * @brief escape json - use it for PEL hex dumps.
20 * @param[in] std::string - the unescaped JSON as a string literal
21 * @return std::string - escaped JSON string literal
22 */
23std::string escapeJSON(const std::string& input);
24
25/**
26 * @brief get hex dump for PEL section in json format.
Matt Spinler4220a152020-03-26 10:18:09 -050027 * @param[in] const void* data - Raw PEL data
28 * @param[i] size_t size - size of Raw PEL
29 * @param[in] size_t indentCount - The number of indent levels to indent
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +080030 * @param[in] bool toJson - if true, output lines as JSON array, else print
31 * output as plain text
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080032 * @return char * - the Hex dump
33 */
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +080034char* dumpHex(const void* data, size_t size, size_t indentCount,
35 bool toJson = true);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080036
37/**
38 * @brief Inserts key-value into a JSON string
39 *
40 * @param[in] jsonStr - The JSON string
41 * @param[in] fieldName - The JSON key to insert
42 * @param[in] fieldValue - The JSON value to insert
43 * @param[in] indentCount - Indent count for the line
44 */
45void jsonInsert(std::string& jsonStr, const std::string& fieldName,
Matt Spinler45796e82022-07-01 11:25:27 -050046 const std::string& fieldValue, uint8_t indentCount);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080047
48/**
49 * @brief Inserts key-value array into a JSON string
50 *
51 * @param[in] jsonStr - The JSON string
52 * @param[in] fieldName - The JSON key to insert
53 * @param[in] values - The JSON array to insert
54 * @param[in] indentCount - Indent count for the line
55 */
56void jsonInsertArray(std::string& jsonStr, const std::string& fieldName,
Matt Spinler45796e82022-07-01 11:25:27 -050057 const std::vector<std::string>& values,
58 uint8_t indentCount);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080059
60/**
61 * @brief Converts an integer to a formatted string
62 * @param[in] format - the format of output string
63 * @param[in] number - the integer to convert
64 * @return std::string - the formatted string
65 */
66template <typename T>
67std::string getNumberString(const char* format, T number)
68{
69 char* value = nullptr;
70 std::string numString;
71
72 static_assert(std::is_integral<T>::value, "Integral required.");
73
74 int len = asprintf(&value, 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 free(value);
85
86 return numString;
87}
88
Harisuddin Mohamed Isae2d1bf32020-02-06 17:32:38 +080089/**
90 * @brief helper function to trim trailing whitespaces
91 * @return std::string - trimmed string
92 * @param[in] std::string - string to trim
93 */
94std::string trimEnd(std::string s);
95
Matt Spinlerb832aa52023-03-21 15:32:34 -050096/**
97 * @brief Returns the component name for the component ID.
98 *
99 * It will try to look up the name to use in JSON files based on
100 * the creator ID. If PHYP, will convert the component ID to
101 * two characters.
102 *
103 * If nothing else, it will just return the name as a string like
104 * "0x1234".
105 *
106 * @return std::string - The component name
107 */
108std::string getComponentName(uint16_t compID, uint8_t creatorID);
109
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800110} // namespace pels
111} // namespace openpower