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

	EG2100_Serial.c
	2001, Z-World

	Example of a target board creating a web page while
	connected to a EG2100 RabbitLink board using the
	console serial port (PROG IN).

	This program demonstrates the ability of a Rabbit-
	based target board to update files on the web
	server of the RabbitLink board it is connected to
	with a serial cable.

	NOTE that the RabbitLink PROG IN port requires
	TTL-level signals!
	
 ********************************************************/

#define	BINBUFSIZE	63
#define	BOUTBUFSIZE	63

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

/*
	Read a response from the RabbitLink.  Blocks until
	some characters are read or the timeout limit passes.
	Make sure that the buffer passed to it is large
	enough to contain the entire response or characters
	may be lost.
	
		parameter 1 = pointer to char buffer
		parameter 2 = length of buffer
		parameter 3 = timeout in milliseconds
 */
 
int get_ser_response(char *buf, int buflen, int timeout)
{
	int	done, len;
	unsigned long	t0;

	t0 = MS_TIMER;

	done = 0;
	while (!done && (MS_TIMER < (t0 + timeout))) {
		len = serBread(buf, buflen, 10);
		if (len > 0) {
			buf[len] = '\0';
			done = 1;
		}
	}
	
	return (len);
}

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

#define	CTRL_D	0x04

main()
{
	char	s[24];
	int	len;
	long	secs;
	struct tm t;	
		
	serBopen(57600);

	// make sure we are not in the middle of a command
	serBputs("\n");

	// turn the RabbitLink's local echo off
	serBputs("echo off\n");
	
	get_ser_response(s, 23, 250);
	printf("RabbitLink returned:  %s", s);

	// delete old web page (if it exists)
	serBputs("delete demo.html\n");

	get_ser_response(s, 23, 250);
	printf("RabbitLink returned:  %s", s);
		
	// create the web page
	serBputs("put demo.html\n");
	serBputs("<H2>A demonstration web page</H2>\n");
	
	// get the current time	
	tm_rd(&t);
	sprintf(s, "Created on %02d/%02d/%04d at %02d:%02d:%02d\n",
				t.tm_mon, t.tm_mday, t.tm_year+1900,
				t.tm_hour, t.tm_min, t.tm_sec);
	serBputs(s);

	// end web page
	sprintf(s, "%c", CTRL_D);
	serBputs(s);

	// this response takes a bit longer
	get_ser_response(s, 23, 1500);
	printf("RabbitLink returned:  %s", s);	
}
