From ed9b11186c7f7cf8f4e8e4ee77aab32ba05a73ba Mon Sep 17 00:00:00 2001 From: Benjamin Runnels Date: Sun, 22 Nov 2015 11:52:38 -0600 Subject: [PATCH 01/19] MQTT define bugfix Possilbe mDNS bugfix Switched to DBG method instead of multiple ifdef in httpd --- WINDOWS.md | 13 ++++++++++ esp-link.vcxproj | 4 +++ esp-link/cgimqtt.c | 15 ++++++++---- esp-link/cgimqtt.h | 2 +- esp-link/cgiwifi.c | 5 ++-- httpd/httpd.c | 61 ++++++++++++++++------------------------------ 6 files changed, 52 insertions(+), 48 deletions(-) create mode 100644 WINDOWS.md diff --git a/WINDOWS.md b/WINDOWS.md new file mode 100644 index 0000000..10480b4 --- /dev/null +++ b/WINDOWS.md @@ -0,0 +1,13 @@ +* Install [SourceTree](https://www.sourcetreeapp.com) and check CLI git or other git distribution to do git from CLI +* Install and latest Java +* Install Python 2.7 to C:\Python27 +* Install link shell extension from [here](http://schinagl.priv.at/nt/hardlinkshellext/linkshellextension.html) +* Download and install the [Windows Unofficial Development Kit for Espressif ESP8266](http://programs74.ru/get.php?file=EspressifESP8266DevKit) to c:\espressif +* Create a symbolic link for java/bin and git/bin directories under C:\espressif\git-bin and C:\espressif\java-bin. You have to do it like that because make doesn't like the full path with program files(x86). You can see all the expected paths in the [espmake.cmd](https://github.com/jeelabs/esp-link/blob/master/espmake.cmd) +* [Download](http://sourceforge.net/projects/mingw/files/Installer/) and install MinGW. Run mingw-get-setup.exe, the installation process to select without GUI, ie uncheck "... also install support for the graphical user interface". +* [Download](http://programs74.ru/get.php?file=EspressifESP8266DevKitAddon) the scripts to automate the installation of additional modules for MinGW. +* Run install-mingw-package.bat. This will establish the basic modules for MinGW. Installation should proceed without error. +* Checkout esp-link under C:\espressif\esp-link +* espmake.cmd "make all wiflash" +* new flash over serial then is espmake.cmd "make all flash" +* if you want to program with serial but not loose your config each time espmake.cmd "make all baseflash" \ No newline at end of file diff --git a/esp-link.vcxproj b/esp-link.vcxproj index 57e3a8d..3e727e4 100644 --- a/esp-link.vcxproj +++ b/esp-link.vcxproj @@ -16,6 +16,7 @@ + @@ -50,6 +51,7 @@ + @@ -57,6 +59,7 @@ + @@ -81,6 +84,7 @@ + diff --git a/esp-link/cgimqtt.c b/esp-link/cgimqtt.c index 627d679..de98d57 100644 --- a/esp-link/cgimqtt.c +++ b/esp-link/cgimqtt.c @@ -1,5 +1,14 @@ // Copyright 2015 by Thorsten von Eicken, see LICENSE.txt -#ifdef MQTT + +static char *mqtt_states[] = { + "disconnected", "reconnecting", "connecting", "connected", "disabled" +}; + +#if !defined(MQTT) +char *mqttState(void) { + return mqtt_states[4]; +} +#else #include #include "cgi.h" #include "config.h" @@ -7,10 +16,6 @@ #include "mqtt_client.h" #include "cgimqtt.h" -static char *mqtt_states[] = { - "disconnected", "reconnecting", "connecting", "connected", -}; - char *mqttState(void) { return mqtt_states[mqttClient.connState]; } diff --git a/esp-link/cgimqtt.h b/esp-link/cgimqtt.h index b720fac..b3e7809 100644 --- a/esp-link/cgimqtt.h +++ b/esp-link/cgimqtt.h @@ -1,10 +1,10 @@ +char *mqttState(void); #ifdef MQTT #ifndef CGIMQTT_H #define CGIMQTT_H #include "httpd.h" int cgiMqtt(HttpdConnData *connData); -char *mqttState(void); #endif // CGIMQTT_H #endif // MQTT diff --git a/esp-link/cgiwifi.c b/esp-link/cgiwifi.c index 0fcbf71..1000c17 100644 --- a/esp-link/cgiwifi.c +++ b/esp-link/cgiwifi.c @@ -121,10 +121,11 @@ static ICACHE_FLASH_ATTR void wifiStartMDNS(struct ip_addr ip) { if (!mdns_started) { os_memset(&mdns_info, 0, sizeof(struct mdns_info)); - mdns_info.host_name = flashConfig.hostname; + mdns_info.host_name = flashConfig.hostname, mdns_info.server_name = "http", // service name mdns_info.server_port = 80, // service port - mdns_info.ipAddr = ip.addr, + mdns_info.ipAddr = ip.addr, + mdns_info.txt_data[0] = (char *) "version = now", espconn_mdns_init(&mdns_info); mdns_started = true; } diff --git a/httpd/httpd.c b/httpd/httpd.c index 22da6bf..f56e579 100644 --- a/httpd/httpd.c +++ b/httpd/httpd.c @@ -17,7 +17,11 @@ Esp8266 http server - core routines #include #include "httpd.h" -//#define HTTPD_DBG +#ifdef HTTPD_DBG +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) +#else +#define DBG(format, ...) do { } while(0) +#endif //Max length of request head @@ -97,7 +101,7 @@ static void debugConn(void *arg, char *what) { os_sprintf(connStr, "%d.%d.%d.%d:%d ", tcp->remote_ip[0], tcp->remote_ip[1], tcp->remote_ip[2], tcp->remote_ip[3], tcp->remote_port); - os_printf("%s %s\n", connStr, what); + DBG("%s %s\n", connStr, what); #else connStr[0] = 0; #endif @@ -108,20 +112,18 @@ static void ICACHE_FLASH_ATTR httpdRetireConn(HttpdConnData *conn) { if (conn->conn && conn->conn->reverse == conn) conn->conn->reverse = NULL; // break reverse link -#ifdef HTTPD_DBG // log information about the request we handled uint32 dt = conn->startTime; if (dt > 0) dt = (system_get_time() - dt) / 1000; if (conn->conn && conn->url) #if 0 - os_printf("HTTP %s %s from %s -> %d in %ums, heap=%ld\n", + DBG("HTTP %s %s from %s -> %d in %ums, heap=%ld\n", conn->requestType == HTTPD_METHOD_GET ? "GET" : "POST", conn->url, conn->priv->from, conn->priv->code, dt, (unsigned long)system_get_free_heap_size()); #else - os_printf("HTTP %s %s: %d, %ums, h=%ld\n", + DBG("HTTP %s %s: %d, %ums, h=%ld\n", conn->requestType == HTTPD_METHOD_GET ? "GET" : "POST", conn->url, conn->priv->code, dt, (unsigned long)system_get_free_heap_size()); -#endif #endif conn->conn = NULL; // don't try to send anything, the SDK crashes... @@ -276,10 +278,8 @@ int ICACHE_FLASH_ATTR cgiRedirect(HttpdConnData *connData) { int ICACHE_FLASH_ATTR httpdSend(HttpdConnData *conn, const char *data, int len) { if (len<0) len = strlen(data); if (conn->priv->sendBuffLen + len>MAX_SENDBUFF_LEN) { -#ifdef HTTPD_DBG - os_printf("%sERROR! httpdSend full (%d of %d)\n", + DBG("%sERROR! httpdSend full (%d of %d)\n", connStr, conn->priv->sendBuffLen, MAX_SENDBUFF_LEN); -#endif return 0; } os_memcpy(conn->priv->sendBuff + conn->priv->sendBuffLen, data, len); @@ -292,10 +292,8 @@ static void ICACHE_FLASH_ATTR xmitSendBuff(HttpdConnData *conn) { if (conn->priv->sendBuffLen != 0) { sint8 status = espconn_sent(conn->conn, (uint8_t*)conn->priv->sendBuff, conn->priv->sendBuffLen); if (status != 0) { -#ifdef HTTPD_DBG - os_printf("%sERROR! espconn_sent returned %d, trying to send %d to %s\n", + DBG("%sERROR! espconn_sent returned %d, trying to send %d to %s\n", connStr, status, conn->priv->sendBuffLen, conn->url); -#endif } conn->priv->sendBuffLen = 0; } @@ -323,9 +321,7 @@ static void ICACHE_FLASH_ATTR httpdSentCb(void *arg) { conn->cgi = NULL; //mark for destruction. } if (r == HTTPD_CGI_NOTFOUND || r == HTTPD_CGI_AUTHENTICATED) { -#ifdef HTTPD_DBG - os_printf("%sERROR! Bad CGI code %d\n", connStr, r); -#endif + DBG("%sERROR! Bad CGI code %d\n", connStr, r); conn->cgi = NULL; //mark for destruction. } xmitSendBuff(conn); @@ -342,9 +338,7 @@ static void ICACHE_FLASH_ATTR httpdProcessRequest(HttpdConnData *conn) { int r; int i = 0; if (conn->url == NULL) { -#ifdef HTTPD_DBG - os_printf("%sWtF? url = NULL\n", connStr); -#endif + DBG("%sWtF? url = NULL\n", connStr); return; //Shouldn't happen } //See if we can find a CGI that's happy to handle the request. @@ -370,9 +364,7 @@ static void ICACHE_FLASH_ATTR httpdProcessRequest(HttpdConnData *conn) { if (builtInUrls[i].url == NULL) { //Drat, we're at the end of the URL table. This usually shouldn't happen. Well, just //generate a built-in 404 to handle this. -#ifdef HTTPD_DBG - os_printf("%s%s not found. 404!\n", connStr, conn->url); -#endif + DBG("%s%s not found. 404!\n", connStr, conn->url); httpdSend(conn, httpNotFoundHeader, -1); xmitSendBuff(conn); conn->cgi = NULL; //mark for destruction. @@ -436,19 +428,15 @@ static void ICACHE_FLASH_ATTR httpdParseHeader(char *h, HttpdConnData *conn) { *e = 0; //terminate url part // Count number of open connections -#ifdef HTTPD_DBG //esp_tcp *tcp = conn->conn->proto.tcp; - //os_printf("%sHTTP %s %s from %s\n", connStr, + //DBG("%sHTTP %s %s from %s\n", connStr, // conn->requestType == HTTPD_METHOD_GET ? "GET" : "POST", conn->url, conn->priv->from); -#endif //Parse out the URL part before the GET parameters. conn->getArgs = (char*)os_strstr(conn->url, "?"); if (conn->getArgs != 0) { *conn->getArgs = 0; conn->getArgs++; -#ifdef HTTPD_DBG - //os_printf("%sargs = %s\n", connStr, conn->getArgs); -#endif + //DBG("%sargs = %s\n", connStr, conn->getArgs); } else { conn->getArgs = NULL; @@ -470,7 +458,7 @@ static void ICACHE_FLASH_ATTR httpdParseHeader(char *h, HttpdConnData *conn) { else { conn->post->buffSize = conn->post->len; } - //os_printf("Mallocced buffer for %d + 1 bytes of post data.\n", conn->post->buffSize); + //DBG("Mallocced buffer for %d + 1 bytes of post data.\n", conn->post->buffSize); conn->post->buff = (char*)os_malloc(conn->post->buffSize + 1); conn->post->buffLen = 0; } @@ -482,7 +470,7 @@ static void ICACHE_FLASH_ATTR httpdParseHeader(char *h, HttpdConnData *conn) { conn->post->multipartBoundary = b + 7; // move the pointer 2 chars before boundary then fill them with dashes conn->post->multipartBoundary[0] = '-'; conn->post->multipartBoundary[1] = '-'; - //os_printf("boundary = %s\n", conn->post->multipartBoundary); + //DBG("boundary = %s\n", conn->post->multipartBoundary); } } } @@ -562,10 +550,7 @@ static void ICACHE_FLASH_ATTR httpdReconCb(void *arg, sint8 err) { struct espconn* pCon = (struct espconn *)arg; HttpdConnData *conn = (HttpdConnData *)pCon->reverse; if (conn == NULL) return; // aborted connection - -#ifdef HTTPD_DBG - os_printf("%s***** reset, err=%d\n", connStr, err); -#endif + DBG("%s***** reset, err=%d\n", connStr, err); httpdRetireConn(conn); } @@ -577,7 +562,7 @@ static void ICACHE_FLASH_ATTR httpdConnectCb(void *arg) { // Find empty conndata in pool int i; for (i = 0; i Date: Sun, 22 Nov 2015 19:32:13 -0600 Subject: [PATCH 02/19] Fixed WINDOWS.md --- WINDOWS.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/WINDOWS.md b/WINDOWS.md index 10480b4..08e7a03 100644 --- a/WINDOWS.md +++ b/WINDOWS.md @@ -1,13 +1,14 @@ -* Install [SourceTree](https://www.sourcetreeapp.com) and check CLI git or other git distribution to do git from CLI -* Install and latest Java +* Install [SourceTree](https://www.sourcetreeapp.com) and check CLI git or other git distribution to obtain git from CLI +* Install the latest Java JRE * Install Python 2.7 to C:\Python27 * Install link shell extension from [here](http://schinagl.priv.at/nt/hardlinkshellext/linkshellextension.html) * Download and install the [Windows Unofficial Development Kit for Espressif ESP8266](http://programs74.ru/get.php?file=EspressifESP8266DevKit) to c:\espressif -* Create a symbolic link for java/bin and git/bin directories under C:\espressif\git-bin and C:\espressif\java-bin. You have to do it like that because make doesn't like the full path with program files(x86). You can see all the expected paths in the [espmake.cmd](https://github.com/jeelabs/esp-link/blob/master/espmake.cmd) -* [Download](http://sourceforge.net/projects/mingw/files/Installer/) and install MinGW. Run mingw-get-setup.exe, the installation process to select without GUI, ie uncheck "... also install support for the graphical user interface". +* Create a symbolic link for java/bin and git/bin directories under C:\espressif\git-bin and C:\espressif\java-bin. You must do this because "make" doesn't work properly with paths like "program files(x86)". You can see all the expected paths in the [espmake.cmd](https://github.com/jeelabs/esp-link/blob/master/espmake.cmd) +* [Download](http://sourceforge.net/projects/mingw/files/Installer/) and install MinGW. Run mingw-get-setup.exe. During the installation process select without GUI. (uncheck "... also install support for the graphical user interface") * [Download](http://programs74.ru/get.php?file=EspressifESP8266DevKitAddon) the scripts to automate the installation of additional modules for MinGW. -* Run install-mingw-package.bat. This will establish the basic modules for MinGW. Installation should proceed without error. -* Checkout esp-link under C:\espressif\esp-link -* espmake.cmd "make all wiflash" -* new flash over serial then is espmake.cmd "make all flash" -* if you want to program with serial but not loose your config each time espmake.cmd "make all baseflash" \ No newline at end of file +* Run install-mingw-package.bat. This will install the basic modules required for MinGW to build esp8266. +* Checkout esp-link from git to C:\espressif\esp-link +* When you're done open a command prompt and run: espmake.cmd "make all wiflash" +* For a new flash over serial use: espmake.cmd "make all flash" +* If you want to program with serial but not loose your config each time use: espmake.cmd "make all baseflash" +* You can open the esp-link.sln file in Visual Studio 2013. "Build Solution" will issue "make all wiflash". "Clean Solution" will issue "make clean". "Rebuild Solution" will issue "make clean all". This can be changed under solution properties -> Configuration Properties -> NMake \ No newline at end of file From b7ae39ae8d779593608f4b1c0ccb17d11905f8ae Mon Sep 17 00:00:00 2001 From: Benjamin Runnels Date: Mon, 23 Nov 2015 08:01:26 -0600 Subject: [PATCH 03/19] Moved to debug macro for SLIP_DBG and fixed an error when SLIP_DBG was defined Removed unnecessary stuff Switched VS project to use sdk 1.4.0 sources for intellisense --- esp-link.vcxproj | 4 ++-- esp-link/mqtt_client.c | 31 ------------------------------- serial/slip.c | 24 ++++++++++++++---------- 3 files changed, 16 insertions(+), 43 deletions(-) diff --git a/esp-link.vcxproj b/esp-link.vcxproj index 3e727e4..27d9fcc 100644 --- a/esp-link.vcxproj +++ b/esp-link.vcxproj @@ -129,8 +129,8 @@ - __ets__;_STDINT_H;ICACHE_FLASH;__MINGW32__;__WIN32__ - .\rest;.\esp-link;.\mqtt;.\cmd;.\serial;.\user;.\espfs;.\httpd;.\include;..\esp_iot_sdk_v1.3.0\include;..\xtensa-lx106-elf\xtensa-lx106-elf\include;c:\tools\mingw64\x86_64-w64-mingw32\include;c:\tools\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.3\include + __ets__;_STDINT_H;ICACHE_FLASH;__MINGW32__;__WIN32__;MQTT + .\rest;.\esp-link;.\mqtt;.\cmd;.\serial;.\user;.\espfs;.\httpd;.\include;..\esp_iot_sdk_v1.4.0\include;..\xtensa-lx106-elf\xtensa-lx106-elf\include;c:\tools\mingw64\x86_64-w64-mingw32\include;c:\tools\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.3\include diff --git a/esp-link/mqtt_client.c b/esp-link/mqtt_client.c index 3969530..431e5f0 100644 --- a/esp-link/mqtt_client.c +++ b/esp-link/mqtt_client.c @@ -12,11 +12,6 @@ MQTT_Client mqttClient; // main mqtt client used by esp-link -#ifdef BRUNNELS -char* statusTopicStr; -static char* onlineMsgStr; -#endif - static MqttCallback connected_cb; static MqttCallback disconnected_cb; static MqttCallback published_cb; @@ -27,9 +22,6 @@ mqttConnectedCb(uint32_t *args) { DBG_MQTTCLIENT("MQTT Client: Connected\n"); //MQTT_Client* client = (MQTT_Client*)args; //MQTT_Subscribe(client, "system/time", 0); // handy for testing -#ifdef BRUNNELS - MQTT_Publish(client, "announce/all", onlineMsgStr, 0, 0); -#endif if (connected_cb) connected_cb(args); } @@ -93,29 +85,6 @@ mqtt_client_init() flashConfig.mqtt_clientid, flashConfig.mqtt_username, flashConfig.mqtt_password, flashConfig.mqtt_keepalive); -// removed client_id concat for now until a better solution is devised -// statusTopicStr = (char*)os_zalloc(strlen(flashConfig.mqtt_clientid) + strlen(flashConfig.mqtt_status_topic) + 2); -// os_strcpy(statusTopicStr, flashConfig.mqtt_clientid); -// os_strcat(statusTopicStr, "/"); - -#ifdef BRUNNELS - char* onlineMsg = " is online"; - onlineMsgStr = (char*)os_zalloc(strlen(flashConfig.mqtt_clientid) + strlen(onlineMsg) + 1); - os_strcpy(onlineMsgStr, flashConfig.mqtt_clientid); - os_strcat(onlineMsgStr, onlineMsg); - - char* offlineMsg = " is offline"; - char* offlineMsgStr = (char*)os_zalloc(strlen(flashConfig.mqtt_clientid) + strlen(offlineMsg) + 1); - os_strcpy(offlineMsgStr, flashConfig.mqtt_clientid); - os_strcat(offlineMsgStr, offlineMsg); - - char* lwt = "/lwt"; - char *lwtMsgStr = (char*)os_zalloc(strlen(flashConfig.mqtt_clientid) + strlen(lwt) + 1); - os_strcpy(lwtMsgStr, flashConfig.mqtt_clientid); - os_strcat(lwtMsgStr, lwt); - MQTT_InitLWT(&mqttClient, lwtMsgStr, offlineMsg, 0, 0); -#endif - MQTT_OnConnected(&mqttClient, mqttConnectedCb); MQTT_OnDisconnected(&mqttClient, mqttDisconnectedCb); MQTT_OnPublished(&mqttClient, mqttPublishedCb); diff --git a/serial/slip.c b/serial/slip.c index 4125a08..b9446e3 100644 --- a/serial/slip.c +++ b/serial/slip.c @@ -7,6 +7,12 @@ #include "console.h" #include "cmd.h" +#ifdef SLIP_DBG +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) +#else +#define DBG(format, ...) do { } while(0) +#endif + uint8_t slip_disabled; // temporarily disable slip to allow flashing of attached MCU extern void ICACHE_FLASH_ATTR console_process(char *buf, short len); @@ -48,15 +54,15 @@ slip_process() { } else { os_printf("SLIP: bad CRC, crc=%x rcv=%x\n", crc, rcv); -#ifdef SLIP_DBG for (short i=0; i= ' ' && slip_buf[i] <= '~') - os_printf("%c", slip_buf[i]); - else - os_printf("\\%02X", slip_buf[i]); + if (slip_buf[i] >= ' ' && slip_buf[i] <= '~') { + DBG("%c", slip_buf[i]); + } + else { + DBG("\\%02X", slip_buf[i]); + } } - os_printf("\n"); -#endif + DBG("\n"); } } } @@ -86,9 +92,7 @@ slip_parse_char(char c) { if (slip_len > 0) console_process(slip_buf, slip_len); slip_reset(); slip_inpkt = true; -#ifdef SLIP_DBG - os_printf("SLIP: start\n"); -#endif + DBG("SLIP: start\n"); return; } } else if (slip_escaped) { From 5935a30f031da3237648b1548d912cdbb2c794bc Mon Sep 17 00:00:00 2001 From: Benjamin Runnels Date: Thu, 26 Nov 2015 06:41:29 -0600 Subject: [PATCH 04/19] Started adding in standalone mdns implementation --- esp-link.vcxproj | 2 + esp-link/cgiwifi.c | 22 ++- esp-link/mdns.c | 335 ++++++++++++++++++++++++++++++++++++++++++ esp-link/mdns.h | 52 +++++++ include/user_config.h | 1 + 5 files changed, 404 insertions(+), 8 deletions(-) create mode 100644 esp-link/mdns.c create mode 100644 esp-link/mdns.h diff --git a/esp-link.vcxproj b/esp-link.vcxproj index 27d9fcc..c753fd6 100644 --- a/esp-link.vcxproj +++ b/esp-link.vcxproj @@ -23,6 +23,7 @@ + @@ -57,6 +58,7 @@ + diff --git a/esp-link/cgiwifi.c b/esp-link/cgiwifi.c index 1000c17..8032833 100644 --- a/esp-link/cgiwifi.c +++ b/esp-link/cgiwifi.c @@ -20,6 +20,7 @@ Cgi/template routines for the /wifi url. #include "status.h" #include "config.h" #include "log.h" +#include "mdns.h" #ifdef CGIWIFI_DBG #define DBG(format, ...) os_printf(format, ## __VA_ARGS__) @@ -114,19 +115,24 @@ wifiAddStateChangeCb(WifiStateChangeCb cb) { } static bool mdns_started = false; -static struct mdns_info mdns_info; +//static struct mdns_info mdns_info; // cannot allocate the info struct on the stack, it crashes! static ICACHE_FLASH_ATTR void wifiStartMDNS(struct ip_addr ip) { if (!mdns_started) { - os_memset(&mdns_info, 0, sizeof(struct mdns_info)); - mdns_info.host_name = flashConfig.hostname, - mdns_info.server_name = "http", // service name - mdns_info.server_port = 80, // service port - mdns_info.ipAddr = ip.addr, - mdns_info.txt_data[0] = (char *) "version = now", - espconn_mdns_init(&mdns_info); +// struct mdns_info *ard_mdns_info = (struct mdns_info *)os_zalloc(sizeof(struct mdns_info)); +// ard_mdns_info->host_name = flashConfig.hostname; +// ard_mdns_info->server_name = "arduino"; // service name +// ard_mdns_info->server_port = 2323; // service port +// ard_mdns_info->ipAddr = ip.addr; +// ard_mdns_info->txt_data[0] = (char *) "version = now"; +// espconn_mdns_init(ard_mdns_info); +// espconn_mdns_server_register(); + + mdns_init(60, 3600); + mdns_addhost(flashConfig.hostname, &ip); + //mdns_addhost("arduino", &ip); mdns_started = true; } } diff --git a/esp-link/mdns.c b/esp-link/mdns.c new file mode 100644 index 0000000..1cd504f --- /dev/null +++ b/esp-link/mdns.c @@ -0,0 +1,335 @@ +/* +* mdns.h +* +* Simple mDNS responder. +* It replys to mDNS IP (IPv4/A) queries and optionally broadcasts ip advertisements +* using the mDNS protocol +* Created on: Apr 10, 2015 +* Author: Kevin Uhlir (n0bel) +* +*/ + +#include +#include "mdns.h" + +#ifdef MDNS_DBG +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) +#else +#define DBG(format, ...) do { } while(0) +#endif + +static int mdns_initialized = 0; +static int ttl = 0; +static os_timer_t bc_timer; + +static struct espconn mdns_conn; + +#define MAX_HOSTS 5 +static struct _host { + char *hostname; + unsigned char *mdns; + struct ip_addr ip; + int len; +} hosts[MAX_HOSTS] = { { NULL } }; + +int nhosts = 0; + +struct ip_addr mdns_multicast; +struct ip_addr ipaddr_any; + +#ifdef MDNS_DBG +void ICACHE_FLASH_ATTR hexdump(char *desc, void *addr, int len) { + int i; + unsigned char buff[17]; + unsigned char *pc = (unsigned char*)addr; + + // Output description if given. + if (desc != NULL) + DBG("%s:\n", desc); + + // Process every byte in the data. + for (i = 0; i < len; i++) { + // Multiple of 16 means new line (with line offset). + + if ((i % 16) == 0) { + // Just don't print ASCII for the zeroth line. + if (i != 0) + DBG(" %s\n", buff); + + // Output the offset. + DBG(" %04x ", i); + } + + // Now the hex code for the specific character. + DBG(" %02x", pc[i]); + + // And store a printable ASCII character for later. + if ((pc[i] < 0x20) || (pc[i] > 0x7e)) + buff[i % 16] = '.'; + else + buff[i % 16] = pc[i]; + buff[(i % 16) + 1] = '\0'; + } + + // Pad out last line if not exactly 16 characters. + while ((i % 16) != 0) { + DBG(" "); + i++; + } + + // And print the final ASCII bit. + DBG(" %s\n", buff); +} +#endif + +unsigned ICACHE_FLASH_ATTR char * encodeResp(struct ip_addr * addr, char *name, int *len) +{ + *len = 12 + strlen(name) + 16; + unsigned char *data = (unsigned char *)os_zalloc(*len); + + data[2] = 0x84; + data[7] = 1; + + unsigned char *p = data + 12; + char *np = name; + char *i; + while ((i = strchr(np, '.'))) + { + *p = i - np; + p++; + memcpy(p, np, i - np); + p += i - np; + np = i + 1; + } + *p = strlen(np); + p++; + memcpy(p, np, strlen(np)); + p += strlen(np); + *p++ = 0; // terminate string sequence + + *p++ = 0; *p++ = 1; // type 0001 (A) + + *p++ = 0x80; *p++ = 1; // class code (IPV4) + + *p++ = ttl >> 24; + *p++ = ttl >> 16; + *p++ = ttl >> 8; + *p++ = ttl; + + *p++ = 0; *p++ = 4; // length (of ip) + + memcpy(p, addr, 4); + + return data; +} + +char ICACHE_FLASH_ATTR *decode_name_strings(unsigned char *p, int *len) +{ + unsigned char *s = p; + int c; + *len = 0; + while ((c = *s++)) + { + *len += c + 1; + s += c; + } + char *name = (char *)os_zalloc(*len + 1); + char *np = name; + s = p; + while ((c = *s++)) + { + memcpy(np, s, c); + np += c; + *np++ = '.'; + s += c; + } + np--; *np = 0; + return name; +} + +void ICACHE_FLASH_ATTR sendOne(struct _host *h) +{ + mdns_conn.proto.udp->remote_ip[0] = 224; + mdns_conn.proto.udp->remote_ip[1] = 0; + mdns_conn.proto.udp->remote_ip[2] = 0; + mdns_conn.proto.udp->remote_ip[3] = 251; + mdns_conn.proto.udp->remote_port = 5353; + mdns_conn.proto.udp->local_port = 5353; +#ifdef MDNS_DBG + hexdump("sending", h->mdns, h->len); +#endif + espconn_sent(&mdns_conn, h->mdns, h->len); + +} +void ICACHE_FLASH_ATTR decodeQuery(unsigned char *data) +{ + if (data[0] != 0 || data[1] != 0 || data[2] != 0 || data[3] != 0) return; // only queries + + int qcount = data[5]; // purposely ignore qcount > 255 + unsigned char *p = data + 12; + char *name; + int len; + while (qcount-- > 0) + { + if (*p == 0xc0) // pointer + { + name = decode_name_strings(data + p[1], &len); + p += 2; + } + else + { + name = decode_name_strings(p, &len); + p += len + 1; + } + int qtype = p[0] * 256 + p[1]; + int qclass = p[2] * 256 + p[3]; + p += 4; + DBG("decoded name %s qtype=%d qclass=%d\n", name, qtype, qclass); + + if (qtype == 1 && (qclass & 0x7fff) == 1) + { + struct _host *h; + int i; + for (h = hosts, i = 0; i < nhosts; i++, h++) + { + if (h->hostname && strcmp(name, h->hostname) == 0) + { + DBG("its %s!\n", h->hostname); + sendOne(h); + } + } + os_free(name); + } + } + + return; +} + +static void ICACHE_FLASH_ATTR broadcast(void *arg) +{ + struct _host *h; + int i; + for (h = hosts, i = 0; i < nhosts; i++, h++) + { + DBG("broadcast for %s\n", h->hostname); + sendOne(h); + } +} + +static void ICACHE_FLASH_ATTR mdns_udp_recv(void *arg, char *pusrdata, unsigned short length) { + unsigned char *data = (unsigned char *)pusrdata; + + + DBG("remote port=%d remoteip=%d.%d.%d.%d\n", mdns_conn.proto.udp->remote_port, + mdns_conn.proto.udp->remote_ip[0], + mdns_conn.proto.udp->remote_ip[1], + mdns_conn.proto.udp->remote_ip[2], + mdns_conn.proto.udp->remote_ip[3] + ); +#ifdef MDNS_DBG +// hexdump("rec mNDS:", data, length); +#endif + decodeQuery(data); +} + +int ICACHE_FLASH_ATTR mdns_addhost(char *hn, struct ip_addr* ip) +{ + struct _host *h = &hosts[nhosts]; + if (nhosts >= MAX_HOSTS) + { + int i; + for (h = hosts, i = 0; i < MAX_HOSTS; i++, h++) + { + if (!h->hostname) break; + } + if (i >= MAX_HOSTS) return -1; + } + else + { + nhosts++; + } + h->hostname = (char *)os_zalloc(os_strlen(hn) + 1); + os_strcpy(h->hostname, hn); + h->ip.addr = ip->addr; + h->mdns = encodeResp(ip, hn, &(h->len)); +#ifdef MDNS_DBG + hexdump("addhost", hosts, sizeof(hosts)); +#endif + return(0); +} + +int ICACHE_FLASH_ATTR mdns_delhost(char *hn, struct ip_addr* ip) +{ + if (nhosts < 1) return -1; + struct _host *h; + int i; + for (h = hosts, i = 0; i < MAX_HOSTS; i++, h++) + { + if ((hn && strcmp(hn, h->hostname) == 0) + || (ip && h->ip.addr == ip->addr)) + { + if (h->hostname) os_free(h->hostname); + if (h->mdns) os_free(h->mdns); + h->hostname = NULL; + h->mdns = NULL; + h->ip.addr = 0; + } + } +#ifdef MDNS_DBG +// hexdump("delhost", hosts, sizeof(hosts)); +#endif + return(0); +} + +int ICACHE_FLASH_ATTR mdns_init(int bctime, int sttl) +{ + if (mdns_initialized) mdns_stop(); + mdns_initialized = 1; + ttl = sttl; + + IP4_ADDR(&mdns_multicast, 224, 0, 0, 251); + ipaddr_any.addr = IPADDR_ANY; + + espconn_igmp_join(&ipaddr_any, &mdns_multicast); + + static esp_udp mdns_udp; + mdns_conn.type = ESPCONN_UDP; + mdns_conn.state = ESPCONN_NONE; + mdns_conn.proto.udp = &mdns_udp; + mdns_udp.local_port = 5353; + mdns_conn.reverse = NULL; + + espconn_regist_recvcb(&mdns_conn, mdns_udp_recv); + espconn_create(&mdns_conn); + + os_timer_disarm(&bc_timer); + if (bctime > 0) + { + os_timer_setfn(&bc_timer, (os_timer_func_t *)broadcast, (void *)0); + os_timer_arm(&bc_timer, bctime * 1000, 1); + } + + return 0; +} + +int ICACHE_FLASH_ATTR mdns_stop() +{ + if (!mdns_initialized) return(0); + os_timer_disarm(&bc_timer); + espconn_disconnect(&mdns_conn); + espconn_delete(&mdns_conn); + espconn_igmp_leave(&ipaddr_any, &mdns_multicast); + mdns_initialized = 0; + struct _host *h; + int i; + for (h = hosts, i = 0; i < MAX_HOSTS; i++, h++) + { + if (h->hostname) os_free(h->hostname); + if (h->mdns) os_free(h->mdns); + h->hostname = NULL; + h->mdns = NULL; + h->ip.addr = 0; + } + nhosts = 0; + return(0); +} \ No newline at end of file diff --git a/esp-link/mdns.h b/esp-link/mdns.h new file mode 100644 index 0000000..49273a1 --- /dev/null +++ b/esp-link/mdns.h @@ -0,0 +1,52 @@ +/* +* mdns.h +* +* Simple mDNS responder. +* It replys to mDNS IP (IPv4/A) queries and optionally broadcasts ip advertisements +* using the mDNS protocol +* Created on: Apr 10, 2015 +* Author: Kevin Uhlir (n0bel) +* +*/ + +#ifndef MDNS_H +#define MDNS_H + +/****************************************************************************** +* FunctionName : mdns_init +* Description : Initialize the module +* done once after the system is up and ready to run +* Parameters : bctime -- broadcast timer (seconds) can be 0 for no broadcasting +* ttl -- Time To Live must be > 0, should be a reasonable DNS ttl +* Returns : 0 success, -1 failed +*******************************************************************************/ +int mdns_init(int bctime, int ttl); +/****************************************************************************** +* FunctionName : mdns_stop +* Description : Stop the module. +* Parameters : None +* Returns : 0 success, -1 failed +*******************************************************************************/ +int mdns_stop(void); +/****************************************************************************** +* FunctionName : mdns_addhost +* Description : Add a host to be resolved by mDNS +* Parameters : hostname -- the hostname to add (must end in .local) +* for most clients to function +* ip -- the ip address of the host +* Returns : 0 success, -1 failed +*******************************************************************************/ +int mdns_addhost(char *hostname, struct ip_addr* ip); +/****************************************************************************** +* FunctionName : mdns_delhost +* Description : Delete a host to be resolved by mDNS +* Parameters : hostname -- the hostname to delete +* if NULL, hostname won't be used +* ip -- the ip address of the host to delete +* if NULL, ip won't be used +* Returns : 0 success, -1 failed +*******************************************************************************/ +int mdns_delhost(char *hostname, struct ip_addr* ip); + + +#endif /* MDNS_H */ \ No newline at end of file diff --git a/include/user_config.h b/include/user_config.h index 06d19eb..2fa067a 100644 --- a/include/user_config.h +++ b/include/user_config.h @@ -29,6 +29,7 @@ #define SERLED_DBG #undef SLIP_DBG #define UART_DBG +#define MDNS_DBG // If defined, the default hostname for DHCP will include the chip ID to make it unique #undef CHIP_IN_HOSTNAME From 26339962bf7240f99eb729ee45daffbe774089b3 Mon Sep 17 00:00:00 2001 From: Benjamin Runnels Date: Sun, 29 Nov 2015 08:05:02 -0600 Subject: [PATCH 05/19] Updates for SDK 1.5 --- Makefile | 8 +++---- cmd/cmd.c | 55 ++++++++++++++++++++++--------------------- cmd/handlers.c | 22 ++++++++--------- esp-link.vcxproj | 2 +- esp-link/cgiwifi.c | 23 +++++++----------- esp-link/mdns.c | 29 ++++++++++------------- include/user_config.h | 2 +- 7 files changed, 65 insertions(+), 76 deletions(-) diff --git a/Makefile b/Makefile index 78342c1..b3d9f1f 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,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.4.0) +SDK_BASE ?= $(abspath ../esp_iot_sdk_v1.5.0) # Esptool.py path and port, only used for 1-time serial flashing # Typically you'll use https://github.com/themadinventor/esptool @@ -185,7 +185,7 @@ MODULES += $(foreach sdir,$(LIBRARIES_DIR),$(wildcard $(sdir)/*)) EXTRA_INCDIR = include . # libraries used in this project, mainly provided by the SDK -LIBS = c gcc hal phy pp net80211 wpa main lwip +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 \ @@ -300,7 +300,7 @@ $(FW_BASE)/user1.bin: $(USER1_OUT) $(FW_BASE) $(Q) $(OBJCP) --only-section .rodata -O binary $(USER1_OUT) eagle.app.v6.rodata.bin $(Q) $(OBJCP) --only-section .irom0.text -O binary $(USER1_OUT) eagle.app.v6.irom0text.bin ls -ls eagle*bin - $(Q) COMPILE=gcc PATH=$(XTENSA_TOOLS_ROOT):$(PATH) python $(APPGEN_TOOL) $(USER1_OUT) 2 $(ESP_FLASH_MODE) $(ESP_FLASH_FREQ_DIV) $(ESP_SPI_SIZE) + $(Q) COMPILE=gcc PATH=$(XTENSA_TOOLS_ROOT):$(PATH) python $(APPGEN_TOOL) $(USER1_OUT) 2 $(ESP_FLASH_MODE) $(ESP_FLASH_FREQ_DIV) $(ESP_SPI_SIZE) 0 $(Q) rm -f eagle.app.v6.*.bin $(Q) mv eagle.app.flash.bin $@ @echo "** user1.bin uses $$(stat -c '%s' $@) bytes of" $(ESP_FLASH_MAX) "available" @@ -311,7 +311,7 @@ $(FW_BASE)/user2.bin: $(USER2_OUT) $(FW_BASE) $(Q) $(OBJCP) --only-section .data -O binary $(USER2_OUT) eagle.app.v6.data.bin $(Q) $(OBJCP) --only-section .rodata -O binary $(USER2_OUT) eagle.app.v6.rodata.bin $(Q) $(OBJCP) --only-section .irom0.text -O binary $(USER2_OUT) eagle.app.v6.irom0text.bin - $(Q) COMPILE=gcc PATH=$(XTENSA_TOOLS_ROOT):$(PATH) python $(APPGEN_TOOL) $(USER2_OUT) 2 $(ESP_FLASH_MODE) $(ESP_FLASH_FREQ_DIV) $(ESP_SPI_SIZE) + $(Q) COMPILE=gcc PATH=$(XTENSA_TOOLS_ROOT):$(PATH) python $(APPGEN_TOOL) $(USER2_OUT) 2 $(ESP_FLASH_MODE) $(ESP_FLASH_FREQ_DIV) $(ESP_SPI_SIZE) 0 $(Q) rm -f eagle.app.v6.*.bin $(Q) mv eagle.app.flash.bin $@ $(Q) if [ $$(stat -c '%s' $@) -gt $$(( $(ESP_FLASH_MAX) )) ]; then echo "$@ too big!"; false; fi diff --git a/cmd/cmd.c b/cmd/cmd.c index e7ec63f..291939a 100644 --- a/cmd/cmd.c +++ b/cmd/cmd.c @@ -7,8 +7,22 @@ #include "crc16.h" #include "uart.h" +#ifdef CMD_DBG +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) +#else +#define DBG(format, ...) do { } while(0) +#endif + extern const CmdList commands[]; +static const char *cmd_names[] = { + "NULL", "RESET", "IS_READY", "WIFI_CONNECT", + "MQTT_SETUP", "MQTT_CONNECT", "MQTT_DISCONNECT", + "MQTT_PUBLISH", "MQTT_SUBSCRIBE", "MQTT_LWT", "MQTT_EVENTS", + "REST_SETUP", "REST_REQUEST", "REST_SETHEADER", "REST_EVENTS", + "CB_ADD", "CB_EVENTS", +}; + //===== ESP -> Serial responses static void ICACHE_FLASH_ATTR @@ -82,39 +96,25 @@ CMD_Exec(const CmdList *scp, CmdPacket *packet) { // Iterate through the command table and call the appropriate function while (scp->sc_function != NULL) { if(scp->sc_name == packet->cmd) { - //os_printf("CMD: Dispatching cmd=%d\n", packet->cmd); + DBG("CMD_Exec: Dispatching cmd=%s\n", cmd_names[packet->cmd]); // call command function uint32_t ret = scp->sc_function(packet); // if requestor asked for a response, send it if (packet->_return){ -#ifdef CMD_DBG - os_printf("CMD: Response: 0x%lx, cmd: %d\r\n", ret, packet->cmd); -#endif + DBG("CMD_Exec: Response: 0x%lx, cmd: %d\r\n", ret, packet->cmd); crc = CMD_ResponseStart(packet->cmd, 0, ret, 0); CMD_ResponseEnd(crc); } else { -#ifdef CMD_DBG - os_printf("CMD: no response (%lu)\n", packet->_return); -#endif + DBG("CMD_Exec: no response (%lu)\n", packet->_return); } return ret; } scp++; } -#ifdef CMD_DBG - os_printf("CMD: cmd=%d not found\n", packet->cmd); -#endif + DBG("CMD_Exec: cmd=%d not found\n", packet->cmd); return 0; } -char *cmd_names[] = { - "NULL", "RESET", "IS_READY", "WIFI_CONNECT", - "MQTT_SETUP", "MQTT_CONNECT", "MQTT_DISCONNECT", - "MQTT_PUBLISH", "MQTT_SUBSCRIBE", "MQTT_LWT", "MQTT_EVENTS", - "REST_SETUP", "REST_REQUEST", "REST_SETHEADER", "REST_EVENTS", - "CB_ADD", "CB_EVENTS", -}; - // Parse a packet and print info about it void ICACHE_FLASH_ATTR CMD_parse_packet(uint8_t *buf, short len) { @@ -125,18 +125,21 @@ CMD_parse_packet(uint8_t *buf, short len) { CmdPacket *packet = (CmdPacket*)buf; uint8_t *data_ptr = (uint8_t*)&packet->args; uint8_t *data_limit = data_ptr+len; -#ifdef CMD_DBG uint16_t argn = 0; - os_printf("CMD: cmd=%d(%s) argc=%d cb=%p ret=%lu\n", - packet->cmd, cmd_names[packet->cmd], packet->argc, (void *)packet->callback, packet->_return); -#endif + DBG("CMD_parse_packet: cmd=%d(%s) argc=%d cb=%p ret=%lu\n", + packet->cmd, + cmd_names[packet->cmd], + packet->argc, + (void *)packet->callback, + packet->_return + ); #if 0 - // print out arguments + // print out arguments uint16_t argc = packet->argc; while (data_ptr+2 < data_limit && argc--) { short l = *(uint16_t*)data_ptr; - os_printf("CMD: arg[%d] len=%d:", argn++, l); + os_printf("CMD_parse_packet: arg[%d] len=%d:", argn++, l); data_ptr += 2; while (data_ptr < data_limit && l--) { os_printf(" %02X", *data_ptr++); @@ -148,9 +151,7 @@ CMD_parse_packet(uint8_t *buf, short len) { if (data_ptr <= data_limit) { CMD_Exec(commands, packet); } else { -#ifdef CMD_DBG - os_printf("CMD: packet length overrun, parsing arg %d\n", argn-1); -#endif + DBG("CMD_parse_packet: packet length overrun, parsing arg %d\n", argn - 1); } } diff --git a/cmd/handlers.c b/cmd/handlers.c index 9642856..5baf821 100644 --- a/cmd/handlers.c +++ b/cmd/handlers.c @@ -12,6 +12,12 @@ #include #endif +#ifdef CMD_DBG +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) +#else +#define DBG(format, ...) do { } while(0) +#endif + static uint32_t CMD_Null(CmdPacket *cmd); static uint32_t CMD_IsReady(CmdPacket *cmd); static uint32_t CMD_Reset(CmdPacket *cmd); @@ -81,9 +87,7 @@ CMD_AddCb(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; -#ifdef CMD_DBG - os_printf("CMD_AddCb: cb %s added at index %d\n", callbacks[i].name, i); -#endif + DBG("CMD_AddCb: cb %s added at index %d\n", callbacks[i].name, i); return 1; } } @@ -97,9 +101,7 @@ CMD_GetCbByName(char* name) { // (void *)callbacks[i].callback); // if callback doesn't exist or it's null if (os_strncmp(callbacks[i].name, name, CMD_CBNLEN) == 0) { -#ifdef CMD_DBG - os_printf("CMD_GetCbByName: cb %s found at index %d\n", name, i); -#endif + DBG("CMD_GetCbByName: cb %s found at index %d\n", name, i); return &callbacks[i]; } } @@ -111,9 +113,7 @@ CMD_GetCbByName(char* name) { static void ICACHE_FLASH_ATTR CMD_WifiCb(uint8_t wifiStatus) { if (wifiStatus != lastWifiStatus){ -#ifdef CMD_DBG - os_printf("CMD_WifiCb: wifiStatus=%d\n", wifiStatus); -#endif + DBG("CMD_WifiCb: wifiStatus=%d\n", wifiStatus); lastWifiStatus = wifiStatus; cmdCallback *wifiCb = CMD_GetCbByName("wifiCb"); if ((uint32_t)wifiCb->callback != -1) { @@ -160,9 +160,7 @@ CMD_AddCallback(CmdPacket *cmd) { if (len > 15) return 0; // max size of name is 15 characters if (CMD_PopArg(&req, (uint8_t *)name, len)) return 0; name[len] = 0; -#ifdef CMD_DBG - os_printf("CMD_AddCallback: name=%s\n", name); -#endif + DBG("CMD_AddCallback: name=%s\n", name); return CMD_AddCb(name, (uint32_t)cmd->callback); // save the sensor callback } diff --git a/esp-link.vcxproj b/esp-link.vcxproj index c753fd6..1230f90 100644 --- a/esp-link.vcxproj +++ b/esp-link.vcxproj @@ -132,7 +132,7 @@ __ets__;_STDINT_H;ICACHE_FLASH;__MINGW32__;__WIN32__;MQTT - .\rest;.\esp-link;.\mqtt;.\cmd;.\serial;.\user;.\espfs;.\httpd;.\include;..\esp_iot_sdk_v1.4.0\include;..\xtensa-lx106-elf\xtensa-lx106-elf\include;c:\tools\mingw64\x86_64-w64-mingw32\include;c:\tools\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.3\include + .\rest;.\esp-link;.\mqtt;.\cmd;.\serial;.\user;.\espfs;.\httpd;.\include;..\esp_iot_sdk_v1.5.0\include;..\xtensa-lx106-elf\xtensa-lx106-elf\include;c:\tools\mingw64\x86_64-w64-mingw32\include;c:\tools\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.3\include diff --git a/esp-link/cgiwifi.c b/esp-link/cgiwifi.c index 8032833..5297e54 100644 --- a/esp-link/cgiwifi.c +++ b/esp-link/cgiwifi.c @@ -115,25 +115,18 @@ wifiAddStateChangeCb(WifiStateChangeCb cb) { } static bool mdns_started = false; -//static struct mdns_info mdns_info; -// cannot allocate the info struct on the stack, it crashes! +// static ICACHE_FLASH_ATTR void wifiStartMDNS(struct ip_addr ip) { if (!mdns_started) { -// struct mdns_info *ard_mdns_info = (struct mdns_info *)os_zalloc(sizeof(struct mdns_info)); -// ard_mdns_info->host_name = flashConfig.hostname; -// ard_mdns_info->server_name = "arduino"; // service name -// ard_mdns_info->server_port = 2323; // service port -// ard_mdns_info->ipAddr = ip.addr; -// ard_mdns_info->txt_data[0] = (char *) "version = now"; -// espconn_mdns_init(ard_mdns_info); -// espconn_mdns_server_register(); - - mdns_init(60, 3600); - mdns_addhost(flashConfig.hostname, &ip); - //mdns_addhost("arduino", &ip); - mdns_started = true; + struct mdns_info *mdns_info = (struct mdns_info *)os_zalloc(sizeof(struct mdns_info)); + mdns_info->host_name = flashConfig.hostname; + mdns_info->server_name = "arduino"; // service name + mdns_info->server_port = 80; // service port + mdns_info->ipAddr = ip.addr; + espconn_mdns_init(mdns_info); + mdns_started = true; } } diff --git a/esp-link/mdns.c b/esp-link/mdns.c index 1cd504f..9455ce8 100644 --- a/esp-link/mdns.c +++ b/esp-link/mdns.c @@ -38,6 +38,7 @@ struct ip_addr mdns_multicast; struct ip_addr ipaddr_any; #ifdef MDNS_DBG + void ICACHE_FLASH_ATTR hexdump(char *desc, void *addr, int len) { int i; unsigned char buff[17]; @@ -118,8 +119,7 @@ unsigned ICACHE_FLASH_ATTR char * encodeResp(struct ip_addr * addr, char *name, *p++ = 0; *p++ = 4; // length (of ip) - memcpy(p, addr, 4); - + memcpy(p, addr, 4); return data; } @@ -156,7 +156,7 @@ void ICACHE_FLASH_ATTR sendOne(struct _host *h) mdns_conn.proto.udp->remote_port = 5353; mdns_conn.proto.udp->local_port = 5353; #ifdef MDNS_DBG - hexdump("sending", h->mdns, h->len); +// hexdump("sending", h->mdns, h->len); #endif espconn_sent(&mdns_conn, h->mdns, h->len); @@ -184,7 +184,7 @@ void ICACHE_FLASH_ATTR decodeQuery(unsigned char *data) int qtype = p[0] * 256 + p[1]; int qclass = p[2] * 256 + p[3]; p += 4; - DBG("decoded name %s qtype=%d qclass=%d\n", name, qtype, qclass); +// DBG("decoded name %s qtype=%d qclass=%d\n", name, qtype, qclass); if (qtype == 1 && (qclass & 0x7fff) == 1) { @@ -194,7 +194,7 @@ void ICACHE_FLASH_ATTR decodeQuery(unsigned char *data) { if (h->hostname && strcmp(name, h->hostname) == 0) { - DBG("its %s!\n", h->hostname); +// DBG("its %s!\n", h->hostname); sendOne(h); } } @@ -211,7 +211,7 @@ static void ICACHE_FLASH_ATTR broadcast(void *arg) int i; for (h = hosts, i = 0; i < nhosts; i++, h++) { - DBG("broadcast for %s\n", h->hostname); +// DBG("broadcast for %s\n", h->hostname); sendOne(h); } } @@ -220,12 +220,12 @@ static void ICACHE_FLASH_ATTR mdns_udp_recv(void *arg, char *pusrdata, unsigned unsigned char *data = (unsigned char *)pusrdata; - DBG("remote port=%d remoteip=%d.%d.%d.%d\n", mdns_conn.proto.udp->remote_port, - mdns_conn.proto.udp->remote_ip[0], - mdns_conn.proto.udp->remote_ip[1], - mdns_conn.proto.udp->remote_ip[2], - mdns_conn.proto.udp->remote_ip[3] - ); +// DBG("remote port=%d remoteip=%d.%d.%d.%d\n", mdns_conn.proto.udp->remote_port, +// mdns_conn.proto.udp->remote_ip[0], +// mdns_conn.proto.udp->remote_ip[1], +// mdns_conn.proto.udp->remote_ip[2], +// mdns_conn.proto.udp->remote_ip[3] +// ); #ifdef MDNS_DBG // hexdump("rec mNDS:", data, length); #endif @@ -252,6 +252,7 @@ int ICACHE_FLASH_ATTR mdns_addhost(char *hn, struct ip_addr* ip) os_strcpy(h->hostname, hn); h->ip.addr = ip->addr; h->mdns = encodeResp(ip, hn, &(h->len)); +// DBG("resp len: %d\n", ets_strlen(h->mdns)); #ifdef MDNS_DBG hexdump("addhost", hosts, sizeof(hosts)); #endif @@ -270,8 +271,6 @@ int ICACHE_FLASH_ATTR mdns_delhost(char *hn, struct ip_addr* ip) { if (h->hostname) os_free(h->hostname); if (h->mdns) os_free(h->mdns); - h->hostname = NULL; - h->mdns = NULL; h->ip.addr = 0; } } @@ -326,8 +325,6 @@ int ICACHE_FLASH_ATTR mdns_stop() { if (h->hostname) os_free(h->hostname); if (h->mdns) os_free(h->mdns); - h->hostname = NULL; - h->mdns = NULL; h->ip.addr = 0; } nhosts = 0; diff --git a/include/user_config.h b/include/user_config.h index 2fa067a..02c0a0c 100644 --- a/include/user_config.h +++ b/include/user_config.h @@ -19,7 +19,7 @@ #define CONFIG_DBG #define LOG_DBG #define STATUS_DBG -#define HTTPD_DBG +#undef HTTPD_DBG #define MQTT_DBG #define MQTTCMD_DBG #undef PKTBUF_DBG From a0451b2b7b0cfb0c7f2e6e6348b54898d111e2ac Mon Sep 17 00:00:00 2001 From: Benjamin Runnels Date: Sun, 29 Nov 2015 09:02:47 -0600 Subject: [PATCH 06/19] renamed debug command for consistancy --- esp-link.vcxproj | 2 +- mqtt/mqtt_cmd.c | 30 +++++++++++++++--------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/esp-link.vcxproj b/esp-link.vcxproj index 1230f90..5d723c6 100644 --- a/esp-link.vcxproj +++ b/esp-link.vcxproj @@ -131,7 +131,7 @@ - __ets__;_STDINT_H;ICACHE_FLASH;__MINGW32__;__WIN32__;MQTT + __ets__;_STDINT_H;ICACHE_FLASH;__MINGW32__;__WIN32__;MQTT;REST .\rest;.\esp-link;.\mqtt;.\cmd;.\serial;.\user;.\espfs;.\httpd;.\include;..\esp_iot_sdk_v1.5.0\include;..\xtensa-lx106-elf\xtensa-lx106-elf\include;c:\tools\mingw64\x86_64-w64-mingw32\include;c:\tools\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.3\include diff --git a/mqtt/mqtt_cmd.c b/mqtt/mqtt_cmd.c index 503b41e..4260be9 100644 --- a/mqtt/mqtt_cmd.c +++ b/mqtt/mqtt_cmd.c @@ -8,9 +8,9 @@ #include "mqtt_cmd.h" #ifdef MQTTCMD_DBG -#define DBG_MQTTCMD(format, ...) os_printf(format, ## __VA_ARGS__) +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) #else -#define DBG_MQTTCMD(format, ...) do { } while(0) +#define DBG(format, ...) do { } while(0) #endif // if MQTT_1_CLIENT is defined we only support the one client that is built into esp-link. @@ -25,7 +25,7 @@ void ICACHE_FLASH_ATTR cmdMqttConnectedCb(uint32_t* args) { MQTT_Client* client = (MQTT_Client*)args; MqttCmdCb* cb = (MqttCmdCb*)client->user_data; - DBG_MQTTCMD("MQTT: Connected connectedCb=%p, disconnectedCb=%p, publishedCb=%p, dataCb=%p\n", + DBG("MQTT: Connected connectedCb=%p, disconnectedCb=%p, publishedCb=%p, dataCb=%p\n", (void*)cb->connectedCb, (void*)cb->disconnectedCb, (void*)cb->publishedCb, @@ -38,7 +38,7 @@ void ICACHE_FLASH_ATTR cmdMqttDisconnectedCb(uint32_t* args) { MQTT_Client* client = (MQTT_Client*)args; MqttCmdCb* cb = (MqttCmdCb*)client->user_data; - DBG_MQTTCMD("MQTT: Disconnected\n"); + DBG("MQTT: Disconnected\n"); uint16_t crc = CMD_ResponseStart(CMD_MQTT_EVENTS, cb->disconnectedCb, 0, 0); CMD_ResponseEnd(crc); } @@ -47,7 +47,7 @@ void ICACHE_FLASH_ATTR cmdMqttPublishedCb(uint32_t* args) { MQTT_Client* client = (MQTT_Client*)args; MqttCmdCb* cb = (MqttCmdCb*)client->user_data; - DBG_MQTTCMD("MQTT: Published\n"); + DBG("MQTT: Published\n"); uint16_t crc = CMD_ResponseStart(CMD_MQTT_EVENTS, cb->publishedCb, 0, 0); CMD_ResponseEnd(crc); } @@ -79,7 +79,7 @@ MQTTCMD_Lwt(CmdPacket *cmd) { MQTT_Client* client = &mqttClient; #else MQTT_Client* client = (MQTT_Client*)client_ptr; - DBG_MQTTCMD("MQTT: MQTTCMD_Lwt client ptr=%p\n", (void*)client_ptr); + DBG("MQTT: MQTTCMD_Lwt client ptr=%p\n", (void*)client_ptr); #endif // free old topic & message @@ -110,7 +110,7 @@ MQTTCMD_Lwt(CmdPacket *cmd) { // get retain CMD_PopArg(&req, (uint8_t*)&client->connect_info.will_retain, 4); - DBG_MQTTCMD("MQTT: MQTTCMD_Lwt topic=%s, message=%s, qos=%d, retain=%d\n", + DBG("MQTT: MQTTCMD_Lwt topic=%s, message=%s, qos=%d, retain=%d\n", client->connect_info.will_topic, client->connect_info.will_message, client->connect_info.will_qos, @@ -136,7 +136,7 @@ MQTTCMD_Publish(CmdPacket *cmd) { MQTT_Client* client = &mqttClient; #else MQTT_Client* client = (MQTT_Client*)client_ptr; - DBG_MQTTCMD("MQTT: MQTTCMD_Publish client ptr=%p\n", (void*)client_ptr); + DBG("MQTT: MQTTCMD_Publish client ptr=%p\n", (void*)client_ptr); #endif uint16_t len; @@ -170,7 +170,7 @@ MQTTCMD_Publish(CmdPacket *cmd) { // get retain CMD_PopArg(&req, (uint8_t*)&retain, 4); - DBG_MQTTCMD("MQTT: MQTTCMD_Publish topic=%s, data_len=%d, qos=%ld, retain=%ld\n", + DBG("MQTT: MQTTCMD_Publish topic=%s, data_len=%d, qos=%ld, retain=%ld\n", topic, os_strlen((char*)data), qos, @@ -197,7 +197,7 @@ MQTTCMD_Subscribe(CmdPacket *cmd) { MQTT_Client* client = &mqttClient; #else MQTT_Client* client = (MQTT_Client*)client_ptr; - DBG_MQTTCMD("MQTT: MQTTCMD_Subscribe client ptr=%p\n", (void*)client_ptr); + DBG("MQTT: MQTTCMD_Subscribe client ptr=%p\n", (void*)client_ptr); #endif uint16_t len; @@ -213,7 +213,7 @@ MQTTCMD_Subscribe(CmdPacket *cmd) { uint32_t qos = 0; CMD_PopArg(&req, (uint8_t*)&qos, 4); - DBG_MQTTCMD("MQTT: MQTTCMD_Subscribe topic=%s, qos=%ld\n", topic, qos); + DBG("MQTT: MQTTCMD_Subscribe topic=%s, qos=%ld\n", topic, qos); MQTT_Subscribe(client, (char*)topic, (uint8_t)qos); os_free(topic); @@ -273,7 +273,7 @@ MQTTCMD_Setup(CmdPacket *cmd) { // get clean session CMD_PopArg(&req, (uint8_t*)&clean_session, 4); #ifdef MQTTCMD_DBG - DBG_MQTTCMD("MQTT: MQTTCMD_Setup clientid=%s, user=%s, pw=%s, keepalive=%ld, clean_session=%ld\n", client_id, user_data, pass_data, keepalive, clean_session); + DBG("MQTT: MQTTCMD_Setup clientid=%s, user=%s, pw=%s, keepalive=%ld, clean_session=%ld\n", client_id, user_data, pass_data, keepalive, clean_session); #endif // init client @@ -324,7 +324,7 @@ MQTTCMD_Connect(CmdPacket *cmd) { uint32_t client_ptr; CMD_PopArg(&req, (uint8_t*)&client_ptr, 4); MQTT_Client* client = (MQTT_Client*)client_ptr; - DBG_MQTTCMD("MQTT: MQTTCMD_Connect client ptr=%p\n", (void*)client_ptr); + DBG("MQTT: MQTTCMD_Connect client ptr=%p\n", (void*)client_ptr); uint16_t len; @@ -342,7 +342,7 @@ MQTTCMD_Connect(CmdPacket *cmd) { // get security CMD_PopArg(&req, (uint8_t*)&client->security, 4); - DBG_MQTTCMD("MQTT: MQTTCMD_Connect host=%s, port=%d, security=%d\n", + DBG("MQTT: MQTTCMD_Connect host=%s, port=%d, security=%d\n", client->host, client->port, client->security); @@ -368,7 +368,7 @@ MQTTCMD_Disconnect(CmdPacket *cmd) { uint32_t client_ptr; CMD_PopArg(&req, (uint8_t*)&client_ptr, 4); MQTT_Client* client = (MQTT_Client*)client_ptr; - DBG_MQTTCMD("MQTT: MQTTCMD_Disconnect client ptr=%p\n", (void*)client_ptr); + DBG("MQTT: MQTTCMD_Disconnect client ptr=%p\n", (void*)client_ptr); // disconnect MQTT_Disconnect(client); From 25ef0e15783b212e9d79cf3e8a30acbf7fdca1a0 Mon Sep 17 00:00:00 2001 From: Benjamin Runnels Date: Sun, 29 Nov 2015 09:16:30 -0600 Subject: [PATCH 07/19] Fixed bug where callbacks were not getting called if the CMD MQTT client called connect while the ESP MQTT client was connected or disconnected. --- mqtt/mqtt_cmd.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mqtt/mqtt_cmd.c b/mqtt/mqtt_cmd.c index 4260be9..3cca79b 100644 --- a/mqtt/mqtt_cmd.c +++ b/mqtt/mqtt_cmd.c @@ -314,6 +314,14 @@ MQTTCMD_Connect(CmdPacket *cmd) { CMD_Request(&req, cmd); #ifdef MQTT_1_CLIENT + + if (mqttClient.connState == MQTT_CONNECTED && mqttClient.cmdConnectedCb) { + mqttClient.cmdConnectedCb((uint32_t*)&mqttClient); + } + else if (mqttClient.connState == MQTT_DISCONNECTED && mqttClient.cmdDisconnectedCb) { + mqttClient.cmdDisconnectedCb((uint32_t*)&mqttClient); + } + return 1; #else From b8567ad2ef94465a1aedf09c4e623478f72a1461 Mon Sep 17 00:00:00 2001 From: Benjamin Runnels Date: Tue, 1 Dec 2015 15:20:20 -0600 Subject: [PATCH 08/19] Updated DBG macros as suggested by susisstrolch --- cmd/cmd.c | 2 +- cmd/handlers.c | 2 +- esp-link/cgioptiboot.c | 4 +--- esp-link/cgiwifi.c | 2 +- esp-link/mdns.c | 2 +- httpd/httpd.c | 2 +- include/user_config.h | 1 + mqtt/mqtt_cmd.c | 2 +- serial/slip.c | 2 +- 9 files changed, 9 insertions(+), 10 deletions(-) diff --git a/cmd/cmd.c b/cmd/cmd.c index 291939a..f1a7da9 100644 --- a/cmd/cmd.c +++ b/cmd/cmd.c @@ -8,7 +8,7 @@ #include "uart.h" #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/cmd/handlers.c b/cmd/handlers.c index 5baf821..35d19ee 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/cgioptiboot.c b/esp-link/cgioptiboot.c index 47cdea2..c266014 100644 --- a/esp-link/cgioptiboot.c +++ b/esp-link/cgioptiboot.c @@ -16,10 +16,8 @@ #define PGM_TIMEOUT 20000 // timeout when sync is achieved, in milliseconds #define PGM_INTERVAL 200 // send sync at this interval in ms when in programming mode -#define OPTIBOOT_DBG -#undef DBG #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/cgiwifi.c b/esp-link/cgiwifi.c index 5297e54..1b7f71c 100644 --- a/esp-link/cgiwifi.c +++ b/esp-link/cgiwifi.c @@ -23,7 +23,7 @@ Cgi/template routines for the /wifi url. #include "mdns.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 diff --git a/esp-link/mdns.c b/esp-link/mdns.c index 9455ce8..a6039bb 100644 --- a/esp-link/mdns.c +++ b/esp-link/mdns.c @@ -13,7 +13,7 @@ #include "mdns.h" #ifdef MDNS_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/httpd/httpd.c b/httpd/httpd.c index f56e579..e9fa6c5 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/include/user_config.h b/include/user_config.h index 02c0a0c..853dc8a 100644 --- a/include/user_config.h +++ b/include/user_config.h @@ -30,6 +30,7 @@ #undef SLIP_DBG #define UART_DBG #define MDNS_DBG +#define OPTIBOOT_DBG // If defined, the default hostname for DHCP will include the chip ID to make it unique #undef CHIP_IN_HOSTNAME diff --git a/mqtt/mqtt_cmd.c b/mqtt/mqtt_cmd.c index 3cca79b..0d7e65a 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/serial/slip.c b/serial/slip.c index b9446e3..866c811 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 From e39ee808c960666fc2e7fd1aac8afa88268f74f2 Mon Sep 17 00:00:00 2001 From: Benjamin Runnels Date: Tue, 1 Dec 2015 15:26:11 -0600 Subject: [PATCH 09/19] DOH! missing semicolon --- cmd/cmd.c | 2 +- cmd/handlers.c | 2 +- esp-link/cgioptiboot.c | 2 +- esp-link/cgiwifi.c | 2 +- esp-link/log.c | 2 +- esp-link/mdns.c | 2 +- esp-link/mqtt_client.c | 10 +++++----- httpd/httpd.c | 2 +- mqtt/mqtt_cmd.c | 2 +- serial/slip.c | 2 +- 10 files changed, 14 insertions(+), 14 deletions(-) diff --git a/cmd/cmd.c b/cmd/cmd.c index f1a7da9..e8fd745 100644 --- a/cmd/cmd.c +++ b/cmd/cmd.c @@ -8,7 +8,7 @@ #include "uart.h" #ifdef CMD_DBG -#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__) } while(0) +#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) #else #define DBG(format, ...) do { } while(0) #endif diff --git a/cmd/handlers.c b/cmd/handlers.c index 35d19ee..4d20e4d 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, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) #else #define DBG(format, ...) do { } while(0) #endif diff --git a/esp-link/cgioptiboot.c b/esp-link/cgioptiboot.c index c266014..6d8b71c 100644 --- a/esp-link/cgioptiboot.c +++ b/esp-link/cgioptiboot.c @@ -17,7 +17,7 @@ #define PGM_INTERVAL 200 // send sync at this interval in ms when in programming mode #ifdef OPTIBOOT_DBG -#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__) } while(0) +#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) #else #define DBG(format, ...) do { } while(0) #endif diff --git a/esp-link/cgiwifi.c b/esp-link/cgiwifi.c index 1b7f71c..a3f3a42 100644 --- a/esp-link/cgiwifi.c +++ b/esp-link/cgiwifi.c @@ -23,7 +23,7 @@ Cgi/template routines for the /wifi url. #include "mdns.h" #ifdef CGIWIFI_DBG -#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__) } while(0) +#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) #else #define DBG(format, ...) do { } while(0) #endif 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/mdns.c b/esp-link/mdns.c index a6039bb..89ae337 100644 --- a/esp-link/mdns.c +++ b/esp-link/mdns.c @@ -13,7 +13,7 @@ #include "mdns.h" #ifdef MDNS_DBG -#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__) } while(0) +#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) #else #define DBG(format, ...) do { } while(0) #endif diff --git a/esp-link/mqtt_client.c b/esp-link/mqtt_client.c index 431e5f0..12b9a8d 100644 --- a/esp-link/mqtt_client.c +++ b/esp-link/mqtt_client.c @@ -5,9 +5,9 @@ #include "mqtt.h" #ifdef MQTTCLIENT_DBG -#define DBG_MQTTCLIENT(format, ...) os_printf(format, ## __VA_ARGS__) +#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__) } while(0) #else -#define DBG_MQTTCLIENT(format, ...) do { } while(0) +#define DBG(format, ...) do { } while(0) #endif MQTT_Client mqttClient; // main mqtt client used by esp-link @@ -19,7 +19,7 @@ static MqttDataCallback data_cb; void ICACHE_FLASH_ATTR mqttConnectedCb(uint32_t *args) { - DBG_MQTTCLIENT("MQTT Client: Connected\n"); + DBG("MQTT Client: Connected\n"); //MQTT_Client* client = (MQTT_Client*)args; //MQTT_Subscribe(client, "system/time", 0); // handy for testing if (connected_cb) @@ -29,7 +29,7 @@ mqttConnectedCb(uint32_t *args) { void ICACHE_FLASH_ATTR mqttDisconnectedCb(uint32_t *args) { // MQTT_Client* client = (MQTT_Client*)args; - DBG_MQTTCLIENT("MQTT Client: Disconnected\n"); + DBG("MQTT Client: Disconnected\n"); if (disconnected_cb) disconnected_cb(args); } @@ -37,7 +37,7 @@ mqttDisconnectedCb(uint32_t *args) { void ICACHE_FLASH_ATTR mqttPublishedCb(uint32_t *args) { // MQTT_Client* client = (MQTT_Client*)args; - DBG_MQTTCLIENT("MQTT Client: Published\n"); + DBG("MQTT Client: Published\n"); if (published_cb) published_cb(args); } diff --git a/httpd/httpd.c b/httpd/httpd.c index e9fa6c5..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, ...) do { os_printf(format, ## __VA_ARGS__) } while(0) +#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) #else #define DBG(format, ...) do { } while(0) #endif diff --git a/mqtt/mqtt_cmd.c b/mqtt/mqtt_cmd.c index 0d7e65a..85cdfa1 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, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) #else #define DBG(format, ...) do { } while(0) #endif diff --git a/serial/slip.c b/serial/slip.c index 866c811..b78369b 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, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) #else #define DBG(format, ...) do { } while(0) #endif From 59eb02daa9fd95fdda31928d1c8849f6dc829226 Mon Sep 17 00:00:00 2001 From: Benjamin Runnels Date: Thu, 3 Dec 2015 23:43:19 -0600 Subject: [PATCH 10/19] Added in sntp update --- cmd/cmd.c | 16 ++++++++-------- esp-link/cgiwifi.c | 40 ++++++++++++++++++++++++++++++++++++++-- esp-link/main.c | 2 +- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/cmd/cmd.c b/cmd/cmd.c index e8fd745..8f04faf 100644 --- a/cmd/cmd.c +++ b/cmd/cmd.c @@ -9,12 +9,6 @@ #ifdef CMD_DBG #define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) -#else -#define DBG(format, ...) do { } while(0) -#endif - -extern const CmdList commands[]; - static const char *cmd_names[] = { "NULL", "RESET", "IS_READY", "WIFI_CONNECT", "MQTT_SETUP", "MQTT_CONNECT", "MQTT_DISCONNECT", @@ -22,6 +16,11 @@ static const char *cmd_names[] = { "REST_SETUP", "REST_REQUEST", "REST_SETHEADER", "REST_EVENTS", "CB_ADD", "CB_EVENTS", }; +#else +#define DBG(format, ...) do { } while(0) +#endif + +extern const CmdList commands[]; //===== ESP -> Serial responses @@ -125,7 +124,7 @@ CMD_parse_packet(uint8_t *buf, short len) { CmdPacket *packet = (CmdPacket*)buf; uint8_t *data_ptr = (uint8_t*)&packet->args; uint8_t *data_limit = data_ptr+len; - uint16_t argn = 0; + DBG("CMD_parse_packet: cmd=%d(%s) argc=%d cb=%p ret=%lu\n", packet->cmd, cmd_names[packet->cmd], @@ -136,6 +135,7 @@ CMD_parse_packet(uint8_t *buf, short len) { #if 0 // print out arguments + uint16_t argn = 0; uint16_t argc = packet->argc; while (data_ptr+2 < data_limit && argc--) { short l = *(uint16_t*)data_ptr; @@ -151,7 +151,7 @@ CMD_parse_packet(uint8_t *buf, short len) { if (data_ptr <= data_limit) { CMD_Exec(commands, packet); } else { - DBG("CMD_parse_packet: packet length overrun, parsing arg %d\n", argn - 1); + DBG("CMD_parse_packet: packet length overrun, parsing arg %d\n", packet->argc); } } diff --git a/esp-link/cgiwifi.c b/esp-link/cgiwifi.c index a3f3a42..9fe6fe1 100644 --- a/esp-link/cgiwifi.c +++ b/esp-link/cgiwifi.c @@ -20,7 +20,7 @@ Cgi/template routines for the /wifi url. #include "status.h" #include "config.h" #include "log.h" -#include "mdns.h" +#include "sntp.h" #ifdef CGIWIFI_DBG #define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) @@ -29,6 +29,7 @@ Cgi/template routines for the /wifi url. #endif static void wifiStartMDNS(struct ip_addr); +static void wifiStartSNTP(); // ===== wifi status change callbacks static WifiStateChangeCb wifi_state_change_cb[4]; @@ -84,6 +85,7 @@ static void ICACHE_FLASH_ATTR wifiHandleEventCb(System_Event_t *evt) { IP2STR(&evt->event_info.got_ip.gw)); statusWifiUpdate(wifiState); wifiStartMDNS(evt->event_info.got_ip.ip); + wifiStartSNTP(); break; case EVENT_SOFTAPMODE_STACONNECTED: DBG("Wifi AP: station " MACSTR " joined, AID = %d\n", @@ -116,7 +118,6 @@ wifiAddStateChangeCb(WifiStateChangeCb cb) { static bool mdns_started = false; -// static ICACHE_FLASH_ATTR void wifiStartMDNS(struct ip_addr ip) { if (!mdns_started) { @@ -130,6 +131,41 @@ void wifiStartMDNS(struct ip_addr ip) { } } +static bool sntp_started = false; + +static ETSTimer sntp_timer; + +void ICACHE_FLASH_ATTR +user_check_sntp_stamp(void *arg){ + uint32 current_stamp; + current_stamp = sntp_get_current_timestamp(); + if (current_stamp == 0){ + os_timer_arm(&sntp_timer, 100, 0); + } + else{ + os_timer_disarm(&sntp_timer); + os_printf("sntp: %d, %s \n", current_stamp, sntp_get_real_time(current_stamp)); + } +} + +static ICACHE_FLASH_ATTR +void wifiStartSNTP() { + if (!sntp_started) { + ip_addr_t *addr = (ip_addr_t *)os_zalloc(sizeof(ip_addr_t)); + sntp_setservername(0, "us.pool.ntp.org"); // set server 0 by domain name + sntp_setservername(1, "ntp.sjtu.edu.cn"); // set server 1 by domain name + IP4_ADDR(addr, 210, 72, 145, 44); + sntp_setserver(2, addr); // set server 2 by IP address + sntp_init(); + os_free(addr); + + os_timer_disarm(&sntp_timer); + os_timer_setfn(&sntp_timer, (os_timer_func_t *)user_check_sntp_stamp, NULL); + os_timer_arm(&sntp_timer, 100, 0); + sntp_started = true; + } +} + // ===== wifi scanning //WiFi access point data diff --git a/esp-link/main.c b/esp-link/main.c index 3719d9c..6819046 100644 --- a/esp-link/main.c +++ b/esp-link/main.c @@ -29,7 +29,7 @@ #include "console.h" #include "config.h" #include "log.h" -#include +#include "gpio.h" static int ICACHE_FLASH_ATTR cgiSystemInfo(HttpdConnData *connData); static int ICACHE_FLASH_ATTR cgiSystemSet(HttpdConnData *connData); From bfee1cf6940134106d57c7de160d0f0f9a374aca Mon Sep 17 00:00:00 2001 From: Benjamin Runnels Date: Mon, 7 Dec 2015 07:03:10 -0600 Subject: [PATCH 11/19] fixes for services --- esp-link/cgiservices.c | 61 +++++++++++++++++++++++------------------- esp-link/cgiservices.h | 1 + esp-link/cgiwifi.c | 25 ++++++++++------- esp-link/main.c | 2 +- html/services.js | 10 ++++--- syslog/syslog.c | 9 ++++--- syslog/syslog.h | 2 +- 7 files changed, 63 insertions(+), 47 deletions(-) diff --git a/esp-link/cgiservices.c b/esp-link/cgiservices.c index 775461e..4b4721c 100644 --- a/esp-link/cgiservices.c +++ b/esp-link/cgiservices.c @@ -91,13 +91,14 @@ int ICACHE_FLASH_ATTR cgiSystemInfo(HttpdConnData *connData) { return HTTPD_CGI_DONE; } -static void ICACHE_FLASH_ATTR cgiServicesSNTPInit() { - if (flashConfig.sntp_server[0]) { - DBG("SNTP timesource set to %s with offset %d\n", flashConfig.sntp_server, flashConfig.timezone_offset); +void ICACHE_FLASH_ATTR cgiServicesSNTPInit() { + if (flashConfig.sntp_server[0] != '\0') { sntp_stop(); - sntp_setservername(0, flashConfig.sntp_server); - sntp_set_timezone(flashConfig.timezone_offset); - sntp_init(); + if (true == sntp_set_timezone(flashConfig.timezone_offset)) { + sntp_setservername(0, flashConfig.sntp_server); + sntp_init(); + } + DBG("SNTP timesource set to %s with offset %d\n", flashConfig.sntp_server, flashConfig.timezone_offset); } } @@ -162,14 +163,33 @@ 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; - mdns |= getStringArg(connData, "mdns_servername", flashConfig.mdns_servername, sizeof(flashConfig.mdns_servername)); - if (mdns < 0) return HTTPD_CGI_DONE; - - if (syslog > 0) { - if (!flashConfig.syslog_enable) { - flashConfig.syslog_host[0] = '\0'; + if (mdns < 0) + return HTTPD_CGI_DONE; + else if (flashConfig.mdns_enable){ + struct ip_info ipconfig; + wifi_get_ip_info(STATION_IF, &ipconfig); + + if (wifiState == wifiGotIP && ipconfig.ip.addr != 0) { + wifiStartMDNS(ipconfig.ip); } + } + else { + espconn_mdns_server_unregister(); + espconn_mdns_close(); + mdns_started = true; + } + + mdns |= getStringArg(connData, "mdns_servername", flashConfig.mdns_servername, sizeof(flashConfig.mdns_servername)); + if (mdns < 0) + return HTTPD_CGI_DONE; + else if(mdns_started) { + espconn_mdns_server_unregister(); + espconn_mdns_close(); + espconn_mdns_set_servername(flashConfig.mdns_servername); + espconn_mdns_server_register(); + } + + if (syslog > 0) { syslog_init(flashConfig.syslog_host); } @@ -177,21 +197,6 @@ int ICACHE_FLASH_ATTR cgiServicesSet(HttpdConnData *connData) { cgiServicesSNTPInit(); } - if (mdns > 0) { - espconn_mdns_disable(); - if (flashConfig.mdns_enable) { - struct ip_info ipconfig; - wifi_get_ip_info(STATION_IF, &ipconfig); - mdns_started = false; - if (wifiState == wifiGotIP && ipconfig.ip.addr != 0) { - wifiStartMDNS(ipconfig.ip); - } - } - else { - mdns_started = true; - } - } - if (configSave()) { httpdStartResponse(connData, 204); httpdEndHeaders(connData); diff --git a/esp-link/cgiservices.h b/esp-link/cgiservices.h index 7c34af2..9c44242 100644 --- a/esp-link/cgiservices.h +++ b/esp-link/cgiservices.h @@ -6,6 +6,7 @@ int cgiSystemSet(HttpdConnData *connData); int cgiSystemInfo(HttpdConnData *connData); +void cgiServicesSNTPInit(); int cgiServicesInfo(HttpdConnData *connData); int cgiServicesSet(HttpdConnData *connData); diff --git a/esp-link/cgiwifi.c b/esp-link/cgiwifi.c index 1efa5d5..de49938 100644 --- a/esp-link/cgiwifi.c +++ b/esp-link/cgiwifi.c @@ -60,28 +60,29 @@ static void ICACHE_FLASH_ATTR wifiHandleEventCb(System_Event_t *evt) { wifiState = wifiIsConnected; wifiReason = 0; DBG("Wifi connected to ssid %s, ch %d\n", evt->event_info.connected.ssid, - evt->event_info.connected.channel); + 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", - evt->event_info.disconnected.ssid, wifiGetReason(), evt->event_info.disconnected.reason); + 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", - evt->event_info.auth_change.old_mode, evt->event_info.auth_change.new_mode); + 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", - IP2STR(&evt->event_info.got_ip.ip), IP2STR(&evt->event_info.got_ip.mask), - IP2STR(&evt->event_info.got_ip.gw)); + IP2STR(&evt->event_info.got_ip.ip), IP2STR(&evt->event_info.got_ip.mask), + IP2STR(&evt->event_info.got_ip.gw)); statusWifiUpdate(wifiState); - wifiStartMDNS(evt->event_info.got_ip.ip); + if (!mdns_started) + wifiStartMDNS(evt->event_info.got_ip.ip); break; case EVENT_SOFTAPMODE_STACONNECTED: DBG("Wifi AP: station " MACSTR " joined, AID = %d\n", @@ -112,15 +113,19 @@ void ICACHE_FLASH_ATTR wifiAddStateChangeCb(WifiStateChangeCb cb) { } void ICACHE_FLASH_ATTR wifiStartMDNS(struct ip_addr ip) { - if (!mdns_started) { + if (flashConfig.mdns_enable) { struct mdns_info *mdns_info = (struct mdns_info *)os_zalloc(sizeof(struct mdns_info)); mdns_info->host_name = flashConfig.hostname; - mdns_info->server_name = flashConfig.mdns_servername; // service name - mdns_info->server_port = 80; // service port + mdns_info->server_name = flashConfig.mdns_servername; + mdns_info->server_port = 80; mdns_info->ipAddr = ip.addr; espconn_mdns_init(mdns_info); - mdns_started = true; } + else { + espconn_mdns_server_unregister(); + espconn_mdns_close(); + } + mdns_started = true; } // ===== wifi scanning diff --git a/esp-link/main.c b/esp-link/main.c index 7e1ed1c..8d10eef 100644 --- a/esp-link/main.c +++ b/esp-link/main.c @@ -176,7 +176,7 @@ void user_init(void) { fid & 0xff, (fid&0xff00)|((fid>>16)&0xff)); NOTICE("** esp-link ready"); - //enableSNTP(); + cgiServicesSNTPInit(); #ifdef MQTT NOTICE("initializing MQTT"); diff --git a/html/services.js b/html/services.js index c6ae526..5b2395d 100644 --- a/html/services.js +++ b/html/services.js @@ -1,9 +1,11 @@ function changeServices(e) { e.preventDefault(); var url = "services/update?1=1"; - var i, inputs = document.querySelectorAll('#' + e.srcElement.id + ' input'); + var i, inputs = document.querySelectorAll("#" + e.srcElement.id + " input"); for (i = 0; i < inputs.length; i++) { if (inputs[i].type == "checkbox") { + if (inputs[i].name.slice(-6) == "enable") + continue; var val = (inputs[i].checked) ? 1 : 0; url += "&" + inputs[i].name + "=" + val; } @@ -14,13 +16,13 @@ function changeServices(e) { hideWarning(); var n = e.srcElement.id.replace("-form", ""); var cb = $("#" + n + "-button"); - addClass(cb, 'pure-button-disabled'); + addClass(cb, "pure-button-disabled"); ajaxSpin("POST", url, function (resp) { showNotification(n + " updated"); - removeClass(cb, 'pure-button-disabled'); + removeClass(cb, "pure-button-disabled"); }, function (s, st) { showWarning("Error: " + st); - removeClass(cb, 'pure-button-disabled'); + removeClass(cb, "pure-button-disabled"); window.setTimeout(fetchServices, 100); }); } diff --git a/syslog/syslog.c b/syslog/syslog.c index cdbfef6..cfb9528 100644 --- a/syslog/syslog.c +++ b/syslog/syslog.c @@ -234,12 +234,15 @@ static void ICACHE_FLASH_ATTR syslog_gethostbyname_cb(const char *name, ip_addr_ /****************************************************************************** * FunctionName : initSyslog * Description : Initialize the syslog library - * Parameters : hostname -- the syslog server (host:port) + * Parameters : syslog_host -- the syslog host (host:port) * host: IP-Addr | hostname * Returns : none *******************************************************************************/ -void ICACHE_FLASH_ATTR syslog_init(char *syslog_server) +void ICACHE_FLASH_ATTR syslog_init(char *syslog_host) { + if (!flashConfig.syslog_enable) { + syslog_host[0] = '\0'; + } char host[32], *port = &host[0]; syslog_task = register_usr_task(syslog_udp_send_event); @@ -247,7 +250,7 @@ void ICACHE_FLASH_ATTR syslog_init(char *syslog_server) syslogHost.port = 514; syslogState = SYSLOG_WAIT; - os_strncpy(host, syslog_server, 32); + os_strncpy(host, syslog_host, 32); while (*port && *port != ':') // find port delimiter port++; if (*port) { diff --git a/syslog/syslog.h b/syslog/syslog.h index f13d383..faf0eec 100644 --- a/syslog/syslog.h +++ b/syslog/syslog.h @@ -84,7 +84,7 @@ struct syslog_entry_t { syslog_host_t syslogserver; -void ICACHE_FLASH_ATTR syslog_init(char *syslog_server); +void ICACHE_FLASH_ATTR syslog_init(char *syslog_host); void ICACHE_FLASH_ATTR syslog(uint8_t facility, uint8_t severity, const char tag[], const char message[], ...); // some convenience macros From ebda9517691f4daf6a8fba350a256ce0dc2ecb1f Mon Sep 17 00:00:00 2001 From: Benjamin Runnels Date: Mon, 7 Dec 2015 12:54:48 -0600 Subject: [PATCH 12/19] Removed unused mdns files --- esp-link.vcxproj | 2 - esp-link/mdns.c | 332 ----------------------------------------------- esp-link/mdns.h | 52 -------- 3 files changed, 386 deletions(-) delete mode 100644 esp-link/mdns.c delete mode 100644 esp-link/mdns.h diff --git a/esp-link.vcxproj b/esp-link.vcxproj index 369d0cb..4a1ef3a 100644 --- a/esp-link.vcxproj +++ b/esp-link.vcxproj @@ -24,7 +24,6 @@ - @@ -62,7 +61,6 @@ - diff --git a/esp-link/mdns.c b/esp-link/mdns.c deleted file mode 100644 index 89ae337..0000000 --- a/esp-link/mdns.c +++ /dev/null @@ -1,332 +0,0 @@ -/* -* mdns.h -* -* Simple mDNS responder. -* It replys to mDNS IP (IPv4/A) queries and optionally broadcasts ip advertisements -* using the mDNS protocol -* Created on: Apr 10, 2015 -* Author: Kevin Uhlir (n0bel) -* -*/ - -#include -#include "mdns.h" - -#ifdef MDNS_DBG -#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) -#else -#define DBG(format, ...) do { } while(0) -#endif - -static int mdns_initialized = 0; -static int ttl = 0; -static os_timer_t bc_timer; - -static struct espconn mdns_conn; - -#define MAX_HOSTS 5 -static struct _host { - char *hostname; - unsigned char *mdns; - struct ip_addr ip; - int len; -} hosts[MAX_HOSTS] = { { NULL } }; - -int nhosts = 0; - -struct ip_addr mdns_multicast; -struct ip_addr ipaddr_any; - -#ifdef MDNS_DBG - -void ICACHE_FLASH_ATTR hexdump(char *desc, void *addr, int len) { - int i; - unsigned char buff[17]; - unsigned char *pc = (unsigned char*)addr; - - // Output description if given. - if (desc != NULL) - DBG("%s:\n", desc); - - // Process every byte in the data. - for (i = 0; i < len; i++) { - // Multiple of 16 means new line (with line offset). - - if ((i % 16) == 0) { - // Just don't print ASCII for the zeroth line. - if (i != 0) - DBG(" %s\n", buff); - - // Output the offset. - DBG(" %04x ", i); - } - - // Now the hex code for the specific character. - DBG(" %02x", pc[i]); - - // And store a printable ASCII character for later. - if ((pc[i] < 0x20) || (pc[i] > 0x7e)) - buff[i % 16] = '.'; - else - buff[i % 16] = pc[i]; - buff[(i % 16) + 1] = '\0'; - } - - // Pad out last line if not exactly 16 characters. - while ((i % 16) != 0) { - DBG(" "); - i++; - } - - // And print the final ASCII bit. - DBG(" %s\n", buff); -} -#endif - -unsigned ICACHE_FLASH_ATTR char * encodeResp(struct ip_addr * addr, char *name, int *len) -{ - *len = 12 + strlen(name) + 16; - unsigned char *data = (unsigned char *)os_zalloc(*len); - - data[2] = 0x84; - data[7] = 1; - - unsigned char *p = data + 12; - char *np = name; - char *i; - while ((i = strchr(np, '.'))) - { - *p = i - np; - p++; - memcpy(p, np, i - np); - p += i - np; - np = i + 1; - } - *p = strlen(np); - p++; - memcpy(p, np, strlen(np)); - p += strlen(np); - *p++ = 0; // terminate string sequence - - *p++ = 0; *p++ = 1; // type 0001 (A) - - *p++ = 0x80; *p++ = 1; // class code (IPV4) - - *p++ = ttl >> 24; - *p++ = ttl >> 16; - *p++ = ttl >> 8; - *p++ = ttl; - - *p++ = 0; *p++ = 4; // length (of ip) - - memcpy(p, addr, 4); - return data; -} - -char ICACHE_FLASH_ATTR *decode_name_strings(unsigned char *p, int *len) -{ - unsigned char *s = p; - int c; - *len = 0; - while ((c = *s++)) - { - *len += c + 1; - s += c; - } - char *name = (char *)os_zalloc(*len + 1); - char *np = name; - s = p; - while ((c = *s++)) - { - memcpy(np, s, c); - np += c; - *np++ = '.'; - s += c; - } - np--; *np = 0; - return name; -} - -void ICACHE_FLASH_ATTR sendOne(struct _host *h) -{ - mdns_conn.proto.udp->remote_ip[0] = 224; - mdns_conn.proto.udp->remote_ip[1] = 0; - mdns_conn.proto.udp->remote_ip[2] = 0; - mdns_conn.proto.udp->remote_ip[3] = 251; - mdns_conn.proto.udp->remote_port = 5353; - mdns_conn.proto.udp->local_port = 5353; -#ifdef MDNS_DBG -// hexdump("sending", h->mdns, h->len); -#endif - espconn_sent(&mdns_conn, h->mdns, h->len); - -} -void ICACHE_FLASH_ATTR decodeQuery(unsigned char *data) -{ - if (data[0] != 0 || data[1] != 0 || data[2] != 0 || data[3] != 0) return; // only queries - - int qcount = data[5]; // purposely ignore qcount > 255 - unsigned char *p = data + 12; - char *name; - int len; - while (qcount-- > 0) - { - if (*p == 0xc0) // pointer - { - name = decode_name_strings(data + p[1], &len); - p += 2; - } - else - { - name = decode_name_strings(p, &len); - p += len + 1; - } - int qtype = p[0] * 256 + p[1]; - int qclass = p[2] * 256 + p[3]; - p += 4; -// DBG("decoded name %s qtype=%d qclass=%d\n", name, qtype, qclass); - - if (qtype == 1 && (qclass & 0x7fff) == 1) - { - struct _host *h; - int i; - for (h = hosts, i = 0; i < nhosts; i++, h++) - { - if (h->hostname && strcmp(name, h->hostname) == 0) - { -// DBG("its %s!\n", h->hostname); - sendOne(h); - } - } - os_free(name); - } - } - - return; -} - -static void ICACHE_FLASH_ATTR broadcast(void *arg) -{ - struct _host *h; - int i; - for (h = hosts, i = 0; i < nhosts; i++, h++) - { -// DBG("broadcast for %s\n", h->hostname); - sendOne(h); - } -} - -static void ICACHE_FLASH_ATTR mdns_udp_recv(void *arg, char *pusrdata, unsigned short length) { - unsigned char *data = (unsigned char *)pusrdata; - - -// DBG("remote port=%d remoteip=%d.%d.%d.%d\n", mdns_conn.proto.udp->remote_port, -// mdns_conn.proto.udp->remote_ip[0], -// mdns_conn.proto.udp->remote_ip[1], -// mdns_conn.proto.udp->remote_ip[2], -// mdns_conn.proto.udp->remote_ip[3] -// ); -#ifdef MDNS_DBG -// hexdump("rec mNDS:", data, length); -#endif - decodeQuery(data); -} - -int ICACHE_FLASH_ATTR mdns_addhost(char *hn, struct ip_addr* ip) -{ - struct _host *h = &hosts[nhosts]; - if (nhosts >= MAX_HOSTS) - { - int i; - for (h = hosts, i = 0; i < MAX_HOSTS; i++, h++) - { - if (!h->hostname) break; - } - if (i >= MAX_HOSTS) return -1; - } - else - { - nhosts++; - } - h->hostname = (char *)os_zalloc(os_strlen(hn) + 1); - os_strcpy(h->hostname, hn); - h->ip.addr = ip->addr; - h->mdns = encodeResp(ip, hn, &(h->len)); -// DBG("resp len: %d\n", ets_strlen(h->mdns)); -#ifdef MDNS_DBG - hexdump("addhost", hosts, sizeof(hosts)); -#endif - return(0); -} - -int ICACHE_FLASH_ATTR mdns_delhost(char *hn, struct ip_addr* ip) -{ - if (nhosts < 1) return -1; - struct _host *h; - int i; - for (h = hosts, i = 0; i < MAX_HOSTS; i++, h++) - { - if ((hn && strcmp(hn, h->hostname) == 0) - || (ip && h->ip.addr == ip->addr)) - { - if (h->hostname) os_free(h->hostname); - if (h->mdns) os_free(h->mdns); - h->ip.addr = 0; - } - } -#ifdef MDNS_DBG -// hexdump("delhost", hosts, sizeof(hosts)); -#endif - return(0); -} - -int ICACHE_FLASH_ATTR mdns_init(int bctime, int sttl) -{ - if (mdns_initialized) mdns_stop(); - mdns_initialized = 1; - ttl = sttl; - - IP4_ADDR(&mdns_multicast, 224, 0, 0, 251); - ipaddr_any.addr = IPADDR_ANY; - - espconn_igmp_join(&ipaddr_any, &mdns_multicast); - - static esp_udp mdns_udp; - mdns_conn.type = ESPCONN_UDP; - mdns_conn.state = ESPCONN_NONE; - mdns_conn.proto.udp = &mdns_udp; - mdns_udp.local_port = 5353; - mdns_conn.reverse = NULL; - - espconn_regist_recvcb(&mdns_conn, mdns_udp_recv); - espconn_create(&mdns_conn); - - os_timer_disarm(&bc_timer); - if (bctime > 0) - { - os_timer_setfn(&bc_timer, (os_timer_func_t *)broadcast, (void *)0); - os_timer_arm(&bc_timer, bctime * 1000, 1); - } - - return 0; -} - -int ICACHE_FLASH_ATTR mdns_stop() -{ - if (!mdns_initialized) return(0); - os_timer_disarm(&bc_timer); - espconn_disconnect(&mdns_conn); - espconn_delete(&mdns_conn); - espconn_igmp_leave(&ipaddr_any, &mdns_multicast); - mdns_initialized = 0; - struct _host *h; - int i; - for (h = hosts, i = 0; i < MAX_HOSTS; i++, h++) - { - if (h->hostname) os_free(h->hostname); - if (h->mdns) os_free(h->mdns); - h->ip.addr = 0; - } - nhosts = 0; - return(0); -} \ No newline at end of file diff --git a/esp-link/mdns.h b/esp-link/mdns.h deleted file mode 100644 index 49273a1..0000000 --- a/esp-link/mdns.h +++ /dev/null @@ -1,52 +0,0 @@ -/* -* mdns.h -* -* Simple mDNS responder. -* It replys to mDNS IP (IPv4/A) queries and optionally broadcasts ip advertisements -* using the mDNS protocol -* Created on: Apr 10, 2015 -* Author: Kevin Uhlir (n0bel) -* -*/ - -#ifndef MDNS_H -#define MDNS_H - -/****************************************************************************** -* FunctionName : mdns_init -* Description : Initialize the module -* done once after the system is up and ready to run -* Parameters : bctime -- broadcast timer (seconds) can be 0 for no broadcasting -* ttl -- Time To Live must be > 0, should be a reasonable DNS ttl -* Returns : 0 success, -1 failed -*******************************************************************************/ -int mdns_init(int bctime, int ttl); -/****************************************************************************** -* FunctionName : mdns_stop -* Description : Stop the module. -* Parameters : None -* Returns : 0 success, -1 failed -*******************************************************************************/ -int mdns_stop(void); -/****************************************************************************** -* FunctionName : mdns_addhost -* Description : Add a host to be resolved by mDNS -* Parameters : hostname -- the hostname to add (must end in .local) -* for most clients to function -* ip -- the ip address of the host -* Returns : 0 success, -1 failed -*******************************************************************************/ -int mdns_addhost(char *hostname, struct ip_addr* ip); -/****************************************************************************** -* FunctionName : mdns_delhost -* Description : Delete a host to be resolved by mDNS -* Parameters : hostname -- the hostname to delete -* if NULL, hostname won't be used -* ip -- the ip address of the host to delete -* if NULL, ip won't be used -* Returns : 0 success, -1 failed -*******************************************************************************/ -int mdns_delhost(char *hostname, struct ip_addr* ip); - - -#endif /* MDNS_H */ \ No newline at end of file From 3eccd26e76affd6978ce5c7c60dc92b799252844 Mon Sep 17 00:00:00 2001 From: Benjamin Runnels Date: Tue, 8 Dec 2015 15:55:17 -0600 Subject: [PATCH 13/19] Normalized Wifi to WiFI. Fixed button spacing on console clear and log reset buttons Renamed cgiReboot method to cgiReset and called the proper reset method Normalized whitespace in cgiflash.c --- esp-link/cgi.c | 2 +- esp-link/cgiflash.c | 223 ++++++++++++++++++++++---------------------- esp-link/cgiflash.h | 2 +- esp-link/main.c | 2 +- html/console.html | 6 +- html/home.html | 10 +- html/log.html | 19 ++-- html/mqtt.html | 2 +- html/services.html | 2 +- html/wifi/wifi.html | 18 ++-- 10 files changed, 139 insertions(+), 147 deletions(-) diff --git a/esp-link/cgi.c b/esp-link/cgi.c index da54997..081ef49 100644 --- a/esp-link/cgi.c +++ b/esp-link/cgi.c @@ -206,7 +206,7 @@ int ICACHE_FLASH_ATTR cgiMenu(HttpdConnData *connData) { "{ " "\"menu\": [ " "\"Home\", \"/home.html\", " - "\"Wifi\", \"/wifi/wifi.html\", " + "\"WiFI\", \"/wifi/wifi.html\", " "\"µC Console\", \"/console.html\", " "\"Services\", \"/services.html\", " #ifdef MQTT diff --git a/esp-link/cgiflash.c b/esp-link/cgiflash.c index d272465..8c39e36 100644 --- a/esp-link/cgiflash.c +++ b/esp-link/cgiflash.c @@ -27,16 +27,16 @@ Some flash handling cgi routines. Used for reading the existing flash and updati // Check that the header of the firmware blob looks like actual firmware... static char* ICACHE_FLASH_ATTR check_header(void *buf) { - uint8_t *cd = (uint8_t *)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]); + uint32_t *buf32 = buf; + os_printf("%p: %08lX %08lX %08lX %08lX\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"; - if (((uint16_t *)buf)[3] != 0x4010) return "Invalid entry addr"; - if (((uint32_t *)buf)[2] != 0) return "Invalid start offset"; - return NULL; + if (cd[0] != 0xEA) return "IROM magic missing"; + if (cd[1] != 4 || cd[2] > 3 || (cd[3]>>4) > 6) return "bad flash header"; + if (((uint16_t *)buf)[3] != 0x4010) return "Invalid entry addr"; + if (((uint32_t *)buf)[2] != 0) return "Invalid start offset"; + return NULL; } // check whether the flash map/size we have allows for OTA upgrade @@ -49,143 +49,143 @@ static char *flash_too_small = "Flash too small for OTA update"; //===== Cgi to query which firmware needs to be uploaded next int ICACHE_FLASH_ATTR cgiGetFirmwareNext(HttpdConnData *connData) { - if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. + if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. if (!canOTA()) { errorResponse(connData, 400, flash_too_small); return HTTPD_CGI_DONE; } - uint8 id = system_upgrade_userbin_check(); - httpdStartResponse(connData, 200); - httpdHeader(connData, "Content-Type", "text/plain"); - httpdHeader(connData, "Content-Length", "9"); - httpdEndHeaders(connData); - char *next = id == 1 ? "user1.bin" : "user2.bin"; - httpdSend(connData, next, -1); + uint8 id = system_upgrade_userbin_check(); + httpdStartResponse(connData, 200); + httpdHeader(connData, "Content-Type", "text/plain"); + httpdHeader(connData, "Content-Length", "9"); + httpdEndHeaders(connData); + char *next = id == 1 ? "user1.bin" : "user2.bin"; + httpdSend(connData, next, -1); DBG("Next firmware: %s (got %d)\n", next, id); - return HTTPD_CGI_DONE; + return HTTPD_CGI_DONE; } //===== Cgi that allows the firmware to be replaced via http POST int ICACHE_FLASH_ATTR cgiUploadFirmware(HttpdConnData *connData) { - if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. + if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. if (!canOTA()) { errorResponse(connData, 400, flash_too_small); return HTTPD_CGI_DONE; } - int offset = connData->post->received - connData->post->buffLen; - if (offset == 0) { - connData->cgiPrivData = NULL; - } else if (connData->cgiPrivData != NULL) { - // we have an error condition, do nothing - return HTTPD_CGI_DONE; - } - - // assume no error yet... - char *err = NULL; - int code = 400; - - // check overall size - //os_printf("FW: %d (max %d)\n", connData->post->len, FIRMWARE_SIZE); - if (connData->post->len > FIRMWARE_SIZE) err = "Firmware image too large"; - if (connData->post->buff == NULL || connData->requestType != HTTPD_METHOD_POST || - connData->post->len < 1024) err = "Invalid request"; - - // check that data starts with an appropriate header - if (err == NULL && offset == 0) err = check_header(connData->post->buff); - - // make sure we're buffering in 1024 byte chunks - if (err == NULL && offset % 1024 != 0) { - err = "Buffering problem"; - code = 500; - } - - // return an error if there is one - if (err != NULL) { + int offset = connData->post->received - connData->post->buffLen; + if (offset == 0) { + connData->cgiPrivData = NULL; + } else if (connData->cgiPrivData != NULL) { + // we have an error condition, do nothing + return HTTPD_CGI_DONE; + } + + // assume no error yet... + char *err = NULL; + int code = 400; + + // check overall size + //os_printf("FW: %d (max %d)\n", connData->post->len, FIRMWARE_SIZE); + if (connData->post->len > FIRMWARE_SIZE) err = "Firmware image too large"; + if (connData->post->buff == NULL || connData->requestType != HTTPD_METHOD_POST || + connData->post->len < 1024) err = "Invalid request"; + + // check that data starts with an appropriate header + if (err == NULL && offset == 0) err = check_header(connData->post->buff); + + // make sure we're buffering in 1024 byte chunks + if (err == NULL && offset % 1024 != 0) { + err = "Buffering problem"; + code = 500; + } + + // return an error if there is one + if (err != NULL) { DBG("Error %d: %s\n", code, err); - httpdStartResponse(connData, code); - httpdHeader(connData, "Content-Type", "text/plain"); - //httpdHeader(connData, "Content-Length", strlen(err)+2); - httpdEndHeaders(connData); - httpdSend(connData, err, -1); - httpdSend(connData, "\r\n", -1); - connData->cgiPrivData = (void *)1; - return HTTPD_CGI_DONE; - } - - // let's see which partition we need to flash and what flash address that puts us at - uint8 id = system_upgrade_userbin_check(); - int address = id == 1 ? 4*1024 // either start after 4KB boot partition - : 4*1024 + FIRMWARE_SIZE + 16*1024 + 4*1024; // 4KB boot, fw1, 16KB user param, 4KB reserved - address += offset; - - // erase next flash block if necessary - if (address % SPI_FLASH_SEC_SIZE == 0){ + httpdStartResponse(connData, code); + httpdHeader(connData, "Content-Type", "text/plain"); + //httpdHeader(connData, "Content-Length", strlen(err)+2); + httpdEndHeaders(connData); + httpdSend(connData, err, -1); + httpdSend(connData, "\r\n", -1); + connData->cgiPrivData = (void *)1; + return HTTPD_CGI_DONE; + } + + // let's see which partition we need to flash and what flash address that puts us at + uint8 id = system_upgrade_userbin_check(); + int address = id == 1 ? 4*1024 // either start after 4KB boot partition + : 4*1024 + FIRMWARE_SIZE + 16*1024 + 4*1024; // 4KB boot, fw1, 16KB user param, 4KB reserved + address += offset; + + // erase next flash block if necessary + if (address % SPI_FLASH_SEC_SIZE == 0){ DBG("Flashing 0x%05x (id=%d)\n", address, 2 - id); - spi_flash_erase_sector(address/SPI_FLASH_SEC_SIZE); - } - - // Write the data - //DBG("Writing %d bytes at 0x%05x (%d of %d)\n", connData->post->buffSize, address, - // connData->post->received, connData->post->len); - spi_flash_write(address, (uint32 *)connData->post->buff, connData->post->buffLen); - - if (connData->post->received == connData->post->len){ - httpdStartResponse(connData, 200); - httpdEndHeaders(connData); - return HTTPD_CGI_DONE; - } else { - return HTTPD_CGI_MORE; - } + spi_flash_erase_sector(address/SPI_FLASH_SEC_SIZE); + } + + // Write the data + //DBG("Writing %d bytes at 0x%05x (%d of %d)\n", connData->post->buffSize, address, + // connData->post->received, connData->post->len); + spi_flash_write(address, (uint32 *)connData->post->buff, connData->post->buffLen); + + if (connData->post->received == connData->post->len){ + httpdStartResponse(connData, 200); + httpdEndHeaders(connData); + return HTTPD_CGI_DONE; + } else { + return HTTPD_CGI_MORE; + } } static ETSTimer flash_reboot_timer; // Handle request to reboot into the new firmware int ICACHE_FLASH_ATTR cgiRebootFirmware(HttpdConnData *connData) { - if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. + if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. if (!canOTA()) { errorResponse(connData, 400, flash_too_small); return HTTPD_CGI_DONE; } - // sanity-check that the 'next' partition actually contains something that looks like - // valid firmware - uint8 id = system_upgrade_userbin_check(); - int address = id == 1 ? 4*1024 // either start after 4KB boot partition - : 4*1024 + FIRMWARE_SIZE + 16*1024 + 4*1024; // 4KB boot, fw1, 16KB user param, 4KB reserved - uint32 buf[8]; + // sanity-check that the 'next' partition actually contains something that looks like + // valid firmware + uint8 id = system_upgrade_userbin_check(); + int address = id == 1 ? 4*1024 // either start after 4KB boot partition + : 4*1024 + FIRMWARE_SIZE + 16*1024 + 4*1024; // 4KB boot, fw1, 16KB user param, 4KB reserved + uint32 buf[8]; DBG("Checking %p\n", (void *)address); - spi_flash_read(address, buf, sizeof(buf)); - char *err = check_header(buf); - if (err != NULL) { + spi_flash_read(address, buf, sizeof(buf)); + char *err = check_header(buf); + if (err != NULL) { DBG("Error %d: %s\n", 400, err); - httpdStartResponse(connData, 400); - httpdHeader(connData, "Content-Type", "text/plain"); - //httpdHeader(connData, "Content-Length", strlen(err)+2); - httpdEndHeaders(connData); - httpdSend(connData, err, -1); - httpdSend(connData, "\r\n", -1); - return HTTPD_CGI_DONE; - } - - httpdStartResponse(connData, 200); - httpdHeader(connData, "Content-Length", "0"); - httpdEndHeaders(connData); - - // Schedule a reboot - system_upgrade_flag_set(UPGRADE_FLAG_FINISH); - os_timer_disarm(&flash_reboot_timer); - os_timer_setfn(&flash_reboot_timer, (os_timer_func_t *)system_upgrade_reboot, NULL); - os_timer_arm(&flash_reboot_timer, 2000, 1); - return HTTPD_CGI_DONE; + httpdStartResponse(connData, 400); + httpdHeader(connData, "Content-Type", "text/plain"); + //httpdHeader(connData, "Content-Length", strlen(err)+2); + httpdEndHeaders(connData); + httpdSend(connData, err, -1); + httpdSend(connData, "\r\n", -1); + return HTTPD_CGI_DONE; + } + + httpdStartResponse(connData, 200); + httpdHeader(connData, "Content-Length", "0"); + httpdEndHeaders(connData); + + // Schedule a reboot + system_upgrade_flag_set(UPGRADE_FLAG_FINISH); + os_timer_disarm(&flash_reboot_timer); + os_timer_setfn(&flash_reboot_timer, (os_timer_func_t *)system_upgrade_reboot, NULL); + os_timer_arm(&flash_reboot_timer, 2000, 1); + return HTTPD_CGI_DONE; } -int ICACHE_FLASH_ATTR cgiReboot(HttpdConnData *connData) { +int ICACHE_FLASH_ATTR cgiReset(HttpdConnData *connData) { if (connData->conn == NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. httpdStartResponse(connData, 200); @@ -193,9 +193,8 @@ int ICACHE_FLASH_ATTR cgiReboot(HttpdConnData *connData) { httpdEndHeaders(connData); // Schedule a reboot - system_upgrade_flag_set(UPGRADE_FLAG_FINISH); os_timer_disarm(&flash_reboot_timer); - os_timer_setfn(&flash_reboot_timer, (os_timer_func_t *)system_upgrade_reboot, NULL); + 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/cgiflash.h b/esp-link/cgiflash.h index d8a239d..f03b8e0 100644 --- a/esp-link/cgiflash.h +++ b/esp-link/cgiflash.h @@ -7,6 +7,6 @@ int cgiReadFlash(HttpdConnData *connData); int cgiGetFirmwareNext(HttpdConnData *connData); int cgiUploadFirmware(HttpdConnData *connData); int cgiRebootFirmware(HttpdConnData *connData); -int cgiReboot(HttpdConnData *connData); +int cgiReset(HttpdConnData *connData); #endif diff --git a/esp-link/main.c b/esp-link/main.c index 8d10eef..09904e1 100644 --- a/esp-link/main.c +++ b/esp-link/main.c @@ -57,7 +57,7 @@ HttpdBuiltInUrl builtInUrls[] = { { "/pgm/upload", cgiOptibootData, NULL }, { "/log/text", ajaxLog, NULL }, { "/log/dbg", ajaxLogDbg, NULL }, - { "/log/reboot", cgiReboot, NULL }, + { "/log/reset", cgiReset, NULL }, { "/console/reset", ajaxConsoleReset, NULL }, { "/console/baud", ajaxConsoleBaud, NULL }, { "/console/text", ajaxConsole, NULL }, diff --git a/html/console.html b/html/console.html index 6f81aca..d84da72 100644 --- a/html/console.html +++ b/html/console.html @@ -6,8 +6,8 @@

Reset µC - Clear Log -   Baud: +   Clear Log +   Baud: -   Fmt: 8N1 +   Fmt: 8N1

Console
diff --git a/html/home.html b/html/home.html index 33cbe27..2413566 100644 --- a/html/home.html +++ b/html/home.html @@ -22,8 +22,8 @@
Network SSID - Wifi status - Wifi address + WiFI status + WiFI address SLIP status MQTT status Serial baud @@ -32,7 +32,7 @@

Info

The JeeLabs esp-link firmware bridges the ESP8266 - serial port to Wifi and can + serial port to WiFI and can program microcontrollers over the serial port, in particular Arduinos, AVRs, and NXP's LPC800 and other ARM processors. Typical avrdude command line to program an Arduino:

@@ -109,8 +109,8 @@

System details

- - + +