optimize lwip test
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user