
#include <string.h>

#include "conf_eth.h"
#include "lwipopts.h"

/* Scheduler include files. */
#if (NO_SYS == 0)
#include "FreeRTOS.h"
#include "task.h"
#endif


/* ethernet includes */
#include "ethernet.h"
#include "conf_eth.h"
#include "mss_ethernet_mac.h"
#include "mss_watchdog.h"

#if (HTTP_USED == 1)
  //#include "sfwebserver.h"
#endif

#if (TFTP_USED == 1)
  #include "BasicTFTP.h"
#endif

#if (SMTP_USED == 1)
  #include "BasicSMTP.h"
#endif

/* lwIP includes */
#include "lwip/sys.h"
#include "lwip/api.h" 
#include "lwip/tcpip.h"
#include "lwip/memp.h" 
#include "lwip/stats.h"
#include "netif/loopif.h"



/* global variable containing MAC Config (hw addr, IP, GW, ...) */
struct netif MACB_if;


/* Initialisation required by lwIP. */
void prvlwIPInit( void );

/* Initialisation of ethernet interfaces by reading config file */
static void prvEthernetConfigureInterface(void * param);


#if (NO_SYS == 0)
extern portTASK_FUNCTION( vBasicWEBServer, pvParameters );
void vStartEthernetTask( unsigned portBASE_TYPE uxPriority )
{
  /* Setup lwIP. */
  demolwIPInit();


}
#endif

/*!
 *  \brief start lwIP layer.
 */
void demolwIPInit( void )
{
	/* Initialize lwIP and its interface layer. */
	#if LWIP_STATS
		stats_init();
	#endif
	sys_init();
	mem_init();
	memp_init();
	pbuf_init();
	netif_init();

#if (NO_SYS == 0)	

	
	/* once TCP stack has been initalized, set hw and IP parameters, initialize MACB too */
	tcpip_init( prvEthernetConfigureInterface, NULL );
#else
	prvEthernetConfigureInterface(NULL);
#endif
}

/*!
 *  \brief set ethernet config 
 */
#if (NO_SYS == 0)
extern void vMBServerTask( void *arg );
#endif
static void prvEthernetConfigureInterface(void * param)
{
struct ip_addr xIpAddr, xNetMask, xGateway;
extern err_t ethernetif_init( struct netif *netif );
extern unsigned char my_ip[4];
unsigned char MacAddress[6];
//struct ip_addr ip;
   /* Default MAC addr. */
   MacAddress[0] = ETHERNET_CONF_ETHADDR0;
   MacAddress[1] = ETHERNET_CONF_ETHADDR1;
   MacAddress[2] = ETHERNET_CONF_ETHADDR2;
   MacAddress[3] = ETHERNET_CONF_ETHADDR3;
   MacAddress[4] = ETHERNET_CONF_ETHADDR4;
   MacAddress[5] = ETHERNET_CONF_ETHADDR5;
   
   /* pass the MAC address to MACB module */
   MSS_MAC_set_mac_address((uint8_t *)MacAddress);
	
   /* set MAC hardware address length to be used by lwIP */
   MACB_if.hwaddr_len = 6;
   
   /* set MAC hardware address to be used by lwIP */
   memcpy( MACB_if.hwaddr, MacAddress, MACB_if.hwaddr_len );
   
   /* Default ip addr. */
   IP4_ADDR( &xIpAddr,my_ip[0],my_ip[1],my_ip[2],my_ip[3] );
   
   /* Default Subnet mask. */
   IP4_ADDR( &xNetMask,ETHERNET_CONF_NET_MASK0,ETHERNET_CONF_NET_MASK1,ETHERNET_CONF_NET_MASK2,ETHERNET_CONF_NET_MASK3 );
   
   /* Default Gw addr. */
   IP4_ADDR( &xGateway,ETHERNET_CONF_GATEWAY_ADDR0,ETHERNET_CONF_GATEWAY_ADDR1,ETHERNET_CONF_GATEWAY_ADDR2,ETHERNET_CONF_GATEWAY_ADDR3 );
   
#if (NO_SYS == 0)  
   /* add data to netif */
   netif_add( &MACB_if, &xIpAddr, &xNetMask, &xGateway, NULL, ethernetif_init, tcpip_input );
#else
   netif_add( &MACB_if, &xIpAddr, &xNetMask, &xGateway, NULL, ethernetif_init, ip_input );
#endif   
   /* make it the default interface */
   netif_set_default( &MACB_if );
   
   /* bring it up */
   netif_set_up( &MACB_if );
   
#if (NO_SYS == 0)   
   sys_thread_new( "modbustask", vMBServerTask, NULL, 512, configMAX_PRIORITIES - 1 /*mainMB_TASK_PRIORITY*/ );
#endif
}



