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

📄 atcmdparser.c

📁 C语言源代码及相关资料
💻 C
📖 第 1 页 / 共 3 页
字号:

//#include "stdafx.h"

#define AMOD_ATCMD_PARSER_H
#include "atcmdParser.h"
#undef AMOD_ATCMD_PARSER_H

int	fun_strcmp( char * p, char * q )
{
	char c = '\0';
	char d = '\0';

	if ( p == 0 ) return -1;
	if ( q == 0 ) return  1;

	do {
		c = ( *p++ );
		d = ( *q++ );

		if ( c < d ) return -1;		//	include c == null char
		if ( c > d ) return  1;		//	include d == null char
	} while  ( c != '\0' || d != '\0' );

	return 0;
}

int	fun_strncmp ( char * p, char * q, int n )
{
	char c = '\0';
	char d = '\0';	

	if ( p == 0 ) return -1;
	if ( q == 0 ) return  1;

	do {
		c = ( *p++ );
		d = ( *q++ );

		if ( c < d ) return -1;		//	include c == null char
		if ( c > d ) return  1;		//	include d == null char

		if ( --n <= 0 ) break;
	} while  ( c != '\0' || d != '\0' );

	return 0;
}

char fun_lowercase ( char c )
{
	if ( 'A' <= c && c <= 'Z' ) return (char)(c - 'A' + 'a');
	return c;
}

int	fun_strcmpnocase ( char * p, char * q )
{
	char c = '\0';
	char d = '\0';

	if ( p == 0 ) return -1;
	if ( q == 0 ) return  1;

	do {
		c = fun_lowercase ( *p++ );
		d = fun_lowercase ( *q++ );

		if ( c < d ) return -1;		//	include c == null char
		if ( c > d ) return  1;		//	include d == null char
	} while  ( c != '\0' || d != '\0' );

	return 0;
}

int	fun_strncmpnocase ( char * p, char * q, int n )
{
	char c = '\0';
	char d = '\0';	

	if ( p == 0 ) return -1;
	if ( q == 0 ) return  1;

	do {
		c = fun_lowercase ( *p++ );
		d = fun_lowercase ( *q++ );

		if ( c < d ) return -1;		//	include c == null char
		if ( c > d ) return  1;		//	include d == null char

		if ( --n <= 0 ) break;
	} while  ( c != '\0' || d != '\0' );

	return 0;
}

unsigned char funPro_RepMS_DataGetField(unsigned char * pData, unsigned char * pField, int nFieldNum, int nMaxFieldLen)
{
	int i = 0;
	int nField = 0;
	int nStrRegState=0;
	int i2 = 0;
	
	//
	// Validate params
	//
	if(pData == 0  || pField == 0 || nMaxFieldLen <= 0)
	{
		return 1;
	}

	//
	// Go to the beginning of the selected field
	//
	while(nField != nFieldNum && pData[i])
	{
		if (pData[i] == '\"') {
			if (nStrRegState==0)
				nStrRegState=1;		// string region start from first  '"'
			else if (nStrRegState==1)
				nStrRegState=0;		// string region end to second  '"'
		} else if(pData[i] == ',') {
			if (nStrRegState==0)	// string region has been close.
				nField++;
		}

		i++;

		if(pData[i] == '\0' )
		{
			pField[0] = '\0';
			return 2;
		}
	}

	if(pData[i] == ',' || pData[i] == '\r' || pData[i] == '\n')
	{
		pField[0] = '\0';
		return 3;
	}

	//
	// copy field from pData to Field
	//
	nStrRegState=0;
	while( (pData[i] != '\r') && (pData[i] != '\n') && pData[i])
	{
		if (pData[i] == '\"') {
			if (nStrRegState==0)
				nStrRegState=1;		// string region start from first  '"'
			else if (nStrRegState==1)
				nStrRegState=0;		// string region end to second  '"'
		} else if(pData[i] == ',') {
			if (nStrRegState==0)	// string region has been close.
				break;
			else {
				pField[i2] = pData[i];
				i2++;
			}
		} else {
			pField[i2] = pData[i];
			i2++;
		}

		i++;

		//
		// check if field is too big to fit on passed parameter. If it is,
		// crop returned field to its max length.
		//
		if(i2 >= nMaxFieldLen)
		{
			i2 = nMaxFieldLen-1;
			break;
		}
	}
	pField[i2] = '\0';

	return 0;
}

unsigned char funPro_RepPlusMS_FINAL_CheckIfEndData(unsigned char nbtData)
{
	int nRtnCheck = 0;
	int nComEndStrLen = 0;
	
	gw_str_CmpEndString[gw_cmpendstr_Index++]=nbtData;
	nComEndStrLen = gw_cmpendstr_Index;
	
	nRtnCheck = fun_strncmp(gw_str_CmpEndString, "\r\n\r\nOK\r\n", nComEndStrLen);
	if ( nRtnCheck==0 ) {
		// end of Tail Data String
		if ( nComEndStrLen==8 ) {
			// End of message, and ready to seatch other response message
			gw_nRCVState_RFPLUS = RCV_STARP_PLUS_EOM;

			// empty end compare string for next time comparsion
			gw_str_CmpEndString[0]='\0';
			gw_cmpendstr_Index=0;
		} // end of if ( nComEndStrLen==8 )
	} else {
		// if compare end string fail, do not continue to compare
		// go to original state to skip unknow sentence
		gw_nRCVState_RFPLUS = RCV_STARP_PLUS_BACK_N_CHAR;		
	} // end of if ( nRtnCheck==0 )

	return 0;
}

// Check if touch end data of this response Plus Command "CMGR".
unsigned char funPro_RepPlusMS_CMGR_CheckIfEndData(unsigned char nbtData)
{
	int nRtnCheck = 0;
	int nComEndStrLen = 0;

	int nContinueCheckIndex=-1;
	int nCheckIndex=1;
	int nMoveCount=0;
	int i=0;
	
	gw_str_CmpEndString[gw_cmpendstr_Index++]=nbtData;
	nComEndStrLen = gw_cmpendstr_Index;
	
	nRtnCheck = fun_strncmp(gw_str_CmpEndString, "\r\n\r\nOK\r\n", nComEndStrLen);
	if ( nRtnCheck==0 ) {
		// end of Tail Data String
		if ( nComEndStrLen==8 ) {
			// End of message, and ready to seatch other response message
			gw_nRCVState_RFPLUS = RCV_STARP_PLUS_EOM;

			// erase end string \r\n\r\nOK\r\n from output data string
			//gw_rpplusms_Index-=8;
			//gw_szRFPLUS_Data[gw_rpplusms_Index]='\0';
			gw_rppluctxsms_Index-=8;
			gw_szRFPLUS_CxtData[gw_rppluctxsms_Index] = '\0';			
			// Output CMGR Data String to TextBox Item..
			// CB: Callback function to process the parameters
		}
	} else {
		// continue to get tail data...
		for ( nCheckIndex=1; nCheckIndex<nComEndStrLen; nCheckIndex++) {
			nRtnCheck = 0;
			nRtnCheck = fun_strncmp(gw_str_CmpEndString+nCheckIndex, "\r\n\r\nOK\r\n", nComEndStrLen-nCheckIndex);
			if ( nRtnCheck==0 ) {
				// Get new match string, continue to match
				// erase nCheckIndex count character from end compare string, move other character to top
				nMoveCount = nComEndStrLen-nCheckIndex;
				for( i=0; i<nMoveCount; i++) {
					gw_str_CmpEndString[i]=gw_str_CmpEndString[nCheckIndex+i];
				}
				nComEndStrLen = nMoveCount;
				gw_str_CmpEndString[nComEndStrLen]='\0';
				gw_cmpendstr_Index = nComEndStrLen;
				nContinueCheckIndex=0;
				break;
			}
		}

		if ( nContinueCheckIndex==-1 ) {
			gw_nRCVState_RFPLUS = RCV_STARP_PLUS_DATA_OTHERS;
			// empty end compare string for next time comparsion
			gw_str_CmpEndString[0]='\0';
			gw_cmpendstr_Index=0;
		} // end of if ( nContinueCheckIndex==-1 )
	} // end of if ( nRtnCheck==0 )

	return 0;
}

// Check if touch end data of this response Plus Command "CMGL"
unsigned char funPro_RepPlusMS_CMGL_CheckIfEndData(unsigned char nbtData)
{
	int nRtnCheck = 0;
	int nComEndStrLen = 0;

	int nContinueCheckIndex=-1;
	int nCheckIndex=1;
	int nMoveCount=0;
	int nAgainEachLineEndCheck=0;
	int i=0;
		
	gw_str_CmpEndString[gw_cmpendstr_Index++]=nbtData;
	nComEndStrLen = gw_cmpendstr_Index;
	
	nRtnCheck = fun_strncmp(gw_str_CmpEndString, "\r\n\r\nOK\r\n", nComEndStrLen);
	if ( nRtnCheck==0 ) {
		// end of Tail Data String
		if ( nComEndStrLen==8 ) {
			// End of message, and ready to seatch other response message
			gw_nRCVState_RFPLUS = RCV_STARP_PLUS_EOM;
			
			// erase end string \r\n\r\nOK\r\n from output data string
			//gw_rpplusms_Index-=8;
			//gw_szRFPLUS_Data[gw_rpplusms_Index]='\0'
			gw_rppluctxsms_Index -= 8;
			gw_szRFPLUS_CxtData[gw_rppluctxsms_Index] = '\0';
			// Output CMGL Data String to ListView..
			// CB: Callback function to process the parameters
			// gw_rpplusms_Index=0;
		}
	} else {
		// compare fail, and not match end string, therefore skip first bytes,
		// and then continue to compare edn string or cmgl new string from second bytes position
		// continue to get tail data...
		
		// If not end string, therefore first check if the string ia matched with +cmgl string.
		if ( nComEndStrLen>=3 ) {
			int nEachLineEndCheck=0;
			nEachLineEndCheck = fun_strncmp(gw_str_CmpEndString+2, "+CMGL:", nComEndStrLen-2);
			if ( nEachLineEndCheck==0 ) {
				// we may get next CMGL message, and try to make sure that then go to process again CMGL message.
				if ( nComEndStrLen==8 ) {
					// Go to process next CMGL message, and 
					// notice that the current message content have to be processed.
					gw_nRCVState_RFPLUS = RCV_STARP_PLUS_POM; // one part of message

					// erase end string \r\n\r\nOK\r\n from output data string
					//gw_rpplusms_Index-=8;
					//gw_szRFPLUS_Data[gw_rpplusms_Index]='\0';
					gw_rppluctxsms_Index -= 8;
					gw_szRFPLUS_CxtData[gw_rppluctxsms_Index] = '\0';
					// Output CMGL Data String to ListView..
					// CB: Callback function to process the parameters
					// gw_rpplusms_Index=0;
				} // end of if ( nComEndStrLen==8 )
				return 0;
			} // end of if ( nEachLineEndCheck==0 )
		} // end of if ( nComEndStrLen>=3 )

		// start to compare from second byte 
		for ( nCheckIndex=1; nCheckIndex<nComEndStrLen; nCheckIndex++) {
			nRtnCheck = 0;
			nRtnCheck = fun_strncmp(gw_str_CmpEndString+nCheckIndex, "\r\n\r\nOK\r\n", nComEndStrLen-nCheckIndex);							
			if ( nRtnCheck==0 ) {
				// Get new match string, continue to match
				nMoveCount = nComEndStrLen-nCheckIndex;
				for( i=0; i<nMoveCount; i++) {
					gw_str_CmpEndString[i]=gw_str_CmpEndString[nCheckIndex+i];
				}
				nComEndStrLen = nMoveCount;
				gw_str_CmpEndString[nComEndStrLen]='\0';
				gw_cmpendstr_Index = nComEndStrLen;

				nContinueCheckIndex=0;
				break;
			} else {
				if ( (nComEndStrLen-nCheckIndex) >= 3 ) {
					nAgainEachLineEndCheck=0;
					nAgainEachLineEndCheck = fun_strncmp(gw_str_CmpEndString+nCheckIndex+2, "+CMGL:", (nComEndStrLen-nCheckIndex)-2);
					if ( nAgainEachLineEndCheck==0 ) {
						// Again, we may get next CMGL message, and try to make sure that then go to process again CMGL message.
						if ( (nComEndStrLen-nCheckIndex)==8 ) {
							// Go to process next CMGL message, and 
							// notice that the current message content have to be processed.
							gw_nRCVState_RFPLUS = RCV_STARP_PLUS_POM; // one part of message

							// erase end string \r\n\r\nOK\r\n from output data string
							//gw_rpplusms_Index-=8;
							//gw_szRFPLUS_Data[gw_rpplusms_Index]='\0';
							gw_rppluctxsms_Index -= 8;
							gw_szRFPLUS_CxtData[gw_rppluctxsms_Index] = '\0';
							// Output CMGL Data String to ListView..

⌨️ 快捷键说明

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