From 0f0b6cd6640105b11033bae7a8619cb417ac3b00 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Fri, 15 Dec 2017 19:03:03 -0800 Subject: [PATCH] Fix EEPROM save/load and corruption on restart EEPROM saving was not calculating the correct checksum because it was not clearing memory before writing. Fix that so it automatically will restart the connection on a reset. There was a fatal corruption caused by improper delete'ing of objects without clearing their pointers when the initial connection to a stream failed. Refactor to use the proper StopPlaying() routine. Extend the URL to 96 characters. We not have enough memory to do so! --- examples/WebRadio/WebRadio.ino | 91 +++++++++++++++++----------------- examples/WebRadio/web.cpp | 11 ---- 2 files changed, 46 insertions(+), 56 deletions(-) diff --git a/examples/WebRadio/WebRadio.ino b/examples/WebRadio/WebRadio.ino index ce0dc7e..c0da4be 100644 --- a/examples/WebRadio/WebRadio.ino +++ b/examples/WebRadio/WebRadio.ino @@ -45,13 +45,13 @@ AudioOutputI2SDAC *out = NULL; int volume = 100; char title[64]; -char url[64]; +char url[96]; char status[64]; bool newUrl = false; bool isAAC = false; typedef struct { - char url[64]; + char url[96]; bool isAAC; int16_t checksum; } Settings; @@ -209,13 +209,13 @@ void HandleStop(WiFiClient *client) RedirectToIndex(client); } -void MDCallback(void *cbData, const char *type, bool isUnicode, const char *string) +void MDCallback(void *cbData, const char *type, bool isUnicode, const char *str) { const char *ptr = reinterpret_cast(cbData); (void) isUnicode; // Punt this ball for now (void) ptr; if (strstr_P(type, PSTR("Title"))) { - strncpy_P(title, string, sizeof(title)); + strncpy(title, str, sizeof(title)); title[sizeof(title)-1] = 0; } else { // Who knows what to do? Not me! @@ -270,23 +270,7 @@ void setup() out = NULL; decoder = NULL; - // Restore from EEPROM, check the checksum matches - Settings s; - uint8_t *ptr = reinterpret_cast(&s); - EEPROM.begin(sizeof(s)); - for (size_t i=0; i(&s); - EEPROM.begin(sizeof(s)); - for (size_t i=0; iRegisterMetadataCB(MDCallback, NULL); buff = new AudioFileSourceBuffer(file, 4096); @@ -322,18 +294,49 @@ void StartNewURL() out->SetGain(((float)volume)/100.0); if (!decoder->isRunning()) { Serial.printf_P(PSTR("Can't connect to URL")); - decoder->stop(); - out->stop(); - buff->close(); - delete decoder; - delete file; - delete buff; - delete out; - decoder = NULL; + StopPlaying(); strcpy_P(status, PSTR("Unable to connect to URL")); } } +void LoadSettings() +{ + // Restore from EEPROM, check the checksum matches + Settings s; + uint8_t *ptr = reinterpret_cast(&s); + EEPROM.begin(sizeof(s)); + for (size_t i=0; i(&s); + EEPROM.begin(sizeof(s)); + for (size_t i=0; iloop()) { Serial.printf_P(PSTR("Stopping decoder\n")); - decoder->stop(); StopPlaying(); retryms = millis() + 2000; } @@ -369,7 +371,6 @@ void loop() WiFiClient client = server.available(); char *reqBuff = (char *)alloca(384); if (client && WebReadRequest(&client, reqBuff, 384, &reqUrl, ¶ms)) { - Serial.printf_P(PSTR("URL: '%s'\n"), reqUrl); if (IsIndexHTML(reqUrl)) { HandleIndex(&client); } else if (!strcmp_P(reqUrl, PSTR("stop"))) { diff --git a/examples/WebRadio/web.cpp b/examples/WebRadio/web.cpp index ec79f2d..aa4b29e 100644 --- a/examples/WebRadio/web.cpp +++ b/examples/WebRadio/web.cpp @@ -22,9 +22,6 @@ #include #include "web.h" -#define LogPrintf(fmt, ...) { Serial.printf_P(PSTR(fmt), ## __VA_ARGS__); } - - void WebPrintError(WiFiClient *client, int code) { switch(code) { @@ -40,8 +37,6 @@ void WebPrintError(WiFiClient *client, int code) void WebError(WiFiClient *client, int code, const char *headers, bool usePMEM) { - LogPrintf("+WebError: Begin, free=%d\n", ESP.getFreeHeap()); - LogPrintf(" Sending headers...\n"); WebPrintf(client, "HTTP/1.1 %d\r\n", code); WebPrintf(client, "Server: PsychoPlug\r\n"); WebPrintf(client, "Content-type: text/html\r\n"); @@ -49,7 +44,6 @@ void WebError(WiFiClient *client, int code, const char *headers, bool usePMEM) WebPrintf(client, "Pragma: no-cache\r\n"); WebPrintf(client, "Expires: 0\r\n"); WebPrintf(client, "Connection: close\r\n"); - LogPrintf("+WebError: Writing error headers: %08x\n", headers); if (headers) { if (!usePMEM) { WebPrintf(client, "%s", headers); @@ -65,7 +59,6 @@ void WebError(WiFiClient *client, int code, const char *headers, bool usePMEM) WebPrintf(client, "

"); WebPrintError(client, code); WebPrintf(client, "

\r\n"); - LogPrintf("-WebError\n"); } @@ -160,11 +153,9 @@ bool WebReadRequest(WiFiClient *client, char *reqBuff, int reqBuffLen, char **ur *urlStr = NULL; *paramStr = NULL; - LogPrintf("+WebReadRequest @ %d\n", millis()); unsigned long timeoutMS = millis() + 5000; // Max delay before we timeout while (!client->available() && millis() < timeoutMS) { delay(10); } if (!client->available()) { - LogPrintf("-WebReadRequest: Timeout @ %d\n", millis()); return false; } int wlen = client->readBytesUntil('\r', reqBuff, reqBuffLen-1); @@ -213,14 +204,12 @@ bool WebReadRequest(WiFiClient *client, char *reqBuff, int reqBuffLen, char **ur } else { // Not a GET or POST, error WebError(client, 405, PSTR("Allow: GET, POST")); - LogPrintf("-WebReadRequest(): Illegal command\n"); return false; } if (urlStr) *urlStr = url; if (paramStr) *paramStr = qp; - LogPrintf("-WebReadRequest(): Success\n"); return true; }