text: /**
    ******************************************************************************
    * @file    LwIP/LwIP_HTTP_Server_Raw/Src/app_ethernet.c 
    * @author  MCD Application Team
    * @brief   Ethernet specefic module
    ******************************************************************************
    * @attention
    *
    * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
    * All rights reserved.</center></h2>
    *
    * This software component is licensed by ST under Ultimate Liberty license
    * SLA0044, the "License"; You may not use this file except in compliance with
    * the License. You may obtain a copy of the License at:
    *                             www.st.com/SLA0044
    *
    ******************************************************************************
    */
  /* Includes ------------------------------------------------------------------*/
  #include "main.h"
  #include "lwip/dhcp.h"
  #include "app_ethernet.h"
  #ifdef USE_LCD
  #include "lcd_log.h"
  #endif
  
  /* Private typedef -----------------------------------------------------------*/
  /* Private define ------------------------------------------------------------*/
  /* Private macro -------------------------------------------------------------*/
  /* Private variables ---------------------------------------------------------*/
  #ifdef USE_DHCP
  #define MAX_DHCP_TRIES  4
  uint32_t DHCPfineTimer = 0;
  __IO uint8_t DHCP_state = DHCP_OFF;
  #endif
  
  /* Private function prototypes -----------------------------------------------*/
  /* Private functions ---------------------------------------------------------*/
  /**
    * @brief  Notify the User about the nework interface config status 
    * @param  netif: the network interface
    * @retval None
    */
  void User_notification(struct netif *netif) 
  {
    if (netif_is_up(netif))
   {
  #ifdef USE_DHCP
      /* Update DHCP state machine */
      DHCP_state = DHCP_START;
  #else
  #ifdef USE_LCD
      uint8_t iptxt[20];
      sprintf((char *)iptxt, "%s", ip4addr_ntoa((const ip4_addr_t *)&netif->ip_addr));
      LCD_UsrLog ("Static IP address: %s\n", iptxt);
  #else    
      /* Turn On LED 1 to indicate ETH and LwIP init success*/
      BSP_LED_On(LED1);
  #endif /* USE_LCD */
  #endif /* USE_DHCP */
   }
   else
    {  
  #ifdef USE_DHCP
      /* Update DHCP state machine */
      DHCP_state = DHCP_LINK_DOWN;
  #endif  /* USE_DHCP */
  #ifdef USE_LCD
     LCD_UsrLog ("The network cable is not connected \n");
  #else    
      /* Turn On LED 2 to indicate ETH and LwIP init error */
      BSP_LED_On(LED2);
  #endif /* USE_LCD */
    } 
  }
  
  /**
    * @brief  This function notify user about link status changement.
    * @param  netif: the network interface
    * @retval None
    */
  void ethernetif_notify_conn_changed(struct netif *netif)
  {
  #ifndef USE_DHCP
    ip_addr_t ipaddr;
    ip_addr_t netmask;
    ip_addr_t gw;
  #endif
    
    if(netif_is_link_up(netif))
    {
  #ifdef USE_LCD
      LCD_UsrLog ("The network cable is now connected \n");
  #else
      BSP_LED_Off(LED2);
      BSP_LED_On(LED1);
  #endif /* USE_LCD */
      
  #ifdef USE_DHCP
      /* Update DHCP state machine */
      DHCP_state = DHCP_START;
  #else
      IP_ADDR4(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
      IP_ADDR4(&netmask, NETMASK_ADDR0, NETMASK_ADDR1 , NETMASK_ADDR2, NETMASK_ADDR3);
      IP_ADDR4(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);  
      
      netif_set_addr(netif, &ipaddr , &netmask, &gw);  
      
  #ifdef USE_LCD        
      uint8_t iptxt[20];
      sprintf((char *)iptxt, "%s", ip4addr_ntoa((const ip4_addr_t *)&netif->ip_addr));
      LCD_UsrLog ("Static IP address: %s\n", iptxt);
  #endif
  #endif /* USE_DHCP */   
      
      /* When the netif is fully configured this function must be called.*/
      netif_set_up(netif);     
    }
    else
    {
  #ifdef USE_DHCP
      /* Update DHCP state machine */
      DHCP_state = DHCP_LINK_DOWN;
  #endif /* USE_DHCP */
      
      /*  When the netif link is down this function must be called.*/
      netif_set_down(netif);
      
  #ifdef USE_LCD
      LCD_UsrLog ("The network cable is not connected \n");
  #else
      BSP_LED_Off(LED1);
      BSP_LED_On(LED2);
  #endif /* USE_LCD */    
    }
  }
  
  #ifdef USE_DHCP
  /**
    * @brief  DHCP_Process_Handle
    * @param  None
    * @retval None
    */
  void DHCP_Process(struct netif *netif)
  {
    ip_addr_t ipaddr;
    ip_addr_t netmask;
    ip_addr_t gw;
    struct dhcp *dhcp;   
  #ifdef USE_LCD 
    uint8_t iptxt[20];
  #endif
    
    switch (DHCP_state)
    {
      case DHCP_START:
      {
        ip_addr_set_zero_ip4(&netif->ip_addr);
        ip_addr_set_zero_ip4(&netif->netmask);
        ip_addr_set_zero_ip4(&netif->gw);
        DHCP_state = DHCP_WAIT_ADDRESS;
        dhcp_start(netif);
  #ifdef USE_LCD
        LCD_UsrLog ("  State: Looking for DHCP server ...\n");
  #endif
      }
      break;
      
    case DHCP_WAIT_ADDRESS:
      {
        if (dhcp_supplied_address(netif)) 
        {
          DHCP_state = DHCP_ADDRESS_ASSIGNED;
          
  #ifdef USE_LCD 
          sprintf((char *)iptxt, "%s", ip4addr_ntoa((const ip4_addr_t *)&netif->ip_addr));
          LCD_UsrLog ("IP address assigned by a DHCP server: %s\n", iptxt); 
  #else
          BSP_LED_On(LED1);   
  #endif
        }
        else
        {
          dhcp = (struct dhcp *)netif_get_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP);
      
          /* DHCP timeout */
          if (dhcp->tries > MAX_DHCP_TRIES)
          {
            DHCP_state = DHCP_TIMEOUT;
            
            /* Stop DHCP */
            dhcp_stop(netif);
            
            /* Static address used */
            IP_ADDR4(&ipaddr, IP_ADDR0 ,IP_ADDR1 , IP_ADDR2 , IP_ADDR3 );
            IP_ADDR4(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
            IP_ADDR4(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
            netif_set_addr(netif, &ipaddr, &netmask, &gw);
            
  #ifdef USE_LCD     
            sprintf((char *)iptxt, "%s", ip4addr_ntoa((const ip4_addr_t *)&netif->ip_addr));
            LCD_UsrLog ("DHCP Timeout !! \n");
            LCD_UsrLog ("Static IP address: %s\n", iptxt);   
  #else
            BSP_LED_On(LED1);  
  #endif
          }
        }
      }
      break;
    case DHCP_LINK_DOWN:
      {
        /* Stop DHCP */
        dhcp_stop(netif);
        DHCP_state = DHCP_OFF; 
      }
      break;
    default: break;
    }
  }
  
  /**
    * @brief  DHCP periodic check
    * @param  localtime the current LocalTime value
    * @retval None
    */
  void DHCP_Periodic_Handle(struct netif *netif)
  {  
    /* Fine DHCP periodic process every 500ms */
    if (HAL_GetTick() - DHCPfineTimer >= DHCP_FINE_TIMER_MSECS)
    {
      DHCPfineTimer =  HAL_GetTick();
      /* process DHCP state machine */
      DHCP_Process(netif);    
    }
  }
  #endif
  
  /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  