commands/sms/smssend.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // smssend.cpp
       
     2 // 
       
     3 // Copyright (c) 2008 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 // 
       
     9 // Initial Contributors:
       
    10 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include <gsmuset.h>
       
    14 #include "smssend.h"
       
    15 
       
    16 CSmsSender* CSmsSender::NewL(RFs& aFs, MSmsSendObserver& aParent, const TDesC& aMsgToSend, const TDesC& aDestination)
       
    17 	{
       
    18 	CSmsSender* self = new (ELeave) CSmsSender(aParent, aFs, aMsgToSend, aDestination);
       
    19 	CleanupStack::PushL(self);
       
    20 	self->ConstructL();
       
    21 	CleanupStack::Pop(self);
       
    22 	return self;
       
    23 	}
       
    24 
       
    25 CSmsSender::CSmsSender(MSmsSendObserver& aParent, RFs& aFs, const TDesC& aMsgToSend, const TDesC& aDestination):
       
    26 CActive(CActive::EPriorityStandard), iParent(aParent), iFs(aFs), iMsgContent(aMsgToSend), iDestination(aDestination), iState(ESmsSenderIdle)
       
    27 	{
       
    28 	CActiveScheduler::Add(this);
       
    29 	}
       
    30 
       
    31 CSmsSender::~CSmsSender()
       
    32 	{
       
    33 	// iMsgBuffer doesn't need to be explicitly deleted. iMsgSend takes care of that
       
    34 	if (iMsgToSend)
       
    35 		delete iMsgToSend;
       
    36 	if (iSmspList)
       
    37 		delete iSmspList;
       
    38 	if (iMobileMessaging.SubSessionHandle() > 0)
       
    39 		iMobileMessaging.Close();
       
    40 	if (iPhone.SubSessionHandle() > 0)
       
    41 		iPhone.Close();
       
    42 	if (iTelServer.Handle() > 0)
       
    43 		iTelServer.Close();
       
    44 	if (iSocket.SubSessionHandle())
       
    45 		iSocket.Close();
       
    46 	if (iSocketServer.Handle())
       
    47 		iSocketServer.Close();
       
    48 	}
       
    49 
       
    50 void CSmsSender::ConstructL()
       
    51 	{
       
    52 	// connect to ETel. Note we're assuming the TSY has been loaded already
       
    53 	RTelServer::TPhoneInfo phoneInfo;
       
    54 	User::LeaveIfError(iTelServer.Connect());
       
    55 	User::LeaveIfError(iTelServer.GetPhoneInfo(0, phoneInfo));
       
    56 	
       
    57 	// open the first phone object
       
    58 	User::LeaveIfError(iPhone.Open(iTelServer, phoneInfo.iName));
       
    59 	
       
    60 	// open a hook to messaging functionality
       
    61 	User::LeaveIfError(iMobileMessaging.Open(iPhone));
       
    62 	
       
    63 	// JoeF: I can't see a sane way to just retrieve the gsm service centre address from the sim?!
       
    64 	iSmspList = CRetrieveMobilePhoneSmspList::NewL(iMobileMessaging);
       
    65 	
       
    66 	// connect & bind the sms socket
       
    67 	User::LeaveIfError(iSocketServer.Connect());
       
    68 	User::LeaveIfError(iSocket.Open(iSocketServer,KSMSAddrFamily,KSockDatagram,KSMSDatagramProtocol));
       
    69 	iSmsAddr.SetSmsAddrFamily(ESmsAddrSendOnly);
       
    70 	User::LeaveIfError(iSocket.Bind(iSmsAddr));
       
    71 	}
       
    72 
       
    73 void CSmsSender::StartL()
       
    74 	{
       
    75 	ASSERT(!IsActive());
       
    76 	
       
    77 	// retrieve the gsm service centre number from the sim
       
    78 	iSmspList->Start(iStatus);
       
    79 	iState = ESmsSenderGetServiceCentre;
       
    80 	SetActive();
       
    81 	}
       
    82 
       
    83 void CSmsSender::RunL()
       
    84 	{
       
    85 	TInt error = iStatus.Int();
       
    86 	if (error != KErrNone)
       
    87 		{
       
    88 		iParent.SendComplete(error);
       
    89 		return;
       
    90 		}
       
    91 	switch (iState)
       
    92 		{
       
    93 		case ESmsSenderGetServiceCentre:
       
    94 			{
       
    95 			error = GetServiceCentreAndSendSms();
       
    96 			if (error == KErrNone)
       
    97 				return;
       
    98 			}
       
    99 		break;
       
   100 		
       
   101 		case ESmsSenderSendMessage:
       
   102 			iState = ESmsSenderIdle;
       
   103 		break;
       
   104 		
       
   105 		default:
       
   106 		break;
       
   107 		};
       
   108 	iParent.SendComplete(error);
       
   109 	}
       
   110 
       
   111 void CSmsSender::DoCancel()
       
   112 	{
       
   113 	switch (iState)
       
   114 		{
       
   115 		case ESmsSenderGetServiceCentre:
       
   116 			iSmspList->Cancel();
       
   117 		break;
       
   118 		
       
   119 		case ESmsSenderSendMessage:
       
   120 			iSocket.CancelIoctl();
       
   121 		break;
       
   122 		
       
   123 		default:
       
   124 		break;
       
   125 		};
       
   126 	}
       
   127 
       
   128 //
       
   129 // CSmsSender::GetServiceCentreAndSendSms
       
   130 // retrieve the service centre phone number & 
       
   131 // create a correctly formatted sms message from
       
   132 // the constiuent parts gathered. Send the sms
       
   133 //
       
   134 TInt CSmsSender::GetServiceCentreAndSendSms()
       
   135 	{
       
   136 	TRAPD(err, RetrieveServiceCentreL());
       
   137 	if (err == KErrNone)
       
   138 		TRAP(err, CreateAndSendSmsL());
       
   139 	return err;
       
   140 	}
       
   141 
       
   142 //
       
   143 // CSmsSender::RetrieveServiceCentreL
       
   144 // extract the service centre from the smsp list
       
   145 //
       
   146 void CSmsSender::RetrieveServiceCentreL()
       
   147 	{
       
   148 	if (iSmspList)
       
   149 		{
       
   150 		CMobilePhoneSmspList* list = iSmspList->RetrieveListL();
       
   151 		if (list)
       
   152 			{
       
   153 			TInt count = list->Enumerate();
       
   154 			while (--count >= 0)
       
   155 				{
       
   156 				const RMobileSmsMessaging::TMobileSmspEntryV1& entry = list->GetEntryL(count);
       
   157 				if (entry.iServiceCentre.iTelNumber.Length() > 0)
       
   158 					{
       
   159 					iServiceCentre.iTypeOfNumber = entry.iServiceCentre.iTypeOfNumber;
       
   160 					iServiceCentre.iNumberPlan = entry.iServiceCentre.iNumberPlan;
       
   161 					iServiceCentre.iTelNumber = entry.iServiceCentre.iTelNumber;
       
   162 					return;
       
   163 					}
       
   164 				}
       
   165 			}
       
   166 		}
       
   167 	User::Leave(KErrNotFound);
       
   168 	}
       
   169 
       
   170 //
       
   171 // CSmsSender::CreateAndSendSmsL
       
   172 // create a correctly formatted sms from the available data
       
   173 // send it on its way via smsstack
       
   174 //
       
   175 void CSmsSender::CreateAndSendSmsL()
       
   176 	{
       
   177 	TSmsUserDataSettings settings;
       
   178 	settings.SetAlphabet(TSmsDataCodingScheme::ESmsAlphabet7Bit);
       
   179 	settings.SetTextCompressed(EFalse);
       
   180 	
       
   181 	iMsgBuffer = CSmsBuffer::NewL();
       
   182 	iMsgBuffer->InsertL(0, iMsgContent);
       
   183 	iMsgToSend = CSmsMessage::NewL(iFs, CSmsPDU::ESmsSubmit, iMsgBuffer);
       
   184 	iMsgToSend->SetUserDataSettingsL(settings);
       
   185 	iMsgToSend->SetToFromAddressL(iDestination);
       
   186 	iMsgToSend->SmsPDU().SetServiceCenterAddressL(iServiceCentre.iTelNumber);
       
   187 	
       
   188 	ASSERT(iSocket.SubSessionHandle() > 0);
       
   189 	RSmsSocketWriteStream writestream(iSocket);
       
   190 	writestream << *iMsgToSend;
       
   191 	writestream.CommitL();
       
   192 
       
   193 	TPckgBuf<TUint> sbuf;
       
   194 	iSocket.Ioctl(KIoctlSendSmsMessage, iStatus, &sbuf, KSolSmsProv);
       
   195 	SetActive();
       
   196 	iState = ESmsSenderSendMessage;
       
   197 	}
       
   198 
       
   199 
       
   200