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


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();

#define MAX_IPADDRESS_LENGTH    15

/*
 *		LIFTED FROM DIALOG.C
 *
 *  Function: static int kbGetIpAddress (char *ipAddrDesc, unsigned long *ipAddress)
 *
 *  Description:
 *
 *      This routine print out what kind of IP address it needs and gets it from keyboard.
 *
 *  Notes:
 *
 *      THIS CODE MUST BE CUSTOMIZED FOR YOUR BOARD SHOULD YOU USE DIFFERENT 
 *      DISPLAY FORMAT. The implementation below is for NET+OS development 
 *      boards only.
 *
 *  Parameters:
 *
 *      ipAddrDesc    points to the string which contains some descriptions.
 *      ipAddres      points to an unsigned long which holds the old IP address.
 *                    upon return, will hold the new IP address if any.
 *
 *  Return Values:
 *
 *      0             no update, keep the old IP address
 *      1             got a new IP address     
 *
 */ 
int kbGetIpAddress (char *ipAddrDesc, unsigned long *ipAddress)
{
	int rc;

	char buffer[128];
	unsigned long   IPAddr;

	while(1)
	{
		printf ("%s ", ipAddrDesc);
		//        printIpAddress("IP address [%d.%d.%d.%d]? ", *ipAddress);
		readLine (buffer);
		if(buffer[0] == '\0')
		{
			rc = 0;
			break;
		}

		if(checkIpAddress(buffer))
		{
			*ipAddress = NAInet_addr(buffer);

			printf ("  %s ", ipAddrDesc);
			//          	printIpAddress("IP address is %d.%d.%d.%d\r\n", *ipAddress);
			rc = 1;
			break;
		}

		printf("Invalid IP address\r\n");
	}

	return(rc);
}

/*
 *		LIFTED FROM DIALOG.C
 *
 *  Function: static int  checkIpAddress(char *buffer)
 *
 *  Description:
 *
 *      This routine validates an IP address.
 *
 *  Parameters:
 *
 *      buffer    points to the string which contains an IP address in "a.b.c.d" format.
 *
 *  Return Values:
 *
 *      0         invalid
 *      1         valid
 *
 */ 
static int  checkIpAddress(char *buffer)
{
	unsigned int length,i;
	unsigned int j =0;
	unsigned int val;


	length = strlen(buffer);

	if((length > MAX_IPADDRESS_LENGTH))
		return(0);
	else
	{
		for(i=0; i<length; i++)
		{

			if(buffer[i] =='.')
			{
				j++;
				if(j > 3)	/* only three '.' allowed */
					return(0);
			}
			else if(!(isdigit(buffer[i]))) /* all input char must be numeric */
				return(0);
		} 

		for(i = 0; i < 4; i++)
		{
			if(*buffer != '\0')
			{
				val = 0;
				while(*buffer != '\0' && *buffer != '.')
				{
					val *= 10;
					val += *buffer - '0';
					buffer++;
				}
				if(val > 255)
					return(0);		 /* any field should be less than 256 */

				if(*buffer != '\0')
					buffer++;
			}
		}   
	}
	return(1); 
}  
/*
 *		LIFTED FROM DIALOG.C
 *
 *  Function: static void printIpAddress(char *buffer, unsigned long ip)
 *
 *  Description:
 *
 *      This routine print out the board's IP address with descriptions.
 *
 *  Notes:
 *
 *      THIS CODE MUST BE CUSTOMIZED FOR YOUR BOARD SHOULD YOU USE DIFFERENT 
 *      DISPLAY FORMAT. The implementation below is for NET+OS development 
 *      boards only.
 *
 *  Parameters:
 *
 *      buffer    points to the string which contains some descriptions.
 *      ip        ip address
 *
 *  Return Values:
 *
 *      none
 *
 */ 
void printIpAddress(char *buffer, unsigned long ip)
{
#ifndef _LITTLE_ENDIAN
	printf(buffer, (ip >> 24) & 0xFF,
		   (ip >> 16) & 0xFF,
		   (ip >> 8)  & 0xFF,
		   (ip) & 0xFF);
#else
	printf(buffer, (ip) & 0xFF,
		   (ip >> 8)  & 0xFF,
		   (ip >> 16) & 0xFF,
		   (ip >> 24) & 0xFF);
#endif
} 

/*
 *	unsigned long kbGetHexValue(char * buffer)
 *					
 *	returns the hex value
 */


unsigned long kbGetHexValue(char * buffer)
{
	int             i = 0;
	unsigned long   val = 0xffffffff;
	unsigned char   temp;

	printf("<hex> ");
	readLine(buffer);

	while(*buffer != '\0' && i < 9)
	{
		if(*buffer >= 0 && *buffer <= 9)
		{
			val *= 0x10;
			val += *buffer - '0';
		}
		else
		{
			if(*buffer >= 'a' && *buffer <= 'f')
			{
				temp = *buffer - 'a';
				val *= 0x10;
				val += temp;
			}
			else
			{
				if(*buffer >= 'A' && *buffer <= 'F')
				{
					temp = *buffer - 'A';
					val *= 0x10;
					val += temp;
				}
				else // if it's not a decimal digit (0-9), and not a letter a-f or A-F,
				{
					// then it's not a valid hex digit so get the hell outta here.

					printf("\nINVALID HEX DIGIT IN THI STRING: %s\n", buffer);
					return(-1); 
					// The caller won't necessarily recognize this as
					// a failure, since it is a legitimate hex number (ffffffff).
				}
			}
		}
		buffer++;
		i++;
	}
	return(val);
}


/*
 *
 *  Function: void printfMACAddress()
 *
 *  Description:
 *
 *      This function displays the MAC address is the familiar format
 *		(each two digit pair separated by a colon). 
 *
 *  Parameters:	M, pointer to an array of digit pairs (the product of
 *				customizeGetMACAddress()) in binary/hex form.
 *      
 *		none
 *
 *  Return Values:
 *
 *      none
 *
 */ 
void    printMACAddress(unsigned char * M)
{
	int             i;
	char    tempM;

	// To print the MAC Address - iterate through the 6 bytes and print
	//	each digit with a colon printed between pairs to create
	// the expected display.

	printf("MAC Address = ");
	for(i = 0; i < 6; i++)
	{
		tempM = M[i];
		printMACPair(tempM);
		if(i < 5)
			printf(":");
	}
	printf("\n");

}

/*
 *
 *  Function: void printfMACPair()
 *
 *  Description:
 *
 *      This function prints the digit pair representing the 8 bit number passed in. 
 *
 *  Parameters:	M, the binary/hex number representing one sixth of the MAC address.
 *      
 *		none
 *
 *  Return Values:
 *
 *      none
 *
 */ 
void    printMACPair(unsigned char M)
{
	unsigned char   a, b;
	a = (M & 0xf0) >> 4;
	b = (M & 0x0f);
	if(a > 9)
	{
		a -= 10;
		a += 'A';
	}
	else
		a += '0';

	printf("%c", a);
	if(b > 9)
	{
		b -= 10;
		b += 'A';

	}
	else
		b += '0';
	printf("%c",b);

}
/*
 *
 *  Function: static void readLine(char * buf)
 *
 *  Description:
 *
 *      This routine gets one line of the information input from keyboard.
 *      It prints out the information as well.
 *
 *  Parameters:
 *      buf     points to an array to hold the information
 *
 *  Return Values:
 *
 *      none
 *
 */ 
void readLine(char * buf)
{
	char character;
	int i = 0;
	do
	{
		character = getchar();
		if(character == '\b')
		{
			if(i > 0)
			{
				putchar(character);
				putchar(0x20);	 /*space*/
				putchar(character);
				--i;
			}
		}

		else if(character != '\n')
		{
			buf[i] = character;
			putchar(character);
			++i;
		}
	}
	while(character != '\r');
	putchar('\n');

	buf[i-1] = '\0';
}  


