/********************************************************************
	tcp_send.c
	Z-World, 2001
 	(Adapted from Intellicom's tcp_send.c) 	

 	This sample program is used with controller boards
 	equipped with ethernet, LCD and keypad.  A 122x32 pixel
 	display and 1x7 keypad module with LED's are assumed.

	This program and tcp_respond.c are to be executed on two different
	controller boards so that the two boards communicate with each
	other.  In the absence of a second board, pcrespond.exe can be
	used on the PC console side at the command prompt. This executable
	and source code is located in \samples\lcd_keypad\windows directory.

	When a key on the keypad is pressed, a message associated with
	that key	is sent to a specified destination address and port.
	The destination then responds to that message.  The response is
	displayed to the LCD.

	Note: Currently only LEFT and UP scroll keys are setup to
	      cause a message to be sent. 
		
********************************************************************/

fontInfo fi6x8;
windowFrame wholewindow;

/***********************************
 * Configuration                   *
 * -------------                   *
 * All fields in this section must *
 * be altered to match your local  *
 * network settings.               *
 ***********************************/

/*
 * You must change the following values to whatever
 * your local IP address and netmask are. Contact your
 * network administrator for these numbers.
 */
#define MY_IP_ADDRESS   "10.10.6.112"
#define MY_NETMASK      "255.255.255.0"
#define MY_GATEWAY		"10.10.6.1"

/*
 * Also note that you will need to set up the messages in the main()
 * function.
 */
 
/********************************
 * End of configuration section *
 ********************************/
 
#memmap xmem
#use "dcrtcp.lib"

// The structure that holds a message and the destination
typedef struct {
	char *text;
	char *addr;
	word port;
} Message;

#define IGNORE 0x20

/*
 * Send the message specified by the key value.  Note that the message
 * number is actually one less than the key number (since key number 0
 * cannot be used).
 */
void SendMessage(unsigned key, Message messageArray[])
{
	tcp_Socket tcpSock;
	longword remIP;
	char buffer[2048];
	int status;
	int numBytes;
	
	remIP = inet_addr(messageArray[key].addr);
	tcp_open(&tcpSock, 0, remIP, messageArray[key].port, NULL);
	sock_wait_established(&tcpSock, 10, NULL, &status);
	sock_write(&tcpSock, messageArray[key].text, strlen(messageArray[key].text)+1);
	glBlankScreen();
	TextGotoXY(&wholewindow, 0, 0 );
	TextPrintf(&wholewindow, "Message sent.......\nwaiting for response");
	sock_wait_input(&tcpSock, 0, NULL, &status);
	numBytes = sock_fastread(&tcpSock, buffer, 2048);
	glBlankScreen();
	if (numBytes != -1) {
		buffer[numBytes] = '\0';
		TextGotoXY(&wholewindow, 0, 0 );
		TextPrintf(&wholewindow, "%s", buffer);
	} else {
		TextGotoXY(&wholewindow, 0, 0 );
		TextPrintf(&wholewindow, "Error");
	}	
	sock_close(&tcpSock);
	while (tcp_tick(&tcpSock) != 0);
	return;

sock_err:
	switch(status) {
	case 1: /* foreign host closed */
		glBlankScreen();
		TextGotoXY(&wholewindow, 0, 0 );
		TextPrintf(&wholewindow, "Error");
		break;

	case -1: /* timeout */
		glBlankScreen();
		TextGotoXY(&wholewindow, 0, 0 );
		TextPrintf(&wholewindow, "Error");
		break;
	}
}

void msdelay (long sd)
{
	auto unsigned long t1;

	t1=MS_TIMER;
	for (t1=MS_TIMER; MS_TIMER<(sd+t1); );
}

void flashled(int led)
{
	switch (led)
	{
		case 0:
			led = 0;
			break;
		case 1:
			led = 1;
			break;
		case 2:
			led = 2;
			break;
		case 3:
			led = 3;
			break;
		default:
			led = 6;
			break;
	}
	ledOut(led,1);
	msdelay(400);
	ledOut(led,0);
}

void initsystem()
{
	auto int status;
	
	brdInit();
	glInit();
	glXFontInit(&fi6x8, 6, 8, 32, 127, Font6x8);
	status = TextWindowFrame(&wholewindow, &fi6x8, 0, 0, LCD_XS, LCD_YS);
	glBackLight(1);
	keyInit();
}

void main(void)
{
	Message messageArray[12];
	int i;
	unsigned key;	  // User keypress

	// Initialize the message array
	for (i = 0; i < 12; i++) {
		messageArray[i].text = "";
	}

	// Define messages here--note that you only need define the messages
	// you will actually use.
	messageArray[0].text = "This is test 1.";
	messageArray[0].addr = "10.10.6.100";
	messageArray[0].port = 4040;

	messageArray[1].text = "This is test 2.";
	messageArray[1].addr = "10.10.6.100";
	messageArray[1].port = 4040;
		
	initsystem();
	sock_init();
	
	// Configure keys on the keypad, in order from left
	// to right
	for (i = 0; i < 7; i++)
	{
		// Only enable a key if there is a corresponding message
		if (strcmp(messageArray[i].text, "") != 0)
		{
			keyConfig ( i, i+0x30, 0, 0, 0, 0, 0 );
		} else {
			keyConfig ( i, IGNORE, 0, 0, 0, 0, 0 );
		}
	}

	while (1) {
		costate {								//	Process Keypad Press/Hold/Release
			keyProcess ();
			waitfor ( DelayMs(10) );
		}
			
		costate {								//	Process Keypad Press/Hold/Release
			TextGotoXY(&wholewindow, 0, 0 );
			TextPrintf(&wholewindow, "Press the KEY that\ncorresponds to the\nmessage that you\nwant to send...");
			waitfor ( key = keyGet() );	//	Wait for Keypress
			if (key != 0)
				flashled(key-0x30);
			if (key != IGNORE) {
				SendMessage(key-0x30, messageArray);
			}
		}
	}
}