Files
OpenBMC/meta-luxshare/meta-common/recipes-luxshare/leds/phosphor-led-manager/0007-Retrieve-SEL-sensor-info-from-JSON-file-instead-of-c.patch
T

148 lines
4.5 KiB
Diff
Raw Normal View History

2026-04-23 17:07:55 +08:00
From baa59772c55c45a237d471b1227296d09b33705d Mon Sep 17 00:00:00 2001
From: wangjue <jue.wang2@luxshare-ict.com>
Date: Thu, 19 Dec 2024 19:11:25 +0800
Subject: [PATCH] Retrieve SEL sensor info from JSON file instead of calling
from IPMI service
Signed-off-by: wangjue <jue.wang2@luxshare-ict.com>
---
fault-monitor/fru-fault-monitor.cpp | 95 +++++++++++++++++++++++++----
1 file changed, 83 insertions(+), 12 deletions(-)
diff --git a/fault-monitor/fru-fault-monitor.cpp b/fault-monitor/fru-fault-monitor.cpp
index 1bc41aa..a64f375 100644
--- a/fault-monitor/fru-fault-monitor.cpp
+++ b/fault-monitor/fru-fault-monitor.cpp
@@ -5,11 +5,16 @@
#include <phosphor-logging/lg2.hpp>
#include <sdbusplus/exception.hpp>
#include <xyz/openbmc_project/Common/error.hpp>
+#include <nlohmann/json.hpp>
+#include <phosphor-logging/lg2.hpp>
#include <string>
#include <vector>
#include <string>
#include <vector>
#include <iostream>
+#include <unordered_map>
+#include <filesystem>
+#include <fstream>
extern "C"
{
@@ -230,6 +235,14 @@ using AdditionalList = std::vector<std::string>;
using sensorCodes = std::vector<uint8_t>;
using ipmiSensorsInfo = std::unordered_map<std::string, sensorCodes>;
std::optional<ipmiSensorsInfo> ipmiSensors;
+struct SensorInfo
+{
+ std::string sensorName;
+ uint8_t readingType;
+ uint8_t sensorType;
+};
+
+using SensorMessages = std::unordered_map<std::string, SensorInfo>;
const std::unordered_map<uint8_t, discrectSensorEvent> genericEventTypes{
{0x03,
@@ -376,6 +389,72 @@ static const std::unordered_map<std::string, std::vector<uint8_t>>
EVENT_TYPE_07H_TRANSITION_TO_NC_FROM_OK}},
};
+static constexpr auto sensorJsonPath =
+ "/usr/share/ipmi-providers/sensor-data-record.json";
+
+SensorMessages loadSensorFromJson()
+{
+ try
+ {
+ const std::filesystem::path path(sensorJsonPath);
+ SensorMessages sensorMessages;
+ const nlohmann::json empty{};
+ nlohmann::json js;
+ if (!std::filesystem::exists(path) || std::filesystem::is_empty(path))
+ {
+ std::cout << "Incorrect File Path or empty file, FILE_PATH = "
+ << path << std::endl;
+ return sensorMessages;
+ }
+
+ try
+ {
+ std::ifstream jsonFile(path);
+ js = nlohmann::json::parse(jsonFile);
+ }
+ catch (const std::exception& e)
+ {
+ std::cout << "Failed to parse config file, ERROR = " << e.what()
+ << ", FILE_PATH = " << path << std::endl;
+ throw std::runtime_error("Failed to parse config file");
+ }
+
+ for (const auto& sensor : js.items())
+ {
+ SensorInfo info;
+ std::string sensorPath = sensor.key();
+ auto sensorParam = sensor.value();
+ info.sensorName = sensorParam["sensorName"];
+ info.readingType = sensorParam["sensorReadingType"];
+ info.sensorType = sensorParam["sensorType"];
+ sensorMessages.insert(std::make_pair(sensorPath, std::move(info)));
+ }
+ return sensorMessages;
+ }
+ catch (const std::exception& e)
+ {
+ throw std::runtime_error("Failed to parse sensor config file");
+ }
+}
+
+const SensorMessages& getSensorMessages()
+{
+ static const SensorMessages sensorMessages = loadSensorFromJson();
+ return sensorMessages;
+}
+
+std::optional<SensorInfo>
+ getSensorInfo(const std::string& sensorPath)
+{
+ const auto& sensorMessages = getSensorMessages();
+ auto ite = sensorMessages.find(sensorPath);
+ if (ite == sensorMessages.end())
+ {
+ return std::nullopt;
+ }
+ return std::make_optional(ite->second);
+}
+
std::optional<ipmiSensorsInfo> initIPMISensorInfo(sdbusplus::bus::bus& bus)
{
constexpr auto service = "xyz.openbmc_project.Ipmi.Host";
@@ -1212,19 +1291,11 @@ void Add::filterSEL(sdbusplus::bus::bus& bus, const std::string& path)
}
}
- if (!ipmiSensors)
+ auto sensorInfo = getSensorInfo(sensorPath);
+ if (sensorInfo != std::nullopt)
{
- ipmiSensors = initIPMISensorInfo(bus);
- }
-
- if (ipmiSensors)
- {
- auto iter = ipmiSensors->find(sensorPath);
- if (iter != ipmiSensors->end())
- {
- sensorType = iter->second[0];
- typeCode = iter->second[1];
- }
+ sensorType = sensorInfo->sensorType;
+ typeCode = sensorInfo->readingType;
}
if (sensorData == "" || typeCode == 0 || eventDir == "" || sensorType == 0)
--
2.34.1