Add parsing JSON for State Sensor PDR
According to spec DSP0248_1.2.0: 28.6 and sensor_pdr.json, parse JSON
and generate PDR structure.
Tested with JSON file:
https://gist.github.com/lxwinspur/6a40abea7330c25e4d49826e890c4be9
Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I654913b6fa07f34f405f7dd41a5f1ac0ae2706fb
diff --git a/libpldmresponder/pdr_state_effecter.hpp b/libpldmresponder/pdr_state_effecter.hpp
index 173d328..9d10d32 100644
--- a/libpldmresponder/pdr_state_effecter.hpp
+++ b/libpldmresponder/pdr_state_effecter.hpp
@@ -96,7 +96,7 @@
auto bit = state - (index * 8);
bitfield8_t* bf = reinterpret_cast<bitfield8_t*>(start + index);
bf->byte |= 1 << bit;
- stateValues.emplace_back(std::move(state));
+ stateValues.emplace_back(state);
}
start += possibleStates->possible_states_size;
diff --git a/libpldmresponder/pdr_state_sensor.hpp b/libpldmresponder/pdr_state_sensor.hpp
new file mode 100644
index 0000000..f57773a
--- /dev/null
+++ b/libpldmresponder/pdr_state_sensor.hpp
@@ -0,0 +1,157 @@
+#pragma once
+
+#include "libpldm/platform.h"
+
+#include "libpldmresponder/pdr_utils.hpp"
+
+namespace pldm
+{
+
+namespace responder
+{
+
+namespace pdr_state_sensor
+{
+
+using Json = nlohmann::json;
+
+static const Json empty{};
+
+/** @brief Parse PDR JSON file and generate state sensor PDR structure
+ *
+ * @param[in] json - the JSON Object with the state sensor PDR
+ * @param[out] handler - the Parser of PLDM command handler
+ * @param[out] repo - pdr::RepoInterface
+ *
+ */
+template <class DBusInterface, class Handler>
+void generateStateSensorPDR(const DBusInterface& dBusIntf, const Json& json,
+ Handler& handler, pdr_utils::RepoInterface& repo)
+{
+ static const std::vector<Json> emptyList{};
+ auto entries = json.value("entries", emptyList);
+ for (const auto& e : entries)
+ {
+ size_t pdrSize = 0;
+ auto sensors = e.value("sensors", emptyList);
+ for (const auto& sensor : sensors)
+ {
+ auto set = sensor.value("set", empty);
+ auto statesSize = set.value("size", 0);
+ if (!statesSize)
+ {
+ std::cerr << "Malformed PDR JSON return "
+ "pdrEntry;- no state set "
+ "info, TYPE="
+ << PLDM_STATE_SENSOR_PDR << "\n";
+ throw InternalFailure();
+ }
+ pdrSize += sizeof(state_sensor_possible_states) -
+ sizeof(bitfield8_t) + (sizeof(bitfield8_t) * statesSize);
+ }
+ pdrSize += sizeof(pldm_state_sensor_pdr) - sizeof(uint8_t);
+
+ std::vector<uint8_t> entry{};
+ entry.resize(pdrSize);
+
+ pldm_state_sensor_pdr* pdr =
+ reinterpret_cast<pldm_state_sensor_pdr*>(entry.data());
+ pdr->hdr.record_handle = 0;
+ pdr->hdr.version = 1;
+ pdr->hdr.type = PLDM_STATE_SENSOR_PDR;
+ pdr->hdr.record_change_num = 0;
+ pdr->hdr.length = pdrSize - sizeof(pldm_pdr_hdr);
+
+ HTOLE32(pdr->hdr.record_handle);
+ HTOLE16(pdr->hdr.record_change_num);
+ HTOLE16(pdr->hdr.length);
+
+ pdr->terminus_handle = 0;
+ pdr->sensor_id = handler.getNextSensorId();
+ pdr->entity_type = e.value("type", 0);
+ pdr->entity_instance = e.value("instance", 0);
+ pdr->container_id = e.value("container", 0);
+ pdr->sensor_init = PLDM_NO_INIT;
+ pdr->sensor_auxiliary_names_pdr = false;
+ if (sensors.size() > 8)
+ {
+ throw std::runtime_error("sensor size must be less than 8");
+ }
+ pdr->composite_sensor_count = sensors.size();
+
+ HTOLE16(pdr->terminus_handle);
+ HTOLE16(pdr->sensor_id);
+ HTOLE16(pdr->entity_type);
+ HTOLE16(pdr->entity_instance);
+ HTOLE16(pdr->container_id);
+
+ DbusMappings dbusMappings{};
+ DbusValMaps dbusValMaps{};
+ uint8_t* start =
+ entry.data() + sizeof(pldm_state_sensor_pdr) - sizeof(uint8_t);
+ for (const auto& sensor : sensors)
+ {
+ auto set = sensor.value("set", empty);
+ state_sensor_possible_states* possibleStates =
+ reinterpret_cast<state_sensor_possible_states*>(start);
+ possibleStates->state_set_id = set.value("id", 0);
+ HTOLE16(possibleStates->state_set_id);
+ possibleStates->possible_states_size = set.value("size", 0);
+
+ start += sizeof(possibleStates->state_set_id) +
+ sizeof(possibleStates->possible_states_size);
+ static const std::vector<uint8_t> emptyStates{};
+ PossibleValues stateValues;
+ auto states = set.value("states", emptyStates);
+ for (const auto& state : states)
+ {
+ auto index = state / 8;
+ auto bit = state - (index * 8);
+ bitfield8_t* bf = reinterpret_cast<bitfield8_t*>(start + index);
+ bf->byte |= 1 << bit;
+ stateValues.emplace_back(state);
+ }
+ start += possibleStates->possible_states_size;
+ auto dbusEntry = sensor.value("dbus", empty);
+ auto objectPath = dbusEntry.value("path", "");
+ auto interface = dbusEntry.value("interface", "");
+ auto propertyName = dbusEntry.value("property_name", "");
+ auto propertyType = dbusEntry.value("property_type", "");
+
+ try
+ {
+ auto service =
+ dBusIntf.getService(objectPath.c_str(), interface.c_str());
+ }
+ catch (const std::exception& e)
+ {
+ continue;
+ }
+
+ pldm::utils::DBusMapping dbusMapping{objectPath, interface,
+ propertyName, propertyType};
+ dbusMappings.emplace_back(std::move(dbusMapping));
+
+ Json propValues = dbusEntry["property_values"];
+ StatestoDbusVal dbusIdToValMap =
+ populateMapping(propertyType, propValues, stateValues);
+ if (!dbusIdToValMap.empty())
+ {
+ dbusValMaps.emplace_back(std::move(dbusIdToValMap));
+ }
+ }
+
+ handler.addDbusObjMaps(
+ pdr->sensor_id,
+ std::make_tuple(std::move(dbusMappings), std::move(dbusValMaps)),
+ TypeId::PLDM_SENSOR_ID);
+ PdrEntry pdrEntry{};
+ pdrEntry.data = entry.data();
+ pdrEntry.size = pdrSize;
+ repo.addRecord(pdrEntry);
+ }
+}
+
+} // namespace pdr_state_sensor
+} // namespace responder
+} // namespace pldm
diff --git a/libpldmresponder/pdr_utils.hpp b/libpldmresponder/pdr_utils.hpp
index 2843ee6..c463f1f 100644
--- a/libpldmresponder/pdr_utils.hpp
+++ b/libpldmresponder/pdr_utils.hpp
@@ -30,6 +30,15 @@
namespace pdr_utils
{
+/** @struct Type ID associated with pdr
+ *
+ */
+enum class TypeId
+{
+ PLDM_EFFECTER_ID,
+ PLDM_SENSOR_ID
+};
+
/** @struct PdrEntry
* PDR entry structure that acts as a PDR record structure in the PDR
* repository to handle PDR APIs.
diff --git a/libpldmresponder/platform.cpp b/libpldmresponder/platform.cpp
index 3a9266e..61d09ad 100644
--- a/libpldmresponder/platform.cpp
+++ b/libpldmresponder/platform.cpp
@@ -6,6 +6,7 @@
#include "pdr.hpp"
#include "pdr_numeric_effecter.hpp"
#include "pdr_state_effecter.hpp"
+#include "pdr_state_sensor.hpp"
#include "platform_numeric_effecter.hpp"
#include "platform_state_effecter.hpp"
@@ -54,16 +55,31 @@
}};
void Handler::addDbusObjMaps(
- uint16_t effecterId,
- std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps> dbusObj)
+ uint16_t id,
+ std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps> dbusObj,
+ TypeId typeId)
{
- dbusObjMaps.emplace(effecterId, dbusObj);
+ if (typeId == TypeId::PLDM_SENSOR_ID)
+ {
+ sensorDbusObjMaps.emplace(id, dbusObj);
+ }
+ else
+ {
+ effecterDbusObjMaps.emplace(id, dbusObj);
+ }
}
const std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps>&
- Handler::getDbusObjMaps(uint16_t effecterId) const
+ Handler::getDbusObjMaps(uint16_t id, TypeId typeId) const
{
- return dbusObjMaps.at(effecterId);
+ if (typeId == TypeId::PLDM_SENSOR_ID)
+ {
+ return sensorDbusObjMaps.at(id);
+ }
+ else
+ {
+ return effecterDbusObjMaps.at(id);
+ }
}
void Handler::generate(const pldm::utils::DBusHandler& dBusIntf,
@@ -93,6 +109,12 @@
pdr_numeric_effecter::generateNumericEffecterPDR<
pldm::utils::DBusHandler, Handler>(dBusIntf, json, *this,
repo);
+ }},
+ {PLDM_STATE_SENSOR_PDR, [this](const DBusHandler& dBusIntf,
+ const auto& json, RepoInterface& repo) {
+ pdr_state_sensor::generateStateSensorPDR<pldm::utils::DBusHandler,
+ Handler>(dBusIntf, json,
+ *this, repo);
}}};
Type pdrType{};
@@ -109,6 +131,13 @@
pdrType = effecter.value("pdrType", 0);
generateHandlers.at(pdrType)(dBusIntf, effecter, repo);
}
+
+ auto sensorPDRs = json.value("sensorPDRs", empty);
+ for (const auto& sensor : sensorPDRs)
+ {
+ pdrType = sensor.value("pdrType", 0);
+ generateHandlers.at(pdrType)(dBusIntf, sensor, repo);
+ }
}
}
catch (const InternalFailure& e)
diff --git a/libpldmresponder/platform.hpp b/libpldmresponder/platform.hpp
index 5cf2534..06496dc 100644
--- a/libpldmresponder/platform.hpp
+++ b/libpldmresponder/platform.hpp
@@ -137,33 +137,42 @@
}
/** @brief Add D-Bus mapping and value mapping(stateId to D-Bus) for the
- * effecterId. If the same id is added, the previous dbusObjs will
+ * Id. If the same id is added, the previous dbusObjs will
* be "over-written".
*
- * @param[in] effecterId - effecter id
+ * @param[in] Id - effecter/sensor id
* @param[in] dbusObj - list of D-Bus object structure and list of D-Bus
* property value to attribute value
+ * @param[in] typeId - the type id of enum
*/
void addDbusObjMaps(
- uint16_t effecterId,
- std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps> dbusObj);
+ uint16_t id,
+ std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps> dbusObj,
+ TypeId typeId = TypeId::PLDM_EFFECTER_ID);
- /** @brief Retrieve an effecter id -> D-Bus objects mapping
+ /** @brief Retrieve an id -> D-Bus objects mapping
*
- * @param[in] effecterId - effecter id
+ * @param[in] Id - id
+ * @param[in] typeId - the type id of enum
*
* @return std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps> -
* list of D-Bus object structure and list of D-Bus property value
* to attribute value
*/
const std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps>&
- getDbusObjMaps(uint16_t effecterId) const;
+ getDbusObjMaps(uint16_t id,
+ TypeId typeId = TypeId::PLDM_EFFECTER_ID) const;
uint16_t getNextEffecterId()
{
return ++nextEffecterId;
}
+ uint16_t getNextSensorId()
+ {
+ return ++nextSensorId;
+ }
+
/** @brief Parse PDR JSONs and build PDR repository
*
* @param[in] dBusIntf - The interface object
@@ -340,7 +349,7 @@
try
{
const auto& [dbusMappings, dbusValMaps] =
- dbusObjMaps.at(effecterId);
+ effecterDbusObjMaps.at(effecterId);
for (uint8_t currState = 0; currState < compEffecterCnt;
++currState)
{
@@ -412,7 +421,9 @@
private:
pdr_utils::Repo pdrRepo;
uint16_t nextEffecterId{};
- DbusObjMaps dbusObjMaps{};
+ uint16_t nextSensorId{};
+ DbusObjMaps effecterDbusObjMaps{};
+ DbusObjMaps sensorDbusObjMaps{};
HostPDRHandler* hostPDRHandler;
events::StateSensorHandler stateSensorHandler;
fru::Handler* fruHandler;