337 lines
11 KiB
C
Raw Normal View History

2020-11-15 23:51:48 +01:00
#include <cmdHandler.h>
2020-12-01 12:43:18 +01:00
#include <cmdHelper.h>
2020-11-11 14:19:02 +01:00
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
2020-11-16 15:51:00 +01:00
#include <stdio.h>
2020-12-01 11:39:12 +01:00
#include <stdarg.h>
2020-11-11 14:19:02 +01:00
#include <socket.h>
#include <logger.h>
#include <PontCoopScheduler.h>
2020-11-11 14:26:28 +01:00
#include <wizHelper.h>
2020-11-11 14:19:02 +01:00
2020-11-15 23:51:48 +01:00
extern const uint8_t CMD_SOCK;
2020-11-11 14:19:02 +01:00
2020-11-16 14:16:54 +01:00
const uint16_t cmdPort = 23;
2020-11-16 13:23:58 +01:00
2020-11-16 14:33:54 +01:00
typedef enum {
CH_INIT,
CH_LISTEN,
CH_WAITING,
CH_BANNER,
CH_PROMPT,
CH_RECEIVE,
CH_DISCONNECT,
CH_DISCONNECT_WAIT,
CH_ERROR
} chState_t;
2020-11-16 14:07:29 +01:00
2020-12-01 12:43:18 +01:00
void sendString(const char *buf) {
2020-12-01 11:39:12 +01:00
send(CMD_SOCK, (uint8_t*)buf, strlen(buf));
}
2020-12-01 12:43:18 +01:00
bool sendFormatString(const char *format, ...) {
2020-12-01 11:39:12 +01:00
bool retCode = true;
va_list vl;
va_start(vl, format);
char buf[4096];
int vcnt = vsnprintf(buf, sizeof(buf), format, vl);
retCode = (vcnt < sizeof(buf));
va_end(vl);
sendString(buf);
return retCode;
}
2020-11-16 14:07:29 +01:00
// returns 0 to continue waiting for input
// returns -1 to close the connection
2020-12-01 11:39:12 +01:00
// returns 1 to toggle to admin mode
// returns 2 to toggle to config mode
// returns 3 to toggle back to default mode
static int8_t cmdExecuteCommand(uint8_t *cmdLine, bool resetSpecialModes) {
2020-11-20 11:23:44 +01:00
const static char HELP_MSG[] = \
2020-11-16 15:16:06 +01:00
"Usage\n\r" \
"\n\r" \
"help ................................. Show this help page\n\r" \
"quit ................................. Terminate the console session\n\r" \
2020-11-28 22:33:15 +01:00
"enable ............................... Enable admin mode\n\r" \
2020-12-01 11:39:12 +01:00
"config ............................... Enter configuration mode\n\r" \
"disable .............................. Disable admin/config mode\n\r" \
2020-11-16 15:16:06 +01:00
;
2020-12-01 12:20:02 +01:00
const static char CONFIG_INTRO_MSG[] = \
"In configuration mode each command changing the configuration\n\r" \
"will save changes directly to the EEPROM.\n\r" \
"However, the system will only consider these changes after a\n\r" \
"restart since only in this sitution the EEPROM is read.\n\r" \
"\n\r" \
;
2020-11-20 11:23:44 +01:00
const static char GOODBYE_MSG[] = "Good bye\n\r";
const static char OK_MSG[] = "OK\n\r";
const static char FAILED_MSG[] = "Failed\n\r";
2020-12-01 12:23:26 +01:00
const static char UNKNOWN_COMMAND[] = "Unknown command\n\r";
2020-11-16 14:56:05 +01:00
uint8_t *messageToSend = NULL;
2020-11-28 22:33:15 +01:00
static bool adminMode = false;
2020-12-01 11:39:12 +01:00
static bool configMode = false;
2020-11-16 14:42:23 +01:00
2020-12-01 11:39:12 +01:00
if (resetSpecialModes) {
2020-11-28 22:33:15 +01:00
adminMode = false;
2020-12-01 11:39:12 +01:00
configMode = false;
}
2020-12-01 12:43:18 +01:00
cmd_t const * commands = getRegularCommands();
2020-12-01 11:39:12 +01:00
if (adminMode) {
2020-12-01 12:43:18 +01:00
commands = getAdminCommands();
2020-12-01 11:39:12 +01:00
} else if (configMode) {
2020-12-01 12:43:18 +01:00
commands = getConfigCommands();
2020-11-16 14:42:23 +01:00
}
2020-11-16 14:07:29 +01:00
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, false, "ch cec cmdLine is %s", cmdLine);;
2020-11-16 16:13:20 +01:00
#define MAX_NUM_OF_ARGS 8
char *args[MAX_NUM_OF_TASKS];
uint8_t argc = 0;
2020-11-20 11:23:44 +01:00
char *inCmdLine = (char*)cmdLine;
2020-11-16 16:13:20 +01:00
do {
args[argc++] = strtok(inCmdLine, " ");
inCmdLine = NULL;
} while (args[argc - 1] != NULL);
2020-11-16 16:16:04 +01:00
if (argc > 0) {
argc--;
}
2020-11-16 16:13:20 +01:00
char *cmd = args[0];
2020-11-16 14:07:29 +01:00
int8_t retCode = 0;
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, false, "ch cec cmd is %s, number of arguments %d", cmd, argc);
2020-11-16 16:13:20 +01:00
2020-11-16 14:07:29 +01:00
if (0 == strcmp(cmd, "quit")) {
2020-11-20 11:23:44 +01:00
messageToSend = (uint8_t*)GOODBYE_MSG;
2020-11-16 14:07:29 +01:00
retCode = -1;
2020-11-16 15:16:06 +01:00
} else if (0 == strcmp(cmd, "help")) {
2020-12-01 12:20:02 +01:00
if (configMode) {
sendString(CONFIG_INTRO_MSG);
}
sendString(HELP_MSG);
2020-11-16 15:40:50 +01:00
uint8_t cmdIdx = 0;
while (true) {
2020-12-01 11:39:12 +01:00
cmd_t command = commands[cmdIdx];
2020-11-16 15:40:50 +01:00
if (0 == strcmp("END_OF_CMDS", command.name)) {
break;
}
2020-12-01 12:20:02 +01:00
sendString(command.help);
2020-11-16 15:42:30 +01:00
cmdIdx++;
2020-11-16 15:40:50 +01:00
}
messageToSend = NULL;
2020-11-16 14:42:23 +01:00
} else if (0 == strcmp(cmd, "enable")) {
2020-11-28 22:33:15 +01:00
coloredMsg(LOG_YELLOW, true, "ch cec enable admin mode");
adminMode = true;
2020-11-16 14:42:23 +01:00
retCode = 1;
} else if (0 == strcmp(cmd, "disable")) {
2020-11-28 22:33:15 +01:00
coloredMsg(LOG_YELLOW, true, "ch cec disable admin mode");
adminMode = false;
2020-12-01 12:20:02 +01:00
configMode = false;
2020-12-01 11:39:12 +01:00
retCode = 3;
} else if (0 == strcmp(cmd, "config")) {
coloredMsg(LOG_YELLOW, true, "ch cec enable config mode");
configMode = true;
2020-11-16 14:42:23 +01:00
retCode = 2;
2020-11-16 15:40:50 +01:00
} else {
uint8_t cmdIdx = 0;
while (true) {
2020-12-01 11:39:12 +01:00
cmd_t command = commands[cmdIdx];
2020-11-16 15:40:50 +01:00
if (0 == strcmp("END_OF_CMDS", command.name)) {
2020-12-01 12:23:26 +01:00
messageToSend = (uint8_t*) UNKNOWN_COMMAND;
2020-11-16 15:40:50 +01:00
break;
}
if (0 == strcmp(cmd, command.name)) {
2020-12-01 11:39:12 +01:00
messageToSend = command.cmdFunc(argc, args) ? (uint8_t*)OK_MSG : (uint8_t*)FAILED_MSG;
break;
2020-11-16 15:40:50 +01:00
}
cmdIdx++;
2020-11-16 14:56:05 +01:00
}
}
if (messageToSend) {
2020-11-20 11:23:44 +01:00
send(CMD_SOCK, messageToSend, strlen((char*)messageToSend));
2020-11-16 14:07:29 +01:00
}
return retCode;
}
2020-12-01 11:39:12 +01:00
static void cmdHandlerEngine(void *handle) {
2020-11-16 13:23:58 +01:00
static uint8_t receiveBuffer[256];
2020-11-11 14:19:02 +01:00
2020-11-16 14:33:54 +01:00
static chState_t state = CH_INIT;
2020-12-01 11:39:12 +01:00
static bool resetSpecialModes = false;
2020-11-11 14:45:35 +01:00
2020-11-20 11:23:44 +01:00
static char banner[] = \
2020-11-16 13:23:58 +01:00
"MBGW3\n\r" \
"Type help for usage help\n\r" \
"or quit to close the connection.\n\r";
2020-11-16 14:33:54 +01:00
2020-11-20 11:23:44 +01:00
static char *prompt;
2020-12-11 12:54:40 +01:00
static char defaultPrompt[] = " > ";
static char adminPrompt[] = " (admin) # ";
static char configPrompt[] = " (config) $ ";
2020-11-16 14:33:54 +01:00
2020-11-16 13:23:58 +01:00
int8_t res = 0;
uint8_t sockState;
int32_t resultSend;
int16_t receivedOctets;
int32_t resultRecv;
2020-11-16 13:51:16 +01:00
uint8_t resultDisconnect;
2020-11-11 14:26:28 +01:00
2020-11-16 14:42:23 +01:00
2020-11-11 14:26:28 +01:00
if (isNetworkAvailable()) {
2020-11-11 14:45:35 +01:00
switch (state) {
2020-11-16 14:33:54 +01:00
case CH_INIT:
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, false, "ch che initializing socket");
2020-11-11 14:26:28 +01:00
2020-11-16 13:23:58 +01:00
res = socket(CMD_SOCK, Sn_MR_TCP, cmdPort, SF_IO_NONBLOCK);
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, false, "ch che socket returns %d", res);
2020-11-11 14:26:28 +01:00
2020-11-15 23:51:48 +01:00
if (res == CMD_SOCK) {
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, false, "ch che socket is initialized");
2020-11-16 14:33:54 +01:00
state = CH_LISTEN;
2020-11-11 14:50:48 +01:00
} else {
2020-11-16 14:33:54 +01:00
state = CH_ERROR;
2020-11-11 14:26:28 +01:00
}
2020-11-11 14:45:35 +01:00
break;
2020-11-11 14:50:48 +01:00
2020-11-16 14:33:54 +01:00
case CH_LISTEN:
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, false, "ch che listening");
2020-11-11 14:45:35 +01:00
2020-11-16 13:23:58 +01:00
res = listen(CMD_SOCK);
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, false, "ch che listen returns %d", res);
2020-11-11 14:45:35 +01:00
2020-11-16 13:29:01 +01:00
if (res == SOCK_OK) {
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, false, "ch che ok, waiting for established");
2020-11-16 14:33:54 +01:00
state = CH_WAITING;
2020-11-11 14:50:48 +01:00
} else {
2020-11-16 14:33:54 +01:00
state = CH_ERROR;
2020-11-11 14:50:48 +01:00
}
2020-11-11 14:45:35 +01:00
break;
2020-11-11 14:50:48 +01:00
2020-11-16 14:33:54 +01:00
case CH_WAITING:
2020-11-16 13:23:58 +01:00
sockState = getSn_SR(CMD_SOCK);
2020-11-16 13:34:36 +01:00
if (sockState != SOCK_LISTEN) {
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, false, "ch che socket state is 0x%02x", sockState);
state = CH_DISCONNECT;
2020-11-16 13:34:36 +01:00
}
2020-11-11 14:50:48 +01:00
if (sockState == SOCK_ESTABLISHED) {
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, true, "ch che connection is established");
2020-11-16 14:33:54 +01:00
state = CH_BANNER;
2020-11-11 14:50:48 +01:00
}
2020-11-11 14:55:46 +01:00
break;
2020-11-11 15:01:03 +01:00
2020-11-16 14:33:54 +01:00
case CH_BANNER:
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, false, "ch che send banner");
2020-11-16 13:41:10 +01:00
sockState = getSn_SR(CMD_SOCK);
if (sockState != SOCK_ESTABLISHED) {
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, true, "ch che sockState is 0x%02x when trying to send banner", sockState);
2020-11-16 14:33:54 +01:00
state = CH_DISCONNECT;
2020-11-16 13:41:10 +01:00
} else {
2020-11-20 11:23:44 +01:00
resultSend = send(CMD_SOCK, (uint8_t*)banner, strlen(banner));
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, false, "ch che sent banner, send returns 0x%02x", resultSend);
2020-11-16 14:33:54 +01:00
prompt = defaultPrompt;
2020-12-01 11:39:12 +01:00
resetSpecialModes = true;
2020-11-16 14:33:54 +01:00
state = CH_PROMPT;
}
break;
case CH_PROMPT:
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, false, "ch che send prompt");
2020-11-16 14:33:54 +01:00
sockState = getSn_SR(CMD_SOCK);
if (sockState != SOCK_ESTABLISHED) {
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, true, "ch che sockState is 0x%02x when trying to send promt", sockState);
2020-11-16 14:33:54 +01:00
state = CH_DISCONNECT;
} else {
2020-12-11 12:54:40 +01:00
sendFormatString("%s %s", getConfig()->deviceName, prompt);
coloredMsg(LOG_YELLOW, false, "ch che sent prompt %s %s", getConfig()->deviceName, prompt);
2020-11-16 14:33:54 +01:00
state = CH_RECEIVE;
2020-11-16 13:41:10 +01:00
}
2020-11-11 18:26:54 +01:00
break;
2020-11-16 14:33:54 +01:00
case CH_RECEIVE:
2020-11-16 13:41:10 +01:00
sockState = getSn_SR(CMD_SOCK);
if (sockState != SOCK_ESTABLISHED) {
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, true, "ch che sockState is 0x%02x when trying to receive something", sockState);
2020-11-16 14:33:54 +01:00
state = CH_DISCONNECT;
2020-11-16 13:41:10 +01:00
} else {
receivedOctets = getSn_RX_RSR(CMD_SOCK);
if (receivedOctets > 0) {
memset(receiveBuffer, 0, sizeof(receiveBuffer));
resultRecv = recv(CMD_SOCK, receiveBuffer, sizeof(receiveBuffer));
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, false, "ch che recv returns 0x%02x", resultRecv);
2020-11-16 13:41:10 +01:00
if (resultRecv > 0) {
2020-11-20 11:23:44 +01:00
if ((receiveBuffer[strlen((char*)receiveBuffer) - 1] == 0x0a) ||
(receiveBuffer[strlen((char*)receiveBuffer) - 1] == 0x0d)) {
receiveBuffer[strlen((char*)receiveBuffer) - 1] = 0;
2020-11-16 14:15:25 +01:00
}
2020-11-20 11:23:44 +01:00
if ((receiveBuffer[strlen((char*)receiveBuffer) - 1] == 0x0a) ||
(receiveBuffer[strlen((char*)receiveBuffer) - 1] == 0x0d)) {
receiveBuffer[strlen((char*)receiveBuffer) - 1] = 0;
2020-11-16 14:11:11 +01:00
}
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, false, "ch che received: %s", receiveBuffer);
2020-12-01 11:39:12 +01:00
int8_t resCEC = cmdExecuteCommand(receiveBuffer, resetSpecialModes);
resetSpecialModes = false;
2020-11-16 14:42:23 +01:00
switch (resCEC) {
case 0:
state = CH_PROMPT;
break;
case -1:
state = CH_DISCONNECT;
break;
case 1:
2020-12-01 11:39:12 +01:00
prompt = adminPrompt;
2020-11-16 14:42:23 +01:00
state = CH_PROMPT;
break;
case 2:
2020-12-01 11:39:12 +01:00
prompt = configPrompt;
state = CH_PROMPT;
break;
case 3:
2020-11-16 14:42:23 +01:00
prompt = defaultPrompt;
state = CH_PROMPT;
break;
2020-11-16 14:07:29 +01:00
}
2020-11-16 13:41:10 +01:00
}
2020-11-11 18:37:53 +01:00
}
}
2020-11-11 18:26:54 +01:00
break;
2020-11-16 14:33:54 +01:00
case CH_DISCONNECT:
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, true, "ch che close our end");
2020-11-16 13:51:16 +01:00
resultDisconnect = disconnect(CMD_SOCK);
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, true, "ch che disconnect returns 0x%02x", resultDisconnect);
2020-11-16 14:33:54 +01:00
state = CH_DISCONNECT_WAIT;
2020-11-16 13:51:16 +01:00
break;
2020-11-16 14:33:54 +01:00
case CH_DISCONNECT_WAIT:
2020-12-01 12:43:18 +01:00
//coloredMsg(LOG_YELLOW, false, "ch che waiting after disconnect");
2020-11-16 13:51:16 +01:00
sockState = getSn_SR(CMD_SOCK);
2020-12-01 12:43:18 +01:00
//coloredMsg(LOG_YELLOW, false, "ch che sockState is 0x%02x", sockState);
2020-11-16 13:54:23 +01:00
if (sockState == SOCK_CLOSED) {
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, true, "ch che socket is closed now");
2020-11-16 14:33:54 +01:00
state = CH_INIT;
2020-11-16 13:54:23 +01:00
}
2020-11-16 13:51:16 +01:00
break;
2020-11-16 14:33:54 +01:00
case CH_ERROR:
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, true, "ch che error state, will stop here");
2020-11-16 13:23:58 +01:00
schDel(cmdHandlerEngine, NULL);
2020-11-11 14:45:35 +01:00
break;
2020-11-11 14:26:28 +01:00
}
}
2020-11-11 14:19:02 +01:00
}
2020-11-15 23:51:48 +01:00
void cmdHandlerInit() {
2020-11-16 13:23:58 +01:00
schAdd(cmdHandlerEngine, NULL, 0, 100);
2020-11-11 14:19:02 +01:00
}