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

📄 bluegpsparser.cpp

📁 S60系统下的蓝牙GPS连接程序 包括client和server端的连接方式
💻 CPP
字号:
/*****************************************************************************
 COPYRIGHT All rights reserved Sony Ericsson Mobile Communications AB 2003.

 The software is the copyrighted work of Sony Ericsson Mobile Communications AB.
 The use of the software is subject to the terms of the end-user license
 agreement which accompanies or is included with the software. The software is
 provided "as is" and Sony Ericsson specifically disclaim any warranty or
 condition whatsoever regarding merchantability or fitness for a specific
 purpose, title or non-infringement. No warranty of any kind is made in
 relation to the condition, suitability, availability, accuracy, reliability,
 merchantability and/or non-infringement of the software provided herein.

 *****************************************************************************/

#include "BlueGPSParser.h"
#include "InetProtTextUtils.h"

CBlueGPSParser::CBlueGPSParser() : CActive(EPriorityStandard)
{
	CActiveScheduler::Add(this);
}

CBlueGPSParser::~CBlueGPSParser()
{
	Cancel();
	iTimer.Close();
	iSemaphore.Close();
}


void CBlueGPSParser::Parse()
{
	iSemaphore.Wait();
	TInt offset;
	TBool error = EFalse;

	offset = iInputBuffer.Locate('$');               //look for first $
	if (offset != KErrNotFound)                      //remove $ and everything before 
	{
		iInputBuffer.Delete(0, offset + 1 );          //to make sure we have a whole packet    
		iInputBuffer.ZeroTerminate();
	}
	else 
	{
		iInputBuffer.Zero();
		iInputBuffer.ZeroTerminate();
		iSemaphore.Signal();
		return;
	}
	
	while (!error)
	{
		offset = iInputBuffer.Locate('$');           //look for second $ ( first after previous deletion ) 
		if (offset != KErrNotFound)
		{
			TUint8* tmp = &iInputBuffer[0];
			
			if (offset > KPacketBufferLength )
			{
				iInputBuffer.Delete(0, offset + 1);
				iInputBuffer.ZeroTerminate();
			}
			else
			{
				iPacketBuffer.Copy(tmp, offset );        //Copy resulting packet to iPacketBuffer
				iInputBuffer.Delete(0, offset + 1 );
				iInputBuffer.ZeroTerminate();
				iPacketBuffer.ZeroTerminate();
				ParsePacket();
			}
		}
		else error = ETrue;
	}
	iInputBuffer.Insert(0, _L8("$"));                 // to make sure a packet isn't deleted next cycle.
	iSemaphore.Signal();
}

void CBlueGPSParser::ParsePacket()
{

	TBuf8<KMaxNumChars> iElement[KMaxNumCommas];
	for (int i = 0; i < KMaxNumCommas; i++)
	{
		iElement[i].Zero();
		iElement[i].ZeroTerminate();
	}

	TInt offset = 1;
	TInt loop = 0;
	TBool breakLoop = EFalse;
	TUint8* tmp = 0;
	while ((offset != KErrNotFound) && (!breakLoop) && (iPacketBuffer.Length() != 0))
	{
		offset = iPacketBuffer.Locate(',');
		tmp = &iPacketBuffer[0];
		if (offset > KMaxNumChars )
		{
			iPacketBuffer.Delete(0, offset + 1);
			iPacketBuffer.ZeroTerminate();
		}
		else if (offset != KErrNotFound) 
		{
			iElement[loop].Copy(tmp, offset );
			iPacketBuffer.Delete(0, offset + 1);
			iPacketBuffer.ZeroTerminate();
		}
		else iElement[loop].Copy(tmp);
		iElement[loop].ZeroTerminate();
		
		loop = loop + 1;
		if (loop > (KMaxNumCommas - 1)) 
			breakLoop = ETrue;
	} 
    
	if (iElement[0].Compare(_L8("GPGSV")) == 0)
	{
		InetProtTextUtils::ConvertDescriptorToInt( iElement[3], iInfo.iNumberSatellites );
	}
	if (iElement[0].Compare(_L8("GPRMC")) == 0)
	{
		//return if mismatch in parsed message
		
		if (iElement[9].Length() != 6)
			return;
		if (iElement[1].Length() == 9)    //Length *may* be 10 but should not be...
			iElement[1].Append(_L("0000"));
	    else if (iElement[1].Length() == 10)    //Length *may* be 10 but should not be...
			iElement[1].Append(_L("000"));
		else return;

		if (iElement[3].Length() != 9)
			return;
		if (iElement[5].Length() != 10)
			return;

		//end of return part

		//start parse gpstime and date
		tmp = &iElement[9][0];
		
		TBuf16<30> tmpBuf16Bit;
		TBuf8<20> tmpBuf8Bit;
		TBuf8<2> tmpYY;
		TBuf8<2> tmpMM;
		TBuf8<2> tmpDD;
		tmpDD.Copy(tmp, 2);
		iElement[9].Delete(0, 2);
		tmpMM.Copy(tmp, 2);
		iElement[9].Delete(0, 2);
		tmpYY.Copy(tmp, 2);

		tmpBuf8Bit.Copy(_L8("20"));
		tmpBuf8Bit.Copy(tmpYY);
		tmpBuf8Bit.Copy(tmpMM);
		tmpBuf8Bit.Copy(tmpDD);

		tmpBuf16Bit.Copy(_L8(":"));
		tmpBuf16Bit.Copy(iElement[1]);
		
		iInfo.iGPSTime.Set(tmpBuf16Bit);
		
		//end parse gpstime

		//start parse longitude and latitude
		counter++;
		tmp = &iElement[3][0];
		TBuf8<2> latXX; 
		latXX.Copy( tmp, 2 );
		iElement[3].Delete( 0, 2 );
		
		TBuf8<2> latMM;
		latMM.Copy( tmp, 2 );
		iElement[3].Delete( 0, 3 );
		
		TBuf8<4> latDDDD;
		latDDDD.Copy( tmp, 4 );
		iElement[3].Delete( 0, 4 );		

		tmp = &iElement[5][0];
		TBuf8<3> longYYY; 
		longYYY.Copy( tmp, 3 );
		iElement[5].Delete( 0, 3 );
		
		TBuf8<2> longMM;
		longMM.Copy( tmp, 2 );
		iElement[5].Delete( 0, 3 );
		
		TBuf8<4> longDDDD;
		longDDDD.Copy( tmp, 4 );
		iElement[5].Delete( 0, 4 );
			
		InetProtTextUtils::ConvertDescriptorToInt( latXX, iInfo.iLatitudeXX );
		InetProtTextUtils::ConvertDescriptorToInt( latMM, iInfo.iLatitudeMM );
		InetProtTextUtils::ConvertDescriptorToInt( latDDDD, iInfo.iLatitudeDDDD );

		InetProtTextUtils::ConvertDescriptorToInt( longYYY, iInfo.iLongitudeYYY );
		InetProtTextUtils::ConvertDescriptorToInt( longMM, iInfo.iLongitudeMM );
		InetProtTextUtils::ConvertDescriptorToInt( longDDDD, iInfo.iLongitudeDDDD );

		//end parse longitude and latitude
		
		//start parse status indicator
		
		if (iElement[2].Compare(_L8("A")) == 0)
			iInfo.iValidData = ETrue;  
		else iInfo.iValidData = EFalse;

		//end parse status indicator
		
		//start parse cardinal point

		if (iElement[4].Compare(_L8("N")) == 0)
			iInfo.iLatitudeCP = ENorth;
		else iInfo.iLatitudeCP = ESouth;

		if (iElement[6].Compare(_L8("E")) == 0)
			iInfo.iLongitudeCP = EEast;
		else iInfo.iLongitudeCP = EWest;
		//end parse cardinal point

		//start parse speed
		// does not work for now...
/*		
		if (iElement[7].Length() != 0)
		{
			TBuf8<4> tempSpeed1;
			tempSpeed1.Zero();
			tempSpeed1.ZeroTerminate();
			TBuf8<4> tempSpeed2;
			tempSpeed2.Zero();
			tempSpeed2.ZeroTerminate();

			TInt tmp1 = 0;
			TInt tmp2 = 0;

			offset = iElement[7].Locate('.');
			if ( offset != KErrNotFound )
			{
				tmp = &iElement[7][0];
				tempSpeed1.Copy(tmp, offset);
				iElement[7].Delete(0, offset + 1);
				tempSpeed2.Copy(tmp);

				InetProtTextUtils::ConvertDescriptorToInt( tempSpeed1, tmp1 );
				InetProtTextUtils::ConvertDescriptorToInt( tempSpeed2, tmp2 );
		
			}
			else
			{
				tempSpeed1.Copy(tmp);
				InetProtTextUtils::ConvertDescriptorToInt( tempSpeed1, tmp1 );
			}
			if (tempSpeed2.Length() == 0)
				iInfo.iSpeed = (TReal32) tmp1;
			if (tempSpeed2.Length() == 1)
				iInfo.iSpeed = (TReal32) tmp1 + (TReal32) tmp2 / 10;
			if (tempSpeed2.Length() == 2)
				iInfo.iSpeed = (TReal32) tmp1 + (TReal32) tmp2 / 100;
		}

		
		//end parse speed
		// start parse heading
		if (iElement[8].Length() != 0)
		{
			TBuf8<4> tempHeading1;
			TBuf8<4> tempHeading2;
			tempHeading1.Zero();
			tempHeading2.Zero();
			tempHeading1.ZeroTerminate();
			tempHeading2.ZeroTerminate();

			TInt tmp1 = 0;
			TInt tmp2 = 0;

			offset = iElement[8].Locate('.');
			if ( offset != KErrNotFound )
			{
				tmp = &iElement[8][0];
				tempHeading1.Copy(tmp, offset);
				iElement[7].Delete(0, offset + 1);
				tempHeading2.Copy(tmp);

				InetProtTextUtils::ConvertDescriptorToInt( tempHeading1, tmp1 );
				InetProtTextUtils::ConvertDescriptorToInt( tempHeading2, tmp2 );
		
			}
			else
			{
				tempHeading1.Copy(tmp);
				InetProtTextUtils::ConvertDescriptorToInt( tempHeading1, tmp1 );
			}
			if (tempHeading2.Length() == 0)
				iInfo.iHeading = (TReal32) tmp1;
			if (tempHeading2.Length() == 1)
				iInfo.iHeading = (TReal32) tmp1 + (TReal32) tmp2 / 10;
			if (tempHeading2.Length() == 2)
				iInfo.iHeading = (TReal32) tmp1 + (TReal32) tmp2 / 100;
		}
*/
		//end parse heading

		iServer->SetGPSInfo( iInfo );
	}
	if (iElement[0].Compare(_L8("GPGGA")) == 0)
	{

	}
	if (iElement[0].Compare(_L8("GPGSA")) == 0)
	{
	}
}


void CBlueGPSParser::AddData( TDesC8& aData )
{	
	iSemaphore.Wait();

	TInt newLength = aData.Length() + iInputBuffer.Length();
	if (newLength > KInputBufferLength)
	{
		iInputBuffer.Delete(0, newLength - KInputBufferLength);   //If appended message does not fit into
	}														//inputBuffer, make place for it.
	iInputBuffer.Append(aData);                  //Append new input to end of iInputBuffer
	if (!IsActive())
	{
		iTimer.After(iStatus, 0);
		SetActive();                                  //ParsePacket iPacketBuffer
	}
	
    iSemaphore.Signal();                          
}



/****** what happens in AddData and Parse ***********************************************************
* 1.  99,V,0000.0000,N,00000.0000,E,,,260102,,*16                                 //original iInputBuffer
*     $GPGGA,235955.999,0000.0000,N,00000.0000,E,0,00,50.0,0
* 
* 2.  99,V,0000.0000,N,00000.0000,E,,,260102,,*16
*     $GPGGA,235955.999,0000.0000,N,00000.0000,E,0,00,50.0,0.0,M,,M,,0000*72      //Added iInputBuffer
*     $GPGSA,A,1,,,,,,,,,,,,,50.0,50.0,50.0*05
*
* 3.  GPGGA,235955.999,0000.0000,N,00000.0000,E,0,00,50.0,0.0,M,,M,,0000*72       //Trimmed iInputBuffer
*     $GPGSA,A,1,,,,,,,,,,,,,50.0,50.0,50.0*05
*
* 4.  GPGGA,235955.999,0000.0000,N,00000.0000,E,0,00,50.0,0.0,M,,M,,0000*72       //Finished iPacketBuffer
*
* 5.  $GPGSA,A,1,,,,,,,,,,,,,50.0,50.0,50.0*05                                    //rest if iInputBuffer
********************************************************************************************************/


void CBlueGPSParser::RunL()
{
    Parse();
}

void CBlueGPSParser::DoCancel()
{
	iTimer.Cancel();
}

CBlueGPSParser* CBlueGPSParser::NewL( CBlueGPSServer* aServer )
{
    CBlueGPSParser* self = new(ELeave) CBlueGPSParser;
    CleanupStack::PushL(self);
    self->ConstructL( aServer );
    CleanupStack::Pop(self);
    return self;
}

void CBlueGPSParser::ConstructL( CBlueGPSServer* aServer )
{
	iServer = aServer;
	User::LeaveIfError(iTimer.CreateLocal());
	User::LeaveIfError(iSemaphore.CreateLocal(1));
	counter = 0;
}

⌨️ 快捷键说明

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