/*! \file netconf.c \brief network connection configuration */ /* Copyright (C) 2016 GigaDevice 2016-08-15, V1.0.0, firmware for GD32F4xx */ #include "lwip/mem.h" #include "lwip/memp.h" #include "lwip/dhcp.h" #include "ethernetif.h" #include "main.h" #include "netconf.h" #include "lwip/tcpip.h" #include #define MAX_DHCP_TRIES 4 typedef enum { DHCP_START = 0, DHCP_WAIT_ADDRESS, DHCP_ADDRESS_ASSIGNED, DHCP_TIMEOUT } dhcp_state_enum; #ifdef USE_DHCP dhcp_state_enum dhcp_state = DHCP_START; ip4_addr_t ip_address = {0}; #endif /* USE_DHCP */ struct netif xnetif; /*! \brief initializes the LwIP stack \param[in] none \param[out] none \retval none */ void lwip_stack_init(void) { ip4_addr_t ipaddr; ip4_addr_t netmask; ip4_addr_t gw; /* create tcp_ip stack thread */ tcpip_init(NULL, NULL); /* IP address setting */ #ifdef USE_DHCP ipaddr.addr = 0; netmask.addr = 0; gw.addr = 0; #else IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3); IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3); IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3); #endif /* USE_DHCP */ /* - netif_add(struct netif *netif, struct ip_addr *ipaddr, struct ip_addr *netmask, struct ip_addr *gw, void *state, err_t (* init)(struct netif *netif), err_t (* input)(struct pbuf *p, struct netif *netif)) Adds your network interface to the netif_list. Allocate a struct netif and pass a pointer to this structure as the first argument. Give pointers to cleared ip_addr structures when using DHCP, or fill them with sane numbers otherwise. The state pointer may be NULL. The init function pointer must point to a initialization function for your ethernet netif interface. The following code illustrates it's use. */ netif_add(&xnetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, &tcpip_input); /* registers the default network interface */ netif_set_default(&xnetif); /* when the netif is fully configured this function must be called */ netif_set_up(&xnetif); } #ifdef USE_DHCP /*! \brief lwip_dhcp_process_handle \param[in] none \param[out] none \retval none */ void dhcp_task(void *pvParameters) { ip4_addr_t ipaddr; ip4_addr_t netmask; ip4_addr_t gw; struct dhcp *dhcp_client; dhcp_client = netif_dhcp_data(&xnetif); for (;;) { switch (dhcp_state) { case DHCP_START: dhcp_start(&xnetif); ip_address.addr = 0; dhcp_state = DHCP_WAIT_ADDRESS; break; case DHCP_WAIT_ADDRESS: /* read the new IP address */ ip_address.addr = xnetif.ip_addr.u_addr.ip4.addr; if (ip_address.addr != 0) { dhcp_state = DHCP_ADDRESS_ASSIGNED; /* stop DHCP */ dhcp_stop(&xnetif); printf("\r\nDHCP -- eval board ip address: %d.%d.%d.%d \r\n", ip4_addr1_16(&ip_address), ip4_addr2_16(&ip_address), ip4_addr3_16(&ip_address), ip4_addr4_16(&ip_address)); OSTaskSuspend(OS_PRIO_SELF); } else { /* DHCP timeout */ if (dhcp_client->tries > MAX_DHCP_TRIES) { dhcp_state = DHCP_TIMEOUT; /* stop DHCP */ dhcp_stop(&xnetif); /* static address used */ IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3); IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3); IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3); netif_set_addr(&xnetif, &ipaddr, &netmask, &gw); OSTaskSuspend(OS_PRIO_SELF); } } break; default: break; } /* wait 500 ms */ OSTimeDlyHMSM(0, 0, 0, 500); } } #endif /* USE_DHCP */