From ef6a611045cb588f0be9a07a4bf2491798a874de Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sun, 19 Jun 2016 23:05:37 -0700 Subject: [PATCH] Reduce memory consumption; fix syslog alloc when disabled --- cmd/handlers.c | 10 ++++++++++ esp-link/cgimqtt.c | 13 ++++++++----- esp-link/main.c | 19 +++++++++++++++++-- esp-link/task.h | 2 +- include/esp8266.h | 5 ++++- include/espmissingincludes.h | 6 +++--- mqtt/mqtt_cmd.c | 8 ++++++++ syslog/syslog.c | 9 +++++---- 8 files changed, 56 insertions(+), 16 deletions(-) diff --git a/cmd/handlers.c b/cmd/handlers.c index 061526a..9e719a7 100644 --- a/cmd/handlers.c +++ b/cmd/handlers.c @@ -3,6 +3,7 @@ // Adapted from: github.com/tuanpmt/esp_bridge, Created on: Jan 9, 2015, Author: Minh #include "esp8266.h" +#include "sntp.h" #include "cmd.h" #include #ifdef MQTT @@ -21,6 +22,7 @@ static void cmdNull(CmdPacket *cmd); static void cmdSync(CmdPacket *cmd); static void cmdWifiStatus(CmdPacket *cmd); +static void cmdGetTime(CmdPacket *cmd); static void cmdAddCallback(CmdPacket *cmd); // keep track of last status sent to uC so we can notify it when it changes @@ -33,6 +35,7 @@ const CmdList commands[] = { {CMD_SYNC, "SYNC", cmdSync}, // synchronize {CMD_WIFI_STATUS, "WIFI_STATUS", cmdWifiStatus}, {CMD_CB_ADD, "ADD_CB", cmdAddCallback}, + {CMD_GET_TIME, "GET_TIME", cmdGetTime}, #ifdef MQTT {CMD_MQTT_SETUP, "MQTT_SETUP", MQTTCMD_Setup}, {CMD_MQTT_PUBLISH, "MQTT_PUB", MQTTCMD_Publish}, @@ -149,6 +152,13 @@ cmdWifiStatus(CmdPacket *cmd) { return; } +// Command handler for time +static void ICACHE_FLASH_ATTR +cmdGetTime(CmdPacket *cmd) { + cmdResponseStart(CMD_RESP_V, sntp_get_current_timestamp(), 0); + cmdResponseEnd(); + return; +} // Command handler to add a callback to the named-callbacks list, this is for a callback to the uC static void ICACHE_FLASH_ATTR diff --git a/esp-link/cgimqtt.c b/esp-link/cgimqtt.c index 88aa6ed..1d58e0d 100644 --- a/esp-link/cgimqtt.c +++ b/esp-link/cgimqtt.c @@ -134,13 +134,16 @@ int ICACHE_FLASH_ATTR cgiMqttSet(HttpdConnData *connData) { mqtt_client_init(); // if just enable changed we just need to bounce the client - } - else if (mqtt_en_chg > 0) { + } else if (mqtt_en_chg > 0) { DBG("MQTT server enable=%d changed\n", flashConfig.mqtt_enable); - if (flashConfig.mqtt_enable && strlen(flashConfig.mqtt_host) > 0) + if (flashConfig.mqtt_enable && strlen(flashConfig.mqtt_host) > 0) { + MQTT_Free(&mqttClient); // safe even if not connected + mqtt_client_init(); MQTT_Reconnect(&mqttClient); - else + } else { MQTT_Disconnect(&mqttClient); + MQTT_Free(&mqttClient); // safe even if not connected + } } // no action required if mqtt status settings change, they just get picked up at the @@ -154,7 +157,7 @@ int ICACHE_FLASH_ATTR cgiMqttSet(HttpdConnData *connData) { // if SLIP-enable is toggled it gets picked-up immediately by the parser int slip_update = getBoolArg(connData, "slip-enable", &flashConfig.slip_enable); if (slip_update < 0) return HTTPD_CGI_DONE; - if (slip_update > 0) + if (slip_update > 0) DBG("SLIP-enable changed: %d\n", flashConfig.slip_enable); DBG("Saving config\n"); diff --git a/esp-link/main.c b/esp-link/main.c index 5994e65..cf0b995 100644 --- a/esp-link/main.c +++ b/esp-link/main.c @@ -43,6 +43,14 @@ } while ( 0 ) #endif +#ifdef MEMLEAK_DEBUG +#include "mem.h" +bool ICACHE_FLASH_ATTR check_memleak_debug_enable(void) +{ + return MEMLEAK_DEBUG_ENABLE; +} +#endif + /* This is the main url->function dispatching data struct. In short, it's a struct with various URLs plus their handlers. The handlers can @@ -133,6 +141,7 @@ void user_init(void) { os_delay_us(10000L); os_printf("\n\n** %s\n", esp_link_version); os_printf("Flash config restore %s\n", restoreOk ? "ok" : "*FAILED*"); + prHeapTimerCb(0); // Status LEDs statusInit(); serledInit(); @@ -166,10 +175,16 @@ void user_init(void) { // Init SNTP service cgiServicesSNTPInit(); #ifdef MQTT - NOTICE("initializing MQTT"); - mqtt_client_init(); + if (flashConfig.mqtt_enable) { + NOTICE("initializing MQTT"); + mqtt_client_init(); + } #endif NOTICE("initializing user application"); app_init(); NOTICE("Waiting for work to do..."); +#ifdef MEMLEAK_DEBUG + system_show_malloc(); +#endif + prHeapTimerCb(0); } diff --git a/esp-link/task.h b/esp-link/task.h index 2dfd5d8..15679fc 100644 --- a/esp-link/task.h +++ b/esp-link/task.h @@ -12,7 +12,7 @@ #define USRTASK_H #define _taskPrio 1 -#define _task_queueLen 64 +#define _task_queueLen 8 uint8_t register_usr_task (os_task_t event); bool post_usr_task(uint8_t task, os_param_t par); diff --git a/include/esp8266.h b/include/esp8266.h index 3e7eeb9..72b3b4d 100644 --- a/include/esp8266.h +++ b/include/esp8266.h @@ -2,6 +2,9 @@ #ifndef _ESP8266_H_ #define _ESP8266_H_ +#undef MEMLEAK_DEBUG +#define USE_OPTIMIZE_PRINTF + #include #include #include @@ -24,4 +27,4 @@ #include <_mingw.h> #endif -#endif // _ESP8266_H_ \ No newline at end of file +#endif // _ESP8266_H_ diff --git a/include/espmissingincludes.h b/include/espmissingincludes.h index 2c35965..b961ce0 100644 --- a/include/espmissingincludes.h +++ b/include/espmissingincludes.h @@ -62,9 +62,9 @@ int os_printf_plus(const char *format, ...) __attribute__((format(printf, 1, 2) // memory allocation functions are "different" due to memory debugging functionality // added in SDK 1.4.0 -void vPortFree(void *ptr, char * file, int line); -void *pvPortMalloc(size_t xWantedSize, char * file, int line); -void *pvPortZalloc(size_t, char * file, int line); +void vPortFree(void *ptr, const char * file, int line); +void *pvPortMalloc(size_t xWantedSize, const char * file, int line); +void *pvPortZalloc(size_t, const char * file, int line); void *vPortMalloc(size_t xWantedSize); void pvPortFree(void *ptr); diff --git a/mqtt/mqtt_cmd.c b/mqtt/mqtt_cmd.c index 71c5d20..37fb4a4 100644 --- a/mqtt/mqtt_cmd.c +++ b/mqtt/mqtt_cmd.c @@ -185,6 +185,10 @@ MQTTCMD_Setup(CmdPacket *cmd) { if (cmdGetArgc(&req) != 4) return; #if 0 +// This section is commented-out because we're using the same client as esp-link is using itself, +// i.e. the one set-up in the Web UI. This code was here when we used a separate client for the +// attached uC, which just makes life more complicated... + if (cmdGetArgc(&req) != 9) return 0; @@ -261,6 +265,10 @@ MQTTCMD_Setup(CmdPacket *cmd) { } #if 0 +// This section is commented-out because we're using the same client as esp-link is using itself, +// i.e. the one set-up in the Web UI. This code was here when we used a separate client for the +// attached uC, which just makes life more complicated... + uint32_t ICACHE_FLASH_ATTR MQTTCMD_Connect(CmdPacket *cmd) { CmdRequest req; diff --git a/syslog/syslog.c b/syslog/syslog.c index 27d8c58..f359b85 100644 --- a/syslog/syslog.c +++ b/syslog/syslog.c @@ -46,7 +46,6 @@ static void ICACHE_FLASH_ATTR syslog_udp_recv_cb(void *arg, char *pusrdata, unsi #define syslog_send_udp() post_usr_task(syslog_task,0) -#ifdef SYSLOG_DBG static char ICACHE_FLASH_ATTR *syslog_get_status(void) { switch (syslogState) { @@ -77,11 +76,13 @@ static char ICACHE_FLASH_ATTR *syslog_get_status(void) { } return "UNKNOWN "; } -#endif static void ICACHE_FLASH_ATTR syslog_set_status(enum syslog_state state) { syslogState = state; DBG("[%dµs] %s: %s (%d)\n", WDEV_NOW(), __FUNCTION__, syslog_get_status(), state); +#ifndef SYSLOG_DBG + os_printf("Syslog state: %s\n", syslog_get_status()); +#endif } static void ICACHE_FLASH_ATTR syslog_timer_arm(int delay) { @@ -261,6 +262,7 @@ void ICACHE_FLASH_ATTR syslog_init(char *syslog_host) { DBG("[%uµs] %s\n", WDEV_NOW(), __FUNCTION__); + os_printf("SYSLOG host=%s *host=0x%x\n", syslog_host, *syslog_host); if (!*syslog_host) { syslog_set_status(SYSLOG_HALTED); return; @@ -504,8 +506,7 @@ void ICACHE_FLASH_ATTR syslog(uint8_t facility, uint8_t severity, const char *ta { DBG("[%dµs] %s status: %s\n", WDEV_NOW(), __FUNCTION__, syslog_get_status()); - if (syslogState == SYSLOG_ERROR || - syslogState == SYSLOG_HALTED) + if (flashConfig.syslog_host[0] == 0 || syslogState == SYSLOG_ERROR || syslogState == SYSLOG_HALTED) return; if (severity > flashConfig.syslog_filter)