summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--entity_map_json.cpp30
-rw-r--r--entity_map_json.hpp20
-rw-r--r--sensorhandler.cpp12
3 files changed, 57 insertions, 5 deletions
diff --git a/entity_map_json.cpp b/entity_map_json.cpp
index 96ef61c..c0b836c 100644
--- a/entity_map_json.cpp
+++ b/entity_map_json.cpp
@@ -3,6 +3,7 @@
#include <exception>
#include <fstream>
#include <ipmid/types.hpp>
+#include <memory>
#include <nlohmann/json.hpp>
#include <string>
#include <utility>
@@ -14,9 +15,34 @@ namespace sensor
extern const EntityInfoMap entities;
-const EntityInfoMap& getIpmiEntityRecords()
+EntityInfoMapContainer* EntityInfoMapContainer::getContainer()
{
- return entities;
+ static std::unique_ptr<EntityInfoMapContainer> instance;
+
+ if (!instance)
+ {
+ /* TODO: With multi-threading this would all need to be locked so
+ * the first thread to hit it would set it up.
+ */
+ EntityInfoMap builtEntityMap = buildEntityMapFromFile();
+ if (!builtEntityMap.empty())
+ {
+ instance = std::unique_ptr<EntityInfoMapContainer>(
+ new EntityInfoMapContainer(builtEntityMap));
+ }
+ else
+ {
+ instance = std::unique_ptr<EntityInfoMapContainer>(
+ new EntityInfoMapContainer(entities));
+ }
+ }
+
+ return instance.get();
+}
+
+const EntityInfoMap& EntityInfoMapContainer::getIpmiEntityRecords()
+{
+ return entityRecords;
}
EntityInfoMap buildEntityMapFromFile()
diff --git a/entity_map_json.hpp b/entity_map_json.hpp
index 54884ee..3ab4ff2 100644
--- a/entity_map_json.hpp
+++ b/entity_map_json.hpp
@@ -1,6 +1,7 @@
#pragma once
#include <ipmid/types.hpp>
+#include <memory>
#include <nlohmann/json.hpp>
namespace ipmi
@@ -33,5 +34,24 @@ EntityInfoMap buildEntityMapFromFile();
*/
EntityInfoMap buildJsonEntityMap(const nlohmann::json& data);
+/**
+ * @brief Owner of the EntityInfoMap.
+ */
+class EntityInfoMapContainer
+{
+ public:
+ /** Get ahold of the owner. */
+ static EntityInfoMapContainer* getContainer();
+ /** Get ahold of the records. */
+ const EntityInfoMap& getIpmiEntityRecords();
+
+ private:
+ EntityInfoMapContainer(const EntityInfoMap& entityRecords) :
+ entityRecords(entityRecords)
+ {
+ }
+ EntityInfoMap entityRecords;
+};
+
} // namespace sensor
} // namespace ipmi
diff --git a/sensorhandler.cpp b/sensorhandler.cpp
index b5e2f2f..bda7fda 100644
--- a/sensorhandler.cpp
+++ b/sensorhandler.cpp
@@ -602,7 +602,9 @@ ipmi::RspType<uint8_t, // respcount
if (count.value_or(0) == getSdrCount)
{
// Get SDR count. This returns the total number of SDRs in the device.
- const auto& entityRecords = ipmi::sensor::getIpmiEntityRecords();
+ const auto& entityRecords =
+ ipmi::sensor::EntityInfoMapContainer::getContainer()
+ ->getIpmiEntityRecords();
sdrCount =
ipmi::sensor::sensors.size() + frus.size() + entityRecords.size();
}
@@ -771,7 +773,9 @@ ipmi_ret_t ipmi_fru_get_sdr(ipmi_request_t request, ipmi_response_t response,
{
// we have reached till end of fru, so assign the next record id to
// 512(Max fru ID = 511) + Entity Record ID(may start with 0).
- const auto& entityRecords = ipmi::sensor::getIpmiEntityRecords();
+ const auto& entityRecords =
+ ipmi::sensor::EntityInfoMapContainer::getContainer()
+ ->getIpmiEntityRecords();
auto next_record_id =
(entityRecords.size())
? entityRecords.begin()->first + ENTITY_RECORD_ID_START
@@ -810,7 +814,9 @@ ipmi_ret_t ipmi_entity_get_sdr(ipmi_request_t request, ipmi_response_t response,
get_sdr::SensorDataEntityRecord record{};
auto dataLength = 0;
- const auto& entityRecords = ipmi::sensor::getIpmiEntityRecords();
+ const auto& entityRecords =
+ ipmi::sensor::EntityInfoMapContainer::getContainer()
+ ->getIpmiEntityRecords();
auto entity = entityRecords.begin();
uint8_t entityRecordID;
auto recordID = get_sdr::request::get_record_id(req);
OpenPOWER on IntegriCloud