cmd handler stuff

This commit is contained in:
2020-11-16 13:23:58 +01:00
parent 961e5a73aa
commit bd09d4db45

View File

@ -14,27 +14,35 @@
extern const uint8_t CMD_SOCK; extern const uint8_t CMD_SOCK;
uint8_t remoteAddr[] = { 172, 16, 3, 31 }; const uint16_t cmdPort = 2000;
uint16_t remotePort = 5000;
void cmdHandlerEngine(void *handle) {
static uint8_t receiveBuffer[256];
void cmdHandler(void *handle) {
static uint8_t state = 0; static uint8_t state = 0;
int8_t res = 0;
int16_t res16 = 0;
int32_t res32 = 0;
static uint8_t message[] = "Hello world\n\r"; 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;
if (isNetworkAvailable()) { if (isNetworkAvailable()) {
switch (state) { switch (state) {
case 0: case 0:
coloredMsg(LOG_YELLOW, "tth, initializing socket"); coloredMsg(LOG_YELLOW, "che, initializing socket");
res = socket(CMD_SOCK, Sn_MR_TCP, 12345, SF_IO_NONBLOCK); res = socket(CMD_SOCK, Sn_MR_TCP, cmdPort, SF_IO_NONBLOCK);
coloredMsg(LOG_YELLOW, "tth, socket returns %d", res); coloredMsg(LOG_YELLOW, "che, socket returns %d", res);
if (res == CMD_SOCK) { if (res == CMD_SOCK) {
coloredMsg(LOG_YELLOW, "tth, socket is initialized"); coloredMsg(LOG_YELLOW, "che, socket is initialized");
state = 1; state = 1;
} else { } else {
state = 255; state = 255;
@ -42,13 +50,13 @@ void cmdHandler(void *handle) {
break; break;
case 1: case 1:
coloredMsg(LOG_YELLOW, "tth, connecting"); coloredMsg(LOG_YELLOW, "che, listening");
res = connect(CMD_SOCK, remoteAddr, remotePort); res = listen(CMD_SOCK);
coloredMsg(LOG_YELLOW, "tth, connect returns %d", res); coloredMsg(LOG_YELLOW, "che, listen returns %d", res);
if (res == SOCK_BUSY) { if (res == SOCK_BUSY) {
coloredMsg(LOG_YELLOW, "tth, ok, waiting for established"); coloredMsg(LOG_YELLOW, "che, ok, waiting for established");
state = 2; state = 2;
} else { } else {
state = 255; state = 255;
@ -56,54 +64,52 @@ void cmdHandler(void *handle) {
break; break;
case 2: case 2:
coloredMsg(LOG_YELLOW, "tth, waiting for established"); coloredMsg(LOG_YELLOW, "che, waiting for established");
uint8_t sockState = getSn_SR(CMD_SOCK); sockState = getSn_SR(CMD_SOCK);
coloredMsg(LOG_YELLOW, "tth, socket state is 0x%02x", sockState); coloredMsg(LOG_YELLOW, "che, socket state is 0x%02x", sockState);
if (sockState == SOCK_ESTABLISHED) { if (sockState == SOCK_ESTABLISHED) {
coloredMsg(LOG_YELLOW, "tth, connection is established"); coloredMsg(LOG_YELLOW, "che, connection is established");
state = 3; state = 3;
} }
break; break;
case 3: case 3:
coloredMsg(LOG_YELLOW, "tth, now sending something"); coloredMsg(LOG_YELLOW, "che, send banner");
res32 = send(CMD_SOCK, message, strlen(message)); resultSend = send(CMD_SOCK, banner, strlen(banner));
coloredMsg(LOG_YELLOW, "tth, sent a message, send returns 0x%02x", res32); coloredMsg(LOG_YELLOW, "che, sent banner, send returns 0x%02x", resultSend);
state = 4; state = 4;
break; break;
case 4: case 4:
// coloredMsg(LOG_YELLOW, "tth, now waiting for some input"); // coloredMsg(LOG_YELLOW, "che, now waiting for some input");
res16 = getSn_RX_RSR(CMD_SOCK); receivedOctets = getSn_RX_RSR(CMD_SOCK);
// coloredMsg(LOG_YELLOW, "tth, getSn_RxMAX returns %d", res16); // coloredMsg(LOG_YELLOW, "che, getSn_RxMAX returns %d", res16);
if (res16 > 0) { if (receivedOctets > 0) {
uint8_t *buf = (uint8_t*) malloc(res16); memset(receiveBuffer, 0, sizeof(receiveBuffer));
memset(buf, 0, res16); resultRecv = recv(CMD_SOCK, receiveBuffer, sizeof(receiveBuffer));
res32 = recv(CMD_SOCK, buf, res16); coloredMsg(LOG_YELLOW, "che, recv returns 0x%02x", resultRecv);
coloredMsg(LOG_YELLOW, "tth, recv returns 0x%02x", res32); if (resultRecv > 0) {
if (res32 > 0) { coloredMsg(LOG_YELLOW, "che, received: %s", receiveBuffer);
coloredMsg(LOG_YELLOW, "tth, received: %d, %s", res32, buf);
} }
free(buf);
} }
break; break;
case 255: case 255:
coloredMsg(LOG_YELLOW, "tth, error state, will stop here"); coloredMsg(LOG_YELLOW, "che, error state, will stop here");
schDel(cmdHandler, NULL); schDel(cmdHandlerEngine, NULL);
break; break;
} }
} else { } else {
coloredMsg(LOG_YELLOW, "tth, network not yet ready"); coloredMsg(LOG_YELLOW, "che, network not yet ready");
} }
} }
void cmdHandlerInit() { void cmdHandlerInit() {
schAdd(cmdHandler, NULL, 0, 100); schAdd(cmdHandlerEngine, NULL, 0, 100);
} }