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);