From b00bfaf181048ac3877d9da82aed1a60ad76ddd9 Mon Sep 17 00:00:00 2001 From: Terrence Date: Mon, 13 Jan 2025 06:49:01 +0800 Subject: [PATCH] delete background task before upgrade --- main/application.cc | 29 +++++++++++++++++++++-------- main/application.h | 2 +- main/background_task.cc | 3 +++ main/main.cc | 2 +- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/main/application.cc b/main/application.cc index a2cb6726..8e8cd7fc 100644 --- a/main/application.cc +++ b/main/application.cc @@ -37,14 +37,18 @@ static const char* const STATE_STRINGS[] = { "invalid_state" }; -Application::Application() : background_task_(4096 * 8) { +Application::Application() { event_group_ = xEventGroupCreate(); + background_task_ = new BackgroundTask(4096 * 8); ota_.SetCheckVersionUrl(CONFIG_OTA_VERSION_URL); ota_.SetHeader("Device-Id", SystemInfo::GetMacAddress().c_str()); } Application::~Application() { + if (background_task_ != nullptr) { + delete background_task_; + } vEventGroupDelete(event_group_); } @@ -62,7 +66,7 @@ void Application::CheckNewVersion() { vTaskDelay(pdMS_TO_TICKS(3000)); } while (GetDeviceState() != kDeviceStateIdle); - // Use main task to do the upgrade + // Use main task to do the upgrade, not cancelable Schedule([this, &board, display]() { SetDeviceState(kDeviceStateUpgrading); @@ -71,6 +75,13 @@ void Application::CheckNewVersion() { // 预先关闭音频输出,避免升级过程有音频操作 board.GetAudioCodec()->EnableOutput(false); + { + std::lock_guard lock(mutex_); + audio_decode_queue_.clear(); + } + background_task_->WaitForCompletion(); + delete background_task_; + background_task_ = nullptr; vTaskDelay(pdMS_TO_TICKS(1000)); ota_.StartUpgrade([display](int progress, size_t speed) { @@ -80,8 +91,10 @@ void Application::CheckNewVersion() { }); // If upgrade success, the device will reboot and never reach here + display->SetStatus("更新失败"); ESP_LOGI(TAG, "Firmware upgrade failed..."); - SetDeviceState(kDeviceStateIdle); + vTaskDelay(pdMS_TO_TICKS(3000)); + esp_restart(); }); } else { ota_.MarkCurrentVersionValid(); @@ -239,7 +252,7 @@ void Application::Start() { #if CONFIG_IDF_TARGET_ESP32S3 audio_processor_.Initialize(codec->input_channels(), codec->input_reference()); audio_processor_.OnOutput([this](std::vector&& data) { - background_task_.Schedule([this, data = std::move(data)]() mutable { + background_task_->Schedule([this, data = std::move(data)]() mutable { opus_encoder_->Encode(std::move(data), [this](std::vector&& opus) { Schedule([this, opus = std::move(opus)]() { protocol_->SendAudio(opus); @@ -348,7 +361,7 @@ void Application::Start() { } else if (strcmp(state->valuestring, "stop") == 0) { Schedule([this]() { if (device_state_ == kDeviceStateSpeaking) { - background_task_.WaitForCompletion(); + background_task_->WaitForCompletion(); if (keep_listening_) { protocol_->SendStartListening(kListeningModeAutoStop); SetDeviceState(kDeviceStateListening); @@ -457,7 +470,7 @@ void Application::OutputAudio() { audio_decode_queue_.pop_front(); lock.unlock(); - background_task_.Schedule([this, codec, opus = std::move(opus)]() mutable { + background_task_->Schedule([this, codec, opus = std::move(opus)]() mutable { if (aborted_) { return; } @@ -519,7 +532,7 @@ void Application::InputAudio() { } #else if (device_state_ == kDeviceStateListening) { - background_task_.Schedule([this, data = std::move(data)]() mutable { + background_task_->Schedule([this, data = std::move(data)]() mutable { opus_encoder_->Encode(std::move(data), [this](std::vector&& opus) { Schedule([this, opus = std::move(opus)]() { protocol_->SendAudio(opus); @@ -544,7 +557,7 @@ void Application::SetDeviceState(DeviceState state) { device_state_ = state; ESP_LOGI(TAG, "STATE: %s", STATE_STRINGS[device_state_]); // The state is changed, wait for all background tasks to finish - background_task_.WaitForCompletion(); + background_task_->WaitForCompletion(); auto display = Board::GetInstance().GetDisplay(); auto led = Board::GetInstance().GetLed(); diff --git a/main/application.h b/main/application.h index 45bdaaf4..d49a6184 100644 --- a/main/application.h +++ b/main/application.h @@ -82,7 +82,7 @@ private: std::string last_iot_states_; // Audio encode / decode - BackgroundTask background_task_; + BackgroundTask* background_task_ = nullptr; std::chrono::steady_clock::time_point last_output_time_; std::list> audio_decode_queue_; diff --git a/main/background_task.cc b/main/background_task.cc index 44481ffe..5782a941 100644 --- a/main/background_task.cc +++ b/main/background_task.cc @@ -23,6 +23,9 @@ BackgroundTask::~BackgroundTask() { if (background_task_handle_ != nullptr) { vTaskDelete(background_task_handle_); } + if (task_stack_ != nullptr) { + heap_caps_free(task_stack_); + } } void BackgroundTask::Schedule(std::function callback) { diff --git a/main/main.cc b/main/main.cc index 0faeb344..6bc176cb 100755 --- a/main/main.cc +++ b/main/main.cc @@ -24,7 +24,7 @@ extern "C" void app_main(void) } ESP_ERROR_CHECK(ret); - // Otherwise, launch the application + // Launch the application Application::GetInstance().Start(); // Dump CPU usage every 10 second