cellularsrvapitest/datatransferhaitest/esock/src/T_RSocketData.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_RSocketData.h"
       
    20 #include "T_RHostResolverData.h"
       
    21 #include <e32property.h>
       
    22 #include <etelmm.h>   // RMobilePhone
       
    23 #include <etelpckt.h> // RPacketService
       
    24 
       
    25 
       
    26 const TInt KBufferSize = 1024;
       
    27 _LIT( KNoFileSave, "NULL" );
       
    28 const TUint KNoDataDealer = 0;
       
    29 const TInt KNoDuration = -1;
       
    30 const TInt KNoSpeedCheck = -1;
       
    31 
       
    32 // Misc Types
       
    33 _LIT( KProtocolHTTP,   "HTTP" );
       
    34 _LIT( KOff,            "Off" );        // Can be used every2*where off is needed
       
    35 
       
    36 // HSDPA checks
       
    37 _LIT( KHsdpaIndicator, "Indicator" );  // Operator supports
       
    38 _LIT( KHsdpaSupported, "Supported" );  // Device supports
       
    39 _LIT( KHsdpaInUse,     "Used" );       // In use during PSD
       
    40 
       
    41 // Constants for creating a HTTP request.
       
    42 _LIT8(KHTTPGET, 		"GET");
       
    43 _LIT8(KHTTPSeparator, 	" ");
       
    44 _LIT8(KHTTPSuffix, 		"HTTP/1.0");
       
    45 _LIT8(KEmptyLine, 		"\r\n\r\n");
       
    46 const TInt KEmptySize = 4;
       
    47 
       
    48 const TInt KHTTPSize = 17;
       
    49 const TInt KMaxTag = 256;
       
    50 const TInt KHeaderWithoutData=200;
       
    51 //
       
    52 // Manually created HTTP Post request is difficult to maintain.
       
    53 // Request and server responce is logged into file during test run.
       
    54 _LIT8(KHTTPPOST, "POST");
       
    55 _LIT8(KLineBreak, "\r\n");
       
    56 
       
    57 _LIT8(KAhti, "Ahti");
       
    58 
       
    59 _LIT8(KServerScript, 	"/dy/upload/upfile.php");
       
    60 _LIT8(KFrom, 			"From:");
       
    61 _LIT8(KHost, 			"Host:");
       
    62 _LIT8(KContentType, 	"Content-Type:");
       
    63 _LIT8(KContentLength, 	"Content-Length:");
       
    64 _LIT8(KContentDisposition, "Content-Disposition:");
       
    65 
       
    66 _LIT8(KMultipartType, "multipart/form-data;");
       
    67 _LIT8(KOctetType,     "application/octet-stream");
       
    68 _LIT8(KBoundary,      "boundary=---------------------------sg976436h73");
       
    69 _LIT8(KBoundaryStart, "-----------------------------sg976436h73");
       
    70 _LIT8(KBoundaryEnd,   "-----------------------------sg976436h73--");
       
    71 _LIT8(KDisposition,   "form-data; name=\"userfile\"; filename=\"test.bin\"");
       
    72 
       
    73 _LIT( KPropertyCategory, 		"PropertyCategory");
       
    74 _LIT( KPropertyKey,				"PropertyKey");
       
    75 _LIT( KMaxPropertyLength,		"PropertyLength");
       
    76 
       
    77 //const TInt 
       
    78 _LIT( KPort,         					"Port" );
       
    79 _LIT( KAddress,      					"Address" );
       
    80 _LIT( KProtocol,     					"Protocol" );
       
    81 _LIT( KFileName,     					"File" );
       
    82 _LIT( KSaveName,     					"Save" );
       
    83 _LIT( KDataDealerID, 					"DataDealer" );    // Data dealer ID is given may not be 0
       
    84 _LIT( KHsdpaCheck,   					"HSDPA" );         // HSDPA check can be done several ways
       
    85 _LIT( KDuration,     					"Duration" );      // How long should i.e. download last when it is stopped (THRO)
       
    86 _LIT( KMinimumSpeed, 					"MinSpeed" );      // Data transfer must be faster than MinSpeed
       
    87 _LIT( KBytes,        					"Bytes" );         // How many bytes are send
       
    88 _LIT( KResponseLogFile,					"ResponseLogFile");
       
    89 _LIT( KRequestLogFile,					"RequestLogFile");
       
    90 //LIT's to read from ini file
       
    91 _LIT( KDefaultSection,					"Default");
       
    92 _LIT(KRConnection, 						"RConnection");
       
    93 _LIT(KRSocketServ,						"RSocketServ");
       
    94 _LIT(KRHostResolver,					"RHostResolver");
       
    95 _LIT(KRMobilePhone,						"RMobilePhone");
       
    96 
       
    97 /*@}*/
       
    98 
       
    99 /*@{*/
       
   100 //LIT's for commands
       
   101 _LIT(KCmdRSocketOpen,							"Open");
       
   102 _LIT(KCmdRSocketClose,							"Close");
       
   103 _LIT(KCmdRSocketConnect, 						"Connect");
       
   104 _LIT(KCmdRSocketShutDown,						"ShutDown");
       
   105 _LIT(KCmdRSocketWrite,							"Write");
       
   106 _LIT(KCmdResponseFromServer, 					"ResponseFromServer");
       
   107 _LIT(KWriteRequest,								"WriteRequest");
       
   108 _LIT(KCmdUtilityStartFile,						"StartFile");
       
   109 _LIT(KCmdUtilityStartDealer,					"StartDealer");
       
   110 _LIT(KCmdUtilityCheckHSDPA,						"CheckHSDPA");
       
   111 _LIT(KCmdRecvOneOrMore,							"RecvOneOrMore");
       
   112 _LIT(KCmdUtilityEndFile,						"EndFile");
       
   113 _LIT(KCmdUtilityCalcDnloadStats,				"CalcDnloadStats");
       
   114 _LIT(KCmdUtilityBuildRequest,					"BuildRequest");
       
   115 _LIT(KCmdSendBytesToSocket,						"SendBytesToSocket");
       
   116 _LIT(KCmdUtilityEndRequest,						"EndRequest");
       
   117 _LIT(KCmdInfo,									"Info");
       
   118 /*@}*/
       
   119 
       
   120 
       
   121 /**
       
   122  * Two phase constructor
       
   123  */
       
   124 CT_RSocketData* CT_RSocketData::NewL()
       
   125 	{
       
   126 	CT_RSocketData*	ret=new (ELeave) CT_RSocketData();
       
   127 	CleanupStack::PushL(ret);
       
   128 	ret->ConstructL();	
       
   129 	CleanupStack::Pop(ret);
       
   130 	return ret;
       
   131 	}
       
   132 
       
   133 /**
       
   134 * Protected constructor. First phase construction
       
   135 */
       
   136 CT_RSocketData::CT_RSocketData():
       
   137 	iSocket(NULL),    
       
   138 	iActiveCallback(NULL),
       
   139 	iBreathSaver(NULL)
       
   140 	{
       
   141 	}
       
   142 
       
   143 CT_RSocketData::~CT_RSocketData()
       
   144 	{
       
   145 	if(iBreathSaver)
       
   146 		{
       
   147 		delete iBreathSaver;
       
   148 		iBreathSaver = NULL;
       
   149 		}
       
   150 	if(iActiveCallback)
       
   151 		{
       
   152 		delete iActiveCallback;
       
   153 		iActiveCallback = NULL;
       
   154 		}	
       
   155 	if(iSocket)
       
   156 		{
       
   157 		delete iSocket;
       
   158 		iSocket = NULL;
       
   159 		}
       
   160 	}
       
   161 
       
   162 void CT_RSocketData::ConstructL()
       
   163 	{
       
   164 	iActiveCallback = CActiveCallback::NewL(*this);
       
   165 	iBreathSaver = CBreathSaver::NewL();
       
   166 	iSocket = new (ELeave)RSocket();
       
   167 	}
       
   168 
       
   169 void CT_RSocketData::InitialiseL()
       
   170 	{
       
   171 	CDataWrapperBase::InitialiseL();
       
   172 	
       
   173 	TInt propertyCategory;
       
   174 	GetHexFromConfig(KDefaultSection, KPropertyCategory, propertyCategory);
       
   175 	iPropertyCategory.iUid = propertyCategory;
       
   176 	
       
   177 	TInt propertyKey;
       
   178 	GetIntFromConfig(KDefaultSection, KPropertyKey, propertyKey );
       
   179 	iPropertyKey =(TUint)propertyKey;
       
   180 	
       
   181 	GetIntFromConfig(KDefaultSection, KMaxPropertyLength, iMaxPropertyLength );
       
   182 	
       
   183 	}
       
   184 
       
   185 
       
   186 
       
   187 /**
       
   188 	* Process a command read from the ini file
       
   189 	*
       
   190 	* @param aCommand			The command to process
       
   191 	* @param aSection			The section in the ini containing data for the command
       
   192 	* @param aAsyncErrorIndex	Command index for async calls to return errors to
       
   193 	*
       
   194 	* @return					ETrue if the command is processed
       
   195 	*
       
   196 	* @leave					System wide error
       
   197 	*/
       
   198 TBool CT_RSocketData::DoCommandL(const TTEFFunction& aCommand, const TTEFSectionName& aSection, const TInt aAsyncErrorIndex)
       
   199 	{
       
   200 	TBool retVal=ETrue;
       
   201 	
       
   202 	if (aCommand==KCmdRSocketOpen)
       
   203 		{	
       
   204 		DoCmdOpenL(aSection);
       
   205 		}
       
   206 	else if (aCommand==KCmdRSocketConnect)
       
   207 		{
       
   208 		DoCmdConnectL(aSection, aAsyncErrorIndex);
       
   209 		}
       
   210 	else if (aCommand==KCmdRSocketShutDown)
       
   211 		{
       
   212 		DoCmdShutDown(aAsyncErrorIndex);
       
   213 		}
       
   214 	else if (aCommand==KCmdRSocketClose)
       
   215 		{
       
   216 		DoCmdClose();
       
   217 		}
       
   218 	else if ( aCommand == KWriteRequest )
       
   219 		{		
       
   220 		DoCmdWriteRequest(aSection, aAsyncErrorIndex);
       
   221 		}
       
   222 	else if ( aCommand == KCmdUtilityStartFile )
       
   223 		{
       
   224 		DoCmdUtilityStartFile(aSection);
       
   225 		}
       
   226 	else if (aCommand == KCmdUtilityStartDealer )
       
   227 		{
       
   228 		DoCmdUtilityStartDealer(aSection);
       
   229 		}
       
   230 	else if (aCommand == KCmdUtilityCheckHSDPA )
       
   231 		{
       
   232 		DoCmdUtilityCheckHSDPAL(aSection);
       
   233 		}
       
   234 	else if (aCommand==KCmdRecvOneOrMore)
       
   235 		{
       
   236 		DoCmdRecvOneOrMore(aSection);
       
   237 		}
       
   238 	else if (aCommand == KCmdUtilityEndFile )
       
   239 		{
       
   240 		DoCmdUtilityEndFile(aSection);		
       
   241 		}
       
   242 	else if (aCommand == KCmdUtilityCalcDnloadStats )
       
   243 		{
       
   244 		DoCmdUtilityCalculateDownloadStats(aSection);		
       
   245 		}
       
   246 	else if (aCommand==KCmdUtilityBuildRequest)
       
   247 		{
       
   248 		DoCmdUtilityBuildRequest(aSection);
       
   249 		}
       
   250 	else if (aCommand==KCmdRSocketWrite)
       
   251 		{
       
   252 		DoCmdWrite(aAsyncErrorIndex);
       
   253 		}
       
   254 	else if (aCommand==KCmdSendBytesToSocket)
       
   255 		{
       
   256 		DoCmdSendBytesToSocket(aSection);
       
   257 		}
       
   258 	else if (aCommand==KCmdResponseFromServer)
       
   259 		{
       
   260 		DoCmdResponseFromServer(aSection);
       
   261 		}
       
   262 	else if (aCommand==KCmdUtilityEndRequest)
       
   263 		{
       
   264 		DoCmdUtilityEndRequest();
       
   265 		}
       
   266 	else if (aCommand==KCmdInfo)
       
   267 		{
       
   268 		DoCmdInfoL(aSection);
       
   269 		}
       
   270 	else
       
   271 		{
       
   272 		ERR_PRINTF1(_L("Unknown command"));
       
   273 		retVal=EFalse;
       
   274 		}
       
   275 
       
   276 	return retVal;
       
   277 	}
       
   278 	
       
   279 /**
       
   280 	* Return a pointer to the object that the data wraps
       
   281 	*
       
   282 	* @return pointer to the object that the data wraps
       
   283 	*/
       
   284 TAny* CT_RSocketData::GetObject()
       
   285 	{
       
   286 	return iSocket;
       
   287 	}
       
   288 	
       
   289 
       
   290 void CT_RSocketData::RunL(CActive* aActive, TInt aIndex)
       
   291 	{
       
   292 	INFO_PRINTF1(_L("*START* CT_RSocketData::RunL"));
       
   293 	DecOutstanding(); // One of the async calls has completed 
       
   294     
       
   295 	if(aActive == iActiveCallback)
       
   296 		{
       
   297 		INFO_PRINTF1(_L("Asynchronous task has completed. RunL  called"));
       
   298 		}
       
   299 	else
       
   300 		{ 
       
   301 		ERR_PRINTF1(_L("Stray RunL signal"));
       
   302 		TInt err = aActive->iStatus.Int(); 
       
   303 		if( err != KErrNone )
       
   304 			{
       
   305 	        ERR_PRINTF2(_L("RunL Error %d"), err); 
       
   306 	        SetAsyncError( aIndex, err );
       
   307 			}
       
   308 		}
       
   309 	
       
   310 	INFO_PRINTF1(_L("*END* CT_RSocketData::RunL"));
       
   311 	}
       
   312 	
       
   313 /**
       
   314  * Opens a socket.
       
   315  * @param aSection - Section in config file to get a RSocketServer and RConnection
       
   316  * Sets TEF error if not successful.
       
   317  */	
       
   318 void CT_RSocketData::DoCmdOpenL(const TTEFSectionName& aSection)
       
   319 	{
       
   320 	INFO_PRINTF1(_L("*START* CT_RSocketData::DoCmdOpenL"));
       
   321 	TBool dataOk = ETrue;
       
   322 	
       
   323 	TPtrC connectionName;
       
   324 	if ( !GetStringFromConfig(aSection, KRConnection(), connectionName) )
       
   325 		{
       
   326 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KRConnection);
       
   327 		SetBlockResult(EFail);
       
   328 		dataOk = EFalse;
       
   329 		}
       
   330 
       
   331 	TPtrC socketServName;
       
   332 	if ( !GetStringFromConfig(aSection, KRSocketServ(), socketServName) )
       
   333 		{
       
   334 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KRSocketServ);
       
   335 		SetBlockResult(EFail);
       
   336 		dataOk = EFalse;
       
   337 		}
       
   338 
       
   339 	if (dataOk)
       
   340 		{
       
   341 		INFO_PRINTF1(_L("Create a RConnection for iSocket"));
       
   342 		RConnection* connection = static_cast<RConnection*>(GetDataObjectL(connectionName));
       
   343 		INFO_PRINTF1(_L("Create a RSocketServ for iSocket"));
       
   344 		RSocketServ* socketServ = static_cast<RSocketServ*>(GetDataObjectL(socketServName));
       
   345 
       
   346 		TInt error = iSocket->Open(*socketServ, KAfInet, KSockStream, KProtocolInetTcp, *connection);
       
   347 		
       
   348 		if (error == KErrNone)
       
   349 			{
       
   350 			INFO_PRINTF1(_L("Socket opening succeeded"));
       
   351 			}
       
   352 		else
       
   353 			{
       
   354 			ERR_PRINTF2(_L("Socket opening failed [%d]"), error);
       
   355 			SetError(error);
       
   356 			}
       
   357 		}
       
   358 
       
   359 	INFO_PRINTF1(_L("*END* CT_RSocketData::DoCmdOpenL"));
       
   360 	}
       
   361 
       
   362 /**
       
   363  * Connects socket with a given address and port.
       
   364  * @param aSection			- The section in config file to look for the address, port and RHostResolver
       
   365  * @param aAsyncErrorIndex	- Command index for async calls to return errors to	
       
   366  */
       
   367 void CT_RSocketData::DoCmdConnectL(const TTEFSectionName& aSection, const TInt aAsyncErrorIndex)
       
   368 	{
       
   369 	INFO_PRINTF1(_L("*START* CT_RSocketData::DoCmdConnectL"));
       
   370 	TBool dataOk = ETrue;
       
   371 	
       
   372 	TInt port;
       
   373     if(!GetIntFromConfig( aSection, KPort, port ))
       
   374     	{
       
   375     	ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KPort);
       
   376     	SetBlockResult(EFail);
       
   377     	dataOk = EFalse;
       
   378     	}
       
   379     
       
   380     TPtrC address;
       
   381     if(!GetStringFromConfig( aSection, KAddress, address ))
       
   382     	{
       
   383     	ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KAddress);
       
   384     	SetBlockResult(EFail);
       
   385     	dataOk = EFalse;
       
   386     	}
       
   387     
       
   388 	// Connect ready socket to given host and port number
       
   389 	TPtrC hostResolverName;
       
   390 	if(	!GetStringFromConfig(aSection, KRHostResolver, hostResolverName) )
       
   391 		{
       
   392 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KRHostResolver);
       
   393     	SetBlockResult(EFail);
       
   394     	dataOk = EFalse;
       
   395 		}
       
   396 
       
   397 	if (dataOk)
       
   398 		{
       
   399 		INFO_PRINTF3(_L("Connecting socket to server \"%S\" at port [%d]"), &address, port);
       
   400 		
       
   401 		CT_RHostResolverData* hostResolver = static_cast<CT_RHostResolverData*>(GetDataWrapperL(hostResolverName));
       
   402 		iNameEntry = hostResolver->GetNameEntry();
       
   403 		iNameEntry().iAddr.SetPort(port);
       
   404 		iSocket->Connect(iNameEntry().iAddr, iActiveCallback->iStatus);
       
   405 		iActiveCallback->Activate(aAsyncErrorIndex);
       
   406 	    IncOutstanding();
       
   407 		}
       
   408 
       
   409     INFO_PRINTF1(_L("*END* CT_RSocketData::DoCmdConnectL"));
       
   410     }
       
   411 
       
   412 /**
       
   413  * Shuts down a connected socket - asynchronous.
       
   414  * This method is asynchronous as non emergency shutdown may take a while.
       
   415  * The shut down method allows input and output to be individually stopped for a protocol endpoint. 
       
   416  * @param aAsyncErrorIndex - Command index for async calls to return errors to
       
   417  */
       
   418 void CT_RSocketData::DoCmdShutDown(const TInt aAsyncErrorIndex)
       
   419     {
       
   420     INFO_PRINTF1(_L("*START* CT_RSocketData::DoCmdShutDownSocket"));
       
   421 
       
   422 	// Shutdown the socket.
       
   423 	iSocket->Shutdown(RSocket::ENormal, iActiveCallback->iStatus);
       
   424 	iActiveCallback->Activate(aAsyncErrorIndex);
       
   425     IncOutstanding();
       
   426 
       
   427     INFO_PRINTF1(_L("*END* CT_RSocketData::DoCmdShutDownSocket"));
       
   428     }
       
   429 
       
   430 /**
       
   431  * Closes a socket.
       
   432  * If a socket has been opened using Open() then it should be closed using Close(). 
       
   433  * This will ensure all associated resources are released. 
       
   434  * Closing serves two distinct purposes: 
       
   435  * - To release resources associated with the IPC channel to the socket server. 
       
   436  * - To disconnect a socket if it is connected. 
       
   437  */
       
   438 void CT_RSocketData::DoCmdClose()
       
   439 	{
       
   440 	INFO_PRINTF1(_L("*START* CT_RSocketData::DoCmdCloseSocket"));
       
   441 	iSocket->Close(); // Close does not return error code
       
   442     INFO_PRINTF1(_L("*END* CT_RSocketData::DoCmdCloseSocket"));
       
   443     }
       
   444 
       
   445 
       
   446 /**
       
   447  * Builds an HTTP Get request
       
   448  * @param  aSection - The section in config file to look for address, port, protocol, filename and save path.
       
   449  * @param aAsyncErrorIndex - Command index for async calls to return errors to
       
   450  */
       
   451 void CT_RSocketData::DoCmdWriteRequest(const TTEFSectionName& aSection, const TInt aAsyncErrorIndex)
       
   452 	{
       
   453 	INFO_PRINTF1(_L("*START* CT_RSocketData::DoCmdWriteRequest"));
       
   454 	TBool dataOk = ETrue;
       
   455 
       
   456 	TPtrC filename; 
       
   457 	if(!GetStringFromConfig(aSection, KFileName, filename ))
       
   458 		{
       
   459 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KFileName);
       
   460     	SetBlockResult(EFail);
       
   461     	dataOk = EFalse;
       
   462 		}
       
   463 	
       
   464 	TPtrC protocol; 
       
   465     if(!GetStringFromConfig(aSection, KProtocol, protocol ))
       
   466     	{
       
   467     	ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KProtocol);
       
   468     	SetBlockResult(EFail);
       
   469     	dataOk = EFalse;
       
   470     	}
       
   471     
       
   472     
       
   473     // Send http get request
       
   474     if (dataOk && protocol != KProtocolHTTP)
       
   475         {
       
   476         ERR_PRINTF2(_L("Protocol \"%S\" is unknown"), &protocol);
       
   477         SetBlockResult(EFail);
       
   478         dataOk = EFalse;
       
   479         }
       
   480 
       
   481     if (dataOk && filename.Length() > KMaxTag)
       
   482         {
       
   483         ERR_PRINTF1(_L("Filename is too long, cannot send HTTP request"));
       
   484         SetBlockResult(EFail);
       
   485         dataOk = EFalse;
       
   486         }
       
   487 
       
   488     if (dataOk)
       
   489     	{
       
   490         INFO_PRINTF1(_L("Sending HTTP request"));
       
   491 
       
   492     	// Buffer that will hold the request.
       
   493     	TBuf8<KHTTPSize + KMaxTag> request;
       
   494 
       
   495     	// Construct the final request.
       
   496     	request.Append(KHTTPGET);
       
   497     	request.Append(KHTTPSeparator);
       
   498     	request.Append(filename);
       
   499     	request.Append(KHTTPSeparator);
       
   500     	request.Append(KHTTPSuffix);
       
   501     	request.Append(KEmptyLine);
       
   502 
       
   503         // Send the request through socket
       
   504     	iSocket->Write(request, iActiveCallback->iStatus);
       
   505     	iActiveCallback->Activate(aAsyncErrorIndex);
       
   506         IncOutstanding();
       
   507     	}
       
   508 
       
   509 	INFO_PRINTF1(_L("*END* CT_RSocketData::DoCmdWriteRequest"));
       
   510 	}
       
   511 
       
   512 
       
   513 /**
       
   514  * Uses helper class BreathSaver to generate a file and save downloaded data in a given path.
       
   515  * @param  aSection - The section in config file to look for address, port, protocol, filename and save path.
       
   516  * Sets TEF error if not successful.
       
   517  */
       
   518 void CT_RSocketData::DoCmdUtilityStartFile(const TTEFSectionName& aSection)
       
   519 	{
       
   520 	INFO_PRINTF1(_L("*START* CT_RSocketData::DoCmdUtilityStartFile"));
       
   521 	TBool dataOk = ETrue;
       
   522 	
       
   523     TPtrC savename( KNoFileSave );
       
   524     if(!GetStringFromConfig(aSection, KSaveName, savename))
       
   525     	{
       
   526     	ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KSaveName);
       
   527     	SetBlockResult(EFail);
       
   528     	dataOk = EFalse;
       
   529     	}
       
   530 	
       
   531     if ( dataOk && savename == KNoFileSave)
       
   532     	{
       
   533     	ERR_PRINTF1(_L("Incorrect file name"));
       
   534     	SetBlockResult(EFail);
       
   535     	dataOk = EFalse;
       
   536     	}
       
   537     
       
   538 	if (dataOk)
       
   539 		{
       
   540 		INFO_PRINTF2(_L("Save incoming file to \"%S\""), &savename);
       
   541 		TInt error = iBreathSaver->StartFile(savename);
       
   542 	    
       
   543 	    if (error != KErrNone)
       
   544 	        {
       
   545 	        ERR_PRINTF2(_L("Failed to save data file [%d]"), error);
       
   546 	        SetError(error);
       
   547 	        }
       
   548 		}
       
   549 
       
   550 	INFO_PRINTF1(_L("*END* CT_RSocketData::DoCmdUtilityStartFile"));
       
   551     }
       
   552 	
       
   553 /**
       
   554  * Uses helper class BreathSaver to start a Data dealer
       
   555  * @param  aSection - The section in config file to look for Data Dealer ID.
       
   556  * Sets TEF error if not successful.
       
   557  */
       
   558 void CT_RSocketData::DoCmdUtilityStartDealer(const TTEFSectionName& aSection)
       
   559 	{
       
   560 	INFO_PRINTF1(_L("*START* CT_RSocketData::DoCmdUtilityStartDealer"));
       
   561 	TBool dataOk = ETrue;
       
   562 	
       
   563 	TUint datadealerid( KNoDataDealer );
       
   564     if(!GetUintFromConfig(aSection,  KDataDealerID, datadealerid ))
       
   565     	{
       
   566     	ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KDataDealerID);
       
   567     	SetBlockResult(EFail);
       
   568     	dataOk = EFalse;
       
   569     	}
       
   570 	
       
   571     if (dataOk && datadealerid != KNoDataDealer)
       
   572 		{
       
   573 		INFO_PRINTF2(_L("Use data dealer with id (%u) for incoming file"), datadealerid);
       
   574 		TInt error = iBreathSaver->StartDealer(datadealerid);
       
   575         
       
   576         if (error == KErrNone)
       
   577             {
       
   578             INFO_PRINTF1(_L("iBreathSaver->StartDealer(datadealerid) was executed"));
       
   579             }
       
   580         else
       
   581         	{
       
   582             ERR_PRINTF2(_L("Failed to use data dealer [%d]"), error);
       
   583             SetError(error);
       
   584         	}
       
   585 		}
       
   586     
       
   587 	INFO_PRINTF1(_L("*END* CT_RSocketData::DoCmdUtilityStartDealer"));
       
   588 	}
       
   589 	
       
   590 
       
   591 
       
   592 /**
       
   593  * Checks HSDPA options.
       
   594  * @param  aSection - The section in config file to look for HSDPA parameters.
       
   595  * @return error - Error code. KErrNone if successful.
       
   596  */
       
   597 void CT_RSocketData::DoCmdUtilityCheckHSDPAL(const TTEFSectionName& aSection)
       
   598 	{
       
   599 	INFO_PRINTF1(_L("*START* CT_RSocketData::DoCmdUtilityCheckHSDPAL"));
       
   600 	TBool dataOk = ETrue;
       
   601 
       
   602     TPtrC hsdpacheck( KNullDesC );
       
   603     if(!GetStringFromConfig(aSection, KHsdpaCheck, hsdpacheck))
       
   604     	{
       
   605     	ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KHsdpaCheck);
       
   606     	SetBlockResult(EFail);
       
   607     	dataOk = EFalse;
       
   608     	}
       
   609     
       
   610     if (dataOk)
       
   611     	{
       
   612         TPtrC mobilePhoneName;
       
   613         RMobilePhone* mobilePhone = NULL;
       
   614         if(GetStringFromConfig(aSection, KRMobilePhone(), mobilePhoneName))
       
   615         	{
       
   616         	mobilePhone = static_cast<RMobilePhone*>(GetDataObjectL(mobilePhoneName));
       
   617         	}
       
   618         
       
   619         INFO_PRINTF1(_L( "Checking HSDPA..." ));
       
   620 
       
   621     	if (hsdpacheck == KNullDesC)
       
   622             {
       
   623             INFO_PRINTF1(_L( "No HSDPA used. Nothing to check." ));
       
   624             }
       
   625     	else if (hsdpacheck == KHsdpaIndicator)
       
   626             {
       
   627             if (CheckHSDPAIndicator())
       
   628                 {
       
   629                 INFO_PRINTF1(_L("HSDPA indicator available"));
       
   630                 }
       
   631             else
       
   632                 {
       
   633                 ERR_PRINTF1(_L("HSDPA indicator not available"));
       
   634                 SetBlockResult(EFail);
       
   635                 }
       
   636             }
       
   637         else if (hsdpacheck == KHsdpaSupported && mobilePhone)
       
   638             {
       
   639             if (CheckHSDPASupportL(*mobilePhone))
       
   640             	{
       
   641             	INFO_PRINTF1(_L("HSDPA support available"));
       
   642             	}
       
   643             else
       
   644             	{
       
   645             	ERR_PRINTF1(_L("HSDPA support not available"));
       
   646             	SetBlockResult(EFail);
       
   647             	}
       
   648             }
       
   649         else if (hsdpacheck == KHsdpaInUse && mobilePhone)
       
   650             {
       
   651     		if (CheckHSDPAUsageL(*mobilePhone))
       
   652     			{
       
   653     			INFO_PRINTF1(_L("HSDPA in use"));
       
   654     			}
       
   655     		else
       
   656     			{
       
   657     			ERR_PRINTF1(_L("HSDPA not in use"));
       
   658     			SetBlockResult(EFail);
       
   659     			}
       
   660             }
       
   661         else if (hsdpacheck == KOff)
       
   662             {
       
   663             INFO_PRINTF1(_L("Can HSDPA be turned off?"));
       
   664             }
       
   665         else
       
   666             {
       
   667             ERR_PRINTF2(_L("Unknown HSDPA usage parameter \"%S\""), &hsdpacheck);
       
   668             SetBlockResult(EFail);
       
   669             }
       
   670     	}
       
   671     
       
   672 	INFO_PRINTF1(_L("*END* CT_RSocketData::DoCmdUtilityCheckHSDPAL"));
       
   673 	}
       
   674 
       
   675 /**
       
   676  * Downloads a file from a given address and saves it in specified path.
       
   677  * @param  aSection - The section in config file to look for address, port, protocol, filename and save path.
       
   678  * @return error - Error code. KErrNone if successful.
       
   679  */
       
   680 void CT_RSocketData::DoCmdRecvOneOrMore(const TTEFSectionName& aSection)
       
   681 	{
       
   682 	INFO_PRINTF1(_L("*START* CT_RSocketData::DoCmdDownloadToFile"));
       
   683 	TBool dataOk = ETrue;
       
   684 
       
   685 	TInt duration( KNoDuration );
       
   686 	if(!GetIntFromConfig(aSection, KDuration, duration  ))
       
   687 		{
       
   688 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KDuration);
       
   689 		SetBlockResult(EFail);
       
   690 		dataOk = EFalse;
       
   691 		}
       
   692 	
       
   693 	TPtrC protocol;
       
   694 	if(!GetStringFromConfig(aSection, KProtocol, protocol ))
       
   695 		{
       
   696 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KProtocol);
       
   697 		SetBlockResult(EFail);
       
   698 		dataOk = EFalse;
       
   699 		}
       
   700 
       
   701 	TUint datadealerid( KNoDataDealer );
       
   702 	if(!GetUintFromConfig(aSection,  KDataDealerID, datadealerid ))
       
   703 		{
       
   704 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KDataDealerID);
       
   705 		SetBlockResult(EFail);
       
   706 		dataOk = EFalse;
       
   707 		}
       
   708 	
       
   709 	TPtrC savename( KNoFileSave );
       
   710     if(!GetStringFromConfig(aSection, KSaveName, savename))
       
   711     	{
       
   712     	ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KSaveName);
       
   713     	SetBlockResult(EFail);
       
   714     	dataOk = EFalse;
       
   715     	}
       
   716 	
       
   717 	TBuf8<KBufferSize> buffer;
       
   718 	if (protocol != KProtocolHTTP)
       
   719 		{
       
   720     	ERR_PRINTF3(_L("Error, %S protocol is not supported. %S protocol is expected"), &protocol, &KProtocolHTTP);
       
   721     	SetBlockResult(EFail);
       
   722     	dataOk = EFalse;
       
   723 		}
       
   724 	
       
   725 	if (dataOk)
       
   726 		{
       
   727 	    INFO_PRINTF1(_L("Download starts"));
       
   728 	    
       
   729 		TInt error = KErrNone;
       
   730 		TBool header = ETrue;
       
   731 		TBool writeFail = EFalse;
       
   732 		TRequestStatus status;
       
   733 		TSockXfrLength received;
       
   734 
       
   735 		// Start and end time for calculating the transfer speed.
       
   736 		iStart.HomeTime();
       
   737 		
       
   738 		// receive until RecvOneOrMore fails.
       
   739 		do
       
   740 			{
       
   741 			iSocket->RecvOneOrMore(buffer, 0, status, received);
       
   742 			User::WaitForRequest(status);
       
   743 
       
   744 			if (status == KErrNone)
       
   745 				{
       
   746 				iTotalReceived += received();
       
   747 
       
   748 				if (header)
       
   749 					{
       
   750 					// HTTP server first sends header and then data
       
   751 					TInt location = buffer.Find(KEmptyLine);
       
   752 					// If the separator (two line breaks) is found...
       
   753 					if (location != KErrNotFound)
       
   754 						{
       
   755 						TInt i = location + KEmptySize;
       
   756 						// Remove the header from the buffer
       
   757 						buffer.Delete(0, i);
       
   758 				        if (savename != KNoFileSave ||
       
   759 	                        datadealerid != KNoDataDealer)
       
   760 				            {
       
   761 				            error = iBreathSaver->Append(buffer);
       
   762 				            if (error != KErrNone)
       
   763 								{
       
   764 								WARN_PRINTF2(_L("Data append failed [%d]"), error);
       
   765 								iBreathSaver->End();
       
   766 								writeFail = ETrue;
       
   767 								}
       
   768 				            }
       
   769 						header = EFalse;
       
   770 						}
       
   771 					}
       
   772 				// Header is already removed from the input data
       
   773 				else
       
   774 					{
       
   775 					// Write data into file
       
   776 			        if (savename != KNoFileSave ||
       
   777 	                    datadealerid != KNoDataDealer)
       
   778 			            {
       
   779 			            error = iBreathSaver->Append(buffer);
       
   780 			            if (error != KErrNone)
       
   781 							{
       
   782 							WARN_PRINTF2(_L("Data append failed [%d]"), error);
       
   783 							iBreathSaver->End();
       
   784 							writeFail = ETrue;
       
   785 							}
       
   786 			            }
       
   787 					}
       
   788 				}
       
   789 	        else
       
   790 	            {
       
   791 	            // KErrEof means that socket was closed or there is not any data to be read anymore
       
   792 	            ERR_PRINTF2(_L("RecvOneOrMore returned error status [%d]"), status.Int());
       
   793 	            }
       
   794 	        // Check if duration parameter is given
       
   795 	        if (duration != KNoDuration)
       
   796 	            {
       
   797 	            iEnd.HomeTime();
       
   798 	            TTimeIntervalSeconds seconds (0);
       
   799 	            error = iEnd.SecondsFrom(iStart, seconds);
       
   800 	            if(error != KErrNone)
       
   801 	            	{
       
   802 	            	ERR_PRINTF2(_L("Failed to get time interval with error %d"), error);
       
   803 	            	SetError(error);
       
   804 	            	break;
       
   805 	            	}
       
   806 	            if ( seconds.Int() >= duration )
       
   807 	                {
       
   808 	                WARN_PRINTF2(_L("Transfer is stopped because defined duration [%d] exceeded"), seconds.Int());
       
   809 	                break;
       
   810 	                }
       
   811 	            }
       
   812 			}
       
   813 		while(status == KErrNone && writeFail == EFalse);
       
   814 		}
       
   815 	
       
   816 	INFO_PRINTF1(_L("*END* CT_RSocketData::DoCmdDownloadToFile"));
       
   817 	}
       
   818 
       
   819 /**
       
   820  * Ends downloaded file.
       
   821  * @param  aSection - The section in config file to look for file's name.
       
   822  * @return error - Error code. KErrNone if successful.
       
   823  */
       
   824 void CT_RSocketData::DoCmdUtilityEndFile(const TTEFSectionName& aSection)
       
   825 	{	
       
   826 	INFO_PRINTF1(_L("*START* CT_RSocketData::DoCmdUtilityEndFile"));
       
   827 	TBool dataOk = ETrue;
       
   828 
       
   829 	TPtrC savename( KNoFileSave );
       
   830     if(!GetStringFromConfig(aSection, KSaveName, savename))
       
   831     	{
       
   832     	ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KSaveName);
       
   833     	SetBlockResult(EFail);
       
   834     	dataOk = EFalse;
       
   835     	}
       
   836     
       
   837     if (dataOk)
       
   838     	{
       
   839         if (savename != KNoFileSave)
       
   840             {
       
   841             iBreathSaver->End();
       
   842             }
       
   843 
       
   844     	iEnd.HomeTime();
       
   845     	
       
   846     	INFO_PRINTF1(_L("Download ends"));
       
   847     	}
       
   848 
       
   849 	INFO_PRINTF1(_L("*END* CT_RSocketData::DoCmdUtilityEndFile"));
       
   850 	}	
       
   851 
       
   852 /**
       
   853  * Calculates download time and compares with a given duration and speed
       
   854  * @param  aSection - The section in config file to look for speed and duration parameters. 
       
   855  */
       
   856 void CT_RSocketData::DoCmdUtilityCalculateDownloadStats(const TTEFSectionName& aSection)
       
   857 	{
       
   858 	INFO_PRINTF1(_L("*START* CT_RSocketData::DoCmdUtilityCalculateDownloadStats"));
       
   859 	TBool dataOk = ETrue;
       
   860 	
       
   861 	TInt speed( KNoSpeedCheck );
       
   862 	if(!GetIntFromConfig(aSection, KMinimumSpeed, speed))
       
   863     	{
       
   864     	ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KMinimumSpeed);
       
   865     	SetBlockResult(EFail);
       
   866     	dataOk = EFalse;
       
   867     	}
       
   868     
       
   869 	TInt duration( KNoDuration );
       
   870 	if(!GetIntFromConfig(aSection, KDuration, duration  ))
       
   871 		{
       
   872 		ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KDuration);
       
   873 		SetBlockResult(EFail);
       
   874 		dataOk = EFalse;
       
   875 		}
       
   876 	
       
   877 	if (dataOk)
       
   878 		{
       
   879 		// Calculate the duration of the download.
       
   880 		TTimeIntervalMicroSeconds microseconds = iEnd.MicroSecondsFrom(iStart);
       
   881 	    TInt error = KErrNone;
       
   882 	    TReal throughput = 0;
       
   883 	    
       
   884 	    if (iTotalReceived != 0)
       
   885 	        {
       
   886 	        throughput = ThroughputInKiloBits( microseconds, iTotalReceived );
       
   887 	        }
       
   888 	    
       
   889 	    error = SetThroughput(throughput);
       
   890 	    if (error == KErrNone)
       
   891 	        {
       
   892 	        TBool speedEnough = ETrue;
       
   893 		    if (speed != KNoSpeedCheck)
       
   894 		        {
       
   895 		        INFO_PRINTF2(_L("Check that the download speed was faster than (%d kpbs)"), speed);
       
   896 		        if ( speed > throughput )
       
   897 		            {
       
   898 		            ERR_PRINTF1(_L("Speed was not fast enough"));
       
   899 		            SetBlockResult(EFail);
       
   900 		            speedEnough = EFalse;
       
   901 		            }
       
   902 		        else
       
   903 		            {
       
   904 		            INFO_PRINTF1(_L("Speed was fast enough"));
       
   905 		            }
       
   906 		        }
       
   907 
       
   908 		    if (speedEnough && duration != KNoDuration)
       
   909 		        {
       
   910 		        TTimeIntervalSeconds seconds (0);
       
   911 		        error = iEnd.SecondsFrom(iStart, seconds);
       
   912 		        if(error != KErrNone)
       
   913 		        	{
       
   914 		        	ERR_PRINTF2(_L("Failed to get time interval with error %d"), error);
       
   915 		        	SetError(error);
       
   916 		        	}
       
   917 		        else if ( seconds.Int() < duration )
       
   918 		            {
       
   919 		            ERR_PRINTF2(_L("Transfer failed, transfer did not last the defined duration [%d]"), duration);
       
   920 		            SetBlockResult(EFail);
       
   921 		            }
       
   922 		        }
       
   923 	        }
       
   924 	    else
       
   925 	    	{
       
   926 	        ERR_PRINTF2(_L("SetThroughput failed [%d]"), error);
       
   927 	        SetError(error);
       
   928 	    	}
       
   929 		}
       
   930     
       
   931     INFO_PRINTF1(_L("*END* CT_RSocketData::DoCmdUtilityCalculateDownloadStats"));
       
   932     }
       
   933 
       
   934 
       
   935 void CT_RSocketData::DoCmdInfoL(const TTEFSectionName& aSection)	
       
   936 	{
       
   937 	INFO_PRINTF1(_L("*START* CT_RSocketData::DoCmdInfoL"));
       
   938 	TBool dataOk = ETrue;
       
   939 	
       
   940 	TPtrC protocol;
       
   941     if(!GetStringFromConfig(aSection, KProtocol, protocol))
       
   942     	{
       
   943     	ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KProtocol);
       
   944     	SetBlockResult(EFail);
       
   945     	dataOk = EFalse;
       
   946     	}
       
   947 	
       
   948     if(dataOk)
       
   949     	{
       
   950     	TProtocolDesc protocolDesc;
       
   951     	TInt err = iSocket->Info(protocolDesc);
       
   952     	if(err != KErrNone)
       
   953     		{
       
   954     		ERR_PRINTF2(_L("Getting Info failed with error %d"), err);
       
   955     		SetError(err);
       
   956     		}
       
   957     	
       
   958     	TInt* protocolApi = static_cast<TInt*>(GetDataObjectL(protocolDesc.iName));
       
   959     	TInt* protocolIni = static_cast<TInt*>(GetDataObjectL(protocol));
       
   960     	
       
   961     	if(protocolApi != protocolIni)
       
   962     		{
       
   963     		ERR_PRINTF3(_L("Mismatch between expected protocol [%S] and current protocol [%S]"),protocolIni, protocolApi);
       
   964     		SetBlockResult(EFail);
       
   965     		}
       
   966     	}
       
   967 	
       
   968 	INFO_PRINTF1(_L("*END* CT_RSocketData::DoCmdInfoL"));
       
   969 	}	
       
   970 
       
   971 
       
   972 /**
       
   973  * Build http post request
       
   974  */
       
   975 void CT_RSocketData::DoCmdUtilityBuildRequest(const TTEFSectionName& aSection)
       
   976 	{
       
   977 	INFO_PRINTF1(_L("*START* CT_RSocketData::DoCmdUtilityBuildRequest"));
       
   978 	TBool dataOk = ETrue;
       
   979 
       
   980 	INFO_PRINTF1(_L("Sending HTTP request"));
       
   981 	TPtrC address( KNullDesC );
       
   982 	if(!GetStringFromConfig(aSection, KAddress, address ))
       
   983 	  	{
       
   984 	  	ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KAddress);
       
   985 	   	SetBlockResult(EFail);
       
   986 	   	dataOk = EFalse;
       
   987 	   	}
       
   988 	
       
   989 	TInt bytes;
       
   990     if(!GetIntFromConfig(aSection, KBytes, bytes ))
       
   991     	{
       
   992     	ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KBytes);
       
   993     	SetBlockResult(EFail);
       
   994     	dataOk = EFalse;
       
   995     	}
       
   996     TPtrC requestLogFile;
       
   997     if(!GetStringFromConfig(aSection, KRequestLogFile, requestLogFile ))
       
   998 	  	{
       
   999 	  	ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KRequestLogFile);
       
  1000 	   	SetBlockResult(EFail);
       
  1001 	   	dataOk = EFalse;
       
  1002 	   	}
       
  1003     
       
  1004     if (dataOk)
       
  1005     	{
       
  1006     	// Construct request
       
  1007     	iRequest.Append(KHTTPPOST);
       
  1008     	iRequest.Append(KHTTPSeparator);
       
  1009     	iRequest.Append(KServerScript);
       
  1010     	iRequest.Append(KHTTPSeparator);
       
  1011     	iRequest.Append(KHTTPSuffix);
       
  1012     	iRequest.Append(KLineBreak);
       
  1013     	
       
  1014     	iRequest.Append(KHost);
       
  1015     	iRequest.Append(KHTTPSeparator);
       
  1016     	iRequest.Append(address);
       
  1017     	iRequest.Append(KLineBreak);
       
  1018     	
       
  1019     	iRequest.Append(KFrom);
       
  1020     	iRequest.Append(KHTTPSeparator);
       
  1021     	iRequest.Append(KAhti);
       
  1022     	iRequest.Append(KLineBreak);
       
  1023     	
       
  1024     	iRequest.Append(KContentType);
       
  1025     	iRequest.Append(KHTTPSeparator);
       
  1026     	iRequest.Append(KMultipartType);
       
  1027     	iRequest.Append(KHTTPSeparator);
       
  1028     	iRequest.Append(KBoundary);
       
  1029     	iRequest.Append(KLineBreak);
       
  1030     	
       
  1031     	iRequest.Append(KContentLength);
       
  1032     	iRequest.Append(KHTTPSeparator);
       
  1033     	// Request size + size of the data to be sent. Server must know how much
       
  1034     	// data is coming.
       
  1035     	iRequest.AppendNum(KHeaderWithoutData+bytes);
       
  1036     	iRequest.Append(KLineBreak);
       
  1037     	
       
  1038     	// extra line break
       
  1039     	iRequest.Append(KLineBreak);
       
  1040     	
       
  1041     	iRequest.Append(KBoundaryStart);
       
  1042     	iRequest.Append(KLineBreak);
       
  1043     	
       
  1044     	iRequest.Append(KContentDisposition);
       
  1045     	iRequest.Append(KHTTPSeparator);
       
  1046     	iRequest.Append(KDisposition);
       
  1047     	iRequest.Append(KLineBreak);
       
  1048     	
       
  1049     	iRequest.Append(KContentType);
       
  1050     	iRequest.Append(KHTTPSeparator);
       
  1051     	iRequest.Append(KOctetType);
       
  1052     	iRequest.Append(KLineBreak);
       
  1053     	
       
  1054     	iRequest.Append(KLineBreak);
       
  1055     	
       
  1056     	// Make a log file of the request
       
  1057     	TFileName fileName;
       
  1058     	TDriveUnit driveUnit(EDriveC);
       
  1059     	fileName.Zero();
       
  1060     	fileName.Append(driveUnit.Name()); // c:
       
  1061     	fileName.Append(requestLogFile);	
       
  1062     	iBreathSaver->StartFile(fileName);
       
  1063 
       
  1064     	iBreathSaver->Append(iRequest);
       
  1065     	}
       
  1066 
       
  1067 	INFO_PRINTF1(_L("*END* CT_RSocketData::DoCmdUtilityBuildRequest"));
       
  1068 	}
       
  1069 
       
  1070 void CT_RSocketData::DoCmdWrite(const TInt aAsyncErrorIndex)
       
  1071 	{
       
  1072 	INFO_PRINTF1(_L("*START* CT_RSocketData::DoCmdWrite"));
       
  1073 	
       
  1074 	// Send the header before data
       
  1075    	iSocket->Write(iRequest, iActiveCallback->iStatus);
       
  1076    	iActiveCallback->Activate(aAsyncErrorIndex);
       
  1077    	IncOutstanding();
       
  1078    	
       
  1079    	INFO_PRINTF1(_L("*END* CT_RSocketData::DoCmdWrite"));
       
  1080 	}
       
  1081 
       
  1082 void CT_RSocketData::DoCmdSendBytesToSocket(const TTEFSectionName& aSection)
       
  1083 	{
       
  1084 	INFO_PRINTF1(_L("*START* CT_RSocketData::DoCmdSendBytesToSocket"));
       
  1085 	TBool dataOk = ETrue;
       
  1086 	
       
  1087 	TInt bytes;
       
  1088 	if(!GetIntFromConfig(aSection, KBytes, bytes ))
       
  1089     	{
       
  1090     	ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KBytes);
       
  1091     	SetBlockResult(EFail);
       
  1092     	dataOk = EFalse;
       
  1093     	}
       
  1094 	TInt duration ( KNoDuration );
       
  1095 	if(!GetIntFromConfig(aSection, KDuration, duration ))
       
  1096 		{
       
  1097     	ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KDuration);
       
  1098     	SetBlockResult(EFail);
       
  1099     	dataOk = EFalse;
       
  1100     	}
       
  1101 	
       
  1102 	if (dataOk)
       
  1103 		{
       
  1104 		TInt error = SendBytesToSocket(bytes, duration);
       
  1105 		if (error != KErrNone)
       
  1106 			{
       
  1107 			ERR_PRINTF1(_L("Error in sending bytes to socket"));
       
  1108 			SetError(error);
       
  1109 			}
       
  1110 		}
       
  1111 
       
  1112 	INFO_PRINTF1(_L("*END* CT_RSocketData::DoCmdSendBytesToSocket"));
       
  1113 	}
       
  1114 
       
  1115 void CT_RSocketData::DoCmdUtilityEndRequest()
       
  1116 	{
       
  1117 	INFO_PRINTF1(_L("*START* CT_RSocketData::DoCmdUtilityEndRequest"));
       
  1118 
       
  1119 	iRequest.Delete(0, iRequest.Length());
       
  1120     // Now the rest of the header data
       
  1121     iRequest.Append(KLineBreak);
       
  1122 
       
  1123     iRequest.Append(KBoundaryEnd);
       
  1124     iRequest.Append(KLineBreak);
       
  1125 
       
  1126     // add the rest of the reaquest to log file
       
  1127     iBreathSaver->Append(iRequest);
       
  1128     iBreathSaver->End(); // Close the file
       
  1129 
       
  1130     INFO_PRINTF1(_L("*END* CT_RSocketData::DoCmdUtilityEndRequest"));
       
  1131 	}
       
  1132 
       
  1133 void CT_RSocketData::DoCmdResponseFromServer(const TTEFSectionName& aSection)
       
  1134 	{
       
  1135     INFO_PRINTF1(_L("*START* CT_RSocketData::DoCmdResponseFromServer"));
       
  1136     TBool dataOk = ETrue;
       
  1137     TPtrC responseLogFile;
       
  1138     if(!GetStringFromConfig(aSection, KResponseLogFile, responseLogFile ))
       
  1139 	  	{
       
  1140 	  	ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KResponseLogFile);
       
  1141 	   	SetBlockResult(EFail);
       
  1142 	   	dataOk = EFalse;
       
  1143 	   	}
       
  1144     if(dataOk)
       
  1145     	{
       
  1146     	// After file was succesfully sent to HTTP server we can receive the response
       
  1147     	// from the server.
       
  1148     	// Create variables for receive buffer and received data counting variables.
       
  1149     	
       
  1150     	TBuf8<KBufferSize> buffer;
       
  1151     	TSockXfrLength received;
       
  1152     	TRequestStatus status;
       
  1153     	
       
  1154     	TFileName fileName;
       
  1155     	TDriveUnit driveUnit(EDriveC);
       
  1156     	fileName.Zero();
       
  1157     	fileName.Append(driveUnit.Name()); // c:
       
  1158     	fileName.Append(responseLogFile);
       
  1159     	
       
  1160     	iBreathSaver->StartFile(fileName);
       
  1161     	// receive until RecvOneOrMore fails.
       
  1162     	do
       
  1163     		{
       
  1164     		iSocket->RecvOneOrMore(buffer, 0, status, received);
       
  1165     		User::WaitForRequest(status);
       
  1166     	
       
  1167     	    iBreathSaver->Append(buffer);
       
  1168     		}
       
  1169     	while(status.Int() == KErrNone);
       
  1170     	
       
  1171     	iBreathSaver->End();
       
  1172     	}
       
  1173     
       
  1174 	
       
  1175     INFO_PRINTF1(_L("*END* CT_RSocketData::DoCmdResponseFromServer"));
       
  1176 	}
       
  1177 
       
  1178 /**
       
  1179  * Send Bytes to the socket
       
  1180  * @param TInt How many bytes are sent
       
  1181  * @return Symbian error code
       
  1182  */
       
  1183 TInt CT_RSocketData::SendBytesToSocket(TInt aBytes, TInt aDuration)
       
  1184     {
       
  1185     const TInt KDataBufferSize = 256;
       
  1186     TBuf8<KDataBufferSize> data;
       
  1187     data.SetMax();
       
  1188     data.FillZ();
       
  1189     TInt bytesSent = 0;
       
  1190 
       
  1191     TRequestStatus status;
       
  1192 
       
  1193     INFO_PRINTF1(_L("Uploading bytes to server"));
       
  1194 	iStart.HomeTime();
       
  1195 
       
  1196     // Loop while enough bytes are sent to socket
       
  1197     while (bytesSent < aBytes)
       
  1198         {
       
  1199         if ((aBytes - bytesSent) < KDataBufferSize)
       
  1200             {
       
  1201             // this is the last round where we don't send full buffer anymore
       
  1202             data.SetLength(aBytes - bytesSent);
       
  1203             iSocket->Write(data, status);
       
  1204             User::WaitForRequest(status);
       
  1205             if (status.Int() != KErrNone)
       
  1206                 {
       
  1207                 ERR_PRINTF2(_L("Failed to send data to socket [%d]"), status.Int());
       
  1208                 return status.Int();
       
  1209                 }
       
  1210 
       
  1211             bytesSent += (aBytes - bytesSent);
       
  1212             // We can break because there are no more data to be send
       
  1213             break;
       
  1214             }
       
  1215         iSocket->Write(data, status);
       
  1216         User::WaitForRequest(status);
       
  1217         if (status.Int() != KErrNone)
       
  1218             {
       
  1219             ERR_PRINTF2(_L("Failed to send data to socket [%d]"), status.Int());
       
  1220             return status.Int();
       
  1221             }
       
  1222 
       
  1223         bytesSent += KDataBufferSize;
       
  1224         
       
  1225         if (aDuration != KNoDuration)
       
  1226             {
       
  1227             iEnd.HomeTime();
       
  1228             TTimeIntervalSeconds seconds (0);
       
  1229             TInt error = iEnd.SecondsFrom(iStart, seconds);
       
  1230             if (error != KErrNone)
       
  1231                 {
       
  1232             	ERR_PRINTF2(_L("Failed to get time interval with error %d"), error);
       
  1233                 return error;
       
  1234                 }
       
  1235             if ( seconds.Int() >= aDuration )
       
  1236                 {
       
  1237                 // Transfer has lasted long enough. Let's stop transfer
       
  1238                 INFO_PRINTF2(_L("Transfer is stopped because defined duration [%d] exceeded"), seconds.Int());
       
  1239                 break;
       
  1240                 }
       
  1241             }
       
  1242         }
       
  1243 
       
  1244     iEnd.HomeTime();
       
  1245 
       
  1246     TTimeIntervalMicroSeconds microseconds = iEnd.MicroSecondsFrom(iStart);
       
  1247 
       
  1248 	// Calculate the upload speed
       
  1249     TInt error=0;
       
  1250     TReal throughput = 0;
       
  1251     if (bytesSent != 0)
       
  1252         {
       
  1253         throughput = ThroughputInKiloBits( microseconds, bytesSent );
       
  1254         }
       
  1255     	error= SetThroughput(throughput);
       
  1256 
       
  1257     if (error != KErrNone)
       
  1258         {
       
  1259         ERR_PRINTF2(_L("SetThroughput failed [%d]"), error);
       
  1260         return error;
       
  1261         }
       
  1262 
       
  1263     if (aDuration != KNoDuration)
       
  1264         {
       
  1265         // Close the socket because next we try to read response from the server and it won't come
       
  1266         // because transfer is cut early (server waits until all bytes are sent or socket is closed)
       
  1267         iSocket->Shutdown(RSocket::ENormal, status);
       
  1268         User::WaitForRequest(status);
       
  1269         
       
  1270         TTimeIntervalSeconds seconds (0);
       
  1271         TInt error = iEnd.SecondsFrom(iStart, seconds);
       
  1272         if (error != KErrNone)
       
  1273             {
       
  1274         	ERR_PRINTF2(_L("Failed to get time interval with error %d"), error);
       
  1275             return error;
       
  1276             }
       
  1277         if ( seconds.Int() < aDuration )
       
  1278             {
       
  1279             // Transfer didn't last the defined duration!
       
  1280             ERR_PRINTF2(_L("Transfer failed, transfer did not last the defined duration [%d]"), aDuration);
       
  1281             return KErrEof;
       
  1282             }
       
  1283         else
       
  1284             {
       
  1285             // If duration was used and socket was closed early return KErrCancel (it won't cause the
       
  1286             // whole test case to fail
       
  1287             return KErrCancel;
       
  1288             }
       
  1289         }
       
  1290 
       
  1291     return KErrNone;
       
  1292     }
       
  1293 
       
  1294 
       
  1295 
       
  1296 /**
       
  1297  * Calculates throughput based on duration of a data transfer and total transferred bytes.
       
  1298  * @param aDuration Duration of the transfer.
       
  1299  * @param aTotalTransferred Total transferred data length in bytes.
       
  1300  * @return Throughput in KBps.
       
  1301  */
       
  1302 TReal CT_RSocketData::ThroughputInKiloBits( TTimeIntervalMicroSeconds aDuration, TInt aBytes )
       
  1303 	{
       
  1304 	const TReal KKiloConversion(1000.0);
       
  1305 	TReal throughput = ThroughputInMegaBits( aDuration, aBytes ) * KKiloConversion;
       
  1306 	
       
  1307 	return throughput;
       
  1308 	}
       
  1309 
       
  1310 
       
  1311 /**
       
  1312  * Calculates throughput based on duration of a data transfer and total transferred bytes.
       
  1313  * @param aDuration Duration of the transfer.
       
  1314  * @param aTotalTransferred Total transferred data length in bytes.
       
  1315  * @return Throughput in MBps.
       
  1316  */
       
  1317 TReal CT_RSocketData::ThroughputInMegaBits( TTimeIntervalMicroSeconds aDuration, TInt aBytes )
       
  1318 	{
       
  1319 	const TReal KBitsInByte(8.0);
       
  1320 	TReal throughput = ( KBitsInByte * (TReal) aBytes ) / (TReal) aDuration.Int64();
       
  1321 	
       
  1322 	return throughput;
       
  1323 	}
       
  1324 
       
  1325 /**
       
  1326  * Set throughput into RProperty
       
  1327  * @param aThroughput The calculated throughput
       
  1328  */
       
  1329 TInt CT_RSocketData::SetThroughput( const TReal aThroughput )
       
  1330     {
       
  1331 	TInt err = RProperty::Define( iPropertyCategory, iPropertyKey, RProperty::EByteArray );
       
  1332 
       
  1333 	if( err == KErrAlreadyExists )
       
  1334 		{
       
  1335 		INFO_PRINTF1(_L( "Property already exists. Contents will be overridden") );
       
  1336 		err = KErrNone;
       
  1337 		}
       
  1338 	
       
  1339 	if (err == KErrNone)
       
  1340 		{
       
  1341 	    // Convert throughput value (TReal) into descriptor
       
  1342 	    TBuf8<256> amount;
       
  1343 	    TRealFormat realFormat;
       
  1344 	    err = amount.AppendNum( aThroughput, realFormat );
       
  1345 	    // Set property value
       
  1346 	    err = RProperty::Set( iPropertyCategory, iPropertyKey, amount );
       
  1347 		}
       
  1348 	else
       
  1349 		{
       
  1350 		ERR_PRINTF2(_L( "Could not create pub&sub property: %d"), err );
       
  1351 		}
       
  1352 
       
  1353     return err;
       
  1354     }
       
  1355 
       
  1356 /**
       
  1357 Check if RMobileCall has HSDPA indicator
       
  1358 @return TBool value of the indicator
       
  1359 */
       
  1360 TBool CT_RSocketData::CheckHSDPAIndicator()
       
  1361     {
       
  1362     RMobilePhone::TMobilePhoneNetworkInfoV5 networkInfo;
       
  1363     return networkInfo.iHsdpaAvailableIndicator;
       
  1364     }
       
  1365 
       
  1366 /**
       
  1367 Check if RPacketService has HSDPA support
       
  1368 @param RMobileCall aMobileCall
       
  1369 @return TBool value of the indicator
       
  1370 */
       
  1371 TBool CT_RSocketData::CheckHSDPASupportL(RMobilePhone &aMobilePhone)
       
  1372     {
       
  1373     RPacketService packetservice;
       
  1374     
       
  1375     CleanupClosePushL(packetservice);
       
  1376     User::LeaveIfError(packetservice.Open(aMobilePhone));
       
  1377     TUint caps;
       
  1378     // Ask for static capabilities using CDMA context
       
  1379     TInt error = packetservice.GetStaticCaps(caps, RPacketContext::EPdpTypeCDPD);
       
  1380     CleanupStack::PopAndDestroy(&packetservice); // RPacketService.Close()
       
  1381 
       
  1382     User::LeaveIfError(error);
       
  1383     
       
  1384     return caps & RPacketService::KCapsHSDPASupported;
       
  1385     }
       
  1386     
       
  1387 /**
       
  1388 Check if RPacketService is currently using HSDPA
       
  1389 @param RMobileCall aMobileCall
       
  1390 @return TBool value of the indicator
       
  1391 */
       
  1392 TBool CT_RSocketData::CheckHSDPAUsageL(RMobilePhone &aMobilePhone) 
       
  1393     {
       
  1394     RPacketService ps;
       
  1395     
       
  1396     CleanupClosePushL(ps); 
       
  1397     User::LeaveIfError(ps.Open(aMobilePhone)); 
       
  1398     RPacketService::TDynamicCapsFlags caps;
       
  1399     TInt error = ps.GetDynamicCaps(caps);
       
  1400 	CleanupStack::PopAndDestroy(&ps); // RPacketService
       
  1401 
       
  1402 	User::LeaveIfError(error); 
       
  1403     
       
  1404 	return caps & RPacketService::KCapsHSDPA;
       
  1405     }