diff --git a/APP_Framework/Applications/connection_app/Makefile b/APP_Framework/Applications/connection_app/Makefile index e4ad12741..cb4403cd8 100755 --- a/APP_Framework/Applications/connection_app/Makefile +++ b/APP_Framework/Applications/connection_app/Makefile @@ -1,7 +1,7 @@ SRC_DIR := ifeq ($(CONFIG_RESOURCES_LWIP),y) - SRC_DIR += lwip_demo + SRC_DIR += socket_demo endif include $(KERNEL_ROOT)/compiler.mk \ No newline at end of file diff --git a/APP_Framework/Applications/connection_app/socket_demo/Makefile b/APP_Framework/Applications/connection_app/socket_demo/Makefile new file mode 100755 index 000000000..748a8d6e2 --- /dev/null +++ b/APP_Framework/Applications/connection_app/socket_demo/Makefile @@ -0,0 +1,3 @@ +SRC_FILES := udp_socket.c lwip_tcp_socket_demo.c lwip_udp_socket_demo.c + +include $(KERNEL_ROOT)/compiler.mk diff --git a/APP_Framework/Applications/connection_app/lwip_demo/lwip_demo.h b/APP_Framework/Applications/connection_app/socket_demo/d/lwip_demo.h similarity index 100% rename from APP_Framework/Applications/connection_app/lwip_demo/lwip_demo.h rename to APP_Framework/Applications/connection_app/socket_demo/d/lwip_demo.h diff --git a/APP_Framework/Applications/connection_app/lwip_demo/lwip_dhcp_demo.c b/APP_Framework/Applications/connection_app/socket_demo/d/lwip_dhcp_demo.c similarity index 100% rename from APP_Framework/Applications/connection_app/lwip_demo/lwip_dhcp_demo.c rename to APP_Framework/Applications/connection_app/socket_demo/d/lwip_dhcp_demo.c diff --git a/APP_Framework/Applications/connection_app/lwip_demo/lwip_ping_demo.c b/APP_Framework/Applications/connection_app/socket_demo/d/lwip_ping_demo.c similarity index 100% rename from APP_Framework/Applications/connection_app/lwip_demo/lwip_ping_demo.c rename to APP_Framework/Applications/connection_app/socket_demo/d/lwip_ping_demo.c diff --git a/APP_Framework/Applications/connection_app/lwip_demo/ping.c b/APP_Framework/Applications/connection_app/socket_demo/d/ping.c similarity index 100% rename from APP_Framework/Applications/connection_app/lwip_demo/ping.c rename to APP_Framework/Applications/connection_app/socket_demo/d/ping.c diff --git a/APP_Framework/Applications/connection_app/lwip_demo/ping.h b/APP_Framework/Applications/connection_app/socket_demo/d/ping.h similarity index 100% rename from APP_Framework/Applications/connection_app/lwip_demo/ping.h rename to APP_Framework/Applications/connection_app/socket_demo/d/ping.h diff --git a/APP_Framework/Applications/connection_app/socket_demo/d/tcp_socket.c b/APP_Framework/Applications/connection_app/socket_demo/d/tcp_socket.c new file mode 100755 index 000000000..5ceb54449 --- /dev/null +++ b/APP_Framework/Applications/connection_app/socket_demo/d/tcp_socket.c @@ -0,0 +1,302 @@ +/* + * Copyright (c) 2001-2004 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 and a contribution to the lwIP TCP/IP stack. + * + * Credits go to Adam Dunkels (and the current maintainers) of this software. + * + * Christiaan Simons rewrote this file to get a more stable echo example. + */ + +/** + * @file + * TCP echo server example using raw API. + * + * Echos all bytes sent by connecting client, + * and passively closes when client is done. + * + */ + +#include "lwip/opt.h" +#include "lwip/debug.h" +#include "lwip/stats.h" +#include "lwip/tcp.h" +#include "tcp_socket.h" + +#if LWIP_TCP && LWIP_CALLBACK_API + +static struct tcp_pcb *tcpecho_raw_pcb; + +enum tcpecho_raw_states +{ + ES_NONE = 0, + ES_ACCEPTED, + ES_RECEIVED, + ES_CLOSING +}; + +struct tcpecho_raw_state +{ + u8_t state; + u8_t retries; + struct tcp_pcb *pcb; + /* pbuf (chain) to recycle */ + struct pbuf *p; +}; + +static void +tcpecho_raw_free(struct tcpecho_raw_state *es) +{ + if (es != NULL) { + if (es->p) { + /* free the buffer chain if present */ + pbuf_free(es->p); + } + + mem_free(es); + } +} + +static void +tcpecho_raw_close(struct tcp_pcb *tpcb, struct tcpecho_raw_state *es) +{ + tcp_arg(tpcb, NULL); + tcp_sent(tpcb, NULL); + tcp_recv(tpcb, NULL); + tcp_err(tpcb, NULL); + tcp_poll(tpcb, NULL, 0); + + tcpecho_raw_free(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) +{ + struct tcpecho_raw_state *es; + + LWIP_UNUSED_ARG(err); + + es = (struct tcpecho_raw_state *)arg; + + tcpecho_raw_free(es); +} + +static err_t +tcpecho_raw_poll(void *arg, struct tcp_pcb *tpcb) +{ + err_t ret_err; + struct tcpecho_raw_state *es; + + es = (struct tcpecho_raw_state *)arg; + if (es != NULL) { + if (es->p != NULL) { + /* there is a remaining pbuf (chain) */ + tcpecho_raw_send(tpcb, es); + } else { + /* no remaining pbuf (chain) */ + if(es->state == ES_CLOSING) { + tcpecho_raw_close(tpcb, es); + } + } + ret_err = ERR_OK; + } else { + /* nothing to be done */ + tcp_abort(tpcb); + ret_err = ERR_ABRT; + } + return ret_err; +} + +static err_t +tcpecho_raw_sent(void *arg, struct tcp_pcb *tpcb, u16_t len) +{ + struct tcpecho_raw_state *es; + + LWIP_UNUSED_ARG(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); + } + } + return ERR_OK; +} + +static err_t +tcpecho_raw_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) +{ + struct tcpecho_raw_state *es; + err_t ret_err; + + LWIP_ASSERT("arg != NULL",arg != NULL); + es = (struct tcpecho_raw_state *)arg; + if (p == NULL) { + /* remote host closed connection */ + es->state = ES_CLOSING; + if(es->p == NULL) { + /* we're done sending, close it */ + tcpecho_raw_close(tpcb, es); + } else { + /* we're not done yet */ + tcpecho_raw_send(tpcb, es); + } + ret_err = ERR_OK; + } else if(err != ERR_OK) { + /* cleanup, for unknown reason */ + if (p != NULL) { + pbuf_free(p); + } + ret_err = err; + } + else if(es->state == ES_ACCEPTED) { + /* first data chunk in p->payload */ + es->state = ES_RECEIVED; + /* store reference to incoming pbuf (chain) */ + es->p = p; + tcpecho_raw_send(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); + } else { + struct pbuf *ptr; + + /* chain pbufs to the end of what we recv'ed previously */ + ptr = es->p; + pbuf_cat(ptr,p); + } + ret_err = ERR_OK; + } else { + /* unkown es->state, trash data */ + tcp_recved(tpcb, p->tot_len); + pbuf_free(p); + ret_err = ERR_OK; + } + return ret_err; +} + +static err_t +tcpecho_raw_accept(void *arg, struct tcp_pcb *newpcb, err_t err) +{ + err_t ret_err; + struct tcpecho_raw_state *es; + + LWIP_UNUSED_ARG(arg); + if ((err != ERR_OK) || (newpcb == NULL)) { + return ERR_VAL; + } + + /* Unless this pcb should have NORMAL priority, set its priority now. + When running out of pcbs, low priority pcbs can be aborted to create + new pcbs of higher priority. */ + tcp_setprio(newpcb, TCP_PRIO_MIN); + + es = (struct tcpecho_raw_state *)mem_malloc(sizeof(struct tcpecho_raw_state)); + if (es != NULL) { + es->state = ES_ACCEPTED; + es->pcb = newpcb; + es->retries = 0; + es->p = NULL; + /* pass newly allocated es to our callbacks */ + tcp_arg(newpcb, es); + tcp_recv(newpcb, tcpecho_raw_recv); + tcp_err(newpcb, tcpecho_raw_error); + tcp_poll(newpcb, tcpecho_raw_poll, 0); + tcp_sent(newpcb, tcpecho_raw_sent); + ret_err = ERR_OK; + } else { + ret_err = ERR_MEM; + } + return ret_err; +} + +void +tcpecho_raw_init(void) +{ + tcpecho_raw_pcb = tcp_new_ip_type(IPADDR_TYPE_ANY); + if (tcpecho_raw_pcb != NULL) { + err_t err; + err = tcp_bind(tcpecho_raw_pcb, IP_ANY_TYPE, LWIP_TEST_TCP_PORT); + if (err == ERR_OK) { + tcpecho_raw_pcb = tcp_listen(tcpecho_raw_pcb); + tcp_accept(tcpecho_raw_pcb, tcpecho_raw_accept); + } else { + /* abort? output diagnostic? */ + } + } else { + /* abort? output diagnostic? */ + } +} + +#endif /* LWIP_TCP && LWIP_CALLBACK_API */ diff --git a/APP_Framework/Applications/connection_app/socket_demo/d/tcp_socket.h b/APP_Framework/Applications/connection_app/socket_demo/d/tcp_socket.h new file mode 100755 index 000000000..9c28450b1 --- /dev/null +++ b/APP_Framework/Applications/connection_app/socket_demo/d/tcp_socket.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2001-2004 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. + * + */ +#ifndef __LWIP_TCP_SOCKET_H_ +#define __LWIP_TCP_SOCKET_H_ + +#define LWIP_TEST_TCP_PORT 4840 + +void tcpecho_raw_init(void); + +#endif /* __LWIP_TCP_SOCKET_H_ */ diff --git a/APP_Framework/Applications/connection_app/socket_demo/lwip_tcp_socket_demo.c b/APP_Framework/Applications/connection_app/socket_demo/lwip_tcp_socket_demo.c new file mode 100755 index 000000000..8d49b5122 --- /dev/null +++ b/APP_Framework/Applications/connection_app/socket_demo/lwip_tcp_socket_demo.c @@ -0,0 +1,111 @@ +/* +* 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 tcp_echo_socket_demo.c +* @brief One UDP demo based on LwIP +* @version 1.0 +* @author AIIT XUOS Lab +* @date 2021-05-29 +*/ +#include +#include +#include "board.h" +#include "sys_arch.h" +#include +#include "lwip/sys.h" + +/******************************************************************************* + * Definitions + ******************************************************************************/ + +/******************************************************************************* + * Prototypes + ******************************************************************************/ + +/******************************************************************************* + * Variables + ******************************************************************************/ + +char tcp_socket_ip[] = {192, 168, 250, 252}; +char* tcp_socket_str = "\n\nThis one is TCP pkg. Congratulations on you.\n\n"; + +/******************************************************************************* + * Code + ******************************************************************************/ + +static void lwip_tcp_socket_thread(void *arg) +{ + int cnt = TEST_LWIP_TIMES; + lw_print("lwip_tcp_socket_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; + } + + 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_socket_ip[0],tcp_socket_ip[1],tcp_socket_ip[2],tcp_socket_ip[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; + } + + 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_socket_str, + strlen(tcp_socket_str),0, + (struct sockaddr*)&tcp_sock, + sizeof(struct sockaddr)); + + lw_print("Send tcp msg: %s ", tcp_socket_str); + + MdelayKTask(1000); + } + + __exit: + if (sock_tcp_send_once >= 0) + closesocket(sock_tcp_send_once); + + return; +} + + +void lwip_tcp_socket_run(int argc, char *argv[]) +{ + if(argc == 2) + { + lw_print("lw: [%s] gw %s\n", __func__, argv[1]); + sscanf(argv[1], "%d.%d.%d.%d", &tcp_socket_ip[0], &tcp_socket_ip[1], &tcp_socket_ip[2], &tcp_socket_ip[3]); + } + + ETH_BSP_Config(); + lwip_config_tcp(lwip_ipaddr, lwip_netmask, lwip_gwaddr); + sys_thread_new("tcp socket", lwip_tcp_socket_thread, NULL, 4096, 25); +} + +SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0), + TCPSocket, lwip_tcp_socket_run, TCP Client); + diff --git a/APP_Framework/Applications/connection_app/socket_demo/lwip_udp_socket_demo.c b/APP_Framework/Applications/connection_app/socket_demo/lwip_udp_socket_demo.c new file mode 100755 index 000000000..09277eee1 --- /dev/null +++ b/APP_Framework/Applications/connection_app/socket_demo/lwip_udp_socket_demo.c @@ -0,0 +1,70 @@ +/* +* 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 lwip_udp_demo.c +* @brief One UDP demo based on LwIP +* @version 1.0 +* @author AIIT XUOS Lab +* @date 2021-05-29 +*/ +#include +#include +#include "board.h" +#include "sys_arch.h" +#include "udp_socket.h" +#include "lwip/udp.h" + +/******************************************************************************* + * Definitions + ******************************************************************************/ + +/******************************************************************************* + * Prototypes + ******************************************************************************/ + +/******************************************************************************* + * Variables + ******************************************************************************/ + +static struct udp_pcb *udpecho_raw_pcb; +char udp_socket_ip[] = {192, 168, 250, 252}; + +/******************************************************************************* + * Code + ******************************************************************************/ + +static void lwip_udp_socket_thread(void* param) +{ + ETH_BSP_Config(); + lwip_config_tcp(lwip_ipaddr, lwip_netmask, lwip_gwaddr); + UdpSocketInit(); +} + +void lwip_udp_socket_run(int argc, char *argv[]) +{ + int result = 0; + pthread_t th_id; + pthread_attr_t attr; + + if(argc == 2) + { + lw_print("lw: [%s] gw %s\n", __func__, argv[1]); + sscanf(argv[1], "%d.%d.%d.%d", &udp_socket_ip[0], &udp_socket_ip[1], &udp_socket_ip[2], &udp_socket_ip[3]); + } + + sys_thread_new("udp socket", lwip_udp_socket_thread, NULL, 4096, 15); +} + +SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3), + UDPSocket, lwip_udp_socket_run, UDP send echo); + diff --git a/APP_Framework/Applications/connection_app/socket_demo/udp_socket.c b/APP_Framework/Applications/connection_app/socket_demo/udp_socket.c new file mode 100755 index 000000000..b0410baa6 --- /dev/null +++ b/APP_Framework/Applications/connection_app/socket_demo/udp_socket.c @@ -0,0 +1,202 @@ +/* + * 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 + * + */ + +/* +* 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_socket.h" +#include +#include "lwip/opt.h" +#include "sys_arch.h" + +#if LWIP_SOCKET +#include +#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_socket_str = "\n\nThis one is UDP pkg. Congratulations on you.\n\n"; + +static void UdpSocketThreadServer(void *arg) +{ + lw_print("UdpSocketThreadServer 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 UdpSocketThreadClient(void *arg) +{ + int cnt = TEST_LWIP_TIMES; + lw_print("UdpSocketThreadClient 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); + + while (cnt --) + { + lw_print("UDP Client is running.\n"); + + sendto(sock_udp_send_once, udp_socket_str, + strlen(udp_socket_str), 0, + (struct sockaddr*)&udp_sock, + sizeof(struct sockaddr)); + + lw_pr_info("Send UDP msg: %s ", udp_socket_str); + + MdelayKTask(1000); + } + + __exit: + if (sock_udp_send_once >= 0) + { + closesocket(sock_udp_send_once); + } + + return; +} + +void +UdpSocketInit(void) +{ +#ifdef SET_AS_SERVER + sys_thread_new("UdpSocketThreadServer", UdpSocketThreadServer, NULL, LWIP_UDP_TASK_STACK, LWIP_UDP_TASK_PRIO); +#else + sys_thread_new("UdpSocketThreadClient", UdpSocketThreadClient, NULL, LWIP_UDP_TASK_STACK, LWIP_UDP_TASK_PRIO); +#endif +} + +#endif /* LWIP_NETCONN */ diff --git a/APP_Framework/Applications/connection_app/socket_demo/udp_socket.h b/APP_Framework/Applications/connection_app/socket_demo/udp_socket.h new file mode 100755 index 000000000..7c9a412f2 --- /dev/null +++ b/APP_Framework/Applications/connection_app/socket_demo/udp_socket.h @@ -0,0 +1,42 @@ +/* + * 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 + * + */ + + +#ifndef LWIP_UDP_SOCKET_H +#define LWIP_UDP_SOCKET_H + +extern char udp_target[]; + +void UdpSocketInit(void); + + +#endif /* LWIP_TCPECHO_H */ diff --git a/Ubiquitous/XiUOS/resources/ethernet/Makefile b/Ubiquitous/XiUOS/resources/ethernet/Makefile index a511fa3f4..b5c4253d8 100644 --- a/Ubiquitous/XiUOS/resources/ethernet/Makefile +++ b/Ubiquitous/XiUOS/resources/ethernet/Makefile @@ -1,3 +1,3 @@ -SRC_DIR += LwIP +SRC_DIR += LwIP test include $(KERNEL_ROOT)/compiler.mk diff --git a/APP_Framework/Applications/connection_app/lwip_demo/Makefile b/Ubiquitous/XiUOS/resources/ethernet/test/Makefile similarity index 100% rename from APP_Framework/Applications/connection_app/lwip_demo/Makefile rename to Ubiquitous/XiUOS/resources/ethernet/test/Makefile diff --git a/APP_Framework/Applications/connection_app/lwip_demo/lwip_config_demo.c b/Ubiquitous/XiUOS/resources/ethernet/test/lwip_config_demo.c similarity index 100% rename from APP_Framework/Applications/connection_app/lwip_demo/lwip_config_demo.c rename to Ubiquitous/XiUOS/resources/ethernet/test/lwip_config_demo.c diff --git a/Ubiquitous/XiUOS/resources/ethernet/test/lwip_demo.h b/Ubiquitous/XiUOS/resources/ethernet/test/lwip_demo.h new file mode 100755 index 000000000..c2cf2ebbb --- /dev/null +++ b/Ubiquitous/XiUOS/resources/ethernet/test/lwip_demo.h @@ -0,0 +1,6 @@ +#ifndef __LWIP_DEMO_H__ +#define __LWIP_DEMO_H__ + +void *eth_input_thread(void *param); + +#endif /* __LWIP_DEMO_H__ */ diff --git a/Ubiquitous/XiUOS/resources/ethernet/test/lwip_dhcp_demo.c b/Ubiquitous/XiUOS/resources/ethernet/test/lwip_dhcp_demo.c new file mode 100755 index 000000000..40c5ceaf2 --- /dev/null +++ b/Ubiquitous/XiUOS/resources/ethernet/test/lwip_dhcp_demo.c @@ -0,0 +1,172 @@ +/* + * Copyright (c) 2016, Freescale Semiconductor, Inc. + * Copyright 2016-2019 NXP + * All rights reserved. + * + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +/******************************************************************************* + * Includes + ******************************************************************************/ + +#include "lwip/opt.h" + +#if LWIP_IPV4 && LWIP_DHCP + +#include "lwip/timeouts.h" +#include "lwip/ip_addr.h" +#include "lwip/init.h" +#include "lwip/dhcp.h" +#include "lwip/prot/dhcp.h" +#include "netif/ethernet.h" +#include "enet_ethernetif.h" + +#include "board.h" + +#include "pin_mux.h" +#include "clock_config.h" +#include "fsl_gpio.h" +#include "fsl_iomuxc.h" +#include "sys_arch.h" + +/******************************************************************************* + * Definitions + ******************************************************************************/ + +/******************************************************************************* + * Prototypes + ******************************************************************************/ + +/******************************************************************************* + * Variables + ******************************************************************************/ + +/******************************************************************************* + * Code + ******************************************************************************/ + +/*! + * @brief Prints DHCP status of the interface when it has changed from last status. + * + * @param netif network interface structure + */ +int print_dhcp_state(struct netif *netif) +{ + static u8_t dhcp_last_state = DHCP_STATE_OFF; + struct dhcp *dhcp = netif_dhcp_data(netif); + + if (dhcp == NULL) + { + dhcp_last_state = DHCP_STATE_OFF; + } + else if (dhcp_last_state != dhcp->state) + { + dhcp_last_state = dhcp->state; + + lw_pr_info(" DHCP state : "); + switch (dhcp_last_state) + { + case DHCP_STATE_OFF: + lw_pr_info("OFF"); + break; + case DHCP_STATE_REQUESTING: + lw_pr_info("REQUESTING"); + break; + case DHCP_STATE_INIT: + lw_pr_info("INIT"); + break; + case DHCP_STATE_REBOOTING: + lw_pr_info("REBOOTING"); + break; + case DHCP_STATE_REBINDING: + lw_pr_info("REBINDING"); + break; + case DHCP_STATE_RENEWING: + lw_pr_info("RENEWING"); + break; + case DHCP_STATE_SELECTING: + lw_pr_info("SELECTING"); + break; + case DHCP_STATE_INFORMING: + lw_pr_info("INFORMING"); + break; + case DHCP_STATE_CHECKING: + lw_pr_info("CHECKING"); + break; + case DHCP_STATE_BOUND: + lw_pr_info("BOUND"); + break; + case DHCP_STATE_BACKING_OFF: + lw_pr_info("BACKING_OFF"); + break; + default: + lw_pr_info("%u", dhcp_last_state); + assert(0); + break; + } + lw_pr_info("\r\n"); + + if (dhcp_last_state == DHCP_STATE_BOUND) + { + lw_pr_info("\r\n IPv4 Address : %s\r\n", ipaddr_ntoa(&netif->ip_addr)); + lw_pr_info(" IPv4 Subnet mask : %s\r\n", ipaddr_ntoa(&netif->netmask)); + lw_pr_info(" IPv4 Gateway : %s\r\n\r\n", ipaddr_ntoa(&netif->gw)); + return 1; + } + } + return 0; +} + +/*! + * @brief Main function. + */ +void lwip_dhcp_test(void) +{ + static int flag = 0; + char ip_addr[4] = {0, 0, 0, 0}; + + ETH_BSP_Config(); + + lwip_config_net(ip_addr, ip_addr, ip_addr); + is_lwip_test = 1; + + dhcp_start(&gnetif); + + lw_pr_info("\r\n************************************************\r\n"); + lw_pr_info(" DHCP example\r\n"); + lw_pr_info("************************************************\r\n"); + + while (1) + { + /* Poll the driver, get any outstanding frames */ + ethernetif_input(&gnetif); + + /* Handle all system timeouts for all core protocols */ + sys_check_timeouts(); + + /* Print DHCP progress */ + if(print_dhcp_state(&gnetif)) + { + sscanf(ipaddr_ntoa(&gnetif.ip_addr), "%d.%d.%d.%d", &lwip_ipaddr[0], &lwip_ipaddr[1], + &lwip_ipaddr[2], &lwip_ipaddr[3]); + + sscanf(ipaddr_ntoa(&gnetif.netmask), "%d.%d.%d.%d", &lwip_netmask[0], &lwip_netmask[1], + &lwip_netmask[2], &lwip_netmask[3]); + + sscanf(ipaddr_ntoa(&gnetif.gw), "%d.%d.%d.%d", &lwip_gwaddr[0], &lwip_gwaddr[1], + &lwip_gwaddr[2], &lwip_gwaddr[3]); + + break; + } + } + + is_lwip_test = 0; +} + + +SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0), + getip, lwip_dhcp_test, DHCP_Test); + +#endif diff --git a/Ubiquitous/XiUOS/resources/ethernet/test/lwip_ping_demo.c b/Ubiquitous/XiUOS/resources/ethernet/test/lwip_ping_demo.c new file mode 100755 index 000000000..63379a12d --- /dev/null +++ b/Ubiquitous/XiUOS/resources/ethernet/test/lwip_ping_demo.c @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2016, Freescale Semiconductor, Inc. + * Copyright 2016-2019 NXP + * All rights reserved. + * + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +/* + * Copyright (c) 2021 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 lwip_ping_demo.c + * @brief Demo for ping function + * @version 1.0 + * @author AIIT XUOS Lab + * @date 2021.12.15 + */ + + +/******************************************************************************* + * Includes + ******************************************************************************/ + +#include "lwip/opt.h" + +#if LWIP_IPV4 && LWIP_RAW + +#include "ping.h" + +#include "lwip/timeouts.h" +#include "lwip/init.h" +#include "netif/ethernet.h" + +#include "board.h" +#include "pin_mux.h" +#include "clock_config.h" + +#include +#include +#include "connect_ethernet.h" + +/******************************************************************************* + * Definitions + ******************************************************************************/ + +/******************************************************************************* + * Prototypes + ******************************************************************************/ + +/******************************************************************************* + * Variables + ******************************************************************************/ + +char test_ip_addr[] = {192, 168, 250, 253}; +char test_net_mask[] = {255, 255, 255, 0}; +char test_gw_addr[] = {192, 168, 250, 252}; +ip4_addr_t ping_addr; + +/******************************************************************************* + * Code + ******************************************************************************/ + +static void *lwip_ping_test(void *param) +{ + IP4_ADDR(&ping_addr, lwip_gwaddr[0], lwip_gwaddr[1], lwip_gwaddr[2], lwip_gwaddr[3]); + ETH_BSP_Config(); + lwip_config_net(lwip_ipaddr, lwip_netmask, lwip_gwaddr); + ping_init(&ping_addr); +} + +void lwip_ping_thread(int argc, char *argv[]) +{ + int result = 0; + + if(argc >= 4) + { + lw_print("lw: [%s] ip %s mask %s gw %s\n", __func__, argv[1], argv[2], argv[3]); + sscanf(argv[1], "%d.%d.%d.%d", &lwip_ipaddr[0], &lwip_ipaddr[1], &lwip_ipaddr[2], &lwip_ipaddr[3]); + sscanf(argv[2], "%d.%d.%d.%d", &lwip_netmask[0], &lwip_netmask[1], &lwip_netmask[2], &lwip_netmask[3]); + sscanf(argv[3], "%d.%d.%d.%d", &lwip_gwaddr[0], &lwip_gwaddr[1], &lwip_gwaddr[2], &lwip_gwaddr[3]); + } + else if(argc == 2) + { + lw_print("lw: [%s] gw %s\n", __func__, argv[1]); + sscanf(argv[1], "%d.%d.%d.%d", &lwip_gwaddr[0], &lwip_gwaddr[1], &lwip_gwaddr[2], &lwip_gwaddr[3]); + } + + lw_print("lw: [%s] argc %d\n", __func__, argc); + + IP4_ADDR(&ping_addr, lwip_gwaddr[0], lwip_gwaddr[1], lwip_gwaddr[2], lwip_gwaddr[3]); + ETH_BSP_Config(); + lwip_config_net(lwip_ipaddr, lwip_netmask, lwip_gwaddr); + ping_init(&ping_addr); +} + +SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3), + ping, lwip_ping_thread, ping [IP] 3 times); + +int lwip_dns_test(int argc, char **argv) +{ + if (argc <= 1) + { + lw_pr_info("Please input: ping \n"); + return 0; + } + + get_url_ip(argv[1]); + return 0; +} +SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(2), + dns, lwip_dns_test, dns [url]); + +#endif diff --git a/APP_Framework/Applications/connection_app/lwip_demo/lwip_tcp_demo.c b/Ubiquitous/XiUOS/resources/ethernet/test/lwip_tcp_demo.c similarity index 100% rename from APP_Framework/Applications/connection_app/lwip_demo/lwip_tcp_demo.c rename to Ubiquitous/XiUOS/resources/ethernet/test/lwip_tcp_demo.c diff --git a/APP_Framework/Applications/connection_app/lwip_demo/lwip_udp_demo.c b/Ubiquitous/XiUOS/resources/ethernet/test/lwip_udp_demo.c similarity index 100% rename from APP_Framework/Applications/connection_app/lwip_demo/lwip_udp_demo.c rename to Ubiquitous/XiUOS/resources/ethernet/test/lwip_udp_demo.c diff --git a/Ubiquitous/XiUOS/resources/ethernet/test/ping.c b/Ubiquitous/XiUOS/resources/ethernet/test/ping.c new file mode 100755 index 000000000..925f174ba --- /dev/null +++ b/Ubiquitous/XiUOS/resources/ethernet/test/ping.c @@ -0,0 +1,544 @@ +/** + * @file + * Ping sender module + * + */ + +/* + * 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. + * + */ + +/** + * This is an example of a "ping" sender (with raw API and socket API). + * It can be used as a start point to maintain opened a network connection, or + * like a network "watchdog" for your device. + * + */ + +#include +#include "lwip/opt.h" + +#if LWIP_RAW /* don't build if not configured for use in lwipopts.h */ + +#include "ping.h" + +#include "lwip/mem.h" +#include "lwip/raw.h" +#include "lwip/icmp.h" +#include "lwip/netif.h" +#include "lwip/sys.h" +#include "lwip/timeouts.h" +#include "lwip/inet_chksum.h" +#include "lwip/prot/ip4.h" +#include "lwip/netdb.h" +#include "lwip/prot/ip.h" + +#if PING_USE_SOCKETS +#include "lwip/sockets.h" +#include "lwip/inet.h" +#include +#endif /* PING_USE_SOCKETS */ + + +/** + * PING_DEBUG: Enable debugging for PING. + */ +#ifndef PING_DEBUG +#define PING_DEBUG LWIP_DBG_ON +#endif + +/** ping receive timeout - in milliseconds */ +#ifndef PING_RCV_TIMEO +#define PING_RCV_TIMEO 2000 +#endif + +/** ping delay - in milliseconds */ +#ifndef PING_DELAY +#define PING_DELAY 1000 +#endif + +/** ping identifier - must fit on a u16_t */ +#ifndef PING_ID +#define PING_ID 0xAFAF +#endif + +/** ping additional data size to include in the packet */ +#ifndef PING_DATA_SIZE +#define PING_DATA_SIZE 32 +#endif + +/** ping result action - no default action */ +#ifndef PING_RESULT +#define PING_RESULT(ping_ok) +#endif + +/* ping variables */ +static const ip_addr_t* ping_target; +static u16_t ping_seq_num; +#ifdef LWIP_DEBUG +static u32_t ping_time; +#endif /* LWIP_DEBUG */ +#if !PING_USE_SOCKETS +static struct raw_pcb *ping_pcb; +#endif /* PING_USE_SOCKETS */ + +#define PING_THREAD_STACKSIZE 4096 +#define PING_THREAD_PRIO 15 + + +/** Prepare a echo ICMP request */ +static void +ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len) +{ + size_t i; + size_t data_len = len - sizeof(struct icmp_echo_hdr); + + ICMPH_TYPE_SET(iecho, ICMP_ECHO); + ICMPH_CODE_SET(iecho, 0); + iecho->chksum = 0; + iecho->id = PING_ID; + iecho->seqno = lwip_htons(++ping_seq_num); + + /* fill the additional data buffer with some data */ + for(i = 0; i < data_len; i++) { + ((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i; + } + + iecho->chksum = inet_chksum(iecho, len); +} + +#if PING_USE_SOCKETS + +/* Ping using the socket ip */ +static err_t +ping_send(int s, const ip_addr_t *addr) +{ + int err; + struct icmp_echo_hdr *iecho; + struct sockaddr_storage to; + size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE; + LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff); + +#if LWIP_IPV6 + if(IP_IS_V6(addr) && !ip6_addr_isipv4mappedipv6(ip_2_ip6(addr))) { + /* todo: support ICMP6 echo */ + return ERR_VAL; + } +#endif /* LWIP_IPV6 */ + + iecho = (struct icmp_echo_hdr *)mem_malloc((mem_size_t)ping_size); + if (!iecho) { + return ERR_MEM; + } + + ping_prepare_echo(iecho, (u16_t)ping_size); + +#if LWIP_IPV4 + if(IP_IS_V4(addr)) { + struct sockaddr_in *to4 = (struct sockaddr_in*)&to; + to4->sin_len = sizeof(to4); + to4->sin_family = AF_INET; + inet_addr_from_ip4addr(&to4->sin_addr, ip_2_ip4(addr)); + } +#endif /* LWIP_IPV4 */ + +#if LWIP_IPV6 + if(IP_IS_V6(addr)) { + struct sockaddr_in6 *to6 = (struct sockaddr_in6*)&to; + to6->sin6_len = sizeof(to6); + to6->sin6_family = AF_INET6; + inet6_addr_from_ip6addr(&to6->sin6_addr, ip_2_ip6(addr)); + } +#endif /* LWIP_IPV6 */ + + err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*)&to, sizeof(to)); + + mem_free(iecho); + + return (err ? ERR_OK : ERR_VAL); +} + +static void +ping_recv(int s) +{ + char buf[64]; + int len; + struct sockaddr_storage from; + int fromlen = sizeof(from); + + while((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0) { + if (len >= (int)(sizeof(struct ip_hdr)+sizeof(struct icmp_echo_hdr))) { + ip_addr_t fromaddr; + memset(&fromaddr, 0, sizeof(fromaddr)); + +#if LWIP_IPV4 + if(from.ss_family == AF_INET) { + struct sockaddr_in *from4 = (struct sockaddr_in*)&from; + inet_addr_to_ip4addr(ip_2_ip4(&fromaddr), &from4->sin_addr); + IP_SET_TYPE_VAL(fromaddr, IPADDR_TYPE_V4); + } +#endif /* LWIP_IPV4 */ + +#if LWIP_IPV6 + if(from.ss_family == AF_INET6) { + struct sockaddr_in6 *from6 = (struct sockaddr_in6*)&from; + inet6_addr_to_ip6addr(ip_2_ip6(&fromaddr), &from6->sin6_addr); + IP_SET_TYPE_VAL(fromaddr, IPADDR_TYPE_V6); + } +#endif /* LWIP_IPV6 */ + + LWIP_DEBUGF( PING_DEBUG, ("ping: recv ")); + ip_addr_debug_print_val(PING_DEBUG, fromaddr); + LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", (sys_now() - ping_time))); + + /* todo: support ICMP6 echo */ +#if LWIP_IPV4 + if (IP_IS_V4_VAL(fromaddr)) { + struct ip_hdr *iphdr; + struct icmp_echo_hdr *iecho; + + iphdr = (struct ip_hdr *)buf; + iecho = (struct icmp_echo_hdr *)(buf + (IPH_HL(iphdr) * 4)); + if ((iecho->id == PING_ID) && (iecho->seqno == lwip_htons(ping_seq_num))) { + /* do some ping result processing */ + PING_RESULT((ICMPH_TYPE(iecho) == ICMP_ER)); + return; + } else { + LWIP_DEBUGF( PING_DEBUG, ("ping: drop\n")); + } + } +#endif /* LWIP_IPV4 */ + } + fromlen = sizeof(from); + } + + if (len == 0) { + LWIP_DEBUGF( PING_DEBUG, ("ping: recv - %"U32_F" ms - timeout\n", (sys_now()-ping_time))); + } + + /* do some ping result processing */ + PING_RESULT(0); +} + +static void +ping_thread(void *arg) +{ + int s; + int ret; + int cnt = TEST_LWIP_TIMES; + +#if LWIP_SO_SNDRCVTIMEO_NONSTANDARD + int timeout = PING_RCV_TIMEO; +#else + struct timeval timeout; + timeout.tv_sec = PING_RCV_TIMEO/1000; + timeout.tv_usec = (PING_RCV_TIMEO%1000)*1000; +#endif + LWIP_UNUSED_ARG(arg); + + lw_print("lw: [%s] start...\n", __func__); + +#if LWIP_IPV6 + if(IP_IS_V4(ping_target) || ip6_addr_isipv4mappedipv6(ip_2_ip6(ping_target))) { + s = lwip_socket(AF_INET6, SOCK_RAW, IP_PROTO_ICMP); + } else { + s = lwip_socket(AF_INET6, SOCK_RAW, IP6_NEXTH_ICMP6); + } +#else + s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP); +#endif + if (s < 0) { + lw_print("lw: [%s] ping failed %d!\n", __func__, s); + return; + } + + lw_print("lw: [%s] ping start!\n", __func__); + + ret = lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); + LWIP_ASSERT("setting receive timeout failed", ret != 0); + LWIP_UNUSED_ARG(ret); + + while (cnt --) { + if (ping_send(s, ping_target) == ERR_OK) { + LWIP_DEBUGF( PING_DEBUG, ("ping: send ")); + ip_addr_debug_print(PING_DEBUG, ping_target); + LWIP_DEBUGF( PING_DEBUG, ("\n")); + +#ifdef LWIP_DEBUG + ping_time = sys_now(); +#endif /* LWIP_DEBUG */ + ping_recv(s); + } else { + LWIP_DEBUGF( PING_DEBUG, ("ping: send ")); + ip_addr_debug_print(PING_DEBUG, ping_target); + LWIP_DEBUGF( PING_DEBUG, (" - error\n")); + } + sys_msleep(PING_DELAY); + } + lwip_close(s); +} + +#else /* PING_USE_SOCKETS */ + +/* Ping using the raw ip */ +static u8_t +ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *addr) +{ + struct icmp_echo_hdr *iecho; + LWIP_UNUSED_ARG(arg); + LWIP_UNUSED_ARG(pcb); + LWIP_UNUSED_ARG(addr); + LWIP_ASSERT("p != NULL", p != NULL); + + if ((p->tot_len >= (PBUF_IP_HLEN + sizeof(struct icmp_echo_hdr))) && + pbuf_remove_header(p, PBUF_IP_HLEN) == 0) { + iecho = (struct icmp_echo_hdr *)p->payload; + + if ((iecho->id == PING_ID) && (iecho->seqno == lwip_htons(ping_seq_num))) { + LWIP_DEBUGF( PING_DEBUG, ("ping: recv ")); + ip_addr_debug_print(PING_DEBUG, addr); + LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", (sys_now()-ping_time))); + + /* do some ping result processing */ + PING_RESULT(1); + pbuf_free(p); + return 1; /* eat the packet */ + } + /* not eaten, restore original packet */ + /* Changed to the "_force" version because of LPC zerocopy pbufs */ + pbuf_add_header_force(p, PBUF_IP_HLEN); + } + + return 0; /* don't eat the packet */ +} + +static void +ping_send(struct raw_pcb *raw, const ip_addr_t *addr) +{ + struct pbuf *p; + struct icmp_echo_hdr *iecho; + size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE; + + LWIP_DEBUGF( PING_DEBUG, ("ping: send ")); + ip_addr_debug_print(PING_DEBUG, addr); + LWIP_DEBUGF( PING_DEBUG, ("\n")); + LWIP_ASSERT("ping_size <= 0xffff", ping_size <= 0xffff); + + p = pbuf_alloc(PBUF_IP, (u16_t)ping_size, PBUF_RAM); + if (!p) { + return; + } + if ((p->len == p->tot_len) && (p->next == NULL)) { + iecho = (struct icmp_echo_hdr *)p->payload; + + ping_prepare_echo(iecho, (u16_t)ping_size); + + raw_sendto(raw, p, addr); +#ifdef LWIP_DEBUG + ping_time = sys_now(); +#endif /* LWIP_DEBUG */ + } + pbuf_free(p); +} + +static void +ping_timeout(void *arg) +{ + static int cnt = TEST_LWIP_TIMES; + struct raw_pcb *pcb = (struct raw_pcb*)arg; + + LWIP_ASSERT("ping_timeout: no pcb given!", pcb != NULL); + + ping_send(pcb, ping_target); + + if(cnt -- > 0) + { + sys_timeout(PING_DELAY, ping_timeout, pcb); + } + else + { + cnt = TEST_LWIP_TIMES; + } +} + +static void +ping_raw_init(void) +{ + ping_pcb = raw_new(IP_PROTO_ICMP); + LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL); + + raw_recv(ping_pcb, ping_recv, NULL); + raw_bind(ping_pcb, IP_ADDR_ANY); + sys_timeout(PING_DELAY, ping_timeout, ping_pcb); +} + +void +ping_send_now(void) +{ + LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL); + ping_send(ping_pcb, ping_target); +} + +#endif /* PING_USE_SOCKETS */ + +void +ping_init(const ip_addr_t* ping_addr) +{ + sys_thread_t th; + + ping_target = ping_addr; + +#if PING_USE_SOCKETS + th = sys_thread_new("ping_thread", ping_thread, NULL, PING_THREAD_STACKSIZE, PING_THREAD_PRIO); + lw_print("lw: [%s] new thread %d addr %#x\n", __func__, th, (*ping_addr).addr); +#else /* PING_USE_SOCKETS */ + ping_raw_init(); +#endif /* PING_USE_SOCKETS */ +} + +int lwip_ping_send(int s, ip_addr_t *addr, int size) +{ + int err; + struct icmp_echo_hdr *iecho; + struct sockaddr_in to; + int ping_size = sizeof(struct icmp_echo_hdr) + size; + LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff); + + iecho = malloc(ping_size); + if (iecho == NULL) + { + return ERR_MEM; + } + + ping_prepare_echo(iecho, (u16_t) ping_size); + + to.sin_len = sizeof(to); + to.sin_family = AF_INET; +#if LWIP_IPV4 && LWIP_IPV6 + to.sin_addr.s_addr = addr->u_addr.ip4.addr; +#elif LWIP_IPV4 + to.sin_addr.s_addr = addr->addr; +#elif LWIP_IPV6 +#error Not supported IPv6. +#endif + + err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*) &to, sizeof(to)); + free(iecho); + + return (err == ping_size ? ERR_OK : ERR_VAL); +} + +int lwip_ping_recv(int s, int *ttl) +{ + char buf[64]; + int fromlen = sizeof(struct sockaddr_in), len; + struct sockaddr_in from; + struct ip_hdr *iphdr; + struct icmp_echo_hdr *iecho; + + while ((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*) &from, (socklen_t*) &fromlen)) > 0) + { + if (len >= (int)(sizeof(struct ip_hdr) + sizeof(struct icmp_echo_hdr))) + { + iphdr = (struct ip_hdr *) buf; + iecho = (struct icmp_echo_hdr *) (buf + (IPH_HL(iphdr) * 4)); + if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num))) + { + *ttl = iphdr->_ttl; + return len; + } + } + } + + return len; +} + +int get_url_ip(char* url) +{ +#if LWIP_VERSION_MAJOR >= 2U + struct timeval timeout = { PING_RCV_TIMEO / TICK_PER_SECOND, PING_RCV_TIMEO % TICK_PER_SECOND }; +#else + int timeout = PING_RCV_TIMEO * 1000UL / TICK_PER_SECOND; +#endif + int cnt = TEST_LWIP_TIMES; + + int s, ttl, recv_len; + ip_addr_t target_addr; + struct addrinfo hint, *res = NULL; + struct sockaddr_in *h = NULL; + struct in_addr ina; + + memset(&hint, 0, sizeof(hint)); + /* convert URL to IP */ + if (lwip_getaddrinfo(url, NULL, &hint, &res) != 0) + { + lw_pr_info("ping: unknown host %s\n", url); + return -1; + } + memcpy(&h, &res->ai_addr, sizeof(struct sockaddr_in *)); + memcpy(&ina, &h->sin_addr, sizeof(ina)); + lwip_freeaddrinfo(res); + if (inet_aton(inet_ntoa(ina), &target_addr) == 0) + { + lw_pr_info("ping: unknown host %s\n", url); + return -2; + } + /* new a socket */ + if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) + { + lw_pr_info("ping: create socket failed\n"); + return -3; + } + + lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); + + while (cnt --) + { + if (ping_send(s, &target_addr) == ERR_OK) + { +#ifdef LWIP_DEBUG + ping_time = sys_now(); +#endif /* LWIP_DEBUG */ + if ((recv_len = lwip_ping_recv(s, &ttl)) >= 0) + { + lw_pr_info("%d bytes from %s icmp_seq=%d ttl=%d time=%d ms\n", recv_len, inet_ntoa(ina), cnt, + ttl, sys_now() - ping_time); + } + else + { + lw_pr_info("From %s icmp_seq=%d timeout\n", inet_ntoa(ina), cnt); + } + } + + sys_msleep(PING_DELAY); + } + + lwip_close(s); + return 0; +} + + +#endif /* LWIP_RAW */ diff --git a/Ubiquitous/XiUOS/resources/ethernet/test/ping.h b/Ubiquitous/XiUOS/resources/ethernet/test/ping.h new file mode 100755 index 000000000..109bca9c5 --- /dev/null +++ b/Ubiquitous/XiUOS/resources/ethernet/test/ping.h @@ -0,0 +1,21 @@ +#ifndef LWIP_PING_H +#define LWIP_PING_H + +#include "lwip/ip_addr.h" + +/** + * PING_USE_SOCKETS: Set to 1 to use sockets, otherwise the raw api is used + */ +#ifndef PING_USE_SOCKETS +#define PING_USE_SOCKETS LWIP_SOCKET +#endif + +void ping_init(const ip_addr_t* ping_addr); + +#if !PING_USE_SOCKETS +void ping_send_now(void); +#endif /* !PING_USE_SOCKETS */ + +int get_url_ip(char* url); + +#endif /* LWIP_PING_H */ diff --git a/APP_Framework/Applications/connection_app/lwip_demo/tcpecho_raw.c b/Ubiquitous/XiUOS/resources/ethernet/test/tcpecho_raw.c similarity index 100% rename from APP_Framework/Applications/connection_app/lwip_demo/tcpecho_raw.c rename to Ubiquitous/XiUOS/resources/ethernet/test/tcpecho_raw.c diff --git a/APP_Framework/Applications/connection_app/lwip_demo/tcpecho_raw.h b/Ubiquitous/XiUOS/resources/ethernet/test/tcpecho_raw.h similarity index 100% rename from APP_Framework/Applications/connection_app/lwip_demo/tcpecho_raw.h rename to Ubiquitous/XiUOS/resources/ethernet/test/tcpecho_raw.h diff --git a/APP_Framework/Applications/connection_app/lwip_demo/udp_echo.c b/Ubiquitous/XiUOS/resources/ethernet/test/udp_echo.c similarity index 86% rename from APP_Framework/Applications/connection_app/lwip_demo/udp_echo.c rename to Ubiquitous/XiUOS/resources/ethernet/test/udp_echo.c index 218933c03..cd5d711db 100755 --- a/APP_Framework/Applications/connection_app/lwip_demo/udp_echo.c +++ b/Ubiquitous/XiUOS/resources/ethernet/test/udp_echo.c @@ -73,7 +73,7 @@ char* udp_send_msg = "\n\nThis one is UDP pkg. Congratulations on you.\n\n"; static void UdpEchoThreadServer(void *arg) { - KPrintf("UdpEchoThreadServer start.\n"); + lw_print("UdpEchoThreadServer start.\n"); int sock = -1; char *recv_data; @@ -86,14 +86,14 @@ static void UdpEchoThreadServer(void *arg) recv_data = (char *)malloc(RECV_DATA); if (recv_data == NULL) { - KPrintf("No memory\n"); - goto __exit; + lw_print("No memory\n"); + goto __exit; } sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { - KPrintf("Socket error\n"); + lw_print("Socket error\n"); goto __exit; } @@ -104,12 +104,12 @@ static void UdpEchoThreadServer(void *arg) if (bind(sock, (struct sockaddr *)&udp_addr, sizeof(struct sockaddr)) == -1) { - KPrintf("Unable to bind\n"); + lw_print("Unable to bind\n"); goto __exit; } - KPrintf("UDP bind sucess, start to receive.\n"); - KPrintf("\n\nLocal Port:%d\n\n", LOCAL_PORT_SERVER); + lw_print("UDP bind sucess, start to receive.\n"); + lw_print("\n\nLocal Port:%d\n\n", LOCAL_PORT_SERVER); while(1) { @@ -119,9 +119,9 @@ static void UdpEchoThreadServer(void *arg) (struct sockaddr*)&seraddr, &addrlen); - KPrintf("Receive from : %s\n",inet_ntoa(seraddr.sin_addr)); + lw_print("Receive from : %s\n",inet_ntoa(seraddr.sin_addr)); - KPrintf("Recevce data : %s\n\n",recv_data); + lw_print("Recevce data : %s\n\n",recv_data); sendto(sock,recv_data, recv_data_len,0, @@ -138,13 +138,13 @@ __exit: static void UdpEchoThreadClient(void *arg) { int cnt = TEST_LWIP_TIMES; - KPrintf("UdpEchoThreadClient start.\n"); + 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) { - KPrintf("Socket error\n"); + lw_print("Socket error\n"); goto __exit; } @@ -156,21 +156,21 @@ static void UdpEchoThreadClient(void *arg) if (connect(sock_udp_send_once, (struct sockaddr *)&udp_sock, sizeof(struct sockaddr))) { - KPrintf("Unable to connect\n"); + lw_print("Unable to connect\n"); goto __exit; } - KPrintf("UDP connect success, start to send.\n"); - KPrintf("\n\nTarget Port:%d\n\n", udp_sock.sin_port); + lw_print("UDP connect success, start to send.\n"); + lw_print("\n\nTarget Port:%d\n\n", udp_sock.sin_port); while (cnt --) { lw_print("UDP Client is running.\n"); - sendto(sock_udp_send_once,udp_send_msg, - strlen(udp_send_msg),0, - (struct sockaddr*)&udp_sock, - sizeof(struct sockaddr)); + 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); diff --git a/APP_Framework/Applications/connection_app/lwip_demo/udp_echo.h b/Ubiquitous/XiUOS/resources/ethernet/test/udp_echo.h similarity index 100% rename from APP_Framework/Applications/connection_app/lwip_demo/udp_echo.h rename to Ubiquitous/XiUOS/resources/ethernet/test/udp_echo.h