status: spotify_error, the last Spotify session error and when it happened

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-02 19:59:40 +10:00
parent 2e136ddb8c
commit 66e72cebcc
4 changed files with 19 additions and 1 deletions
+2
View File
@@ -58,6 +58,8 @@ typedef struct {
bool spotify_now_playing(spotify_track_t *t);
// Current state for status.spotify_state.
const char *spotify_state(void);
// Last session error (exception text and uptime) for status.spotify_error. False if none yet.
bool spotify_last_error(char *out, size_t len);
#ifdef __cplusplus
}
+12
View File
@@ -16,6 +16,7 @@
#include "SpircHandler.h"
#include "TrackPlayer.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "mdns.h"
@@ -39,6 +40,8 @@ static std::atomic<bool> s_got_blob{false};
static std::atomic<bool> s_in_session{false}; // session() running (its tasks may exist)
static std::atomic<int> s_volume{65535}; // player volume in Spotify units (0..65535)
static std::atomic<const char *> s_state{"disabled"};
static std::mutex s_error_mutex;
static std::string s_error; // last session error, kept for the status
static std::atomic<uint64_t> s_pcm_bytes{0};
static std::shared_ptr<cspot::SpircHandler> s_handler; // running session, else null
static spotify_pcm_cb_t s_pcm_cb;
@@ -230,6 +233,13 @@ const char *spotify_state(void)
return s_state.load();
}
bool spotify_last_error(char *out, size_t len)
{
std::lock_guard<std::mutex> lock(s_error_mutex);
snprintf(out, len, "%s", s_error.c_str());
return !s_error.empty();
}
/* ----- cspot/bell logging -> esp_log (UART + syslog) ----- */
// cspot logs through bell::bellGlobalLogger; without one, CSPOT_LOG dereferences NULL.
@@ -494,6 +504,8 @@ static void spotify_task(void *arg)
} catch (const std::exception &e) {
s_state = "error";
ESP_LOGE(TAG, "session ended: %s", e.what());
std::lock_guard<std::mutex> lock(s_error_mutex);
s_error = std::string(e.what()) + " (at " + std::to_string(esp_timer_get_time() / 1000000) + " s)";
}
s_handler.reset();
s_in_session = false;