Skip to content

Commit

Permalink
delete background task before upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
78 committed Jan 12, 2025
1 parent 03020d8 commit b00bfaf
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
29 changes: 21 additions & 8 deletions main/application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
}

Expand All @@ -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);

Expand All @@ -71,6 +75,13 @@ void Application::CheckNewVersion() {

// 预先关闭音频输出,避免升级过程有音频操作
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));

ota_.StartUpgrade([display](int progress, size_t speed) {
Expand All @@ -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();
Expand Down Expand Up @@ -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<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) {
Schedule([this, opus = std::move(opus)]() {
protocol_->SendAudio(opus);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<uint8_t>&& opus) {
Schedule([this, opus = std::move(opus)]() {
protocol_->SendAudio(opus);
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion main/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Application {
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<std::vector<uint8_t>> audio_decode_queue_;

Expand Down
3 changes: 3 additions & 0 deletions main/background_task.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<void()> callback) {
Expand Down
2 changes: 1 addition & 1 deletion main/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b00bfaf

Please sign in to comment.