syncmlfw/common/syncagent/src/NSmlAgentBase.cpp
changeset 0 b497e44ab2fc
child 1 a9c0ce913924
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "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 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Base class in Commmon Agent
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    21 #include <es_enum_partner.h>
       
    22 #endif
       
    23 #include <imcvcodc.h>
       
    24 #include <hash.h>
       
    25 #include <SyncMLHistory.h>
       
    26 #include <nsmlconstants.h>
       
    27 #include <nsmldebug.h>
       
    28 #include <nsmldefines.h>
       
    29 #include <nsmlphoneinfo.h>
       
    30 #include <nsmlunicodeconverter.h>
       
    31 #include <centralrepository.h>
       
    32 #include <DevManInternalCRKeys.h>
       
    33 #include <e32property.h>
       
    34 #include <thttpevent.h>
       
    35 #include "nsmlprivatepskeys.h"
       
    36 #include "nsmlcliagconstants.h"
       
    37 #include "NSmlAgentBase.h"
       
    38 #include "NSmlCmdsBase.h"
       
    39 #include "NSmlStatusContainer.h"
       
    40 #include "NSmlResponseController.h"
       
    41 #include "nsmlerror.h"
       
    42 #include "NSmlURI.h"
       
    43 #include "nsmltransport.h"
       
    44 #include "nsmlagenttestdefines.h"
       
    45 #ifdef __NOTRANSPORT
       
    46 #include "wbxml2xmlconverter.h"
       
    47 #endif
       
    48 //Rnd_AutoRestart
       
    49 #include <es_sock.h> // RConnection RSocket
       
    50 #include <es_enum.h> // TConnectionInfo
       
    51 #include <commdb.h>
       
    52 #include <ApListItemList.h>
       
    53 #include <ApListItem.h>
       
    54 #include <ApSelect.h>
       
    55 #include <ApUtils.h>
       
    56 #include "nsmlhttp.h"
       
    57 
       
    58 //Fix to Remove the Bad Compiler Warnings
       
    59 #ifndef __WINS__
       
    60 // This lowers the unnecessary compiler warning (armv5) to remark.
       
    61 // "Warning:  #174-D: expression has no effect..." is caused by 
       
    62 // DBG_ARGS8 macro in no-debug builds.
       
    63 #pragma diag_remark 174
       
    64 #endif
       
    65 
       
    66 const TUid KCRUidDataSyncInternalKeys  = { 0x2000CF7E };
       
    67 const TInt KNsmlDsAutoRestart = 7; //(0x00000007)
       
    68 //Rnd_AutoRestart
       
    69 
       
    70 // CONSTANTS
       
    71 _LIT( KPanicInvalidState, "Invalid state" );
       
    72 
       
    73 // ================= MEMBER FUNCTIONS =======================
       
    74 // ---------------------------------------------------------
       
    75 // CNSmlAgentBase::CNSmlAgentBase()
       
    76 // C++ constructor
       
    77 // ---------------------------------------------------------
       
    78 //
       
    79 EXPORT_C CNSmlAgentBase::CNSmlAgentBase() : CActive(EPriorityLow), iBufferArea(0, 0)
       
    80     {
       
    81 	}
       
    82 
       
    83 // ---------------------------------------------------------
       
    84 // CNSmlAgentBase::~CNSmlAgentBase()
       
    85 // Destructor
       
    86 // ---------------------------------------------------------
       
    87 //
       
    88 EXPORT_C CNSmlAgentBase::~CNSmlAgentBase()
       
    89     {
       
    90 	Cancel();
       
    91 	delete iImeiCode;
       
    92 	delete iSyncMLUserName;
       
    93 	delete iSyncMLPassword;
       
    94 	delete iSyncServer;
       
    95 	delete iSyncMLCmds;
       
    96 	delete iSyncMLMIMEType;
       
    97 	delete iResultAlertCode;
       
    98 	delete iEndOfDataAlertCode;
       
    99 	delete iSessionAbortAlertCode;
       
   100 	delete iNextMessageCode;
       
   101 	delete iTransport;		
       
   102 	delete iError;
       
   103     delete iIAPIdArray;
       
   104 	delete iSyncHTTPAuthUserName;
       
   105 	delete iSyncHTTPAuthPassword;
       
   106 	}
       
   107 
       
   108 // ---------------------------------------------------------
       
   109 // CNSmlAgentBase::AlreadyAuthenticated()
       
   110 // 
       
   111 // ---------------------------------------------------------
       
   112 TBool CNSmlAgentBase::AlreadyAuthenticated() const
       
   113 	{
       
   114 	return iAlreadyAuthenticated;
       
   115 	}
       
   116 	
       
   117 // ---------------------------------------------------------
       
   118 // CNSmlAgentBase::BasicCredentialL
       
   119 // Creates B64 string form SyncML userID and password 
       
   120 // ---------------------------------------------------------
       
   121 HBufC8* CNSmlAgentBase::BasicCredentialL() const
       
   122 	{
       
   123 	TImCodecB64 B64Coder;
       
   124 	HBufC8* userName;
       
   125 	NSmlUnicodeConverter::HBufC8InUTF8LC( *iSyncMLUserName, userName );
       
   126 	HBufC8* password;
       
   127 	NSmlUnicodeConverter::HBufC8InUTF8LC( *iSyncMLPassword, password );
       
   128 	_LIT8( KColon, ":" );
       
   129 	HBufC8* clearText = HBufC8::NewLC( userName->Length() + KColon.iTypeLength + password->Length());
       
   130 	TPtr8 clearTextPtr = clearText->Des();
       
   131 	clearTextPtr = *userName;
       
   132 	clearTextPtr += KColon;
       
   133 	clearTextPtr += *password;
       
   134 	HBufC8* B64String = HBufC8::NewLC( clearText->Length() * 2 );
       
   135 	TPtr8 B64StringPtr( B64String->Des() );
       
   136 	User::LeaveIfError( B64Coder.Encode( *clearText, B64StringPtr ) );
       
   137 	CleanupStack::Pop(); // B64String
       
   138 	CleanupStack::PopAndDestroy( 3 ); //clearText, password, userName
       
   139 	return B64String;
       
   140 	}
       
   141 	
       
   142 // ---------------------------------------------------------
       
   143 // CNSmlAgentBase::Md5CredentialL
       
   144 // Creates MD5 string form SyncML userID, password and nonce
       
   145 // ---------------------------------------------------------
       
   146 HBufC8* CNSmlAgentBase::Md5CredentialL( TBool aServerAuth ) const
       
   147 	{
       
   148 
       
   149 	CMD5* md5 = CMD5::NewL();
       
   150 	CleanupStack::PushL( md5 );
       
   151 	TImCodecB64 B64Coder;
       
   152 	HBufC8* userName;
       
   153 	if ( aServerAuth )
       
   154 		{
       
   155 		NSmlUnicodeConverter::HBufC8InUTF8LC( ServerUserName(), userName );
       
   156 		}
       
   157 	else
       
   158 		{
       
   159 		NSmlUnicodeConverter::HBufC8InUTF8LC( *iSyncMLUserName, userName );
       
   160 		}
       
   161 	HBufC8* password;
       
   162 	if ( aServerAuth )
       
   163 		{
       
   164 		NSmlUnicodeConverter::HBufC8InUTF8LC( ServerPassword(), password );
       
   165 		}
       
   166 	else
       
   167 		{
       
   168 		NSmlUnicodeConverter::HBufC8InUTF8LC( *iSyncMLPassword, password );
       
   169 		}
       
   170 	HBufC* nonceInUnicode;
       
   171 	if ( aServerAuth )
       
   172 		{
       
   173 		nonceInUnicode = ServerNonceAllocL();
       
   174 		}
       
   175 	else
       
   176 		{
       
   177 		nonceInUnicode = NonceL();
       
   178 		}
       
   179 	CleanupStack::PushL( nonceInUnicode );
       
   180 	HBufC8* nonce = HBufC8::NewLC( nonceInUnicode->Length() );
       
   181 	nonce->Des().Copy( *nonceInUnicode );
       
   182 	_LIT8( KColon, ":" );
       
   183 		
       
   184 	HBufC8* userNamePassword = HBufC8::NewLC( userName->Length() + KColon.iTypeLength + password->Length() );
       
   185 	TPtr8 userNamePasswordPtr = userNamePassword->Des();
       
   186 	userNamePasswordPtr = *userName;
       
   187 	userNamePasswordPtr += KColon;
       
   188 	userNamePasswordPtr += *password;
       
   189 	TPtrC8 userNamePasswordHash;
       
   190 	userNamePasswordHash.Set( md5->Hash( *userNamePassword ) );
       
   191 	HBufC8* B64UserNamePasswordString = HBufC8::NewLC( userNamePasswordHash.Length() * 2 );
       
   192 	TPtr8 B64UserNamePasswordStringPtr( B64UserNamePasswordString->Des() );
       
   193 	User::LeaveIfError( B64Coder.Encode( userNamePasswordHash, B64UserNamePasswordStringPtr ) );
       
   194 	HBufC8* userNamePasswordNonce = HBufC8::NewLC( B64UserNamePasswordStringPtr.Length() + KColon.iTypeLength + nonce->Length() );
       
   195 	TPtr8 userNamePasswordNoncePtr = userNamePasswordNonce->Des();
       
   196 	userNamePasswordNoncePtr = B64UserNamePasswordStringPtr;
       
   197 	userNamePasswordNoncePtr += KColon;
       
   198 	userNamePasswordNoncePtr += *nonce;
       
   199 		
       
   200 	TPtrC8 finalHash;
       
   201 	md5->Reset();
       
   202 	finalHash.Set( md5->Hash( *userNamePasswordNonce ) );
       
   203 	HBufC8* B64String = HBufC8::NewLC( finalHash.Length() * 2 );
       
   204 	TPtr8 B64StringPtr( B64String->Des() );
       
   205 	User::LeaveIfError( B64Coder.Encode( finalHash, B64StringPtr ) );
       
   206 	CleanupStack::Pop();   // B64String
       
   207 	CleanupStack::PopAndDestroy( 8 ); //userNamePasswordNonce, B64userNamePasswordNonce, userNamePassword, nonce, nonceInUnicode, password, userName, md5
       
   208 	return  B64String;
       
   209 	}
       
   210 
       
   211 // ---------------------------------------------------------
       
   212 // CNSmlAgentBase::ServerNonceAllocL()
       
   213 // 
       
   214 // ---------------------------------------------------------
       
   215 EXPORT_C HBufC* CNSmlAgentBase::ServerNonceAllocL() const
       
   216 	{
       
   217 	return HBufC::NewL( 0 );
       
   218 	}
       
   219 // ---------------------------------------------------------
       
   220 // CNSmlAgentBase::SetServerNonceL()
       
   221 // 
       
   222 // ---------------------------------------------------------
       
   223 EXPORT_C void CNSmlAgentBase::SetServerNonceL( const TDesC& /*aNonce*/ ) const
       
   224 	{
       
   225 	}
       
   226 // ---------------------------------------------------------
       
   227 // CNSmlAgentBase::SetEndOfDataAlertRequest()
       
   228 // 
       
   229 // ---------------------------------------------------------
       
   230 EXPORT_C void CNSmlAgentBase::SetEndOfDataAlertRequest()
       
   231 	{
       
   232 	iEndOfDataAlertRequest = ETrue;
       
   233 	}
       
   234 	
       
   235 // ---------------------------------------------------------
       
   236 // CNSmlAgentBase::SyncLog()
       
   237 // Returns pointer to the syncml history log.
       
   238 // ---------------------------------------------------------
       
   239 EXPORT_C CSyncMLHistoryJob* CNSmlAgentBase::SyncLog()
       
   240 	{
       
   241 	return iSyncLog;
       
   242 	}
       
   243 
       
   244 // ---------------------------------------------------------
       
   245 // CNSmlAgentBase::CallbackCalled()
       
   246 // 
       
   247 // ---------------------------------------------------------
       
   248 EXPORT_C TBool CNSmlAgentBase::CallbackCalled() const
       
   249 	{
       
   250 	return iCallbackCalled;
       
   251 	}
       
   252 
       
   253 // ---------------------------------------------------------
       
   254 // CNSmlAgentBase::SetOffCallbackCalled()
       
   255 // 
       
   256 // ---------------------------------------------------------
       
   257 EXPORT_C void CNSmlAgentBase::SetOffCallbackCalled()
       
   258 	{
       
   259 	iCallbackCalled = EFalse;
       
   260 	}
       
   261 
       
   262 // ---------------------------------------------------------
       
   263 // CNSmlAgentBase::RunL()
       
   264 // 
       
   265 // ---------------------------------------------------------
       
   266 EXPORT_C void CNSmlAgentBase::RunL()
       
   267     {
       
   268 	TRAPD( leaveCode, NavigateMainStateL() );
       
   269 	if ( leaveCode != KErrNone )
       
   270 		{
       
   271 		ErrorHandling( leaveCode );
       
   272 		}
       
   273 		
       
   274 	if ( iCancelForced )
       
   275 		{
       
   276 		ErrorHandling( KErrCancel );
       
   277 		}
       
   278 		
       
   279 	if ( !iEnd )
       
   280 		{
       
   281 		SetActive();
       
   282 		#ifndef __NOTRANSPORT
       
   283 		if ( !iCommandIssued ) //When asychronous transport service is not called
       
   284 							   //thread is signalled immediately 	
       
   285 		#endif
       
   286 			{
       
   287 			TRequestStatus* status = &iStatus;
       
   288 			User::RequestComplete( status, KErrNone );
       
   289 			}
       
   290 		}
       
   291 	
       
   292 	if ( iEnd )
       
   293 		{
       
   294 		FreeResources();	
       
   295 		}
       
   296 	
       
   297 	}
       
   298 
       
   299 
       
   300 // ---------------------------------------------------------
       
   301 // CNSmlAgentBase::DoOutsideCancel()
       
   302 // 
       
   303 // ---------------------------------------------------------
       
   304 EXPORT_C void CNSmlAgentBase::DoOutsideCancel() 
       
   305 	{ 
       
   306 	iCancelForced = ETrue;
       
   307 	}
       
   308 	
       
   309 // ---------------------------------------------------------
       
   310 // CNSmlAgentBase::DoCancel()
       
   311 // 
       
   312 // ---------------------------------------------------------
       
   313 EXPORT_C void CNSmlAgentBase::DoCancel()
       
   314     {
       
   315 	#ifndef __NOTRANSPORT
       
   316 	iTransport->Disconnect(); 
       
   317 	#endif
       
   318 	ErrorHandling( KErrCancel );
       
   319 	return;
       
   320     }
       
   321 // ---------------------------------------------------------
       
   322 // CNSmlAgentBase::smlAddCmdFunc
       
   323 // Add command from server
       
   324 // ---------------------------------------------------------
       
   325 EXPORT_C Ret_t CNSmlAgentBase::smlAddCmdFuncL( SmlAddPtr_t aContent )
       
   326 	{
       
   327 	iCallbackCalled = ETrue;
       
   328 	switch ( iCurrMainState )
       
   329 		{
       
   330 		case EServerModifications:
       
   331 			ServerModUpdatesStateL( KNSmlAgentAdd(), aContent );
       
   332 			break;
       
   333 		default:
       
   334 			iSyncMLCmds->StatusToUnsupportedCommandL( ETrue, KNSmlAgentAdd, aContent->cmdID, aContent->flags );
       
   335 		}
       
   336 	return 0;
       
   337 	}
       
   338 // ---------------------------------------------------------
       
   339 // CNSmlAgentBase::smlCopyCmdFunc
       
   340 // Copy command from server, Copy is not supported
       
   341 // ---------------------------------------------------------
       
   342 EXPORT_C Ret_t CNSmlAgentBase::smlCopyCmdFuncL( SmlCopyPtr_t aContent )
       
   343 	{
       
   344 	iCallbackCalled = ETrue;
       
   345 	iSyncMLCmds->StatusToUnsupportedCommandL( EFalse, KNSmlAgentCopy, aContent->cmdID, aContent->flags );
       
   346 	return 0;
       
   347 	}
       
   348 // ---------------------------------------------------------
       
   349 // CNSmlAgentBaset::smlDeleteCmdFunc
       
   350 // Delete command from server
       
   351 // ---------------------------------------------------------
       
   352 EXPORT_C Ret_t CNSmlAgentBase::smlDeleteCmdFuncL( SmlDeletePtr_t aContent )
       
   353 	{
       
   354 	iCallbackCalled = ETrue;
       
   355 	switch ( iCurrMainState )
       
   356 		{
       
   357 		case EServerModifications:
       
   358 			ServerModUpdatesStateL( KNSmlAgentDelete(), aContent );
       
   359 			break;
       
   360 		default:
       
   361 			iSyncMLCmds->StatusToUnsupportedCommandL( ETrue, KNSmlAgentDelete, aContent->cmdID, aContent->flags );
       
   362 		}
       
   363 	return 0;
       
   364 	}
       
   365 
       
   366 // ---------------------------------------------------------
       
   367 // CNSmlAgentBase::smlExecCmdFunc
       
   368 // Exec command from server
       
   369 // ---------------------------------------------------------
       
   370 EXPORT_C Ret_t CNSmlAgentBase::smlExecCmdFuncL( SmlExecPtr_t aContent )
       
   371 	{
       
   372 	iCallbackCalled = ETrue;
       
   373 	iSyncMLCmds->StatusToUnsupportedCommandL( EFalse, KNSmlAgentExec, aContent->cmdID, aContent->flags );
       
   374 	return 0;
       
   375 	}
       
   376 
       
   377 // ---------------------------------------------------------
       
   378 // CNSmlAgentBase::smlMapCmdFunc
       
   379 // Map command from server, Map is not supported
       
   380 // ---------------------------------------------------------
       
   381 EXPORT_C Ret_t CNSmlAgentBase::smlMapCmdFuncL( SmlMapPtr_t aContent )
       
   382 	{
       
   383 	iCallbackCalled = ETrue;
       
   384 	iSyncMLCmds->StatusToUnsupportedCommandL( ETrue, KNSmlAgentMap, aContent->cmdID );
       
   385 	return 0;
       
   386 	}
       
   387 // ---------------------------------------------------------
       
   388 // CNSmlAgentBase::smlReplaceCmdFunc
       
   389 // Replace command from server
       
   390 // ---------------------------------------------------------
       
   391 EXPORT_C Ret_t CNSmlAgentBase::smlReplaceCmdFuncL( SmlReplacePtr_t aContent )
       
   392 	{
       
   393 	iCallbackCalled = ETrue;
       
   394 	switch ( iCurrMainState )
       
   395 		{
       
   396 		case EServerModifications:
       
   397 			ServerModUpdatesStateL( KNSmlAgentReplace(), aContent );
       
   398 			break;
       
   399 		default:
       
   400 			iSyncMLCmds->StatusToUnsupportedCommandL( ETrue, KNSmlAgentReplace, aContent->cmdID, aContent->flags );
       
   401 		}
       
   402 	return 0;
       
   403 	}
       
   404 // ---------------------------------------------------------
       
   405 // CNSmlAgentBase::smlSearchCmdFunc
       
   406 // Search command from server
       
   407 // ---------------------------------------------------------
       
   408 EXPORT_C Ret_t CNSmlAgentBase::smlSearchCmdFuncL( SmlSearchPtr_t aContent )
       
   409 	{
       
   410 	iCallbackCalled = ETrue;
       
   411 	iSyncMLCmds->StatusToUnsupportedCommandL( EFalse, KNSmlAgentSearch, aContent->cmdID, aContent->flags );
       
   412 	return 0;
       
   413 	}
       
   414 
       
   415 // ---------------------------------------------------------
       
   416 // CNSmlAgentBase::smlMoveCmdFunc
       
   417 // Copy command from server, Copy is not supported
       
   418 // ---------------------------------------------------------
       
   419 //1.2 CHANGES
       
   420 EXPORT_C Ret_t CNSmlAgentBase::smlMoveCmdFuncL( SmlMovePtr_t aContent )
       
   421 	{
       
   422 	iCallbackCalled = ETrue;
       
   423 	iSyncMLCmds->StatusToUnsupportedCommandL( EFalse, KNSmlAgentMove, aContent->cmdID, aContent->flags );
       
   424 	return 0;
       
   425 	}
       
   426 //end chnages
       
   427 
       
   428 
       
   429 //
       
   430 //  Protected member functions
       
   431 //
       
   432 // ---------------------------------------------------------
       
   433 // CNSmlAgentBase::ConstructL()
       
   434 // 
       
   435 // ---------------------------------------------------------
       
   436 //
       
   437 EXPORT_C void CNSmlAgentBase::ConstructL()
       
   438     {
       
   439     iSendRetries = 3;
       
   440 	iCurrMainState = EBeginning;
       
   441 	#ifndef __NOTRANSPORT
       
   442 	iTransport = CNSmlTransport::NewL();	
       
   443 	#endif
       
   444 	CNSmlPhoneInfo* phoneInfo = CNSmlPhoneInfo::NewLC();
       
   445 	iImeiCode = HBufC::NewL( 50 );
       
   446 	TPtr imeiCodePtr = iImeiCode->Des();  
       
   447 	phoneInfo->PhoneDataL( CNSmlPhoneInfo::EPhoneSerialNumber, imeiCodePtr );
       
   448 	CleanupStack::PopAndDestroy();  //phoneInfo
       
   449 	InitialiseSubStates();
       
   450 	//Auto_Restart
       
   451     iPacketDataUnAvailable = EFalse;    
       
   452     iNetmonAPId = 0;
       
   453     iNetmonAPBearerType = TApBearerType(-1);
       
   454 	}
       
   455 
       
   456 
       
   457 //
       
   458 // Beginning state functions 
       
   459 //
       
   460 // ---------------------------------------------------------
       
   461 // CNSmlAgentBase::BeginningStateL()
       
   462 // This state is performed in the beginning of sync.
       
   463 // Navigate according to sub state code
       
   464 // ---------------------------------------------------------
       
   465 //
       
   466 
       
   467 EXPORT_C void CNSmlAgentBase::BeginningStateL() 
       
   468 	{
       
   469 	switch ( iCurrBeginningState )
       
   470 		{
       
   471 		case EBegin:
       
   472 			BeginStateL();
       
   473 			break;
       
   474 		case EBeginConnectingToServer:
       
   475 			BeginConnectingStateL();
       
   476 			break;
       
   477 		default:
       
   478 			User::Panic( KPanicInvalidState, iCurrBeginningState );
       
   479 		}
       
   480 	}
       
   481 
       
   482 
       
   483 
       
   484 //
       
   485 //  Client Initialisation state functions 
       
   486 //
       
   487 // ---------------------------------------------------------
       
   488 // CNSmlAgentBase::ClientInitialisationStateL()
       
   489 // Makes Sync Initialization Package from Client 
       
   490 // Navigate according to sub state code.
       
   491 // ---------------------------------------------------------
       
   492 EXPORT_C void CNSmlAgentBase::ClientInitialisationStateL()
       
   493 	{
       
   494 	switch ( iCurrClientInitState )
       
   495 		{
       
   496 		case EInitStartingMessage:
       
   497 			InitStartingMessageStateL();
       
   498 			break;
       
   499 		case EInitMakingStatus:
       
   500 			InitMakingStatusStateL();
       
   501 			break;
       
   502 		case EInitAlerting:
       
   503 			InitAlertingStateL();
       
   504 			break;
       
   505 		case EInitDeviceInfo:
       
   506 			InitDeviceInfoStateL();
       
   507 			break;
       
   508 		case EInitAskingDeviceInfo:
       
   509 			InitAskingDeviceInfoStateL();
       
   510 			break;
       
   511 		case EInitEndingMessage:
       
   512 			InitEndingMessageStateL();
       
   513 			break;
       
   514 		default:
       
   515 			User::Panic( KPanicInvalidState, iCurrClientInitState );
       
   516 		}
       
   517 	}
       
   518 // ---------------------------------------------------------
       
   519 // CNSmlAgentBase::InitStartingMessageBaseStateL()
       
   520 // "Initialising" event to UI,  makes SyncHdr  
       
   521 // ---------------------------------------------------------
       
   522 EXPORT_C void CNSmlAgentBase::InitStartingMessageStateBaseL()
       
   523 	{
       
   524 	iChallengeRequest = EFalse;
       
   525 	iSyncMLCmds->DoSyncHdrL();
       
   526 	iCurrClientInitState = EInitMakingStatus;
       
   527 	}
       
   528 
       
   529 
       
   530 // ---------------------------------------------------------
       
   531 // CNSmlAgentBase::InitAskingDeviceInfoStateL()
       
   532 // There is only default implementation in the base class, which 
       
   533 // does not anything but set the next sub state.
       
   534 // ---------------------------------------------------------
       
   535 EXPORT_C void CNSmlAgentBase::InitAskingDeviceInfoStateL()
       
   536 	{
       
   537 	iCurrClientInitState = EInitEndingMessage;
       
   538 	}
       
   539 
       
   540 //
       
   541 //  Server Modifications state functions 
       
   542 //
       
   543 // ---------------------------------------------------------
       
   544 // CNSmlAgentBase::ServerModStartMessageStateL(()
       
   545 // Handles SyncHdr in Server Modifications to Client Package
       
   546 // ---------------------------------------------------------
       
   547 EXPORT_C void CNSmlAgentBase::ServerModStartMessageStateL( SmlSyncHdr_t* aSyncHdr )
       
   548 	{
       
   549 	DBG_FILE(_S8("CNSmlAgentBase::ServerModStartMessageStateL begins"));
       
   550 	if ( iCurrServerModState != EServerModWaitingStartMessage )
       
   551 		{
       
   552 		User::Leave( TNSmlError::ESmlUnexpectedCmd ); 
       
   553 		}
       
   554 	iSyncMLCmds->ProcessSyncHdrL( aSyncHdr );
       
   555 	iCurrServerModState = EServerModWaitingCommands;
       
   556 	DBG_FILE(_S8("CNSmlAgentBase::ServerModStartMessageStateL ends"));
       
   557 	}
       
   558 
       
   559 // ---------------------------------------------------------
       
   560 // CNSmlAgentBase::ServerModStatusCmdStateL()
       
   561 // Handles Status command in Server Modifications to Client Package
       
   562 // ---------------------------------------------------------
       
   563 EXPORT_C void CNSmlAgentBase::ServerModStatusCmdStateL( SmlStatus_t* aStatus )
       
   564 	{
       
   565 	DBG_FILE(_S8("CNSmlAgentBase::ServerModStatusCmdStateL begins"));
       
   566 	if ( iChallengeRequest )
       
   567 		{
       
   568 		return;
       
   569 		}
       
   570 	if ( iCurrServerModState != EServerModWaitingCommands ) 
       
   571 		{
       
   572 		User::Leave( TNSmlError::ESmlUnexpectedCmd ); 
       
   573 		}
       
   574 	iSyncMLCmds->ProcessStatusCmdL ( aStatus );
       
   575 	DBG_FILE(_S8("CNSmlAgentBase::ServerModStatusCmdStateL ends"));
       
   576 	}
       
   577 // ---------------------------------------------------------
       
   578 // CNSmlAgentBase::ServerModAlertCmdStateL()
       
   579 // Handles Alert command in Server Modifications to Client Package
       
   580 // ---------------------------------------------------------
       
   581 EXPORT_C void CNSmlAgentBase::ServerModAlertCmdStateL( SmlAlert_t* aAlert )
       
   582 	{
       
   583 	DBG_FILE(_S8("CNSmlAgentBase::ServerModAlertCmdStateL begins"));
       
   584 
       
   585 	//Moved from CNSmlDSAgent::smlAlertCmdFuncL to here
       
   586 	if ( !iFinalMessageFromClient )
       
   587 		{
       
   588 		return;
       
   589 		}
       
   590 
       
   591 	if ( iCurrServerModState != EServerModWaitingCommands ) 
       
   592 		{
       
   593 		User::Leave( TNSmlError::ESmlUnexpectedCmd ); 
       
   594 		}
       
   595 
       
   596 	if ( iSyncMLCmds->AlertCode( aAlert ) == KNSmlAgentDisplayAlert )
       
   597 		{
       
   598 		iSyncMLCmds->ProcessAlertCmdL( aAlert, EFalse, EFalse, ETrue );
       
   599 		}
       
   600 	else
       
   601 		{
       
   602 		iSyncMLCmds->ProcessAlertCmdL( aAlert, ETrue );
       
   603 		}
       
   604 
       
   605 	DBG_FILE(_S8("CNSmlAgentBase::ServerModAlertCmdStateL ends"));
       
   606 	}
       
   607 
       
   608 // ---------------------------------------------------------
       
   609 // CNSmlAgentBase::ServerModEndMessageStateL()
       
   610 // Handles end of SyncML message, amount of received items is 
       
   611 // notified to UI
       
   612 // ---------------------------------------------------------
       
   613 EXPORT_C void CNSmlAgentBase::ServerModEndMessageStateL( TBool aFinal )
       
   614 	{
       
   615 	DBG_FILE(_S8("CNSmlAgentBase::ServerModEndMessageStateL begins"));
       
   616 	if ( iCurrServerModState != EServerModWaitingCommands ) 
       
   617 		{
       
   618 		User::Leave( TNSmlError::ESmlUnexpectedCmd ); 
       
   619 		}
       
   620 	if ( aFinal )
       
   621 		{
       
   622 		iCurrServerModState = EServerModPackageReceived;
       
   623 		if ( iSyncMLCmds->WaitingLargeObjectChunk() )
       
   624 			{
       
   625 			iSyncMLCmds->ResetLargeObjectBuffer();
       
   626 			iEndOfDataAlertRequest = ETrue;
       
   627 			}
       
   628 		}
       
   629 	else
       
   630 		{
       
   631 		iCurrServerModState = EServerModMessageReceived;
       
   632 		}
       
   633 	DBG_FILE(_S8("CNSmlAgentBase::ServerModEndMessageStateL ends"));
       
   634 	}
       
   635 //
       
   636 //  Data Update Status state functions 
       
   637 //
       
   638 // ---------------------------------------------------------
       
   639 // CNSmlAgentBase::DataUpdateStatusStateL()
       
   640 // Makes Data Update Status Package to Server
       
   641 // Navigate according to state code
       
   642 // ---------------------------------------------------------
       
   643 EXPORT_C void CNSmlAgentBase::DataUpdateStatusStateL()
       
   644 	{
       
   645 	switch ( iCurrDataUpdateStatusState )
       
   646 		{
       
   647 		case EStatusStartingMessage:
       
   648 			DataUpdateStatusStartingMessageStateL();
       
   649 			break;
       
   650 		case EStatusMakingAlert:
       
   651 			DataUpdateStatusMakingAlertStateL();
       
   652 			break;
       
   653 		case EStatusMakingStatus:
       
   654 			DataUpdateStatusMakingStatusStateL();
       
   655 			break;
       
   656 		case EStatusMakingMap:
       
   657 			DataUpdateStatusMakingMapStateL();
       
   658 			break;
       
   659 		case EStatusMakingResults:
       
   660 			DataUpdateStatusMakingResultsStateL();
       
   661 			break;
       
   662 		case EStatusEndingMessage:
       
   663 			DataUpdateStatusEndingMessageStateL();
       
   664 			break;
       
   665 		default:
       
   666 			User::Panic( KPanicInvalidState, iCurrDataUpdateStatusState );
       
   667 		}
       
   668 	}
       
   669 // ---------------------------------------------------------
       
   670 // CNSmlAgentBase::DataUpdateStatusStartingMessageStateBaseL()
       
   671 // Notify "Finalising" to UI, makes SyncHdr 
       
   672 // ---------------------------------------------------------
       
   673 EXPORT_C void CNSmlAgentBase::DataUpdateStatusStartingMessageStateBaseL()
       
   674 	{
       
   675 	DBG_FILE(_S8("CNSmlAgentBase::DataUpdateStatusStartingMessageStateBAseL begins"));
       
   676 	iBufferFull = EFalse;
       
   677 	iSyncMLCmds->DoSyncHdrL();
       
   678 	iCurrDataUpdateStatusState = EStatusMakingStatus;
       
   679 	DBG_FILE(_S8("CNSmlAgentBase::DataUpdateStatusStartingMessageStateBaseL ends"));
       
   680 	}
       
   681 
       
   682 
       
   683 
       
   684 
       
   685 // ---------------------------------------------------------
       
   686 // CNSmlAgentBase::DataUpdateStatusMakingMapStateL()
       
   687 // 
       
   688 // ---------------------------------------------------------
       
   689 EXPORT_C void CNSmlAgentBase::DataUpdateStatusMakingMapStateL()
       
   690 	{
       
   691 	DBG_FILE(_S8("CNSmlAgentBase::DataUpdateStatusMakingMapStateL begins"));
       
   692 	iCurrDataUpdateStatusState = EStatusMakingResults;
       
   693 	DBG_FILE(_S8("CNSmlAgentBase::DataUpdateStatusMakingMapStateL ends"));
       
   694 	}
       
   695 // ---------------------------------------------------------
       
   696 // CNSmlAgentBase::DataUpdateStatusMakingResultsStateL()
       
   697 // 
       
   698 // ---------------------------------------------------------
       
   699 EXPORT_C void CNSmlAgentBase::DataUpdateStatusMakingResultsStateL()
       
   700 	{
       
   701 	DBG_FILE(_S8("CNSmlAgentBase::DataUpdateStatusMakingResultsStateL begins"));
       
   702 	iCurrDataUpdateStatusState = EStatusEndingMessage;
       
   703 	DBG_FILE(_S8("CNSmlAgentBase::DataUpdateStatusMakingResultsStateL ends"));
       
   704 	}
       
   705 
       
   706 //
       
   707 // Result Alert state functions 
       
   708 //
       
   709 // ---------------------------------------------------------
       
   710 // CNSmlAgentBase::ResultAlertStateL()
       
   711 // Makes Result Alert Package to Server (response to busy 
       
   712 // Status from server)
       
   713 // Navigate according to state code
       
   714 // ---------------------------------------------------------
       
   715 EXPORT_C void CNSmlAgentBase::ResultAlertStateL()
       
   716 	{
       
   717 	switch ( iCurrResultAlertState )
       
   718 		{
       
   719 		case EResultAlertStartingMessage:
       
   720 			ResultAlertStartingMessageStateL();
       
   721 			break;
       
   722 		case EResultAlertMakingAlert:
       
   723 			ResultAlertAlertingStateL();
       
   724 			break;
       
   725 		case EResultAlertEndingMessage:
       
   726 			ResultAlertEndingMessageStateL();
       
   727 			break;
       
   728 		default:
       
   729 			User::Panic( KPanicInvalidState, iCurrResultAlertState );
       
   730 		}
       
   731 	}
       
   732 
       
   733 //
       
   734 // Alert for next state functions 
       
   735 //
       
   736 // ---------------------------------------------------------
       
   737 // CNSmlAgentBase::ClientAlertNextStateL()
       
   738 // Makes Next Alert Package to Server (response to incomplete Package)
       
   739 // Navigate according to state code
       
   740 // ---------------------------------------------------------
       
   741 EXPORT_C void CNSmlAgentBase::ClientAlertNextStateL()
       
   742 	{
       
   743 	DBG_FILE(_S8("CNSmlAgentBase::ClientAlertNextStateL begins"));
       
   744 	switch ( iCurrClientAlertNextState )
       
   745 		{
       
   746 		case EClientAlertNextStartingMessage:
       
   747 			ClientAlertNextStartingMessageStateL();
       
   748 			break;
       
   749 		case EClientAlertNextMakingAlert:
       
   750 			ClientAlertNextAlertingStateL();
       
   751 			break;
       
   752 		case EClientAlertNextMakingStatus:
       
   753 			ClientAlertNextMakingStatusStateL();
       
   754 			break;
       
   755 		case EClientAlertNextMakingMap:
       
   756 			ClientAlertNextMakingMapStateL();
       
   757 			break;
       
   758 		case EClientAlertNextEndingMessage:
       
   759 			ClientAlertNextEndingMessageStateL();
       
   760 			break;
       
   761 		default:
       
   762 			User::Panic( KPanicInvalidState, iCurrClientAlertNextState );
       
   763 		}
       
   764 	DBG_FILE(_S8("CNSmlAgentBase::ClientAlertNextStateL ends"));
       
   765 	}
       
   766 
       
   767 
       
   768 // ---------------------------------------------------------
       
   769 // CNSmlAgentBase::ClientAlertNextMakingMapStateL()
       
   770 // 
       
   771 // ---------------------------------------------------------
       
   772 EXPORT_C void CNSmlAgentBase::ClientAlertNextMakingMapStateL()
       
   773 	{
       
   774 	DBG_FILE(_S8("CNSmlAgentBase::ClientAlertNextMakingMapStateL begins"));
       
   775 	iCurrClientAlertNextState = EClientAlertNextEndingMessage;
       
   776 	DBG_FILE(_S8("CNSmlAgentBase::ClientAlertNextMakingMapStateL ends"));
       
   777 	}
       
   778 
       
   779 //
       
   780 //  Server Alert For Next state functions 
       
   781 //
       
   782 // ---------------------------------------------------------
       
   783 // CNSmlAgentBase::ServerAlertNextStateL()
       
   784 // Main state function to handle Next Alert from Server Package 
       
   785 // ---------------------------------------------------------
       
   786 EXPORT_C void CNSmlAgentBase::ServerAlertNextStateL()
       
   787 	{
       
   788 	if ( !iSyncMLCmds->ProcessReceivedDataL() )
       
   789 		{
       
   790 		if ( iCurrServerAlertNextState == EServerAlertNextMessageReceived )
       
   791 			{
       
   792 			CheckCommandsAreReceivedL();
       
   793 			}
       
   794 		else
       
   795 			{
       
   796 			User::Leave( TNSmlError::ESmlIncompleteMessage );
       
   797 			}
       
   798 		NextMainStateL();
       
   799 		}
       
   800 	}
       
   801 // ---------------------------------------------------------
       
   802 // CNSmlAgentBase::ServerAlertNextStartMessageStateL(()
       
   803 // Handles SyncHdr in Next Alert from Server Package
       
   804 // ---------------------------------------------------------
       
   805 EXPORT_C void CNSmlAgentBase::ServerAlertNextStartMessageStateL( SmlSyncHdr_t* aSyncHdr )
       
   806 	{
       
   807 	DBG_FILE(_S8("CNSmlAgentBase::ServerAlertNextStartMessageStateL begins"));
       
   808 	if ( iCurrServerAlertNextState != EServerAlertNextWaitingStartMessage )
       
   809 		{
       
   810 		User::Leave( TNSmlError::ESmlUnexpectedCmd ); 
       
   811 		}
       
   812 	iSyncMLCmds->ProcessSyncHdrL( aSyncHdr );
       
   813 	iCurrServerAlertNextState = EServerAlertNextWaitingCommands;
       
   814 	DBG_FILE(_S8("CNSmlAgentBase::ServerAlertNextStartMessageStateL ends"));
       
   815 	}
       
   816 // ---------------------------------------------------------
       
   817 // CNSmlAgentBase::ServerAlertNextStatusCmdStateL()
       
   818 // Handles Status command in Next Alert from Server Package
       
   819 // ---------------------------------------------------------
       
   820 EXPORT_C void CNSmlAgentBase::ServerAlertNextStatusCmdStateL( SmlStatus_t* aStatus )
       
   821 	{
       
   822 	DBG_FILE(_S8("CNSmlAgentBase::ServerAlertNextStatusCmdStateL begins"));
       
   823 	if ( iCurrServerAlertNextState != EServerAlertNextWaitingCommands ) 
       
   824 		{
       
   825 		User::Leave( TNSmlError::ESmlUnexpectedCmd ); 
       
   826 		}
       
   827 	iSyncMLCmds->ProcessStatusCmdL ( aStatus );
       
   828 	DBG_FILE(_S8("CNSmlAgentBase::ServerAlertNextStatusCmdStateL ends"));
       
   829 	}
       
   830 // ---------------------------------------------------------
       
   831 // CNSmlAgentBase::ServerAlertNextAlertCmdStateL()
       
   832 // Handles Alert command in Next Alert from Server Package
       
   833 // ---------------------------------------------------------
       
   834 EXPORT_C void CNSmlAgentBase::ServerAlertNextAlertCmdStateL( SmlAlert_t* aAlert )
       
   835 	{
       
   836 	DBG_FILE(_S8("CNSmlAgentBase::ServerAlertNextAlertCmdStateL begins"));
       
   837 	if ( iCurrServerAlertNextState != EServerAlertNextWaitingCommands ) 
       
   838 		{
       
   839 		User::Leave( TNSmlError::ESmlUnexpectedCmd );
       
   840 		}
       
   841 
       
   842 	if ( iSyncMLCmds->AlertCode( aAlert ) == KNSmlAgentDisplayAlert )
       
   843 		{
       
   844 		iSyncMLCmds->ProcessAlertCmdL( aAlert, EFalse, EFalse, ETrue );
       
   845 		}
       
   846 	else
       
   847 		{
       
   848 		iSyncMLCmds->ProcessAlertCmdL( aAlert, ETrue );
       
   849 		}
       
   850 
       
   851 	DBG_FILE(_S8("CNSmlAgentBase::ServerAlertNextAlertCmdStateL ends"));
       
   852 	}
       
   853 // ---------------------------------------------------------
       
   854 // CNSmlAgentBase::ServerAlertNextEndMessageStateL()
       
   855 // Handles end of SyncML message in Next Alert from Server Package
       
   856 // ---------------------------------------------------------
       
   857 EXPORT_C void  CNSmlAgentBase::ServerAlertNextEndMessageStateL( TBool /*aFinal*/ )
       
   858 	{
       
   859 	DBG_FILE(_S8("CNSmlAgentBase::ServerAlertNextEndMessageStateL begins"));
       
   860 	if ( iCurrServerAlertNextState != EServerAlertNextWaitingCommands ) 
       
   861 		{
       
   862 		User::Leave( TNSmlError::ESmlUnexpectedCmd ); 
       
   863 		}
       
   864 	iCurrServerAlertNextState = EServerAlertNextMessageReceived;
       
   865 	DBG_FILE(_S8("CNSmlAgentBase::ServerAlertNextEndMessageStateL ends"));
       
   866 	}
       
   867 
       
   868 //
       
   869 // Sending state functions 
       
   870 //
       
   871 // ---------------------------------------------------------
       
   872 // CNSmlAgentBase::SendingStateL()
       
   873 // Sends SyncML Package to server
       
   874 // Navigate according to state code
       
   875 // ---------------------------------------------------------
       
   876 EXPORT_C void CNSmlAgentBase::SendingStateL()
       
   877 	{
       
   878 	DBG_FILE(_S8("CNSmlAgentBase::SendingStateL begins"));
       
   879 	TInt err= KErrNone;
       
   880 	if ( !iCommandIssued )
       
   881 	{
       
   882 		SendDataIssueL();
       
   883 	}
       
   884 	else
       
   885 	{
       
   886 		TRAP(err,SendDataDoneL());
       
   887 		DBG_FILE_CODE(iStatus.Int(), _S8("CNSmlAgentBase::SendingStateL, ERROR CODE is "));
       
   888 		// IF session timeout feature is ON and then if its DM session then retry 
       
   889 		if(err == KErrNone)
       
   890 		{
       
   891 			iSendRetries = 3; 
       
   892 			NextMainStateL();	
       
   893 		}
       
   894 		else
       
   895 		{
       
   896 		   //Rnd_AutoRestart
       
   897 		  // Check the session type ESyncMLDMSession or ESyncMLDMSession		  
       
   898 		  /*
       
   899 		  	If the session is DM (ESyncMLDMSession) then check for feature sessiontimeout ON.
       
   900 		  	If its ON then if teh failure is -33(timeout) or 9 (http:efailed) then 
       
   901 		  	The state machine has to be reset to Sending, so that the packet is sent again.
       
   902 		  	If any other error leave with the error code.
       
   903 		  	Changes applicable for ***DM ONLY***. 
       
   904 		  	For DS any SendDataDoneL() should leave with error 
       
   905 		  	code for cases other than KErrNone 
       
   906 		  */
       
   907 			TInt Session=0;    
       
   908 	    	TInt r=RProperty::Get( KPSUidNSmlSOSServerKey, KNSmlSyncJobOngoing, Session);                       
       
   909 	    	DBG_FILE_CODE(Session, _S8("CNSmlAgentBase::SendingStateL, Session type is 1 for DS and 2 for DM"));
       
   910 		    if( Session == ESyncMLDMSession ) //DM Session 
       
   911 		    {
       
   912 		        TInt dmsessionTimeout = -1;
       
   913 		        CRepository * rep = 0;
       
   914 				TRAPD( err1, rep = CRepository::NewL( KCRUidDeviceManagementInternalKeys ))
       
   915 				DBG_FILE_CODE(err1, _S8("CNSmlAgentBase::SendingStateL, Crepository reading returned code"));
       
   916 				if(err1 == KErrNone)
       
   917 				{
       
   918 					rep->Get( KDevManDMSessionTimeout, dmsessionTimeout );
       
   919 					delete rep;
       
   920 					DBG_FILE_CODE(dmsessionTimeout, _S8("CNSmlAgentBase::SendingStateL, DMSessiontimeout feature value from cenrep"));
       
   921 					if( dmsessionTimeout > KNSmlDMMaxSessionTimeout || dmsessionTimeout < KNSmlDMMinSessionTimeout)
       
   922 					{
       
   923 						dmsessionTimeout = -1;
       
   924 					}
       
   925 					DBG_FILE_CODE(dmsessionTimeout, _S8("CNSmlAgentBase::SendingStateL, DMSessiontimeout feature value "));
       
   926 				}			
       
   927 				
       
   928 				if(dmsessionTimeout != -1 )
       
   929 				{
       
   930 					if((err == KErrTimedOut|| err == THTTPEvent::EFailed ) && iSendRetries >0 )
       
   931 					{
       
   932 					DBG_FILE(_S8("CNSmlAgentBase::SendingStateL reset to Sending state again and send same packet"));
       
   933 					iCurrMainState = ESending;
       
   934 					iSendRetries --;
       
   935 					}
       
   936 					else //if (err == KErrCancel)
       
   937 					{
       
   938 						DBG_FILE_CODE(err, _S8("CNSmlAgentBase::SendingStateL, DMSessiontimeout feature is ON but error code is not for retrying"));
       
   939 						User::Leave(err);
       
   940 					}
       
   941 					
       
   942 				}
       
   943 				else
       
   944 				{
       
   945 					DBG_FILE_CODE(err, _S8("CNSmlAgentBase::SendingStateL, DMSessiontimeout feature is OFF, leave with error code"));
       
   946 					User::Leave(err);
       
   947 				}
       
   948 		       	
       
   949 	       	   }
       
   950 		       	else  // DS Sessions , has to leave upon any leave happening from SendData
       
   951 		       	{
       
   952 		       		TInt val = 0;
       
   953 		       		CRepository* rep = CRepository::NewLC(KCRUidDataSyncInternalKeys);
       
   954     				TInt error = rep->Get(KNsmlDsAutoRestart, val);
       
   955     				CleanupStack::PopAndDestroy(rep);
       
   956     				
       
   957     				if(error == KErrNone && val == 1)
       
   958 					{
       
   959 		       		
       
   960 			       		DBG_FILE_CODE(iNetmonAPBearerType, _S8("CNSmlAgentBase::SendingStateL The Network Bearer Type is"));
       
   961 			       		DBG_FILE_CODE(err, _S8("CNSmlAgentBase::SendingStateL The Network Error is"));
       
   962 			       		
       
   963 			       		if(err == TNSmlHTTPErrCode::ENSmlHTTPErr_RequestTimeout)
       
   964 			       		{
       
   965 			       			DBG_FILE(_S8("CNSmlAgentBase::SendingStateL Invoking the NETMON exe due to Time Out"));
       
   966 			       			
       
   967 			       			iPacketDataUnAvailable = ETrue;
       
   968 		       				
       
   969 			       			//Invoke the NetMon exe via the Agent
       
   970 			       			LaunchAutoRestartL(err);
       
   971 			       		}
       
   972 		       		
       
   973 			       		else if(iNetmonAPBearerType == EApBearerTypeGPRS ||
       
   974 		    				   iNetmonAPBearerType == EApBearerTypeCDMA )
       
   975 			       		{
       
   976 			       			DBG_FILE(_S8("CNSmlAgentBase::SendingStateL Waiting for 30 sec"));
       
   977 			       			User::After(TTimeIntervalMicroSeconds32(30000000));
       
   978 			       			
       
   979 			       			if(	iPacketDataUnAvailable )
       
   980 			       			{
       
   981 			       				DBG_FILE(_S8("CNSmlAgentBase::SendingStateL Invoking the NETMON exe"));
       
   982 				       			//Invoke the NetMon exe via the Agent
       
   983 				       			LaunchAutoRestartL(err);	
       
   984 			       			}
       
   985 							else
       
   986 							{
       
   987 								DBG_FILE_CODE(err, _S8("CNSmlAgentBase::SendingStateL, Leaving with error code "));
       
   988 								User::Leave(err);	
       
   989 							}
       
   990 			       			
       
   991 			       			
       
   992 			       		}
       
   993 		       	
       
   994 			       		else
       
   995 			       		{
       
   996 			       			DBG_FILE_CODE(err, _S8("CNSmlAgentBase::SendingStateL, Leaving with error code "));
       
   997 			       			User::Leave(err);	
       
   998 			       		}
       
   999 		       		}
       
  1000 		       		
       
  1001 		       		else
       
  1002 		       		{
       
  1003 		       			DBG_FILE_CODE(err, _S8("CNSmlAgentBase::SendingStateL, Auto-Restart Feature is not Enabled Leaving with error code "));
       
  1004 		       			User::Leave(err);	
       
  1005 		       		}
       
  1006 		     	}
       
  1007 			}
       
  1008 		}
       
  1009 	DBG_FILE(_S8("CNSmlAgentBase::SendingStateL ends"));
       
  1010 	}
       
  1011 
       
  1012 //
       
  1013 // Receiving state functions 
       
  1014 //
       
  1015 // ---------------------------------------------------------
       
  1016 // CNSmlAgentBase:::ReceivingStateL()
       
  1017 // Receive fata from a server
       
  1018 // Navigate according to state code 
       
  1019 // ---------------------------------------------------------
       
  1020 EXPORT_C void CNSmlAgentBase::ReceivingStateL()
       
  1021 	{
       
  1022 	DBG_FILE(_S8("CNSmlAgentBase::ReceivingStateL begins"));
       
  1023 	if ( !iCommandIssued )
       
  1024 		{
       
  1025 		ReceiveDataIssueL();
       
  1026 		}
       
  1027 	else
       
  1028 		{
       
  1029 		ReceiveDataDoneL();
       
  1030 		NextMainStateL();
       
  1031 		}
       
  1032 	DBG_FILE(_S8("CNSmlAgentBase::ReceivingStateL ends"));
       
  1033 	}
       
  1034 
       
  1035 // ---------------------------------------------------------
       
  1036 // CNSmlAgentBase::CheckStatusCodesAreReceivedL
       
  1037 // Check that server has sent Status to all Client's commands.
       
  1038 // ---------------------------------------------------------
       
  1039 EXPORT_C TBool CNSmlAgentBase::CheckStatusCodesAreReceivedL( TBool aFinal )
       
  1040 	{
       
  1041 	TBool allReceived = ETrue; 
       
  1042 
       
  1043 	iSyncMLCmds->ResponseController()->Begin();	
       
  1044 	TBool continues = ETrue;
       
  1045 	while ( continues )
       
  1046 		{
       
  1047 		TInt entryID;
       
  1048 		if ( !iSyncMLCmds->ResponseController()->NextResponseInfo( entryID ) )
       
  1049 			{
       
  1050 			continues = EFalse;
       
  1051 			if ( aFinal )
       
  1052 				{ 
       
  1053 				iSyncMLCmds->ResponseController()->ResetL();
       
  1054 				}
       
  1055 			else
       
  1056 				{
       
  1057 				iSyncMLCmds->ResponseController()->RemoveAllReceivedOnes();
       
  1058 				}
       
  1059 			}
       
  1060 		else if ( !iSyncMLCmds->ResponseController()->StatusReceived( entryID ) )
       
  1061 			{
       
  1062 			TPtrC8 respContCmd = iSyncMLCmds->ResponseController()->Cmd(entryID);
       
  1063 
       
  1064 			if ( ( iAlreadyAuthenticated && respContCmd == KNSmlAgentSyncHdr ) 
       
  1065 				 ||  respContCmd == KNSmlAgentResults  
       
  1066 				 ||  respContCmd == KNSmlAgentGet  )
       
  1067 				{
       
  1068 				}
       
  1069 			else
       
  1070 				{
       
  1071 				allReceived = EFalse;
       
  1072 				if ( aFinal )
       
  1073 					{
       
  1074 					InterruptL( iSyncMLCmds->ResponseController()->AppIndex( entryID ), TNSmlError::ESmlStatusMissing, ETrue, EFalse );
       
  1075 					}
       
  1076 				}
       
  1077 			}
       
  1078 		}
       
  1079 	return allReceived;
       
  1080 	}
       
  1081 	
       
  1082 //
       
  1083 // Authentications 
       
  1084 //
       
  1085 
       
  1086 // ---------------------------------------------------------
       
  1087 // CNSmlAgentBase::SaveIfNonceL
       
  1088 // If server has transmitted NextNonce element, it is stored to Agent Log
       
  1089 // ---------------------------------------------------------
       
  1090 EXPORT_C void CNSmlAgentBase::SaveIfNonceL( const CNSmlResponseController& aResponseController, TInt aEntryID ) const
       
  1091 	{
       
  1092 	TPtrC8 chalType;
       
  1093 	chalType.Set( aResponseController.ChalType( aEntryID ) );
       
  1094 	if ( chalType.Length() == 0 )
       
  1095 		{
       
  1096 		return;
       
  1097 		}
       
  1098 	if ( chalType != KNSmlAgentAuthMD5 )
       
  1099 		{
       
  1100 		return;
       
  1101 		}
       
  1102 	TPtrC8 chalNextNonce;
       
  1103 	chalNextNonce.Set( aResponseController.ChalNextNonce( aEntryID ) );
       
  1104 	if ( chalNextNonce.Length() > 0 )
       
  1105 		{
       
  1106 		HBufC8* nextNonceDecoded = HBufC8::NewLC( chalNextNonce.Length() );
       
  1107 		TPtr8 nextNonceDecodedPtr( nextNonceDecoded->Des() );
       
  1108 		TPtrC8 chalFormat;
       
  1109 		chalFormat.Set( aResponseController.ChalFormat( aEntryID ) );
       
  1110 		if ( chalFormat == KNSmlAgentBase64Format )
       
  1111 			{
       
  1112 			//decode before saving
       
  1113 			TImCodecB64 B64Coder;
       
  1114 			B64Coder.Decode( chalNextNonce, nextNonceDecodedPtr );
       
  1115 			}
       
  1116 		else
       
  1117 			{
       
  1118 			*nextNonceDecoded = chalNextNonce;
       
  1119 			}
       
  1120 		HBufC* nextNonceUnicode = HBufC::NewLC( nextNonceDecoded->Length() );
       
  1121 		nextNonceUnicode->Des().Copy( *nextNonceDecoded );
       
  1122 		SetNonceL( *nextNonceUnicode );
       
  1123 		CleanupStack::PopAndDestroy( 2 ); //nextNonceUnicode, nextNonceDecoded
       
  1124 		}
       
  1125 	}
       
  1126 // ---------------------------------------------------------
       
  1127 // CNSmlAgentBase::AuthenticationRequirementL
       
  1128 // Check if a server has challenged authetication
       
  1129 // ---------------------------------------------------------
       
  1130 EXPORT_C TBool CNSmlAgentBase::AuthenticationRequirementL( const CNSmlResponseController& aResponseController, TInt aEntryID )
       
  1131 	{
       
  1132 	TPtrC8 chalType;
       
  1133 	chalType.Set( aResponseController.ChalType( aEntryID ) );
       
  1134 	if ( chalType.Length() == 0 )
       
  1135 		{
       
  1136 		chalType.Set( KNSmlAgentAuthBasic );
       
  1137 		}
       
  1138 	if ( chalType != KNSmlAgentAuthBasic && chalType != KNSmlAgentAuthMD5 )
       
  1139 		{
       
  1140 		Interrupt( TNSmlError::ESmlChalInvalid, EFalse, EFalse );
       
  1141 		return EFalse;
       
  1142 		}
       
  1143 	if ( !iSyncMLUserName )
       
  1144 		{
       
  1145 		return EFalse;
       
  1146 		}
       
  1147 	
       
  1148 	TAuthenticationType prevAuthType;
       
  1149 	prevAuthType = AuthTypeL();
       
  1150 	if ( chalType == KNSmlAgentAuthBasic )
       
  1151 		{
       
  1152 		if ( prevAuthType == EBasic )
       
  1153 			{
       
  1154 			return EFalse;
       
  1155 			}
       
  1156 		else
       
  1157 			{
       
  1158 			SetAuthTypeL( EBasic );
       
  1159 			TPtrC empty;
       
  1160 			SetNonceL( empty );
       
  1161 			}
       
  1162 		}
       
  1163 	if ( chalType == KNSmlAgentAuthMD5 )
       
  1164 		{
       
  1165 		if ( prevAuthType == EMD5 )
       
  1166 			{
       
  1167 			// only one MD5 chal per session is accepted
       
  1168 			// loop must be prevented
       
  1169 			if (iMD5AlreadyRequested )
       
  1170 				{
       
  1171 				return EFalse;
       
  1172 				}
       
  1173 			}
       
  1174 		else
       
  1175 			{
       
  1176 			SetAuthTypeL( EMD5 );
       
  1177 			}
       
  1178 		iMD5AlreadyRequested = ETrue;
       
  1179 		}
       
  1180 	return ETrue;
       
  1181 	}
       
  1182 
       
  1183 // ---------------------------------------------------------
       
  1184 // CNSmlAgentBase::ServerUserName()
       
  1185 // 
       
  1186 // ---------------------------------------------------------
       
  1187 EXPORT_C TPtrC CNSmlAgentBase::ServerUserName() const
       
  1188 	{
       
  1189 	return KNullDesC();
       
  1190 	}
       
  1191 
       
  1192 // ---------------------------------------------------------
       
  1193 // CNSmlAgentBase::ServerPassword()
       
  1194 // 
       
  1195 // ---------------------------------------------------------
       
  1196 EXPORT_C TPtrC CNSmlAgentBase::ServerPassword() const
       
  1197 	{
       
  1198 	return KNullDesC();
       
  1199 	}
       
  1200 
       
  1201 
       
  1202 // ---------------------------------------------------------
       
  1203 // CNSmlAgentBase::ProfileId()
       
  1204 // 
       
  1205 // ---------------------------------------------------------
       
  1206 EXPORT_C TInt CNSmlAgentBase::ProfileId() const
       
  1207 	{
       
  1208 	return iProfileID;
       
  1209 	}
       
  1210 
       
  1211 // ---------------------------------------------------------
       
  1212 // CNSmlAgentBase::StatusReference()
       
  1213 // Returns reference to the iStatus member variable.
       
  1214 // ---------------------------------------------------------
       
  1215 EXPORT_C TRequestStatus& CNSmlAgentBase::StatusReference()
       
  1216 	{
       
  1217 	return iStatus;
       
  1218 	}
       
  1219 
       
  1220 
       
  1221 // ---------------------------------------------------------
       
  1222 // CNSmlAgentBase::FreeBaseResources()
       
  1223 // Release allocated resources
       
  1224 // ---------------------------------------------------------
       
  1225 //
       
  1226 EXPORT_C void CNSmlAgentBase::FreeBaseResources()
       
  1227 	{
       
  1228 	delete iImeiCode;
       
  1229 	iImeiCode = NULL;
       
  1230 	delete iSyncMLUserName;
       
  1231 	iSyncMLUserName = NULL;
       
  1232 	delete iSyncMLPassword;
       
  1233 	iSyncMLPassword = NULL;
       
  1234 	delete iSyncServer;
       
  1235 	iSyncServer = NULL;
       
  1236 	delete iSyncMLCmds;
       
  1237 	iSyncMLCmds = NULL;
       
  1238 	delete iSyncMLMIMEType;
       
  1239 	iSyncMLMIMEType = NULL;
       
  1240 	delete iResultAlertCode;
       
  1241 	iResultAlertCode = NULL;
       
  1242 	delete iEndOfDataAlertCode;
       
  1243 	iEndOfDataAlertCode = NULL;
       
  1244 	delete iSessionAbortAlertCode;
       
  1245 	iSessionAbortAlertCode = NULL;
       
  1246 	delete iNextMessageCode;
       
  1247 	iNextMessageCode = NULL;
       
  1248 	delete iTransport;		
       
  1249 	iTransport = NULL;
       
  1250     delete iIAPIdArray;
       
  1251     iIAPIdArray = NULL;
       
  1252 	delete iSyncHTTPAuthUserName;
       
  1253 	iSyncHTTPAuthUserName = NULL;
       
  1254 	delete iSyncHTTPAuthPassword;
       
  1255 	iSyncHTTPAuthPassword = NULL;
       
  1256 	}
       
  1257 
       
  1258 //
       
  1259 // Begin state functions
       
  1260 //
       
  1261 // ---------------------------------------------------------
       
  1262 // CNSmlAgentBase::BeginConnectingStateL()
       
  1263 // 
       
  1264 // ---------------------------------------------------------
       
  1265 void CNSmlAgentBase::BeginConnectingStateL()
       
  1266 	{
       
  1267 	if ( !iCommandIssued ) 
       
  1268 		{
       
  1269 		ConnectIssueL();
       
  1270 		}
       
  1271 	else
       
  1272 		{
       
  1273 		ConnectDoneL();
       
  1274 		NextMainStateL();
       
  1275 		}
       
  1276 	}
       
  1277 //
       
  1278 // Client Initation state functions
       
  1279 //
       
  1280 // ---------------------------------------------------------
       
  1281 // CNSmlAgentBase::InitMakingStatusStateL()
       
  1282 // Writes all buffered status elements
       
  1283 // ---------------------------------------------------------
       
  1284 void CNSmlAgentBase::InitMakingStatusStateL()
       
  1285 	{
       
  1286 	TBool found( ETrue ); 
       
  1287 	if ( iSyncMLCmds->StatusContainer() )  
       
  1288 		{
       
  1289 		iSyncMLCmds->StatusContainer()->Begin();
       
  1290 		while( found )
       
  1291 			{
       
  1292 			SmlStatus_t* status;
       
  1293 			found = iSyncMLCmds->StatusContainer()->NextStatusElement( status, ETrue ); 
       
  1294 			if ( found )
       
  1295 				{
       
  1296 				iSyncMLCmds->DoStatusL( status );
       
  1297 				}
       
  1298 			}
       
  1299 		}
       
  1300 	iCurrClientInitState = EInitAlerting;
       
  1301 	}
       
  1302 
       
  1303 // ---------------------------------------------------------
       
  1304 // CNSmlAgentBase::InitEndingMessageStateL()
       
  1305 // 
       
  1306 // ---------------------------------------------------------
       
  1307 void CNSmlAgentBase::InitEndingMessageStateL()
       
  1308 	{
       
  1309 	iSyncMLCmds->DoEndMessageL();
       
  1310 	iFinalMessageFromClient = ETrue;
       
  1311 	NextMainStateL();
       
  1312 	}
       
  1313 
       
  1314 // ---------------------------------------------------------
       
  1315 // CNSmlAgentBase::DataUpdateStatusMakingAlertStateL()
       
  1316 // 
       
  1317 // ---------------------------------------------------------
       
  1318 void CNSmlAgentBase::DataUpdateStatusMakingAlertStateL()
       
  1319 	{
       
  1320 	DBG_FILE(_S8("CNSmlAgentBase::DataUpdateStatusMakingAlertStateL begins"));
       
  1321 	if ( iEndOfDataAlertRequest )
       
  1322 		{
       
  1323 		iSyncMLCmds->DoAlertL( *iEndOfDataAlertCode );
       
  1324 		iEndOfDataAlertRequest = EFalse;
       
  1325 		iStatusPackage = ETrue;
       
  1326 		}
       
  1327     if ( iSessionAbortAlertRequest )
       
  1328 		{
       
  1329 		iSyncMLCmds->DoAlertL( *iSessionAbortAlertCode );
       
  1330 		iSessionAbortAlertRequest = EFalse;
       
  1331 		iStatusPackage = ETrue;
       
  1332 		}
       
  1333 	iCurrDataUpdateStatusState = EStatusMakingMap;
       
  1334 	DBG_FILE(_S8("CNSmlAgentBase::DataUpdateStatusMakingAlertStateL ends"));
       
  1335 	}
       
  1336 // ---------------------------------------------------------
       
  1337 // CNSmlAgentBase::DataUpdateStatusMakingStatusStateL()
       
  1338 // Writes all buffered Status elements 
       
  1339 // ---------------------------------------------------------
       
  1340 void CNSmlAgentBase::DataUpdateStatusMakingStatusStateL()
       
  1341 	{
       
  1342 	DBG_FILE(_S8("CNSmlAgentBase::DataUpdateStatusMakingStatusStateL begins"));
       
  1343 	CNSmlCmdsBase::TReturnValue ret = CNSmlCmdsBase::EReturnOK;
       
  1344 	TBool found( ETrue ); 
       
  1345 	iStatusPackage = EFalse;
       
  1346 
       
  1347 	if ( iSyncMLCmds->StatusContainer() )
       
  1348 		{
       
  1349 		if ( iSyncMLCmds->StatusContainer()->AnyOtherThanOkSyncHdrStatus() )
       
  1350 			{
       
  1351 			// there is something to send
       
  1352 			iStatusPackage = ETrue;
       
  1353 			}
       
  1354 		iSyncMLCmds->StatusContainer()->Begin();
       
  1355 		while( found && ret == CNSmlCmdsBase::EReturnOK )
       
  1356 			{
       
  1357 			SmlStatus_t* status;
       
  1358 			found = iSyncMLCmds->StatusContainer()->NextStatusElement( status, ETrue ); 
       
  1359 			if ( found )
       
  1360 				{
       
  1361 				ret = iSyncMLCmds->DoStatusL( status );
       
  1362 				}
       
  1363 			}
       
  1364 		}
       
  1365 	if ( ret == CNSmlCmdsBase::EReturnBufferFull )
       
  1366 		{
       
  1367 		iBufferFull = ETrue;
       
  1368 		iCurrDataUpdateStatusState = EStatusEndingMessage;
       
  1369 		}
       
  1370 	else
       
  1371 		{
       
  1372 		iCurrDataUpdateStatusState = EStatusMakingAlert;
       
  1373 		}
       
  1374 	DBG_FILE(_S8("CNSmlAgentBase::DataUpdateStatusMakingStatusStateL ends"));
       
  1375 	}
       
  1376 // ---------------------------------------------------------
       
  1377 // CNSmlAgentBase::DataUpdateStatusEndingMessageStateL()
       
  1378 // End of SyncML Message
       
  1379 // ---------------------------------------------------------
       
  1380 void CNSmlAgentBase::DataUpdateStatusEndingMessageStateL()
       
  1381 	{
       
  1382 	DBG_FILE(_S8("CNSmlAgentBase::DataUpdateStatusEndingMessageStateL begins"));
       
  1383 	if ( iBufferFull )
       
  1384 		{
       
  1385 		iSyncMLCmds->DoEndMessageL( EFalse );
       
  1386 		iFinalMessageFromClient = EFalse;
       
  1387 		}
       
  1388 	else
       
  1389 		{
       
  1390 		iSyncMLCmds->DoEndMessageL();
       
  1391 		iFinalMessageFromClient = ETrue;
       
  1392 		}
       
  1393 	NextMainStateL();
       
  1394 	DBG_FILE(_S8("CNSmlAgentBase::DataUpdateStatusEndingMessageStateL ends"));
       
  1395 	}
       
  1396 // ---------------------------------------------------------
       
  1397 // CNSmlAgentBase::ResultAlertStartingMessageStateL()
       
  1398 // Makes SyncHdr
       
  1399 // ---------------------------------------------------------
       
  1400 void CNSmlAgentBase::ResultAlertStartingMessageStateL()
       
  1401 	{
       
  1402 	DBG_FILE(_S8("CNSmlAgentBase::ResultAlertStartingMessageStateL begins"));
       
  1403 	if ( iResultAlertIssued )
       
  1404 		{
       
  1405 		User::Leave( TNSmlError::ESmlServerIsBusy );
       
  1406 		}
       
  1407 	else
       
  1408 		{
       
  1409 		iBusyStatusReceived = EFalse;
       
  1410 		iResultAlertIssued = ETrue;
       
  1411 		}
       
  1412 	iSyncMLCmds->DoSyncHdrL();
       
  1413 	iCurrResultAlertState = EResultAlertMakingAlert;
       
  1414 	DBG_FILE(_S8("CNSmlAgentBase::ResultAlertStartingMessageStateL ends"));
       
  1415 	}
       
  1416 // ---------------------------------------------------------
       
  1417 // CNSmlAgentBase::ResultAlertAlertingStateL()
       
  1418 // Makes Alert command
       
  1419 // ---------------------------------------------------------
       
  1420 void CNSmlAgentBase::ResultAlertAlertingStateL()
       
  1421 	{
       
  1422 	DBG_FILE(_S8("CNSmlAgentBase::ResultAlertAlertingStateL begins"));
       
  1423 	if ( !Interrupted() )	
       
  1424 		{
       
  1425 		iSyncMLCmds->DoAlertL( *iResultAlertCode );
       
  1426 		}
       
  1427 	iCurrResultAlertState = EResultAlertEndingMessage;
       
  1428 	DBG_FILE(_S8("CNSmlAgentBase::ResultAlertAlertingStateL ends"));
       
  1429 	}
       
  1430 // ---------------------------------------------------------
       
  1431 // CNSmlAgentBase::ResultAlertEndingMessageStateL()
       
  1432 // 
       
  1433 // ---------------------------------------------------------
       
  1434 void CNSmlAgentBase::ResultAlertEndingMessageStateL()
       
  1435 	{
       
  1436 	DBG_FILE(_S8("CNSmlAgentBase::ResultAlertEndingMessageStateL begins"));
       
  1437 	iSyncMLCmds->DoEndMessageL( EFalse );
       
  1438 	iFinalMessageFromClient = EFalse;
       
  1439 	NextMainStateL();
       
  1440 	DBG_FILE(_S8("CNSmlAgentBase::ResultAlertEndingMessageStateL ends"));
       
  1441 	}
       
  1442 // ---------------------------------------------------------
       
  1443 // CNSmlAgentBase::ClientAlertNextStartingMessageStateL()
       
  1444 // Makes SyncHdr
       
  1445 // ---------------------------------------------------------
       
  1446 void CNSmlAgentBase::ClientAlertNextStartingMessageStateL()
       
  1447 	{
       
  1448 	DBG_FILE(_S8("CNSmlAgentBase::ClientAlertNextStartingMessageStateL begins"));
       
  1449 	iSyncMLCmds->DoSyncHdrL();
       
  1450 	iCurrClientAlertNextState = EClientAlertNextMakingAlert;
       
  1451 	DBG_FILE(_S8("CNSmlAgentBase::ClientAlertNextStartingMessageStateL ends"));
       
  1452 	}
       
  1453 // ---------------------------------------------------------
       
  1454 // CNSmlAgentBase::ClientAlertNextAlertingStateL()
       
  1455 // Makes Alert command
       
  1456 // ---------------------------------------------------------
       
  1457 void CNSmlAgentBase::ClientAlertNextAlertingStateL()
       
  1458 	{
       
  1459 	DBG_FILE(_S8("CNSmlAgentBase::ClientAlertNextAlertingStateL begins"));
       
  1460 	if ( !Interrupted() ) 
       
  1461 		{
       
  1462 		iSyncMLCmds->DoAlertL( *iNextMessageCode );
       
  1463 		}
       
  1464 	if ( iEndOfDataAlertRequest )
       
  1465 		{
       
  1466 		iSyncMLCmds->DoAlertL( *iEndOfDataAlertCode );
       
  1467 		iEndOfDataAlertRequest = EFalse;
       
  1468 		}
       
  1469 	if ( iSessionAbortAlertRequest )
       
  1470 		{
       
  1471 		iSyncMLCmds->DoAlertL( *iSessionAbortAlertCode );
       
  1472 		iSessionAbortAlertRequest = EFalse;
       
  1473 		}
       
  1474 	iCurrClientAlertNextState = EClientAlertNextMakingStatus;
       
  1475 	DBG_FILE(_S8("CNSmlAgentBase::ClientAlertNextAlertingStateL ends"));
       
  1476 	}
       
  1477 // ---------------------------------------------------------
       
  1478 // CNSmlAgentBase::ClientAlertNextMakingStatusStateL()
       
  1479 // Writes all buffered Status elements  
       
  1480 // ---------------------------------------------------------
       
  1481 void CNSmlAgentBase::ClientAlertNextMakingStatusStateL()
       
  1482 	{
       
  1483 	DBG_FILE(_S8("CNSmlAgentBase::ClientAlertNextMakingStatusStateL begins"));
       
  1484 	CNSmlCmdsBase::TReturnValue ret = CNSmlCmdsBase::EReturnOK;
       
  1485 	if ( iSyncMLCmds->StatusContainer() )
       
  1486 		{
       
  1487 		iSyncMLCmds->StatusContainer()->Begin();
       
  1488 		TBool found( ETrue ); 
       
  1489 		while( found )
       
  1490 			{
       
  1491 			SmlStatus_t* status;
       
  1492 			found = iSyncMLCmds->StatusContainer()->NextStatusElement( status, ETrue ); 
       
  1493 			if ( found )
       
  1494 				{
       
  1495 				ret = iSyncMLCmds->DoStatusL( status );
       
  1496 				}
       
  1497 			}
       
  1498 		}
       
  1499 	if ( ret == CNSmlCmdsBase::EReturnBufferFull )
       
  1500 		{
       
  1501 		iCurrClientAlertNextState = EClientAlertNextEndingMessage;
       
  1502 		}
       
  1503 	else
       
  1504 		{
       
  1505 		iCurrClientAlertNextState = EClientAlertNextMakingMap;
       
  1506 		}
       
  1507 	DBG_FILE(_S8("CNSmlAgentBase::ClientAlertNextMakingStatusStateL ends"));
       
  1508 	}
       
  1509 // ---------------------------------------------------------
       
  1510 // CNSmlAgentBase::ClientAlertNextEndingMessageStateL()
       
  1511 // 
       
  1512 // ---------------------------------------------------------
       
  1513 void CNSmlAgentBase::ClientAlertNextEndingMessageStateL()
       
  1514 	{
       
  1515 	DBG_FILE(_S8("CNSmlAgentBase::ClientAlertNextEndingMessageStateL begins"));
       
  1516 	iSyncMLCmds->DoEndMessageL( EFalse );
       
  1517 	iFinalMessageFromClient = EFalse;
       
  1518 	NextMainStateL();
       
  1519 	DBG_FILE(_S8("CNSmlAgentBase::ClientAlertNextEndingMessageStateL ends"));
       
  1520 	}
       
  1521 //
       
  1522 //  Transport functions
       
  1523 //
       
  1524 // ---------------------------------------------------------
       
  1525 // CNSmlAgentBase::ConnectIssueL()
       
  1526 // 
       
  1527 // ---------------------------------------------------------
       
  1528 void CNSmlAgentBase::ConnectIssueL()
       
  1529 	{
       
  1530 	DBG_FILE(_S8("CNSmlAgentBase::ConnectIssueL begins"));
       
  1531 	#ifndef __NOTRANSPORT
       
  1532 	HBufC8* uri8 = NULL;
       
  1533 	HBufC8* httpusername8 = NULL;
       
  1534 	HBufC8* httppassword8 = NULL;
       
  1535 
       
  1536 	NSmlUnicodeConverter::HBufC8InUTF8LC( iSyncServer->HostNameWithPortL(), uri8 );
       
  1537 	if( iSyncHTTPAuthUserName )
       
  1538 		{
       
  1539 		TPtrC uname( iSyncHTTPAuthUserName->Des() );	
       
  1540 		httpusername8 = HBufC8::NewLC( uname.Length() );
       
  1541 		httpusername8->Des().Copy( uname );
       
  1542 
       
  1543 		TPtrC pword( iSyncHTTPAuthPassword->Des() );
       
  1544 		httppassword8 = HBufC8::NewLC( pword.Length() );
       
  1545 		httppassword8->Des().Copy( pword );
       
  1546 		}
       
  1547 	else
       
  1548 		{	
       
  1549 		httpusername8 = KNullDesC8().AllocLC();
       
  1550 		httppassword8 = KNullDesC8().AllocLC();
       
  1551 		}
       
  1552 
       
  1553 	iTransport->ConnectL( iMediumType, iSyncInitiation == EServerAlerted, iIAPIdArray, *uri8, *iSyncMLMIMEType, iStatus, *httpusername8, *httppassword8, iSyncHTTPAuthUsed );	
       
  1554     CleanupStack::PopAndDestroy(3);  //uri8, httpusername8, httppassword8
       
  1555 	#endif
       
  1556 	iCommandIssued = ETrue;	
       
  1557 	DBG_FILE(_S8("CNSmlAgentBase::ConnectIssueL ends"));
       
  1558 	}
       
  1559 // ---------------------------------------------------------
       
  1560 // CNSmlAgentBase::ConnectDoneL()
       
  1561 // 
       
  1562 // ---------------------------------------------------------
       
  1563 void CNSmlAgentBase::ConnectDoneL()
       
  1564 	{
       
  1565 	DBG_FILE(_S8("CNSmlAgentBase::ConnectDoneL begins"));
       
  1566 	iCommandIssued = EFalse;
       
  1567 	if ( iStatus != KErrNone ) 
       
  1568 		{
       
  1569 		User::Leave( iStatus.Int() );
       
  1570 		}
       
  1571 	//RD_AUTO_RESTART		
       
  1572 	//Get the Access Point ID used for Synchronizing
       
  1573 	ReadAcessPointL();
       
  1574 	//RD_AUTO_RESTART		
       
  1575 	DBG_FILE(_S8("CNSmlAgentBase::ConnectDoneL ends"));
       
  1576 	}
       
  1577 
       
  1578 //RD_AUTO_RESTART
       
  1579 // -----------------------------------------------------------------------------
       
  1580 // CNSmlAgentBase::ReadAcessPointL
       
  1581 // Reads the open connections' Access Point Id.
       
  1582 // -----------------------------------------------------------------------------
       
  1583 void CNSmlAgentBase::ReadAcessPointL()
       
  1584 {
       
  1585 	// Take AP from open connection
       
  1586     RSocketServ socketServer;
       
  1587     TInt err( KErrNone );
       
  1588     err = socketServer.Connect();
       
  1589     RConnection myConnection;
       
  1590     err = myConnection.Open( socketServer );
       
  1591     TUint connectionCount( 0 );
       
  1592     err = myConnection.EnumerateConnections( connectionCount );
       
  1593     DBG_FILE_CODE(connectionCount, _S8("CNSmlAgentBase::ReadAcessPointL(), The Connection count is:"));
       
  1594     if ( err != KErrNone || connectionCount < 1 )
       
  1595     {
       
  1596         iNetmonAPId = 0;
       
  1597     }
       
  1598 
       
  1599     else
       
  1600     {
       
  1601 
       
  1602 		TPckgBuf<TConnectionInfoV2> connectionInfo;
       
  1603 		err = myConnection.GetConnectionInfo( connectionCount,
       
  1604 				connectionInfo );	
       
  1605 		iNetmonAPId = connectionInfo().iIapId;
       
  1606 		DBG_FILE_CODE(iNetmonAPId, _S8("CNSmlAgentBase::ReadAcessPointL(), The IAPId is:"));
       
  1607 		iNetmonAPBearerType = CheckAPBearerTypeL( iNetmonAPId );
       
  1608 		DBG_FILE_CODE(TInt(iNetmonAPBearerType), _S8("CNSmlAgentBase::ReadAcessPointL(), The Enumerated IAPId is:"));
       
  1609     	
       
  1610     }
       
  1611 
       
  1612     myConnection.Close();
       
  1613     socketServer.Close();
       
  1614 }
       
  1615 
       
  1616 // -----------------------------------------------------------------------------
       
  1617 // CNSmlAgentBase::CheckAPBearerTypeL
       
  1618 // Returns bearer type of the selected Access Point.
       
  1619 // -----------------------------------------------------------------------------
       
  1620 EXPORT_C TApBearerType CNSmlAgentBase::CheckAPBearerTypeL( const TUint32 aIAPId )
       
  1621 {
       
  1622     // Create connection to the Access Points setting data.
       
  1623     CCommsDatabase* cAPCommsDatabase = CCommsDatabase::NewL( EDatabaseTypeIAP );
       
  1624     
       
  1625     TApBearerType bearerType(TApBearerType(-1));
       
  1626     
       
  1627     // Attach to the Access Point Engine.
       
  1628     CApSelect* apSelect = CApSelect::NewLC( 
       
  1629                                         *cAPCommsDatabase,
       
  1630                                         KEApIspTypeAll,
       
  1631                                             EApBearerTypeWLAN |
       
  1632                                             EApBearerTypeCDMA |
       
  1633                                             EApBearerTypeGPRS,
       
  1634                                         KEApSortUidAscending,
       
  1635                                         EIPv4 | EIPv6
       
  1636                                         );
       
  1637     
       
  1638     // Create ApUtils for some utilities functions.
       
  1639     CApUtils* apUtils = CApUtils::NewLC( *cAPCommsDatabase );
       
  1640     
       
  1641     // Get supported Access Points from Access Point Engine..
       
  1642     CApListItemList* apItems = new (ELeave) CApListItemList;
       
  1643     CleanupStack::PushL( apItems );
       
  1644     apSelect->AllListItemDataL( *apItems );
       
  1645     
       
  1646     for ( TInt i = 0; i < apItems->Count(); i++ )
       
  1647         {
       
  1648         // Get id from APEngine and convert it to the CommsDB id.
       
  1649         TUint32 iapId = apUtils->IapIdFromWapIdL( apItems->At( i )->Uid() );
       
  1650         
       
  1651         // Change bearer type according to id match.
       
  1652         if ( aIAPId == iapId )
       
  1653             {
       
  1654             bearerType = apItems->At( i )->BearerType(); 
       
  1655             
       
  1656             //Getting the IAP name
       
  1657             const TDesC& name = apItems->At( i )->Name();
       
  1658 		
       
  1659 			DBG_ARGS(_S("CNSmlAgentBase::CheckAPBearerTypeL(), The IAP Name is: %S"), &name);         
       
  1660                    
       
  1661             i = apItems->Count();
       
  1662             }
       
  1663         }
       
  1664     
       
  1665     // PopAndDestroy some items.
       
  1666     CleanupStack::PopAndDestroy( apItems );
       
  1667     CleanupStack::PopAndDestroy( apUtils );
       
  1668     CleanupStack::PopAndDestroy( apSelect );
       
  1669     
       
  1670     delete cAPCommsDatabase;
       
  1671     
       
  1672     // Return bearer type.
       
  1673     return bearerType;    
       
  1674 }
       
  1675 //RD_AUTO_RESTART
       
  1676 
       
  1677 // ---------------------------------------------------------
       
  1678 // CNSmlAgentBase::SendDataIssueL()
       
  1679 // Send data to a server
       
  1680 // This is asynchronous request. 
       
  1681 // ---------------------------------------------------------
       
  1682 void CNSmlAgentBase::SendDataIssueL()
       
  1683 	{
       
  1684 	DBG_FILE(_S8("CNSmlAgentBase::SendDataIssueL begins"));
       
  1685 	TPtrC8 document( iSyncMLCmds->GeneratedDocument() );
       
  1686 #ifdef __NOTRANSPORT
       
  1687 	CWbxml2XmlConverter* c = CWbxml2XmlConverter::NewLC();
       
  1688 	c->ConvertL(document.Ptr(), document.Length());
       
  1689 	DBG_DUMP((void*)iSyncMLCmds->GeneratedDocument().Ptr(), iSyncMLCmds->GeneratedDocument().Length(), _S8("SendDataL (WBXML)\n") );
       
  1690 	DBG_DUMP((void*)c->Document().Ptr(), c->Document().Length(), _S8("SendDataL (XML)") );
       
  1691 	CleanupStack::PopAndDestroy(); // c
       
  1692 #else
       
  1693 	if ( iSyncMLCmds->RespURI() != NULL )
       
  1694 		{
       
  1695 		iTransport->ChangeTargetURIL( *iSyncMLCmds->RespURI()->HostNameInUTF8AllocLC() );
       
  1696 		CleanupStack::PopAndDestroy();
       
  1697 		}
       
  1698 	iTransport->Send( document, EFalse, iStatus ); 
       
  1699 #endif  // __NOTRANSPORT
       
  1700 	iCommandIssued = ETrue;	
       
  1701 	DBG_FILE(_S8("CNSmlAgentBase::SendDataIssueL ends"));
       
  1702 	}
       
  1703 
       
  1704 // ---------------------------------------------------------
       
  1705 // CNSmlAgentBase::SendDataDoneL()
       
  1706 // Response to asynchronous SendDataIssueL() call
       
  1707 // ---------------------------------------------------------
       
  1708 void CNSmlAgentBase::SendDataDoneL()
       
  1709 	{
       
  1710 	DBG_FILE(_S8("CNSmlAgentBase::SendDataDoneL begins"));
       
  1711 	DBG_FILE_CODE(iStatus.Int(), _S8(" CNSmlAgentBase::SendDataDoneL ERROR CODE is "));
       
  1712 	iCommandIssued = EFalse;
       
  1713 	if ( iStatus != KErrNone ) 
       
  1714 		{
       
  1715 		DBG_FILE(_S8("CNSmlAgentBase::SendDataDoneL ERROR"));
       
  1716 		User::Leave( iStatus.Int() );
       
  1717 		}
       
  1718 	DBG_FILE(_S8("CNSmlAgentBase::SendDataDoneL ends"));
       
  1719 	}
       
  1720 
       
  1721 // ---------------------------------------------------------
       
  1722 // CNSmlAgentBase::ReceiveDataIssueL()
       
  1723 // Receive data from a server
       
  1724 // This is asynchronous request.
       
  1725 // ---------------------------------------------------------
       
  1726 void CNSmlAgentBase::ReceiveDataIssueL()
       
  1727 	{
       
  1728 	DBG_FILE(_S8("CNSmlAgentBase::ReceiveDataIssueL begins"));
       
  1729 #ifndef __NOTRANSPORT
       
  1730 	iBufferArea.Set(iSyncMLCmds->BufferAreaForParsingL());
       
  1731 	iTransport->Receive( iBufferArea, iStatus );
       
  1732 	
       
  1733 #else  
       
  1734 	//TESTSEQU
       
  1735 	_LIT(KServerInitFile,"C:\\system\\data\\servinit.wbxml");
       
  1736 	_LIT(KServerInitFile1,"C:\\system\\data\\servinit1.wbxml");
       
  1737 	_LIT(KServerInitFile2,"C:\\system\\data\\servinit2.wbxml");
       
  1738 	_LIT(KServerDSModFile, "C:\\system\\data\\servmod%d.wbxml");
       
  1739     _LIT(KServerModFile,"C:\\system\\data\\servmod.wbxml");
       
  1740 	_LIT(KServerModFile1,"C:\\system\\data\\servmod1.wbxml");
       
  1741 	_LIT(KServerModFile2,"C:\\system\\data\\servmod2.wbxml");
       
  1742 	_LIT(KServerModFile3,"C:\\system\\data\\servmod3.wbxml");
       
  1743 	_LIT(KServerMapFile,"C:\\system\\data\\servmap.wbxml");
       
  1744 
       
  1745 	RFs fsSession;
       
  1746 	User::LeaveIfError(fsSession.Connect());
       
  1747 	RFile syncmlFile;
       
  1748 
       
  1749 
       
  1750 
       
  1751 #ifdef __DM_MODULETEST
       
  1752      if ( iPrevSyncState == EClientInitialisation || iPrevSyncState == EServerInitialisation ) 
       
  1753 		{
       
  1754 		User::LeaveIfError( syncmlFile.Open(fsSession, KServerInitFile, EFileShareAny) );
       
  1755 		}
       
  1756 	 else
       
  1757 #ifdef __SEVERAL_SERVER_MODS
       
  1758 		{  
       
  1759 		iTestCounter ++;
       
  1760 		if ( iTestCounter == 3 )
       
  1761 			{
       
  1762 			User::LeaveIfError( syncmlFile.Open(fsSession, KServerModFile3, EFileShareAny) );
       
  1763 			}
       
  1764 		else
       
  1765 		if ( iTestCounter == 2 )
       
  1766 			{
       
  1767 			User::LeaveIfError( syncmlFile.Open(fsSession, KServerModFile2, EFileShareAny) );
       
  1768 			}
       
  1769 		else
       
  1770 			{
       
  1771 			User::LeaveIfError( syncmlFile.Open(fsSession, KServerModFile1, EFileShareAny) );
       
  1772 			}
       
  1773 		}
       
  1774 #else
       
  1775 		User::LeaveIfError( syncmlFile.Open(fsSession, KServerModFile, EFileShareAny) );
       
  1776 #endif // __SEVERAL_SERVER_MODS
       
  1777 
       
  1778 #else
       
  1779 	if ( iPrevSyncState == EClientInitialisation || iPrevSyncState == EServerInitialisation ) 
       
  1780 		{
       
  1781 #ifdef __TWO_SERVER_INITS
       
  1782         ++iTestCounter;
       
  1783 		if ( iTestCounter == 2 )
       
  1784 			{
       
  1785 			User::LeaveIfError( syncmlFile.Open(fsSession, KServerInitFile2, EFileShareAny) );
       
  1786 			}
       
  1787 		else
       
  1788 			{
       
  1789 			User::LeaveIfError( syncmlFile.Open(fsSession, KServerInitFile1, EFileShareAny) );
       
  1790 			} 
       
  1791 #else
       
  1792 		User::LeaveIfError( syncmlFile.Open(fsSession, KServerInitFile, EFileShareAny) );
       
  1793 #endif // __TWO_SERVER_INITS
       
  1794 		}
       
  1795 	else if (iPrevSyncState == EClientModifications || iPrevSyncState == EServerModifications )		
       
  1796 		{
       
  1797 		TBuf<35> modFile;
       
  1798 		modFile.Format( KServerDSModFile, ++iTestCounter );
       
  1799 		User::LeaveIfError( syncmlFile.Open( fsSession, modFile, EFileShareAny ) );
       
  1800 		}
       
  1801 	else
       
  1802 		{
       
  1803 		User::LeaveIfError( syncmlFile.Open(fsSession, KServerMapFile, EFileShareAny) );
       
  1804 		}
       
  1805 #endif // __DM_MODULE_TEST
       
  1806 	TPtr8 ptr = iSyncMLCmds->BufferAreaForParsingL();
       
  1807 	syncmlFile.Read( ptr );
       
  1808 	syncmlFile.Close();
       
  1809 	fsSession.Close();
       
  1810 
       
  1811 	DBG_DUMP((void*)iSyncMLCmds->BufferAreaForDebugDumpL().Ptr(), iSyncMLCmds->BufferAreaForDebugDumpL().Length(), _S8("ReceiveDataL (XML)\n") );
       
  1812 	TPtrC8 document( iSyncMLCmds->BufferAreaForDebugDumpL() );
       
  1813 	CWbxml2XmlConverter* c = CWbxml2XmlConverter::NewLC();
       
  1814 	TRAPD(err,c->ConvertL(document.Ptr(), document.Length()));
       
  1815 	if ( err == KErrNone )
       
  1816 		DBG_DUMP((void*)c->Document().Ptr(), c->Document().Length(), _S8("ReceiveDataL (XML)") );
       
  1817 	CleanupStack::PopAndDestroy(); // c
       
  1818 
       
  1819 #endif // __NOTRANSPORT
       
  1820 	
       
  1821 	iCommandIssued = ETrue;	
       
  1822 	DBG_FILE(_S8("CNSmlAgentBase::ReceiveDataIssueL begins"));
       
  1823 	}
       
  1824 // ---------------------------------------------------------
       
  1825 // CNSmlAgentBase::ReceiveDataDoneL()
       
  1826 // Response to asynchronous ReceiveDataIssueL() call
       
  1827 // ---------------------------------------------------------
       
  1828 void CNSmlAgentBase::ReceiveDataDoneL()
       
  1829 	{
       
  1830 	DBG_FILE(_S8("CNSmlAgentBase::ReceiveDataDoneL begins"));
       
  1831 	iCommandIssued = EFalse;
       
  1832 	if ( iStatus != KErrNone ) 
       
  1833 		{
       
  1834 		DBG_FILE(_S8("CNSmlAgentBase::ReceiveDataDoneL ERROR"));
       
  1835 		User::Leave( iStatus.Int() );
       
  1836 		}
       
  1837 	DBG_FILE(_S8("CNSmlAgentBase::ReceiveDataDoneL begins"));
       
  1838 	}
       
  1839 
       
  1840 // ---------------------------------------------------------
       
  1841 // CNSmlAgentBase::Disconnect()
       
  1842 // 
       
  1843 // ---------------------------------------------------------
       
  1844 void CNSmlAgentBase::Disconnect()
       
  1845 	{
       
  1846 	DBG_FILE(_S8("CNSmlAgentBase::Disconnect begins"));
       
  1847 	#ifndef __NOTRANSPORT
       
  1848 	iTransport->Disconnect();
       
  1849 	#endif
       
  1850 	iCommandIssued = ETrue;	
       
  1851 	DBG_FILE(_S8("CNSmlAgentBase::Disconnect ends"));
       
  1852 	}
       
  1853 //
       
  1854 // Error Handling functions
       
  1855 //
       
  1856 // ---------------------------------------------------------
       
  1857 // CNSmlAgentBase::ErrorHandling()
       
  1858 // This function is called if sync is broken down immediately (Leave errors)
       
  1859 // This function MUST NOT leave.
       
  1860 // ---------------------------------------------------------
       
  1861 void CNSmlAgentBase::ErrorHandling( TInt aErrorCode )
       
  1862 	{
       
  1863 	DBG_FILE(_S8("CNSmlAgentBase::ErrorHandling begins"));
       
  1864 	
       
  1865 	DBG_FILE_CODE(aErrorCode, _S8(" CNSmlAgentBase::ErrorHandling ERROR CODE is "));
       
  1866 	if ( iError )
       
  1867 		{
       
  1868 		iError->SetErrorCode( aErrorCode );
       
  1869 		}
       
  1870 		
       
  1871 	TRAP_IGNORE( FinaliseWhenErrorL() );
       
  1872 	
       
  1873 	TRAP_IGNORE(  
       
  1874 	
       
  1875 	if ( iMediumType != KUidNSmlMediumTypeInternet &&
       
  1876 	     iCurrMainState < EClientInitialisation &&
       
  1877 	     iSyncInitiation == EServerAlerted )
       
  1878 		{		
       
  1879 		delete iTransport;
       
  1880 		iTransport = 0;	
       
  1881 		
       
  1882 		iTransport = CNSmlTransport::NewL();
       
  1883 			
       
  1884 		TRequestStatus status;
       
  1885 		TDesC8 temp = KNullDesC8();
       
  1886 		
       
  1887 	
       
  1888 		iTransport->ConnectL( KUidNSmlMediumTypeUSB, ETrue, NULL, temp, *iSyncMLMIMEType , status, temp, temp, 0 ); 
       
  1889 		User::WaitForRequest(status);
       
  1890 		
       
  1891 		if (status.Int() == KErrNone)
       
  1892 			{
       
  1893 			iTransport->Disconnect();
       
  1894 			}
       
  1895 		}
       
  1896 	
       
  1897 	)
       
  1898 	
       
  1899 	//RD_AUTO_RESTART		
       
  1900 	if(iPacketDataUnAvailable)
       
  1901 	{
       
  1902 		DBG_FILE(_S8("CNSmlAgentBase::FinaliseWhenErrorL Prompting for a Dialog"));
       
  1903 		iError->SetErrorCode( TNSmlError::ESmlCommunicationError);		
       
  1904 		User::RequestComplete( iCallerStatus, TNSmlError::ESmlCommunicationError );
       
  1905 	}
       
  1906 	//RD_AUTO_RESTART
       
  1907 	else if ( aErrorCode == KErrCancel )
       
  1908 		{
       
  1909 		User::RequestComplete( iCallerStatus, KErrCancel );
       
  1910 		}
       
  1911 	else
       
  1912 		{
       
  1913 		User::RequestComplete( iCallerStatus, aErrorCode );
       
  1914 		}
       
  1915 		
       
  1916 	// free resources to get space (if memory is full) to write to Sync Log.
       
  1917 	FreeResources();
       
  1918 	
       
  1919 	FinalizeSyncLog();
       
  1920 	
       
  1921 	iEnd = ETrue;
       
  1922 	DBG_FILE(_S8("CNSmlAgentBase::ErrorHandling ends"));
       
  1923 	}
       
  1924 
       
  1925 //RD_AUTO_RESTART
       
  1926 // ---------------------------------------------------------
       
  1927 // CNSmlAgentBase::LaunchAutoRestart()
       
  1928 // **Auto Restart**
       
  1929 // ---------------------------------------------------------	
       
  1930 EXPORT_C void CNSmlAgentBase::LaunchAutoRestartL(TInt aError)
       
  1931 {
       
  1932 	//Do Nothing
       
  1933 }
       
  1934 //  End of File