/**************************************************************************
 * (c) Copyright 2009 Actel Corporation.  All rights reserved.
 *
 *  Application demo for Smartfusion
 *
 *
 * Author : Actel Application Team
 * Rev     : 1.0.0.3
 *
 **************************************************************************/

/**************************************************************************/
/* Standard Includes */
/**************************************************************************/

#include <stdio.h>
#include <stdlib.h>

/**************************************************************************/
/* Driver Includes */
/**************************************************************************/

#include "../drivers/mss_ethernet_mac/mss_ethernet_mac.h"
#include "tcpip.h"
#include "conf_eth.h"

/**************************************************************************/
/* RTOS Includes */
/**************************************************************************/

#include "FreeRTOS.h"
#include "semphr.h"

/**************************************************************************/
/*Web Server Includes */
/**************************************************************************/

/**************************************************************************/
/* Definitions for Ethernet test */
/**************************************************************************/

#define OPEN_IP
#define BUF                       ((struct uip_eth_hdr *)&uip_buf[0])
#ifndef DEFAULT_NETMASK0
#define DEFAULT_NETMASK0          255
#endif

#ifndef DEFAULT_NETMASK1
#define DEFAULT_NETMASK1          255
#endif

#ifndef DEFAULT_NETMASK2
#define DEFAULT_NETMASK2          0
#endif

#ifndef DEFAULT_NETMASK3
#define DEFAULT_NETMASK3          0
#endif
#define TCP_PKT_SIZE              1600
#define IP_ADDR_LEN               4
#define PHY_ADDRESS               1
#define DHCP_ATTEMPTS             4
#define USER_RX_BUFF_SIZE         1600

/**************************************************************************/
/* Extern Declarations */
/**************************************************************************/
extern unsigned char              my_ip[4];
extern unsigned int               num_pkt_rx;
extern unsigned char              ip_known;
extern unsigned char              my_ip[IP_ADDR_LEN];
extern unsigned char              tcp_packet[TCP_PKT_SIZE];
extern unsigned char              dhcp_ip_found;
volatile unsigned char            oled_on_irq = 0;
volatile unsigned char            sw1_menu_scroll = 0;
volatile unsigned char            sw2_select = 0;
volatile unsigned char            led_on = 0;
volatile unsigned char            analog_mode = 0;
volatile unsigned char            hyper_teminal_on = 0;
volatile unsigned char            webServerFlag = 0;
extern unsigned char              oled_string[20];

char ethAddr[6] = {0xaa,0xbb,0xcc,0x66,0x55,0x44};
uint8_t                                     PRINT_MENU_var = 1;
extern volatile unsigned char inWebTask;
extern xSemaphoreHandle webSemHndl;

extern void menu_show(char *, char *);

/**************************************************************************/
/* Function to show IP address on OLED and UART */
/**************************************************************************/

void show_ip()
{
    char ipStr[20], *textStr;
    printf( "Successfully requested IP addr \n\r" );

    if((my_ip[0] == 192) &&
       (my_ip[1]==168)   &&
       (my_ip[2]==0)     &&
       (my_ip[3]==14))
    {
        printf( "Static IP address: " );
        textStr = "Browse Static IP:";
    }
    else
    {
        printf( "Dynamic IP address: " );
        textStr = "Browse Dynamic IP:";
    }
    printf( "%d.%d.%d.%d \n\r", my_ip[0], my_ip[1], my_ip[2], my_ip[3] );

}

/**************************************************************************/
/* Function to Initialize the MAC, setting the MAC address and */
/* fetches the IP address */
/**************************************************************************/
uint32_t mac_cfg;
void init_mac()
{
	uint32_t time_out = 0;
	int32_t i;
	int32_t rx_size;
    uint8_t rx_buffer[USER_RX_BUFF_SIZE];
    
	portCHAR MacAddress[6];

	   /* 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;
   

    MSS_MAC_init(PHY_ADDRESS );
	/*
	 * Configure the MAC.
	 */
	mac_cfg = MSS_MAC_get_configuration();

	mac_cfg &= ~( MSS_MAC_CFG_RECEIVE_ALL | MSS_MAC_CFG_STORE_AND_FORWARD | MSS_MAC_CFG_PASS_BAD_FRAMES );
//	mac_cfg &= ~(MSS_MAC_CFG_PASS_BAD_FRAMES );
	mac_cfg |=
//          MSS_MAC_CFG_RECEIVE_ALL |
          MSS_MAC_CFG_PROMISCUOUS_MODE |
          MSS_MAC_CFG_FULL_DUPLEX_MODE |
          MSS_MAC_CFG_TRANSMIT_THRESHOLD_MODE |
          MSS_MAC_CFG_THRESHOLD_CONTROL_00;

	MSS_MAC_configure(mac_cfg );

	MSS_MAC_set_mac_address((uint8_t *)MacAddress);

	ip_known = 0;
	num_pkt_rx = 0;
	time_out = 0;
	for (i = 0; i < 1600; i++)
	{
		rx_buffer[i] = 0;
	}
	
#ifdef OPEN_IP
	do{

    	send_bootp_packet(0);
    	do {		    		
			rx_size = MSS_MAC_rx_pckt_size();
			time_out++;
			if(dhcp_ip_found)
			   break;//goto here;
    	}while ( rx_size == 0 && (time_out < 3000000));
		MSS_MAC_rx_packet( rx_buffer, USER_RX_BUFF_SIZE, MSS_MAC_BLOCKING );
		num_pkt_rx++;		
		process_packet( rx_buffer );
    }while((!dhcp_ip_found) && (time_out < 7000000));
#endif
    show_ip();
} // End of ethernet


