|
|
@ -10,17 +10,19 @@ |
|
|
|
#include "uart.h" |
|
|
|
#include "uart.h" |
|
|
|
#include "cgiwifi.h" |
|
|
|
#include "cgiwifi.h" |
|
|
|
|
|
|
|
|
|
|
|
static uint32_t ICACHE_FLASH_ATTR CMD_Null(CmdPacket *cmd); |
|
|
|
static uint32_t CMD_Null(CmdPacket *cmd); |
|
|
|
static uint32_t ICACHE_FLASH_ATTR CMD_IsReady(CmdPacket *cmd); |
|
|
|
static uint32_t CMD_IsReady(CmdPacket *cmd); |
|
|
|
static uint32_t ICACHE_FLASH_ATTR CMD_WifiConnect(CmdPacket *cmd); |
|
|
|
static uint32_t CMD_Reset(CmdPacket *cmd); |
|
|
|
static uint32_t ICACHE_FLASH_ATTR CMD_AddSensor(CmdPacket *cmd); |
|
|
|
static uint32_t CMD_WifiConnect(CmdPacket *cmd); |
|
|
|
|
|
|
|
static uint32_t CMD_AddCallback(CmdPacket *cmd); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// keep track of last status sent to uC so we can notify it when it changes
|
|
|
|
static uint8_t lastWifiStatus = wifiIsDisconnected; |
|
|
|
static uint8_t lastWifiStatus = wifiIsDisconnected; |
|
|
|
|
|
|
|
|
|
|
|
// Command dispatch table for serial -> ESP commands
|
|
|
|
// Command dispatch table for serial -> ESP commands
|
|
|
|
const CmdList commands[] = { |
|
|
|
const CmdList commands[] = { |
|
|
|
{CMD_NULL, CMD_Null}, |
|
|
|
{CMD_NULL, CMD_Null}, |
|
|
|
{CMD_RESET, CMD_Null}, |
|
|
|
{CMD_RESET, CMD_Reset}, |
|
|
|
{CMD_IS_READY, CMD_IsReady}, |
|
|
|
{CMD_IS_READY, CMD_IsReady}, |
|
|
|
{CMD_WIFI_CONNECT, CMD_WifiConnect}, |
|
|
|
{CMD_WIFI_CONNECT, CMD_WifiConnect}, |
|
|
|
|
|
|
|
|
|
|
@ -37,25 +39,14 @@ const CmdList commands[] = { |
|
|
|
{CMD_REST_REQUEST, REST_Request}, |
|
|
|
{CMD_REST_REQUEST, REST_Request}, |
|
|
|
{CMD_REST_SETHEADER, REST_SetHeader}, |
|
|
|
{CMD_REST_SETHEADER, REST_SetHeader}, |
|
|
|
|
|
|
|
|
|
|
|
{CMD_ADD_SENSOR, CMD_AddSensor }, |
|
|
|
{CMD_ADD_CALLBACK, CMD_AddCallback }, |
|
|
|
|
|
|
|
|
|
|
|
{CMD_NULL, NULL} |
|
|
|
{CMD_NULL, NULL} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// WifiCb plus 10 for sensors
|
|
|
|
// WifiCb plus 10 for sensors
|
|
|
|
cmdCallback callbacks[12] = { |
|
|
|
#define MAX_CALLBACKS 12 |
|
|
|
{ { '\0' }, -1 }, |
|
|
|
cmdCallback callbacks[MAX_CALLBACKS]; // cleared in CMD_Reset
|
|
|
|
{ { '\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
|
|
|
|
// Command handler for IsReady (healthcheck) command
|
|
|
|
static uint32_t ICACHE_FLASH_ATTR |
|
|
|
static uint32_t ICACHE_FLASH_ATTR |
|
|
@ -71,20 +62,31 @@ CMD_Null(CmdPacket *cmd) { |
|
|
|
return 1; |
|
|
|
return 1; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void ICACHE_FLASH_ATTR
|
|
|
|
// Command handler for Reset command, this was originally to reset the ESP but we don't want to
|
|
|
|
|
|
|
|
// do that is esp-link. It is still good to clear any information the ESP has about the attached
|
|
|
|
|
|
|
|
// uC.
|
|
|
|
|
|
|
|
static uint32_t ICACHE_FLASH_ATTR |
|
|
|
|
|
|
|
CMD_Reset(CmdPacket *cmd) { |
|
|
|
|
|
|
|
os_printf("CMD_Reset\n"); |
|
|
|
|
|
|
|
// clear callbacks table
|
|
|
|
|
|
|
|
os_memset(callbacks, 0, sizeof(callbacks)); |
|
|
|
|
|
|
|
return 1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static uint32_t ICACHE_FLASH_ATTR |
|
|
|
CMD_AddCb(char* name, uint32_t cb) { |
|
|
|
CMD_AddCb(char* name, uint32_t cb) { |
|
|
|
char checkname[16]; |
|
|
|
for (uint8_t i = 0; i < MAX_CALLBACKS; i++) { |
|
|
|
os_strncpy(checkname, name, sizeof(checkname)); |
|
|
|
//os_printf("CMD_AddCb: index %d name=%s cb=%p\n", i, callbacks[i].name,
|
|
|
|
for (uint8_t i = 0; i < sizeof(commands); i++) {
|
|
|
|
// (void *)callbacks[i].callback);
|
|
|
|
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
|
|
|
|
// find existing callback or add to the end
|
|
|
|
if (os_strcmp(callbacks[i].name, checkname) == 0 || callbacks[i].name[0] == '\0') { |
|
|
|
if (os_strcmp(callbacks[i].name, name) == 0 || callbacks[i].name[0] == '\0') { |
|
|
|
os_strncpy(callbacks[i].name, checkname, sizeof(checkname)); |
|
|
|
os_strncpy(callbacks[i].name, name, sizeof(callbacks[i].name)); |
|
|
|
callbacks[i].callback = cb; |
|
|
|
callbacks[i].callback = cb; |
|
|
|
os_printf("CMD_AddCb: cb %s added at index %d\n", callbacks[i].name, i); |
|
|
|
os_printf("CMD_AddCb: cb %s added at index %d\n", callbacks[i].name, i); |
|
|
|
break; |
|
|
|
return 1; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
cmdCallback* ICACHE_FLASH_ATTR |
|
|
|
cmdCallback* ICACHE_FLASH_ATTR |
|
|
@ -92,7 +94,8 @@ CMD_GetCbByName(char* name) { |
|
|
|
char checkname[16]; |
|
|
|
char checkname[16]; |
|
|
|
os_strncpy(checkname, name, sizeof(checkname)); |
|
|
|
os_strncpy(checkname, name, sizeof(checkname)); |
|
|
|
for (uint8_t i = 0; i < sizeof(commands); i++) { |
|
|
|
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); |
|
|
|
//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 callback doesn't exist or it's null
|
|
|
|
if (os_strcmp(callbacks[i].name, checkname) == 0) { |
|
|
|
if (os_strcmp(callbacks[i].name, checkname) == 0) { |
|
|
|
os_printf("CMD_GetCbByName: cb %s found at index %d\n", callbacks[i].name, i); |
|
|
|
os_printf("CMD_GetCbByName: cb %s found at index %d\n", callbacks[i].name, i); |
|
|
@ -130,33 +133,31 @@ CMD_WifiConnect(CmdPacket *cmd) { |
|
|
|
|
|
|
|
|
|
|
|
wifiStatusCb = CMD_WifiCb; // register our callback with wifi subsystem
|
|
|
|
wifiStatusCb = CMD_WifiCb; // register our callback with wifi subsystem
|
|
|
|
CMD_AddCb("wifiCb", (uint32_t)cmd->callback); // save the MCU's callback
|
|
|
|
CMD_AddCb("wifiCb", (uint32_t)cmd->callback); // save the MCU's callback
|
|
|
|
lastWifiStatus = wifiIsDisconnected; |
|
|
|
lastWifiStatus = 0xff; // set to invalid value so we immediately send status cb in all cases
|
|
|
|
CMD_WifiCb(wifiState); |
|
|
|
CMD_WifiCb(wifiState); |
|
|
|
|
|
|
|
|
|
|
|
return 1; |
|
|
|
return 1; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Command handler for Wifi connect command
|
|
|
|
// Command handler to add a callback to the named-callbacks list, this is for a callback to the uC
|
|
|
|
static uint32_t ICACHE_FLASH_ATTR |
|
|
|
static uint32_t ICACHE_FLASH_ATTR |
|
|
|
CMD_AddSensor(CmdPacket *cmd) { |
|
|
|
CMD_AddCallback(CmdPacket *cmd) { |
|
|
|
CmdRequest req; |
|
|
|
CmdRequest req; |
|
|
|
CMD_Request(&req, cmd); |
|
|
|
CMD_Request(&req, cmd); |
|
|
|
os_printf("CMD_AddSensor: setup argc=%ld\n", CMD_GetArgc(&req)); |
|
|
|
os_printf("CMD_AddCallback: setup argc=%ld\n", CMD_GetArgc(&req)); |
|
|
|
if (cmd->argc != 1 || cmd->callback == 0) |
|
|
|
if (cmd->argc != 1 || cmd->callback == 0) |
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|
|
|
|
|
|
|
|
|
uint8_t* name; |
|
|
|
char name[16]; |
|
|
|
uint16_t len; |
|
|
|
uint16_t len; |
|
|
|
|
|
|
|
|
|
|
|
// get the sensor name
|
|
|
|
// get the sensor name
|
|
|
|
len = CMD_ArgLen(&req); |
|
|
|
len = CMD_ArgLen(&req); |
|
|
|
os_printf("CMD_AddSensor: name len=%d\n", len); |
|
|
|
os_printf("CMD_AddCallback: name len=%d\n", len); |
|
|
|
if (len > 15) return 0; // max size of name is 15 characters
|
|
|
|
if (len > 15) return 0; // max size of name is 15 characters
|
|
|
|
name = (uint8_t*)os_zalloc(len + 1); |
|
|
|
if (CMD_PopArg(&req, (uint8_t *)name, len)) return 0; |
|
|
|
if (CMD_PopArg(&req, name, len)) return 0; |
|
|
|
|
|
|
|
name[len] = 0; |
|
|
|
name[len] = 0; |
|
|
|
os_printf("CMD_AddSensor: name=%s\n", name); |
|
|
|
os_printf("CMD_AddCallback: name=%s\n", name); |
|
|
|
|
|
|
|
|
|
|
|
CMD_AddCb((char*)name, (uint32_t)cmd->callback); // save the sensor callback
|
|
|
|
return CMD_AddCb(name, (uint32_t)cmd->callback); // save the sensor callback
|
|
|
|
return 1; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|