diff --git a/Makefile b/Makefile index 51ac65f..844e00f 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,9 @@ # `VERBOSE=1 make ...` will print debug info # `ESP_HOSTNAME=my.esp.example.com make wiflash` is an easy way to override a variable +# CFLAGS may be changed by local.conf +CFLAGS= + # optional local configuration file -include local.conf @@ -54,17 +57,25 @@ ESP_HOSTNAME ?= esp-link # Typically you'll install https://github.com/pfalcon/esp-open-sdk XTENSA_TOOLS_ROOT ?= $(abspath ../esp-open-sdk/xtensa-lx106-elf/bin)/ -# Base directory of the ESP8266 SDK package, absolute -ifneq ($(wildcard ../esp-open-sdk/esp_iot_sdk_v1.5.2/.*),) -# One that has been extracted as part of open-sdk -SDK_BASE ?= $(abspath ../esp-open-sdk/esp_iot_sdk_v1.5.2) -else -ifneq ($(wildcard ../esp_iot_sdk_v1.5.1/.*),) -# Manually downloaded from Espressif's BBS, http://bbs.espressif.com/viewforum.php?f=5 -SDK_BASE ?= $(abspath ../esp_iot_sdk_v1.5.1) -endif +# Firmware version (if you change this expect to make code adjustments elsewhere!) +SDK_VERS ?= esp_iot_sdk_v1.5.2 + +# Try to find the firmware manually extracted, e.g. after downloading from Espressif's BBS, +# http://bbs.espressif.com/viewforum.php?f=46 +SDK_BASE ?= $(wildcard ../$(SDK_VERS)) + +# If the firmware isn't there, see whether it got downloaded as part of esp-open-sdk +ifeq ($(SDK_BASE),) +SDK_BASE := $(wildcard $(XTENSA_TOOLS_ROOT)/../../$(SDK_VERS)) endif +# Clean up SDK path +SDK_BASE := $(abspath $(SDK_BASE)) +$(warning Using SDK from $(SDK_BASE)) + +# Path to bootloader file +BOOTFILE ?= $(SDK_BASE/bin/boot_v1.5.bin) + # Esptool.py path and port, only used for 1-time serial flashing # Typically you'll use https://github.com/themadinventor/esptool # Windows users use the com port i.e: ESPPORT ?= com3 @@ -195,8 +206,6 @@ TARGET = httpd # espressif tool to concatenate sections for OTA upload using bootloader v1.2+ APPGEN_TOOL ?= gen_appbin.py -CFLAGS= - # set defines for optional modules ifneq (,$(findstring mqtt,$(MODULES))) CFLAGS += -DMQTT @@ -333,6 +342,7 @@ all: echo_version checkdirs $(FW_BASE)/user1.bin $(FW_BASE)/user2.bin echo_version: @echo VERSION: $(VERSION) + @echo MODULES: $(MODULES) $(USER1_OUT): $(APP_AR) $(LD_SCRIPT1) $(vecho) "LD $@" @@ -389,7 +399,7 @@ baseflash: all flash: all $(Q) $(ESPTOOL) --port $(ESPPORT) --baud $(ESPBAUD) write_flash -fs $(ET_FS) -ff $(ET_FF) \ - 0x00000 "$(SDK_BASE)/bin/boot_v1.5.bin" 0x01000 $(FW_BASE)/user1.bin \ + 0x00000 "$(BOOTFILE)" 0x01000 $(FW_BASE)/user1.bin \ $(ET_BLANK) $(SDK_BASE)/bin/blank.bin ifeq ($(OS),Windows_NT) @@ -436,7 +446,7 @@ ifeq ("$(COMPRESS_W_HTMLCOMPRESSOR)","yes") else $(Q) cp -r html/head- html_compressed; $(Q) cp -r html/*.html html_compressed; - $(Q) cp -r html/wifi/*.html html_compressed/wifi; + $(Q) cp -r html/wifi/*.html html_compressed/wifi; endif ifeq (,$(findstring mqtt,$(MODULES))) $(Q) rm -rf html_compressed/mqtt.html @@ -471,7 +481,7 @@ release: all $(Q) egrep -a 'esp-link [a-z0-9.]+ - 201' $(FW_BASE)/user1.bin | cut -b 1-80 $(Q) egrep -a 'esp-link [a-z0-9.]+ - 201' $(FW_BASE)/user2.bin | cut -b 1-80 $(Q) cp $(FW_BASE)/user1.bin $(FW_BASE)/user2.bin $(SDK_BASE)/bin/blank.bin \ - "$(SDK_BASE)/bin/boot_v1.5.bin" wiflash avrflash release/esp-link-$(BRANCH) + "$(BOOTFILE)" wiflash avrflash release/esp-link-$(BRANCH) $(Q) tar zcf esp-link-$(BRANCH).tgz -C release esp-link-$(BRANCH) $(Q) echo "Release file: esp-link-$(BRANCH).tgz" $(Q) rm -rf release diff --git a/cmd/handlers.c b/cmd/handlers.c index 061526a..b62894b 100644 --- a/cmd/handlers.c +++ b/cmd/handlers.c @@ -13,7 +13,7 @@ #endif #ifdef CMD_DBG -#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) #else #define DBG(format, ...) do { } while(0) #endif diff --git a/esp-link/cgi.c b/esp-link/cgi.c index 96c03e9..c42d769 100644 --- a/esp-link/cgi.c +++ b/esp-link/cgi.c @@ -18,7 +18,7 @@ Some random cgi routines. #include "config.h" #ifdef CGI_DBG -#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) #else #define DBG(format, ...) do { } while(0) #endif diff --git a/esp-link/cgiflash.c b/esp-link/cgiflash.c index 4b9277d..de49094 100644 --- a/esp-link/cgiflash.c +++ b/esp-link/cgiflash.c @@ -20,7 +20,7 @@ Some flash handling cgi routines. Used for reading the existing flash and updati #include "cgiflash.h" #ifdef CGIFLASH_DBG -#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) #else #define DBG(format, ...) do { } while(0) #endif diff --git a/esp-link/cgimqtt.c b/esp-link/cgimqtt.c index 88aa6ed..09d6ce0 100644 --- a/esp-link/cgimqtt.c +++ b/esp-link/cgimqtt.c @@ -17,7 +17,7 @@ char *mqttState(void) { #include "cgimqtt.h" #ifdef CGIMQTT_DBG -#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) #else #define DBG(format, ...) do { } while(0) #endif @@ -134,7 +134,7 @@ 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) { DBG("MQTT server enable=%d changed\n", flashConfig.mqtt_enable); if (flashConfig.mqtt_enable && strlen(flashConfig.mqtt_host) > 0) @@ -154,7 +154,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/cgioptiboot.c b/esp-link/cgioptiboot.c index ed87d0e..d1dca68 100644 --- a/esp-link/cgioptiboot.c +++ b/esp-link/cgioptiboot.c @@ -17,7 +17,7 @@ #define ATTEMPTS 8 // number of attempts total to make #ifdef OPTIBOOT_DBG -#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) #else #define DBG(format, ...) do { } while(0) #endif diff --git a/esp-link/cgiservices.c b/esp-link/cgiservices.c index cf55dd1..d9b3508 100644 --- a/esp-link/cgiservices.c +++ b/esp-link/cgiservices.c @@ -7,7 +7,7 @@ #include "cgimqtt.h" #ifdef CGISERVICES_DBG -#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) #else #define DBG(format, ...) do { } while(0) #endif diff --git a/esp-link/cgiwifi.c b/esp-link/cgiwifi.c index 50ea536..0971dbc 100755 --- a/esp-link/cgiwifi.c +++ b/esp-link/cgiwifi.c @@ -19,13 +19,34 @@ Cgi/template routines for the /wifi url. #include "status.h" #include "config.h" #include "log.h" +#include "syslog.h" #ifdef CGIWIFI_DBG -#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) #else #define DBG(format, ...) do { } while(0) #endif +#define LOG(severity, format, ...) do { \ + char buffer[128]; \ + os_sprintf(buffer, format, ## __VA_ARGS__); \ + syslog(SYSLOG_FAC_USER, severity, "WIFI", buffer); \ + DBG("%s %s\n", "WIFI", buffer); \ +} while(0) + +#define NOTICE(format, ...) do { \ + LOG(SYSLOG_PRIO_NOTICE, format, ## __VA_ARGS__); \ +} while(0) + +#define INFO(format, ...) do { \ + LOG(SYSLOG_PRIO_INFO, format, ## __VA_ARGS__); \ +} while(0) + +#define WARNING(format, ...) do { \ + LOG(SYSLOG_PRIO_WARNING, format, ## __VA_ARGS__); \ +} while(0) + + # define VERS_STR_STR(V) #V # define VERS_STR(V) VERS_STR_STR(V) @@ -34,7 +55,7 @@ bool mdns_started = false; // ===== wifi status change callbacks static WifiStateChangeCb wifi_state_change_cb[4]; -// Temp store for new staion config +// Temp store for new station config struct station_config stconf; // Temp store for new ap config @@ -68,25 +89,25 @@ static void ICACHE_FLASH_ATTR wifiHandleEventCb(System_Event_t *evt) { case EVENT_STAMODE_CONNECTED: wifiState = wifiIsConnected; wifiReason = 0; - DBG("Wifi connected to ssid %s, ch %d\n", evt->event_info.connected.ssid, + NOTICE("connected to ssid %s, ch %d", evt->event_info.connected.ssid, evt->event_info.connected.channel); statusWifiUpdate(wifiState); break; case EVENT_STAMODE_DISCONNECTED: wifiState = wifiIsDisconnected; wifiReason = evt->event_info.disconnected.reason; - DBG("Wifi disconnected from ssid %s, reason %s (%d)\n", + WARNING("disconnected from ssid %s, reason %s (%d)", evt->event_info.disconnected.ssid, wifiGetReason(), evt->event_info.disconnected.reason); statusWifiUpdate(wifiState); break; case EVENT_STAMODE_AUTHMODE_CHANGE: - DBG("Wifi auth mode: %d -> %d\n", + NOTICE("auth mode: %d -> %d", evt->event_info.auth_change.old_mode, evt->event_info.auth_change.new_mode); break; case EVENT_STAMODE_GOT_IP: wifiState = wifiGotIP; wifiReason = 0; - DBG("Wifi got ip:" IPSTR ",mask:" IPSTR ",gw:" IPSTR "\n", + NOTICE("got ip:" IPSTR ",mask:" IPSTR ",gw:" IPSTR, IP2STR(&evt->event_info.got_ip.ip), IP2STR(&evt->event_info.got_ip.mask), IP2STR(&evt->event_info.got_ip.gw)); statusWifiUpdate(wifiState); @@ -94,11 +115,11 @@ static void ICACHE_FLASH_ATTR wifiHandleEventCb(System_Event_t *evt) { wifiStartMDNS(evt->event_info.got_ip.ip); break; case EVENT_SOFTAPMODE_STACONNECTED: - DBG("Wifi AP: station " MACSTR " joined, AID = %d\n", + NOTICE("AP: station " MACSTR " joined, AID = %d", MAC2STR(evt->event_info.sta_connected.mac), evt->event_info.sta_connected.aid); break; case EVENT_SOFTAPMODE_STADISCONNECTED: - DBG("Wifi AP: station " MACSTR " left, AID = %d\n", + NOTICE("AP: station " MACSTR " left, AID = %d", MAC2STR(evt->event_info.sta_disconnected.mac), evt->event_info.sta_disconnected.aid); break; default: @@ -118,7 +139,7 @@ void ICACHE_FLASH_ATTR wifiAddStateChangeCb(WifiStateChangeCb cb) { return; } } - DBG("WIFI: max state change cb count exceeded\n"); + WARNING("max state change cb count exceeded"); } void ICACHE_FLASH_ATTR wifiStartMDNS(struct ip_addr ip) { @@ -163,7 +184,7 @@ void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) { struct bss_info *bss_link = (struct bss_info *)arg; if (status!=OK) { - DBG("wifiScanDoneCb status=%d\n", status); + WARNING("wifiScanDoneCb status=%d", status); cgiWifiAps.scanInProgress=0; return; } @@ -183,7 +204,7 @@ void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) { //Allocate memory for access point data cgiWifiAps.apData=(ApData **)os_malloc(sizeof(ApData *)*n); cgiWifiAps.noAps=n; - DBG("Scan done: found %d APs\n", n); + INFO("Scan done: found %d APs", n); //Copy access point data to the static struct n=0; @@ -192,7 +213,7 @@ void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) { if (n>=cgiWifiAps.noAps) { //This means the bss_link changed under our nose. Shouldn't happen! //Break because otherwise we will write in unallocated memory. - DBG("Huh? I have more than the allocated %d aps!\n", cgiWifiAps.noAps); + WARNING("Huh? I have more than the allocated %d aps!", cgiWifiAps.noAps); break; } //Save the ap data. @@ -200,7 +221,7 @@ void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) { cgiWifiAps.apData[n]->rssi=bss_link->rssi; cgiWifiAps.apData[n]->enc=bss_link->authmode; strncpy(cgiWifiAps.apData[n]->ssid, (char*)bss_link->ssid, 32); - DBG("bss%d: %s (%d)\n", n+1, (char*)bss_link->ssid, bss_link->rssi); + INFO("bss%d: %s (%d)", n+1, (char*)bss_link->ssid, bss_link->rssi); bss_link = bss_link->next.stqe_next; n++; @@ -211,7 +232,7 @@ void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) { static ETSTimer scanTimer; static void ICACHE_FLASH_ATTR scanStartCb(void *arg) { - DBG("Starting a scan\n"); + INFO("Starting a scan"); wifi_station_scan(NULL, wifiScanDoneCb); } @@ -303,28 +324,28 @@ static ETSTimer resetTimer; static void ICACHE_FLASH_ATTR resetTimerCb(void *arg) { int x = wifi_station_get_connect_status(); int m = wifi_get_opmode() & 0x3; - DBG("Wifi check: mode=%s status=%d\n", wifiMode[m], x); + NOTICE("check: mode=%s status=%d", wifiMode[m], x); if(m!=2){ if ( x == STATION_GOT_IP ) { if (m != 1) { #ifdef CHANGE_TO_STA // We're happily connected, go to STA mode - DBG("Wifi got IP. Going into STA mode..\n"); + NOTICE("got IP. Going into STA mode.."); wifi_set_opmode(1); os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); // check one more time after switching to STA-only #endif } log_uart(false); // no more resetTimer at this point, gotta use physical reset to recover if in trouble - } else { - if (m != 3) { - DBG("Wifi connect failed. Going into STA+AP mode..\n"); - wifi_set_opmode(3); - wifi_softap_set_config(&apconf); + } else { + if (m != 3) { + NOTICE("connect failed. Going into STA+AP mode.."); + wifi_set_opmode(3); + wifi_softap_set_config(&apconf); } log_uart(true); - DBG("Enabling/continuing uart log\n"); + INFO("Enabling/continuing uart log"); os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); } } @@ -335,7 +356,7 @@ static ETSTimer reassTimer; // Callback actually doing reassociation static void ICACHE_FLASH_ATTR reassTimerCb(void *arg) { - DBG("Wifi changing association\n"); + NOTICE("changing association"); wifi_station_disconnect(); stconf.bssid_set = 0; wifi_station_set_config(&stconf); @@ -369,7 +390,7 @@ int ICACHE_FLASH_ATTR cgiWiFiConnect(HttpdConnData *connData) { //Set to 0 if you want to disable the actual reconnecting bit os_strncpy((char*)stconf.ssid, essid, 32); os_strncpy((char*)stconf.password, passwd, 64); - DBG("Wifi try to connect to AP %s pw %s\n", essid, passwd); + NOTICE("try to connect to AP %s pw %s", essid, passwd); //Schedule disconnect/connect os_timer_disarm(&reassTimer); @@ -430,7 +451,7 @@ void ICACHE_FLASH_ATTR configWifiIP() { if (wifi_station_dhcpc_status() == DHCP_STARTED) wifi_station_dhcpc_stop(); wifi_station_dhcpc_start(); - DBG("Wifi uses DHCP, hostname=%s\n", flashConfig.hostname); + NOTICE("uses DHCP, hostname=%s", flashConfig.hostname); } else { // no DHCP, we got static network config! wifi_station_dhcpc_stop(); @@ -439,7 +460,7 @@ void ICACHE_FLASH_ATTR configWifiIP() { ipi.netmask.addr = flashConfig.netmask; ipi.gw.addr = flashConfig.gateway; wifi_set_ip_info(0, &ipi); - DBG("Wifi uses static IP %d.%d.%d.%d\n", IP2STR(&ipi.ip.addr)); + NOTICE("uses static IP %d.%d.%d.%d", IP2STR(&ipi.ip.addr)); } #ifdef DEBUGIP debugIP(); diff --git a/esp-link/log.c b/esp-link/log.c index 821641f..d6c245b 100644 --- a/esp-link/log.c +++ b/esp-link/log.c @@ -7,7 +7,7 @@ #include "log.h" #ifdef LOG_DBG -#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) #else #define DBG(format, ...) do { } while(0) #endif diff --git a/esp-link/main.c b/esp-link/main.c index d38ad24..bf8c6ef 100644 --- a/esp-link/main.c +++ b/esp-link/main.c @@ -151,7 +151,7 @@ void user_init(void) { uint32_t fid = spi_flash_get_id(); NOTICE("Flash map %s, manuf 0x%02X chip 0x%04X", flash_maps[system_get_flash_size_map()], fid & 0xff, (fid&0xff00)|((fid>>16)&0xff)); - NOTICE("** esp-link ready"); + NOTICE("** %s: ready, heap=%ld", esp_link_version, (unsigned long)system_get_free_heap_size()); // Init SNTP service cgiServicesSNTPInit(); diff --git a/esp-link/mqtt_client.c b/esp-link/mqtt_client.c index f0d9108..3d390bb 100644 --- a/esp-link/mqtt_client.c +++ b/esp-link/mqtt_client.c @@ -5,7 +5,7 @@ #include "mqtt.h" #ifdef MQTTCLIENT_DBG -#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__) } while(0) +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) #else #define DBG(format, ...) do { } while(0) #endif diff --git a/esp-link/task.c b/esp-link/task.c index 879a694..a780534 100644 --- a/esp-link/task.c +++ b/esp-link/task.c @@ -16,19 +16,18 @@ #define MAXUSRTASKS 8 #ifdef USRTASK_DBG -#define DBG_USRTASK(format, ...) os_printf(format, ## __VA_ARGS__) +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) #else -#define DBG_USRTASK(format, ...) do { } while(0) +#define DBG(format, ...) do { } while(0) #endif LOCAL os_event_t *_task_queue = NULL; // system_os_task queue LOCAL os_task_t *usr_task_queue = NULL; // user task queue // it seems save to run the usr_event_handler from RAM, so no ICACHE_FLASH_ATTR here... - LOCAL void usr_event_handler(os_event_t *e) { - DBG_USRTASK("usr_event_handler: event %p (sig=%d, par=%p)\n", e, (int)e->sig, (void *)e->par); + DBG("usr_event_handler: event %p (sig=%d, par=%p)\n", e, (int)e->sig, (void *)e->par); if (usr_task_queue[e->sig] == NULL || e->sig < 0 || e->sig >= MAXUSRTASKS) { os_printf("usr_event_handler: task %d %s\n", (int)e->sig, usr_task_queue[e->sig] == NULL ? "not registered" : "out of range"); @@ -57,7 +56,7 @@ uint8_t register_usr_task (os_task_t event) { int task; - DBG_USRTASK("register_usr_task: %p\n", event); + DBG("register_usr_task: %p\n", event); if (_task_queue == NULL || usr_task_queue == NULL) init_usr_task(); @@ -68,7 +67,7 @@ uint8_t register_usr_task (os_task_t event) for (task = 0; task < MAXUSRTASKS; task++) { if (usr_task_queue[task] == NULL) { - DBG_USRTASK("register_usr_task: assign task #%d\n", task); + DBG("register_usr_task: assign task #%d\n", task); usr_task_queue[task] = event; break; } diff --git a/httpd/httpd.c b/httpd/httpd.c index 5590809..d6f0097 100644 --- a/httpd/httpd.c +++ b/httpd/httpd.c @@ -18,7 +18,7 @@ Esp8266 http server - core routines #include "httpd.h" #ifdef HTTPD_DBG -#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) +#define DBG(format, ...)os_printf(format, ## __VA_ARGS__) #else #define DBG(format, ...) do { } while(0) #endif diff --git a/mqtt/mqtt.c b/mqtt/mqtt.c index 30e2dd0..81734e1 100644 --- a/mqtt/mqtt.c +++ b/mqtt/mqtt.c @@ -42,9 +42,9 @@ #include "mqtt.h" #ifdef MQTT_DBG -#define DBG_MQTT(format, ...) os_printf(format, ## __VA_ARGS__) +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) #else -#define DBG_MQTT(format, ...) do { } while(0) +#define DBG(format, ...) do { } while(0) #endif extern void dumpMem(void *buf, int len); @@ -145,7 +145,7 @@ mqtt_tcpclient_recv(void* arg, char* pdata, unsigned short len) { if (client->connState != MQTT_CONNECTED) { // why are we receiving something?? - DBG_MQTT("MQTT ERROR: recv in invalid state %d\n", client->connState); + DBG("MQTT ERROR: recv in invalid state %d\n", client->connState); mqtt_doAbort(client); return; } @@ -157,12 +157,12 @@ mqtt_tcpclient_recv(void* arg, char* pdata, unsigned short len) { pending_msg_type = mqtt_get_type(client->pending_buffer->data); pending_msg_id = mqtt_get_id(client->pending_buffer->data, client->pending_buffer->filled); } - DBG_MQTT("MQTT: Recv type=%s id=%04X len=%d; Pend type=%s id=%02X\n", + DBG("MQTT: Recv type=%s id=%04X len=%d; Pend type=%s id=%02X\n", mqtt_msg_type[msg_type], msg_id, msg_len, mqtt_msg_type[pending_msg_type],pending_msg_id); switch (msg_type) { case MQTT_MSG_TYPE_CONNACK: - //DBG_MQTT("MQTT: Connect successful\n"); + //DBG("MQTT: Connect successful\n"); // callbacks for internal and external clients if (client->connectedCb) client->connectedCb(client); if (client->cmdConnectedCb) client->cmdConnectedCb(client); @@ -171,28 +171,28 @@ mqtt_tcpclient_recv(void* arg, char* pdata, unsigned short len) { case MQTT_MSG_TYPE_SUBACK: if (pending_msg_type == MQTT_MSG_TYPE_SUBSCRIBE && pending_msg_id == msg_id) { - //DBG_MQTT("MQTT: Subscribe successful\n"); + //DBG("MQTT: Subscribe successful\n"); client->pending_buffer = PktBuf_ShiftFree(client->pending_buffer); } break; case MQTT_MSG_TYPE_UNSUBACK: if (pending_msg_type == MQTT_MSG_TYPE_UNSUBSCRIBE && pending_msg_id == msg_id) { - //DBG_MQTT("MQTT: Unsubscribe successful\n"); + //DBG("MQTT: Unsubscribe successful\n"); client->pending_buffer = PktBuf_ShiftFree(client->pending_buffer); } break; case MQTT_MSG_TYPE_PUBACK: // ack for a publish we sent if (pending_msg_type == MQTT_MSG_TYPE_PUBLISH && pending_msg_id == msg_id) { - //DBG_MQTT("MQTT: QoS1 Publish successful\n"); + //DBG("MQTT: QoS1 Publish successful\n"); client->pending_buffer = PktBuf_ShiftFree(client->pending_buffer); } break; case MQTT_MSG_TYPE_PUBREC: // rec for a publish we sent if (pending_msg_type == MQTT_MSG_TYPE_PUBLISH && pending_msg_id == msg_id) { - //DBG_MQTT("MQTT: QoS2 publish cont\n"); + //DBG("MQTT: QoS2 publish cont\n"); client->pending_buffer = PktBuf_ShiftFree(client->pending_buffer); // we need to send PUBREL mqtt_msg_pubrel(&client->mqtt_connection, msg_id); @@ -203,7 +203,7 @@ mqtt_tcpclient_recv(void* arg, char* pdata, unsigned short len) { case MQTT_MSG_TYPE_PUBCOMP: // comp for a pubrel we sent (originally publish we sent) if (pending_msg_type == MQTT_MSG_TYPE_PUBREL && pending_msg_id == msg_id) { - //DBG_MQTT("MQTT: QoS2 Publish successful\n"); + //DBG("MQTT: QoS2 Publish successful\n"); client->pending_buffer = PktBuf_ShiftFree(client->pending_buffer); } break; @@ -229,7 +229,7 @@ mqtt_tcpclient_recv(void* arg, char* pdata, unsigned short len) { case MQTT_MSG_TYPE_PUBREL: // rel for a rec we sent (originally publish received) if (pending_msg_type == MQTT_MSG_TYPE_PUBREC && pending_msg_id == msg_id) { - //DBG_MQTT("MQTT: Cont QoS2 recv\n"); + //DBG("MQTT: Cont QoS2 recv\n"); client->pending_buffer = PktBuf_ShiftFree(client->pending_buffer); // we need to send PUBCOMP mqtt_msg_pubcomp(&client->mqtt_connection, msg_id); @@ -262,16 +262,16 @@ mqtt_tcpclient_recv(void* arg, char* pdata, unsigned short len) { */ static void ICACHE_FLASH_ATTR mqtt_tcpclient_sent_cb(void* arg) { - //DBG_MQTT("MQTT: sent CB\n"); + //DBG("MQTT: sent CB\n"); struct espconn* pCon = (struct espconn *)arg; MQTT_Client* client = (MQTT_Client *)pCon->reverse; if (client == NULL) return; // aborted connection ? - //DBG_MQTT("MQTT: Sent\n"); + //DBG("MQTT: Sent\n"); // if the message we sent is not a "pending" one, we need to free the buffer if (client->sending_buffer != NULL) { PktBuf *buf = client->sending_buffer; - //DBG_MQTT("PktBuf free %p l=%d\n", buf, buf->filled); + //DBG("PktBuf free %p l=%d\n", buf, buf->filled); os_free(buf); client->sending_buffer = NULL; } @@ -290,7 +290,7 @@ mqtt_tcpclient_sent_cb(void* arg) { static void ICACHE_FLASH_ATTR mqtt_timer(void* arg) { MQTT_Client* client = (MQTT_Client*)arg; - //DBG_MQTT("MQTT: timer CB\n"); + //DBG("MQTT: timer CB\n"); switch (client->connState) { default: break; @@ -314,7 +314,7 @@ mqtt_timer(void* arg) { // check whether we need to send a keep-alive message if (client->keepAliveTick > 0 && --client->keepAliveTick == 0) { // timeout: we need to send a ping message - //DBG_MQTT("MQTT: Send keepalive\n"); + //DBG("MQTT: Send keepalive\n"); mqtt_msg_pingreq(&client->mqtt_connection); PktBuf *buf = PktBuf_New(client->mqtt_connection.message.length); os_memcpy(buf->data, client->mqtt_connection.message.data, @@ -350,13 +350,13 @@ void ICACHE_FLASH_ATTR mqtt_tcpclient_discon_cb(void* arg) { struct espconn* pespconn = (struct espconn *)arg; MQTT_Client* client = (MQTT_Client *)pespconn->reverse; - DBG_MQTT("MQTT: Disconnect CB, freeing espconn %p\n", arg); + DBG("MQTT: Disconnect CB, freeing espconn %p\n", arg); if (pespconn->proto.tcp) os_free(pespconn->proto.tcp); os_free(pespconn); // if this is an aborted connection we're done if (client == NULL) return; - DBG_MQTT("MQTT: Disconnected from %s:%d\n", client->host, client->port); + DBG("MQTT: Disconnected from %s:%d\n", client->host, client->port); if (client->disconnectedCb) client->disconnectedCb(client); if (client->cmdDisconnectedCb) client->cmdDisconnectedCb(client); @@ -376,7 +376,7 @@ static void ICACHE_FLASH_ATTR mqtt_tcpclient_recon_cb(void* arg, int8_t err) { struct espconn* pespconn = (struct espconn *)arg; MQTT_Client* client = (MQTT_Client *)pespconn->reverse; - //DBG_MQTT("MQTT: Reset CB, freeing espconn %p (err=%d)\n", arg, err); + //DBG("MQTT: Reset CB, freeing espconn %p (err=%d)\n", arg, err); if (pespconn->proto.tcp) os_free(pespconn->proto.tcp); os_free(pespconn); os_printf("MQTT: Connection reset from %s:%d\n", client->host, client->port); @@ -439,7 +439,7 @@ mqtt_enq_message(MQTT_Client *client, const uint8_t *data, uint16_t len) { */ static void ICACHE_FLASH_ATTR mqtt_send_message(MQTT_Client* client) { - //DBG_MQTT("MQTT: Send_message\n"); + //DBG("MQTT: Send_message\n"); PktBuf *buf = client->msgQueue; if (buf == NULL || client->sending) return; // ahem... client->msgQueue = PktBuf_Shift(client->msgQueue); @@ -502,7 +502,7 @@ mqtt_dns_found(const char* name, ip_addr_t* ipaddr, void* arg) { client->connState = TCP_RECONNECT_REQ; // the timer will kick-off a reconnection return; } - DBG_MQTT("MQTT: ip %d.%d.%d.%d\n", + DBG("MQTT: ip %d.%d.%d.%d\n", *((uint8 *)&ipaddr->addr), *((uint8 *)&ipaddr->addr + 1), *((uint8 *)&ipaddr->addr + 2), @@ -521,7 +521,7 @@ mqtt_dns_found(const char* name, ip_addr_t* ipaddr, void* arg) { if (client->reconTimeout < 128) client->reconTimeout <<= 1; client->connState = TCP_RECONNECT_REQ; } else { - DBG_MQTT("MQTT: connecting...\n"); + DBG("MQTT: connecting...\n"); } } } @@ -574,7 +574,7 @@ MQTT_Publish(MQTT_Client* client, const char* topic, const char* data, uint16_t os_memcpy(buf->data, msg.message.data, msg.message.length); buf->filled = msg.message.length; - DBG_MQTT("MQTT: Publish, topic: \"%s\", length: %d\n", topic, msg.message.length); + DBG("MQTT: Publish, topic: \"%s\", length: %d\n", topic, msg.message.length); //dumpMem(buf, buf_len); client->msgQueue = PktBuf_Push(client->msgQueue, buf); @@ -598,7 +598,7 @@ MQTT_Subscribe(MQTT_Client* client, char* topic, uint8_t qos) { os_printf("MQTT ERROR: Queuing Subscribe failed (too long)\n"); return FALSE; } - DBG_MQTT("MQTT: Subscribe, topic: \"%s\"\n", topic); + DBG("MQTT: Subscribe, topic: \"%s\"\n", topic); mqtt_enq_message(client, client->mqtt_connection.message.data, client->mqtt_connection.message.length); return TRUE; @@ -627,7 +627,7 @@ void ICACHE_FLASH_ATTR MQTT_Init(MQTT_Client* client, char* host, uint32 port, uint8_t security, uint8_t sendTimeout, char* client_id, char* client_user, char* client_pass, uint8_t keepAliveTime) { - DBG_MQTT("MQTT_Init\n"); + DBG("MQTT_Init\n"); os_memset(client, 0, sizeof(MQTT_Client)); @@ -754,7 +754,7 @@ mqtt_doAbort(MQTT_Client* client) { void ICACHE_FLASH_ATTR MQTT_Reconnect(MQTT_Client* client) { - DBG_MQTT("MQTT: Reconnect requested\n"); + DBG("MQTT: Reconnect requested\n"); if (client->connState == MQTT_DISCONNECTED) MQTT_Connect(client); else if (client->connState == MQTT_CONNECTED) @@ -764,7 +764,7 @@ MQTT_Reconnect(MQTT_Client* client) { void ICACHE_FLASH_ATTR MQTT_Disconnect(MQTT_Client* client) { - DBG_MQTT("MQTT: Disconnect requested\n"); + DBG("MQTT: Disconnect requested\n"); os_timer_disarm(&client->mqttTimer); if (client->connState == MQTT_DISCONNECTED) return; if (client->connState == TCP_RECONNECT_REQ) { @@ -779,7 +779,7 @@ MQTT_Disconnect(MQTT_Client* client) { void ICACHE_FLASH_ATTR MQTT_Free(MQTT_Client* client) { - DBG_MQTT("MQTT: Free requested\n"); + DBG("MQTT: Free requested\n"); MQTT_Disconnect(client); if (client->host) os_free(client->host); diff --git a/mqtt/mqtt_cmd.c b/mqtt/mqtt_cmd.c index 71c5d20..8c1611d 100644 --- a/mqtt/mqtt_cmd.c +++ b/mqtt/mqtt_cmd.c @@ -8,7 +8,7 @@ #include "mqtt_cmd.h" #ifdef MQTTCMD_DBG -#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) #else #define DBG(format, ...) do { } while(0) #endif diff --git a/rest/rest.c b/rest/rest.c index 67c0f7d..d98d095 100644 --- a/rest/rest.c +++ b/rest/rest.c @@ -9,9 +9,9 @@ #include "cmd.h" #ifdef REST_DBG -#define DBG_REST(format, ...) os_printf(format, ## __VA_ARGS__) +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) #else -#define DBG_REST(format, ...) do { } while(0) +#define DBG(format, ...) do { } while(0) #endif typedef enum { @@ -93,7 +93,7 @@ tcpclient_recv(void *arg, char *pdata, unsigned short len) { // collect body and send it int body_len = len-pi; - DBG_REST("REST: status=%d, body=%d\n", code, body_len); + DBG("REST: status=%d, body=%d\n", code, body_len); if (pi == len) { cmdResponseStart(CMD_RESP_CB, client->resp_cb, 1); cmdResponseBody(&code, sizeof(code)); @@ -120,7 +120,7 @@ static void ICACHE_FLASH_ATTR tcpclient_sent_cb(void *arg) { struct espconn *pCon = (struct espconn *)arg; RestClient* client = (RestClient *)pCon->reverse; - DBG_REST("REST: Sent\n"); + DBG("REST: Sent\n"); if (client->data_sent != client->data_len) { // we only sent part of the buffer, send the rest espconn_sent(client->pCon, (uint8_t*)(client->data+client->data_sent), @@ -156,13 +156,13 @@ static void ICACHE_FLASH_ATTR tcpclient_connect_cb(void *arg) { struct espconn *pCon = (struct espconn *)arg; RestClient* client = (RestClient *)pCon->reverse; - DBG_REST("REST #%d: connected\n", client-restClient); + DBG("REST #%d: connected\n", client-restClient); espconn_regist_disconcb(client->pCon, tcpclient_discon_cb); espconn_regist_recvcb(client->pCon, tcpclient_recv); espconn_regist_sentcb(client->pCon, tcpclient_sent_cb); client->data_sent = client->data_len <= 1400 ? client->data_len : 1400; - DBG_REST("REST #%d: sending %d\n", client-restClient, client->data_sent); + DBG("REST #%d: sending %d\n", client-restClient, client->data_sent); //if(client->security){ // espconn_secure_sent(client->pCon, client->data, client->data_sent); //} @@ -180,7 +180,7 @@ rest_dns_found(const char *name, ip_addr_t *ipaddr, void *arg) { os_printf("REST DNS: Got no ip, try to reconnect\n"); return; } - DBG_REST("REST DNS: found ip %d.%d.%d.%d\n", + DBG("REST DNS: found ip %d.%d.%d.%d\n", *((uint8 *) &ipaddr->addr), *((uint8 *) &ipaddr->addr + 1), *((uint8 *) &ipaddr->addr + 2), @@ -193,7 +193,7 @@ rest_dns_found(const char *name, ip_addr_t *ipaddr, void *arg) { } else #endif espconn_connect(client->pCon); - DBG_REST("REST: connecting...\n"); + DBG("REST: connecting...\n"); } } @@ -252,7 +252,7 @@ REST_Setup(CmdPacket *cmd) { os_free(client->pCon); } os_memset(client, 0, sizeof(RestClient)); - DBG_REST("REST: setup #%d host=%s port=%d security=%d\n", clientNum, rest_host, port, security); + DBG("REST: setup #%d host=%s port=%d security=%d\n", clientNum, rest_host, port, security); client->resp_cb = cmd->value; @@ -315,7 +315,7 @@ REST_SetHeader(CmdPacket *cmd) { client->header[len] = '\r'; client->header[len+1] = '\n'; client->header[len+2] = 0; - DBG_REST("REST: Set header: %s\r\n", client->header); + DBG("REST: Set header: %s\r\n", client->header); break; case HEADER_CONTENT_TYPE: if(client->content_type) os_free(client->content_type); @@ -324,7 +324,7 @@ REST_SetHeader(CmdPacket *cmd) { client->content_type[len] = '\r'; client->content_type[len+1] = '\n'; client->content_type[len+2] = 0; - DBG_REST("REST: Set content_type: %s\r\n", client->content_type); + DBG("REST: Set content_type: %s\r\n", client->content_type); break; case HEADER_USER_AGENT: if(client->user_agent) os_free(client->user_agent); @@ -333,7 +333,7 @@ REST_SetHeader(CmdPacket *cmd) { client->user_agent[len] = '\r'; client->user_agent[len+1] = '\n'; client->user_agent[len+2] = 0; - DBG_REST("REST: Set user_agent: %s\r\n", client->user_agent); + DBG("REST: Set user_agent: %s\r\n", client->user_agent); break; } } @@ -342,13 +342,13 @@ void ICACHE_FLASH_ATTR REST_Request(CmdPacket *cmd) { CmdRequest req; cmdRequest(&req, cmd); - DBG_REST("REST: request"); + DBG("REST: request"); if (cmd->argc != 2 && cmd->argc != 3) return; // Get client uint32_t clientNum = cmd->value; RestClient *client = restClient + (clientNum % MAX_REST); - DBG_REST(" #%d", clientNum); + DBG(" #%d", clientNum); // Get HTTP method uint16_t len = cmdArgLen(&req); @@ -356,7 +356,7 @@ REST_Request(CmdPacket *cmd) { char method[16]; cmdPopArg(&req, method, len); method[len] = 0; - DBG_REST(" method=%s", method); + DBG(" method=%s", method); // Get HTTP path len = cmdArgLen(&req); @@ -364,7 +364,7 @@ REST_Request(CmdPacket *cmd) { char path[1024]; cmdPopArg(&req, path, len); path[len] = 0; - DBG_REST(" path=%s", path); + DBG(" path=%s", path); // Get HTTP body uint32_t realLen = 0; @@ -374,7 +374,7 @@ REST_Request(CmdPacket *cmd) { realLen = cmdArgLen(&req); if (realLen > 2048) goto fail; } - DBG_REST(" bodyLen=%d", realLen); + DBG(" bodyLen=%ld", realLen); // we need to allocate memory for the header plus the body. First we count the length of the // header (including some extra counted "%s" and then we add the body length. We allocate the @@ -389,30 +389,30 @@ REST_Request(CmdPacket *cmd) { "User-Agent: %s\r\n\r\n"; uint16_t headerLen = strlen(headerFmt) + strlen(method) + strlen(path) + strlen(client->host) + strlen(client->header) + strlen(client->content_type) + strlen(client->user_agent); - DBG_REST(" hdrLen=%d", headerLen); + DBG(" hdrLen=%d", headerLen); if (client->data) os_free(client->data); client->data = (char*)os_zalloc(headerLen + realLen); if (client->data == NULL) goto fail; - DBG_REST(" totLen=%d data=%p", headerLen + realLen, client->data); + DBG(" totLen=%ld data=%p", headerLen + realLen, client->data); client->data_len = os_sprintf((char*)client->data, headerFmt, method, path, client->host, client->header, realLen, client->content_type, client->user_agent); - DBG_REST(" hdrLen=%d", client->data_len); + DBG(" hdrLen=%d", client->data_len); if (realLen > 0) { cmdPopArg(&req, client->data + client->data_len, realLen); client->data_len += realLen; } - DBG_REST("\n"); + DBG("\n"); - //DBG_REST("REST request: %s", (char*)client->data); + //DBG("REST request: %s", (char*)client->data); - //DBG_REST("REST: pCon state=%d\n", client->pCon->state); + //DBG("REST: pCon state=%d\n", client->pCon->state); client->pCon->state = ESPCONN_NONE; espconn_regist_connectcb(client->pCon, tcpclient_connect_cb); espconn_regist_reconcb(client->pCon, tcpclient_recon_cb); if(UTILS_StrToIP((char *)client->host, &client->pCon->proto.tcp->remote_ip)) { - DBG_REST("REST: Connect to ip %s:%d\n",client->host, client->port); + DBG("REST: Connect to ip %s:%ld\n",client->host, client->port); //if(client->security){ // espconn_secure_connect(client->pCon); //} @@ -420,12 +420,12 @@ REST_Request(CmdPacket *cmd) { espconn_connect(client->pCon); //} } else { - DBG_REST("REST: Connect to host %s:%d\n", client->host, client->port); + DBG("REST: Connect to host %s:%ld\n", client->host, client->port); espconn_gethostbyname(client->pCon, (char *)client->host, &client->ip, rest_dns_found); } return; fail: - DBG_REST("\n"); + DBG("\n"); } diff --git a/serial/slip.c b/serial/slip.c index 76d8f97..6cc418e 100644 --- a/serial/slip.c +++ b/serial/slip.c @@ -8,7 +8,7 @@ #include "cmd.h" #ifdef SLIP_DBG -#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) #else #define DBG(format, ...) do { } while(0) #endif diff --git a/serial/uart.c b/serial/uart.c index f7f07fa..febcc1f 100644 --- a/serial/uart.c +++ b/serial/uart.c @@ -22,9 +22,9 @@ #include "uart.h" #ifdef UART_DBG -#define DBG_UART(format, ...) os_printf(format, ## __VA_ARGS__) +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) #else -#define DBG_UART(format, ...) do { } while(0) +#define DBG(format, ...) do { } while(0) #endif LOCAL uint8_t uart_recvTaskNum; @@ -203,7 +203,7 @@ uart0_rx_intr_handler(void *para) if (UART_RXFIFO_FULL_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_FULL_INT_ST) || UART_RXFIFO_TOUT_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_TOUT_INT_ST)) { - //DBG_UART("stat:%02X",*(uint8 *)UART_INT_ENA(uart_no)); + //DBG("stat:%02X",*(uint8 *)UART_INT_ENA(uart_no)); ETS_UART_INTR_DISABLE(); post_usr_task(uart_recvTaskNum, 0); } @@ -226,7 +226,7 @@ uart_recvTask(os_event_t *events) (length < 128)) { buf[length++] = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF; } - //DBG_UART("%d ix %d\n", system_get_time(), length); + //DBG("%d ix %d\n", system_get_time(), length); for (int i=0; imsgid, syslogQueue->tick); DBG("[%uµs] %s: id=%lu\n", WDEV_NOW(), __FUNCTION__, syslogQueue ? syslogQueue->msgid : 0); - if (syslogQueue == NULL) syslog_set_status(SYSLOG_READY); else { @@ -234,11 +234,14 @@ static void ICACHE_FLASH_ATTR syslog_udp_recv_cb(void *arg, char *pusrdata, unsi ******************************************************************************/ static void ICACHE_FLASH_ATTR syslog_gethostbyname_cb(const char *name, ip_addr_t *ipaddr, void *arg) { + DBG("[%uµs] %s\n", WDEV_NOW(), __FUNCTION__); struct espconn *pespconn = (struct espconn *)arg; - (void) pespconn; + // espconn not longer required + os_free(pespconn->proto.udp); + os_free(pespconn); - DBG("[%uµs] %s\n", WDEV_NOW(), __FUNCTION__); if (ipaddr != NULL) { + syslog(SYSLOG_FAC_USER, SYSLOG_PRIO_NOTICE, "SYSLOG", "resolved hostname: %s: " IPSTR, name, IP2STR(ipaddr)); syslogHost.addr.addr = ipaddr->addr; @@ -273,7 +276,7 @@ void ICACHE_FLASH_ATTR syslog_init(char *syslog_host) if (syslog_espconn->proto.udp) { // there's no counterpart to espconn_create... os_free(syslog_espconn->proto.udp); - } + } os_free(syslog_espconn); } syslog_espconn = NULL; @@ -322,16 +325,18 @@ void ICACHE_FLASH_ATTR syslog_init(char *syslog_host) // wifi_set_broadcast_if(STATIONAP_MODE); // send UDP broadcast from both station and soft-AP interface espconn_create(syslog_espconn); // create udp - syslog(SYSLOG_FAC_USER, SYSLOG_PRIO_NOTICE, "SYSLOG", - "syslogserver: %s:%d %d", host, syslogHost.port, syslog_espconn->proto.udp->local_port); - if (UTILS_StrToIP((const char *)host, (void*)&syslogHost.addr)) { syslog_set_status(SYSLOG_READY); } else { + // we use our own espconn structure to avoid side effects... + if (syslog_dnsconn == NULL) + syslog_dnsconn = (espconn *)os_zalloc(sizeof(espconn)); + + if (syslog_dnsconn->proto.udp == NULL) + syslog_dnsconn->proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp)); + syslog_set_status(SYSLOG_DNSWAIT); - syslog(SYSLOG_FAC_USER, SYSLOG_PRIO_NOTICE, "SYSLOG", - "must resolve hostname \"%s\"", host); - espconn_gethostbyname(syslog_espconn, host, &syslogHost.addr, syslog_gethostbyname_cb); + espconn_gethostbyname(syslog_dnsconn, host, &syslogHost.addr, syslog_gethostbyname_cb); } } @@ -355,13 +360,15 @@ syslog_add_entry(syslog_entry_t *entry) pse = pse->next; pse->next = entry; // append msg to syslog queue } -// DBG("%p %lu %d\n", entry, entry->msgid, system_get_free_heap_size()); + // Debug: show queue addr, current msgid, avail. heap and syslog datagram + // DBG("%p %lu %d %s\n", entry, entry->msgid, system_get_free_heap_size(), entry->datagram); // ensure we have sufficient heap for the rest of the system if (system_get_free_heap_size() < syslogHost.min_heap_size) { if (syslogState != SYSLOG_HALTED) { - os_printf("syslog_add_entry: Warning: queue filled up, halted\n"); - entry->next = syslog_compose(SYSLOG_FAC_USER, SYSLOG_PRIO_CRIT, "SYSLOG", "queue filled up, halted"); + // os_printf("syslog_add_entry: Warning: queue filled up (%d), halted\n", system_get_free_heap_size()); + entry->next = syslog_compose(SYSLOG_FAC_USER, SYSLOG_PRIO_CRIT, "SYSLOG", "queue filled up (%d), halted", system_get_free_heap_size()); + os_printf("%s\n", entry->next->datagram); if (syslogState == SYSLOG_READY) syslog_send_udp(); syslog_set_status(SYSLOG_HALTED); @@ -378,6 +385,11 @@ syslog_add_entry(syslog_entry_t *entry) LOCAL syslog_entry_t ICACHE_FLASH_ATTR * syslog_compose(uint8_t facility, uint8_t severity, const char *tag, const char *fmt, ...) { + union { + uint8_t buf[sizeof (syslog_entry_t) + 1024]; + syslog_entry_t se; + } sl; + DBG("[%dµs] %s id=%lu\n", WDEV_NOW(), __FUNCTION__, syslog_msgid); syslog_entry_t *se = os_zalloc(sizeof (syslog_entry_t) + 1024); // allow up to 1k datagram if (se == NULL) return NULL; @@ -399,21 +411,22 @@ syslog_compose(uint8_t facility, uint8_t severity, const char *tag, const char * // create timestamp: FULL-DATE "T" PARTIAL-TIME "Z": 'YYYY-mm-ddTHH:MM:SSZ ' // as long as realtime_stamp is 0 we use tick div 10⁶ as date - now = (realtime_stamp == 0) ? (se->tick / 1000000) : realtime_stamp; + now = (realtime_stamp == 0) ? (sl.se.tick / 1000000) : realtime_stamp; tp = gmtime(&now); p += os_sprintf(p, "%4d-%02d-%02dT%02d:%02d:%02d", tp->tm_year + 1900, tp->tm_mon + 1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec); if (realtime_stamp == 0) - p += os_sprintf(p, ".%06uZ ", se->tick % 1000000); + p += os_sprintf(p, ".%06uZ ", sl.se.tick % 1000000); else p += os_sprintf(p, "%+03d:00 ", flashConfig.timezone_offset); } // add HOSTNAME APP-NAME PROCID MSGID if (flashConfig.syslog_showtick) - p += os_sprintf(p, "%s %s %u.%06u %u ", flashConfig.hostname, tag, se->tick / 1000000, se->tick % 1000000, syslog_msgid++); + p += os_sprintf(p, "%s %s %u.%06u %u ", flashConfig.hostname, tag, sl.se.tick / 1000000, + sl.se.tick % 1000000, syslog_msgid++); else p += os_sprintf(p, "%s %s - %u ", flashConfig.hostname, tag, syslog_msgid++); @@ -423,8 +436,9 @@ syslog_compose(uint8_t facility, uint8_t severity, const char *tag, const char * p += ets_vsprintf(p, fmt, arglist ); va_end(arglist); - se->datagram_len = p - se->datagram; - se = mem_trim(se, sizeof(syslog_entry_t) + se->datagram_len + 1); + sl.se.datagram_len = 1 + p - sl.se.datagram; + syslog_entry_t *se = os_zalloc(sizeof (syslog_entry_t) + sl.se.datagram_len); + os_memcpy(se, &sl.se, sizeof (syslog_entry_t) + sl.se.datagram_len); return se; } @@ -504,7 +518,8 @@ 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) + syslogState == SYSLOG_HALTED || + flashConfig.syslog_host[0] == '\0') return; if (severity > flashConfig.syslog_filter) diff --git a/user/user_main.c b/user/user_main.c index 5a4331d..fc32899 100644 --- a/user/user_main.c +++ b/user/user_main.c @@ -1,5 +1,13 @@ #include -#include +#include "config.h" +#include "syslog.h" + +#define APPINIT_DBG +#ifdef APPINIT_DBG +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) +#else +#define DBG(format, ...) do { } while(0) +#endif // initialize the custom stuff that goes beyond esp-link void app_init() {