diff --git a/cmd/cmd.h b/cmd/cmd.h index 23bd584..b489108 100644 --- a/cmd/cmd.h +++ b/cmd/cmd.h @@ -20,12 +20,12 @@ #define SLIP_ESC_ESC 0335 // ESC ESC_ESC means ESC data byte #endif -typedef struct __attribute((__packed__)) { +typedef struct __attribute__((__packed__)) { uint16_t len; // length of data uint8_t data[0]; // really data[len] } CmdArg; -typedef struct __attribute((__packed__)) { +typedef struct __attribute__((__packed__)) { uint16_t cmd; // command to perform, from CmdName enum uint32_t callback; // callback pointer to embed in response uint32_t _return; // return value to embed in response (?) @@ -54,7 +54,9 @@ typedef enum { CMD_REST_SETUP, // (11) CMD_REST_REQUEST, CMD_REST_SETHEADER, - CMD_REST_EVENTS + CMD_REST_EVENTS, + CMD_ADD_SENSOR, // 15 + CMD_SENSOR_EVENTS } CmdName; typedef uint32_t (*cmdfunc_t)(CmdPacket *cmd); @@ -64,18 +66,24 @@ typedef struct { cmdfunc_t sc_function; } CmdList; -void CMD_parse_packet(uint8_t *buf, short len); +typedef struct { + char name[16]; + uint32_t callback; +} cmdCallback; + +void ICACHE_FLASH_ATTR CMD_parse_packet(uint8_t *buf, short len); +cmdCallback* ICACHE_FLASH_ATTR CMD_GetCbByName(char* name); // Responses // Start a response, returns the partial CRC -uint16_t CMD_ResponseStart(uint16_t cmd, uint32_t callback, uint32_t _return, uint16_t argc); +uint16_t ICACHE_FLASH_ATTR CMD_ResponseStart(uint16_t cmd, uint32_t callback, uint32_t _return, uint16_t argc); // Adds data to a response, returns the partial CRC -uint16_t CMD_ResponseBody(uint16_t crc_in, uint8_t* data, short len); +uint16_t ICACHE_FLASH_ATTR CMD_ResponseBody(uint16_t crc_in, uint8_t* data, short len); // Ends a response -void CMD_ResponseEnd(uint16_t crc); +void ICACHE_FLASH_ATTR CMD_ResponseEnd(uint16_t crc); -void CMD_Response(uint16_t cmd, uint32_t callback, uint32_t _return, uint16_t argc, CmdArg* args[]); +//void ICACHE_FLASH_ATTR CMD_Response(uint16_t cmd, uint32_t callback, uint32_t _return, uint16_t argc, CmdArg* args[]); // Requests diff --git a/cmd/handlers.c b/cmd/handlers.c index 73370dd..8e6deb0 100644 --- a/cmd/handlers.c +++ b/cmd/handlers.c @@ -9,10 +9,14 @@ #include "serbridge.h" #include "uart.h" #include "cgiwifi.h" +#include static uint32_t ICACHE_FLASH_ATTR CMD_Null(CmdPacket *cmd); static uint32_t ICACHE_FLASH_ATTR CMD_IsReady(CmdPacket *cmd); static uint32_t ICACHE_FLASH_ATTR CMD_WifiConnect(CmdPacket *cmd); +static uint32_t ICACHE_FLASH_ATTR CMD_AddSensor(CmdPacket *cmd); + +static uint8_t lastWifiStatus = wifiIsDisconnected; // Command dispatch table for serial -> ESP commands const CmdList commands[] = { @@ -33,34 +37,83 @@ const CmdList commands[] = { {CMD_REST_SETUP, REST_Setup}, {CMD_REST_REQUEST, REST_Request}, {CMD_REST_SETHEADER, REST_SetHeader}, + + {CMD_ADD_SENSOR, CMD_AddSensor }, + {CMD_NULL, NULL} }; +// WifiCb plus 10 for sensors +cmdCallback callbacks[12] = { + { { '\0' }, -1 }, + { { '\0' }, -1 }, + { { '\0' }, -1 }, + { { '\0' }, -1 }, + { { '\0' }, -1 }, + { { '\0' }, -1 }, + { { '\0' }, -1 }, + { { '\0' }, -1 }, + { { '\0' }, -1 }, + { { '\0' }, -1 }, + { { '\0' }, -1 } +}; + // Command handler for IsReady (healthcheck) command static uint32_t ICACHE_FLASH_ATTR CMD_IsReady(CmdPacket *cmd) { - os_printf("CMD: Check ready\n"); + os_printf("CMD_IsReady: Check ready\n"); return 1; } // Command handler for Null command static uint32_t ICACHE_FLASH_ATTR CMD_Null(CmdPacket *cmd) { - os_printf("CMD: NULL/unsupported command\n"); + os_printf("CMD_Null: NULL/unsupported command\n"); return 1; } -static uint8_t lastWifiStatus; -static uint32_t wifiCallback; +static void ICACHE_FLASH_ATTR +CMD_AddCb(char* name, uint32_t cb) { + char checkname[16]; + os_strncpy(checkname, name, sizeof(checkname)); + for (uint8_t i = 0; i < sizeof(commands); i++) { + os_printf("CMD_AddCb: index %d name=%s cb=%p\n", i, callbacks[i].name, (void *)callbacks[i].callback); + // find existing callback or add to the end + if (os_strcmp(callbacks[i].name, checkname) == 0 || callbacks[i].name[0] == '\0') { + os_strncpy(callbacks[i].name, checkname, sizeof(checkname)); + callbacks[i].callback = cb; + os_printf("CMD_AddCb: cb %s added at index %d\n", callbacks[i].name, i); + break; + } + } +} + +cmdCallback* ICACHE_FLASH_ATTR +CMD_GetCbByName(char* name) { + char checkname[16]; + os_strncpy(checkname, name, sizeof(checkname)); + for (uint8_t i = 0; i < sizeof(commands); i++) { + os_printf("CMD_GetCbByName: index %d name=%s cb=%p\n", i, callbacks[i].name, (void *)callbacks[i].callback); + // if callback doesn't exist or it's null + if (os_strcmp(callbacks[i].name, checkname) == 0) { + os_printf("CMD_GetCbByName: cb %s found at index %d\n", callbacks[i].name, i); + return &callbacks[i]; + } + } + os_printf("CMD_GetCbByName: cb %s not found\n", name); + return 0; +} // Callback from wifi subsystem to notify us of status changes static void ICACHE_FLASH_ATTR CMD_WifiCb(uint8_t wifiStatus) { if (wifiStatus != lastWifiStatus){ + os_printf("CMD_WifiCb: wifiStatus=%d\n", wifiStatus); lastWifiStatus = wifiStatus; - if (wifiCallback) { + cmdCallback *wifiCb = CMD_GetCbByName("wifiCb"); + if ((uint32_t)wifiCb->callback != -1) { uint8_t status = wifiStatus == wifiGotIP ? 5 : 1; - uint16_t crc = CMD_ResponseStart(CMD_WIFI_CONNECT, wifiCallback, 0, 1); + uint16_t crc = CMD_ResponseStart(CMD_WIFI_CONNECT, (uint32_t)wifiCb->callback, 0, 1); crc = CMD_ResponseBody(crc, (uint8_t*)&status, 1); CMD_ResponseEnd(crc); } @@ -70,10 +123,43 @@ CMD_WifiCb(uint8_t wifiStatus) { // Command handler for Wifi connect command static uint32_t ICACHE_FLASH_ATTR CMD_WifiConnect(CmdPacket *cmd) { + CmdRequest req; + CMD_Request(&req, cmd); + os_printf("CMD_WifiConnect: setup argc=%ld\n", CMD_GetArgc(&req)); if(cmd->argc != 2 || cmd->callback == 0) - return 0xFFFFFFFF; + return 0; wifiStatusCb = CMD_WifiCb; // register our callback with wifi subsystem - wifiCallback = cmd->callback; // save the MCU's callback + CMD_AddCb("wifiCb", (uint32_t)cmd->callback); // save the MCU's callback + lastWifiStatus = wifiIsDisconnected; + if (statusWifiState() != wifiIsDisconnected) { + CMD_WifiCb(statusWifiState()); + } + + return 1; +} + +// Command handler for Wifi connect command +static uint32_t ICACHE_FLASH_ATTR +CMD_AddSensor(CmdPacket *cmd) { + CmdRequest req; + CMD_Request(&req, cmd); + os_printf("CMD_AddSensor: setup argc=%ld\n", CMD_GetArgc(&req)); + if (cmd->argc != 1 || cmd->callback == 0) + return 0; + + uint8_t* name; + uint16_t len; + + // get the sensor name + len = CMD_ArgLen(&req); + os_printf("CMD_AddSensor: name len=%d\n", len); + if (len > 15) return 0; // max size of name is 15 characters + name = (uint8_t*)os_zalloc(len + 1); + if (CMD_PopArg(&req, name, len)) return 0; + name[len] = 0; + os_printf("CMD_AddSensor: name=%s\n", name); + + CMD_AddCb((char*)name, (uint32_t)cmd->callback); // save the sensor callback return 1; } diff --git a/html-old/console.html b/httpd/html-old/console.html similarity index 100% rename from html-old/console.html rename to httpd/html-old/console.html diff --git a/html-old/console.js b/httpd/html-old/console.js similarity index 100% rename from html-old/console.js rename to httpd/html-old/console.js diff --git a/html-old/favicon.ico b/httpd/html-old/favicon.ico similarity index 100% rename from html-old/favicon.ico rename to httpd/html-old/favicon.ico diff --git a/html-old/head- b/httpd/html-old/head- similarity index 100% rename from html-old/head- rename to httpd/html-old/head- diff --git a/html-old/home.html b/httpd/html-old/home.html similarity index 100% rename from html-old/home.html rename to httpd/html-old/home.html diff --git a/html-old/jl-400x110.png- b/httpd/html-old/jl-400x110.png- similarity index 100% rename from html-old/jl-400x110.png- rename to httpd/html-old/jl-400x110.png- diff --git a/html-old/log.html b/httpd/html-old/log.html similarity index 100% rename from html-old/log.html rename to httpd/html-old/log.html diff --git a/html-old/pure.css b/httpd/html-old/pure.css similarity index 100% rename from html-old/pure.css rename to httpd/html-old/pure.css diff --git a/html-old/style.css b/httpd/html-old/style.css similarity index 100% rename from html-old/style.css rename to httpd/html-old/style.css diff --git a/html-old/ui.js b/httpd/html-old/ui.js similarity index 100% rename from html-old/ui.js rename to httpd/html-old/ui.js diff --git a/html-old/wifi/icons.png b/httpd/html-old/wifi/icons.png similarity index 100% rename from html-old/wifi/icons.png rename to httpd/html-old/wifi/icons.png diff --git a/html-old/wifi/wifi.html b/httpd/html-old/wifi/wifi.html similarity index 100% rename from html-old/wifi/wifi.html rename to httpd/html-old/wifi/wifi.html diff --git a/html-old/wifi/wifi.js b/httpd/html-old/wifi/wifi.js similarity index 100% rename from html-old/wifi/wifi.js rename to httpd/html-old/wifi/wifi.js