pldmd: move to libpldm instance ID alloc/free

Refactor the dbus_api::Requester class to be implemented in terms of
libpldm's instance ID database. To make that easier to deal with we
introduce a light-weight RAII C++ binding along with a helper class for
unit tests.

Change-Id: Ia03de8245dfb114e6266ba36dcf26ca4398a4ce0
Signed-off-by: Rashmica Gupta <rashmica@linux.ibm.com>
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
diff --git a/pldmd/dbus_impl_requester.hpp b/pldmd/dbus_impl_requester.hpp
index a1dfcf5..2daaf18 100644
--- a/pldmd/dbus_impl_requester.hpp
+++ b/pldmd/dbus_impl_requester.hpp
@@ -1,6 +1,7 @@
 #pragma once
 
 #include "instance_id.hpp"
+#include "xyz/openbmc_project/Common/error.hpp"
 #include "xyz/openbmc_project/PLDM/Requester/server.hpp"
 
 #include <sdbusplus/bus.hpp>
@@ -34,26 +35,53 @@
     /** @brief Constructor to put object onto bus at a dbus path.
      *  @param[in] bus - Bus to attach to.
      *  @param[in] path - Path to attach at.
+     *  @param[in] db - The database to use for allocating instance IDs
+     *  @note will throw TooManyResources() if there were no free instance IDs
+     *  Throws std::system_category().default_error_condition if there is
+     *  something wrong with the instance ID database.
      */
-    Requester(sdbusplus::bus_t& bus, const std::string& path) :
-        RequesterIntf(bus, path.c_str()){};
+    Requester(sdbusplus::bus_t& bus, const std::string& path,
+              InstanceIdDb& db) :
+        RequesterIntf(bus, path.c_str()),
+        pldmInstanceIdDb(db){};
 
     /** @brief Implementation for RequesterIntf.GetInstanceId */
-    uint8_t getInstanceId(uint8_t eid) override;
+    uint8_t getInstanceId(uint8_t eid) override
+    {
+        int id;
+
+        // Ideally we would be able to look up the TID for a given EID. We don't
+        // have that infrastructure in place yet. So use the EID value for the
+        // TID. This is an interim step towards the PLDM requester logic moving
+        // into libpldm, and eventually this won't be needed.
+        try
+        {
+            id = pldmInstanceIdDb.next(eid);
+        }
+        catch (const std::runtime_error& e)
+        {
+            throw sdbusplus::xyz::openbmc_project::Common::Error::
+                TooManyResources();
+        }
+
+        return id;
+    }
 
     /** @brief Mark an instance id as unused
      *  @param[in] eid - MCTP eid to which this instance id belongs
      *  @param[in] instanceId - PLDM instance id to be freed
-     *  @note will throw std::out_of_range if instanceId > 31
+     *  @note will throw std::runtime_error if the instance ID was not
+     *  previously allocated.
+     *  Throws std::system_category().default_error_condition if there is
+     *  something wrong with the instance ID database.
      */
     void markFree(uint8_t eid, uint8_t instanceId)
     {
-        ids[eid].markFree(instanceId);
+        pldmInstanceIdDb.free(eid, instanceId);
     }
 
   private:
-    /** @brief EID to PLDM Instance ID map */
-    std::map<uint8_t, InstanceId> ids;
+    InstanceIdDb& pldmInstanceIdDb;
 };
 
 } // namespace dbus_api