wlanapitest/wlanhaitest/common/src/DataWrapperBase.cpp
branchGCC_SURGE
changeset 37 1ff9bf2737cf
parent 29 d1f0fe5eccbe
parent 36 1c425781161e
equal deleted inserted replaced
29:d1f0fe5eccbe 37:1ff9bf2737cf
     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 "datawrapperbase.h"
       
    20 
       
    21 /*@{*/
       
    22 ///	Constant Literals used.
       
    23 _LIT(KPrefixHex,			"0x");
       
    24 _LIT(KPrefixOctal,			"0");
       
    25 _LIT(KSuffixBinary,			"b");
       
    26 
       
    27 _LIT(KIncludeSection,		"include");
       
    28 _LIT(KFile,					"file%d");
       
    29 _LIT(KMatch,				"*{*,*}*");
       
    30 _LIT(KStart,				"{");
       
    31 _LIT(KSeparator,			",");
       
    32 _LIT(KEnd,					"}");
       
    33 _LIT(KDataRead,				"INI READ : %S %S %S");
       
    34 _LIT(KLogMissingParameter,	"Missing parameter '%S'");
       
    35 _LIT(KExpected,				"expected");
       
    36 /*@}*/
       
    37 
       
    38 CDataWrapperBase::CDataWrapperBase()
       
    39 :	CDataWrapper()
       
    40 	{
       
    41 	}
       
    42 
       
    43 /**
       
    44  * Public destructor
       
    45  */
       
    46 CDataWrapperBase::~CDataWrapperBase()
       
    47 	{
       
    48 	iInclude.ResetAndDestroy();
       
    49 	iBuffer.ResetAndDestroy();
       
    50 	iFs.Close();
       
    51 	}
       
    52 
       
    53 void CDataWrapperBase::InitialiseL()
       
    54 	{
       
    55 	iTimer.CreateLocal();
       
    56 	CDataWrapper::InitialiseL();
       
    57 	TBuf<KMaxTestExecuteCommandLength>	tempStore;
       
    58 	TPtrC		fileName;
       
    59 	TBool		moreData=ETrue;
       
    60 	TBool		index=0;
       
    61 	while ( moreData )
       
    62 		{
       
    63 		tempStore.Format(KFile(), ++index);
       
    64 		moreData=GetStringFromConfig(KIncludeSection, tempStore, fileName);
       
    65 
       
    66 		if (moreData)
       
    67 			{
       
    68 			CIniData*	iniData=CIniData::NewL(fileName);
       
    69 			CleanupStack::PushL(iniData);
       
    70 			iInclude.Append(iniData);
       
    71 			CleanupStack::Pop(iniData);
       
    72 			}
       
    73 		}
       
    74 	User::LeaveIfError(iFs.Connect());
       
    75 	}
       
    76 
       
    77 /**
       
    78  * Reads the value present from the test steps ini file within the mentioned section name and key name
       
    79  * Copies the value to the TBool reference passed in possible values TRUE, FALSE
       
    80  * @param aSectName - Section within the test steps ini file
       
    81  * @param aKeyName - Name of a key within a section
       
    82  * @return aResult - The value of the boolean
       
    83  * @return TBool - ETrue for found, EFalse for not found 
       
    84  */
       
    85 TBool CDataWrapperBase::GetBoolFromConfig(const TDesC& aSectName,const TDesC& aKeyName,TBool& aResult)
       
    86 	{
       
    87 	TBool	ret=EFalse;
       
    88 	TPtrC	result;
       
    89 	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
       
    90 	if ( err != KErrNone )
       
    91 		{
       
    92 		ret=EFalse;
       
    93 		}
       
    94 	if ( ret )
       
    95 		{
       
    96 		_LIT(KTrue,"true");
       
    97 		aResult=(result.FindF(KTrue) != KErrNotFound);
       
    98 		}
       
    99 
       
   100 	return ret;
       
   101 	}
       
   102 
       
   103 /**
       
   104  * Reads the value present from the test steps ini file within the mentioned section name and key name
       
   105  * Copies the value to the TInt reference passed in
       
   106  * @param aSectName - Section within the test steps ini file
       
   107  * @param aKeyName - Name of a key within a section
       
   108  * @return aResult - The value of the integer
       
   109  * @return TBool - ETrue for found, EFalse for not found 
       
   110  */
       
   111 TBool CDataWrapperBase::GetIntFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TInt& aResult)
       
   112 	{
       
   113 	TPtrC	result;
       
   114 	TBool	ret=EFalse;
       
   115 	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
       
   116 	if ( err != KErrNone )
       
   117 		{
       
   118 		ret=EFalse;
       
   119 		}
       
   120 	if ( ret )
       
   121 		{
       
   122 		TLex	lex(result);
       
   123 		ret=(lex.Val(aResult)==KErrNone);
       
   124 		}
       
   125 
       
   126 	return ret;
       
   127 	}
       
   128 
       
   129 /**
       
   130  * Reads the value present from the test steps ini file within the mentioned section name and key name
       
   131  * Copies the value to the TPtrC reference passed in
       
   132  * @param aSectName - Section within the test steps ini file
       
   133  * @param aKeyName - Name of a key within a section
       
   134  * @return aResult - Reference to the string on the heap
       
   135  * @return TBool - ETrue for found, EFalse for not found 
       
   136  */
       
   137 TBool CDataWrapperBase::GetStringFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TPtrC& aResult)
       
   138 	{
       
   139 	TBool	ret=EFalse;
       
   140 	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, aResult));
       
   141 	if ( err != KErrNone )
       
   142 		{
       
   143 		ret=EFalse;
       
   144 		}
       
   145 	return ret;
       
   146 	}
       
   147 
       
   148 /**
       
   149  * Reads the value present from the test steps ini file within the mentioned section name and key name
       
   150  * Copies the value to the TInt reference passed in. The value can optionally be prefixed with 0x
       
   151  * @param aSectName - Section within the test steps ini file
       
   152  * @param aKeyName - Name of a key within a section
       
   153  * @return aResult - The integer value of the Hex input
       
   154  * @return TBool - ETrue for found, EFalse for not found 
       
   155  */	
       
   156 TBool CDataWrapperBase::GetHexFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TInt& aResult)
       
   157 	{
       
   158 	TPtrC	result;
       
   159 	TBool	ret=EFalse;
       
   160 	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
       
   161 	if ( err != KErrNone )
       
   162 		{
       
   163 		ret=EFalse;
       
   164 		}
       
   165 	if ( ret )
       
   166 		{
       
   167 		TLex	lex;
       
   168 		if( result.FindC(KPrefixHex)==KErrNone )
       
   169 			{
       
   170 			lex=result.Mid(KPrefixHex().Length());
       
   171 			}
       
   172 		else
       
   173 			{
       
   174 			lex=result;
       
   175 			}
       
   176 		ret=(lex.Val((TUint &)aResult, EHex)==KErrNone);
       
   177 		}
       
   178 
       
   179 	return ret;
       
   180 	}
       
   181 
       
   182 /**
       
   183  * Reads the value present from the test steps ini file within the mentioned section name and key name
       
   184  * Copies the value to the TUint reference passed in.
       
   185  * If the value is prefixed with 0x the value is read as a hexadecimal value
       
   186  * If the value is suffixed with b the value is read as a binary value
       
   187  * If the value is prefixed with a 0 the value is read as an octal value
       
   188  * If it does not match the above it is read in as an integer
       
   189  * @param aSectName - Section within the test steps ini file
       
   190  * @param aKeyName - Name of a key within a section
       
   191  * @return aResult - The integer value of the Hex input
       
   192  * @return TBool - ETrue for found, EFalse for not found 
       
   193  */	
       
   194 TBool CDataWrapperBase::GetUintFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TUint& aResult)
       
   195 	{
       
   196 	TPtrC	result;
       
   197 	TBool	ret=EFalse;
       
   198 	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
       
   199 	if ( err != KErrNone )
       
   200 		{
       
   201 		ret=EFalse;
       
   202 		}
       
   203 	if ( ret )
       
   204 		{
       
   205 		TLex	lex(result);
       
   206 		if( result.FindC(KPrefixHex)==KErrNone )
       
   207 			{
       
   208 			lex=result.Mid(KPrefixHex().Length());
       
   209 			ret=(lex.Val(aResult, EHex)==KErrNone);
       
   210 			}
       
   211 		else
       
   212 			{
       
   213 			TInt	binarySuffixPosition=result.Length()-KSuffixBinary().Length();
       
   214 			if ( result.FindC(KSuffixBinary)==binarySuffixPosition )
       
   215 				{
       
   216 				lex=result.Left(binarySuffixPosition);
       
   217 				ret=(lex.Val(aResult, EBinary)==KErrNone);
       
   218 				}
       
   219 			else
       
   220 				{
       
   221 				if( result.FindC(KPrefixOctal)==KErrNone )
       
   222 					{
       
   223 					ret=(lex.Val(aResult, EOctal)==KErrNone);
       
   224 					}
       
   225 				else
       
   226 					{
       
   227 					TInt	intResult;
       
   228 					ret=(lex.Val(intResult)==KErrNone);
       
   229 					if ( ret )
       
   230 						{
       
   231 						aResult=(TUint)intResult;
       
   232 						}
       
   233 					}
       
   234 				}
       
   235 			}
       
   236 		}
       
   237 
       
   238 	return ret;
       
   239 	}
       
   240 
       
   241 TBool CDataWrapperBase::GetCommandStringParameterL(const TDesC& aSectName, const TDesC& aKeyName, TPtrC& aResult)
       
   242 	{
       
   243 	TBool	ret=EFalse;
       
   244 
       
   245 	if ( aSectName.Length()!=0 )
       
   246 		{
       
   247 		ret=CDataWrapper::GetStringFromConfig(aSectName, aKeyName, aResult);
       
   248 
       
   249 		for ( TInt index=iInclude.Count(); (index>0) && (!ret); )
       
   250 			{
       
   251 			ret=iInclude[--index]->FindVar(aSectName, aKeyName, aResult);
       
   252 			}
       
   253 		}
       
   254 
       
   255 	if ( ret )
       
   256 		{
       
   257 		if ( aResult.Match(KMatch)!=KErrNotFound )
       
   258 			{
       
   259 			//	We have an entry of the format
       
   260 			//	entry =*{section,entry}*
       
   261 			//	where * is one or more characters
       
   262 			//	We need to construct this from other data in the ini file replacing {*,*}
       
   263 			//	with the data from
       
   264 			//	[section]
       
   265 			//	entry =some_value
       
   266 			HBufC*	buffer=HBufC::NewLC(aResult.Length());
       
   267 			buffer->Des().Copy(aResult);
       
   268 
       
   269 			TInt	startLength=KStart().Length();
       
   270 			TInt	sparatorLength=KSeparator().Length();
       
   271 			TInt	endLength=KEnd().Length();
       
   272 			TInt	bufferLength;
       
   273 			TInt	start;
       
   274 			TInt	sparator;
       
   275 			TInt	end;
       
   276 			TPtrC	remaining;
       
   277 			TLex	lex;
       
   278 			do
       
   279 				{
       
   280 				bufferLength=buffer->Length();
       
   281 				start=buffer->Find(KStart);
       
   282 
       
   283 				remaining.Set(buffer->Des().Right(bufferLength-start-startLength));
       
   284 				sparator=remaining.Find(KSeparator);
       
   285 				remaining.Set(remaining.Right(remaining.Length()-sparator-sparatorLength));
       
   286 				sparator += (start + startLength);
       
   287 
       
   288 				end=remaining.Find(KEnd) + sparator + sparatorLength;
       
   289 
       
   290 				TPtrC	sectionName(buffer->Ptr()+start+startLength, sparator-start-startLength);
       
   291 				TPtrC	keyName(buffer->Ptr()+sparator+sparatorLength, end-sparator-sparatorLength);
       
   292 				sectionName.Set(TLex(sectionName).NextToken());
       
   293 				keyName.Set(TLex(keyName).NextToken());
       
   294 
       
   295 				TInt	entrySize=0;
       
   296 				TPtrC	entryData;
       
   297 				TBool	found=CDataWrapper::GetStringFromConfig(sectionName, keyName, entryData);
       
   298 				for ( TInt index=iInclude.Count(); (index>0) && (!found);  )
       
   299 					{
       
   300 					found=iInclude[--index]->FindVar(sectionName, keyName, entryData);
       
   301 					}
       
   302 				if ( found )
       
   303 					{
       
   304 					entrySize=entryData.Length();
       
   305 					}
       
   306 
       
   307 				TInt	newLength=start + bufferLength - end - endLength + entrySize;
       
   308 				HBufC*	bufferNew=HBufC::NewLC(newLength);
       
   309 				bufferNew->Des().Copy(buffer->Ptr(), start);
       
   310 				if ( entrySize>0 )
       
   311 					{
       
   312 					bufferNew->Des().Append(entryData);
       
   313 					}
       
   314 				bufferNew->Des().Append(buffer->Ptr() + end + endLength, bufferLength - end - endLength);
       
   315 				CleanupStack::Pop(bufferNew);
       
   316 				CleanupStack::PopAndDestroy(buffer);
       
   317 				buffer=bufferNew;
       
   318 				CleanupStack::PushL(buffer);
       
   319 				}
       
   320 			while ( buffer->Match(KMatch)!=KErrNotFound );
       
   321 			iBuffer.Append(buffer);
       
   322 			CleanupStack::Pop(buffer);
       
   323 			aResult.Set(*buffer);
       
   324 			INFO_PRINTF4(KDataRead, &aSectName, &aKeyName , &aResult);
       
   325 			}
       
   326 		}
       
   327 
       
   328 	return ret;
       
   329 	}
       
   330 
       
   331 
       
   332 /** 
       
   333  *Utility function to produce time delay
       
   334  * @param aTimeoutInSecs Times in micro seconds
       
   335  */
       
   336 void CDataWrapperBase::Timedelay(TInt aTimeoutInSecs)
       
   337 	{
       
   338 	TRequestStatus	status;
       
   339 	iTimer.After(status, aTimeoutInSecs);
       
   340 	User::WaitForRequest(status);
       
   341 	}
       
   342 
       
   343 
       
   344 /** Validates the returned valua as a Bool. Compares aValue with the expected result in the ini file and Sets the block result to EFail if not equal.
       
   345  *	@param aSection - Section within the ini file to read the expected value
       
   346  *	@param aValue	 - The value we need to validate
       
   347  *  @param aForceValidation - ETrue forces validation if there's a missing parameter.
       
   348  *  @param aErrorMessage	- Contains a descriptor with a custom error message
       
   349  *  @return - Print error messages to log file
       
   350  *  @return - Sets test block result
       
   351  */
       
   352 void CDataWrapperBase::GetValidationBool(const TDesC& aSection, TInt aValue, TBool aForceValidation, const TDesC& aErrorMessage)
       
   353 	{
       
   354 	TBool	expectedValue;
       
   355 	TBool	ret=GetBoolFromConfig(aSection, KExpected(), expectedValue);
       
   356 	if ( !ret )
       
   357 		{
       
   358 		if ( aForceValidation )
       
   359 			{
       
   360 			ERR_PRINTF2(KLogMissingParameter, & KExpected);
       
   361 			SetBlockResult(EFail);
       
   362 			}
       
   363 		}
       
   364 	else
       
   365 		{
       
   366 		if ( aValue!=expectedValue )
       
   367 			{
       
   368 			SetBlockResult(EFail);
       
   369 			ERR_PRINTF1(aErrorMessage);
       
   370 			ERR_PRINTF3(_L("Retrived Value(%d) != expectedValue(%d)"), aValue, expectedValue);
       
   371 			}
       
   372 		}
       
   373 	}
       
   374 
       
   375 
       
   376 /** 
       
   377  * Validates the returned valua as a TInt. Compares aValue with the expected result in the ini file and Sets the block result to EFail if not equal.
       
   378  *	@param aSection - Section within the ini file to read the expected value
       
   379  *	@param aValue	 - The value we need to validate
       
   380  *  @param aForceValidation - ETrue forces validation if there's a missing parameter.
       
   381  *  @param aErrorMessage	- Contains a descriptor with a custom error message
       
   382  *  @return - Print error messages to log file
       
   383  *  @return - Sets test block result
       
   384  */
       
   385 void CDataWrapperBase::GetValidationInt(const TDesC& aSection, TInt aValue, TBool aForceValidation, const TDesC& aErrorMessage)
       
   386 	{
       
   387 	TInt	expectedValue;
       
   388 	TBool	ret=GetIntFromConfig(aSection, KExpected(), expectedValue);
       
   389 	if ( !ret )
       
   390 		{
       
   391 		if ( aForceValidation )
       
   392 			{
       
   393 			ERR_PRINTF2(KLogMissingParameter, &KExpected());
       
   394 			SetBlockResult(EFail);
       
   395 			}
       
   396 		}
       
   397 	else
       
   398 		{
       
   399 		if ( aValue!=expectedValue )
       
   400 			{
       
   401 			SetBlockResult(EFail);
       
   402 			ERR_PRINTF1(aErrorMessage);
       
   403 			ERR_PRINTF3(_L("Retrived Value(%d) != expectedValue(%d)"), aValue, expectedValue);
       
   404 			}
       
   405 		}
       
   406 	}
       
   407 
       
   408 /** 
       
   409  * Validates the returned valua as a TUint. Compares aValue with the expected result in the ini file and Sets the block result to EFail if not equal.
       
   410  *	@param aSection - Section within the ini file to read the expected value
       
   411  *	@param aValue	 - The value we need to validate
       
   412  *  @param aForceValidation - ETrue forces validation if there's a missing parameter.
       
   413  *  @param aErrorMessage	- Contains a descriptor with a custom error message
       
   414  *  @return - Print error messages to log file
       
   415  *  @return - Sets test block result
       
   416  */
       
   417 void CDataWrapperBase::GetValidationUint(const TDesC& aSection, TInt aValue, TBool aForceValidation, const TDesC& aErrorMessage)
       
   418 	{
       
   419 	TUint	expectedValue;
       
   420 	TBool	ret=GetUintFromConfig(aSection, KExpected(), expectedValue);
       
   421 	if ( !ret )
       
   422 		{
       
   423 		if ( aForceValidation )
       
   424 			{
       
   425 			ERR_PRINTF2(KLogMissingParameter, &KExpected());
       
   426 			SetBlockResult(EFail);
       
   427 			}
       
   428 		}
       
   429 	else
       
   430 		{
       
   431 		if ( aValue!=expectedValue )
       
   432 			{
       
   433 			SetBlockResult(EFail);
       
   434 			ERR_PRINTF1(aErrorMessage);
       
   435 			ERR_PRINTF3(_L("Retrived Value(%d) != expectedValue(%d)"), aValue, expectedValue);
       
   436 			}
       
   437 		}
       
   438 	}
       
   439 
       
   440 
       
   441 /**
       
   442  *   Return array of string parameters i.e. key=a1,a2,a3 returns array which contains
       
   443  *   String a1, a2 and a3.
       
   444  *   @return ret - EFalse if can't get a String parameter from Config file.  ETrue if KErrNone
       
   445  */
       
   446 TBool CDataWrapperBase::GetArrayFromConfig(const TDesC& aSectName, const TDesC& aKeyName, RPointerArray<HBufC>& aResult)
       
   447 	{
       
   448 	TBool	ret=EFalse;
       
   449 	TPtrC completeArray;
       
   450 	
       
   451 	TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, completeArray));
       
   452 	if ( err != KErrNone )
       
   453 		{
       
   454 		ret=EFalse;
       
   455 		}
       
   456 
       
   457     TLex16 lex(completeArray); // Here we have the array as a string i.e. "a1,a2,a3"
       
   458     TBuf<256> buf;
       
   459     TChar chr;
       
   460     
       
   461     while(!lex.Eos())
       
   462         {
       
   463         chr = lex.Get();
       
   464         // Check if there was a list separator
       
   465         if (chr == ',')
       
   466             {
       
   467             HBufC* param = buf.AllocLC();
       
   468             buf.Zero();
       
   469             aResult.Append(param);
       
   470             CleanupStack::Pop(param); // pointer to buf is stored in RPointerArray
       
   471             }
       
   472         // If not separator character we can store the character into array
       
   473         else
       
   474             {
       
   475             buf.Append(chr);
       
   476             }
       
   477         }
       
   478     // Remember to put last token into array (,a3)
       
   479     HBufC* param = buf.AllocLC();
       
   480     aResult.Append(param);
       
   481     CleanupStack::Pop(param);
       
   482     
       
   483     return ret;
       
   484 	}
       
   485 
       
   486 
       
   487 TBool CDataWrapperBase::GetEnumFromConfig(const TDesC& aSectName, const TDesC& aKeyName, const TEnumEntryTable* aTable, TInt& aResult)
       
   488 	{
       
   489 	TPtrC	str;
       
   490 	TBool	ret=GetStringFromConfig(aSectName, aKeyName, str);
       
   491 
       
   492 	if ( ret )
       
   493 		{
       
   494 		TBool	found=EFalse;
       
   495 		TInt	index=0;
       
   496 		while ( (aTable[index].iValue!=-1) && !found )
       
   497 			{
       
   498 			if ( aTable[index].iString==str )
       
   499 				{
       
   500 				found=ETrue;
       
   501 				aResult=aTable[index].iValue;
       
   502 				}
       
   503 			else
       
   504 				{
       
   505 				++index;
       
   506 				}
       
   507 			}
       
   508 
       
   509 		if ( !found )
       
   510 			{
       
   511 			ret=GetIntFromConfig(aSectName, aKeyName, aResult);
       
   512 			}
       
   513 		}
       
   514 
       
   515 	return ret;
       
   516 	}
       
   517