/*****
ppp_answer.c

static web server over PPP, handles peers that call in
Once connected, the server will print outs it's IP address on STDIO
You can then access the static page from that address: 'http://x.x.x.x/'

You will need to change the default addresses, and
the send/expect sequence at the beginning of the program

********/

#define MODEM_DEBUG 1

#define PKTDRV "ppp.lib"

//IP address will be assigned by the ISP
#define MY_IP_ADDRESS "10.1.10.1"

#define MY_NETMASK "255.255.255.255"

//this may not be necessary
#define MY_GATEWAY "10.1.10.2"
#define MY_NAMESERVER "10.1.10.2"

#define CINBUFSIZE 31
#define COUTBUFSIZE 31

#define PPP_RTSPORT PBDR
#define PPP_RTSSHADOW PBDRShadow
#define PPP_RTSPIN 7
#define PPP_CTSPORT PBDR
#define PPP_CTSPIN 0

#define HTTP_MAXSERVERS 2
#define MAX_TCP_SOCKET_BUFFERS 2

#memmap xmem
#use "dcrtcp.lib"
#use "modem.lib"
#use "http.lib"


#define TIMEZONE        -8

#define LCP_TIMEOUT 5000



//#define FRAGSUPPORT		//make sure FRAGSUPPORT is on

/*
 *  ximport is a Dynamic C language feature that takes the binary image
 *  of a file, places it in extended memory on the controller, and
 *  associates a symbol with the physical address on the controller of
 *  the image.
 *
 */

#ximport "samples/tcpip/http/pages/static.html"    index_html
#ximport "samples/tcpip/http/pages/rabbit1.gif"    rabbit1_gif

/*
 *  http_types gives the HTTP server hints about handling incoming
 *  requests.  The server compares the extension of the incoming
 *  request with the http_types list and returns the second field
 *  as the Content-Type field.  The third field defines a custom
 *  function to handle that mime type.
 *
 *  You can get a list of mime types from Netscape's browser in:
 *
 *  Edit->Preferences->Navigator->Applications
 *
 */

/* the default mime type for '/' must be first */
const HttpType http_types[] =
{
   { ".html", "text/html", NULL},
   { ".gif", "image/gif", NULL}
};

/*
 *  http_flashspec assocates the file image we brought in with ximport
 *  and associates it with its name on the webserver.  In this example
 *  the file "samples/http/pages/static.html" will be sent to the 
 *  client when they request either "http://yoururl.com/" or
 *  "http://yoururl.com/index.html"
 *
 */

const HttpSpec http_flashspec[] = 
{
   { HTTPSPEC_FILE,  "/",              index_html,    NULL, 0, NULL, NULL},
   { HTTPSPEC_FILE,  "/index.html",    index_html,    NULL, 0, NULL, NULL},
   { HTTPSPEC_FILE,  "/rabbit1.gif",   rabbit1_gif,   NULL, 0, NULL, NULL},
};

void main()
{
	auto unsigned long t;
	auto char buffer[100];


	while(1)
	{
		if(ModemOpen(19200) == 0)
		{
			printf("No modem detected\n");
			exit(-1);
		}
		if(ModemInit())
		{
			printf("Modem Initialized\n");
		}
		else
		{
			printf("Could not init modem\n");
			exit(-1);
		}
	
		//set to answer phone
		while(ModemRinging() == 0)
		{
			//do other stuff while waiting for caller
			continue;
		}
		ModemSend("ATA\r");
		printf("Answered call\n");
		if(ModemExpect("CONNECT", 30000) == 0)
		{
			printf("Did not connect to caller\n");
			
		}
		else
		{
			//connected
			sock_init();
			PPPinit(19200);
			PPPflowcontrolOn();

			PPPnegotiateIP(inet_addr("10.1.10.1"), inet_addr("10.1.10.2"));
			PPPsetAuthenticator("name", "password");

		
			if(PPPstart(5000, 2))
			{
				printf("PPP established\n");
				printf("IP address is %s\n", inet_ntoa( buffer, gethostid()));

				http_init();
			   tcp_reserveport(80);
   
  			 	while (LCPState == 3 && ModemConnected())
  			 	{
     	 			http_handler();
   			}
   		}
			else
			{
				printf("PPP failed\n");				
			}
		} 
		if(PPPshutdown(2000) == 0)
		{
			printf("PPP terminate failed\n");
		}
		PPPclose();
		ModemOpen(57600);
		ModemHangup();
		ModemClose();
	}		 	  	 	  	
}
