From 1d0f3136c50e883e29cd3a558b3a4d1735a2f43d Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sat, 27 Feb 2016 09:44:41 -0800 Subject: [PATCH 01/20] fixes #111: handle malloc failure in syslog --- syslog/syslog.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/syslog/syslog.c b/syslog/syslog.c index 20a51cb..207b678 100644 --- a/syslog/syslog.c +++ b/syslog/syslog.c @@ -380,6 +380,7 @@ syslog_compose(uint8_t facility, uint8_t severity, const char *tag, const char * { 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; char *p = se->datagram; se->tick = WDEV_NOW(); // 0 ... 4294.967295s se->msgid = syslog_msgid; @@ -512,6 +513,7 @@ void ICACHE_FLASH_ATTR syslog(uint8_t facility, uint8_t severity, const char *ta // compose the syslog message void *arg = __builtin_apply_args(); void *res = __builtin_apply((void*)syslog_compose, arg, 128); + if (res == NULL) return; // compose failed, probably due to malloc failure syslog_entry_t *se = *(syslog_entry_t **)res; // and append it to the message queue From 3b8011f4714211531675c324091c0e5f91acf3db Mon Sep 17 00:00:00 2001 From: Alastair D'Silva Date: Thu, 31 Mar 2016 23:26:10 +1100 Subject: [PATCH 02/20] Use the latest SDK as packaged in esp-open-sdk --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 4e5fa72..587e188 100644 --- a/Makefile +++ b/Makefile @@ -56,7 +56,7 @@ XTENSA_TOOLS_ROOT ?= $(abspath ../esp-open-sdk/xtensa-lx106-elf/bin)/ # Base directory of the ESP8266 SDK package, absolute # Typically you'll download from Espressif's BBS, http://bbs.espressif.com/viewforum.php?f=5 -SDK_BASE ?= $(abspath ../esp_iot_sdk_v1.5.1) +SDK_BASE ?= $(abspath ../esp-open-sdk/esp_iot_sdk_v1.5.2) # Esptool.py path and port, only used for 1-time serial flashing # Typically you'll use https://github.com/themadinventor/esptool @@ -213,7 +213,7 @@ EXTRA_INCDIR = include . LIBS = c gcc hal phy pp net80211 wpa main lwip crypto # compiler flags using during compilation of source files -CFLAGS += -Os -ggdb -std=c99 -Werror -Wpointer-arith -Wundef -Wall -Wl,-EL -fno-inline-functions \ +CFLAGS += -Os -ggdb -std=c99 -I$(SDK_BASE)/include -Wpointer-arith -Wundef -Wall -Wl,-EL -fno-inline-functions \ -nostdlib -mlongcalls -mtext-section-literals -ffunction-sections -fdata-sections \ -D__ets__ -DICACHE_FLASH -D_STDINT_H -Wno-address -DFIRMWARE_SIZE=$(ESP_FLASH_MAX) \ -DMCU_RESET_PIN=$(MCU_RESET_PIN) -DMCU_ISP_PIN=$(MCU_ISP_PIN) \ From 8709e92d459300d03f107675be87af444231c6dc Mon Sep 17 00:00:00 2001 From: Alastair D'Silva Date: Thu, 31 Mar 2016 23:29:20 +1100 Subject: [PATCH 03/20] Fix sytax errors --- esp-link/cgi.c | 6 +++--- esp-link/cgi.h | 2 +- esp-link/config.c | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/esp-link/cgi.c b/esp-link/cgi.c index 713ba31..96c03e9 100644 --- a/esp-link/cgi.c +++ b/esp-link/cgi.c @@ -106,18 +106,18 @@ int8_t ICACHE_FLASH_ATTR getUInt16Arg(HttpdConnData *connData, char *name, uint1 return 1; } -int8_t ICACHE_FLASH_ATTR getBoolArg(HttpdConnData *connData, char *name, bool *config) { +int8_t ICACHE_FLASH_ATTR getBoolArg(HttpdConnData *connData, char *name, uint8_t *config) { char buff[16]; int len = httpdFindArg(connData->getArgs, name, buff, sizeof(buff)); if (len < 0) return 0; // not found, skip if (os_strcmp(buff, "1") == 0 || os_strcmp(buff, "true") == 0) { - *config = true; + *config = 1; return 1; } if (os_strcmp(buff, "0") == 0 || os_strcmp(buff, "false") == 0) { - *config = false; + *config = 0; return 1; } diff --git a/esp-link/cgi.h b/esp-link/cgi.h index cb897b0..330a5e6 100644 --- a/esp-link/cgi.h +++ b/esp-link/cgi.h @@ -26,7 +26,7 @@ int8_t getUInt16Arg(HttpdConnData *connData, char *name, uint16_t *config); // Get the HTTP query-string param 'name' and store it boolean value at 'config', // supports 1/true and 0/false, returns -1 on error, 0 if not found, 1 if found -int8_t getBoolArg(HttpdConnData *connData, char *name, bool *config); +int8_t getBoolArg(HttpdConnData *connData, char *name, uint8_t *config); int cgiMenu(HttpdConnData *connData); diff --git a/esp-link/config.c b/esp-link/config.c index 9b928cc..026d139 100644 --- a/esp-link/config.c +++ b/esp-link/config.c @@ -27,7 +27,7 @@ FlashConfig flashDefault = { .mqtt_host = "\0", .mqtt_clientid = "\0", .mqtt_username= "\0", .mqtt_password = "\0", .mqtt_status_topic = "\0", .sys_descr = "\0", - .rx_pullup = 1, + .rx_pullup = 1, .sntp_server = "us.pool.ntp.org\0", .syslog_host = "\0", .syslog_minheap = 8192, .syslog_filter = 7, .syslog_showtick = 1, .syslog_showdate = 0, .mdns_enable = 1, .mdns_servername = "http\0", .timezone_offset = 0 From 406b4666b653aff3a2de241e1ccbca279be87d8e Mon Sep 17 00:00:00 2001 From: Alastair D'Silva Date: Fri, 1 Apr 2016 00:10:23 +1100 Subject: [PATCH 04/20] Revert "Use the latest SDK as packaged in esp-open-sdk" This reverts commit 3b8011f4714211531675c324091c0e5f91acf3db. --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 587e188..4e5fa72 100644 --- a/Makefile +++ b/Makefile @@ -56,7 +56,7 @@ XTENSA_TOOLS_ROOT ?= $(abspath ../esp-open-sdk/xtensa-lx106-elf/bin)/ # Base directory of the ESP8266 SDK package, absolute # Typically you'll download from Espressif's BBS, http://bbs.espressif.com/viewforum.php?f=5 -SDK_BASE ?= $(abspath ../esp-open-sdk/esp_iot_sdk_v1.5.2) +SDK_BASE ?= $(abspath ../esp_iot_sdk_v1.5.1) # Esptool.py path and port, only used for 1-time serial flashing # Typically you'll use https://github.com/themadinventor/esptool @@ -213,7 +213,7 @@ EXTRA_INCDIR = include . LIBS = c gcc hal phy pp net80211 wpa main lwip crypto # compiler flags using during compilation of source files -CFLAGS += -Os -ggdb -std=c99 -I$(SDK_BASE)/include -Wpointer-arith -Wundef -Wall -Wl,-EL -fno-inline-functions \ +CFLAGS += -Os -ggdb -std=c99 -Werror -Wpointer-arith -Wundef -Wall -Wl,-EL -fno-inline-functions \ -nostdlib -mlongcalls -mtext-section-literals -ffunction-sections -fdata-sections \ -D__ets__ -DICACHE_FLASH -D_STDINT_H -Wno-address -DFIRMWARE_SIZE=$(ESP_FLASH_MAX) \ -DMCU_RESET_PIN=$(MCU_RESET_PIN) -DMCU_ISP_PIN=$(MCU_ISP_PIN) \ From e7ea88295e2d260c660e68e0c44988f54198a569 Mon Sep 17 00:00:00 2001 From: Alastair D'Silva Date: Fri, 1 Apr 2016 19:20:24 +1100 Subject: [PATCH 05/20] Remove definition of _STDINT_H, it suppresses the inclusion of the compiler stdint.h, resulting in uint8_t & friends being undefined --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 4e5fa72..cacdc65 100644 --- a/Makefile +++ b/Makefile @@ -215,7 +215,7 @@ LIBS = c gcc hal phy pp net80211 wpa main lwip crypto # compiler flags using during compilation of source files CFLAGS += -Os -ggdb -std=c99 -Werror -Wpointer-arith -Wundef -Wall -Wl,-EL -fno-inline-functions \ -nostdlib -mlongcalls -mtext-section-literals -ffunction-sections -fdata-sections \ - -D__ets__ -DICACHE_FLASH -D_STDINT_H -Wno-address -DFIRMWARE_SIZE=$(ESP_FLASH_MAX) \ + -D__ets__ -DICACHE_FLASH -Wno-address -DFIRMWARE_SIZE=$(ESP_FLASH_MAX) \ -DMCU_RESET_PIN=$(MCU_RESET_PIN) -DMCU_ISP_PIN=$(MCU_ISP_PIN) \ -DLED_CONN_PIN=$(LED_CONN_PIN) -DLED_SERIAL_PIN=$(LED_SERIAL_PIN) \ -DVERSION="$(VERSION)" From 80d994526634329f10328278f857009e55243370 Mon Sep 17 00:00:00 2001 From: Alastair D'Silva Date: Fri, 1 Apr 2016 19:48:37 +1100 Subject: [PATCH 06/20] Fix warnings reported in #113 (format warnings, differing types) --- cmd/cmd.c | 4 ++-- cmd/handlers.c | 2 +- esp-link/cgiflash.c | 4 ++-- esp-link/cgioptiboot.c | 12 ++++++------ esp-link/cgipins.c | 2 +- esp-link/cgiservices.c | 14 +++++++------- esp-link/config.h | 10 +++++----- esp-link/main.c | 8 ++++---- mqtt/mqtt_cmd.c | 6 +++--- rest/rest.c | 12 ++++++------ serial/console.c | 6 +++--- syslog/syslog.c | 6 +++--- 12 files changed, 43 insertions(+), 43 deletions(-) diff --git a/cmd/cmd.c b/cmd/cmd.c index 6ebb8cb..705ee8a 100644 --- a/cmd/cmd.c +++ b/cmd/cmd.c @@ -43,7 +43,7 @@ static uint16_t resp_crc; // Start a response, returns the partial CRC void ICACHE_FLASH_ATTR cmdResponseStart(uint16_t cmd, uint32_t value, uint16_t argc) { - DBG("cmdResponse: cmd=%d val=%ld argc=%d\n", cmd, value, argc); + DBG("cmdResponse: cmd=%d val=%d argc=%d\n", cmd, value, argc); uart0_write_char(SLIP_END); cmdProtoWriteBuf((uint8_t*)&cmd, 2); @@ -107,7 +107,7 @@ cmdParsePacket(uint8_t *buf, short len) { uint8_t *data_ptr = (uint8_t*)&packet->args; uint8_t *data_limit = data_ptr+len; - DBG("cmdParsePacket: cmd=%d argc=%d value=%lu\n", + DBG("cmdParsePacket: cmd=%d argc=%d value=%u\n", packet->cmd, packet->argc, packet->value diff --git a/cmd/handlers.c b/cmd/handlers.c index 2b9c1ad..061526a 100644 --- a/cmd/handlers.c +++ b/cmd/handlers.c @@ -62,7 +62,7 @@ cmdAddCb(char* name, uint32_t cb) { os_strncpy(callbacks[i].name, name, sizeof(callbacks[i].name)); callbacks[i].name[CMD_CBNLEN-1] = 0; // strncpy doesn't null terminate callbacks[i].callback = cb; - DBG("cmdAddCb: '%s'->0x%lx added at %d\n", callbacks[i].name, cb, i); + DBG("cmdAddCb: '%s'->0x%x added at %d\n", callbacks[i].name, cb, i); return 1; } } diff --git a/esp-link/cgiflash.c b/esp-link/cgiflash.c index 8c39e36..4b9277d 100644 --- a/esp-link/cgiflash.c +++ b/esp-link/cgiflash.c @@ -30,7 +30,7 @@ static char* ICACHE_FLASH_ATTR check_header(void *buf) { uint8_t *cd = (uint8_t *)buf; #ifdef CGIFLASH_DBG uint32_t *buf32 = buf; - os_printf("%p: %08lX %08lX %08lX %08lX\n", buf, buf32[0], buf32[1], buf32[2], buf32[3]); + os_printf("%p: %08X %08X %08X %08X\n", buf, buf32[0], buf32[1], buf32[2], buf32[3]); #endif if (cd[0] != 0xEA) return "IROM magic missing"; if (cd[1] != 4 || cd[2] > 3 || (cd[3]>>4) > 6) return "bad flash header"; @@ -197,4 +197,4 @@ int ICACHE_FLASH_ATTR cgiReset(HttpdConnData *connData) { os_timer_setfn(&flash_reboot_timer, (os_timer_func_t *)system_restart, NULL); os_timer_arm(&flash_reboot_timer, 2000, 1); return HTTPD_CGI_DONE; -} \ No newline at end of file +} diff --git a/esp-link/cgioptiboot.c b/esp-link/cgioptiboot.c index 6594973..ed87d0e 100644 --- a/esp-link/cgioptiboot.c +++ b/esp-link/cgioptiboot.c @@ -153,7 +153,7 @@ int ICACHE_FLASH_ATTR cgiOptibootSync(HttpdConnData *connData) { if (!errMessage[0] && progState >= stateProg) { char buf[64]; DBG("OB got sync\n"); - os_sprintf(buf, "SYNC at %ld baud: bootloader v%d.%d", + os_sprintf(buf, "SYNC at %d baud: bootloader v%d.%d", baudRate, optibootVers>>8, optibootVers&0xff); httpdSend(connData, buf, -1); } else if (errMessage[0] && progState == stateSync) { @@ -314,7 +314,7 @@ int ICACHE_FLASH_ATTR cgiOptibootData(HttpdConnData *connData) { float dt = ((system_get_time() - optibootData->startTime)/1000)/1000.0; // in seconds uint16_t pgmDone = optibootData->pgmDone; optibootInit(); - os_sprintf(errMessage, "Success. %d bytes at %ld baud in %d.%ds, %dB/s %d%% efficient", + os_sprintf(errMessage, "Success. %d bytes at %d baud in %d.%ds, %dB/s %d%% efficient", pgmDone, baudRate, (int)dt, (int)(dt*10)%10, (int)(pgmDone/dt), (int)(100.0*(10.0*pgmDone/baudRate)/dt)); } else { @@ -387,7 +387,7 @@ static bool ICACHE_FLASH_ATTR processRecord(char *buf, short len) { optibootData->eof = true; break; case 0x04: // address - DBG("OB address 0x%lx\n", getHexValue(buf+8, 4) << 16); + DBG("OB address 0x%x\n", getHexValue(buf+8, 4) << 16); // program any remaining partial page if (optibootData->pageLen > 0) if (!programPage()) return false; @@ -435,7 +435,7 @@ static bool ICACHE_FLASH_ATTR programPage(void) { uint16_t pgmLen = optibootData->pageLen; if (pgmLen > optibootData->pgmSz) pgmLen = optibootData->pgmSz; - DBG("OB pgm %d@0x%lx\n", pgmLen, optibootData->address); + DBG("OB pgm %d@0x%x\n", pgmLen, optibootData->address); // send address to optiboot (little endian format) #ifdef DBG_GPIO5 @@ -525,7 +525,7 @@ static void ICACHE_FLASH_ATTR optibootTimerCB(void *arg) { return; } // time to switch baud rate and issue a reset - DBG("OB no sync response @%ld baud\n", baudRate); + DBG("OB no sync response @%d baud\n", baudRate); setBaud(); serbridgeReset(); progState = stateInit; @@ -539,7 +539,7 @@ static void ICACHE_FLASH_ATTR optibootTimerCB(void *arg) { return; default: // we're trying to get some info from optiboot and it should have responded! optibootInit(); // abort - os_sprintf(errMessage, "No response in state %s(%d) @%ld baud\n", + os_sprintf(errMessage, "No response in state %s(%d) @%d baud\n", progStates[progState], progState, baudRate); DBG("OB %s\n", errMessage); return; // do not re-arm timer diff --git a/esp-link/cgipins.c b/esp-link/cgipins.c index c219f77..b763d12 100644 --- a/esp-link/cgipins.c +++ b/esp-link/cgipins.c @@ -49,7 +49,7 @@ int ICACHE_FLASH_ATTR cgiPinsSet(HttpdConnData *connData) { int8_t ok = 0; int8_t reset, isp, conn, ser; - bool swap, rxpup; + uint8_t swap, rxpup; ok |= getInt8Arg(connData, "reset", &reset); ok |= getInt8Arg(connData, "isp", &isp); ok |= getInt8Arg(connData, "conn", &conn); diff --git a/esp-link/cgiservices.c b/esp-link/cgiservices.c index 81f7200..cf55dd1 100644 --- a/esp-link/cgiservices.c +++ b/esp-link/cgiservices.c @@ -66,11 +66,11 @@ int ICACHE_FLASH_ATTR cgiSystemInfo(HttpdConnData *connData) { "\"name\": \"%s\", " "\"reset cause\": \"%d=%s\", " "\"size\": \"%s\", " - "\"id\": \"0x%02lX 0x%04lX\", " + "\"id\": \"0x%02X 0x%04X\", " "\"partition\": \"%s\", " "\"slip\": \"%s\", " "\"mqtt\": \"%s/%s\", " - "\"baud\": \"%ld\", " + "\"baud\": \"%d\", " "\"description\": \"%s\"" " }", flashConfig.hostname, @@ -92,10 +92,10 @@ int ICACHE_FLASH_ATTR cgiSystemInfo(HttpdConnData *connData) { } void ICACHE_FLASH_ATTR cgiServicesSNTPInit() { - if (flashConfig.sntp_server[0] != '\0') { + if (flashConfig.sntp_server[0] != '\0') { sntp_stop(); if (true == sntp_set_timezone(flashConfig.timezone_offset)) { - sntp_setservername(0, flashConfig.sntp_server); + sntp_setservername(0, flashConfig.sntp_server); sntp_init(); } DBG("SNTP timesource set to %s with offset %d\n", flashConfig.sntp_server, flashConfig.timezone_offset); @@ -107,7 +107,7 @@ int ICACHE_FLASH_ATTR cgiServicesInfo(HttpdConnData *connData) { if (connData->conn == NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. - os_sprintf(buff, + os_sprintf(buff, "{ " "\"syslog_host\": \"%s\", " "\"syslog_minheap\": %d, " @@ -118,7 +118,7 @@ int ICACHE_FLASH_ATTR cgiServicesInfo(HttpdConnData *connData) { "\"sntp_server\": \"%s\", " "\"mdns_enable\": \"%s\", " "\"mdns_servername\": \"%s\"" - " }", + " }", flashConfig.syslog_host, flashConfig.syslog_minheap, flashConfig.syslog_filter, @@ -168,7 +168,7 @@ int ICACHE_FLASH_ATTR cgiServicesSet(HttpdConnData *connData) { int8_t mdns = 0; mdns |= getBoolArg(connData, "mdns_enable", &flashConfig.mdns_enable); if (mdns < 0) return HTTPD_CGI_DONE; - + if (mdns > 0) { if (flashConfig.mdns_enable){ DBG("Services: MDNS Enabled\n"); diff --git a/esp-link/config.h b/esp-link/config.h index 59d5f3d..c3da742 100644 --- a/esp-link/config.h +++ b/esp-link/config.h @@ -13,7 +13,7 @@ typedef struct { char hostname[32]; // if using DHCP uint32_t staticip, netmask, gateway; // using DHCP if staticip==0 uint8_t log_mode; // UART log debug mode - int8_t swap_uart; // swap uart0 to gpio 13&15 + uint8_t swap_uart; // swap uart0 to gpio 13&15 uint8_t tcp_enable, rssi_enable; // TCP client settings char api_key[48]; // RSSI submission API key (Grovestreams for now) uint8_t slip_enable, mqtt_enable, // SLIP protocol, MQTT client @@ -21,9 +21,9 @@ typedef struct { mqtt_timeout, // MQTT send timeout mqtt_clean_session; // MQTT clean session uint16_t mqtt_port, mqtt_keepalive; // MQTT Host port, MQTT Keepalive timer - char mqtt_host[32], - mqtt_clientid[48], - mqtt_username[32], + char mqtt_host[32], + mqtt_clientid[48], + mqtt_username[32], mqtt_password[32], mqtt_status_topic[32]; char sys_descr[129]; // system description @@ -35,7 +35,7 @@ typedef struct { syslog_showtick, // show system tick (µs) syslog_showdate; // populate SYSLOG date field uint8_t mdns_enable; - char mdns_servername[32]; + char mdns_servername[32]; int8_t timezone_offset; } FlashConfig; extern FlashConfig flashConfig; diff --git a/esp-link/main.c b/esp-link/main.c index 4f7de35..d38ad24 100644 --- a/esp-link/main.c +++ b/esp-link/main.c @@ -73,7 +73,7 @@ HttpdBuiltInUrl builtInUrls[] = { { "/wifi/setmode", cgiWiFiSetMode, NULL }, { "/wifi/special", cgiWiFiSpecial, NULL }, { "/wifi/apinfo", cgiApSettingsInfo, NULL }, - { "/wifi/apchange", cgiApSettingsChange, NULL }, + { "/wifi/apchange", cgiApSettingsChange, NULL }, { "/system/info", cgiSystemInfo, NULL }, { "/system/update", cgiSystemSet, NULL }, { "/services/info", cgiServicesInfo, NULL }, @@ -81,7 +81,7 @@ HttpdBuiltInUrl builtInUrls[] = { { "/pins", cgiPins, NULL }, #ifdef MQTT { "/mqtt", cgiMqtt, NULL }, -#endif +#endif { "*", cgiEspFsHook, NULL }, //Catch-all cgi function for the filesystem { NULL, NULL, NULL } }; @@ -149,10 +149,10 @@ void user_init(void) { rst_info->exccause, rst_info->epc1, rst_info->epc2, rst_info->epc3, rst_info->excvaddr, rst_info->depc); uint32_t fid = spi_flash_get_id(); - NOTICE("Flash map %s, manuf 0x%02lX chip 0x%04lX", flash_maps[system_get_flash_size_map()], + 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"); - + // Init SNTP service cgiServicesSNTPInit(); #ifdef MQTT diff --git a/mqtt/mqtt_cmd.c b/mqtt/mqtt_cmd.c index 05f277f..71c5d20 100644 --- a/mqtt/mqtt_cmd.c +++ b/mqtt/mqtt_cmd.c @@ -42,7 +42,7 @@ cmdMqttDataCb(MQTT_Client* client, const char* topic, uint32_t topic_len, const char* data, uint32_t data_len) { MqttCmdCb* cb = (MqttCmdCb*)client->user_data; - DBG("MQTT: Data cb=%p topic=%s len=%ld\n", (void*)cb->dataCb, topic, data_len); + DBG("MQTT: Data cb=%p topic=%s len=%u\n", (void*)cb->dataCb, topic, data_len); cmdResponseStart(CMD_RESP_CB, cb->dataCb, 2); cmdResponseBody(topic, topic_len); @@ -168,7 +168,7 @@ MQTTCMD_Subscribe(CmdPacket *cmd) { uint32_t qos = 0; cmdPopArg(&req, (uint8_t*)&qos, 4); - DBG("MQTT: MQTTCMD_Subscribe topic=%s, qos=%ld\n", topic, qos); + DBG("MQTT: MQTTCMD_Subscribe topic=%s, qos=%u\n", topic, qos); MQTT_Subscribe(client, (char*)topic, (uint8_t)qos); os_free(topic); @@ -245,7 +245,7 @@ MQTTCMD_Setup(CmdPacket *cmd) { cmdPopArg(&req, &callback->dataCb, 4); client->user_data = callback; - DBG("MQTT connectedCb=%lx\n", callback->connectedCb); + DBG("MQTT connectedCb=%x\n", callback->connectedCb); client->cmdConnectedCb = cmdMqttConnectedCb; client->cmdDisconnectedCb = cmdMqttDisconnectedCb; diff --git a/rest/rest.c b/rest/rest.c index 81e296a..67c0f7d 100644 --- a/rest/rest.c +++ b/rest/rest.c @@ -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=%ld security=%ld\n", clientNum, rest_host, port, security); + DBG_REST("REST: setup #%d host=%s port=%d security=%d\n", clientNum, rest_host, port, security); client->resp_cb = cmd->value; @@ -348,7 +348,7 @@ REST_Request(CmdPacket *cmd) { // Get client uint32_t clientNum = cmd->value; RestClient *client = restClient + (clientNum % MAX_REST); - DBG_REST(" #%ld", clientNum); + DBG_REST(" #%d", clientNum); // Get HTTP method uint16_t len = cmdArgLen(&req); @@ -374,7 +374,7 @@ REST_Request(CmdPacket *cmd) { realLen = cmdArgLen(&req); if (realLen > 2048) goto fail; } - DBG_REST(" bodyLen=%ld", realLen); + DBG_REST(" bodyLen=%d", 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 @@ -393,7 +393,7 @@ REST_Request(CmdPacket *cmd) { if (client->data) os_free(client->data); client->data = (char*)os_zalloc(headerLen + realLen); if (client->data == NULL) goto fail; - DBG_REST(" totLen=%ld data=%p", headerLen + realLen, client->data); + DBG_REST(" totLen=%d 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); @@ -412,7 +412,7 @@ REST_Request(CmdPacket *cmd) { 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:%ld\n",client->host, client->port); + DBG_REST("REST: Connect to ip %s:%d\n",client->host, client->port); //if(client->security){ // espconn_secure_connect(client->pCon); //} @@ -420,7 +420,7 @@ REST_Request(CmdPacket *cmd) { espconn_connect(client->pCon); //} } else { - DBG_REST("REST: Connect to host %s:%ld\n", client->host, client->port); + DBG_REST("REST: Connect to host %s:%d\n", client->host, client->port); espconn_gethostbyname(client->pCon, (char *)client->host, &client->ip, rest_dns_found); } diff --git a/serial/console.c b/serial/console.c index 68b5b55..44e860a 100644 --- a/serial/console.c +++ b/serial/console.c @@ -76,7 +76,7 @@ ajaxConsoleBaud(HttpdConnData *connData) { } jsonHeader(connData, status); - os_sprintf(buff, "{\"rate\": %ld}", flashConfig.baud_rate); + os_sprintf(buff, "{\"rate\": %d}", flashConfig.baud_rate); httpdSend(connData, buff, -1); return HTTPD_CGI_DONE; } @@ -86,14 +86,14 @@ ajaxConsoleSend(HttpdConnData *connData) { if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. char buff[2048]; int len, status = 400; - + // figure out where to start in buffer based on URI param len = httpdFindArg(connData->getArgs, "text", buff, sizeof(buff)); if (len > 0) { uart0_tx_buffer(buff, len); status = 200; } - + jsonHeader(connData, status); return HTTPD_CGI_DONE; } diff --git a/syslog/syslog.c b/syslog/syslog.c index 207b678..f187114 100644 --- a/syslog/syslog.c +++ b/syslog/syslog.c @@ -406,16 +406,16 @@ syslog_compose(uint8_t facility, uint8_t severity, const char *tag, const char * 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, ".%06luZ ", se->tick % 1000000); + p += os_sprintf(p, ".%06uZ ", 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 %lu.%06lu %lu ", flashConfig.hostname, tag, se->tick / 1000000, se->tick % 1000000, syslog_msgid++); + p += os_sprintf(p, "%s %s %u.%06u %u ", flashConfig.hostname, tag, se->tick / 1000000, se->tick % 1000000, syslog_msgid++); else - p += os_sprintf(p, "%s %s - %lu ", flashConfig.hostname, tag, syslog_msgid++); + p += os_sprintf(p, "%s %s - %u ", flashConfig.hostname, tag, syslog_msgid++); // append syslog message va_list arglist; From b408103b4cdf9504bafefaa978114ad4e52e2ad5 Mon Sep 17 00:00:00 2001 From: Alastair D'Silva Date: Fri, 1 Apr 2016 19:56:18 +1100 Subject: [PATCH 07/20] Use the SKD included in esp-open-sdk, if available --- Makefile | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index cacdc65..51ac65f 100644 --- a/Makefile +++ b/Makefile @@ -55,8 +55,15 @@ ESP_HOSTNAME ?= esp-link XTENSA_TOOLS_ROOT ?= $(abspath ../esp-open-sdk/xtensa-lx106-elf/bin)/ # Base directory of the ESP8266 SDK package, absolute -# Typically you'll download from Espressif's BBS, http://bbs.espressif.com/viewforum.php?f=5 +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 +endif # Esptool.py path and port, only used for 1-time serial flashing # Typically you'll use https://github.com/themadinventor/esptool From b56b2767cba5695076ce0b1369f331e1b17b12eb Mon Sep 17 00:00:00 2001 From: Nico Date: Thu, 31 Mar 2016 15:05:23 +0200 Subject: [PATCH 08/20] Update README.adoc --- README.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.adoc b/README.adoc index aaf1d35..737979c 100644 --- a/README.adoc +++ b/README.adoc @@ -20,7 +20,7 @@ The firmware includes a tiny HTTP server based on http://www.esp8266.com/viewforum.php?f=34[esphttpd] with a simple web interface, many thanks to Jeroen Domburg for making it available! The REST and MQTT functionality are loosely based on https://github.com/tuanpmt/espduino -but significantly reqritten and no longer protocol compatible, thanks to tuanpmt for the +but significantly rewritten and no longer protocol compatible, thanks to tuanpmt for the inspiration! Many thanks to https://github.com/brunnels for contributions in particular around the espduino From e3e19ffa387ff0f68fb5c352d7af06ae13f7b079 Mon Sep 17 00:00:00 2001 From: susisstrolch Date: Sat, 2 Apr 2016 06:54:24 +0200 Subject: [PATCH 09/20] TvEmaster Syslog and debug printing fixes --- Makefile | 38 ++++++++++++++-------- cmd/handlers.c | 2 +- esp-link/cgi.c | 2 +- esp-link/cgiflash.c | 2 +- esp-link/cgimqtt.c | 6 ++-- esp-link/cgioptiboot.c | 2 +- esp-link/cgiservices.c | 2 +- esp-link/cgiwifi.c | 73 +++++++++++++++++++++++++++--------------- esp-link/log.c | 2 +- esp-link/main.c | 2 +- esp-link/mqtt_client.c | 2 +- esp-link/task.c | 11 +++---- httpd/httpd.c | 2 +- mqtt/mqtt.c | 56 ++++++++++++++++---------------- mqtt/mqtt_cmd.c | 2 +- rest/rest.c | 52 +++++++++++++++--------------- serial/slip.c | 2 +- serial/uart.c | 8 ++--- syslog/syslog.c | 67 +++++++++++++++++++++++--------------- user/user_main.c | 10 +++++- 20 files changed, 198 insertions(+), 145 deletions(-) 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() { From 5eba5500accd8260005395a01ea85d17a63dcc07 Mon Sep 17 00:00:00 2001 From: Jason Schmidlapp Date: Fri, 1 Apr 2016 23:01:16 -0600 Subject: [PATCH 10/20] Don't reset programming target until actual image data is received from network. Thanks! --- serial/serbridge.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/serial/serbridge.c b/serial/serbridge.c index 1e82e3d..a471f05 100644 --- a/serial/serbridge.c +++ b/serial/serbridge.c @@ -177,8 +177,11 @@ serbridgeRecvCb(void *arg, char *data, unsigned short len) (len == 2 && strncmp(data, "?\n", 2) == 0) || (len == 3 && strncmp(data, "?\r\n", 3) == 0)) { startPGM = true; - conn->conn_mode = cmPGM; + // Don't actually reboot the target until we've actually received + // serial data to send to the target. + conn->conn_mode = cmPGMInit; + return; // If the connection starts with a telnet negotiation we will do telnet } else if (len >= 3 && strncmp(data, (char[]){IAC, WILL, ComPortOpt}, 3) == 0) { From 56e76c6bc42b5a078ae727892cef5b0159e7b053 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sat, 2 Apr 2016 10:30:15 -0700 Subject: [PATCH 11/20] Revert "TvEmaster" This reverts commit f577c140df21e4ff29ec62e6a142921adbf7fc64. --- Makefile | 16 ++++----- cmd/handlers.c | 2 +- esp-link/cgi.c | 2 +- esp-link/cgiflash.c | 2 +- esp-link/cgimqtt.c | 6 ++-- esp-link/cgioptiboot.c | 2 +- esp-link/cgiservices.c | 12 +++---- esp-link/cgiwifi.c | 73 +++++++++++++++--------------------------- esp-link/log.c | 2 +- esp-link/main.c | 6 ++-- esp-link/mqtt_client.c | 2 +- esp-link/task.c | 11 ++++--- httpd/httpd.c | 2 +- mqtt/mqtt.c | 56 ++++++++++++++++---------------- mqtt/mqtt_cmd.c | 2 +- rest/rest.c | 52 +++++++++++++++--------------- serial/slip.c | 2 +- serial/uart.c | 8 ++--- syslog/syslog.c | 62 ++++++++++++++--------------------- user/user_main.c | 10 +----- 20 files changed, 144 insertions(+), 186 deletions(-) diff --git a/Makefile b/Makefile index 844e00f..523f85f 100644 --- a/Makefile +++ b/Makefile @@ -13,9 +13,6 @@ # `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 @@ -57,7 +54,9 @@ 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)/ -# Firmware version (if you change this expect to make code adjustments elsewhere!) +# Firmware version +# WARNING: if you change this expect to make code adjustments elsewhere, don't expect +# that esp-link will magically work with a different version of the SDK!!! SDK_VERS ?= esp_iot_sdk_v1.5.2 # Try to find the firmware manually extracted, e.g. after downloading from Espressif's BBS, @@ -206,6 +205,8 @@ 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 @@ -342,7 +343,6 @@ 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 $@" @@ -399,7 +399,7 @@ baseflash: all flash: all $(Q) $(ESPTOOL) --port $(ESPPORT) --baud $(ESPBAUD) write_flash -fs $(ET_FS) -ff $(ET_FF) \ - 0x00000 "$(BOOTFILE)" 0x01000 $(FW_BASE)/user1.bin \ + 0x00000 "$(SDK_BASE)/bin/boot_v1.5.bin" 0x01000 $(FW_BASE)/user1.bin \ $(ET_BLANK) $(SDK_BASE)/bin/blank.bin ifeq ($(OS),Windows_NT) @@ -446,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 @@ -481,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 \ - "$(BOOTFILE)" wiflash avrflash release/esp-link-$(BRANCH) + "$(SDK_BASE)/bin/boot_v1.5.bin" 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 b62894b..061526a 100644 --- a/cmd/handlers.c +++ b/cmd/handlers.c @@ -13,7 +13,7 @@ #endif #ifdef CMD_DBG -#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) +#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) #else #define DBG(format, ...) do { } while(0) #endif diff --git a/esp-link/cgi.c b/esp-link/cgi.c index c42d769..96c03e9 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, ...) os_printf(format, ## __VA_ARGS__) +#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) #else #define DBG(format, ...) do { } while(0) #endif diff --git a/esp-link/cgiflash.c b/esp-link/cgiflash.c index de49094..4b9277d 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, ...) os_printf(format, ## __VA_ARGS__) +#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) #else #define DBG(format, ...) do { } while(0) #endif diff --git a/esp-link/cgimqtt.c b/esp-link/cgimqtt.c index 09d6ce0..88aa6ed 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, ...) os_printf(format, ## __VA_ARGS__) +#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) #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 d1dca68..ed87d0e 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, ...) os_printf(format, ## __VA_ARGS__) +#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) #else #define DBG(format, ...) do { } while(0) #endif diff --git a/esp-link/cgiservices.c b/esp-link/cgiservices.c index d9b3508..6b3f553 100644 --- a/esp-link/cgiservices.c +++ b/esp-link/cgiservices.c @@ -7,7 +7,7 @@ #include "cgimqtt.h" #ifdef CGISERVICES_DBG -#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) +#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) #else #define DBG(format, ...) do { } while(0) #endif @@ -92,10 +92,10 @@ int ICACHE_FLASH_ATTR cgiSystemInfo(HttpdConnData *connData) { } void ICACHE_FLASH_ATTR cgiServicesSNTPInit() { - if (flashConfig.sntp_server[0] != '\0') { + if (flashConfig.sntp_server[0] != '\0') { sntp_stop(); if (true == sntp_set_timezone(flashConfig.timezone_offset)) { - sntp_setservername(0, flashConfig.sntp_server); + sntp_setservername(0, flashConfig.sntp_server); sntp_init(); } DBG("SNTP timesource set to %s with offset %d\n", flashConfig.sntp_server, flashConfig.timezone_offset); @@ -107,7 +107,7 @@ int ICACHE_FLASH_ATTR cgiServicesInfo(HttpdConnData *connData) { if (connData->conn == NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. - os_sprintf(buff, + os_sprintf(buff, "{ " "\"syslog_host\": \"%s\", " "\"syslog_minheap\": %d, " @@ -118,7 +118,7 @@ int ICACHE_FLASH_ATTR cgiServicesInfo(HttpdConnData *connData) { "\"sntp_server\": \"%s\", " "\"mdns_enable\": \"%s\", " "\"mdns_servername\": \"%s\"" - " }", + " }", flashConfig.syslog_host, flashConfig.syslog_minheap, flashConfig.syslog_filter, @@ -168,7 +168,7 @@ int ICACHE_FLASH_ATTR cgiServicesSet(HttpdConnData *connData) { int8_t mdns = 0; mdns |= getBoolArg(connData, "mdns_enable", &flashConfig.mdns_enable); if (mdns < 0) return HTTPD_CGI_DONE; - + if (mdns > 0) { if (flashConfig.mdns_enable){ DBG("Services: MDNS Enabled\n"); diff --git a/esp-link/cgiwifi.c b/esp-link/cgiwifi.c index 0971dbc..50ea536 100755 --- a/esp-link/cgiwifi.c +++ b/esp-link/cgiwifi.c @@ -19,34 +19,13 @@ 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, ...) os_printf(format, ## __VA_ARGS__) +#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) #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) @@ -55,7 +34,7 @@ bool mdns_started = false; // ===== wifi status change callbacks static WifiStateChangeCb wifi_state_change_cb[4]; -// Temp store for new station config +// Temp store for new staion config struct station_config stconf; // Temp store for new ap config @@ -89,25 +68,25 @@ static void ICACHE_FLASH_ATTR wifiHandleEventCb(System_Event_t *evt) { case EVENT_STAMODE_CONNECTED: wifiState = wifiIsConnected; wifiReason = 0; - NOTICE("connected to ssid %s, ch %d", evt->event_info.connected.ssid, + DBG("Wifi connected to ssid %s, ch %d\n", 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; - WARNING("disconnected from ssid %s, reason %s (%d)", + DBG("Wifi disconnected from ssid %s, reason %s (%d)\n", evt->event_info.disconnected.ssid, wifiGetReason(), evt->event_info.disconnected.reason); statusWifiUpdate(wifiState); break; case EVENT_STAMODE_AUTHMODE_CHANGE: - NOTICE("auth mode: %d -> %d", + DBG("Wifi auth mode: %d -> %d\n", evt->event_info.auth_change.old_mode, evt->event_info.auth_change.new_mode); break; case EVENT_STAMODE_GOT_IP: wifiState = wifiGotIP; wifiReason = 0; - NOTICE("got ip:" IPSTR ",mask:" IPSTR ",gw:" IPSTR, + DBG("Wifi got ip:" IPSTR ",mask:" IPSTR ",gw:" IPSTR "\n", IP2STR(&evt->event_info.got_ip.ip), IP2STR(&evt->event_info.got_ip.mask), IP2STR(&evt->event_info.got_ip.gw)); statusWifiUpdate(wifiState); @@ -115,11 +94,11 @@ static void ICACHE_FLASH_ATTR wifiHandleEventCb(System_Event_t *evt) { wifiStartMDNS(evt->event_info.got_ip.ip); break; case EVENT_SOFTAPMODE_STACONNECTED: - NOTICE("AP: station " MACSTR " joined, AID = %d", + DBG("Wifi AP: station " MACSTR " joined, AID = %d\n", MAC2STR(evt->event_info.sta_connected.mac), evt->event_info.sta_connected.aid); break; case EVENT_SOFTAPMODE_STADISCONNECTED: - NOTICE("AP: station " MACSTR " left, AID = %d", + DBG("Wifi AP: station " MACSTR " left, AID = %d\n", MAC2STR(evt->event_info.sta_disconnected.mac), evt->event_info.sta_disconnected.aid); break; default: @@ -139,7 +118,7 @@ void ICACHE_FLASH_ATTR wifiAddStateChangeCb(WifiStateChangeCb cb) { return; } } - WARNING("max state change cb count exceeded"); + DBG("WIFI: max state change cb count exceeded\n"); } void ICACHE_FLASH_ATTR wifiStartMDNS(struct ip_addr ip) { @@ -184,7 +163,7 @@ void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) { struct bss_info *bss_link = (struct bss_info *)arg; if (status!=OK) { - WARNING("wifiScanDoneCb status=%d", status); + DBG("wifiScanDoneCb status=%d\n", status); cgiWifiAps.scanInProgress=0; return; } @@ -204,7 +183,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; - INFO("Scan done: found %d APs", n); + DBG("Scan done: found %d APs\n", n); //Copy access point data to the static struct n=0; @@ -213,7 +192,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. - WARNING("Huh? I have more than the allocated %d aps!", cgiWifiAps.noAps); + DBG("Huh? I have more than the allocated %d aps!\n", cgiWifiAps.noAps); break; } //Save the ap data. @@ -221,7 +200,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); - INFO("bss%d: %s (%d)", n+1, (char*)bss_link->ssid, bss_link->rssi); + DBG("bss%d: %s (%d)\n", n+1, (char*)bss_link->ssid, bss_link->rssi); bss_link = bss_link->next.stqe_next; n++; @@ -232,7 +211,7 @@ void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) { static ETSTimer scanTimer; static void ICACHE_FLASH_ATTR scanStartCb(void *arg) { - INFO("Starting a scan"); + DBG("Starting a scan\n"); wifi_station_scan(NULL, wifiScanDoneCb); } @@ -324,28 +303,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; - NOTICE("check: mode=%s status=%d", wifiMode[m], x); + DBG("Wifi check: mode=%s status=%d\n", 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 - NOTICE("got IP. Going into STA mode.."); + DBG("Wifi got IP. Going into STA mode..\n"); 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) { - NOTICE("connect failed. Going into STA+AP mode.."); - wifi_set_opmode(3); - wifi_softap_set_config(&apconf); + } else { + if (m != 3) { + DBG("Wifi connect failed. Going into STA+AP mode..\n"); + wifi_set_opmode(3); + wifi_softap_set_config(&apconf); } log_uart(true); - INFO("Enabling/continuing uart log"); + DBG("Enabling/continuing uart log\n"); os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); } } @@ -356,7 +335,7 @@ static ETSTimer reassTimer; // Callback actually doing reassociation static void ICACHE_FLASH_ATTR reassTimerCb(void *arg) { - NOTICE("changing association"); + DBG("Wifi changing association\n"); wifi_station_disconnect(); stconf.bssid_set = 0; wifi_station_set_config(&stconf); @@ -390,7 +369,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); - NOTICE("try to connect to AP %s pw %s", essid, passwd); + DBG("Wifi try to connect to AP %s pw %s\n", essid, passwd); //Schedule disconnect/connect os_timer_disarm(&reassTimer); @@ -451,7 +430,7 @@ void ICACHE_FLASH_ATTR configWifiIP() { if (wifi_station_dhcpc_status() == DHCP_STARTED) wifi_station_dhcpc_stop(); wifi_station_dhcpc_start(); - NOTICE("uses DHCP, hostname=%s", flashConfig.hostname); + DBG("Wifi uses DHCP, hostname=%s\n", flashConfig.hostname); } else { // no DHCP, we got static network config! wifi_station_dhcpc_stop(); @@ -460,7 +439,7 @@ void ICACHE_FLASH_ATTR configWifiIP() { ipi.netmask.addr = flashConfig.netmask; ipi.gw.addr = flashConfig.gateway; wifi_set_ip_info(0, &ipi); - NOTICE("uses static IP %d.%d.%d.%d", IP2STR(&ipi.ip.addr)); + DBG("Wifi uses static IP %d.%d.%d.%d\n", IP2STR(&ipi.ip.addr)); } #ifdef DEBUGIP debugIP(); diff --git a/esp-link/log.c b/esp-link/log.c index d6c245b..821641f 100644 --- a/esp-link/log.c +++ b/esp-link/log.c @@ -7,7 +7,7 @@ #include "log.h" #ifdef LOG_DBG -#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) +#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) #else #define DBG(format, ...) do { } while(0) #endif diff --git a/esp-link/main.c b/esp-link/main.c index bf8c6ef..b386338 100644 --- a/esp-link/main.c +++ b/esp-link/main.c @@ -81,7 +81,7 @@ HttpdBuiltInUrl builtInUrls[] = { { "/pins", cgiPins, NULL }, #ifdef MQTT { "/mqtt", cgiMqtt, NULL }, -#endif +#endif { "*", cgiEspFsHook, NULL }, //Catch-all cgi function for the filesystem { NULL, NULL, NULL } }; @@ -151,8 +151,8 @@ 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("** %s: ready, heap=%ld", esp_link_version, (unsigned long)system_get_free_heap_size()); - + NOTICE("** esp-link ready"); + // Init SNTP service cgiServicesSNTPInit(); #ifdef MQTT diff --git a/esp-link/mqtt_client.c b/esp-link/mqtt_client.c index 3d390bb..f0d9108 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, ...) os_printf(format, ## __VA_ARGS__) +#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__) } while(0) #else #define DBG(format, ...) do { } while(0) #endif diff --git a/esp-link/task.c b/esp-link/task.c index a780534..879a694 100644 --- a/esp-link/task.c +++ b/esp-link/task.c @@ -16,18 +16,19 @@ #define MAXUSRTASKS 8 #ifdef USRTASK_DBG -#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) +#define DBG_USRTASK(format, ...) os_printf(format, ## __VA_ARGS__) #else -#define DBG(format, ...) do { } while(0) +#define DBG_USRTASK(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("usr_event_handler: event %p (sig=%d, par=%p)\n", e, (int)e->sig, (void *)e->par); + DBG_USRTASK("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"); @@ -56,7 +57,7 @@ uint8_t register_usr_task (os_task_t event) { int task; - DBG("register_usr_task: %p\n", event); + DBG_USRTASK("register_usr_task: %p\n", event); if (_task_queue == NULL || usr_task_queue == NULL) init_usr_task(); @@ -67,7 +68,7 @@ uint8_t register_usr_task (os_task_t event) for (task = 0; task < MAXUSRTASKS; task++) { if (usr_task_queue[task] == NULL) { - DBG("register_usr_task: assign task #%d\n", task); + DBG_USRTASK("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 d6f0097..5590809 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, ...)os_printf(format, ## __VA_ARGS__) +#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) #else #define DBG(format, ...) do { } while(0) #endif diff --git a/mqtt/mqtt.c b/mqtt/mqtt.c index 81734e1..30e2dd0 100644 --- a/mqtt/mqtt.c +++ b/mqtt/mqtt.c @@ -42,9 +42,9 @@ #include "mqtt.h" #ifdef MQTT_DBG -#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) +#define DBG_MQTT(format, ...) os_printf(format, ## __VA_ARGS__) #else -#define DBG(format, ...) do { } while(0) +#define DBG_MQTT(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 ERROR: recv in invalid state %d\n", client->connState); + DBG_MQTT("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: Recv type=%s id=%04X len=%d; Pend type=%s id=%02X\n", + DBG_MQTT("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: Connect successful\n"); + //DBG_MQTT("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: Subscribe successful\n"); + //DBG_MQTT("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: Unsubscribe successful\n"); + //DBG_MQTT("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: QoS1 Publish successful\n"); + //DBG_MQTT("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: QoS2 publish cont\n"); + //DBG_MQTT("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: QoS2 Publish successful\n"); + //DBG_MQTT("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: Cont QoS2 recv\n"); + //DBG_MQTT("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: sent CB\n"); + //DBG_MQTT("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: Sent\n"); + //DBG_MQTT("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("PktBuf free %p l=%d\n", buf, buf->filled); + //DBG_MQTT("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: timer CB\n"); + //DBG_MQTT("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: Send keepalive\n"); + //DBG_MQTT("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: Disconnect CB, freeing espconn %p\n", arg); + DBG_MQTT("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: Disconnected from %s:%d\n", client->host, client->port); + DBG_MQTT("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: Reset CB, freeing espconn %p (err=%d)\n", arg, err); + //DBG_MQTT("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: Send_message\n"); + //DBG_MQTT("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: ip %d.%d.%d.%d\n", + DBG_MQTT("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: connecting...\n"); + DBG_MQTT("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: Publish, topic: \"%s\", length: %d\n", topic, msg.message.length); + DBG_MQTT("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: Subscribe, topic: \"%s\"\n", topic); + DBG_MQTT("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_Init\n"); + DBG_MQTT("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: Reconnect requested\n"); + DBG_MQTT("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: Disconnect requested\n"); + DBG_MQTT("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: Free requested\n"); + DBG_MQTT("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 8c1611d..71c5d20 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, ...) os_printf(format, ## __VA_ARGS__) +#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) #else #define DBG(format, ...) do { } while(0) #endif diff --git a/rest/rest.c b/rest/rest.c index d98d095..fd116a1 100644 --- a/rest/rest.c +++ b/rest/rest.c @@ -9,9 +9,9 @@ #include "cmd.h" #ifdef REST_DBG -#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) +#define DBG_REST(format, ...) os_printf(format, ## __VA_ARGS__) #else -#define DBG(format, ...) do { } while(0) +#define DBG_REST(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: status=%d, body=%d\n", code, body_len); + DBG_REST("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: Sent\n"); + DBG_REST("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 #%d: connected\n", client-restClient); + DBG_REST("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 #%d: sending %d\n", client-restClient, client->data_sent); + DBG_REST("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 DNS: found ip %d.%d.%d.%d\n", + DBG_REST("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: connecting...\n"); + DBG_REST("REST: connecting...\n"); } } @@ -252,7 +252,7 @@ REST_Setup(CmdPacket *cmd) { os_free(client->pCon); } os_memset(client, 0, sizeof(RestClient)); - DBG("REST: setup #%d host=%s port=%d security=%d\n", clientNum, rest_host, port, security); + DBG_REST("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: Set header: %s\r\n", client->header); + DBG_REST("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: Set content_type: %s\r\n", client->content_type); + DBG_REST("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: Set user_agent: %s\r\n", client->user_agent); + DBG_REST("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: request"); + DBG_REST("REST: request"); if (cmd->argc != 2 && cmd->argc != 3) return; // Get client uint32_t clientNum = cmd->value; RestClient *client = restClient + (clientNum % MAX_REST); - DBG(" #%d", clientNum); + DBG_REST(" #%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(" method=%s", method); + DBG_REST(" 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(" path=%s", path); + DBG_REST(" 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(" bodyLen=%ld", realLen); + DBG_REST(" 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(" hdrLen=%d", headerLen); + DBG_REST(" hdrLen=%d", headerLen); if (client->data) os_free(client->data); client->data = (char*)os_zalloc(headerLen + realLen); if (client->data == NULL) goto fail; - DBG(" totLen=%ld data=%p", headerLen + realLen, client->data); + DBG_REST(" 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(" hdrLen=%d", client->data_len); + DBG_REST(" hdrLen=%d", client->data_len); if (realLen > 0) { cmdPopArg(&req, client->data + client->data_len, realLen); client->data_len += realLen; } - DBG("\n"); + DBG_REST("\n"); - //DBG("REST request: %s", (char*)client->data); + //DBG_REST("REST request: %s", (char*)client->data); - //DBG("REST: pCon state=%d\n", client->pCon->state); + //DBG_REST("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: Connect to ip %s:%ld\n",client->host, client->port); + DBG_REST("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: Connect to host %s:%ld\n", client->host, client->port); + DBG_REST("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("\n"); + DBG_REST("\n"); } diff --git a/serial/slip.c b/serial/slip.c index 6cc418e..76d8f97 100644 --- a/serial/slip.c +++ b/serial/slip.c @@ -8,7 +8,7 @@ #include "cmd.h" #ifdef SLIP_DBG -#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) +#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) #else #define DBG(format, ...) do { } while(0) #endif diff --git a/serial/uart.c b/serial/uart.c index febcc1f..f7f07fa 100644 --- a/serial/uart.c +++ b/serial/uart.c @@ -22,9 +22,9 @@ #include "uart.h" #ifdef UART_DBG -#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) +#define DBG_UART(format, ...) os_printf(format, ## __VA_ARGS__) #else -#define DBG(format, ...) do { } while(0) +#define DBG_UART(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("stat:%02X",*(uint8 *)UART_INT_ENA(uart_no)); + //DBG_UART("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("%d ix %d\n", system_get_time(), length); + //DBG_UART("%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,14 +234,11 @@ 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; - // espconn not longer required - os_free(pespconn->proto.udp); - os_free(pespconn); + (void) 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; @@ -325,18 +322,16 @@ 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); - espconn_gethostbyname(syslog_dnsconn, host, &syslogHost.addr, syslog_gethostbyname_cb); + syslog(SYSLOG_FAC_USER, SYSLOG_PRIO_NOTICE, "SYSLOG", + "must resolve hostname \"%s\"", host); + espconn_gethostbyname(syslog_espconn, host, &syslogHost.addr, syslog_gethostbyname_cb); } } @@ -360,15 +355,13 @@ syslog_add_entry(syslog_entry_t *entry) pse = pse->next; pse->next = entry; // append msg to syslog queue } - // 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); +// DBG("%p %lu %d\n", entry, entry->msgid, system_get_free_heap_size()); // 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 (%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); + 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"); if (syslogState == SYSLOG_READY) syslog_send_udp(); syslog_set_status(SYSLOG_HALTED); @@ -385,11 +378,6 @@ 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; @@ -411,22 +399,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) ? (sl.se.tick / 1000000) : realtime_stamp; + now = (realtime_stamp == 0) ? (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 ", sl.se.tick % 1000000); + p += os_sprintf(p, ".%06uZ ", 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, sl.se.tick / 1000000, - sl.se.tick % 1000000, syslog_msgid++); + p += os_sprintf(p, "%s %s %lu.%06lu %lu ", flashConfig.hostname, tag, se->tick / 1000000, + se->tick % 1000000, syslog_msgid++); else p += os_sprintf(p, "%s %s - %u ", flashConfig.hostname, tag, syslog_msgid++); @@ -436,9 +424,8 @@ syslog_compose(uint8_t facility, uint8_t severity, const char *tag, const char * p += ets_vsprintf(p, fmt, arglist ); va_end(arglist); - 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); + se->datagram_len = p - se->datagram; + se = mem_trim(se, sizeof(syslog_entry_t) + se->datagram_len + 1); return se; } @@ -518,8 +505,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 || - flashConfig.syslog_host[0] == '\0') + syslogState == SYSLOG_HALTED) return; if (severity > flashConfig.syslog_filter) diff --git a/user/user_main.c b/user/user_main.c index fc32899..5a4331d 100644 --- a/user/user_main.c +++ b/user/user_main.c @@ -1,13 +1,5 @@ #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 +#include // initialize the custom stuff that goes beyond esp-link void app_init() { From 0df5264c8bdd7dee55dfa974a445b92ecef9dab8 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sat, 2 Apr 2016 11:09:06 -0700 Subject: [PATCH 12/20] revert PR #95 due to bug; revert #114 due to timing issues; fix MQTT crashes --- mqtt/mqtt.c | 10 ++++++---- serial/serbridge.c | 5 +---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/mqtt/mqtt.c b/mqtt/mqtt.c index 30e2dd0..33fc972 100644 --- a/mqtt/mqtt.c +++ b/mqtt/mqtt.c @@ -497,9 +497,11 @@ mqtt_dns_found(const char* name, ip_addr_t* ipaddr, void* arg) { if (ipaddr == NULL) { os_printf("MQTT: DNS lookup failed\n"); - client->timeoutTick = client->reconTimeout; - if (client->reconTimeout < 128) client->reconTimeout <<= 1; - client->connState = TCP_RECONNECT_REQ; // the timer will kick-off a reconnection + if (client != NULL) { + client->timeoutTick = client->reconTimeout; + if (client->reconTimeout < 128) client->reconTimeout <<= 1; + client->connState = TCP_RECONNECT_REQ; // the timer will kick-off a reconnection + } return; } DBG_MQTT("MQTT: ip %d.%d.%d.%d\n", @@ -508,7 +510,7 @@ mqtt_dns_found(const char* name, ip_addr_t* ipaddr, void* arg) { *((uint8 *)&ipaddr->addr + 2), *((uint8 *)&ipaddr->addr + 3)); - if (client->ip.addr == 0 && ipaddr->addr != 0) { + if (client != NULL && client->ip.addr == 0 && ipaddr->addr != 0) { os_memcpy(client->pCon->proto.tcp->remote_ip, &ipaddr->addr, 4); uint8_t err; if (client->security) diff --git a/serial/serbridge.c b/serial/serbridge.c index a471f05..1e82e3d 100644 --- a/serial/serbridge.c +++ b/serial/serbridge.c @@ -177,11 +177,8 @@ serbridgeRecvCb(void *arg, char *data, unsigned short len) (len == 2 && strncmp(data, "?\n", 2) == 0) || (len == 3 && strncmp(data, "?\r\n", 3) == 0)) { startPGM = true; + conn->conn_mode = cmPGM; - // Don't actually reboot the target until we've actually received - // serial data to send to the target. - conn->conn_mode = cmPGMInit; - return; // If the connection starts with a telnet negotiation we will do telnet } else if (len >= 3 && strncmp(data, (char[]){IAC, WILL, ComPortOpt}, 3) == 0) { From df2f36b8ef1b25cc403031e6293896f07f612706 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sat, 2 Apr 2016 23:00:09 -0700 Subject: [PATCH 13/20] fix #126: allow short SSIDs --- esp-link/cgiwifi.c | 39 ++++++++++++++++++++------------------- html/wifi/wifiAp.js | 23 +++++++++++------------ mqtt/mqtt.c | 3 ++- 3 files changed, 33 insertions(+), 32 deletions(-) mode change 100755 => 100644 esp-link/cgiwifi.c diff --git a/esp-link/cgiwifi.c b/esp-link/cgiwifi.c old mode 100755 new mode 100644 index 50ea536..ca70554 --- a/esp-link/cgiwifi.c +++ b/esp-link/cgiwifi.c @@ -533,7 +533,7 @@ int ICACHE_FLASH_ATTR cgiApSettingsChange(HttpdConnData *connData) { } // Set new SSID len=httpdFindArg(connData->getArgs, "ap_ssid", buff, sizeof(buff)); - if(checkString(buff) && len>7 && len<32){ + if(checkString(buff) && len>0 && len<=32){ // STRING PREPROCESSING DONE IN CLIENT SIDE os_memset(apconf.ssid, 0, 32); os_memcpy(apconf.ssid, buff, len); @@ -545,13 +545,11 @@ int ICACHE_FLASH_ATTR cgiApSettingsChange(HttpdConnData *connData) { } // Set new PASSWORD len=httpdFindArg(connData->getArgs, "ap_password", buff, sizeof(buff)); - if(checkString(buff) && len>7 && len<64){ + os_memset(apconf.password, 0, 64); + if (checkString(buff) && len>7 && len<=64) { // String preprocessing done in client side, wifiap.js line 31 - os_memset(apconf.password, 0, 64); os_memcpy(apconf.password, buff, len); - }else if (len == 0){ - os_memset(apconf.password, 0, 64); - }else{ + } else if (len != 0) { jsonHeader(connData, 400); httpdSend(connData, "PASSWORD not valid or out of range", -1); return HTTPD_CGI_DONE; @@ -733,11 +731,17 @@ int ICACHE_FLASH_ATTR printWifiInfo(char *buff) { uint8_t chan = wifi_get_channel(); len = os_sprintf(buff, - "\"mode\": \"%s\", \"modechange\": \"%s\", \"ssid\": \"%s\", \"status\": \"%s\", \"phy\": \"%s\", " - "\"rssi\": \"%ddB\", \"warn\": \"%s\", \"apwarn\": \"%s\",\"mac\":\"%02x:%02x:%02x:%02x:%02x:%02x\", \"chan\":\"%d\", \"apssid\": \"%s\", " - "\"appass\": \"%s\", \"apchan\": \"%d\", \"apmaxc\": \"%d\", \"aphidd\": \"%s\", \"apbeac\": \"%d\", \"apauth\": \"%s\",\"apmac\":\"%02x:%02x:%02x:%02x:%02x:%02x\"", - mode, MODECHANGE, (char*)stconf.ssid, status, phy, rssi, warn, apwarn, - mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5], chan, (char*)apconf.ssid,(char*)apconf.password,apconf.channel,apconf.max_connection,apconf.ssid_hidden?"enabled":"disabled",apconf.beacon_interval, apauth,apmac_addr[0], apmac_addr[1], apmac_addr[2], apmac_addr[3], apmac_addr[4], apmac_addr[5]); + "\"mode\": \"%s\", \"modechange\": \"%s\", \"ssid\": \"%s\", \"status\": \"%s\", " + "\"phy\": \"%s\", \"rssi\": \"%ddB\", \"warn\": \"%s\", \"apwarn\": \"%s\", " + "\"mac\":\"%02x:%02x:%02x:%02x:%02x:%02x\", \"chan\":\"%d\", \"apssid\": \"%s\", " + "\"appass\": \"%s\", \"apchan\": \"%d\", \"apmaxc\": \"%d\", \"aphidd\": \"%s\", " + "\"apbeac\": \"%d\", \"apauth\": \"%s\",\"apmac\":\"%02x:%02x:%02x:%02x:%02x:%02x\"", + mode, MODECHANGE, (char*)stconf.ssid, status, phy, rssi, warn, apwarn, + mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5], + chan, (char*)apconf.ssid, (char*)apconf.password, apconf.channel, apconf.max_connection, + apconf.ssid_hidden?"enabled":"disabled", apconf.beacon_interval, + apauth,apmac_addr[0], apmac_addr[1], apmac_addr[2], apmac_addr[3], apmac_addr[4], + apmac_addr[5]); struct ip_info info; if (wifi_get_ip_info(0, &info)) { @@ -806,12 +810,9 @@ int ICACHE_FLASH_ATTR cgiWifiInfo(HttpdConnData *connData) { // Check string againt invalid characters int ICACHE_FLASH_ATTR checkString(char *str){ - int i = 0; - for(; i < os_strlen(str); i++) - { - // Alphanumeric and underscore allowed - if (!(isalnum((unsigned char)str[i]) || str[i] == '_')) - { + for(int i=0; i '~') { DBG("Error: String has non alphanumeric chars\n"); return 0; } @@ -855,7 +856,7 @@ void ICACHE_FLASH_ATTR wifiInit() { } #endif - // Change SOFT_AP settings if defined + // Change SOFT_AP settings if defined in Makefile #if defined(AP_SSID) // Check if ssid and pass are alphanumeric values int ssidlen = os_strlen(VERS_STR(AP_SSID)); @@ -909,7 +910,7 @@ void ICACHE_FLASH_ATTR wifiInit() { // Debug info DBG("Wifi Soft-AP parameters change: %s\n",softap_set_conf? "success":"fail"); -#endif // AP_SSID && AP_PASS +#endif // if defined(AP_SSID) configWifiIP(); diff --git a/html/wifi/wifiAp.js b/html/wifi/wifiAp.js index e2fef60..efa61f8 100644 --- a/html/wifi/wifiAp.js +++ b/html/wifi/wifiAp.js @@ -26,15 +26,14 @@ function changeApSettings(e) { if (inputs[i].type == "checkbox") { var val = (inputs[i].checked) ? 1 : 0; url += "&" + inputs[i].name + "=" + val; - } - else{ - var clean = inputs[i].value.replace(/[^\w]/gi, ""); - var comp = clean.localeCompare(inputs[i].value); - if ( comp != 0 ){ - showWarning("Invalid characters in " + specials[inputs[i].name]); - return; - } - url += "&" + inputs[i].name + "=" + clean; + } else { + var clean = inputs[i].value.replace(/[^!-~]/g, ""); + var comp = clean.localeCompare(inputs[i].value); + if ( comp != 0 ){ + showWarning("Invalid characters in " + specials[inputs[i].name]); + return; + } + url += "&" + inputs[i].name + "=" + clean; } }; @@ -72,8 +71,8 @@ function displayApSettings(data) { } else el.value = data[v]; } }); - - $("#AP_Settings-spinner").setAttribute("hidden", ""); + + $("#AP_Settings-spinner").setAttribute("hidden", ""); $("#AP_Settings-form").removeAttribute("hidden"); showWarning("Don't modify SOFTAP parameters with active connections"); window.setTimeout(hideWarning(), 2000); @@ -95,4 +94,4 @@ function undoApAdvanced(){ $("#AP_Settings-on").setAttribute("hidden", ""); $("#AP_Settings-off").removeAttribute("hidden"); $("#AP_Settings-roff").setAttribute("checked", ""); -} \ No newline at end of file +} diff --git a/mqtt/mqtt.c b/mqtt/mqtt.c index 33fc972..bf785f6 100644 --- a/mqtt/mqtt.c +++ b/mqtt/mqtt.c @@ -707,7 +707,8 @@ MQTT_Connect(MQTT_Client* client) { os_timer_arm(&client->mqttTimer, 1000, 1); // initiate the TCP connection or DNS lookup - os_printf("MQTT: Connect to %s:%d %p\n", client->host, client->port, client->pCon); + os_printf("MQTT: Connect to %s:%d %p (client=%p)\n", + client->host, client->port, client->pCon, client); if (UTILS_StrToIP((const char *)client->host, (void*)&client->pCon->proto.tcp->remote_ip)) { uint8_t err; From a770d0293dabb950289543b55f0ba5ea110cbb4c Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sat, 2 Apr 2016 23:15:26 -0700 Subject: [PATCH 14/20] fix #116: show device name in browser title --- html/ui.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/html/ui.js b/html/ui.js index 80030e3..aa66f69 100644 --- a/html/ui.js +++ b/html/ui.js @@ -273,6 +273,7 @@ onLoad(function() { var v = $("#version"); if (v != null) { v.innerHTML = data.version; } + $('title')[0].innerHTML = data["name"]; setEditToClick("system-name", data["name"]); }, function() { setTimeout(getMenu, 1000); }); }; @@ -303,19 +304,6 @@ function getWifiInfo() { //===== System info -function setEditToClick(klass, value) { - domForEach($("."+klass), function(div) { - if (div.children.length > 0) { - domForEach(div.children, function(el) { - if (el.nodeName === "INPUT") el.value = value; - else if (el.nodeName !== "DIV") el.innerHTML = value; - }); - } else { - div.innerHTML = value; - } - }); -} - function showSystemInfo(data) { Object.keys(data).forEach(function(v) { setEditToClick("system-"+v, data[v]); @@ -366,6 +354,18 @@ function makeAjaxInput(klass, field) { }); } +function setEditToClick(klass, value) { + domForEach($("."+klass), function(div) { + if (div.children.length > 0) { + domForEach(div.children, function(el) { + if (el.nodeName === "INPUT") el.value = value; + else if (el.nodeName !== "DIV") el.innerHTML = value; + }); + } else { + div.innerHTML = value; + } + }); +} //===== Notifications From bd407691387443d9933058c5a4d36a6a0fe10241 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sat, 2 Apr 2016 23:27:02 -0700 Subject: [PATCH 15/20] fix #103: wifi icons wrap when scaling browser --- html/wifi/wifiSta.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/html/wifi/wifiSta.js b/html/wifi/wifiSta.js index 8af3ee0..0ab167a 100644 --- a/html/wifi/wifiSta.js +++ b/html/wifi/wifiSta.js @@ -14,15 +14,15 @@ function createInputForAp(ap) { var bars = e("div"); var rssiVal = -Math.floor(ap.rssi/51)*32; bars.className = "lock-icon"; - bars.style.backgroundPosition = "0px "+rssiVal+"px"; + bars.style.backgroundPosition = "0px "+(rssiVal-1)+"px"; var rssi = e("div"); rssi.innerHTML = "" + ap.rssi +"dB"; var encrypt = e("div"); - var encVal = "-64"; //assume wpa/wpa2 + var encVal = "-65"; //assume wpa/wpa2 if (ap.enc == "0") encVal = "0"; //open - if (ap.enc == "1") encVal = "-32"; //wep + if (ap.enc == "1") encVal = "-33"; //wep encrypt.className = "lock-icon"; encrypt.style.backgroundPosition = "-32px "+encVal+"px"; From 8b4749fb907bb5174711dad492f66001ec0263cf Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sun, 3 Apr 2016 00:00:04 -0700 Subject: [PATCH 16/20] fix #101: doesn't stay in AP-only after reset --- esp-link/cgiwifi.c | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/esp-link/cgiwifi.c b/esp-link/cgiwifi.c index ca70554..6b88167 100644 --- a/esp-link/cgiwifi.c +++ b/esp-link/cgiwifi.c @@ -305,9 +305,11 @@ static void ICACHE_FLASH_ATTR resetTimerCb(void *arg) { int m = wifi_get_opmode() & 0x3; DBG("Wifi check: mode=%s status=%d\n", wifiMode[m], x); - if(m!=2){ - if ( x == STATION_GOT_IP ) { - if (m != 1) { + if (m == 2) return; // 2=AP, in AP-only mode we don't do any auto-switching + + if ( x == STATION_GOT_IP ) { + // if we got an IP we could switch to STA-only... + if (m != 1) { // 1=STA #ifdef CHANGE_TO_STA // We're happily connected, go to STA mode DBG("Wifi got IP. Going into STA mode..\n"); @@ -317,16 +319,16 @@ static void ICACHE_FLASH_ATTR resetTimerCb(void *arg) { } 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 { + // we don't have an IP address + if (m != 3) { + DBG("Wifi connect failed. Going into STA+AP mode..\n"); + wifi_set_opmode(3); + wifi_softap_set_config(&apconf); } log_uart(true); DBG("Enabling/continuing uart log\n"); os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); - } } } @@ -829,11 +831,12 @@ int ICACHE_FLASH_ATTR checkString(char *str){ */ void ICACHE_FLASH_ATTR wifiInit() { - // Check te wifi opmode + // Check the wifi opmode int x = wifi_get_opmode() & 0x3; - // Set opmode to 3 to let system scan aps, otherwise it won't scan - wifi_set_opmode(3); + // If STA is enabled switch to STA+AP to allow for recovery, it will then switch to STA-only + // once it gets an IP address + if (x == 1) wifi_set_opmode(3); // Call both STATION and SOFTAP default config wifi_station_get_config_default(&stconf); @@ -841,7 +844,7 @@ void ICACHE_FLASH_ATTR wifiInit() { DBG("Wifi init, mode=%s\n",wifiMode[x]); - // STATION parameters + // Change STATION parameters, if defined in the Makefile #if defined(STA_SSID) && defined(STA_PASS) // Set parameters if (os_strlen((char*)stconf.ssid) == 0 && os_strlen((char*)stconf.password) == 0) { @@ -856,7 +859,7 @@ void ICACHE_FLASH_ATTR wifiInit() { } #endif - // Change SOFT_AP settings if defined in Makefile + // Change SOFT_AP settings, if defined in Makefile #if defined(AP_SSID) // Check if ssid and pass are alphanumeric values int ssidlen = os_strlen(VERS_STR(AP_SSID)); From ccbe99de8236bdb84a1b01e92eda121333357040 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sun, 3 Apr 2016 00:07:28 -0700 Subject: [PATCH 17/20] fix #122: uninitialized vars in REST_Setup --- rest/rest.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rest/rest.c b/rest/rest.c index fd116a1..a267059 100644 --- a/rest/rest.c +++ b/rest/rest.c @@ -200,7 +200,8 @@ rest_dns_found(const char *name, ip_addr_t *ipaddr, void *arg) { void ICACHE_FLASH_ATTR REST_Setup(CmdPacket *cmd) { CmdRequest req; - uint32_t port, security; + uint16_t port; + uint8_t security; int32_t err = -1; // error code in case of failure // start parsing the command From 06ec5d46f14b5ced1048a631c65c3aa91313eef7 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sun, 3 Apr 2016 09:43:55 -0700 Subject: [PATCH 18/20] switch to sdk 1.5.2 --- include/user_config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/user_config.h b/include/user_config.h index a939533..131e1b9 100644 --- a/include/user_config.h +++ b/include/user_config.h @@ -19,7 +19,7 @@ #define CONFIG_DBG #define LOG_DBG #define STATUS_DBG -#undef HTTPD_DBG +#define HTTPD_DBG #define MQTT_DBG #define MQTTCMD_DBG #undef PKTBUF_DBG From d2bd118f0b5b1e4e1e3c6e2b4683a2fe1643ff21 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sun, 3 Apr 2016 10:41:16 -0700 Subject: [PATCH 19/20] fix c99 types mess; clean up after merges --- esp-link/main.c | 6 +++--- rest/rest.c | 8 ++++---- syslog/syslog.c | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/esp-link/main.c b/esp-link/main.c index b386338..bf8c6ef 100644 --- a/esp-link/main.c +++ b/esp-link/main.c @@ -81,7 +81,7 @@ HttpdBuiltInUrl builtInUrls[] = { { "/pins", cgiPins, NULL }, #ifdef MQTT { "/mqtt", cgiMqtt, NULL }, -#endif +#endif { "*", cgiEspFsHook, NULL }, //Catch-all cgi function for the filesystem { NULL, NULL, NULL } }; @@ -151,8 +151,8 @@ 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(); #ifdef MQTT diff --git a/rest/rest.c b/rest/rest.c index a267059..aa40ccb 100644 --- a/rest/rest.c +++ b/rest/rest.c @@ -375,7 +375,7 @@ REST_Request(CmdPacket *cmd) { realLen = cmdArgLen(&req); if (realLen > 2048) goto fail; } - DBG_REST(" bodyLen=%ld", realLen); + DBG_REST(" bodyLen=%d", 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 @@ -394,7 +394,7 @@ REST_Request(CmdPacket *cmd) { if (client->data) os_free(client->data); client->data = (char*)os_zalloc(headerLen + realLen); if (client->data == NULL) goto fail; - DBG_REST(" totLen=%ld data=%p", headerLen + realLen, client->data); + DBG_REST(" totLen=%d 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); @@ -413,7 +413,7 @@ REST_Request(CmdPacket *cmd) { 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:%ld\n",client->host, client->port); + DBG_REST("REST: Connect to ip %s:%d\n",client->host, client->port); //if(client->security){ // espconn_secure_connect(client->pCon); //} @@ -421,7 +421,7 @@ REST_Request(CmdPacket *cmd) { espconn_connect(client->pCon); //} } else { - DBG_REST("REST: Connect to host %s:%ld\n", client->host, client->port); + DBG_REST("REST: Connect to host %s:%d\n", client->host, client->port); espconn_gethostbyname(client->pCon, (char *)client->host, &client->ip, rest_dns_found); } diff --git a/syslog/syslog.c b/syslog/syslog.c index 3fcf66f..27d8c58 100644 --- a/syslog/syslog.c +++ b/syslog/syslog.c @@ -413,7 +413,7 @@ syslog_compose(uint8_t facility, uint8_t severity, const char *tag, const char * // add HOSTNAME APP-NAME PROCID MSGID if (flashConfig.syslog_showtick) - p += os_sprintf(p, "%s %s %lu.%06lu %lu ", flashConfig.hostname, tag, se->tick / 1000000, + p += os_sprintf(p, "%s %s %u.%06u %u ", flashConfig.hostname, tag, se->tick / 1000000, se->tick % 1000000, syslog_msgid++); else p += os_sprintf(p, "%s %s - %u ", flashConfig.hostname, tag, syslog_msgid++); From ac92202ef6d82de87763c428087068fe5c41296c Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sun, 3 Apr 2016 10:41:36 -0700 Subject: [PATCH 20/20] fix c99 types mess, try 2; clean up after merges --- include/c_types.h | 104 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 include/c_types.h diff --git a/include/c_types.h b/include/c_types.h new file mode 100644 index 0000000..6c4cfbd --- /dev/null +++ b/include/c_types.h @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2010 - 2011 Espressif System + * + */ + +// Modified for esp-link to confiorm with c99 using the patch included with +// esp-open-sdk https://github.com/pfalcon/esp-open-sdk/blob/master/c_types-c99.patch +// This is included here because otherwise there is a discrepancy between users that use +// the original Espressif SDK vs ones who want to use the SDK included with esp-open-sdk. +// This is a mess, if only Espressif fixed their crap! + +#ifndef _C_TYPES_H_ +#define _C_TYPES_H_ + +#include +#include + +//typedef unsigned char uint8_t; +typedef signed char sint8_t; +//typedef signed char int8_t; +//typedef unsigned short uint16_t; +typedef signed short sint16_t; +//typedef signed short int16_t; +//typedef unsigned long uint32_t; +typedef signed long sint32_t; +//typedef signed long int32_t; +typedef signed long long sint64_t; +//typedef unsigned long long uint64_t; +typedef unsigned long long u_int64_t; +typedef float real32_t; +typedef double real64_t; + +typedef unsigned char uint8; +typedef unsigned char u8; +typedef signed char sint8; +typedef signed char int8; +typedef signed char s8; +typedef unsigned short uint16; +typedef unsigned short u16; +typedef signed short sint16; +typedef signed short s16; +typedef unsigned int uint32; +typedef unsigned int u_int; +typedef unsigned int u32; +typedef signed int sint32; +typedef signed int s32; +typedef int int32; +typedef signed long long sint64; +typedef unsigned long long uint64; +typedef unsigned long long u64; +typedef float real32; +typedef double real64; + +#define __le16 u16 + +typedef unsigned int size_t; + +#define __packed __attribute__((packed)) + +#define LOCAL static + +#ifndef NULL +#define NULL (void *)0 +#endif /* NULL */ + +/* probably should not put STATUS here */ +typedef enum { + OK = 0, + FAIL, + PENDING, + BUSY, + CANCEL, +} STATUS; + +#define BIT(nr) (1UL << (nr)) + +#define REG_SET_BIT(_r, _b) (*(volatile uint32_t*)(_r) |= (_b)) +#define REG_CLR_BIT(_r, _b) (*(volatile uint32_t*)(_r) &= ~(_b)) + +#define DMEM_ATTR __attribute__((section(".bss"))) +#define SHMEM_ATTR + +#ifdef ICACHE_FLASH +#define ICACHE_FLASH_ATTR __attribute__((section(".irom0.text"))) +#define ICACHE_RODATA_ATTR __attribute__((section(".irom.text"))) +#else +#define ICACHE_FLASH_ATTR +#define ICACHE_RODATA_ATTR +#endif /* ICACHE_FLASH */ + +#define STORE_ATTR __attribute__((aligned(4))) + +#ifndef __cplusplus +//typedef unsigned char bool; +#define BOOL bool +//#define true (1) +//#define false (0) +#define TRUE true +#define FALSE false + + +#endif /* !__cplusplus */ + +#endif /* _C_TYPES_H_ */