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
diff --git a/common/utils.hpp b/common/utils.hpp
index 358d0d7..b2b5c3c 100644
--- a/common/utils.hpp
+++ b/common/utils.hpp
@@ -203,6 +203,9 @@
 using InterfaceMap = std::map<std::string, PropertyMap>;
 using ObjectValueTree = std::map<sdbusplus::message::object_path, InterfaceMap>;
 
+using SensorPDR = std::vector<uint8_t>;
+using SensorPDRs = std::vector<SensorPDR>;
+
 /**
  * @brief The interface for DBusHandler
  */
@@ -511,6 +514,28 @@
                              uint16_t entityInstance, uint16_t containerId,
                              uint16_t stateSetId, bool localOrRemote);
 
+/** @brief Method to find all state sensor PDRs by type
+ *
+ *  @param[in] entityType - the entity type
+ *  @param[in] repo - opaque pointer acting as a PDR repo handle
+ *
+ *  @return vector of vector of all state sensor PDRs
+ */
+SensorPDRs getStateSensorPDRsByType(uint16_t entityType, const pldm_pdr* repo);
+
+/** @brief method to find sensor IDs based on the pldm_entity
+ *
+ *  @param[in] pdrRepo - opaque pointer acting as a PDR repo handle
+ *  @param[in] entityType - the entity type
+ *  @param[in] entityInstance - the entity instance number
+ *  @param[in] containerId - the container ID
+ *
+ *  @return vector of all sensor IDs
+ */
+std::vector<pldm::pdr::SensorID> findSensorIds(
+    const pldm_pdr* pdrRepo, uint16_t entityType, uint16_t entityInstance,
+    uint16_t containerId);
+
 /** @brief Emit the sensor event signal
  *
  *	@param[in] tid - the terminus id