From 70cfce2bf458221d034074f3f3c4844ecd1143bf Mon Sep 17 00:00:00 2001
From: Serge Bazanski <q3k@q3k.org>
Date: Thu, 3 Aug 2023 01:35:41 +0200
Subject: [PATCH] st3m/audio: more graceful raed error handling
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Short reads generally shouldn't happen, but let's be defensive there.

Instead, let's explicitly check the return code of i2s reads.

Brought to you by: “my interrupt didn't get initialized and i got very
confused”.
---
 components/st3m/st3m_audio.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/components/st3m/st3m_audio.c b/components/st3m/st3m_audio.c
index 9084597757..2fef99f376 100644
--- a/components/st3m/st3m_audio.c
+++ b/components/st3m/st3m_audio.c
@@ -296,11 +296,16 @@ static void _audio_player_task(void *data) {
 
     while (true) {
         count = 0;
-        flow3r_bsp_audio_read(buffer_rx, sizeof(buffer_rx), &count, 1000);
+        esp_err_t ret =
+            flow3r_bsp_audio_read(buffer_rx, sizeof(buffer_rx), &count, 1000);
+        if (ret != ESP_OK) {
+            ESP_LOGE(TAG, "audio_read: %s", esp_err_to_name(ret));
+            abort();
+        }
         if (count != sizeof(buffer_rx)) {
             ESP_LOGE(TAG, "audio_read: count (%d) != length (%d)\n", count,
                      sizeof(buffer_rx));
-            abort();
+            continue;
         }
 
         LOCK;
@@ -574,4 +579,4 @@ DISPATCH_TY_TY(float, float, set_volume_dB)
 DISPATCH_TY_VOID(float, get_volume_dB)
 DISPATCH_VOID_TY(bool, set_mute)
 DISPATCH_TY_VOID(bool, get_mute)
-DISPATCH_TY_VOID(float, get_volume_relative)
\ No newline at end of file
+DISPATCH_TY_VOID(float, get_volume_relative)
-- 
GitLab