395 lines
14 KiB
C
Raw Normal View History

2020-11-15 23:51:48 +01:00
#include <cmdHandler.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-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-16 14:56:05 +01:00
#include <mbusComm.h>
2020-11-16 17:53:18 +01:00
#include <loopCtrl.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-11-16 14:56:05 +01:00
2020-11-16 16:13:20 +01:00
typedef bool (*cmdFunc_t)(uint8_t argc, char **args);
2020-11-16 15:40:50 +01:00
typedef struct {
bool requiredConfigMode;
char name[16];
2020-11-16 17:54:27 +01:00
char help[512];
2020-11-16 15:40:50 +01:00
cmdFunc_t cmdFunc;
} cmd_t;
2020-11-16 14:56:05 +01:00
// clear statistics
2020-11-16 16:13:20 +01:00
bool clearCmd(uint8_t argc, char **args) {
2020-11-25 13:33:46 +01:00
t_mbusCommStats zeroedStats = { .mbusRequestCnt = 0, .mbusErrorCnt = 0, .uartOctetCnt = 0, .uartOverrunCnt = 0, .uartFramingErrCnt = 0, .uartParityErrCnt = 0, .uartNoiseErrCnt = 0 };
2020-11-16 14:56:05 +01:00
mbusCommSetStats(zeroedStats);
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, true, "ch cc global statistics cleared");
2020-11-16 14:56:05 +01:00
return true;
}
2020-11-16 16:13:20 +01:00
bool globalStatsCmd(uint8_t argc, char **args) {
2020-11-16 15:51:00 +01:00
t_mbusCommStats *stats = mbusCommGetStats();
char buf[256];
sprintf(buf, \
"Global statistics\n\r" \
2020-11-25 10:02:38 +01:00
" Meterbus Requests: %ld\n\r" \
" Meterbus Errors: %ld\n\r" \
2020-11-25 12:42:33 +01:00
" UART Octets: %ld\n\r" \
2020-11-25 10:02:38 +01:00
" UART Overruns: %ld\n\r" \
" UART Framing Errs: %ld\n\r" \
2020-11-25 13:33:46 +01:00
" UART Parity Errs: %ld\n\r" \
" UART Noise Errs: %ld\n\r",
2020-11-25 10:02:38 +01:00
stats->mbusRequestCnt, stats->mbusErrorCnt,
2020-11-25 13:33:46 +01:00
stats->uartOctetCnt, stats->uartOverrunCnt, stats->uartFramingErrCnt, stats->uartParityErrCnt, stats->uartNoiseErrCnt
2020-11-16 15:51:00 +01:00
);
2020-11-20 11:23:44 +01:00
send(CMD_SOCK, (uint8_t*)buf, strlen(buf));
2020-11-16 15:51:00 +01:00
return true;
}
2020-11-16 15:40:50 +01:00
2020-11-16 17:53:18 +01:00
bool mbusCommEnableCmd(uint8_t argc, char **args) {
bool retCode = true;
if (argc == 2) {
if (0 == strcmp("false", args[1])) {
mbusCommEnable(false);
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, true, "ch mcec Meterbus communication disabled");
2020-11-16 17:53:18 +01:00
} else if (0 == strcmp("true", args[1])) {
mbusCommEnable(true);
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, true, "ch mcec Meterbus communication enabled");
2020-11-16 17:53:18 +01:00
} else {
retCode = false;
}
} else {
retCode = false;
}
return retCode;
}
bool loopEnableCmd(uint8_t argc, char **args) {
bool retCode = true;
if (argc == 2) {
if (0 == strcmp("false", args[1])) {
loopDisable();
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, true, "ch lec loop disabled");
2020-11-16 17:53:18 +01:00
} else if (0 == strcmp("true", args[1])) {
loopEnable();
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, true, "ch lec loop enabled");
2020-11-16 17:53:18 +01:00
} else {
retCode = false;
}
} else {
retCode = false;
}
return retCode;
}
2020-11-16 15:40:50 +01:00
const static cmd_t COMMANDS[] = {
2020-11-16 15:51:00 +01:00
{ .requiredConfigMode = true, .name = "clear", .cmdFunc = clearCmd,
.help = \
2020-11-16 15:40:50 +01:00
"clear ................................ Clears the global Meterbus\n\r" \
" statistics\n\r" \
2020-11-16 15:51:00 +01:00
" Required configuration mode\n\r"
},
2020-11-16 17:53:18 +01:00
{ .requiredConfigMode = true, .name = "mbusCommEnable", .cmdFunc = mbusCommEnableCmd,
.help = \
"mbusCommEnable true|false ............ Enables or disables the Meterbus\n\r" \
" communication\n\r"
},
{ .requiredConfigMode = true, .name = "loopEnable", .cmdFunc = loopEnableCmd,
.help = \
"loopEnable true|false ................ Enables or disables the loop.\n\r" \
" Disable Meterbus communication\n\r" \
" first if you want to disable the\n\r" \
" for a longer time, otherwise the\n\r" \
" request will enable it again\n\r"
},
2020-11-16 15:51:00 +01:00
{ .requiredConfigMode = false, .name = "globalStats", .cmdFunc = globalStatsCmd,
.help = \
"globalStats .......................... Show the global statistics\n\r" \
" counters requestCnt and errorCnt\n\r"
2020-11-16 15:40:50 +01:00
},
2020-11-16 15:51:00 +01:00
{ .requiredConfigMode = false, .name = "END_OF_CMDS", .help = "",.cmdFunc = NULL }
2020-11-16 15:40:50 +01:00
};
2020-11-16 14:07:29 +01:00
// returns 0 to continue waiting for input
// returns -1 to close the connection
2020-11-16 14:42:23 +01:00
// returns 1 to toggle to config mode
// returns 2 to toggle back to default mode
2020-11-16 15:56:22 +01:00
int8_t cmdExecuteCommand(uint8_t *cmdLine, bool resetConfigMode) {
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" \
"enable ............................... Enable configuration mode\n\r" \
"disable .............................. Disable configuration mode\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";
const static char REQUIRES_CONFIG_MODE_MGS[] = "Not executed, requires config mode\n\r";
2020-11-16 14:56:05 +01:00
uint8_t *messageToSend = NULL;
2020-11-16 14:42:23 +01:00
static bool configMode = false;
if (resetConfigMode) {
configMode = false;
}
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-11-20 11:23:44 +01:00
send(CMD_SOCK, (uint8_t*)HELP_MSG, strlen(HELP_MSG));
2020-11-16 15:40:50 +01:00
uint8_t cmdIdx = 0;
while (true) {
cmd_t command = COMMANDS[cmdIdx];
if (0 == strcmp("END_OF_CMDS", command.name)) {
break;
}
2020-11-20 11:23:44 +01:00
send(CMD_SOCK, (uint8_t*)command.help, strlen(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-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, true, "ch cec enable config mode");
2020-11-16 14:42:23 +01:00
configMode = true;
retCode = 1;
} else if (0 == strcmp(cmd, "disable")) {
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, true, "ch cec disable config mode");
2020-11-16 14:42:23 +01:00
configMode = false;
retCode = 2;
2020-11-16 15:40:50 +01:00
} 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) {
2020-11-20 11:23:44 +01:00
messageToSend = (uint8_t*)REQUIRES_CONFIG_MODE_MGS;
2020-11-16 15:40:50 +01:00
} else {
2020-11-20 11:23:44 +01:00
messageToSend = command.cmdFunc(argc, args) ? (uint8_t*)OK_MSG : (uint8_t*)FAILED_MSG;
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-11-16 13:23:58 +01:00
void cmdHandlerEngine(void *handle) {
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-11-16 14:42:23 +01:00
static bool resetConfigMode = 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;
static char defaultPrompt[] = "MBGW3 # ";
static char elevatedPrompt[] = "MBGW3 (admin) > ";
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);
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-11-16 14:42:23 +01:00
resetConfigMode = 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-11-20 11:23:44 +01:00
resultSend = send(CMD_SOCK, (uint8_t*)prompt, strlen(prompt));
2020-11-17 12:40:24 +01:00
coloredMsg(LOG_YELLOW, false, "ch che sent prompt %s, send returns 0x%02x", prompt, resultSend);
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-11-16 14:42:23 +01:00
int8_t resCEC = cmdExecuteCommand(receiveBuffer, resetConfigMode);
resetConfigMode = false;
switch (resCEC) {
case 0:
state = CH_PROMPT;
break;
case -1:
state = CH_DISCONNECT;
break;
case 1:
prompt = elevatedPrompt;
state = CH_PROMPT;
break;
case 2:
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-11-17 12:40:24 +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-11-17 12:40:24 +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
}