From 2147d9212130c2450bac27e37d0bb3e77d897614 Mon Sep 17 00:00:00 2001 From: lr <1234@qq.com> Date: Thu, 23 May 2024 11:26:36 +0800 Subject: [PATCH] complie and debug test_net --- Ubiquitous/XiZi_AIoT/services/app/Makefile | 2 +- Ubiquitous/XiZi_AIoT/services/app/test_net.c | 8 +- .../services/net/libnet/lwip_service.c | 12 +- .../services/net/libnet/lwip_service.h | 13 +- .../services/net/net_server/Makefile | 1 + .../services/net/net_server/arch/ethernetif.c | 28 ++- .../services/net/net_server/arch/lwipopts.h | 183 ++++++++++++++++++ .../net/net_server/include/lwip/arch.h | 3 +- .../XiZi_AIoT/services/net/net_server/lwip.mk | 1 + 9 files changed, 229 insertions(+), 22 deletions(-) diff --git a/Ubiquitous/XiZi_AIoT/services/app/Makefile b/Ubiquitous/XiZi_AIoT/services/app/Makefile index 86051c9ff..4c3c16d96 100644 --- a/Ubiquitous/XiZi_AIoT/services/app/Makefile +++ b/Ubiquitous/XiZi_AIoT/services/app/Makefile @@ -108,7 +108,7 @@ test_priority: test_priority.o libserial.o printf.o usyscall.o arch_usyscall.o l @${objdump} -S $@ > $@.asm test_net: test_net.o lwip_service.o libipc.o session.o libserial.o printf.o usyscall.o arch_usyscall.o libmem.o - @${ld} ${user_ldflags} -e main -o $@ $^ ${board_specs} + @${ld} ${user_ldflags} -e main -o $@ $^ ${board_specs} -llwip -L$(KERNEL_ROOT)/services/net/net_server @${objdump} -S $@ > $@.asm %.o: %.c diff --git a/Ubiquitous/XiZi_AIoT/services/app/test_net.c b/Ubiquitous/XiZi_AIoT/services/app/test_net.c index 1ecca6edb..424948855 100644 --- a/Ubiquitous/XiZi_AIoT/services/app/test_net.c +++ b/Ubiquitous/XiZi_AIoT/services/app/test_net.c @@ -33,19 +33,23 @@ int main(int argc, char* argv[]) struct Session sess; connect_session(&sess, "LWIPServer", 4096); + printf("%s %d\n", __func__, __LINE__); + fd = ipc_socket(&sess, AF_INET, SOCK_DGRAM, 0); if(fd < 0) { printf("Socket error\n"); return 0; } + printf("%s %d\n", __func__, __LINE__); + struct sockaddr_in udp_sock; udp_sock.sin_family = AF_INET; udp_sock.sin_port = htons(udp_socket_port); udp_sock.sin_addr.s_addr = inet_addr(udp_ip_str); memset(&(udp_sock.sin_zero), 0, sizeof(udp_sock.sin_zero)); - + printf("%s %d\n", __func__, __LINE__); if(ipc_connect(&sess, fd, (struct sockaddr *)&udp_sock, sizeof(struct sockaddr)) < 0) { printf("Unable to connect %s:%d\n", udp_ip_str, udp_socket_port); @@ -53,6 +57,8 @@ int main(int argc, char* argv[]) return 0; } + printf("%s %d\n", __func__, __LINE__); + printf("UDP connect %s:%d success, start to send.\n", udp_ip_str, udp_socket_port); diff --git a/Ubiquitous/XiZi_AIoT/services/net/libnet/lwip_service.c b/Ubiquitous/XiZi_AIoT/services/net/libnet/lwip_service.c index 312d464a5..a6b1cb50f 100644 --- a/Ubiquitous/XiZi_AIoT/services/net/libnet/lwip_service.c +++ b/Ubiquitous/XiZi_AIoT/services/net/libnet/lwip_service.c @@ -18,12 +18,12 @@ int ipc_socket(struct Session* session, int domain, int type, int protocol){ } IPC_INTERFACE(Ipc_bind, 3, s, name, namelen, sizeof(int), sizeof(struct sockaddr), sizeof(socklen_t)); -int ipc_bind(struct Session* session, int s, const struct sockaddr *name, socklen_t namelen){ +int ipc_bind(struct Session* session, int s, struct sockaddr *name, socklen_t namelen){ return IPC_CALL(Ipc_bind)(session, &s, name, &namelen); } IPC_INTERFACE(Ipc_connect, 3, s, name, namelen, sizeof(int), sizeof(struct sockaddr), sizeof(socklen_t)); -int ipc_connect(struct Session* session, int s, const struct sockaddr *name, socklen_t namelen){ +int ipc_connect(struct Session* session, int s, struct sockaddr *name, socklen_t namelen){ return IPC_CALL(Ipc_connect)(session, &s, name, &namelen); } @@ -53,17 +53,17 @@ ssize_t ipc_recvfrom(struct Session* session, int s, void *mem, size_t len, int } IPC_INTERFACE(Ipc_sendto, 6, s, data, size, flags, to, tolen, sizeof(int), *(size_t *)size, sizeof(size_t), sizeof(int), sizeof(struct sockaddr), sizeof(socklen_t)); -ssize_t ipc_sendto(struct Session* session, int s, const void *data, size_t size, int flags, const struct sockaddr *to, socklen_t tolen){ +ssize_t ipc_sendto(struct Session* session, int s, void *data, size_t size, int flags, struct sockaddr *to, socklen_t tolen){ return IPC_CALL(Ipc_sendto)(session, &s, data, &size, &flags, to, &tolen); } IPC_INTERFACE(Ipc_send, 4, s, data, size, flags, sizeof(int), *(size_t *)size, sizeof(size_t), sizeof(int)); -ssize_t ipc_send(struct Session* session, int s, const void *data, size_t size, int flags){ +ssize_t ipc_send(struct Session* session, int s, void *data, size_t size, int flags){ return IPC_CALL(Ipc_send)(session, &s, data, &size, &flags); } IPC_INTERFACE(Ipc_write, 3, s, data, size, sizeof(int), *(size_t *)size, sizeof(size_t)); -ssize_t ipc_write(struct Session* session, int s, const void *data, size_t size){ +ssize_t ipc_write(struct Session* session, int s, void *data, size_t size){ return IPC_CALL(Ipc_write)(session, &s, data, &size); } @@ -73,6 +73,6 @@ int ipc_close(struct Session* session, int s){ } IPC_INTERFACE(Ipc_setsockopt, 5, s, level, optname, optval, optlen, sizeof(int), sizeof(int), sizeof(int), *(socklen_t*)optlen, sizeof(socklen_t)); -int ipc_setsockopt(struct Session* session, int s, int level, int optname, const void *optval, socklen_t optlen){ +int ipc_setsockopt(struct Session* session, int s, int level, int optname, void *optval, socklen_t optlen){ return IPC_CALL(Ipc_setsockopt)(session, &s, &level, &optname, optval, &optlen); } \ No newline at end of file diff --git a/Ubiquitous/XiZi_AIoT/services/net/libnet/lwip_service.h b/Ubiquitous/XiZi_AIoT/services/net/libnet/lwip_service.h index bd8dc71ce..11fa77db1 100644 --- a/Ubiquitous/XiZi_AIoT/services/net/libnet/lwip_service.h +++ b/Ubiquitous/XiZi_AIoT/services/net/libnet/lwip_service.h @@ -19,10 +19,9 @@ IPC_SERVICES(IpcLWIPServer, Ipc_socket, Ipc_bind,Ipc_connect,Ipc_listen,Ipc_acce int ipc_socket(struct Session* session, int domain, int type, int protocol); -int ipc_bind(struct Session* session, int s, - const struct sockaddr *name, socklen_t namelen); +int ipc_bind(struct Session* session, int s, struct sockaddr *name, socklen_t namelen); -int ipc_connect(struct Session* session, int s, const struct sockaddr *name, socklen_t namelen); +int ipc_connect(struct Session* session, int s, struct sockaddr *name, socklen_t namelen); int ipc_listen(struct Session* session, int s, int backlog); @@ -34,12 +33,12 @@ ssize_t ipc_recv(struct Session* session, int s, void *mem, size_t len, int flag ssize_t ipc_recvfrom(struct Session* session, int s, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen); -ssize_t ipc_sendto(struct Session* session, int s, const void *data, size_t size, int flags, const struct sockaddr *to, socklen_t tolen); +ssize_t ipc_sendto(struct Session* session, int s, void *data, size_t size, int flags, struct sockaddr *to, socklen_t tolen); -ssize_t ipc_send(struct Session* session, int s, const void *data, size_t size, int flags); +ssize_t ipc_send(struct Session* session, int s, void *data, size_t size, int flags); -ssize_t ipc_write(struct Session* session, int s, const void *data, size_t size); +ssize_t ipc_write(struct Session* session, int s, void *data, size_t size); int ipc_close(struct Session* session, int s); -int ipc_setsockopt(struct Session* session, int s, int level, int optname, const void *optval, socklen_t optlen); +int ipc_setsockopt(struct Session* session, int s, int level, int optname, void *optval, socklen_t optlen); diff --git a/Ubiquitous/XiZi_AIoT/services/net/net_server/Makefile b/Ubiquitous/XiZi_AIoT/services/net/net_server/Makefile index 67f7a17e8..aeade75e7 100644 --- a/Ubiquitous/XiZi_AIoT/services/net/net_server/Makefile +++ b/Ubiquitous/XiZi_AIoT/services/net/net_server/Makefile @@ -56,6 +56,7 @@ libsem = $(KERNEL_ROOT)/services/app/libsemaphore.o lwip: COMPILER lwip_server.o | bin @${ld} ${user_ldflags} -e main -o $@ ${sys} ${api} ${core} ${ipv4} ${netif} lwip_server.o ${libserial} ${printf} ${libmem} ${usyscall} ${arch_usyscall} ${session} ${libipc} ${libsem} ${board_specs} @${objdump} -S $@ > $@.asm + @$(ar) -r liblwip.a ${sys} ${api} ${core} ${ipv4} ${netif} @mv *.o bin @mv $@ $(KERNEL_ROOT)/services/app @mv $@.asm $(KERNEL_ROOT)/services/app diff --git a/Ubiquitous/XiZi_AIoT/services/net/net_server/arch/ethernetif.c b/Ubiquitous/XiZi_AIoT/services/net/net_server/arch/ethernetif.c index 8a2fb4d6b..91d0c91fa 100644 --- a/Ubiquitous/XiZi_AIoT/services/net/net_server/arch/ethernetif.c +++ b/Ubiquitous/XiZi_AIoT/services/net/net_server/arch/ethernetif.c @@ -51,7 +51,7 @@ static void low_level_init(struct netif *netif) netif->mtu = 1500; #if LWIP_ARP - netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP; + netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP ; #else netif->flags |= NETIF_FLAG_BROADCAST; #endif /* LWIP_ARP */ @@ -60,6 +60,26 @@ static void low_level_init(struct netif *netif) } +void print_packet_data(const char *packet_data, int length) { + printf("Packet Data:\n"); + for (int i = 0; i < length; ++i) { + printf("%02X ", packet_data[i]); // 打印十六进制值,每个字节两位 + if ((i + 1) % 16 == 0 || i == length - 1) { // 每16个字节一行 + for (int j = 0; j < 15 - (i % 16); ++j) { + printf(" "); // 打印空格对齐 + } + printf("| "); + for (int j = i - (i % 16); j <= i; ++j) { + if (packet_data[j] >= 32 && packet_data[j] <= 126) { + printf("%c", packet_data[j]); // 如果是可打印的ASCII字符,打印对应字符 + } else { + printf("."); // 否则打印点表示不可打印字符 + } + } + printf("\n"); + } + } +} /** * This function should do the actual transmission of the packet. The packet is * contained in the pbuf that is passed to the function. This pbuf @@ -85,7 +105,7 @@ static err_t low_level_output(struct netif *netif, struct pbuf *p) pbuf_copy_partial(p, buf, p->tot_len, 0); - printf("output %s\n", buf); + print_packet_data(buf, p->tot_len); return ERR_OK; } @@ -114,12 +134,8 @@ static struct pbuf* low_level_input(struct netif *netif) */ void ethernetif_input(struct netif *netif) { - struct ethernetif *ethernetif; - struct eth_hdr *ethhdr; struct pbuf *p; - ethernetif = netif->state; - /* move received packet into a new pbuf */ p = low_level_input(netif); /* if no packet could be read, silently ignore this */ diff --git a/Ubiquitous/XiZi_AIoT/services/net/net_server/arch/lwipopts.h b/Ubiquitous/XiZi_AIoT/services/net/net_server/arch/lwipopts.h index ce3be76ac..b61aac282 100644 --- a/Ubiquitous/XiZi_AIoT/services/net/net_server/arch/lwipopts.h +++ b/Ubiquitous/XiZi_AIoT/services/net/net_server/arch/lwipopts.h @@ -223,5 +223,188 @@ ---------- Lwip Debug options ---------- ---------------------------------------- */ +#ifndef LWIP_DEBUG +#define LWIP_DEBUG 1 +#define LWIP_SOCKET_DEBUG +#define LWIP_SOCKETS_DEBUG +#define LWIP_TCPIP_DEBUG +#define LWIP_MEMP_DEBUG +#define LWIP_PBUF_DEBUG +#define LWIP_TCP_INPUT_DEBUG +#define LWIP_TCP_OUTPUT_DEBUG +#define LWIP_NETIF_DEBUG +#define LWIP_ETHARP_DEBUG +#define LWIP_API_MSG_DEBUG +#endif +#ifdef LWIP_DEBUG +#ifdef LWIP_SYS_DEBUG +#define SYS_DEBUG LWIP_DBG_ON +#else +#define SYS_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_ETHARP_DEBUG +#define ETHARP_DEBUG LWIP_DBG_ON +#else +#define ETHARP_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_PPP_DEBUG +#define PPP_DEBUG LWIP_DBG_ON +#else +#define PPP_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_MEM_DEBUG +#define MEM_DEBUG LWIP_DBG_ON +#else +#define MEM_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_MEMP_DEBUG +#define MEMP_DEBUG LWIP_DBG_ON +#else +#define MEMP_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_PBUF_DEBUG +#define PBUF_DEBUG LWIP_DBG_ON +#else +#define PBUF_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_API_LIB_DEBUG +#define API_LIB_DEBUG LWIP_DBG_ON +#else +#define API_LIB_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_API_MSG_DEBUG +#define API_MSG_DEBUG LWIP_DBG_ON +#else +#define API_MSG_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_TCPIP_DEBUG +#define TCPIP_DEBUG LWIP_DBG_ON +#else +#define TCPIP_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_NETIF_DEBUG +#define NETIF_DEBUG LWIP_DBG_ON +#else +#define NETIF_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_SOCKETS_DEBUG +#define SOCKETS_DEBUG LWIP_DBG_ON +#else +#define SOCKETS_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_DNS_DEBUG +#define DNS_DEBUG LWIP_DBG_ON +#else +#define DNS_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_AUTOIP_DEBUG +#define AUTOIP_DEBUG LWIP_DBG_ON +#else +#define AUTOIP_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_DHCP_DEBUG +#define DHCP_DEBUG LWIP_DBG_ON +#else +#define DHCP_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_IP_DEBUG +#define IP_DEBUG LWIP_DBG_ON +#else +#define IP_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_IP_REASS_DEBUG +#define IP_REASS_DEBUG LWIP_DBG_ON +#else +#define IP_REASS_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_ICMP_DEBUG +#define ICMP_DEBUG LWIP_DBG_ON +#else +#define ICMP_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_IGMP_DEBUG +#define IGMP_DEBUG LWIP_DBG_ON +#else +#define IGMP_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_UDP_DEBUG +#define UDP_DEBUG LWIP_DBG_ON +#else +#define UDP_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_TCP_DEBUG +#define TCP_DEBUG LWIP_DBG_ON +#else +#define TCP_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_TCP_INPUT_DEBUG +#define TCP_INPUT_DEBUG LWIP_DBG_ON +#else +#define TCP_INPUT_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_TCP_OUTPUT_DEBUG +#define TCP_OUTPUT_DEBUG LWIP_DBG_ON +#else +#define TCP_OUTPUT_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_TCP_RTO_DEBUG +#define TCP_RTO_DEBUG LWIP_DBG_ON +#else +#define TCP_RTO_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_TCP_CWND_DEBUG +#define TCP_CWND_DEBUG LWIP_DBG_ON +#else +#define TCP_CWND_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_TCP_WND_DEBUG +#define TCP_WND_DEBUG LWIP_DBG_ON +#else +#define TCP_WND_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_TCP_FR_DEBUG +#define TCP_FR_DEBUG LWIP_DBG_ON +#else +#define TCP_FR_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_TCP_QLEN_DEBUG +#define TCP_QLEN_DEBUG LWIP_DBG_ON +#else +#define TCP_QLEN_DEBUG LWIP_DBG_OFF +#endif + +#ifdef LWIP_TCP_RST_DEBUG +#define TCP_RST_DEBUG LWIP_DBG_ON +#else +#define TCP_RST_DEBUG LWIP_DBG_OFF +#endif + +#endif /* LWIP_DEBUG */ #endif /* LWIP_LWIPOPTS_H */ diff --git a/Ubiquitous/XiZi_AIoT/services/net/net_server/include/lwip/arch.h b/Ubiquitous/XiZi_AIoT/services/net/net_server/include/lwip/arch.h index 1235f80f9..ed09c50d3 100644 --- a/Ubiquitous/XiZi_AIoT/services/net/net_server/include/lwip/arch.h +++ b/Ubiquitous/XiZi_AIoT/services/net/net_server/include/lwip/arch.h @@ -46,6 +46,7 @@ #endif #include "arch/cc.h" +#include "libserial.h" /** * @defgroup compiler_abstraction Compiler/platform abstraction @@ -78,7 +79,7 @@ * systems, this should be defined to something less resource-consuming. */ #ifndef LWIP_PLATFORM_DIAG -#define LWIP_PLATFORM_DIAG(x) do {printf(x);} while(0) +#define LWIP_PLATFORM_DIAG(x) do {printf x;} while(0) // #include #include #endif diff --git a/Ubiquitous/XiZi_AIoT/services/net/net_server/lwip.mk b/Ubiquitous/XiZi_AIoT/services/net/net_server/lwip.mk index 3c3b7b336..035409694 100644 --- a/Ubiquitous/XiZi_AIoT/services/net/net_server/lwip.mk +++ b/Ubiquitous/XiZi_AIoT/services/net/net_server/lwip.mk @@ -13,6 +13,7 @@ endif cc = ${toolchain}gcc ld = ${toolchain}g++ objdump = ${toolchain}objdump +ar = ${toolchain}ar c_useropts = -O2