Add user friendly info in lan leash event log

Read NIC info defined in configuration and append to lan leash event log

Tested:
Unplug network cable of dedicate NIC
Check the below log message in
https://$bmcip/redfish/v1/Systems/system/LogServices/EventLog/Entries
"Message": "Physical Security: eth0(Dedicated Management NIC) LAN leash lost"

Change-Id: I9fa1984dc4d830fcd91ba9ef78415d75be6389b3
Signed-off-by: Qiang XU <qiang.xu@linux.intel.com>
diff --git a/src/IntrusionSensorMain.cpp b/src/IntrusionSensorMain.cpp
index 2c8628b..fe4a79f 100644
--- a/src/IntrusionSensorMain.cpp
+++ b/src/IntrusionSensorMain.cpp
@@ -36,6 +36,8 @@
 
 static constexpr const char* sensorType =
     "xyz.openbmc_project.Configuration.ChassisIntrusionSensor";
+static constexpr const char* nicType = "xyz.openbmc_project.Configuration.NIC";
+static constexpr std::array<const char*, 1> nicTypes = {nicType};
 
 namespace fs = std::filesystem;
 
@@ -171,7 +173,8 @@
         }
     }
 
-    std::cerr << "can't find matched I2C or GPIO configuration. \n";
+    std::cerr << "can't find matched I2C or GPIO configuration for intrusion "
+                 "sensor. \n";
     *pBusId = -1;
     *pSlaveAddr = -1;
     *pGpioIndex = -1;
@@ -180,8 +183,64 @@
 
 static constexpr bool debugLanLeash = false;
 boost::container::flat_map<int, bool> lanStatusMap;
+boost::container::flat_map<int, std::string> lanInfoMap;
 boost::container::flat_map<std::string, int> pathSuffixMap;
 
+static void
+    getNicNameInfo(std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
+{
+    auto getter = std::make_shared<GetSensorConfiguration>(
+        dbusConnection,
+        std::move([](const ManagedObjectType& sensorConfigurations) {
+            // Get NIC name and save to map
+            lanInfoMap.clear();
+            for (const std::pair<sdbusplus::message::object_path, SensorData>&
+                     sensor : sensorConfigurations)
+            {
+                const std::pair<
+                    std::string,
+                    boost::container::flat_map<std::string, BasicVariantType>>*
+                    baseConfiguration = nullptr;
+
+                // find base configuration
+                auto sensorBase = sensor.second.find(nicType);
+                if (sensorBase == sensor.second.end())
+                {
+                    continue;
+                }
+                baseConfiguration = &(*sensorBase);
+
+                auto findEthIndex = baseConfiguration->second.find("EthIndex");
+                auto findName = baseConfiguration->second.find("Name");
+
+                if (findEthIndex != baseConfiguration->second.end() &&
+                    findName != baseConfiguration->second.end())
+                {
+                    auto* pEthIndex =
+                        std::get_if<uint64_t>(&findEthIndex->second);
+                    auto* pName = std::get_if<std::string>(&findName->second);
+                    if (pEthIndex != nullptr && pName != nullptr)
+                    {
+                        lanInfoMap[*pEthIndex] = *pName;
+                        if (debugLanLeash)
+                        {
+                            std::cout << "find name of eth" << *pEthIndex
+                                      << " is " << *pName << "\n";
+                        }
+                    }
+                }
+            }
+
+            if (lanInfoMap.size() == 0)
+            {
+                std::cerr << "can't find matched NIC name. \n";
+            }
+        }));
+
+    getter->getConfiguration(
+        std::vector<std::string>{nicTypes.begin(), nicTypes.end()});
+}
+
 static void processLanStatusChange(sdbusplus::message::message& message)
 {
     const std::string& pathName = message.get_path();
@@ -194,8 +253,8 @@
     {
         return;
     }
-    std::string* pState = sdbusplus::message::variant_ns::get_if<std::string>(
-        &(findStateProperty->second));
+    std::string* pState =
+        std::get_if<std::string>(&(findStateProperty->second));
     if (pState == nullptr)
     {
         std::cerr << "invalid OperationalState \n";
@@ -221,6 +280,8 @@
         return;
     }
     int ethNum = findEthNum->second;
+
+    // get lan status from map
     auto findLanStatus = lanStatusMap.find(ethNum);
     if (findLanStatus == lanStatusMap.end())
     {
@@ -229,6 +290,21 @@
     }
     bool oldLanConnected = findLanStatus->second;
 
+    // get lan info from map
+    std::string lanInfo = "";
+    if (lanInfoMap.size() > 0)
+    {
+        auto findLanInfo = lanInfoMap.find(ethNum);
+        if (findLanInfo == lanInfoMap.end())
+        {
+            std::cerr << "unexpected eth " << ethNum << " in lanInfoMap \n";
+        }
+        else
+        {
+            lanInfo = "(" + findLanInfo->second + ")";
+        }
+    }
+
     if (debugLanLeash)
     {
         std::cout << "ethNum = " << ethNum << ", state = " << *pState
@@ -240,20 +316,18 @@
 
     if (oldLanConnected != newLanConnected)
     {
-        std::string strEthNum = "eth" + std::to_string(ethNum);
-        std::string strEvent = strEthNum + " LAN leash lost";
-        std::string strAssert = newLanConnected ? "de-asserted" : "asserted";
-        std::string strMsg = strEthNum + " is " +
-                             (newLanConnected ? "connected" : "disconnected");
-        std::string strMsgId = "OpenBMC.0.1.PhysicalSecurity";
-        sd_journal_send("MESSAGE=%s", strMsg.c_str(), "PRIORITY=%i", LOG_INFO,
+        std::string strEthNum = "eth" + std::to_string(ethNum) + lanInfo;
+        std::string strEvent = strEthNum + " LAN leash " +
+                               (newLanConnected ? "connected" : "lost");
+        std::string strMsgId =
+            newLanConnected ? "OpenBMC.0.1.LanRegained" : "OpenBMC.0.1.LanLost";
+        sd_journal_send("MESSAGE=%s", strEvent.c_str(), "PRIORITY=%i", LOG_INFO,
                         "REDFISH_MESSAGE_ID=%s", strMsgId.c_str(),
-                        "REDFISH_MESSAGE_ARGS=%s,%s", strEvent.c_str(),
-                        strAssert.c_str(), NULL);
+                        "REDFISH_MESSAGE_ARGS=%s", strEthNum.c_str(), NULL);
         lanStatusMap[ethNum] = newLanConnected;
         if (debugLanLeash)
         {
-            std::cout << "log redfish event: " << strMsg << "\n";
+            std::cout << "log redfish event: " << strEvent << "\n";
         }
     }
 }
@@ -261,6 +335,10 @@
 static void
     monitorLanStatusChange(std::shared_ptr<sdbusplus::asio::connection> conn)
 {
+    // init lan port name from configuration
+    getNicNameInfo(conn);
+
+    // get eth info from sysfs
     std::vector<fs::path> files;
     if (!findFiles(fs::path("/sys/class/net/"), R"(eth\d+/ifindex)", files))
     {
@@ -311,12 +389,15 @@
                       << ", pathSuffix = " << pathSuffix << "\n";
         }
 
-        // init lan connected status
+        // init lan connected status from networkd
         conn->async_method_call(
             [ethNum](boost::system::error_code ec,
                      const std::variant<std::string>& property) {
+                lanStatusMap[ethNum] = false;
                 if (ec)
                 {
+                    std::cerr << "Error reading init status of eth" << ethNum
+                              << "\n";
                     return;
                 }
                 const std::string* pState = std::get_if<std::string>(&property);
@@ -348,6 +429,20 @@
         "type='signal', member='PropertiesChanged',"
         "arg0namespace='org.freedesktop.network1.Link'",
         [](sdbusplus::message::message& msg) { processLanStatusChange(msg); });
+
+    // add match to monitor entity manager signal about nic name config change
+    static sdbusplus::bus::match::match match2(
+        static_cast<sdbusplus::bus::bus&>(*conn),
+        "type='signal', member='PropertiesChanged',path_namespace='" +
+            std::string(inventoryPath) + "',arg0namespace='" + nicType + "'",
+        [&conn](sdbusplus::message::message& msg) {
+            if (msg.is_method_error())
+            {
+                std::cerr << "callback method error\n";
+                return;
+            }
+            getNicNameInfo(conn);
+        });
 }
 
 int main()