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

📄 tskppp.c

📁 本人编写的无线电话程序,给予PIC18C801设计,包括了uCOS的移植以及菜单,自己设计的拼音注入法,完整地一级汉字库,希望对大家有所帮助
💻 C
📖 第 1 页 / 共 2 页
字号:
//Task_PPPHandler
#include "Network.h"
#include "ppp.h"
#include "event.h"


extern OS_MEM			*pMemSml;
extern OS_MEM			*pMemLge;
extern OS_MEM			*pMemHug;

extern INT8U rom IPAddress[4];	


//Local functions
INT8U PPPSendPacket (INT8U rom *Buffer, INT16U len);
void StartPPPTimer(void);
void Func_InitFCS(void);
void Func_AddFCS(INT8U c);
INT16U Func_GetFCS(void);
INT16U Func_CheckFCS(void);
INT8U Func_CheckLCPOption(INT8U rom * pO, INT16U Len, INT8U CurID, INT16U Map);
void IPNetSend (INT8U rom * pD, INT16U Len);



#pragma romdata CCODE
//the logon user name and password. currently it is suitable for current Unicom CDMA2000 1X network 
INT8U rom User[] = 	"card";		    
INT8U rom Password[] =  "card";		

////////////////////// Protected ROM Data /////////////////////////
//This frame is the configure request, where the ID field shall be modified after copy
INT8U rom LCPConfigureReq[] = {
	0xff,
	0x03,
	0xc0,0x21,		//LCP protocol
	REQ,			//It's a configure req
	0,				//Id shall be modified
	0,				//the high byte of length is 0
	0x0E,			//total length = 14, from "req" to the end
	LCP_ACCM,0x06,0xFF,0xFF,0xFF,0xFF,
	LCP_MRU,0x04,0x01,0x00
};
//the Terminate-Request packet
INT8U rom LCPTerminate[] = {
	0xff,0x03,0xc0,0x21,0x05,0x04,0x00,0x04,0x80,0xfe
};
//Data used to calculate FCS
INT16U rom fcstab[256] = {
	0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
	0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
	0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
	0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
	0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
	0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
	0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
	0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
	0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
	0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
	0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
	0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
	0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
	0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
	0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
	0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
	0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
	0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
	0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
	0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
	0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
	0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
	0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
	0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
	0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
	0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
	0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
	0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
	0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
	0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
	0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
	0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
};

INT16U rom BitMap[]={
	0x0000, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040,
	0x0080,	0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000
};
#pragma romdata EXTRAM

INT8U rom DialNum[MAX_DCALLNUM_LEN];	//number shall be ASCII code, for there may be none digital code - "#" or "*"
INT8U rom ReConnectEnable;				//if enable reconnect when the physical line is not available
INT8U rom PPPRestartCounter;			//how many times the PPP restart due to time out

INT8U rom PPPTxWorkBuf[PPP_BUFFER_SIZE];//private TX buffer for PPP only

INT8U rom TimerEnable;

INT8U PPPStt;	//The status of PPP
INT8U FStt;		//the PPP depack state

INT8U PPPTxFrameSize;
INT8U PPPTxID;	//the ID of current ppp frame

INT16U FCS;		//the value of FCS calculation

#pragma code MYCODE
void PPPInit (void) {
	INT8U i;
	PPPStt = INITIAL;
	PPPTxID = 1;

	FStt = 0x00;

	for(i=0;i<MAX_DCALLNUM_LEN; i++){
		DialNum[i] = 0xff;
	}
	IPAddress[0] = 0;
	IPAddress[1] = 0;
	IPAddress[2] = 0;
	IPAddress[3] = 0;
}

//public function, to make a PPP header to selected buffer

INT8U rom *PPPMakeHeader(INT8U rom * pBuf, INT16U Protocol){
	*pBuf++ = 0xff;
	*pBuf++ = 0x03;
	*pBuf++ = Protocol>>8;
	*pBuf++ = Protocol & 0xff;
	return (pBuf);
}


/************************************************************************************************/
//                            The following is some LCP actions                                 //
/************************************************************************************************/
//tld : This-Layer-Down
void LCP_Down(void){
	IPAddress[0] = 0;
	IPAddress[1] = 0;
	IPAddress[2] = 0;
	IPAddress[3] = 0;
	OSQPost(peventIPHandler, (OS_EVENT *)MSG_LOW_DOWN);
}
//tlu : This-Layer-Up
void LCP_Up(void){
	OSQPost(peventIPHandler, (OS_EVENT *)MSG_LOW_UP);
}

//tls  : This-Layer-Started
//Desc : when the DialNum is empty(all 0xff), the link is direct conncted, so just report with a 
//       UP event. If not, connection request message with number to dial shall be sent to WICCmd
void LCP_Started(void){
	INT8U i,err;
	INT8U rom * pMsg;
	//if the number is empty, means direct connection
	if(DialNum[0] == 0xff){
		OSQPost(peventPPPHandler, (OS_EVENT *)MSG_LOW_UP);
		return;
	}
	
	//If the physical connection has to be established ...
	pMsg = OSMemGet(pMemSml, &err);
	if(err == OS_NO_ERR){
		((MSG_HEAD *)pMsg)->Msg_ID = MSG_DATA_CONREQ;
		((MSG_HEAD *)pMsg)->Origin = peventPPPHandler;
		((MSG_HEAD *)pMsg)->Attached =FALSE;
		((MSG_HEAD *)pMsg)->pmemME = pMemSml;
		for(i=0;i<MAX_DCALLNUM_LEN;i++){
			if(DialNum[i] == 0xff) break;
			((MSG_DCONREQ *)pMsg)->Num[i] = DialNum[i];
		}
		((MSG_HEAD *)pMsg)->LenOfBody = i;
		OSQPost(peventWICCmd, pMsg);
	}
}


//tlf : This-Layer-Finished
void LCP_Finished(void){

	//if the number is empty, means direct connection
	if(DialNum[0] == 0xff){
		OSQPost(peventPPPHandler, (OS_EVENT *)MSG_LOW_DOWN);
		return;
	}
	OSQPost(peventWICCmd, (OS_EVENT *)MSG_DATA_ONHOOK);
}

//Initial-Restart-Counter
void LCP_InitRC(INT8U reson){
	if(reson == REQ){
		PPPRestartCounter = MAXCONFIGURE;
	}
	else{
		PPPRestartCounter = MAXTERMINATE;
	}
}

void LCP_ZeroRC(void){
	PPPRestartCounter = 0;
}

//When this two request is send, timeout timer is started at the same time
void LCP_SendConfReq(void){
	INT8U i;
	for(i=0;i<18;i++){
		PPPTxWorkBuf[i] = LCPConfigureReq[i];
	}
	PPPTxWorkBuf[5]  = PPPTxID;
	PPPSendPacket((INT8U rom *)PPPTxWorkBuf,18);
	//Set TimeOut timer
	StartPPPTimer();
	TimerEnable = TRUE;
}

void LCP_SendTerminateReq(void){
	INT8U i;
	for(i=0;i<8;i++){
		PPPTxWorkBuf[i] = LCPTerminate[i];
	}
	PPPTxWorkBuf[5]  = PPPTxID;
	PPPSendPacket(PPPTxWorkBuf,8);
	//Set TimeOut timer
	StartPPPTimer();
	TimerEnable = TRUE;
}

void LCP_SendTerminateAck(void){
	
}
/**********************************************************************************
  Here is the function for PAP application
 **********************************************************************************/
void PAP_SendConfReq(void){
	INT8U i;
	INT16U it;
	INT8U len;
	
	PPPMakeHeader(PPPTxWorkBuf, PAP);
	PPPTxWorkBuf[4] = REQ;
	PPPTxWorkBuf[5] = PPPTxID;
	PPPTxWorkBuf[6] = 0;
	PPPTxWorkBuf[7] = 4;
	len = 9;
	for(i=0;;i++){
		if(User[i] == 0x00) break;
		PPPTxWorkBuf[len++]=User[i];
	}
	PPPTxWorkBuf[8] = i;	//wirte the length field
	PPPTxWorkBuf[7] += i+1;
	
	it = len;	//save current address, it's the length field of "PASSWORD"
	len++;
	for(i=0;;i++){
		if(User[i] == 0x00) break;
		PPPTxWorkBuf[len++]=Password[i];
	}
	PPPTxWorkBuf[it] = i;
	PPPTxWorkBuf[7] += i+1;
	
	PPPSendPacket((INT8U rom *)PPPTxWorkBuf,len);
}

/**********************************************************************************
  Here is the function for IPCP application
 **********************************************************************************/
void IPCP_SendConfReq(void){
	PPPMakeHeader(PPPTxWorkBuf, IPCP);
	PPPTxWorkBuf[4] = REQ;
	PPPTxWorkBuf[5] = PPPTxID;
	PPPTxWorkBuf[6] = 0;
	PPPTxWorkBuf[7] = 10;
	PPPTxWorkBuf[8] = 3;
	PPPTxWorkBuf[9] = 6;
	PPPTxWorkBuf[10] = IPAddress[0];
	PPPTxWorkBuf[11] = IPAddress[1];
	PPPTxWorkBuf[12] = IPAddress[2];
	PPPTxWorkBuf[13] = IPAddress[3];
	PPPSendPacket((INT8U rom *)PPPTxWorkBuf,14);
}
/***************************************************************************************************************/
//											Internal functions
/***************************************************************************************************************/

/***************************************************************************
	Send PPP packet
	Desc: send "len" bytes at "Buffer". Add FCS and flag to the data, and 
          escape byte is added.
	Note: if can not locate RAM for packeting, return 1, otherwise return 0
 ***************************************************************************/
INT8U PPPSendPacket (INT8U rom *Buffer, INT16U len) {
	INT8U rom * pSml;
	INT8U rom * pLge;
	INT8U err;

	Func_InitFCS();
	
	pSml = OSMemGet(pMemSml, &err);
	if(err == OS_NO_ERR){
		pLge = OSMemGet(pMemHug, &err);
		if(err != OS_NO_ERR){
			OSMemPut(pMemSml, pSml);
			return(1);
		}
	}
	else return (1);

	((MSG_HEAD *)pSml)->Msg_ID = MSG_COM0_TRANSMIT;
	((MSG_HEAD *)pSml)->pmemME = pMemSml;
	((MSG_HEAD *)pSml)->Attached = TRUE;
	((MSG_HEAD *)pSml)->pMem = pLge;
	((MSG_HEAD *)pSml)->pmemATT = pMemHug;

	*pLge++ = END;
	err = 0;
	
	for(;;){
		if (*Buffer < (INT8U)0x20) {
			*pLge++ = ESC;
			*pLge++ = (*Buffer ^ 0x20);
		} else {
			switch (*Buffer) {
				case END:
					*pLge++ = ESC;
					*pLge++= 0x5E;
					break;
				case ESC:
					*pLge++ = ESC;
					*pLge++ = 0x5D;
					break;
				default:
					*pLge = *Buffer;
					pLge++;
					break;
			}
		}
		len--;
		Func_AddFCS(*Buffer);
		Buffer++;
		if(len == 0){
			if(err == 1) break;
			*((INT16U rom *)Buffer) = Func_GetFCS();
			len = 2;
			err = 1;
		}
	}
	
	*pLge++ = END;
	((MSG_HEAD *)pSml)->LenOfAttach = (INT16U)(((INT24U)pLge) -((INT24U)((MSG_HEAD *)pSml)->pMem));
	OSQPost(peventHardware, pSml);
	return(0);
}



//If this process found unacceptable options, REJ is sent, return 1; Or if parameter of acceptable option is not desirable, Nak with
//desirable parameter is sent and return 1. If all options and parameters are acceptable, Ack is sent and return 0
//The process has a priority:
//	ACK < NAK < REJ, that is, if Nak of certain option(s) is to be sent when an ACK is forming, the ACK frame is clear and begin
// to form NAK. While, if REJ is to be sent after beginning to form NAK or ACK, they are cleared and begin to form REJ
INT8U Func_CheckLCPOption(INT8U rom * pO, INT16U Len, INT8U CurID, INT16U Map){
	INT8U 		OpLen;	//length of current option
	INT8U i;
	INT8U rom * pbuf;
	
	PPPTxFrameSize = 0;		// Clear the tx buffer
	pbuf = PPPTxWorkBuf;
	
	while(Len >=2){
		OpLen = *(pO +1);
		if(Len < OpLen) {		//Found error in frame length
			return(1);
		}
		
		if((*pO < 16) && ((BitMap[*pO] & Map) != 0)){	//If the option is acceptable
			if(*pO == LCP_AP){
				if(*(pO+2) != 0xc0){			//The peer don't want PAP
					if(PPPTxFrameSize == 0){	//if the tx buffer is empty, make the message head
						pbuf = PPPMakeHeader(pbuf,LCP);
						*pbuf++ = NAK;
						*pbuf++ = CurID;
						*pbuf++ = 0;
						*pbuf++ = 4;
						PPPTxFrameSize = 8;
					}
					if( ((PPP_FRAME *)PPPTxWorkBuf)->Code == ACK ){	//If any option in current packet shall be Ack
																	//and the Ack frame is being forming, change it
						((PPP_FRAME *)PPPTxWorkBuf)->Code = NAK;
						((PPP_FRAME *)PPPTxWorkBuf)->LengthHigh = 0;
						((PPP_FRAME *)PPPTxWorkBuf)->Length = 4;
						PPPTxFrameSize = 8;
						pbuf = PPPTxWorkBuf + 8;
					}						
					if( ((PPP_FRAME *)PPPTxWorkBuf)->Code == NAK ){
						*pbuf++ = LCP_AP;
						*pbuf++ = 4;
						*pbuf++ = 0xc0;
						*pbuf++ = 0x23;
						PPPTxFrameSize +=4;
						((PPP_FRAME *)PPPTxWorkBuf)-> Length += 4;
					}
				}
				else{
					if(PPPTxFrameSize == 0){
						pbuf = PPPMakeHeader(pbuf,LCP);
						*pbuf++ = ACK;
						*pbuf++ = CurID;
						*pbuf++ = 0;
						*pbuf++ = 4;
						PPPTxFrameSize = 8;
					}
					if( ((PPP_FRAME *)PPPTxWorkBuf)->Code == ACK ){
						*pbuf++ = LCP_AP;
						*pbuf++ = 4;
						*pbuf++ = 0xc0;
						*pbuf++ = 0x23;
						PPPTxFrameSize +=4;
						((PPP_FRAME *)PPPTxWorkBuf)-> Length += 4;
					}
				}
			}
			else{	//We ACK all acceptable options other than Authentication Protocol
				if(PPPTxFrameSize == 0){
					pbuf = PPPMakeHeader(pbuf,LCP);
					*pbuf++ = ACK;
					*pbuf++ = CurID;
					*pbuf++ = 0;
					*pbuf++ = 4;
					PPPTxFrameSize = 8;
				}
				if( ((PPP_FRAME *)PPPTxWorkBuf)->Code == ACK ){
					for(i=0; i<OpLen; i++){
						*pbuf++ = pO[i];
					}
					PPPTxFrameSize +=OpLen;
					((PPP_FRAME *)PPPTxWorkBuf)-> Length += OpLen;
				}
			}
		}
		else{
			//If found unacceptable option
			if(PPPTxFrameSize == 0){
				pbuf = PPPMakeHeader(pbuf,LCP);
				*pbuf++ = REJ;
				*pbuf++ = CurID;
				*pbuf++ = 0;
				*pbuf++ = 4;	//includes the Code and ID and length itself
				PPPTxFrameSize = 8;
			}
			else if( (((PPP_FRAME *)PPPTxWorkBuf)->Code == NAK) || (((PPP_FRAME *)PPPTxWorkBuf)->Code == ACK ) ){
				((PPP_FRAME *)PPPTxWorkBuf)->Code 		= REJ;
				((PPP_FRAME *)PPPTxWorkBuf)->LengthHigh = 0;
				((PPP_FRAME *)PPPTxWorkBuf)->Length 	= 4;
				PPPTxFrameSize = 8;

⌨️ 快捷键说明

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