Skip to content

Commit

Permalink
was: allow cancel active notify_task from WAS
Browse files Browse the repository at this point in the history
This is a bit tricky as in FreeRTOS we cannot get the task data from a
running task. Therefore, change notify_active to a pointer to struct
notify_data we pass to the notify task, and add a member cancel to
cancel the running task.

While at it, also free the task data memory at the end to not leak
memory.
  • Loading branch information
stintel authored and kristiankielhofner committed Nov 13, 2023
1 parent f937658 commit 167dcf2
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions main/was.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

static const char *TAG = "WILLOW/WAS";
static esp_websocket_client_handle_t hdl_wc = NULL;
static volatile uint64_t notify_active;
static volatile struct notify_data *notify_active;

esp_netif_t *hdl_netif;

Expand All @@ -32,6 +32,7 @@ struct notify_data {
char *audio_url;
bool backlight;
bool backlight_max;
bool cancel;
char *text;
int repeat;
int strobe_period_ms;
Expand Down Expand Up @@ -203,6 +204,20 @@ static void IRAM_ATTR cb_ws_event(const void *arg_evh, const esp_event_base_t *b
goto cleanup;
}

cJSON *cancel = cJSON_GetObjectItemCaseSensitive(data, "cancel");
if (cJSON_IsBool(cancel) && cJSON_IsTrue(cancel)) {
if (notify_active == NULL) {
ESP_LOGW(TAG, "trying to cancel notify_task but notify_active is NULL");
goto cleanup;
}
if (notify_active->id == nd->id) {
ESP_LOGI(TAG, "cancel active notify_task with ID='%" PRIu64 "'", nd->id);
notify_active->cancel = true;
esp_audio_stop(hdl_ea, TERMINATION_TYPE_NOW);
goto cleanup;
}
}

cJSON *audio_url = cJSON_GetObjectItemCaseSensitive(data, "audio_url");
if (cJSON_IsString(audio_url) && audio_url->valuestring != NULL) {
ESP_LOGI(TAG, "audio URL in notify command: %s", audio_url->valuestring);
Expand Down Expand Up @@ -592,7 +607,7 @@ void cb_btn_cancel_notify(lv_event_t *ev)
{
ESP_LOGD(TAG, "btn_cancel pressed");
esp_audio_stop(hdl_ea, TERMINATION_TYPE_NOW);
notify_active = 0;
notify_active->cancel = true;
}

static void notify_task(void *data)
Expand All @@ -609,7 +624,7 @@ static void notify_task(void *data)
goto out;
}

notify_active = nd->id;
notify_active = nd;

ESP_LOGI(TAG, "started notify task for notification with ID='%" PRIu64 "'", nd->id);

Expand Down Expand Up @@ -647,7 +662,7 @@ static void notify_task(void *data)
gpio_set_level(get_pa_enable_gpio(), 1);

for (i = 0; i < nd->repeat; i++) {
if (!notify_active) {
if (nd->cancel) {
break;
}
esp_audio_sync_play(hdl_ea, nd->audio_url, 0);
Expand Down Expand Up @@ -700,6 +715,7 @@ static void notify_task(void *data)
cJSON_Delete(cjson);

skip_notify_done:
notify_active = 0;
free(nd);
notify_active = NULL;
vTaskDelete(NULL);
}

0 comments on commit 167dcf2

Please sign in to comment.