forked from xuos/xiuos
fixed the bug of lwip IRQ and optimize TCP and OPCUA demo
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#ifndef __LWIP_DEMO_H__
|
||||
#define __LWIP_DEMO_H__
|
||||
|
||||
void *eth_input_thread(void *param);
|
||||
#define LWIP_TEST_STACK_SIZE 4096
|
||||
#define LWIP_TEST_TASK_PRIO 20
|
||||
|
||||
#endif /* __LWIP_DEMO_H__ */
|
||||
|
||||
@@ -29,6 +29,8 @@
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
|
||||
#define MSG_SIZE 128
|
||||
|
||||
/*******************************************************************************
|
||||
* Prototypes
|
||||
******************************************************************************/
|
||||
@@ -37,10 +39,9 @@
|
||||
* Variables
|
||||
******************************************************************************/
|
||||
|
||||
char tcp_target[] = {192, 168, 250, 252};
|
||||
#define MSG_SIZE 128
|
||||
// this is for test in shell, in fact, shell restrict the length of input string, which is less then 128
|
||||
char tcp_send_msg[MSG_SIZE] = {0};
|
||||
char tcp_target[] = {192, 168, 250, 252};
|
||||
|
||||
/*******************************************************************************
|
||||
* Code
|
||||
@@ -60,7 +61,7 @@ static void lwip_tcp_send_thread(void *arg)
|
||||
|
||||
struct sockaddr_in tcp_sock;
|
||||
tcp_sock.sin_family = AF_INET;
|
||||
tcp_sock.sin_port = htons(TARGET_PORT_CLIENT);
|
||||
tcp_sock.sin_port = htons(LWIP_TARGET_PORT);
|
||||
tcp_sock.sin_addr.s_addr = PP_HTONL(LWIP_MAKEU32(tcp_target[0], tcp_target[1], tcp_target[2], tcp_target[3]));
|
||||
memset(&(tcp_sock.sin_zero), 0, sizeof(tcp_sock.sin_zero));
|
||||
|
||||
@@ -104,7 +105,7 @@ void lwip_tcp_send_run(int argc, char *argv[])
|
||||
|
||||
ETH_BSP_Config();
|
||||
lwip_config_tcp(lwip_ipaddr, lwip_netmask, lwip_gwaddr);
|
||||
sys_thread_new("tcp send", lwip_tcp_send_thread, NULL, 4096, 25);
|
||||
sys_thread_new("tcp send", lwip_tcp_send_thread, NULL, LWIP_TASK_STACK_SIZE, LWIP_TASK_PRIO);
|
||||
}
|
||||
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
|
||||
|
||||
@@ -53,7 +53,7 @@ char udp_send_msg[] = "\n\nThis one is UDP pkg. Congratulations on you.\n\n";
|
||||
|
||||
static void lwip_udp_send(void *arg)
|
||||
{
|
||||
int cnt = TEST_LWIP_TIMES;
|
||||
int cnt = LWIP_DEMO_TIMES;
|
||||
|
||||
lw_print("udp_send_demo start.\n");
|
||||
|
||||
@@ -67,7 +67,7 @@ static void lwip_udp_send(void *arg)
|
||||
|
||||
struct sockaddr_in udp_sock;
|
||||
udp_sock.sin_family = AF_INET;
|
||||
udp_sock.sin_port = htons(TARGET_PORT_CLIENT);
|
||||
udp_sock.sin_port = htons(LWIP_TARGET_PORT);
|
||||
udp_sock.sin_addr.s_addr = PP_HTONL(LWIP_MAKEU32(udp_target[0],udp_target[1],udp_target[2],udp_target[3]));
|
||||
memset(&(udp_sock.sin_zero), 0, sizeof(udp_sock.sin_zero));
|
||||
|
||||
@@ -170,7 +170,7 @@ void udpecho_raw_init(void)
|
||||
return;
|
||||
}
|
||||
|
||||
err = udp_bind(udpecho_raw_pcb, IP_ANY_TYPE, LOCAL_PORT_SERVER);
|
||||
err = udp_bind(udpecho_raw_pcb, IP_ANY_TYPE, LWIP_LOCAL_PORT);
|
||||
if (err == ERR_OK)
|
||||
{
|
||||
udp_recv(udpecho_raw_pcb, udpecho_raw_recv, NULL);
|
||||
|
||||
@@ -248,7 +248,7 @@ ping_thread(void *arg)
|
||||
{
|
||||
int s;
|
||||
int ret;
|
||||
int cnt = TEST_LWIP_TIMES;
|
||||
int cnt = LWIP_DEMO_TIMES;
|
||||
|
||||
#if LWIP_SO_SNDRCVTIMEO_NONSTANDARD
|
||||
int timeout = PING_RCV_TIMEO;
|
||||
@@ -367,7 +367,7 @@ ping_send(struct raw_pcb *raw, const ip_addr_t *addr)
|
||||
static void
|
||||
ping_timeout(void *arg)
|
||||
{
|
||||
static int cnt = TEST_LWIP_TIMES;
|
||||
static int cnt = LWIP_DEMO_TIMES;
|
||||
struct raw_pcb *pcb = (struct raw_pcb*)arg;
|
||||
|
||||
LWIP_ASSERT("ping_timeout: no pcb given!", pcb != NULL);
|
||||
@@ -380,7 +380,7 @@ ping_timeout(void *arg)
|
||||
}
|
||||
else
|
||||
{
|
||||
cnt = TEST_LWIP_TIMES;
|
||||
cnt = LWIP_DEMO_TIMES;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -483,7 +483,7 @@ int get_url_ip(char* url)
|
||||
#else
|
||||
int timeout = PING_RCV_TIMEO * 1000UL / TICK_PER_SECOND;
|
||||
#endif
|
||||
int cnt = TEST_LWIP_TIMES;
|
||||
int cnt = LWIP_DEMO_TIMES;
|
||||
|
||||
int s, ttl, recv_len;
|
||||
ip_addr_t target_addr;
|
||||
|
||||
@@ -49,7 +49,10 @@
|
||||
|
||||
#if LWIP_TCP && LWIP_CALLBACK_API
|
||||
|
||||
#define MAX_TESTED_TCP_SEND_SIZE (56000)
|
||||
#define MAX_TCP_RECV_SIZE (56000)
|
||||
#define MAX_TCP_SHOW_SIZE 80
|
||||
#define TCP_ACK_MSG_SIZE 20
|
||||
#define TCP_EOF_CH '\n'
|
||||
|
||||
static struct tcp_pcb *tcpecho_raw_pcb;
|
||||
|
||||
@@ -109,70 +112,73 @@ tcpecho_raw_error(void *arg, err_t err)
|
||||
tcpecho_raw_free(es);
|
||||
}
|
||||
|
||||
#define SHELL_TCP_LENGTH 80
|
||||
char* recved_msg;
|
||||
int recved_msg_length;
|
||||
static void
|
||||
tcpecho_raw_ack_size(struct tcp_pcb *tpcb, int ack_len)
|
||||
{
|
||||
struct pbuf *ack_buf = NULL;
|
||||
|
||||
// ack message
|
||||
ack_buf = pbuf_alloc(PBUF_TRANSPORT, TCP_ACK_MSG_SIZE, PBUF_RAM);
|
||||
snprintf(ack_buf->payload, TCP_ACK_MSG_SIZE, "%d\n\0", ack_len);
|
||||
ack_buf->len = strlen(ack_buf->payload);
|
||||
ack_buf->tot_len = strlen(ack_buf->payload);
|
||||
ack_buf->next = NULL;
|
||||
|
||||
tcp_write(tpcb, ack_buf->payload, ack_buf->len, 1);
|
||||
pbuf_free(ack_buf);
|
||||
}
|
||||
|
||||
// compute received message length until '\n'
|
||||
static void record_msg(struct tcp_pcb *tpcb, struct tcpecho_raw_state* es){
|
||||
struct pbuf* ptr = es->p;
|
||||
int plen = ptr->len;
|
||||
if(es==NULL||ptr==NULL||plen==0){
|
||||
if(recved_msg){
|
||||
free(recved_msg);
|
||||
recved_msg = NULL;
|
||||
}
|
||||
recved_msg_length = 0;
|
||||
static void
|
||||
tcpecho_raw_ack(struct tcp_pcb *tpcb, struct tcpecho_raw_state* es){
|
||||
struct pbuf *ptr = es->p;
|
||||
char *recv_buf = ptr->payload;
|
||||
int recv_len = ptr->len;
|
||||
static char *g_buf = NULL; //global received buffer
|
||||
static int g_buf_size = 0;
|
||||
|
||||
lw_print("lw: [%s] recv %d tot %d next %p ref %d tye %d id %d %s\n", __func__, ptr->len, ptr->tot_len,
|
||||
ptr->next, ptr->ref, ptr->type_internal,
|
||||
ptr->if_idx, ptr->payload);
|
||||
|
||||
if(g_buf == NULL)
|
||||
{
|
||||
g_buf = (char *)malloc(MAX_TCP_RECV_SIZE);
|
||||
memset(g_buf, 0, MAX_TCP_RECV_SIZE);
|
||||
memcpy(g_buf, recv_buf, recv_len);
|
||||
}
|
||||
int new_size = plen+1;
|
||||
char* temp = recved_msg;
|
||||
if(temp!=NULL){
|
||||
new_size += strlen(temp);
|
||||
}
|
||||
recved_msg = malloc(new_size);
|
||||
memset(recved_msg, 0, new_size);
|
||||
if(temp!=NULL){
|
||||
memcpy(recved_msg,temp,strlen(temp));
|
||||
}
|
||||
memcpy(recved_msg+new_size-plen-1, ptr->payload, plen);
|
||||
free(temp);
|
||||
recved_msg_length += plen;
|
||||
if(recved_msg_length>=SHELL_TCP_LENGTH){
|
||||
if(recved_msg){
|
||||
free(recved_msg);
|
||||
recved_msg = NULL;
|
||||
else
|
||||
{
|
||||
if(g_buf_size + recv_len <= MAX_TCP_RECV_SIZE)
|
||||
{
|
||||
memcpy(g_buf + g_buf_size, recv_buf, recv_len);
|
||||
}
|
||||
}
|
||||
if(((char*)ptr->payload)[plen-1]=='\n'){
|
||||
if(recved_msg_length>MAX_TESTED_TCP_SEND_SIZE){
|
||||
KPrintf("Recved_msg_length is larger than %d, which may lead to unexpected exceptions.\n",MAX_TESTED_TCP_SEND_SIZE);
|
||||
}
|
||||
if(recved_msg_length<SHELL_TCP_LENGTH){
|
||||
KPrintf("Received: %s\n", recved_msg);
|
||||
KPrintf("Received: %s\n", recved_msg);
|
||||
g_buf_size += recv_len;
|
||||
|
||||
if((recv_len != TCP_MSS) || (recv_buf[recv_len - 1] == TCP_EOF_CH))
|
||||
{
|
||||
if(g_buf_size < MAX_TCP_SHOW_SIZE){
|
||||
lw_pr_info("Received: %s\n", g_buf);
|
||||
}else{
|
||||
KPrintf("Received a string of length %d\n", recved_msg_length);
|
||||
lw_pr_info("Received a string of length %d\n", g_buf_size);
|
||||
}
|
||||
struct pbuf* reply_pbuf = pbuf_alloc(PBUF_TRANSPORT, 20, PBUF_RAM); // only reply received message length
|
||||
sprintf(reply_pbuf->payload,"%d\n\0",recved_msg_length);
|
||||
reply_pbuf->len = strlen(reply_pbuf->payload);
|
||||
reply_pbuf->tot_len = strlen(reply_pbuf->payload);
|
||||
reply_pbuf->next = NULL;
|
||||
|
||||
tcp_write(tpcb, reply_pbuf->payload, reply_pbuf->len, 1);
|
||||
|
||||
pbuf_free(reply_pbuf);
|
||||
free(recved_msg);
|
||||
recved_msg = NULL;
|
||||
recved_msg_length = 0;
|
||||
|
||||
tcpecho_raw_ack_size(tpcb, g_buf_size);
|
||||
|
||||
free(g_buf);
|
||||
g_buf = NULL;
|
||||
g_buf_size = 0;
|
||||
}
|
||||
|
||||
es->p = ptr->next;
|
||||
if(es->p != NULL) {
|
||||
/* new reference! */
|
||||
pbuf_ref(es->p);
|
||||
}
|
||||
pbuf_free(ptr);
|
||||
tcp_recved(tpcb, plen);
|
||||
}
|
||||
tcp_recved(tpcb, recv_len);
|
||||
}
|
||||
|
||||
static err_t
|
||||
tcpecho_raw_poll(void *arg, struct tcp_pcb *tpcb)
|
||||
@@ -184,7 +190,7 @@ tcpecho_raw_poll(void *arg, struct tcp_pcb *tpcb)
|
||||
if (es != NULL) {
|
||||
if (es->p != NULL) {
|
||||
/* there is a remaining pbuf (chain) */
|
||||
record_msg(tpcb, es);
|
||||
tcpecho_raw_ack(tpcb, es);
|
||||
} else {
|
||||
/* no remaining pbuf (chain) */
|
||||
if(es->state == ES_CLOSING) {
|
||||
@@ -233,7 +239,7 @@ tcpecho_raw_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
|
||||
tcpecho_raw_close(tpcb, es);
|
||||
} else {
|
||||
/* we're not done yet */
|
||||
record_msg(tpcb, es);
|
||||
tcpecho_raw_ack(tpcb, es);
|
||||
}
|
||||
ret_err = ERR_OK;
|
||||
} else if(err != ERR_OK) {
|
||||
@@ -248,13 +254,13 @@ tcpecho_raw_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
|
||||
es->state = ES_RECEIVED;
|
||||
/* store reference to incoming pbuf (chain) */
|
||||
es->p = p;
|
||||
record_msg(tpcb, es);
|
||||
tcpecho_raw_ack(tpcb, es);
|
||||
ret_err = ERR_OK;
|
||||
} else if (es->state == ES_RECEIVED) {
|
||||
/* read some more data */
|
||||
if(es->p == NULL) {
|
||||
es->p = p;
|
||||
record_msg(tpcb, es);
|
||||
tcpecho_raw_ack(tpcb, es);
|
||||
} else {
|
||||
struct pbuf *ptr;
|
||||
|
||||
@@ -277,8 +283,6 @@ tcpecho_raw_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
|
||||
{
|
||||
err_t ret_err;
|
||||
struct tcpecho_raw_state *es;
|
||||
recved_msg = NULL;
|
||||
recved_msg_length = 0;
|
||||
|
||||
LWIP_UNUSED_ARG(arg);
|
||||
if ((err != ERR_OK) || (newpcb == NULL)) {
|
||||
|
||||
Reference in New Issue
Block a user