/*****************************************************

	act_open.c
	Copyright (C) Z-World, 2001

	Demonstrate use of DeviceMate to perform TCP on our
	behalf.  A socket (or 2) is actively opened to an echo
	server.  This demo works in step-by-step mode.

	The \samples\dmunit\tcp.c program must be running on the
	DeviceMate processor.

	In addition, a TCP echo server must be running on some other
	machine at ports 6656 (and 6657, if TWOSOCK defined).  This
	can be changed.  Actually, the default echo service is on
	port 7 so you might try this number (one socket only).
	
******************************************************/


/*
 * These definitions for this demo only.
 */
#define TWOSOCK							/* Define if two sockets at once */
#define REMOTE_HOST "10.10.6.178"	/* The echo server host */
#define REMOTE_PORT 6656				/* The echo port number on that host */

/* Request use of TCP services */
#define USE_TC_TCPIP

/*
 * Uncomment this line if this program will be loaded over ethernet. This will
 * force the DevMate communication to use serial port A, re-using the programming
 * link that was used to download the program.
 */
//#define USE_TC_LOADER

#ifdef CC_VER
	/* Dynamic C */
	#memmap xmem
	#use "tc_conf.lib"
#else
	/* Non-Rabbit */
	#include "tc_conf.h"
	#include <stdio.h>	
#endif

#define BOOFSIZE 1500
char buf[BOOFSIZE];
char buf2[BOOFSIZE];
char rxbuf[BOOFSIZE];
char rxbuf2[BOOFSIZE];
	
int main()
{
	int rc;
	int socket, socket2;
	int num_tx, num_tx2;
	int num_rx, num_rx2;
	int len;
	uint32 stamp;

	printf("Initializing 1...\n");
	devmate_init();

	/* Initialize TCP. */
	printf("2...\n");
	rc = devmate_sock_init(NULL, NULL, NULL, NULL, NULL, NULL);
	if (rc <= 0) {
		printf("Failed devmate_sock_init.\n");
		exit(20);
	}

	/*
	 * Open a socket, then allocate a local TX/RX buffer
	 */
	while (!(socket = devmate_tcp_open(1, 0, REMOTE_HOST, REMOTE_PORT)))
		devmate_tick();
	if (socket < 0) {
		printf("Failed devmate_tcp_open.\n");
		exit(21);
	}
#ifdef TWOSOCK
	while (!(socket2 = devmate_tcp_open(2, 0, REMOTE_HOST, REMOTE_PORT+1)))
		devmate_tick();
	if (socket2 < 0) {
		printf("Failed devmate_tcp_open 2.\n");
		exit(21);
	}
#endif

	while (!devmate_tcp_isestablished(socket));
	printf("Established...\n");
#ifdef TWOSOCK
	while (!devmate_tcp_isestablished(socket2));
	printf("Established 2...\n");
#endif

	/*
	 * Write a string to the socket and read it back (assuming
	 * talking to echo server).
	 */
	memset(buf, '*', sizeof(buf));
	memset(rxbuf, 0, sizeof(rxbuf));
	strcpy(buf, "Hello world");
	printf("Sending1: '%s'\n", buf);
#ifdef TWOSOCK
	memset(buf2, '*', sizeof(buf2));
	memset(rxbuf2, 0, sizeof(rxbuf2));
	strcpy(buf2, "Goodbye world");
	printf("Sending2: '%s'\n", buf2);
#endif
	len = sizeof(buf);
	num_tx = 0;
	num_rx = 0;
	num_tx2 = 0;
	num_rx2 = 0;
	while (num_tx < len || num_rx < len
#ifdef TWOSOCK
			|| num_tx2 < len || num_rx2 < len
#endif
			) {
		rc = devmate_tcp_fastwrite(socket, buf + num_tx, len - num_tx);
		if (rc < 0) {
			printf("Failed devmate_tcp_fastwrite.\n");
			exit(22);
		}
		//if (rc > 0)
		//	printf("sent1 %d\n", rc);
		num_tx += rc;

#ifdef TWOSOCK
		rc = devmate_tcp_fastwrite(socket2, buf2 + num_tx2, len - num_tx2);
		if (rc < 0) {
			printf("Failed devmate_tcp_fastwrite 2.\n");
			exit(22);
		}
		//if (rc > 0)
		//	printf("sent2 %d\n", rc);
		num_tx2 += rc;
#endif

		rc = devmate_tcp_fastread(socket, rxbuf + num_rx, len - num_rx);
		if (rc < 0) {
			printf("Failed devmate_tcp_fastread.\n");
			break;
		}
		//if (rc > 0)
		//	printf("received1 %d\n", rc);
		num_rx += rc;

#ifdef TWOSOCK
		rc = devmate_tcp_fastread(socket2, rxbuf2 + num_rx2, len - num_rx2);
		if (rc < 0) {
			printf("Failed devmate_tcp_fastread 2.\n");
			break;
		}
		//if (rc > 0)
		//	printf("received2 %d\n", rc);
		num_rx2 += rc;
#endif
	}


	printf("Got response1 (num_rx=%d): %s\n", num_rx, rxbuf);
#ifdef TWOSOCK
	printf("Got response2 (num_rx2=%d): %s\n", num_rx2, rxbuf2);
#endif

	/*
	 * Close the socket.
	 */

	devmate_tcp_close(socket);
#ifdef TWOSOCK
	devmate_tcp_close(socket2);
#endif

	while (!devmate_tcp_isclosed(socket));
#ifdef TWOSOCK
	while (!devmate_tcp_isclosed(socket2));
#endif

	printf("All done.\n");

	return 0;
}