diff --git a/Ubiquitous/XiZi_IIoT/board/edu-arm32/third_party_driver/ethernet/eth_netdev.c b/Ubiquitous/XiZi_IIoT/board/edu-arm32/third_party_driver/ethernet/eth_netdev.c index 145ebc620..1c5c9d3c5 100644 --- a/Ubiquitous/XiZi_IIoT/board/edu-arm32/third_party_driver/ethernet/eth_netdev.c +++ b/Ubiquitous/XiZi_IIoT/board/edu-arm32/third_party_driver/ethernet/eth_netdev.c @@ -1,3 +1,41 @@ +/* + * 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. + * + * Author: Adam Dunkels + */ + +/** +* @file eth_netdev.c +* @brief register net dev function for lwip +* @version 3.0 +* @author AIIT XUOS Lab +* @date 2023-08-07 +*/ #include #include diff --git a/Ubiquitous/XiZi_IIoT/board/edu-arm32/third_party_driver/include/connect_ethernet.h b/Ubiquitous/XiZi_IIoT/board/edu-arm32/third_party_driver/include/connect_ethernet.h index 888efff7b..4f0529a0b 100644 --- a/Ubiquitous/XiZi_IIoT/board/edu-arm32/third_party_driver/include/connect_ethernet.h +++ b/Ubiquitous/XiZi_IIoT/board/edu-arm32/third_party_driver/include/connect_ethernet.h @@ -61,6 +61,9 @@ int32_t low_level_init(struct netif* netif); err_t low_level_output(struct netif* netif, struct pbuf* p); struct pbuf* low_level_input(struct netif* netif); +int lwip_netdev_add(struct netif* lwip_netif); +void lwip_netdev_del(struct netif* lwip_netif); + int HwEthInit(void); #ifdef __cplusplus diff --git a/Ubiquitous/XiZi_IIoT/board/imxrt1176-sbc/third_party_driver/ethernet/Makefile b/Ubiquitous/XiZi_IIoT/board/imxrt1176-sbc/third_party_driver/ethernet/Makefile index 1df7f37e7..968713ab4 100755 --- a/Ubiquitous/XiZi_IIoT/board/imxrt1176-sbc/third_party_driver/ethernet/Makefile +++ b/Ubiquitous/XiZi_IIoT/board/imxrt1176-sbc/third_party_driver/ethernet/Makefile @@ -1,3 +1,3 @@ -SRC_FILES := enet_ethernetif.c enet_ethernetif_kinetis.c fsl_enet.c fsl_enet_qos.c +SRC_FILES := enet_ethernetif.c enet_ethernetif_kinetis.c fsl_enet.c fsl_enet_qos.c eth_netdev.c SRC_DIR := phy mdio include $(KERNEL_ROOT)/compiler.mk \ No newline at end of file diff --git a/Ubiquitous/XiZi_IIoT/board/imxrt1176-sbc/third_party_driver/ethernet/enet_ethernetif.c b/Ubiquitous/XiZi_IIoT/board/imxrt1176-sbc/third_party_driver/ethernet/enet_ethernetif.c index b7bf34f03..ac69b5cc0 100644 --- a/Ubiquitous/XiZi_IIoT/board/imxrt1176-sbc/third_party_driver/ethernet/enet_ethernetif.c +++ b/Ubiquitous/XiZi_IIoT/board/imxrt1176-sbc/third_party_driver/ethernet/enet_ethernetif.c @@ -353,5 +353,11 @@ err_t ethernetif_init(struct netif *netif, } #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */ + SYS_KDEBUG_LOG(NETDEV_DEBUG, ("[%s] Adding netdev.\n", __func__)); + if (EOK != lwip_netdev_add(netif)) { + SYS_KDEBUG_LOG(NETDEV_DEBUG, ("[%s] LWIP add netdev failed.\n", __func__)); + } else { + printf("[%s] Add Netdev successful\n", __func__); + } return ERR_OK; } diff --git a/Ubiquitous/XiZi_IIoT/board/imxrt1176-sbc/third_party_driver/ethernet/eth_netdev.c b/Ubiquitous/XiZi_IIoT/board/imxrt1176-sbc/third_party_driver/ethernet/eth_netdev.c new file mode 100644 index 000000000..03ca27594 --- /dev/null +++ b/Ubiquitous/XiZi_IIoT/board/imxrt1176-sbc/third_party_driver/ethernet/eth_netdev.c @@ -0,0 +1,201 @@ +/* + * 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. + * + * Author: Adam Dunkels + */ + +/** +* @file eth_netdev.c +* @brief register net dev function for lwip +* @version 3.0 +* @author AIIT XUOS Lab +* @date 2023-08-07 +*/ + +#include +#include +#include +#include +#include +#include + +static const uint32_t NETIF_NAME_LEN = 2; + +static int lwip_netdev_set_up(struct netdev* netdev) +{ + netif_set_up((struct netif*)netdev->user_data); + return ERR_OK; +} + +static int lwip_netdev_set_down(struct netdev* netif) +{ + netif_set_down((struct netif*)netif->user_data); + return ERR_OK; +} + +#ifndef ip_2_ip4 +#define ip_2_ip4(ipaddr) (ipaddr) +#endif +static int lwip_netdev_set_addr_info(struct netdev* netdev, ip_addr_t* ip_addr, ip_addr_t* netmask, ip_addr_t* gw) +{ + if (ip_addr && netmask && gw) { + netif_set_addr((struct netif*)netdev->user_data, ip_2_ip4(ip_addr), ip_2_ip4(netmask), ip_2_ip4(gw)); + } else { + if (ip_addr) { + netif_set_ipaddr((struct netif*)netdev->user_data, ip_2_ip4(ip_addr)); + } + if (netmask) { + netif_set_netmask((struct netif*)netdev->user_data, ip_2_ip4(netmask)); + } + if (gw) { + netif_set_gw((struct netif*)netdev->user_data, ip_2_ip4(gw)); + } + } +} + +#ifdef LWIP_DNS +static int lwip_netdev_set_dns_server(struct netdev* netdev, uint8_t dns_num, ip_addr_t* dns_server) +{ +#if LWIP_VERSION_MAJOR == 1U /* v1.x */ + extern void dns_setserver(u8_t numdns, ip_addr_t * dnsserver); +#else /* >=2.x */ + extern void dns_setserver(uint8_t dns_num, const ip_addr_t* dns_server); +#endif /* LWIP_VERSION_MAJOR == 1U */ + + dns_setserver(dns_num, dns_server); + return ERR_OK; +} +#endif + +#ifdef LWIP_DHCP +static int lwip_netdev_set_dhcp(struct netdev* netdev, bool is_enabled) +{ + netdev_low_level_set_dhcp_status(netdev, is_enabled); + + if (true == is_enabled) { + dhcp_start((struct netif*)netdev->user_data); + } else { + dhcp_stop((struct netif*)netdev->user_data); + } + + return ERR_OK; +} +#endif + +static int lwip_netdev_set_default(struct netdev* netdev) +{ + netif_set_default((struct netif*)netdev->user_data); + return ERR_OK; +} + +static const struct netdev_ops lwip_netdev_ops = { + .set_up = lwip_netdev_set_up, + .set_down = lwip_netdev_set_down, + .set_addr_info = lwip_netdev_set_addr_info, +#ifdef LWIP_DNS + .set_dns_server = lwip_netdev_set_dns_server, +#endif +#ifdef LWIP_DHCP + .set_dhcp = lwip_netdev_set_dhcp, +#endif + .set_default = lwip_netdev_set_default, +}; + +static inline int netdev_set_flags(struct netif* lwip_netif) +{ + CHECK(lwip_netif); + struct netdev* netdev = netdev_get_by_name(lwip_netif->name); + if (netdev == NULL) { + return -ERR_IF; + } + + netdev->mtu = lwip_netif->mtu; + // set flags + if (lwip_netif->flags | NETIF_FLAG_BROADCAST) { + netdev->flags |= NETDEV_FLAG_BROADCAST; + } + if (lwip_netif->flags | NETIF_FLAG_ETHARP) { + netdev->flags |= NETDEV_FLAG_ETHARP; + } + if (lwip_netif->flags | NETIF_FLAG_IGMP) { + netdev->flags |= NETDEV_FLAG_IGMP; + } +#if LWIP_VERSION_MAJOR >= 2U /* >= v2.x */ + if (lwip_netif->flags & NETIF_FLAG_MLD6) { + netdev->flags |= NETDEV_FLAG_MLD6; + } +#endif /* LWIP_VERSION_MAJOR >= 2U */ + +#if LWIP_DHCP + netdev_low_level_set_dhcp_status(netdev, true); +#else + netdev_low_level_set_dhcp_status(netdev, false); +#endif + + return ERR_OK; +} + +int lwip_netdev_add(struct netif* lwip_netif) +{ + CHECK(lwip_netif); + + struct netdev* netdev = calloc(1, sizeof(struct netdev)); + if (netdev == NULL) { + return -ERR_IF; + } + + // init netdev + char netif_name[NETIF_NAME_LEN + 1]; + SYS_KDEBUG_LOG(NETDEV_DEBUG, ("[%s] Lwip netif name: %s\n", __func__, lwip_netif->name)); + strncpy(netif_name, lwip_netif->name, NETIF_NAME_LEN); + // register netdev + int result = netdev_register(netdev, netif_name, (void*)lwip_netif); + // set values of netdev + netdev_set_flags(lwip_netif); + netdev->ops = &lwip_netdev_ops; + netdev->hwaddr_len = lwip_netif->hwaddr_len; + memcpy(netdev->hwaddr, lwip_netif->hwaddr, lwip_netif->hwaddr_len); + netdev->ip_addr = lwip_netif->ip_addr; + netdev->gw = lwip_netif->gw; + netdev->netmask = lwip_netif->netmask; + + return result; +} + +void lwip_netdev_del(struct netif* lwip_netif) +{ + char name[NETIF_NAME_LEN + 1]; + struct netdev* netdev; + + CHECK(lwip_netif); + + strncpy(name, lwip_netif->name, NETIF_NAME_LEN); + netdev = netdev_get_by_name(name); + netdev_unregister(netdev); + free(netdev); +} \ No newline at end of file diff --git a/Ubiquitous/XiZi_IIoT/board/imxrt1176-sbc/third_party_driver/include/connect_ethernet.h b/Ubiquitous/XiZi_IIoT/board/imxrt1176-sbc/third_party_driver/include/connect_ethernet.h index 12f65358b..0933eecd5 100755 --- a/Ubiquitous/XiZi_IIoT/board/imxrt1176-sbc/third_party_driver/include/connect_ethernet.h +++ b/Ubiquitous/XiZi_IIoT/board/imxrt1176-sbc/third_party_driver/include/connect_ethernet.h @@ -33,6 +33,8 @@ #define sourceClock CLOCK_GetFreq(kCLOCK_CoreSysClk) #endif +int lwip_netdev_add(struct netif* lwip_netif); +void lwip_netdev_del(struct netif* lwip_netif); #ifdef __cplusplus } diff --git a/Ubiquitous/XiZi_IIoT/board/imxrt1176-sbc/third_party_driver/include/enet_ethernetif.h b/Ubiquitous/XiZi_IIoT/board/imxrt1176-sbc/third_party_driver/include/enet_ethernetif.h index 6c552e28c..e8fb06718 100644 --- a/Ubiquitous/XiZi_IIoT/board/imxrt1176-sbc/third_party_driver/include/enet_ethernetif.h +++ b/Ubiquitous/XiZi_IIoT/board/imxrt1176-sbc/third_party_driver/include/enet_ethernetif.h @@ -164,6 +164,9 @@ void ethernetif_input( void *netif_arg); int ETH_BSP_Config(void); void *ethernetif_config_enet_set(uint8_t enet_port); +int lwip_netdev_add(struct netif* lwip_netif); +void lwip_netdev_del(struct netif* lwip_netif); + #if defined(__cplusplus) } #endif /* __cplusplus */ diff --git a/Ubiquitous/XiZi_IIoT/board/ok1052-c/third_party_driver/ethernet/Makefile b/Ubiquitous/XiZi_IIoT/board/ok1052-c/third_party_driver/ethernet/Makefile index 14be72829..57c50e98b 100755 --- a/Ubiquitous/XiZi_IIoT/board/ok1052-c/third_party_driver/ethernet/Makefile +++ b/Ubiquitous/XiZi_IIoT/board/ok1052-c/third_party_driver/ethernet/Makefile @@ -1,3 +1,3 @@ -SRC_FILES := enet_ethernetif.c enet_ethernetif_kinetis.c fsl_enet.c +SRC_FILES := enet_ethernetif.c enet_ethernetif_kinetis.c fsl_enet.c eth_netdev.c SRC_DIR := ksz8081 include $(KERNEL_ROOT)/compiler.mk \ No newline at end of file diff --git a/Ubiquitous/XiZi_IIoT/board/ok1052-c/third_party_driver/ethernet/enet_ethernetif.c b/Ubiquitous/XiZi_IIoT/board/ok1052-c/third_party_driver/ethernet/enet_ethernetif.c index 9b8ca475a..114f1ef61 100755 --- a/Ubiquitous/XiZi_IIoT/board/ok1052-c/third_party_driver/ethernet/enet_ethernetif.c +++ b/Ubiquitous/XiZi_IIoT/board/ok1052-c/third_party_driver/ethernet/enet_ethernetif.c @@ -68,6 +68,7 @@ #include "fsl_gpio.h" #include "fsl_iomuxc.h" +#include "netdev.h" #include "sys_arch.h" /******************************************************************************* @@ -319,6 +320,12 @@ err_t ethernetif_init(struct netif *netif, struct ethernetif *ethernetif, } #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */ + SYS_KDEBUG_LOG(NETDEV_DEBUG, ("[%s] Adding netdev.\n", __func__)); + if (EOK != lwip_netdev_add(netif)) { + SYS_KDEBUG_LOG(NETDEV_DEBUG, ("[%s] LWIP add netdev failed.\n", __func__)); + } else { + printf("[%s] Add Netdev successful\n", __func__); + } return ERR_OK; } diff --git a/Ubiquitous/XiZi_IIoT/board/ok1052-c/third_party_driver/ethernet/eth_netdev.c b/Ubiquitous/XiZi_IIoT/board/ok1052-c/third_party_driver/ethernet/eth_netdev.c new file mode 100644 index 000000000..03ca27594 --- /dev/null +++ b/Ubiquitous/XiZi_IIoT/board/ok1052-c/third_party_driver/ethernet/eth_netdev.c @@ -0,0 +1,201 @@ +/* + * 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. + * + * Author: Adam Dunkels + */ + +/** +* @file eth_netdev.c +* @brief register net dev function for lwip +* @version 3.0 +* @author AIIT XUOS Lab +* @date 2023-08-07 +*/ + +#include +#include +#include +#include +#include +#include + +static const uint32_t NETIF_NAME_LEN = 2; + +static int lwip_netdev_set_up(struct netdev* netdev) +{ + netif_set_up((struct netif*)netdev->user_data); + return ERR_OK; +} + +static int lwip_netdev_set_down(struct netdev* netif) +{ + netif_set_down((struct netif*)netif->user_data); + return ERR_OK; +} + +#ifndef ip_2_ip4 +#define ip_2_ip4(ipaddr) (ipaddr) +#endif +static int lwip_netdev_set_addr_info(struct netdev* netdev, ip_addr_t* ip_addr, ip_addr_t* netmask, ip_addr_t* gw) +{ + if (ip_addr && netmask && gw) { + netif_set_addr((struct netif*)netdev->user_data, ip_2_ip4(ip_addr), ip_2_ip4(netmask), ip_2_ip4(gw)); + } else { + if (ip_addr) { + netif_set_ipaddr((struct netif*)netdev->user_data, ip_2_ip4(ip_addr)); + } + if (netmask) { + netif_set_netmask((struct netif*)netdev->user_data, ip_2_ip4(netmask)); + } + if (gw) { + netif_set_gw((struct netif*)netdev->user_data, ip_2_ip4(gw)); + } + } +} + +#ifdef LWIP_DNS +static int lwip_netdev_set_dns_server(struct netdev* netdev, uint8_t dns_num, ip_addr_t* dns_server) +{ +#if LWIP_VERSION_MAJOR == 1U /* v1.x */ + extern void dns_setserver(u8_t numdns, ip_addr_t * dnsserver); +#else /* >=2.x */ + extern void dns_setserver(uint8_t dns_num, const ip_addr_t* dns_server); +#endif /* LWIP_VERSION_MAJOR == 1U */ + + dns_setserver(dns_num, dns_server); + return ERR_OK; +} +#endif + +#ifdef LWIP_DHCP +static int lwip_netdev_set_dhcp(struct netdev* netdev, bool is_enabled) +{ + netdev_low_level_set_dhcp_status(netdev, is_enabled); + + if (true == is_enabled) { + dhcp_start((struct netif*)netdev->user_data); + } else { + dhcp_stop((struct netif*)netdev->user_data); + } + + return ERR_OK; +} +#endif + +static int lwip_netdev_set_default(struct netdev* netdev) +{ + netif_set_default((struct netif*)netdev->user_data); + return ERR_OK; +} + +static const struct netdev_ops lwip_netdev_ops = { + .set_up = lwip_netdev_set_up, + .set_down = lwip_netdev_set_down, + .set_addr_info = lwip_netdev_set_addr_info, +#ifdef LWIP_DNS + .set_dns_server = lwip_netdev_set_dns_server, +#endif +#ifdef LWIP_DHCP + .set_dhcp = lwip_netdev_set_dhcp, +#endif + .set_default = lwip_netdev_set_default, +}; + +static inline int netdev_set_flags(struct netif* lwip_netif) +{ + CHECK(lwip_netif); + struct netdev* netdev = netdev_get_by_name(lwip_netif->name); + if (netdev == NULL) { + return -ERR_IF; + } + + netdev->mtu = lwip_netif->mtu; + // set flags + if (lwip_netif->flags | NETIF_FLAG_BROADCAST) { + netdev->flags |= NETDEV_FLAG_BROADCAST; + } + if (lwip_netif->flags | NETIF_FLAG_ETHARP) { + netdev->flags |= NETDEV_FLAG_ETHARP; + } + if (lwip_netif->flags | NETIF_FLAG_IGMP) { + netdev->flags |= NETDEV_FLAG_IGMP; + } +#if LWIP_VERSION_MAJOR >= 2U /* >= v2.x */ + if (lwip_netif->flags & NETIF_FLAG_MLD6) { + netdev->flags |= NETDEV_FLAG_MLD6; + } +#endif /* LWIP_VERSION_MAJOR >= 2U */ + +#if LWIP_DHCP + netdev_low_level_set_dhcp_status(netdev, true); +#else + netdev_low_level_set_dhcp_status(netdev, false); +#endif + + return ERR_OK; +} + +int lwip_netdev_add(struct netif* lwip_netif) +{ + CHECK(lwip_netif); + + struct netdev* netdev = calloc(1, sizeof(struct netdev)); + if (netdev == NULL) { + return -ERR_IF; + } + + // init netdev + char netif_name[NETIF_NAME_LEN + 1]; + SYS_KDEBUG_LOG(NETDEV_DEBUG, ("[%s] Lwip netif name: %s\n", __func__, lwip_netif->name)); + strncpy(netif_name, lwip_netif->name, NETIF_NAME_LEN); + // register netdev + int result = netdev_register(netdev, netif_name, (void*)lwip_netif); + // set values of netdev + netdev_set_flags(lwip_netif); + netdev->ops = &lwip_netdev_ops; + netdev->hwaddr_len = lwip_netif->hwaddr_len; + memcpy(netdev->hwaddr, lwip_netif->hwaddr, lwip_netif->hwaddr_len); + netdev->ip_addr = lwip_netif->ip_addr; + netdev->gw = lwip_netif->gw; + netdev->netmask = lwip_netif->netmask; + + return result; +} + +void lwip_netdev_del(struct netif* lwip_netif) +{ + char name[NETIF_NAME_LEN + 1]; + struct netdev* netdev; + + CHECK(lwip_netif); + + strncpy(name, lwip_netif->name, NETIF_NAME_LEN); + netdev = netdev_get_by_name(name); + netdev_unregister(netdev); + free(netdev); +} \ No newline at end of file diff --git a/Ubiquitous/XiZi_IIoT/board/ok1052-c/third_party_driver/include/connect_ethernet.h b/Ubiquitous/XiZi_IIoT/board/ok1052-c/third_party_driver/include/connect_ethernet.h index 12f65358b..0933eecd5 100755 --- a/Ubiquitous/XiZi_IIoT/board/ok1052-c/third_party_driver/include/connect_ethernet.h +++ b/Ubiquitous/XiZi_IIoT/board/ok1052-c/third_party_driver/include/connect_ethernet.h @@ -33,6 +33,8 @@ #define sourceClock CLOCK_GetFreq(kCLOCK_CoreSysClk) #endif +int lwip_netdev_add(struct netif* lwip_netif); +void lwip_netdev_del(struct netif* lwip_netif); #ifdef __cplusplus } diff --git a/Ubiquitous/XiZi_IIoT/board/ok1052-c/third_party_driver/include/enet_ethernetif.h b/Ubiquitous/XiZi_IIoT/board/ok1052-c/third_party_driver/include/enet_ethernetif.h index 7b8c20c95..889b50acb 100755 --- a/Ubiquitous/XiZi_IIoT/board/ok1052-c/third_party_driver/include/enet_ethernetif.h +++ b/Ubiquitous/XiZi_IIoT/board/ok1052-c/third_party_driver/include/enet_ethernetif.h @@ -181,11 +181,14 @@ err_t ethernetif1_init(struct netif *netif); */ void ethernetif_input( void *netif_arg); -void ETH_BSP_Config(void); +int ETH_BSP_Config(void); void *ethernetif_config_enet_set(uint8_t enet_port); int32 lwip_obtain_semaphore(struct netif *netif); +int lwip_netdev_add(struct netif* lwip_netif); +void lwip_netdev_del(struct netif* lwip_netif); + #define NETIF_ENET0_INIT_FUNC ethernetif0_init #if defined(__cplusplus) diff --git a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/ethernet/eth_netdev.c b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/ethernet/eth_netdev.c index 2dfe75795..03ca27594 100644 --- a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/ethernet/eth_netdev.c +++ b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/ethernet/eth_netdev.c @@ -1,3 +1,41 @@ +/* + * 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. + * + * Author: Adam Dunkels + */ + +/** +* @file eth_netdev.c +* @brief register net dev function for lwip +* @version 3.0 +* @author AIIT XUOS Lab +* @date 2023-08-07 +*/ #include #include diff --git a/Ubiquitous/XiZi_IIoT/board/xiwangtong-arm32/third_party_driver/ethernet/Makefile b/Ubiquitous/XiZi_IIoT/board/xiwangtong-arm32/third_party_driver/ethernet/Makefile index 65f5adc78..6cd8a10fa 100755 --- a/Ubiquitous/XiZi_IIoT/board/xiwangtong-arm32/third_party_driver/ethernet/Makefile +++ b/Ubiquitous/XiZi_IIoT/board/xiwangtong-arm32/third_party_driver/ethernet/Makefile @@ -1,3 +1,3 @@ -SRC_FILES := enet_ethernetif.c enet_ethernetif_kinetis.c fsl_enet.c +SRC_FILES := enet_ethernetif.c enet_ethernetif_kinetis.c fsl_enet.c eth_netdev.c SRC_DIR := lan8720 include $(KERNEL_ROOT)/compiler.mk \ No newline at end of file diff --git a/Ubiquitous/XiZi_IIoT/board/xiwangtong-arm32/third_party_driver/ethernet/enet_ethernetif.c b/Ubiquitous/XiZi_IIoT/board/xiwangtong-arm32/third_party_driver/ethernet/enet_ethernetif.c index 9b8ca475a..114f1ef61 100755 --- a/Ubiquitous/XiZi_IIoT/board/xiwangtong-arm32/third_party_driver/ethernet/enet_ethernetif.c +++ b/Ubiquitous/XiZi_IIoT/board/xiwangtong-arm32/third_party_driver/ethernet/enet_ethernetif.c @@ -68,6 +68,7 @@ #include "fsl_gpio.h" #include "fsl_iomuxc.h" +#include "netdev.h" #include "sys_arch.h" /******************************************************************************* @@ -319,6 +320,12 @@ err_t ethernetif_init(struct netif *netif, struct ethernetif *ethernetif, } #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */ + SYS_KDEBUG_LOG(NETDEV_DEBUG, ("[%s] Adding netdev.\n", __func__)); + if (EOK != lwip_netdev_add(netif)) { + SYS_KDEBUG_LOG(NETDEV_DEBUG, ("[%s] LWIP add netdev failed.\n", __func__)); + } else { + printf("[%s] Add Netdev successful\n", __func__); + } return ERR_OK; } diff --git a/Ubiquitous/XiZi_IIoT/board/xiwangtong-arm32/third_party_driver/ethernet/eth_netdev.c b/Ubiquitous/XiZi_IIoT/board/xiwangtong-arm32/third_party_driver/ethernet/eth_netdev.c new file mode 100644 index 000000000..03ca27594 --- /dev/null +++ b/Ubiquitous/XiZi_IIoT/board/xiwangtong-arm32/third_party_driver/ethernet/eth_netdev.c @@ -0,0 +1,201 @@ +/* + * 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. + * + * Author: Adam Dunkels + */ + +/** +* @file eth_netdev.c +* @brief register net dev function for lwip +* @version 3.0 +* @author AIIT XUOS Lab +* @date 2023-08-07 +*/ + +#include +#include +#include +#include +#include +#include + +static const uint32_t NETIF_NAME_LEN = 2; + +static int lwip_netdev_set_up(struct netdev* netdev) +{ + netif_set_up((struct netif*)netdev->user_data); + return ERR_OK; +} + +static int lwip_netdev_set_down(struct netdev* netif) +{ + netif_set_down((struct netif*)netif->user_data); + return ERR_OK; +} + +#ifndef ip_2_ip4 +#define ip_2_ip4(ipaddr) (ipaddr) +#endif +static int lwip_netdev_set_addr_info(struct netdev* netdev, ip_addr_t* ip_addr, ip_addr_t* netmask, ip_addr_t* gw) +{ + if (ip_addr && netmask && gw) { + netif_set_addr((struct netif*)netdev->user_data, ip_2_ip4(ip_addr), ip_2_ip4(netmask), ip_2_ip4(gw)); + } else { + if (ip_addr) { + netif_set_ipaddr((struct netif*)netdev->user_data, ip_2_ip4(ip_addr)); + } + if (netmask) { + netif_set_netmask((struct netif*)netdev->user_data, ip_2_ip4(netmask)); + } + if (gw) { + netif_set_gw((struct netif*)netdev->user_data, ip_2_ip4(gw)); + } + } +} + +#ifdef LWIP_DNS +static int lwip_netdev_set_dns_server(struct netdev* netdev, uint8_t dns_num, ip_addr_t* dns_server) +{ +#if LWIP_VERSION_MAJOR == 1U /* v1.x */ + extern void dns_setserver(u8_t numdns, ip_addr_t * dnsserver); +#else /* >=2.x */ + extern void dns_setserver(uint8_t dns_num, const ip_addr_t* dns_server); +#endif /* LWIP_VERSION_MAJOR == 1U */ + + dns_setserver(dns_num, dns_server); + return ERR_OK; +} +#endif + +#ifdef LWIP_DHCP +static int lwip_netdev_set_dhcp(struct netdev* netdev, bool is_enabled) +{ + netdev_low_level_set_dhcp_status(netdev, is_enabled); + + if (true == is_enabled) { + dhcp_start((struct netif*)netdev->user_data); + } else { + dhcp_stop((struct netif*)netdev->user_data); + } + + return ERR_OK; +} +#endif + +static int lwip_netdev_set_default(struct netdev* netdev) +{ + netif_set_default((struct netif*)netdev->user_data); + return ERR_OK; +} + +static const struct netdev_ops lwip_netdev_ops = { + .set_up = lwip_netdev_set_up, + .set_down = lwip_netdev_set_down, + .set_addr_info = lwip_netdev_set_addr_info, +#ifdef LWIP_DNS + .set_dns_server = lwip_netdev_set_dns_server, +#endif +#ifdef LWIP_DHCP + .set_dhcp = lwip_netdev_set_dhcp, +#endif + .set_default = lwip_netdev_set_default, +}; + +static inline int netdev_set_flags(struct netif* lwip_netif) +{ + CHECK(lwip_netif); + struct netdev* netdev = netdev_get_by_name(lwip_netif->name); + if (netdev == NULL) { + return -ERR_IF; + } + + netdev->mtu = lwip_netif->mtu; + // set flags + if (lwip_netif->flags | NETIF_FLAG_BROADCAST) { + netdev->flags |= NETDEV_FLAG_BROADCAST; + } + if (lwip_netif->flags | NETIF_FLAG_ETHARP) { + netdev->flags |= NETDEV_FLAG_ETHARP; + } + if (lwip_netif->flags | NETIF_FLAG_IGMP) { + netdev->flags |= NETDEV_FLAG_IGMP; + } +#if LWIP_VERSION_MAJOR >= 2U /* >= v2.x */ + if (lwip_netif->flags & NETIF_FLAG_MLD6) { + netdev->flags |= NETDEV_FLAG_MLD6; + } +#endif /* LWIP_VERSION_MAJOR >= 2U */ + +#if LWIP_DHCP + netdev_low_level_set_dhcp_status(netdev, true); +#else + netdev_low_level_set_dhcp_status(netdev, false); +#endif + + return ERR_OK; +} + +int lwip_netdev_add(struct netif* lwip_netif) +{ + CHECK(lwip_netif); + + struct netdev* netdev = calloc(1, sizeof(struct netdev)); + if (netdev == NULL) { + return -ERR_IF; + } + + // init netdev + char netif_name[NETIF_NAME_LEN + 1]; + SYS_KDEBUG_LOG(NETDEV_DEBUG, ("[%s] Lwip netif name: %s\n", __func__, lwip_netif->name)); + strncpy(netif_name, lwip_netif->name, NETIF_NAME_LEN); + // register netdev + int result = netdev_register(netdev, netif_name, (void*)lwip_netif); + // set values of netdev + netdev_set_flags(lwip_netif); + netdev->ops = &lwip_netdev_ops; + netdev->hwaddr_len = lwip_netif->hwaddr_len; + memcpy(netdev->hwaddr, lwip_netif->hwaddr, lwip_netif->hwaddr_len); + netdev->ip_addr = lwip_netif->ip_addr; + netdev->gw = lwip_netif->gw; + netdev->netmask = lwip_netif->netmask; + + return result; +} + +void lwip_netdev_del(struct netif* lwip_netif) +{ + char name[NETIF_NAME_LEN + 1]; + struct netdev* netdev; + + CHECK(lwip_netif); + + strncpy(name, lwip_netif->name, NETIF_NAME_LEN); + netdev = netdev_get_by_name(name); + netdev_unregister(netdev); + free(netdev); +} \ No newline at end of file diff --git a/Ubiquitous/XiZi_IIoT/board/xiwangtong-arm32/third_party_driver/include/connect_ethernet.h b/Ubiquitous/XiZi_IIoT/board/xiwangtong-arm32/third_party_driver/include/connect_ethernet.h index ad4062e9a..360b28125 100755 --- a/Ubiquitous/XiZi_IIoT/board/xiwangtong-arm32/third_party_driver/include/connect_ethernet.h +++ b/Ubiquitous/XiZi_IIoT/board/xiwangtong-arm32/third_party_driver/include/connect_ethernet.h @@ -32,6 +32,9 @@ extern "C" { #define sourceClock CLOCK_GetFreq(kCLOCK_CoreSysClk) #endif +int lwip_netdev_add(struct netif* lwip_netif); +void lwip_netdev_del(struct netif* lwip_netif); + #ifdef __cplusplus } #endif diff --git a/Ubiquitous/XiZi_IIoT/board/xiwangtong-arm32/third_party_driver/include/enet_ethernetif.h b/Ubiquitous/XiZi_IIoT/board/xiwangtong-arm32/third_party_driver/include/enet_ethernetif.h index 7978b36b6..e6f59df3e 100755 --- a/Ubiquitous/XiZi_IIoT/board/xiwangtong-arm32/third_party_driver/include/enet_ethernetif.h +++ b/Ubiquitous/XiZi_IIoT/board/xiwangtong-arm32/third_party_driver/include/enet_ethernetif.h @@ -185,6 +185,9 @@ int ETH_BSP_Config(void); void *ethernetif_config_enet_set(uint8_t enet_port); int32 lwip_obtain_semaphore(struct netif *netif); +int lwip_netdev_add(struct netif* lwip_netif); +void lwip_netdev_del(struct netif* lwip_netif); + #define NETIF_ENET0_INIT_FUNC ethernetif0_init #if defined(__cplusplus) diff --git a/Ubiquitous/XiZi_IIoT/path_kernel.mk b/Ubiquitous/XiZi_IIoT/path_kernel.mk index 7b44f073e..1603864c8 100755 --- a/Ubiquitous/XiZi_IIoT/path_kernel.mk +++ b/Ubiquitous/XiZi_IIoT/path_kernel.mk @@ -88,6 +88,8 @@ KERNELPATHS += \ -I$(KERNEL_ROOT)/resources/ethernet/LwIP/include/lwip/priv \ -I$(KERNEL_ROOT)/resources/ethernet/LwIP/include/lwip/prot \ -I$(KERNEL_ROOT)/resources/ethernet/LwIP/arch + +KERNELPATHS += -I$(KERNEL_ROOT)/resources/include/netdev endif endif @@ -336,6 +338,8 @@ KERNELPATHS += \ -I$(KERNEL_ROOT)/resources/ethernet/LwIP/include/lwip/priv \ -I$(KERNEL_ROOT)/resources/ethernet/LwIP/include/lwip/prot \ -I$(KERNEL_ROOT)/resources/ethernet/LwIP/arch + +KERNELPATHS += -I$(KERNEL_ROOT)/resources/include/netdev endif endif @@ -367,6 +371,8 @@ KERNELPATHS += \ -I$(KERNEL_ROOT)/resources/ethernet/LwIP/include/lwip/priv \ -I$(KERNEL_ROOT)/resources/ethernet/LwIP/include/lwip/prot \ -I$(KERNEL_ROOT)/resources/ethernet/LwIP/arch + +KERNELPATHS += -I$(KERNEL_ROOT)/resources/include/netdev endif endif diff --git a/Ubiquitous/XiZi_IIoT/resources/ethernet/cmd_lwip/Makefile b/Ubiquitous/XiZi_IIoT/resources/ethernet/cmd_lwip/Makefile index 697c6c141..cd4019ebb 100755 --- a/Ubiquitous/XiZi_IIoT/resources/ethernet/cmd_lwip/Makefile +++ b/Ubiquitous/XiZi_IIoT/resources/ethernet/cmd_lwip/Makefile @@ -1,4 +1,4 @@ -SRC_FILES := ping.c lwip_ping_demo.c lwip_tcp_demo.c tcpecho_raw.c lwip_config_demo.c lwip_dhcp_demo.c iperf.c http_test.c +SRC_FILES := ping.c lwip_ping_demo.c lwip_tcp_demo.c lwip_udp_demo.c tcpecho_raw.c lwip_config_demo.c lwip_dhcp_demo.c iperf.c http_test.c # SRC_FILES := ping.c lwip_ping_demo.c lwip_tcp_demo.c lwip_udp_demo.c tcpecho_raw.c lwip_config_demo.c lwip_dhcp_demo.c iperf.c include $(KERNEL_ROOT)/compiler.mk diff --git a/Ubiquitous/XiZi_IIoT/resources/ethernet/cmd_lwip/iperf.c b/Ubiquitous/XiZi_IIoT/resources/ethernet/cmd_lwip/iperf.c index 29c947a18..e2389af3c 100644 --- a/Ubiquitous/XiZi_IIoT/resources/ethernet/cmd_lwip/iperf.c +++ b/Ubiquitous/XiZi_IIoT/resources/ethernet/cmd_lwip/iperf.c @@ -50,7 +50,7 @@ static void iperf_udp_client(void *thread_param) send_size = IPERF_BUFSZ > 1470 ? 1470 : IPERF_BUFSZ; sock = socket(PF_INET, SOCK_DGRAM, 0); - if(sock < 0){ + if(sock < 0) { KPrintf("[%s:%d] can't create socket! exit!\n", __FILE__, __LINE__); return; } @@ -60,21 +60,21 @@ static void iperf_udp_client(void *thread_param) server.sin_addr.s_addr = inet_addr(param.host); memset(&(server.sin_zero), 0, sizeof(server.sin_zero)); - if (connect(sock, (struct sockaddr *)&server, sizeof(struct sockaddr))){ + if (connect(sock, (struct sockaddr *)&server, sizeof(struct sockaddr))) { lw_error("Unable to connect\n"); closesocket(sock); return; } buffer = malloc(IPERF_BUFSZ); - if (buffer == NULL){ + if (buffer == NULL) { printf("[%s:%d] malloc failed\n", __FILE__, __LINE__); return; } memset(buffer, 0x00, IPERF_BUFSZ); KPrintf("iperf udp mode run...\n"); - while (param.mode != IPERF_MODE_STOP){ + while (param.mode != IPERF_MODE_STOP) { packet_count++; tick = CurrentTicksGain(); buffer[0] = htonl(packet_count); @@ -101,12 +101,12 @@ static void iperf_udp_server(void *thread_param) struct timeval timeout; buffer = malloc(IPERF_BUFSZ); - if (buffer == NULL){ + if (buffer == NULL) { return; } sock = socket(PF_INET, SOCK_DGRAM, 0); - if(sock < 0){ + if(sock < 0) { KPrintf("can't create socket! exit!"); return; } @@ -117,35 +117,34 @@ static void iperf_udp_server(void *thread_param) timeout.tv_sec = 2; timeout.tv_usec = 0; - if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) == -1){ + if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) == -1) { KPrintf("setsockopt failed!"); closesocket(sock); free(buffer); return; } - if (bind(sock, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) < 0){ + if (bind(sock, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) < 0) { KPrintf("iperf server bind failed! exit!"); closesocket(sock); free(buffer); return; } - while (param.mode != IPERF_MODE_STOP){ + while (param.mode != IPERF_MODE_STOP) { tick1 = CurrentTicksGain(); tick2 = tick1; lost = 0; total = 0; sentlen = 0; - while ((tick2 - tick1) < (TICK_PER_SECOND * 5)){ + while ((tick2 - tick1) < (TICK_PER_SECOND * 5)) { r_size = recvfrom(sock, buffer, IPERF_BUFSZ, 0, (struct sockaddr *)&sender, (socklen_t*)&sender_len); - if (r_size > 12){ + if (r_size > 12) { pcount = ntohl(buffer[0]); - if (last_pcount < pcount){ + if (last_pcount < pcount) { lost += pcount - last_pcount - 1; total += pcount - last_pcount; - } - else{ + } else { last_pcount = pcount; } last_pcount = pcount; @@ -153,7 +152,7 @@ static void iperf_udp_server(void *thread_param) } tick2 = CurrentTicksGain(); } - if (sentlen > 0){ + if (sentlen > 0) { long data; int integer, decimal; KTaskDescriptorType tid; @@ -186,10 +185,9 @@ static void iperf_client(void *thread_param) for (i = 0; i < IPERF_BUFSZ; i ++) send_buf[i] = i & 0xff; - while (param.mode != IPERF_MODE_STOP) - { + while (param.mode != IPERF_MODE_STOP) { sock = socket(AF_INET, SOCK_STREAM, 0); - if (sock < 0){ + if (sock < 0) { KPrintf("create socket failed!"); DelayKTask(TICK_PER_SECOND); continue; @@ -200,8 +198,8 @@ static void iperf_client(void *thread_param) addr.sin_addr.s_addr = inet_addr((char *)param.host); ret = connect(sock, (const struct sockaddr *)&addr, sizeof(addr)); - if (ret == -1){ - if (tips){ + if (ret == -1) { + if (tips) { KPrintf("Connect to iperf server faile, Waiting for the server to open!"); tips = 0; } @@ -224,9 +222,9 @@ static void iperf_client(void *thread_param) sentlen = 0; tick1 = CurrentTicksGain(); - while (param.mode != IPERF_MODE_STOP){ + while (param.mode != IPERF_MODE_STOP) { tick2 = CurrentTicksGain(); - if (tick2 - tick1 >= TICK_PER_SECOND * 5){ + if (tick2 - tick1 >= TICK_PER_SECOND * 5) { double speed; // int integer, decimal; KTaskDescriptorType tid; @@ -240,7 +238,7 @@ static void iperf_client(void *thread_param) } ret = send(sock, send_buf, IPERF_BUFSZ, 0); - if (ret > 0){ + if (ret > 0) { sentlen += ret; } @@ -264,7 +262,7 @@ struct sock_conn_cb { int parent_id; }; -void iperf_sever_worker(void* arg) { +void iperf_server_worker(void* arg) { struct sock_conn_cb *sccb = (struct sock_conn_cb *)arg; x_ticks_t tick1, tick2; @@ -287,7 +285,7 @@ void iperf_sever_worker(void* arg) { int cur_tid = GetKTaskDescriptor()->id.id; tick1 = CurrentTicksGain(); - while (param.mode != IPERF_MODE_STOP){ + while (param.mode != IPERF_MODE_STOP) { bytes_received = recv(sccb->connected, recv_data, IPERF_BUFSZ, 0); if (bytes_received == 0) { KPrintf("client disconnected (%s, %d)\n", @@ -336,7 +334,7 @@ void iperf_server_multithread(void *thread_param) struct timeval timeout; sock = socket(AF_INET, SOCK_STREAM, 0); - if (sock < 0){ + if (sock < 0) { KPrintf("[%s:%d] Socket error!\n", __FILE__, __LINE__); goto __exit; } @@ -346,12 +344,12 @@ void iperf_server_multithread(void *thread_param) server_addr.sin_addr.s_addr = INADDR_ANY; memset(&(server_addr.sin_zero), 0x0, sizeof(server_addr.sin_zero)); - if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1){ + if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) { KPrintf("Unable to bind!\n"); goto __exit; } - if (listen(sock, 5) == -1){ + if (listen(sock, 5) == -1) { KPrintf("Listen error!\n"); goto __exit; } @@ -360,7 +358,7 @@ void iperf_server_multithread(void *thread_param) timeout.tv_sec = 5; timeout.tv_usec = 0; - while (param.mode != IPERF_MODE_STOP){ + while (param.mode != IPERF_MODE_STOP) { FD_ZERO(&readset); FD_SET(sock, &readset); @@ -379,8 +377,8 @@ void iperf_server_multithread(void *thread_param) sccb->client_addr = client_addr; sccb->server_addr = server_addr; sccb->parent_id = cur_tid; - int tid = KTaskCreate("iperf server", iperf_sever_worker, sccb, LWIP_TASK_STACK_SIZE, 20); - // iperf_sever_worker(sccb); + int tid = KTaskCreate("iperf server", iperf_server_worker, sccb, LWIP_TASK_STACK_SIZE, 20); + // iperf_server_worker(sccb); if (tid) { StartupKTask(tid); } else { @@ -405,13 +403,13 @@ void iperf_server(void *thread_param) struct timeval timeout; sock = socket(AF_INET, SOCK_STREAM, 0); - if (sock < 0){ + if (sock < 0) { KPrintf("[%s:%d] Socket error!\n", __FILE__, __LINE__); goto __exit; } recv_data = (uint8_t *)malloc(IPERF_BUFSZ); - if (recv_data == NULL){ + if (recv_data == NULL) { KPrintf("No memory!\n"); goto __exit; } @@ -421,12 +419,12 @@ void iperf_server(void *thread_param) server_addr.sin_addr.s_addr = INADDR_ANY; memset(&(server_addr.sin_zero), 0x0, sizeof(server_addr.sin_zero)); - if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1){ + if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) { KPrintf("Unable to bind!\n"); goto __exit; } - if (listen(sock, 5) == -1){ + if (listen(sock, 5) == -1) { KPrintf("Listen error!\n"); goto __exit; } @@ -434,7 +432,7 @@ void iperf_server(void *thread_param) timeout.tv_sec = 3; timeout.tv_usec = 0; - while (param.mode != IPERF_MODE_STOP){ + while (param.mode != IPERF_MODE_STOP) { FD_ZERO(&readset); FD_SET(sock, &readset); @@ -458,7 +456,7 @@ void iperf_server(void *thread_param) recvlen = 0; tick1 = CurrentTicksGain(); - while (param.mode != IPERF_MODE_STOP){ + while (param.mode != IPERF_MODE_STOP) { bytes_received = recv(connected, recv_data, IPERF_BUFSZ, 0); if (bytes_received == 0) { KPrintf("client disconnected (%s, %d)\n", @@ -526,48 +524,37 @@ int iperf(int argc, char **argv) int use_udp = 0; int index = 1; - if (argc == 1) - { + if (argc == 1) { goto __usage; } - if (strcmp(argv[1], "-u") == 0) - { + if (strcmp(argv[1], "-u") == 0) { index = 2; use_udp = 1; } if (strcmp(argv[index], "-h") == 0) goto __usage; - else if (strcmp(argv[index], "--stop") == 0) - { + else if (strcmp(argv[index], "--stop") == 0) { /* stop iperf */ param.mode = IPERF_MODE_STOP; printf("iperf stop.\n"); return 0; - } - else if (strcmp(argv[index], "-s") == 0) - { + } else if (strcmp(argv[index], "-s") == 0) { mode = IPERF_MODE_SERVER; /* server mode */ /* iperf -s -p 5000 */ - if (argc >= 4) - { - if (strcmp(argv[index + 1], "-p") == 0) - { + if (argc >= 4) { + if (strcmp(argv[index + 1], "-p") == 0) { port = atoi(argv[index + 2]); } else goto __usage; } - } - else if (strcmp(argv[index], "-c") == 0) - { + } else if (strcmp(argv[index], "-c") == 0) { mode = IPERF_MODE_CLIENT; /* client mode */ if (argc < 3) goto __usage; host = argv[index + 1]; - if (argc >= 5) - { + if (argc >= 5) { /* iperf -c host -p port */ - if (strcmp(argv[index + 2], "-p") == 0) - { + if (strcmp(argv[index + 2], "-p") == 0) { port = atoi(argv[index + 3]); } else goto __usage; @@ -575,57 +562,43 @@ int iperf(int argc, char **argv) } else goto __usage; - if (argc >= 7) - { - if(strcmp(argv[argc - 2], "-m") == 0) - { + if (argc >= 7) { + if(strcmp(argv[argc - 2], "-m") == 0) { numtid = atoi(argv[argc - 1]); } else goto __usage; } /* start iperf */ - if (param.mode == IPERF_MODE_STOP) - { + if (param.mode == IPERF_MODE_STOP) { int i = 0; char tid_name[NAME_NUM_MAX + 1] = {0}; param.mode = mode; param.port = port; - if (param.host) - { + if (param.host) { free(param.host); param.host = NULL; } if (host) param.host = strdup(host); - for (i = 0; i < numtid; i++) - { + for (i = 0; i < numtid; i++) { int32 tid = 0; void (*function)(void *parameter); - if (use_udp) - { - if (mode == IPERF_MODE_CLIENT) - { + if (use_udp) { + if (mode == IPERF_MODE_CLIENT) { snprintf(tid_name, sizeof(tid_name), "iperfc%02d", i + 1); function = iperf_udp_client; - } - else if (mode == IPERF_MODE_SERVER) - { + } else if (mode == IPERF_MODE_SERVER) { snprintf(tid_name, sizeof(tid_name), "iperfd%02d", i + 1); function = iperf_udp_server; } - } - else - { - if (mode == IPERF_MODE_CLIENT) - { + } else { + if (mode == IPERF_MODE_CLIENT) { snprintf(tid_name, sizeof(tid_name), "iperfc%02d", i + 1); function = iperf_client; - } - else if (mode == IPERF_MODE_SERVER) - { + } else if (mode == IPERF_MODE_SERVER) { snprintf(tid_name, sizeof(tid_name), "iperfd%02d", i + 1); function = iperf_server_multithread; } @@ -634,9 +607,7 @@ int iperf(int argc, char **argv) tid = KTaskCreate(tid_name, function, NULL, LWIP_TASK_STACK_SIZE, 20); if (tid) StartupKTask(tid); } - } - else - { + } else { KPrintf("Please stop iperf firstly, by:\n"); KPrintf("iperf --stop\n"); } diff --git a/Ubiquitous/XiZi_IIoT/resources/ethernet/cmd_lwip/lwip_tcp_demo.c b/Ubiquitous/XiZi_IIoT/resources/ethernet/cmd_lwip/lwip_tcp_demo.c index e1ad56550..1a5fd37bc 100755 --- a/Ubiquitous/XiZi_IIoT/resources/ethernet/cmd_lwip/lwip_tcp_demo.c +++ b/Ubiquitous/XiZi_IIoT/resources/ethernet/cmd_lwip/lwip_tcp_demo.c @@ -27,8 +27,8 @@ #include "lwip/sockets.h" #include "tcpecho_raw.h" char tcp_demo_msg[LWIP_TEST_MSG_SIZE] = { 0 }; -char tcp_demo_ip[] = {192, 168, 250, 252}; -u16_t tcp_demo_port = 80; +char tcp_server_ip[] = {192, 168, 130, 2}; +u16_t tcp_server_port = 80; int tcp_send_num = 0; int tcp_send_task_on = 0; uint32 tcp_interval = 50; @@ -39,14 +39,14 @@ static void LwipTcpSendTask(void *arg) { int fd = -1; fd = socket(AF_INET, SOCK_STREAM, 0); - if (fd < 0) - { - lw_error("Socket error\n"); + if (fd < 0) { + KPrintf("Socket error\n"); return; } LwipTcpSocketParamType *param = (LwipTcpSocketParamType *)arg; + //configure tcp client param struct sockaddr_in tcp_sock; tcp_sock.sin_family = AF_INET; tcp_sock.sin_port = htons(param->port); @@ -55,24 +55,22 @@ static void LwipTcpSendTask(void *arg) if (connect(fd, (struct sockaddr *)&tcp_sock, sizeof(struct sockaddr))) { - lw_error("Unable to connect\n"); + KPrintf("Unable to connect\n"); closesocket(fd); return; } - lw_notice("tcp connect success, start to send.\n"); - lw_notice("\n\nTarget Port:%d\n\n", tcp_sock.sin_port); + KPrintf("tcp connect success, start to send.\n"); + KPrintf("\n\nTarget Port:%d\n\n", tcp_sock.sin_port); tcp_send_task_on = 1; - while(tcp_send_num > 0 || tcp_send_num == -1) - { + while(tcp_send_num > 0 || tcp_send_num == -1) { sendto(fd, tcp_demo_msg, strlen(tcp_demo_msg), 0, (struct sockaddr*)&tcp_sock, sizeof(struct sockaddr)); - lw_notice("Send tcp msg: %s \n", tcp_demo_msg); - if(tcp_send_num > 0) - { + KPrintf("Send tcp msg: %s \n", tcp_demo_msg); + if(tcp_send_num > 0) { tcp_send_num--; } - DelayKTask(tcp_interval); + MdelayKTask(tcp_interval); } closesocket(fd); @@ -82,41 +80,39 @@ static void LwipTcpSendTask(void *arg) void LwipTcpSendTest(int argc, char *argv[]) { - if(tcp_send_task_on){ + if(tcp_send_task_on) { tcp_send_num = 0; printf("waitting send task exit...\n"); - while(tcp_send_task_on){ - DelayKTask(1000); + while(tcp_send_task_on) { + MdelayKTask(1000); } tcp_send_num = 1; } LwipTcpSocketParamType param; uint8_t enet_port = 0; memset(tcp_demo_msg, 0, LWIP_TEST_MSG_SIZE); - if(argc >= 2) - { + if(argc >= 2) { strncpy(tcp_demo_msg, argv[1], strlen(argv[1])); - } - else - { + } else { strncpy(tcp_demo_msg, "hello world", strlen("hello world")); + tcp_send_num = 10;//send 10 msg to server + tcp_interval = 100;//100ms } strcat(tcp_demo_msg, "\r\n"); - if(argc >= 3) - { - if(sscanf(argv[2], "%d.%d.%d.%d:%d", &tcp_demo_ip[0], &tcp_demo_ip[1], &tcp_demo_ip[2], &tcp_demo_ip[3], &tcp_demo_port) == EOK) + if(argc >= 3) { + if(sscanf(argv[2], "%d.%d.%d.%d:%d", &tcp_server_ip[0], &tcp_server_ip[1], &tcp_server_ip[2], &tcp_server_ip[3], &tcp_server_port) == EOK) { - sscanf(argv[2], "%d.%d.%d.%d", &tcp_demo_ip[0], &tcp_demo_ip[1], &tcp_demo_ip[2], &tcp_demo_ip[3]); + sscanf(argv[2], "%d.%d.%d.%d", &tcp_server_ip[0], &tcp_server_ip[1], &tcp_server_ip[2], &tcp_server_ip[3]); } sscanf(argv[3], "%d", &tcp_send_num); sscanf(argv[4], "%d", &tcp_interval); } - lw_notice("get ipaddr %d.%d.%d.%d:%d send msg %d times\n", tcp_demo_ip[0], tcp_demo_ip[1], tcp_demo_ip[2], tcp_demo_ip[3], tcp_demo_port, tcp_send_num); - lwip_config_tcp(enet_port, lwip_ipaddr, lwip_netmask, tcp_demo_ip); + KPrintf("connect ipaddr %d.%d.%d.%d:%d send msg %d times\n", tcp_server_ip[0], tcp_server_ip[1], tcp_server_ip[2], tcp_server_ip[3], tcp_server_port, tcp_send_num); + lwip_config_tcp(enet_port, lwip_ipaddr, lwip_netmask, lwip_gwaddr); - memcpy(param.ip, tcp_demo_ip, 4); - param.port = tcp_demo_port; + memcpy(param.ip, tcp_server_ip, 4); + param.port = tcp_server_port; param.buf = malloc(LWIP_TEST_MSG_SIZE); memcpy(param.buf, tcp_demo_msg, LWIP_TEST_MSG_SIZE); @@ -131,7 +127,96 @@ void LwipTcpRecvTest(void) uint8_t enet_port = 0; ///< test enet port 0 lwip_config_net(enet_port, lwip_ipaddr, lwip_netmask, lwip_gwaddr); - tcpecho_raw_init(); + + uint8_t *recv_data; + socklen_t sin_size; + int sock = -1, connected, bytes_received, i; + struct sockaddr_in server_addr, client_addr; + fd_set readset; + struct timeval timeout; + + sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) { + KPrintf("[%s:%d] Socket error!\n", __FILE__, __LINE__); + goto __exit; + } + + recv_data = (uint8_t *)malloc(128); + if (recv_data == NULL) { + KPrintf("No memory!\n"); + goto __exit; + } + + //configure tcp server param + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(tcp_server_port); + server_addr.sin_addr.s_addr = INADDR_ANY; + memset(&(server_addr.sin_zero), 0x0, sizeof(server_addr.sin_zero)); + + if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) { + KPrintf("Unable to bind!\n"); + goto __exit; + } + + if (listen(sock, 5) == -1){ + KPrintf("Listen error!\n"); + goto __exit; + } + + timeout.tv_sec = 30; + timeout.tv_usec = 0; + + if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) == -1) { + KPrintf("setsockopt failed!"); + goto __exit; + } + + while (1) { + FD_ZERO(&readset); + FD_SET(sock, &readset); + + if (select(sock + 1, &readset, NULL, NULL, &timeout) == 0) { + continue; + } + + sin_size = sizeof(struct sockaddr_in); + + connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size); + + while (1) { + bytes_received = recv(connected, recv_data, 128, 0); + if (bytes_received == 0) { + KPrintf("client disconnected (%s, %d)\n", + inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); + break; + } else if (bytes_received < 0) { + KPrintf("recv error, client: (%s, %d)\n", + inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); + break; + } else { + KPrintf("new client connected from (%s, %d)\n", + inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); + KPrintf("recv data length %d Bytes\n", bytes_received); + for (i = 0; i < bytes_received; i ++) { + KPrintf("data 0x%x\n", recv_data[i]); + } + if (i = bytes_received) { + KPrintf("\r\n"); + memset(recv_data, 0, sizeof(recv_data)); + } + } + } + + if (connected >= 0) { + closesocket(connected); + connected = -1; + break; + } + } + +__exit: + if (sock >= 0) closesocket(sock); + if (recv_data) free(recv_data); } SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0), diff --git a/Ubiquitous/XiZi_IIoT/resources/ethernet/cmd_lwip/lwip_udp_demo.c b/Ubiquitous/XiZi_IIoT/resources/ethernet/cmd_lwip/lwip_udp_demo.c index d7b51b026..0456aa419 100755 --- a/Ubiquitous/XiZi_IIoT/resources/ethernet/cmd_lwip/lwip_udp_demo.c +++ b/Ubiquitous/XiZi_IIoT/resources/ethernet/cmd_lwip/lwip_udp_demo.c @@ -30,8 +30,8 @@ static struct udp_pcb *udpecho_raw_pcb; -char udp_demo_ip[] = {192, 168, 131, 1}; -u16_t udp_demo_port = LWIP_TARGET_PORT; +char udp_server_ip[] = {192, 168, 130, 2}; +u16_t udp_server_port = LWIP_TARGET_PORT; int32 udp_send_num = 0; int8 udp_send_task_on = 0; uint32 udp_interval = 50; @@ -45,38 +45,35 @@ static void LwipUDPSendTask(void *arg) { int cnt = LWIP_DEMO_TIMES; - lw_print("udp_send_demo start.\n"); + KPrintf("udp_send_demo start.\n"); int socket_fd = -1; socket_fd = socket(AF_INET, SOCK_DGRAM, 0); - if (socket_fd < 0) - { - lw_error("Socket error\n"); + if (socket_fd < 0) { + KPrintf("Socket error\n"); return; } struct sockaddr_in udp_sock; udp_sock.sin_family = AF_INET; - udp_sock.sin_port = htons(udp_demo_port); - udp_sock.sin_addr.s_addr = PP_HTONL(LWIP_MAKEU32(udp_demo_ip[0], udp_demo_ip[1], udp_demo_ip[2], udp_demo_ip[3])); + udp_sock.sin_port = htons(udp_server_port); + udp_sock.sin_addr.s_addr = PP_HTONL(LWIP_MAKEU32(udp_server_ip[0], udp_server_ip[1], udp_server_ip[2], udp_server_ip[3])); memset(&(udp_sock.sin_zero), 0, sizeof(udp_sock.sin_zero)); - if (connect(socket_fd, (struct sockaddr *)&udp_sock, sizeof(struct sockaddr))) - { - lw_error("Unable to connect\n"); + if (connect(socket_fd, (struct sockaddr *)&udp_sock, sizeof(struct sockaddr))) { + KPrintf("Unable to connect\n"); closesocket(socket_fd); return; } - lw_notice("UDP connect success, start to send.\n"); - lw_notice("\n\nTarget Port:%d\n\n", udp_sock.sin_port); + KPrintf("UDP connect success, start to send.\n"); + KPrintf("\n\nTarget Port:%d\n\n", udp_sock.sin_port); udp_send_task_on = 1; - while(udp_send_num > 0 || udp_send_num == -1) - { + while(udp_send_num > 0 || udp_send_num == -1) { sendto(socket_fd, udp_demo_msg, strlen(udp_demo_msg), 0, (struct sockaddr*)&udp_sock, sizeof(struct sockaddr)); - lw_notice("Send UDP msg: %s \n", udp_demo_msg); - DelayKTask(udp_interval); + KPrintf("Send UDP msg: %s \n", udp_demo_msg); + MdelayKTask(udp_interval); udp_send_num--; } closesocket(socket_fd); @@ -86,11 +83,11 @@ static void LwipUDPSendTask(void *arg) void *LwipUdpSendTest(int argc, char *argv[]) { - if(udp_send_task_on){ + if(udp_send_task_on) { udp_send_num = 0; printf("waitting send task exit...\n"); while(udp_send_task_on){ - DelayKTask(1000); + MdelayKTask(1000); } udp_send_num = 1; } @@ -98,89 +95,105 @@ void *LwipUdpSendTest(int argc, char *argv[]) uint8_t enet_port = 0; ///< test enet port 0 memset(udp_demo_msg, 0, sizeof(udp_demo_msg)); - if(argc == 1) - { - lw_print("lw: [%s] gw %d.%d.%d.%d:%d\n", __func__, udp_demo_ip[0], udp_demo_ip[1], udp_demo_ip[2], udp_demo_ip[3], udp_demo_port); + if(argc == 1) { + KPrintf("lw: [%s] gw %d.%d.%d.%d:%d\n", __func__, udp_server_ip[0], udp_server_ip[1], udp_server_ip[2], udp_server_ip[3], udp_server_port); strncpy(udp_demo_msg, hello_str, strlen(hello_str)); - } - else - { + udp_send_num = 10; + udp_interval = 100; + } else { strncpy(udp_demo_msg, argv[1], strlen(argv[1])); strncat(udp_demo_msg, "\r\n", 2); - if(argc == 3) - { - sscanf(argv[2], "%d.%d.%d.%d:%d", &udp_demo_ip[0], &udp_demo_ip[1], &udp_demo_ip[2], &udp_demo_ip[3], &udp_demo_port); + if(argc == 3) { + sscanf(argv[2], "%d.%d.%d.%d:%d", &udp_server_ip[0], &udp_server_ip[1], &udp_server_ip[2], &udp_server_ip[3], &udp_server_port); } - if(argc > 3) - { + if(argc > 3) { sscanf(argv[3], "%d", &udp_send_num); sscanf(argv[4], "%d", &udp_interval); } } - lw_print("lw: [%s] gw %d.%d.%d.%d:%d send time %d udp_interval %d\n", __func__, udp_demo_ip[0], udp_demo_ip[1], udp_demo_ip[2], udp_demo_ip[3], udp_demo_port, udp_send_num, udp_interval); + KPrintf("lw: [%s] gw %d.%d.%d.%d:%d send time %d udp_interval %d\n", __func__, udp_server_ip[0], udp_server_ip[1], udp_server_ip[2], udp_server_ip[3], udp_server_port, udp_send_num, udp_interval); - lwip_config_net(enet_port, lwip_ipaddr, lwip_netmask, udp_demo_ip); + //init lwip and net dirver + lwip_config_net(enet_port, lwip_ipaddr, lwip_netmask, lwip_gwaddr); sys_thread_new("udp send", LwipUDPSendTask, NULL, LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO); } SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3), UDPSend, LwipUdpSendTest, UDPSend msg [ip:port [num [interval]]]); -static void LwipUdpRecvTask(void *arg, struct udp_pcb *upcb, struct pbuf *p, - const ip_addr_t *addr, u16_t port) -{ - int udp_len; - err_t err; - struct pbuf* udp_buf; - - LWIP_UNUSED_ARG(arg); - - if (p == NULL) - { - return; - } - - udp_len = p->tot_len; - lw_notice("Receive data :%dB\r\n", udp_len); - - if(udp_len <= 80) - { - lw_notice("%.*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, udp_buf, addr, port); - - /* free the pbuf */ - pbuf_free(p); - pbuf_free(udp_buf); -} - void LwipUdpRecvTest(void) { - err_t err; uint8_t enet_port = 0; ///< test enet port 0 + //init lwip and net dirver lwip_config_net(enet_port, lwip_ipaddr, lwip_netmask, lwip_gwaddr); - udpecho_raw_pcb = udp_new_ip_type(IPADDR_TYPE_ANY); - if (udpecho_raw_pcb == NULL) - { - return; + uint8_t *recv_data; + socklen_t sin_size; + int sock = -1, connected, bytes_received, i; + struct sockaddr_in server_addr, client_addr; + fd_set readset; + struct timeval timeout; + + sock = socket(PF_INET, SOCK_DGRAM, 0); + if (sock < 0) { + KPrintf("[%s:%d] Socket error!\n", __FILE__, __LINE__); + goto __exit; } - err = udp_bind(udpecho_raw_pcb, IP_ANY_TYPE, LWIP_LOCAL_PORT); - if (err == ERR_OK) - { - udp_recv(udpecho_raw_pcb, LwipUdpRecvTask, NULL); + recv_data = (uint8_t *)malloc(128); + if (recv_data == NULL) { + KPrintf("No memory!\n"); + goto __exit; } + + //configure udp server param + server_addr.sin_family = PF_INET; + server_addr.sin_port = htons(udp_server_port); + server_addr.sin_addr.s_addr = INADDR_ANY; + memset(&(server_addr.sin_zero), 0x0, sizeof(server_addr.sin_zero)); + + if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) { + KPrintf("Unable to bind!\n"); + goto __exit; + } + + timeout.tv_sec = 30; + timeout.tv_usec = 0; + + if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) == -1) { + KPrintf("setsockopt failed!"); + goto __exit; + } + + while (1) { + bytes_received = recvfrom(sock, recv_data, 128, 0, (struct sockaddr *)&client_addr, (socklen_t*)&sin_size); + if (bytes_received == 0) { + KPrintf("client disconnected (%s, %d)\n", + inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); + break; + } else if (bytes_received < 0) { + KPrintf("recv error, client: (%s, %d)\n", + inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); + break; + } else { + KPrintf("new client connected from (%s, %d)\n", + inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); + KPrintf("recv data length %d Bytes\n", bytes_received); + for (i = 0; i < bytes_received; i ++) { + KPrintf("data 0x%x\n", recv_data[i]); + } + if (i = bytes_received) { + KPrintf("\r\n"); + memset(recv_data, 0, sizeof(recv_data)); + } + } + } + +__exit: + if (sock >= 0) closesocket(sock); + if (recv_data) free(recv_data); } SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0), diff --git a/Ubiquitous/XiZi_IIoT/resources/ethernet/netdev/netdev_ipaddr_util.c b/Ubiquitous/XiZi_IIoT/resources/ethernet/netdev/netdev_ipaddr_util.c deleted file mode 100644 index e69de29bb..000000000 diff --git a/Ubiquitous/XiZi_IIoT/resources/ethernet/netdev/netdev_lowlevel.c b/Ubiquitous/XiZi_IIoT/resources/ethernet/netdev/netdev_lowlevel.c index 694fdcdbc..c1d4e0fa4 100644 --- a/Ubiquitous/XiZi_IIoT/resources/ethernet/netdev/netdev_lowlevel.c +++ b/Ubiquitous/XiZi_IIoT/resources/ethernet/netdev/netdev_lowlevel.c @@ -1,3 +1,33 @@ +/* + * Copyright (c) 2006-2022, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-03-18 ChenYong First version + */ + +/** +* @file netdev_lowlevel.c +* @brief register low-level net dev function +* @version 3.0 +* @author AIIT XUOS Lab +* @date 2023-08-07 +*/ + +/************************************************* +File name: netdev_lowlevel.c +Description: register low-level net dev function +Others: take RT-Thread v4.0.2/components/driver/serial/serial.c for references + https://github.com/RT-Thread/rt-thread/tree/v4.0.2 +History: +1. Date: 2023-08-07 +Author: AIIT XUOS Lab +Modification: +1. support low-level net dev set-ip, set-netmask, set-gw, set-status, set-link-status, set-dns-server and set-dhcp-status +*************************************************/ + #include #include #include diff --git a/Ubiquitous/XiZi_IIoT/resources/ethernet/netdev/netdev_manipulate.c b/Ubiquitous/XiZi_IIoT/resources/ethernet/netdev/netdev_manipulate.c index 450155d54..97e15d3a1 100644 --- a/Ubiquitous/XiZi_IIoT/resources/ethernet/netdev/netdev_manipulate.c +++ b/Ubiquitous/XiZi_IIoT/resources/ethernet/netdev/netdev_manipulate.c @@ -1,3 +1,33 @@ +/* + * Copyright (c) 2006-2022, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-03-18 ChenYong First version + */ + +/** +* @file netdev_manipulate.c +* @brief register net dev function +* @version 3.0 +* @author AIIT XUOS Lab +* @date 2023-08-07 +*/ + +/************************************************* +File name: netdev_manipulate.c +Description: register net dev function +Others: take RT-Thread v4.0.2/components/driver/serial/serial.c for references + https://github.com/RT-Thread/rt-thread/tree/v4.0.2 +History: +1. Date: 2023-08-07 +Author: AIIT XUOS Lab +Modification: +1. support net dev set-ip, set-netmask, set-gw, set-addr-callback, set-status-callback, set-up and set-down +*************************************************/ + #include #include #include diff --git a/Ubiquitous/XiZi_IIoT/resources/ethernet/netdev/netdev_register.c b/Ubiquitous/XiZi_IIoT/resources/ethernet/netdev/netdev_register.c index d4e855e09..73ef0ed88 100644 --- a/Ubiquitous/XiZi_IIoT/resources/ethernet/netdev/netdev_register.c +++ b/Ubiquitous/XiZi_IIoT/resources/ethernet/netdev/netdev_register.c @@ -1,3 +1,32 @@ +/* + * Copyright (c) 2006-2022, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-03-18 ChenYong First version + */ + +/** +* @file netdev_register.c +* @brief register net dev function for net driver +* @version 3.0 +* @author AIIT XUOS Lab +* @date 2023-08-07 +*/ + +/************************************************* +File name: netdev_register.c +Description: register net dev function for net driver +Others: take RT-Thread v4.0.2/components/driver/serial/serial.c for references + https://github.com/RT-Thread/rt-thread/tree/v4.0.2 +History: +1. Date: 2023-08-07 +Author: AIIT XUOS Lab +Modification: +1. support net dev register, unregister function +*************************************************/ #include #include diff --git a/Ubiquitous/XiZi_IIoT/resources/include/netdev/netdev.h b/Ubiquitous/XiZi_IIoT/resources/include/netdev/netdev.h index 8ebe09c76..e3007fd9e 100644 --- a/Ubiquitous/XiZi_IIoT/resources/include/netdev/netdev.h +++ b/Ubiquitous/XiZi_IIoT/resources/include/netdev/netdev.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #ifdef __cplusplus