📄 t_datagramservice.cpp
字号:
// T_DatagramService.cpp
//
// Copyright (c) 2003 Symbian Ltd. All rights reserved.
#include "CommonFramework.h"
#include <DatagramService.h>
#include <e32svr.h>
const TUid KSMSDatagramServiceInterfaceUID = {0x101FA9C3};
const TUint KSMSMaxBufferSize = 256;
_LIT8(KTestNumber, "+447900000000"); // Change this
_LIT8(KTestPattern, "//TEST");
_LIT8(KTestMessage, "Test SMS Message");
_LIT(KInitTxt, "\nPress Any Key to Continue");
_LIT(KTerminateTxt, "\nPress Any Key to Exit");
_LIT(KTextEsc,"\n");
_LIT(KSendingSMS, "\nSending SMS...");
_LIT(KSentSMS, "\nSent SMS!");
_LIT(KReceivingSMS, "\nReceiving SMS...");
_LIT(KReceivedSMS, "\nReceived SMS\n!");
//////////////////////////////////////////////////////////////////////////////
//
// -----> CActiveConsole (definition)
//
// Provides the facility to issue key requests, and start send and receive SMS
// tests.
//
//////////////////////////////////////////////////////////////////////////////
class CActiveConsole : public CActive
{
public:
enum TActiveConsoleState {EIdle, ESending, EReceiving};
public:
// Static constuction
static CActiveConsole *NewLC(CConsoleBase* aConsole);
static CActiveConsole *NewL(CConsoleBase* aConsole);
// Construction
CActiveConsole(CConsoleBase* aConsole);
void ConstructL();
// Destruction
~CActiveConsole();
// Issue request
void RequestCharacter();
// Cancel request.
// Defined as pure virtual by CActive;
// implementation provided by this class.
void DoCancel();
void DoStartTestsL();
// Sends an SMS asynchronously.
void DoSendSMSL();
// Receives an SMS asynchronously
void DoReceiveSMSL();
// Service completed request.
// Defined as pure virtual by CActive;
// implementation provided by this class,
void RunL();
// Called from RunL() - an implementation must be provided
// by derived classes to handle the completed request
virtual void ProcessKeyPress(TChar aChar);
protected:
// Data members defined by this class
CConsoleBase* iConsole; // A console for reading from
private:
CDatagramService* iService;
CDatagram* iDatagram;
HBufC8* iBuf;
TActiveConsoleState iState;
};
//////////////////////////////////////////////////////////////////////////////
//
// -----> CActiveConsole (implementation)
//
//////////////////////////////////////////////////////////////////////////////
CActiveConsole::CActiveConsole( CConsoleBase* aConsole)
: CActive(CActive::EPriorityUserInput)
// Construct high-priority active object
{
iConsole = aConsole;
}
CActiveConsole* CActiveConsole::NewLC(CConsoleBase* aConsole)
{
CActiveConsole* self=new (ELeave) CActiveConsole(aConsole);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
CActiveConsole* CActiveConsole::NewL(CConsoleBase* aConsole)
{
CActiveConsole* self=NewLC(aConsole);
CleanupStack::Pop();
return self;
}
void CActiveConsole::ConstructL()
{
// Get the SMS Datagram Service by ECOM UID
iService = CDatagramService::NewL(KSMSDatagramServiceInterfaceUID);
iState = EIdle;
// Add to active scheduler
CActiveScheduler::Add(this);
}
CActiveConsole::~CActiveConsole()
{
// Make sure we're cancelled
Cancel();
delete iService;
delete iDatagram;
delete iBuf;
}
void CActiveConsole::DoCancel()
{
iConsole->ReadCancel();
}
void CActiveConsole::RunL()
{
switch (iState)
{
case ESending:
{
// Finished sending tests move on to receiving
iConsole->Printf(KSentSMS);
iConsole->Printf(KReceivingSMS);
iState = EIdle;
DoReceiveSMSL();
break;
}
case EReceiving:
{
// Finished receiving tests. All tests completed
iConsole->Printf(KReceivedSMS);
TBuf<KSMSMaxBufferSize> recvBuf;
recvBuf.Copy(iDatagram->GetData());
iConsole->Printf(recvBuf);
iConsole->Printf(KTerminateTxt);
iConsole->Getch();
CActiveScheduler::Stop();
return;
}
default:
// Handle completed request
ProcessKeyPress(TChar(iConsole->KeyCode()));
}
}
void CActiveConsole::RequestCharacter()
{
iConsole->Printf(KInitTxt);
// A request is issued to the CConsoleBase to accept a
// character from the keyboard.
iConsole->Read(iStatus);
SetActive();
}
void CActiveConsole::ProcessKeyPress(TChar aChar)
{
switch (aChar)
{
case EKeyEscape :
// "Esc" character prints a new line and stops the scheduler
iConsole->Printf(KTextEsc);
CActiveScheduler::Stop();
return;
case EKeyEnter :
// "Enter" prints a new line character
// An alphabetic or space is printed as a character;
// anything else is printed as an integer.
iConsole->Printf(KTextEsc);
break;
default :
{
// Begin SMS tests by sending an SMS
iConsole->Printf(KSendingSMS);
DoSendSMSL();
}
}
}
void CActiveConsole::DoSendSMSL()
{
if (iState != EIdle)
return;
// Create the Datagram we wish to populate.
delete iDatagram;
iDatagram = NULL;
iDatagram = CDatagram::NewL(KTestMessage(), KTestNumber());
// and now send it asynchronously
iService->SendL(iDatagram, iStatus);
iState = ESending;
SetActive();
}
void CActiveConsole::DoReceiveSMSL()
{
if (iState != EIdle)
return;
// Create our buffer to store received data
delete iBuf;
iBuf = NULL;
iBuf = HBufC8::NewMaxL(KSMSMaxBufferSize);
// Create the Datagram we wish to populate.
delete iDatagram;
iDatagram = NULL;
iDatagram = CDatagram::NewL(*iBuf);
iService->ReceiveL(iDatagram, KTestPattern(), iStatus);
iState = EReceiving;
SetActive();
}
// do the example
LOCAL_C void doExampleL()
{
// Construct and install the active scheduler
CActiveScheduler* scheduler = new (ELeave) CActiveScheduler;
// Push onto the cleanup stack
CleanupStack::PushL(scheduler);
// Install as the active scheduler
CActiveScheduler::Install(scheduler);
CActiveConsole* activeConsole = CActiveConsole::NewLC(console);
// Issue the first request
activeConsole->RequestCharacter();
// Main part of the program is a wait loop
CActiveScheduler::Start();
// Remove from the cleanup stack and destroy:
// 1. the CMessageKeyProcessor active object.
// 2. Active Scheduler
CleanupStack::PopAndDestroy(2);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -