/********************************************************************
	tcp_send.c
	Z-World, 2000

 	This sample program is used with Intellicom Series.
 	
	This program and tcp_respond.c are to be executed on two different
	Intellicom 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\icom\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.
	
********************************************************************/

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

/*
 * 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-1].addr);
	tcp_open(&tcpSock, 0, remIP, messageArray[key-1].port, NULL);
	sock_wait_established(&tcpSock, 10, NULL, &status);
	sock_write(&tcpSock, messageArray[key-1].text, strlen(messageArray[key-1].text)+1);
	dispClear();
	dispPrintf("Message sent");
	sock_wait_input(&tcpSock, 0, NULL, &status);
	numBytes = sock_fastread(&tcpSock, buffer, 2048);
	dispClear();
	if (numBytes != -1) {
		buffer[numBytes] = '\0';
		dispPrintf("%s", buffer);
	} else {
		dispPrintf("Error");
	}	
	sock_close(&tcpSock);
	while (tcp_tick(&tcpSock) != 0);
	return;

sock_err:
	switch(status) {
	case 1: /* foreign host closed */
		dispClear();
		dispPrintf("Error");
		break;

	case -1: /* timeout */
		dispClear();
		dispPrintf("Error");
		break;
	}
}

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.111";
	messageArray[0].port = 4040;

	messageArray[1].text = "This is test 2.";
	messageArray[1].addr = "10.10.6.111";
	messageArray[1].port = 4040;
		
	brdInit();
	sock_init();
	
	// Configure the upper row of keys on the keypad, in order from left
	// to right
	for (i = 0; i < 6; i++) {
		// Only enable a key if there is a corresponding message
		if (strcmp(messageArray[i].text, "") != 0) {
			keyConfig ( 5-i, i+1, 0, 0, 0, 0, 0 );
		} else {
			keyConfig ( 5-i, IGNORE, 0, 0, 0, 0, 0 );
		}
	}
	// Configure the lower row of keys on the keypad, in order from left
	// to right
	for (i = 6; i < 12; i++) {
		// Only enable a key if there is a corresponding message
		if (strcmp(messageArray[i].text, "") != 0) {
			keyConfig ( 19-i, i+1, 0, 0, 0, 0, 0 );
		} else {
			keyConfig ( 19-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
			waitfor ( key = keyGet() );	//	Wait for Keypress
			if (key != IGNORE) {
				SendMessage(key, messageArray);
			}
		}
	}
}