[Stack] Reduce allocations on stack

This commit is contained in:
Gijs Noorlander
2018-11-30 17:32:38 +01:00
parent 84a31c0af9
commit b76e8b4e8b
10 changed files with 104 additions and 103 deletions
+6 -7
View File
@@ -225,13 +225,12 @@ void ExecuteCommand(byte source, const char *Line)
TempEvent.Source = source;
{
// Use extra scope to delete the TmpStr1 before executing command.
char TmpStr1[INPUT_COMMAND_SIZE];
TmpStr1[0] = 0;
if (GetArgv(Line, TmpStr1, 2)) TempEvent.Par1 = CalculateParam(TmpStr1);
if (GetArgv(Line, TmpStr1, 3)) TempEvent.Par2 = CalculateParam(TmpStr1);
if (GetArgv(Line, TmpStr1, 4)) TempEvent.Par3 = CalculateParam(TmpStr1);
if (GetArgv(Line, TmpStr1, 5)) TempEvent.Par4 = CalculateParam(TmpStr1);
if (GetArgv(Line, TmpStr1, 6)) TempEvent.Par5 = CalculateParam(TmpStr1);
String TmpStr1;
if (GetArgv(Line, TmpStr1, 2)) TempEvent.Par1 = CalculateParam(TmpStr1.c_str());
if (GetArgv(Line, TmpStr1, 3)) TempEvent.Par2 = CalculateParam(TmpStr1.c_str());
if (GetArgv(Line, TmpStr1, 4)) TempEvent.Par3 = CalculateParam(TmpStr1.c_str());
if (GetArgv(Line, TmpStr1, 5)) TempEvent.Par4 = CalculateParam(TmpStr1.c_str());
if (GetArgv(Line, TmpStr1, 6)) TempEvent.Par5 = CalculateParam(TmpStr1.c_str());
}
String status = doExecuteCommand(cmd.c_str(), &TempEvent, Line);
+6 -4
View File
@@ -61,13 +61,15 @@ String Command_Settings_Load(struct EventStruct *event, const char* Line)
String Command_Settings_Print(struct EventStruct *event, const char* Line)
{
char str[20];
serialPrintln();
serialPrintln(F("System Info"));
IPAddress ip = WiFi.localIP();
sprintf_P(str, PSTR("%u.%u.%u.%u"), ip[0], ip[1], ip[2], ip[3]);
serialPrint(F(" IP Address : ")); serialPrintln(str);
{
char str[20];
IPAddress ip = WiFi.localIP();
sprintf_P(str, PSTR("%u.%u.%u.%u"), ip[0], ip[1], ip[2], ip[3]);
serialPrint(F(" IP Address : ")); serialPrintln(str);
}
serialPrint(F(" Build : ")); serialPrintln(String((int)BUILD));
serialPrint(F(" Name : ")); serialPrintln(Settings.Name);
serialPrint(F(" Unit : ")); serialPrintln(String((int)Settings.Unit));
+10 -17
View File
@@ -80,24 +80,26 @@ boolean validUserVar(struct EventStruct *event) {
\*********************************************************************************************/
// handle MQTT messages
void callback(char* c_topic, byte* b_payload, unsigned int length) {
// char log[256];
char c_payload[384];
statusLED(true);
int enabledMqttController = firstEnabledMQTTController();
if (enabledMqttController < 0) {
addLog(LOG_LEVEL_ERROR, F("MQTT : No enabled MQTT controller"));
return;
}
if ((length + 1) > sizeof(c_payload))
if (length > 384)
{
addLog(LOG_LEVEL_ERROR, F("MQTT : Ignored too big message"));
return;
}
//convert payload to string, and 0 terminate
strncpy(c_payload,(char*)b_payload,length);
c_payload[length] = 0;
struct EventStruct TempEvent;
// TD-er: This one cannot set the TaskIndex, but that may seem to work out.... hopefully.
TempEvent.String1 = c_topic;
TempEvent.String2.reserve(length);
for (unsigned int i = 0; i < length; ++i) {
char c = static_cast<char>(*(b_payload + i));
TempEvent.String2 += c;
}
/*
if (loglevelActiveFor(LOG_LEVEL_DEBUG_MORE)) {
@@ -107,20 +109,11 @@ void callback(char* c_topic, byte* b_payload, unsigned int length) {
addLog(LOG_LEVEL_DEBUG_MORE, log);
log=F("MQTT : Payload: ");
log+=c_payload;
log+=TempEvent.String2;
addLog(LOG_LEVEL_DEBUG_MORE, log);
}
*/
// sprintf_P(log, PSTR("%s%s"), "MQTT : Topic: ", c_topic);
// addLog(LOG_LEVEL_DEBUG, log);
// sprintf_P(log, PSTR("%s%s"), "MQTT : Payload: ", c_payload);
// addLog(LOG_LEVEL_DEBUG, log);
struct EventStruct TempEvent;
// TD-er: This one cannot set the TaskIndex, but that may seem to work out.... hopefully.
TempEvent.String1 = c_topic;
TempEvent.String2 = c_payload;
byte ProtocolIndex = getProtocolIndex(Settings.Protocol[enabledMqttController]);
schedule_controller_event_timer(ProtocolIndex, CPLUGIN_PROTOCOL_RECV, &TempEvent);
}
+3 -1
View File
@@ -148,7 +148,8 @@ String rulesProcessingFile(const String& fileName, String& event)
byte ifBlock = 0;
byte fakeIfBlock = 0;
byte buf[RULES_BUFFER_SIZE];
byte *buf = new byte[RULES_BUFFER_SIZE]();
int len = 0;
while (f.available())
{
@@ -178,6 +179,7 @@ String rulesProcessingFile(const String& fileName, String& event)
}
}
}
delete[] buf;
if (f) f.close();
nestingLevel--;
+10 -7
View File
@@ -639,16 +639,19 @@ byte getNotificationProtocolIndex(byte Number)
\*********************************************************************************************/
bool HasArgv(const char *string, unsigned int argc) {
char TmpStr1[INPUT_COMMAND_SIZE];
return GetArgv(string, TmpStr1, INPUT_COMMAND_SIZE, argc);
String tmp;
return GetArgv(string, tmp, argc);
}
bool GetArgv(const char *string, String& argvString, unsigned int argc) {
char TmpStr1[INPUT_COMMAND_SIZE];
bool hasArgument = GetArgv(string, TmpStr1, INPUT_COMMAND_SIZE, argc);
size_t string_len = strlen(string);
if (string_len > INPUT_COMMAND_SIZE) string_len = INPUT_COMMAND_SIZE;
char *TmpStr1 = new char[string_len]();
bool hasArgument = GetArgv(string, TmpStr1, string_len, argc);
if (hasArgument) {
argvString = TmpStr1;
}
delete[] TmpStr1;
return hasArgument;
}
@@ -2240,7 +2243,7 @@ int Calculate(const char *input, float* result)
return CALCULATE_OK;
}
int CalculateParam(char *TmpStr) {
int CalculateParam(const char *TmpStr) {
int returnValue;
// Minimize calls to the Calulate function.
@@ -2249,8 +2252,8 @@ int CalculateParam(char *TmpStr) {
returnValue=str2int(TmpStr);
} else {
float param=0;
TmpStr[0] = ' '; //replace '=' with space
int returnCode=Calculate(TmpStr, &param);
// Starts with an '=', so Calculate starting at next position
int returnCode=Calculate(&TmpStr[1], &param);
if (returnCode!=CALCULATE_OK) {
String errorDesc;
switch (returnCode) {
+20 -18
View File
@@ -428,17 +428,9 @@ bool SSDP_begin() {
Send SSDP messages (notify & responses)
\*********************************************************************************************/
void SSDP_send(byte method) {
char buffer[1460];
uint32_t ip = WiFi.localIP();
uint32_t chipId = ESP.getChipId();
char uuid[64];
sprintf_P(uuid, PSTR("38323636-4558-4dda-9188-cda0e6%02x%02x%02x"),
(uint16_t) ((chipId >> 16) & 0xff),
(uint16_t) ((chipId >> 8) & 0xff),
(uint16_t) chipId & 0xff );
// FIXME TD-er: Why create String objects of these flashstrings?
String _ssdp_response_template = F(
"HTTP/1.1 200 OK\r\n"
"EXT:\r\n"
@@ -457,17 +449,27 @@ void SSDP_send(byte method) {
"USN: uuid:%s\r\n" // _uuid
"LOCATION: http://%u.%u.%u.%u:80/ssdp.xml\r\n" // WiFi.localIP(),
"\r\n");
{
char uuid[64];
uint32_t chipId = ESP.getChipId();
sprintf_P(uuid, PSTR("38323636-4558-4dda-9188-cda0e6%02x%02x%02x"),
(uint16_t) ((chipId >> 16) & 0xff),
(uint16_t) ((chipId >> 8) & 0xff),
(uint16_t) chipId & 0xff );
int len = snprintf(buffer, sizeof(buffer),
_ssdp_packet_template.c_str(),
(method == 0) ? _ssdp_response_template.c_str() : _ssdp_notify_template.c_str(),
SSDP_INTERVAL,
Settings.Build,
uuid,
IPADDR2STR(&ip)
);
char *buffer = new char[1460]();
int len = snprintf(buffer, 1460,
_ssdp_packet_template.c_str(),
(method == 0) ? _ssdp_response_template.c_str() : _ssdp_notify_template.c_str(),
SSDP_INTERVAL,
Settings.Build,
uuid,
IPADDR2STR(&ip)
);
_server->append(buffer, len);
_server->append(buffer, len);
delete[] buffer;
}
ip_addr_t remoteAddr;
uint16_t remotePort;
+1 -1
View File
@@ -2,7 +2,7 @@
Convert a char string to integer
\*********************************************************************************************/
//FIXME: change original code so it uses String and String.toInt()
unsigned long str2int(char *string)
unsigned long str2int(const char *string)
{
unsigned long temp = atof(string);
return temp;
+46 -40
View File
@@ -885,15 +885,16 @@ void handle_root() {
TXBuffer += F("<font color='red'>NTP disabled</font>");
addRowLabel(F("Uptime"));
char strUpTime[40];
int minutes = wdcounter / 2;
int days = minutes / 1440;
minutes = minutes % 1440;
int hrs = minutes / 60;
minutes = minutes % 60;
sprintf_P(strUpTime, PSTR("%d days %d hours %d minutes"), days, hrs, minutes);
TXBuffer += strUpTime;
{
int minutes = wdcounter / 2;
int days = minutes / 1440;
minutes = minutes % 1440;
int hrs = minutes / 60;
minutes = minutes % 60;
char strUpTime[40];
sprintf_P(strUpTime, PSTR("%d days %d hours %d minutes"), days, hrs, minutes);
TXBuffer += strUpTime;
}
addRowLabel(F("Load"));
if (wdcounter > 0)
{
@@ -996,9 +997,11 @@ void handle_root() {
}
html_TD();
html_add_wide_button_prefix();
char url[80];
sprintf_P(url, PSTR("http://%u.%u.%u.%u'>%u.%u.%u.%u</a>"), it->second.ip[0], it->second.ip[1], it->second.ip[2], it->second.ip[3], it->second.ip[0], it->second.ip[1], it->second.ip[2], it->second.ip[3]);
TXBuffer += url;
{
char url[80];
sprintf_P(url, PSTR("http://%u.%u.%u.%u'>%u.%u.%u.%u</a>"), it->second.ip[0], it->second.ip[1], it->second.ip[2], it->second.ip[3], it->second.ip[0], it->second.ip[1], it->second.ip[2], it->second.ip[3]);
TXBuffer += url;
}
html_TD();
TXBuffer += String( it->second.age);
}
@@ -4110,11 +4113,6 @@ void handle_login() {
sendHeadandTail_stdtemplate(_HEAD);
String webrequest = WebServer.arg(F("password"));
char command[80];
command[0] = 0;
webrequest.toCharArray(command, 80);
TXBuffer += F("<form method='post'>");
html_table_class_normal();
TXBuffer += F("<TR><TD>Password<TD>");
@@ -4130,6 +4128,10 @@ void handle_login() {
if (webrequest.length() != 0)
{
char command[80];
command[0] = 0;
webrequest.toCharArray(command, 80);
// compare with stored password and set timer if there's a match
if ((strcasecmp(command, SecuritySettings.Password) == 0) || (SecuritySettings.Password[0] == 0))
{
@@ -5134,10 +5136,12 @@ boolean handle_custom(String path) {
if (it != Nodes.end()) {
TXBuffer.startStream();
sendHeadandTail(F("TmplDsh"),_HEAD);
char url[40];
sprintf_P(url, PSTR("http://%u.%u.%u.%u/dashboard.esp"), it->second.ip[0], it->second.ip[1], it->second.ip[2], it->second.ip[3]);
TXBuffer += F("<meta http-equiv=\"refresh\" content=\"0; URL=");
TXBuffer += url;
{
char url[40];
sprintf_P(url, PSTR("http://%u.%u.%u.%u/dashboard.esp"), it->second.ip[0], it->second.ip[1], it->second.ip[2], it->second.ip[3]);
TXBuffer += url;
}
TXBuffer += "\">";
sendHeadandTail(F("TmplDsh"),_TAIL);
TXBuffer.endStream();
@@ -5461,7 +5465,6 @@ void handle_SDfilelist() {
String change_to_dir = "";
String current_dir = "";
String parent_dir = "";
char SDcardDir[80];
for (uint8_t i = 0; i < WebServer.args(); i++) {
if (WebServer.argName(i) == F("delete"))
@@ -5495,8 +5498,7 @@ void handle_SDfilelist() {
current_dir = "/";
}
current_dir.toCharArray(SDcardDir, current_dir.length()+1);
File root = SD.open(SDcardDir);
File root = SD.open(current_dir.c_str());
root.rewindDirectory();
File entry = root.openNextFile();
parent_dir = current_dir;
@@ -6083,14 +6085,16 @@ void handle_sysinfo() {
}
addRowLabel(F("Uptime"));
char strUpTime[40];
int minutes = wdcounter / 2;
int days = minutes / 1440;
minutes = minutes % 1440;
int hrs = minutes / 60;
minutes = minutes % 60;
sprintf_P(strUpTime, PSTR("%d days %d hours %d minutes"), days, hrs, minutes);
TXBuffer += strUpTime;
{
char strUpTime[40];
int minutes = wdcounter / 2;
int days = minutes / 1440;
minutes = minutes % 1440;
int hrs = minutes / 60;
minutes = minutes % 60;
sprintf_P(strUpTime, PSTR("%d days %d hours %d minutes"), days, hrs, minutes);
TXBuffer += strUpTime;
}
addRowLabel(F("Load"));
if (wdcounter > 0)
@@ -6178,16 +6182,18 @@ void handle_sysinfo() {
addRowLabel(F("STA MAC"));
uint8_t mac[] = {0, 0, 0, 0, 0, 0};
uint8_t* macread = WiFi.macAddress(mac);
char macaddress[20];
formatMAC(macread, macaddress);
TXBuffer += macaddress;
{
uint8_t mac[] = {0, 0, 0, 0, 0, 0};
uint8_t* macread = WiFi.macAddress(mac);
char macaddress[20];
formatMAC(macread, macaddress);
TXBuffer += macaddress;
addRowLabel(F("AP MAC"));
macread = WiFi.softAPmacAddress(mac);
formatMAC(macread, macaddress);
TXBuffer += macaddress;
addRowLabel(F("AP MAC"));
macread = WiFi.softAPmacAddress(mac);
formatMAC(macread, macaddress);
TXBuffer += macaddress;
}
addRowLabel(F("SSID"));
TXBuffer += WiFi.SSID();
+1 -5
View File
@@ -58,11 +58,7 @@ boolean CPlugin_010(byte function, struct EventStruct *event, String& string)
parseControllerVariables(element.txt[x], event, false);
element.txt[x].replace(F("%valname%"), ExtraTaskSettings.TaskDeviceValueNames[x]);
element.txt[x].replace(F("%value%"), formattedValue);
if (loglevelActiveFor(LOG_LEVEL_DEBUG_MORE)) {
char log[80];
element.txt[x].toCharArray(log, 80);
addLog(LOG_LEVEL_DEBUG_MORE, log);
}
addLog(LOG_LEVEL_DEBUG_MORE, element.txt[x]);
}
}
success = C010_DelayHandler.addToQueue(element);
+1 -3
View File
@@ -132,9 +132,7 @@ boolean Plugin_001(byte function, struct EventStruct *event, String& string)
if (switchtype == PLUGIN_001_TYPE_DIMMER)
{
char tmpString[128];
sprintf_P(tmpString, PSTR("<TR><TD>Dim value:<TD><input type='text' name='plugin_001_dimvalue' value='%u'>"), Settings.TaskDevicePluginConfig[event->TaskIndex][1]);
addHtml(tmpString);
addFormNumericBox(F("Dim value"), F("p001_dimvalue"), Settings.TaskDevicePluginConfig[event->TaskIndex][1], 0, 255);
}
byte choice = Settings.TaskDevicePluginConfig[event->TaskIndex][2];