165 lines
5.0 KiB
C
165 lines
5.0 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>
|
|
|
|
|
|
extern const uint8_t CMD_SOCK;
|
|
|
|
const uint16_t cmdPort = 2000;
|
|
|
|
|
|
// returns 0 to continue waiting for input
|
|
// returns -1 to close the connection
|
|
int8_t cmdExecuteCommand(uint8_t *cmd) {
|
|
const static uint8_t GOODBYE_MSG[] = "Good bye\n\r";
|
|
|
|
int8_t retCode = 0;
|
|
coloredMsg(LOG_YELLOW, "cec, cmd is %s", cmd);
|
|
if (0 == strcmp(cmd, "quit")) {
|
|
send(CMD_SOCK, GOODBYE_MSG, sizeof(GOODBYE_MSG));
|
|
retCode = -1;
|
|
}
|
|
|
|
return retCode;
|
|
}
|
|
|
|
void cmdHandlerEngine(void *handle) {
|
|
static uint8_t receiveBuffer[256];
|
|
|
|
static uint8_t state = 0;
|
|
|
|
static uint8_t banner[] = \
|
|
"MBGW3\n\r" \
|
|
"Type help for usage help\n\r" \
|
|
"or quit to close the connection.\n\r";
|
|
|
|
|
|
int8_t res = 0;
|
|
uint8_t sockState;
|
|
int32_t resultSend;
|
|
int16_t receivedOctets;
|
|
int32_t resultRecv;
|
|
uint8_t resultDisconnect;
|
|
|
|
if (isNetworkAvailable()) {
|
|
switch (state) {
|
|
case 0:
|
|
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 = 1;
|
|
} else {
|
|
state = 255;
|
|
}
|
|
break;
|
|
|
|
case 1:
|
|
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 = 2;
|
|
} else {
|
|
state = 255;
|
|
}
|
|
break;
|
|
|
|
case 2:
|
|
//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 = 3;
|
|
}
|
|
break;
|
|
|
|
case 3:
|
|
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;
|
|
} else {
|
|
resultSend = send(CMD_SOCK, banner, strlen(banner));
|
|
coloredMsg(LOG_YELLOW, "che, sent banner, send returns 0x%02x", resultSend);
|
|
state = 4;
|
|
}
|
|
break;
|
|
|
|
case 4:
|
|
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;
|
|
} 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] = 0;
|
|
}
|
|
coloredMsg(LOG_YELLOW, "che, received: %s", receiveBuffer);
|
|
if (-1 == cmdExecuteCommand(receiveBuffer)) {
|
|
state = 5;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
|
|
case 5:
|
|
coloredMsg(LOG_YELLOW, "che, close our end");
|
|
resultDisconnect = disconnect(CMD_SOCK);
|
|
coloredMsg(LOG_YELLOW, "che, disconnect returns 0x%02x", resultDisconnect);
|
|
state = 6;
|
|
break;
|
|
|
|
case 6:
|
|
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;
|
|
}
|
|
break;
|
|
|
|
|
|
case 255:
|
|
coloredMsg(LOG_YELLOW, "che, error state, will stop here");
|
|
schDel(cmdHandlerEngine, NULL);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void cmdHandlerInit() {
|
|
schAdd(cmdHandlerEngine, NULL, 0, 100);
|
|
} |