Free flash space (WSSend)

Free flash space (WSSend)
This commit is contained in:
Theo Arends 2019-02-23 15:29:42 +01:00
parent a1b04d166b
commit 19bdf7e803
3 changed files with 42 additions and 40 deletions

View File

@ -432,11 +432,8 @@ const char HTTP_END[] PROGMEM =
const char HTTP_DEVICE_CONTROL[] PROGMEM = "<td style='width:%d%%'><button onclick='la(\"?o=%d\");'>%s%s</button></td>"; // ?o is related to WebGetArg("o", tmp, sizeof(tmp)); const char HTTP_DEVICE_CONTROL[] PROGMEM = "<td style='width:%d%%'><button onclick='la(\"?o=%d\");'>%s%s</button></td>"; // ?o is related to WebGetArg("o", tmp, sizeof(tmp));
const char HTTP_DEVICE_STATE[] PROGMEM = "%s<td style='width:%d{c}%s;font-size:%dpx'>%s</div></td>"; // {c} = %'><div style='text-align:center;font-weight: const char HTTP_DEVICE_STATE[] PROGMEM = "%s<td style='width:%d{c}%s;font-size:%dpx'>%s</div></td>"; // {c} = %'><div style='text-align:center;font-weight:
const char HDR_CTYPE_PLAIN[] PROGMEM = "text/plain"; enum CTypes { CT_HTML, CT_PLAIN, CT_XML, CT_JSON, CT_STREAM };
const char HDR_CTYPE_HTML[] PROGMEM = "text/html"; const char kContentTypes[] PROGMEM = "text/html|text/plain|text/xml|application/json|application/octet-stream";
const char HDR_CTYPE_XML[] PROGMEM = "text/xml";
const char HDR_CTYPE_JSON[] PROGMEM = "application/json";
const char HDR_CTYPE_STREAM[] PROGMEM = "application/octet-stream";
const char kUploadErrors[] PROGMEM = const char kUploadErrors[] PROGMEM =
D_UPLOAD_ERR_1 "|" D_UPLOAD_ERR_2 "|" D_UPLOAD_ERR_3 "|" D_UPLOAD_ERR_4 "|" D_UPLOAD_ERR_5 "|" D_UPLOAD_ERR_6 "|" D_UPLOAD_ERR_7 "|" D_UPLOAD_ERR_8 "|" D_UPLOAD_ERR_9 D_UPLOAD_ERR_1 "|" D_UPLOAD_ERR_2 "|" D_UPLOAD_ERR_3 "|" D_UPLOAD_ERR_4 "|" D_UPLOAD_ERR_5 "|" D_UPLOAD_ERR_6 "|" D_UPLOAD_ERR_7 "|" D_UPLOAD_ERR_8 "|" D_UPLOAD_ERR_9
@ -582,6 +579,12 @@ void PollDnsWebserver(void)
/*********************************************************************************************/ /*********************************************************************************************/
void WSSend(int code, int ctype, const String& content)
{
char ct[25]; // strlen("application/octet-stream") +1 = Longest Content type string
WebServer->send(code, GetTextIndexed(ct, sizeof(ct), ctype, kContentTypes), content);
}
void SetHeader(void) void SetHeader(void)
{ {
WebServer->sendHeader(F("Cache-Control"), F("no-cache, no-store, must-revalidate")); WebServer->sendHeader(F("Cache-Control"), F("no-cache, no-store, must-revalidate"));
@ -641,7 +644,7 @@ void ShowPage(String &page, bool auth)
ShowFreeMem(PSTR("ShowPage")); ShowFreeMem(PSTR("ShowPage"));
WebServer->send(200, FPSTR(HDR_CTYPE_HTML), page); WSSend(200, CT_HTML, page);
} }
void ShowPage(String &page) void ShowPage(String &page)
@ -857,7 +860,7 @@ void HandleAjaxStatusRefresh(void)
} }
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s</tr></table>"), mqtt_data); snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s</tr></table>"), mqtt_data);
} }
WebServer->send(200, FPSTR(HDR_CTYPE_HTML), mqtt_data); WSSend(200, CT_HTML, mqtt_data);
} }
bool HttpCheckPriviledgedAccess(bool autorequestauth = true) bool HttpCheckPriviledgedAccess(bool autorequestauth = true)
@ -921,7 +924,7 @@ void HandleTemplateConfiguration(void)
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SCRIPT_MODULE3, midx, AnyModuleName(midx).c_str(), midx +1); snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SCRIPT_MODULE3, midx, AnyModuleName(midx).c_str(), midx +1);
page += mqtt_data; page += mqtt_data;
} }
WebServer->send(200, FPSTR(HDR_CTYPE_PLAIN), page); WSSend(200, CT_PLAIN, page);
return; return;
} }
@ -958,7 +961,7 @@ void HandleTemplateConfiguration(void)
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,%d,%d"), mqtt_data, flag, Settings.user_template_base); // FLAG: ,1 BASE: ,17 snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,%d,%d"), mqtt_data, flag, Settings.user_template_base); // FLAG: ,1 BASE: ,17
page += mqtt_data; page += mqtt_data;
WebServer->send(200, FPSTR(HDR_CTYPE_PLAIN), page); WSSend(200, CT_PLAIN, page);
return; return;
} }
@ -1050,7 +1053,7 @@ void HandleModuleConfiguration(void)
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SCRIPT_MODULE3, midx, AnyModuleName(midx).c_str(), vidx); snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SCRIPT_MODULE3, midx, AnyModuleName(midx).c_str(), vidx);
page += mqtt_data; page += mqtt_data;
} }
WebServer->send(200, FPSTR(HDR_CTYPE_PLAIN), page); WSSend(200, CT_PLAIN, page);
return; return;
} }
@ -1063,7 +1066,7 @@ void HandleModuleConfiguration(void)
page += mqtt_data; page += mqtt_data;
} }
} }
WebServer->send(200, FPSTR(HDR_CTYPE_PLAIN), page); WSSend(200, CT_PLAIN, page);
return; return;
} }
@ -1461,7 +1464,7 @@ void HandleBackupConfiguration(void)
snprintf_P(attachment, sizeof(attachment), PSTR("attachment; filename=Config_%s_%s.dmp"), NoAlNumToUnderscore(friendlyname, Settings.friendlyname[0]), my_version); snprintf_P(attachment, sizeof(attachment), PSTR("attachment; filename=Config_%s_%s.dmp"), NoAlNumToUnderscore(friendlyname, Settings.friendlyname[0]), my_version);
WebServer->sendHeader(F("Content-Disposition"), attachment); WebServer->sendHeader(F("Content-Disposition"), attachment);
WebServer->send(200, FPSTR(HDR_CTYPE_STREAM), ""); WSSend(200, CT_STREAM, "");
uint16_t cfg_crc = Settings.cfg_crc; uint16_t cfg_crc = Settings.cfg_crc;
Settings.cfg_crc = GetSettingsCrc(); // Calculate crc (again) as it might be wrong when savedata = 0 (#3918) Settings.cfg_crc = GetSettingsCrc(); // Calculate crc (again) as it might be wrong when savedata = 0 (#3918)
@ -1944,7 +1947,7 @@ void HandlePreflightRequest(void)
WebServer->sendHeader(F("Access-Control-Allow-Origin"), F("*")); WebServer->sendHeader(F("Access-Control-Allow-Origin"), F("*"));
WebServer->sendHeader(F("Access-Control-Allow-Methods"), F("GET, POST")); WebServer->sendHeader(F("Access-Control-Allow-Methods"), F("GET, POST"));
WebServer->sendHeader(F("Access-Control-Allow-Headers"), F("authorization")); WebServer->sendHeader(F("Access-Control-Allow-Headers"), F("authorization"));
WebServer->send(200, FPSTR(HDR_CTYPE_HTML), ""); WSSend(200, CT_HTML, "");
} }
/*-------------------------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------------------------*/
@ -2002,7 +2005,7 @@ void HandleHttpCommand(void)
message += F(D_NEED_USER_AND_PASSWORD "\"}"); message += F(D_NEED_USER_AND_PASSWORD "\"}");
} }
SetHeader(); SetHeader();
WebServer->send(200, FPSTR(HDR_CTYPE_JSON), message); WSSend(200, CT_JSON, message);
} }
/*-------------------------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------------------------*/
@ -2072,7 +2075,7 @@ void HandleAjaxConsoleRefresh(void)
} while (counter != web_log_index); } while (counter != web_log_index);
} }
message += F("\1"); message += F("\1");
WebServer->send(200, FPSTR(HDR_CTYPE_XML), message); WSSend(200, CT_XML, message);
} }
/********************************************************************************************/ /********************************************************************************************/
@ -2097,7 +2100,7 @@ void HandleNotFound(void)
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s %s: %s\n"), mqtt_data, WebServer->argName(i).c_str(), WebServer->arg(i).c_str()); snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s %s: %s\n"), mqtt_data, WebServer->argName(i).c_str(), WebServer->arg(i).c_str());
} }
SetHeader(); SetHeader();
WebServer->send(404, FPSTR(HDR_CTYPE_PLAIN), mqtt_data); WSSend(404, CT_PLAIN, mqtt_data);
} }
} }
@ -2108,7 +2111,7 @@ bool CaptivePortal(void)
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_HTTP D_REDIRECTED)); AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_HTTP D_REDIRECTED));
WebServer->sendHeader(F("Location"), String("http://") + WebServer->client().localIP().toString(), true); WebServer->sendHeader(F("Location"), String("http://") + WebServer->client().localIP().toString(), true);
WebServer->send(302, FPSTR(HDR_CTYPE_PLAIN), ""); // Empty content inhibits Content-length header so we have to close the socket ourselves. WSSend(302, CT_PLAIN, ""); // Empty content inhibits Content-length header so we have to close the socket ourselves.
WebServer->client().stop(); // Stop is needed because we sent no content length WebServer->client().stop(); // Stop is needed because we sent no content length
return true; return true;
} }

View File

@ -980,18 +980,18 @@ void HandleKNXConfiguration(void)
page.replace( F("</script>"), page.replace( F("</script>"),
F("function GAwarning()" F("function GAwarning()"
"{" "{"
"var GA_FNUM = document.getElementById('GA_FNUM');" "var GA_FNUM = eb('GA_FNUM');"
"var GA_AREA = document.getElementById('GA_AREA');" "var GA_AREA = eb('GA_AREA');"
"var GA_FDEF = document.getElementById('GA_FDEF');" "var GA_FDEF = eb('GA_FDEF');"
"if ( GA_FNUM != null && GA_FNUM.value == '0' && GA_AREA.value == '0' && GA_FDEF.value == '0' ) {" "if ( GA_FNUM != null && GA_FNUM.value == '0' && GA_AREA.value == '0' && GA_FDEF.value == '0' ) {"
"alert('" D_KNX_WARNING "');" "alert('" D_KNX_WARNING "');"
"}" "}"
"}" "}"
"function CBwarning()" "function CBwarning()"
"{" "{"
"var CB_FNUM = document.getElementById('CB_FNUM');" "var CB_FNUM = eb('CB_FNUM');"
"var CB_AREA = document.getElementById('CB_AREA');" "var CB_AREA = eb('CB_AREA');"
"var CB_FDEF = document.getElementById('CB_FDEF');" "var CB_FDEF = eb('CB_FDEF');"
"if ( CB_FNUM != null && CB_FNUM.value == '0' && CB_AREA.value == '0' && CB_FDEF.value == '0' ) {" "if ( CB_FNUM != null && CB_FNUM.value == '0' && CB_AREA.value == '0' && CB_FDEF.value == '0' ) {"
"alert('" D_KNX_WARNING "');" "alert('" D_KNX_WARNING "');"
"}" "}"

View File

@ -405,21 +405,21 @@ void HandleUpnpEvent(void)
state_xml.replace(F("Set"), F("Get")); state_xml.replace(F("Set"), F("Get"));
} }
state_xml.replace("{x1", String(bitRead(power, devices_present -1))); state_xml.replace("{x1", String(bitRead(power, devices_present -1)));
WebServer->send(200, FPSTR(HDR_CTYPE_XML), state_xml); WSSend(200, CT_XML, state_xml);
} }
void HandleUpnpService(void) void HandleUpnpService(void)
{ {
AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, PSTR(D_WEMO_EVENT_SERVICE)); AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, PSTR(D_WEMO_EVENT_SERVICE));
WebServer->send(200, FPSTR(HDR_CTYPE_PLAIN), FPSTR(WEMO_EVENTSERVICE_XML)); WSSend(200, CT_PLAIN, FPSTR(WEMO_EVENTSERVICE_XML));
} }
void HandleUpnpMetaService(void) void HandleUpnpMetaService(void)
{ {
AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, PSTR(D_WEMO_META_SERVICE)); AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, PSTR(D_WEMO_META_SERVICE));
WebServer->send(200, FPSTR(HDR_CTYPE_PLAIN), FPSTR(WEMO_METASERVICE_XML)); WSSend(200, CT_PLAIN, FPSTR(WEMO_METASERVICE_XML));
} }
void HandleUpnpSetupWemo(void) void HandleUpnpSetupWemo(void)
@ -430,7 +430,7 @@ void HandleUpnpSetupWemo(void)
setup_xml.replace("{x1", Settings.friendlyname[0]); setup_xml.replace("{x1", Settings.friendlyname[0]);
setup_xml.replace("{x2", WemoUuid()); setup_xml.replace("{x2", WemoUuid());
setup_xml.replace("{x3", WemoSerialnumber()); setup_xml.replace("{x3", WemoSerialnumber());
WebServer->send(200, FPSTR(HDR_CTYPE_XML), setup_xml); WSSend(200, CT_XML, setup_xml);
} }
/*********************************************************************************************\ /*********************************************************************************************\
@ -532,7 +532,7 @@ void HandleUpnpSetupHue(void)
description_xml.replace("{x1", WiFi.localIP().toString()); description_xml.replace("{x1", WiFi.localIP().toString());
description_xml.replace("{x2", HueUuid()); description_xml.replace("{x2", HueUuid());
description_xml.replace("{x3", HueSerialnumber()); description_xml.replace("{x3", HueSerialnumber());
WebServer->send(200, FPSTR(HDR_CTYPE_XML), description_xml); WSSend(200, CT_XML, description_xml);
} }
void HueNotImplemented(String *path) void HueNotImplemented(String *path)
@ -540,7 +540,7 @@ void HueNotImplemented(String *path)
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_HTTP D_HUE_API_NOT_IMPLEMENTED " (%s)"), path->c_str()); snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_HTTP D_HUE_API_NOT_IMPLEMENTED " (%s)"), path->c_str());
AddLog(LOG_LEVEL_DEBUG_MORE); AddLog(LOG_LEVEL_DEBUG_MORE);
WebServer->send(200, FPSTR(HDR_CTYPE_JSON), "{}"); WSSend(200, CT_JSON, "{}");
} }
void HueConfigResponse(String *response) void HueConfigResponse(String *response)
@ -559,7 +559,7 @@ void HueConfig(String *path)
{ {
String response = ""; String response = "";
HueConfigResponse(&response); HueConfigResponse(&response);
WebServer->send(200, FPSTR(HDR_CTYPE_JSON), response); WSSend(200, CT_JSON, response);
} }
bool g_gotct = false; bool g_gotct = false;
@ -610,7 +610,7 @@ void HueGlobalConfig(String *path)
response += F("},\"groups\":{},\"schedules\":{},\"config\":"); response += F("},\"groups\":{},\"schedules\":{},\"config\":");
HueConfigResponse(&response); HueConfigResponse(&response);
response += "}"; response += "}";
WebServer->send(200, FPSTR(HDR_CTYPE_JSON), response); WSSend(200, CT_JSON, response);
} }
void HueAuthentication(String *path) void HueAuthentication(String *path)
@ -618,7 +618,7 @@ void HueAuthentication(String *path)
char response[38]; char response[38];
snprintf_P(response, sizeof(response), PSTR("[{\"success\":{\"username\":\"%s\"}}]"), GetHueUserId().c_str()); snprintf_P(response, sizeof(response), PSTR("[{\"success\":{\"username\":\"%s\"}}]"), GetHueUserId().c_str());
WebServer->send(200, FPSTR(HDR_CTYPE_JSON), response); WSSend(200, CT_JSON, response);
} }
void HueLights(String *path) void HueLights(String *path)
@ -627,15 +627,16 @@ void HueLights(String *path)
* http://sonoff/api/username/lights/1/state?1={"on":true,"hue":56100,"sat":254,"bri":254,"alert":"none","transitiontime":40} * http://sonoff/api/username/lights/1/state?1={"on":true,"hue":56100,"sat":254,"bri":254,"alert":"none","transitiontime":40}
*/ */
String response; String response;
uint8_t device = 1; int code = 200;
uint16_t tmp = 0;
float bri = 0; float bri = 0;
float hue = 0; float hue = 0;
float sat = 0; float sat = 0;
uint16_t tmp = 0;
uint16_t ct = 0; uint16_t ct = 0;
bool resp = false; bool resp = false;
bool on = false; bool on = false;
bool change = false; bool change = false;
uint8_t device = 1;
uint8_t maxhue = (devices_present > MAX_FRIENDLYNAMES) ? MAX_FRIENDLYNAMES : devices_present; uint8_t maxhue = (devices_present > MAX_FRIENDLYNAMES) ? MAX_FRIENDLYNAMES : devices_present;
path->remove(0,path->indexOf("/lights")); // Remove until /lights path->remove(0,path->indexOf("/lights")); // Remove until /lights
@ -651,7 +652,6 @@ void HueLights(String *path)
} }
} }
response += "}"; response += "}";
WebServer->send(200, FPSTR(HDR_CTYPE_JSON), response);
} }
else if (path->endsWith("/state")) { // Got ID/state else if (path->endsWith("/state")) { // Got ID/state
path->remove(0,8); // Remove /lights/ path->remove(0,8); // Remove /lights/
@ -761,8 +761,6 @@ void HueLights(String *path)
else { else {
response = FPSTR(HUE_ERROR_JSON); response = FPSTR(HUE_ERROR_JSON);
} }
WebServer->send(200, FPSTR(HDR_CTYPE_JSON), response);
} }
else if(path->indexOf("/lights/") >= 0) { // Got /lights/ID else if(path->indexOf("/lights/") >= 0) { // Got /lights/ID
path->remove(0,8); // Remove /lights/ path->remove(0,8); // Remove /lights/
@ -773,11 +771,12 @@ void HueLights(String *path)
response += F("{\"state\":"); response += F("{\"state\":");
HueLightStatus1(device, &response); HueLightStatus1(device, &response);
HueLightStatus2(device, &response); HueLightStatus2(device, &response);
WebServer->send(200, FPSTR(HDR_CTYPE_JSON), response);
} }
else { else {
WebServer->send(406, FPSTR(HDR_CTYPE_JSON), "{}"); response = "{}";
code = 406;
} }
WSSend(code, CT_JSON, response);
} }
void HueGroups(String *path) void HueGroups(String *path)
@ -799,7 +798,7 @@ void HueGroups(String *path)
response += F("}"); response += F("}");
} }
WebServer->send(200, FPSTR(HDR_CTYPE_JSON), response); WSSend(200, CT_JSON, response);
} }
void HandleHueApi(String *path) void HandleHueApi(String *path)