📄 gdpbt.cpp
字号:
// gdpbt.cpp
//
// Bluetooth implentation of GDP
//
// Copyright (c) 2000-2002 Symbian Ltd. All rights reserved
#include <btmanclient.h>
#include <gdp.h>
#include "gdpbt.h"
#include <ImplementationProxy.h>
#include <ecom.h>
// Beep then wait a second before carrying on...
#define HIGHBEEP //User::Beep(990,250000); User::After(2000000);
#define BEEP //User::Beep(330,250000); User::After(2000000);
#define SLOW_DEBUG_PRINT(text) //User::InfoPrint(text); User::After(2000000);
#define DEBUG_PRINT(text) //User::InfoPrint(text); User::After(500000);
/**************** GdpBTComms ****************/
CGdpBTComms::CGdpBTComms()
{
}
CGdpBTComms::~CGdpBTComms()
{
REComSession::DestroyedImplementation(iDtor_ID_Key);
// Delete all the things we created in OpenL()
delete iSender;
delete iReceiver;
delete iResMan;
}
CGdpSession* CGdpBTComms::NewL()
{
return new (ELeave) CGdpBTComms();
}
void CGdpBTComms::OpenL(MGdpPacketHandler* aHandler)
{
DEBUG_PRINT(_L("Entering CGdpBTComms::OpenL"));
// Make sure we haven't already got a sender; if we do, Panic!
__ASSERT_ALWAYS(iSender == NULL, GdpUtil::Fault(GdpUtil::EProtocolAlreadyOpen));
// Create a new Resource manager...
iResMan = new(ELeave) CGdpBTResourceManager();
// ...and open it - no params, it just stores info for us
// 5 beeps in here
iResMan->OpenL();
BEEP
// Create a new Sender...
iSender = new(ELeave) CGdpBTSender(*iResMan);
// ...and open it - no params as it just sends whatever is passed to it
BEEP
iSender->OpenL(*aHandler); //Inform handler of successful send
BEEP
// Create a new Receiver...
iReceiver = new(ELeave) CGdpBTReceiver(*iResMan);
// ...and open it - needs to know where to put packets it receives
BEEP
iReceiver->OpenL(*aHandler); // route packets up to the handler passed to us
BEEP
DEBUG_PRINT(_L("Leaving CGdpBTComms::OpenL"));
}
void CGdpBTComms::SendL(const TDesC8& aToAddress, const TDesC8& aData)
// Sends info received from GSDP out to the other Bluetooth device
// specified by the address passed in
{
//Make sure we've got the sender set up before we use it...
__ASSERT_ALWAYS(iSender != NULL, GdpUtil::Fault(GdpUtil::EProtocolNotOpen));
//...to send the message to the specified address
iSender->SendL(aToAddress, aData);
}
TInt CGdpBTComms::ReceiveAll()
//
// Do Nothing! IR uses async. notifications to receive
//
{
// Make sure we've got a receiver - is this neccessaruy??? doesn't hurt...
__ASSERT_ALWAYS(iReceiver != NULL, GdpUtil::Fault(GdpUtil::EProtocolNotOpen));
return KErrNone;
}
TInt CGdpBTComms::GetMaxPacketLength() const
{
return KGdpBTMaxPacketLength;
// We should check LMP frame size, if there
// were any chance we would otherwise exceed it.
}
TBool CGdpBTComms::IsNetworked() const
{
return ETrue; // Uses addressing so is in effect, networked
}
/**************** GdpBTStateMachine ****************/
CGdpBTStateMachine::CGdpBTStateMachine(CGdpBTResourceManager& aResMan)
: CGdpStateMachine(EPriorityStandard), iResMan(aResMan)
{
}
/**************** GdpBTSender ****************/
#pragma warning(disable:4355) // Disable "C4355: 'this' used in member initialization".
CGdpBTSender::CGdpBTSender(CGdpBTResourceManager& aResMan)
// Initialise the parent state machine
: CGdpBTStateMachine(aResMan),
// Make sure our member states know who owns them
iConnectState(*this),
iWriteState(*this)
{
}
#pragma warning(default:4355) // Turn C4355 back on.
CGdpBTSender::~CGdpBTSender()
{
// Close everything down before we die
DEBUG_PRINT(_L("Entering CGdpBTSender::~CGdpBTSender"));
Close();
DEBUG_PRINT(_L("Leaving CGdpBTSender::~CGdpBTSender"));
}
void CGdpBTSender::OpenL(MGdpPacketHandler& aHandler)
{
DEBUG_PRINT(_L("Entering CGdpBTSender::OpenL"));
// Just reset ourselves, the state machine will take care of the rest
iHandler = &aHandler;
Reset();
DEBUG_PRINT(_L("Leaving CGdpBTSender::OpenL"));
}
void CGdpBTSender::Close()
{
// Cancel any outstanding requests...
Cancel();
// ...and then close the socket down
iWriteSocket.Close();
}
void CGdpBTSender::SendL(const TDesC8& aAddress, const TDesC8& aData)
{
TLex8 lex(aAddress);
lex.Mark();
if (!(lex.Get() == '0' && lex.Get() == 'x'))
lex.UnGetToMark();
TInt64 addr;
User::LeaveIfError(lex.Val(addr, EHex));
iAddr.SetBTAddr(TBTDevAddr(addr));
// Check the size of the data we're trying to send:
// - if it's bigger than the maximum we can store in our member
// variable then call GdpUtil::Fault()
__ASSERT_ALWAYS(aData.Size() <= iPacket.MaxSize(), GdpUtil::Fault(GdpUtil::EBadSendDescriptor));
// If the current state is NULL then panic as we're not ready
__ASSERT_DEBUG(CurrentState() != NULL, GdpUtil::Panic(GdpUtil::ESenderNotReady));
// If we're already waiting for another packet to be sent, we can't
// send another one but we don't leave, we just quietly drop the
// overflow packet and quit this send
if (IsActive())
return;
// We're still here so it's OK to send the packet.
iPacket.Copy(aData); // Buffer packet until needed
// Set the number of retries to the maximum allowed
iRetries = KGdpBTSendRetries;
ReEnterCurrentState(); // Kick us off again but remain connected...
}
CGdpStateMachine::TState* CGdpBTSender::ErrorOnStateEntry(TInt /*aError*/)
//
// Failed to init a request. Can't really help it: Give up!
//
{
Reset();
return NULL; // Give up
}
CGdpStateMachine::TState* CGdpBTSender::ErrorOnStateExit(TInt /*aError*/)
//
// Request failed -- reset and re-enter current state if appropriate,
// decrementing number of retries so we don't continuously retry
//
{
Reset();
if (--iRetries < 0)
return NULL;
return CurrentState();
}
void CGdpBTSender::Reset()
{
DEBUG_PRINT(_L("Entering CGdpBTSender::Reset"));
// Start the state machine off in the 'connect' state.
// N.B. Was Discover State for GDP-IR but there's not one in BT implementation
// and as we need a fully operational SDP to do what IrDA did with the 'query'
// state we've skipped that one too
SetNextState(&iConnectState);
DEBUG_PRINT(_L("Leaving CGdpBTSender::Reset"));
}
/**************** GdpBTSender State transitions ****************/
CGdpBTSender::TSenderState::TSenderState(CGdpBTSender& aSender)
// Just set the sender to be whatever was passed in
: iSender(aSender)
{
}
CGdpStateMachine::TState* CGdpBTSender::TSenderState::ErrorL(TInt aCode)
//
// Sender default -- leave, giving the error code.
//
{
User::Leave(aCode);
return NULL; // Never actually hit
}
//--------------- Sender Connect State ---------------//
CGdpBTSender::TConnectState::TConnectState(CGdpBTSender& aSender)
: TSenderState(aSender)
{
}
void CGdpBTSender::TConnectState::EnterL()
{
DEBUG_PRINT(_L("Entering CGdpBTSender::TConnectState::EnterL"));
// What are we going to attempt to connect to?
TProtocolDesc& desc = iSender.iResMan.ProtocolDesc();
// Close the socket, just in case it's already open
iSender.iWriteSocket.Close();
// Now open the socket according to what the Resource Manager told us
User::LeaveIfError(
iSender.iWriteSocket.Open (iSender.iResMan.SocketServer(),
desc.iAddrFamily,
desc.iSockType,
desc.iProtocol)
);
// Use this hard-coded port num for GDP
// GDP will be port 11, 1 = SDP, 3 = RFCOMM, etc
iSender.iAddr.SetPort(0x000b);
// Send the connect request out of the socket!
iSender.iWriteSocket.Connect(iSender.iAddr, iSender.iStatus);
// and wait until we're connected...
iSender.SetActive();
DEBUG_PRINT(_L("Leaving CGdpBTSender::TConnectState::EnterL"));
}
CGdpStateMachine::TState* CGdpBTSender::TConnectState::CompleteL()
{
// The connect worked, so now we want to write same data
// - go into the write state
return &(iSender.iWriteState);
}
void CGdpBTSender::TConnectState::Cancel()
{
// Cancel any connect requests for the socket
iSender.iWriteSocket.CancelConnect();
}
//--------------- Sender Write State ---------------//
CGdpBTSender::TWriteState::TWriteState(CGdpBTSender& aSender)
: TSenderState(aSender)
{
}
void CGdpBTSender::TWriteState::EnterL()
{
// Initiate a write...
iSender.iWriteSocket.Write(iSender.iPacket, iSender.iStatus);
// and wait...
iSender.SetActive();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -