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

📄 bluegpsserver.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 "BlueGPSClientServer.h"
#include "BlueGPSServer.h"
#include "BlueGPSServerSession.h"

#include <e32svr.h>
#include <e32math.h>
#include <e32uid.h>

#define infoPrint(text, secs) { \
	RNotifier notifier; \
	notifier.Connect(); \
	notifier.InfoPrint(text); \
	User::After( 1000000 * secs); \
	notifier.Close(); \
	}

// DLL entry point
GLDEF_C TInt E32Dll(TDllReason /*aReason*/)
{
	return(KErrNone);
}


 // utility - panic the server
void CBlueGPSServer::PanicServer( TInt aPanic )
{
	_LIT(KTxtBlueGPSServer, "BlueGPSServer");
	User::Panic(KTxtBlueGPSServer,aPanic);
}


// start the server thread
// This is called from the client.
EXPORT_C TInt StartThread()
{
	// check server not already started
	TFindServer findBlueGPSServer(KBlueGPSServerName);
	TFullName name;
	if (findBlueGPSServer.Next(name)==KErrNone)
		{ // found server already - bad news
		infoPrint( _L("Server name already exists"), 2);
		return KErrGeneral;
		}

	// create server thread
	RThread thread;
	RSemaphore threadStarted;
	threadStarted.CreateLocal(0);
	TInt res = thread.Create(
		KBlueGPSServerName, // name of thread
		CBlueGPSServer::ThreadFunction, // thread function
		KDefaultStackSize, KDefaultHeapSize, KDefaultHeapSize,
		&threadStarted // parameter to thread function
		);

	if (res != KErrNone)
	{
		thread.Close();
		infoPrint( _L("Server thread create failed"), 2);
		
		return KErrGeneral;
	}

	// now start thread
	thread.SetPriority(EPriorityMuchMore); // set its priority
	thread.Resume(); // kick it into life
	threadStarted.Wait(); // wait until it's got going
	// tidy up
	thread.Close(); // no longer interested in that thread
	threadStarted.Close(); // or semaphore
	// notify the kernel that a server has started.
	#if defined (__WINS__)
	UserSvr::ServerStarted();
	#endif
	// all well
    return KErrNone;
}


// thread function; the active scheduler is installed and started here
TInt CBlueGPSServer::ThreadFunction( TAny* aStarted )
{
	// create cleanup stack
	CTrapCleanup* cleanup = CTrapCleanup::New(); 
	if ( cleanup == NULL)
		PanicServer(ECreateTrapCleanup);
	// convert argument to semaphore
	RSemaphore& started = *(RSemaphore *)aStarted;
	// construct and install active scheduler
	CActiveScheduler* scheduler = new(ELeave) CActiveScheduler;

	__ASSERT_ALWAYS(scheduler, PanicServer(EMainSchedulerError));
	CActiveScheduler::Install(scheduler);
	// construct server, an active object
	CBlueGPSServer* server;
	TRAPD(err, server = CBlueGPSServer::NewL());
	__ASSERT_ALWAYS(!err, PanicServer(ESvrCreateServer));
	// signal everything has started
	started.Signal(); // now started ok
	
	// start handling requests
	CActiveScheduler::Start();

	// After scheduler stop:
	delete server;
	delete scheduler;
	delete cleanup;

	return KErrNone;
}


// allocation and construction
CBlueGPSServer* CBlueGPSServer::NewL()
{
	CBlueGPSServer* self = new (ELeave) CBlueGPSServer;
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop();
	self->StartL(KBlueGPSServerName);
	return self;
}



// C++ constructor - just pass priority to base CServer class
CBlueGPSServer::CBlueGPSServer() : CServer(EBlueGPSServerPriority)
{
	// declare server name
	__DECLARE_NAME(_S("CBlueGPSServer"));
}

	

// second-phase constructor; create the object container index.
void CBlueGPSServer::ConstructL()
{
	SetConnected( EFalse );

	// create parser
	iParser = CBlueGPSParser::NewL( this );

	// create engine
 	iEngine = CBlueGPSEngine::NewL( this, iParser );

	// initialize semaphore
	User::LeaveIfError( iInfoSemaphore.CreateLocal(1) );
	
	iSessionCount = 0;
	iShutdown = CShutdownServer::NewL();

	/*TInt res = iEngine->ReadDeviceAddressFile();
	if ( res != KErrNone )
		infoPrint( _L("Select a GPS device"), 2);
	else 
		iValidEngineAddr = ETrue;
	*/
 }


// destruction
CBlueGPSServer::~CBlueGPSServer()
{
	Cancel();
	
	//delete CActiveScheduler::Current();
	delete iEngine;
	delete iParser;
	delete iShutdown;
	
	// semaphore no longer needed
	iInfoSemaphore.Close();
}


// Create a new client session for this server.
CSharableSession* CBlueGPSServer::NewSessionL( const TVersion& aVersion ) const
{
	// check version is ok
	TVersion v( KBlueGPSServerMajorVersionNumber, KBlueGPSServerMinorVersionNumber, 
			    KBlueGPSServerBuildVersionNumber);
	if (!User::QueryVersionSupported( v, aVersion ))
		User::Leave(KErrNotSupported);
	// make new session
	RThread client = Message().Client();
	return CBlueGPSServerSession::NewL( client, (CBlueGPSServer*)this );
}


void CBlueGPSServer::SetConnected( TBool aConnected )
{
	iConnected = aConnected;
/*	if ( !iConnected )
	{
		iInfo.iGPSTime = 0;
		iInfo.iLatitudeCP = 0;
		iInfo.iLatitudeXX = 0;
		iInfo.iLatitudeMM = 0;
		iInfo.iLatitudeDDDD = 0;
		iInfo.iLongitudeCP = 0;
		iInfo.iLongitudeYYY = 0;
		iInfo.iLongitudeMM = 0;
		iInfo.iLongitudeDDDD = 0;
	}
*/
}


void CBlueGPSServer::EngineReadAddr()
{
	iEngine->ReadDeviceAddressFile();
}


void CBlueGPSServer::SetGPSInfo( const TBlueGPSInfo& aInfo )
{
	iInfoSemaphore.Wait();
	iInfo = aInfo;
	iInfoSemaphore.Signal();
}


void CBlueGPSServer::GetGPSInfo( TBlueGPSInfo& aInfo )
{
	iInfoSemaphore.Wait();
	aInfo = iInfo;
	iInfoSemaphore.Signal();
}


void CBlueGPSServer::IncrementSessions()
{
	iSessionCount++;
	iShutdown->Cancel();
}

void CBlueGPSServer::DecrementSessions()
{
	iSessionCount--;
	if( iSessionCount > 0 ) return;
	
	iEngine->Stop();	// Not really needed ? bah
	
	iShutdown->Start();
}


void CBlueGPSServer::StartEngine()
{
	iEngine->Start();
}

//FFF
void CBlueGPSServer::StopEngine()
{
	iEngine->Stop();
}



/*
 *********************************************************************
 * CShutdownServer
 *
 *********************************************************************
 */
CBlueGPSServer::CShutdownServer::CShutdownServer() : CActive(EPriorityStandard)
{
	CActiveScheduler::Add(this);
}


CBlueGPSServer::CShutdownServer::~CShutdownServer()
{
	Cancel();
	iTimer.Close();
}


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


void CBlueGPSServer::CShutdownServer::ConstructL()
{
	User::LeaveIfError(iTimer.CreateLocal());
}


void CBlueGPSServer::CShutdownServer::Start()
{
	iTimer.After( iStatus, 0 );
	SetActive();
}


void CBlueGPSServer::CShutdownServer::DoCancel()
{
	iTimer.Cancel();
}


void CBlueGPSServer::CShutdownServer::RunL()
{
	CActiveScheduler::Stop();
}

⌨️ 快捷键说明

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