From 2331328784161593c6080597b784857a1544c37a Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Tue, 2 Jul 2024 19:04:27 +0200 Subject: [PATCH] [ESP32] Initialize LwIP stack when initializing CHIP stack (#34158) --- src/platform/ESP32/PlatformManagerImpl.cpp | 22 +++++++++++++-------- src/system/tests/TestSystemPacketBuffer.cpp | 2 -- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/platform/ESP32/PlatformManagerImpl.cpp b/src/platform/ESP32/PlatformManagerImpl.cpp index dfc7064d2d1c53..2c73019933c3c9 100644 --- a/src/platform/ESP32/PlatformManagerImpl.cpp +++ b/src/platform/ESP32/PlatformManagerImpl.cpp @@ -48,7 +48,7 @@ namespace chip { namespace DeviceLayer { namespace Internal { -extern CHIP_ERROR InitLwIPCoreLock(void); +extern CHIP_ERROR InitLwIPCoreLock(); } PlatformManagerImpl PlatformManagerImpl::sInstance; @@ -60,20 +60,25 @@ static int app_entropy_source(void * data, unsigned char * output, size_t len, s return 0; } -CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) +CHIP_ERROR PlatformManagerImpl::_InitChipStack() { // Arrange for CHIP-encapsulated ESP32 errors to be translated to text Internal::ESP32Utils::RegisterESP32ErrorFormatter(); // Make sure the LwIP core lock has been initialized ReturnErrorOnFailure(Internal::InitLwIPCoreLock()); + + // Initialize TCP/IP network interface, which internally initializes LwIP stack. We have to + // call this before the usage of PacketBufferHandle::New() because in case of LwIP-based pool + // allocator, the LwIP pool allocator uses the LwIP stack. + esp_err_t err = esp_netif_init(); + VerifyOrReturnError(err == ESP_OK, Internal::ESP32Utils::MapError(err)); + // Arrange for the ESP event loop to deliver events into the CHIP Device layer. - esp_err_t err = esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, PlatformManagerImpl::HandleESPSystemEvent, NULL); - if (err != ESP_OK) - { - return Internal::ESP32Utils::MapError(err); - } + err = esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, PlatformManagerImpl::HandleESPSystemEvent, nullptr); + VerifyOrReturnError(err == ESP_OK, Internal::ESP32Utils::MapError(err)); + mStartTime = System::SystemClock().GetMonotonicTimestamp(); - ReturnErrorOnFailure(chip::Crypto::add_entropy_source(app_entropy_source, NULL, 16)); + ReturnErrorOnFailure(chip::Crypto::add_entropy_source(app_entropy_source, nullptr, 16)); // Call _InitChipStack() on the generic implementation base class // to finish the initialization process. @@ -108,6 +113,7 @@ void PlatformManagerImpl::_Shutdown() Internal::GenericPlatformManagerImpl_FreeRTOS::_Shutdown(); esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, PlatformManagerImpl::HandleESPSystemEvent); + esp_netif_deinit(); } void PlatformManagerImpl::HandleESPSystemEvent(void * arg, esp_event_base_t eventBase, int32_t eventId, void * eventData) diff --git a/src/system/tests/TestSystemPacketBuffer.cpp b/src/system/tests/TestSystemPacketBuffer.cpp index 28cd93be86bab9..bdfa771888bd92 100644 --- a/src/system/tests/TestSystemPacketBuffer.cpp +++ b/src/system/tests/TestSystemPacketBuffer.cpp @@ -324,7 +324,6 @@ TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckNew) } } -#if 0 // TODO: Fix this check on ESP32 (issue #34145) #if CHIP_SYSTEM_PACKETBUFFER_FROM_LWIP_POOL || CHIP_SYSTEM_PACKETBUFFER_FROM_CHIP_POOL // Use the rest of the buffer space std::vector allocate_all_the_things; @@ -339,7 +338,6 @@ TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckNew) allocate_all_the_things.push_back(std::move(buffer)); } #endif // CHIP_SYSTEM_PACKETBUFFER_FROM_LWIP_POOL || CHIP_SYSTEM_PACKETBUFFER_FROM_CHIP_POOL -#endif } /**