cmdHandler started
This commit is contained in:
109
cube/User/Src/cmdHandler.c
Normal file
109
cube/User/Src/cmdHandler.c
Normal file
@ -0,0 +1,109 @@
|
||||
#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;
|
||||
|
||||
uint8_t remoteAddr[] = { 172, 16, 3, 31 };
|
||||
uint16_t remotePort = 5000;
|
||||
|
||||
void cmdHandler(void *handle) {
|
||||
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";
|
||||
|
||||
if (isNetworkAvailable()) {
|
||||
switch (state) {
|
||||
case 0:
|
||||
coloredMsg(LOG_YELLOW, "tth, initializing socket");
|
||||
|
||||
res = socket(CMD_SOCK, Sn_MR_TCP, 12345, SF_IO_NONBLOCK);
|
||||
coloredMsg(LOG_YELLOW, "tth, socket returns %d", res);
|
||||
|
||||
if (res == CMD_SOCK) {
|
||||
coloredMsg(LOG_YELLOW, "tth, socket is initialized");
|
||||
state = 1;
|
||||
} else {
|
||||
state = 255;
|
||||
}
|
||||
break;
|
||||
|
||||
case 1:
|
||||
coloredMsg(LOG_YELLOW, "tth, connecting");
|
||||
|
||||
res = connect(CMD_SOCK, remoteAddr, remotePort);
|
||||
coloredMsg(LOG_YELLOW, "tth, connect returns %d", res);
|
||||
|
||||
if (res == SOCK_BUSY) {
|
||||
coloredMsg(LOG_YELLOW, "tth, ok, waiting for established");
|
||||
state = 2;
|
||||
} else {
|
||||
state = 255;
|
||||
}
|
||||
break;
|
||||
|
||||
case 2:
|
||||
coloredMsg(LOG_YELLOW, "tth, waiting for established");
|
||||
|
||||
uint8_t sockState = getSn_SR(CMD_SOCK);
|
||||
coloredMsg(LOG_YELLOW, "tth, socket state is 0x%02x", sockState);
|
||||
|
||||
if (sockState == SOCK_ESTABLISHED) {
|
||||
coloredMsg(LOG_YELLOW, "tth, connection is established");
|
||||
state = 3;
|
||||
}
|
||||
break;
|
||||
|
||||
case 3:
|
||||
coloredMsg(LOG_YELLOW, "tth, now sending something");
|
||||
|
||||
res32 = send(CMD_SOCK, message, strlen(message));
|
||||
coloredMsg(LOG_YELLOW, "tth, sent a message, send returns 0x%02x", res32);
|
||||
|
||||
state = 4;
|
||||
break;
|
||||
|
||||
case 4:
|
||||
// coloredMsg(LOG_YELLOW, "tth, now waiting for some input");
|
||||
res16 = getSn_RX_RSR(CMD_SOCK);
|
||||
// coloredMsg(LOG_YELLOW, "tth, getSn_RxMAX returns %d", res16);
|
||||
|
||||
if (res16 > 0) {
|
||||
uint8_t *buf = (uint8_t*) malloc(res16);
|
||||
memset(buf, 0, res16);
|
||||
res32 = recv(CMD_SOCK, buf, res16);
|
||||
coloredMsg(LOG_YELLOW, "tth, recv returns 0x%02x", res32);
|
||||
if (res32 > 0) {
|
||||
coloredMsg(LOG_YELLOW, "tth, received: %d, %s", res32, buf);
|
||||
}
|
||||
free(buf);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 255:
|
||||
coloredMsg(LOG_YELLOW, "tth, error state, will stop here");
|
||||
schDel(cmdHandler, NULL);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
coloredMsg(LOG_YELLOW, "tth, network not yet ready");
|
||||
}
|
||||
}
|
||||
|
||||
void cmdHandlerInit() {
|
||||
schAdd(cmdHandler, NULL, 0, 100);
|
||||
}
|
Reference in New Issue
Block a user