First public contribution.
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
18 #include "DataWrapperBase.h"
20 #define SECS_TO_MICROSEC(x) (x*1000000)
23 /// Constant Literals used.
24 _LIT(KPrefixHex, "0x");
25 _LIT(KPrefixOctal, "0");
26 _LIT(KSuffixBinary, "b");
28 _LIT(KIncludeSection, "include");
29 _LIT(KFile, "file%d");
30 _LIT(KMatch, "*{*,*}*");
32 _LIT(KSeparator, ",");
34 _LIT(KDataRead, "INI READ : %S %S %S");
35 _LIT(KTagPointX, "%S_x");
36 _LIT(KTagPointY, "%S_y");
37 _LIT(KTagRectMinX, "%S_minX");
38 _LIT(KTagRectMaxX, "%S_maxX");
39 _LIT(KTagRectMinY, "%S_minY");
40 _LIT(KTagRectMaxY, "%S_maxY");
41 _LIT(KTagSizeHeight, "%S_height");
42 _LIT(KTagSizeWidth, "%S_width");
43 _LIT(KEnumElements, "enum_elements");
44 _LIT(KPrefixHexKey, "0x%S");
46 _LIT(KCmdUtilityPromptMessage, "utilityPromptMessage");
47 _LIT(KPromptText, "message");
48 _LIT(KDelay, "delay");
49 _LIT(KExpectedKey, "keyCode");
52 const TInt KDefaultDelay = 10;
53 const TInt KBounceErrKey = 50000;
55 CDataWrapperBase::CDataWrapperBase()
60 CDataWrapperBase::~CDataWrapperBase()
65 iInclude.ResetAndDestroy();
66 iBuffer.ResetAndDestroy();
70 void CDataWrapperBase::InitialiseL()
73 CDataWrapper::InitialiseL();
74 TBuf<KMaxTestExecuteCommandLength> tempStore;
80 tempStore.Format(KFile(), ++index);
81 moreData=GetStringFromConfig(KIncludeSection, tempStore, fileName);
85 CIniData* iniData=CIniData::NewL(fileName);
86 CleanupStack::PushL(iniData);
87 iInclude.Append(iniData);
88 CleanupStack::Pop(iniData);
91 User::LeaveIfError(iFs.Connect());
94 TBool CDataWrapperBase::GetBoolFromConfig(const TDesC& aSectName,const TDesC& aKeyName,TBool& aResult)
96 * Reads the value present from the test steps ini file within the mentioned section name and key name
97 * Copies the value to the TBool reference passed in possible values TRUE, FALSE
98 * @param aSectName - Section within the test steps ini file
99 * @param aKeyName - Name of a key within a section
100 * @return aResult - The value of the boolean
101 * @return TBool - ETrue for found, EFalse for not found
106 TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
107 if ( err != KErrNone )
114 aResult=(result.FindF(KTrue) != KErrNotFound);
120 TBool CDataWrapperBase::GetIntFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TInt& aResult)
122 * Reads the value present from the test steps ini file within the mentioned section name and key name
123 * Copies the value to the TInt reference passed in
124 * @param aSectName - Section within the test steps ini file
125 * @param aKeyName - Name of a key within a section
126 * @return aResult - The value of the integer
127 * @return TBool - ETrue for found, EFalse for not found
132 TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
133 if ( err != KErrNone )
140 ret=(lex.Val(aResult)==KErrNone);
146 TBool CDataWrapperBase::GetStringFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TPtrC& aResult)
148 * Reads the value present from the test steps ini file within the mentioned section name and key name
149 * Copies the value to the TPtrC reference passed in
150 * @param aSectName - Section within the test steps ini file
151 * @param aKeyName - Name of a key within a section
152 * @return aResult - Reference to the string on the heap
153 * @return TBool - ETrue for found, EFalse for not found
157 TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, aResult));
158 if ( err != KErrNone )
165 TBool CDataWrapperBase::GetHexFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TInt& aResult)
167 * Reads the value present from the test steps ini file within the mentioned section name and key name
168 * Copies the value to the TInt reference passed in. The value can optionally be prefixed with 0x
169 * @param aSectName - Section within the test steps ini file
170 * @param aKeyName - Name of a key within a section
171 * @return aResult - The integer value of the Hex input
172 * @return TBool - ETrue for found, EFalse for not found
177 TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
178 if ( err != KErrNone )
185 if( result.FindC(KPrefixHex)==KErrNone )
187 lex=result.Mid(KPrefixHex().Length());
193 ret=(lex.Val((TUint &)aResult, EHex)==KErrNone);
199 TBool CDataWrapperBase::GetUintFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TUint& aResult)
201 * Reads the value present from the test steps ini file within the mentioned section name and key name
202 * Copies the value to the TUint reference passed in.
203 * If the value is prefixed with 0x the value is read as a hexidecimal value
204 * If the value is suffixed with b the value is read as a binary value
205 * If the value is prefixed with a 0 the value is read as an octal value
206 * If it does not match the above it is read in as an integer
207 * @param aSectName - Section within the test steps ini file
208 * @param aKeyName - Name of a key within a section
209 * @return aResult - The integer value of the Hex input
210 * @return TBool - ETrue for found, EFalse for not found
215 TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result));
216 if ( err != KErrNone )
223 if( result.FindC(KPrefixHex)==KErrNone )
225 lex=result.Mid(KPrefixHex().Length());
226 ret=(lex.Val(aResult, EHex)==KErrNone);
230 TInt binarySuffixPosition=result.Length()-KSuffixBinary().Length();
231 if ( result.FindC(KSuffixBinary)==binarySuffixPosition )
233 lex=result.Left(binarySuffixPosition);
234 ret=(lex.Val(aResult, EBinary)==KErrNone);
238 if( result.FindC(KPrefixOctal)==KErrNone )
240 ret=(lex.Val(aResult, EOctal)==KErrNone);
245 ret=(lex.Val(intResult)==KErrNone);
248 aResult=(TUint)intResult;
258 TBool CDataWrapperBase::GetEnumFromConfig(const TDesC& aSectName, const TDesC& aKeyName, const TEnumEntryTable* aTable, TInt& aResult)
261 TBool ret=GetStringFromConfig(aSectName, aKeyName, str);
267 while ( (aTable[index].iValue!=-1) && !found )
269 if ( aTable[index].iString==str )
272 aResult=aTable[index].iValue;
282 ret=GetIntFromConfig(aSectName, aKeyName, aResult);
289 TBool CDataWrapperBase::GetOrFromConfig(const TDesC& aSectName, const TDesC& aKeyName, const TEnumEntryTable* aTable, TUint& aResult)
292 TBool ret=GetStringFromConfig(aSectName, aKeyName, str);
296 ret=ProcessString(str, temp, aTable);
306 TBool CDataWrapperBase::ProcessString(const TDesC& aStr, TUint& aResult, const TEnumEntryTable* aTable)
309 TInt location=aStr.Match(_L("*|*"));
310 if( location!=KErrNotFound )
312 // Converting Left part of the data
313 TPtrC tempStr=aStr.Left(location);
314 ret=ProcessString(tempStr, aResult, aTable);
316 // Converting right data can be with another "|"
317 tempStr.Set(aStr.Mid(location+1));
320 if ( ProcessString(tempStr, temp, aTable) )
331 ret=ProcessEntry(aStr, aResult, aTable);
337 TBool CDataWrapperBase::ProcessEntry(const TDesC& aStr, TUint& aResult, const TEnumEntryTable* aTable)
343 while ( (aTable[index].iValue!=-1) && !found )
345 if ( aTable[index].iString==aStr )
348 aResult=aTable[index].iValue;
360 ret=(lex.Val(flags, EHex)==KErrNone);
370 TBool CDataWrapperBase::GetCommandStringParameterL(const TDesC& aSectName, const TDesC& aKeyName, TPtrC& aResult)
374 if ( aSectName.Length()!=0 )
376 ret=CDataWrapper::GetStringFromConfig(aSectName, aKeyName, aResult);
378 for ( TInt index=iInclude.Count(); (index>0) && (!ret); )
380 ret=iInclude[--index]->FindVar(aSectName, aKeyName, aResult);
383 INFO_PRINTF4(KDataRead, &aSectName, &aKeyName, &aResult);
390 if ( aResult.Match(KMatch)!=KErrNotFound )
392 // We have an entry of the format
393 // entry =*{section,entry}*
394 // where * is one or more characters
395 // We need to construct this from other data in the ini file replacing {*,*}
396 // with the data from
399 HBufC* buffer=HBufC::NewLC(aResult.Length());
400 buffer->Des().Copy(aResult);
402 TInt startLength=KStart().Length();
403 TInt sparatorLength=KSeparator().Length();
404 TInt endLength=KEnd().Length();
413 bufferLength=buffer->Length();
414 start=buffer->Find(KStart);
416 remaining.Set(buffer->Des().Right(bufferLength-start-startLength));
417 sparator=remaining.Find(KSeparator);
418 remaining.Set(remaining.Right(remaining.Length()-sparator-sparatorLength));
419 sparator += (start + startLength);
421 end=remaining.Find(KEnd) + sparator + sparatorLength;
423 TPtrC sectionName(buffer->Ptr()+start+startLength, sparator-start-startLength);
424 TPtrC keyName(buffer->Ptr()+sparator+sparatorLength, end-sparator-sparatorLength);
425 sectionName.Set(TLex(sectionName).NextToken());
426 keyName.Set(TLex(keyName).NextToken());
430 TBool found=CDataWrapper::GetStringFromConfig(sectionName, keyName, entryData);
431 for ( TInt index=iInclude.Count(); (index>0) && (!found); )
433 found=iInclude[--index]->FindVar(sectionName, keyName, entryData);
437 entrySize=entryData.Length();
440 TInt newLength=start + bufferLength - end - endLength + entrySize;
441 HBufC* bufferNew=HBufC::NewLC(newLength);
442 bufferNew->Des().Copy(buffer->Ptr(), start);
445 bufferNew->Des().Append(entryData);
447 bufferNew->Des().Append(buffer->Ptr() + end + endLength, bufferLength - end - endLength);
448 CleanupStack::Pop(bufferNew);
449 CleanupStack::PopAndDestroy(buffer);
451 CleanupStack::PushL(buffer);
453 while ( buffer->Match(KMatch)!=KErrNotFound );
454 iBuffer.Append(buffer);
455 CleanupStack::Pop(buffer);
456 aResult.Set(*buffer);
457 INFO_PRINTF4(KDataRead, &aSectName, &aKeyName, &aResult);
464 TBool CDataWrapperBase::GetCommandStringParameter(const TDesC& aParameterName, const TDesC& aSection, TPtrC& aResult, TText8 *aFileName, TInt aLine, TBool aMandatory)
466 TBool ret = GetStringFromConfig(aSection, aParameterName, aResult);
467 if (aMandatory && !ret)
469 Logger().LogExtra(aFileName, aLine, ESevrErr, _L("No %S"), &aParameterName);
470 SetBlockResult(EFail);
475 TBool CDataWrapperBase::GetCommandIntParameter(const TDesC& aParameterName, const TDesC& aSection, TInt& aResult, TText8* aFileName, TInt aLine, TBool aMandatory)
477 TBool ret = GetIntFromConfig(aSection, aParameterName, aResult);
478 if (aMandatory && !ret)
480 Logger().LogExtra(aFileName, aLine, ESevrErr, _L("No %S"), &aParameterName);
481 SetBlockResult(EFail);
486 TBool CDataWrapperBase::GetCommandBoolParameter(const TDesC& aParameterName, const TDesC& aSection, TBool& aResult, TText8 *aFileName, TInt aLine, TBool aMandatory)
488 TBool ret = GetBoolFromConfig(aSection, aParameterName, aResult);
489 if (aMandatory && !ret)
491 Logger().LogExtra(aFileName, aLine, ESevrErr, _L("No %S"), &aParameterName);
492 SetBlockResult(EFail);
497 TBool CDataWrapperBase::GetPointFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TPoint& aResult)
499 * Reads the value present from the test steps ini file within the mentioned section name and key name
500 * Copies the value to the TPoint reference passed in
501 * @param aSectName - Section within the test steps ini file
502 * @param aKeyName - Name of a key within a section
503 * @return aResult - The value of the TPoint
504 * @return TBool - ETrue for found, EFalse for not found
507 TBuf<KMaxTestExecuteCommandLength> tempStore;
510 tempStore.Format(KTagPointX, &aKeyName);
511 TBool ret=GetIntFromConfig(aSectName, tempStore, x);
513 tempStore.Format(KTagPointY, &aKeyName);
514 if ( !GetIntFromConfig(aSectName, tempStore, y) )
526 TBool CDataWrapperBase::GetRectFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TRect& aResult)
528 * Reads the value present from the test steps ini file within the mentioned section name and key name
529 * Copies the value to the TPoint reference passed in
530 * @param aSectName - Section within the test steps ini file
531 * @param aKeyName - Name of a key within a section
532 * @return aResult - The value of the TRect
533 * @return TBool - ETrue for found, EFalse for not found
536 TBuf<KMaxTestExecuteCommandLength> tempStore;
539 tempStore.Format(KTagRectMinX, &aKeyName);
540 TBool ret=GetIntFromConfig(aSectName, tempStore, minX);
543 tempStore.Format(KTagRectMaxX, &aKeyName);
544 if ( !GetIntFromConfig(aSectName, tempStore, maxX) )
550 tempStore.Format(KTagRectMinY, &aKeyName);
551 if ( !GetIntFromConfig(aSectName, tempStore, minY) )
557 tempStore.Format(KTagRectMaxY, &aKeyName);
558 if ( !GetIntFromConfig(aSectName, tempStore, maxY) )
565 aResult.SetRect(minX, minY, maxX, maxY);
571 TBool CDataWrapperBase::GetSizeFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TSize& aResult)
573 * Reads the value present from the test steps ini file within the mentioned section name and key name
574 * Copies the value to the TPoint reference passed in
575 * @param aSectName - Section within the test steps ini file
576 * @param aKeyName - Name of a key within a section
577 * @return aResult - The value of the TPoint
578 * @return TBool - ETrue for found, EFalse for not found
581 TBuf<KMaxTestExecuteCommandLength> tempStore;
584 tempStore.Format(KTagSizeHeight, &aKeyName);
585 TBool ret=GetIntFromConfig(aSectName, tempStore, height);
587 tempStore.Format(KTagSizeWidth, &aKeyName);
588 if ( !GetIntFromConfig(aSectName, tempStore, width) )
594 aResult.SetSize(width, height);
600 TBool CDataWrapperBase::GetUint8FromConfig(const TDesC& aSectName, const TDesC& aKeyName, TUint8& aResult)
603 TBool ret=GetUintFromConfig(aSectName, aKeyName,temp);
606 aResult=(TUint8)temp;
612 void CDataWrapperBase::Timedelay(TInt aTimeoutInSecs)
613 /*Utility function to produce time delay
614 @param aTimeoutInSecs Times in micro seconds
617 TRequestStatus status;
618 iTimer.After(status, aTimeoutInSecs);
619 User::WaitForRequest(status);
622 TBool CDataWrapperBase::TranslateEnumToString(CDataWrapper& aDataWrapper,
626 /* Function translates
633 TInt intError = desToWrapInt.Create(KMaxFullName);
634 TInt hexError = desToFormHex.Create(KMaxFullName);
635 if(intError == KErrNone && hexError == KErrNone)
637 desToFormHex.NumFixedWidth(aValue, EHex, 8);
638 desToWrapInt.Format(KPrefixHexKey, &desToFormHex);
639 if (!aDataWrapper.GetStringFromConfig(aEnum, desToWrapInt, static_cast<TPtrC&>(aTranslation)))
649 if(hexError == KErrNone)
651 desToFormHex.Close();
653 if(intError == KErrNone)
655 desToWrapInt.Close();
661 TBool CDataWrapperBase::ProcessEnumList(CDataWrapper& aDataWrapper,
668 TInt location=aStr.Match(_L("*|*"));
669 if( location!=KErrNotFound )
671 // Converting Left part of the data
672 TPtrC tempStr=aStr.Left(location);
673 ret=ProcessEnumList(aDataWrapper, tempStr, aEnum, aValue, aTranslation);
675 // Converting right data can be with another "|"
676 tempStr.Set(aStr.Mid(location+1));
678 if ( !ProcessEnumList(aDataWrapper, tempStr, aEnum, aValue, aTranslation) )
686 if (aDataWrapper.GetHexFromConfig(aEnum, aStr, value ))
690 if ( aTranslation.Length() )
692 aTranslation.Append(_L("|"));
695 aTranslation.Append(aStr);
707 TBool CDataWrapperBase::TranslateNumberToEnumStringL(CDataWrapper& aDataWrapper,
708 const TDesC& aSection,
715 if ( aDataWrapper.GetStringFromConfig(aSection, KEnumElements, enumElements) )
717 ret = ProcessEnumList(aDataWrapper, enumElements, aSection, aValue, aTranslation);
723 * Function which creates a consoles and writes to and reads from the console
725 * @param aMsg Message wrote to the console
726 * @param aDelayMiliSec waiting period for user input, read from config file
727 * @param aKeyCode specifies key required to be pressed to pass the test
729 * @leave System wide error
732 void CDataWrapperBase::PromptMessageL(const TDesC& aMsg,
733 const TInt aDelayMiliSec,
736 CConsoleBase* console = GetConsoleL();
737 CleanupStack::PushL(console);
739 if( aMsg.Length() != 0 )
741 console->Printf(aMsg);
743 TRequestStatus keyStatus(KRequestPending);
744 TRequestStatus timerStatus(KRequestPending);
746 iTimer.After(timerStatus,SECS_TO_MICROSEC(aDelayMiliSec));
747 console->Read(keyStatus);
748 User::WaitForRequest(keyStatus,timerStatus);
749 if (keyStatus!=KRequestPending)
753 aKeyCode =console->KeyCode();
754 User::WaitForRequest(timerStatus);
758 console->ReadCancel();
759 SetBlockResult(EFail);
762 CleanupStack::PopAndDestroy(console);
765 * Process a command read from the script file calling the required function
767 * @param aSection The section in the ini containing data for the command
768 * @param aCommand Command to be called
770 * @leave System wide error
773 TBool CDataWrapperBase::DoCommandL(const TTEFFunction& aCommand, const TTEFSectionName& aSection, const TInt /*aAsyncErrorIndex*/)
776 if ( aCommand==KCmdUtilityPromptMessage )
778 DoCmdUtilityPromptMessageL(aSection);
782 ERR_PRINTF1(_L("No Function Matches Requested Command"));
783 SetBlockResult(EFail);
789 * Functon called through TEF for manual verification which writes a message to the console and reads user input keyboard
791 * @param aSection The section in the ini containing data for the command
793 * @leave System wide error
796 void CDataWrapperBase::DoCmdUtilityPromptMessageL(const TDesC& aSection)
799 INFO_PRINTF1(_L("DoCmdUtilityPromptMessageL called"));
802 TBool hasMessage = GetStringFromConfig(aSection, KPromptText(), message);
806 INFO_PRINTF1(_L("Prompt message not specified in the ini file"));
809 // 10 second default delay
810 TInt delay = KDefaultDelay;
811 GetIntFromConfig(aSection, KDelay(), delay);
815 PromptMessageL(message, delay, keyCode);
816 }while (keyCode>KBounceErrKey);
817 INFO_PRINTF2(_L("Key %d pressed"), keyCode);
819 if( GetIntFromConfig(aSection, KExpectedKey(), expectedKey) )
821 // compares key pressed with key in config file
822 if(keyCode != (TKeyCode)expectedKey)
824 ERR_PRINTF3(_L("expectedKey=%d pressed key=%d"), expectedKey, keyCode);
825 SetBlockResult(EFail);
830 ERR_PRINTF1(_L("KeyCode not specified in the ini file"));
831 SetBlockResult(EFail);