PEL: Print component names in peltool

Every PEL section has a 2 byte component ID field in its header that
peltool prints.  Currently, it just prints the hex version, like
"0x1000".

There are JSON files in the flash already that contain mappings of
component IDs to to component names, and this commit starts looking
up the component names from those files and using those in the peltool
output.

An example of a file is:
/usr/share/phosphor-logging/pels/O_component_ids.json:
{
    "1000": "bmc common function",
    "2000": "bmc error logging",
    ...
}

Where the 'O' in the filename is the creator ID field of the PEL.  There
is also a file for hostboot, which is B_component_ids.json.

Also, for PELs with a PHYP creator ID, just convert the ID to two
characters, like 0x4552 - > "ER" as that is what they are.

peltool output examples:
    "Created by": "bmc error logging",
    "Created by": "hostboot: errl",
    "Created by": "IO",

This matches what is already done by the python peltool.

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: Id616739e1b7ca67c85dc7efa85dc34acf6aca9b5
diff --git a/test/openpower-pels/json_utils_test.cpp b/test/openpower-pels/json_utils_test.cpp
index a7203aa..5a3d1a1 100644
--- a/test/openpower-pels/json_utils_test.cpp
+++ b/test/openpower-pels/json_utils_test.cpp
@@ -14,6 +14,11 @@
  * limitations under the License.
  */
 #include "extensions/openpower-pels/json_utils.hpp"
+#include "extensions/openpower-pels/paths.hpp"
+
+#include <nlohmann/json.hpp>
+
+#include <filesystem>
 
 #include <gtest/gtest.h>
 
@@ -47,3 +52,33 @@
     EXPECT_EQ(json, "    \"Key\":                      \"Value1\",\n"
                     "        \"Keyxxxxxxxxxxxxxxxxxxxxxxxxxx\": \"Value2\",\n");
 }
+
+TEST(JsonUtilsTest, GetComponentNameTest)
+{
+    const auto compIDs = R"(
+    {
+        "1000": "some comp",
+        "2222": "another comp"
+    })"_json;
+
+    auto dataPath = getPELReadOnlyDataPath();
+    std::ofstream file{dataPath / "O_component_ids.json"};
+    file << compIDs;
+    file.close();
+
+    // The component ID file exists
+    EXPECT_EQ(getComponentName(0x1000, 'O'), "some comp");
+    EXPECT_EQ(getComponentName(0x2222, 'O'), "another comp");
+    EXPECT_EQ(getComponentName(0x0001, 'O'), "0x0001");
+
+    // No component ID file
+    EXPECT_EQ(getComponentName(0x3456, 'B'), "0x3456");
+
+    // PHYP, uses characters if both bytes nonzero
+    EXPECT_EQ(getComponentName(0x4552, 'H'), "ER");
+    EXPECT_EQ(getComponentName(0x584D, 'H'), "XM");
+    EXPECT_EQ(getComponentName(0x5800, 'H'), "0x5800");
+    EXPECT_EQ(getComponentName(0x0058, 'H'), "0x0058");
+
+    std::filesystem::remove_all(dataPath);
+}