/*****
Tests modem.lib and ppp.lib
Connects to ISP and sends a test mail
Runs threee times

You will nedd to change the default addresses and the mail_to[]
and 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 "cname\r"
#define DIALUP_PASSWORD "password\r"

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

#define MY_NETMASK "255.255.255.255"
#define MY_GATEWAY "10.10.1.1"
#define MY_NAMESERVER "10.10.1.2"
#define SMTP_SERVER "mail.yourisp.com"

#define SMTP_DEBUG 1
#define MODEM_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@isp.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 count;
	//test repeated open/close of modem link
	for(count = 0; count < 3; count++)
	{	
	
	if(ModemOpen(57600) == 0)
	{
		printf("No modem detected\n");
		exit(-1);
	}
	if(ModemInit())
	{
		printf("Modem Initialized\n");
	}
	else
	{
		printf("Could not init modem\n");
		exit(-1);
	}

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

	if(PPPstart(5000, 3))
	{
		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();
	}
	if(mail_status == SMTP_SUCCESS)
	{
		printf("Message sent\n");
	}
	else
	{
		printf("Failed to send message\n");
	}
	
	if(PPPshutdown(2000) == 0)
	{
		printf("PPP terminate failed\n");
	}
	PPPclose();
	ModemOpen(57600);
	if(ModemHangup() == 0)
	{
		printf("Failed to hangup modem\n");
	}
	ModemClose();

	} //end of for loop		 	  	 	  	
}
