Skip to content

Commit

Permalink
reserve heap
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Ludewig committed Apr 22, 2021
1 parent cdc260f commit 61c8ca8
Show file tree
Hide file tree
Showing 16 changed files with 213 additions and 535 deletions.
138 changes: 81 additions & 57 deletions lib/App/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,30 @@ AppConfig appcfg;
AppConfig appcfgWR;
AppConfig appcfgRD;

char *formatChipId( char *attribute )
char *buffer;
char *buffer2;

void appMemoryInit()
{
if (buffer == NULL)
buffer = (char *)malloc(BUFFER_LENGTH + 1);
if (buffer2 == NULL)
buffer2 = (char *)malloc(BUFFER2_LENGTH + 1);
}

void appMemoryFree()
{
snprintf( buffer, 63, attribute, ESP.getChipId());
buffer[63]= 0;
strncpy(attribute,buffer,63);
free(buffer);
free(buffer2);
buffer = buffer2 = NULL;
delay(250);
}

char *formatChipId(char *attribute)
{
snprintf(buffer, (ATTRIBUTE_SIZE-1), attribute, ESP.getChipId());
buffer[(ATTRIBUTE_SIZE-1)] = 0;
strncpy(attribute, buffer, (ATTRIBUTE_SIZE-1));
return attribute;
}

Expand All @@ -38,18 +57,20 @@ const char *appUptime()

void appShowHeader(Stream &out)
{
out.println("\n\n" APP_NAME " - " APP_VERSION " - " APP_AUTHOR);
out.println("BUILD: " __DATE__ " " __TIME__);
out.println("PIOENV: " PIOENV);
out.println("PIOPLATFORM: " PIOPLATFORM);
out.println("PIOFRAMEWORK: " PIOFRAMEWORK);
out.printf("ESP SDK Version: %s\n", ESP.getSdkVersion());
out.printf("ESP Core Version: %s\n\n", ESP.getCoreVersion().c_str());
out.println(PSTR("\n\n" APP_NAME " - " APP_VERSION " - " APP_AUTHOR));
out.println(PSTR("BUILD: " __DATE__ " " __TIME__));
out.println(PSTR("PIOENV: " PIOENV));
out.println(PSTR("PIOPLATFORM: " PIOPLATFORM));
out.println(PSTR("PIOFRAMEWORK: " PIOFRAMEWORK));
out.printf_P(PSTR("ESP SDK Version: %s\n"), ESP.getSdkVersion());
out.printf_P(PSTR("ESP Core Version: %s\n\n"), ESP.getCoreVersion().c_str());
}

App::App()
{
sprintf(initFilename, "/%08X.ini", ESP.getChipId());
buffer = buffer2 = NULL;
appMemoryInit();
sprintf_P(initFilename, PSTR("/%08X.ini"), ESP.getChipId());
defaultConfig();
initSPIFFS = false;
initialized = true;
Expand All @@ -60,58 +81,60 @@ App::App()

void App::defaultConfig()
{
strncpy(appcfg.wifi_ssid, DEFAULT_WIFI_SSID, 63);
strncpy(appcfg.wifi_password, DEFAULT_WIFI_PASSWORD, 63);
strncpy(appcfg.wifi_ssid, DEFAULT_WIFI_SSID, (ATTRIBUTE_SIZE-1));
strncpy(appcfg.wifi_password, DEFAULT_WIFI_PASSWORD, (ATTRIBUTE_SIZE-1));
appcfg.wifi_mode = DEFAULT_WIFI_MODE;

appcfg.net_mode = DEFAULT_NET_MODE;
strncpy(appcfg.net_host, DEFAULT_NET_HOST, 63);
strncpy(appcfg.net_mask, DEFAULT_NET_MASK, 63);
strncpy(appcfg.net_gateway, DEFAULT_NET_GATEWAY, 63);
strncpy(appcfg.net_dns, DEFAULT_NET_DNS, 63);
strncpy(appcfg.net_host, DEFAULT_NET_HOST, (ATTRIBUTE_SIZE-1));
strncpy(appcfg.net_mask, DEFAULT_NET_MASK, (ATTRIBUTE_SIZE-1));
strncpy(appcfg.net_gateway, DEFAULT_NET_GATEWAY, (ATTRIBUTE_SIZE-1));
strncpy(appcfg.net_dns, DEFAULT_NET_DNS, (ATTRIBUTE_SIZE-1));

strncpy(appcfg.ota_hostname, DEFAULT_OTA_HOSTNAME, 63);
strncpy(appcfg.ota_password, DEFAULT_OTA_PASSWORD, 63);
strncpy(appcfg.ota_hostname, DEFAULT_OTA_HOSTNAME, (ATTRIBUTE_SIZE-1));
strncpy(appcfg.ota_password, DEFAULT_OTA_PASSWORD, (ATTRIBUTE_SIZE-1));

strncpy(appcfg.admin_password, DEFAULT_ADMIN_PASSWORD, 63);
strncpy(appcfg.admin_password, DEFAULT_ADMIN_PASSWORD, (ATTRIBUTE_SIZE-1));

appcfg.ohab_enabled = DEFAULT_OHAB_ENABLED;
appcfg.ohab_version = DEFAULT_OHAB_VERSION;
strncpy(appcfg.ohab_host, DEFAULT_OHAB_HOST, 63);
strncpy(appcfg.ohab_host, DEFAULT_OHAB_HOST, (ATTRIBUTE_SIZE-1));
appcfg.ohab_port = DEFAULT_OHAB_PORT;
appcfg.ohab_useauth = DEFAULT_OHAB_USEAUTH;
strncpy(appcfg.ohab_user, DEFAULT_OHAB_USER, 63);
strncpy(appcfg.ohab_password, DEFAULT_OHAB_PASSWORD, 63);
strncpy(appcfg.ohab_itemname, DEFAULT_OHAB_ITEMNAME, 63);
strncpy(appcfg.ohab_user, DEFAULT_OHAB_USER, (ATTRIBUTE_SIZE-1));
strncpy(appcfg.ohab_password, DEFAULT_OHAB_PASSWORD, (ATTRIBUTE_SIZE-1));
strncpy(appcfg.ohab_itemname, DEFAULT_OHAB_ITEMNAME, (ATTRIBUTE_SIZE-1));

appcfg.alexa_enabled = DEFAULT_ALEXA_ENABLED;
strncpy(appcfg.alexa_devicename, DEFAULT_ALEXA_DEVICENAME, 63);
strncpy(appcfg.alexa_devicename, DEFAULT_ALEXA_DEVICENAME, (ATTRIBUTE_SIZE-1));

appcfg.mqtt_enabled = DEFAULT_MQTT_ENABLED;
strncpy(appcfg.mqtt_clientid, DEFAULT_MQTT_CLIENTID, 63);
strncpy(appcfg.mqtt_host, DEFAULT_MQTT_HOST, 63);
strncpy(appcfg.mqtt_clientid, DEFAULT_MQTT_CLIENTID, (ATTRIBUTE_SIZE-1));
strncpy(appcfg.mqtt_host, DEFAULT_MQTT_HOST, (ATTRIBUTE_SIZE-1));
appcfg.mqtt_port = DEFAULT_MQTT_PORT;
appcfg.mqtt_useauth = DEFAULT_MQTT_USEAUTH;
strncpy(appcfg.mqtt_user, DEFAULT_MQTT_USER, 63);
strncpy(appcfg.mqtt_password, DEFAULT_MQTT_PASSWORD, 63);
strncpy(appcfg.mqtt_intopic, DEFAULT_MQTT_INTOPIC, 63);
strncpy(appcfg.mqtt_outtopic, DEFAULT_MQTT_OUTTOPIC, 63);
strncpy(appcfg.mqtt_user, DEFAULT_MQTT_USER, (ATTRIBUTE_SIZE-1));
strncpy(appcfg.mqtt_password, DEFAULT_MQTT_PASSWORD, (ATTRIBUTE_SIZE-1));
strncpy(appcfg.mqtt_intopic, DEFAULT_MQTT_INTOPIC, (ATTRIBUTE_SIZE-1));
strncpy(appcfg.mqtt_outtopic, DEFAULT_MQTT_OUTTOPIC, (ATTRIBUTE_SIZE-1));

/*
appcfg.syslog_enabled = DEFAULT_SYSLOG_ENABLED;
strncpy(appcfg.syslog_host, DEFAULT_SYSLOG_HOST, 63);
strncpy(appcfg.syslog_host, DEFAULT_SYSLOG_HOST, (ATTRIBUTE_SIZE-1));
appcfg.syslog_port = DEFAULT_SYSLOG_PORT;
strncpy(appcfg.syslog_app_name, DEFAULT_SYSLOG_APP_NAME, 63);
strncpy(appcfg.syslog_app_name, DEFAULT_SYSLOG_APP_NAME, (ATTRIBUTE_SIZE-1));
*/

#ifdef HAVE_ENERGY_SENSOR
strncpy(appcfg.ohab_item_voltage, DEFAULT_OHAB_ITEM_VOLTAGE, 63);
strncpy(appcfg.ohab_item_current, DEFAULT_OHAB_ITEM_CURRENT, 63);
strncpy(appcfg.ohab_item_power, DEFAULT_OHAB_ITEM_POWER, 63);
strncpy(appcfg.ohab_item_voltage, DEFAULT_OHAB_ITEM_VOLTAGE, (ATTRIBUTE_SIZE-1));
strncpy(appcfg.ohab_item_current, DEFAULT_OHAB_ITEM_CURRENT, (ATTRIBUTE_SIZE-1));
strncpy(appcfg.ohab_item_power, DEFAULT_OHAB_ITEM_POWER, (ATTRIBUTE_SIZE-1));
appcfg.ohab_sending_interval = DEFAULT_OHAB_SENDING_INTERVAL;

strncpy(appcfg.mqtt_topic_voltage, DEFAULT_MQTT_TOPIC_VOLTAGE, 63);
strncpy(appcfg.mqtt_topic_current, DEFAULT_MQTT_TOPIC_CURRENT, 63);
strncpy(appcfg.mqtt_topic_power, DEFAULT_MQTT_TOPIC_POWER, 63);
strncpy(appcfg.mqtt_topic_json, DEFAULT_MQTT_TOPIC_JSON, 63);
strncpy(appcfg.mqtt_topic_voltage, DEFAULT_MQTT_TOPIC_VOLTAGE, (ATTRIBUTE_SIZE-1));
strncpy(appcfg.mqtt_topic_current, DEFAULT_MQTT_TOPIC_CURRENT, (ATTRIBUTE_SIZE-1));
strncpy(appcfg.mqtt_topic_power, DEFAULT_MQTT_TOPIC_POWER, (ATTRIBUTE_SIZE-1));
strncpy(appcfg.mqtt_topic_json, DEFAULT_MQTT_TOPIC_JSON, (ATTRIBUTE_SIZE-1));
#endif
appcfg.mqtt_sending_interval = DEFAULT_MQTT_SENDING_INTERVAL;

Expand Down Expand Up @@ -201,7 +224,7 @@ void App::setup()

#if defined(BOARD_TYPE_OBI_V2) || defined(BOARD_TYPE_DEV1) || \
defined(BOARD_TYPE_BW_SHP6) || defined(BOARD_TYPE_BW_SHP6_V11) || \
defined(BOARD_TYPE_SHELLY1) || defined(BOARD_TYPE_GEBA_01SWP) || \
defined(BOARD_TYPE_SHELLY1) || defined(BOARD_TYPE_GEBA_01SWP) || \
defined(BOARD_TYPE_SONOFF_BASIC)

#ifdef POWER_LED
Expand All @@ -222,12 +245,8 @@ void App::setup()
Serial.println();
}

Serial.println("\n\n");
Serial.println("\n\n");
Serial.println(F(APP_NAME ", Version " APP_VERSION ", by " APP_AUTHOR));
Serial.println("Build date: " __DATE__ " " __TIME__);
Serial.printf("appcfg file size: %d bytes\n\n", sizeof(appcfg));

appShowHeader(Serial);
Serial.printf("appcfg size: %d bytes\n\n", sizeof(appcfg));
showChipInfo();

if (LittleFS.begin())
Expand Down Expand Up @@ -292,16 +311,16 @@ void App::setup()
///////////////////////////////////////////////////////////////
#ifdef OVERRIDE_WIFI_SETTINGS
appcfg.wifi_mode = OVERRIDE_WIFI_MODE;
strcpy( appcfg.wifi_ssid, OVERRIDE_WIFI_SSID );
strcpy( appcfg.wifi_password, OVERRIDE_WIFI_PASSWORD );
strcpy(appcfg.wifi_ssid, OVERRIDE_WIFI_SSID);
strcpy(appcfg.wifi_password, OVERRIDE_WIFI_PASSWORD);
appcfg.ota_enabled = OVERRIDE_OTA_ENABLED;
#endif
///////////////////////////////////////////////////////////////
formatChipId( appcfg.ota_hostname );
formatChipId( appcfg.mqtt_clientid );
formatChipId( appcfg.mqtt_intopic );
formatChipId( appcfg.mqtt_outtopic );
///////////////////////////////////////////////////////////////

formatChipId(appcfg.ota_hostname);
formatChipId(appcfg.mqtt_clientid);
formatChipId(appcfg.mqtt_intopic);
formatChipId(appcfg.mqtt_outtopic);

memcpy(&appcfgWR, &appcfg, sizeof(appcfg));
}
Expand Down Expand Up @@ -398,11 +417,12 @@ void App::writeConfig()
#endif
j.writeEntry(A_mqtt_sending_interval, appcfgWR.mqtt_sending_interval);

/*
j.writeEntry(A_syslog_enabled, appcfgWR.syslog_enabled);
j.writeEntry(A_syslog_host, appcfgWR.syslog_host);
j.writeEntry(A_syslog_port, appcfgWR.syslog_port);
j.writeEntry(A_syslog_app_name, appcfgWR.syslog_app_name);

*/
#ifdef POWER_BUTTON_IS_MULTIMODE
j.writeEntry(A_power_button_mode, appcfgWR.power_button_mode);
#endif
Expand Down Expand Up @@ -495,12 +515,14 @@ void App::printConfig(AppConfig ac)
#endif
Serial.printf(" Sending Interval: %ld\n", ac.mqtt_sending_interval);

/*
Serial.println("\n Syslog:");
Serial.printf(" Enabled: %s\n",
(ac.syslog_enabled ? "true" : "false"));
Serial.printf(" Host: %s\n", ac.syslog_host);
Serial.printf(" Port: %d\n", ac.syslog_port);
Serial.printf(" App Name: %s\n", ac.syslog_app_name);
*/
#ifdef POWER_BUTTON_IS_MULTIMODE
Serial.println("\n Power button:");
Serial.printf(" Mode: %d\n", ac.power_button_mode);
Expand Down Expand Up @@ -621,10 +643,12 @@ bool App::loadJsonConfig(const char *filename)
#endif
readError |= j.readEntryULong(attributeName, A_mqtt_sending_interval, &appcfgRD.mqtt_sending_interval);

/*
readError |= j.readEntryBoolean(attributeName, A_syslog_enabled, &appcfgRD.syslog_enabled);
readError |= j.readEntryChars(attributeName, A_syslog_host, appcfgRD.syslog_host);
readError |= j.readEntryInteger(attributeName, A_syslog_port, &appcfgRD.syslog_port);
readError |= j.readEntryChars(attributeName, A_syslog_app_name, appcfgRD.syslog_app_name);
*/

#ifdef POWER_BUTTON_IS_MULTIMODE
readError |= j.readEntryInteger(attributeName, A_power_button_mode, &appcfgRD.power_button_mode);
Expand Down
70 changes: 41 additions & 29 deletions lib/App/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,62 +32,74 @@

extern const char *appUptime();

#define BUFFER_LENGTH 4096
#define BUFFER2_LENGTH 256

extern char *buffer;
extern char *buffer2;
extern void appMemoryInit();
extern void appMemoryFree();

#define ATTRIBUTE_SIZE 40

typedef struct appconfig
{
char wifi_ssid[64];
char wifi_password[64];
char wifi_ssid[ATTRIBUTE_SIZE];
char wifi_password[ATTRIBUTE_SIZE];
int wifi_mode;

int net_mode;
char net_host[64];
char net_mask[64];
char net_gateway[64];
char net_dns[64];
char net_host[ATTRIBUTE_SIZE];
char net_mask[ATTRIBUTE_SIZE];
char net_gateway[ATTRIBUTE_SIZE];
char net_dns[ATTRIBUTE_SIZE];

bool ota_enabled;
char ota_hostname[64];
char ota_password[64];
char ota_hostname[ATTRIBUTE_SIZE];
char ota_password[ATTRIBUTE_SIZE];

char admin_password[64];
char admin_password[ATTRIBUTE_SIZE];

bool ohab_enabled;
int ohab_version;
char ohab_host[64];
char ohab_host[ATTRIBUTE_SIZE];
int ohab_port;
bool ohab_useauth;
char ohab_user[64];
char ohab_password[64];
char ohab_itemname[64];
char ohab_user[ATTRIBUTE_SIZE];
char ohab_password[ATTRIBUTE_SIZE];
char ohab_itemname[ATTRIBUTE_SIZE];
#ifdef HAVE_ENERGY_SENSOR
char ohab_item_voltage[64];
char ohab_item_current[64];
char ohab_item_power[64];
char ohab_item_voltage[ATTRIBUTE_SIZE];
char ohab_item_current[ATTRIBUTE_SIZE];
char ohab_item_power[ATTRIBUTE_SIZE];
unsigned long ohab_sending_interval;
#endif
bool alexa_enabled;
char alexa_devicename[64];
char alexa_devicename[ATTRIBUTE_SIZE];

bool mqtt_enabled;
char mqtt_clientid[64];
char mqtt_host[64];
char mqtt_clientid[ATTRIBUTE_SIZE];
char mqtt_host[ATTRIBUTE_SIZE];
int mqtt_port;
bool mqtt_useauth;
char mqtt_user[64];
char mqtt_password[64];
char mqtt_intopic[64];
char mqtt_outtopic[64];
char mqtt_user[ATTRIBUTE_SIZE];
char mqtt_password[ATTRIBUTE_SIZE];
char mqtt_intopic[ATTRIBUTE_SIZE];
char mqtt_outtopic[ATTRIBUTE_SIZE];
#ifdef HAVE_ENERGY_SENSOR
char mqtt_topic_voltage[64];
char mqtt_topic_current[64];
char mqtt_topic_power[64];
char mqtt_topic_json[64];
char mqtt_topic_voltage[ATTRIBUTE_SIZE];
char mqtt_topic_current[ATTRIBUTE_SIZE];
char mqtt_topic_power[ATTRIBUTE_SIZE];
char mqtt_topic_json[ATTRIBUTE_SIZE];
#endif
unsigned long mqtt_sending_interval;

/*
bool syslog_enabled;
char syslog_host[64];
char syslog_host[ATTRIBUTE_SIZE];
int syslog_port;
char syslog_app_name[64];
char syslog_app_name[ATTRIBUTE_SIZE];
*/

#ifdef POWER_BUTTON_IS_MULTIMODE
int power_button_mode;
Expand Down
5 changes: 3 additions & 2 deletions lib/MicroJson/MicroJson.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <App.hpp>
#include <Arduino.h>
#include <FS.h>

Expand Down Expand Up @@ -178,11 +179,11 @@ bool uJson::readEntryChars(const char *n1, const char *n2, char *value)
{
int i = 0;
int r;
while ((r = file.read()) >= 0 && r != '"' && i < 64)
while ((r = file.read()) >= 0 && r != '"' && i < ATTRIBUTE_SIZE)
{
value[i++] = r;
}
if (r == '"' && i < 64)
if (r == '"' && i < ATTRIBUTE_SIZE)
{
value[i] = 0;
return false;
Expand Down
9 changes: 0 additions & 9 deletions lib/Util/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@

extern void sendPrint(const char *message);

char buffer[BUFFER_LENGTH+1];
char buffer2[BUFFER2_LENGTH+1];

#define MAX_MESSAGE_LENGTH 200
#define MESSAGE_BUFFER_LINES 11
char messageBuffer[(MAX_MESSAGE_LENGTH + 1) * MESSAGE_BUFFER_LINES];
int messageStartIndex = 0;
int messageEndIndex = 0;

/*
Find the description about the boot device code here:
https://www.sigmdel.ca/michel/program/esp8266/arduino/watchdogs2_en.html
Expand Down
Loading

0 comments on commit 61c8ca8

Please sign in to comment.