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!
This commit is contained in:
1 parent
65a6cc4be7
commit
0f0b6cd664
2 files changed
+46
-56
No files matched your search
@@ -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<const char *>(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<uint8_t *>(&s);
|
||||
EEPROM.begin(sizeof(s));
|
||||
for (size_t i=0; i<sizeof(s); i++) {
|
||||
ptr[i] = EEPROM.read(i);
|
||||
}
|
||||
EEPROM.end();
|
||||
int16_t sum = 0x1234;
|
||||
for (size_t i=0; i<sizeof(url); i++) sum += s.url[i];
|
||||
|
||||
if (s.checksum == sum) {
|
||||
strcpy(url, s.url);
|
||||
isAAC = s.isAAC;
|
||||
Serial.printf_P(PSTR("Resuming stream from EEPROM: %s"), url);
|
||||
newUrl = true;
|
||||
}
|
||||
LoadSettings();
|
||||
}
|
||||
|
||||
void StartNewURL()
|
||||
@@ -297,20 +281,8 @@ void StartNewURL()
|
||||
// Stop and free existing ones
|
||||
StopPlaying();
|
||||
|
||||
// Store in "EEPROM" to restart automatically
|
||||
Settings s;
|
||||
strcpy(s.url, url);
|
||||
s.isAAC = isAAC;
|
||||
s.checksum = 0x1234;
|
||||
for (size_t i=0; i<sizeof(url); i++) s.checksum += url[i];
|
||||
uint8_t *ptr = reinterpret_cast<uint8_t *>(&s);
|
||||
EEPROM.begin(sizeof(s));
|
||||
for (size_t i=0; i<sizeof(s); i++) {
|
||||
EEPROM.write(i, ptr[i]);
|
||||
}
|
||||
EEPROM.commit();
|
||||
EEPROM.end();
|
||||
|
||||
SaveSettings();
|
||||
|
||||
file = new AudioFileSourceICYStream(url);
|
||||
file->RegisterMetadataCB(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<uint8_t *>(&s);
|
||||
EEPROM.begin(sizeof(s));
|
||||
for (size_t i=0; i<sizeof(s); i++) {
|
||||
ptr[i] = EEPROM.read(i);
|
||||
}
|
||||
EEPROM.end();
|
||||
int16_t sum = 0x1234;
|
||||
for (size_t i=0; i<sizeof(url); i++) sum += s.url[i];
|
||||
if (s.checksum == sum) {
|
||||
strcpy(url, s.url);
|
||||
isAAC = s.isAAC;
|
||||
Serial.printf_P(PSTR("Resuming stream from EEPROM: %s"), url);
|
||||
newUrl = true;
|
||||
}
|
||||
}
|
||||
|
||||
void SaveSettings()
|
||||
{
|
||||
// Store in "EEPROM" to restart automatically
|
||||
Settings s;
|
||||
memset(&s, 0, sizeof(s));
|
||||
strcpy(s.url, url);
|
||||
s.isAAC = isAAC;
|
||||
s.checksum = 0x1234;
|
||||
for (size_t i=0; i<sizeof(url); i++) s.checksum += s.url[i];
|
||||
uint8_t *ptr = reinterpret_cast<uint8_t *>(&s);
|
||||
EEPROM.begin(sizeof(s));
|
||||
for (size_t i=0; i<sizeof(s); i++) {
|
||||
EEPROM.write(i, ptr[i]);
|
||||
}
|
||||
EEPROM.commit();
|
||||
EEPROM.end();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
static int lastms = 0;
|
||||
@@ -356,7 +359,6 @@ void loop()
|
||||
strcpy_P(status, PSTR("Playing")); // By default we're OK unless the decoder says otherwise
|
||||
if (!decoder->loop()) {
|
||||
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"))) {
|
||||
|
||||
@@ -22,9 +22,6 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
#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, "<body><h1>");
|
||||
WebPrintError(client, code);
|
||||
WebPrintf(client, "</h1></body></html>\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;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user