cellularsrvapitest/datatransferhaitest/esock/src/T_RConnectionData.cpp
changeset 0 3553901f7fa8
equal deleted inserted replaced
-1:000000000000 0:3553901f7fa8
       
     1 /*
       
     2 * Copyright (c) 2005-2009 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:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "T_RConnectionData.h"
       
    20 #include "T_RSocketServData.h"
       
    21 #include <in_sock.h>
       
    22 
       
    23 /*@{*/
       
    24 //LIT's for reading parameters from ini
       
    25 _LIT( KRSocketServ,		"RSocketServ");
       
    26 _LIT( KIAPName,      	"IAP" );
       
    27 _LIT( KRetries,      	"Retries" );
       
    28 /*@}*/
       
    29 
       
    30 /*@{*/
       
    31 //LIT's for Commands
       
    32 _LIT(KCmdOpen,				"Open");
       
    33 _LIT(KCmdStart,				"Start");
       
    34 _LIT(KCmdStop,				"Stop");
       
    35 _LIT(KCmdClose,				"Close");
       
    36 _LIT(KCmdSelectOutgoingIAP,	"SelectOutgoingIAP");
       
    37 /*@}*/
       
    38 
       
    39 
       
    40 CT_RConnectionData* CT_RConnectionData::NewL()
       
    41 	{
       
    42 	CT_RConnectionData * ret = new (ELeave)CT_RConnectionData();
       
    43 	CleanupStack::PushL(ret);
       
    44 	ret->ConstructL();
       
    45 	CleanupStack::Pop(ret);
       
    46 	return ret;
       
    47 	}
       
    48 
       
    49 CT_RConnectionData::~CT_RConnectionData()
       
    50 	{
       
    51 		
       
    52 	if (iCommDbConnPref)
       
    53 		{
       
    54 		delete iCommDbConnPref;
       
    55 		iCommDbConnPref = NULL;
       
    56 		}
       
    57 	if (iConnection)
       
    58 		{
       
    59 		delete iConnection;
       
    60 		iConnection = NULL;
       
    61 		}
       
    62 	}
       
    63 
       
    64 CT_RConnectionData::CT_RConnectionData()
       
    65 :	iConnection(NULL),
       
    66 	iCommDbConnPref(NULL),
       
    67 	iIAP(0)
       
    68 	{
       
    69 	
       
    70 	}
       
    71 void CT_RConnectionData::ConstructL()
       
    72 	{
       
    73 	iConnection = new (ELeave)RConnection();
       
    74 	}
       
    75 
       
    76 void CT_RConnectionData::RunL(CActive* aActive, TInt aIndex)
       
    77     {
       
    78     INFO_PRINTF1(_L("*START* CT_RSocketData::RunL"));
       
    79     DecOutstanding(); // One of the async calls has completed 
       
    80     
       
    81 	if(aActive == iActiveCallback)
       
    82 		{
       
    83 		INFO_PRINTF1(_L("Asynchronous task has completed. RunL  called"));
       
    84 		}
       
    85 	else
       
    86 		{ 
       
    87 		ERR_PRINTF1(_L("Stray RunL signal"));
       
    88 		TInt err = aActive->iStatus.Int(); 
       
    89 		if( err != KErrNone )
       
    90 			{
       
    91 	        ERR_PRINTF2(_L("RunL Error %d"), err); 
       
    92 	        SetAsyncError( aIndex, err );
       
    93 			}
       
    94 		}
       
    95 	INFO_PRINTF1(_L("*END* CT_RSocketData::RunL"));
       
    96 	}
       
    97     
       
    98 
       
    99 /**
       
   100  * Return a pointer to the object that the data wraps
       
   101  *
       
   102  * @return	pointer to the object that the data wraps
       
   103  */
       
   104 TAny* CT_RConnectionData::GetObject()
       
   105 	{
       
   106 	 return iConnection;
       
   107 	}
       
   108 
       
   109 
       
   110 /**
       
   111  * Process a command read from the ini file
       
   112  *
       
   113  * @param aCommand			The command to process
       
   114  * @param aSection			The section in the ini containing data for the command
       
   115  * @param aAsyncErrorIndex	Command index for async calls to return errors to
       
   116  *
       
   117  * @return					ETrue if the command is processed
       
   118  *
       
   119  */
       
   120 TBool CT_RConnectionData::DoCommandL(const TTEFFunction& aCommand, const TTEFSectionName& aSection, const TInt /*aAsyncErrorIndex*/)
       
   121 	{
       
   122 	TBool ret = ETrue;
       
   123 	if(aCommand == KCmdOpen)
       
   124 		{
       
   125 		DoCmdOpenL(aSection);
       
   126 		}
       
   127 	else if(aCommand == KCmdStart)
       
   128 		{
       
   129 		DoCmdStart(aSection);
       
   130 		}
       
   131 	else if ( aCommand == KCmdStop)
       
   132 		{
       
   133 		DoCmdStop();
       
   134 		}
       
   135 	else if(aCommand == KCmdClose)
       
   136 		{
       
   137 		DoCmdClose();
       
   138 		}
       
   139 	else if (aCommand == KCmdSelectOutgoingIAP)
       
   140 		{
       
   141 		DoCmdSelectOutgoingIAPL(aSection);		
       
   142 		}
       
   143 	else
       
   144 		{
       
   145 		ERR_PRINTF1(_L("Unknown command"));
       
   146 		ret= EFalse;
       
   147 		}
       
   148 	
       
   149 	return ret;
       
   150 	}
       
   151 
       
   152 /**
       
   153  * 	Opens a RConnection to connect to an Internet Access Point.
       
   154  * 	@param aSection - The section in config file to look for the Access Point name and RSocketServ  
       
   155  */
       
   156 void CT_RConnectionData::DoCmdOpenL(const TTEFSectionName& aSection)
       
   157     {
       
   158     INFO_PRINTF1(_L("*START* CT_RConnectionData::DoCmdOpenL"));
       
   159     
       
   160     TBool dataOk = ETrue;
       
   161     
       
   162     TPtrC accessPointName;    
       
   163     if(!GetStringFromConfig(aSection, KIAPName, accessPointName))
       
   164     	{
       
   165     	ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KIAPName);
       
   166     	SetBlockResult(EFail);
       
   167     	dataOk = EFalse;
       
   168     	}
       
   169     else
       
   170     	{
       
   171     	INFO_PRINTF2(_L("Connect to IAP \"%S\""), &accessPointName);
       
   172         }
       
   173         
       
   174     TPtrC socketServName;
       
   175     // Open RConnection for CommsDat IAP selection and connection information reading.
       
   176 	if(!GetStringFromConfig(aSection, KRSocketServ, socketServName))
       
   177 		{
       
   178 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KRSocketServ);
       
   179     	SetBlockResult(EFail);
       
   180     	dataOk = EFalse;
       
   181 		}
       
   182 	
       
   183     TInt error = KErrNone;
       
   184     
       
   185     if (dataOk)
       
   186     	{
       
   187     	RSocketServ* socketServ=static_cast<RSocketServ*>(GetDataObjectL(socketServName));
       
   188     	error = iConnection->Open(*socketServ, KAfInet);
       
   189     	if(error == KErrNone)
       
   190     		{
       
   191     		INFO_PRINTF1(_L("RConnection was opened successfully"));		
       
   192     		}
       
   193     	else
       
   194     		{
       
   195     		ERR_PRINTF2(_L("Failed to open RConnection with error [%d]"),error);
       
   196     		SetError(error);
       
   197     		}
       
   198     	}
       
   199     INFO_PRINTF1(_L("*END* CT_RConnectionData::DoCmdOpenL"));
       
   200 	}
       
   201 
       
   202 /**
       
   203  *	Selects an outgoing Internet Access Point.
       
   204  * 	@param aSection - Section from config file to read the IAP's name.
       
   205  */
       
   206 void CT_RConnectionData::DoCmdSelectOutgoingIAPL(const TTEFSectionName& aSection)
       
   207 	{
       
   208     INFO_PRINTF1(_L("*START* CT_RConnectionData::DoCmdSelectOutgoingIAPL"));
       
   209 
       
   210     TBool dataOk = ETrue;
       
   211     
       
   212 	TPtrC accessPointName;    
       
   213     if(!GetStringFromConfig(aSection, KIAPName, accessPointName))
       
   214     	{
       
   215     	ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KIAPName);
       
   216     	SetBlockResult(EFail);
       
   217     	dataOk = EFalse;
       
   218     	}
       
   219     
       
   220     TInt error = KErrNone;
       
   221     
       
   222     if (dataOk)
       
   223     	{
       
   224     	// Search for IAP from CommsDat.
       
   225     	TRAP(error, iCommDbConnPref = SelectOutgoingIAPL(accessPointName))
       
   226 		if (error != KErrNone)
       
   227 			{
       
   228 			ERR_PRINTF2(_L("Failed to select IAP [%d]"), error);
       
   229 			SetError(error);
       
   230 			}
       
   231     	}	
       
   232     INFO_PRINTF1(_L("*END* CT_RConnectionData::DoCmdSelectOutgoingIAPL"));
       
   233 	}
       
   234 
       
   235 /**
       
   236  * 	Starts RConnection using CActiveCallback.
       
   237  * 
       
   238  * 	@param aSection 		-	Section in config file to look for number of retries.
       
   239  * 
       
   240  */
       
   241 void CT_RConnectionData::DoCmdStart(const TTEFSectionName& aSection)
       
   242 	{
       
   243     INFO_PRINTF1(_L("*START* CT_RConnectionData::DoCmdStart"));
       
   244 	
       
   245     TBool dataOk = ETrue;
       
   246     
       
   247 	TInt retries = 1;
       
   248     if(!GetIntFromConfig(aSection, KRetries, retries ))
       
   249     	{
       
   250     	ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KRetries);
       
   251     	SetBlockResult(EFail);
       
   252     	dataOk = EFalse;
       
   253     	}
       
   254 	// If IAP was found, start RConnection with that IAPs preferences (ID and No dialog shown)
       
   255 	// if no IAP was found return with error code.
       
   256 	if (dataOk && iCommDbConnPref)
       
   257 		{
       
   258 		INFO_PRINTF2(_L("Connecting to the IAP with [%d] tries"), retries);
       
   259 
       
   260 		// Wait before the connection is really made, this is because
       
   261 		// the DOS seems to need some time to get ready for data calls.
       
   262 		// Wait time is 8 seconds.
       
   263 		const TInt KTimeBeforeConnection = 8000000;
       
   264         // In future releases this should be checked on time to time
       
   265         // Heavy testing is needed before this pause can be changed.
       
   266 		User::After(KTimeBeforeConnection);
       
   267 
       
   268 		TRequestStatus status;
       
   269 
       
   270         // Try connection more than once to avoid operator problems
       
   271 		INFO_PRINTF1(_L("Try connection more than once to avoid operator problems"));
       
   272 		TInt i = 0;
       
   273         do	{
       
   274             iConnection->Start(*iCommDbConnPref,status);
       
   275             INFO_PRINTF2(_L("Status = [%d]"),status.Int());
       
   276             User::WaitForRequest(status);
       
   277     	    }
       
   278         while(status.Int() != KErrNone && i++ < retries);
       
   279 
       
   280 		}    
       
   281 	
       
   282     INFO_PRINTF1(_L("*END* CT_RConnectionData::DoCmdStart"));
       
   283     }
       
   284 
       
   285 
       
   286 
       
   287 /**
       
   288  * Stops RConnection.
       
   289  * Sets TEF error if not successful.
       
   290  */
       
   291 void CT_RConnectionData::DoCmdStop()
       
   292     {
       
   293     INFO_PRINTF1(_L("*START* CT_RConnectionData::DoCmdStop"));
       
   294 
       
   295     TInt error = iConnection->Stop(RConnection::EStopNormal);
       
   296     if (error != KErrNone)
       
   297         {
       
   298         ERR_PRINTF2(_L("Failed to stop RConnection [%d]"), error);
       
   299         SetError(error);
       
   300         }
       
   301     
       
   302     INFO_PRINTF1(_L("*END* CT_RConnectionData::DoCmdStop"));
       
   303     }
       
   304 
       
   305 /**
       
   306  * Closes RConnection.
       
   307  * Sets TEF error if not successful.
       
   308  */
       
   309 void CT_RConnectionData::DoCmdClose()
       
   310 	{
       
   311 	INFO_PRINTF1(_L("*START* CT_RConnectionData::DoCmdClose"));
       
   312 	
       
   313 	iConnection->Close();
       
   314 	
       
   315 	INFO_PRINTF1(_L("RConnection is closed"));
       
   316 
       
   317 	INFO_PRINTF1(_L("*END* CT_RConnectionData::DoCmdClose"));
       
   318 	}
       
   319 
       
   320 
       
   321 /**
       
   322  *  Select the outgoing IAP
       
   323  * 	@param TDesC IAP name
       
   324  * 	@return Pointer to TCommDbConnPref
       
   325  */
       
   326 TCommDbConnPref* CT_RConnectionData::SelectOutgoingIAPL(const TDesC& aIAPName)
       
   327 	{
       
   328 	const TUint32 KIAPMask = 0xffffffff;
       
   329 	
       
   330 	INFO_PRINTF1(_L("Connecting to CommsDat. This will leave with KErrNotSupported if it does not exist"));
       
   331 	
       
   332 	// Open a connection to the CommsDat.
       
   333 	CCommsDatabase* commsDat = CCommsDatabase::NewL(ETrue);
       
   334 	CleanupStack::PushL(commsDat);
       
   335 	
       
   336 	INFO_PRINTF1(_L("Connected to CommsDat"));
       
   337 	INFO_PRINTF1(_L("Searching for IAPs"));
       
   338 	
       
   339 	// Open a view from CommsDat IAP table listing all outgoing IAPs.
       
   340 	CCommsDbTableView* searchView = commsDat->OpenIAPTableViewMatchingBearerSetLC(KIAPMask,
       
   341 												ECommDbConnectionDirectionOutgoing);
       
   342 	
       
   343 	// Go to first record in the view.
       
   344 	TInt error = searchView->GotoFirstRecord();
       
   345 	
       
   346 	// Buffer for IAP names in the view.
       
   347 	TBuf<KCommsDbSvrMaxColumnNameLength> iapName;
       
   348 	TUint32 iapID = 0;
       
   349 	TBool found = EFalse;
       
   350 	
       
   351 	INFO_PRINTF1(_L("Searching through IAPs"));
       
   352 	
       
   353 	// Read IAPs until correct one is found or all IAPs are browsed through.
       
   354 	while (error == KErrNone)
       
   355 		{
       
   356 		// Read the IAP name from CommsDat.
       
   357 		iapName.FillZ();
       
   358 		searchView->ReadTextL(TPtrC(COMMDB_NAME), iapName);
       
   359 	    searchView->ReadUintL( TPtrC( COMMDB_ID ), iapID );
       
   360 	
       
   361 	    INFO_PRINTF3(_L( "IAP (ID = %d): \"%S\""), iapID, &iapName );
       
   362 	
       
   363 		// Try to match the name with user input.
       
   364 		if (iapName.Match(aIAPName) == KErrNone)
       
   365 			{
       
   366 			searchView->ReadUintL(TPtrC(COMMDB_ID), iIAP);
       
   367 	        found = ETrue;
       
   368 			}
       
   369 	
       
   370 		error = searchView->GotoNextRecord();
       
   371 		}
       
   372 	
       
   373 	// CommsDat and search view.
       
   374 	CleanupStack::PopAndDestroy(searchView);
       
   375 	CleanupStack::PopAndDestroy(commsDat);
       
   376 
       
   377 	// no IAP found.
       
   378 	if (!found)
       
   379 		{
       
   380 		ERR_PRINTF3(_L("No IAP found with name \"%S\" [%d]"), &aIAPName, error);
       
   381 		User::Leave(error);
       
   382 		}
       
   383 	else
       
   384 	    {
       
   385 		INFO_PRINTF2(_L("IAP found with ID \"%d\""), iIAP);
       
   386 	    }
       
   387 	
       
   388 	// Construct a CommsDat connection preferences structure.
       
   389 	TCommDbConnPref* commDbConnPref = new (ELeave) TCommDbConnPref;
       
   390 	
       
   391 	// Connection preferences IAP Id, Network Id, Dialog Preference, Direction, Bearer Set
       
   392 	// can be set through commDbConnPref.
       
   393 	
       
   394 	// Set only the values we use and leave rest untouched .
       
   395 	commDbConnPref->SetIapId(iIAP);
       
   396 	commDbConnPref->SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
       
   397 	commDbConnPref->SetBearerSet(KCommDbBearerUnknown);
       
   398 	
       
   399 	INFO_PRINTF1(_L("Constructed TCommDbConnPref for the IAP ID"));
       
   400 	
       
   401 	return commDbConnPref;
       
   402 	}
       
   403