cmd handler stuff
This commit is contained in:
parent
699fea520f
commit
2266ce6f7e
@ -30,6 +30,16 @@ typedef enum {
|
||||
} chState_t;
|
||||
|
||||
|
||||
typedef bool (*cmdFunc_t)(char *cmdLine);
|
||||
|
||||
typedef struct {
|
||||
bool requiredConfigMode;
|
||||
char name[16];
|
||||
char help[256];
|
||||
cmdFunc_t cmdFunc;
|
||||
} cmd_t;
|
||||
|
||||
|
||||
// clear statistics
|
||||
bool clearCmd() {
|
||||
t_mbusCommStats zeroedStats = { .requestCnt = 0, .errorCnt = 0 };
|
||||
@ -37,6 +47,26 @@ bool clearCmd() {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const static cmd_t COMMANDS[] = {
|
||||
{ .requiredConfigMode = true,
|
||||
.name = "clear",
|
||||
.help = \
|
||||
"clear ................................ Clears the global Meterbus\n\r" \
|
||||
" statistics\n\r" \
|
||||
" Required configuration mode\n\r",
|
||||
.cmdFunc = clearCmd
|
||||
},
|
||||
{ .requiredConfigMode = false,
|
||||
.name = "END_OF_CMDS",
|
||||
.help = "",
|
||||
.cmdFunc = NULL
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// returns 0 to continue waiting for input
|
||||
// returns -1 to close the connection
|
||||
// returns 1 to toggle to config mode
|
||||
@ -49,9 +79,6 @@ int8_t cmdExecuteCommand(uint8_t *cmd, bool resetConfigMode) {
|
||||
"quit ................................. Terminate the console session\n\r" \
|
||||
"enable ............................... Enable configuration mode\n\r" \
|
||||
"disable .............................. Disable configuration mode\n\r" \
|
||||
"clear ................................ Clears the global Meterbus\n\r" \
|
||||
" statistics\n\r" \
|
||||
" Required configuration mode\n\r" \
|
||||
;
|
||||
const static uint8_t GOODBYE_MSG[] = "Good bye\n\r";
|
||||
const static uint8_t OK_MSG[] = "OK\n\r";
|
||||
@ -71,7 +98,16 @@ int8_t cmdExecuteCommand(uint8_t *cmd, bool resetConfigMode) {
|
||||
messageToSend = GOODBYE_MSG;
|
||||
retCode = -1;
|
||||
} else if (0 == strcmp(cmd, "help")) {
|
||||
messageToSend = HELP_MSG;
|
||||
send(CMD_SOCK, HELP_MSG, strlen(HELP_MSG));
|
||||
uint8_t cmdIdx = 0;
|
||||
while (true) {
|
||||
cmd_t command = COMMANDS[cmdIdx];
|
||||
if (0 == strcmp("END_OF_CMDS", command.name)) {
|
||||
break;
|
||||
}
|
||||
send(CMD_SOCK, command.help, strlen(command.help));
|
||||
}
|
||||
messageToSend = NULL;
|
||||
} else if (0 == strcmp(cmd, "enable")) {
|
||||
coloredMsg(LOG_YELLOW, "cec, enable config mode");
|
||||
configMode = true;
|
||||
@ -80,14 +116,25 @@ int8_t cmdExecuteCommand(uint8_t *cmd, bool resetConfigMode) {
|
||||
coloredMsg(LOG_YELLOW, "cec, disable config mode");
|
||||
configMode = false;
|
||||
retCode = 2;
|
||||
} else if (0 == strcmp(cmd, "clear")) {
|
||||
if (configMode) {
|
||||
messageToSend = clearCmd() ? OK_MSG : FAILED_MSG;
|
||||
} else {
|
||||
messageToSend = REQUIRES_CONFIG_MODE_MGS;
|
||||
} else {
|
||||
uint8_t cmdIdx = 0;
|
||||
while (true) {
|
||||
cmd_t command = COMMANDS[cmdIdx];
|
||||
if (0 == strcmp("END_OF_CMDS", command.name)) {
|
||||
break;
|
||||
}
|
||||
if (0 == strcmp(cmd, command.name)) {
|
||||
if (command.requiredConfigMode && !configMode) {
|
||||
messageToSend = REQUIRES_CONFIG_MODE_MGS;
|
||||
} else {
|
||||
messageToSend = command.cmdFunc(cmd) ? OK_MSG : FAILED_MSG;
|
||||
}
|
||||
}
|
||||
cmdIdx++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (messageToSend) {
|
||||
send(CMD_SOCK, messageToSend, strlen(messageToSend));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user