Disable WiFi/BT for qemu_esp32 (#11395)

diff --git a/src/include/platform/PlatformManager.h b/src/include/platform/PlatformManager.h
index 38cf83f..7e35cf8 100644
--- a/src/include/platform/PlatformManager.h
+++ b/src/include/platform/PlatformManager.h
@@ -393,8 +393,10 @@
  */
 inline CHIP_ERROR PlatformManager::Shutdown()
 {
-    mInitialized = false;
-    return static_cast<ImplClass *>(this)->_Shutdown();
+    CHIP_ERROR err = static_cast<ImplClass *>(this)->_Shutdown();
+    if (err == CHIP_NO_ERROR)
+        mInitialized = false;
+    return err;
 }
 
 inline void PlatformManager::LockChipStack()
diff --git a/src/include/platform/internal/GenericPlatformManagerImpl_FreeRTOS.cpp b/src/include/platform/internal/GenericPlatformManagerImpl_FreeRTOS.cpp
index 9368734..e4dd424 100644
--- a/src/include/platform/internal/GenericPlatformManagerImpl_FreeRTOS.cpp
+++ b/src/include/platform/internal/GenericPlatformManagerImpl_FreeRTOS.cpp
@@ -245,7 +245,6 @@
 template <class ImplClass>
 CHIP_ERROR GenericPlatformManagerImpl_FreeRTOS<ImplClass>::_Shutdown(void)
 {
-    VerifyOrDieWithMsg(false, DeviceLayer, "Shutdown is not implemented");
     return CHIP_ERROR_NOT_IMPLEMENTED;
 }
 
diff --git a/src/platform/ESP32/CHIPPlatformConfig.h b/src/platform/ESP32/CHIPPlatformConfig.h
index 899aae8..33bca2b 100644
--- a/src/platform/ESP32/CHIPPlatformConfig.h
+++ b/src/platform/ESP32/CHIPPlatformConfig.h
@@ -96,3 +96,15 @@
 #define CHIP_CONFIG_ENABLE_CASE_INITIATOR CONFIG_ENABLE_CASE_INITIATOR
 #define CHIP_CONFIG_ENABLE_CASE_RESPONDER CONFIG_ENABLE_CASE_RESPONDER
 #define CHIP_CONFIG_DEFAULT_INCOMING_CONNECTION_IDLE_TIMEOUT CONFIG_DEFAULT_INCOMING_CONNECTION_IDLE_TIMEOUT
+
+#ifdef CONFIG_ENABLE_WIFI_STATION
+#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION 1
+#else
+#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION 0
+#endif
+
+#ifdef CONFIG_ENABLE_WIFI_AP
+#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP 1
+#else
+#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP 0
+#endif
diff --git a/src/platform/ESP32/ConnectivityManagerImpl.h b/src/platform/ESP32/ConnectivityManagerImpl.h
index efa0b6b..e1ffdb0 100644
--- a/src/platform/ESP32/ConnectivityManagerImpl.h
+++ b/src/platform/ESP32/ConnectivityManagerImpl.h
@@ -124,11 +124,6 @@
     CHIP_ERROR _GetWiFiCurrentMaxRate(uint64_t & currentMaxRate);
     CHIP_ERROR _GetWiFiOverrunCount(uint64_t & overrunCount);
 
-    // ===== Members for internal use by the following friends.
-
-    friend ConnectivityManager & ConnectivityMgr(void);
-    friend ConnectivityManagerImpl & ConnectivityMgrImpl(void);
-
     // ===== Private members reserved for use by this class only.
 
     System::Clock::Timestamp mLastStationConnectFailTime;
@@ -162,6 +157,11 @@
 
 #endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI
 
+    // ===== Members for internal use by the following friends.
+
+    friend ConnectivityManager & ConnectivityMgr(void);
+    friend ConnectivityManagerImpl & ConnectivityMgrImpl(void);
+
     static ConnectivityManagerImpl sInstance;
 };
 
diff --git a/src/platform/ESP32/PlatformManagerImpl.cpp b/src/platform/ESP32/PlatformManagerImpl.cpp
index 94989ac..9236f51 100644
--- a/src/platform/ESP32/PlatformManagerImpl.cpp
+++ b/src/platform/ESP32/PlatformManagerImpl.cpp
@@ -61,18 +61,15 @@
     // Arrange for CHIP-encapsulated ESP32 errors to be translated to text
     Internal::ESP32Utils::RegisterESP32ErrorFormatter();
 
-#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
-    wifi_init_config_t cfg;
-    uint8_t ap_mac[6];
-    wifi_mode_t mode;
     // Make sure the LwIP core lock has been initialized
     ReturnErrorOnFailure(Internal::InitLwIPCoreLock());
+
     err = esp_netif_init();
     if (err != ESP_OK)
     {
         goto exit;
     }
-#endif
+
     // Arrange for the ESP event loop to deliver events into the CHIP Device layer.
     err = esp_event_loop_create_default();
     if (err != ESP_OK)
@@ -81,32 +78,38 @@
     }
 
 #if CHIP_DEVICE_CONFIG_ENABLE_WIFI
-    esp_netif_create_default_wifi_ap();
-    esp_netif_create_default_wifi_sta();
-
-    esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, PlatformManagerImpl::HandleESPSystemEvent, NULL);
-    esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, PlatformManagerImpl::HandleESPSystemEvent, NULL);
-    mStartTime = System::SystemClock().GetMonotonicTimestamp();
-
-    // Initialize the ESP WiFi layer.
-    cfg = WIFI_INIT_CONFIG_DEFAULT();
-    err = esp_wifi_init(&cfg);
-    if (err != ESP_OK)
     {
-        goto exit;
-    }
+        wifi_init_config_t cfg;
+        uint8_t ap_mac[6];
+        wifi_mode_t mode;
 
-    esp_wifi_get_mode(&mode);
-    if ((mode == WIFI_MODE_AP) || (mode == WIFI_MODE_APSTA))
-    {
-        esp_fill_random(ap_mac, sizeof(ap_mac));
-        /* Bit 0 of the first octet of MAC Address should always be 0 */
-        ap_mac[0] &= (uint8_t) ~0x01;
-        err = esp_wifi_set_mac(WIFI_IF_AP, ap_mac);
+        esp_netif_create_default_wifi_ap();
+        esp_netif_create_default_wifi_sta();
+
+        esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, PlatformManagerImpl::HandleESPSystemEvent, NULL);
+        esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, PlatformManagerImpl::HandleESPSystemEvent, NULL);
+        mStartTime = System::SystemClock().GetMonotonicTimestamp();
+
+        // Initialize the ESP WiFi layer.
+        cfg = WIFI_INIT_CONFIG_DEFAULT();
+        err = esp_wifi_init(&cfg);
         if (err != ESP_OK)
         {
             goto exit;
         }
+
+        esp_wifi_get_mode(&mode);
+        if ((mode == WIFI_MODE_AP) || (mode == WIFI_MODE_APSTA))
+        {
+            esp_fill_random(ap_mac, sizeof(ap_mac));
+            /* Bit 0 of the first octet of MAC Address should always be 0 */
+            ap_mac[0] &= (uint8_t) ~0x01;
+            err = esp_wifi_set_mac(WIFI_IF_AP, ap_mac);
+            if (err != ESP_OK)
+            {
+                goto exit;
+            }
+        }
     }
 #endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI
 
diff --git a/src/platform/Zephyr/PlatformManagerImpl.h b/src/platform/Zephyr/PlatformManagerImpl.h
index e7999e5..bf07fa1 100644
--- a/src/platform/Zephyr/PlatformManagerImpl.h
+++ b/src/platform/Zephyr/PlatformManagerImpl.h
@@ -77,5 +77,17 @@
 {
     return PlatformManagerImpl::sInstance;
 }
+
+/**
+ * Returns the platform-specific implementation of the PlatformManager singleton object.
+ *
+ * chip applications can use this to gain access to features of the PlatformManager
+ * that are specific to the ESP32 platform.
+ */
+inline PlatformManagerImpl & PlatformMgrImpl()
+{
+    return PlatformManagerImpl::sInstance;
+}
+
 } // namespace DeviceLayer
 } // namespace chip
diff --git a/src/system/tests/TestSystemPacketBuffer.cpp b/src/system/tests/TestSystemPacketBuffer.cpp
index 70c7d6b..15a9a5e 100644
--- a/src/system/tests/TestSystemPacketBuffer.cpp
+++ b/src/system/tests/TestSystemPacketBuffer.cpp
@@ -37,6 +37,7 @@
 #include <lib/support/CHIPMem.h>
 #include <lib/support/CodeUtils.h>
 #include <lib/support/UnitTestRegistration.h>
+#include <platform/CHIPDeviceLayer.h>
 #include <system/SystemPacketBuffer.h>
 
 #if CHIP_SYSTEM_CONFIG_USE_LWIP
@@ -214,6 +215,10 @@
 int PacketBufferTest::TestSetup(void * inContext)
 {
     chip::Platform::MemoryInit();
+
+    if (chip::DeviceLayer::PlatformMgr().InitChipStack() != CHIP_NO_ERROR)
+        return FAILURE;
+
     TestContext * const theContext = reinterpret_cast<TestContext *>(inContext);
     theContext->test               = new PacketBufferTest(theContext);
     if (theContext->test == nullptr)
@@ -225,6 +230,11 @@
 
 int PacketBufferTest::TestTeardown(void * inContext)
 {
+    CHIP_ERROR err = chip::DeviceLayer::PlatformMgr().Shutdown();
+    // RTOS shutdown is not implemented, ignore CHIP_ERROR_NOT_IMPLEMENTED
+    if (err != CHIP_NO_ERROR && err != CHIP_ERROR_NOT_IMPLEMENTED)
+        return FAILURE;
+
     return SUCCESS;
 }
 
diff --git a/src/test_driver/esp32/main/main_app.cpp b/src/test_driver/esp32/main/main_app.cpp
index a742ac8..1eef2ae 100644
--- a/src/test_driver/esp32/main/main_app.cpp
+++ b/src/test_driver/esp32/main/main_app.cpp
@@ -38,13 +38,6 @@
 
 const char * TAG = "CHIP-tests";
 
-static int app_entropy_source(void * data, unsigned char * output, size_t len, size_t * olen)
-{
-    esp_fill_random(output, len);
-    *olen = len;
-    return 0;
-}
-
 static void tester_task(void * pvParameters)
 {
     ESP_LOGI(TAG, "Starting CHIP tests!");
@@ -74,56 +67,5 @@
         exit(err);
     }
 
-    // Initialize the LwIP core lock.  This must be done before the ESP
-    // tcpip_adapter layer is initialized.
-    CHIP_ERROR error = PlatformMgrImpl().InitLwIPCoreLock();
-    if (error != CHIP_NO_ERROR)
-    {
-        ESP_LOGE(TAG, "PlatformMgr().InitLocks() failed: %s", ErrorStr(error));
-        exit(1);
-    }
-
-    err = esp_netif_init();
-    if (err != ESP_OK)
-    {
-        ESP_LOGE(TAG, "esp_netif_init() failed: %s", esp_err_to_name(err));
-        exit(err);
-    }
-
-    // Arrange for the ESP event loop to deliver events into the CHIP Device layer.
-    err = esp_event_loop_create_default();
-    if (err != ESP_OK)
-    {
-        ESP_LOGE(TAG, "esp_event_loop_create_default() failed: %s", esp_err_to_name(err));
-        exit(err);
-    }
-    esp_netif_create_default_wifi_ap();
-    esp_netif_create_default_wifi_sta();
-
-    err = esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, PlatformManagerImpl::HandleESPSystemEvent, NULL);
-    if (err != ESP_OK)
-    {
-        ESP_LOGE(TAG, "esp_event_handler_register() failed for WIFI_EVENT: %s", esp_err_to_name(err));
-        exit(err);
-    }
-    err = esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, PlatformManagerImpl::HandleESPSystemEvent, NULL);
-    if (err != ESP_OK)
-    {
-        ESP_LOGE(TAG, "esp_event_handler_register() failed for IP_EVENT: %s", esp_err_to_name(err));
-        exit(err);
-    }
-
-    error = Crypto::add_entropy_source(app_entropy_source, NULL, 16);
-    if (error != CHIP_NO_ERROR)
-    {
-        ESP_LOGE(TAG, "add_entropy_source() failed: %s", ErrorStr(error));
-        exit(error.AsInteger());
-    }
-
-    xTaskCreate(tester_task, "tester", 12288, (void *) NULL, tskIDLE_PRIORITY + 10, NULL);
-
-    while (1)
-    {
-        vTaskDelay(50 / portTICK_PERIOD_MS);
-    }
+    tester_task(nullptr);
 }
diff --git a/src/test_driver/esp32/sdkconfig_qemu.defaults b/src/test_driver/esp32/sdkconfig_qemu.defaults
index cfce44f..6509c5b 100644
--- a/src/test_driver/esp32/sdkconfig_qemu.defaults
+++ b/src/test_driver/esp32/sdkconfig_qemu.defaults
@@ -35,7 +35,11 @@
 CONFIG_ESP32_PANIC_PRINT_HALT=n
 
 # enable BT
-CONFIG_BT_ENABLED=y
+CONFIG_BT_ENABLED=n
+
+# disable WiFi
+CONFIG_ENABLE_WIFI_STATION=n
+CONFIG_ENABLE_WIFI_AP=n
 
 # Use a custom partition table
 CONFIG_PARTITION_TABLE_CUSTOM=y