hti/HtiServicePlugins/HtiStifTfServicePlugin/src/HtiStifTfIf.cpp
branchRCL_3
changeset 59 8ad140f3dd41
parent 0 a03f92240627
equal deleted inserted replaced
49:7fdc9a71d314 59:8ad140f3dd41
       
     1 /*
       
     2 * Copyright (c) 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:  Implements the functions to control STIF Test Framework
       
    15 *                through the STIF TF interface.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include <badesca.h>
       
    23 #include <bautils.h>
       
    24 #include <charconv.h>
       
    25 #include <e32cons.h>
       
    26 #include <HtiLogging.h>
       
    27 #include "HtiStifTfIf.h"
       
    28 
       
    29 // EXTERNAL DATA STRUCTURES
       
    30 
       
    31 // EXTERNAL FUNCTION PROTOTYPES
       
    32 
       
    33 // CONSTANTS
       
    34 
       
    35 // MACROS
       
    36 
       
    37 // LOCAL CONSTANTS AND MACROS
       
    38 const static TUid KStifTfServiceUid = { 0x10210CC3 };
       
    39 
       
    40 enum TStifTfCommands
       
    41     {
       
    42     ECmdOpen            = 0x01,
       
    43     ECmdClose           = 0x02,
       
    44     ECmdLoadModule      = 0x03,
       
    45     ECmdUnloadModule    = 0x04,
       
    46     ECmdListCases       = 0x05,
       
    47     ECmdStartCase       = 0x06,
       
    48     ECmdCancelCase      = 0x07,
       
    49     ECmdPauseCase       = 0x08,
       
    50     ECmdResumeCase      = 0x09,
       
    51     ECmdAddCaseFile     = 0x0A,
       
    52     ECmdRemoveCaseFile  = 0x0B,
       
    53     ECmdCaseMsg         = 0x0C,
       
    54     ECmdSetDevId        = 0x0D,
       
    55     ECmdTestCompleted   = 0x0E,
       
    56     ECmdAtsMessage      = 0x0F,
       
    57     ECmdSetAttribute    = 0x10
       
    58     };
       
    59 
       
    60 _LIT8( KParameterDelimiter, "|" );
       
    61 
       
    62 // NOTE: Max length for error description is defined
       
    63 // in HtiDispatcherInterface.h (currently 118).
       
    64 _LIT8( KErrorStifTfNotOpen, "ERROR: STIF TF is not open" );
       
    65 _LIT8( KErrorOpen, "ERROR: Failed to open STIF TF" );
       
    66 _LIT8( KErrorAlreadyOpen, "ERROR: STIF TF is already open" );
       
    67 _LIT8( KErrorClose, "ERROR: Failed to close STIF TF" );
       
    68 _LIT8( KErrorUnknownCmd, "ERROR: Unknown STIF TF command" );
       
    69 _LIT8( KErrorNoCmd, "ERROR: No command given" );
       
    70 _LIT8( KErrorParameterMissing, "ERROR: Mandatory parameter missing" );
       
    71 _LIT8( KErrorParameterTooLong, "ERROR: Parameter too long" );
       
    72 _LIT8( KErrorTooManyParameters, "ERROR: Too many parameters" );
       
    73 _LIT8( KErrorNoMemory, "ERROR: Memory allocation failure" );
       
    74 _LIT8( KErrorLoadModule, "ERROR: Load Module failed" );
       
    75 _LIT8( KErrorUnloadModule, "ERROR: Unload Module failed" );
       
    76 _LIT8( KErrorInvalidCaseNumber, "ERROR: Invalid test case number parameter" );
       
    77 _LIT8( KErrorModuleNotLoaded, "ERROR: Given test module is not loaded" );
       
    78 _LIT8( KErrorGetTestCases, "ERROR: Failed to get test cases from module" );
       
    79 _LIT8( KErrorGetTestCaseInfos, "ERROR: Failed to get test case infos" );
       
    80 _LIT8( KErrorStartTestCase, "ERROR: Failed to start test case" );
       
    81 _LIT8( KErrorCancelTestCase, "ERROR: Failed to cancel test case" );
       
    82 _LIT8( KErrorPauseTestCase, "ERROR: Failed to pause test case" );
       
    83 _LIT8( KErrorResumeTestCase, "ERROR: Failed to resume test case" );
       
    84 _LIT8( KErrorInvalidDeviceId, "ERROR: Invalid device ID parameter" );
       
    85 _LIT8( KErrorAddCaseFile, "ERROR: Add Case File failed" );
       
    86 _LIT8( KErrorRemoveCaseFile, "ERROR: Remove Case File failed" );
       
    87 _LIT8( KErrorFileServer, "ERROR: Failed to connect to file server" );
       
    88 _LIT8( KErrorCaseFileNotFound, "ERROR: Given test case file was not found" );
       
    89 _LIT8( KErrorIniFileNotFound, "ERROR: Given ini-file was not found" );
       
    90 _LIT8( KErrorCaseMessage, "ERROR: Test case message sending failed" );
       
    91 _LIT8( KErrorCharConv, "ERROR: Character conversion failed" );
       
    92 _LIT8( KErrorInvalidAttributeId, "ERROR: Invalid attribute id parameter" );
       
    93 _LIT8( KErrorSetAttribute, "ERROR: Failed to set attribute" );
       
    94 _LIT8( KErrorUnknown, "ERROR: Unspecified error occured" );
       
    95 
       
    96 // MODULE DATA STRUCTURES
       
    97 
       
    98 // LOCAL FUNCTION PROTOTYPES
       
    99 
       
   100 // FORWARD DECLARATIONS
       
   101 
       
   102 
       
   103 // ============================ MEMBER FUNCTIONS ===============================
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 // CHtiStifTfIf::CHtiStifTfIf
       
   107 // C++ default constructor can NOT contain any code, that
       
   108 // might leave.
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 CHtiStifTfIf::CHtiStifTfIf():iIsBusy( EFalse ),
       
   112                              iMessage( NULL ),
       
   113                              iErrorCode( 0 ),
       
   114                              iStifTfOpen( EFalse ),
       
   115                              iDevId( KDefaultDevId ),
       
   116                              iConsole( NULL )
       
   117     {
       
   118     }
       
   119 
       
   120 // -----------------------------------------------------------------------------
       
   121 // CHtiStifTfIf::ConstructL
       
   122 // Symbian 2nd phase constructor can leave.
       
   123 // -----------------------------------------------------------------------------
       
   124 //
       
   125 void CHtiStifTfIf::ConstructL( CHtiStifTfServicePlugin* aPlugin )
       
   126     {
       
   127     iCommandId = 0;
       
   128     iParameters = new (ELeave) CDesC8ArrayFlat( 5 );
       
   129     iPlugin = aPlugin;
       
   130     }
       
   131 
       
   132 // -----------------------------------------------------------------------------
       
   133 // CHtiStifTfIf::NewL
       
   134 // Two-phased constructor.
       
   135 // -----------------------------------------------------------------------------
       
   136 //
       
   137 CHtiStifTfIf* CHtiStifTfIf::NewL( CHtiStifTfServicePlugin* aPlugin )
       
   138     {
       
   139     CHtiStifTfIf* self = new( ELeave ) CHtiStifTfIf;
       
   140     CleanupStack::PushL( self );
       
   141     self->ConstructL( aPlugin );
       
   142     CleanupStack::Pop();
       
   143     return self;
       
   144     }
       
   145 
       
   146 
       
   147 // Destructor
       
   148 CHtiStifTfIf::~CHtiStifTfIf()
       
   149     {
       
   150     if ( iParameters )
       
   151         {
       
   152         iParameters->Reset();
       
   153         }
       
   154     delete iParameters;
       
   155     iLoadedInfos.ResetAndDestroy();
       
   156     iLoadedInfos.Close();
       
   157     delete iMessage;
       
   158     if ( iStifTfOpen ) Close();
       
   159     iPlugin = NULL;  // Not owned, so not deleted.
       
   160     iConsole = NULL; // Not owned, so not deleted.
       
   161     }
       
   162 
       
   163 
       
   164 // -----------------------------------------------------------------------------
       
   165 // CHtiStifTfIf::ProcessMessageL
       
   166 // Interpretes the message and calls appropriate command handling method.
       
   167 // -----------------------------------------------------------------------------
       
   168 //
       
   169 void CHtiStifTfIf::ProcessMessageL( const TDesC8& aMessage,
       
   170     THtiMessagePriority /*aPriority*/)
       
   171     {
       
   172     HTI_LOG_FUNC_IN( "CHtiStifTfIf::ProcessMessageL" );
       
   173     HTI_LOG_FORMAT( "Message length = %d", aMessage.Length() );
       
   174 
       
   175     if ( iIsBusy )
       
   176         {
       
   177         HTI_LOG_TEXT( "Plugin is busy - leaving" );
       
   178         User::Leave( KErrInUse );
       
   179         }
       
   180 
       
   181     iIsBusy = ETrue;
       
   182 
       
   183     if ( aMessage.Length() < 1 )
       
   184         {
       
   185         User::LeaveIfError(
       
   186             SendErrorResponseMsg( KErrArgument, KErrorNoCmd ) );
       
   187         return;
       
   188         }
       
   189 
       
   190     iCommandId = aMessage[0];
       
   191     HTI_LOG_FORMAT( "Command = %d", iCommandId );
       
   192 
       
   193     TRAPD( err, ParseParametersL( aMessage ) );
       
   194 
       
   195     if ( err == KErrNoMemory )
       
   196         {
       
   197         User::LeaveIfError( SendErrorResponseMsg( err, KErrorNoMemory ) );
       
   198         return;
       
   199         }
       
   200 
       
   201     else if ( err )
       
   202         {
       
   203         User::LeaveIfError(
       
   204             SendErrorResponseMsg( KErrGeneral, KErrorUnknown ) );
       
   205         return;
       
   206         }
       
   207 
       
   208     // If STIF TF is not open, any other command than OPEN returns KErrNotReady
       
   209     if ( !iStifTfOpen )
       
   210         {
       
   211         if ( iCommandId == ECmdOpen )
       
   212             {
       
   213             HandleOpenCmdL();
       
   214             return;
       
   215             }
       
   216         else
       
   217             {
       
   218             User::LeaveIfError(
       
   219                 SendErrorResponseMsg( KErrNotReady, KErrorStifTfNotOpen ) );
       
   220             return;
       
   221             }
       
   222         }
       
   223 
       
   224 
       
   225     // Go through the rest of the commands
       
   226     if ( iCommandId == ECmdLoadModule )
       
   227         {
       
   228         HandleLoadModuleCmdL();
       
   229         }
       
   230 
       
   231     else if ( iCommandId == ECmdUnloadModule )
       
   232         {
       
   233         HandleUnloadModuleCmdL();
       
   234         }
       
   235 
       
   236     else if ( iCommandId == ECmdListCases )
       
   237         {
       
   238         HandleListCasesCmdL();
       
   239         }
       
   240 
       
   241     else if ( iCommandId == ECmdStartCase )
       
   242         {
       
   243         HandleStartCaseCmdL();
       
   244         }
       
   245 
       
   246     else if ( iCommandId == ECmdCancelCase )
       
   247         {
       
   248         HandleCancelCaseCmdL();
       
   249         }
       
   250 
       
   251     else if ( iCommandId == ECmdPauseCase )
       
   252         {
       
   253         HandlePauseCaseCmdL();
       
   254         }
       
   255 
       
   256     else if ( iCommandId == ECmdResumeCase )
       
   257         {
       
   258         HandleResumeCaseCmdL();
       
   259         }
       
   260 
       
   261     else if ( iCommandId == ECmdAddCaseFile )
       
   262         {
       
   263         HandleAddCaseFileCmdL();
       
   264         }
       
   265 
       
   266     else if ( iCommandId == ECmdRemoveCaseFile )
       
   267         {
       
   268         HandleRemoveCaseFileCmdL();
       
   269         }
       
   270 
       
   271     else if ( iCommandId == ECmdCaseMsg )
       
   272         {
       
   273         HandleCaseMsgCmdL();
       
   274         }
       
   275 
       
   276     else if ( iCommandId == ECmdSetDevId )
       
   277         {
       
   278         HandleSetDevIdCmdL();
       
   279         }
       
   280 
       
   281     else if ( iCommandId == ECmdSetAttribute )
       
   282         {
       
   283         HandleSetAttributeCmdL();
       
   284         }
       
   285 
       
   286     else if ( iCommandId == ECmdClose )
       
   287         {
       
   288         HandleCloseCmdL();
       
   289         }
       
   290 
       
   291     else if ( iCommandId == ECmdOpen )
       
   292         {
       
   293         User::LeaveIfError(
       
   294             SendErrorResponseMsg( KErrArgument, KErrorAlreadyOpen ) );
       
   295         }
       
   296 
       
   297     else
       
   298         {
       
   299         User::LeaveIfError(
       
   300             SendErrorResponseMsg( KErrArgument, KErrorUnknownCmd ) );
       
   301         }
       
   302 
       
   303     HTI_LOG_FUNC_OUT( "CHtiStifTfIf::ProcessMessageL" );
       
   304     }
       
   305 
       
   306 
       
   307 // -----------------------------------------------------------------------------
       
   308 // CHtiStifTfIf::HandleOpenCmdL
       
   309 // Handles opening of the STIF Test Framework.
       
   310 // -----------------------------------------------------------------------------
       
   311 //
       
   312 void CHtiStifTfIf::HandleOpenCmdL()
       
   313     {
       
   314     HTI_LOG_FUNC_IN( "CHtiStifTfIf::HandleOpenCmdL" );
       
   315 
       
   316     TInt err = KErrNone;
       
   317 
       
   318     // OPEN command has 1 optional parameter (test framework ini-file)
       
   319     if ( iParameters->Count() == 0 )
       
   320         {
       
   321         err = Open();
       
   322         if ( err )
       
   323             {
       
   324             SendErrorResponseMsg( err, KErrorOpen );
       
   325             return;
       
   326             }
       
   327 
       
   328         iStifTfOpen = ETrue;
       
   329         SendResponseMsg( _L8( "OK" ) );
       
   330         }
       
   331 
       
   332     else if ( iParameters->Count() == 1 )
       
   333         {
       
   334         TBuf<KMaxFileName> iniFilePath;
       
   335         if ( (*iParameters)[0].Length() > iniFilePath.MaxLength() )
       
   336             {
       
   337             SendErrorResponseMsg( KErrArgument, KErrorParameterTooLong );
       
   338             return;
       
   339             }
       
   340 
       
   341         iniFilePath.Copy( (*iParameters)[0] );
       
   342 
       
   343         RFs fsSession;
       
   344         err = fsSession.Connect();
       
   345         if ( err )
       
   346             {
       
   347             SendErrorResponseMsg( err, KErrorFileServer );
       
   348             return;
       
   349             }
       
   350 
       
   351         if ( !( BaflUtils::FileExists( fsSession, iniFilePath ) ) )
       
   352             {
       
   353             SendErrorResponseMsg( KErrNotFound, KErrorIniFileNotFound );
       
   354             fsSession.Close();
       
   355             return;
       
   356             }
       
   357 
       
   358         fsSession.Close();
       
   359 
       
   360         err = Open( iniFilePath );
       
   361 
       
   362         if ( err )
       
   363             {
       
   364             SendErrorResponseMsg( err, KErrorOpen );
       
   365             return;
       
   366             }
       
   367 
       
   368         // Load the test case infos of all currently loaded test
       
   369         // modules (modules listed in the given ini-file).
       
   370         TRAP( err, LoadTestCaseInfosL() );
       
   371         if ( err )
       
   372             {
       
   373             SendErrorResponseMsg( err, KErrorGetTestCaseInfos );
       
   374             return;
       
   375             }
       
   376 
       
   377         iStifTfOpen = ETrue;
       
   378         SendResponseMsg( _L8( "OK" ) );
       
   379         }
       
   380 
       
   381     else
       
   382         {
       
   383         SendErrorResponseMsg( KErrArgument, KErrorTooManyParameters );
       
   384         }
       
   385 
       
   386     HTI_LOG_FUNC_OUT( "CHtiStifTfIf::HandleOpenCmdL" );
       
   387     }
       
   388 
       
   389 
       
   390 // -----------------------------------------------------------------------------
       
   391 // CHtiStifTfIf::HandleCloseCmdL
       
   392 // Closes the STIF Test Framework.
       
   393 // -----------------------------------------------------------------------------
       
   394 //
       
   395 void CHtiStifTfIf::HandleCloseCmdL()
       
   396     {
       
   397     HTI_LOG_FUNC_IN( "CHtiStifTfIf::HandleCloseCmdL" );
       
   398 
       
   399     TInt err = KErrNone;
       
   400 
       
   401     // CLOSE command has no parameters
       
   402     if ( iParameters->Count() == 0 )
       
   403         {
       
   404         err = Close();
       
   405         if ( err )
       
   406             {
       
   407             SendErrorResponseMsg( err, KErrorClose );
       
   408             return;
       
   409             }
       
   410 
       
   411         iStifTfOpen = EFalse;
       
   412         iLoadedInfos.ResetAndDestroy(); // empty the cache of test case infos
       
   413         SendResponseMsg( _L8( "OK" ) );
       
   414         }
       
   415 
       
   416     else
       
   417         {
       
   418         SendErrorResponseMsg( KErrArgument, KErrorTooManyParameters );
       
   419         }
       
   420 
       
   421     HTI_LOG_FUNC_OUT( "CHtiStifTfIf::HandleCloseCmdL" );
       
   422     }
       
   423 
       
   424 
       
   425 // -----------------------------------------------------------------------------
       
   426 // CHtiStifTfIf::HandleLoadModuleCmdL
       
   427 // Loads a STIF TF test module (optionally with ini-file).
       
   428 // Also calls the LoadTestCaseInfosL helper method to load the test case info
       
   429 // objects from the test module.
       
   430 // -----------------------------------------------------------------------------
       
   431 //
       
   432 void CHtiStifTfIf::HandleLoadModuleCmdL()
       
   433     {
       
   434     HTI_LOG_FUNC_IN( "CHtiStifTfIf::HandleLoadModuleCmdL" );
       
   435 
       
   436     TInt err = KErrNone;
       
   437 
       
   438     TBuf<KMaxFileName> moduleName;
       
   439     TBuf<KMaxFileName> iniFilePath;
       
   440 
       
   441     // LOAD_MODULE command has 1 mandatory parameter (module name)
       
   442     // and 1 optional parameter (ini-file path).
       
   443     if ( iParameters->Count() == 0 )
       
   444         {
       
   445         SendErrorResponseMsg( KErrArgument, KErrorParameterMissing );
       
   446         return;
       
   447         }
       
   448 
       
   449     else if ( iParameters->Count() == 1 )
       
   450         {
       
   451         if ( (*iParameters)[0].Length() > moduleName.MaxLength() )
       
   452             {
       
   453             SendErrorResponseMsg( KErrArgument, KErrorParameterTooLong );
       
   454             return;
       
   455             }
       
   456         moduleName.Copy( (*iParameters)[0] );
       
   457         err = AddTestModule( moduleName );
       
   458         }
       
   459 
       
   460     else if ( iParameters->Count() == 2 )
       
   461         {
       
   462         if ( (*iParameters)[0].Length() > moduleName.MaxLength() ||
       
   463              (*iParameters)[1].Length() > iniFilePath.MaxLength() )
       
   464             {
       
   465             SendErrorResponseMsg( KErrArgument, KErrorParameterTooLong );
       
   466             return;
       
   467             }
       
   468 
       
   469         moduleName.Copy( (*iParameters)[0] );
       
   470         iniFilePath.Copy( (*iParameters)[1] );
       
   471 
       
   472         RFs fsSession;
       
   473         err = fsSession.Connect();
       
   474         if ( err )
       
   475             {
       
   476             SendErrorResponseMsg( err, KErrorFileServer );
       
   477             return;
       
   478             }
       
   479 
       
   480         if ( !( BaflUtils::FileExists( fsSession, iniFilePath ) ) )
       
   481             {
       
   482             SendErrorResponseMsg( KErrNotFound, KErrorIniFileNotFound );
       
   483             fsSession.Close();
       
   484             return;
       
   485             }
       
   486 
       
   487         fsSession.Close();
       
   488 
       
   489         err = AddTestModule( moduleName, iniFilePath );
       
   490         }
       
   491 
       
   492     else
       
   493         {
       
   494         SendErrorResponseMsg( KErrArgument, KErrorTooManyParameters );
       
   495         return;
       
   496         }
       
   497 
       
   498     if ( err )
       
   499         {
       
   500         HTI_LOG_FORMAT( "LoadModule: Error %d", err );
       
   501         SendErrorResponseMsg( err, KErrorLoadModule );
       
   502         }
       
   503 
       
   504     else
       
   505         {
       
   506         TRAP( err, LoadTestCaseInfosL( moduleName ) );
       
   507 
       
   508         if ( err )
       
   509             {
       
   510             HTI_LOG_FORMAT(
       
   511                 "LoadModule - Error from LoadTestCaseInfosL %d", err );
       
   512             RemoveTestModule( moduleName );
       
   513             SendErrorResponseMsg( err, KErrorGetTestCases );
       
   514             return;
       
   515             }
       
   516 
       
   517         // Sends the name of the loaded module as a response.
       
   518         SendResponseMsg( (*iParameters)[0] );
       
   519         }
       
   520 
       
   521     HTI_LOG_FUNC_OUT( "CHtiStifTfIf::HandleLoadModuleCmdL" );
       
   522     }
       
   523 
       
   524 
       
   525 // -----------------------------------------------------------------------------
       
   526 // CHtiStifTfIf::HandleUnloadModuleCmdL
       
   527 // Unloads a STIF TF test module.
       
   528 // Also removes and deletes the corresponding CHtiTestModuleCaseInfos object
       
   529 // from iLoadedInfos pointer array.
       
   530 // -----------------------------------------------------------------------------
       
   531 //
       
   532 void CHtiStifTfIf::HandleUnloadModuleCmdL()
       
   533     {
       
   534     HTI_LOG_FUNC_IN( "CHtiStifTfIf::HandleUnloadModuleCmdL" );
       
   535 
       
   536     TInt err = KErrNone;
       
   537 
       
   538     TBuf<KMaxFileName> moduleName;
       
   539 
       
   540     // UNLOAD_MODULE command has always 1 parameter (module name).
       
   541     if ( iParameters->Count() == 0 )
       
   542         {
       
   543         SendErrorResponseMsg( KErrArgument, KErrorParameterMissing );
       
   544         return;
       
   545         }
       
   546 
       
   547     if ( iParameters->Count() == 1 )
       
   548         {
       
   549         if ( (*iParameters)[0].Length() > moduleName.MaxLength() )
       
   550             {
       
   551             SendErrorResponseMsg( KErrArgument, KErrorParameterTooLong );
       
   552             return;
       
   553             }
       
   554         moduleName.Copy( (*iParameters)[0] );
       
   555         err = RemoveTestModule( moduleName );
       
   556         }
       
   557 
       
   558     else
       
   559         {
       
   560         SendErrorResponseMsg( KErrArgument, KErrorTooManyParameters );
       
   561         return;
       
   562         }
       
   563 
       
   564     if ( err )
       
   565         {
       
   566         HTI_LOG_FORMAT( "UnloadModule: Error %d", err );
       
   567         SendErrorResponseMsg( err, KErrorUnloadModule );
       
   568         }
       
   569 
       
   570     else
       
   571         {
       
   572         // Remove the test case infos of the unloaded module.
       
   573         TInt moduleIndex = IndexByModuleName( moduleName );
       
   574         if ( moduleIndex != KErrNotFound )
       
   575             {
       
   576             CHtiTestModuleCaseInfos* module = iLoadedInfos[moduleIndex];
       
   577             iLoadedInfos.Remove( moduleIndex );
       
   578             delete module;
       
   579             }
       
   580 
       
   581         // Sends the name of the unloaded module as a response.
       
   582         SendResponseMsg( (*iParameters)[0] );
       
   583         }
       
   584 
       
   585     HTI_LOG_FUNC_OUT( "CHtiStifTfIf::HandleUnloadModuleCmdL" );
       
   586     }
       
   587 
       
   588 
       
   589 // -----------------------------------------------------------------------------
       
   590 // CHtiStifTfIf::HandleListCasesCmdL
       
   591 // Creates a list of titles of the test cases inside the given test module.
       
   592 // Output is a string of test case titles separated with the | character.
       
   593 // -----------------------------------------------------------------------------
       
   594 //
       
   595 void CHtiStifTfIf::HandleListCasesCmdL()
       
   596     {
       
   597     HTI_LOG_FUNC_IN( "CHtiStifTfIf::HandleListCasesCmdL" );
       
   598 
       
   599     TInt err = KErrNone;
       
   600 
       
   601     TBuf<KMaxFileName> moduleName;
       
   602     TBuf8<128> buf8;  // temporary buffer for case titles
       
   603 
       
   604     CHtiTestModuleCaseInfos* module = NULL;
       
   605     CTestInfo* caseInfo = NULL;
       
   606 
       
   607     // LIST_CASES command has 1 optional parameter (module name).
       
   608 
       
   609     // If no parameter given all cases of all currently loaded
       
   610     // modules are listed.
       
   611     if ( iParameters->Count() == 0 )
       
   612         {
       
   613         /*
       
   614         // (Re)load case infos from all loaded modules
       
   615         TRAP( err, LoadTestCaseInfosL() );
       
   616 
       
   617         if ( err )
       
   618             {
       
   619             HTI_LOG_FORMAT(
       
   620                 "ListCases - Error from LoadTestCaseInfosL %d", err );
       
   621             SendErrorResponseMsg( err, KErrorGetTestCaseInfos );
       
   622             }
       
   623         */
       
   624         CBufFlat* caseListBuf = NULL;
       
   625         TRAP( err, caseListBuf = CBufFlat::NewL( 128 ) );
       
   626         if ( err )
       
   627             {
       
   628             SendErrorResponseMsg( KErrNoMemory, KErrorNoMemory );
       
   629             return;
       
   630             }
       
   631 
       
   632         // Loop all loaded test modules
       
   633         TInt pos = 0;
       
   634         for ( TInt i = 0; i < iLoadedInfos.Count(); i++ )
       
   635             {
       
   636             module = NULL;
       
   637             module = iLoadedInfos[i];
       
   638             HTI_LOG_FORMAT( "Listing cases from module %S",
       
   639                 module->iModuleName );
       
   640 
       
   641             // Loop all case infos in this module
       
   642             for ( TInt j = 0; j < module->iTestInfos.Count(); j++ )
       
   643                 {
       
   644                 caseInfo = NULL;
       
   645                 caseInfo = module->iTestInfos[j];
       
   646 
       
   647                 if ( !( i == 0 && j == 0 ) )
       
   648                     {
       
   649                     buf8.Append( KParameterDelimiter );
       
   650                     }
       
   651 
       
   652                 buf8.Append( caseInfo->TestCaseTitle() );
       
   653 
       
   654                 TRAP( err, caseListBuf->ExpandL( pos, buf8.Length() ) );
       
   655                 if ( err )
       
   656                     {
       
   657                     SendErrorResponseMsg( KErrNoMemory, KErrorNoMemory );
       
   658                     delete caseListBuf;
       
   659                     return;
       
   660                     }
       
   661                 caseListBuf->Write( pos, buf8, buf8.Length() );
       
   662                 pos += buf8.Length();
       
   663                 buf8.Zero();
       
   664                 }
       
   665             }
       
   666 
       
   667         if ( caseListBuf->Size() > 0 )
       
   668             {
       
   669             TPtr8 ptr = caseListBuf->Ptr( 0 );
       
   670             SendResponseMsg( ptr );
       
   671             }
       
   672 
       
   673         else
       
   674             {
       
   675             SendResponseMsg( buf8 );
       
   676             }
       
   677 
       
   678         delete caseListBuf;
       
   679         }
       
   680 
       
   681     else if ( iParameters->Count() == 1 )
       
   682         {
       
   683         if ( (*iParameters)[0].Length() > moduleName.MaxLength() )
       
   684             {
       
   685             SendErrorResponseMsg( KErrArgument, KErrorParameterTooLong );
       
   686             return;
       
   687             }
       
   688 
       
   689         moduleName.Copy( (*iParameters)[0] );
       
   690         TInt moduleIndex = IndexByModuleName( moduleName );
       
   691 
       
   692         if ( moduleIndex == KErrNotFound )
       
   693             {
       
   694             HTI_LOG_TEXT( "Module is not loaded" );
       
   695             SendErrorResponseMsg( KErrNotFound, KErrorModuleNotLoaded );
       
   696             return;
       
   697             }
       
   698         /*
       
   699         // (Re)load the case infos of given module
       
   700         TRAP( err, LoadTestCaseInfosL( moduleName ) );
       
   701         if ( err )
       
   702             {
       
   703             HTI_LOG_FORMAT(
       
   704                 "ListCases - Error from LoadTestCaseInfosL %d", err );
       
   705             SendErrorResponseMsg( err, KErrorGetTestCaseInfos );
       
   706             return;
       
   707             }
       
   708         */
       
   709         module = iLoadedInfos[moduleIndex];
       
   710         CBufFlat* caseListBuf = NULL;
       
   711 
       
   712         TRAP( err, caseListBuf = CBufFlat::NewL( 128 ) );
       
   713 
       
   714         if ( err )
       
   715             {
       
   716             SendErrorResponseMsg( KErrNoMemory, KErrorNoMemory );
       
   717             return;
       
   718             }
       
   719 
       
   720         TInt pos = 0;
       
   721         for ( TInt i = 0; i < module->iTestInfos.Count(); i++ )
       
   722             {
       
   723             caseInfo = NULL;
       
   724             caseInfo = module->iTestInfos[i];
       
   725             buf8.Copy( caseInfo->TestCaseTitle() );
       
   726             if ( !( i == module->iTestInfos.Count() - 1 ) )
       
   727                 {
       
   728                 buf8.Append( KParameterDelimiter );
       
   729                 }
       
   730             TRAP( err, caseListBuf->ExpandL( pos, buf8.Length() ) );
       
   731             if ( err )
       
   732                 {
       
   733                 SendErrorResponseMsg( KErrNoMemory, KErrorNoMemory );
       
   734                 delete caseListBuf;
       
   735                 return;
       
   736                 }
       
   737             caseListBuf->Write( pos, buf8, buf8.Length() );
       
   738             pos += buf8.Length();
       
   739             buf8.Zero();
       
   740             }
       
   741 
       
   742         if ( module->iTestInfos.Count() > 0 )
       
   743             {
       
   744             TPtr8 ptr = caseListBuf->Ptr( 0 );
       
   745             SendResponseMsg( ptr );
       
   746             }
       
   747 
       
   748         else
       
   749             {
       
   750             SendResponseMsg( buf8 );
       
   751             }
       
   752 
       
   753         delete caseListBuf;
       
   754         }
       
   755 
       
   756     else
       
   757         {
       
   758         SendErrorResponseMsg( KErrArgument, KErrorTooManyParameters );
       
   759         }
       
   760 
       
   761     HTI_LOG_FUNC_OUT( "CHtiStifTfIf::HandleListCasesCmdL" );
       
   762     }
       
   763 
       
   764 
       
   765 // -----------------------------------------------------------------------------
       
   766 // CHtiStifTfIf::HandleStartCaseCmdL
       
   767 // Starts one test case based on test module name and test case index number.
       
   768 // STIF TF gives an ID to the started case and that ID is sent as a response
       
   769 // message.
       
   770 // -----------------------------------------------------------------------------
       
   771 //
       
   772 void CHtiStifTfIf::HandleStartCaseCmdL()
       
   773     {
       
   774     HTI_LOG_FUNC_IN( "CHtiStifTfIf::HandleStartCaseCmdL" );
       
   775 
       
   776     TInt err = KErrNone;
       
   777     TInt caseNumber = -1;
       
   778 
       
   779     // START_CASE command has 1 or 2 parameters.
       
   780     // If only one parameter given, it's the index of test case to start. The
       
   781     // index refers to the list of test cases of all currently loaded modules.
       
   782     // If two parameters given, then the first one is the module name and
       
   783     // second is the test case index number inside the given module,
       
   784 
       
   785     if ( iParameters->Count() == 0 )
       
   786         {
       
   787         SendErrorResponseMsg( KErrArgument, KErrorParameterMissing );
       
   788         return;
       
   789         }
       
   790 
       
   791     else if ( iParameters->Count() == 1 )
       
   792         {
       
   793         TLex8 lex( (*iParameters)[0] );
       
   794         err = lex.Val( caseNumber );
       
   795         if ( err )
       
   796             {
       
   797             SendErrorResponseMsg( err, KErrorInvalidCaseNumber );
       
   798             return;
       
   799             }
       
   800 
       
   801         CTestInfo* caseInfo = NULL;
       
   802         TRAP( err, caseInfo = GetCaseInfoL( caseNumber ) );
       
   803 
       
   804         if ( err )
       
   805             {
       
   806             SendErrorResponseMsg( err, KErrorInvalidCaseNumber );
       
   807             return;
       
   808             }
       
   809 
       
   810         TInt caseId = -1;
       
   811 
       
   812         err = StartTestCase( caseId, *caseInfo );
       
   813 
       
   814         if ( err )
       
   815             {
       
   816             SendErrorResponseMsg( err, KErrorStartTestCase );
       
   817             return;
       
   818             }
       
   819 
       
   820         TBuf8<32> msg;
       
   821         msg.AppendNum( caseId );
       
   822         SendResponseMsg( msg );
       
   823         }
       
   824 
       
   825     else if ( iParameters->Count() == 2 )
       
   826         {
       
   827         TBuf<KMaxFileName> moduleName;
       
   828         if ( (*iParameters)[0].Length() > moduleName.MaxLength() )
       
   829             {
       
   830             SendErrorResponseMsg( KErrArgument, KErrorParameterTooLong );
       
   831             return;
       
   832             }
       
   833 
       
   834         moduleName.Copy( (*iParameters)[0] );
       
   835         TLex8 lex( (*iParameters)[1] );
       
   836         err = lex.Val( caseNumber );
       
   837         if ( err )
       
   838             {
       
   839             SendErrorResponseMsg( err, KErrorInvalidCaseNumber );
       
   840             return;
       
   841             }
       
   842 
       
   843         TInt moduleIndex = IndexByModuleName( moduleName );
       
   844 
       
   845         if ( moduleIndex == KErrNotFound )
       
   846             {
       
   847             // We don't have case infos of given module loaded - try to
       
   848             // load them.
       
   849             TRAP( err, LoadTestCaseInfosL( moduleName ) );
       
   850             if ( err )
       
   851                 {
       
   852                 HTI_LOG_FORMAT(
       
   853                     "StartCase - Error from LoadTestCaseInfosL %d", err );
       
   854                 SendErrorResponseMsg( err, KErrorModuleNotLoaded );
       
   855                 return;
       
   856                 }
       
   857             // Now there should be the module added - get its index
       
   858             moduleIndex = IndexByModuleName( moduleName );
       
   859             }
       
   860 
       
   861         CHtiTestModuleCaseInfos* module = iLoadedInfos[moduleIndex];
       
   862 
       
   863         if ( caseNumber < 0 ||
       
   864              caseNumber >= module->iTestInfos.Count() ||
       
   865              module->iTestInfos.Count() == 0 )
       
   866             {
       
   867             SendErrorResponseMsg( KErrArgument, KErrorInvalidCaseNumber );
       
   868             return;
       
   869             }
       
   870 
       
   871         CTestInfo* caseInfo = module->iTestInfos[caseNumber];
       
   872         TInt caseId = -1;
       
   873 
       
   874         err = StartTestCase( caseId, *caseInfo );
       
   875 
       
   876         if ( err )
       
   877             {
       
   878             SendErrorResponseMsg( err, KErrorStartTestCase );
       
   879             return;
       
   880             }
       
   881 
       
   882         TBuf8<32> msg;
       
   883         msg.AppendNum( caseId );
       
   884         SendResponseMsg( msg );
       
   885         }
       
   886 
       
   887     else
       
   888         {
       
   889         SendErrorResponseMsg( KErrArgument, KErrorTooManyParameters );
       
   890         }
       
   891 
       
   892     HTI_LOG_FUNC_OUT( "CHtiStifTfIf::HandleStartCaseCmdL" );
       
   893     }
       
   894 
       
   895 
       
   896 // -----------------------------------------------------------------------------
       
   897 // CHtiStifTfIf::HandleCancelCaseCmdL
       
   898 // Cancels test case execution based on test case ID.
       
   899 // -----------------------------------------------------------------------------
       
   900 //
       
   901 void CHtiStifTfIf::HandleCancelCaseCmdL()
       
   902     {
       
   903     HTI_LOG_FUNC_IN( "CHtiStifTfIf::HandleCancelCaseCmdL" );
       
   904 
       
   905     TInt err = KErrNone;
       
   906 
       
   907     // CANCEL_CASE command always has 1 parameter (test case id).
       
   908     if ( iParameters->Count() == 0 )
       
   909         {
       
   910         SendErrorResponseMsg( KErrArgument, KErrorParameterMissing );
       
   911         return;
       
   912         }
       
   913 
       
   914     else if ( iParameters->Count() == 1 )
       
   915         {
       
   916         TInt caseId = -1;
       
   917         TLex8 lex( (*iParameters)[0] );
       
   918         err = lex.Val( caseId );
       
   919 
       
   920         if ( err )
       
   921             {
       
   922             SendErrorResponseMsg( err, KErrorInvalidCaseNumber );
       
   923             return;
       
   924             }
       
   925 
       
   926         err = CancelTestCase( caseId );
       
   927 
       
   928         if ( err )
       
   929             {
       
   930             SendErrorResponseMsg( err, KErrorCancelTestCase );
       
   931             }
       
   932 
       
   933         else
       
   934             {
       
   935             SendResponseMsg( _L8( "OK" ) );
       
   936             }
       
   937         }
       
   938 
       
   939     else
       
   940         {
       
   941         SendErrorResponseMsg( KErrArgument, KErrorTooManyParameters );
       
   942         }
       
   943 
       
   944     HTI_LOG_FUNC_OUT( "CHtiStifTfIf::HandleCancelCaseCmdL" );
       
   945     }
       
   946 
       
   947 
       
   948 // -----------------------------------------------------------------------------
       
   949 // CHtiStifTfIf::HandlePauseCaseCmdL
       
   950 // Pauses test case execution based on test case ID.
       
   951 // -----------------------------------------------------------------------------
       
   952 //
       
   953 void CHtiStifTfIf::HandlePauseCaseCmdL()
       
   954     {
       
   955     HTI_LOG_FUNC_IN( "CHtiStifTfIf::HandlePauseCaseCmdL" );
       
   956 
       
   957     TInt err = KErrNone;
       
   958 
       
   959     // PAUSE_CASE command always has 1 parameter (test case id).
       
   960     if ( iParameters->Count() == 0 )
       
   961         {
       
   962         SendErrorResponseMsg( KErrArgument, KErrorParameterMissing );
       
   963         return;
       
   964         }
       
   965 
       
   966     else if ( iParameters->Count() == 1 )
       
   967         {
       
   968         TInt caseId = -1;
       
   969         TLex8 lex( (*iParameters)[0] );
       
   970         err = lex.Val( caseId );
       
   971 
       
   972         if ( err )
       
   973             {
       
   974             SendErrorResponseMsg( err, KErrorInvalidCaseNumber );
       
   975             return;
       
   976             }
       
   977 
       
   978         err = PauseTestCase( caseId );
       
   979 
       
   980         if ( err )
       
   981             {
       
   982             SendErrorResponseMsg( err, KErrorPauseTestCase );
       
   983             }
       
   984 
       
   985         else
       
   986             {
       
   987             SendResponseMsg( _L8( "OK" ) );
       
   988             }
       
   989         }
       
   990 
       
   991     else
       
   992         {
       
   993         SendErrorResponseMsg( KErrArgument, KErrorTooManyParameters );
       
   994         }
       
   995 
       
   996     HTI_LOG_FUNC_OUT( "CHtiStifTfIf::HandlePauseCaseCmdL" );
       
   997     }
       
   998 
       
   999 
       
  1000 // -----------------------------------------------------------------------------
       
  1001 // CHtiStifTfIf::HandleResumeCaseCmdL
       
  1002 // Resumes execution of a paused test case based on test case ID.
       
  1003 // -----------------------------------------------------------------------------
       
  1004 //
       
  1005 void CHtiStifTfIf::HandleResumeCaseCmdL()
       
  1006     {
       
  1007     HTI_LOG_FUNC_IN( "CHtiStifTfIf::HandleResumeCaseCmdL" );
       
  1008 
       
  1009     TInt err = KErrNone;
       
  1010 
       
  1011     // RESUME_CASE command always has 1 parameter (test case id).
       
  1012     if ( iParameters->Count() == 0 )
       
  1013         {
       
  1014         SendErrorResponseMsg( KErrArgument, KErrorParameterMissing );
       
  1015         return;
       
  1016         }
       
  1017 
       
  1018     else if ( iParameters->Count() == 1 )
       
  1019         {
       
  1020         TInt caseId = -1;
       
  1021         TLex8 lex( (*iParameters)[0] );
       
  1022         err = lex.Val( caseId );
       
  1023 
       
  1024         if ( err )
       
  1025             {
       
  1026             SendErrorResponseMsg( err, KErrorInvalidCaseNumber );
       
  1027             return;
       
  1028             }
       
  1029 
       
  1030         err = ResumeTestCase( caseId );
       
  1031 
       
  1032         if ( err )
       
  1033             {
       
  1034             SendErrorResponseMsg( err, KErrorResumeTestCase );
       
  1035             }
       
  1036 
       
  1037         else
       
  1038             {
       
  1039             SendResponseMsg( _L8( "OK" ) );
       
  1040             }
       
  1041         }
       
  1042 
       
  1043     else
       
  1044         {
       
  1045         SendErrorResponseMsg( KErrArgument, KErrorTooManyParameters );
       
  1046         }
       
  1047 
       
  1048     HTI_LOG_FUNC_OUT( "CHtiStifTfIf::HandleResumeCaseCmdL" );
       
  1049     }
       
  1050 
       
  1051 
       
  1052 // -----------------------------------------------------------------------------
       
  1053 // CHtiStifTfIf::HandleAddCaseFileCmdL
       
  1054 // Adds a test case file to an earlier loaded test module.
       
  1055 // Checks that the module is loaded and that the given test case file exists.
       
  1056 // -----------------------------------------------------------------------------
       
  1057 //
       
  1058 void CHtiStifTfIf::HandleAddCaseFileCmdL()
       
  1059     {
       
  1060     HTI_LOG_FUNC_IN( "CHtiStifTfIf::HandleAddCaseFileCmdL" );
       
  1061 
       
  1062     TInt err = KErrNone;
       
  1063 
       
  1064     TBuf<KMaxFileName> moduleName;
       
  1065     TBuf<KMaxFileName> caseFilePath;
       
  1066 
       
  1067     // ADD_CASE_FILE command always has 2 mandatory parameters (module name
       
  1068     // and test case file path).
       
  1069     if ( iParameters->Count() == 0 || iParameters->Count() == 1 )
       
  1070         {
       
  1071         SendErrorResponseMsg( KErrArgument, KErrorParameterMissing );
       
  1072         return;
       
  1073         }
       
  1074 
       
  1075     else if ( iParameters->Count() == 2 )
       
  1076         {
       
  1077         if ( (*iParameters)[0].Length() > moduleName.MaxLength() ||
       
  1078              (*iParameters)[1].Length() > caseFilePath.MaxLength() )
       
  1079             {
       
  1080             SendErrorResponseMsg( KErrArgument, KErrorParameterTooLong );
       
  1081             return;
       
  1082             }
       
  1083 
       
  1084         moduleName.Copy( (*iParameters)[0] );
       
  1085         caseFilePath.Copy( (*iParameters)[1] );
       
  1086 
       
  1087         RFs fsSession;
       
  1088         err = fsSession.Connect();
       
  1089         if ( err )
       
  1090             {
       
  1091             SendErrorResponseMsg( err, KErrorFileServer );
       
  1092             return;
       
  1093             }
       
  1094 
       
  1095         if ( !( BaflUtils::FileExists( fsSession, caseFilePath ) ) )
       
  1096             {
       
  1097             SendErrorResponseMsg( KErrNotFound, KErrorCaseFileNotFound );
       
  1098             fsSession.Close();
       
  1099             return;
       
  1100             }
       
  1101 
       
  1102         fsSession.Close();
       
  1103 
       
  1104         err = AddTestCaseFile( moduleName, caseFilePath );
       
  1105 
       
  1106         if ( err )
       
  1107             {
       
  1108             SendErrorResponseMsg( err, KErrorAddCaseFile );
       
  1109             return;
       
  1110             }
       
  1111 
       
  1112         // Test case file has been added - update test case infos
       
  1113         TRAP( err, LoadTestCaseInfosL( moduleName ) );
       
  1114 
       
  1115         if ( err )
       
  1116             {
       
  1117             HTI_LOG_FORMAT(
       
  1118                 "AddCaseFile - Error from LoadTestCaseInfosL %d", err );
       
  1119             SendErrorResponseMsg( err, KErrorGetTestCases );
       
  1120             return;
       
  1121             }
       
  1122 
       
  1123         SendResponseMsg( _L8( "OK" ) );
       
  1124         }
       
  1125 
       
  1126     else
       
  1127         {
       
  1128         SendErrorResponseMsg( KErrArgument, KErrorTooManyParameters );
       
  1129         }
       
  1130 
       
  1131     HTI_LOG_FUNC_OUT( "CHtiStifTfIf::HandleAddCaseFileCmdL" );
       
  1132     }
       
  1133 
       
  1134 
       
  1135 // -----------------------------------------------------------------------------
       
  1136 // CHtiStifTfIf::HandleRemoveCaseFileCmdL
       
  1137 // Removes a test case file from test module.
       
  1138 // Checks that the module is loaded and that the given test case file exists.
       
  1139 // -----------------------------------------------------------------------------
       
  1140 //
       
  1141 void CHtiStifTfIf::HandleRemoveCaseFileCmdL()
       
  1142     {
       
  1143     HTI_LOG_FUNC_IN( "CHtiStifTfIf::HandleRemoveCaseFileCmdL" );
       
  1144 
       
  1145     TInt err = KErrNone;
       
  1146 
       
  1147     TBuf<KMaxFileName> moduleName;
       
  1148     TBuf<KMaxFileName> caseFilePath;
       
  1149 
       
  1150     // REMOVE_CASE_FILE command always has 2 mandatory parameters (module name
       
  1151     // and test case file path).
       
  1152     if ( iParameters->Count() == 0 || iParameters->Count() == 1 )
       
  1153         {
       
  1154         SendErrorResponseMsg( KErrArgument, KErrorParameterMissing );
       
  1155         return;
       
  1156         }
       
  1157 
       
  1158     else if ( iParameters->Count() == 2 )
       
  1159         {
       
  1160         if ( (*iParameters)[0].Length() > moduleName.MaxLength() ||
       
  1161              (*iParameters)[1].Length() > caseFilePath.MaxLength() )
       
  1162             {
       
  1163             SendErrorResponseMsg( KErrArgument, KErrorParameterTooLong );
       
  1164             return;
       
  1165             }
       
  1166 
       
  1167         moduleName.Copy( (*iParameters)[0] );
       
  1168         caseFilePath.Copy( (*iParameters)[1] );
       
  1169 
       
  1170         RFs fsSession;
       
  1171         err = fsSession.Connect();
       
  1172         if ( err )
       
  1173             {
       
  1174             SendErrorResponseMsg( err, KErrorFileServer );
       
  1175             return;
       
  1176             }
       
  1177 
       
  1178         if ( !( BaflUtils::FileExists( fsSession, caseFilePath ) ) )
       
  1179             {
       
  1180             SendErrorResponseMsg( KErrNotFound, KErrorCaseFileNotFound );
       
  1181             fsSession.Close();
       
  1182             return;
       
  1183             }
       
  1184 
       
  1185         fsSession.Close();
       
  1186 
       
  1187         err = RemoveTestCaseFile( moduleName, caseFilePath );
       
  1188 
       
  1189         if ( err )
       
  1190             {
       
  1191             SendErrorResponseMsg( err, KErrorRemoveCaseFile );
       
  1192             return;
       
  1193             }
       
  1194 
       
  1195         // Test case file has been removed - update test case infos
       
  1196         TRAP( err, LoadTestCaseInfosL( moduleName ) );
       
  1197 
       
  1198         if ( err )
       
  1199             {
       
  1200             HTI_LOG_FORMAT(
       
  1201                 "RemoveCaseFile - Error from LoadTestCaseInfosL %d", err );
       
  1202             SendErrorResponseMsg( err, KErrorGetTestCases );
       
  1203             return;
       
  1204             }
       
  1205 
       
  1206         SendResponseMsg( _L8( "OK" ) );
       
  1207         }
       
  1208 
       
  1209     else
       
  1210         {
       
  1211         SendErrorResponseMsg( KErrArgument, KErrorTooManyParameters );
       
  1212         }
       
  1213 
       
  1214     HTI_LOG_FUNC_OUT( "CHtiStifTfIf::HandleRemoveCaseFileCmdL" );
       
  1215     }
       
  1216 
       
  1217 
       
  1218 // -----------------------------------------------------------------------------
       
  1219 // CHtiStifTfIf::HandleCaseMsgCmdL
       
  1220 // Sends a message to STIF Test Framework.
       
  1221 // -----------------------------------------------------------------------------
       
  1222 //
       
  1223 void CHtiStifTfIf::HandleCaseMsgCmdL()
       
  1224     {
       
  1225     HTI_LOG_FUNC_IN( "CHtiStifTfIf::HandleCaseMsgCmdL" );
       
  1226 
       
  1227     TInt err = KErrNone;
       
  1228 
       
  1229     // CASE_MESSAGE command always has 1 parameter (the message).
       
  1230     if ( iParameters->Count() == 0 )
       
  1231         {
       
  1232         SendErrorResponseMsg( KErrArgument, KErrorParameterMissing );
       
  1233         return;
       
  1234         }
       
  1235 
       
  1236     else if ( iParameters->Count() == 1 )
       
  1237         {
       
  1238         HBufC* msg = HBufC::New( (*iParameters)[0].Length() );
       
  1239         if ( msg == NULL )
       
  1240             {
       
  1241             SendErrorResponseMsg( KErrNoMemory, KErrorNoMemory );
       
  1242             return;
       
  1243             }
       
  1244         msg->Des().Copy( (*iParameters)[0] );
       
  1245         HTI_LOG_TEXT( "Sending case message:" );
       
  1246         HTI_LOG_DES( *msg );
       
  1247         err = AtsReceive( *msg );
       
  1248         delete msg;
       
  1249 
       
  1250         if ( err )
       
  1251             {
       
  1252             SendErrorResponseMsg( err, KErrorCaseMessage );
       
  1253             }
       
  1254 
       
  1255         else
       
  1256             {
       
  1257             SendResponseMsg( _L8( "OK" ) );
       
  1258             }
       
  1259         }
       
  1260 
       
  1261     else
       
  1262         {
       
  1263         SendErrorResponseMsg( KErrArgument, KErrorTooManyParameters );
       
  1264         }
       
  1265 
       
  1266     HTI_LOG_FUNC_OUT( "CHtiStifTfIf::HandleCaseMsgCmdL" );
       
  1267     }
       
  1268 
       
  1269 
       
  1270 // -----------------------------------------------------------------------------
       
  1271 // CHtiStifTfIf::HandleSetDevIdCmdL
       
  1272 // Sets device ID. If successfull sends the set ID back as response message.
       
  1273 // -----------------------------------------------------------------------------
       
  1274 //
       
  1275 void CHtiStifTfIf::HandleSetDevIdCmdL()
       
  1276     {
       
  1277     HTI_LOG_FUNC_IN( "CHtiStifTfIf::HandleSetDevIdCmdL" );
       
  1278 
       
  1279     TInt err = KErrNone;
       
  1280 
       
  1281     // SET_DEVICEID command always has 1 parameter (device id).
       
  1282     if ( iParameters->Count() == 0 )
       
  1283         {
       
  1284         SendErrorResponseMsg( KErrArgument, KErrorParameterMissing );
       
  1285         return;
       
  1286         }
       
  1287 
       
  1288     else if ( iParameters->Count() == 1 )
       
  1289         {
       
  1290         TUint16 deviceId = KDefaultDevId;
       
  1291         TLex8 lex( (*iParameters)[0] );
       
  1292         err = lex.Val( deviceId, EDecimal );
       
  1293 
       
  1294         if ( err )
       
  1295             {
       
  1296             SendErrorResponseMsg( err, KErrorInvalidDeviceId );
       
  1297             return;
       
  1298             }
       
  1299 
       
  1300         iDevId = deviceId;
       
  1301 
       
  1302         // On success sends the set device id as response.
       
  1303         TBuf8<32> msg;
       
  1304         msg.AppendNum( (TInt)iDevId );
       
  1305         SendResponseMsg( msg );
       
  1306         }
       
  1307 
       
  1308     else
       
  1309         {
       
  1310         SendErrorResponseMsg( KErrArgument, KErrorTooManyParameters );
       
  1311         }
       
  1312 
       
  1313     HTI_LOG_FUNC_OUT( "CHtiStifTfIf::HandleSetDevIdCmdL" );
       
  1314     }
       
  1315 
       
  1316 
       
  1317 // -----------------------------------------------------------------------------
       
  1318 // CHtiStifTfIf::HandleSetAttributeCmdL
       
  1319 // Sets attributes for test framework.
       
  1320 // -----------------------------------------------------------------------------
       
  1321 //
       
  1322 void CHtiStifTfIf::HandleSetAttributeCmdL()
       
  1323     {
       
  1324     HTI_LOG_FUNC_IN( "CHtiStifTfIf::HandleSetAttributeCmdL" );
       
  1325 
       
  1326     TInt err = KErrNone;
       
  1327 
       
  1328     // SET_ATTRIBUTE command always has 2 paramters (attribute id and value)
       
  1329     if ( iParameters->Count() < 2 )
       
  1330         {
       
  1331         SendErrorResponseMsg( KErrArgument, KErrorParameterMissing );
       
  1332         }
       
  1333 
       
  1334     else if ( iParameters->Count() == 2 )
       
  1335         {
       
  1336         TInt attributeId = -1;
       
  1337         TLex8 lex( (*iParameters)[0] );
       
  1338         err = lex.Val( attributeId );
       
  1339 
       
  1340         if ( err )
       
  1341             {
       
  1342             SendErrorResponseMsg( err, KErrorInvalidAttributeId );
       
  1343             return;
       
  1344             }
       
  1345 
       
  1346         TBuf<256> value;
       
  1347         if ( (*iParameters)[1].Length() > value.MaxLength() )
       
  1348             {
       
  1349             SendErrorResponseMsg( KErrArgument, KErrorParameterTooLong );
       
  1350             return;
       
  1351             }
       
  1352         value.Copy( (*iParameters)[1] );
       
  1353 
       
  1354         err = SetAttribute( (TAttribute)attributeId, value );
       
  1355 
       
  1356         if ( err )
       
  1357             {
       
  1358             SendErrorResponseMsg( err, KErrorSetAttribute );
       
  1359             }
       
  1360 
       
  1361         else
       
  1362             {
       
  1363             SendResponseMsg( _L8( "OK" ) );
       
  1364             }
       
  1365         }
       
  1366 
       
  1367     else
       
  1368         {
       
  1369         SendErrorResponseMsg( KErrArgument, KErrorTooManyParameters );
       
  1370         }
       
  1371 
       
  1372     HTI_LOG_FUNC_OUT( "CHtiStifTfIf::HandleSetAttributeCmdL" );
       
  1373     }
       
  1374 
       
  1375 // -----------------------------------------------------------------------------
       
  1376 // CHtiStifTfIf::TestCompleted
       
  1377 // This method is called by the STIF TF when a test case execution completes.
       
  1378 // The test case result is send out as a formatted string.
       
  1379 // -----------------------------------------------------------------------------
       
  1380 //
       
  1381 void CHtiStifTfIf::TestCompleted( TInt aTestId,
       
  1382                                   TInt aTestExecutionResult,
       
  1383                                   TTestResult& aTestResult )
       
  1384     {
       
  1385     // Note that the STIF TF is not aware whether the service is busy processing,
       
  1386     // sending, or waiting to send a previous message. So if this method
       
  1387     // is called by the STIF TF while the service is busy, the currently
       
  1388     // pending message will be overridden by the message sent from this method.
       
  1389 
       
  1390     HTI_LOG_FUNC_IN( "CHtiStifTfIf::TestCompleted" );
       
  1391 
       
  1392     iIsBusy = ETrue;  // Will be set to EFalse when response have been sent
       
  1393 
       
  1394     // The maximum length of TResultDes is 128, so a buffer of 256
       
  1395     // should be enough for the whole "Test Completed" message.
       
  1396     TBuf8<256> buf8;
       
  1397 
       
  1398     // Add first the test id
       
  1399     buf8.AppendFormat( _L8( "%d" ), aTestId );
       
  1400     buf8.Append( KParameterDelimiter );
       
  1401 
       
  1402     // Add the test result - if it's 0 case has passed
       
  1403     buf8.AppendFormat( _L8( "%d" ), aTestResult.iResult );
       
  1404     buf8.Append( KParameterDelimiter );
       
  1405 
       
  1406     // Add the test execution result
       
  1407     buf8.AppendFormat( _L8( "%d" ), aTestExecutionResult );
       
  1408     buf8.Append( KParameterDelimiter );
       
  1409 
       
  1410     // Finally add the description (if there's any)
       
  1411     if ( aTestResult.iResultDes.Length() > 0 )
       
  1412         {
       
  1413         buf8.Append( aTestResult.iResultDes );
       
  1414         }
       
  1415 
       
  1416     // and send it
       
  1417     SendResponseMsg( buf8, ECmdTestCompleted );
       
  1418     HTI_LOG_FUNC_OUT( "CHtiStifTfIf::TestCompleted" );
       
  1419     }
       
  1420 
       
  1421 
       
  1422 // -----------------------------------------------------------------------------
       
  1423 // CHtiStifTfIf::AtsSend
       
  1424 // This method is called by the STIF TF when it needs to send a message to ATS
       
  1425 // (or some other PC-side component listening for messages)
       
  1426 // The message is converted to 8-bit representation before sending.
       
  1427 // -----------------------------------------------------------------------------
       
  1428 //
       
  1429 void CHtiStifTfIf::AtsSend( const TDesC& aMessage )
       
  1430     {
       
  1431     // Note that the STIF TF is not aware whether the service is busy processing,
       
  1432     // sending, or waiting to send a previous message. So if this method
       
  1433     // is called by the STIF TF while the service is busy, the currently
       
  1434     // pending message will be overridden by the message sent from this method.
       
  1435 
       
  1436     HTI_LOG_FUNC_IN( "CHtiStifTfIf::AtsSend" );
       
  1437     HTI_LOG_TEXT( "Message: " );
       
  1438     HTI_LOG_DES( aMessage );
       
  1439 
       
  1440     iIsBusy = ETrue;  // Will be set to EFalse when response have been sent
       
  1441 
       
  1442     TInt err = KErrNone;
       
  1443 
       
  1444     CCnvCharacterSetConverter* converter = NULL;
       
  1445     TRAP( err, converter = CCnvCharacterSetConverter::NewL() );
       
  1446 
       
  1447     if ( err )
       
  1448         {
       
  1449         SendErrorResponseMsg( err, KErrorCharConv );
       
  1450         return;
       
  1451         }
       
  1452 
       
  1453     RFs fsSession;
       
  1454     err = fsSession.Connect();
       
  1455     if ( err )
       
  1456         {
       
  1457         SendErrorResponseMsg( err, KErrorFileServer );
       
  1458         delete converter;
       
  1459         return;
       
  1460         }
       
  1461 
       
  1462     CCnvCharacterSetConverter::TAvailability availability =
       
  1463         CCnvCharacterSetConverter::ENotAvailable;
       
  1464 
       
  1465     TRAP( err, availability = converter->PrepareToConvertToOrFromL(
       
  1466         KCharacterSetIdentifierAscii, fsSession ) );
       
  1467 
       
  1468     if ( err || availability == CCnvCharacterSetConverter::ENotAvailable )
       
  1469         {
       
  1470         SendErrorResponseMsg( err, KErrorCharConv );
       
  1471         fsSession.Close();
       
  1472         delete converter;
       
  1473         return;
       
  1474         }
       
  1475 
       
  1476     fsSession.Close();
       
  1477 
       
  1478     HBufC8* asciiBuffer = NULL;
       
  1479     TRAP( err, asciiBuffer = HBufC8::NewL( aMessage.Length() ) );
       
  1480 
       
  1481     if ( err )
       
  1482         {
       
  1483         SendErrorResponseMsg( err, KErrorNoMemory );
       
  1484         delete converter;
       
  1485         return;
       
  1486         }
       
  1487 
       
  1488     TPtr8 ptr8 = asciiBuffer->Des();
       
  1489     ptr8.Zero();
       
  1490 
       
  1491     err = converter->ConvertFromUnicode( ptr8, aMessage );
       
  1492 
       
  1493     if ( err )
       
  1494         {
       
  1495         SendErrorResponseMsg( err, KErrorCharConv );
       
  1496         }
       
  1497 
       
  1498     else
       
  1499         {
       
  1500         TPtr8 converted = asciiBuffer->Des();
       
  1501         HTI_LOG_TEXT( "Converted message: " );
       
  1502         HTI_LOG_DES( converted );
       
  1503         SendResponseMsg( converted, ECmdAtsMessage );
       
  1504         }
       
  1505 
       
  1506     delete converter;
       
  1507     delete asciiBuffer;
       
  1508     HTI_LOG_FUNC_OUT( "CHtiStifTfIf::AtsSend" );
       
  1509     }
       
  1510 
       
  1511 
       
  1512 // -----------------------------------------------------------------------------
       
  1513 // CHtiStifTfIf::Print
       
  1514 // This method is called by the STIF TF when a test case prints.
       
  1515 // -----------------------------------------------------------------------------
       
  1516 //
       
  1517 void CHtiStifTfIf::Print( TInt /*aTestId*/,
       
  1518                           TTestProgress& aProgress )
       
  1519     {
       
  1520     if ( iConsole )
       
  1521         {
       
  1522         iConsole->Printf( _L("%S: %S\n"),
       
  1523                          &aProgress.iDescription,
       
  1524                          &aProgress.iText );
       
  1525         }
       
  1526     }
       
  1527 
       
  1528 
       
  1529 // -----------------------------------------------------------------------------
       
  1530 // CHtiStifTfIf::LoadTestCaseInfosL
       
  1531 // A helper method that loads the test case info objects from all currently
       
  1532 // loaded test modules. Creates and inserts new CHtiTestModuleCaseInfos objects
       
  1533 // to iLoadedInfos pointer array. Leaves on failure.
       
  1534 // -----------------------------------------------------------------------------
       
  1535 //
       
  1536 void CHtiStifTfIf::LoadTestCaseInfosL()
       
  1537     {
       
  1538     HTI_LOG_FUNC_IN( "CHtiStifTfIf::LoadTestCaseInfosL()" );
       
  1539 
       
  1540     RPointerArray<CTestInfo> allTestInfos;
       
  1541     CleanupClosePushL( allTestInfos );
       
  1542 
       
  1543     HTI_LOG_TEXT( "Getting test cases" );
       
  1544     // Ignoring possible errors
       
  1545     GetTestCases( allTestInfos );
       
  1546     HTI_LOG_FORMAT( "Got %d cases", allTestInfos.Count() );
       
  1547 
       
  1548     // Clear all old test case infos from iLoadedInfos
       
  1549     iLoadedInfos.ResetAndDestroy();
       
  1550 
       
  1551     // Loop through all test case infos and add them to the correct modules
       
  1552     // to iLoadedInfos
       
  1553     for ( TInt i = 0; i < allTestInfos.Count(); i++ )
       
  1554         {
       
  1555         CHtiTestModuleCaseInfos* currentModule = NULL;
       
  1556         CTestInfo* caseInfo = NULL;
       
  1557 
       
  1558         caseInfo = allTestInfos[i];
       
  1559 
       
  1560         // Get or create the CHtiTestModuleCaseInfos where this case belongs.
       
  1561         TInt moduleIndex = IndexByModuleName( caseInfo->ModuleName() );
       
  1562 
       
  1563         if ( moduleIndex == KErrNotFound )
       
  1564             {
       
  1565             HTI_LOG_TEXT( "Module not found - adding it" );
       
  1566             currentModule = CHtiTestModuleCaseInfos::NewLC(
       
  1567                 caseInfo->ModuleName() );
       
  1568             User::LeaveIfError( iLoadedInfos.Append( currentModule ) );
       
  1569             CleanupStack::Pop(); // currentModule
       
  1570             }
       
  1571 
       
  1572         else
       
  1573             {
       
  1574             HTI_LOG_TEXT( "Module found - getting it" );
       
  1575             currentModule = iLoadedInfos[moduleIndex];
       
  1576             }
       
  1577 
       
  1578         // Add the case info to the CHtiTestModuleCaseInfos object
       
  1579         currentModule->iTestInfos.Append( caseInfo );
       
  1580         }
       
  1581 
       
  1582     HTI_LOG_FORMAT( "Case infos of %d modules added", iLoadedInfos.Count() );
       
  1583     allTestInfos.Reset();
       
  1584     CleanupStack::PopAndDestroy(); // allTestInfos
       
  1585     HTI_LOG_FUNC_OUT( "CHtiStifTfIf::LoadTestCaseInfosL()" );
       
  1586     }
       
  1587 
       
  1588 
       
  1589 // -----------------------------------------------------------------------------
       
  1590 // CHtiStifTfIf::LoadTestCaseInfosL
       
  1591 // A helper method that loads the test case info objects from the given test
       
  1592 // module. Creates and inserts a new CHtiTestModuleCaseInfos object to
       
  1593 // iLoadedInfos pointer array. Leaves on failure.
       
  1594 // -----------------------------------------------------------------------------
       
  1595 //
       
  1596 void CHtiStifTfIf::LoadTestCaseInfosL( TDesC& aModuleName )
       
  1597     {
       
  1598     HTI_LOG_FUNC_IN( "CHtiStifTfIf::LoadTestCaseInfosL( moduleName )" );
       
  1599 
       
  1600     // Check if test case infos of this module already loaded
       
  1601     TInt index = IndexByModuleName( aModuleName );
       
  1602     if ( index != KErrNotFound )
       
  1603         {
       
  1604         // If infos already loaded, delete and load again
       
  1605         HTI_LOG_TEXT( "Already loaded - updating" );
       
  1606         CHtiTestModuleCaseInfos* module = iLoadedInfos[index];
       
  1607         iLoadedInfos.Remove( index );
       
  1608         delete module;
       
  1609         }
       
  1610 
       
  1611     CHtiTestModuleCaseInfos* newModule =
       
  1612             CHtiTestModuleCaseInfos::NewLC( aModuleName );
       
  1613 
       
  1614     HTI_LOG_TEXT( "Getting test cases" );
       
  1615 
       
  1616     // If the module is not loaded STIF TF seems to behave so that
       
  1617     // GetTestCases method does not return error but it returns
       
  1618     // zero test cases.
       
  1619     // At least in some cases if the module is loaded but it does not have
       
  1620     // any test cases, GetTestCases returns KErrNotFound.
       
  1621     // So possible errors or zero test case situations from GetTestCases
       
  1622     // are ignored here - if there's an error then the module will just
       
  1623     // have 0 test case infos.
       
  1624     GetTestCases( newModule->iTestInfos, aModuleName );
       
  1625 
       
  1626     HTI_LOG_FORMAT( "Got %d cases", newModule->iTestInfos.Count() );
       
  1627 
       
  1628     User::LeaveIfError( iLoadedInfos.Append( newModule ) );
       
  1629     CleanupStack::Pop(); // newModule
       
  1630 
       
  1631     HTI_LOG_FUNC_OUT( "CHtiStifTfIf::LoadTestCaseInfosL( moduleName )" );
       
  1632     }
       
  1633 
       
  1634 
       
  1635 // -----------------------------------------------------------------------------
       
  1636 // CHtiStifTfIf::ParseParametersL
       
  1637 // A helper method for parsing the parameters from the given command message.
       
  1638 // Parameters must be separated with a character defined by the
       
  1639 // KParameterDelimiter. Parsed parameters are stored to iParameters array.
       
  1640 // -----------------------------------------------------------------------------
       
  1641 //
       
  1642 void CHtiStifTfIf::ParseParametersL( const TDesC8& aCommand )
       
  1643     {
       
  1644     HTI_LOG_FUNC_IN( "CHtiStifTfIf::ParseParametersL" );
       
  1645 
       
  1646     // Clear the parameter array
       
  1647     iParameters->Reset();
       
  1648 
       
  1649     // Then loop all the parameters.
       
  1650     TInt sectionStart = 0;
       
  1651     TInt sectionEnd = 0;
       
  1652     while ( true )
       
  1653         {
       
  1654         sectionStart = sectionEnd + 1;
       
  1655         if ( sectionStart >= aCommand.Length() )
       
  1656             {
       
  1657             HTI_LOG_FORMAT( "Parameters %d", iParameters->Count() );
       
  1658             HTI_LOG_FUNC_OUT( "CHtiStifTfIf::ParseParametersL" );
       
  1659             return;
       
  1660             }
       
  1661 
       
  1662         sectionEnd = aCommand.Mid( sectionStart ).Find( KParameterDelimiter )
       
  1663             + sectionStart;
       
  1664 
       
  1665         if ( sectionEnd >= sectionStart )
       
  1666             {
       
  1667             iParameters->AppendL(
       
  1668                 aCommand.Mid( sectionStart, sectionEnd - sectionStart ) );
       
  1669             HTI_LOG_DES( aCommand.Mid( sectionStart, sectionEnd - sectionStart ) );
       
  1670             }
       
  1671         else
       
  1672             {
       
  1673             iParameters->AppendL( aCommand.Mid( sectionStart ) );
       
  1674             HTI_LOG_DES( aCommand.Mid( sectionStart ) );
       
  1675             HTI_LOG_FORMAT( "Parameters %d", iParameters->Count() );
       
  1676             HTI_LOG_FUNC_OUT( "CHtiStifTfIf::ParseParametersL" );
       
  1677             return;  // End of parameters.
       
  1678             }
       
  1679         }
       
  1680     }
       
  1681 
       
  1682 
       
  1683 // -----------------------------------------------------------------------------
       
  1684 // CHtiStifTfIf::SetDispatcher
       
  1685 // -----------------------------------------------------------------------------
       
  1686 //
       
  1687 void CHtiStifTfIf::SetDispatcher( MHtiDispatcher* aDispatcher )
       
  1688     {
       
  1689     iDispatcher = aDispatcher;
       
  1690     iConsole = iDispatcher->GetConsole();
       
  1691     }
       
  1692 
       
  1693 
       
  1694 // -----------------------------------------------------------------------------
       
  1695 // CHtiStifTfIf::NotifyMemoryChange
       
  1696 // Called when HTI Framework has dispatched a message forward and the amount
       
  1697 // of free memory in the message queue has changed.
       
  1698 // -----------------------------------------------------------------------------
       
  1699 //
       
  1700 void CHtiStifTfIf::NotifyMemoryChange( TInt aAvailableMemory )
       
  1701     {
       
  1702     if ( iIsBusy && iMessage )
       
  1703         {
       
  1704         if ( aAvailableMemory >= iMessage->Size() )
       
  1705             {
       
  1706 
       
  1707             if ( iErrorCode == 0 )
       
  1708                 {
       
  1709                 TInt err = iDispatcher->DispatchOutgoingMessage(
       
  1710                     iMessage, KStifTfServiceUid );
       
  1711 
       
  1712                 if ( err == KErrNone )
       
  1713                     {
       
  1714                     // Ownership of iMessage has been transferred
       
  1715                     iMessage = NULL;
       
  1716                     iIsBusy = EFalse;
       
  1717                     iDispatcher->RemoveMemoryObserver(
       
  1718                             ( MHtiMemoryObserver* ) iPlugin );
       
  1719                     }
       
  1720 
       
  1721                 else if ( err == KErrNoMemory )
       
  1722                     {
       
  1723                     // Just keep retrying.
       
  1724                     }
       
  1725 
       
  1726                 else // Give up on sending
       
  1727                     {
       
  1728                     delete iMessage;
       
  1729                     iMessage = NULL;
       
  1730                     iIsBusy = EFalse;
       
  1731                     iDispatcher->RemoveMemoryObserver(
       
  1732                             ( MHtiMemoryObserver* ) iPlugin );
       
  1733                     }
       
  1734 
       
  1735                 }
       
  1736 
       
  1737             else
       
  1738                 {
       
  1739                 TInt err = iDispatcher->DispatchOutgoingErrorMessage(
       
  1740                     iErrorCode, *iMessage, KStifTfServiceUid );
       
  1741 
       
  1742                 // If it was success or some other error than KErrNoMemory
       
  1743                 // we are done sending or trying to send this message.
       
  1744                 if ( err != KErrNoMemory )
       
  1745                     {
       
  1746                     delete iMessage;
       
  1747                     iMessage = NULL;
       
  1748                     iIsBusy = EFalse;
       
  1749                     iDispatcher->RemoveMemoryObserver(
       
  1750                             ( MHtiMemoryObserver* ) iPlugin );
       
  1751                     }
       
  1752 
       
  1753                 else
       
  1754                     {
       
  1755                     // Just keep retrying.
       
  1756                     }
       
  1757                 }
       
  1758             }
       
  1759         }
       
  1760     }
       
  1761 
       
  1762 
       
  1763 // -----------------------------------------------------------------------------
       
  1764 // CHtiStifTfIf::SendResponseMsg
       
  1765 // Sends a message out to the message dispatcher.
       
  1766 // -----------------------------------------------------------------------------
       
  1767 //
       
  1768 TInt CHtiStifTfIf::SendResponseMsg( const TDesC8& aMsg,
       
  1769                         const TUint8 aCommandId )
       
  1770     {
       
  1771     HTI_LOG_FUNC_IN( "CHtiStifTfIf::SendResponseMsg" );
       
  1772 
       
  1773     iErrorCode = 0;
       
  1774 
       
  1775     if ( iDispatcher == NULL )
       
  1776         {
       
  1777         iIsBusy = EFalse;
       
  1778         return KErrGeneral;
       
  1779         }
       
  1780 
       
  1781     iDispatcher->RemoveMemoryObserver( ( MHtiMemoryObserver* ) iPlugin );
       
  1782 
       
  1783     delete iMessage;
       
  1784     iMessage = NULL;
       
  1785     iMessage = HBufC8::New( aMsg.Length() + 1 );
       
  1786 
       
  1787 
       
  1788     if ( iMessage == NULL )
       
  1789         {
       
  1790         iIsBusy = EFalse;
       
  1791         return KErrNoMemory;
       
  1792         }
       
  1793 
       
  1794     TPtr8 ptr8 = iMessage->Des();
       
  1795     if ( aCommandId != 0 )
       
  1796         {
       
  1797         ptr8.Append( aCommandId );
       
  1798         }
       
  1799     else
       
  1800         {
       
  1801         ptr8.Append( iCommandId );
       
  1802         }
       
  1803 
       
  1804     ptr8.Append( aMsg );
       
  1805 
       
  1806     TInt err = KErrNone;
       
  1807 
       
  1808     err = iDispatcher->DispatchOutgoingMessage( iMessage, KStifTfServiceUid );
       
  1809 
       
  1810     if ( err == KErrNoMemory )
       
  1811         {
       
  1812         HTI_LOG_TEXT( "Message queue memory full - waiting" );
       
  1813         iIsBusy = ETrue; // Should already be true, but just in case
       
  1814         iDispatcher->AddMemoryObserver( ( MHtiMemoryObserver* ) iPlugin );
       
  1815         // For the caller of this method all is OK, sending is just delayed
       
  1816         err = KErrNone;
       
  1817         }
       
  1818 
       
  1819     else if ( err == KErrNone )
       
  1820         {
       
  1821         HTI_LOG_TEXT( "Message sent to dispatcher" );
       
  1822         iMessage = NULL; // Ownership of iMessage has been transferred
       
  1823         iIsBusy = EFalse;
       
  1824         }
       
  1825 
       
  1826     else // give up on sending
       
  1827         {
       
  1828         HTI_LOG_FORMAT( "Other dispatcher error %d", err );
       
  1829         delete iMessage;
       
  1830         iMessage = NULL;
       
  1831         iIsBusy = EFalse;
       
  1832         }
       
  1833 
       
  1834     HTI_LOG_FUNC_OUT( "CHtiStifTfIf::SendResponseMsg" );
       
  1835     return err;
       
  1836     }
       
  1837 
       
  1838 
       
  1839 // -----------------------------------------------------------------------------
       
  1840 // CHtiStifTfIf::SendErrorResponseMsg
       
  1841 // Sends an error message out to the message dispatcher.
       
  1842 // -----------------------------------------------------------------------------
       
  1843 //
       
  1844 TInt CHtiStifTfIf::SendErrorResponseMsg( TInt aErrorCode,
       
  1845                         const TDesC8& aErrorDescription,
       
  1846                         const TUint8 aCommandId )
       
  1847     {
       
  1848     HTI_LOG_FUNC_IN( "CHtiStifTfIf::SendErrorResponseMsg" );
       
  1849 
       
  1850     iErrorCode = aErrorCode;
       
  1851 
       
  1852     if ( iDispatcher == NULL )
       
  1853         {
       
  1854         iIsBusy = EFalse;
       
  1855         return KErrGeneral;
       
  1856         }
       
  1857 
       
  1858     iDispatcher->RemoveMemoryObserver( ( MHtiMemoryObserver* ) iPlugin );
       
  1859 
       
  1860     delete iMessage;
       
  1861     iMessage = NULL;
       
  1862     iMessage = HBufC8::New( aErrorDescription.Length() + 1 );
       
  1863 
       
  1864     if ( iMessage == NULL )
       
  1865         {
       
  1866         iIsBusy = EFalse;
       
  1867         return KErrNoMemory;
       
  1868         }
       
  1869 
       
  1870     TPtr8 ptr8 = iMessage->Des();
       
  1871     if ( aCommandId != 0 )
       
  1872         {
       
  1873         ptr8.Append( aCommandId );
       
  1874         }
       
  1875     else
       
  1876         {
       
  1877         ptr8.Append( iCommandId );
       
  1878         }
       
  1879 
       
  1880     ptr8.Append( aErrorDescription );
       
  1881 
       
  1882     TInt err = KErrNone;
       
  1883 
       
  1884     err = iDispatcher->DispatchOutgoingErrorMessage(
       
  1885         aErrorCode, *iMessage, KStifTfServiceUid );
       
  1886 
       
  1887     if ( err == KErrNoMemory )
       
  1888         {
       
  1889         HTI_LOG_TEXT( "Message queue memory full - waiting" );
       
  1890         iIsBusy = ETrue; // Should already be true, but just in case
       
  1891         iDispatcher->AddMemoryObserver( ( MHtiMemoryObserver* ) iPlugin );
       
  1892         // For the caller of this method all is OK, sending is just delayed
       
  1893         err = KErrNone;
       
  1894         }
       
  1895 
       
  1896     else if ( err == KErrNone )
       
  1897         {
       
  1898         HTI_LOG_TEXT( "Error message sent to dispatcher" );
       
  1899         delete iMessage;
       
  1900         iMessage = NULL;
       
  1901         iIsBusy = EFalse;
       
  1902         }
       
  1903 
       
  1904     else // give up on sending
       
  1905         {
       
  1906         HTI_LOG_FORMAT( "Other dispatcher error %d", err );
       
  1907         delete iMessage;
       
  1908         iMessage = NULL;
       
  1909         iIsBusy = EFalse;
       
  1910         }
       
  1911 
       
  1912     HTI_LOG_FUNC_OUT( "CHtiStifTfIf::SendErrorResponseMsg" );
       
  1913     return err;
       
  1914     }
       
  1915 
       
  1916 
       
  1917 // -----------------------------------------------------------------------------
       
  1918 // CHtiStifTfIf::IndexByModuleName
       
  1919 // A helper method to search for a CHtiTestModuleCaseInfos object from the
       
  1920 // iLoadedInfos array based on the module name.
       
  1921 // -----------------------------------------------------------------------------
       
  1922 //
       
  1923 TInt CHtiStifTfIf::IndexByModuleName( const TDesC& aModuleName )
       
  1924     {
       
  1925     HTI_LOG_FUNC_IN( "CHtiStifTfIf::IndexByModuleName" );
       
  1926 
       
  1927     CHtiTestModuleCaseInfos* module;
       
  1928     for ( TInt i = 0; i < iLoadedInfos.Count(); i++ )
       
  1929         {
       
  1930         module = NULL;
       
  1931         module = iLoadedInfos[i];
       
  1932         if ( module->iModuleName->Compare( aModuleName ) == 0 )
       
  1933             {
       
  1934             HTI_LOG_FORMAT( "Module found from index %d", i );
       
  1935             HTI_LOG_FUNC_OUT( "CHtiStifTfIf::IndexByModuleName" );
       
  1936             return i;
       
  1937             }
       
  1938         }
       
  1939     HTI_LOG_TEXT( "Module not found" );
       
  1940     HTI_LOG_FUNC_OUT( "CHtiStifTfIf::IndexByModuleName" );
       
  1941     return KErrNotFound;
       
  1942     }
       
  1943 
       
  1944 
       
  1945 // -----------------------------------------------------------------------------
       
  1946 // CHtiStifTfIf::GetCaseInfoL
       
  1947 // A helper method to get the correct CTestInfo object based on the index
       
  1948 // number, referring to list of case infos of all loaded test modules.
       
  1949 // -----------------------------------------------------------------------------
       
  1950 //
       
  1951 CTestInfo* CHtiStifTfIf::GetCaseInfoL( TInt aCaseIndex )
       
  1952     {
       
  1953     HTI_LOG_FUNC_IN( "CHtiStifTfIf::GetCaseInfoL" );
       
  1954 
       
  1955     if ( aCaseIndex < 0 )
       
  1956         {
       
  1957         User::Leave( KErrArgument );
       
  1958         }
       
  1959 
       
  1960     TInt index = -1;
       
  1961     TInt caseCount = 0;
       
  1962     CHtiTestModuleCaseInfos* module = NULL;
       
  1963     for ( TInt i = 0; i < iLoadedInfos.Count(); i++ )
       
  1964         {
       
  1965         module = NULL;
       
  1966         module = iLoadedInfos[i];
       
  1967         caseCount += module->iTestInfos.Count();
       
  1968         HTI_LOG_FORMAT( "Case count = %d", caseCount );
       
  1969         if ( aCaseIndex < caseCount )
       
  1970             {
       
  1971             // This is the right module
       
  1972             // Get the case index inside this module
       
  1973             index = module->iTestInfos.Count() - ( caseCount - aCaseIndex );
       
  1974             }
       
  1975         if ( index >= 0 ) break; // Case found - break out of loop
       
  1976         }
       
  1977 
       
  1978     if ( index < 0 )
       
  1979         {
       
  1980         HTI_LOG_TEXT( "Given index was too big - case not found" );
       
  1981         User::Leave( KErrNotFound );
       
  1982         }
       
  1983 
       
  1984     // Following should never be true - this is for testing.
       
  1985     if ( index > module->iTestInfos.Count() )
       
  1986         {
       
  1987         HTI_LOG_TEXT( "**Calculated index > test info count of module**" );
       
  1988         User::Leave( KErrGeneral );
       
  1989         }
       
  1990 
       
  1991     HTI_LOG_FORMAT( "Case found from index %d", index );
       
  1992     HTI_LOG_FUNC_OUT( "CHtiStifTfIf::GetCaseInfoL" );
       
  1993     return module->iTestInfos[index];
       
  1994     }
       
  1995 
       
  1996 
       
  1997 // ============================ MEMBER FUNCTIONS ===============================
       
  1998 
       
  1999 
       
  2000 // -----------------------------------------------------------------------------
       
  2001 // CHtiTestModuleCaseInfos::NewL
       
  2002 // -----------------------------------------------------------------------------
       
  2003 //
       
  2004 CHtiTestModuleCaseInfos* CHtiTestModuleCaseInfos::NewL(
       
  2005                                                 const TDesC& aModuleName )
       
  2006     {
       
  2007     CHtiTestModuleCaseInfos* self = NewLC( aModuleName );
       
  2008     CleanupStack::Pop( self );
       
  2009     return self;
       
  2010     }
       
  2011 
       
  2012 
       
  2013 // -----------------------------------------------------------------------------
       
  2014 // CHtiTestModuleCaseInfos::NewLC
       
  2015 // -----------------------------------------------------------------------------
       
  2016 //
       
  2017 CHtiTestModuleCaseInfos* CHtiTestModuleCaseInfos::NewLC(
       
  2018                                                 const TDesC& aModuleName )
       
  2019     {
       
  2020     CHtiTestModuleCaseInfos* self = new (ELeave) CHtiTestModuleCaseInfos;
       
  2021     CleanupStack::PushL( self );
       
  2022     self->ConstructL( aModuleName );
       
  2023     return self;
       
  2024     }
       
  2025 
       
  2026 
       
  2027 // -----------------------------------------------------------------------------
       
  2028 // CHtiTestModuleCaseInfos::ConstructL
       
  2029 // 2nd phase constructor.
       
  2030 // -----------------------------------------------------------------------------
       
  2031 //
       
  2032 void CHtiTestModuleCaseInfos::ConstructL( const TDesC& aModuleName )
       
  2033     {
       
  2034     iModuleName = aModuleName.AllocL();
       
  2035     }
       
  2036 
       
  2037 
       
  2038 // -----------------------------------------------------------------------------
       
  2039 // CHtiTestModuleCaseInfos::CHtiTestModuleCaseInfos
       
  2040 // C++ default constructor
       
  2041 // -----------------------------------------------------------------------------
       
  2042 //
       
  2043 CHtiTestModuleCaseInfos::CHtiTestModuleCaseInfos()
       
  2044     {
       
  2045     }
       
  2046 
       
  2047 
       
  2048 // -----------------------------------------------------------------------------
       
  2049 // CHtiTestModuleCaseInfos::~CHtiTestModuleCaseInfos
       
  2050 // Destructor.
       
  2051 // -----------------------------------------------------------------------------
       
  2052 //
       
  2053 CHtiTestModuleCaseInfos::~CHtiTestModuleCaseInfos()
       
  2054     {
       
  2055     delete iModuleName;
       
  2056     iTestInfos.ResetAndDestroy();
       
  2057     iTestInfos.Close();
       
  2058     }
       
  2059 
       
  2060 
       
  2061 // -----------------------------------------------------------------------------
       
  2062 // CHtiTestModuleCaseInfos::Compare
       
  2063 // Compares the order of two CHtiTestModuleCaseInfos objects using module name
       
  2064 // as the ordering key.
       
  2065 // -----------------------------------------------------------------------------
       
  2066 //
       
  2067 TInt CHtiTestModuleCaseInfos::Compare( const CHtiTestModuleCaseInfos& aFirst,
       
  2068         const CHtiTestModuleCaseInfos& aSecond )
       
  2069     {
       
  2070     return ( aFirst.iModuleName )->Compare( *aSecond.iModuleName );
       
  2071     }
       
  2072 
       
  2073 
       
  2074 // -----------------------------------------------------------------------------
       
  2075 // CHtiTestModuleCaseInfos::Match
       
  2076 // Checks the equality of two CHtiTestModuleCaseInfos objects.
       
  2077 // Objects are considered to be equal if their module name is equal.
       
  2078 // -----------------------------------------------------------------------------
       
  2079 //
       
  2080 TBool CHtiTestModuleCaseInfos::Match( const CHtiTestModuleCaseInfos& aFirst,
       
  2081         const CHtiTestModuleCaseInfos& aSecond )
       
  2082     {
       
  2083     if ( ( aFirst.iModuleName )->Compare( *aSecond.iModuleName ) == 0 )
       
  2084         {
       
  2085         return true;
       
  2086         }
       
  2087 
       
  2088     return false;
       
  2089     }
       
  2090 
       
  2091 
       
  2092 //  End of File