From 1dbcd966fcd79f3a11924d6cdff2adf14ffd4f60 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Mon, 16 Nov 2020 14:33:54 +0100 Subject: [PATCH] cmd handler stuff --- cube/User/Src/cmdHandler.c | 73 +++++++++++++++++++++++++++----------- 1 file changed, 52 insertions(+), 21 deletions(-) diff --git a/cube/User/Src/cmdHandler.c b/cube/User/Src/cmdHandler.c index b33a8d6..dc98432 100644 --- a/cube/User/Src/cmdHandler.c +++ b/cube/User/Src/cmdHandler.c @@ -16,6 +16,17 @@ 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; // returns 0 to continue waiting for input // returns -1 to close the connection @@ -35,13 +46,17 @@ int8_t cmdExecuteCommand(uint8_t *cmd) { void cmdHandlerEngine(void *handle) { static uint8_t receiveBuffer[256]; - static uint8_t state = 0; + static chState_t state = CH_INIT; 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; @@ -52,7 +67,7 @@ void cmdHandlerEngine(void *handle) { if (isNetworkAvailable()) { switch (state) { - case 0: + case CH_INIT: coloredMsg(LOG_YELLOW, "che, initializing socket"); res = socket(CMD_SOCK, Sn_MR_TCP, cmdPort, SF_IO_NONBLOCK); @@ -60,13 +75,13 @@ void cmdHandlerEngine(void *handle) { if (res == CMD_SOCK) { coloredMsg(LOG_YELLOW, "che, socket is initialized"); - state = 1; + state = CH_LISTEN; } else { - state = 255; + state = CH_ERROR; } break; - case 1: + case CH_LISTEN: coloredMsg(LOG_YELLOW, "che, listening"); res = listen(CMD_SOCK); @@ -74,13 +89,13 @@ void cmdHandlerEngine(void *handle) { if (res == SOCK_OK) { coloredMsg(LOG_YELLOW, "che, ok, waiting for established"); - state = 2; + state = CH_WAITING; } else { - state = 255; + state = CH_ERROR; } break; - case 2: + case CH_WAITING: //coloredMsg(LOG_YELLOW, "che, waiting for established"); sockState = getSn_SR(CMD_SOCK); @@ -90,28 +105,42 @@ void cmdHandlerEngine(void *handle) { if (sockState == SOCK_ESTABLISHED) { coloredMsg(LOG_YELLOW, "che, connection is established"); - state = 3; + state = CH_BANNER; } break; - case 3: + 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 = 5; + state = CH_DISCONNECT; } else { resultSend = send(CMD_SOCK, banner, strlen(banner)); coloredMsg(LOG_YELLOW, "che, sent banner, send returns 0x%02x", resultSend); - state = 4; + prompt = defaultPrompt; + state = CH_PROMPT; } break; - case 4: + 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 = 5; + state = CH_DISCONNECT; } else { // coloredMsg(LOG_YELLOW, "che, now waiting for some input"); receivedOctets = getSn_RX_RSR(CMD_SOCK); @@ -132,32 +161,34 @@ void cmdHandlerEngine(void *handle) { } coloredMsg(LOG_YELLOW, "che, received: %s", receiveBuffer); if (-1 == cmdExecuteCommand(receiveBuffer)) { - state = 5; + state = CH_DISCONNECT; + } else { + state = CH_PROMPT; } } } } break; - case 5: + case CH_DISCONNECT: coloredMsg(LOG_YELLOW, "che, close our end"); resultDisconnect = disconnect(CMD_SOCK); coloredMsg(LOG_YELLOW, "che, disconnect returns 0x%02x", resultDisconnect); - state = 6; + state = CH_DISCONNECT_WAIT; break; - case 6: + 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 = 0; + state = CH_INIT; } break; - case 255: + case CH_ERROR: coloredMsg(LOG_YELLOW, "che, error state, will stop here"); schDel(cmdHandlerEngine, NULL); break;