oemcommands: add JSON parsing handling
The IPMI host service fails to activate
if the oemData.json or fbSelRaw.json file is corrupted.
To address this, add JSON parsing error handling
to prevent abnormal termination during file writing in an AC cycle.
Change-Id: I19a1274ac029413c8b1f2efb25bb1488076dab2d
Signed-off-by: Peter Yin <peter.yin@quantatw.com>
diff --git a/src/selcommands.cpp b/src/selcommands.cpp
index b959382..7069153 100644
--- a/src/selcommands.cpp
+++ b/src/selcommands.cpp
@@ -128,6 +128,22 @@
selDataObj[KEY_FREE_SPACE] = 0xFFFF;
}
+ void writeEmptyJson()
+ {
+ selDataObj = nlohmann::json::object(); // Create an empty JSON object
+ std::ofstream outFile(SEL_JSON_DATA_FILE);
+ if (outFile)
+ {
+ // Write empty JSON object to the file
+ outFile << selDataObj.dump(4);
+ outFile.close();
+ }
+ else
+ {
+ lg2::info("Failed to create SEL JSON file with empty JSON.");
+ }
+ }
+
public:
SELData()
{
@@ -135,9 +151,24 @@
std::ifstream file(SEL_JSON_DATA_FILE);
if (file)
{
- file >> selDataObj;
+ try
+ {
+ file >> selDataObj;
+ }
+ catch (const nlohmann::json::parse_error& e)
+ {
+ lg2::error("Error parsing SEL JSON file: {ERROR}", "ERROR", e);
+ writeEmptyJson();
+ init(); // Initialize to default values
+ }
file.close();
}
+ else
+ {
+ lg2::info("Failed to open SEL JSON file.");
+ writeEmptyJson();
+ init();
+ }
/* Initialize SelData object if no entries. */
if (selDataObj.find(KEY_SEL_COUNT) == selDataObj.end())