/*****
Tests modem.lib and ppp.lib
Connects to ISP and sends a test mail
Runs three 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.

********/


//Use serial port C (IF_PPP2)
#define USE_PPP_SERIAL 0x04

//Uncomment to get PPP detail
#define PPP_VERBOSE
#define PPPLINK_VERBOSE

#define PPPLINK_DEBUG
#define PPP_DEBUG

#define DIALUP_NAME "username"
#define DIALUP_PASSWORD "password"
#define DIALUP_NUMBER "5551212"
#define DIALUP_SENDEXPECT "ATDT" DIALUP_NUMBER " CONNECT '' ogin: " DIALUP_NAME " word: " DIALUP_PASSWORD " PPP"

#define SMTP_SERVER "smtp.isp.com"

#define SMTP_DEBUG 1
#define MODEM_DEBUG 1

#define CINBUFSIZE 31
#define COUTBUFSIZE 31

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


#define TIMEZONE        -8

#define LCP_TIMEOUT 5000

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


const char mail_to[] = "you@wherever.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;

	sock_init(); //initialize TCP/IP

	//test repeated open/close of modem link
	for(count = 0; count < 3; count++)
	{

	//configure PPP for dialing in to ISP and bring it up
	ifconfig(IF_PPP2,
				IFS_PPP_INIT,
				IFS_PPP_SPEED, 57600L,
				IFS_PPP_RTSPIN, PBDR, &PBDRShadow, 7,
				IFS_PPP_CTSPIN, PBDR, 0,
				IFS_PPP_FLOWCONTROL, 1,
				IFS_PPP_SENDEXPECT, DIALUP_SENDEXPECT,
				IFS_PPP_HANGUP, "ATH",
				IFS_PPP_MODEMESCAPE, 1,
				IFS_PPP_ACCEPTIP, 1,
				IFS_PPP_ACCEPTDNS, 1,
				IFS_PPP_REMOTEAUTH, DIALUP_NAME, DIALUP_PASSWORD,
				IFS_UP,
				IFS_END);

	while(ifpending(IF_PPP2) % 2)
	{
		tcp_tick(NULL);
	}
	if(ifstatus(IF_PPP2))
	{
		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");
	}

	ifconfig(IF_PPP2, IFS_DOWN, IFS_END);

	//wait while PPP terminates
	tcp_tick(NULL);
	while(ifpending(IF_PPP2) % 2)
	{
		tcp_tick(NULL);
	}

	} //end of for loop
}