cmd handler stuff

This commit is contained in:
Wolfgang Hottgenroth 2020-11-16 15:40:50 +01:00
parent 699fea520f
commit 2266ce6f7e
Signed by: wn
GPG Key ID: 6C1E5E531E0D5D7F

View File

@ -30,6 +30,16 @@ typedef enum {
} chState_t; } 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 // clear statistics
bool clearCmd() { bool clearCmd() {
t_mbusCommStats zeroedStats = { .requestCnt = 0, .errorCnt = 0 }; t_mbusCommStats zeroedStats = { .requestCnt = 0, .errorCnt = 0 };
@ -37,6 +47,26 @@ bool clearCmd() {
return true; 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 0 to continue waiting for input
// returns -1 to close the connection // returns -1 to close the connection
// returns 1 to toggle to config mode // 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" \ "quit ................................. Terminate the console session\n\r" \
"enable ............................... Enable configuration mode\n\r" \ "enable ............................... Enable configuration mode\n\r" \
"disable .............................. Disable 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 GOODBYE_MSG[] = "Good bye\n\r";
const static uint8_t OK_MSG[] = "OK\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; messageToSend = GOODBYE_MSG;
retCode = -1; retCode = -1;
} else if (0 == strcmp(cmd, "help")) { } 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")) { } else if (0 == strcmp(cmd, "enable")) {
coloredMsg(LOG_YELLOW, "cec, enable config mode"); coloredMsg(LOG_YELLOW, "cec, enable config mode");
configMode = true; configMode = true;
@ -80,14 +116,25 @@ int8_t cmdExecuteCommand(uint8_t *cmd, bool resetConfigMode) {
coloredMsg(LOG_YELLOW, "cec, disable config mode"); coloredMsg(LOG_YELLOW, "cec, disable config mode");
configMode = false; configMode = false;
retCode = 2; retCode = 2;
} else if (0 == strcmp(cmd, "clear")) { } else {
if (configMode) { uint8_t cmdIdx = 0;
messageToSend = clearCmd() ? OK_MSG : FAILED_MSG; while (true) {
} else { cmd_t command = COMMANDS[cmdIdx];
messageToSend = REQUIRES_CONFIG_MODE_MGS; 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) { if (messageToSend) {
send(CMD_SOCK, messageToSend, strlen(messageToSend)); send(CMD_SOCK, messageToSend, strlen(messageToSend));
} }