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