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