oem_ibm: Persist the remote PDRs before merging

This commit persists the remote range PDRs in the repo
before they are merged. This change was needed as the
remote terminus would not send down to BMC the entity
Association PDRs of the remote terminus after the BMC
takes a reset after the PDR exchange as the PDRs will be
in BMC range of record handles.

TESTED: Tested by triggering a PDR exchange with the
remote terminus by doing poweron test and also reset the
BMC after the PDR exchange.

Change-Id: Iec2109073f984e5980d9b6e445ce0fa4198d4a0f
Signed-off-by: Sagar Srinivas <sagar.srinivas@ibm.com>
diff --git a/oem/ibm/libpldmresponder/oem_ibm_handler.cpp b/oem/ibm/libpldmresponder/oem_ibm_handler.cpp
index 3c877d8..98903a4 100644
--- a/oem/ibm/libpldmresponder/oem_ibm_handler.cpp
+++ b/oem/ibm/libpldmresponder/oem_ibm_handler.cpp
@@ -597,6 +597,21 @@
     return PLDM_SUCCESS;
 }
 
+const pldm_pdr_record*
+    pldm::responder::oem_ibm_platform::Handler::fetchLastBMCRecord(
+        const pldm_pdr* repo)
+{
+    return pldm_pdr_find_last_in_range(repo, BMC_PDR_START_RANGE,
+                                       BMC_PDR_END_RANGE);
+}
+
+bool pldm::responder::oem_ibm_platform::Handler::checkRecordHandleInRange(
+    const uint32_t& record_handle)
+{
+    return record_handle >= HOST_PDR_START_RANGE &&
+           record_handle <= HOST_PDR_END_RANGE;
+}
+
 } // namespace oem_ibm_platform
 } // namespace responder
 } // namespace pldm
diff --git a/oem/ibm/libpldmresponder/oem_ibm_handler.hpp b/oem/ibm/libpldmresponder/oem_ibm_handler.hpp
index 964534e..03369a4 100644
--- a/oem/ibm/libpldmresponder/oem_ibm_handler.hpp
+++ b/oem/ibm/libpldmresponder/oem_ibm_handler.hpp
@@ -21,6 +21,11 @@
 constexpr uint16_t ENTITY_INSTANCE_0 = 0;
 constexpr uint16_t ENTITY_INSTANCE_1 = 1;
 
+constexpr uint32_t BMC_PDR_START_RANGE = 0x00000000;
+constexpr uint32_t BMC_PDR_END_RANGE = 0x00FFFFFF;
+constexpr uint32_t HOST_PDR_START_RANGE = 0x01000000;
+constexpr uint32_t HOST_PDR_END_RANGE = 0x01FFFFFF;
+
 enum SetEventReceiverCount
 {
     SET_EVENT_RECEIVER_SENT = 0x2,
@@ -181,6 +186,23 @@
     /** @brief to check the BMC state*/
     int checkBMCState();
 
+    /** @brief Method to fetch the last BMC record from the PDR repo
+     *
+     * @param[in] repo - pointer to BMC's primary PDR repo
+     *
+     * @return the last BMC record from the repo
+     */
+    const pldm_pdr_record* fetchLastBMCRecord(const pldm_pdr* repo);
+
+    /** @brief Method to check if the record handle passed is in remote PDR
+     *         record handle range
+     *
+     *  @param[in] record_handle - record handle of the PDR
+     *
+     *  @return true if record handle passed is in host PDR record handle range
+     */
+    bool checkRecordHandleInRange(const uint32_t& record_handle);
+
     ~Handler() = default;
 
     pldm::responder::CodeUpdate* codeUpdate; //!< pointer to CodeUpdate object
diff --git a/oem/ibm/libpldmresponder/utils.cpp b/oem/ibm/libpldmresponder/utils.cpp
index c41211e..231a476 100644
--- a/oem/ibm/libpldmresponder/utils.cpp
+++ b/oem/ibm/libpldmresponder/utils.cpp
@@ -139,6 +139,11 @@
     return 0;
 }
 
+bool checkIfLogicalBitSet(const uint16_t& containerId)
+{
+    return !(containerId & 0x8000);
+}
+
 } // namespace utils
 } // namespace responder
 } // namespace pldm
diff --git a/oem/ibm/libpldmresponder/utils.hpp b/oem/ibm/libpldmresponder/utils.hpp
index 4a1055e..0ad6e74 100644
--- a/oem/ibm/libpldmresponder/utils.hpp
+++ b/oem/ibm/libpldmresponder/utils.hpp
@@ -35,6 +35,14 @@
  */
 int writeToUnixSocket(const int sock, const char* buf,
                       const uint64_t blockSize);
+
+/** @brief Method to check if the logical bit is set
+ *
+ *  @param[containerId] - container id of the entity
+ *
+ *  @return true or false based on the logic bit set
+ * */
+bool checkIfLogicalBitSet(const uint16_t& containerId);
 } // namespace utils
 } // namespace responder
 } // namespace pldm