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

📄 bluetoothtransport.cpp

📁 Symbian OS C++ for Mobile Phones Volume 3 源码
💻 CPP
字号:
// Copyright (c) 2004 - 2007, Symbian Software Ltd. All rights reserved.

#include "bluetoothtransport.h"


// -------- (de)allocation --------

CBluetoothTransport* CBluetoothTransport::NewL(TAny* aTransportCreateInfo)
/**
	This factory function is defined so the class can be instantiated
	via ECOM, which means it can only take a single TAny* argument.
	
	@param	aTransportCreateInfo	Pointer to an instance of
							TTransportCreateInfo, which contains the
							data required to allocate the transport.
	@return					Transport that sends messages to a remote
							device over Bluetooth.  This is owned by
							the caller.
 */
	{
	const TTransportInterfaceCreateInfo& tci =
		*reinterpret_cast<TTransportInterfaceCreateInfo*>(aTransportCreateInfo);
	return New2L(tci.iObserver, *tci.iAddress, tci.iInitListen);
	}

CBluetoothTransport* CBluetoothTransport::New2L(MTransportObserver& aObserver, const TDesC& aProtocolName, TBool aInitListen)
/**
	Factory function allocates a new instance of CBluetoothTransport.

	@param	aObserver		Observer to notify about transport events.
							This is managed by the CTransport superclass.
	@param	aProtocolName	Which protocol is used to communicate with the
							remote device.  Supported values are KRFCOMMDesC
							("RFCOMM") and KL2CAPDesC ("L2CAP").  This is only
							used when establishing a host -> client connection.
							The client reads the protocol from the SDP record.
	@param	aInitListen		If true, the transport should initially expect a
							payload from the remote device.  Otherwise it
							should wait for the local owner to send a payload
							to the remote device.  If this value is true,
							the transport is created as a Bluetooth server;
							otherwise it is created as a Bluetooth client.
	@return					Transport which is connected to a remote device.
							This is owned by the caller.
*/
	{
	if (aInitListen)
		return CBtClientToServer::NewL(aObserver);
	else
		return CBtServerToClient::NewL(aObserver, aProtocolName);
	}

CBluetoothTransport::CBluetoothTransport(MTransportObserver& aObserver)
/**
	This constructor is defined to initialize the CTransport superclass
	with the supplied observer.  It also records the full version of the
	service class.
	
	@param	aObserver		Observer to notify about transport events.
							This is managed by the CTransport superclass.
 */
:	CTransport(aObserver),
	iServiceUuid(KOandXServiceUid)		// appends Symbian OS UUID
	{
	// empty.
	}

void CBluetoothTransport::ConnectToSocketServerL()
/**
	Connect to the socket server.  The client or server subclass
	calls this function before performing its own initialization.
 */
	{
	TInt r = iSocketServ.Connect();
	User::LeaveIfError(r);
	}

CBluetoothTransport::~CBluetoothTransport()
/**
	Closes connection with socket server.
 */
	{
	iSocketServ.Close();
	}

void CBluetoothTransport::FreeDataSocket()
/**
	Destroys the socket which is used to exchange data with the
	remote device.
 */
	{
	delete iBtSocket;
	iBtSocket = 0;
	}


// -------- implement CTransport --------

void CBluetoothTransport::DoSendPayloadL()
/**
	Implement CTransport by copying iPayload into an 8-bit buffer
	and sending it over the connected Bluetooth socket.
	
	CTransport sets to this AO to active when this function returns.
	This AO is completed in HandleSendCompleteL.
	
	@see HandleSendCompleteL
 */
	{
	iPayload8.Copy(iPayload);
	iBtSocket->Write(iPayload8);
	iStatus = KRequestPending;
	
	// CTransport::SendPayload calls SetActive
	}

void CBluetoothTransport::DoLaunchRead()
/**
	Implement CTransport by reading data from the connected
	device into iPayload8.
	
	CTransport sets this AO to active when this function returns.
	This AO is completed in HandleReceiveCompleteL which
	copies the received data into iPayload.
	
	@see HandleReceiveCompleteL
 */
	{
	TRAN_LOG0(">CBluetoothTransport::DoLaunchRead");
	iBtSocket->Read(iPayload8);
	iStatus = KRequestPending;
	
	// CTransport::LaunchRead calls SetActive
	TRAN_LOG0("<CBluetoothTransport::DoLaunchRead");
	}


// -------- partially implement CActive --------

void CBluetoothTransport::DoCancel()
/**
	Implement CActive by cancelling any outstanding reads or writes.
	This object should be destroyed after it has been cancelled.
 */
	{
	if (iBtSocket != 0)
		{
		// this could be implemented more efficiently as a call to
		// CBluetoothSocket::CancelAll, but this shows how the read
		// and write states and identified and handled.

		if (iListening)
			{
			iBtSocket->CancelRead();
			iListening = EFalse;
			}
		else
			iBtSocket->CancelWrite();
		}
	
	CompleteSelf(KErrCancel);
	}


// -------- implement MBluetoothSocketNotifier --------

void CBluetoothTransport::HandleShutdownCompleteL(TInt aErr)
/**
	Implement MBluetoothSocketNotifier by doing nothing.
	
	@param	aErr			Error code.  Not used.
 */
	{
	(void) aErr;
	// empty.
	}
	
void CBluetoothTransport::HandleSendCompleteL(TInt aErr)
/**
	Implement MBluetoothSocketNotifier by completing this object.
	This causes the observer to be notified.
	
	This function is called when the write operation which was
	launched from DoSendPayload completes.

	@param	aErr			Symbian OS error code.
	@see DoSendPayload
 */
	{
	CompleteSelf(aErr);
	}

void CBluetoothTransport::HandleReceiveCompleteL(TInt aErr)
/**
	Implement MBluetoothSocketNotifier by copying the supplied
	payload into iPayload and completing this object.  This causes
	the observer to be notified.
	
	This function is called when the read operation which was
	launched from DoLaunchRead completes.

	@param	aErr			Symbian OS error code.
	@see DoLaunchRead
 */
	{
	TRAN_LOG1(">CBluetoothTransport::HandleReceiveCompleteL,err=%d", aErr);
	
	if (aErr == KErrNone)
		iPayload.Copy(iPayload8);
	
	CompleteSelf(aErr);

	TRAN_LOG0("<CBluetoothTransport::HandleReceiveCompleteL");
	}

void CBluetoothTransport::HandleIoctlCompleteL(TInt aErr)
/**
	Implement MBluetoothSocketNotifier by doing nothing.
	CBluetoothTransport and its subclasses do not use Ioctls.

	@param	aErr			Symbian OS error code.  Not used.
 */
	{
	(void) aErr;
	// empty.
	}

void CBluetoothTransport::HandleActivateBasebandEventNotifierCompleteL(TInt aErr, TBTBasebandEventNotification& aEventNotification)
/**
	Implement MBluetoothSocketNotifier by doing nothing.
	CBluetoothTransport and its subclasses do not use baseband events.
	
	@param	aErr			Symbian OS error code.  Not used.
	@param	aEventNotification The notification which completed.  Not used.
 */
	{
	(void) aErr;
	(void) aEventNotification;
	// empty.
	}

void CBluetoothTransport::HandleAcceptCompleteL(TInt aErr)
/**
	Implement MBluetoothSocketNotifier by panicking.
	
	This function must be implemented for CBtClientToServer to compile,
	but it should only be used by the CBtServerToClient subclass, which
	provides its own implementation.
	
	@param	aErr			Symbian OS error code.  Not used.
 */
	{
	(void) aErr;
	__BTT_ASSERT(EFalse, ETransHacUnexpected);
	// empty.
	}

void CBluetoothTransport::HandleConnectCompleteL(TInt aErr)
/**
	Implement MBluetoothSocketNotifier by panicking.
	
	This function must be implemented for CBtServerToClient to compile,
	but it should only be used by the CBtClientToServer subclass, which
	provides its own implementation.
	
	@param	aErr			Symbian OS error code.  Not used.
 */
	{
	(void) aErr;
	__BTT_ASSERT(EFalse, ETraceHccUnexpected);
	}

⌨️ 快捷键说明

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