256 lines
8.4 KiB
C

#include <cmdHandler.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <socket.h>
#include <logger.h>
#include <PontCoopScheduler.h>
#include <wizHelper.h>
#include <mbusComm.h>
extern const uint8_t CMD_SOCK;
const uint16_t cmdPort = 23;
typedef enum {
CH_INIT,
CH_LISTEN,
CH_WAITING,
CH_BANNER,
CH_PROMPT,
CH_RECEIVE,
CH_DISCONNECT,
CH_DISCONNECT_WAIT,
CH_ERROR
} chState_t;
// clear statistics
bool clearCmd() {
t_mbusCommStats zeroedStats = { .requestCnt = 0, .errorCnt = 0 };
mbusCommSetStats(zeroedStats);
return true;
}
// 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 *cmd, bool resetConfigMode) {
const static uint8_t GOODBYE_MSG[] = "Good bye\n\r";
const static uint8_t OK_MSG[] = "OK\n\r";
const static uint8_t FAILED_MSG[] = "Failed\n\r";
const static uint8_t REQUIRES_CONFIG_MODE_MGS[] = "Not executed, requires config mode\n\r";
uint8_t *messageToSend = NULL;
static bool configMode = false;
if (resetConfigMode) {
configMode = false;
}
int8_t retCode = 0;
coloredMsg(LOG_YELLOW, "cec, cmd is %s", cmd);
if (0 == strcmp(cmd, "quit")) {
messageToSend = GOODBYE_MSG;
retCode = -1;
} else if (0 == strcmp(cmd, "enable")) {
coloredMsg(LOG_YELLOW, "cec, enable config mode");
configMode = true;
retCode = 1;
} else if (0 == strcmp(cmd, "disable")) {
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;
}
}
if (messageToSend) {
send(CMD_SOCK, messageToSend, sizeof(messageToSend));
}
return retCode;
}
void cmdHandlerEngine(void *handle) {
static uint8_t receiveBuffer[256];
static chState_t state = CH_INIT;
static bool resetConfigMode = false;
static uint8_t banner[] = \
"MBGW3\n\r" \
"Type help for usage help\n\r" \
"or quit to close the connection.\n\r";
static uint8_t *prompt;
static uint8_t defaultPrompt[] = "MBGW3 # ";
static uint8_t elevatedPrompt[] = "MBGW3 (admin) > ";
int8_t res = 0;
uint8_t sockState;
int32_t resultSend;
int16_t receivedOctets;
int32_t resultRecv;
uint8_t resultDisconnect;
if (isNetworkAvailable()) {
switch (state) {
case CH_INIT:
coloredMsg(LOG_YELLOW, "che, initializing socket");
res = socket(CMD_SOCK, Sn_MR_TCP, cmdPort, SF_IO_NONBLOCK);
coloredMsg(LOG_YELLOW, "che, socket returns %d", res);
if (res == CMD_SOCK) {
coloredMsg(LOG_YELLOW, "che, socket is initialized");
state = CH_LISTEN;
} else {
state = CH_ERROR;
}
break;
case CH_LISTEN:
coloredMsg(LOG_YELLOW, "che, listening");
res = listen(CMD_SOCK);
coloredMsg(LOG_YELLOW, "che, listen returns %d", res);
if (res == SOCK_OK) {
coloredMsg(LOG_YELLOW, "che, ok, waiting for established");
state = CH_WAITING;
} else {
state = CH_ERROR;
}
break;
case CH_WAITING:
//coloredMsg(LOG_YELLOW, "che, waiting for established");
sockState = getSn_SR(CMD_SOCK);
if (sockState != SOCK_LISTEN) {
coloredMsg(LOG_YELLOW, "che, socket state is 0x%02x", sockState);
}
if (sockState == SOCK_ESTABLISHED) {
coloredMsg(LOG_YELLOW, "che, connection is established");
state = CH_BANNER;
}
break;
case CH_BANNER:
coloredMsg(LOG_YELLOW, "che, send banner");
sockState = getSn_SR(CMD_SOCK);
if (sockState != SOCK_ESTABLISHED) {
coloredMsg(LOG_YELLOW, "che sockState is 0x%02x when trying to send banner", sockState);
state = CH_DISCONNECT;
} else {
resultSend = send(CMD_SOCK, banner, strlen(banner));
coloredMsg(LOG_YELLOW, "che, sent banner, send returns 0x%02x", resultSend);
prompt = defaultPrompt;
resetConfigMode = true;
state = CH_PROMPT;
}
break;
case CH_PROMPT:
coloredMsg(LOG_YELLOW, "che send prompt");
sockState = getSn_SR(CMD_SOCK);
if (sockState != SOCK_ESTABLISHED) {
coloredMsg(LOG_YELLOW, "che sockState is 0x%02x when trying to send promt", sockState);
state = CH_DISCONNECT;
} else {
resultSend = send(CMD_SOCK, prompt, strlen(prompt));
coloredMsg(LOG_YELLOW, "che, sent prompt %s, send returns 0x%02x", prompt, resultSend);
state = CH_RECEIVE;
}
break;
case CH_RECEIVE:
sockState = getSn_SR(CMD_SOCK);
if (sockState != SOCK_ESTABLISHED) {
coloredMsg(LOG_YELLOW, "che sockState is 0x%02x when trying to receive something", sockState);
state = CH_DISCONNECT;
} else {
// coloredMsg(LOG_YELLOW, "che, now waiting for some input");
receivedOctets = getSn_RX_RSR(CMD_SOCK);
// coloredMsg(LOG_YELLOW, "che, getSn_RxMAX returns %d", res16);
if (receivedOctets > 0) {
memset(receiveBuffer, 0, sizeof(receiveBuffer));
resultRecv = recv(CMD_SOCK, receiveBuffer, sizeof(receiveBuffer));
coloredMsg(LOG_YELLOW, "che, recv returns 0x%02x", resultRecv);
if (resultRecv > 0) {
if ((receiveBuffer[strlen(receiveBuffer) - 1] == 0x0a) ||
(receiveBuffer[strlen(receiveBuffer) - 1] == 0x0d)) {
receiveBuffer[strlen(receiveBuffer) - 1] = 0;
}
if ((receiveBuffer[strlen(receiveBuffer) - 1] == 0x0a) ||
(receiveBuffer[strlen(receiveBuffer) - 1] == 0x0d)) {
receiveBuffer[strlen(receiveBuffer) - 1] = 0;
}
coloredMsg(LOG_YELLOW, "che, received: %s", receiveBuffer);
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;
}
}
}
}
break;
case CH_DISCONNECT:
coloredMsg(LOG_YELLOW, "che, close our end");
resultDisconnect = disconnect(CMD_SOCK);
coloredMsg(LOG_YELLOW, "che, disconnect returns 0x%02x", resultDisconnect);
state = CH_DISCONNECT_WAIT;
break;
case CH_DISCONNECT_WAIT:
coloredMsg(LOG_YELLOW, "che, waiting after disconnect");
sockState = getSn_SR(CMD_SOCK);
coloredMsg(LOG_YELLOW, "che, sockState is 0x%02x", sockState);
if (sockState == SOCK_CLOSED) {
coloredMsg(LOG_YELLOW, "che, socket is closed now");
state = CH_INIT;
}
break;
case CH_ERROR:
coloredMsg(LOG_YELLOW, "che, error state, will stop here");
schDel(cmdHandlerEngine, NULL);
break;
}
}
}
void cmdHandlerInit() {
schAdd(cmdHandlerEngine, NULL, 0, 100);
}