platform-mc: Handle `Connectivity` propertiesChanged signal

From Mctp codeConstruct version 2.0 [1], mctpd supports `.Connectivity`
property under `au.com.CodeConstruct.MCTP.Endpoint` interface of the
endpoint. This commit handles the propertiesChanged signal from this
interface, and updates the Availability of the MCTP Endpoint accordingly
in the source to enable or disable message sending/receiving via that
endpoint of the terminus.

[1] https://github.com/CodeConstruct/mctp/blob/v2.0/docs/endpoint-recovery.md#proposed-design

When the discovery process first starts, it will only handle the
endpoints that have `Available` `.Connectivity`. It lets the
propertiesChanged signal trigger the adding of the endpoints when they
are back to` Available`.

On interfaceAdded signal, it assumes that mctpd only publishes available
endpoints to D-Bus, so it adds the endpoints to the terminus anyway.

Tested:
1. Enable `unsafe-writable-connectivity` option in PACKAGECONFIG of mctp
recipe.
2. After PLDM discovers all the endpoints, write `Degraded` to
`.Connectivity` of one of the endpoint.
3. Write it back to `Available` to see how message is stopped from being
sent/received via the endpoint.

Signed-off-by: Chau Ly <chaul@amperecomputing.com>
Signed-off-by: Thu Nguyen <thu@os.amperecomputing.com>
Change-Id: I5b7a38ae72e655b60d71396a1118f2809aaa3838
diff --git a/platform-mc/terminus_manager.cpp b/platform-mc/terminus_manager.cpp
index dd5ee7f..be70740 100644
--- a/platform-mc/terminus_manager.cpp
+++ b/platform-mc/terminus_manager.cpp
@@ -125,6 +125,21 @@
     return true;
 }
 
+void TerminusManager::updateMctpEndpointAvailability(const MctpInfo& mctpInfo,
+                                                     Availability availability)
+{
+    mctpInfoAvailTable.insert_or_assign(mctpInfo, availability);
+
+    if (manager)
+    {
+        auto tid = toTid(mctpInfo);
+        if (tid)
+        {
+            manager->updateAvailableState(tid.value(), availability);
+        }
+    }
+}
+
 void TerminusManager::discoverMctpTerminus(const MctpInfos& mctpInfos)
 {
     queuedMctpInfos.emplace(mctpInfos);
@@ -176,6 +191,7 @@
             auto it = findTerminusPtr(mctpInfo);
             if (it == termini.end())
             {
+                mctpInfoAvailTable[mctpInfo] = true;
                 co_await initMctpTerminus(mctpInfo);
             }
 
@@ -183,6 +199,7 @@
             auto tid = toTid(mctpInfo);
             if (!tid)
             {
+                mctpInfoAvailTable.erase(mctpInfo);
                 co_return PLDM_ERROR;
             }
             addedTids.push_back(tid.value());
@@ -221,6 +238,7 @@
 
         unmapTid(it->first);
         termini.erase(it);
+        mctpInfoAvailTable.erase(mctpInfo);
     }
 }
 
@@ -627,6 +645,17 @@
         co_return PLDM_ERROR_NOT_READY;
     }
 
+    // There's a cost of maintaining another table to hold availability
+    // status as we can't ensure that it always synchronizes with the
+    // mctpInfoTable; std::map operator[] will insert a default of boolean
+    // which is false to the mctpInfoAvailTable if the mctpInfo key doesn't
+    // exist. Once we miss to initialize the availability of an available
+    // endpoint, it will drop all the messages to/from it.
+    if (!mctpInfoAvailTable[mctpInfo.value()])
+    {
+        co_return PLDM_ERROR_NOT_READY;
+    }
+
     auto eid = std::get<0>(mctpInfo.value());
     auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
     requestMsg->hdr.instance_id = instanceIdDb.next(eid);