forked from xuos/xiuos
optimize lwip test
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
SRC_FILES := ping.c lwip_ping_demo.c udp_echo.c lwip_udp_demo.c lwip_tcp_demo.c tcpecho_raw.c lwip_config_demo.c lwip_dhcp_demo.c
|
||||
SRC_FILES := ping.c lwip_ping_demo.c lwip_udp_demo.c lwip_tcp_demo.c tcpecho_raw.c lwip_config_demo.c lwip_dhcp_demo.c
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
|
||||
@@ -38,7 +38,9 @@
|
||||
******************************************************************************/
|
||||
|
||||
char tcp_target[] = {192, 168, 250, 252};
|
||||
char* tcp_send_msg = "\n\nThis one is TCP pkg. Congratulations on you.\n\n";
|
||||
#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};
|
||||
|
||||
/*******************************************************************************
|
||||
* Code
|
||||
@@ -46,78 +48,69 @@ char* tcp_send_msg = "\n\nThis one is TCP pkg. Congratulations on you.\n\n";
|
||||
|
||||
static void lwip_tcp_send_thread(void *arg)
|
||||
{
|
||||
int cnt = TEST_LWIP_TIMES;
|
||||
lw_print("lwip_tcp_send_thread start.\n");
|
||||
lw_print("lwip_tcp_send_thread start.\n");
|
||||
|
||||
int sock_tcp_send_once = -1;
|
||||
sock_tcp_send_once = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sock_tcp_send_once < 0)
|
||||
{
|
||||
lw_print("Socket error\n");
|
||||
goto __exit;
|
||||
}
|
||||
int fd = -1;
|
||||
fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (fd < 0)
|
||||
{
|
||||
lw_print("Socket error\n");
|
||||
return;
|
||||
}
|
||||
|
||||
struct sockaddr_in tcp_sock;
|
||||
tcp_sock.sin_family = AF_INET;
|
||||
tcp_sock.sin_port = htons(TARGET_PORT_CLIENT);
|
||||
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));
|
||||
struct sockaddr_in tcp_sock;
|
||||
tcp_sock.sin_family = AF_INET;
|
||||
tcp_sock.sin_port = htons(TARGET_PORT_CLIENT);
|
||||
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));
|
||||
|
||||
if (connect(sock_tcp_send_once, (struct sockaddr *)&tcp_sock, sizeof(struct sockaddr)))
|
||||
{
|
||||
lw_print("Unable to connect\n");
|
||||
goto __exit;
|
||||
}
|
||||
if (connect(fd, (struct sockaddr *)&tcp_sock, sizeof(struct sockaddr)))
|
||||
{
|
||||
lw_print("Unable to connect\n");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
lw_print("tcp connect success, start to send.\n");
|
||||
lw_print("\n\nTarget Port:%d\n\n", tcp_sock.sin_port);
|
||||
lw_print("tcp connect success, start to send.\n");
|
||||
lw_print("\n\nTarget Port:%d\n\n", tcp_sock.sin_port);
|
||||
|
||||
while (cnt --)
|
||||
{
|
||||
lw_print("Lwip client is running.\n");
|
||||
|
||||
sendto(sock_tcp_send_once,tcp_send_msg,
|
||||
strlen(tcp_send_msg),0,
|
||||
(struct sockaddr*)&tcp_sock,
|
||||
sizeof(struct sockaddr));
|
||||
sendto(fd, tcp_send_msg, strlen(tcp_send_msg), 0, (struct sockaddr*)&tcp_sock, sizeof(struct sockaddr));
|
||||
|
||||
lw_print("Send tcp msg: %s ", tcp_send_msg);
|
||||
|
||||
MdelayKTask(1000);
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (sock_tcp_send_once >= 0)
|
||||
closesocket(sock_tcp_send_once);
|
||||
if (fd >= 0)
|
||||
closesocket(fd);
|
||||
|
||||
return;
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
lwip_tcp_send_init(void)
|
||||
void lwip_tcp_send_run(int argc, char *argv[])
|
||||
{
|
||||
sys_thread_new("tcp send", lwip_tcp_send_thread, NULL, 4096, 25);
|
||||
}
|
||||
|
||||
|
||||
void lwip_tcp_client_run(int argc, char *argv[])
|
||||
{
|
||||
if(argc == 2)
|
||||
memset(tcp_send_msg, 0, MSG_SIZE);
|
||||
if(argc >= 2)
|
||||
{
|
||||
lw_print("lw: [%s] gw %s\n", __func__, argv[1]);
|
||||
sscanf(argv[1], "%d.%d.%d.%d", &tcp_target[0], &tcp_target[1], &tcp_target[2], &tcp_target[3]);
|
||||
strncpy(tcp_send_msg, argv[1], strlen(argv[1]));
|
||||
}
|
||||
else
|
||||
{
|
||||
strncpy(tcp_send_msg, "hello world", strlen("hello world"));
|
||||
}
|
||||
strcat(tcp_send_msg, "\r\n");
|
||||
|
||||
if(argc >= 3)
|
||||
{
|
||||
sscanf(argv[2], "%d.%d.%d.%d", &tcp_target[0], &tcp_target[1], &tcp_target[2], &tcp_target[3]);
|
||||
}
|
||||
|
||||
ETH_BSP_Config();
|
||||
lwip_config_tcp(lwip_ipaddr, lwip_netmask, lwip_gwaddr);
|
||||
lwip_tcp_send_init();
|
||||
sys_thread_new("tcp send", lwip_tcp_send_thread, NULL, 4096, 25);
|
||||
}
|
||||
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0),
|
||||
TCPSend, lwip_tcp_client_run, TCP Client);
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
|
||||
TCPSend, lwip_tcp_send_run, TCP Send message);
|
||||
|
||||
|
||||
void lwip_tcp_server_run(void)
|
||||
void lwip_tcp_recv_run(void)
|
||||
{
|
||||
ETH_BSP_Config();
|
||||
lwip_config_net(lwip_ipaddr, lwip_netmask, lwip_gwaddr);
|
||||
@@ -125,5 +118,5 @@ void lwip_tcp_server_run(void)
|
||||
}
|
||||
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0),
|
||||
TCPRecv, lwip_tcp_server_run, TCP Server);
|
||||
TCPRecv, lwip_tcp_recv_run, TCP Recv message);
|
||||
|
||||
|
||||
@@ -21,8 +21,10 @@
|
||||
#include <xiuos.h>
|
||||
#include "board.h"
|
||||
#include "sys_arch.h"
|
||||
#include "udp_echo.h"
|
||||
#include "lwip/udp.h"
|
||||
#include <lwip/sockets.h>
|
||||
#include "lwip/sys.h"
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
@@ -30,6 +32,7 @@
|
||||
|
||||
#define UDP_TASK_STACK_SIZE 4096
|
||||
#define UDP_TASK_PRIO 15
|
||||
#define PBUF_SIZE 27
|
||||
|
||||
/*******************************************************************************
|
||||
* Prototypes
|
||||
@@ -41,77 +44,142 @@
|
||||
|
||||
static struct udp_pcb *udpecho_raw_pcb;
|
||||
char udp_target[] = {192, 168, 250, 252};
|
||||
char hello_str[] = {"hello world\r\n"};
|
||||
char udp_send_msg[] = "\n\nThis one is UDP pkg. Congratulations on you.\n\n";
|
||||
|
||||
/*******************************************************************************
|
||||
* Code
|
||||
******************************************************************************/
|
||||
|
||||
static void *lwip_udp_test(void* param)
|
||||
static void lwip_udp_send(void *arg)
|
||||
{
|
||||
ETH_BSP_Config();
|
||||
lwip_config_tcp(lwip_ipaddr, lwip_netmask, lwip_gwaddr);
|
||||
UdpEchoInit();
|
||||
int cnt = TEST_LWIP_TIMES;
|
||||
|
||||
lw_print("udp_send_demo start.\n");
|
||||
|
||||
int socket_fd = -1;
|
||||
socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (socket_fd < 0)
|
||||
{
|
||||
lw_print("Socket error\n");
|
||||
return;
|
||||
}
|
||||
|
||||
struct sockaddr_in udp_sock;
|
||||
udp_sock.sin_family = AF_INET;
|
||||
udp_sock.sin_port = htons(TARGET_PORT_CLIENT);
|
||||
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));
|
||||
|
||||
if (connect(socket_fd, (struct sockaddr *)&udp_sock, sizeof(struct sockaddr)))
|
||||
{
|
||||
lw_print("Unable to connect\n");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
lw_print("UDP connect success, start to send.\n");
|
||||
lw_print("\n\nTarget Port:%d\n\n", udp_sock.sin_port);
|
||||
|
||||
sendto(socket_fd, udp_send_msg, strlen(udp_send_msg), 0, (struct sockaddr*)&udp_sock, sizeof(struct sockaddr));
|
||||
lw_pr_info("Send UDP msg: %s ", udp_send_msg);
|
||||
|
||||
__exit:
|
||||
if (socket_fd >= 0)
|
||||
{
|
||||
closesocket(socket_fd);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void lwip_udp_thread(int argc, char *argv[])
|
||||
void *lwip_udp_send_run(int argc, char *argv[])
|
||||
{
|
||||
int result = 0;
|
||||
pthread_t th_id;
|
||||
pthread_attr_t attr;
|
||||
|
||||
if(argc == 2)
|
||||
memset(udp_send_msg, 0, sizeof(udp_send_msg));
|
||||
|
||||
if(argc == 1)
|
||||
{
|
||||
lw_print("lw: [%s] gw %s\n", __func__, argv[1]);
|
||||
sscanf(argv[1], "%d.%d.%d.%d", &udp_target[0], &udp_target[1], &udp_target[2], &udp_target[3]);
|
||||
lw_print("lw: [%s] gw %d.%d.%d.%d\n", __func__, udp_target[0], udp_target[1], udp_target[2], udp_target[3]);
|
||||
strncpy(udp_send_msg, hello_str, strlen(hello_str));
|
||||
}
|
||||
|
||||
attr.schedparam.sched_priority = UDP_TASK_PRIO;
|
||||
attr.stacksize = UDP_TASK_STACK_SIZE;
|
||||
|
||||
result = pthread_create(&th_id, &attr, lwip_udp_test, NULL);
|
||||
if (0 == result) {
|
||||
lw_print("lwip_udp_test successfully!\n");
|
||||
} else {
|
||||
lw_print("lwip_udp_test failed! error code is %d\n", result);
|
||||
else
|
||||
{
|
||||
strncpy(udp_send_msg, argv[1], strlen(argv[1]));
|
||||
strncat(udp_send_msg, "\r\n", 2);
|
||||
if(argc == 3)
|
||||
{
|
||||
sscanf(argv[2], "%d.%d.%d.%d", &udp_target[0], &udp_target[1], &udp_target[2], &udp_target[3]);
|
||||
}
|
||||
}
|
||||
lw_print("lw: [%s] gw %d.%d.%d.%d\n", __func__, udp_target[0], udp_target[1], udp_target[2], udp_target[3]);
|
||||
|
||||
ETH_BSP_Config();
|
||||
lwip_config_tcp(lwip_ipaddr, lwip_netmask, lwip_gwaddr);
|
||||
sys_thread_new("udp socket send", lwip_udp_send, NULL, 4096, 25);
|
||||
}
|
||||
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
|
||||
UDPSend, lwip_udp_thread, UDP send echo);
|
||||
UDPSend, lwip_udp_send_run, UDP send echo);
|
||||
|
||||
static void
|
||||
udpecho_raw_recv(void *arg, struct udp_pcb *upcb, struct pbuf *p,
|
||||
static void udpecho_raw_recv(void *arg, struct udp_pcb *upcb, struct pbuf *p,
|
||||
const ip_addr_t *addr, u16_t port)
|
||||
{
|
||||
LWIP_UNUSED_ARG(arg);
|
||||
if (p != NULL) {
|
||||
int udp_len;
|
||||
err_t err;
|
||||
struct pbuf* udp_buf;
|
||||
|
||||
LWIP_UNUSED_ARG(arg);
|
||||
|
||||
if (p == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
udp_len = p->tot_len;
|
||||
lw_pr_info("Receive data :%dB\r\n", udp_len);
|
||||
|
||||
if(udp_len <= 80)
|
||||
{
|
||||
lw_pr_info("%.*s\r\n", udp_len, (char *)(p->payload));
|
||||
}
|
||||
|
||||
udp_buf = pbuf_alloc(PBUF_TRANSPORT, PBUF_SIZE, PBUF_RAM);
|
||||
|
||||
memset(udp_buf->payload, 0, PBUF_SIZE);
|
||||
|
||||
err = pbuf_take(udp_buf, "Client receive success!\r\n", 27);
|
||||
|
||||
/* send received packet back to sender */
|
||||
udp_sendto(upcb, p, addr, port);
|
||||
udp_sendto(upcb, udp_buf, addr, port);
|
||||
|
||||
/* free the pbuf */
|
||||
pbuf_free(p);
|
||||
}
|
||||
pbuf_free(udp_buf);
|
||||
}
|
||||
|
||||
void
|
||||
udpecho_raw_init(void)
|
||||
void udpecho_raw_init(void)
|
||||
{
|
||||
udpecho_raw_pcb = udp_new_ip_type(IPADDR_TYPE_ANY);
|
||||
if (udpecho_raw_pcb != NULL) {
|
||||
err_t err;
|
||||
lw_print("udpecho_raw_init\r\n");
|
||||
|
||||
udpecho_raw_pcb = udp_new_ip_type(IPADDR_TYPE_ANY);
|
||||
if (udpecho_raw_pcb == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
err = udp_bind(udpecho_raw_pcb, IP_ANY_TYPE, LOCAL_PORT_SERVER);
|
||||
if (err == ERR_OK) {
|
||||
udp_recv(udpecho_raw_pcb, udpecho_raw_recv, NULL);
|
||||
} else {
|
||||
/* abort? output diagnostic? */
|
||||
if (err == ERR_OK)
|
||||
{
|
||||
udp_recv(udpecho_raw_pcb, udpecho_raw_recv, NULL);
|
||||
}
|
||||
} else {
|
||||
/* abort? output diagnostic? */
|
||||
}
|
||||
}
|
||||
|
||||
void lwip_udp_server(void)
|
||||
{
|
||||
lw_print("lwip_udp_server\r\n");
|
||||
ETH_BSP_Config();
|
||||
lwip_config_net(lwip_ipaddr, lwip_netmask, lwip_gwaddr);
|
||||
udpecho_raw_init();
|
||||
|
||||
@@ -45,6 +45,7 @@
|
||||
#include "lwip/stats.h"
|
||||
#include "lwip/tcp.h"
|
||||
#include "tcpecho_raw.h"
|
||||
#include <string.h>
|
||||
|
||||
#if LWIP_TCP && LWIP_CALLBACK_API
|
||||
|
||||
@@ -94,42 +95,6 @@ tcpecho_raw_close(struct tcp_pcb *tpcb, struct tcpecho_raw_state *es)
|
||||
tcp_close(tpcb);
|
||||
}
|
||||
|
||||
static void
|
||||
tcpecho_raw_send(struct tcp_pcb *tpcb, struct tcpecho_raw_state *es)
|
||||
{
|
||||
struct pbuf *ptr;
|
||||
err_t wr_err = ERR_OK;
|
||||
|
||||
while ((wr_err == ERR_OK) &&
|
||||
(es->p != NULL) &&
|
||||
(es->p->len <= tcp_sndbuf(tpcb))) {
|
||||
ptr = es->p;
|
||||
|
||||
/* enqueue data for transmission */
|
||||
wr_err = tcp_write(tpcb, ptr->payload, ptr->len, 1);
|
||||
if (wr_err == ERR_OK) {
|
||||
u16_t plen;
|
||||
|
||||
plen = ptr->len;
|
||||
/* continue with next pbuf in chain (if any) */
|
||||
es->p = ptr->next;
|
||||
if(es->p != NULL) {
|
||||
/* new reference! */
|
||||
pbuf_ref(es->p);
|
||||
}
|
||||
/* chop first pbuf from chain */
|
||||
pbuf_free(ptr);
|
||||
/* we can read more data now */
|
||||
tcp_recved(tpcb, plen);
|
||||
} else if(wr_err == ERR_MEM) {
|
||||
/* we are low on memory, try later / harder, defer to poll */
|
||||
es->p = ptr;
|
||||
} else {
|
||||
/* other problem ?? */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
tcpecho_raw_error(void *arg, err_t err)
|
||||
{
|
||||
@@ -142,6 +107,51 @@ tcpecho_raw_error(void *arg, err_t err)
|
||||
tcpecho_raw_free(es);
|
||||
}
|
||||
|
||||
|
||||
char* recved_msg;
|
||||
static void record_msg(struct tcp_pcb *tpcb, struct tcpecho_raw_state* es){
|
||||
if(es==NULL||es->p==NULL||es->p->len==0){
|
||||
recved_msg = NULL;
|
||||
}
|
||||
int new_size = es->p->len+1;
|
||||
char* temp = recved_msg;
|
||||
if(temp!=NULL){
|
||||
new_size += strlen(temp);
|
||||
lw_print("temp: %s\r\n", recved_msg);
|
||||
}
|
||||
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-es->p->len-1, es->p->payload, es->p->len);
|
||||
free(temp);
|
||||
if(((char*)es->p->payload)[es->p->len-1]=='\n'){
|
||||
if(strlen(recved_msg)<80){
|
||||
lw_pr_info("Received: %s\n", recved_msg);
|
||||
}else{
|
||||
lw_pr_info("Received a string of length %d\n", strlen(recved_msg));
|
||||
}
|
||||
struct pbuf* reply_pbuf = pbuf_alloc(PBUF_TRANSPORT, 20, PBUF_RAM); // only reply received message length
|
||||
sprintf(reply_pbuf->payload,"%d\n\0",strlen(recved_msg));
|
||||
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);
|
||||
tcp_recved(tpcb, reply_pbuf->len);
|
||||
|
||||
pbuf_free(reply_pbuf);
|
||||
free(recved_msg);
|
||||
recved_msg = NULL;
|
||||
}
|
||||
es->p = es->p->next;
|
||||
if(es->p != NULL) {
|
||||
/* new reference! */
|
||||
pbuf_ref(es->p);
|
||||
}
|
||||
}
|
||||
|
||||
static err_t
|
||||
tcpecho_raw_poll(void *arg, struct tcp_pcb *tpcb)
|
||||
{
|
||||
@@ -152,7 +162,7 @@ tcpecho_raw_poll(void *arg, struct tcp_pcb *tpcb)
|
||||
if (es != NULL) {
|
||||
if (es->p != NULL) {
|
||||
/* there is a remaining pbuf (chain) */
|
||||
tcpecho_raw_send(tpcb, es);
|
||||
record_msg(tpcb, es);
|
||||
} else {
|
||||
/* no remaining pbuf (chain) */
|
||||
if(es->state == ES_CLOSING) {
|
||||
@@ -177,16 +187,10 @@ tcpecho_raw_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
|
||||
|
||||
es = (struct tcpecho_raw_state *)arg;
|
||||
es->retries = 0;
|
||||
|
||||
if(es->p != NULL) {
|
||||
/* still got pbufs to send */
|
||||
tcp_sent(tpcb, tcpecho_raw_sent);
|
||||
tcpecho_raw_send(tpcb, es);
|
||||
} else {
|
||||
/* no more pbufs to send */
|
||||
if(es->state == ES_CLOSING) {
|
||||
tcpecho_raw_close(tpcb, es);
|
||||
}
|
||||
es->p = NULL;
|
||||
// the sent reply from server never have successors
|
||||
if(es->state == ES_CLOSING) {
|
||||
tcpecho_raw_close(tpcb, es);
|
||||
}
|
||||
return ERR_OK;
|
||||
}
|
||||
@@ -207,7 +211,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 */
|
||||
tcpecho_raw_send(tpcb, es);
|
||||
record_msg(tpcb, es);
|
||||
}
|
||||
ret_err = ERR_OK;
|
||||
} else if(err != ERR_OK) {
|
||||
@@ -222,13 +226,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;
|
||||
tcpecho_raw_send(tpcb, es);
|
||||
record_msg(tpcb, es);
|
||||
ret_err = ERR_OK;
|
||||
} else if (es->state == ES_RECEIVED) {
|
||||
/* read some more data */
|
||||
if(es->p == NULL) {
|
||||
es->p = p;
|
||||
tcpecho_raw_send(tpcb, es);
|
||||
record_msg(tpcb, es);
|
||||
} else {
|
||||
struct pbuf *ptr;
|
||||
|
||||
@@ -251,6 +255,7 @@ tcpecho_raw_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
|
||||
{
|
||||
err_t ret_err;
|
||||
struct tcpecho_raw_state *es;
|
||||
recved_msg = NULL;
|
||||
|
||||
LWIP_UNUSED_ARG(arg);
|
||||
if ((err != ERR_OK) || (newpcb == NULL)) {
|
||||
|
||||
@@ -1,192 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
* OF SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the lwIP TCP/IP stack.
|
||||
*
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file tcpecho.c
|
||||
* @brief Add UDP client function
|
||||
* @version 1.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2021-05-29
|
||||
*/
|
||||
|
||||
#include "udp_echo.h"
|
||||
#include <transform.h>
|
||||
#include "lwip/opt.h"
|
||||
#include "sys_arch.h"
|
||||
|
||||
#if LWIP_SOCKET
|
||||
#include <lwip/sockets.h>
|
||||
#include "lwip/sys.h"
|
||||
|
||||
#ifdef BOARD_CORTEX_M7_EVB
|
||||
#define LWIP_UDP_TASK_STACK 4096
|
||||
#else
|
||||
#define LWIP_UDP_TASK_STACK 2048
|
||||
#endif
|
||||
|
||||
#define LWIP_UDP_TASK_PRIO 25
|
||||
|
||||
#define RECV_DATA (1024)
|
||||
|
||||
char* udp_send_msg = "\n\nThis one is UDP pkg. Congratulations on you.\n\n";
|
||||
|
||||
static void UdpEchoThreadServer(void *arg)
|
||||
{
|
||||
lw_print("UdpEchoThreadServer start.\n");
|
||||
|
||||
int sock = -1;
|
||||
char *recv_data;
|
||||
struct sockaddr_in udp_addr,seraddr;
|
||||
int recv_data_len;
|
||||
socklen_t addrlen;
|
||||
|
||||
while(1)
|
||||
{
|
||||
recv_data = (char *)malloc(RECV_DATA);
|
||||
if (recv_data == NULL)
|
||||
{
|
||||
lw_print("No memory\n");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sock < 0)
|
||||
{
|
||||
lw_print("Socket error\n");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
udp_addr.sin_family = AF_INET;
|
||||
udp_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
udp_addr.sin_port = htons(LOCAL_PORT_SERVER);
|
||||
memset(&(udp_addr.sin_zero), 0, sizeof(udp_addr.sin_zero));
|
||||
|
||||
if (bind(sock, (struct sockaddr *)&udp_addr, sizeof(struct sockaddr)) == -1)
|
||||
{
|
||||
lw_print("Unable to bind\n");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
lw_print("UDP bind sucess, start to receive.\n");
|
||||
lw_print("\n\nLocal Port:%d\n\n", LOCAL_PORT_SERVER);
|
||||
|
||||
while(1)
|
||||
{
|
||||
memset(recv_data, 0, RECV_DATA);
|
||||
recv_data_len=recvfrom(sock,recv_data,
|
||||
RECV_DATA,0,
|
||||
(struct sockaddr*)&seraddr,
|
||||
&addrlen);
|
||||
|
||||
lw_print("Receive from : %s\n",inet_ntoa(seraddr.sin_addr));
|
||||
|
||||
lw_print("Recevce data : %s\n\n",recv_data);
|
||||
|
||||
sendto(sock,recv_data,
|
||||
recv_data_len,0,
|
||||
(struct sockaddr*)&seraddr,
|
||||
addrlen);
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (sock >= 0) closesocket(sock);
|
||||
if (recv_data) free(recv_data);
|
||||
}
|
||||
}
|
||||
|
||||
static void UdpEchoThreadClient(void *arg)
|
||||
{
|
||||
int cnt = TEST_LWIP_TIMES;
|
||||
lw_print("UdpEchoThreadClient start.\n");
|
||||
|
||||
int sock_udp_send_once = -1;
|
||||
sock_udp_send_once = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sock_udp_send_once < 0)
|
||||
{
|
||||
lw_print("Socket error\n");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
struct sockaddr_in udp_sock;
|
||||
udp_sock.sin_family = AF_INET;
|
||||
udp_sock.sin_port = htons(TARGET_PORT_CLIENT);
|
||||
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));
|
||||
|
||||
if (connect(sock_udp_send_once, (struct sockaddr *)&udp_sock, sizeof(struct sockaddr)))
|
||||
{
|
||||
lw_print("Unable to connect\n");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
lw_print("UDP connect success, start to send.\n");
|
||||
lw_print("\n\nTarget Port:%d\n\n", udp_sock.sin_port);
|
||||
|
||||
sendto(sock_udp_send_once, udp_send_msg,
|
||||
strlen(udp_send_msg), 0,
|
||||
(struct sockaddr*)&udp_sock,
|
||||
sizeof(struct sockaddr));
|
||||
|
||||
lw_pr_info("Send UDP msg: %s ", udp_send_msg);
|
||||
|
||||
__exit:
|
||||
if (sock_udp_send_once >= 0)
|
||||
{
|
||||
closesocket(sock_udp_send_once);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
UdpEchoInit(void)
|
||||
{
|
||||
#ifdef SET_AS_SERVER
|
||||
sys_thread_new("UdpEchoThreadServer", UdpEchoThreadServer, NULL, LWIP_UDP_TASK_STACK, LWIP_UDP_TASK_PRIO);
|
||||
#else
|
||||
sys_thread_new("UdpEchoThreadClient", UdpEchoThreadClient, NULL, LWIP_UDP_TASK_STACK, LWIP_UDP_TASK_PRIO);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif /* LWIP_NETCONN */
|
||||
@@ -1,42 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||
* OF SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the lwIP TCP/IP stack.
|
||||
*
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef LWIP_TCPECHO_H
|
||||
#define LWIP_TCPECHO_H
|
||||
|
||||
extern char udp_target[];
|
||||
|
||||
void UdpEchoInit(void);
|
||||
|
||||
|
||||
#endif /* LWIP_TCPECHO_H */
|
||||
Reference in New Issue
Block a user