98 lines
2.6 KiB
C
Raw Normal View History

2020-11-11 14:19:02 +01:00
#include <tcpTest.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <socket.h>
#include <logger.h>
#include <PontCoopScheduler.h>
2020-11-11 14:26:28 +01:00
#include <wizHelper.h>
2020-11-11 14:19:02 +01:00
const uint8_t TCPTEST_SOCK;
2020-11-11 14:45:35 +01:00
uint8_t remoteAddr[] = { 172, 16, 3, 31 };
2020-11-11 14:45:59 +01:00
uint16_t remotePort = 5000;
2020-11-11 14:19:02 +01:00
void tcpTestHandler(void *handle) {
2020-11-11 14:45:35 +01:00
static uint8_t state = 0;
int8_t res = 0;
2020-11-11 15:01:03 +01:00
int32_t res32 = 0;
2020-11-11 14:45:35 +01:00
2020-11-11 15:01:03 +01:00
static uint8_t message[] = "Hello world\n\r";
2020-11-11 14:26:28 +01:00
if (isNetworkAvailable()) {
2020-11-11 14:45:35 +01:00
switch (state) {
case 0:
2020-11-11 14:26:28 +01:00
coloredMsg(LOG_YELLOW, "tth, initializing socket");
2020-11-11 14:45:35 +01:00
res = socket(TCPTEST_SOCK, Sn_MR_TCP, 12345, SF_IO_NONBLOCK);
2020-11-11 14:26:28 +01:00
coloredMsg(LOG_YELLOW, "tth, socket returns %d", res);
if (res == TCPTEST_SOCK) {
coloredMsg(LOG_YELLOW, "tth, socket is initialized");
2020-11-11 14:45:35 +01:00
state = 1;
2020-11-11 14:50:48 +01:00
} else {
state = 255;
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-11 14:45:35 +01:00
case 1:
coloredMsg(LOG_YELLOW, "tth, connecting");
res = connect(TCPTEST_SOCK, remoteAddr, remotePort);
coloredMsg(LOG_YELLOW, "tth, connect returns %d", res);
2020-11-11 14:50:48 +01:00
if (res == SOCK_BUSY) {
coloredMsg(LOG_YELLOW, "tth, ok, waiting for established");
state = 2;
} else {
state = 255;
}
2020-11-11 14:45:35 +01:00
break;
2020-11-11 14:50:48 +01:00
case 2:
coloredMsg(LOG_YELLOW, "tth, waiting for established");
uint8_t sockState = getSn_SR(TCPTEST_SOCK);
2020-11-11 14:53:19 +01:00
coloredMsg(LOG_YELLOW, "tth, socket state is 0x%02x", sockState);
2020-11-11 14:50:48 +01:00
if (sockState == SOCK_ESTABLISHED) {
coloredMsg(LOG_YELLOW, "tth, connection is established");
2020-11-11 15:03:01 +01:00
state = 3;
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
case 3:
coloredMsg(LOG_YELLOW, "tth, now sending something");
2020-11-11 15:01:29 +01:00
res32 = send(TCPTEST_SOCK, message, strlen(message));
2020-11-11 15:01:03 +01:00
coloredMsg(LOG_YELLOW, "tth, sent a message, send returns 0x%02x", res32);
2020-11-11 18:26:54 +01:00
state = 4;
break;
case 4:
coloredMsg(LOG_YELLOW, "tth, now waiting for some input");
uint16_t s = getSn_RxMAX(TCPTEST_SOCK);
coloredMsg(LOG_YELLOW, "tth, getSn_RxMAX returns %d", s);
2020-11-11 15:01:03 +01:00
state = 255;
2020-11-11 18:26:54 +01:00
break;
2020-11-11 14:45:35 +01:00
case 255:
coloredMsg(LOG_YELLOW, "tth, error state, will stop here");
schDel(tcpTestHandler, NULL);
break;
2020-11-11 14:26:28 +01:00
}
} else {
2020-11-11 14:26:56 +01:00
coloredMsg(LOG_YELLOW, "tth, network not yet ready");
2020-11-11 14:26:28 +01:00
}
2020-11-11 14:19:02 +01:00
}
void tcpTestInit() {
2020-11-11 14:26:28 +01:00
schAdd(tcpTestHandler, NULL, 0, 100);
2020-11-11 14:19:02 +01:00
}