telephonyserverplugins/multimodetsy/Multimode/sms/mSMSWRIT.CPP
changeset 0 3553901f7fa8
equal deleted inserted replaced
-1:000000000000 0:3553901f7fa8
       
     1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // This writes an SMS message and stores the message location number. AT+CMGW=xx
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "NOTIFY.H"
       
    19 #include "mSMSMESS.H"
       
    20 #include "mSMSWRIT.H"
       
    21 #include "mSLOGGER.H"
       
    22 #include "ATIO.H"
       
    23 #include "mSMSSTOR.H"
       
    24 #include "smsutil.h"
       
    25 #include "et_phone_util.h"
       
    26 
       
    27 _LIT8(KSmsWriteLengthCommand,"AT+CMGW = %d\r");
       
    28 _LIT8(KSmsWriteCommand,"AT+CMGW = %d,%d\r");
       
    29 
       
    30 CATSmsMessagingWrite* CATSmsMessagingWrite::NewL(CATIO* aIo, CTelObject* aTelObject, CATInit* aInit, CPhoneGlobals* aMmStatus)
       
    31 	{
       
    32 	CATSmsMessagingWrite* self = new(ELeave) CATSmsMessagingWrite(aIo, aTelObject, aInit, aMmStatus);
       
    33 	CleanupStack::PushL(self);
       
    34 	self->ConstructL();
       
    35 	CleanupStack::Pop();
       
    36 	return self;
       
    37 	}
       
    38 
       
    39 CATSmsMessagingWrite::CATSmsMessagingWrite(CATIO* aIo, CTelObject* aTelObject, CATInit* aInit, CPhoneGlobals* aMmStatus)
       
    40 	:CATSmsCommands(aIo, aTelObject, aInit, aMmStatus)
       
    41 	,iExpectString(NULL)
       
    42 	,iSmsEntry(NULL)
       
    43 	,iCancelled(EFalse)
       
    44 	,iSmsStore(reinterpret_cast<CMobileSmsStore*>(aTelObject))
       
    45 	{
       
    46 	}
       
    47 
       
    48 CATSmsMessagingWrite::~CATSmsMessagingWrite()
       
    49 	{}
       
    50 
       
    51 void CATSmsMessagingWrite::Start(TTsyReqHandle aTsyReqHandle, TAny* aParams)
       
    52 	{
       
    53 	__ASSERT_DEBUG(aParams,Panic(EMobileSmsMessagingNullParameter));
       
    54 	__ASSERT_DEBUG(aTsyReqHandle!=TSY_HANDLE_INIT_VALUE,Panic(EMobileSmsMessagingNullParameter));
       
    55 
       
    56 	// Save the parameters into our class data
       
    57 	iReqHandle = aTsyReqHandle;
       
    58 	iCancelled=EFalse;
       
    59 	iSmsEntry = reinterpret_cast<RMobileSmsStore::TMobileGsmSmsEntryV1*>(aParams);
       
    60 
       
    61 	iNextAttempt=iPhoneGlobals->iPhoneTestState;
       
    62 	// If undefined, try old standard first
       
    63 	if(iNextAttempt==CPhoneGlobals::EPhoneTestUndefined)
       
    64 		iNextAttempt=CPhoneGlobals::EPhoneTestOldStandard;
       
    65 
       
    66 	iPhoneGlobals->iEventSignalActive = ETrue;
       
    67 	if (ComparePrefMem(iSmsStore->iStoreName))
       
    68 		StartPduWrite();
       
    69 	else // Compared memories are different
       
    70 		{
       
    71 		SetCurrentPrefMem(iSmsStore->iStoreName);
       
    72 		iState = EATWaitForSendingPrefMemComplete;
       
    73 		}
       
    74 	}
       
    75 
       
    76 void CATSmsMessagingWrite::EventSignal(TEventSource aSource)
       
    77 	{
       
    78 	LOGTEXT2(_L8("CATSmsMessagingWrite:\tiState = %D"), iState);
       
    79 
       
    80 	if (aSource == ETimeOutCompletion)
       
    81 		{
       
    82 		LOGTEXT(_L8("CATSmsMessagingWrite:\tTimeout Error during Sms Messaging Write"));
       
    83 		iPhoneGlobals->iPhoneStatus.iModemDetected = RPhone::EDetectedNotPresent;
       
    84 		RemoveStdExpectStrings();
       
    85 		if ((iState != EATWaitForPrefMemResponse)&&!ComparePrefMem(iSmsStore->iStoreName))
       
    86 			iPhoneGlobals->iPhonePrefMem = iSmsStore->iStoreName; 
       
    87 		Complete(KErrTimedOut, aSource);
       
    88 		return;
       
    89 		} //if
       
    90 
       
    91 	switch(iState)
       
    92 		{
       
    93 	// the following 3 cases are special cases, occuring only when the PhonePrefMem != ClientPrefMem
       
    94 	case EATWaitForSendingPrefMemComplete:
       
    95 		//
       
    96 		// Finished writing preferred memory, now wait for response
       
    97 		//
       
    98 			{
       
    99 			HandleWriteCompletion(aSource);
       
   100 			iState = EATWaitForPrefMemResponse;
       
   101 			}
       
   102 		break;
       
   103 
       
   104 	case EATWaitForPrefMemResponse:
       
   105 		//
       
   106 		// Got response from writing preferred memory.  Validate response, and if
       
   107 		// successful, then start the write sequence
       
   108 		//
       
   109 			{	
       
   110 			TInt ret=HandleResponseCompletion(aSource,EFalse);
       
   111 			if (ret!=KErrNone)
       
   112 				{
       
   113 				Complete(ret,aSource);
       
   114 				return;
       
   115 				}		
       
   116 			TInt pos=iIo->BufferFindF(KCPMSResponseString);
       
   117 			if (pos == KErrNotFound)
       
   118 				{
       
   119 				Complete(pos, aSource);
       
   120 				return;
       
   121 				}
       
   122 			LOGTEXT(_L8("CATSmsMessagingWrite:\tPhone's preferred memory successfully set to client's preferred memory."));
       
   123 			iPhoneGlobals->iPhonePrefMem=iSmsStore->iStoreName;
       
   124 			if (iCancelled)
       
   125 				Complete(KErrCancel,aSource);
       
   126 			else
       
   127 				StartPduWrite();
       
   128 			}
       
   129 		break;
       
   130 
       
   131 	case EATWaitForSendingPduLengthComplete:
       
   132 		//
       
   133 		// Finished sending pdu length command, now wait for response
       
   134 		//
       
   135 			{
       
   136 			HandleWriteCompletion(aSource);
       
   137 			iExpectString=iIo->AddExpectString(this,KSmsEnterPduModeResponse,ETrue); // Expecting "> "
       
   138 			iIo->SetTimeOut(this, 5000);
       
   139 			iState=EATReadPduEnterPduModeResponseCompleted;
       
   140 			}
       
   141 		break;
       
   142 	
       
   143 	case EATReadPduEnterPduModeResponseCompleted:
       
   144 		//
       
   145 		// Received a response to the pdu length command.  Validate, and if successful
       
   146 		// send the actual pdu in ascii form
       
   147 		//
       
   148 			{
       
   149 			TInt ret=HandleResponseCompletion(aSource,EFalse);
       
   150 			if (ret!=KErrNone)
       
   151 				{
       
   152 				Complete(ret,aSource);
       
   153 				return;
       
   154 				}		
       
   155 			ret=ConvertCMSErrorToKErr(CMSErrorValue());
       
   156 			iIo->RemoveExpectString(iExpectString);
       
   157 			iExpectString=NULL;
       
   158 			if(ret)
       
   159 				{
       
   160 				Complete(ret,aSource);
       
   161 				return;
       
   162 				}
       
   163 			iMsgDataAscii.Append(KCtrlZChar); // Adding the CTRL-Z that indicates the end of the PDU
       
   164 			iIo->Write(this,iMsgDataAscii);
       
   165 			iIo->SetTimeOut(this, 5000);
       
   166 			iState=EATWaitForSendingPduComplete;
       
   167 			}
       
   168 		break;
       
   169 
       
   170 	case EATWaitForSendingPduComplete:
       
   171 		//
       
   172 		// Finished writing pdu, wait for a response
       
   173 		//
       
   174 			{
       
   175 			HandleWriteCompletion(aSource);
       
   176 			iState=EATReadSmsRefNumCompleted;
       
   177 			}
       
   178 		break;
       
   179 
       
   180 	case EATReadSmsRefNumCompleted:
       
   181 		//
       
   182 		// Received response to pdu write, validate and if successful set preferred
       
   183 		// memory back to original value if it was changed for this command,
       
   184 		// otherwise on failure try the new format if the current attempt used the
       
   185 		// old ETSI format
       
   186 		//
       
   187 			{
       
   188 			TInt ret=HandleResponseCompletion(aSource,EFalse);
       
   189 			if (ret!=KErrNone)
       
   190 				{
       
   191 				Complete(ret,aSource);
       
   192 				return;
       
   193 				}		
       
   194 			TInt err=ConvertCMSErrorToKErr(CMSErrorValue());
       
   195 			
       
   196 			if (err==KErrNone)
       
   197 			 	TRAP(err,ParseResponseL());
       
   198 			
       
   199 			if(err!=KErrNone)
       
   200 				{ 
       
   201 				if (iCancelled)
       
   202 					Complete(KErrCancel,aSource);
       
   203 				else
       
   204 					{	
       
   205 					// Try new standard if old standard failed
       
   206 					LOGTEXT2(_L8("CATSmsMessagingWrite:\tFailed with code %d"),err);
       
   207 					if (iNextAttempt==CPhoneGlobals::EPhoneTestOldStandard)
       
   208 						{
       
   209 						iNextAttempt=CPhoneGlobals::EPhoneTestNewStandard;
       
   210 						StartPduWrite();
       
   211 						}
       
   212 					else
       
   213 						// Must have tried new standard and this failed too
       
   214 						Complete(err);
       
   215 					}
       
   216 				}
       
   217 			else
       
   218 				{
       
   219 				// If new standard worked then store this is as default for this phone
       
   220 				if (iNextAttempt==CPhoneGlobals::EPhoneTestNewStandard)
       
   221 					iPhoneGlobals->iPhoneTestState=CPhoneGlobals::EPhoneTestNewStandard;
       
   222 				Complete(KErrNone,aSource);
       
   223 				}
       
   224 			}
       
   225 		break;
       
   226 
       
   227 
       
   228 	case EATCancellingWaitForWriteComplete:
       
   229 		//
       
   230 		// Finished writing cancel request, now wait for response
       
   231 		//
       
   232 			{
       
   233 			__ASSERT_ALWAYS(aSource==EWriteCompletion,Panic(EATCommand_IllegalCompletionWriteExpected));
       
   234 			AddStdExpectStrings();
       
   235 			iIo->SetTimeOut(this);
       
   236 			iState=EATCancellingReadCompleted;
       
   237 			}
       
   238 		break;
       
   239 
       
   240 	case EATCancellingReadCompleted:
       
   241 		//
       
   242 		// Cancel command response received
       
   243 		//
       
   244 			{
       
   245 			__ASSERT_ALWAYS(aSource==EReadCompletion,Panic(EATCommand_IllegalCompletionReadExpected));
       
   246 			iIo->WriteAndTimerCancel(this);
       
   247 			Complete(KErrCancel, aSource);
       
   248 			}
       
   249 		break;
       
   250 
       
   251 	default:
       
   252 		break;
       
   253 		}
       
   254 	}
       
   255 
       
   256 void CATSmsMessagingWrite::StartPduWrite()
       
   257 /**
       
   258  * Kicks off the writing of the actual pdu. Begins by converting the pdu
       
   259  * into ascii form, and writing the pdu length and message status to the phone.
       
   260  */
       
   261 	{
       
   262 	iMsgDataAscii.Zero();
       
   263 
       
   264 	// Convert PDU from binary to ASCII
       
   265 	CATSmsUtils::AppendDataToAscii(iMsgDataAscii,iSmsEntry->iMsgData);
       
   266 	if(iMsgDataAscii.Length()==0)
       
   267 		{
       
   268 		LOGTEXT(_L8("CATSmsMessagingWrite:\tStartPduWrite - Failed to convert binary PDU to ASCII"));
       
   269 		Complete(KErrCorrupt);
       
   270 		return;
       
   271 		}
       
   272 
       
   273 	// Send PDU length to the phone
       
   274 	const TInt pduLengthSemiOctets(iMsgDataAscii.Length());
       
   275 	const TInt pduLengthOctets(pduLengthSemiOctets/2+pduLengthSemiOctets%2);
       
   276 
       
   277 	if (iNextAttempt==CPhoneGlobals::EPhoneTestNewStandard)
       
   278  		{
       
   279 		// Prepend a zero length SCA to PDU - this forces phone to use default SCA
       
   280 		// See GSM 07.05 for details
       
   281 		_LIT8(zeroLengthSca, "00");
       
   282 		iMsgDataAscii.Insert(0,zeroLengthSca);
       
   283 		}
       
   284 	
       
   285 	iTxBuffer.Zero();
       
   286 	TInt stat=SetMsgStatus();
       
   287 	if (stat==KErrNotSupported)
       
   288 		iTxBuffer.Format(KSmsWriteLengthCommand,pduLengthOctets);
       
   289 	else
       
   290 		iTxBuffer.Format(KSmsWriteCommand,pduLengthOctets,stat);
       
   291 	iIo->Write(this,iTxBuffer);
       
   292 	iIo->SetTimeOut(this,5000);
       
   293 	iState=EATWaitForSendingPduLengthComplete;
       
   294 	}
       
   295 
       
   296 void CATSmsMessagingWrite::Complete(TInt aError)
       
   297 	{
       
   298 	Complete(aError, EReadCompletion);
       
   299 	}
       
   300 
       
   301 void CATSmsMessagingWrite::Complete(TInt aError, TEventSource aSource)
       
   302 	{
       
   303 	iIo->WriteAndTimerCancel(this);
       
   304 	iIo->RemoveExpectStrings(this);
       
   305 	iOKExpectString = NULL;
       
   306 	iErrorExpectString = NULL;
       
   307 	iExpectString = NULL;
       
   308 	iPhoneGlobals->iEventSignalActive = EFalse; 
       
   309 	if (iReqHandle != 0)
       
   310 		iTelObject->ReqCompleted(iReqHandle, aError);
       
   311 	if (aSource == EWriteCompletion)
       
   312 		iIo->Read();
       
   313 
       
   314 	//
       
   315 	// Call our base classes Complete so that
       
   316 	// we allow it do what ever it needs to do.
       
   317 	CATCommands::Complete(aError,aSource);
       
   318 
       
   319 	iState = EATNotInProgress;
       
   320 	}
       
   321 
       
   322 void CATSmsMessagingWrite::ParseResponseL()
       
   323 //
       
   324 // Parse the message reference number and return in smsEntry.iIndex
       
   325 //
       
   326 	{
       
   327 	LOGTEXT(_L8("CATSmsMessagingWrite:\tRetrieving message reference number"));
       
   328 	iBuffer.Set(iIo->Buffer());
       
   329 	TInt pos = iBuffer.FindF(KCMGWResponseString);
       
   330 	if (pos == KErrNotFound)
       
   331 		{
       
   332 		LOGTEXT(_L8("CATSmsMessagingWrite:\tError - Cannot find '+CMGW:' string"));
       
   333 		pos = iBuffer.FindF(KErrorString);
       
   334 		if (pos != KErrNotFound)
       
   335 			User::Leave(KErrGeneral);
       
   336 		User::Leave(pos);
       
   337 		}
       
   338 	pos += 6;
       
   339 	
       
   340 	// Place the message reference number into buffer (ie: everything after +CMGW: string)
       
   341 	TInt woop = iBuffer.Length()-pos;
       
   342 	iBuffer.Set(iBuffer.Right(woop));
       
   343 	
       
   344 	TLex8 yyLex(iBuffer);
       
   345 	yyLex.Inc();			// steps over the ':' Should always do this....NEVER falls over!
       
   346 	yyLex.SkipSpace();
       
   347 	TUint val;
       
   348 	if(yyLex.Val(val)==KErrNone) // Grab the message reference number.
       
   349 		{
       
   350 		iSmsEntry->iIndex = val;
       
   351 		}
       
   352 	else
       
   353 		{
       
   354 		LOGTEXT(_L8("CATSmsMessagingWrite:\tError. Invalid Message Reference Number."));
       
   355 		User::Leave(KErrGeneral);
       
   356 		}
       
   357 	}
       
   358 
       
   359 void CATSmsMessagingWrite::Stop(TTsyReqHandle aTsyReqHandle)
       
   360 //
       
   361 //	Attempts to halt the process
       
   362 //
       
   363 	{
       
   364 	__ASSERT_ALWAYS(aTsyReqHandle == iReqHandle,Panic(EIllegalTsyReqHandle));		
       
   365 	LOGTEXT(_L8("CATSmsMessagingWrite:\tCancelling Sms Write"));
       
   366 
       
   367 	if ((iState==EATWaitForSendingPduLengthComplete) ||
       
   368 		(iState==EATReadPduEnterPduModeResponseCompleted) ||
       
   369 		(iState==EATWaitForSendingPduComplete) ||
       
   370 		(iState==EATReadSmsRefNumCompleted))
       
   371 		{
       
   372 		// Attempt to cancel write command if we can
       
   373 		RemoveStdExpectStrings();
       
   374 		iIo->WriteAndTimerCancel(this);
       
   375 		iTxBuffer.Format(KEscapeChar);
       
   376 		iIo->Write(this,iTxBuffer);
       
   377 		iIo->SetTimeOut(this);
       
   378 		iState = EATCancellingWaitForWriteComplete;
       
   379 		}
       
   380 	else
       
   381 		// Wait until safe point to cancel the write
       
   382 		iCancelled=ETrue;
       
   383 	}
       
   384 
       
   385 void CATSmsMessagingWrite::CompleteWithIOError(TEventSource /*aSource*/,TInt aStatus)
       
   386 	{
       
   387 	if (iState!=EATNotInProgress)
       
   388 		{
       
   389 		iIo->WriteAndTimerCancel(this);
       
   390 		iTelObject->ReqCompleted(iReqHandle, aStatus);
       
   391 		iState = EATNotInProgress;
       
   392 		}
       
   393 	}
       
   394 
       
   395 TInt CATSmsMessagingWrite::SetMsgStatus()
       
   396 //
       
   397 // Sets the message status
       
   398 //
       
   399 	{
       
   400 	TInt status;
       
   401 	switch(iSmsEntry->iMsgStatus)
       
   402 		{
       
   403 		case (RMobileSmsStore::EStoredMessageUnread): 
       
   404 			status = 0;
       
   405 		break;
       
   406 		
       
   407 		case (RMobileSmsStore::EStoredMessageRead):
       
   408 			status = 1;
       
   409 		break;
       
   410 
       
   411 		case (RMobileSmsStore::EStoredMessageUnsent):
       
   412 			status = 2;
       
   413 		break;
       
   414 
       
   415 		case (RMobileSmsStore::EStoredMessageSent):  
       
   416 		case (RMobileSmsStore::EStoredMessageDelivered):   
       
   417 			status = 3;
       
   418 		break;
       
   419 
       
   420 		default:
       
   421 			status = KErrNotSupported;
       
   422 		break;
       
   423 		}
       
   424 	return status;
       
   425 	}
       
   426