From a8285da11f87e3f6c448ba8bf43759bd742f320b Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Mon, 26 Sep 2016 22:30:54 -0700 Subject: [PATCH 1/3] fix dockerfile --- Dockerfile | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 572c102..eedeba0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,13 +9,12 @@ FROM ubuntu:16.04 RUN apt-get update \ - && apt-get install -y software-properties-common build-essential python curl git + && apt-get install -y software-properties-common build-essential python curl git \ + zlib1g-dev openjdk-8-jre-headless RUN curl -Ls http://s3.voneicken.com/xtensa-lx106-elf-20160330.tgx | tar Jxf - RUN curl -Ls http://s3.voneicken.com/esp_iot_sdk_v2.0.0.p1.tgx | tar -Jxf - -RUN apt-get install zlib1g-dev openjdk-8-jre-headless - ENV XTENSA_TOOLS_ROOT /xtensa-lx106-elf/bin/ # This could be used to create an image with esp-link in it from github: From 3a16d3ee618a0b28526c5b494c686fcf1bb60803 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Mon, 26 Sep 2016 22:33:23 -0700 Subject: [PATCH 2/3] fix MQTT start-up --- esp-link/cgiflash.c | 8 +-- esp-link/mqtt_client.c | 9 +-- httpd/httpd.c | 7 ++- include/user_config.h | 3 +- mqtt/mqtt_cmd.c | 139 ----------------------------------------- 5 files changed, 15 insertions(+), 151 deletions(-) diff --git a/esp-link/cgiflash.c b/esp-link/cgiflash.c index 4b9277d..6b8de5c 100644 --- a/esp-link/cgiflash.c +++ b/esp-link/cgiflash.c @@ -71,10 +71,10 @@ int ICACHE_FLASH_ATTR cgiGetFirmwareNext(HttpdConnData *connData) { int ICACHE_FLASH_ATTR cgiUploadFirmware(HttpdConnData *connData) { if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. - if (!canOTA()) { - errorResponse(connData, 400, flash_too_small); - return HTTPD_CGI_DONE; - } + if (!canOTA()) { + errorResponse(connData, 400, flash_too_small); + return HTTPD_CGI_DONE; + } int offset = connData->post->received - connData->post->buffLen; if (offset == 0) { diff --git a/esp-link/mqtt_client.c b/esp-link/mqtt_client.c index f0d9108..a9c1902 100644 --- a/esp-link/mqtt_client.c +++ b/esp-link/mqtt_client.c @@ -5,7 +5,7 @@ #include "mqtt.h" #ifdef MQTTCLIENT_DBG -#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__) } while(0) +#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) #else #define DBG(format, ...) do { } while(0) #endif @@ -66,7 +66,7 @@ void ICACHE_FLASH_ATTR wifiStateChangeCb(uint8_t status) { if (flashConfig.mqtt_enable) { - if (status == wifiGotIP && mqttClient.connState != TCP_CONNECTING) { + if (status == wifiGotIP && mqttClient.connState < TCP_CONNECTING) { MQTT_Connect(&mqttClient); } else if (status == wifiIsDisconnected && mqttClient.connState == TCP_CONNECTING) { @@ -87,8 +87,9 @@ mqtt_client_init() MQTT_OnPublished(&mqttClient, mqttPublishedCb); MQTT_OnData(&mqttClient, mqttDataCb); - if (flashConfig.mqtt_enable && strlen(flashConfig.mqtt_host) > 0) - MQTT_Connect(&mqttClient); + // Don't connect now, wait for a wifi status change callback + //if (flashConfig.mqtt_enable && strlen(flashConfig.mqtt_host) > 0) + // MQTT_Connect(&mqttClient); wifiAddStateChangeCb(wifiStateChangeCb); } diff --git a/httpd/httpd.c b/httpd/httpd.c index bc16649..62ae35c 100644 --- a/httpd/httpd.c +++ b/httpd/httpd.c @@ -17,6 +17,7 @@ Esp8266 http server - core routines #include #include "httpd.h" +//#define HTTPD_DBG #ifdef HTTPD_DBG #define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) #else @@ -635,7 +636,7 @@ HttpdConnData * ICACHE_FLASH_ATTR httpdLookUpConn(uint8_t * ip, int port) { for (i = 0; iconn == NULL) continue; if (conn->cgi == NULL) @@ -644,7 +645,7 @@ HttpdConnData * ICACHE_FLASH_ATTR httpdLookUpConn(uint8_t * ip, int port) { continue; if (os_memcmp(conn->conn->proto.tcp->remote_ip, ip, 4) != 0) continue; - + return conn; } return NULL; @@ -664,6 +665,6 @@ int ICACHE_FLASH_ATTR httpdSetCGIResponse(HttpdConnData * conn, void * response) conn->cgiResponse = response; httpdProcessRequest(conn); conn->cgiResponse = NULL; - + return HTTPD_CGI_DONE; } diff --git a/include/user_config.h b/include/user_config.h index 131e1b9..537b521 100644 --- a/include/user_config.h +++ b/include/user_config.h @@ -22,6 +22,7 @@ #define HTTPD_DBG #define MQTT_DBG #define MQTTCMD_DBG +#define MQTTCLIENT_DBG #undef PKTBUF_DBG #define REST_DBG #define RESTCMD_DBG @@ -32,7 +33,7 @@ #define MDNS_DBG #define OPTIBOOT_DBG #undef SYSLOG_DBG -#undef CGISERVICES_DBG +#define CGISERVICES_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 37fb4a4..0f40b79 100644 --- a/mqtt/mqtt_cmd.c +++ b/mqtt/mqtt_cmd.c @@ -184,63 +184,6 @@ MQTTCMD_Setup(CmdPacket *cmd) { if (cmdGetArgc(&req) != 4) return; -#if 0 -// This section is commented-out because we're using the same client as esp-link is using itself, -// i.e. the one set-up in the Web UI. This code was here when we used a separate client for the -// attached uC, which just makes life more complicated... - - if (cmdGetArgc(&req) != 9) - return 0; - - // create mqtt client - uint8_t clientLen = sizeof(MQTT_Client); - MQTT_Client* client = (MQTT_Client*)os_zalloc(clientLen); - if (client == NULL) return 0; - os_memset(client, 0, clientLen); - - uint16_t len; - uint8_t *client_id, *user_data, *pass_data; - uint32_t keepalive, clean_session; - - // get client id - len = cmdArgLen(&req); - if (len > 32) return 0; // safety check - client_id = (uint8_t*)os_zalloc(len + 1); - cmdPopArg(&req, client_id, len); - client_id[len] = 0; - - // get username - len = cmdArgLen(&req); - if (len > 32) return 0; // safety check - user_data = (uint8_t*)os_zalloc(len + 1); - cmdPopArg(&req, user_data, len); - user_data[len] = 0; - - // get password - len = cmdArgLen(&req); - if (len > 32) return 0; // safety check - pass_data = (uint8_t*)os_zalloc(len + 1); - cmdPopArg(&req, pass_data, len); - pass_data[len] = 0; - - // get keepalive - cmdPopArg(&req, (uint8_t*)&keepalive, 4); - - // get clean session - cmdPopArg(&req, (uint8_t*)&clean_session, 4); -#ifdef MQTTCMD_DBG - 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 - // TODO: why malloc these all here, pass to MQTT_InitClient to be malloc'd again? - MQTT_InitClient(client, (char*)client_id, (char*)user_data, (char*)pass_data, keepalive, clean_session); - - os_free(client_id); - os_free(user_data); - os_free(pass_data); -#endif - // create callback MqttCmdCb* callback = (MqttCmdCb*)os_zalloc(sizeof(MqttCmdCb)); cmdPopArg(&req, &callback->connectedCb, 4); @@ -263,85 +206,3 @@ MQTTCMD_Setup(CmdPacket *cmd) { cmdMqttDisconnectedCb(client); } } - -#if 0 -// This section is commented-out because we're using the same client as esp-link is using itself, -// i.e. the one set-up in the Web UI. This code was here when we used a separate client for the -// attached uC, which just makes life more complicated... - -uint32_t ICACHE_FLASH_ATTR -MQTTCMD_Connect(CmdPacket *cmd) { - CmdRequest req; - cmdRequest(&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 - if (cmdGetArgc(&req) != 4) - return 0; - - // get mqtt client - uint32_t client_ptr; - cmdPopArg(&req, (uint8_t*)&client_ptr, 4); - MQTT_Client* client = (MQTT_Client*)client_ptr; - DBG("MQTT: MQTTCMD_Connect client ptr=%p\n", (void*)client_ptr); - - uint16_t len; - - // get host - if (client->host) - os_free(client->host); - len = cmdArgLen(&req); - if (len > 128) return 0; // safety check - client->host = (char*)os_zalloc(len + 1); - cmdPopArg(&req, client->host, len); - client->host[len] = 0; - - // get port - cmdPopArg(&req, (uint8_t*)&client->port, 4); - - // get security - cmdPopArg(&req, (uint8_t*)&client->security, 4); - DBG("MQTT: MQTTCMD_Connect host=%s, port=%d, security=%d\n", - client->host, - client->port, - client->security); - - MQTT_Connect(client); - return 1; -#endif -} - -uint32_t ICACHE_FLASH_ATTR -MQTTCMD_Disconnect(CmdPacket *cmd) { - CmdRequest req; - cmdRequest(&req, cmd); - -#ifdef MQTT_1_CLIENT - return 1; - -#else - if (cmdGetArgc(&req) != 1) - return 0; - - // get mqtt client - uint32_t client_ptr; - cmdPopArg(&req, (uint8_t*)&client_ptr, 4); - MQTT_Client* client = (MQTT_Client*)client_ptr; - DBG("MQTT: MQTTCMD_Disconnect client ptr=%p\n", (void*)client_ptr); - - // disconnect - MQTT_Disconnect(client); - return 1; -#endif -} -#endif From c67750ad181372321dbc38d8069297050048cd1f Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Tue, 27 Sep 2016 23:33:57 -0700 Subject: [PATCH 3/3] add support to reset slip communication --- cmd/cmd.c | 8 +++++--- cmd/cmd.h | 8 +++++++- cmd/handlers.c | 10 +++++++++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/cmd/cmd.c b/cmd/cmd.c index 705ee8a..4a8dc64 100644 --- a/cmd/cmd.c +++ b/cmd/cmd.c @@ -13,8 +13,6 @@ #define DBG(format, ...) do { } while(0) #endif -extern const CmdList commands[]; - //===== ESP -> Serial responses static void ICACHE_FLASH_ATTR @@ -128,7 +126,11 @@ cmdParsePacket(uint8_t *buf, short len) { } #endif - if (data_ptr <= data_limit) { + if (!cmdInSync && packet->cmd != CMD_SYNC) { + // we have not received a sync, perhaps we reset? Tell MCU to do a sync + cmdResponseStart(CMD_SYNC, 0, 0); + cmdResponseEnd(); + } else if (data_ptr <= data_limit) { cmdExec(commands, packet); } else { DBG("cmdParsePacket: packet length overrun, parsing arg %d\n", packet->argc); diff --git a/cmd/cmd.h b/cmd/cmd.h index be1e684..d1a8b19 100644 --- a/cmd/cmd.h +++ b/cmd/cmd.h @@ -6,6 +6,9 @@ #define CMD_H #include +// keep track of whether we received a sync command from uC +extern bool cmdInSync; + // Standard SLIP escape chars from RFC #define SLIP_END 0300 // indicates end of packet #define SLIP_ESC 0333 // indicates byte stuffing @@ -48,7 +51,7 @@ typedef enum { CMD_REST_SETUP = 20, CMD_REST_REQUEST, CMD_REST_SETHEADER, - + CMD_WEB_DATA = 30, // MCU pushes data using this command CMD_WEB_REQ_CB, // esp-link WEB callback } CmdName; @@ -61,6 +64,9 @@ typedef struct { cmdfunc_t sc_function; // pointer to function } CmdList; +// command dispatch table +extern const CmdList commands[]; + #define CMD_CBNLEN 16 typedef struct { char name[CMD_CBNLEN]; diff --git a/cmd/handlers.c b/cmd/handlers.c index af328e7..1b45e1f 100644 --- a/cmd/handlers.c +++ b/cmd/handlers.c @@ -5,6 +5,7 @@ #include "esp8266.h" #include "sntp.h" #include "cmd.h" +#include "uart.h" #include #ifdef MQTT #include @@ -28,7 +29,10 @@ static void cmdAddCallback(CmdPacket *cmd); // keep track of last status sent to uC so we can notify it when it changes static uint8_t lastWifiStatus = wifiIsDisconnected; +// keep track of whether we have registered our cb handler with the wifi subsystem static bool wifiCbAdded = false; +// keep track of whether we received a sync command from uC +bool cmdInSync = false; // Command dispatch table for serial -> ESP commands const CmdList commands[] = { @@ -53,7 +57,7 @@ const CmdList commands[] = { //===== List of registered callbacks (to uC) -// WifiCb plus 10 for sensors +// WifiCb plus 10 for other stuff #define MAX_CALLBACKS 12 CmdCallback callbacks[MAX_CALLBACKS]; // cleared in cmdSync @@ -118,6 +122,7 @@ cmdNull(CmdPacket *cmd) { static void ICACHE_FLASH_ATTR cmdSync(CmdPacket *cmd) { CmdRequest req; + uart0_write_char(SLIP_END); // prefix with a SLIP END to ensure we get a clean start cmdRequest(&req, cmd); if(cmd->argc != 0 || cmd->value == 0) { cmdResponseStart(CMD_RESP_V, 0, 0); @@ -128,6 +133,8 @@ cmdSync(CmdPacket *cmd) { // clear callbacks table os_memset(callbacks, 0, sizeof(callbacks)); + // TODO: call other protocols back to tell them to reset + // register our callback with wifi subsystem if (!wifiCbAdded) { wifiAddStateChangeCb(cmdWifiCb); @@ -137,6 +144,7 @@ cmdSync(CmdPacket *cmd) { // send OK response cmdResponseStart(CMD_RESP_V, cmd->value, 0); cmdResponseEnd(); + cmdInSync = true; // save the MCU's callback and trigger an initial callback cmdAddCb("wifiCb", cmd->value);