⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 slip.c

📁 基于51的单片机的PPP协议实现
💻 C
字号:
/*/////////////////////////////////////////////////////////////////////////////

File Name : SLIP.C

Author : Rene Trenado

Location : Motorola Applications Lab, Baja California

Date Created : September 2000

Current Revision : 0.0

Notes : This file contains the code for the SLIP module 
/////////////////////////////////////////////////////////////////////////////*/

#include "CommdRV.h"
#include "slip.h"
#include "IP.h"
#include "Icmp.h"
#include "udp.h"

static BYTE		*SLIP_Packet;		// local pointer to the SLIP buffer */
BYTE  			SLIPStatus = 0;		// status and control byte of the SLIP module */
static volatile BYTE 	FrameSize = 0;		// provides internal control for SLIP buffer management */


/***********************************************************************
Function : 	ProcSLIPReceive 

Parameters : 	A Byte character to stream in a SLIP Packet

Date : 		August 2000

Desc : 		This function process a BYTE following SLIP popular 
		specification. The Async event on input driver should 
		call this function (usually the COMM ISR).
***********************************************************************/
void ProcSLIPReceive (BYTE c) {

	if (SLIPStatus & IsFrame) return;

	if (SLIPStatus & ReSync) {					// Ignore incoming data until a start of packet is found */
		if (c != 0xC0) {
			return;
		}
		SLIPStatus &= ~ReSync;					// Clear the synchronization flag to stream incoming packet in SLIP buffer */
		FrameSize = 0;						// FrameSize will record the size of the incoming packet */
	}

	if (SLIPStatus & IsESC) {					// Is the byte received a control char?
		switch (c) {						// if so decode it
			case ESC_END:
            			SLIP_Packet [FrameSize++] = SLIP_END;	// Store Special char on Input Buffer
			break;

			case ESC_ESC:
				SLIP_Packet [FrameSize++] = SLIP_ESC;	// Store Special char on Input Buffer
			break;

			default:					// SLIP Protocol violation
			break;
		}
		SLIPStatus &= ~IsESC;					// Clear the special control character flag
	}
	else {
		switch (c) {
			case SLIP_ESC:					// Special ESC Character received
				SLIPStatus |= IsESC;
			break;

			case SLIP_END:					// Special END Character received
     			if (FrameSize > 0) {				// Avoid cero length packets (0xC0-0xC0 conditions)
					SLIP_Packet [FrameSize] = 0;	// Format data received to a NULL terminated string
					SLIPStatus |= IsFrame;		// Signal Frame availability to main application							
									// Extra control processing can be done here
					/* ..... */
				}
			break;

			default:					// Data of Packet received
				SLIP_Packet [FrameSize++] = c;		// Store Byte on Input Buffer
				if (FrameSize > (SLIP_MAX_SIZE)) {	// Avoid & discard large SLIP packets
					FrameSize = 0;			
					SLIPStatus |= ReSync;		// Resynchronize SLIP packet reception
				}
			break;
		}
	}
}


/***********************************************************************
Function : 	SLIPInit

Parameters : 	None

Date : 		September 2000

Desc : 		Initialize the SLIP Module
***********************************************************************/
void SLIPInit (void) {
	SLIPStatus |= ReSync;
	SLIP_Packet = (BYTE *)ip_in;
}


/***********************************************************************
Function : 	ProcSLIPSend 

Parameters : 	Buffer:	a pointer to a buffer containing the IP packet to send
		len:	the size of the SLIP packet 

Date : 		September 2000

Desc : 		Sends a BYTE array of len length following the popular SLIP format

***********************************************************************/
void ProcSLIPSend (BYTE *ptr, BYTE len) {

	WriteComm (SLIP_END);							// Write start of SLIP frame

	while (len--) {								// Send all buffer in SLIP format
		switch (*ptr) {							// check to see if is a special character
			case SLIP_END:
				WriteComm (SLIP_ESC);				// escape special character
				WriteComm (ESC_END);
			break;

			case SLIP_ESC:
				WriteComm (SLIP_ESC);				// escape special character
				WriteComm (ESC_ESC);
			break;

			default:
				WriteComm (*ptr);				// send raw character
		}
		ptr++;								// continue with next character send
	}
	WriteComm (SLIP_END);							// Write END of SLIP frame
}


/***********************************************************************
Function : 	SLIPEntry

Parameters : 	None

Date : 		August 2000

Desc : 		SLIP Module Entry, Applications should call SLIPEntry
		frequently in the main loop or in portions of the app
		code.
***********************************************************************/
void SLIPEntry (void) {
	if (SLIPStatus & IsFrame) {
		if (!IPCompare (&ip_in->DestAddress[0])) {			// Misrouted datagram or broadcast message received */
										// Do extra processing here
			NoOperation;
		}		
		else {	
			switch (ip_in->Protocol) {				// Select protocol handler of IP payload */
				case UDP:
					UDP_Handler ((UDPDatagram *)&ip_in->SourceAddress[0]);
				break;
	
			  	case TCP:
			  	break;

			  	case ICMP:
					IcmpHandler ((IPDatagram *)ip_in);
				break;

				default:
				break;
			}
		}
		SLIPStatus &= ~IsFrame;						// Acknowledge datagram processing
		SLIPStatus |= ReSync;						// Synchronize packet reception
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -