[Silabs] Adds feature to update network name in LCD status screen for WiFi combinations (#34410)

* chore: Update network name in LCD status screen

* Merge redundent event handler

* chore: Update LCD status screen to include network name from DeviceNetworkInfo

* Adds auto update of LCD (#322)

* Add an optional `withLock` parameter to `UpdateLCDStatusScreen` to control concurrent access to the chip stack.

* Added GetScreen API

* chore: Update LCD status screen to include network name from DeviceNetworkInfo

* Align `PostEvent`

* chore: Set LCD screen in BaseApplication::OnPlatformEvent

---------

Co-authored-by: Rohan S <3526930+brosahay@users.noreply.github.com>

---------

Co-authored-by: Rohan S <3526930+brosahay@users.noreply.github.com>
diff --git a/examples/platform/silabs/BaseApplication.cpp b/examples/platform/silabs/BaseApplication.cpp
index 44e6a4f..beda9d3 100644
--- a/examples/platform/silabs/BaseApplication.cpp
+++ b/examples/platform/silabs/BaseApplication.cpp
@@ -166,22 +166,6 @@
 LEDWidget * BaseApplication::sAppActionLed            = nullptr;
 BaseApplicationDelegate BaseApplication::sAppDelegate = BaseApplicationDelegate();
 
-#ifdef DIC_ENABLE
-namespace {
-void AppSpecificConnectivityEventCallback(const ChipDeviceEvent * event, intptr_t arg)
-{
-    SILABS_LOG("AppSpecificConnectivityEventCallback: call back for IPV4");
-    if ((event->Type == DeviceEventType::kInternetConnectivityChange) &&
-        (event->InternetConnectivityChange.IPv4 == kConnectivity_Established))
-    {
-        SILABS_LOG("Got IPv4 Address! Starting DIC module\n");
-        if (DIC_OK != dic_init(dic::control::subscribeCB))
-            SILABS_LOG("Failed to initialize DIC module\n");
-    }
-}
-} // namespace
-#endif // DIC_ENABLE
-
 void BaseApplicationDelegate::OnCommissioningSessionStarted()
 {
     isComissioningStarted = true;
@@ -203,7 +187,7 @@
             ChipLogError(DeviceLayer, "Failed to enable the TA Deep Sleep");
         }
     }
-#endif // CHIP_CONFIG_ENABLE_ICD_SERVER && SLI_SI917qq
+#endif // CHIP_CONFIG_ENABLE_ICD_SERVER && SLI_SI917
 }
 
 void BaseApplicationDelegate::OnFabricCommitted(const FabricTable & fabricTable, FabricIndex fabricIndex)
@@ -297,10 +281,6 @@
     SILABS_LOG("Current Software Version String: %s", CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING);
     SILABS_LOG("Current Software Version: %d", CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION);
 
-#ifdef DIC_ENABLE
-    chip::DeviceLayer::PlatformMgr().AddEventHandler(AppSpecificConnectivityEventCallback, reinterpret_cast<intptr_t>(nullptr));
-#endif // DIC_ENABLE
-
     ConfigurationMgr().LogDeviceConfig();
 
     OutputQrCode(true /*refreshLCD at init*/);
@@ -730,14 +710,25 @@
     return slLCD;
 }
 
-void BaseApplication::UpdateLCDStatusScreen(void)
+void BaseApplication::UpdateLCDStatusScreen(bool withChipStackLock)
 {
     SilabsLCD::DisplayStatus_t status;
     bool enabled, attached;
-    chip::DeviceLayer::PlatformMgr().LockChipStack();
+    if (withChipStackLock)
+    {
+        chip::DeviceLayer::PlatformMgr().LockChipStack();
+    }
 #ifdef SL_WIFI
     enabled  = ConnectivityMgr().IsWiFiStationEnabled();
     attached = ConnectivityMgr().IsWiFiStationConnected();
+    chip::DeviceLayer::NetworkCommissioning::Network network;
+    memset(reinterpret_cast<void *>(&network), 0, sizeof(network));
+    chip::DeviceLayer::NetworkCommissioning::GetConnectedNetwork(network);
+    if (network.networkIDLen)
+    {
+        chip::Platform::CopyString(status.networkName, sizeof(status.networkName),
+                                   reinterpret_cast<const char *>(network.networkID));
+    }
 #endif /* SL_WIFI */
 #if CHIP_ENABLE_OPENTHREAD
     enabled  = ConnectivityMgr().IsThreadEnabled();
@@ -751,7 +742,10 @@
         ? SilabsLCD::ICDMode_e::SIT
         : SilabsLCD::ICDMode_e::LIT;
 #endif
-    chip::DeviceLayer::PlatformMgr().UnlockChipStack();
+    if (withChipStackLock)
+    {
+        chip::DeviceLayer::PlatformMgr().UnlockChipStack();
+    }
     slLCD.SetStatus(status);
 }
 #endif
@@ -822,10 +816,33 @@
 
 void BaseApplication::OnPlatformEvent(const ChipDeviceEvent * event, intptr_t)
 {
-    if (event->Type == DeviceEventType::kServiceProvisioningChange)
+    switch (event->Type)
     {
+    case DeviceEventType::kServiceProvisioningChange:
         // Note: This is only called on Attach, we need to add a method to detect Thread Network Detach
         BaseApplication::sIsProvisioned = event->ServiceProvisioningChange.IsServiceProvisioned;
+        break;
+    case DeviceEventType::kInternetConnectivityChange:
+#ifdef DIC_ENABLE
+        VerifyOrReturn(event->InternetConnectivityChange.IPv4 == kConnectivity_Established);
+        if (DIC_OK != dic_init(dic::control::subscribeCB))
+        {
+            SILABS_LOG("Failed to initialize DIC module\n");
+        }
+#endif // DIC_ENABLE
+        break;
+    case DeviceEventType::kWiFiConnectivityChange:
+#ifdef DISPLAY_ENABLED
+        SilabsLCD::Screen_e screen;
+        AppTask::GetLCD().GetScreen(screen);
+        // Update the LCD screen with SSID and connected state
+        VerifyOrReturn(screen == SilabsLCD::Screen_e::StatusScreen);
+        BaseApplication::UpdateLCDStatusScreen(false);
+        AppTask::GetLCD().SetScreen(screen);
+#endif // DISPLAY_ENABLED
+        break;
+    default:
+        break;
     }
 }
 
diff --git a/examples/platform/silabs/BaseApplication.h b/examples/platform/silabs/BaseApplication.h
index 161f1cd..9052e93 100644
--- a/examples/platform/silabs/BaseApplication.h
+++ b/examples/platform/silabs/BaseApplication.h
@@ -132,7 +132,7 @@
      */
     static SilabsLCD & GetLCD(void);
 
-    static void UpdateLCDStatusScreen(void);
+    static void UpdateLCDStatusScreen(bool withChipStackLock = true);
 #endif
 
     /**
diff --git a/examples/platform/silabs/display/lcd.cpp b/examples/platform/silabs/display/lcd.cpp
index 80cd5a1..434dedf 100644
--- a/examples/platform/silabs/display/lcd.cpp
+++ b/examples/platform/silabs/display/lcd.cpp
@@ -203,6 +203,11 @@
     customUI = cb;
 }
 
+void SilabsLCD::GetScreen(Screen_e & screen)
+{
+    screen = static_cast<Screen_e>(mCurrentScreen);
+}
+
 void SilabsLCD::SetScreen(Screen_e screen)
 {
     if (screen >= InvalidScreen)
@@ -226,6 +231,7 @@
     default:
         break;
     }
+    mCurrentScreen = screen;
 }
 
 void SilabsLCD::CycleScreens(void)
diff --git a/examples/platform/silabs/display/lcd.h b/examples/platform/silabs/display/lcd.h
index 703e72a..b62664c 100644
--- a/examples/platform/silabs/display/lcd.h
+++ b/examples/platform/silabs/display/lcd.h
@@ -26,8 +26,7 @@
 #endif // QR_CODE_ENABLED
 
 #include "demo-ui.h"
-
-#define MAX_STR_LEN 48
+#include <platform/internal/DeviceNetworkInfo.h>
 
 class SilabsLCD
 {
@@ -52,11 +51,11 @@
 
     typedef struct dStatus
     {
-        uint8_t nbFabric     = 0;
-        bool connected       = false;
-        char networkName[50] = { "TODO" };
-        bool advertising     = false;
-        ICDMode_e icdMode    = NotICD;
+        uint8_t nbFabric                                                  = 0;
+        bool connected                                                    = false;
+        char networkName[chip::DeviceLayer::Internal::kMaxWiFiSSIDLength] = { 0 };
+        bool advertising                                                  = false;
+        ICDMode_e icdMode                                                 = NotICD;
     } DisplayStatus_t;
 
     typedef void (*customUICB)(GLIB_Context_t * context);
@@ -68,6 +67,7 @@
     void WriteDemoUI(bool state);
     void SetCustomUI(customUICB cb);
 
+    void GetScreen(Screen_e & screen);
     void SetScreen(Screen_e screen);
     void CycleScreens(void);
     void SetStatus(DisplayStatus_t & status);
diff --git a/src/platform/silabs/ConnectivityManagerImpl_WIFI.cpp b/src/platform/silabs/ConnectivityManagerImpl_WIFI.cpp
index 1eb7b01..574bde2 100644
--- a/src/platform/silabs/ConnectivityManagerImpl_WIFI.cpp
+++ b/src/platform/silabs/ConnectivityManagerImpl_WIFI.cpp
@@ -357,14 +357,8 @@
 
 void ConnectivityManagerImpl::OnStationConnected()
 {
-    ChipDeviceEvent event;
     wfx_setup_ip6_link_local(SL_WFX_STA_INTERFACE);
-
     NetworkCommissioning::SlWiFiDriver::GetInstance().OnConnectWiFiNetwork();
-    // Alert other components of the new state.
-    event.Type                          = DeviceEventType::kWiFiConnectivityChange;
-    event.WiFiConnectivityChange.Result = kConnectivity_Established;
-    (void) PlatformMgr().PostEvent(&event);
     // Setting the rs911x in the power save mode
 #if (CHIP_CONFIG_ENABLE_ICD_SERVER && RS911X_WIFI)
 #if SLI_SI917
@@ -378,19 +372,22 @@
     }
 #endif /* CHIP_CONFIG_ENABLE_ICD_SERVER && RS911X_WIFI */
     UpdateInternetConnectivityState();
+    // Alert other components of the new state.
+    ChipDeviceEvent event;
+    event.Type                          = DeviceEventType::kWiFiConnectivityChange;
+    event.WiFiConnectivityChange.Result = kConnectivity_Established;
+    (void) PlatformMgr().PostEvent(&event);
 }
 
 void ConnectivityManagerImpl::OnStationDisconnected()
 {
     // TODO: Invoke WARM to perform actions that occur when the WiFi station interface goes down.
-
+    UpdateInternetConnectivityState();
     // Alert other components of the new state.
     ChipDeviceEvent event;
     event.Type                          = DeviceEventType::kWiFiConnectivityChange;
     event.WiFiConnectivityChange.Result = kConnectivity_Lost;
     (void) PlatformMgr().PostEvent(&event);
-
-    UpdateInternetConnectivityState();
 }
 
 void ConnectivityManagerImpl::DriveStationState(::chip::System::Layer * aLayer, void * aAppState)
@@ -440,8 +437,6 @@
         event.InternetConnectivityChange.IPv6      = GetConnectivityChange(hadIPv6Conn, haveIPv6Conn);
         event.InternetConnectivityChange.ipAddress = addr;
 
-        (void) PlatformMgr().PostEvent(&event);
-
         if (haveIPv4Conn != hadIPv4Conn)
         {
             ChipLogProgress(DeviceLayer, "%s Internet connectivity %s", "IPv4", (haveIPv4Conn) ? "ESTABLISHED" : "LOST");
@@ -451,6 +446,7 @@
         {
             ChipLogProgress(DeviceLayer, "%s Internet connectivity %s", "IPv6", (haveIPv6Conn) ? "ESTABLISHED" : "LOST");
         }
+        (void) PlatformMgr().PostEvent(&event);
     }
 }
 
diff --git a/src/platform/silabs/NetworkCommissioningWiFiDriver.cpp b/src/platform/silabs/NetworkCommissioningWiFiDriver.cpp
index f798054..614a121 100644
--- a/src/platform/silabs/NetworkCommissioningWiFiDriver.cpp
+++ b/src/platform/silabs/NetworkCommissioningWiFiDriver.cpp
@@ -325,19 +325,14 @@
 CHIP_ERROR GetConnectedNetwork(Network & network)
 {
     wfx_wifi_provision_t wifiConfig;
-
-    if (!wfx_is_sta_connected() || !wfx_get_wifi_provision(&wifiConfig))
-    {
-        return CHIP_ERROR_INCORRECT_STATE;
-    }
-
-    uint8_t length = strnlen(wifiConfig.ssid, DeviceLayer::Internal::kMaxWiFiSSIDLength);
-    if (length > sizeof(network.networkID))
-    {
-        ChipLogError(DeviceLayer, "SSID too long");
-        return CHIP_ERROR_INTERNAL;
-    }
-
+    network.networkIDLen = 0;
+    network.connected    = false;
+    // we are able to fetch the wifi provision data and STA should be connected
+    VerifyOrReturnError(wfx_get_wifi_provision(&wifiConfig), CHIP_ERROR_UNINITIALIZED);
+    VerifyOrReturnError(wfx_is_sta_connected(), CHIP_ERROR_NOT_CONNECTED);
+    network.connected = true;
+    uint8_t length    = strnlen(wifiConfig.ssid, DeviceLayer::Internal::kMaxWiFiSSIDLength);
+    VerifyOrReturnError(length < sizeof(network.networkID), CHIP_ERROR_BUFFER_TOO_SMALL);
     memcpy(network.networkID, wifiConfig.ssid, length);
     network.networkIDLen = length;
 
diff --git a/src/platform/silabs/NetworkCommissioningWiFiDriver.h b/src/platform/silabs/NetworkCommissioningWiFiDriver.h
index 55148e3..f152edb 100644
--- a/src/platform/silabs/NetworkCommissioningWiFiDriver.h
+++ b/src/platform/silabs/NetworkCommissioningWiFiDriver.h
@@ -30,6 +30,8 @@
 inline constexpr uint8_t kWiFiConnectNetworkTimeoutSeconds = 20;
 } // namespace
 
+CHIP_ERROR GetConnectedNetwork(Network & network);
+
 template <typename T>
 class SlScanResponseIterator : public Iterator<T>
 {