delete background task before upgrade

This commit is contained in:
Terrence
2025-01-13 06:49:01 +08:00
parent 03020d8e2e
commit b00bfaf181
4 changed files with 26 additions and 10 deletions

View File

@@ -37,14 +37,18 @@ static const char* const STATE_STRINGS[] = {
"invalid_state" "invalid_state"
}; };
Application::Application() : background_task_(4096 * 8) { Application::Application() {
event_group_ = xEventGroupCreate(); event_group_ = xEventGroupCreate();
background_task_ = new BackgroundTask(4096 * 8);
ota_.SetCheckVersionUrl(CONFIG_OTA_VERSION_URL); ota_.SetCheckVersionUrl(CONFIG_OTA_VERSION_URL);
ota_.SetHeader("Device-Id", SystemInfo::GetMacAddress().c_str()); ota_.SetHeader("Device-Id", SystemInfo::GetMacAddress().c_str());
} }
Application::~Application() { Application::~Application() {
if (background_task_ != nullptr) {
delete background_task_;
}
vEventGroupDelete(event_group_); vEventGroupDelete(event_group_);
} }
@@ -62,7 +66,7 @@ void Application::CheckNewVersion() {
vTaskDelay(pdMS_TO_TICKS(3000)); vTaskDelay(pdMS_TO_TICKS(3000));
} while (GetDeviceState() != kDeviceStateIdle); } while (GetDeviceState() != kDeviceStateIdle);
// Use main task to do the upgrade // Use main task to do the upgrade, not cancelable
Schedule([this, &board, display]() { Schedule([this, &board, display]() {
SetDeviceState(kDeviceStateUpgrading); SetDeviceState(kDeviceStateUpgrading);
@@ -71,6 +75,13 @@ void Application::CheckNewVersion() {
// 预先关闭音频输出,避免升级过程有音频操作 // 预先关闭音频输出,避免升级过程有音频操作
board.GetAudioCodec()->EnableOutput(false); board.GetAudioCodec()->EnableOutput(false);
{
std::lock_guard<std::mutex> lock(mutex_);
audio_decode_queue_.clear();
}
background_task_->WaitForCompletion();
delete background_task_;
background_task_ = nullptr;
vTaskDelay(pdMS_TO_TICKS(1000)); vTaskDelay(pdMS_TO_TICKS(1000));
ota_.StartUpgrade([display](int progress, size_t speed) { 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 // If upgrade success, the device will reboot and never reach here
display->SetStatus("更新失败");
ESP_LOGI(TAG, "Firmware upgrade failed..."); ESP_LOGI(TAG, "Firmware upgrade failed...");
SetDeviceState(kDeviceStateIdle); vTaskDelay(pdMS_TO_TICKS(3000));
esp_restart();
}); });
} else { } else {
ota_.MarkCurrentVersionValid(); ota_.MarkCurrentVersionValid();
@@ -239,7 +252,7 @@ void Application::Start() {
#if CONFIG_IDF_TARGET_ESP32S3 #if CONFIG_IDF_TARGET_ESP32S3
audio_processor_.Initialize(codec->input_channels(), codec->input_reference()); audio_processor_.Initialize(codec->input_channels(), codec->input_reference());
audio_processor_.OnOutput([this](std::vector<int16_t>&& data) { audio_processor_.OnOutput([this](std::vector<int16_t>&& 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<uint8_t>&& opus) { opus_encoder_->Encode(std::move(data), [this](std::vector<uint8_t>&& opus) {
Schedule([this, opus = std::move(opus)]() { Schedule([this, opus = std::move(opus)]() {
protocol_->SendAudio(opus); protocol_->SendAudio(opus);
@@ -348,7 +361,7 @@ void Application::Start() {
} else if (strcmp(state->valuestring, "stop") == 0) { } else if (strcmp(state->valuestring, "stop") == 0) {
Schedule([this]() { Schedule([this]() {
if (device_state_ == kDeviceStateSpeaking) { if (device_state_ == kDeviceStateSpeaking) {
background_task_.WaitForCompletion(); background_task_->WaitForCompletion();
if (keep_listening_) { if (keep_listening_) {
protocol_->SendStartListening(kListeningModeAutoStop); protocol_->SendStartListening(kListeningModeAutoStop);
SetDeviceState(kDeviceStateListening); SetDeviceState(kDeviceStateListening);
@@ -457,7 +470,7 @@ void Application::OutputAudio() {
audio_decode_queue_.pop_front(); audio_decode_queue_.pop_front();
lock.unlock(); lock.unlock();
background_task_.Schedule([this, codec, opus = std::move(opus)]() mutable { background_task_->Schedule([this, codec, opus = std::move(opus)]() mutable {
if (aborted_) { if (aborted_) {
return; return;
} }
@@ -519,7 +532,7 @@ void Application::InputAudio() {
} }
#else #else
if (device_state_ == kDeviceStateListening) { 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<uint8_t>&& opus) { opus_encoder_->Encode(std::move(data), [this](std::vector<uint8_t>&& opus) {
Schedule([this, opus = std::move(opus)]() { Schedule([this, opus = std::move(opus)]() {
protocol_->SendAudio(opus); protocol_->SendAudio(opus);
@@ -544,7 +557,7 @@ void Application::SetDeviceState(DeviceState state) {
device_state_ = state; device_state_ = state;
ESP_LOGI(TAG, "STATE: %s", STATE_STRINGS[device_state_]); ESP_LOGI(TAG, "STATE: %s", STATE_STRINGS[device_state_]);
// The state is changed, wait for all background tasks to finish // The state is changed, wait for all background tasks to finish
background_task_.WaitForCompletion(); background_task_->WaitForCompletion();
auto display = Board::GetInstance().GetDisplay(); auto display = Board::GetInstance().GetDisplay();
auto led = Board::GetInstance().GetLed(); auto led = Board::GetInstance().GetLed();

View File

@@ -82,7 +82,7 @@ private:
std::string last_iot_states_; std::string last_iot_states_;
// Audio encode / decode // Audio encode / decode
BackgroundTask background_task_; BackgroundTask* background_task_ = nullptr;
std::chrono::steady_clock::time_point last_output_time_; std::chrono::steady_clock::time_point last_output_time_;
std::list<std::vector<uint8_t>> audio_decode_queue_; std::list<std::vector<uint8_t>> audio_decode_queue_;

View File

@@ -23,6 +23,9 @@ BackgroundTask::~BackgroundTask() {
if (background_task_handle_ != nullptr) { if (background_task_handle_ != nullptr) {
vTaskDelete(background_task_handle_); vTaskDelete(background_task_handle_);
} }
if (task_stack_ != nullptr) {
heap_caps_free(task_stack_);
}
} }
void BackgroundTask::Schedule(std::function<void()> callback) { void BackgroundTask::Schedule(std::function<void()> callback) {

View File

@@ -24,7 +24,7 @@ extern "C" void app_main(void)
} }
ESP_ERROR_CHECK(ret); ESP_ERROR_CHECK(ret);
// Otherwise, launch the application // Launch the application
Application::GetInstance().Start(); Application::GetInstance().Start();
// Dump CPU usage every 10 second // Dump CPU usage every 10 second