SMS Stack Tutorial

This tutorial describes how to send and receive messages using the SMS stack.

Send SMS example

#include <e32std.h>
#include <es_sock.h>
#include <gsmubuf.h>
#include <gsmumsg.h>
#include <gsmuset.h>
#include <smsustrm.h>
#include <smsuaddr.h>

_LIT(KSmsMessage, "Sample SMS message for illustration");
_LIT(KDestination,"+44789456123");
_LIT(KServiceCentre, "+44123456789");
RFs  fs;
RSocketServ socketServer;
RSocket socket;

//connect to the file server
User::LeaveIfError(fs.Connect());
CleanupClosePushL(fs);

//connect to the socket server
User::LeaveIfError(socketServer.Connect());
CleanupClosePushL(socketServer);

//Open a SMS socket
User::LeaveIfError(socket.Open(socketServer, KSMSAddrFamily, KSockDatagram, KSMSDatagramProtocol));
CleanupClosePushL(socket);
TSmsAddr smsAddr;

//set the sockets to only send the messages
smsAddr.SetSmsAddrFamily(ESmsAddrSendOnly);

//Bind the socket
socket.Bind(smsAddr);

//create a message and encoding scheme to be used
CSmsBuffer* buffer=CSmsBuffer::NewL();
CSmsMessage* smsMessage=CSmsMessage::NewL(fs, CSmsPDU::ESmsSubmit, buffer);
CleanupStack::PushL(smsMessage);

TSmsUserDataSettings smsSettings;
smsSettings.SetAlphabet(TSmsDataCodingScheme::ESmsAlphabet7Bit);
smsMessage->SetUserDataSettingsL(smsSettings);

smsMessage->SetToFromAddressL(KDestination);
smsMessage->SmsPDU().SetServiceCenterAddressL(KServiceCentre);
buffer->InsertL(0,KSmsMessage);

TRequestStatus status;

RSmsSocketWriteStream writestream(socket);

writestream << *smsMessage;
writestream.CommitL();

TPckgBuf<TUint> sbuf;
socket.Ioctl(KIoctlSendSmsMessage, status, &sbuf, KSolSmsProv);
User::WaitForRequest(status);

CleanupStack::PopAndDestroy(smsMessage);
CleanupStack::PopAndDestroy(&socket);
CleanupStack::PopAndDestroy(&socketServer);
CleanupStack::PopAndDestroy(&fs);

Receive SMS example

#include <e32std.h>
#include <es_sock.h>
#include <gsmubuf.h>
#include <gsmumsg.h>
#include <gsmuset.h>
#include <smsustrm.h>
#include <smsuaddr.h>

_LIT(KSmsMessage, "Sample SMS message for illustration");
_LIT(KDestination,"+44789456123");
_LIT(KServiceCentre, "+44123456789");
RFs fs;
RSocketServ socketServer;
RSocket socket;

//connect to the file server
User::LeaveIfError(fs.Connect());
CleanupClosePushL(fs);

//connect to the socket server
User::LeaveIfError(socketServer.Connect());
CleanupClosePushL(socketServer);

//Open a SMS socket
User::LeaveIfError(socket.Open(socketServer, KSMSAddrFamily, KSockDatagram, KSMSDatagramProtocol));
CleanupClosePushL(socket);

TSmsAddr smsAddr;

//set the sockets to receive all SMS messages
smsAddr.SetSmsAddrFamily(ESmsAddrRecvAny);

//Bind the socket
socket.Bind(smsAddr);
TRequestStatus status;
TPckgBuf<TUint> sbuf;

socket.Ioctl(KIOctlSelect,status,&sbuf,KSOLSocket);
User::WaitForRequest(status);
CSmsBuffer* buffer=CSmsBuffer::NewL();
CSmsMessage* smsMessage=CSmsMessage::NewL(fs, CSmsPDU::ESmsSubmit, buffer);
CleanupStack::PushL(smsMessage);

//read from the stream
RSmsSocketReadStream readstream(socket);

readstream >> *smsMessage;

socket.Ioctl(KIoctlReadMessageSucceeded, status, &sbuf, KSolSmsProv);
User::WaitForRequest(status);
CleanupStack::Pop(smsMessage);    

//clear the contents from buffer
CleanupStack::PopAndDestroy(smsMessage);
CleanupStack::PopAndDestroy(&socket);
CleanupStack::PopAndDestroy(&socketServer);
CleanupStack::PopAndDestroy(&fs);