Skip to content

Commit

Permalink
[ESP32] Add matter shutdown flow in all-clusters-app.
Browse files Browse the repository at this point in the history
  • Loading branch information
jadhavrohit924 committed Jan 7, 2025
1 parent 1b4c56c commit 62ff25f
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 0 deletions.
6 changes: 6 additions & 0 deletions examples/all-clusters-app/esp32/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ CHIP_ERROR AppTask::StartAppTask()
return (xReturned == pdPASS) ? CHIP_NO_ERROR : APP_ERROR_CREATE_TASK_FAILED;
}

void AppTask::DeleteAppTask()
{
vTaskDelete(sAppTaskHandle);
vQueueDelete(sAppEventQueue);
}

void AppTask::TimerEventHandler(TimerHandle_t xTimer)
{
AppEvent event;
Expand Down
1 change: 1 addition & 0 deletions examples/all-clusters-app/esp32/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class AppTask
{
public:
CHIP_ERROR StartAppTask();
void DeleteAppTask();
static void AppTaskMain(void * pvParameter);
void PostEvent(const AppEvent * event);
void ButtonEventHandler(uint8_t btnIdx, uint8_t btnAction);
Expand Down
27 changes: 27 additions & 0 deletions examples/all-clusters-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,33 @@ void emberAfLaundryDryerControlsClusterInitCallback(EndpointId endpoint)
LaundryDryerControlsServer::SetDefaultDelegate(endpoint, &LaundryDryerControlDelegate::getLaundryDryerControlDelegate());
}

static void Shutdown(TimerHandle_t xTimer)
{
DeviceLayer::StackLock lock;
ESP_LOGE(TAG, "Shutdown called");
Esp32AppServer::Shutdown();

ESP_LOGE(TAG, "Esp32AppServer Shutdown");
GetAppTask().DeleteAppTask();

ESP_LOGE(TAG, "DeleteAppTask");
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
if (DeviceLayer::Internal::ESP32Utils::DeInitWiFiStack() != CHIP_NO_ERROR)
{
ESP_LOGE(TAG, "Failed to deinitialize the Wi-Fi stack");
return;
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI

ESP_LOGE(TAG, "nvs_flash_deinit");
esp_err_t err = nvs_flash_deinit();
if (err != ESP_OK)
{
ESP_LOGE(TAG, "nvs_flash_init() failed: %s", esp_err_to_name(err));
return;
}
}

extern "C" void app_main()
{
// Initialize the ESP NVS layer.
Expand Down
9 changes: 9 additions & 0 deletions examples/platform/esp32/common/Esp32AppServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ void Esp32AppServer::DeInitBLEIfCommissioned(void)
#endif /* CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING */
}

void Esp32AppServer::Shutdown()
{
chip::DeviceLayer::Internal::BLEMgr().Shutdown();
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
chip::app::DnssdServer::Instance().StopServer();
#endif
chip::Server::GetInstance().Shutdown();
}

void Esp32AppServer::Init(AppDelegate * sAppDelegate)
{
// Init ZCL Data Model and CHIP App Server
Expand Down
1 change: 1 addition & 0 deletions examples/platform/esp32/common/Esp32AppServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
namespace Esp32AppServer {
void DeInitBLEIfCommissioned(void);
void Init(AppDelegate * context = nullptr);
void Shutdown();
} // namespace Esp32AppServer
24 changes: 24 additions & 0 deletions src/platform/ESP32/ESP32Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,30 @@ CHIP_ERROR ESP32Utils::InitWiFiStack(void)
}
return CHIP_NO_ERROR;
}

CHIP_ERROR ESP32Utils::DeInitWiFiStack(void)
{
esp_err_t err = esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, PlatformManagerImpl::HandleESPSystemEvent);
if (err != ESP_OK)
{
return ESP32Utils::MapError(err);
}

// Deinitialize the ESP WiFi layer.
err = esp_wifi_stop();
if (err != ESP_OK)
{
ESP_LOGE(TAG, "esp_wifi_stop (0x%x)", err);
return ESP32Utils::MapError(err);
}
err = esp_wifi_deinit();
if (err != ESP_OK)
{
ESP_LOGE(TAG, "esp_wifi_deinit (0x%x)", err);
return ESP32Utils::MapError(err);
}
return CHIP_NO_ERROR;
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI

struct netif * ESP32Utils::GetNetif(const char * ifKey)
Expand Down
1 change: 1 addition & 0 deletions src/platform/ESP32/ESP32Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class ESP32Utils
static CHIP_ERROR SetWiFiStationProvision(const Internal::DeviceNetworkInfo & netInfo);
static CHIP_ERROR ClearWiFiStationProvision(void);
static CHIP_ERROR InitWiFiStack(void);
static CHIP_ERROR DeInitWiFiStack(void);

static CHIP_ERROR MapError(esp_err_t error);
static void RegisterESP32ErrorFormatter();
Expand Down

0 comments on commit 62ff25f

Please sign in to comment.