/*!
    \file  netconf.c
    \brief network connection configuration 

    \version 2017-02-10, V1.0.0, firmware for GD32F30x
    \version 2018-10-10, V1.1.0, firmware for GD32F30x
    \version 2018-12-25, V2.0.0, firmware for GD32F30x
*/

/*
    Copyright (c) 2018, GigaDevice Semiconductor Inc.

    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. Neither the name of the copyright holder nor the names of its contributors 
       may be used to endorse or promote products derived from this software without 
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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.
*/

#include "lwip/mem.h"
#include "lwip/memp.h"
#include "lwip/dhcp.h"
#include "ethernetif.h"
#include "main.h"
#include "netconf.h"
#include "tcpip.h"
#include <stdio.h>
#include "lwip/inet.h"
#include "hd_eth.h"

#define MAX_DHCP_TRIES       4

typedef enum 
{ 
    DHCP_START = 0,
    DHCP_WAIT_ADDRESS,
    DHCP_ADDRESS_ASSIGNED,
    DHCP_TIMEOUT
}dhcp_state_enum;


volatile uint8_t EthLinkStatus = 0;
#ifdef USE_DHCP
dhcp_state_enum dhcp_state = DHCP_START;
#endif /* USE_DHCP */

struct netif g_mynetif;

void ETH_link_callback(struct netif *netif);
void ETH_CheckLinkStatus(uint16_t PHYAddress);
void ETH_CheckLinkStatus_timer(void *arg);
/*!
    \brief      initializes the LwIP stack
    \param[in]  none
    \param[out] none
    \retval     none
*/
void lwip_stack_init(void)
{
    struct ip_addr ipaddr;
    struct ip_addr netmask;
    struct ip_addr 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(&g_mynetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);

    /* registers the default network interface */
    netif_set_default(&g_mynetif);

    /* when the netif is fully configured this function must be called */
    netif_set_up(&g_mynetif);
		uint16_t data;
		enet_phy_write_read(ENET_PHY_READ, DP83848_PHY_ADDRESS,PHY_REG_BSR,&data);
		if(data & 0x4)
		{
			EthStatus |= ETH_LINK_FLAG;
		}
		if (EthStatus == (ETH_INIT_FLAG | ETH_LINK_FLAG))
		{ 
    /* Set Ethernet link flag */
    g_mynetif.flags |= NETIF_FLAG_LINK_UP;

    /* When the netif is fully configured this function must be called.*/
    netif_set_up(&g_mynetif);
#ifdef USE_DHCP
    dhcp_state = DHCP_START;
#else
#ifdef SERIAL_DEBUG
		printf("\n  Static IP address   \n");
		printf("IP: %d.%d.%d.%d\n",IP_ADDR0,IP_ADDR1,IP_ADDR2,IP_ADDR3);
		printf("NETMASK: %d.%d.%d.%d\n",NETMASK_ADDR0,NETMASK_ADDR1,NETMASK_ADDR2,NETMASK_ADDR3);
		printf("Gateway: %d.%d.%d.%d\n",GW_ADDR0,GW_ADDR1,GW_ADDR2,GW_ADDR3);
#endif /* SERIAL_DEBUG */
#endif /* USE_DHCP */
  }
  else
  {
    /*  When the netif link is down this function must be called.*/
    netif_set_down(&g_mynetif);
#ifdef USE_DHCP
    dhcp_state = DHCP_TIMEOUT;
#endif /* USE_DHCP */
#ifdef SERIAL_DEBUG
		printf("\n  Network Cable is  \n");
		printf("    not connected   \n");
#endif /* SERIAL_DEBUG */
  }
  /* Set the link callback function, this function is called on change of link status*/
  netif_set_link_callback(&g_mynetif, ETH_link_callback);
	sys_timeout(2000, ETH_CheckLinkStatus_timer, NULL);	
}

#ifdef USE_DHCP
/*!
    \brief      dhcp_task
    \param[in]  none
    \param[out] none
    \retval     none
*/
void dhcp_task(void * pvParameters)
{
    struct ip_addr ipaddr;
    struct ip_addr netmask;
    struct ip_addr gw;
    uint32_t ip_address = 0;
  
    for(;;){
        switch(dhcp_state){
        case DHCP_START:
            dhcp_start(&g_mynetif);
            /* IP address should be set to 0 every time we want to assign a new DHCP address*/
            ip_address = 0;
            dhcp_state = DHCP_WAIT_ADDRESS;
            break;

        case DHCP_WAIT_ADDRESS:    
            /* read the new IP address */
            ip_address = g_mynetif.ip_addr.addr;

            if(0 != ip_address){ 
                dhcp_state = DHCP_ADDRESS_ASSIGNED;
                /* stop DHCP */
                dhcp_stop(&g_mynetif);
							char p[100];
							sprintf(p,"\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));
            }else{
                /* DHCP timeout */
                if(g_mynetif.dhcp->tries > MAX_DHCP_TRIES){
                    dhcp_state = DHCP_TIMEOUT;
                    /* stop DHCP */
                    dhcp_stop(&g_mynetif);

                    /* 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(&g_mynetif, &ipaddr , &netmask, &gw);
                }
            }
            break;

        default: 
            break;
        }
    
        /* wait 250 ms */
        vTaskDelay(1000);
    }   
}

#endif /* USE_DHCP */
//监测以太网热拔插定时器
void ETH_CheckLinkStatus_timer(void *arg)
{
	LWIP_UNUSED_ARG(arg);
	ETH_CheckLinkStatus(DP83848_PHY_ADDRESS);
	sys_timeout(1000, ETH_CheckLinkStatus_timer, NULL);
}
/* This function is called periodically each second */
/* It checks link status for ethernet controller */
void ETH_CheckLinkStatus(uint16_t PHYAddress) 
{
	static uint8_t status = 0;
	uint16_t data;
	uint8_t linksta;
	//读取以太网链接状态
  if(enet_phy_write_read(ENET_PHY_READ, DP83848_PHY_ADDRESS,PHY_REG_BSR,&data)==1)
	{
		linksta=data & 0x4;
	}
	if(EthStatus & ETH_INIT_FLAG)
	{
		if(EthStatus & ETH_LINK_FLAG)
		{
			if(linksta == 0)
			{
				netif_set_link_down(&g_mynetif);
				EthStatus &= ~ETH_LINK_FLAG;
			}
		}
		else 
		{
			if(linksta != 0)
			{
				netif_set_link_up(&g_mynetif);
				EthStatus |= ETH_LINK_FLAG;
			}
		}
	}
	else//处理未初始化的问题
	{
		if(linksta != 0)
		{
			low_level_init(&g_mynetif);
			netif_set_link_up(&g_mynetif);
			EthStatus |= ETH_INIT_FLAG;
#if LWIP_DHCP == 1
			dhcp_start(&g_mynetif);
#endif 			
		}
	}
}

/**
  * @brief  Link callback function, this function is called on change of link status.
  * @param  The network interface
  * @retval None
  */
void ETH_link_callback(struct netif *netif)
{
	__IO uint32_t timeout = 0;
	uint32_t tmpreg,RegValue;
	uint32_t media_temp = 0U;
	uint16_t phy_value;
	if(netif_is_link_up(netif))
	{
		printf("network is up");
		//进入自协商模式 
		do{
        enet_phy_write_read(ENET_PHY_READ, PHY_ADDRESS, PHY_REG_BSR, &phy_value);
        phy_value &= PHY_AUTONEGO_COMPLETE;
        timeout++;
    }while((RESET == phy_value) && (timeout < PHY_READ_TO));
/*
     enet_phy_write_read(ENET_PHY_READ, PHY_ADDRESS, PHY_SR, &phy_value);  
        if((uint16_t)RESET != (phy_value & PHY_DUPLEX_STATUS)){
            media_temp = ENET_MODE_FULLDUPLEX;
        }else{
            media_temp = ENET_MODE_HALFDUPLEX;
        }
        if((uint16_t)RESET !=(phy_value & PHY_SPEED_STATUS)){
            media_temp |= ENET_SPEEDMODE_10M;
        }else{
            media_temp |= ENET_SPEEDMODE_100M;
        }
*/			
		enet_enable();
		netif_set_up(&g_mynetif);
	}
	else 
	{
		printf("network is down");		
		enet_disable();
		netif_set_down(&g_mynetif);
	}
}