www.pudn.com > pppcode.rar > main.c


 
#include "commdrv.h" 
#include "modemdrv.h" 
#include "ppp.h" 
#include "udp.h" 
#include "ip.h" 
 
 
BYTE RemoteServer [4] = {200, 168, 3, 11}; // Remote Server to send notifications 
const char * ModemCommand [] = { // Array of modem initialization commands 
	"ATZ\r", // Reset Command 
		"ATE0\r", // Disable Echo 
		"AT&C1\r", // Track presence of data carrier 
		"AT&D3\r" // Reset modem when an on-to-off transition of DTR ocurres 
}; 
/*********************************************************************** 
Function : ModemHandler 
Parameters : Code - Numeric response code from a Modem dial command 
Date : January 2001 
Desc : This function handles the numeric responses from a dial command 
issued to the modem 
***********************************************************************/ 
void ModemHandler (BYTE Code) { 
	switch (Code) { 
case '0': // OK 
	break; 
case '1': // CONNECT 
	ModemBuffFlush (); // Flush contents of Modem Buffer 
	if (ModemGetch () != 0x7F) { // Test for PPP packets 
		Waitfor (":", 100); // Wait for "Username:" of ISP script 
		PPPSendVoidLCP (); // ForcePPPtransactionsinsteadof 
		// scripts 
	} 
	CommEventProc (ProcPPPReceive); // Install PPP service routine 
 
	break; 
case '2': // RING 
	break; 
case '3': // NO CARRIER 
	break; 
case '4': // ERROR 
	break; 
case '6': // NO DIAL TONE 
	break; 
case '7': // BUSY 
	break; 
case '8': // NO ANSWER 
	break; 
case '9': // CONNECT 2400 
	break; 
default: // TIME OUT, NO RESPONSE FROM MODEM RECEIVED! 
	break; 
	} 
} 
/*********************************************************************** 
Function : UDPReceive 
Parameters : Data of UDP packet, 
size - size of data in bytes 
RemoteIP - sender IP address 
port - UDP port number 
Date : January 2001 
Desc : This function is executed each time a UDP packet is received 
and validated. 
***********************************************************************/ 
void UDPReceive (BYTE *udpdata, BYTE size, DWORD RemoteIP, WORD port) { 
	switch (port) { // Select the port number of the UDP packet 
case 1080: // If port number equals 1080 then reply 
	// with ADC channel 0 
	ADSCR &= 0x00; // Get an A/D lecture 
	while (!(0x80 & ADSCR)); 
	udp_out->Payload [0] = ADR; // Format UDP payload 
	UDPSendData ((BYTE *)&RemoteIP, 11222, 0, 1);// Send UDP reply 
	break; 
case 1081: // Port = 1081, reply with ADC ch1 
	ADSCR &= 0x01; 
	while (!(0x80 & ADSCR)); 
	udp_out->Payload [0] = ADR; 
	UDPSendData ((BYTE *)&RemoteIP, 11222, 0, 1); 
	break; 
case 1082: // Data through UDP port 1082 
	// Do something here 
	break; 
case 1083: // Data through UDP port 1083 
	break; 
	} 
} 
/*********************************************************************** 
Function : LinkTask 
Parameters : None 
Date : January 2001 
Desc : This function synchronize the phone line with the PPP 
link. 
***********************************************************************/ 
void LinkTask (void) { 
	if ((PPPStatus & LinkOn) && (!ModemOnLine())) {// PPP Link ON while Phone is 
		// on-hook! 
		PPPStatus &= ~LinkOn; // Clear PPP link flag 
		PORTC = 0x00; 
		CommEventProc (ProcModemReceive); // Install Modem handler 
	} 
} 
/*********************************************************************** 
Function : ApplicationTask 
Parameters : None 
Date : January 2001 
Desc : This function checks channel 2 of the A/D and sends a warning 
message to a remote server using UDP if a conversion is higher than 
hexadecimal 0x35. 
***********************************************************************/ 
void ApplicationTask (void) { 
	ADSCR &= 0x02; // Test A/D channel 2 
	while (!(0x80 & ADSCR)); // Wait for A/D conversion 
	if (ADR > 0x35) { // If sample is above 0x35 
		// Send a potification 
		if (!ModemOnLine ()) { // Test if Modem on-line 
			NoOperation; // Modem Not on-line, 
			// we can re-dial here 
		} 
		UDPSendData ((BYTE *)&RemoteServer, 8010, "Warning" , 7); 
	} 
} 
///////////////////////////////////////////////////////////////////////////// 
// M A I N 
///////////////////////////////////////////////////////////////////////////// 
void main(void) { 
 
	IPInit (); // Initialize IP 
 
	PPPInit (); // Initialize PPP interface 
	IPBindAdapter (PPP); // Send IP packets using PPP format 
 
	UDPSetCALLBACK (UDPReceive); // Set Callback for incoming UDP data 
	ModemInit (); // Modem Init 
	ModemBindBuff (PPPGetInputBuffer()); // Set Modem Buffer for command reception 
	CommEventProc (ProcModemReceive); // re-direct incoming SCI characters to the 
	// modem interface 
	OpenComm (BAUDS_2400, // Open the serial port 
		ENABLE_RX | // Enable SCI Rx and Tx modules 
		ENABLE_TX | 
		ENABLE_RX_EVENTS); // Enable Rx IRQs 
	{ // Create some stack variables 
		BYTE Res = 0; // Create two temp vars in the stack 
		BYTE index; 
		for (index = 0; index <= 3; index++) { // Loop through Modem initiazation 
			// commands 
			transmit (ModemCommand [index]); // Transmit modem command 
			Res = Waitfor ("OK", 30); // Wait for OK 
			if (!Res) { // Invalid response received 
				// Do something here 
				ModemReset (); // Reset modem 
				index = 0; // Loop again 
			} 
		} 
		Res = ModemDial ("6842626"); // Dial ISP 
		ModemHandler (Res); // Handle Modem response 
	} 
	EnableInterrupts; 
	for (;;) { // Application Loop 
 
		LinkTask (); // Synchronize PPP link with Modem 
		PPPEntry (); // Poll for PPP packets 
 
		ApplicationTask (); // Call application 
	} 
}