srsf/nssvasapi/nssvasrecognition/src/nssvascrecognitionhandler.cpp
branchRCL_3
changeset 19 e36f3802f733
parent 0 bf1d17376201
equal deleted inserted replaced
18:cad71a31b7fc 19:e36f3802f733
       
     1 /*
       
     2 * Copyright (c) 2003-2007 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:  The CNssRecognitionHandler class performs the recognition function.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <e32base.h>
       
    21 #include <e32property.h>
       
    22 #include "nssvascoreconstant.h"
       
    23 
       
    24 #include "nssvascrecognitionhandler.h"
       
    25 #include "nssvasctagmgr.h"
       
    26 #include "nssvascadaptationitem.h"
       
    27 #include <nsssispeechrecognitionutilityobserver.h>
       
    28 
       
    29 #ifndef __WINS__
       
    30 	// for hardware
       
    31     #include <AudioPreference.h>
       
    32 #else
       
    33   // use default values for WINS
       
    34   const TUint KAudioPriorityVoiceDial = 73;
       
    35 
       
    36   const TUint KAudioPrefVocosPlayback = 0x00030001;
       
    37   const TUint KAudioPrefVocosTrain = 0x00020001;
       
    38   const TUint KAudioPrefVocosRecog = 0x00010001;
       
    39 #endif
       
    40 #include "nssvascdbtagselectnotifier.h"
       
    41 #include "nssvascoreconstant.h"
       
    42 #include "nssvasmrecognizeinitcompletehandler.h"
       
    43 #include "nssvascvasdbbuilder.h"
       
    44 #include "rubydebug.h"
       
    45 
       
    46 // maximal number of results that can be handled
       
    47 const TInt KMaxResults = 100;
       
    48 
       
    49 #ifdef _DEBUG
       
    50 // Used under UDEB only
       
    51 _LIT( KRecognitionHandlerPanic, "VasCNssRecognitionHandler.cpp" );
       
    52 #endif  // _DEBUG
       
    53 
       
    54 
       
    55 // ============================ MEMBER FUNCTIONS ===============================
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // CNssRecognitionHandler::NewL
       
    59 // Two-phased constructor.
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 CNssRecognitionHandler* CNssRecognitionHandler::NewL()
       
    63     {
       
    64     RUBY_DEBUG_BLOCK( "CNssRecognitionHandler::NewL" );
       
    65 
       
    66     // version of NewLC which leaves nothing on the cleanup stack
       
    67     CNssRecognitionHandler* self=NewLC();
       
    68     CleanupStack::Pop( self );
       
    69     return self;
       
    70     }
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // CNssRecognitionHandler::NewLC
       
    74 // Two-phased constructor.
       
    75 // -----------------------------------------------------------------------------
       
    76 //
       
    77 CNssRecognitionHandler* CNssRecognitionHandler::NewLC()
       
    78     {
       
    79     CNssRecognitionHandler* self = new (ELeave)  CNssRecognitionHandler();
       
    80     CleanupStack::PushL(self);
       
    81     // push onto cleanup stack 
       
    82     // (in case self->ConstructL leaves)
       
    83     self->ConstructL();      // use two-stage construct
       
    84     return self;
       
    85     }
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 // CNssRecognitionHandler::~CNssRecognitionHandler
       
    89 // Destructor
       
    90 // -----------------------------------------------------------------------------
       
    91 //
       
    92 CNssRecognitionHandler::~CNssRecognitionHandler()
       
    93     {
       
    94     RUBY_DEBUG0( "CNssRecognitionHandler::~CNssRecognitionHandler" );
       
    95     
       
    96     Cleanup();
       
    97     
       
    98     for ( TInt k( 0 ); k < iAdaptItemArray.Count(); k++ )
       
    99         {
       
   100         iAdaptItemArray[k]->Disable();
       
   101         }
       
   102     iAdaptItemArray.Close();
       
   103     
       
   104     CNssVASDBBuilder::RemoveInstance();  // remove iVasDBBuilder instance
       
   105     
       
   106     TInt state( ERecognitionFail );
       
   107     RProperty::Get( KSINDUID, ERecognitionState, state );
       
   108     
       
   109     if ( state == ERecognitionStarted || state == ERecognitionSpeechEnd )
       
   110         {
       
   111         RProperty::Set( KSINDUID, ERecognitionState, ERecognitionFail );
       
   112         }
       
   113     // Delete P&S property
       
   114     RProperty::Delete( KSINDUID, ERecognitionState ); 
       
   115     }
       
   116 
       
   117 // -----------------------------------------------------------------------------
       
   118 // CNssRecognitionHandler::RecognizeInitL
       
   119 // Method to perform initialization for the recognize funtion.
       
   120 // -----------------------------------------------------------------------------
       
   121 //
       
   122 MNssRecognitionHandler::TNssRecognitionResult CNssRecognitionHandler::RecognizeInitL(
       
   123     MNssRecognizeInitCompleteHandler* aInitCompleteHandler,
       
   124     const CArrayPtrFlat<MNssContext>& aRecognitionVocabulary,
       
   125 	TInt aMaxResults)
       
   126     {
       
   127     RUBY_DEBUG_BLOCK( "CNssRecognitionHandler::RecognizeInitL" );
       
   128 
       
   129     MNssRecognitionHandler::TNssRecognitionResult res = EVasErrorNone;
       
   130 
       
   131     // Vocabulary independent initialization:
       
   132     // CSISpeechRecognitionUtility and CSIClientResultSet.
       
   133     res = CommonRecognizeInitL( aInitCompleteHandler, aMaxResults );
       
   134 
       
   135     if ( res != EVasErrorNone )
       
   136         {
       
   137         return( res );
       
   138         }
       
   139 
       
   140     // Agenda for vocab dependent init:
       
   141     // 1. Get the list of global contexts from VAS DB.
       
   142     //    The information we need is:
       
   143     //     * Model Bank ID, which is the same for all contexts
       
   144     //     * Grammar ID for each context
       
   145     // 2. Load a model bank from SRS DB.
       
   146     // 3. Load the grammars from SRS DB. Each context has one grammar.
       
   147 	if ( !iContextList )
       
   148 	    {
       
   149 		iContextList = new (ELeave) TMNssContextList(1);
       
   150         }
       
   151     else
       
   152         {
       
   153         iContextList->Reset();
       
   154         }
       
   155 
       
   156 
       
   157     for ( TInt k( 0 ); k < aRecognitionVocabulary.Count(); k++ )
       
   158         {
       
   159         CNssContext* context = ((CNssContext*)aRecognitionVocabulary[k])->CopyL();
       
   160         CleanupStack::PushL( context );
       
   161 
       
   162         iContextList->AppendL( context );
       
   163 
       
   164         CleanupStack::Pop( context );
       
   165         }
       
   166     iContextCount = iContextList->Count();
       
   167 
       
   168     // Check if there are tags in the contexts.
       
   169     // If there are, load a model bank. After that, further initializations
       
   170     // follow: Loading lexicons and grammars.
       
   171 	// Check for Tags in contexts and continue recognition
       
   172 	TRAPD( err, HandleTagCheckL() );
       
   173 
       
   174 	if ( err != KErrNone )
       
   175 	    {
       
   176         RUBY_DEBUG0( "CNssRecognitionHandler: TRAPD HandleTagCheckL() failed" );
       
   177 		// Notify P&S
       
   178         RProperty::Set( KSINDUID, ERecognitionState, ERecognitionFail ); 
       
   179 		Cleanup();
       
   180         return EVasRecognitionInitFailed;
       
   181 	    }
       
   182 
       
   183     return( EVasErrorNone );
       
   184     }
       
   185 
       
   186 // -----------------------------------------------------------------------------
       
   187 // CNssRecognitionHandler::RecognizeInitL
       
   188 // Method to perform initialization for the recognize funtion.
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 MNssRecognitionHandler::TNssRecognitionResult CNssRecognitionHandler::RecognizeInitL(
       
   192 		                   MNssRecognizeInitCompleteHandler* aInitCompleteHandler,
       
   193 						   TInt aMaxResults)
       
   194     {
       
   195     RUBY_DEBUG_BLOCK( "CNssRecognitionHandler::RecognizeInitL" );
       
   196 
       
   197     MNssRecognitionHandler::TNssRecognitionResult res = EVasErrorNone;
       
   198 
       
   199     // Vocabulary independent initialization:
       
   200     // CSISpeechRecognitionUtility and CSIClientResultSet.
       
   201     res = CommonRecognizeInitL( aInitCompleteHandler, aMaxResults );
       
   202 
       
   203     if ( res != EVasErrorNone )
       
   204         {
       
   205         return( res );
       
   206         }
       
   207 
       
   208     // Agenda for vocab dependent init:
       
   209     // 1. Get the list of global contexts from VAS DB.
       
   210     //    The information we need is:
       
   211     //     * Model Bank ID, which is the same for all contexts
       
   212     //     * Grammar ID for each context
       
   213     // 2. Load a model bank from SRS DB.
       
   214     // 3. Load the grammars from SRS DB. Each context has one grammar.
       
   215 
       
   216     if ( iContextList )
       
   217         {
       
   218         iContextList->Reset();
       
   219         delete iContextList;
       
   220         }
       
   221 
       
   222 	// Get Global Context List from Vas Db
       
   223     iContextList = iContextMgr->GetGlobalContexts();
       
   224     
       
   225     if ( !iContextList )
       
   226         {
       
   227         return EVasRecognitionInitFailed;
       
   228         }
       
   229 
       
   230     iContextCount = iContextList->Count();
       
   231 
       
   232 
       
   233 	// Check for Tags in contexts and continue recognition
       
   234 	TRAPD( err, HandleTagCheckL() );
       
   235 
       
   236 	if ( err != KErrNone )
       
   237 	    {
       
   238         RUBY_DEBUG0( "CNssRecognitionHandler: TRAPD HandleTagCheckL() failed" );
       
   239 
       
   240 		// Notify P&S
       
   241         RProperty::Set( KSINDUID, ERecognitionState, ERecognitionFail ); 
       
   242 		Cleanup();
       
   243         return EVasRecognitionInitFailed;
       
   244 	    }
       
   245 
       
   246 	return EVasErrorNone;
       
   247     }
       
   248 
       
   249 // -----------------------------------------------------------------------------
       
   250 // CNssRecognitionHandler::Recognize
       
   251 // Method to perform the recognize function.
       
   252 // -----------------------------------------------------------------------------
       
   253 //
       
   254 MNssRecognitionHandler::TNssRecognitionResult CNssRecognitionHandler::Recognize(
       
   255                                         MNssRecognizeEventHandler* aEventHandler )
       
   256     {
       
   257     RUBY_DEBUG0( "CNssRecognitionHandler::Recognize" );
       
   258     
       
   259     if ( iRState != EVasWaitingForRecognize )
       
   260         {
       
   261         RUBY_DEBUG0( "CNssRecognitionHandler::Recognize - Unexpected Request" );
       
   262         // Notify P&S
       
   263         RProperty::Set( KSINDUID, ERecognitionState, ERecognitionFail ); 
       
   264         Cleanup();
       
   265         return EVasUnexpectedRequest;
       
   266         }
       
   267     
       
   268     iEventHandler = aEventHandler;
       
   269 	   
       
   270     iRState = EVasWaitingForRecordStarted;
       
   271     
       
   272     RUBY_DEBUG0( "CNssRecognitionHandler: SRSF Record()" );
       
   273     
       
   274     // Asynchronous call: next expected event: ERecordStarted
       
   275     
       
   276     TInt errorCode = iSrsApi->Record( KNssVASRecordTime );
       
   277     
       
   278     if ( errorCode != KErrNone )
       
   279         {
       
   280         RUBY_DEBUG1( "CNssRecognitionHandler: Record Err = %d", errorCode );
       
   281 
       
   282         // Notify P&S
       
   283         RProperty::Set( KSINDUID, ERecognitionState, ERecognitionFail ); 
       
   284         Cleanup();
       
   285         return EVasRecognitionFailed;
       
   286         }
       
   287     
       
   288     return EVasErrorNone;
       
   289     }
       
   290 
       
   291 // -----------------------------------------------------------------------------
       
   292 // CNssRecognitionHandler::PreStartSampling
       
   293 // Method to start pre-sampling before the actual recognition start.
       
   294 // -----------------------------------------------------------------------------
       
   295 //
       
   296 MNssRecognitionHandler::TNssRecognitionResult 
       
   297 CNssRecognitionHandler::PreStartSampling( MNssRecognizeInitCompleteHandler* aInitCompleteHandler )
       
   298     {
       
   299     RUBY_DEBUG0( "" ); 
       
   300     
       
   301     // State check
       
   302     if ( iRState != EVasIdle )
       
   303         {
       
   304         RUBY_DEBUG0( "Unexpected Request" );
       
   305         return EVasUnexpectedRequest;
       
   306         }
       
   307     
       
   308     // Check callback
       
   309     if ( !aInitCompleteHandler )
       
   310         {
       
   311         return EVasInvalidParameter;
       
   312         }    
       
   313     
       
   314     iInitCompleteHandler = aInitCompleteHandler;
       
   315     
       
   316     if ( !iSrsApi )
       
   317         {		
       
   318         TRAPD( err, CreateSrfL() );
       
   319         if ( err != KErrNone )
       
   320             {
       
   321             RUBY_DEBUG0( "SRS creation fails" );
       
   322             iSrsApi = NULL;
       
   323             Cleanup();
       
   324             return EVasRecognitionInitFailed;
       
   325             }    
       
   326         }
       
   327       
       
   328     iSrsApi->SetAudioPriority( KAudioPriorityVoiceDial, 
       
   329                                KAudioPrefVocosTrain, 
       
   330                                KAudioPrefVocosPlayback, 
       
   331                                KAudioPrefVocosRecog );               
       
   332     
       
   333     TInt errorCode = iSrsApi->PreStartSampling();
       
   334     
       
   335     if ( errorCode != KErrNone )
       
   336         {
       
   337         RUBY_DEBUG1( "PreStartSampling Err = %d", errorCode );
       
   338         Cleanup();
       
   339         return EVasRecognitionInitFailed;
       
   340         }    
       
   341     
       
   342     return EVasErrorNone;
       
   343     }
       
   344 
       
   345 // -----------------------------------------------------------------------------
       
   346 // CNssRecognitionHandler::SelectTagL
       
   347 // Method to initiate the Select Tag event.
       
   348 // -----------------------------------------------------------------------------
       
   349 //
       
   350 MNssRecognitionHandler::TNssRecognitionResult CNssRecognitionHandler::SelectTagL(
       
   351 															   MNssTag *aClientTag)
       
   352     {
       
   353     return SelectTagL( aClientTag, ETrue );
       
   354     }
       
   355 
       
   356 // -----------------------------------------------------------------------------
       
   357 // CNssRecognitionHandler::SelectTagL
       
   358 // Method to initiate the Select Tag event.
       
   359 // -----------------------------------------------------------------------------
       
   360 //
       
   361 MNssRecognitionHandler::TNssRecognitionResult CNssRecognitionHandler::SelectTagL( 
       
   362                                                             MNssTag* aClientTag, 
       
   363                                                             TBool aNotifyHandlers )
       
   364     {
       
   365     RUBY_DEBUG_BLOCK( "CNssRecognitionHandler::SelectTagL" );
       
   366     
       
   367     if ( iRState != EVasWaitingForSelectTag )
       
   368         {
       
   369         RUBY_DEBUG0( "CNssRecognitionHandler::SelectTagL - Unexpected Request" );
       
   370 
       
   371         // Notify P&S
       
   372         RProperty::Set( KSINDUID, ERecognitionState, ERecognitionFail ); 
       
   373         
       
   374         Cleanup();
       
   375         return EVasUnexpectedRequest;
       
   376         }
       
   377     
       
   378     if ( !aClientTag )
       
   379         {
       
   380         RUBY_DEBUG0( "CNssRecognitionHandler: Invalid Parameter" );
       
   381 
       
   382         // Notify P&S
       
   383         RProperty::Set( KSINDUID, ERecognitionState, ERecognitionFail ); 
       
   384         
       
   385         Cleanup();
       
   386         return EVasInvalidParameter;
       
   387         }
       
   388     
       
   389     // Check if we want to notify GlobalContext handlers
       
   390     if ( !aNotifyHandlers )
       
   391         {
       
   392         // No; just cleanup
       
   393         Cleanup();
       
   394         }
       
   395     else
       
   396         { // Yes
       
   397         // make a copy of the client tag, aClientTag, so the client can delete it
       
   398         CNssTag* internalTag = ((CNssTag*)aClientTag)->CopyL();
       
   399         
       
   400         Cleanup();
       
   401         
       
   402         CNssDBTagSelectNotifier *selectNotifier = iVasDBBuilder->GetTagSelectNotifier();
       
   403         
       
   404         selectNotifier->HandleSelection(internalTag);
       
   405         
       
   406         delete internalTag;
       
   407         }
       
   408     
       
   409     return EVasErrorNone;
       
   410     }
       
   411 
       
   412 // -----------------------------------------------------------------------------
       
   413 // CNssRecognitionHandler::RejectTagL
       
   414 // Method to perform Reject Tag, i.e. blacklisting.
       
   415 // -----------------------------------------------------------------------------
       
   416 //
       
   417 MNssRecognitionHandler::TNssRecognitionResult CNssRecognitionHandler::RejectTagL(
       
   418 						   MNssRejectTagCompleteHandler* aRejectTagCompleteHandler,
       
   419 		                   CArrayPtrFlat<MNssTag>* aClientTagList)
       
   420     {
       
   421     RUBY_DEBUG_BLOCK( "CNssRecognitionHandler::RejectTagL" );
       
   422     
       
   423     if ( iRState != EVasWaitingForSelectTag )
       
   424         {
       
   425         RUBY_DEBUG0( "CNssRecognitionHandler::RejectTagL - Unexpected Request" );
       
   426 
       
   427         // Notify P&S
       
   428         RProperty::Set( KSINDUID, ERecognitionState, ERecognitionFail ); 	
       
   429         Cleanup();
       
   430         return EVasUnexpectedRequest;
       
   431         }
       
   432     
       
   433     // Begin a Loop to unload rules. A rule is contained in a tag.
       
   434     // The unload rule requests are Asynchronous. The 
       
   435     // loop is continued and completed in the event handler logic, 
       
   436     // MsruoEvent(aEvent=UnloadRuleComplete).
       
   437     // Loop through the input client tag list to build a Rule List, one rule 	
       
   438     // for each tag. Unload the rule corresponding to each tag.
       
   439     // iRejectTagCurrentLoopCount and iRejectTagCount control the loop.
       
   440     
       
   441     iRejectTagCompleteHandler = aRejectTagCompleteHandler;
       
   442     
       
   443     if (aClientTagList == 0)
       
   444         {
       
   445         // client tag list is NULL error
       
   446         RUBY_DEBUG0( "CNssRecognitionHandler: Invalid Parm: Tag List Null" );
       
   447 
       
   448         // Notify P&S
       
   449         RProperty::Set( KSINDUID, ERecognitionState, ERecognitionFail ); 
       
   450         Cleanup();
       
   451         return EVasInvalidParameter;
       
   452         }
       
   453     iRejectTagCount = aClientTagList->Count(); 
       
   454     
       
   455     if (iRejectTagCount == 0)
       
   456         {
       
   457         // tag list is empty error
       
   458         RUBY_DEBUG0( "NssRecognitionHandler: Invalid Parm: Tag List Empty" );
       
   459 
       
   460         // Notify P&S
       
   461         RProperty::Set( KSINDUID, ERecognitionState, ERecognitionFail ); 
       
   462         Cleanup();
       
   463         return EVasInvalidParameter;
       
   464         }
       
   465     
       
   466     iRejectTagCurrentLoopCount = 0; 
       
   467     
       
   468     if (!iRuleList)
       
   469         {
       
   470         iRuleList = new (ELeave) CArrayFixFlat<TRuleEntry>(iRejectTagCount);
       
   471         }
       
   472     
       
   473     TRuleEntry ruleEntry;
       
   474     
       
   475     for ( TInt index=0; index < iRejectTagCount; index++ )
       
   476         {
       
   477         CNssTag* tempTag = (CNssTag*)aClientTagList->At(index);
       
   478         CNssSpeechItem* speechItem = (CNssSpeechItem*)tempTag->SpeechItem();
       
   479         TUint32 ruleID = speechItem->RuleID();
       
   480         
       
   481         CNssContext* context = (CNssContext*)tempTag->Context();
       
   482         TUint32 grammarID = context->GrammarId();
       
   483         
       
   484         ruleEntry.ruleId = ruleID; 
       
   485         ruleEntry.grammarId = grammarID;
       
   486         
       
   487         iRuleList->AppendL(ruleEntry);
       
   488         }
       
   489     
       
   490     // get the first iRuleList entry = 0. The rest of the entries are
       
   491     // processed in the event handler, MsruoEvent(aEvent=UnloadRuleComplete)
       
   492     ruleEntry = iRuleList->At(iRejectTagCurrentLoopCount);
       
   493     TUint32 ruleID = ruleEntry.ruleId;
       
   494     TUint32 grammarID = ruleEntry.grammarId;
       
   495     
       
   496     iRState = EVasWaitingForUnloadRuleComplete;
       
   497     
       
   498     RUBY_DEBUG0( "CNssRecognitionHandler: SRSF UnloadRule()" );
       
   499     
       
   500     TInt errorCode = iSrsApi->UnloadRule( (TSIGrammarID)grammarID, ruleID );
       
   501     if ( errorCode )
       
   502         {
       
   503         RUBY_DEBUG1( "CNssRecognitionHandler: UnloadRule Err = %d", errorCode );
       
   504 
       
   505         // Notify P&S
       
   506         RProperty::Set( KSINDUID, ERecognitionState, ERecognitionFail ); 
       
   507         Cleanup();
       
   508         return EVasRejectTagFailed;
       
   509         }
       
   510     
       
   511     return EVasErrorNone;
       
   512     }
       
   513 
       
   514 // -----------------------------------------------------------------------------
       
   515 // CNssRecognitionHandler::Cancel
       
   516 // Method to cancel the last recognition function.
       
   517 // -----------------------------------------------------------------------------
       
   518 //
       
   519 MNssRecognitionHandler::TNssRecognitionResult CNssRecognitionHandler::Cancel()
       
   520     {
       
   521     RUBY_DEBUG1( "CNssRecognitionHandler::Cancel() iRState = %d", iRState );
       
   522 
       
   523 	// a Cancel request for the Idle state requires no action.	
       
   524     if ( iRState == EVasIdle )
       
   525         {
       
   526 		return EVasErrorNone;    
       
   527         }
       
   528  
       
   529     // Notify P&S
       
   530     RProperty::Set( KSINDUID, ERecognitionState, ERecognitionFail ); 
       
   531 
       
   532 	// for all states except Idle call Cleanup which calls SRF cancel and 
       
   533 	// transitions to Idle state.
       
   534     Cleanup();
       
   535     return EVasErrorNone;
       
   536     }
       
   537 
       
   538 // -----------------------------------------------------------------------------
       
   539 // CNssRecognitionHandler::GetAdaptationItemL
       
   540 // Method to get the adaptation data.
       
   541 // -----------------------------------------------------------------------------
       
   542 //
       
   543 MNssAdaptationItem* CNssRecognitionHandler::GetAdaptationItemL(void)
       
   544     {
       
   545     RUBY_DEBUG_BLOCK( "CNssRecognitionHandler::GetAdaptationItemL" );
       
   546 	
       
   547     // Check state
       
   548 	if ( iRState != EVasWaitingForSelectTag )
       
   549     	{
       
   550         User::Leave( KErrNotReady );
       
   551     	}
       
   552 
       
   553     // Client Result Set should be there since the state is what it is.
       
   554     if ( !iSIClientResultSet )
       
   555         {
       
   556         User::Leave( KErrCorrupt );
       
   557         }
       
   558 
       
   559     // Construct adaptation item
       
   560     CNssAdaptationItem* adaptationItem = CNssAdaptationItem::NewL( this, iSIClientResultSet );
       
   561     // Adaptation item now owns the previous Client Result Set.
       
   562     iSIClientResultSet = 0; 
       
   563 
       
   564     // Add the item to our catalog of adaptation items.
       
   565     CleanupStack::PushL( adaptationItem );
       
   566     User::LeaveIfError( iAdaptItemArray.Append( adaptationItem ) );
       
   567     CleanupStack::Pop( adaptationItem );
       
   568 
       
   569     return( adaptationItem );
       
   570     }
       
   571 
       
   572 // -----------------------------------------------------------------------------
       
   573 // CNssRecognitionHandler::GetAdaptationItemL
       
   574 // Method to get the adaptation data.
       
   575 // -----------------------------------------------------------------------------
       
   576 //
       
   577 void CNssRecognitionHandler::RemoveAdaptationItem(CNssAdaptationItem* anItem)
       
   578     {
       
   579     for ( TInt k( 0 ); k < iAdaptItemArray.Count(); k++ )
       
   580         {
       
   581         if ( iAdaptItemArray[k] == anItem )
       
   582             {
       
   583             iAdaptItemArray.Remove( k );
       
   584             break;
       
   585             }
       
   586         }
       
   587     }
       
   588 
       
   589 // -----------------------------------------------------------------------------
       
   590 // CNssRecognitionHandler::MsruoEvent
       
   591 // Method to process the events from SRS utility object
       
   592 // -----------------------------------------------------------------------------
       
   593 //
       
   594 void CNssRecognitionHandler::MsruoEvent( TUid aEvent, TInt aResult )
       
   595     {
       
   596     RUBY_DEBUG2( "CNssRecognitionHandler::MsruoEvent Event = 0x%x, Result = %d", aEvent.iUid, aResult );
       
   597 
       
   598     switch ( aEvent.iUid )
       
   599         {
       
   600         case KUidAsrEventLoadModelsVal:
       
   601             {
       
   602             if (aResult == KErrNone)
       
   603                 {
       
   604                 DoLoadModelsComplete();
       
   605                 }
       
   606             else
       
   607                 {
       
   608                 DoLoadModelsFailed();
       
   609                 }
       
   610             break;
       
   611             }
       
   612             
       
   613         case KUidAsrEventLoadGrammarVal:
       
   614             {
       
   615             if (aResult == KErrNone)
       
   616                 {
       
   617                 DoLoadGrammarComplete();
       
   618                 }
       
   619             else
       
   620                 {
       
   621                 DoLoadGrammarFailed();
       
   622                 }
       
   623             break;
       
   624             }	
       
   625         case KUidAsrEventRecognitionReadyVal:
       
   626             {
       
   627             if (aResult == KErrNone)
       
   628                 {
       
   629                 DoRecognitionReady();                
       
   630                 }
       
   631             else
       
   632                 {
       
   633                 DoRecognitionFailed();
       
   634                 }
       
   635             break;
       
   636             }	
       
   637         case KUidAsrEventRecognitionVal:
       
   638             {
       
   639             
       
   640             if (aResult == KErrNone)
       
   641                 {
       
   642                 DoRecognitionComplete();
       
   643                 }
       
   644             else if ((aResult == KErrAsrNoSpeech) ||
       
   645                 (aResult == KErrAsrSpeechTooEarly) ||
       
   646                 (aResult == KErrAsrSpeechTooLong) ||
       
   647                 (aResult == KErrAsrSpeechTooShort))
       
   648                 {
       
   649                 DoRecognitionFailedNoSpeech(aResult);
       
   650                 }
       
   651             else if (aResult == KErrAsrNoMatch)
       
   652                 {
       
   653                 DoRecognitionFailedNoMatch();
       
   654                 }
       
   655             // Handle Cancelled the same as Failed
       
   656             else
       
   657                 {
       
   658                 DoRecognitionFailed();
       
   659                 }
       
   660             break;
       
   661             }	
       
   662         case KUidAsrEventRecordStartedVal:
       
   663             {
       
   664             DoRecordStarted();
       
   665             break;
       
   666             }
       
   667         case KUidAsrEventRecordVal:
       
   668             {
       
   669             if (aResult != KErrNone)
       
   670                 {
       
   671                 DoRecordFailed(aResult);
       
   672                 }
       
   673             // ignore Record KErrNone
       
   674             break;
       
   675             }
       
   676         case KUidAsrEventEouDetectedVal:
       
   677             {
       
   678             DoEouDetected();
       
   679             break;
       
   680             }
       
   681             
       
   682         case KUidAsrEventUnloadRuleVal:
       
   683             {
       
   684             if (aResult == KErrNone)
       
   685                 {
       
   686                 DoUnloadRuleComplete();
       
   687                 }
       
   688             else
       
   689                 {
       
   690                 DoUnloadRuleFailed();
       
   691                 }
       
   692             break;
       
   693             }	
       
   694             
       
   695         case KUidAsrEventAdaptVal:
       
   696             {
       
   697             if ( iRState != EVasDirectAdaptation && iRState != EVasIdleAdaptation )
       
   698                 {
       
   699 #ifdef _DEBUG
       
   700                 User::Panic( KRecognitionHandlerPanic, __LINE__ );
       
   701 #endif
       
   702                 return;
       
   703                 }
       
   704             
       
   705             if ( iRState == EVasIdleAdaptation )
       
   706                 {
       
   707                 Cleanup();
       
   708                 }
       
   709             
       
   710             if ( aResult == KErrNone )
       
   711                 {
       
   712                 iAdaptationHandler->HandleAdaptComplete( KErrNone );
       
   713                 }
       
   714             else{
       
   715                 iAdaptationHandler->HandleAdaptComplete( KErrGeneral );
       
   716                 }
       
   717             iRState = EVasWaitingForSelectTag;
       
   718             break;
       
   719             }
       
   720             
       
   721         case KUidAsrEventRemoveRuleVal:
       
   722             {
       
   723             if ( iRState == EVasCorrectingPluginDatabase )
       
   724                 {
       
   725                 if ( iWait.IsStarted() )
       
   726                     {
       
   727                     iWait.AsyncStop();
       
   728                     }
       
   729                 }
       
   730             else
       
   731                 {
       
   732                 Cleanup();
       
   733                 }
       
   734             break;
       
   735             }
       
   736             
       
   737         case KUidAsrEventPreStartSamplingVal:
       
   738             {
       
   739             DoPreStartSamplingComplete( aResult );
       
   740             break;
       
   741             }     
       
   742                
       
   743         default:
       
   744             {
       
   745             UnexpectedEvent();
       
   746             }
       
   747         }
       
   748     }
       
   749 
       
   750 // -----------------------------------------------------------------------------
       
   751 // CNssRecognitionHandler::CNssRecognitionHandler
       
   752 // C++ default constructor can NOT contain any code, that
       
   753 // might leave.
       
   754 // -----------------------------------------------------------------------------
       
   755 //
       
   756 CNssRecognitionHandler::CNssRecognitionHandler()
       
   757     {
       
   758     }
       
   759 
       
   760 // -----------------------------------------------------------------------------
       
   761 // CNssRecognitionHandler::ConstructL
       
   762 // Symbian 2nd phase constructor can leave.
       
   763 // -----------------------------------------------------------------------------
       
   764 //
       
   765 void CNssRecognitionHandler::ConstructL()
       
   766     {
       
   767     RUBY_DEBUG_BLOCK( "CNssRecognitionHandler::ConstructL" );
       
   768     iVasDBBuilder = CNssVASDBBuilder::InstanceL();
       
   769     iVasDBBuilder->InitializeL();
       
   770     
       
   771     iContextMgr = iVasDBBuilder->GetContextMgr();
       
   772     
       
   773     iTagMgr = iVasDBBuilder->GetTagMgr();
       
   774     
       
   775     iRState = EVasIdle;
       
   776     
       
   777     // Define P&S property
       
   778     TInt err = RProperty::Define( KSINDUID, ERecognitionState, RProperty::EInt); 
       
   779     if ( err != KErrNotFound && err != KErrAlreadyExists )
       
   780         {
       
   781         User::LeaveIfError( err );
       
   782         }
       
   783     }
       
   784 
       
   785 // -----------------------------------------------------------------------------
       
   786 // CNssRecognitionHandler::CleanupRecognizeInitFailed
       
   787 // Perform required cleanup to get back to the Idle state and issue the
       
   788 // HandleRecognizeInitError event with RecognizeInitFailed error code
       
   789 // -----------------------------------------------------------------------------
       
   790 //
       
   791 void CNssRecognitionHandler::CleanupRecognizeInitFailed()
       
   792     {
       
   793     RUBY_DEBUG0( "CNssRecognitionHandler::CleanupRecognizeInitFailed" );
       
   794     
       
   795     // Notify P&S
       
   796     RProperty::Set( KSINDUID, ERecognitionState, ERecognitionFail ); 
       
   797     
       
   798     Cleanup();
       
   799     iInitCompleteHandler->HandleRecognizeInitComplete(
       
   800         MNssRecognizeInitCompleteHandler::EVasRecognizeInitFailed );
       
   801     }	
       
   802 
       
   803 // -----------------------------------------------------------------------------
       
   804 // CNssRecognitionHandler::CleanupRecognizeFailed
       
   805 // Perform required cleanup to get back to the Idle state and issue the
       
   806 // HandleRecognizeError event with RecognizeFailed error code
       
   807 // -----------------------------------------------------------------------------
       
   808 //
       
   809 void CNssRecognitionHandler::CleanupRecognizeFailed()
       
   810     {
       
   811     RUBY_DEBUG0( "CNssRecognitionHandler::CleanupRecognizeFailed" );
       
   812     
       
   813     // Notify P&S
       
   814     RProperty::Set( KSINDUID, ERecognitionState, ERecognitionFail ); 
       
   815     
       
   816     Cleanup();
       
   817     iEventHandler->HandleRecognizeComplete( NULL,
       
   818         MNssRecognizeEventHandler::EVasRecognizeFailed);
       
   819     }
       
   820 
       
   821 // -----------------------------------------------------------------------------
       
   822 // CNssRecognitionHandler::CleanupRejectTagFailed
       
   823 // Perform required cleanup to get back to the Idle state and issue the
       
   824 // HandleRejectTagError event with RejectTagFailed error code
       
   825 // -----------------------------------------------------------------------------
       
   826 //
       
   827 void CNssRecognitionHandler::CleanupRejectTagFailed()
       
   828     {
       
   829     RUBY_DEBUG0( "CNssRecognitionHandler::CleanupRejectTagFailed" );
       
   830     
       
   831     // Notify P&S
       
   832     RProperty::Set( KSINDUID, ERecognitionState, ERecognitionFail ); 	
       
   833     
       
   834     Cleanup();
       
   835     iRejectTagCompleteHandler->HandleRejectTagComplete(
       
   836         MNssRejectTagCompleteHandler::EVasRejectTagFailed);
       
   837     }
       
   838 
       
   839 // -----------------------------------------------------------------------------
       
   840 // CNssRecognitionHandler::Cleanup
       
   841 // Perform required cleanup to get back to the Idle state
       
   842 // -----------------------------------------------------------------------------
       
   843 //
       
   844 void CNssRecognitionHandler::Cleanup()
       
   845     {
       
   846     RUBY_DEBUG0( "CNssRecognitionHandler::Cleanup" );
       
   847     
       
   848     if ( iTagMgr )
       
   849         {
       
   850         iTagMgr->CancelGetTag();
       
   851         }
       
   852     if ( iContextMgr )
       
   853         {
       
   854         iContextMgr->CancelGetContext();
       
   855         }
       
   856     DeleteSrf();
       
   857     ClearContextList();
       
   858     ClearTagList();
       
   859     ClearRuleList();
       
   860     ClearContextStatus();
       
   861     
       
   862     iRState = EVasIdle;
       
   863     }
       
   864 
       
   865 // -----------------------------------------------------------------------------
       
   866 // CNssRecognitionHandler:: DeleteSrf
       
   867 // Gracefully terminate and delete the SRF object, iSrsApi.
       
   868 // -----------------------------------------------------------------------------
       
   869 //
       
   870 void CNssRecognitionHandler::DeleteSrf()
       
   871     {
       
   872     if ( iSrsApi )
       
   873         {
       
   874         RUBY_DEBUG0( "CNssRecognitionHandler::DeleteSrf" );
       
   875         
       
   876         iSrsApi->CancelUtility();
       
   877         
       
   878         iSrsApi->EndRecSession();  // no need to check return code
       
   879         
       
   880         iSrsApi->SetEventHandler(NULL);
       
   881         
       
   882         delete iSrsApi;
       
   883         
       
   884         iSrsApi=NULL;
       
   885         }
       
   886 
       
   887     if ( iSIClientResultSet )
       
   888         {
       
   889         delete iSIClientResultSet;
       
   890         iSIClientResultSet = NULL;
       
   891         }
       
   892     }
       
   893 
       
   894 // -----------------------------------------------------------------------------
       
   895 // CNssRecognitionHandler::ClearContextList
       
   896 // Delete the context objects from iContextList
       
   897 // -----------------------------------------------------------------------------
       
   898 //
       
   899 void CNssRecognitionHandler::ClearContextList()
       
   900     {
       
   901     if (iContextList)
       
   902         {
       
   903         iContextList->ResetAndDestroy();
       
   904         delete iContextList;
       
   905         iContextList = NULL;
       
   906         }
       
   907     }
       
   908 
       
   909 // -----------------------------------------------------------------------------
       
   910 // CNssRecognitionHandler::ClearTagList
       
   911 // Delete the tag objects and tag list, iTagList
       
   912 // -----------------------------------------------------------------------------
       
   913 //
       
   914 void CNssRecognitionHandler::ClearTagList()
       
   915     {
       
   916     // The client deletes iTagList, its pointers,  and its tags
       
   917     // There is a window in RecognitionComplete while looping
       
   918     // through the ClientResultData and getting corresponding tags from
       
   919     // VasDb (GetTag). If a VasDb error occurs during this loop, 
       
   920     // Recognition still owns the iTagList tags and must delete them.
       
   921     // Otherwise at the end of the GetTag loop the list is given to the
       
   922     // client, and the client deletes the tags.
       
   923     
       
   924     // if iTagList is not NULL, delete iTagList and its tags.
       
   925     
       
   926     if ( iTagList )
       
   927         {
       
   928         iTagList->ResetAndDestroy();
       
   929         delete iTagList;
       
   930         iTagList = NULL;
       
   931         }
       
   932     }
       
   933 
       
   934 // -----------------------------------------------------------------------------
       
   935 // CNssRecognitionHandler::ClearRuleList
       
   936 // Delete the TRuleEntry objects from iRuleList
       
   937 // -----------------------------------------------------------------------------
       
   938 //
       
   939 void CNssRecognitionHandler::ClearRuleList()
       
   940     {
       
   941     if ( iRuleList )
       
   942         {
       
   943         iRuleList->Reset();
       
   944         delete iRuleList;
       
   945         iRuleList = NULL;
       
   946         }
       
   947     }
       
   948 
       
   949 // -----------------------------------------------------------------------------
       
   950 // CNssRecognitionHandler::ClearContextStatus
       
   951 // Delete the entries from iContextStatus
       
   952 // -----------------------------------------------------------------------------
       
   953 //
       
   954 void CNssRecognitionHandler::ClearContextStatus()
       
   955     {
       
   956     if ( iContextStatus )
       
   957         {
       
   958         iContextStatus->Reset();
       
   959         delete iContextStatus;
       
   960         iContextStatus = NULL;
       
   961         }
       
   962     }
       
   963 
       
   964 // -----------------------------------------------------------------------------
       
   965 // CNssRecognitionHandler::UnexpectedEvent
       
   966 // Unexpected event occurred. . 
       
   967 // -----------------------------------------------------------------------------
       
   968 //
       
   969 void CNssRecognitionHandler::UnexpectedEvent()
       
   970     {
       
   971     RUBY_DEBUG1( "CNssRecognitionHandler - UnexpectedEvent() iRState = %d", iRState );
       
   972     
       
   973     switch ( iRState )
       
   974         {
       
   975         case EVasIdle:
       
   976             {
       
   977             RUBY_DEBUG0( "CNssRecognitionHandler - Idle, Unexpected Srs Event" );
       
   978             break;
       
   979             }
       
   980             
       
   981             // RecognitionInit
       
   982         case EVasWaitingForLoadModelsComplete:
       
   983         case EVasWaitingForLoadLexiconComplete:
       
   984         case EVasWaitingForLoadGrammarComplete:
       
   985         case EVasWaitingForRecognitionReadyRecognitionInit:
       
   986             {
       
   987             CleanupRecognizeInitFailed();
       
   988             break;
       
   989             }
       
   990             
       
   991             // Recognition
       
   992         case EVasWaitingForRecordStarted:
       
   993         case EVasWaitingForEouDetected:
       
   994         case EVasWaitingForRecognitionComplete:
       
   995             {
       
   996             CleanupRecognizeFailed();
       
   997             break;
       
   998             }
       
   999             
       
  1000             // RejectTag
       
  1001         case EVasWaitingForUnloadRuleComplete:
       
  1002         case EVasWaitingForRecognitionReadyRejectTag:
       
  1003             {
       
  1004             CleanupRejectTagFailed();
       
  1005             break;
       
  1006             }
       
  1007         case EVasWaitingForRecognize:
       
  1008         case EVasWaitingForSelectTag:
       
  1009             {
       
  1010             // There is no event handler to return to.
       
  1011             // Notify P&S
       
  1012             RProperty::Set( KSINDUID, ERecognitionState, ERecognitionFail ); 			
       
  1013             Cleanup();
       
  1014             RUBY_DEBUG1( "CNssRecognitionHandler - Invalid State = %d", iRState );
       
  1015             break;
       
  1016             }
       
  1017         default:
       
  1018             {
       
  1019             // unknown state - do not know event handler to return to
       
  1020             // Notify P&S
       
  1021             RProperty::Set( KSINDUID, ERecognitionState, ERecognitionFail ); 
       
  1022             Cleanup();
       
  1023             RUBY_DEBUG1( "CNssRecognitionHandler - Unknown State = %d", iRState );
       
  1024             }
       
  1025         }
       
  1026     }
       
  1027 
       
  1028 // -----------------------------------------------------------------------------
       
  1029 // CNssRecognitionHandler::DoPreStartSamplingComplete
       
  1030 // Handle PreStartSampling SRSF event 
       
  1031 // -----------------------------------------------------------------------------
       
  1032 //
       
  1033 void CNssRecognitionHandler::DoPreStartSamplingComplete( TInt aError )
       
  1034     {
       
  1035     iInitCompleteHandler->HandlePreSamplingStarted( aError );
       
  1036     }
       
  1037 
       
  1038 // -----------------------------------------------------------------------------
       
  1039 // CNssRecognitionHandler::DoLoadModelsComplete
       
  1040 // Handle LoadModelsComplete SRF event 
       
  1041 // -----------------------------------------------------------------------------
       
  1042 //
       
  1043 void CNssRecognitionHandler::DoLoadModelsComplete()
       
  1044     {
       
  1045     if ( iRState != EVasWaitingForLoadModelsComplete )
       
  1046         {
       
  1047         UnexpectedEvent();
       
  1048         return;
       
  1049         }
       
  1050     /*	iRState = EVasWaitingForLoadLexiconComplete;
       
  1051     TUint32 lexiconId = iContextList->
       
  1052     At(iContextCurrentLoopCount)->LexiconId();
       
  1053       
       
  1054     TInt errorCode = iSrsApi->LoadLexicon( (TSILexiconID)lexiconId );
       
  1055     if (errorCode)
       
  1056         {
       
  1057         CleanupRecognizeInitFailed();	
       
  1058         }
       
  1059     */
       
  1060     iRState = EVasWaitingForLoadGrammarComplete;
       
  1061     
       
  1062     // iContextCurrentLoopCount is 0
       
  1063     // set loop count to -1 so Load Grammar Complete converts it back to 0
       
  1064     iContextCurrentLoopCount = -1;
       
  1065     DoLoadGrammarComplete();
       
  1066     }
       
  1067 
       
  1068 // -----------------------------------------------------------------------------
       
  1069 // CNssRecognitionHandler::DoLoadModelsFailed
       
  1070 // Handle LoadModelsFailed SRF event 
       
  1071 // -----------------------------------------------------------------------------
       
  1072 //
       
  1073 void CNssRecognitionHandler::DoLoadModelsFailed()
       
  1074     {
       
  1075     if ( iRState != EVasWaitingForLoadModelsComplete )
       
  1076         {
       
  1077         UnexpectedEvent();
       
  1078         return;
       
  1079         }
       
  1080     CleanupRecognizeInitFailed();	
       
  1081     }
       
  1082 
       
  1083 // -----------------------------------------------------------------------------
       
  1084 // CNssRecognitionHandler::DoLoadLexiconComplete
       
  1085 // Handle LoadLexiconComplete SRF event 
       
  1086 // -----------------------------------------------------------------------------
       
  1087 //
       
  1088 void CNssRecognitionHandler::DoLoadLexiconComplete()
       
  1089     {
       
  1090     if ( iRState != EVasWaitingForLoadLexiconComplete )
       
  1091         {
       
  1092         UnexpectedEvent();
       
  1093         return;
       
  1094         }
       
  1095     iRState = EVasWaitingForLoadGrammarComplete;
       
  1096     
       
  1097     // iContextCurrentLoopCount is 0
       
  1098     // set loop count to -1 so Load Grammar Complete converts it back to 0
       
  1099     iContextCurrentLoopCount--;
       
  1100     DoLoadGrammarComplete();
       
  1101     }
       
  1102 
       
  1103 // -----------------------------------------------------------------------------
       
  1104 // CNssRecognitionHandler::DoLoadLexiconFailed
       
  1105 // Handle LoadLexiconFailed SRF event 
       
  1106 // -----------------------------------------------------------------------------
       
  1107 //
       
  1108 void CNssRecognitionHandler::DoLoadLexiconFailed()
       
  1109     {
       
  1110     if ( iRState != EVasWaitingForLoadLexiconComplete )
       
  1111         {
       
  1112         UnexpectedEvent();
       
  1113         return;
       
  1114         }
       
  1115     CleanupRecognizeInitFailed();			
       
  1116     }
       
  1117 
       
  1118 // -----------------------------------------------------------------------------
       
  1119 // CNssRecognitionHandler::DoLoadGrammarComplete
       
  1120 // Handle LoadGrammarComplete SRF event 
       
  1121 // -----------------------------------------------------------------------------
       
  1122 //
       
  1123 void CNssRecognitionHandler::DoLoadGrammarComplete()
       
  1124     {
       
  1125     if ( iRState != EVasWaitingForLoadGrammarComplete )
       
  1126         {
       
  1127         UnexpectedEvent();
       
  1128         return;
       
  1129         }
       
  1130     // if there are more contexts, 
       
  1131     // continue loading Grammars, into the SRS
       
  1132     iContextCurrentLoopCount++;
       
  1133     
       
  1134     while ( iContextCurrentLoopCount < iContextCount )
       
  1135         {
       
  1136         TBool status = iContextStatus->At( iContextCurrentLoopCount );
       
  1137         // if True, there are tags in the context load the grammar
       
  1138         if ( status )
       
  1139             {
       
  1140             TUint32 grammarId = iContextList->
       
  1141                 At( iContextCurrentLoopCount )->GrammarId();
       
  1142             TInt errorCode = iSrsApi->LoadGrammar( (TSIGrammarID) grammarId ); 
       
  1143             if ( errorCode )
       
  1144                 {
       
  1145                 RUBY_DEBUG1( "CNssRecognitionHandler: LoadGrammar Err = %d", errorCode );
       
  1146                 CleanupRecognizeInitFailed();			
       
  1147                 }
       
  1148             return;
       
  1149             }
       
  1150         
       
  1151         // else False, skip the context and go to the next
       
  1152         else
       
  1153             {
       
  1154             iContextCurrentLoopCount++;	
       
  1155             }
       
  1156         }
       
  1157     
       
  1158     // are done loading contexts;				
       
  1159     iRState = EVasWaitingForRecognitionReadyRecognitionInit;
       
  1160     
       
  1161     RUBY_DEBUG0( "CNssRecognitionHandler: SRF Recognize" );
       
  1162     
       
  1163     // Asynchronous call: next expected event:  ERecognitionReady 
       
  1164     // dereference iClientResult ptr to make it a reference 
       
  1165     TInt errorCode = iSrsApi->Recognize( *iSIClientResultSet );
       
  1166     
       
  1167     if ( errorCode ) // Check if errorCode is zero (KErrNone) or non-zero.
       
  1168         {
       
  1169         RUBY_DEBUG1( "CNssRecognitionHandler: Recognize Err = %d", errorCode );
       
  1170         CleanupRecognizeInitFailed();
       
  1171         }							
       
  1172     }
       
  1173 
       
  1174 // -----------------------------------------------------------------------------
       
  1175 // CNssRecognitionHandler::DoLoadGrammarFailed
       
  1176 // Handle LoadGrammarFailed SRF event 
       
  1177 // -----------------------------------------------------------------------------
       
  1178 //
       
  1179 void CNssRecognitionHandler::DoLoadGrammarFailed()
       
  1180     {
       
  1181     if ( iRState != EVasWaitingForLoadGrammarComplete )
       
  1182         {
       
  1183         UnexpectedEvent();
       
  1184         return;
       
  1185         }
       
  1186     CleanupRecognizeInitFailed();
       
  1187     }
       
  1188 
       
  1189 // -----------------------------------------------------------------------------
       
  1190 // CNssRecognitionHandler::DoRecognitionReady
       
  1191 // Handle RecognitionReady SRF event 
       
  1192 // -----------------------------------------------------------------------------
       
  1193 //
       
  1194 void CNssRecognitionHandler::DoRecognitionReady()
       
  1195     {
       
  1196     if ( iRState == EVasWaitingForRecognitionReadyRecognitionInit )
       
  1197         {
       
  1198         ClearContextStatus();
       
  1199         iRState = EVasWaitingForRecognize;
       
  1200         
       
  1201         RUBY_DEBUG0( "CNssRecognitionHandler: HandleRecognizeInitComplete" );
       
  1202        
       
  1203         iInitCompleteHandler->HandleRecognizeInitComplete( 
       
  1204             MNssRecognizeInitCompleteHandler::EVasErrorNone );
       
  1205         }
       
  1206     else if ( iRState == EVasWaitingForRecognitionReadyRejectTag )
       
  1207         {
       
  1208         iRState = EVasWaitingForRecognize;
       
  1209         
       
  1210         RUBY_DEBUG0( "CNssRecognitionHandler: HandleRejectTagComplete" );
       
  1211         
       
  1212         iRejectTagCompleteHandler->HandleRejectTagComplete(
       
  1213             MNssRejectTagCompleteHandler::EVasErrorNone );
       
  1214         }
       
  1215     else
       
  1216         {
       
  1217         UnexpectedEvent();
       
  1218         }
       
  1219     }
       
  1220 
       
  1221 // -----------------------------------------------------------------------------
       
  1222 // CNssRecognitionHandler::DoRecognitionComplete
       
  1223 // Handle RecognitionComplete SRF event 
       
  1224 // -----------------------------------------------------------------------------
       
  1225 //
       
  1226 void CNssRecognitionHandler::DoRecognitionComplete()
       
  1227     {
       
  1228     if ( iRState != EVasWaitingForRecognitionComplete )
       
  1229         {
       
  1230         UnexpectedEvent();
       
  1231         return;
       
  1232         }
       
  1233     
       
  1234     TInt resultSetCount = iSIClientResultSet->ResultCount();
       
  1235     
       
  1236     // allocate the Tag List - the client is responsible for deleting this
       
  1237     if (!iTagList)
       
  1238         {
       
  1239         iTagList = new CArrayPtrFlat<CNssTag>( 1 );
       
  1240         if ( !iTagList )
       
  1241             {
       
  1242             RUBY_DEBUG0( "CNssRecognitionHandler: TRAPD new CTAG Array failed" );
       
  1243             CleanupRecognizeFailed();
       
  1244             return;
       
  1245             }		
       
  1246         }
       
  1247     
       
  1248     // Loop to convert result set data to tags and put the tags 
       
  1249     // into a Tag List
       
  1250     // Since getTag is asynchronous the loop continues at
       
  1251     // member function GetTagCompleted(). Use loop control variables:
       
  1252     // iTagCurrentLoopCount and iTagCount
       
  1253     
       
  1254     iTagCurrentLoopCount = 0;
       
  1255     iTagCount = resultSetCount;
       
  1256     /*
       
  1257     const CSDClientResult& clientResult = iClientResultSet->At(0);
       
  1258     */
       
  1259     TInt count( iSIClientResultSet->ResultCount() );
       
  1260     TInt k( 0 );
       
  1261     TInt err( KErrNone );
       
  1262     
       
  1263     for ( k = 0; k < count; k++ )
       
  1264         {
       
  1265         const CSIClientResult* clientResult( NULL );
       
  1266         TRAP( err, clientResult = &iSIClientResultSet->AtL( k ) );
       
  1267         if ( err != KErrNone )
       
  1268             {
       
  1269             break;
       
  1270             }
       
  1271         
       
  1272         // Get the tag from DB
       
  1273         CNssTag* tag = iTagMgr->GetTag( 
       
  1274             clientResult->GrammarID(),
       
  1275             clientResult->RuleID() );
       
  1276         
       
  1277         if ( !tag )
       
  1278             {
       
  1279             RUBY_DEBUG0( "CNssRecognitionHandler::DoRecognitionComplete found a tag which is NOT in VAS database" );
       
  1280             TNssRState oldState = iRState;
       
  1281             iRState = EVasCorrectingPluginDatabase;
       
  1282 
       
  1283             TInt error = iSrsApi->RemoveRule( clientResult->GrammarID(), clientResult->RuleID() );
       
  1284             if ( error == KErrNone && !iWait.IsStarted() )
       
  1285                 {
       
  1286                 iWait.Start();
       
  1287                 }
       
  1288             iRState = oldState;
       
  1289             }
       
  1290         else
       
  1291             {
       
  1292             // Append it to the "our" tag list (which will be copied
       
  1293             // to produce client tag list)
       
  1294             TRAP( err, iTagList->AppendL( tag ) );
       
  1295         
       
  1296             if ( err != KErrNone )
       
  1297                 {
       
  1298                 break;
       
  1299                 }
       
  1300             }
       
  1301         }
       
  1302     
       
  1303     // If no tags could be found
       
  1304     if ( iTagList->Count() == 0 )
       
  1305         {
       
  1306         RUBY_DEBUG0( "CNssRecognitionHandler::DoRecognitionComplete no tags found at all" );
       
  1307         // Cleans and makes the "RecognizeFailed" callback.
       
  1308         CleanupRecognizeFailed();
       
  1309         return;
       
  1310         }
       
  1311     
       
  1312     // Copy the tag list. The copy is sent to the client.
       
  1313     CArrayPtrFlat<MNssTag>* clientTagList = NULL;
       
  1314     
       
  1315     clientTagList = new CArrayPtrFlat<MNssTag>(1);
       
  1316     
       
  1317     if ( clientTagList == 0 )
       
  1318         {
       
  1319         // Cleans and makes the "RecognizeFailed" callback.
       
  1320         CleanupRecognizeFailed();
       
  1321         return;
       
  1322         }
       
  1323     
       
  1324     for ( k = 0; k < iTagList->Count(); k++ )
       
  1325         {
       
  1326         MNssTag* tag = STATIC_CAST( MNssTag*, (*iTagList)[k] );
       
  1327         
       
  1328         TRAP( err, clientTagList->AppendL( tag ) );
       
  1329         
       
  1330         if ( err != KErrNone )
       
  1331             {
       
  1332             RUBY_DEBUG0( "CRecognitionHandler: TRAPD DestList AppendL failed" );
       
  1333             
       
  1334             // Don't do the "ResetAndDestroy()" : iTagList still owns the tags.
       
  1335             clientTagList->Reset();
       
  1336             delete clientTagList;
       
  1337             
       
  1338             // Cleans and makes the "RecognizeFailed" callback.
       
  1339             CleanupRecognizeFailed();
       
  1340             return;
       
  1341             }			
       
  1342         }
       
  1343     
       
  1344     iRState = EVasWaitingForSelectTag;
       
  1345     
       
  1346     // After this, clientTagList owns the tags:
       
  1347     iTagList->Reset();
       
  1348     delete iTagList;
       
  1349     iTagList = 0;
       
  1350     
       
  1351     RUBY_DEBUG0( "CRecognitionHandler: HandleRecognizeComplete" );
       
  1352     
       
  1353     // Notify P&S
       
  1354     RProperty::Set( KSINDUID, ERecognitionState, ERecognitionSuccess ); 
       
  1355     
       
  1356     iEventHandler->HandleRecognizeComplete(
       
  1357         clientTagList, MNssRecognizeEventHandler::EVasErrorNone );
       
  1358     
       
  1359     //    const CSIClientResult& clientResult = iSIClientResultSet->AtL(0);
       
  1360     
       
  1361     /******************** Debug recognized tags *********************/
       
  1362     /*
       
  1363     RFs session;
       
  1364     RFile file;
       
  1365     _LIT( aFileName, "c:\\documents\\recotag.txt" );
       
  1366     
       
  1367       session.Connect();
       
  1368       if ( file.Open( session, aFileName, EFileWrite ) != KErrNone )
       
  1369       {
       
  1370       if ( file.Create( session, aFileName, EFileWrite ) != KErrNone )
       
  1371       {
       
  1372       User::Panic( _L("SDContrlDebug"), __LINE__ );
       
  1373       }
       
  1374       }
       
  1375       
       
  1376         for ( TInt k( 0 ); k < iSIClientResultSet->ResultCount(); k++ )
       
  1377         {
       
  1378         const CSIClientResult& clientResult = iSIClientResultSet->AtL(k);
       
  1379         
       
  1380           TBuf8<100> debugBuf;
       
  1381           debugBuf.AppendFormat( _L8("Recognized tag with RID:%d, GID:%d\n"), clientResult.RuleID(), clientResult.GrammarID() );
       
  1382           file.Write( debugBuf );
       
  1383           }
       
  1384           
       
  1385             
       
  1386               file.Close();
       
  1387               session.Close();
       
  1388     */
       
  1389     /****************************************************************/
       
  1390     
       
  1391     // get next tag
       
  1392     /*	TUint32 grammarId = clientResult.GrammarID();
       
  1393     TUint32 ruleId = clientResult.RuleID();
       
  1394     
       
  1395       TInt dbErrorCode;
       
  1396       dbErrorCode = iTagMgr->GetTag( this, 
       
  1397       grammarId, 
       
  1398       ruleId);
       
  1399       if (dbErrorCode!=KErrNone)
       
  1400       {
       
  1401         CleanupRecognizeFailed();
       
  1402         }
       
  1403     */
       
  1404     }
       
  1405 
       
  1406 // -----------------------------------------------------------------------------
       
  1407 // CNssRecognitionHandler::DoRecognitionFailedNoSpeech
       
  1408 // Handle RecognitionFailedNoSpeech, RecognitionFailedTooEarly, 
       
  1409 // RecognitionFailedTooLong, and RecognitionFailedTooShort SRF events 
       
  1410 // -----------------------------------------------------------------------------
       
  1411 //
       
  1412 void CNssRecognitionHandler::DoRecognitionFailedNoSpeech( TInt aResult )
       
  1413     {
       
  1414     if ( iRState != EVasWaitingForRecognitionComplete )
       
  1415         {
       
  1416         UnexpectedEvent();
       
  1417         return;
       
  1418         }
       
  1419     
       
  1420     MNssRecognizeEventHandler::TNssRecognizeError errCode;
       
  1421     errCode = MNssRecognizeEventHandler::EVasRecognizeFailed;
       
  1422     if ( aResult == KErrAsrNoSpeech )
       
  1423         {
       
  1424         errCode = MNssRecognizeEventHandler::EVasRecognitionFailedNoSpeech;
       
  1425         }
       
  1426     else if ( aResult == KErrAsrSpeechTooEarly )
       
  1427         {
       
  1428         errCode = MNssRecognizeEventHandler::EVasRecognitionFailedTooEarly;
       
  1429         }
       
  1430     else if ( aResult == KErrAsrSpeechTooLong )
       
  1431         {
       
  1432         errCode = MNssRecognizeEventHandler::EVasRecognitionFailedTooLong;
       
  1433         }
       
  1434     else if ( aResult == KErrAsrSpeechTooShort )
       
  1435         {
       
  1436         errCode = MNssRecognizeEventHandler::EVasRecognitionFailedTooShort;
       
  1437         }
       
  1438     
       
  1439     // Notify P&S
       
  1440     RProperty::Set( KSINDUID, ERecognitionState, ERecognitionFail ); 
       
  1441     
       
  1442     Cleanup();
       
  1443     iEventHandler->HandleRecognizeComplete( NULL, errCode );
       
  1444     }
       
  1445 
       
  1446 // -----------------------------------------------------------------------------
       
  1447 // CNssRecognitionHandler::DoRecognitionFailedNoMatch
       
  1448 // Handle RecognitionFailedNoMatch SRF event 
       
  1449 // -----------------------------------------------------------------------------
       
  1450 //
       
  1451 void CNssRecognitionHandler::DoRecognitionFailedNoMatch()
       
  1452     {
       
  1453     if ( iRState == EVasWaitingForRecognitionReadyRecognitionInit )
       
  1454         {
       
  1455         CleanupRecognizeInitFailed();
       
  1456         }
       
  1457     else if ( iRState == EVasWaitingForRecognitionComplete )
       
  1458         {
       
  1459         // Notify P&S
       
  1460         RProperty::Set( KSINDUID, ERecognitionState, ERecognitionFail ); 
       
  1461         Cleanup();
       
  1462         iEventHandler->HandleRecognizeComplete( NULL,
       
  1463             MNssRecognizeEventHandler::EVasRecognitionFailedNoMatch );	
       
  1464         }
       
  1465     else if ( iRState == EVasWaitingForRecognitionReadyRejectTag )
       
  1466         {
       
  1467         // Notify P&S
       
  1468         RProperty::Set( KSINDUID, ERecognitionState, ERecognitionFail ); 
       
  1469         Cleanup();
       
  1470         iRejectTagCompleteHandler->HandleRejectTagComplete(
       
  1471             MNssRejectTagCompleteHandler::EVasNoTagInContexts);
       
  1472         }
       
  1473     else
       
  1474         {
       
  1475         UnexpectedEvent();
       
  1476         }
       
  1477     }
       
  1478 
       
  1479 // -----------------------------------------------------------------------------
       
  1480 // CNssRecognitionHandler::DoRecognitionFailed
       
  1481 // Handle RecognitionFailed SRF event 
       
  1482 // -----------------------------------------------------------------------------
       
  1483 //
       
  1484 void CNssRecognitionHandler::DoRecognitionFailed()
       
  1485     {
       
  1486     switch ( iRState )
       
  1487         {
       
  1488         case EVasWaitingForRecognitionReadyRecognitionInit:
       
  1489             {
       
  1490             CleanupRecognizeInitFailed();
       
  1491             break;
       
  1492             }
       
  1493         case EVasWaitingForRecordStarted:
       
  1494         case EVasWaitingForEouDetected:
       
  1495         case EVasWaitingForRecognitionComplete:
       
  1496             {
       
  1497             CleanupRecognizeFailed();
       
  1498             break;
       
  1499             }
       
  1500         case EVasWaitingForRecognitionReadyRejectTag:
       
  1501             {
       
  1502             CleanupRejectTagFailed();
       
  1503             break;
       
  1504             }
       
  1505         default:
       
  1506             {
       
  1507             UnexpectedEvent();
       
  1508             }
       
  1509         }
       
  1510     }
       
  1511 
       
  1512 // -----------------------------------------------------------------------------
       
  1513 // CNssRecognitionHandler::DoRecordStarted
       
  1514 // Handle RecordStarted SRF event 
       
  1515 // -----------------------------------------------------------------------------
       
  1516 //
       
  1517 void CNssRecognitionHandler::DoRecordStarted()
       
  1518     {
       
  1519     if ( iRState != EVasWaitingForRecordStarted )
       
  1520         {
       
  1521         UnexpectedEvent();
       
  1522         return;
       
  1523         }
       
  1524     iRState = EVasWaitingForEouDetected;
       
  1525     
       
  1526     // Notify P&S
       
  1527     RProperty::Set( KSINDUID, ERecognitionState, ERecognitionStarted ); 
       
  1528     
       
  1529     iEventHandler->HandleRecordStarted(); 
       
  1530     }
       
  1531 
       
  1532 // -----------------------------------------------------------------------------
       
  1533 // CNssRecognitionHandler::DoRecordFailed
       
  1534 // Handle RecordFailed SRF event 
       
  1535 // -----------------------------------------------------------------------------
       
  1536 //
       
  1537 void CNssRecognitionHandler::DoRecordFailed( TInt aResult )
       
  1538     {
       
  1539     if ( iRState != EVasWaitingForEouDetected )
       
  1540         {
       
  1541         UnexpectedEvent();
       
  1542         return;
       
  1543         }
       
  1544     if ( ( aResult == KErrDied ) ||
       
  1545         ( aResult == KErrAccessDenied ) )
       
  1546         {
       
  1547         
       
  1548         Cleanup();
       
  1549         iEventHandler->HandleRecognizeComplete( NULL,
       
  1550             MNssRecognizeEventHandler::EVasRecognizeAccessDeny );
       
  1551         }
       
  1552     else
       
  1553         {
       
  1554         CleanupRecognizeFailed();
       
  1555         }
       
  1556     }
       
  1557 
       
  1558 // -----------------------------------------------------------------------------
       
  1559 // CNssRecognitionHandler::DoEouDetected
       
  1560 // Handle EouDetected SRF event 
       
  1561 // -----------------------------------------------------------------------------
       
  1562 //
       
  1563 void CNssRecognitionHandler::DoEouDetected()
       
  1564     {
       
  1565     if ( iRState != EVasWaitingForEouDetected )
       
  1566         {
       
  1567         UnexpectedEvent();
       
  1568         return;
       
  1569         }
       
  1570     iRState = EVasWaitingForRecognitionComplete;
       
  1571     // Notify P&S
       
  1572     RProperty::Set( KSINDUID, ERecognitionState, ERecognitionSpeechEnd ); 
       
  1573     
       
  1574     iEventHandler->HandleEouDetected();
       
  1575     }
       
  1576 
       
  1577 // -----------------------------------------------------------------------------
       
  1578 // CNssRecognitionHandler::DoUnloadRuleComplete
       
  1579 // Handle UnloadRuleComplete SRF event 
       
  1580 // -----------------------------------------------------------------------------
       
  1581 //
       
  1582 void CNssRecognitionHandler::DoUnloadRuleComplete()
       
  1583     {
       
  1584     if ( iRState != EVasWaitingForUnloadRuleComplete )
       
  1585         {
       
  1586         UnexpectedEvent();
       
  1587         return;
       
  1588         }
       
  1589     // if there are more tags
       
  1590     // continue unloading rules, from the SRS
       
  1591     iRejectTagCurrentLoopCount++;
       
  1592     if ( iRejectTagCurrentLoopCount < iRejectTagCount )
       
  1593         {
       
  1594         TRuleEntry ruleEntry = iRuleList->At( iRejectTagCurrentLoopCount );
       
  1595         TUint32 ruleID = ruleEntry.ruleId;
       
  1596         TUint32 grammarID = ruleEntry.grammarId;
       
  1597         
       
  1598         RUBY_DEBUG0( "CNssRecognitionHandler: SRF UnloadRule" );
       
  1599         
       
  1600         TInt errorCode = iSrsApi->UnloadRule( (TSIGrammarID)grammarID, ruleID );
       
  1601         if ( errorCode )
       
  1602             {
       
  1603             RUBY_DEBUG1( "CNssRecognitionHandler: UnloadRule Err = %d", errorCode );
       
  1604 
       
  1605             CleanupRejectTagFailed();
       
  1606             }
       
  1607         }
       
  1608     else
       
  1609         {
       
  1610         ClearRuleList();
       
  1611         iRState = EVasWaitingForRecognitionReadyRejectTag;
       
  1612         
       
  1613         RUBY_DEBUG0( "CNssRecognitionHandler: SRF Recognize" );
       
  1614         
       
  1615         // dereference iClientResult ptr to make it a reference 
       
  1616         //TInt errorCode = iSrsApi->Recognize(*iClientResultSet);
       
  1617         TInt errorCode = iSrsApi->Recognize( *iSIClientResultSet );
       
  1618         if ( errorCode )
       
  1619             {
       
  1620             RUBY_DEBUG1( "CNssRecognitionHandler: Recognize Err = %d", errorCode );
       
  1621            
       
  1622             CleanupRejectTagFailed();
       
  1623             }	
       
  1624         }
       
  1625     }
       
  1626 
       
  1627 // -----------------------------------------------------------------------------
       
  1628 // CNssRecognitionHandler::DoUnloadRuleFailed
       
  1629 // Handle UnloadRuleFailed SRF event 
       
  1630 // -----------------------------------------------------------------------------
       
  1631 //
       
  1632 void CNssRecognitionHandler::DoUnloadRuleFailed()
       
  1633     {
       
  1634     ClearRuleList();
       
  1635     if ( iRState != EVasWaitingForUnloadRuleComplete )
       
  1636         {
       
  1637         UnexpectedEvent();
       
  1638         return;
       
  1639         }
       
  1640     CleanupRejectTagFailed();
       
  1641     }
       
  1642 
       
  1643 // -----------------------------------------------------------------------------
       
  1644 // CNssRecognitionHandler::HandleTagCheckL
       
  1645 // Loop through the contexts to build the ContextStatus array.
       
  1646 // If there are any tags in any context,
       
  1647 // Then proceed on with recognition and load models,
       
  1648 // Else return with EVasNoTagInContexts.
       
  1649 // -----------------------------------------------------------------------------
       
  1650 //
       
  1651 void CNssRecognitionHandler::HandleTagCheckL()
       
  1652     {
       
  1653     TBool anyTag = EFalse;
       
  1654     CNssContext* context;
       
  1655     
       
  1656     if ( !iContextStatus )
       
  1657         {
       
  1658         iContextStatus = new (ELeave) CArrayFixFlat<TBool>( iContextCount );
       
  1659         }
       
  1660     
       
  1661     __ASSERT_DEBUG( iContextCount == iContextList->Count(), User::Panic( KRecognitionHandlerPanic, __LINE__ ) );
       
  1662     
       
  1663     for ( TInt index=0; index < iContextCount; index++ )
       
  1664         {
       
  1665         context = iContextList->At( index );
       
  1666         // if a tag exists in the requested context, status is set to true
       
  1667         // if there are no tags in the requested context, status is set to false
       
  1668         TBool status = iContextMgr->TagExist( context );
       
  1669         
       
  1670         iContextStatus->AppendL( status );
       
  1671         if ( status )
       
  1672             {
       
  1673             // if a tag is found, set to True
       
  1674             anyTag = ETrue;
       
  1675             }
       
  1676         }
       
  1677     
       
  1678     if ( anyTag )
       
  1679         {
       
  1680         // True: there are tags, continue recognition
       
  1681         iRState = EVasWaitingForLoadModelsComplete;
       
  1682         
       
  1683         TUint32 modelBankID = iContextList->At( 0 )->ModelBankId();
       
  1684         TInt errorCode = iSrsApi->LoadModels( (TSIModelBankID)modelBankID ); 
       
  1685         if ( errorCode )
       
  1686             {
       
  1687             RUBY_DEBUG1( "CNssRecognitionHandler: LoadModels Err = %d", errorCode );
       
  1688             
       
  1689             CleanupRecognizeInitFailed();
       
  1690             }
       
  1691         }
       
  1692     else
       
  1693         {
       
  1694         // Notify P&S
       
  1695         RProperty::Set( KSINDUID, ERecognitionState, ERecognitionFail ); 
       
  1696         // False: There are no tags in any context.
       
  1697         Cleanup();
       
  1698         iInitCompleteHandler->HandleRecognizeInitComplete(
       
  1699             MNssRecognizeInitCompleteHandler::EVasNoTagInContexts );
       
  1700         }
       
  1701     }
       
  1702 
       
  1703 // -----------------------------------------------------------------------------
       
  1704 // CNssRecognitionHandler::CreateSrfL
       
  1705 // Create the SRF object, iSrsApi.
       
  1706 // -----------------------------------------------------------------------------
       
  1707 //
       
  1708 void CNssRecognitionHandler::CreateSrfL()
       
  1709     {
       
  1710     RUBY_DEBUG_BLOCK( "CNssRecognitionHandler::CreateSrfL" );
       
  1711     // passing the CNssRecognitionHandler object as an active event handler
       
  1712     // and passing the VAS Api's UID
       
  1713     iSrsApi	= CNssSiUtilityWrapper::NewL(*this, KNssVASApiUid);
       
  1714     }
       
  1715 
       
  1716 // -----------------------------------------------------------------------------
       
  1717 // CNssRecognitionHandler::AdaptL
       
  1718 // Starts adaptation. Called by MNssAdaptationItem.
       
  1719 // -----------------------------------------------------------------------------
       
  1720 //
       
  1721 void CNssRecognitionHandler::AdaptL( MNssAdaptationEventHandler* aHandler,
       
  1722                                      CSIClientResultSet* aResult,
       
  1723                                      TInt aCorrect )
       
  1724     {
       
  1725     RUBY_DEBUG_BLOCK( "CNssRecognitionHandler::AdaptL" );
       
  1726     
       
  1727     if ( !aHandler || !aResult )
       
  1728         {
       
  1729         User::Leave( KErrArgument );
       
  1730         }
       
  1731     
       
  1732     // We can do an adaptation either
       
  1733     //  * directly after a recognition (direct adaptation), or
       
  1734     //  * when the state is idle (idle adaptation).
       
  1735     // In IDLE   Adaptation: We need to create and destroy SRS Utility object.
       
  1736     // In DIRECT Adaptation: We need to return to the same state where we left.
       
  1737     
       
  1738     if ( iRState == EVasIdle )
       
  1739         {
       
  1740         iRState = EVasIdleAdaptation;
       
  1741         
       
  1742         CreateSrfL();
       
  1743         
       
  1744         User::LeaveIfError( iSrsApi->Adapt( *aResult, aCorrect ) );
       
  1745         }
       
  1746     else if ( iRState == EVasWaitingForSelectTag )
       
  1747         {
       
  1748         iRState = EVasDirectAdaptation;
       
  1749         
       
  1750         if ( !iSrsApi )
       
  1751             {
       
  1752             User::Leave( KErrNotReady );
       
  1753             }
       
  1754         
       
  1755         User::LeaveIfError( iSrsApi->Adapt( *aResult, aCorrect ) );
       
  1756         }
       
  1757     else
       
  1758         {
       
  1759         User::Leave( KErrNotReady );
       
  1760         }
       
  1761     
       
  1762     iAdaptationHandler = aHandler;
       
  1763     }
       
  1764 
       
  1765 // -----------------------------------------------------------------------
       
  1766 // CNssRecognitionHandler::CommonRecognizeInitL
       
  1767 //
       
  1768 // Called by RecognizeInitL -variants
       
  1769 // to perform those initializations,
       
  1770 // which do not depend on vocabulary.
       
  1771 // -----------------------------------------------------------------------
       
  1772 //
       
  1773 MNssRecognitionHandler::TNssRecognitionResult
       
  1774 CNssRecognitionHandler::CommonRecognizeInitL( 
       
  1775                     MNssRecognizeInitCompleteHandler* aInitCompleteHandler,
       
  1776                     TInt aMaxResults )
       
  1777     {
       
  1778     // Agenda:
       
  1779     // 1. Create CSISpeechRecognitoinUtility object
       
  1780     //
       
  1781     // 2. Call SetPrioritySettings & StartRecSession
       
  1782     //    - nothing interesting happens there
       
  1783     //
       
  1784     // 4. Initialize CSIClientResultSet for storing recongition results
       
  1785     //
       
  1786     if ( iRState != EVasIdle )
       
  1787         {
       
  1788         RUBY_DEBUG0( "CNssRecognitionHandler::RecognizeInitL - Unexpected Request" );
       
  1789 
       
  1790         // Notify P&S
       
  1791         RProperty::Set( KSINDUID, ERecognitionState, ERecognitionFail ); 
       
  1792         Cleanup();
       
  1793         return EVasUnexpectedRequest;
       
  1794         }
       
  1795     
       
  1796     // Check callback
       
  1797     if ( !aInitCompleteHandler )
       
  1798         {
       
  1799         return EVasInvalidParameter;
       
  1800         }
       
  1801     iInitCompleteHandler = aInitCompleteHandler;
       
  1802     
       
  1803     // Check that aMaxResults is reasonable (100 is an arbitrary limit)
       
  1804     if ( aMaxResults < 0 || aMaxResults > KMaxResults )
       
  1805         {
       
  1806         return EVasInvalidParameter; 
       
  1807         }
       
  1808     iMaxResults = aMaxResults;
       
  1809     
       
  1810     if ( !iSrsApi )
       
  1811         {		
       
  1812         // Put a trap in case instantiation of SRF leaves
       
  1813         TRAPD( err, CreateSrfL() );
       
  1814         if ( err != KErrNone )
       
  1815             {
       
  1816             RUBY_DEBUG0( "CNssRecognitionHandler::RecognizeInitL - create SRS fail" );
       
  1817             
       
  1818             iSrsApi = NULL;     // For clean up purpose
       
  1819             
       
  1820             // Notify P&S
       
  1821             RProperty::Set( KSINDUID, ERecognitionState, ERecognitionFail ); 
       
  1822             
       
  1823             Cleanup();
       
  1824             return EVasRecognitionInitFailed;
       
  1825             }
       
  1826         }
       
  1827     
       
  1828     // Set audio priority. If some other application wants to play/record
       
  1829     // while we are recognizing, these priorities determine who gets the access.
       
  1830     iSrsApi->SetAudioPriority( KAudioPriorityVoiceDial, 
       
  1831                                KAudioPrefVocosTrain, 
       
  1832                                KAudioPrefVocosPlayback, 
       
  1833                                KAudioPrefVocosRecog );
       
  1834     
       
  1835     // speaker independent mode recognition session
       
  1836     TInt errorCode = iSrsApi->StartRecSession( ENSSSiMode );
       
  1837     
       
  1838     if ( errorCode != KErrNone )
       
  1839         {
       
  1840         RUBY_DEBUG1( "CNssRecognitionHandler: StartRecSession Err = %d", errorCode );
       
  1841 
       
  1842         // Notify P&S
       
  1843         RProperty::Set( KSINDUID, ERecognitionState, ERecognitionFail ); 
       
  1844         
       
  1845         Cleanup();
       
  1846         return EVasRecognitionInitFailed;
       
  1847         }
       
  1848     
       
  1849     // Create the structure for recognition results
       
  1850     if ( !iSIClientResultSet )
       
  1851         {
       
  1852         iSIClientResultSet = CSIClientResultSet::NewL();
       
  1853         }
       
  1854     iSIClientResultSet->SetMaxResultsL( iMaxResults );
       
  1855     
       
  1856     return EVasErrorNone;
       
  1857     }
       
  1858 
       
  1859 // End of file