From e434fe8becad5f9d7cdb13b3ca126affd32b36ef Mon Sep 17 00:00:00 2001 From: Sascha Date: Thu, 28 Dec 2023 02:10:02 +0100 Subject: [PATCH] Wait when trying to play a webstream while connecting to wifi The previous code had some issues. First of all, with DONT_ACCEPT_SAME_RFID_TWICE it would not work at all. But without a change, not even that works correctly (due to gTriedToConnectToHost never being set). IMHO the code is simplified a lot. The whole control flow can be simplified if we just wait until a connection try hast completed. --- src/AudioPlayer.cpp | 13 +++---- src/LogMessages_DE.cpp | 1 - src/LogMessages_EN.cpp | 1 - src/Wlan.cpp | 9 +---- src/logmessages.h | 1 - src/main.cpp | 84 ++++++++++++++++++------------------------ src/main.h | 6 --- 7 files changed, 42 insertions(+), 73 deletions(-) diff --git a/src/AudioPlayer.cpp b/src/AudioPlayer.cpp index efa5ff165..44d57953d 100644 --- a/src/AudioPlayer.cpp +++ b/src/AudioPlayer.cpp @@ -693,9 +693,12 @@ void AudioPlayer_Task(void *parameter) { audioReturnCode = false; if (gPlayProperties.playMode == WEBSTREAM || (gPlayProperties.playMode == LOCAL_M3U && gPlayProperties.isWebstream)) { // Webstream + // wait for wlan to connect or fail to connect before continuing + while (Wlan_ConnectionTryInProgress()) { + vTaskDelay(portTICK_PERIOD_MS * 100u); + } audioReturnCode = audio->connecttohost(*(gPlayProperties.playlist + gPlayProperties.currentTrackNumber)); gPlayProperties.playlistFinished = false; - gTriedToConnectToHost = true; } else if (gPlayProperties.playMode != WEBSTREAM && !gPlayProperties.isWebstream) { // Files from SD if (!gFSystem.exists(*(gPlayProperties.playlist + gPlayProperties.currentTrackNumber))) { // Check first if file/folder exists @@ -1081,13 +1084,7 @@ void AudioPlayer_TrackQueueDispatcher(const char *_itemToPlay, const uint32_t _l case WEBSTREAM: { // This is always just one "track" Log_Println(modeWebstream, LOGLEVEL_NOTICE); - if (Wlan_IsConnected()) { - xQueueSend(gTrackQueue, &(musicFiles), 0); - } else { - Log_Println(webstreamNotAvailable, LOGLEVEL_ERROR); - System_IndicateError(); - gPlayProperties.playMode = NO_PLAYLIST; - } + xQueueSend(gTrackQueue, &(musicFiles), 0); break; } diff --git a/src/LogMessages_DE.cpp b/src/LogMessages_DE.cpp index 0e69fd97a..387c5edf0 100644 --- a/src/LogMessages_DE.cpp +++ b/src/LogMessages_DE.cpp @@ -75,7 +75,6 @@ const char modeAllTrackAlphSortedLoop[] = "Modus: Alle Tracks eines Ordners sort const char modeAllTrackRandomLoop[] = "Modus: Alle Tracks eines Ordners zufällig in Endlosschleife"; const char modeWebstream[] = "Modus: Webstream"; const char modeWebstreamM3u[] = "Modus: Webstream (lokale .m3u-Datei)"; -const char webstreamNotAvailable[] = "Aktuell kein Webstream möglich, da keine WLAN-Verbindung vorhanden!"; const char modeInvalid[] = "Ungültiger Abspielmodus %d!"; const char modeRepeatNone[] = "Repeatmodus: Kein Repeat"; const char modeRepeatTrack[] = "Repeatmodus: Aktueller Titel"; diff --git a/src/LogMessages_EN.cpp b/src/LogMessages_EN.cpp index 3a6a08af5..8c3def797 100644 --- a/src/LogMessages_EN.cpp +++ b/src/LogMessages_EN.cpp @@ -75,7 +75,6 @@ const char modeAllTrackAlphSortedLoop[] = "Mode: all tracks (in alph. order) of const char modeAllTrackRandomLoop[] = "Mode: all tracks (in random order) of directory as infinite loop"; const char modeWebstream[] = "Mode: webstream"; const char modeWebstreamM3u[] = "Mode: Webstream (local .m3u-file)"; -const char webstreamNotAvailable[] = "Unable to access webstream as no wifi-connection is available!"; const char modeDoesNotExist[] = "Playmode does not exist!"; const char modeRepeatNone[] = "Repeatmode: no repeat"; const char modeRepeatTrack[] = "Repeatmode: current track"; diff --git a/src/Wlan.cpp b/src/Wlan.cpp index 76cfbe7e2..b30aa7cd2 100644 --- a/src/Wlan.cpp +++ b/src/Wlan.cpp @@ -333,13 +333,6 @@ void handleWifiStateConnectionSuccess() { delete dnsServer; dnsServer = nullptr; -#ifdef PLAY_LAST_RFID_AFTER_REBOOT - if (gPlayLastRfIdWhenWiFiConnected && gTriedToConnectToHost) { - gPlayLastRfIdWhenWiFiConnected = false; - recoverLastRfidPlayedFromNvs(true); - } -#endif - wifiState = WIFI_STATE_CONNECTED; } @@ -547,7 +540,7 @@ bool Wlan_DeleteNetwork(String ssid) { } bool Wlan_ConnectionTryInProgress(void) { - return wifiState == WIFI_STATE_SCAN_CONN; + return wifiState == WIFI_STATE_INIT || wifiState == WIFI_STATE_CONNECT_LAST || wifiState == WIFI_STATE_SCAN_CONN; } String Wlan_GetIpAddress(void) { diff --git a/src/logmessages.h b/src/logmessages.h index d92ae9448..6868aa122 100644 --- a/src/logmessages.h +++ b/src/logmessages.h @@ -71,7 +71,6 @@ extern const char modeAllTrackAlphSortedLoop[]; extern const char modeAllTrackRandomLoop[]; extern const char modeWebstream[]; extern const char modeWebstreamM3u[]; -extern const char webstreamNotAvailable[]; extern const char modeInvalid[]; extern const char modeRepeatNone[]; extern const char modeRepeatTrack[]; diff --git a/src/main.cpp b/src/main.cpp index 3a6e35be1..9f89ea3e4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -30,9 +30,6 @@ #include -bool gPlayLastRfIdWhenWiFiConnected = false; -bool gTriedToConnectToHost = false; - static constexpr const char *logo = R"literal( _____ ____ ____ _ | ____| / ___| | _ \ _ _ (_) _ __ ___ @@ -49,13 +46,6 @@ bool testSPIRAM(void) { return true; } -#ifdef PLAY_LAST_RFID_AFTER_REBOOT -bool recoverLastRfid = true; -bool recoverBootCount = true; -bool resetBootCount = false; -uint32_t bootCount = 0; -#endif - //////////// #if (HAL == 2) @@ -75,49 +65,43 @@ TwoWire i2cBusTwo = TwoWire(1); // At start of a boot, bootCount is incremented by one and after 30s decremented because // uptime of 30s is considered as "successful boot". void recoverBootCountFromNvs(void) { - if (recoverBootCount) { - recoverBootCount = false; - resetBootCount = true; - bootCount = gPrefsSettings.getUInt("bootCount", 999); - - if (bootCount == 999) { // first init - bootCount = 1; - gPrefsSettings.putUInt("bootCount", bootCount); - } else if (bootCount >= 3) { // considered being a bootloop => don't recover last rfid! - bootCount = 1; - gPrefsSettings.putUInt("bootCount", bootCount); - gPrefsSettings.putString("lastRfid", "-1"); // reset last rfid - Log_Println(bootLoopDetected, LOGLEVEL_ERROR); - recoverLastRfid = false; - } else { // normal operation - gPrefsSettings.putUInt("bootCount", ++bootCount); - } - } + uint32_t bootCount = gPrefsSettings.getUInt("bootCount", 999); - if (resetBootCount && millis() >= 30000) { // reset bootcount - resetBootCount = false; - bootCount = 0; + if (bootCount == 999) { // first init + bootCount = 1; gPrefsSettings.putUInt("bootCount", bootCount); + } else if (bootCount >= 3) { // considered being a bootloop => don't recover last rfid! + bootCount = 1; + gPrefsSettings.putUInt("bootCount", bootCount); + gPrefsSettings.remove("lastRfid"); // reset last rfid + Log_Println(bootLoopDetected, LOGLEVEL_ERROR); + } else { // normal operation + gPrefsSettings.putUInt("bootCount", ++bootCount); + } +} + +void resetBootCount(void) { + static bool wasReset = false; + + if (!wasReset && millis() >= 30000) { // reset bootcount + wasReset = true; + gPrefsSettings.putUInt("bootCount", 0); Log_Println(noBootLoopDetected, LOGLEVEL_INFO); } } // Get last RFID-tag applied from NVS -void recoverLastRfidPlayedFromNvs(bool force) { - if (recoverLastRfid || force) { - if (System_GetOperationMode() == OPMODE_BLUETOOTH_SINK) { // Don't recover if BT-mode is desired - recoverLastRfid = false; - return; - } - recoverLastRfid = false; - String lastRfidPlayed = gPrefsSettings.getString("lastRfid", "-1"); - if (!lastRfidPlayed.compareTo("-1")) { - Log_Println(unableToRestoreLastRfidFromNVS, LOGLEVEL_INFO); - } else { - xQueueSend(gRfidCardQueue, lastRfidPlayed.c_str(), 0); - gPlayLastRfIdWhenWiFiConnected = !force; - Log_Printf(LOGLEVEL_INFO, restoredLastRfidFromNVS, lastRfidPlayed.c_str()); - } +void recoverLastRfidPlayedFromNvs() { + if (System_GetOperationMode() == OPMODE_BLUETOOTH_SINK) { // Don't recover if BT-mode is desired + return; + } + + String lastRfidPlayed = gPrefsSettings.getString("lastRfid", "-1"); + if (!lastRfidPlayed.compareTo("-1")) { + Log_Println(unableToRestoreLastRfidFromNVS, LOGLEVEL_INFO); + } else { + xQueueSend(gRfidCardQueue, lastRfidPlayed.c_str(), 0); + Log_Printf(LOGLEVEL_INFO, restoredLastRfidFromNVS, lastRfidPlayed.c_str()); } } #endif @@ -240,6 +224,11 @@ void setup() { #ifdef CONTROLS_LOCKED_BY_DEFAULT System_SetLockControls(true); #endif + +#ifdef PLAY_LAST_RFID_AFTER_REBOOT + recoverBootCountFromNvs(); + recoverLastRfidPlayedFromNvs(); +#endif } void loop() { @@ -269,8 +258,7 @@ void loop() { Rfid_PreferenceLookupHandler(); #ifdef PLAY_LAST_RFID_AFTER_REBOOT - recoverBootCountFromNvs(); - recoverLastRfidPlayedFromNvs(); + resetBootCount(); #endif IrReceiver_Cyclic(); diff --git a/src/main.h b/src/main.h index 08092fb16..3f59c932d 100644 --- a/src/main.h +++ b/src/main.h @@ -1,8 +1,2 @@ #pragma once -extern bool gPlayLastRfIdWhenWiFiConnected; -extern bool gTriedToConnectToHost; - -#ifdef PLAY_LAST_RFID_AFTER_REBOOT -extern void recoverLastRfidPlayedFromNvs(bool force = false); -#endif