/*
 *            Copyright (c) 1998-2004 by NETsilicon Inc.
 *
 *  This software is copyrighted by and is the sole property of 
 *  NETsilicon.  All rights, title, ownership, or other interests
 *  in the software remain the property of NETsilicon.  This
 *  software may only be used in accordance with the corresponding
 *  license agreement.  Any unauthorized use, duplication, transmission,  
 *  distribution, or disclosure of this software is expressly forbidden.
 *
 *  This Copyright notice may not be removed or modified without prior
 *  written consent of NETsilicon.
 *
 *  NETsilicon, reserves the right to modify this software
 *  without notice.
 *
 *  NETsilicon
 *  411 Waverley Oaks Road                  USA 781.647.1234
 *  Suite 304                               http://www.netsilicon.com
 *  Waltham, MA 02452                       AmericaSales@netsilicon.com
 *
 *************************************************************************
 *
 *  Description
 *  ====================================================================
 *  This file is an example of changing ACE parameters.
 *
 * 8/2/04	RL	This is a sample of stopping ACE, reconfiguring
 *			  	the protocols, restarting ACE. It include a user interface
 *				for the serial port, allowing the user to make a few choices
 *				regarding protocol and IP options.   
 *             
 *
 */

#include <stdio.h>
#include <tx_api.h>
#include <hservapi.h>
#include <sysAccess.h>
#include <ace_params.h>
#include <boardParams.h>

//#define MAX_IPADDRESS_LENGTH    15


#include	"gen_utils.h"

// prototypes

/*
int     kbGetIpAddress (char *ipAddrDesc, unsigned long *ipAddress);
void    printIpAddress(char *buffer, unsigned long ip);
int     checkIpAddress(char *buffer);
unsigned long kbGetHexValue(char * buffer);
void    printMACAddress(unsigned char * M);
void    printMACPair(unsigned char M);
void    readLine(char * buf);

*/

void    getNVParams();

//	Global variables for this module

aceProtoStartInfo       staticIPStartInfo = {ACE_PROT_STATIC, 0, 0, ACE_CONT_IF_GOT_ADDRESS};
aceProtoStartInfo       autoIPStartInfo = {ACE_PROT_AUTOIP, 0, 0, ACE_CONT_IF_GOT_ADDRESS};

aceConfigInfo           ACEFromNV;
unsigned long           ipaddr, subnetMask, defGW;

aceConfigInterfaceInfo  * ace_intfc;

configAceStaticInfo     * staticIPInfo;
configAceAutoipInfo     * autoIPInfo;
configAceDhcpInfo       * dhcpInfo;
configAcePingArpInfo    * pingArpInfo;
configAceBootpInfo      * bootPInfo;
configAceRarpInfo       * rarpInfo;
devBoardParamsType  	params;



void    aceTest()
{
	char            buffer[128];
	int             result = -1;

	// mode selection - codes the user's choice into enum.
	typedef enum
	{
		DEFAULT_MODE,
		STATIC_MODE,
		AUTOIP_MODE,
		DHCP_MODE,
		RARP_MODE,
		PING_ARP_MODE,
	}IPADDR_MODE;

	IPADDR_MODE AddressMode = STATIC_MODE;

	// some default values

	unsigned long   gatewayIP = 0x0a34010f;			// 10.52.1.15
	unsigned long   localIP = 0x0a343117;			// 10.52.49.23
	unsigned long   subnetMask = 0xfffff800;		// 255.255.248.0
	unsigned long   autoip_start_addr = 0x0a343119;	// 10.52.49.25

	/////////////////////////////////////////////////////////
	// Some ACE variables and structures
	/////////////////////////////////////////////////////////

	aceProtoStartInfo   staticIPStartInfo = {ACE_PROT_STATIC, 0, 0, ACE_CONT_IF_GOT_ADDRESS};
	aceProtoStartInfo   autoIPStartInfo = {ACE_PROT_AUTOIP, 0, 0, ACE_ALWAYS_SHUTDOWN};
	aceProtoStartInfo   dhcpStartInfo = {ACE_PROT_DHCP, 0, 0, ACE_CONT_IF_GOT_ADDRESS}; 
	aceProtoStartInfo   rarpStartInfo = {ACE_PROT_RARP, 0, 0, ACE_CONT_IF_GOT_ADDRESS};
	aceProtoStartInfo   bootPStartInfo = {ACE_PROT_BOOTP, 0, 0, ACE_CONT_IF_GOT_ADDRESS}; 


	char ipAddrDesc[16];
	unsigned long ipAddress;

	////////////////////////////////////////////////////////////
	// First get a copy of the ACE Configuration from NVRAM.
	// This function displays some parameters on the screen
	// (via printf).
	////////////////////////////////////////////////////////////

	getNVParams(&params);

	printf("\naceTest() - select ace mode by typing one of the following:\n");
	printf("autoip\nstatic\ndhcp\nnone\n");
	printf("What is your selection?");
	readLine(&buffer[0]);

	if(buffer[0] == "\0")
		printf("Empty line, default mode is static IP\n");
	else
	{
		if(strncmp(&buffer[0], "static", 4) == 0)
		{
			printf("Static ip mode selected, type in the IP address to use> ");
			NAWait(1000);
			AddressMode = STATIC_MODE;
			if(kbGetIpAddress (&ipAddrDesc, &ipAddress) == 1)
				localIP = ipAddress;
			else
			{
				printf("Bad IP address - I will not take it pffffffft!\n");
				return;
			}
		}
		else
		{
			if(strncmp(&buffer[0], "autoip", 4) == 0)
			{
				printf("AutoIP mode selected type in the IP address to use>");
				NAWait(1000);
				AddressMode = AUTOIP_MODE;
				if(kbGetIpAddress (&ipAddrDesc, &ipAddress) == 1)
					localIP = ipAddress;
				else
				{
					printf("Bad IP address - I will not take it pffffffft!\n");
					return;
				}
				printf("Specify autoIP start delay>");
				autoIPStartInfo.delay_before_start = kbGetHexValue(&buffer[0]);
			}
			else
			{
				if(strncmp(&buffer[0], "dhcp", 4) == 0)
				{
					printf("DHCP Mode selected\n");
					AddressMode = DHCP_MODE;
				}
				else
				{
					AddressMode = DEFAULT_MODE;
					printf("None selected, skipping Ace Setup\n");
				}
			}
		}
	}


	if(AddressMode == DEFAULT_MODE)
		return;


	/////////////////////////////////////////////////////
	// ACE reconfiguration sample
	/////////////////////////////////////////////////////


	if(AddressMode == AUTOIP_MODE || AddressMode == STATIC_MODE)
		aceStop("eth0");
	else
		printf("Unsupported mode selected - aborting.\n");

	/////////////////////////////////////////////////////////
	// Change the parameters (ACE parameters) in the NVRAM
	// using the aceParams API.
	/////////////////////////////////////////////////////////

	if(AddressMode == STATIC_MODE)
	{
		printf("aceTest() Static mode\n");

		// Get the info from: params.aceConfig.ace_interface_info[0].static_config
		// and save it in a configAceStaticInfo structure.
		// This is for debug only. The values in this structure 
		// are not used. The other staticIPInfo structure is filled below
		// and contains teh data used to configure ACE static IP.

		//		result = customizeAceGetStaticConfig("eth0", staticIPInfo2);

		// Now set the Static IP parameters the way we want them.
		// First a little initialization...

		staticIPInfo->auto_assign = TRUE;
		strcpy(staticIPInfo->domain_name,"");
		staticIPInfo->gateway = gatewayIP;
		staticIPInfo->ip_address = localIP;
		staticIPInfo->isConfigValid = TRUE;
		staticIPInfo->isEnabled = TRUE;
		//		staticIPInfo->name_server_address[0] = 0;
		staticIPInfo->startInfo = staticIPStartInfo;
		staticIPInfo->subnet_mask = subnetMask;

		result = customizeAceSetStaticConfig("eth0", staticIPInfo);
		if(result != BP_SUCCESS)
		{
			printf("STATIC IP MODE: Failed to set static config info - bye-bye\n");
			exit(-6);
		}
		else
		{
			printf("STATIC IP MODE: IP = %x, GW = %x, Mask = %x\n", staticIPInfo->ip_address,
				   staticIPInfo->gateway, staticIPInfo->subnet_mask);
		}

		// falls through to ace start call at the bottom ...
	}

	///////////////////////////////////////////
	// For auto ip selection.

	if(AddressMode == AUTOIP_MODE)
	{
		printf("AutoIP mode - setting parameters.\n");

		// First get the static ip config
		// and disable it.

		autoIPInfo->isConfigValid = TRUE;
		autoIPInfo->isEnabled = TRUE;
		autoIPInfo->startInfo = autoIPStartInfo;
		//		autoIPInfo->autoip_local_addr = BP_AUTOIP_START_WITH_RANDOM_IP_ADDRESS;
		autoIPInfo->autoip_local_addr = localIP;
		//		autoIPInfo->autoip_local_addr = autoip_start_addr;


		result = customizeAceSetAutoipConfig("eth0", autoIPInfo);
		if(result != BP_SUCCESS)
		{
			printf("AUTO IP MODE: Failed to set auto ip config info - bye-bye\n");
			exit(-10);
		}
		else
		{
			printf("AUTO IP MODE: Starting with ");
			if(autoIPInfo->autoip_local_addr == BP_AUTOIP_START_WITH_RANDOM_IP_ADDRESS)
				printf("random ip address\n");
			else
				printf("%x\n", autoIPInfo->autoip_local_addr);
		}
	}

	if(AddressMode == DHCP_MODE)
	{
		printf("DHCP mode - setting parameters.\n");

		dhcpInfo->isConfigValid = TRUE;
		dhcpInfo->isEnabled = TRUE;
		dhcpInfo->startInfo = dhcpStartInfo;

		/*
		 *	Disable autoip and static ip to ensure dhcp is tried.
		 *	also disable RARP, PingARP and BootP
		 */

		autoIPInfo->isEnabled = FALSE;
		staticIPInfo->isEnabled = FALSE;
		rarpInfo->isEnabled = FALSE;
		bootPInfo->isEnabled = FALSE;


		// Save all these modified parameters to NVRAM (one protocol at a time)

		customizeAceSetAutoipConfig("eth0", autoIPInfo);
		customizeAceSetStaticConfig("eth0", staticIPInfo);
		customizeAceSetDhcpConfig("eth0", dhcpInfo);
		customizeAceSetRarpConfig("eth0", rarpInfo);
		customizeAceSetBootpConfig("eth0", bootPInfo);

		printf("DHCP implemented - ACE restarting with new parameters.\n");

		getNVParams(&params);
	}

	/////////////////////////////////////////////////////////
	// Once the parameters are set up, start ACE.
	/////////////////////////////////////////////////////////

	printf("starting ACE\n");
	customizeStartAce();	// starts ACE with the parameters in the NVRAM.

	NAWait(10000);
	customizeGetIPParameters(&ipaddr, &subnetMask, &defGW);
	printf("Checked parameters after 10 second delay:\n");
	printf("IP Address = %x, Mask = %x, Gateway = %x\n", ipaddr, subnetMask, defGW);

}
/*
 *
 *  Function: void getNVParams()
 *
 *  Description:
 *
 *      This function fetches the ACE configuration parameters from the NVRAM.
 *
 *  Parameters:
 *      
 *		none
 *
 *  Return Values:
 *
 *      none
 *
 */ 

void    getNVParams()
{

	char            MACAddr[16];

	//	result = customizeAceGetStaticConfig("eth0", &staticIPInfo);

	customizeReadDevBoardParams(&params);
	customizeGetIPParameters(&ipaddr, &subnetMask, &defGW);

	ACEFromNV = params.aceConfig;
	ace_intfc = &(ACEFromNV.ace_interface_info[0]);

	staticIPInfo = &(ace_intfc->static_config);
	autoIPInfo = &(ace_intfc->autoip_config);
	dhcpInfo = &(ace_intfc->dhcp_config);
	pingArpInfo = &(ace_intfc->ping_arp_config);
	bootPInfo = &(ace_intfc->bootp_config);
	rarpInfo = &(ace_intfc->rarp_config);

	printf("getNVParams() - Configuration dump:\n");
	customizeGetMACAddress((BYTE *) &MACAddr[0]);
	printf("static IP info: IP = %x gateway = %x mask = %x\n",staticIPInfo->ip_address,
		   staticIPInfo->gateway, staticIPInfo->subnet_mask);

	printMACAddress((unsigned char *) &MACAddr[0]);


	printf("auto IP info: IP = %x\n", autoIPInfo->autoip_local_addr);

	printf("dhcp info: IP suggested = %x lease time = %x server IP = %x\n", dhcpInfo->suggested_ip_address, dhcpInfo->suggested_lease_time,
		   dhcpInfo->server_ip_address);

	printf("pingArpInfo: start_info.delay = %x\n", pingArpInfo->startInfo.delay_before_start);
	printf("bootPInfo: file = %s   server IP = %x\n", bootPInfo->bootfile_name, bootPInfo->server_ip_address);
	printf("rarpInfo: rexint = %x\n", rarpInfo->rarp_rexint);
}


