// System include
#include <bt_sock.h>
#include <s32mem.h>
// User include
#include "BluetoothServer.h"
#include "BluetoothAdvertiser.h"
#include "BluetoothChatApplication.h"
/**
* Constructor.
*
* Set observer and flag to indicate not connected
* Add this active object to the scheduler
*
* @param aObserver reference to MBluetoothObserver
**/
CBluetoothServer::CBluetoothServer(MBluetoothObserver& aObserver)
: CActive(CActive::EPriorityStandard),
iObserver(aObserver),
iState(EDisconnected)
{
CActiveScheduler::Add(this);
}
/**
* Factory Constructor.
* Only available way to construct class.
* This function can leave L
* @param none
* @return new instance of the CBluetoothServer
*/
CBluetoothServer* CBluetoothServer::NewL(MBluetoothObserver& aObserver)
{
CBluetoothServer* self = NewLC(aObserver);
CleanupStack::Pop(self);
return self;
}
/**
* Factory Constructor.
* Only available way to construct class.
* This function can leave L, returning value is on Cleanup Stack C
* @param none
* @return new instance of the CBluetoothServer on Cleanup stack
*/
CBluetoothServer* CBluetoothServer::NewLC(MBluetoothObserver& aObserver)
{
CBluetoothServer* self = new (ELeave) CBluetoothServer(aObserver);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
/**
* Destructor.
*
* Cancel this active object, and disconnect the Server
* Delete the advertiser
*
* @param none
* @retval none
**/
CBluetoothServer::~CBluetoothServer()
{
TRAPD(err,StopL());
// Panic in debug if this didn't work
__ASSERT_DEBUG(err == KErrNone, Panic(EErrorStoppingServer));
Cancel();
// Close handles
// if (iSecSettingsSession.SubSessionHandle() != 0)
// {
// iSecSettingsSession.Close();
// }
if (iSecManager.Handle() != 0)
{
iSecManager.Close();
}
if (iAcceptedSocket.SubSessionHandle() != 0)
{
iAcceptedSocket.Close();
}
if (iListeningSocket.SubSessionHandle() != 0)
{
iListeningSocket.Close();
}
if (iSocketServer.Handle() != 0)
{
iSocketServer.Close();
}
delete iAdvertiser;
}
/**
* ConstructL
*
* Create the advertiser
*
* @see NewL
* @see NewLC
* @param none
* @return none
**/
void CBluetoothServer::ConstructL()
{
iAdvertiser = CBluetoothAdvertiser::NewL();
}
void CBluetoothServer::DoCancel()
{
switch(iState)
{
case EDisconnected :
{
break;
}
case ESettingSecurity :
{
//iSecSettingsSession.CancelRequest(iStatus); // not asynch call
break;
}
case EConnecting :
{
iListeningSocket.CancelAccept();
break;
}
case EConnected :
{
break;
}
case EWaitingForMessage :
{
iAcceptedSocket.CancelRecv();
break;
}
case ESendData :
{
iAcceptedSocket.CancelSend();
break;
}
default:
{
break;
}
}
}
/**
* StartServerL.
*
* Connect to Socket Server
* Open a listening socket on the Server on the RFCOMM protocol (KServerTransportName)
* Get a channel to listen on
* Bind the socket to this port, and listen
* Open another socket on the Server
* Issue an asynchronous Accept on the listening socket, passing through the AcceptedSocket
* When the Accept completes the Accepted Socket may be utilised for communication and the listening socket will continue
* listening for connections.
* Set Security options on the Port/Channel
*
* @param none
* @return none
**/
void CBluetoothServer::StartServerL()
{
if (iState != EDisconnected)
{
User::Leave(KErrInUse);
}
User::LeaveIfError(iSocketServer.Connect());
User::LeaveIfError(iListeningSocket.Open(iSocketServer, KServerTransportName));
// Get a channel to listen on - same as the socket's port number
User::LeaveIfError(iListeningSocket.GetOpt(KRFCOMMGetAvailableServerChannel, KSolBtRFCOMM, iChannel));
TBTSockAddr listeningAddress;
listeningAddress.SetPort(iChannel);
User::LeaveIfError(iListeningSocket.Bind(listeningAddress));
User::LeaveIfError(iListeningSocket.Listen(KListeningQueSize));
SetSecurityOnChannelL(EFalse, EFalse, ETrue);
}
/**
* SetSecurityOnChannelL.
*
* Connect to security manager
* Open a subsession on the security manager used to register settings
* Build the security settings within a TBTServiceSecurity object
* Register these settings
*
* @param aAuthentication true if you wish to enforce authentication
* @param aEncryption true if you wish to enforce encryption on the incomming data
* @param eAuthorisation true if you wish to enforce authorisation.
* @return none
**/
void CBluetoothServer::SetSecurityOnChannelL(TBool aAuthentication, TBool aEncryption, TBool aAuthorisation)
{
// connect to the security manager and open a settings session.
User::LeaveIfError(iSecManager.Connect());
// the security settings
TBTServiceSecurity serviceSecurity;
//Define security requirements
serviceSecurity.SetAuthentication(aAuthentication);
serviceSecurity.SetEncryption(aEncryption);
serviceSecurity.SetAuthorisation(aAuthorisation);
serviceSecurity.SetUid(KUidBluetoothChatApp);
// make asynch request
iBTSockAddr.SetSecurity(serviceSecurity);
iState = ESettingSecurity;
SetActive();
}
/**
* AcceptConnectionsL.
*
* Sets the socket which should be used to accept incoming connections at the
* listening socket.
*
* @param none
* @return none
**/
void CBluetoothServer::AcceptConnectionsL()
{
iAcceptedSocket.Close(); // close old connection - if any
User::LeaveIfError(iAcceptedSocket.Open(iSocketServer)); // Open abstract socket
iState = EConnecting;
// set the listening socket to accept new connections on
// the accepted socket.
iListeningSocket.Accept(iAcceptedSocket, iStatus);
SetActive();
// notify the observer that the server has started
iObserver.ServerStartedL();
}
/**
* StartAdvertisingL.
*
* Start Advertising
*
* @param none
* @return none
**/
void CBluetoothServer::StartAdvertisingL()
{
iAdvertiser->StartAdvertisingL(iChannel);
iAdvertiser->UpdateAvailabilityL(ETrue);
}
/**
* StopL.
*
* Stop Advertising
* Close the communication socket
* Close the listening socket
* Close the connection to the socket server
*
* @param none
* @return none
**/
void CBluetoothServer::StopL()
{
if (iState != EDisconnected)
{
if (iAdvertiser->IsAdvertising())
{
iAdvertiser->StopAdvertisingL();
}
iAcceptedSocket.Close();
iListeningSocket.Close();
iSocketServer.Close();
}
iState = EDisconnected;
}
/**
* SendL.
*
* Issue an asynchronous Write on the socket, passing through the message to send
*
* @param aMessage the message to send the client
* @return none
**/
void CBluetoothServer::Send(const TDesC& aMessage)
{
TRAPD(err, SendL(aMessage));
}
void CBluetoothServer::SendL(const TDesC& aMessage)
{
iMessage.Zero();
TDesBuf buffer;
buffer.Set (iMessage);
RWriteStream stream(&buffer);
CleanupClosePushL(stream);
stream << aMessage;
CleanupStack::PopAndDestroy();
iState = ESendData;
iAcceptedSocket.Write(iMessage, iStatus);
SetActive();
}
/**
* RequestData.
*
* Issue an asynchronous receive function on the socket, passing through a buffer to be populated
* @param none
* @return none
**/
void CBluetoothServer::RequestData()
{
iMessage.Zero();
iState = EWaitingForMessage;
iAcceptedSocket.RecvOneOrMore(iMessage, 0, iStatus, iLen);
SetActive();
}
/**
* RunL
*
* Called when an asynchronous request completes.
* iStatus variable indicates error conditions
* iState indicates present state of the Server
*
* @param none
* @return none
**/
void CBluetoothServer::RunL()
{
if (iStatus.Int() == KErrNone)
{
switch (iState)
{
case ESettingSecurity :
{
// cleanup after security settings
//iSecSettingsSession.Close();
iSecManager.Close();
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
BluetoothChat.rar (82个子文件)
BluetoothChat
data
BluetoothChat_reg.rss 738B
BluetoothChat.rls 1KB
BluetoothChat.rss 7KB
src
BluetoothChatDialog.cpp 856B
BluetoothAdvertiser.cpp 5KB
TBluetoothAttributeParser.cpp 3KB
BluetoothChatDocument.cpp 3KB
BluetoothChatAppUi.cpp 9KB
BluetoothChatApplication.cpp 2KB
BluetoothDeviceSearcher.cpp 3KB
BluetoothServiceSearcher.cpp 9KB
BluetoothChat.cpp 562B
BluetoothClient.cpp 5KB
BluetoothServer.cpp 9KB
.generated
winscw_uid.cpp 252B
.cdtproject 906B
.cdtbuild 13KB
inc
BluetoothDefinitions.h 987B
BluetoothServer.h 2KB
BluetoothChat.hrh 865B
TBluetoothAttributeParser.h 1KB
BluetoothDeviceSearcher.h 931B
BluetoothServiceSearcher.h 2KB
BluetoothChatDocument.h 3KB
BluetoothAdvertiser.h 1KB
BluetoothClient.h 2KB
BluetoothChatApplication.h 1KB
BluetoothAttributeObserver.h 243B
BluetoothChat.pan 705B
BluetoothChatDialog.h 642B
BluetoothChatAppUi.h 2KB
BluetoothObserver.h 393B
.settings
org.eclipse.cdt.managedbuilder.core.prefs 3KB
gfx
qgn_menu_BluetoothChat.svg 4KB
BluetoothChat.mifdef 36B
sis
BluetoothChat.pkg 1KB
backup_registration.xml 144B
S60 3.0 Emulator Debug
BluetoothChatDialog.inc 1.14MB
BluetoothClient.inc 610KB
data
subdir.mk 2KB
src
BluetoothChat.o 19KB
BluetoothChatApplication.d 700B
BluetoothAdvertiser.o 34KB
BluetoothClient.o 57KB
subdir.mk 26KB
BluetoothClient.d 759B
BluetoothServiceSearcher.o 75KB
TBluetoothAttributeParser.d 827B
BluetoothChatDialog.o 43KB
BluetoothChatAppUi.o 472KB
BluetoothServiceSearcher.d 944B
BluetoothChatDocument.o 65KB
BluetoothAdvertiser.d 673B
BluetoothChatAppUi.d 2KB
BluetoothChat.d 553B
BluetoothChatApplication.o 12KB
BluetoothServer.o 69KB
BluetoothChatDialog.d 561B
BluetoothChatDocument.d 785B
BluetoothServer.d 989B
BluetoothDeviceSearcher.d 799B
TBluetoothAttributeParser.o 21KB
BluetoothDeviceSearcher.o 30KB
.generated
winscw_uid.o 3KB
subdir.mk 3KB
winscw_uid.d 445B
BluetoothServer.inc 793KB
BluetoothDeviceSearcher.inc 502KB
TBluetoothAttributeParser.inc 635KB
BluetoothChat.inc 535KB
BluetoothAdvertiser.inc 636KB
BluetoothChatAppUi.inc 2.12MB
gfx
subdir.mk 1KB
sis
BluetoothChat.pkg 1KB
objects.mk 568B
BluetoothChatDocument.inc 1.19MB
sources.mk 1010B
makefile 2KB
winscw_uid.inc 163KB
BluetoothServiceSearcher.inc 901KB
BluetoothChatApplication.inc 540KB
.project 819B
共 82 条
- 1
sijialogin
- 粉丝: 3
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0