syncmlfw/common/http/src/nsmldialiap.cpp
changeset 0 b497e44ab2fc
child 9 57a65a3a658c
child 24 bf47f3b79154
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2002 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 HTTP client
       
    15 *
       
    16 */
       
    17 
       
    18 #include <CoreApplicationUIsSDKCRKeys.h>
       
    19 #include <centralrepository.h>
       
    20 #include <featmgr.h>   // FeatureManager
       
    21 #include <cmdefconnvalues.h>
       
    22 #include <cmmanager.h>
       
    23 #include <ApUtils.h>
       
    24 #include <etelpckt.h>
       
    25 #include "nsmldialiap.h"
       
    26 #include <nsmlconstants.h>
       
    27 #include "nsmlerror.h"
       
    28 
       
    29 
       
    30 //------------------------------------------------------------
       
    31 // CNSmlDialUpAgent::CNSmlDialUpAgent() 
       
    32 // constructor
       
    33 //------------------------------------------------------------
       
    34 CNSmlDialUpAgent::CNSmlDialUpAgent() 
       
    35 	: CActive( EPriorityStandard ){}
       
    36 //------------------------------------------------------------
       
    37 // CNSmlDialUpAgent::~CNSmlDialUpAgent()
       
    38 // desctructor
       
    39 //------------------------------------------------------------
       
    40 CNSmlDialUpAgent::~CNSmlDialUpAgent()
       
    41 	{
       
    42 	FeatureManager::UnInitializeLib();
       
    43 	Cancel();
       
    44 	// close handles for sure
       
    45 	if( !iCancelCalled && iSocketConnection )
       
    46 		{
       
    47 		iConnection.SetOpt( KCOLProvider, KConnDisableTimers, EFalse );		
       
    48 		iConnection.Close();
       
    49 		iSocketServer.Close();
       
    50 		}
       
    51 	}
       
    52 // --------------------------------------------------------------------
       
    53 // CNSmlDialUpAgent::ConstructL()
       
    54 // 2-phase
       
    55 // --------------------------------------------------------------------
       
    56 void CNSmlDialUpAgent::ConstructL()
       
    57     {
       
    58 	DBG_FILE( _S8("CNSmlDialUpAgent::ConstructL") );
       
    59 	CActiveScheduler::Add( this );
       
    60 	iCancelCalled = EFalse;    
       
    61 	iSocketConnection = EFalse;    
       
    62 	FeatureManager::InitializeLibL();  
       
    63 	}
       
    64 //------------------------------------------------------------
       
    65 // CNSmlDialUpAgent::DoCancel()
       
    66 // DoCancel() from CActive
       
    67 //------------------------------------------------------------
       
    68 void CNSmlDialUpAgent::DoCancel() 
       
    69 	{
       
    70 	DBG_FILE( _S8("CNSmlDialUpAgent::DoCancel...") );
       
    71 	iCancelCalled = ETrue;
       
    72 	if(iSocketConnection)
       
    73 	{
       
    74 	iConnection.SetOpt( KCOLProvider, KConnDisableTimers, EFalse );
       
    75 	iConnection.Close();
       
    76 	iSocketServer.Close();
       
    77 	iSocketConnection = EFalse;
       
    78 	}
       
    79 	}
       
    80 //------------------------------------------------------------
       
    81 // CNSmlDialUpAgent::RunL()
       
    82 // Runl() from CActive
       
    83 // called when asynchronous request completed
       
    84 //------------------------------------------------------------
       
    85 void CNSmlDialUpAgent::RunL() 
       
    86 	{
       
    87 	DBG_FILE_CODE( iStatus.Int(), _S8("CNSmlDialUpAgent::RunL with status") );
       
    88 	
       
    89 	/**
       
    90 	   In 2G Calls , data and voice are not allowed in same channel
       
    91 	   If the error code is KErrGprsServicesNotAllowed and if access point is 
       
    92 	   not Always ask, every minute retry is made , max for 35 times
       
    93 	**/
       
    94 	TInt isAccessPointDefined = EFalse;
       
    95 	if (static_cast<TInt32>(iIAPid) == -2)
       
    96 	{
       
    97 		RCmManager cmmgr;
       
    98 		cmmgr.OpenL();
       
    99 		TCmDefConnValue defConnValue;
       
   100 		cmmgr.ReadDefConnL(defConnValue);
       
   101 		cmmgr.Close();
       
   102 		if(defConnValue.iType == ECmDefConnDestination)
       
   103 		{
       
   104 			isAccessPointDefined = ETrue;
       
   105 		}
       
   106 	}
       
   107 	else
       
   108 	{
       
   109 		if(static_cast<TInt32>(iIAPid)!=-1)
       
   110 		{
       
   111 		isAccessPointDefined = ETrue;	
       
   112 		}
       
   113 	}
       
   114 	if( iRetryCounter<KNSmlConNumberOfRetries && !iTimerReturn && 
       
   115 		iStatus.Int()==KErrGprsServicesNotAllowed && 
       
   116 		isAccessPointDefined)
       
   117 	    {
       
   118 	      DBG_FILE( _S8("CNSmlDialUpAgent::RunL() ,Timer is started"));
       
   119 		    DBG_FILE_CODE(iRetryCounter, _S8("CNSmlDialUpAgent::RunL() for Iteration "));
       
   120 		    
       
   121 		    iTimerReturn=ETrue;
       
   122 	    	iTimer.CreateLocal();
       
   123 	      iTimer.After( iStatus, TTimeIntervalMicroSeconds32(KNSmlConRetryInterval));
       
   124 	      iConnection.Close();
       
   125 		  iSocketServer.Close();
       
   126 		  iSocketConnection = EFalse;
       
   127 	      SetActive();
       
   128 	      return;
       
   129 	    }
       
   130 	 else if(iRetryCounter<KNSmlConNumberOfRetries && iTimerReturn)
       
   131 	    {
       
   132 	        DBG_FILE_CODE(iRetryCounter, _S8("CNSmlDialUpAgent::RunL() Retrying connection"));
       
   133 	        Cancel();
       
   134 	        iTimerReturn=EFalse;
       
   135 	        iRetryCounter++;
       
   136 	       	TRAPD( ret, StartDatacallL() );
       
   137 	        if ( ret != KErrNone )
       
   138 	            {
       
   139 		            TRequestStatus* status = &iStatus;
       
   140 				    		User::RequestComplete( status, ret );		
       
   141 	            }
       
   142 	        return;    
       
   143         }
       
   144     if ( iStatus.Int() != KErrNone && iIAPCnt < iIAPArray->Count() )
       
   145         {
       
   146         this->Cancel();        
       
   147         iIAPid = iIAPArray->At( iIAPCnt );
       
   148         DoSettingsL();
       
   149 
       
   150         iIAPCnt++;
       
   151 
       
   152         TRAPD( ret, StartDatacallL() );
       
   153         if ( ret != KErrNone )
       
   154             {
       
   155             TRequestStatus* status = &iStatus;
       
   156 		    User::RequestComplete( status, ret );		
       
   157             }
       
   158         }
       
   159     else
       
   160         {
       
   161 	    if ( iStatus.Int() == KErrCancel )
       
   162 	    	{
       
   163 	    	this->Cancel();
       
   164             iStatus = KErrCancel;
       
   165 	    	}
       
   166 	    else if ( iStatus.Int() != KErrNone )
       
   167 	    	{
       
   168 	    	this->Cancel();
       
   169             iStatus = TNSmlError::ESmlCommunicationError;
       
   170 	    	}
       
   171 	    if(iSocketConnection)
       
   172 	    {
       
   173 	    iConnection.SetOpt( KCOLProvider, KConnDisableTimers, ETrue );
       
   174 	    }
       
   175 	    TRequestStatus* status = iEngineStatus;
       
   176 	    User::RequestComplete( status, iStatus.Int() );			
       
   177         }
       
   178 	}
       
   179 // ---------------------------------------------------------
       
   180 // CNSmlDialUpAgent::ConnectL( TUint aIAPid, TRequestStatus &aStatus )
       
   181 // Opens data call handle 
       
   182 // This is asynchronous request 
       
   183 // ---------------------------------------------------------
       
   184 void CNSmlDialUpAgent::ConnectL( CArrayFixFlat<TUint32>* aIAPIdArray, TRequestStatus &aStatus )
       
   185 	{
       
   186 	//DBG_FILE_CODE( aIAPid, _S8("CNSmlDialUpAgent::ConnectL") );
       
   187 	// aStatus set pending from HTTP
       
   188 	iCancelCalled = EFalse;
       
   189 	iEngineStatus = &aStatus;
       
   190 
       
   191     iIAPCnt = 0;
       
   192     iIAPArray = aIAPIdArray;
       
   193 
       
   194 
       
   195 	TBool connected( EFalse );
       
   196 	TRAPD( err, ( connected = IsConnectedL() ) );
       
   197 	if( err != KErrNone )
       
   198 		{
       
   199 		TRequestStatus* status = &iStatus;
       
   200 		User::RequestComplete( status, err );		
       
   201 		}
       
   202 	if( !connected )
       
   203 		{
       
   204         iIAPid = iIAPArray->At( iIAPCnt );
       
   205         DoSettingsL();
       
   206 
       
   207         iIAPCnt++;
       
   208 
       
   209         TRAPD( ret, StartDatacallL() );
       
   210         if ( ret != KErrNone )
       
   211             {
       
   212             TRequestStatus* status = &iStatus;
       
   213 		    User::RequestComplete( status, ret );		
       
   214             }
       
   215 /*
       
   216 			#ifdef __WINS__
       
   217 	            if ( !IsConnectedL() )
       
   218 		            {						
       
   219 					if ( !IsConnectedL() )	// Double check
       
   220 						{
       
   221 						this->Cancel();
       
   222 						iIAPCnt++;
       
   223 						}
       
   224 					else
       
   225 						{
       
   226 						callStarted = ETrue;
       
   227 						}
       
   228 	                }
       
   229 		        else
       
   230 			        {
       
   231 				    callStarted = ETrue;
       
   232 					}
       
   233 			#else
       
   234 	            if ( !IsConnectedL() )
       
   235 	                {
       
   236 		            this->Cancel();
       
   237 			        iIAPCnt++;
       
   238 				    }
       
   239 	            else
       
   240 		            {
       
   241 			        callStarted = ETrue;
       
   242 				    }
       
   243 			#endif
       
   244 */
       
   245 
       
   246 		}
       
   247 	else
       
   248 		{
       
   249 		TRAPD( error, AttachToConnectionL() );
       
   250 		TRequestStatus* status = &iStatus;
       
   251 		User::RequestComplete( status, error );		
       
   252 		}  
       
   253 	}
       
   254 // ---------------------------------------------------------
       
   255 // CNSmlDialUpAgent::StartDatacallL()
       
   256 // Opens data call handle 
       
   257 // This is asynchronous request 
       
   258 // ---------------------------------------------------------
       
   259 void CNSmlDialUpAgent::StartDatacallL()
       
   260     {
       
   261     DBG_FILE( _S8("CNSmlDialUpAgent::StartDatacallL") );
       
   262     SetActive();
       
   263     User::LeaveIfError( iSocketServer.Connect() );		
       
   264     User::LeaveIfError( iConnection.Open( iSocketServer ) );
       
   265     iSocketConnection = ETrue;
       
   266     if ( static_cast<TInt32>(iIAPid) == -1 )
       
   267         {
       
   268         iConnection.Start( iPref, iStatus );
       
   269         }
       
   270     else if( static_cast<TInt32>(iIAPid) == -2 ) // default connection 
       
   271         {
       
   272 
       
   273         iConnection.Start( iStatus );	
       
   274 
       
   275         }
       
   276     else
       
   277         {
       
   278         iConnection.Start( iPref, iStatus );
       
   279         }
       
   280     }
       
   281 	
       
   282 // ---------------------------------------------------------
       
   283 // CNSmlDialUpAgent::AttachToConnectionL()
       
   284 // Attach to connection allready active 
       
   285 // 
       
   286 // ---------------------------------------------------------
       
   287 void CNSmlDialUpAgent::AttachToConnectionL()
       
   288 	{
       
   289 	DBG_FILE( _S8("CNSmlDialUpAgent::AttachToConnectionL") );
       
   290 	iStatus = KRequestPending;
       
   291 	SetActive();
       
   292 	TConnectionInfo connectionInfo;
       
   293 	TConnectionInfoBuf connInfo( connectionInfo );
       
   294 	
       
   295 	User::LeaveIfError( iSocketServer.Connect() );
       
   296 	User::LeaveIfError( iConnection.Open( iSocketServer ) );
       
   297 	
       
   298 	TUint count( 0 );
       
   299 	User::LeaveIfError( iConnection.EnumerateConnections ( count ) );
       
   300 	DBG_FILE_CODE( count, _S8("Number of connections ") );
       
   301 	
       
   302 	if( count == 0 )
       
   303 		{
       
   304 		iConnection.Close();
       
   305 		iSocketServer.Close();
       
   306 		User::Leave( KErrNotFound );
       
   307 		}
       
   308 	else{
       
   309 		for( TUint i=1; i<=count; ++i )
       
   310 			{
       
   311 			User::LeaveIfError( iConnection.GetConnectionInfo( i, connInfo ) );
       
   312 			DBG_FILE_CODE( connInfo().iIapId, _S8("Comparing IDs ") );			
       
   313 			if( connInfo().iIapId == iIAPid )
       
   314 				{
       
   315 				DBG_FILE_CODE( iIAPid, _S8("Attaching to existing connection, IDs match") );
       
   316 				User::LeaveIfError( iConnection.Attach( connInfo, RConnection::EAttachTypeNormal ) );				
       
   317 				break;
       
   318 				}
       
   319 			}
       
   320 		}
       
   321 	}
       
   322 // ---------------------------------------------------------
       
   323 // CNSmlDialUpAgent::IsConnectedL()
       
   324 // Checks if data call is up or connecting
       
   325 // ---------------------------------------------------------
       
   326 TBool CNSmlDialUpAgent::IsConnectedL() const
       
   327 	{
       
   328 	DBG_FILE( _S8("CNSmlDialUpAgent::IsConnected") );
       
   329 	TInt err( KErrNone );
       
   330 	TBool isConnected( EFalse );
       
   331 	TConnectionInfo connectionInfo;
       
   332 	TConnectionInfoBuf connInfo( connectionInfo );
       
   333 
       
   334 	RSocketServ serv;
       
   335 	CleanupClosePushL( serv );
       
   336 	User::LeaveIfError( serv.Connect() );
       
   337 
       
   338 	RConnection conn;
       
   339 	CleanupClosePushL( conn );
       
   340 	User::LeaveIfError( conn.Open( serv ) );
       
   341 
       
   342 	TUint count( 0 );
       
   343 	User::LeaveIfError( conn.EnumerateConnections ( count ) );
       
   344 	DBG_FILE_CODE( count, _S8("Number of connections ") );
       
   345 	
       
   346 	if( count == 0 )
       
   347 		{
       
   348 		CleanupStack::PopAndDestroy( 2 ); // conn, serv
       
   349 		return isConnected;
       
   350 		}
       
   351 	else{
       
   352 		for( TUint i=1; i<=count; ++i )
       
   353 			{
       
   354 			err = conn.GetConnectionInfo( i, connInfo );
       
   355 			if( err != KErrNone )
       
   356 				{
       
   357 				CleanupStack::PopAndDestroy( 2 ); // conn, serv
       
   358 				User::Leave( err );				
       
   359 				}	
       
   360 			DBG_FILE_CODE( connInfo().iIapId, _S8("Comparing IDs ") );
       
   361 			if( connInfo().iIapId == iIAPid )
       
   362 				{
       
   363 				DBG_FILE( _S8("Connection allready active") );
       
   364 				isConnected = ETrue;
       
   365 				break;
       
   366 				}
       
   367 		    }
       
   368 		}
       
   369 	CleanupStack::PopAndDestroy( 2 ); // conn, serv
       
   370 	return isConnected;
       
   371 	}
       
   372 // ---------------------------------------------------------
       
   373 // CNSmlDialUpAgent::DoSettingsL()
       
   374 //  
       
   375 //  
       
   376 // ---------------------------------------------------------
       
   377 void CNSmlDialUpAgent::DoSettingsL()
       
   378     {
       
   379     DBG_FILE( _S8("CNSmlDialUpAgent::DoSettingsL") );
       
   380     iOffline=IsInOfflineModeL();
       
   381     if( static_cast<TInt32>(iIAPid) == -1 )
       
   382         {		
       
   383         if(iOffline)
       
   384             {		
       
   385             iPref.SetDialogPreference( ECommDbDialogPrefPrompt );
       
   386             iPref.SetBearerSet(EApBearerTypeWLAN);	
       
   387             }
       
   388         else
       
   389             {		
       
   390             iPref.SetDialogPreference( ECommDbDialogPrefPrompt );		
       
   391             iPref.SetBearerSet(EApBearerTypeAllBearers);
       
   392             }				
       
   393         }
       
   394     else if( iOffline &&  ( static_cast<TInt32>(iIAPid) == -2 ) ) // default connection
       
   395         {
       
   396         iPref.SetDialogPreference( ECommDbDialogPrefPrompt );
       
   397         iPref.SetBearerSet(EApBearerTypeWLAN);	
       
   398         }
       
   399     else
       
   400         {
       
   401         iPref.SetIapId( iIAPid );
       
   402         iPref.SetDialogPreference( ECommDbDialogPrefDoNotPrompt );
       
   403         }
       
   404     }
       
   405 //----------------------------------------------------------
       
   406 //CNsmlDialUpAgent::IsOfflineModeL()
       
   407 TBool CNSmlDialUpAgent::IsInOfflineModeL()
       
   408     {
       
   409     TInt operationsAllowed( ECoreAppUIsNetworkConnectionAllowed );
       
   410 	CRepository* rep = CRepository::NewLC( KCRUidCoreApplicationUIs );
       
   411 	rep->Get(KCoreAppUIsNetworkConnectionAllowed, operationsAllowed );
       
   412 	CleanupStack::PopAndDestroy(); //rep
       
   413 	
       
   414     return ( operationsAllowed == ECoreAppUIsNetworkConnectionNotAllowed ) ? ETrue : EFalse;
       
   415     }