pldm: use std::expected for instance ID allocation

Refactor InstanceIdDb::next() to return
std::expected<uint8_t, InstanceIdError> instead of throwing exceptions.
This change enables callers to explicitly handle allocation errors via
value inspection, rather than relying on exception handling.

This approach prevents core dumps from uncaught exceptions and
eliminates the need for pervasive try-catch blocks.
Callers can now access the error code and message directly, improving
clarity and control of error propagation.

Note:
Errors from InstanceIdDb::next() are currently handled via early
return, which may silently discard failures.
This is a temporary solution; APIs and callers will need to be updated
in the future to propagate and handle errors explicitly.

Change-Id: Ibf2e0034b0ee725cb59adfd93b74e48db8c42cba
Signed-off-by: Eric Yang <eric.yang.wiwynn@gmail.com>
diff --git a/host-bmc/dbus_to_event_handler.cpp b/host-bmc/dbus_to_event_handler.cpp
index 6149050..3c82cef 100644
--- a/host-bmc/dbus_to_event_handler.cpp
+++ b/host-bmc/dbus_to_event_handler.cpp
@@ -27,7 +27,13 @@
 void DbusToPLDMEvent::sendEventMsg(uint8_t eventType,
                                    const std::vector<uint8_t>& eventDataVec)
 {
-    auto instanceId = instanceIdDb.next(mctp_eid);
+    auto instanceIdResult =
+        pldm::utils::getInstanceId(instanceIdDb.next(mctp_eid));
+    if (!instanceIdResult)
+    {
+        return;
+    }
+    auto instanceId = instanceIdResult.value();
     std::vector<uint8_t> requestMsg(
         sizeof(pldm_msg_hdr) + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
         eventDataVec.size());
diff --git a/host-bmc/host_pdr_handler.cpp b/host-bmc/host_pdr_handler.cpp
index 61b4ccb..20f36df 100644
--- a/host-bmc/host_pdr_handler.cpp
+++ b/host-bmc/host_pdr_handler.cpp
@@ -189,7 +189,13 @@
     {
         recordHandle = nextRecordHandle;
     }
-    auto instanceId = instanceIdDb.next(mctp_eid);
+    auto instanceIdResult =
+        pldm::utils::getInstanceId(instanceIdDb.next(mctp_eid));
+    if (!instanceIdResult)
+    {
+        return;
+    }
+    auto instanceId = instanceIdResult.value();
 
     auto rc =
         encode_get_pdr_req(instanceId, recordHandle, 0, PLDM_GET_FIRSTPART,
@@ -388,7 +394,13 @@
             "RC", rc);
         return;
     }
-    auto instanceId = instanceIdDb.next(mctp_eid);
+    auto instanceIdResult =
+        pldm::utils::getInstanceId(instanceIdDb.next(mctp_eid));
+    if (!instanceIdResult)
+    {
+        return;
+    }
+    auto instanceId = instanceIdResult.value();
     std::vector<uint8_t> requestMsg(
         sizeof(pldm_msg_hdr) + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
         actualSize);
@@ -715,7 +727,13 @@
 void HostPDRHandler::setHostFirmwareCondition()
 {
     responseReceived = false;
-    auto instanceId = instanceIdDb.next(mctp_eid);
+    auto instanceIdResult =
+        pldm::utils::getInstanceId(instanceIdDb.next(mctp_eid));
+    if (!instanceIdResult)
+    {
+        return;
+    }
+    auto instanceId = instanceIdResult.value();
     std::vector<uint8_t> requestMsg(
         sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES);
     auto request = new (requestMsg.data()) pldm_msg;
@@ -787,7 +805,13 @@
                 sensorRearm.byte = 0;
                 uint8_t tid = std::get<0>(terminusInfo);
 
-                auto instanceId = instanceIdDb.next(mctp_eid);
+                auto instanceIdResult =
+                    pldm::utils::getInstanceId(instanceIdDb.next(mctp_eid));
+                if (!instanceIdResult)
+                {
+                    return;
+                }
+                auto instanceId = instanceIdResult.value();
                 std::vector<uint8_t> requestMsg(
                     sizeof(pldm_msg_hdr) +
                     PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES);
@@ -929,7 +953,13 @@
 void HostPDRHandler::getFRURecordTableMetadataByRemote(
     const PDRList& fruRecordSetPDRs)
 {
-    auto instanceId = instanceIdDb.next(mctp_eid);
+    auto instanceIdResult =
+        pldm::utils::getInstanceId(instanceIdDb.next(mctp_eid));
+    if (!instanceIdResult)
+    {
+        return;
+    }
+    auto instanceId = instanceIdResult.value();
     std::vector<uint8_t> requestMsg(
         sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_METADATA_REQ_BYTES);
 
@@ -1007,7 +1037,13 @@
         return;
     }
 
-    auto instanceId = instanceIdDb.next(mctp_eid);
+    auto instanceIdResult =
+        pldm::utils::getInstanceId(instanceIdDb.next(mctp_eid));
+    if (!instanceIdResult)
+    {
+        return;
+    }
+    auto instanceId = instanceIdResult.value();
     std::vector<uint8_t> requestMsg(
         sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES);