syncmlfw/dm/syncagent/src/nsmldmagent.cpp
changeset 0 b497e44ab2fc
child 9 57a65a3a658c
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:  SyncML DM command handling 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <SyncMLObservers.h>
       
    21 #include <SyncMLHistory.h>
       
    22 #include <PolicyEngineClient.h>
       
    23 #include <centralrepository.h>
       
    24 #include "PMUtilInternalCRKeys.h"
       
    25 
       
    26 #include <nsmlconstants.h>
       
    27 #include <nsmldebug.h>
       
    28 #include <nsmlunicodeconverter.h>
       
    29 // DM specific includes
       
    30 #include <nsmldmmodule.h>
       
    31 // common includes with DS
       
    32 #include "nsmlcliagconstants.h"
       
    33 #include "NSmlAgentBase.h" 
       
    34 #include "NSmlCmdsBase.h"
       
    35 #include "NSmlStatusContainer.h"
       
    36 #include "NSmlResultsContainer.h"
       
    37 #include "NSmlResponseController.h"
       
    38 #include "NSmlAuth.h"
       
    39 #include "NSmlURI.h"
       
    40 #include "nsmlagenttestdefines.h"
       
    41 // DM specific includes
       
    42 #include "nsmldmagconstants.h"
       
    43 #include "NSmlDMAgent.h"
       
    44 #include "NSmlDMCmds.h"
       
    45 #include "nsmldmsettings.h"
       
    46 #include "nsmldmerror.h"
       
    47 #include "NSmlHistoryArray.h"
       
    48 // FOTA
       
    49 #include "NSmlDMGenericAlert.h"
       
    50 #include "NSmlPrivateAPI.h"
       
    51 // FOTA end
       
    52 #include <featmgr.h>
       
    53 const TUid KUidNotifier = { 0x101F8769 };
       
    54 const TInt KNotifierTimeout = 300000000; // 5 min timeout
       
    55 _LIT8 ( KNSmlDMFotaNode, "FUMO" );
       
    56 
       
    57 // ================= MEMBER FUNCTIONS =======================
       
    58 
       
    59 // ---------------------------------------------------------
       
    60 // CNSmlDMAgent::NewL()
       
    61 // Creates new instance of CNSmlDMAgent. 
       
    62 // Does not leave instance pointer to CleanupStack.
       
    63 // ---------------------------------------------------------
       
    64 //
       
    65 CNSmlDMAgent* CNSmlDMAgent::NewL( MSyncMLProgressObserver* aDMObserver )
       
    66     {
       
    67 	CNSmlDMAgent* self = new (ELeave) CNSmlDMAgent();
       
    68     CleanupStack::PushL( self );
       
    69 	self->ConstructL( aDMObserver );
       
    70 	CleanupStack::Pop();
       
    71     return self;
       
    72     }
       
    73 
       
    74 // ---------------------------------------------------------
       
    75 // CNSmlDMAgent::~CNSmlDMAgent()
       
    76 // Destructor
       
    77 // ---------------------------------------------------------
       
    78 //
       
    79 CNSmlDMAgent::~CNSmlDMAgent()
       
    80     {
       
    81 	delete iDMTriggerMessage;
       
    82 	delete iDMServerId;
       
    83 	delete iDMServerPassword;
       
    84 	delete iDMServerDigest;
       
    85 	delete iSyncLog;
       
    86 	// FOTA
       
    87 	iGenAlerts.ResetAndDestroy();
       
    88 	iGenAlerts.Close();
       
    89 	// FOTA end	
       
    90 if(FeatureManager::FeatureSupported(KFeatureIdSapPolicyManagement))
       
    91 {
       
    92 	delete iNotifierObserver;
       
    93 }
       
    94 FeatureManager::UnInitializeLib();
       
    95 	}
       
    96 
       
    97 // ---------------------------------------------------------
       
    98 // CNSmlDMAgent::Configure()
       
    99 // Starts the sync, returns immediately and the sync continues
       
   100 // in RunL() function 
       
   101 // ---------------------------------------------------------
       
   102 //
       
   103 void CNSmlDMAgent::Configure( TRequestStatus& aStatus,TInt aProfileId,TInt aTransportId,TNSmlSyncInitiation aSyncInitiation,TInt aIapId)
       
   104  	{
       
   105 	DBG_FILE(_S8("CNSmlDMAgent::Configure"));
       
   106 	
       
   107 	CActiveScheduler::Add(this);
       
   108 	iProfileID = aProfileId;
       
   109 	iMediumType.iUid = aTransportId;
       
   110 	iSyncInitiation = aSyncInitiation;
       
   111 	iCallerStatus = &aStatus;
       
   112 	iNewIapId = aIapId;
       
   113 	*iCallerStatus = KRequestPending;
       
   114 	TRAPD(leaveCode,iSyncLog = CSyncMLHistoryJob::NewL());
       
   115 	
       
   116 	if(leaveCode!=KErrNone)
       
   117 		{
       
   118 		User::RequestComplete( iCallerStatus, leaveCode );
       
   119 		return;
       
   120 		}
       
   121 
       
   122 	SetActive();
       
   123 	TRequestStatus* status = &iStatus;
       
   124 	User::RequestComplete( status, KErrNone );
       
   125 	}
       
   126 
       
   127 // FOTA
       
   128 // ---------------------------------------------------------
       
   129 // CNSmlDMAgent::SetGenericAlertL()
       
   130 //	Appends a Generic Alert to the internal list. 
       
   131 //	The alert itself is sent in the later phase of the dm session.
       
   132 //	@param aFwMgmtUri. The uri which has been used in the exe command whose final result is 
       
   133 //	about to be reported.
       
   134 //	@param aMetaType. Meta/Type that should be used in the alert.
       
   135 //	@param aMetaFormat. Meta/Format that should be used in the alert.
       
   136 //	@param aFinalResult. The final result value which is reported to remote server.
       
   137 //	@param aCorrelator. Correlator value used in the original exec command. 
       
   138 // ---------------------------------------------------------
       
   139 //
       
   140 void CNSmlDMAgent::SetGenericAlertL ( const TDesC8& aFwMgmtUri, const TDesC8& aMetaType, const TDesC8& aMetaFormat, 
       
   141 						TInt aFinalResult, const TDesC8& aFwCorrelator )
       
   142 	{
       
   143 	CNSmlDMGenericAlert* genAlert = CNSmlDMGenericAlert::NewL ( aFwMgmtUri, aMetaType, aMetaFormat, aFinalResult, aFwCorrelator );
       
   144 	iGenAlerts.AppendL ( genAlert );	
       
   145 	}
       
   146 
       
   147 // FOTA end
       
   148 
       
   149 // ---------------------------------------------------------
       
   150 // CNSmlDMAgent::SetGenericAlertL()
       
   151 //	Appends a Generic Alert to the internal list. 
       
   152 //	The alert itself is sent in the later phase of the dm session.
       
   153 //	@param aFwMgmtUri. The uri which has been used in the exe command whose final result is 
       
   154 //	about to be reported.
       
   155 //	@param aMetaType. Meta/Type that should be used in the alert.
       
   156 //	@param aMetaFormat. Meta/Format that should be used in the alert.
       
   157 //	@param aFinalResult. The final result value which is reported to remote server.
       
   158 //	@param aCorrelator. Correlator value used in the original exec command. 
       
   159 // ---------------------------------------------------------
       
   160 //
       
   161 void CNSmlDMAgent::SetGenericAlertL ( const TDesC8& aCorrelator, const RArray<CNSmlDMAlertItem>& aItemList  )
       
   162     {
       
   163     CNSmlDMGenericAlert* genAlert = CNSmlDMGenericAlert::NewL ( aCorrelator, aItemList );
       
   164     iGenAlerts.AppendL ( genAlert );	
       
   165     }
       
   166 
       
   167 //
       
   168 //  Private member functions
       
   169 //
       
   170 // ---------------------------------------------------------
       
   171 // CNSmlDMAgent::CNSmlDMAgent()
       
   172 // Constructor
       
   173 // ---------------------------------------------------------
       
   174 //
       
   175 CNSmlDMAgent::CNSmlDMAgent()
       
   176 	{
       
   177 	}
       
   178 // ---------------------------------------------------------
       
   179 // CNSmlDMAgent::ConstructL()
       
   180 // Two-way construction. Constructor may leave in EPOC.
       
   181 // ---------------------------------------------------------
       
   182 //
       
   183 void CNSmlDMAgent::ConstructL( MSyncMLProgressObserver* aDMObserver )
       
   184     {
       
   185 	CNSmlAgentBase::ConstructL();
       
   186 	iDMObserver = aDMObserver;
       
   187 	iError = new (ELeave) TNSmlDMError;
       
   188 	iSyncMLMIMEType = KNSmlDMAgentMIMEType().AllocL();
       
   189 	iResultAlertCode = KNSmlDMAgentResultAlert().AllocL();
       
   190 	iNextMessageCode = KNSmlDMAgentNextMessage().AllocL();
       
   191 	//Session Abort Alert is used because there is no "End of Data Alert" in DM specs
       
   192 	iEndOfDataAlertCode = KNSmlDMAgentSessionAbortAlert().AllocL(); 
       
   193 	iSessionAbortAlertCode = KNSmlDMAgentSessionAbortAlert().AllocL();
       
   194 	FeatureManager::InitializeLibL();
       
   195 	iNewIapId = KErrNotFound;
       
   196 
       
   197 	}
       
   198 
       
   199 // ---------------------------------------------------------
       
   200 // CNSmlDMAgent::SupportedAlert()
       
   201 // Check from code if supported Alert  
       
   202 // ---------------------------------------------------------
       
   203 TBool CNSmlDMAgent::SupportedAlert( const TDesC8& aAlertCode ) const
       
   204 {
       
   205 if ( aAlertCode == KNSmlDMAgentServerInitAlert ||
       
   206         aAlertCode == KNSmlDMAgentClientInitAlert ||
       
   207         aAlertCode == KNSmlDMAgentResultAlert ||
       
   208         aAlertCode == KNSmlDMAgentNextMessage ||
       
   209         aAlertCode == KNSmlDMAgentSessionAbortAlert ||
       
   210         aAlertCode == KNSmlDMAgentDisplayAlert ||
       
   211         aAlertCode == KNSmlDMAgentContinueOrAbortAlert 
       
   212 		 || aAlertCode == KNSmlDMAgentUserInputAlert 
       
   213 		 || aAlertCode == KNSmlDMAgentSingleChoiceAlert
       
   214 		 || aAlertCode == KNSmlDMAgentMultipleChoiceAlert )
       
   215 		{
       
   216 		return ETrue;
       
   217 		}
       
   218 	return EFalse;
       
   219 	}
       
   220 //
       
   221 //  Server Modifications state functions 
       
   222 //
       
   223 // ---------------------------------------------------------
       
   224 // CNSmlDMAgent::ServerModificationsStateL(()
       
   225 // Main state function to read both Initialization from server client package
       
   226 // and Further server management operations package  
       
   227 // ---------------------------------------------------------
       
   228 void CNSmlDMAgent::ServerModificationsStateL()
       
   229     {
       
   230     _DBG_FILE(_S8("CNSmlDMAgent::ServerModificationsStateL begins"));
       
   231 
       
   232     // FOTA
       
   233     if ( !iChallengeRequest )
       
   234         {
       
   235         if ( iResetGenAlerts )
       
   236             {
       
   237             // Forward information to FOTA adapter if 1.1.2 otherwise any adapter 
       
   238             //which has sent GenAlert should be informed
       
   239             // It can now mark GenAlerts sent.
       
   240             if(!FeatureManager::FeatureSupported( KFeatureIdSyncMlDm112  ))
       
   241                 {
       
   242                 for ( TInt i(0); i<iGenAlerts.Count(); i++ )
       
   243                     {
       
   244                     if(iGenAlerts[i]->FinalResult() != 0)
       
   245                         {
       
   246                         iSyncMLCmds->MarkGenAlertsSentL(*iGenAlerts[i]->FwMgmtUri());    											
       
   247                         if(iGenAlerts[i]->FwMgmtUri()->Des().Find(KNSmlDMFotaNode)!=KErrNotFound)
       
   248                             {
       
   249                             iResetUserInitAlert= ETrue;	
       
   250                             }
       
   251                         }
       
   252                     else
       
   253                         {
       
   254                         // Generic alerts could be present for other adapters like SCOMO
       
   255                         iSyncMLCmds->MarkGenAlertsSentL(*(*iGenAlerts[i]->DataItem())[0].iSource);
       
   256                         }
       
   257                     }
       
   258                 }
       
   259             else
       
   260                 {
       
   261                 iSyncMLCmds->MarkGenAlertsSentL();		
       
   262                 }
       
   263             iResetGenAlerts = EFalse;
       
   264             iGenAlerts.ResetAndDestroy();
       
   265             }
       
   266         if ( iResetUserInitAlert )
       
   267             {
       
   268             iSyncMLCmds->ResetGenericUserAlertL();
       
   269             iResetUserInitAlert = EFalse;
       
   270             }
       
   271         }
       
   272     // FOTA end	
       
   273 
       
   274     if ( !iSyncMLCmds->ProcessReceivedDataL() )
       
   275         {
       
   276         DBG_FILE(_S8("CNSmlDMAgent::ServerModificationsStateL end of Data"));
       
   277         if ( iCurrServerModState == EServerModMessageReceived )
       
   278             {
       
   279             iBusyStatusReceived = iSyncMLCmds->BusyStatus();
       
   280             }
       
   281         else
       
   282             {
       
   283             if ( iCurrServerModState != EServerModPackageReceived )
       
   284                 {
       
   285                 User::Leave( TNSmlError::ESmlIncompleteMessage );
       
   286                 }
       
   287             }
       
   288         iSyncMLCmds->EndOfServerMessageL();
       
   289         NextMainStateL();
       
   290         }
       
   291     //DBG_FILE(_S8("CNSmlDMAgent::ServerModificationsStateL ends"));
       
   292     }
       
   293 // ---------------------------------------------------------
       
   294 // CNSmlDMAgent::ServerModGetCmdStateL()
       
   295 // 
       
   296 // ---------------------------------------------------------
       
   297 void CNSmlDMAgent::ServerModGetCmdStateL( SmlGet_t* aContent )
       
   298 	{
       
   299 	DBG_FILE(_S8("CNSmlDMAgent::ServerModGetCmdStateL begins"));
       
   300 	if ( iCurrServerModState != EServerModWaitingCommands )
       
   301 		{
       
   302 		User::Leave( TNSmlError::ESmlUnexpectedCmd ); 
       
   303 		}
       
   304 	iSyncMLCmds->ProcessGetCmdL( aContent );	
       
   305 	DBG_FILE(_S8("CNSmlDMAgent::ServerModGetCmdStateL ends"));
       
   306 	}
       
   307 
       
   308 // ---------------------------------------------------------
       
   309 // CNSmlDMAgent::ServerModStartAtomicStateL()
       
   310 // 
       
   311 // ---------------------------------------------------------
       
   312 void CNSmlDMAgent::ServerModStartAtomicStateL( SmlAtomic_t* aContent )
       
   313 	{
       
   314 	DBG_FILE(_S8("CNSmlDMAgent::ServerModStartAtomicStateL begins"));
       
   315 	if ( iCurrServerModState != EServerModWaitingCommands )
       
   316 		{
       
   317 		User::Leave( TNSmlError::ESmlUnexpectedCmd ); 
       
   318 		}
       
   319 	if ( iNestedAtomicsCount > 0 )
       
   320 		{
       
   321 		iSyncMLCmds->StatusToUnsupportedCommandL( ETrue, KNSmlAgentAtomic, aContent->cmdID, aContent->flags );
       
   322 		WriteWarningL( TNSmlError::ESmlUnexpectedCmd );
       
   323 		}
       
   324 	else
       
   325 		{
       
   326 		iSyncMLCmds->ProcessAtomicL( aContent );
       
   327 		}
       
   328 	++iNestedAtomicsCount;
       
   329 	DBG_FILE(_S8("CNSmlDMAgent::ServerModStartAtomicStateL ends"));
       
   330 	}
       
   331 	
       
   332 
       
   333 // ---------------------------------------------------------
       
   334 // CNSmlDMAgent::ServerModEndAtomicStateL()
       
   335 // 
       
   336 // ---------------------------------------------------------
       
   337 void CNSmlDMAgent::ServerModEndAtomicStateL()
       
   338 	{
       
   339 	DBG_FILE(_S8("CNSmlDMAgent::ServerModEndAtomicStateL begins"));
       
   340 	--iNestedAtomicsCount;
       
   341 	if ( iNestedAtomicsCount == 0 )
       
   342 		{
       
   343 		iSyncMLCmds->ProcessEndAtomicL();
       
   344 		}
       
   345 	DBG_FILE(_S8("CNSmlDMAgent::ServerModEndAtomicStateL ends"));
       
   346 	}
       
   347 
       
   348 // ---------------------------------------------------------
       
   349 // CNSmlDMAgent::ServerModStartSequenceStateL()
       
   350 // 
       
   351 // ---------------------------------------------------------
       
   352 void CNSmlDMAgent::ServerModStartSequenceStateL( SmlSequence_t* aContent )
       
   353 	{
       
   354 	DBG_FILE(_S8("CNSmlDMAgent::ServerModStartSequenceStateL begins"));
       
   355 	if ( iCurrServerModState != EServerModWaitingCommands )
       
   356 		{
       
   357 		User::Leave( TNSmlError::ESmlUnexpectedCmd ); 
       
   358 		}
       
   359 	else
       
   360 		{
       
   361 		iSyncMLCmds->ProcessSequenceL( aContent );
       
   362 		}
       
   363 	DBG_FILE(_S8("CNSmlDMAgent::ServerModStartSequenceStateL ends"));
       
   364 	}
       
   365 
       
   366 // ---------------------------------------------------------
       
   367 // CNSmlDMAgent::ServerModEndSequenceStateL()
       
   368 // 
       
   369 // ---------------------------------------------------------
       
   370 void CNSmlDMAgent::ServerModEndSequenceStateL()
       
   371 	{
       
   372 	DBG_FILE(_S8("CNSmlDMAgent::ServerModEndSequenceStateL begins"));
       
   373 	iSyncMLCmds->ProcessEndSequence();
       
   374 	DBG_FILE(_S8("CNSmlDMAgent::ServerModEndSequenceStateL ends"));
       
   375 	}
       
   376 
       
   377 //
       
   378 //  Finishing state functions 
       
   379 //
       
   380 // ---------------------------------------------------------
       
   381 // CNSmlDMAgent::FinishingStateL()
       
   382 // Finishing functions in the end of sync
       
   383 // ---------------------------------------------------------
       
   384 void CNSmlDMAgent::FinishingStateL()
       
   385 	{
       
   386 	DBG_FILE(_S8("CNSmlDMAgent::FinishingStateL begins"));
       
   387 	FinishL();  
       
   388 	TRAP_IGNORE( FinalizeSyncLogL());
       
   389 	DBG_FILE(_S8("CNSmlDMAgent::FinishingStateL ends"));
       
   390 	}
       
   391 
       
   392 // ---------------------------------------------------------
       
   393 // CNSmlDMAgent::ReadSettingsL()
       
   394 // Reads settings for the DM session
       
   395 // ---------------------------------------------------------
       
   396 void CNSmlDMAgent::ReadSettingsL()
       
   397 	{
       
   398 	CNSmlDMProfile* profile = CNSmlDMSettings::NewLC()->ProfileL( iProfileID );
       
   399 	if ( !profile )
       
   400 		{
       
   401 		User::Leave( TNSmlError::ESmlErrorInSettings );
       
   402 		}
       
   403 	else
       
   404 		{
       
   405 		CleanupStack::PushL( profile );
       
   406 	
       
   407 		if( iNewIapId > KErrNotFound )
       
   408 			{
       
   409 			DBG_FILE_CODE(iIAPId,_S8("CNSmlDMAgent::ReadSettingsL not using profiles IAP:"));	
       
   410 			iIAPId = iNewIapId;
       
   411 			}
       
   412 		else
       
   413 			{
       
   414 			DBG_FILE_CODE(iIAPId,_S8("CNSmlDMAgent::ReadSettingsL using profiles IAP"));	
       
   415     		iIAPId = profile->IntValue( EDMProfileIAPId );
       
   416 			}
       
   417 		iIAPIdArray = new (ELeave) CArrayFixFlat<TUint32>(4);
       
   418 		iIAPIdArray->AppendL( iIAPId );
       
   419 
       
   420 		
       
   421 		iSyncMLUserName = profile->StrValue( EDMProfileServerUsername ).AllocL();
       
   422 		iSyncMLPassword = profile->StrValue( EDMProfileServerPassword ).AllocL();
       
   423 		
       
   424 		iSyncHTTPAuthUsed = profile->IntValue( EDMProfileHttpAuthUsed );	
       
   425 		iSyncHTTPAuthUserName = profile->StrValue( EDMProfileHttpAuthUsername ).AllocL();	
       
   426 		iSyncHTTPAuthPassword = profile->StrValue( EDMProfileHttpAuthPassword ).AllocL();	
       
   427 		
       
   428 		if ( iMediumType ==  KUidNSmlMediumTypeInternet )
       
   429 			{
       
   430 			iSyncServer= CNSmlURI::NewL( profile->StrValue( EDMProfileServerURL ), ETrue );
       
   431 			}
       
   432 		else
       
   433 			{
       
   434 			iSyncServer= CNSmlURI::NewL( profile->StrValue( EDMProfileServerURL ), EFalse );
       
   435 			}
       
   436 		iDMServerId = profile->StrValue( EDMProfileServerId ).AllocL();
       
   437 		iDMServerPassword = profile->StrValue( EDMProfileClientPassword ).AllocL();
       
   438 		iDMServerSessionId = profile->IntValue( EDMProfileSessionId );
       
   439 		CleanupStack::PopAndDestroy( 2 ); //profile, settings
       
   440 		}
       
   441 	}
       
   442 
       
   443 
       
   444 // ---------------------------------------------------------
       
   445 // CNSmlDMAgent::InitSyncLogL()
       
   446 // Sync Log (shown In UI) initilisation in the beginning of sync
       
   447 // ---------------------------------------------------------
       
   448 void CNSmlDMAgent::InitSyncLogL()
       
   449 	{
       
   450 	CNSmlDMProfile* profile = CNSmlDMSettings::NewLC()->ProfileL( iProfileID );
       
   451 	if ( profile )
       
   452 		{
       
   453 		CleanupStack::PushL( profile );
       
   454 		TTime now;
       
   455 		now.HomeTime();
       
   456 		CleanupStack::PopAndDestroy(2); //profile, settings
       
   457 		}
       
   458 	else
       
   459 		{
       
   460 		CleanupStack::PopAndDestroy(); //settings
       
   461 		}
       
   462 	}
       
   463 // ---------------------------------------------------------
       
   464 // CNSmlDMAgent::GetSessionIDL()
       
   465 // Gets SessionID from Agent Log
       
   466 // ---------------------------------------------------------
       
   467 TInt CNSmlDMAgent::GetSessionIDL()
       
   468 	{
       
   469 	CNSmlDMProfile* profile = CNSmlDMSettings::NewLC()->ProfileL( iProfileID );
       
   470 	TInt sessionID = 0;
       
   471 	if ( !profile )
       
   472 		{
       
   473 		User::Leave( TNSmlError::ESmlErrorInSettings );
       
   474 		}
       
   475 	else
       
   476 		{
       
   477 		CleanupStack::PushL( profile );
       
   478 		sessionID = profile->IntValue( EDMProfileSessionId );  
       
   479 		CleanupStack::PopAndDestroy( 2 ); //profile, settings
       
   480 		}
       
   481 	return sessionID;
       
   482 	}
       
   483 // ---------------------------------------------------------
       
   484 // CNSmlDMAgent::UpdateSessionIDL()
       
   485 // Updates SessionID in Agent Log
       
   486 // ---------------------------------------------------------
       
   487 void CNSmlDMAgent::UpdateSessionIDL( TInt aSessionID )
       
   488 	{
       
   489 	CNSmlDMProfile* profile = CNSmlDMSettings::NewLC()->ProfileL( iProfileID );
       
   490 	if ( !profile )
       
   491 		{
       
   492 		User::Leave( TNSmlError::ESmlErrorInSettings );
       
   493 		}
       
   494 	else
       
   495 		{
       
   496 		CleanupStack::PushL( profile );
       
   497 		profile->SetIntValue( EDMProfileSessionId, aSessionID );
       
   498 		profile->SaveL();
       
   499 		CleanupStack::PopAndDestroy( 2 ); //profile, settings
       
   500 		}
       
   501 	}
       
   502 
       
   503 // ---------------------------------------------------------
       
   504 // CNSmlDMAgent::FinishL()
       
   505 // 
       
   506 // ---------------------------------------------------------
       
   507 void CNSmlDMAgent::FinishL()
       
   508 	{
       
   509 	
       
   510 	iSyncMLCmds->WriteMappingInfoToDbL();
       
   511 	iSyncMLCmds->DisconnectFromOtherServers();
       
   512 	if ( Interrupted() )
       
   513 		{
       
   514 		iDMObserver->OnSyncMLSyncError(MSyncMLProgressObserver::ESmlFatalError,iError->SyncLogErrorCode(),0,0,0);
       
   515 		User::RequestComplete( iCallerStatus, iError->SyncLogErrorCode() );
       
   516 		}
       
   517 	else
       
   518 		{
       
   519 		iDMObserver->OnSyncMLSyncProgress(MSyncMLProgressObserver::ESmlCompleted,0,0);
       
   520 		User::RequestComplete( iCallerStatus, KErrNone );
       
   521 		}
       
   522 	iEnd = ETrue;
       
   523 	}
       
   524 
       
   525 // ---------------------------------------------------------
       
   526 // CNSmlDMAgent::InterruptedImmediately()
       
   527 // 
       
   528 // ---------------------------------------------------------
       
   529 TBool CNSmlDMAgent::InterruptedImmediately()
       
   530 	{
       
   531 	return iDMImmediatelyInterruption;
       
   532 	}
       
   533 
       
   534 //
       
   535 // Callback functions implementation
       
   536 //
       
   537 // ---------------------------------------------------------
       
   538 // CNSmlDMAgent::smlAlertCmdFuncL
       
   539 // Alert command from server.  
       
   540 // ---------------------------------------------------------
       
   541 Ret_t CNSmlDMAgent::smlAlertCmdFuncL( SmlAlertPtr_t aContent )
       
   542 	{
       
   543 	iCallbackCalled = ETrue;
       
   544 	if ( !SupportedAlert( iSyncMLCmds->AlertCode( aContent ) ) )
       
   545 		{
       
   546 		iSyncMLCmds->StatusToUnsupportedCommandL( EFalse, KNSmlAgentAlert, aContent->cmdID, aContent->flags );
       
   547 		}
       
   548 	else
       
   549 		{
       
   550 		switch ( iCurrMainState )
       
   551 			{
       
   552 			case EServerAlertNext:
       
   553 				ServerAlertNextAlertCmdStateL( aContent );
       
   554 				break;
       
   555 			case EServerModifications:
       
   556 				ServerModAlertCmdStateL( aContent );
       
   557 				break;
       
   558 			default: 
       
   559 				break;
       
   560 			}
       
   561 		}
       
   562 	return 0;
       
   563 	}
       
   564 
       
   565 // ---------------------------------------------------------
       
   566 // CNSmlAgentBase::smlExecCmdFunc
       
   567 // Exec command from server
       
   568 // ---------------------------------------------------------
       
   569 Ret_t CNSmlDMAgent::smlExecCmdFuncL( SmlExecPtr_t aContent )
       
   570 	{
       
   571 	iCallbackCalled = ETrue;
       
   572 	switch ( iCurrMainState )
       
   573 		{
       
   574 		case EServerModifications:
       
   575 			// FOTA
       
   576 			ServerModUpdatesStateL ( aContent );
       
   577 			// FOTA end
       
   578 			break;
       
   579 		default:
       
   580 			iSyncMLCmds->StatusToUnsupportedCommandL( ETrue, KNSmlAgentExec, aContent->cmdID, aContent->flags );
       
   581 			break;
       
   582 		}
       
   583 	return 0;
       
   584 	}
       
   585 
       
   586 
       
   587 // ---------------------------------------------------------
       
   588 // CNSmlAgentBase::smlCopyCmdFunc
       
   589 // Exec command from server
       
   590 // ---------------------------------------------------------
       
   591 Ret_t CNSmlDMAgent::smlCopyCmdFuncL( SmlCopyPtr_t aContent )
       
   592 	{
       
   593 	iCallbackCalled = ETrue;
       
   594 	switch ( iCurrMainState )
       
   595 		{
       
   596 		case EServerModifications:
       
   597 			ServerModUpdatesStateL( KNSmlAgentCopy(), aContent );
       
   598 			break;
       
   599 		default:
       
   600 			iSyncMLCmds->StatusToUnsupportedCommandL( ETrue, KNSmlAgentCopy, aContent->cmdID, aContent->flags );
       
   601 			break;
       
   602 		}
       
   603 	return 0;
       
   604 	}
       
   605 
       
   606 // ---------------------------------------------------------
       
   607 // CNSmlDMAgent::smlGetCmdFuncL
       
   608 // Get command from server
       
   609 // ---------------------------------------------------------
       
   610 Ret_t CNSmlDMAgent::smlGetCmdFuncL( SmlGetPtr_t aContent )
       
   611 	{
       
   612 	iCallbackCalled = ETrue;
       
   613 	switch ( iCurrMainState )
       
   614 		{
       
   615 		case EServerModifications:
       
   616 			ServerModGetCmdStateL( aContent );
       
   617 			break;
       
   618 		default:
       
   619 			iSyncMLCmds->StatusToUnsupportedCommandL( ETrue, KNSmlAgentGet, aContent->cmdID, aContent->flags );
       
   620 			break;
       
   621 		}
       
   622 	return 0;
       
   623 	}
       
   624 // ---------------------------------------------------------
       
   625 // CNSmlDMAgent::smlPutCmdFuncL
       
   626 // Put command from server
       
   627 // ---------------------------------------------------------
       
   628 Ret_t CNSmlDMAgent::smlPutCmdFuncL( SmlPutPtr_t aContent )
       
   629 	{
       
   630 	iCallbackCalled = ETrue;
       
   631 	iSyncMLCmds->StatusToUnsupportedCommandL( ETrue, KNSmlAgentPut, aContent->cmdID, aContent->flags );
       
   632 	return 0;
       
   633 	}
       
   634 // ---------------------------------------------------------
       
   635 // CNSmlDMAgent::smlResultsCmdFuncL
       
   636 // Results command from server
       
   637 // ---------------------------------------------------------
       
   638 Ret_t CNSmlDMAgent::smlResultsCmdFuncL( SmlResultsPtr_t aContent )
       
   639 	{
       
   640 	iCallbackCalled = ETrue;
       
   641 	iSyncMLCmds->StatusToUnsupportedCommandL( ETrue, KNSmlAgentResults, aContent->cmdID );
       
   642 	return 0;
       
   643 	}
       
   644 // ---------------------------------------------------------
       
   645 // CNSmlDMAgent::smlStatusCmdFuncL
       
   646 // Status command from server
       
   647 // ---------------------------------------------------------
       
   648 Ret_t CNSmlDMAgent::smlStatusCmdFuncL( SmlStatusPtr_t aContent )
       
   649 	{
       
   650 	iCallbackCalled = ETrue;
       
   651 	switch ( iCurrMainState )
       
   652 		{
       
   653 		case EServerModifications:
       
   654 			ServerModStatusCmdStateL( aContent );
       
   655 			break;
       
   656 		case EServerAlertNext:
       
   657 			ServerAlertNextStatusCmdStateL( aContent );
       
   658 			break;
       
   659 		default:
       
   660 			break;
       
   661 		}
       
   662 	return 0;
       
   663 	}
       
   664 
       
   665 // ---------------------------------------------------------
       
   666 // CNSmlDMAgent::smlStartMessageFuncL
       
   667 // SyncHdr from server
       
   668 // ---------------------------------------------------------
       
   669 Ret_t CNSmlDMAgent::smlStartMessageFuncL( SmlSyncHdrPtr_t aContent )
       
   670 	{
       
   671 	iCallbackCalled = ETrue;
       
   672 	switch ( iCurrMainState )
       
   673 		{
       
   674 		case EServerModifications:
       
   675 			ServerModStartMessageStateL( aContent );
       
   676 			break;
       
   677 		case EServerAlertNext:
       
   678 			ServerAlertNextStartMessageStateL( aContent );
       
   679 			break;
       
   680 		default:
       
   681 			break;
       
   682 		}
       
   683 	return 0;
       
   684 	}
       
   685 // ---------------------------------------------------------
       
   686 // CNSmlDMAgent::smlEndMessageFuncL
       
   687 // End of SyncML message was reached
       
   688 // ---------------------------------------------------------
       
   689 Ret_t CNSmlDMAgent::smlEndMessageFuncL( Boolean_t aFinal )
       
   690 	{
       
   691 	iCallbackCalled = ETrue;
       
   692 	switch ( iCurrMainState )
       
   693 		{
       
   694 		case EServerModifications:
       
   695 			ServerModEndMessageStateL( aFinal );
       
   696 			break;
       
   697 		case EServerAlertNext:
       
   698 			ServerAlertNextEndMessageStateL( aFinal );
       
   699 			break;
       
   700 		default:
       
   701 			User::Leave( TNSmlError::ESmlUnexpectedCmd );
       
   702 			break;
       
   703 		}
       
   704 	return 0;
       
   705 	}
       
   706 // ---------------------------------------------------------
       
   707 // CNSmlDMAgent::smlStartSyncFuncL
       
   708 // Sync command from server
       
   709 // ---------------------------------------------------------
       
   710 Ret_t CNSmlDMAgent::smlStartSyncFuncL( SmlSyncPtr_t aContent )
       
   711 	{
       
   712 	iCallbackCalled = ETrue;
       
   713 	iSyncMLCmds->StatusToUnsupportedCommandL( ETrue, KNSmlAgentSync, aContent->cmdID, aContent->flags );
       
   714 	WriteWarningL( TNSmlError::ESmlUnexpectedCmd );
       
   715 	return 0;
       
   716 	}
       
   717 // ---------------------------------------------------------
       
   718 // CNSmlDMAgent::smlEndSyncFuncL
       
   719 // End of Sync command was reached
       
   720 // ---------------------------------------------------------
       
   721 Ret_t CNSmlDMAgent::smlEndSyncFuncL()
       
   722 	{
       
   723 	iCallbackCalled = ETrue;
       
   724 	return 0;
       
   725 	}
       
   726 // ---------------------------------------------------------
       
   727 // CNSmlDMAgent::smlStartAtomicFuncL
       
   728 // Atomic command from server
       
   729 // ---------------------------------------------------------
       
   730 Ret_t CNSmlDMAgent::smlStartAtomicFuncL( SmlAtomicPtr_t aContent )
       
   731 	{
       
   732 	iCallbackCalled = ETrue;
       
   733 	switch ( iCurrMainState )
       
   734 		{
       
   735 		case EServerModifications:
       
   736 			ServerModStartAtomicStateL( aContent );
       
   737 			break;
       
   738 		default:
       
   739 			iSyncMLCmds->StatusToUnsupportedCommandL( ETrue, KNSmlAgentAtomic, aContent->cmdID, aContent->flags );
       
   740 			break;
       
   741 		}
       
   742 	
       
   743 	return 0;
       
   744 	}
       
   745 // ---------------------------------------------------------
       
   746 // CNSmlDMAgent::smlEndAtomicFuncL
       
   747 // End of Atomic command was reached
       
   748 // ---------------------------------------------------------
       
   749 Ret_t CNSmlDMAgent::smlEndAtomicFuncL()
       
   750 	{
       
   751 	iCallbackCalled = ETrue;
       
   752 	ServerModEndAtomicStateL();
       
   753 	return 0;
       
   754 	}
       
   755 
       
   756 // ---------------------------------------------------------
       
   757 // CNSmlDMAgent::smlStartSequenceFuncL
       
   758 // Sequence command from server
       
   759 // ---------------------------------------------------------
       
   760 Ret_t CNSmlDMAgent::smlStartSequenceFuncL( SmlSequencePtr_t aContent )
       
   761 	{
       
   762 	iCallbackCalled = ETrue;
       
   763 	switch ( iCurrMainState )
       
   764 		{
       
   765 		case EServerModifications:
       
   766 			ServerModStartSequenceStateL( aContent );
       
   767 			break;
       
   768 		default:
       
   769 			iSyncMLCmds->StatusToUnsupportedCommandL( ETrue, KNSmlAgentSequence, aContent->cmdID, aContent->flags );
       
   770 			break;
       
   771 		}
       
   772 	
       
   773 	return 0;
       
   774 	}
       
   775 // ---------------------------------------------------------
       
   776 // CNSmlDMAgent::smlEndSequenceFuncL
       
   777 // End of Sequence command was reached
       
   778 // ---------------------------------------------------------
       
   779 Ret_t CNSmlDMAgent::smlEndSequenceFuncL()
       
   780 	{
       
   781 	iCallbackCalled = ETrue;
       
   782 	ServerModEndSequenceStateL();
       
   783 	return 0;
       
   784 	}
       
   785 
       
   786 // ---------------------------------------------------------
       
   787 // CNSmlDMAgent::NavigateMainStateL()
       
   788 // Navigate according to state code
       
   789 // ---------------------------------------------------------
       
   790 void CNSmlDMAgent::NavigateMainStateL() 
       
   791     {
       
   792 #ifdef __CANCEL_TEST
       
   793 	iDMObserver->NotifyDisplayAlertL(KNullDesC,0,-99);
       
   794 #endif
       
   795     switch ( iCurrMainState )
       
   796 		{
       
   797 		case EBeginning: 
       
   798 if(FeatureManager::FeatureSupported(KFeatureIdSapPolicyManagement))
       
   799 {
       
   800 			if ( iCurrBeginningState == EBegin )
       
   801 			{
       
   802 				CheckForcedCertificateCheckStateL();
       
   803 			}
       
   804 }
       
   805 			BeginningStateL();
       
   806 			break;
       
   807 		case EClientInitialisation:
       
   808 			ClientInitialisationStateL();
       
   809 			break;
       
   810 		case EServerModifications:
       
   811 			ServerModificationsStateL();
       
   812 			break;
       
   813 		case EDataUpdateStatus:
       
   814 			DataUpdateStatusStateL();
       
   815 			break;
       
   816 		case EResultAlert:
       
   817 			ResultAlertStateL();
       
   818 			break;
       
   819 		case EClientAlertNext:
       
   820 			ClientAlertNextStateL();
       
   821 			break;
       
   822 		case EServerAlertNext:
       
   823 			ServerAlertNextStateL();
       
   824 			break;
       
   825 		case EFinishing:
       
   826 			FinishingStateL();
       
   827 			break;
       
   828 		case ESending:
       
   829 if(FeatureManager::FeatureSupported(KFeatureIdSapPolicyManagement))
       
   830 {
       
   831   //if loop commented for DM session When TARM enabled TSW:ARAA-73XHUM
       
   832 			//if ( !CheckCertificateL())
       
   833 			//{	
       
   834 				//Start sending state...
       
   835 				SendingStateL();
       
   836 			//}
       
   837 }
       
   838 else
       
   839 {
       
   840 			SendingStateL();
       
   841 }
       
   842 break;
       
   843 		case EReceiving:
       
   844 			ReceivingStateL();
       
   845 			break;
       
   846 		default:
       
   847 			break;
       
   848 		}
       
   849     }
       
   850 
       
   851 // ---------------------------------------------------------
       
   852 // CNSmlDMAgent::NextMainStateL()
       
   853 // Call a function which changes a state depending on type
       
   854 // 
       
   855 // ---------------------------------------------------------
       
   856 //
       
   857 void CNSmlDMAgent::NextMainStateL() 
       
   858 	{
       
   859 	if ( iCurrMainState == EReceiving )
       
   860 		{
       
   861 		iDMObserver->OnSyncMLSyncProgress(MSyncMLProgressObserver::ESmlReceivingServerCommands,0,0);
       
   862 		}
       
   863 	switch ( iCurrMainState )
       
   864 		{
       
   865 		case EBeginning:
       
   866 			if ( Interrupted() )
       
   867 				{
       
   868 				DBG_FILE(_S8("CNSmlDMAgent::NextMainStateL EFinishing"));
       
   869 				iCurrMainState = EFinishing;
       
   870 				} 
       
   871 			else
       
   872 				{
       
   873 				DBG_FILE(_S8("CNSmlDMAgent::NextMainStateL EClientInitialisation"));
       
   874 				iCurrMainState = EClientInitialisation;
       
   875 				}
       
   876 			break;
       
   877 		case EClientInitialisation:
       
   878 			iPrevSyncState = iCurrMainState;
       
   879 			DBG_FILE(_S8("CNSmlDMAgent::NextMainStateL ESending"));
       
   880 			iCurrMainState = ESending;
       
   881 			break;
       
   882 		case EServerModifications:
       
   883 			iPrevSyncState = iCurrMainState;
       
   884 			if ( InterruptedImmediately() ) // immediately interruption needed?
       
   885 				{
       
   886 				DBG_FILE(_S8("CNSmlDMAgent::NextMainState EFinishing"));
       
   887 				iCurrMainState = EFinishing;
       
   888 				}
       
   889 			else
       
   890 				{
       
   891 				if ( iBusyStatusReceived )
       
   892 					{
       
   893 					DBG_FILE(_S8("CNSmlDMAgent::NextMainStateL EResultAlert"));
       
   894 					iCurrMainState = EResultAlert;
       
   895 					}
       
   896 				else
       
   897 					{					
       
   898 					if ( iChallengeRequest )
       
   899 						{
       
   900 						DBG_FILE(_S8("CNSmlDMAgent::NextMainStateL EClientInitialisation"));
       
   901 						iCurrMainState = EClientInitialisation;
       
   902 						}
       
   903 					else
       
   904 						{
       
   905 						if ( iCurrServerModState == EServerModMessageReceived )
       
   906 							{
       
   907 							DBG_FILE(_S8("CNSmlDMAgent::NextMainStateL EClientAlertNext"));
       
   908 							iCurrMainState = EClientAlertNext;
       
   909 							}
       
   910 						else
       
   911 							{
       
   912 							DBG_FILE(_S8("CNSmlDMAgent::NextMainStateL EDataUpdateStatus"));
       
   913 							iCurrMainState = EDataUpdateStatus;
       
   914 							}
       
   915 						}
       
   916 					}
       
   917 				}
       
   918 			break;
       
   919 		case EDataUpdateStatus:
       
   920 			iPrevSyncState = iCurrMainState;
       
   921 			if ( iStatusPackage )
       
   922 				{
       
   923 				DBG_FILE(_S8("CNSmlDMAgent::NextMainStateL ESending"));
       
   924 				iCurrMainState = ESending;
       
   925 				}
       
   926 			else
       
   927 				{
       
   928 				DBG_FILE(_S8("CNSmlDMAgent::NextMainStateL EFinishing"));
       
   929 				iCurrMainState = EFinishing;
       
   930 				}
       
   931 			break;
       
   932 		case EResultAlert:
       
   933 			DBG_FILE(_S8("CNSmlDMAgent::NextMainStateL ESending"));
       
   934 			iCurrMainState = ESending;
       
   935 			break;
       
   936 		case EClientAlertNext:
       
   937 			DBG_FILE(_S8("CNSmlDMAgent::NextMainStateL ESending"));
       
   938 			iCurrMainState = ESending;
       
   939 			break;
       
   940 		case EServerAlertNext:
       
   941 			if ( Interrupted() )
       
   942 				{
       
   943 				DBG_FILE(_S8("CNSmlDMAgent::NextMainStateL EFinishing"));
       
   944 				iCurrMainState = EFinishing;
       
   945 				}
       
   946 			else
       
   947 				{
       
   948 				DBG_FILE(_S8("CNSmlDMAgent::NextMainStateL Previous State"));
       
   949 				iCurrMainState = iPrevSyncState;
       
   950 				}
       
   951 			break;
       
   952 		case ESending:
       
   953 			if ( Interrupted() ) 
       
   954 				{
       
   955 				DBG_FILE(_S8("CNSmlDMAgent::NextMainStateL EFinishing"));
       
   956 				iCurrMainState = EFinishing;
       
   957 				}
       
   958 			else
       
   959 				{
       
   960 				DBG_FILE(_S8("CNSmlDMAgent::NextMainStateL EReceiving"));
       
   961 				iCurrMainState = EReceiving;
       
   962 				}
       
   963 			break;
       
   964 		case EReceiving:
       
   965 			if ( ( !iFinalMessageFromClient ) && ( iPrevSyncState == EDataUpdateStatus ) )
       
   966 				{
       
   967 				DBG_FILE(_S8("CNSmlDMAgent::NextMainStateL EServerAlertNext"));
       
   968 				iCurrMainState = EServerAlertNext;
       
   969 				}
       
   970 			else
       
   971 				{
       
   972 				DBG_FILE(_S8("CNSmlDMAgent::NextMainStateL EServerModifications"));
       
   973 				iCurrMainState = EServerModifications; 
       
   974 				}	
       
   975 			break;
       
   976 		default:
       
   977 			break;
       
   978 		}
       
   979 	InitialiseSubStates();
       
   980 	if ( iCurrMainState == ESending )
       
   981 		{
       
   982 		iDMObserver->OnSyncMLSyncProgress(MSyncMLProgressObserver::ESmlSendingModificationsToServer,0,0);
       
   983 		}
       
   984 	}
       
   985 
       
   986 // ---------------------------------------------------------
       
   987 // CNSmlDMAgent::InitialiseSubStates()
       
   988 // 
       
   989 // ---------------------------------------------------------
       
   990 //
       
   991 void CNSmlDMAgent::InitialiseSubStates()
       
   992 	{
       
   993 	iCurrBeginningState = EBegin;
       
   994 	iCurrClientInitState = EInitStartingMessage;
       
   995 	iCurrResultAlertState = EResultAlertStartingMessage;
       
   996 	iCurrServerModState = EServerModWaitingStartMessage;
       
   997 	iCurrDataUpdateStatusState = EStatusStartingMessage;
       
   998 	iCurrResultAlertState = EResultAlertStartingMessage;
       
   999 	iCurrClientAlertNextState = EClientAlertNextStartingMessage;
       
  1000 	iCurrServerAlertNextState = EServerAlertNextWaitingStartMessage;
       
  1001 	}
       
  1002 	
       
  1003 	
       
  1004 
       
  1005 TBool CNSmlDMAgent::CheckCertificateL()
       
  1006 {
       
  1007 	if(!FeatureManager::FeatureSupported(KFeatureIdSapPolicyManagement))
       
  1008 	{
       
  1009 		User::Leave(KErrNotSupported);
       
  1010 	}
       
  1011 	TBool retVal = EFalse;
       
  1012 
       
  1013 	//do certificate check...
       
  1014 	if ( iForcedCertificateCheck && iCommandIssued)
       
  1015 	{
       
  1016 		//get session certificate via CentRep interface
       
  1017 		CRepository * rep = 0;
       
  1018 		TRAPD( err, rep = CRepository::NewL( KCRUidPolicyManagementUtilInternalKeys ))
       
  1019 		TCertInfo info;
       
  1020 		
       
  1021 		if( err == KErrNone)
       
  1022 		{
       
  1023 			//read certificate
       
  1024 			TPckg<TCertInfo> certPck( info);
       
  1025 			err = rep->Get( KSyncMLSessionCertificate, certPck);
       
  1026 			delete rep;
       
  1027 		}
       
  1028 
       
  1029 		TRole role = ENoRole;
       
  1030 		
       
  1031 		if ( err == KErrNone)
       
  1032 		{
       
  1033 			//connect to policy engine
       
  1034 			RPolicyEngine policyEngine;
       
  1035 			err = policyEngine.Connect();
       
  1036 		
       
  1037 			RPolicyManagement policyManagement;
       
  1038 			err = policyManagement.Open( policyEngine);
       
  1039 
       
  1040 			//check certificate role
       
  1041 			policyManagement.CertificateRole( info, role);
       
  1042 			
       
  1043 			policyManagement.Close();
       
  1044 			policyEngine.Close();
       
  1045 		}
       
  1046 		
       
  1047 		if ( role == ENoRole)
       
  1048 		{	
       
  1049 			//if session doesn't have certificate or certificate doesn't have any role 
       
  1050 			//user acceptance is needed to resume the connection
       
  1051 			UserConfirmateConnectionL();	
       
  1052 			retVal = ETrue;
       
  1053 		}
       
  1054 
       
  1055 		iForcedCertificateCheck = EFalse;
       
  1056 	}
       
  1057 	
       
  1058 	return retVal;
       
  1059 }
       
  1060 
       
  1061 void CNSmlDMAgent::UserConfirmateConnectionL()
       
  1062 {
       
  1063 	if(!FeatureManager::FeatureSupported(KFeatureIdSapPolicyManagement))
       
  1064 	{
       
  1065 		User::Leave(KErrNotSupported);
       
  1066 	}
       
  1067 	delete iNotifierObserver;
       
  1068 	iNotifierObserver = 0;
       
  1069 	iNotifierObserver = new (ELeave) CNSmlAgentNotifierObserver( iStatus, 100);
       
  1070 	iNotifierObserver->ConnectToNotifierL( this);
       
  1071 	
       
  1072 }
       
  1073 
       
  1074 
       
  1075 void CNSmlDMAgent::CheckForcedCertificateCheckStateL()
       
  1076 {
       
  1077 	if(!FeatureManager::FeatureSupported(KFeatureIdSapPolicyManagement))
       
  1078 	{
       
  1079 		User::Leave(KErrNotSupported);
       
  1080 	}
       
  1081 	CRepository * rep = CRepository::NewLC( KCRUidPolicyManagementUtilInternalKeys );
       
  1082 
       
  1083 	User::LeaveIfError( rep->Get( KSyncMLForcedCertificateCheckKey, iForcedCertificateCheck));
       
  1084 	CleanupStack::PopAndDestroy( rep);
       
  1085 }
       
  1086 
       
  1087 
       
  1088 	
       
  1089 	
       
  1090 //
       
  1091 // Beginning state functions 
       
  1092 //
       
  1093 //
       
  1094 // ---------------------------------------------------------
       
  1095 // CNSmlDMAgent::BeginStateL()
       
  1096 // 
       
  1097 // ---------------------------------------------------------
       
  1098 void CNSmlDMAgent::BeginStateL() 
       
  1099 	{
       
  1100 	iDMObserver->OnSyncMLSyncProgress(MSyncMLProgressObserver::ESmlConnecting,0,0);
       
  1101 
       
  1102 	DBG_FILE(_S8("CNSmlDMAgent::BeginStateL Before InitSyncLogL"));
       
  1103 	InitSyncLogL();
       
  1104 	if ( iSyncInitiation == EServerAlerted )
       
  1105 		{
       
  1106 		DBG_FILE(_S8("CNSmlDMAgent::BeginStateL Before ParseTriggerMessageL"));
       
  1107 		}
       
  1108 	DBG_FILE(_S8("CNSmlDMAgent::BeginStateL Before ReadSettingsL"));
       
  1109 	ReadSettingsL();
       
  1110 	DBG_FILE(_S8("CNSmlDMAgent::BeginStateL Before CreateSyncmlCmdsInstanceL"));
       
  1111 	CreateSyncmlCmdsInstanceL();
       
  1112 	iCurrBeginningState = EBeginConnectingToServer;
       
  1113 	}
       
  1114 
       
  1115 //
       
  1116 //  Client Initialisation state functions 
       
  1117 //
       
  1118 
       
  1119 // ---------------------------------------------------------
       
  1120 // CNSmlDMAgent::InitStartingMessageStateL()
       
  1121 // 
       
  1122 // ---------------------------------------------------------
       
  1123 void CNSmlDMAgent::InitStartingMessageStateL()
       
  1124 	{
       
  1125 	InitStartingMessageStateBaseL();
       
  1126 	}
       
  1127 // ---------------------------------------------------------
       
  1128 // CNSmlDMAgent::InitAlertingStateL()
       
  1129 // Makes initialisation Alert command.
       
  1130 // May also generate one or many generic alerts and 
       
  1131 // a generic user alert (i.e. user request to update firmware)
       
  1132 // ---------------------------------------------------------
       
  1133 void CNSmlDMAgent::InitAlertingStateL()
       
  1134 	{
       
  1135 	if ( !Interrupted() )
       
  1136 		{
       
  1137 		TBuf8<4> alertCode;
       
  1138 		if ( iSyncInitiation == EClientInitiated )
       
  1139 			{
       
  1140 			alertCode = KNSmlDMAgentClientInitAlert;
       
  1141 			}
       
  1142 		else
       
  1143 			{
       
  1144 			alertCode =	KNSmlDMAgentServerInitAlert;
       
  1145 			}
       
  1146 		iSyncMLCmds->DoAlertL( alertCode );
       
  1147 		// FOTA
       
  1148     for ( TInt i(0); i<iGenAlerts.Count(); i++ )
       
  1149     {
       
  1150     	// FOTA
       
  1151     	if(iGenAlerts[i]->FinalResult() != 0)
       
  1152       {	
       
  1153       	iSyncMLCmds->DoGenericAlertL ( *iGenAlerts[i]->FwMgmtUri(), *iGenAlerts[i]->MetaType(), *iGenAlerts[i]->MetaFormat(),	
       
  1154                         iGenAlerts[i]->FinalResult(), *iGenAlerts[i]->FwCorrelator()  );                
       
  1155       }
       
  1156       else
       
  1157       {
       
  1158       	// Other than FOTA case
       
  1159       	iSyncMLCmds->DoGenericAlertL ( *iGenAlerts[i]->FwCorrelator(), *iGenAlerts[i]->DataItem());               
       
  1160       }		
       
  1161      }     		
       
  1162 		if ( iGenAlerts.Count() > 0 )
       
  1163 			{
       
  1164 			iResetGenAlerts = ETrue;			
       
  1165 			}
       
  1166 		iResetUserInitAlert = iSyncMLCmds->DoGenericUserAlertL ( iProfileID );
       
  1167 		// FOTA end
       
  1168 		}
       
  1169 	iCurrClientInitState = EInitDeviceInfo;
       
  1170 	}
       
  1171 // ---------------------------------------------------------
       
  1172 // CNSmlDMAgent::InitDeviceInfoStateL()
       
  1173 //  Device Info is sent in Replace command
       
  1174 // ---------------------------------------------------------
       
  1175 void CNSmlDMAgent::InitDeviceInfoStateL()
       
  1176 	{
       
  1177 	if ( !Interrupted() )
       
  1178 		{
       
  1179 		iSyncMLCmds->DoAddOrReplaceOrDeleteL(); 
       
  1180 		}
       
  1181 	iCurrClientInitState = EInitEndingMessage;
       
  1182 	}
       
  1183 
       
  1184 
       
  1185 
       
  1186 // ---------------------------------------------------------
       
  1187 // CNSmlDMAgent::ServerModUpdatesStateL(()
       
  1188 // Handles Add, Replace or Delete command from server 
       
  1189 // ---------------------------------------------------------
       
  1190 void CNSmlDMAgent::ServerModUpdatesStateL( const TDesC8& aCmd, SmlGenericCmd_t* aContent )
       
  1191 	{
       
  1192 	DBG_FILE(_S8("CNSmlDMAgent::ServerModUpdatesStateL begins"));
       
  1193 	if ( iCurrServerModState != EServerModWaitingCommands )
       
  1194 		{
       
  1195 		User::Leave( TNSmlError::ESmlUnexpectedCmd ); 
       
  1196 		}
       
  1197 	iSyncMLCmds->ProcessUpdatesL( aCmd, aContent );	
       
  1198 	DBG_FILE(_S8("CNSmlDMAgent::ServerModUpdatesStateL ends"));
       
  1199 	}
       
  1200 // FOTA
       
  1201 // ---------------------------------------------------------
       
  1202 // CNSmlDMAgent::ServerModUpdatesStateL(()
       
  1203 //	Issues a process request for the arrived exec command.
       
  1204 //	@param aContent. The exec command structure, accordant 
       
  1205 //  with the dtd.
       
  1206 // ---------------------------------------------------------
       
  1207 //
       
  1208 void CNSmlDMAgent::ServerModUpdatesStateL ( SmlExec_t* aContent )
       
  1209 	{
       
  1210 	DBG_FILE(_S8("CNSmlDMAgent::ServerModUpdatesStateL (for exec) begins"));
       
  1211 	if ( iCurrServerModState != EServerModWaitingCommands )
       
  1212 		{
       
  1213 		User::Leave( TNSmlError::ESmlUnexpectedCmd ); 
       
  1214 		}
       
  1215 	iSyncMLCmds->ProcessExecCmdL ( aContent );	
       
  1216 	DBG_FILE(_S8("CNSmlDMAgent::ServerModUpdatesStateL (for exec) ends"));
       
  1217 	}
       
  1218 // FOTA end
       
  1219 
       
  1220 // ---------------------------------------------------------
       
  1221 // CNSmlDMAgent::DataUpdateStatusStartingMessageStateL()
       
  1222 // Notify "Finalising" to UI, makes SyncHdr 
       
  1223 // ---------------------------------------------------------
       
  1224 void CNSmlDMAgent::DataUpdateStatusStartingMessageStateL()
       
  1225 	{
       
  1226 	DBG_FILE(_S8("CNSmlDMAgent::DataUpdateStatusStartingMessageStateL begins"));
       
  1227 	DataUpdateStatusStartingMessageStateBaseL();
       
  1228 	DBG_FILE(_S8("CNSmlDMAgent::DataUpdateStatusStartingMessageStateL ends"));
       
  1229 	}
       
  1230 
       
  1231 // ---------------------------------------------------------
       
  1232 // CNSmlDMAgent::DataUpdateStatusMakingResultsStateL()
       
  1233 // Results to Get commands from a server 
       
  1234 // ---------------------------------------------------------
       
  1235 void CNSmlDMAgent::DataUpdateStatusMakingResultsStateL()
       
  1236 	{
       
  1237 	DBG_FILE(_S8("CNSmlDMAgent::DataUpdateStatusMakingResultsStateL begins"));
       
  1238 	if ( iSyncMLCmds->ResultsContainer() )
       
  1239 		{
       
  1240 		if ( iSyncMLCmds->ResultsContainer()->AnyResults() )
       
  1241 			{
       
  1242 			// there is something to send
       
  1243 			iStatusPackage = ETrue;
       
  1244 			}
       
  1245 		}
       
  1246 	CNSmlCmdsBase::TReturnValue ret = iSyncMLCmds->DoResultsL();
       
  1247 	if ( ret == CNSmlCmdsBase::EReturnBufferFull )
       
  1248 		{
       
  1249 		iBufferFull = ETrue;
       
  1250 		}
       
  1251 	iCurrDataUpdateStatusState = EStatusEndingMessage;
       
  1252 	DBG_FILE(_S8("CNSmlDMAgent::DataUpdateStatusMakingResultsStateL ends"));
       
  1253 	}
       
  1254 
       
  1255 
       
  1256 
       
  1257 // ---------------------------------------------------------
       
  1258 // CNSmlDMAgent::NonceL()
       
  1259 // Nonce (used in MD5 auth) from DM Settings 
       
  1260 // ---------------------------------------------------------
       
  1261 HBufC* CNSmlDMAgent::NonceL() const
       
  1262 	{
       
  1263 	HBufC* nonce = NULL;
       
  1264 #ifndef	__MODULETEST
       
  1265 	CNSmlDMProfile* profile = CNSmlDMSettings::NewLC()->ProfileL( iProfileID );
       
  1266 	if ( !profile )
       
  1267 		{
       
  1268 		User::Leave( TNSmlError::ESmlErrorInSettings );
       
  1269 		}
       
  1270 	else
       
  1271 		{
       
  1272 		CleanupStack::PushL( profile );
       
  1273 		nonce = profile->NonceLC( EDMProfileServerNonce );
       
  1274 		CleanupStack::Pop(); //NonceLC
       
  1275 		CleanupStack::PopAndDestroy( 2 ); //profile, settings
       
  1276 		}
       
  1277 #endif
       
  1278 	return( nonce );
       
  1279 	}
       
  1280 // ---------------------------------------------------------
       
  1281 // CNSmlDMAgent::SetNonceL()
       
  1282 // Nonce from the server's challenge is stored to DM Settings
       
  1283 // ---------------------------------------------------------
       
  1284 void CNSmlDMAgent::SetNonceL( const TDesC& aNonce ) const
       
  1285 	{
       
  1286 #ifndef	__MODULETEST
       
  1287 	CNSmlDMProfile* profile = CNSmlDMSettings::NewLC()->ProfileL( iProfileID );
       
  1288 	if ( !profile )
       
  1289 		{
       
  1290 		User::Leave( TNSmlError::ESmlErrorInSettings );
       
  1291 		}
       
  1292 	else
       
  1293 		{
       
  1294 		CleanupStack::PushL( profile );
       
  1295 		profile->SetNonceL( EDMProfileServerNonce, aNonce );
       
  1296 		CleanupStack::PopAndDestroy( 2 ); //profile, settings
       
  1297 		}
       
  1298 #endif	
       
  1299 	}
       
  1300 
       
  1301 // ---------------------------------------------------------
       
  1302 // CNSmlDMAgent::ServerUserName()
       
  1303 // 
       
  1304 // ---------------------------------------------------------
       
  1305 TPtrC CNSmlDMAgent::ServerUserName() const
       
  1306 	{
       
  1307 	return *iDMServerId;
       
  1308 	}
       
  1309 // ---------------------------------------------------------
       
  1310 // CNSmlDMAgent::ServerPassword()
       
  1311 // 
       
  1312 // ---------------------------------------------------------
       
  1313 TPtrC CNSmlDMAgent::ServerPassword() const 
       
  1314 	{
       
  1315 	return *iDMServerPassword;
       
  1316 	}
       
  1317 // ---------------------------------------------------------
       
  1318 // CNSmlDMAgent::ServerNonceAllocL()
       
  1319 // 
       
  1320 // ---------------------------------------------------------
       
  1321 HBufC* CNSmlDMAgent::ServerNonceAllocL() const
       
  1322 	{
       
  1323 	HBufC* nonce = NULL;
       
  1324 	CNSmlDMProfile* profile = CNSmlDMSettings::NewLC()->ProfileL( iProfileID );
       
  1325 	if ( !profile )
       
  1326 		{
       
  1327 		User::Leave( TNSmlError::ESmlErrorInSettings );
       
  1328 		}
       
  1329 	else
       
  1330 		{
       
  1331 		CleanupStack::PushL( profile );
       
  1332 		nonce = profile->NonceLC( EDMProfileClientNonce ); 
       
  1333 		CleanupStack::Pop(); //NonceLC
       
  1334 		CleanupStack::PopAndDestroy( 2 ); //profile, settings
       
  1335 		}
       
  1336 	return( nonce );
       
  1337 	}
       
  1338 // ---------------------------------------------------------
       
  1339 // CNSmlDMAgent::SetServerNonceL()
       
  1340 // 
       
  1341 // ---------------------------------------------------------
       
  1342 void CNSmlDMAgent::SetServerNonceL( const TDesC& aNonce ) const
       
  1343 	{
       
  1344 	CNSmlDMProfile* profile = CNSmlDMSettings::NewLC()->ProfileL( iProfileID );
       
  1345 	if ( !profile )
       
  1346 		{
       
  1347 		User::Leave( TNSmlError::ESmlErrorInSettings );
       
  1348 		}
       
  1349 	else
       
  1350 		{
       
  1351 		CleanupStack::PushL( profile );
       
  1352 		profile->SetNonceL( EDMProfileClientNonce, aNonce );
       
  1353 		CleanupStack::PopAndDestroy( 2 ); //profile, settings
       
  1354 		}
       
  1355 	}
       
  1356 
       
  1357 // ---------------------------------------------------------
       
  1358 // CNSmlDMAgent::FinalizeSyncLog()
       
  1359 // Sync Log is written after sync
       
  1360 // ---------------------------------------------------------
       
  1361 void CNSmlDMAgent::FinalizeSyncLog()
       
  1362 	{
       
  1363 	TRAP_IGNORE( FinalizeSyncLogL() );
       
  1364 	}
       
  1365 	
       
  1366 // ---------------------------------------------------------
       
  1367 // CNSmlDMAgent::FinalizeSyncLogL()
       
  1368 // Sync Log is written after sync
       
  1369 // ---------------------------------------------------------
       
  1370 void CNSmlDMAgent::FinalizeSyncLogL()
       
  1371 	{
       
  1372 	if(iSyncLog)
       
  1373 		{
       
  1374 		TTime now;
       
  1375 		//now.HomeTime();//previously using
       
  1376 		now.UniversalTime();
       
  1377 		iSyncLog->SetResult(now, iError->SyncLogErrorCode());
       
  1378 
       
  1379 			CNSmlDMSettings* settings = CNSmlDMSettings::NewLC();
       
  1380 			CNSmlDMProfile* profile = settings->ProfileL( iProfileID );
       
  1381 			if(!profile)
       
  1382 			    {
       
  1383 			    iSyncLog = NULL;
       
  1384 			    CleanupStack::PopAndDestroy();  //setttings
       
  1385 			      return;
       
  1386 			    }
       
  1387 			CleanupStack::PushL( profile );
       
  1388            if(iError->SyncLogErrorCode() == KErrNone)
       
  1389            {
       
  1390            RWriteStream& LastSyncStream = profile->LastSyncWriteStreamL();
       
  1391            CleanupClosePushL( LastSyncStream );
       
  1392            TPckgBuf<TTime> lastsynctime(now);
       
  1393            LastSyncStream << lastsynctime;
       
  1394            CleanupStack::PopAndDestroy();
       
  1395            profile->LastSyncStreamCommitL();
       
  1396            iSyncLog->SetLastSyncResult(now);//vijay
       
  1397            }
       
  1398           else
       
  1399           {
       
  1400 			if(profile->HasLastSyncL())
       
  1401 			{
       
  1402 			RReadStream& Lastsyncstream=profile->LastSyncReadStreamL();
       
  1403 			CleanupClosePushL( Lastsyncstream );
       
  1404 	        TTime  SyncSuccessTime;
       
  1405 	        SyncSuccessTime.UniversalTime();
       
  1406 	        TPckgBuf<TTime> lastsynctime(SyncSuccessTime);
       
  1407 	        Lastsyncstream >> lastsynctime;
       
  1408 	        SyncSuccessTime = lastsynctime().Int64();
       
  1409 	        iSyncLog->SetLastSyncResult(SyncSuccessTime);
       
  1410 	        CleanupStack::PopAndDestroy();	
       
  1411 			}
       
  1412           } 
       
  1413 			CNSmlHistoryArray* array = CNSmlHistoryArray::NewL();
       
  1414 			CleanupStack::PushL( array );
       
  1415 			array->SetOwnerShip( ETrue );
       
  1416 
       
  1417 			RReadStream& readStream = profile->LogReadStreamL();
       
  1418 			CleanupClosePushL( readStream );
       
  1419 			
       
  1420 			if ( readStream.Source()->SizeL() > 0 )
       
  1421 				{
       
  1422 				array->InternalizeL( readStream );
       
  1423 				}
       
  1424 				
       
  1425 			CleanupStack::PopAndDestroy(); // readStream
       
  1426 
       
  1427 			array->AppendEntryL( iSyncLog );
       
  1428 			RWriteStream& writeStream = profile->LogWriteStreamL();
       
  1429 			CleanupClosePushL( writeStream );
       
  1430 			array->ExternalizeL( writeStream );
       
  1431 			CleanupStack::PopAndDestroy(); // writeStream
       
  1432 			profile->WriteStreamCommitL();
       
  1433 
       
  1434 			CleanupStack::PopAndDestroy( 3 ); // array, settings, profile
       
  1435 			
       
  1436 			iSyncLog = NULL;
       
  1437 		}
       
  1438 	}
       
  1439 
       
  1440 
       
  1441 // ---------------------------------------------------------
       
  1442 // CNSmlDMAgent::CreateSyncmlCmdsInstanceL()
       
  1443 // 
       
  1444 // ---------------------------------------------------------
       
  1445 void CNSmlDMAgent::CreateSyncmlCmdsInstanceL()
       
  1446 	{
       
  1447 	if ( !iSyncMLCmds )
       
  1448 		{
       
  1449 		TInt numSessionID;
       
  1450 		TBuf8<16> stringSessionID;
       
  1451 		if ( iSyncInitiation == EServerAlerted )
       
  1452 			{
       
  1453 			numSessionID = iDMServerSessionId; 
       
  1454 			}
       
  1455 		else
       
  1456 			{
       
  1457 			numSessionID = GetSessionIDL();
       
  1458 			numSessionID++;
       
  1459 			}
       
  1460 		//Session id is greater than 16-bit max value
       
  1461 		if( numSessionID > KMaxInt16 || numSessionID < 0 )
       
  1462 		   {
       
  1463 		   	numSessionID = 0;
       
  1464 		   }	
       
  1465 		UpdateSessionIDL( numSessionID );
       
  1466 		stringSessionID.Num( numSessionID );
       
  1467 		if(!FeatureManager::FeatureSupported( KFeatureIdSyncMlDm112  ))
       
  1468 		{
       
  1469 		iSyncMLCmds = CNSmlDMCmds::NewL( this, stringSessionID, KNSmlDMAgentVerProto12, *iSyncMLUserName, iSyncServer, *iDMServerId, iDMObserver );
       
  1470 		}
       
  1471 		else
       
  1472 		{
       
  1473 		iSyncMLCmds = CNSmlDMCmds::NewL( this, stringSessionID, KNSmlDMAgentVerProto, *iSyncMLUserName, iSyncServer, *iDMServerId, iDMObserver );		
       
  1474 		}		
       
  1475 		}
       
  1476 
       
  1477 	iSyncMLCmds->ServerAuth()->SetSyncAgent( this );
       
  1478 	if( !(( iMediumType == KUidNSmlMediumTypeBluetooth || iMediumType == KUidNSmlMediumTypeUSB || iMediumType == KUidNSmlMediumTypeIrDA  )  
       
  1479 			&& iDMServerPassword->Length() == 0 ))	
       
  1480 		{
       
  1481 #ifndef __NO_SERVERAUTH
       
  1482 		iSyncMLCmds->ServerAuth()->SetAuthRequiredL();
       
  1483 #endif
       
  1484 		}
       
  1485 	if ( iSyncInitiation == EServerAlerted )
       
  1486 		{
       
  1487 		if ( iDMServerDigest )
       
  1488 		    {
       
  1489 		    iSyncMLCmds->ServerAuth()->SetCredentialL( *iDMServerDigest );    
       
  1490 		    }		
       
  1491 		}
       
  1492 	}
       
  1493 
       
  1494 // ---------------------------------------------------------
       
  1495 // CNSmlDMAgent::CheckCommandsAreReceivedL()
       
  1496 // Check that server has sent all expected commands, 
       
  1497 // empty function in DM
       
  1498 // ---------------------------------------------------------
       
  1499 void CNSmlDMAgent::CheckCommandsAreReceivedL()
       
  1500 	{
       
  1501 	}	
       
  1502 
       
  1503 //
       
  1504 // Authentications 
       
  1505 //
       
  1506 
       
  1507 // ---------------------------------------------------------
       
  1508 // CNSmlDMAgent::SetAuthTypeL()	
       
  1509 // Set authentication type to DM Settings 
       
  1510 // ---------------------------------------------------------
       
  1511 void CNSmlDMAgent::SetAuthTypeL( CNSmlAgentBase::TAuthenticationType aAuthType ) const
       
  1512 	{
       
  1513 #ifndef	__MODULETEST
       
  1514 	CNSmlDMProfile* profile = CNSmlDMSettings::NewLC()->ProfileL( iProfileID );
       
  1515 	if ( !profile )
       
  1516 		{
       
  1517 		User::Leave( TNSmlError::ESmlErrorInSettings );
       
  1518 		}
       
  1519 	else
       
  1520 		{
       
  1521 		CleanupStack::PushL( profile );
       
  1522 		profile->SetIntValue( EDMProfileAuthenticationRequired, aAuthType );
       
  1523 		profile->SaveL();
       
  1524 		CleanupStack::PopAndDestroy( 2 ); //profile, settings
       
  1525 		}
       
  1526 #endif
       
  1527 	}
       
  1528 
       
  1529 // ---------------------------------------------------------
       
  1530 // CNSmlDMAgent::AuthTypeL()
       
  1531 // Read Authentication type from DM settings
       
  1532 // ---------------------------------------------------------
       
  1533 
       
  1534 CNSmlAgentBase::TAuthenticationType CNSmlDMAgent::AuthTypeL() const
       
  1535 	{
       
  1536 	CNSmlAgentBase::TAuthenticationType authType = CNSmlAgentBase::ENoAuth;
       
  1537 	#ifndef	__MODULETEST
       
  1538 	CNSmlDMProfile* profile = CNSmlDMSettings::NewLC()->ProfileL( iProfileID );
       
  1539 	if ( !profile )
       
  1540 		{
       
  1541 		User::Leave( TNSmlError::ESmlErrorInSettings );
       
  1542 		}
       
  1543 	else
       
  1544 		{
       
  1545 		CleanupStack::PushL( profile );
       
  1546 		authType = (CNSmlAgentBase::TAuthenticationType) profile->IntValue( EDMProfileAuthenticationRequired );
       
  1547 		CleanupStack::PopAndDestroy( 2 ); //profile, settings
       
  1548 		}
       
  1549 	#endif
       
  1550 	return authType;
       
  1551 	}
       
  1552 
       
  1553 
       
  1554 // ---------------------------------------------------------
       
  1555 // CNSmlDMAgent::FreeResources()
       
  1556 // Release allocated resources
       
  1557 // ---------------------------------------------------------
       
  1558 //
       
  1559 void CNSmlDMAgent::FreeResources()
       
  1560 	{
       
  1561 	delete iDMTriggerMessage;
       
  1562 	iDMTriggerMessage = NULL;
       
  1563 	delete iDMServerId;
       
  1564 	iDMServerId = NULL;
       
  1565 	delete iDMServerPassword;
       
  1566 	iDMServerPassword = NULL;
       
  1567 	delete iDMServerDigest;
       
  1568 	iDMServerDigest = NULL;
       
  1569 if(FeatureManager::FeatureSupported(KFeatureIdSapPolicyManagement))
       
  1570 {
       
  1571 	delete iNotifierObserver;
       
  1572 	iNotifierObserver = NULL;
       
  1573 }
       
  1574 	FreeBaseResources();
       
  1575 	}
       
  1576 
       
  1577 
       
  1578 //
       
  1579 // Error Handling functions
       
  1580 //
       
  1581 // ---------------------------------------------------------
       
  1582 // CNSmlDMAgent::FinaliseWhenErrorL()
       
  1583 // This function is called if sync is breaked down immediately (Leave errors)
       
  1584 // This function MUST NOT leave.
       
  1585 // ---------------------------------------------------------
       
  1586 void CNSmlDMAgent::FinaliseWhenErrorL()
       
  1587 	{
       
  1588 	DBG_FILE(_S8("CNSmlDMAgent::FinaliseWhenErrorL begins"));
       
  1589 	if ( iSyncMLCmds )
       
  1590 		{
       
  1591 		iSyncMLCmds->WriteMappingInfoToDbL();
       
  1592 		iSyncMLCmds->DisconnectFromOtherServers();
       
  1593 		}
       
  1594 	DBG_FILE(_S8("CNSmlDMAgent::FinaliseWhenErrorL ends"));
       
  1595 	}
       
  1596 
       
  1597 // ---------------------------------------------------------
       
  1598 // CNSmlDMAgent::Interrupted()
       
  1599 // 
       
  1600 // ---------------------------------------------------------
       
  1601 TBool CNSmlDMAgent::Interrupted()
       
  1602 	{
       
  1603 	return iDMInterrupted;
       
  1604 	}
       
  1605 
       
  1606 // ---------------------------------------------------------
       
  1607 // CNSmlDMAgent::Interrupt()
       
  1608 // Synchronisation with all databases is interrupted
       
  1609 // Synchronisation level error code to Sync Log.
       
  1610 // ---------------------------------------------------------
       
  1611 void CNSmlDMAgent::Interrupt( TInt aErrorCode, TBool aImmediatelyInterruption, TBool aStatusError )
       
  1612 	{
       
  1613 	if ( !Interrupted() || aStatusError )
       
  1614 		{
       
  1615 		// Error code is updated only once, except errors due to
       
  1616 		// error Status codes from the server
       
  1617 		iError->SetErrorCode( aErrorCode );
       
  1618 		iDMInterrupted = ETrue;
       
  1619 		iSessionAbortAlertRequest = ETrue;
       
  1620 		iDMImmediatelyInterruption = aImmediatelyInterruption;
       
  1621 		}
       
  1622 	}
       
  1623 
       
  1624 // ---------------------------------------------------------
       
  1625 // CNSmlDMAgent::InterruptL()
       
  1626 // The function is implemented, because base class may use this one
       
  1627 // In the case of DM, appIndex does not point anything.
       
  1628 // ---------------------------------------------------------
       
  1629 void CNSmlDMAgent::InterruptL( TInt /*appIndex*/, TInt aErrorCode, TBool aImmediatelyInterruption, TBool aStatusError )
       
  1630 	{
       
  1631 	Interrupt( aErrorCode, aImmediatelyInterruption, aStatusError );
       
  1632 	}
       
  1633 
       
  1634 // ---------------------------------------------------------
       
  1635 // CNSmlDMAgent::WriteWarningL()
       
  1636 // Sync level warning message to Sync Log
       
  1637 // ---------------------------------------------------------
       
  1638 void CNSmlDMAgent::WriteWarningL( TNSmlError::TNSmlAgentErrorCode aWarningCode )
       
  1639 	{
       
  1640 	iError->SetErrorCode( aWarningCode );
       
  1641 	}
       
  1642 // ---------------------------------------------------------
       
  1643 // CNSmlDMAgent::WriteWarningL()
       
  1644 //  Database level warning message to Sync Log  
       
  1645 // ---------------------------------------------------------
       
  1646 void CNSmlDMAgent::WriteWarningL( TInt /*appIndex*/, TNSmlError::TNSmlAgentErrorCode aWarningCode )
       
  1647 	{
       
  1648 	WriteWarningL( aWarningCode );
       
  1649 	}
       
  1650 // ---------------------------------------------------------
       
  1651 // CNSmlDMAgent::WriteWarningL()
       
  1652 // Database level warning message to Sync Log
       
  1653 // ---------------------------------------------------------
       
  1654 void CNSmlDMAgent::WriteWarningL(  TInt /*appIndex*/, TNSmlError::TNSmlSyncMLStatusCode aWarningCode )
       
  1655 	{
       
  1656 	WriteWarningL( (TNSmlError::TNSmlAgentErrorCode) (TInt) aWarningCode );
       
  1657 	}
       
  1658 // ---------------------------------------------------------
       
  1659 // CNSmlDMAgent::CheckServerStatusCodeL()
       
  1660 // Status code validating
       
  1661 // ---------------------------------------------------------
       
  1662 void CNSmlDMAgent::CheckServerStatusCodeL( TInt aEntryID )
       
  1663 	{
       
  1664 	TDesC8& cmd = iSyncMLCmds->ResponseController()->Cmd( aEntryID );
       
  1665 	TNSmlError::TNSmlSyncMLStatusCode status = STATIC_CAST( TNSmlError::TNSmlSyncMLStatusCode, iSyncMLCmds->ResponseController()->StatusCode( aEntryID ) );
       
  1666 	TBool error (EFalse );
       
  1667 			
       
  1668 	switch ( status )
       
  1669 		{
       
  1670 		case TNSmlError::ESmlStatusAuthenticationAccepted:       //212
       
  1671 			if ( cmd == KNSmlAgentSyncHdr ) 
       
  1672 				{ 
       
  1673 				iAlreadyAuthenticated = ETrue;
       
  1674 				}
       
  1675 			error = EFalse;
       
  1676 			break;
       
  1677 		case  TNSmlError::ESmlStatusUnauthorized: // 401
       
  1678 			if ( cmd == KNSmlAgentSyncHdr  ) 
       
  1679 				{
       
  1680 				if ( iSyncMLCmds->ResponseController()->ChalType( aEntryID ).Length() != 0 )
       
  1681 					{
       
  1682 					if ( AuthenticationRequirementL( *iSyncMLCmds->ResponseController(), aEntryID ) )
       
  1683 						{
       
  1684 						iChallengeRequest = ETrue;
       
  1685 						SaveIfNonceL( *iSyncMLCmds->ResponseController(), aEntryID );
       
  1686 						iSyncMLCmds->ResponseController()->ResetL();
       
  1687 						return;
       
  1688 						}
       
  1689 					else
       
  1690 						{
       
  1691 						error = ETrue;
       
  1692 						}
       
  1693 					}
       
  1694 				else
       
  1695 					{
       
  1696 					error = ETrue;
       
  1697 					}
       
  1698 				}
       
  1699 			break;
       
  1700 		case  TNSmlError::ESmlStatusClientAuthenticationRequired: // 407 
       
  1701 			if ( cmd == KNSmlAgentSyncHdr ) 
       
  1702 				{
       
  1703 				if ( AuthenticationRequirementL( *iSyncMLCmds->ResponseController(), aEntryID ) )
       
  1704 					{
       
  1705 					iChallengeRequest = ETrue;
       
  1706 					SaveIfNonceL( *iSyncMLCmds->ResponseController(), aEntryID );
       
  1707 					iSyncMLCmds->ResponseController()->ResetL();
       
  1708 					return;
       
  1709 					}
       
  1710 				else
       
  1711 					{
       
  1712 					error = ETrue;
       
  1713 					}
       
  1714 				}
       
  1715 			else
       
  1716 				{
       
  1717 				error = ETrue;
       
  1718 				}
       
  1719 			break;
       
  1720 		default:
       
  1721 			error = EFalse;
       
  1722 			break;
       
  1723 		}
       
  1724 	if ( cmd == KNSmlAgentSyncHdr )
       
  1725 		{
       
  1726 		SaveIfNonceL( *iSyncMLCmds->ResponseController(), aEntryID );
       
  1727 		}
       
  1728 	if ( error )
       
  1729 		{
       
  1730 		InterruptL( iSyncMLCmds->ResponseController()->AppIndex( aEntryID ), status, ETrue, ETrue );
       
  1731 		}
       
  1732 	return;
       
  1733 	}
       
  1734 
       
  1735 // FOTA
       
  1736 // ---------------------------------------------------------
       
  1737 // CNSmlDMGenericAlert::NewL()
       
  1738 //	Creates a new generic alert object.
       
  1739 //	@param aFwMgmtUri. The uri which has been used in the exe command whose final result is 
       
  1740 //	about to be reported.
       
  1741 //	@param aMetaType. Meta/Type that should be used in the alert.
       
  1742 //	@param aMetaFormat. Meta/Format that should be used in the alert.
       
  1743 //	@param aFinalResult. The final result value which is reported to remote server.
       
  1744 //	@param aCorrelator. Correlator value used in the original exec command.
       
  1745 //	@return CNSmlDMGenericAlert*. New instance of this class.
       
  1746 // ---------------------------------------------------------
       
  1747 //
       
  1748 CNSmlDMGenericAlert* CNSmlDMGenericAlert::NewL ( const TDesC8& aFwMgmtUri, const TDesC8& aMetaType, const TDesC8& aMetaFormat, 
       
  1749 													TInt aFinalResult, const TDesC8& aFwCorrelator )
       
  1750     {
       
  1751 	CNSmlDMGenericAlert* self = new (ELeave) CNSmlDMGenericAlert();
       
  1752 	CleanupStack::PushL( self );
       
  1753     self->ConstructL ( aFwMgmtUri, aMetaType, aMetaFormat, aFinalResult, aFwCorrelator );
       
  1754     CleanupStack::Pop(); //self
       
  1755 	return self;
       
  1756     }
       
  1757 
       
  1758 // ---------------------------------------------------------
       
  1759 // CNSmlDMGenericAlert::NewL()
       
  1760 //	Creates a new generic alert object.
       
  1761 //	@param aFwMgmtUri. The uri which has been used in the exe command whose final result is 
       
  1762 //	about to be reported.
       
  1763 //	@param aMetaType. Meta/Type that should be used in the alert.
       
  1764 //	@param aMetaFormat. Meta/Format that should be used in the alert.
       
  1765 //	@param aFinalResult. The final result value which is reported to remote server.
       
  1766 //	@param aCorrelator. Correlator value used in the original exec command.
       
  1767 //	@return CNSmlDMGenericAlert*. New instance of this class.
       
  1768 // ---------------------------------------------------------
       
  1769 //
       
  1770 CNSmlDMGenericAlert* CNSmlDMGenericAlert::NewL ( const TDesC8& aCorrelator, const RArray<CNSmlDMAlertItem>& aItemList )
       
  1771     {
       
  1772     CNSmlDMGenericAlert* self = new (ELeave) CNSmlDMGenericAlert();
       
  1773     CleanupStack::PushL( self );
       
  1774     _LIT8(KDummy, "dummy");
       
  1775     self->ConstructL ( KDummy(), KDummy(), KDummy(), 0, aCorrelator, &aItemList ); 
       
  1776     CleanupStack::Pop(); //self
       
  1777     return self;
       
  1778     }
       
  1779 
       
  1780 // ---------------------------------------------------------
       
  1781 // CNSmlDMGenericAlert::CNSmlDMGenericAlert()
       
  1782 //	C++ destructor.
       
  1783 // ---------------------------------------------------------
       
  1784 //
       
  1785 CNSmlDMGenericAlert::~CNSmlDMGenericAlert()
       
  1786     {
       
  1787     delete iFwMgmtUri;
       
  1788     delete iMetaType;
       
  1789     delete iMetaFormat;
       
  1790     delete iFwCorrelator;
       
  1791     if (iDataItem)
       
  1792         {
       
  1793         iDataItem->Close();	
       
  1794         }
       
  1795     delete iDataItem;
       
  1796     }
       
  1797 
       
  1798 
       
  1799 // ---------------------------------------------------------
       
  1800 // CNSmlDMGenericAlert::CNSmlDMGenericAlert()
       
  1801 //	C++ constructor.
       
  1802 // ---------------------------------------------------------
       
  1803 //
       
  1804 CNSmlDMGenericAlert::CNSmlDMGenericAlert()
       
  1805     {
       
  1806     }
       
  1807 // ---------------------------------------------------------
       
  1808 // CNSmlDMGenericAlert::ConstructL()
       
  1809 //	Symbian 2nd phase constructor 
       
  1810 // ---------------------------------------------------------
       
  1811 //
       
  1812 void CNSmlDMGenericAlert::ConstructL( const TDesC8& aFwMgmtUri, const TDesC8& aMetaType, const TDesC8& aMetaFormat, 
       
  1813         TInt aFinalResult, const TDesC8& aFwCorrelator, const RArray<CNSmlDMAlertItem>* aItemList )
       
  1814     {
       
  1815     if(aFinalResult != 0)
       
  1816         {
       
  1817         iFwMgmtUri = aFwMgmtUri.AllocL();
       
  1818         iMetaType = aMetaType.AllocL();
       
  1819         iMetaFormat = aMetaFormat.AllocL();
       
  1820         }
       
  1821     iFwCorrelator = aFwCorrelator.AllocL();
       
  1822     iFinalResult = aFinalResult;   
       
  1823 
       
  1824 
       
  1825     TInt count = 0;
       
  1826     if (aItemList)
       
  1827     	count = aItemList->Count();
       
  1828 
       
  1829     if( count > 0)
       
  1830         { 
       
  1831         iDataItem = new (ELeave) RArray<CNSmlDMAlertItem>();   // new (ELeave) RArray<TDes8<255> > ();  
       
  1832 
       
  1833         for(TInt i =0; i< count ; i++)
       
  1834             {    
       
  1835             CNSmlDMAlertItem* iItem = new (ELeave) CNSmlDMAlertItem ;
       
  1836             iItem->iSource = ((*aItemList)[i].iSource)->AllocL();
       
  1837             iItem->iTarget = ((*aItemList)[i].iTarget)->AllocL();
       
  1838             iItem->iMetaType = ((*aItemList)[i].iMetaType)->AllocL();
       
  1839             iItem->iMetaFormat = ((*aItemList)[i].iMetaFormat)->AllocL();
       
  1840             iItem->iMetaMark = ((*aItemList)[i].iMetaMark)->AllocL();
       
  1841             iItem->iData = ((*aItemList)[i].iData)->AllocL();
       
  1842             iDataItem->AppendL(*iItem);
       
  1843             }
       
  1844         }   
       
  1845 
       
  1846     }
       
  1847 
       
  1848 // FOTA end
       
  1849 
       
  1850 
       
  1851 // --------------------------------------------------------------------------
       
  1852 // CNSmlNotifierObserver::CNSmlNotifierObserver(TRequestStatus& aStatus, CNSmlThreadEngine* aEngine, CNSmlThreadParams& aParams)
       
  1853 // Constructor
       
  1854 // --------------------------------------------------------------------------
       
  1855 //	
       
  1856 CNSmlAgentNotifierObserver::CNSmlAgentNotifierObserver(TRequestStatus& aStatus, TInt aCreatorId)
       
  1857 : CActive(0), iCallerStatus(aStatus), iCreatorId(aCreatorId)
       
  1858 	{
       
  1859 	CActiveScheduler::Add(this);
       
  1860 	}
       
  1861 
       
  1862 // --------------------------------------------------------------------------
       
  1863 // CNSmlNotifierObserver::~CNSmlNotifierObserver()
       
  1864 // Destructor
       
  1865 // --------------------------------------------------------------------------
       
  1866 //	
       
  1867 CNSmlAgentNotifierObserver::~CNSmlAgentNotifierObserver()
       
  1868 	{
       
  1869 	// StartNotifier called to avoid Notifier server panic, if 
       
  1870 	// notifier does not exist anymore.
       
  1871 	TBuf8<1> dummy;	
       
  1872 	iNotifier.StartNotifier(KNullUid, dummy, dummy); // KNullUid should do also..
       
  1873 	iNotifier.CancelNotifier( KUidNotifier );
       
  1874 	iNotifier.Close();
       
  1875 	iNotifierTimeOut.Cancel();
       
  1876 	Cancel();
       
  1877 	}
       
  1878 
       
  1879 // --------------------------------------------------------------------------
       
  1880 // CNSmlNotifierObserver::ConnectToNotifierL( const TSyncMLAppLaunchNotifParams& aParam )
       
  1881 // Starts connection with notifier
       
  1882 // --------------------------------------------------------------------------
       
  1883 //		
       
  1884 void CNSmlAgentNotifierObserver::ConnectToNotifierL( CNSmlAgentBase * aNSmlAgentBase)
       
  1885 	{
       
  1886 	iTimeOut = EFalse;
       
  1887 	iNSmlAgentBase = aNSmlAgentBase;
       
  1888 	
       
  1889 	if ( !IsActive() )
       
  1890 		{
       
  1891 		SetActive();
       
  1892 		}
       
  1893 
       
  1894 	//connect to repository		
       
  1895 	CRepository* rep = CRepository::NewLC( KCRUidPolicyManagementUtilInternalKeys );
       
  1896 		
       
  1897 	//get parameters 	
       
  1898 	TSyncMLAppLaunchNotifParams params;
       
  1899     TPckg<TSyncMLAppLaunchNotifParams> data( params );
       
  1900 	User::LeaveIfError( rep->Get( KSyncMLSessionParamsKey, data)); 
       
  1901 
       
  1902 	CleanupStack::PopAndDestroy( rep);
       
  1903 
       
  1904     iNotifierTimeOut.LaunchNotifierTimer( this );
       
  1905     User::LeaveIfError( iNotifier.Connect() );
       
  1906     iNotifier.StartNotifierAndGetResponse( iStatus, KUidNotifier, data, iResBuf );
       
  1907 	}
       
  1908 
       
  1909 // --------------------------------------------------------------------------
       
  1910 // CNSmlNotifierObserver::NotifierTimeOut()
       
  1911 // Starts notifier time out timer
       
  1912 // --------------------------------------------------------------------------
       
  1913 //
       
  1914 void CNSmlAgentNotifierObserver::NotifierTimeOut()
       
  1915 	{
       
  1916 	iTimeOut = ETrue;
       
  1917 	// StartNotifier called to avoid Notifier server panic, if 
       
  1918 	// notifier does not exist anymore.
       
  1919 	TBuf8<1> dummy;	
       
  1920 	iNotifier.StartNotifier(KNullUid, dummy, dummy); // KNullUid should do also..
       
  1921 	
       
  1922 	iNotifier.CancelNotifier( KUidNotifier );
       
  1923 	}
       
  1924 
       
  1925 // --------------------------------------------------------------------------
       
  1926 // CNSmlNotifierObserver::DoCancel()
       
  1927 // From base class
       
  1928 // --------------------------------------------------------------------------
       
  1929 //	
       
  1930 void CNSmlAgentNotifierObserver::DoCancel()
       
  1931 	{
       
  1932 	}
       
  1933 
       
  1934 // --------------------------------------------------------------------------
       
  1935 // CNSmlNotifierObserver::RunL()
       
  1936 // From base class
       
  1937 // --------------------------------------------------------------------------
       
  1938 //	
       
  1939 void CNSmlAgentNotifierObserver::RunL()
       
  1940 {
       
  1941 	    
       
  1942     TInt ret = iStatus.Int();
       
  1943     if ( ret == KErrCancel || iTimeOut )
       
  1944     {
       
  1945     	iNSmlAgentBase->DoOutsideCancel();
       
  1946     }
       
  1947     else
       
  1948     {
       
  1949 	    if ( ret == KErrNone )
       
  1950     	{	   
       
  1951         
       
  1952 	  		TInt sid = iResBuf().iSecureId.iUid; // read secure id from notifier.
       
  1953 	   	
       
  1954 	    	// Check the response and error code. If there is a fail, dump the job.        
       
  1955 	    	// Also compare sid to creator id saved for current job to secure that listener owns the job.
       
  1956 /*		    if ( ret != KErrNone || sid != iThreadParams.iCurrentJob.CreatorId() )
       
  1957 	       	{
       
  1958 	        	if ( ret == KErrNone && sid != iThreadParams.iCurrentJob.CreatorId() )
       
  1959 	        	{
       
  1960 #ifndef __WINS__	        	
       
  1961 		        	ret = KErrPermissionDenied;
       
  1962 #endif	        	
       
  1963 	        	}
       
  1964 	        }*/
       
  1965 	    	
       
  1966     	}
       
  1967     }
       
  1968     
       
  1969 	TRequestStatus* sStatus = &iCallerStatus;
       
  1970 	User::RequestComplete( sStatus, KErrNone );
       
  1971 }
       
  1972 
       
  1973 // --------------------------------------------------------------------------
       
  1974 // CNSmlNotifierTimeOut::~CNSmlNotifierTimeOut()
       
  1975 // Constructor
       
  1976 // --------------------------------------------------------------------------
       
  1977 //
       
  1978 CNSmlAgentNotifierTimeOut::CNSmlAgentNotifierTimeOut()
       
  1979 :CActive(0)
       
  1980 	{
       
  1981 	CActiveScheduler::Add(this);
       
  1982 	}
       
  1983 
       
  1984 // --------------------------------------------------------------------------
       
  1985 // CNSmlNotifierTimeOut::~CNSmlNotifierTimeOut()
       
  1986 // Destructor
       
  1987 // --------------------------------------------------------------------------
       
  1988 //
       
  1989 CNSmlAgentNotifierTimeOut::~CNSmlAgentNotifierTimeOut()
       
  1990 	{
       
  1991 	Cancel();
       
  1992 	iTimeOutTimer.Close();
       
  1993 	}
       
  1994 
       
  1995 // --------------------------------------------------------------------------
       
  1996 // CNSmlNotifierTimeOut::LaunchNotifierTimer( CNSmlNotifierObserver*  aObserver )
       
  1997 // Starts notifier time out timer
       
  1998 // --------------------------------------------------------------------------
       
  1999 //
       
  2000 void CNSmlAgentNotifierTimeOut::LaunchNotifierTimer( CNSmlAgentNotifierObserver*  aObserver )
       
  2001 	{
       
  2002 	
       
  2003 	if ( !IsActive() )
       
  2004 		{
       
  2005 		SetActive();
       
  2006 		}
       
  2007 		
       
  2008 	iTimeOutTimer.CreateLocal();
       
  2009 	iTimeOutTimer.After( iStatus, KNotifierTimeout );
       
  2010 	iObserver = aObserver;
       
  2011 	}
       
  2012 	
       
  2013 // --------------------------------------------------------------------------
       
  2014 // CNSmlNotifierTimeOut::DoCancel()
       
  2015 // From base class 
       
  2016 // --------------------------------------------------------------------------
       
  2017 //
       
  2018 void CNSmlAgentNotifierTimeOut::DoCancel()
       
  2019 	{
       
  2020 	iTimeOutTimer.Cancel();
       
  2021 	}
       
  2022 
       
  2023 // --------------------------------------------------------------------------
       
  2024 // CNSmlNotifierTimeOut::RunL()
       
  2025 // From base class 
       
  2026 // --------------------------------------------------------------------------
       
  2027 //
       
  2028 void CNSmlAgentNotifierTimeOut::RunL()
       
  2029 	{
       
  2030 	if ( iStatus.Int() != KErrCancel )
       
  2031 		{
       
  2032 		iObserver->NotifierTimeOut();	
       
  2033 		}
       
  2034 	}
       
  2035 
       
  2036 
       
  2037 
       
  2038 // ---------------------------------------------------------
       
  2039 // CreateDMAgentL( MNSmlDMAgentObserver* aDMObserver )
       
  2040 // Return the instance of the CNSmlDMAgent
       
  2041 // ---------------------------------------------------------
       
  2042 //
       
  2043 EXPORT_C CNSmlDMAgent* CreateDMAgentL( MSyncMLProgressObserver* aDMObserver )
       
  2044 	{
       
  2045 	return CNSmlDMAgent::NewL( aDMObserver );
       
  2046 	}
       
  2047 
       
  2048 //  End of File  
       
  2049