common: Add util APIs to find all the sensor IDs and PDRs
This util API is used to find all the sensor IDs based on
the pldm_entity given. Another util API is added to get all
the PDRs based on the entity type.
Change-Id: I5a82e64c664b3ff8b981e09abe8ee610ba5ae98a
Signed-off-by: Pavithra Barithaya <pavithrabarithaya07@gmail.com>
diff --git a/common/utils.cpp b/common/utils.cpp
index 82c72d6..dff6c4e 100644
--- a/common/utils.cpp
+++ b/common/utils.cpp
@@ -816,5 +816,51 @@
return ret;
}
+SensorPDRs getStateSensorPDRsByType(uint16_t entityType, const pldm_pdr* repo)
+{
+ uint8_t* outData = nullptr;
+ uint32_t size{};
+ const pldm_pdr_record* record = nullptr;
+ SensorPDRs pdrs;
+
+ if (repo)
+ {
+ while ((record = pldm_pdr_find_record_by_type(
+ repo, PLDM_STATE_SENSOR_PDR, record, &outData, &size)))
+ {
+ auto pdr = new (outData) pldm_state_sensor_pdr;
+ if (pdr && pdr->entity_type == entityType)
+ {
+ pdrs.emplace_back(outData, outData + size);
+ }
+ }
+ }
+
+ return pdrs;
+}
+
+std::vector<pldm::pdr::SensorID> findSensorIds(
+ const pldm_pdr* pdrRepo, uint16_t entityType, uint16_t entityInstance,
+ uint16_t containerId)
+{
+ std::vector<uint16_t> sensorIDs;
+ auto pdrs = getStateSensorPDRsByType(entityType, pdrRepo);
+
+ for (const auto& pdr : pdrs)
+ {
+ auto sensorPdr =
+ reinterpret_cast<const pldm_state_sensor_pdr*>(pdr.data());
+
+ if (sensorPdr && sensorPdr->entity_type == entityType &&
+ sensorPdr->entity_instance == entityInstance &&
+ sensorPdr->container_id == containerId)
+ {
+ sensorIDs.emplace_back(sensorPdr->sensor_id);
+ }
+ }
+
+ return sensorIDs;
+}
+
} // namespace utils
} // namespace pldm