/*****
Tests modem.lib
Trys to connect to mother.com

Using cofunctions from modem.lib and ppp.lib

Be sure to change the default addresses, and the mail_to[], mail_from[] globals
You will also need to alter the Send/Expect sequence at the beginning of the program

********/

#define PKTDRV "ppp.lib"

//Macros for standard login procedure. You may still need to alter expected strings below
//Don't forget the carriage returns '\r'
#define DIALUP_DIALSTRING "ATDT5551000\r"
#define DIALUP_NAME "name\r"
#define DIALUP_PASSWORD "password\r"

//IP is assigned by the ISP
#define MY_IP_ADDRESS "0.0.0.0"

#define MY_NETMASK "255.255.255.255"
#define MY_GATEWAY "10.1.1.1"
#define MY_NAMESERVER "10.1.1.2"
#define SMTP_SERVER "mail.yourisp.com"

#define SMTP_DEBUG 1

#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


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


#define TIMEZONE        -8

#define LCP_TIMEOUT 5000

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


const char mail_to[] = "you@you.com";
const char mail_from[] = "rabbit@yourisp.com";
const char mail_subject[] = "Mail from Rabbit";
const char mail_body[] = "This is a test message sent by a Rabbit through an ISP.";


void main()
{
	auto unsigned long t;
	auto char buffer[100];
	auto int mail_status;
	auto int status;
	auto int run_flag;

	run_flag = 1;

	while(run_flag)
	{	
		costate
		{
			if(ModemOpen(57600) == 0)
			{
				printf("No modem detected\n");
			}
			wfd { status = CofModemInit(); }
			if(status)
			{
				printf("Modem Initialized\n");
			}
			else
			{
				printf("Could not init modem\n");
				exit(-1);
			}	

			wfd { CofModemSend(DIALUP_DIALSTRING); }
			wfd { status = CofModemExpect("ONNECT", 30000); }
			if( status == 0)
			{
				printf("Expected 'CONNECT'\n");
				exit(-1);
			}

			wfd { status = CofModemExpect("ogin:", 10000); }
			if(status == 0)
			{
				printf("Expected 'Login:'\n");
				exit(-1);
			}
			wfd { CofModemSend(DIALUP_NAME); }
			wfd { status = CofModemExpect("word:", 2000); }
			if( status == 0)
			{
				printf("Expected Password\n");
				exit(-1);
			}
			wfd { CofModemSend(DIALUP_PASSWORD); }		//secret password, erase later
			wfd { status = CofModemExpect("PPP", 5000); }
			if( status == 0)
			{
				printf("Didn't get PPP session\n");
				exit(-1);
			}	
			ModemClose();
	
			sock_init();
			PPPinit(57600);
			PPPflowcontrolOn();

			//PPPnegotiateIP(inet_addr("10.1.10.1"), inet_addr("10.1.10.2"));

			wfd { status = CofPPPstart(5000, 3); }		
			if(status)
			{
				printf("PPP established\n");
			}
			else
			{
				printf("PPP failed\n");
			}
			printf("IP address is %s\n", inet_ntoa( buffer, gethostid()));	

			smtp_sendmail(mail_to, mail_from, mail_subject, mail_body);	
			mail_status = SMTP_PENDING;
			while(mail_status == SMTP_PENDING)
			{
				mail_status = smtp_mailtick();
				yield;
			}
			if(mail_status == SMTP_SUCCESS)
			{
				printf("Message sent\n");
			}
			else
			{
				printf("Failed to send message\n");
			}

			wfd { status = CofPPPshutdown(2000); }	
			if(status == 0)
			{
				printf("PPP terminate failed\n");
			}
			PPPclose();
			ModemOpen(57600);
			wfd { status = CofModemHangup(); }
			ModemClose();		 	  	 	  	
			run_flag = 0;
		}

		costate
		{
			//do somthing else to prove that we can
			WrPortI(SPCR, NULL, 0x84); //set port A to output
			BitWrPortI(PADR, &PADRShadow, 1, 0);
			waitfor(DelayMs(500));
			BitWrPortI(PADR, &PADRShadow, 0, 0);
			waitfor(DelayMs(500));				
		}
	}
	
}
