blob: c972459ff09bf7c3f500cd63cfcc643dbaecdaf2 [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>
11#include <string>
12#include <vector>
13
14namespace openpower
15{
16namespace pels
17{
18const uint8_t indentLevel = 4;
19const uint8_t colAlign = 32;
20/**
21 * @brief escape json - use it for PEL hex dumps.
22 * @param[in] std::string - the unescaped JSON as a string literal
23 * @return std::string - escaped JSON string literal
24 */
25std::string escapeJSON(const std::string& input);
26
27/**
28 * @brief get hex dump for PEL section in json format.
Matt Spinler4220a152020-03-26 10:18:09 -050029 * @param[in] const void* data - Raw PEL data
30 * @param[i] size_t size - size of Raw PEL
31 * @param[in] size_t indentCount - The number of indent levels to indent
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +080032 * @param[in] bool toJson - if true, output lines as JSON array, else print
33 * output as plain text
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080034 * @return char * - the Hex dump
35 */
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +080036char* dumpHex(const void* data, size_t size, size_t indentCount,
37 bool toJson = true);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080038
39/**
40 * @brief Inserts key-value into a JSON string
41 *
42 * @param[in] jsonStr - The JSON string
43 * @param[in] fieldName - The JSON key to insert
44 * @param[in] fieldValue - The JSON value to insert
45 * @param[in] indentCount - Indent count for the line
46 */
47void jsonInsert(std::string& jsonStr, const std::string& fieldName,
Matt Spinler45796e82022-07-01 11:25:27 -050048 const std::string& fieldValue, uint8_t indentCount);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080049
50/**
51 * @brief Inserts key-value array into a JSON string
52 *
53 * @param[in] jsonStr - The JSON string
54 * @param[in] fieldName - The JSON key to insert
55 * @param[in] values - The JSON array to insert
56 * @param[in] indentCount - Indent count for the line
57 */
58void jsonInsertArray(std::string& jsonStr, const std::string& fieldName,
Matt Spinler45796e82022-07-01 11:25:27 -050059 const std::vector<std::string>& values,
60 uint8_t indentCount);
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080061
62/**
63 * @brief Converts an integer to a formatted string
64 * @param[in] format - the format of output string
65 * @param[in] number - the integer to convert
66 * @return std::string - the formatted string
67 */
68template <typename T>
69std::string getNumberString(const char* format, T number)
70{
Matt Spinler920c1972023-12-12 10:21:35 -060071 constexpr size_t valueSize = 100;
72 char value[valueSize];
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080073 std::string numString;
74
75 static_assert(std::is_integral<T>::value, "Integral required.");
76
Matt Spinler920c1972023-12-12 10:21:35 -060077 int len = snprintf(value, valueSize, format, number);
Patrick Williams23867bc2021-04-22 09:26:47 -050078 if (len >= 0)
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080079 {
80 numString = value;
81 }
Patrick Williams23867bc2021-04-22 09:26:47 -050082 else
83 {
84 throw std::invalid_argument(
85 std::string("getNumberString: invalid format string: ") + format);
86 }
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080087
88 return numString;
89}
90
Harisuddin Mohamed Isae2d1bf32020-02-06 17:32:38 +080091/**
92 * @brief helper function to trim trailing whitespaces
93 * @return std::string - trimmed string
94 * @param[in] std::string - string to trim
95 */
96std::string trimEnd(std::string s);
97
Matt Spinlerb832aa52023-03-21 15:32:34 -050098/**
99 * @brief Returns the component name for the component ID.
100 *
101 * It will try to look up the name to use in JSON files based on
102 * the creator ID. If PHYP, will convert the component ID to
103 * two characters.
104 *
105 * If nothing else, it will just return the name as a string like
106 * "0x1234".
107 *
108 * @return std::string - The component name
109 */
110std::string getComponentName(uint16_t compID, uint8_t creatorID);
111
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800112} // namespace pels
113} // namespace openpower