diff --git a/Makefile b/Makefile index 4e5fa72..523f85f 100644 --- a/Makefile +++ b/Makefile @@ -54,9 +54,26 @@ 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 -# 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) +# 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, +# 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 @@ -215,7 +232,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)" 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 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/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/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..6b3f553 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, diff --git a/esp-link/cgiwifi.c b/esp-link/cgiwifi.c old mode 100755 new mode 100644 index 50ea536..6b88167 --- 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); - } } } @@ -533,7 +535,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 +547,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 +733,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 +812,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; } @@ -828,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); @@ -840,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) { @@ -855,7 +859,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 +913,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/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 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..bf8c6ef 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"); - + NOTICE("** %s: ready, heap=%ld", esp_link_version, (unsigned long)system_get_free_heap_size()); + // Init SNTP service cgiServicesSNTPInit(); #ifdef MQTT 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 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/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"; 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_ */ 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 diff --git a/mqtt/mqtt.c b/mqtt/mqtt.c index 30e2dd0..bf785f6 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) @@ -705,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; 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..aa40ccb 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 @@ -252,7 +253,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 +349,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 +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 @@ -393,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); @@ -412,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); //} @@ -420,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/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 20a51cb..27d8c58 100644 --- a/syslog/syslog.c +++ b/syslog/syslog.c @@ -156,8 +156,8 @@ static void ICACHE_FLASH_ATTR syslog_chk_status(void) } } else { if ((wifi_status == STATION_WRONG_PASSWORD || - wifi_status == STATION_NO_AP_FOUND || - wifi_status == STATION_CONNECT_FAIL)) { + wifi_status == STATION_NO_AP_FOUND || + wifi_status == STATION_CONNECT_FAIL)) { syslog_set_status(SYSLOG_ERROR); os_printf("*** connect failure!!!\n"); } else { @@ -273,7 +273,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; @@ -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; @@ -405,16 +406,17 @@ 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; @@ -512,6 +514,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