blob: 2065952fc86aee6d946479af8bb5116c41b8a7e7 [file] [log] [blame]
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +08001#pragma once
2
3#include <ctype.h>
4#include <stdio.h>
5
Matt Spinler81bc5612023-06-01 16:48:19 -05006#include <cstdint>
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +08007#include <fstream>
8#include <iomanip>
9#include <iostream>
10#include <string>
11#include <vector>
12
13namespace openpower
14{
15namespace pels
16{
17const uint8_t indentLevel = 4;
18const 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 */
24std::string escapeJSON(const std::string& input);
25
26/**
27 * @brief get hex dump for PEL section in json format.
Matt Spinler4220a152020-03-26 10:18:09 -050028 * @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 Isaed32c8d2020-10-01 18:12:39 +080031 * @param[in] bool toJson - if true, output lines as JSON array, else print
32 * output as plain text
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080033 * @return char * - the Hex dump
34 */
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +080035char* dumpHex(const void* data, size_t size, size_t indentCount,
36 bool toJson = true);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080037
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 */
46void jsonInsert(std::string& jsonStr, const std::string& fieldName,
Matt Spinler45796e82022-07-01 11:25:27 -050047 const std::string& fieldValue, uint8_t indentCount);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080048
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 */
57void jsonInsertArray(std::string& jsonStr, const std::string& fieldName,
Matt Spinler45796e82022-07-01 11:25:27 -050058 const std::vector<std::string>& values,
59 uint8_t indentCount);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080060
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 */
67template <typename T>
68std::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 Williams23867bc2021-04-22 09:26:47 -050076 if (len >= 0)
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080077 {
78 numString = value;
79 }
Patrick Williams23867bc2021-04-22 09:26:47 -050080 else
81 {
82 throw std::invalid_argument(
83 std::string("getNumberString: invalid format string: ") + format);
84 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080085 free(value);
86
87 return numString;
88}
89
Harisuddin Mohamed Isae2d1bf32020-02-06 17:32:38 +080090/**
91 * @brief helper function to trim trailing whitespaces
92 * @return std::string - trimmed string
93 * @param[in] std::string - string to trim
94 */
95std::string trimEnd(std::string s);
96
Matt Spinlerb832aa52023-03-21 15:32:34 -050097/**
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 */
109std::string getComponentName(uint16_t compID, uint8_t creatorID);
110
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800111} // namespace pels
112} // namespace openpower