device configuration prepared

This commit is contained in:
Wolfgang Hottgenroth 2020-12-01 11:39:12 +01:00
parent a14928b035
commit 78d32a7cc9
Signed by: wn
GPG Key ID: 6C1E5E531E0D5D7F

View File

@ -5,6 +5,7 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <socket.h>
@ -35,25 +36,39 @@ typedef enum {
typedef bool (*cmdFunc_t)(uint8_t argc, char **args);
typedef struct {
bool requiredAdminMode;
char name[16];
char help[512];
cmdFunc_t cmdFunc;
} cmd_t;
static void sendString(char *buf) {
send(CMD_SOCK, (uint8_t*)buf, strlen(buf));
}
static bool sendFormatString(const char *format, ...) {
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;
}
// clear statistics
bool clearCmd(uint8_t argc, char **args) {
static bool clearCmd(uint8_t argc, char **args) {
t_mbusCommStats zeroedStats = { .mbusRequestCnt = 0, .mbusErrorCnt = 0, .uartOctetCnt = 0, .uartOverrunCnt = 0, .uartFramingErrCnt = 0, .uartParityErrCnt = 0, .uartNoiseErrCnt = 0 };
mbusCommSetStats(zeroedStats);
coloredMsg(LOG_YELLOW, true, "ch cc global statistics cleared");
return true;
}
bool globalStatsCmd(uint8_t argc, char **args) {
static bool globalStatsCmd(uint8_t argc, char **args) {
t_mbusCommStats *stats = mbusCommGetStats();
char buf[256];
sprintf(buf, \
sendFormatString(\
"Global statistics\n\r" \
" Meterbus Requests: %ld\n\r" \
" Meterbus Errors: %ld\n\r" \
@ -65,11 +80,10 @@ bool globalStatsCmd(uint8_t argc, char **args) {
stats->mbusRequestCnt, stats->mbusErrorCnt,
stats->uartOctetCnt, stats->uartOverrunCnt, stats->uartFramingErrCnt, stats->uartParityErrCnt, stats->uartNoiseErrCnt
);
send(CMD_SOCK, (uint8_t*)buf, strlen(buf));
return true;
}
bool mbusCommEnableCmd(uint8_t argc, char **args) {
static bool mbusCommEnableCmd(uint8_t argc, char **args) {
bool retCode = true;
if (argc == 2) {
if (0 == strcmp("false", args[1])) {
@ -87,7 +101,7 @@ bool mbusCommEnableCmd(uint8_t argc, char **args) {
return retCode;
}
bool loopEnableCmd(uint8_t argc, char **args) {
static bool loopEnableCmd(uint8_t argc, char **args) {
bool retCode = true;
if (argc == 2) {
if (0 == strcmp("false", args[1])) {
@ -105,19 +119,35 @@ bool loopEnableCmd(uint8_t argc, char **args) {
return retCode;
}
static bool showConfigCmd(uint8_t argc, char **args) {
bool retCode = true;
sendString("This is the configuration output");
return retCode;
}
const static cmd_t COMMANDS[] = {
{ .requiredAdminMode = true, .name = "clear", .cmdFunc = clearCmd,
{ .name = "globalStats", .cmdFunc = globalStatsCmd,
.help = \
"globalStats .......................... Show the global statistics\n\r" \
" counters requestCnt and errorCnt\n\r"
},
{ .name = "END_OF_CMDS", .help = "",.cmdFunc = NULL }
};
const static cmd_t ADMIN_COMMANDS[] = {
{ .name = "clear", .cmdFunc = clearCmd,
.help = \
"clear ................................ Clears the global Meterbus\n\r" \
" statistics\n\r" \
" Required configuration mode\n\r"
" statistics\n\r"
},
{ .requiredAdminMode = true, .name = "mbusCommEnable", .cmdFunc = mbusCommEnableCmd,
{ .name = "mbusCommEnable", .cmdFunc = mbusCommEnableCmd,
.help = \
"mbusCommEnable true|false ............ Enables or disables the Meterbus\n\r" \
" communication\n\r"
},
{ .requiredAdminMode = true, .name = "loopEnable", .cmdFunc = loopEnableCmd,
{ .name = "loopEnable", .cmdFunc = loopEnableCmd,
.help = \
"loopEnable true|false ................ Enables or disables the loop.\n\r" \
" Disable Meterbus communication\n\r" \
@ -125,38 +155,51 @@ const static cmd_t COMMANDS[] = {
" for a longer time, otherwise the\n\r" \
" request will enable it again\n\r"
},
{ .requiredAdminMode = false, .name = "globalStats", .cmdFunc = globalStatsCmd,
{ .name = "END_OF_CMDS", .help = "",.cmdFunc = NULL }
};
const static cmd_t CONFIG_COMMANDS[] = {
{ .name = "show", .cmdFunc = showConfigCmd,
.help = \
"globalStats .......................... Show the global statistics\n\r" \
" counters requestCnt and errorCnt\n\r"
"show ................................. Show the configuration\n\r"
},
{ .requiredAdminMode = false, .name = "END_OF_CMDS", .help = "",.cmdFunc = NULL }
{ .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
// returns 2 to toggle back to default mode
int8_t cmdExecuteCommand(uint8_t *cmdLine, bool resetAdminMode) {
// 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) {
const static char HELP_MSG[] = \
"Usage\n\r" \
"\n\r" \
"help ................................. Show this help page\n\r" \
"quit ................................. Terminate the console session\n\r" \
"enable ............................... Enable admin mode\n\r" \
"disable .............................. Disable admin mode\n\r" \
"config ............................... Enter configuration mode\n\r" \
"disable .............................. Disable admin/config mode\n\r" \
;
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";
const static char REQUIRES_ADMIN_MODE_MGS[] = "Not executed, requires admin mode\n\r";
uint8_t *messageToSend = NULL;
static bool adminMode = false;
static bool configMode = false;
if (resetAdminMode) {
if (resetSpecialModes) {
adminMode = false;
configMode = false;
}
cmd_t const * commands = COMMANDS;
if (adminMode) {
commands = ADMIN_COMMANDS;
} else if (configMode) {
commands = CONFIG_COMMANDS;
}
coloredMsg(LOG_YELLOW, false, "ch cec cmdLine is %s", cmdLine);;
@ -184,7 +227,7 @@ int8_t cmdExecuteCommand(uint8_t *cmdLine, bool resetAdminMode) {
send(CMD_SOCK, (uint8_t*)HELP_MSG, strlen(HELP_MSG));
uint8_t cmdIdx = 0;
while (true) {
cmd_t command = COMMANDS[cmdIdx];
cmd_t command = commands[cmdIdx];
if (0 == strcmp("END_OF_CMDS", command.name)) {
break;
}
@ -199,20 +242,21 @@ int8_t cmdExecuteCommand(uint8_t *cmdLine, bool resetAdminMode) {
} else if (0 == strcmp(cmd, "disable")) {
coloredMsg(LOG_YELLOW, true, "ch cec disable admin mode");
adminMode = false;
retCode = 3;
} else if (0 == strcmp(cmd, "config")) {
coloredMsg(LOG_YELLOW, true, "ch cec enable config mode");
configMode = true;
retCode = 2;
} else {
uint8_t cmdIdx = 0;
while (true) {
cmd_t command = COMMANDS[cmdIdx];
cmd_t command = commands[cmdIdx];
if (0 == strcmp("END_OF_CMDS", command.name)) {
break;
}
if (0 == strcmp(cmd, command.name)) {
if (command.requiredAdminMode && !adminMode) {
messageToSend = (uint8_t*)REQUIRES_ADMIN_MODE_MGS;
} else {
messageToSend = command.cmdFunc(argc, args) ? (uint8_t*)OK_MSG : (uint8_t*)FAILED_MSG;
}
break;
}
cmdIdx++;
}
@ -225,11 +269,11 @@ int8_t cmdExecuteCommand(uint8_t *cmdLine, bool resetAdminMode) {
return retCode;
}
void cmdHandlerEngine(void *handle) {
static void cmdHandlerEngine(void *handle) {
static uint8_t receiveBuffer[256];
static chState_t state = CH_INIT;
static bool resetAdminMode = false;
static bool resetSpecialModes = false;
static char banner[] = \
"MBGW3\n\r" \
@ -237,8 +281,9 @@ void cmdHandlerEngine(void *handle) {
"or quit to close the connection.\n\r";
static char *prompt;
static char defaultPrompt[] = "MBGW3 # ";
static char elevatedPrompt[] = "MBGW3 (admin) > ";
static char defaultPrompt[] = "MBGW3 > ";
static char adminPrompt[] = "MBGW3 (admin) # ";
static char configPrompt[] = "MBGW3 (config) $ ";
int8_t res = 0;
@ -301,7 +346,7 @@ void cmdHandlerEngine(void *handle) {
resultSend = send(CMD_SOCK, (uint8_t*)banner, strlen(banner));
coloredMsg(LOG_YELLOW, false, "ch che sent banner, send returns 0x%02x", resultSend);
prompt = defaultPrompt;
resetAdminMode = true;
resetSpecialModes = true;
state = CH_PROMPT;
}
break;
@ -341,8 +386,8 @@ void cmdHandlerEngine(void *handle) {
receiveBuffer[strlen((char*)receiveBuffer) - 1] = 0;
}
coloredMsg(LOG_YELLOW, false, "ch che received: %s", receiveBuffer);
int8_t resCEC = cmdExecuteCommand(receiveBuffer, resetAdminMode);
resetAdminMode = false;
int8_t resCEC = cmdExecuteCommand(receiveBuffer, resetSpecialModes);
resetSpecialModes = false;
switch (resCEC) {
case 0:
state = CH_PROMPT;
@ -351,10 +396,14 @@ void cmdHandlerEngine(void *handle) {
state = CH_DISCONNECT;
break;
case 1:
prompt = elevatedPrompt;
prompt = adminPrompt;
state = CH_PROMPT;
break;
case 2:
prompt = configPrompt;
state = CH_PROMPT;
break;
case 3:
prompt = defaultPrompt;
state = CH_PROMPT;
break;