| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 | /* Includes ------------------------------------------------------------------*/#include "lwip/mem.h"#include "lwip/memp.h"#include "lwip/tcp.h"#include "lwip/udp.h"#include "netif/etharp.h"#include "lwip/dhcp.h"#include "ethernetif.h"#include "tcpip.h"#include "main.h"#include "netconf.h"#include "common_config.h"#include "settings_api.h"#include "tinystdio.h"#include "lwip/init.h"#include "lwip/timeouts.h"#include "lwip/sys.h"/* Private typedef -----------------------------------------------------------*/#define MAX_DHCP_TRIES        4/* Private define ------------------------------------------------------------*/typedef enum {    DHCP_START = 0,    DHCP_WAIT_ADDRESS,    DHCP_ADDRESS_ASSIGNED,    DHCP_TIMEOUT}DHCP_State_TypeDef;/* Private macro -------------------------------------------------------------*//* Private variables ---------------------------------------------------------*/struct netif netif;uint32_t TCPTimer = 0;uint32_t ARPTimer = 0;uint32_t DHCPfineTimer = 0;uint32_t DHCPcoarseTimer = 0;DHCP_State_TypeDef DHCP_state = DHCP_START;/**  * @brief  Общая структура настроек  */extern SETTINGS_t sSettings;/* Private functions ---------------------------------------------------------*/void LwIP_DHCP_Process_Handle(void);/**  * @brief  Initializes the lwIP stack  * @param  None  * @retval None  */void LwIP_Init(void){    struct ip4_addr ipaddr;    struct ip4_addr netmask;    struct ip4_addr gw;    char str[20];    lwip_init();    //  /* Initializes the dynamic memory heap defined by MEM_SIZE.*/    //  mem_init();    //    //  /* Initializes the memory pools defined by MEMP_NUM_x.*/    //  memp_init();    if (sSettings.sWebParams.dhcpEnable) {        ipaddr.addr = 0;        netmask.addr = 0;        gw.addr = 0;    } else {        sprintf(str, " %s\n\r", sSettings.sWebTempParams.ip);        PRINT_USART("\n\rStatic IP address   \n\r");        PRINT_USART(str);        ipaddr.addr = ipaddr_addr(sSettings.sWebParams.ip);        netmask.addr = ipaddr_addr(sSettings.sWebParams.mask);        gw.addr = ipaddr_addr(sSettings.sWebParams.gate);    }    netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, ðernet_input);    netif_set_default(&netif);    netif_set_link_up(&netif);    netif_set_up(&netif);}/**  * @brief  Called when a frame is received  * @param  None  * @retval None  */void LwIP_Pkt_Handle(void){    /* Read a received packet from the Ethernet buffers and send it to the lwIP for handling */    ethernetif_input(&netif);}///**//  * @brief  LwIP periodic tasks//  * @param  localtime the current LocalTime value//  * @retval None//  *///void LwIP_Periodic_Handle(__IO uint32_t localtime)//{//#if LWIP_TCP//  /* TCP periodic process every 250 ms *///  if (localtime - TCPTimer >= TCP_TMR_INTERVAL) {//    TCPTimer =  localtime;//    tcp_tmr();//  }//#endif////  /* ARP periodic process every 5s *///  if ((localtime - ARPTimer) >= ARP_TMR_INTERVAL) {//    ARPTimer =  localtime;//    etharp_tmr();//  }////  if (sSettings.sWebParams.dhcpEnable)//  {//    /* Fine DHCP periodic process every 500ms *///    if (localtime - DHCPfineTimer >= DHCP_FINE_TIMER_MSECS) {//      DHCPfineTimer =  localtime;//      dhcp_fine_tmr();//      if ((DHCP_state != DHCP_ADDRESS_ASSIGNED)&&(DHCP_state != DHCP_TIMEOUT)) {//        /* process DHCP state machine *///        LwIP_DHCP_Process_Handle();//      }//    }////    /* DHCP Coarse periodic process every 60s *///    if (localtime - DHCPcoarseTimer >= DHCP_COARSE_TIMER_MSECS) {//      DHCPcoarseTimer =  localtime;//      dhcp_coarse_tmr();//    }//  }////}/**  * @brief  LwIP periodic tasks  * @param  localtime the current LocalTime value  * @retval None  */void LwIP_Periodic_Handle(__IO uint32_t localtime){    if (sSettings.sWebParams.dhcpEnable) {        /* Fine DHCP periodic process every 500ms */        if ((DHCP_state != DHCP_ADDRESS_ASSIGNED) && (DHCP_state != DHCP_TIMEOUT)) {            /* process DHCP state machine */            LwIP_DHCP_Process_Handle();        }    }    sys_check_timeouts();}/**  * @brief  LwIP_DHCP_Process_Handle  * @param  None  * @retval None  */void LwIP_DHCP_Process_Handle(){    struct ip4_addr ipaddr;    struct ip4_addr netmask;    struct ip4_addr gw;    struct dhcp *dhcp = netif_dhcp_data(&netif);    switch (DHCP_state) {        case DHCP_START: {            dhcp_start(&netif);            DHCP_state = DHCP_WAIT_ADDRESS;            PRINT_USART("\n\rLooking for DHCP server please wait...\n\r");        }        break;        case DHCP_WAIT_ADDRESS: {            ipaddr = netif.ip_addr;            netmask = netif.netmask;            gw = netif.gw;            if (ipaddr.addr != 0) {                DHCP_state = DHCP_ADDRESS_ASSIGNED;                PRINT_USART("Parameters assigned by a DHCP server:\n\r     IP:   ");                PRINT_USART(ipaddr_ntoa(&ipaddr));                PRINT_USART("\n\r");                PRINT_USART("Netmask:   ");                PRINT_USART(ipaddr_ntoa(&netmask));                PRINT_USART("\n\r");                PRINT_USART("Gateway:   ");                PRINT_USART(ipaddr_ntoa(&gw));                PRINT_USART("\n\r");            } else {                /* DHCP timeout */                if (dhcp->tries > MAX_DHCP_TRIES) {                    DHCP_state = DHCP_TIMEOUT;                    /* Stop DHCP */                    dhcp_stop(&netif);                    ipaddr.addr = ipaddr_addr(sSettings.sWebTempParams.ip);                    netmask.addr = ipaddr_addr(sSettings.sWebTempParams.mask);                    gw.addr = ipaddr_addr(sSettings.sWebTempParams.gate);                    netif_set_addr(&netif, &ipaddr, &netmask, &gw);                    PRINT_USART("DHCP timeout\n\r");                    PRINT_USART("\n\rStatic IP address\n\r");                    PRINT_USART(ipaddr_ntoa(&ipaddr));                    PRINT_USART("\n\r");                }            }        }        break;        default: break;    }}/*********** Portions COPYRIGHT 2012 Embest Tech. Co., Ltd.*****END OF FILE****/
 |