srsf/sisrscontrollerplugin/src/sicontrollerplugin.cpp
changeset 0 bf1d17376201
child 1 b13cd05eeb2f
equal deleted inserted replaced
-1:000000000000 0:bf1d17376201
       
     1 /*
       
     2 * Copyright (c) 2004-2008 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:  This is the main implementation of the SI Controller Plugin.
       
    15 *               Finite State Machine is implemented here.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <mmfcodec.h>
       
    22 #include <nsssispeechrecognitiondatadevasr.h>
       
    23 
       
    24 #include <badesca.h>
       
    25 #include <mmfbase.h>
       
    26 
       
    27 #include "sicontrollerplugin.h"
       
    28 #include "sicontrollerplugininterface.h"
       
    29 #include "sidatabase.h"
       
    30 
       
    31 #include "siresourcehandler.h"
       
    32 #include "asrplugindataloader.h"
       
    33 
       
    34 #include "sigrammardb.h"
       
    35 #include "silexicondb.h"
       
    36 #include "simodelbankdb.h"
       
    37 
       
    38 #include "rubydebug.h"
       
    39  
       
    40 #ifdef __SINDE_TRAINING
       
    41 #include "sindetraining.h"
       
    42 #endif // __SINDE_TRAINING
       
    43  
       
    44 // Package types for general & language specific TTP data
       
    45  
       
    46 // CONSTANTS
       
    47  
       
    48 // Message types used in RunL
       
    49 //const TInt KAddPronunciation			= 1; // Commented out to remove "not referenced" warning
       
    50 const TInt KAddRule						= 2;
       
    51 const TInt KCreateGrammar				= 3;
       
    52 const TInt KCreateLexicon				= 4;
       
    53 const TInt KCreateModelBank				= 5;
       
    54 const TInt KGetAllClientGrammarIDs		= 6;
       
    55 const TInt KGetAllClientLexiconIDs		= 7;
       
    56 const TInt KGetAllClientModelBankIDs	= 8;
       
    57 const TInt KGetAllGrammarIDs			= 9;
       
    58 const TInt KGetAllLexiconIDs			= 10;
       
    59 const TInt KGetAllModelBankIDs			= 11;
       
    60 const TInt KGetAllModelIDs				= 12;
       
    61 const TInt KGetAllPronunciationIDs		= 13;
       
    62 const TInt KGetAllRuleIDs				= 14;
       
    63 const TInt KGetAvailableStorage			= 15;
       
    64 const TInt KGetModelCount				= 16;
       
    65 const TInt KGetRuleValidity				= 17;
       
    66 const TInt KGetUtteranceDuration		= 18;
       
    67 const TInt KLoadGrammar					= 19;
       
    68 const TInt KLoadLexicon					= 20;
       
    69 const TInt KLoadModels					= 21;
       
    70 const TInt KPlayUtterance				= 22;
       
    71 const TInt KRecognize					= 23;
       
    72 const TInt KRecord						= 24;
       
    73 const TInt KRemoveGrammar				= 25;
       
    74 const TInt KRemoveLexicon				= 26;
       
    75 const TInt KRemoveModelBank				= 27;
       
    76 const TInt KRemoveModel					= 28;
       
    77 const TInt KRemovePronunciation			= 29;
       
    78 const TInt KRemoveRule					= 30;
       
    79 const TInt KTrain						= 31;
       
    80 const TInt KUnloadRule					= 32;
       
    81 
       
    82 // SI only functionalities
       
    83 const TInt KAdapt					= 33;
       
    84 //const TInt KSIAddPronunciation				= 34; // Commented out to remove "not referenced" warning
       
    85 const TInt KAddRuleVariant				= 35;
       
    86 //const TInt KAddVoiceTag					= 36; // Commented out to remove "not referenced" warning
       
    87 const TInt KAddVoiceTags				= 37;
       
    88 const TInt KCreateRule					= 38;
       
    89 //const TInt KSIRecognize					= 39; // Commented out to remove "not referenced" warning
       
    90 const TInt KEndRecord					= 40;
       
    91 const TInt KUnloadGrammar 				= 41;
       
    92 //const TInt KUpdateGrammarAndLexicon 				= 42; // Commented out to remove "not referenced" warning
       
    93 const TInt KGetPronunciationCount				= 43;
       
    94 const TInt KGetRuleCount				= 44;
       
    95 const TInt KRemoveRules					= 45;
       
    96  
       
    97 //const TInt KAddSIPronunciation			= 43; // Commented out to remove "not referenced" warning
       
    98 //const TInt KAddOneSIPronunciation	= 44; // Commented out to remove "not referenced" warning
       
    99 
       
   100 #ifdef __SINDE_TRAINING
       
   101 // SINDE voice tags creation
       
   102 const TInt KAddSindeVoiceTags = 46;
       
   103 //const TInt KAddSindeVoiceTag = 47; // Commented out to remove "not referenced" warning
       
   104 #endif // __SINDE_TRAINING
       
   105 
       
   106 const TInt KPreStartSampling = 48;
       
   107 
       
   108 // Define this if SINDE lexicon optimization is on
       
   109 #define __SIND_LEXICON_OPT
       
   110 
       
   111 // Secure ID of voice commands application process
       
   112 _LIT_SECURE_ID( KVoiceCommandsAppUid, 0x101f8555 );
       
   113 
       
   114 // ============================= LOCAL FUNCTIONS ===============================
       
   115 
       
   116 
       
   117 // -----------------------------------------------------------------------------
       
   118 // Panic
       
   119 // User panic when an unrecoverable error occurs.
       
   120 // Returns: -
       
   121 // -----------------------------------------------------------------------------
       
   122 //
       
   123 void Panic( TInt aPanicCode ) // Panic code
       
   124     {
       
   125     _LIT( KSDControllerPanicCategory, "SIControllerPlugin" );
       
   126     User::Panic( KSDControllerPanicCategory, aPanicCode );
       
   127     }
       
   128 
       
   129 // ============================ MEMBER FUNCTIONS ===============================
       
   130 
       
   131 // -----------------------------------------------------------------------------
       
   132 // CSIControllerPlugin::CSIControllerPlugin
       
   133 // C++ default constructor can NOT contain any code, that might leave.
       
   134 // -----------------------------------------------------------------------------
       
   135 //
       
   136 CSIControllerPlugin::CSIControllerPlugin( CSIControllerPluginInterface& aControllerIf )
       
   137                                         : CActive( EPriorityLess ),
       
   138                                           iState( ESiPluginIdle ),
       
   139                                           iControllerIf( aControllerIf ),
       
   140                                           iClientRegistered( EFalse ),  
       
   141                                           iGrammarID( 0 ),
       
   142                                           iDataLoader( NULL )
       
   143     {
       
   144     RUBY_DEBUG0( "CSIControllerPlugin::CSIControllerPlugin" );
       
   145     }
       
   146 
       
   147 // -----------------------------------------------------------------------------
       
   148 // CSIControllerPlugin::ConstructL
       
   149 // Symbian 2nd phase constructor can leave.
       
   150 // -----------------------------------------------------------------------------
       
   151 // 
       
   152 void CSIControllerPlugin::ConstructL()
       
   153     {
       
   154     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::ConstructL" );
       
   155     
       
   156     iResourceHandler = CSIResourceHandler::NewL();
       
   157     
       
   158     //  Data loader
       
   159     iDataLoader = CDataLoader::NewL( *(iResourceHandler->iDataFilenamePrefix), 
       
   160         *(iResourceHandler->iDataFilenameSeperator),
       
   161         *(iResourceHandler->iDataFilenamePostfix ) );
       
   162     
       
   163     TFileName dbFileName( *(iResourceHandler->iDbFileName ));
       
   164     
       
   165     // Database instances
       
   166     iSIDatabase = CSIDatabase::NewL( dbFileName );
       
   167     RDbNamedDatabase& database = iSIDatabase->SIDatabase();
       
   168     RDbs& dbSession = iSIDatabase->DbSession();
       
   169     TInt drive = iSIDatabase->DbDrive();
       
   170     iSIGrammarDB = CSIGrammarDB::NewL( database, dbSession, drive );
       
   171     iSILexiconDB = CSILexiconDB::NewL( database, dbSession, drive );
       
   172     iSIModelBankDB = CSIModelBankDB::NewL( database, dbSession, drive );
       
   173 
       
   174     iSIDatabase->BeginTransactionL();    
       
   175     
       
   176     if ( !( iSIDatabase->DoesLockTableExistL() ) )
       
   177         {
       
   178         iSIDatabase->CreateLockTableL();
       
   179         }
       
   180 
       
   181     // Need to create all 3 ID tables at single transaction
       
   182 
       
   183 
       
   184     // Create tables if they don't already exist    
       
   185     if ( !( iSIDatabase->DoesModelBankTableExistL() ) )
       
   186         { 
       
   187         iSIModelBankDB->CreateIDTableL();
       
   188         }
       
   189 
       
   190     if ( !( iSIDatabase->DoesLexiconTableExistL() ) )
       
   191         { 
       
   192         iSILexiconDB->CreateIDTableL();
       
   193         }
       
   194 
       
   195     if ( !( iSIDatabase->DoesGrammarTableExistL() ) )
       
   196         { 
       
   197         iSIGrammarDB->CreateIDTableL();
       
   198         }
       
   199 
       
   200     iSIDatabase->CommitChangesL( ETrue );
       
   201 
       
   202 
       
   203     iSITtpWordList = CSITtpWordList::NewL();
       
   204     
       
   205     iDevASR = CDevASR::NewL( *this );
       
   206     
       
   207     RUBY_DEBUG2( "[%d] CSIControllerPlugin::ConstructL - DevASR[%d]", this, iDevASR );
       
   208     
       
   209 #ifdef __SINDE_TRAINING 		
       
   210     iSindeTrainer = CSindeTrainer::NewL( *iDevASR, iControllerIf, *this, 
       
   211                                          *iSIDatabase, *iSIGrammarDB, *iSILexiconDB );
       
   212 #endif // __SINDE_TRAINING    
       
   213     
       
   214     // Add active object to active scheduler
       
   215     CActiveScheduler::Add( this );
       
   216     }
       
   217 
       
   218 // -----------------------------------------------------------------------------
       
   219 // CSIControllerPlugin::NewL
       
   220 // Two-phased constructor.
       
   221 // -----------------------------------------------------------------------------
       
   222 //
       
   223 CSIControllerPlugin* CSIControllerPlugin::NewL( CSIControllerPluginInterface& aControllerIf )
       
   224     {
       
   225     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::NewL" );
       
   226     CSIControllerPlugin* self = new( ELeave ) CSIControllerPlugin( aControllerIf );
       
   227     CleanupStack::PushL( self );
       
   228     self->ConstructL();
       
   229     CleanupStack::Pop();
       
   230     return self;
       
   231     }
       
   232 
       
   233 // -----------------------------------------------------------------------------
       
   234 // CSIControllerPlugin::~CSIControllerPlugin
       
   235 // Destructor
       
   236 // -----------------------------------------------------------------------------
       
   237 //
       
   238 CSIControllerPlugin::~CSIControllerPlugin()
       
   239     {
       
   240 	RUBY_DEBUG0( "CSIControllerPlugin::~CSIControllerPlugin" );
       
   241 
       
   242     RThread().SetPriority( EPriorityNormal );
       
   243 
       
   244     Cancel();
       
   245     
       
   246     // if there is any outstanding request, cancel it.
       
   247     if ( iDevASR )
       
   248         {
       
   249         iDevASR->Cancel();
       
   250         }
       
   251     
       
   252     delete iDevASR;
       
   253     delete iResourceHandler;
       
   254     delete iSIGrammarDB;
       
   255     delete iSILexiconDB; // Handled by Grcompiler
       
   256     delete iSIModelBankDB;	
       
   257     delete iDataLoader;
       
   258     
       
   259     delete iSIDatabase;
       
   260     delete iSICompiledGrammarRecompile;
       
   261     
       
   262 	iMaxNPronunsForWord.Close();
       
   263     delete iSITtpWordList;
       
   264     
       
   265 #ifdef __SINDE_TRAINING 		
       
   266     delete iSindeTrainer;
       
   267 #endif // __SINDE_TRAINING
       
   268 
       
   269     if ( iTrainArrays != NULL )
       
   270         {
       
   271         iTrainArrays->ResetAndDestroy();
       
   272         iTrainArrays->Close();
       
   273         delete iTrainArrays;
       
   274         }
       
   275     }
       
   276 
       
   277 // -----------------------------------------------------------------------------
       
   278 // CSIControllerPlugin::AddPronunciationL
       
   279 // Calls the lexicon database handler to add a new pronunciation into the lexicon.
       
   280 // -----------------------------------------------------------------------------
       
   281 //
       
   282 void CSIControllerPlugin::AddPronunciationL( TSILexiconID /*aLexiconID*/,
       
   283                                              TSIModelBankID /*aModelBankID*/,
       
   284                                              const TDesC8& /*aPronunciationPr*/,
       
   285                                              TSIPronunciationID& /*aPronunciationID*/ )
       
   286     {
       
   287     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::AddPronunciationL 1" );
       
   288     User::Leave( KErrNotSupported );
       
   289     }
       
   290 
       
   291 
       
   292 // -----------------------------------------------------------------------------
       
   293 // CSIControllerPlugin::AddPronunciationL
       
   294 // Calls the lexicon database handler to add a new pronunciation into the lexicon.
       
   295 // -----------------------------------------------------------------------------
       
   296 // 
       
   297 void CSIControllerPlugin::AddPronunciationL( TSILexiconID /*aLexiconID*/,
       
   298                                              TSIModelBankID /*aModelBankID*/,
       
   299                                              TSIModelID /*aModelID*/, 
       
   300                                              TSIPronunciationID& /*aPronunciationID*/ )
       
   301     {
       
   302     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::AddPronunciationL 2" );
       
   303     User::Leave( KErrNotSupported );	
       
   304     }
       
   305 
       
   306 // -----------------------------------------------------------------------------
       
   307 // CSIControllerPlugin::SDLexiconL
       
   308 // -----------------------------------------------------------------------------
       
   309 //   
       
   310 #ifdef DEVASR_KEEP_SD
       
   311 CSDLexicon* CSIControllerPlugin::SDLexiconL( TSILexiconID anID )
       
   312     {
       
   313     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::SDLexiconL" );
       
   314     
       
   315     User::Leave( KErrNotSupported );
       
   316     return NULL;	
       
   317     }
       
   318 #endif
       
   319 
       
   320 // -----------------------------------------------------------------------------
       
   321 // CSIControllerPlugin::AddRuleL
       
   322 // Calls the grammar database handler to add a new rule into the grammar.
       
   323 // -----------------------------------------------------------------------------
       
   324 //
       
   325 void CSIControllerPlugin::AddRuleL( TSIGrammarID aGrammarID,
       
   326                                     TSILexiconID aLexiconID,
       
   327                                     TSIPronunciationID aPronunciationID,
       
   328                                     TSIRuleID& aRuleID )
       
   329     {
       
   330     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::AddRuleL" );
       
   331     
       
   332     if ( IsActive() )
       
   333         {
       
   334         User::Leave( KErrServerBusy );
       
   335         }
       
   336     iRequestFunction = KAddRule;
       
   337     iGrammarID = aGrammarID;
       
   338     iLexiconID = aLexiconID;
       
   339     iPronunciationID = aPronunciationID;
       
   340     iRuleIDPtr = &aRuleID;
       
   341     DoAsynch();
       
   342     }
       
   343 
       
   344 // -----------------------------------------------------------------------------
       
   345 // CSIControllerPlugin::HandleAddRule
       
   346 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
   347 // -----------------------------------------------------------------------------
       
   348 //
       
   349 void CSIControllerPlugin::HandleAddRuleL( TSIGrammarID /*aGrammarID*/,
       
   350                                           TSILexiconID /*aLexiconID*/,
       
   351                                           TSIPronunciationID /*aPronunciationID*/,
       
   352                                           TSIRuleID& /*aRuleID*/ )
       
   353     {
       
   354     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleAddRuleL" );
       
   355     
       
   356     User::Leave( KErrNotSupported ); 
       
   357     }
       
   358 
       
   359 // -----------------------------------------------------------------------------
       
   360 // CSIControllerPlugin::Cancel
       
   361 // Cancels the current activity and returns the plugin to Idle state.
       
   362 // -----------------------------------------------------------------------------
       
   363 //
       
   364 void CSIControllerPlugin::Cancel()
       
   365     {
       
   366     RUBY_DEBUG1( "CSIControllerPlugin::Cancel (State=%d)", iState );
       
   367     
       
   368     this->iSIDatabase->Rollback();
       
   369     switch ( iState )
       
   370         {
       
   371         case ESdPluginTrain:
       
   372             iDevASR->Cancel();
       
   373             
       
   374             TRAP_IGNORE( 
       
   375                 iSIDatabase->BeginTransactionL();
       
   376                 iSIModelBankDB->Reset();
       
   377                 iSIDatabase->CommitChangesL( ETrue );
       
   378                 ); // TRAP_IGNORE
       
   379                
       
   380             break;
       
   381             
       
   382         case ESiPluginRecognize:
       
   383             iDevASR->Cancel();
       
   384             iState = ESiPluginRecognize; 	
       
   385             break;
       
   386         case ESiPluginTrainText:
       
   387 #ifdef __SINDE_TRAINING
       
   388         case ESiPluginTrainSinde:
       
   389 #endif // __SINDE_TRAINING
       
   390             iDevASR->Cancel();
       
   391             iState = ESiPluginIdle; 	
       
   392             break;
       
   393         case ESiPluginPlay:
       
   394             iDevASR->Cancel();
       
   395             break;
       
   396             
       
   397         case ESiPluginIdle:
       
   398             // Nothing to cancel
       
   399             break;
       
   400             
       
   401         default:
       
   402             // Unknown state - should never be here
       
   403             break;
       
   404         }
       
   405     
       
   406     // Cancel the active object
       
   407     CActive::Cancel();
       
   408     }
       
   409 
       
   410 // -----------------------------------------------------------------------------
       
   411 // CSIControllerPlugin::CommitChangesL
       
   412 // Commits (saves) all database changes since the last commit request.
       
   413 // -----------------------------------------------------------------------------
       
   414 //
       
   415 void CSIControllerPlugin::CommitChangesL( TBool aCommit )
       
   416     {
       
   417     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::CommitChangesL" );
       
   418     iSIDatabase->CommitChangesL( aCommit );
       
   419     }
       
   420 
       
   421 // -----------------------------------------------------------------------------
       
   422 // CSIControllerPlugin::CreateGrammarL
       
   423 // Calls the grammar database handler to create a new grammar.
       
   424 // -----------------------------------------------------------------------------
       
   425 //
       
   426 void CSIControllerPlugin::CreateGrammarL( TSIGrammarID& aGrammarID )
       
   427     {
       
   428     RUBY_DEBUG_BLOCK("CSIControllerPlugin::CreateGrammarL");
       
   429     
       
   430     if ( IsActive() )
       
   431         {
       
   432         User::Leave( KErrServerBusy );
       
   433         }
       
   434     iRequestFunction = KCreateGrammar;
       
   435     iGrammarIDPtr = &aGrammarID;
       
   436     DoAsynch();
       
   437     }
       
   438 
       
   439 // -----------------------------------------------------------------------------
       
   440 // CSIControllerPlugin::HandleCreateGrammarL
       
   441 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
   442 // -----------------------------------------------------------------------------
       
   443 //
       
   444 void CSIControllerPlugin::HandleCreateGrammarL( TSIGrammarID& aGrammarID )
       
   445     {
       
   446     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleCreateGrammarL" );
       
   447     
       
   448     if ( iClientRegistered )
       
   449         {
       
   450         iSIDatabase->BeginTransactionL();
       
   451         
       
   452         // update model banks if they are not valid anymore
       
   453         RArray<TSIModelBankID> modelBankIDs;
       
   454         CleanupClosePushL( modelBankIDs );
       
   455         iSIModelBankDB->GetAllClientModelBankIDsL( iClientUid, modelBankIDs );
       
   456         for ( TInt i(0); i < modelBankIDs.Count(); i++ )
       
   457             {
       
   458             iSIModelBankDB->UpdateModelBankIfInvalidL( modelBankIDs[i] );
       
   459             }
       
   460         CleanupStack::PopAndDestroy( &modelBankIDs );
       
   461         aGrammarID = iSIGrammarDB->CreateGrammarL(iClientUid);
       
   462         iSIDatabase->CommitChangesL( ETrue );
       
   463         }
       
   464     else
       
   465         {
       
   466         User::Leave( KErrAsrNotRegisted );
       
   467         }
       
   468     }
       
   469 
       
   470 // -----------------------------------------------------------------------------
       
   471 // CSIControllerPlugin::CreateLexiconL
       
   472 // Calls the lexicon database handler to create a new lexicon.
       
   473 // -----------------------------------------------------------------------------
       
   474 //
       
   475 void CSIControllerPlugin::CreateLexiconL( TSILexiconID& aLexiconID )
       
   476     {
       
   477     RUBY_DEBUG_BLOCK("CSIControllerPlugin::CreateLexiconL");
       
   478     
       
   479     if ( IsActive() )
       
   480         {
       
   481         User::Leave( KErrServerBusy );
       
   482         }
       
   483     iRequestFunction = KCreateLexicon;
       
   484     iLexiconIDPtr = &aLexiconID;
       
   485     DoAsynch();
       
   486     }
       
   487 
       
   488 // -----------------------------------------------------------------------------
       
   489 // CSIControllerPlugin::HandleCreateLexiconL
       
   490 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
   491 // -----------------------------------------------------------------------------
       
   492 //
       
   493 void CSIControllerPlugin::HandleCreateLexiconL( TSILexiconID& aLexiconID )
       
   494     {
       
   495     RUBY_DEBUG_BLOCK("CSIControllerPlugin::HandleCreateLexiconL");
       
   496     
       
   497     if ( iClientRegistered )
       
   498         {
       
   499         iSIDatabase->BeginTransactionL();
       
   500         aLexiconID = iSILexiconDB->CreateLexiconL(iClientUid);
       
   501         iSIDatabase->CommitChangesL( ETrue );
       
   502         }
       
   503     else
       
   504         {
       
   505         User::Leave( KErrAsrNotRegisted );
       
   506         }
       
   507     }
       
   508 
       
   509 // -----------------------------------------------------------------------------
       
   510 // CSIControllerPlugin::CreateModelBankL
       
   511 // Calls the model database handler to create a new model bank.
       
   512 // -----------------------------------------------------------------------------
       
   513 //
       
   514 void CSIControllerPlugin::CreateModelBankL( TSIModelBankID& aModelBankID )
       
   515     {
       
   516     RUBY_DEBUG_BLOCK("CSIControllerPlugin::CreateModelBankL");
       
   517     
       
   518     if ( IsActive() )
       
   519         {
       
   520         User::Leave(KErrServerBusy);
       
   521         }
       
   522     iRequestFunction = KCreateModelBank;
       
   523     iModelBankIDPtr = &aModelBankID;
       
   524     DoAsynch();
       
   525     }
       
   526 
       
   527 // -----------------------------------------------------------------------------
       
   528 // CSIControllerPlugin::HandleCreateModelBankL
       
   529 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
   530 // -----------------------------------------------------------------------------
       
   531 //
       
   532 void CSIControllerPlugin::HandleCreateModelBankL( TSIModelBankID& aModelBankID )
       
   533     {
       
   534     RUBY_DEBUG_BLOCK("CSIControllerPlugin::HandleCreateModelBankL");
       
   535     
       
   536     if ( iClientRegistered )
       
   537         {
       
   538         iSIDatabase->BeginTransactionL();
       
   539         aModelBankID = iSIModelBankDB->CreateModelBankL(iClientUid);
       
   540         iSIDatabase->CommitChangesL( ETrue ); 
       
   541         }
       
   542     else
       
   543         {
       
   544         User::Leave(KErrAsrNotRegisted);
       
   545         }
       
   546     }
       
   547 
       
   548 // -----------------------------------------------------------------------------
       
   549 // CSIControllerPlugin::EndRecSessionL
       
   550 // Ends recognition session and releases resources.  If not in recognition state,
       
   551 // no action is taken.
       
   552 // -----------------------------------------------------------------------------
       
   553 //
       
   554 void CSIControllerPlugin::EndRecSessionL()
       
   555     {
       
   556     RUBY_DEBUG_BLOCK("CSIControllerPlugin::EndRecSessionL");
       
   557     
       
   558     switch ( iState )
       
   559         {
       
   560         case ESiPluginRecognize:
       
   561             iDevASR->EndRecSession();
       
   562             // Save the changes during model adaption.
       
   563             iSIDatabase->CommitChangesL( ETrue );  
       
   564             RecognitionReset();
       
   565             break;
       
   566         case ESiPluginIdle:
       
   567             // Ignore if already Idle
       
   568             break;
       
   569         default:
       
   570             // Wrong state
       
   571             User::Leave( KErrNotReady );
       
   572             break;
       
   573         }
       
   574     }
       
   575 
       
   576 // -----------------------------------------------------------------------------
       
   577 // CSIControllerPlugin::ActivateGrammarL
       
   578 // Activate the grammar for the recognition
       
   579 // -----------------------------------------------------------------------------
       
   580 //
       
   581 void CSIControllerPlugin::ActivateGrammarL( TSIGrammarID aGrammarID ) 
       
   582     {
       
   583     RUBY_DEBUG_BLOCK("CSIControllerPlugin::ActivateGrammarL");
       
   584     iDevASR->ActivateGrammarL(aGrammarID);
       
   585     }
       
   586 
       
   587 
       
   588 // -----------------------------------------------------------------------------
       
   589 // CSIControllerPlugin::DeactivateGrammarL
       
   590 // Deactivate the grammar for the recognition 
       
   591 // -----------------------------------------------------------------------------
       
   592 //
       
   593 void CSIControllerPlugin::DeactivateGrammarL(TSIGrammarID aGrammarID ) 
       
   594     {
       
   595     RUBY_DEBUG_BLOCK("CSIControllerPlugin::DeactivateGrammarL");
       
   596     iDevASR->DeactivateGrammarL(aGrammarID);
       
   597     }
       
   598 
       
   599 
       
   600 // -----------------------------------------------------------------------------
       
   601 // CSIControllerPlugin::GetAllClientGrammarIDsL
       
   602 // Returns all grammar Ids that belong to the current client.
       
   603 // -----------------------------------------------------------------------------
       
   604 //
       
   605 void CSIControllerPlugin::GetAllClientGrammarIDsL( RArray<TSIGrammarID>& aGrammarIDs )
       
   606     {
       
   607     RUBY_DEBUG_BLOCK("CSIControllerPlugin::GetAllClientGrammarIDsL");
       
   608     
       
   609     if ( IsActive() )
       
   610         {
       
   611         User::Leave( KErrServerBusy );
       
   612         }
       
   613     iRequestFunction = KGetAllClientGrammarIDs;
       
   614     
       
   615     iGrammarIDs = &aGrammarIDs;
       
   616     
       
   617     DoAsynch();
       
   618     }
       
   619 
       
   620 // -----------------------------------------------------------------------------
       
   621 // CSIControllerPlugin::HandleGetAllClientGrammarIDsL
       
   622 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
   623 // -----------------------------------------------------------------------------
       
   624 //
       
   625 void CSIControllerPlugin::HandleGetAllClientGrammarIDsL( RArray<TSIGrammarID>& aGrammarIDs )
       
   626     {
       
   627     RUBY_DEBUG_BLOCK("CSIControllerPlugin::HandleGetAllClientGrammarIDsL");
       
   628     
       
   629     if ( iClientRegistered )
       
   630         {
       
   631         iSIDatabase->BeginTransactionL();
       
   632         iSIGrammarDB->GetAllClientGrammarIDsL(iClientUid, aGrammarIDs);
       
   633         iSIDatabase->CommitChangesL( EFalse );
       
   634         
       
   635         }
       
   636     else
       
   637         {
       
   638         User::Leave( KErrAsrNotRegisted );
       
   639         }
       
   640     }
       
   641 
       
   642 // -----------------------------------------------------------------------------
       
   643 // CSIControllerPlugin::GetAllClientLexiconIDsL
       
   644 // Returns all lexicon Ids that belong to the current client.
       
   645 // -----------------------------------------------------------------------------
       
   646 //
       
   647 void CSIControllerPlugin::GetAllClientLexiconIDsL( RArray<TSILexiconID>& aLexiconIDs )
       
   648     {
       
   649     RUBY_DEBUG_BLOCK("CSIControllerPlugin::GetAllClientLexiconIDsL");
       
   650     
       
   651     if ( IsActive() )
       
   652         {
       
   653         User::Leave( KErrServerBusy );
       
   654         }
       
   655     iRequestFunction = KGetAllClientLexiconIDs;
       
   656     iLexiconIDs = &aLexiconIDs;
       
   657     DoAsynch();
       
   658     }
       
   659 
       
   660 // -----------------------------------------------------------------------------
       
   661 // CSIControllerPlugin::HandleGetAllClientLexiconIDsL
       
   662 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
   663 // -----------------------------------------------------------------------------
       
   664 //
       
   665 void CSIControllerPlugin::HandleGetAllClientLexiconIDsL( RArray<TSILexiconID>& aLexiconIDs )
       
   666     {
       
   667     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleGetAllClientLexiconIDsL" );
       
   668     
       
   669     if ( iClientRegistered )
       
   670         {
       
   671         iSIDatabase->BeginTransactionL();
       
   672         iSILexiconDB->GetAllClientLexiconIDsL( iClientUid, aLexiconIDs );		
       
   673         iSIDatabase->CommitChangesL( EFalse );
       
   674         }
       
   675     else
       
   676         {
       
   677         User::Leave( KErrAsrNotRegisted );
       
   678         }
       
   679     }
       
   680 
       
   681 // -----------------------------------------------------------------------------
       
   682 // CSIControllerPlugin::GetAllClientModelBankIDsL
       
   683 // Returns all model bank Ids that belong to the current client.
       
   684 // -----------------------------------------------------------------------------
       
   685 //
       
   686 void CSIControllerPlugin::GetAllClientModelBankIDsL( RArray<TSIModelBankID>& aModelBankIDs )
       
   687     {
       
   688     RUBY_DEBUG_BLOCK("CSIControllerPlugin::GetAllClientModelBankIDsL");
       
   689     
       
   690     if ( IsActive() )
       
   691         {
       
   692         User::Leave( KErrServerBusy );
       
   693         }
       
   694     iRequestFunction = KGetAllClientModelBankIDs;
       
   695     iModelBankIDs = &aModelBankIDs;
       
   696     DoAsynch();
       
   697     }
       
   698 
       
   699 // -----------------------------------------------------------------------------
       
   700 // CSIControllerPlugin::HandleGetAllClientModelBankIDsL
       
   701 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
   702 // -----------------------------------------------------------------------------
       
   703 //
       
   704 void CSIControllerPlugin::HandleGetAllClientModelBankIDsL( RArray<TSIModelBankID>& aModelBankIDs )
       
   705     {
       
   706     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleGetAllClientModelBankIDsL" );
       
   707     
       
   708     if ( iClientRegistered )
       
   709         {
       
   710         iSIDatabase->BeginTransactionL();
       
   711         iSIModelBankDB->GetAllClientModelBankIDsL( iClientUid, aModelBankIDs );
       
   712         iSIDatabase->CommitChangesL( EFalse );
       
   713         }
       
   714     else
       
   715         {
       
   716         User::Leave( KErrAsrNotRegisted );
       
   717         }
       
   718     }
       
   719 
       
   720 // -----------------------------------------------------------------------------
       
   721 // CSIControllerPlugin::GetAllGrammarIDsL
       
   722 // Returns all grammar Ids that exist (for all clients).
       
   723 // -----------------------------------------------------------------------------
       
   724 //
       
   725 void CSIControllerPlugin::GetAllGrammarIDsL( RArray<TSIGrammarID>& aGrammarIDs )
       
   726     {
       
   727     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::GetAllGrammarIDsL" );
       
   728     
       
   729     if ( IsActive() )
       
   730         {
       
   731         User::Leave( KErrServerBusy );
       
   732         }
       
   733     iRequestFunction = KGetAllGrammarIDs;
       
   734     iGrammarIDs = &aGrammarIDs;
       
   735     DoAsynch();
       
   736     }
       
   737 
       
   738 // -----------------------------------------------------------------------------
       
   739 // CSIControllerPlugin::HandleGetAllGrammarIDsL
       
   740 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
   741 // -----------------------------------------------------------------------------
       
   742 //
       
   743 void CSIControllerPlugin::HandleGetAllGrammarIDsL( RArray<TSIGrammarID>& aGrammarIDs )
       
   744     {
       
   745     RUBY_DEBUG_BLOCK("CSIControllerPlugin::HandleGetAllGrammarIDsL");
       
   746     
       
   747     iSIDatabase->BeginTransactionL();
       
   748     iSIGrammarDB->GetAllGrammarIDsL(aGrammarIDs);
       
   749     iSIDatabase->CommitChangesL( EFalse );
       
   750     }
       
   751 
       
   752 // -----------------------------------------------------------------------------
       
   753 // CSIControllerPlugin::GetAllLexiconIDsL
       
   754 // Returns all lexicon Ids that exist (for all clients).
       
   755 // -----------------------------------------------------------------------------
       
   756 //
       
   757 void CSIControllerPlugin::GetAllLexiconIDsL( RArray<TSILexiconID>& aLexiconIDs )
       
   758     {
       
   759     RUBY_DEBUG_BLOCK("CSIControllerPlugin::GetAllLexiconIDsL");
       
   760     
       
   761     if ( IsActive() )
       
   762         {
       
   763         User::Leave( KErrServerBusy );
       
   764         }
       
   765     iRequestFunction = KGetAllLexiconIDs;
       
   766     iLexiconIDs = &aLexiconIDs;
       
   767     DoAsynch();
       
   768     }
       
   769 
       
   770 // -----------------------------------------------------------------------------
       
   771 // CSIControllerPlugin::HandleGetAllLexiconIDsL
       
   772 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
   773 // -----------------------------------------------------------------------------
       
   774 //
       
   775 void CSIControllerPlugin::HandleGetAllLexiconIDsL( RArray<TSILexiconID>& aLexiconIDs )
       
   776     {
       
   777     RUBY_DEBUG_BLOCK("CSIControllerPlugin::HandleGetAllLexiconIDsL");
       
   778     
       
   779     iSIDatabase->BeginTransactionL();
       
   780     iSILexiconDB->GetAllLexiconIDsL(aLexiconIDs);
       
   781     iSIDatabase->CommitChangesL( EFalse );
       
   782     }
       
   783 
       
   784 // -----------------------------------------------------------------------------
       
   785 // CSIControllerPlugin::GetAllModelBankIDsL
       
   786 // Returns all model bank Ids that exist (for all clients).
       
   787 // -----------------------------------------------------------------------------
       
   788 //
       
   789 void CSIControllerPlugin::GetAllModelBankIDsL( RArray<TSIModelBankID>& aModelBankIDs )
       
   790     {
       
   791     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::GetAllModelBankIDsL" );
       
   792     
       
   793     if ( IsActive() )
       
   794         {
       
   795         User::Leave( KErrServerBusy );
       
   796         }
       
   797     iRequestFunction = KGetAllModelBankIDs;
       
   798     iModelBankIDs = &aModelBankIDs;
       
   799     DoAsynch();
       
   800     }
       
   801 
       
   802 // -----------------------------------------------------------------------------
       
   803 // CSIControllerPlugin::HandleGetAllModelBankIDsL
       
   804 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
   805 // -----------------------------------------------------------------------------
       
   806 //
       
   807 void CSIControllerPlugin::HandleGetAllModelBankIDsL( RArray<TSIModelBankID>& aModelBankIDs )
       
   808     {
       
   809     RUBY_DEBUG_BLOCK("CSIControllerPlugin::HandleGetAllModelBankIDsL");
       
   810     
       
   811     iSIDatabase->BeginTransactionL();
       
   812     iSIModelBankDB->GetAllModelBankIDsL( aModelBankIDs );
       
   813     iSIDatabase->CommitChangesL( EFalse );
       
   814     }
       
   815 
       
   816 // -----------------------------------------------------------------------------
       
   817 // CSIControllerPlugin::GetAllModelIDsL
       
   818 // Calls the model database handler to get all model IDs in the model bank.
       
   819 // -----------------------------------------------------------------------------
       
   820 //
       
   821 void CSIControllerPlugin::GetAllModelIDsL( TSIModelBankID aModelBankID,
       
   822                                            RArray<TSIModelID>& aModelIDs )
       
   823     { 
       
   824     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::GetAllModelIDsL" );
       
   825     
       
   826     if ( IsActive() )
       
   827         {
       
   828         User::Leave( KErrServerBusy );
       
   829         }
       
   830     iRequestFunction = KGetAllModelIDs;
       
   831     iModelBankID = aModelBankID;
       
   832     iModelIDs = &aModelIDs;
       
   833     DoAsynch();
       
   834     }
       
   835 
       
   836 // -----------------------------------------------------------------------------
       
   837 // CSIControllerPlugin::HandleGetAllModelIDsL
       
   838 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
   839 // -----------------------------------------------------------------------------
       
   840 //
       
   841 void CSIControllerPlugin::HandleGetAllModelIDsL( TSIModelBankID aModelBankID,
       
   842                                                  RArray<TSIModelID>& aModelIDs )
       
   843     {
       
   844     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleGetAllModelIDsL" );
       
   845     
       
   846     iSIDatabase->BeginTransactionL();
       
   847     iSIModelBankDB->GetAllModelIDsL(aModelBankID, aModelIDs);
       
   848     iSIDatabase->CommitChangesL( EFalse );
       
   849     }
       
   850 
       
   851 // -----------------------------------------------------------------------------
       
   852 // CSIControllerPlugin::GetAllPronunciationIDsL
       
   853 // Calls the lexicon database handler to get all pronunciation IDs in the lexicon.
       
   854 // -----------------------------------------------------------------------------
       
   855 //
       
   856 void CSIControllerPlugin::GetAllPronunciationIDsL( TSILexiconID aLexiconID,
       
   857                                                   RArray<TSIPronunciationID>& aPronunciationIDs )
       
   858     {
       
   859     RUBY_DEBUG_BLOCK("CSIControllerPlugin::GetAllPronunciationIDsL");
       
   860     
       
   861     if ( IsActive() )
       
   862         {
       
   863         User::Leave( KErrServerBusy );
       
   864         }
       
   865     iRequestFunction = KGetAllPronunciationIDs;
       
   866     iLexiconID = aLexiconID;
       
   867     iPronunciationIDs = &aPronunciationIDs;
       
   868     DoAsynch();
       
   869     }
       
   870 
       
   871 // -----------------------------------------------------------------------------
       
   872 // CSIControllerPlugin::HandleGetAllPronunciationIDsL
       
   873 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
   874 // -----------------------------------------------------------------------------
       
   875 //
       
   876 void CSIControllerPlugin::HandleGetAllPronunciationIDsL( TSILexiconID aLexiconID,
       
   877                                                          RArray<TSIPronunciationID>& aPronunciationIDs )
       
   878     {
       
   879     RUBY_DEBUG_BLOCK("CSIControllerPlugin::HandleGetAllPronunciationIDsL");
       
   880     
       
   881     iSIDatabase->BeginTransactionL();
       
   882     iSILexiconDB->GetAllPronunciationIDsL(aLexiconID, aPronunciationIDs);
       
   883     iSIDatabase->CommitChangesL( EFalse );
       
   884     }
       
   885 
       
   886 // -----------------------------------------------------------------------------
       
   887 // CSIControllerPlugin::GetAllRuleIDsL
       
   888 // Calls the grammar database handler to get all rule IDs in the grammar.
       
   889 // -----------------------------------------------------------------------------
       
   890 //
       
   891 void CSIControllerPlugin::GetAllRuleIDsL( TSIGrammarID aGrammarID,
       
   892                                           RArray<TSIRuleID>& aRuleIDs )
       
   893     {
       
   894     RUBY_DEBUG_BLOCK("CSIControllerPlugin::GetAllRuleIDsL");
       
   895     
       
   896     if ( IsActive() )
       
   897         {
       
   898         User::Leave( KErrServerBusy );
       
   899         }
       
   900     iRequestFunction = KGetAllRuleIDs;
       
   901     iGrammarID = aGrammarID;
       
   902     iRuleIDs = &aRuleIDs;
       
   903     DoAsynch();
       
   904     }
       
   905 
       
   906 // -----------------------------------------------------------------------------
       
   907 // CSIControllerPlugin::HandleGetAllRuleIDsL
       
   908 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
   909 // -----------------------------------------------------------------------------
       
   910 //
       
   911 void CSIControllerPlugin::HandleGetAllRuleIDsL( TSIGrammarID aGrammarID,
       
   912                                                 RArray<TSIRuleID>& aRuleIDs )
       
   913     {
       
   914     RUBY_DEBUG_BLOCK("CSIControllerPlugin::HandleGetAllRuleIDsL");
       
   915     
       
   916     iSIDatabase->BeginTransactionL();
       
   917     iSIGrammarDB->GetAllRuleIDsL(aGrammarID, aRuleIDs);
       
   918     iSIDatabase->CommitChangesL( EFalse );
       
   919     }
       
   920 
       
   921 // -----------------------------------------------------------------------------
       
   922 // CSIControllerPlugin::GetAvailableStorageL
       
   923 // Calculates the available number of models that can be trained by subtracting
       
   924 // the current total from the system defined max.
       
   925 // -----------------------------------------------------------------------------
       
   926 //
       
   927 void CSIControllerPlugin::GetAvailableStorageL( TInt& aCount )
       
   928     {
       
   929     RUBY_DEBUG_BLOCK("CSIControllerPlugin::GetAvailableStorageL");
       
   930     
       
   931     if ( IsActive() )
       
   932         {
       
   933         User::Leave( KErrServerBusy );
       
   934         }
       
   935     iRequestFunction = KGetAvailableStorage;
       
   936     iCountPtr = &aCount;
       
   937     DoAsynch();
       
   938     }
       
   939 
       
   940 // -----------------------------------------------------------------------------
       
   941 // CSIControllerPlugin::HandleGetAvailableStorageL
       
   942 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
   943 // -----------------------------------------------------------------------------
       
   944 //
       
   945 void CSIControllerPlugin::HandleGetAvailableStorageL( TInt& aCount )
       
   946     {
       
   947     RUBY_DEBUG_BLOCK("CSIControllerPlugin::HandleGetAvailableStorageL");
       
   948     
       
   949     iSIDatabase->BeginTransactionL();
       
   950     TInt count = iSIModelBankDB->AllModelCountL();
       
   951     iSIDatabase->CommitChangesL( EFalse );
       
   952     
       
   953     if ( count > iResourceHandler->iMaxModelsSystem )
       
   954         {
       
   955         aCount = 0;
       
   956         }
       
   957     else
       
   958         {
       
   959         aCount = iResourceHandler->iMaxModelsSystem - count;
       
   960         }
       
   961     }
       
   962 
       
   963 // -----------------------------------------------------------------------------
       
   964 // CSIControllerPlugin::GetEnginePropertiesL
       
   965 // Returns the current recognition engine properties.
       
   966 // -----------------------------------------------------------------------------
       
   967 //
       
   968 void CSIControllerPlugin::GetEnginePropertiesL( const RArray<TInt>& aPropertyId,
       
   969                                                 RArray<TInt>& aPropertyValue )
       
   970     {
       
   971     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::GetEnginePropertiesL" );
       
   972     
       
   973     for ( TInt index = 0; index < aPropertyId.Count(); index++ )
       
   974         {
       
   975         switch( aPropertyId[index] )
       
   976             {
       
   977             case KModelStorageCapacity:
       
   978                 aPropertyValue.Append( iResourceHandler->iMaxModelsSystem );
       
   979                 break;
       
   980                 
       
   981             case KMaxLoadableModels:
       
   982                 aPropertyValue.Append( iResourceHandler->iMaxModelsPerBank );
       
   983                 break;
       
   984                 
       
   985             default:
       
   986                 aPropertyValue.Append( KErrNotSupported );
       
   987                 break;
       
   988             }
       
   989         }
       
   990     
       
   991     iDevASR->GetEnginePropertiesL( aPropertyId, aPropertyValue );
       
   992     }
       
   993 
       
   994 // -----------------------------------------------------------------------------
       
   995 // CSIControllerPlugin::GetModelCountL
       
   996 // Calls the model database handler for the number of models in a model bank.
       
   997 // -----------------------------------------------------------------------------
       
   998 //
       
   999 void CSIControllerPlugin::GetModelCountL( TSIModelBankID aModelBankID,
       
  1000                                           TInt& aCount )
       
  1001     {
       
  1002     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::GetModelCountL" );
       
  1003     
       
  1004     if ( IsActive() )
       
  1005         {
       
  1006         User::Leave( KErrServerBusy );
       
  1007         }
       
  1008     iRequestFunction = KGetModelCount;
       
  1009     iModelBankID = aModelBankID;
       
  1010     iCountPtr = &aCount;
       
  1011     DoAsynch();
       
  1012     }
       
  1013 
       
  1014 // -----------------------------------------------------------------------------
       
  1015 // CSIControllerPlugin::HandleGetModelCountL
       
  1016 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
  1017 // -----------------------------------------------------------------------------
       
  1018 //
       
  1019 void CSIControllerPlugin::HandleGetModelCountL( TSIModelBankID aModelBankID,
       
  1020                                                 TInt& aCount )
       
  1021     {
       
  1022     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleGetModelCountL" );
       
  1023     
       
  1024     iSIDatabase->BeginTransactionL();
       
  1025     aCount = iSIModelBankDB->ModelCountL( aModelBankID );
       
  1026     iSIDatabase->CommitChangesL( EFalse );
       
  1027     }
       
  1028 
       
  1029 // -----------------------------------------------------------------------------
       
  1030 // CSIControllerPlugin::GetPronunciationCountL
       
  1031 // Calls the model database handler for the number of Pronunciation  in a lexicon
       
  1032 // -----------------------------------------------------------------------------
       
  1033 //
       
  1034 void CSIControllerPlugin::GetPronunciationCountL( TSILexiconID aLexiconID,
       
  1035                                                   TInt& aCount )
       
  1036     {
       
  1037     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::GetPronunciationCountL" );
       
  1038     
       
  1039     if ( IsActive() )
       
  1040         {
       
  1041         User::Leave( KErrServerBusy );
       
  1042         }
       
  1043     iRequestFunction = KGetPronunciationCount;
       
  1044     iLexiconID = aLexiconID;
       
  1045     iCountPtr = &aCount;
       
  1046     DoAsynch();
       
  1047     }
       
  1048 
       
  1049 // -----------------------------------------------------------------------------
       
  1050 // CSIControllerPlugin::HandleGetPronunciationCountL
       
  1051 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
  1052 // -----------------------------------------------------------------------------
       
  1053 //
       
  1054 void CSIControllerPlugin::HandleGetPronunciationCountL( TSIModelBankID aLexiconID,
       
  1055                                                         TInt& aCount )
       
  1056     {
       
  1057     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleGetPronunciationCountL" );
       
  1058     
       
  1059     iSIDatabase->BeginTransactionL();
       
  1060     aCount = iSILexiconDB->PronunciationCountL( aLexiconID );
       
  1061     iSIDatabase->CommitChangesL( EFalse );
       
  1062     }
       
  1063 
       
  1064 // -----------------------------------------------------------------------------
       
  1065 // CSIControllerPlugin::GetRuleCountL
       
  1066 // Calls the model database handler for the number of Pronunciation  in a lexicon
       
  1067 // -----------------------------------------------------------------------------
       
  1068 //
       
  1069 void CSIControllerPlugin::GetRuleCountL( TSIGrammarID aGrammarID,
       
  1070                                          TInt& aCount )
       
  1071     {
       
  1072     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::GetRuleCountL" );
       
  1073     
       
  1074     if ( IsActive() )
       
  1075         {
       
  1076         User::Leave( KErrServerBusy );
       
  1077         }
       
  1078     iRequestFunction = KGetRuleCount;
       
  1079     iGrammarID = aGrammarID;
       
  1080     iCountPtr = &aCount;
       
  1081     DoAsynch();
       
  1082     }
       
  1083 
       
  1084 // -----------------------------------------------------------------------------
       
  1085 // CSIControllerPlugin::HandleGetRuleCountL
       
  1086 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
  1087 // -----------------------------------------------------------------------------
       
  1088 //
       
  1089 void CSIControllerPlugin::HandleGetRuleCountL( TSIGrammarID aGrammarID,
       
  1090                                                TInt& aCount )
       
  1091     {
       
  1092     RUBY_DEBUG_BLOCK("CSIControllerPlugin::HandleGetRuleCountL");
       
  1093     
       
  1094     iSIDatabase->BeginTransactionL();
       
  1095     aCount = iSIGrammarDB->RuleCountL(aGrammarID);
       
  1096     iSIDatabase->CommitChangesL( EFalse );
       
  1097     }
       
  1098 
       
  1099 
       
  1100 
       
  1101 // -----------------------------------------------------------------------------
       
  1102 // CSIControllerPlugin::GetRuleValidityL
       
  1103 // Calls the grammar database handler to see if the rule exists in the specified
       
  1104 // grammar.
       
  1105 // (other items were commented in a header).
       
  1106 // -----------------------------------------------------------------------------
       
  1107 //
       
  1108 void CSIControllerPlugin::GetRuleValidityL( TSIGrammarID aGrammarID,
       
  1109                                             TSIRuleID aRuleID,
       
  1110                                             TBool& aValid )
       
  1111     {
       
  1112     RUBY_DEBUG_BLOCK("CSIControllerPlugin::GetRuleValidityL");
       
  1113     
       
  1114     if ( IsActive() )
       
  1115         {
       
  1116         User::Leave( KErrServerBusy );
       
  1117         }
       
  1118     iRequestFunction = KGetRuleValidity;
       
  1119     iGrammarID = aGrammarID;
       
  1120     iRuleID = aRuleID;
       
  1121     iValidPtr = &aValid;
       
  1122     DoAsynch();
       
  1123     }
       
  1124 
       
  1125 // -----------------------------------------------------------------------------
       
  1126 // CSIControllerPlugin::HandleGetRuleValidityL
       
  1127 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
  1128 // -----------------------------------------------------------------------------
       
  1129 //
       
  1130 void CSIControllerPlugin::HandleGetRuleValidityL( TSIGrammarID aGrammarID,
       
  1131                                                   TSIRuleID aRuleID,
       
  1132                                                   TBool& aValid )
       
  1133     {
       
  1134     RUBY_DEBUG_BLOCK("CSIControllerPlugin::HandleGetRuleValidityL");
       
  1135     
       
  1136     iSIDatabase->BeginTransactionL();
       
  1137     aValid = iSIGrammarDB->IsRuleValidL(aGrammarID, aRuleID);
       
  1138     iSIDatabase->CommitChangesL( EFalse );
       
  1139     }
       
  1140 
       
  1141 // -----------------------------------------------------------------------------
       
  1142 // CSIControllerPlugin::GetUtteranceDurationL
       
  1143 // Calls the model database handler to get the duration of an utterance.
       
  1144 // -----------------------------------------------------------------------------
       
  1145 //
       
  1146 void CSIControllerPlugin::GetUtteranceDurationL( TSIModelBankID aModelBankID,
       
  1147                                                  TSIModelID aModelID,
       
  1148                                                  TTimeIntervalMicroSeconds32& aDuration )
       
  1149     {
       
  1150     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::GetUtteranceDurationL" );
       
  1151     
       
  1152     if ( IsActive() )
       
  1153         {
       
  1154         User::Leave( KErrServerBusy );
       
  1155         }
       
  1156     iRequestFunction = KGetUtteranceDuration;
       
  1157     iModelBankID = aModelBankID;
       
  1158     iModelID = aModelID;
       
  1159     iDurationPtr = &aDuration;
       
  1160     DoAsynch();
       
  1161     }
       
  1162 
       
  1163 // -----------------------------------------------------------------------------
       
  1164 // CSIControllerPlugin::HandleGetUtteranceDurationL
       
  1165 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
  1166 // -----------------------------------------------------------------------------
       
  1167 //
       
  1168 void CSIControllerPlugin::HandleGetUtteranceDurationL( TSIModelBankID /*aModelBankID*/,
       
  1169                                                        TSIModelID /*aModelID*/,
       
  1170                                                        TTimeIntervalMicroSeconds32& /*aDuration*/ )
       
  1171     {
       
  1172     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleGetUtteranceDurationL" );
       
  1173     User::Leave( KErrNotSupported );		
       
  1174     }
       
  1175 
       
  1176 
       
  1177  // -----------------------------------------------------------------------------
       
  1178 // CSIControllerPlugin::HandlePlayUtteranceL
       
  1179 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
  1180 // -----------------------------------------------------------------------------
       
  1181 //
       
  1182 void CSIControllerPlugin::HandlePlayUtteranceL( TSIModelBankID /*aModelBankID*/,
       
  1183                                                 TSIModelID /*aModelID*/ )
       
  1184     {
       
  1185     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandlePlayUtteranceL" );
       
  1186     User::Leave( KErrNotSupported );		 
       
  1187     }
       
  1188 
       
  1189 // -----------------------------------------------------------------------------
       
  1190 // CSIControllerPlugin::LoadEngineParametersL
       
  1191 // Loads the specified recognizer parameter(s).  These parameters are used to
       
  1192 // alter the recognizer's default parameters.  The parameters are specified as
       
  1193 // attribute and value pairs.
       
  1194 // -----------------------------------------------------------------------------
       
  1195 //
       
  1196 void CSIControllerPlugin::LoadEngineParametersL( const RArray<TInt>& aParameterId,
       
  1197                                                  const RArray<TInt>& aParameterValue )
       
  1198     {
       
  1199     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::LoadEngineParametersL" );
       
  1200     iDevASR->LoadEngineParametersL( aParameterId, aParameterValue );
       
  1201     }
       
  1202 
       
  1203 // -----------------------------------------------------------------------------
       
  1204 // CSIControllerPlugin::LoadGrammarL
       
  1205 // Requests the grammar database handler for a grammar object to be loaded into
       
  1206 // the recognizer for recognition purpose.
       
  1207 // -----------------------------------------------------------------------------
       
  1208 //
       
  1209 void CSIControllerPlugin::LoadGrammarL( TSIGrammarID aGrammarID )
       
  1210     {
       
  1211     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::LoadGrammarL" );
       
  1212     
       
  1213     if ( IsActive() )
       
  1214         {
       
  1215         User::Leave( KErrServerBusy );
       
  1216         } 
       
  1217     
       
  1218     iRequestFunction = KLoadGrammar;
       
  1219     iGrammarID = aGrammarID;
       
  1220     DoAsynch();
       
  1221     }
       
  1222 
       
  1223 // -----------------------------------------------------------------------------
       
  1224 // CSIControllerPlugin::HandleLoadGrammarL
       
  1225 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
  1226 // -----------------------------------------------------------------------------
       
  1227 //
       
  1228 void CSIControllerPlugin::HandleLoadGrammarL( TSIGrammarID aGrammarID )
       
  1229     {
       
  1230     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleLoadGrammarL" );
       
  1231     
       
  1232     if ( iState == ESiPluginRecognize )
       
  1233         {
       
  1234         const CSICompiledGrammar* grammar = NULL;
       
  1235         // iGrammarDB keeps a reference to the grammar object so we don't
       
  1236         // have to push it onto a cleanupstack.
       
  1237         
       
  1238         iSIDatabase->BeginTransactionL();
       
  1239         grammar = iSIGrammarDB->LoadGrammarL(aGrammarID);
       
  1240         iSIDatabase->CommitChangesL( EFalse );
       
  1241         
       
  1242         iDevASR->LoadGrammar( *grammar );
       
  1243         }
       
  1244     else
       
  1245         {
       
  1246         User::Leave( KErrAsrInvalidState );
       
  1247         }
       
  1248     }
       
  1249 
       
  1250 // -----------------------------------------------------------------------------
       
  1251 // CSIControllerPlugin::LoadLexiconL
       
  1252 // Requests the lexicon database handler for a lexicon object to be loaded into
       
  1253 // the recognizer for recognition purpose.
       
  1254 // -----------------------------------------------------------------------------
       
  1255 //
       
  1256 void CSIControllerPlugin::LoadLexiconL( TSILexiconID aLexiconID )
       
  1257     {
       
  1258     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::LoadLexiconL" );
       
  1259     
       
  1260     if ( IsActive() )
       
  1261         {
       
  1262         User::Leave( KErrServerBusy );
       
  1263         }
       
  1264     iRequestFunction = KLoadLexicon;
       
  1265     iLexiconID = aLexiconID;
       
  1266     DoAsynch();
       
  1267     }
       
  1268 
       
  1269 // -----------------------------------------------------------------------------
       
  1270 // CSIControllerPlugin::HandleLoadLexiconL
       
  1271 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
  1272 // -----------------------------------------------------------------------------
       
  1273 //
       
  1274 void CSIControllerPlugin::HandleLoadLexiconL( TSILexiconID aLexiconID )
       
  1275     {
       
  1276     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleLoadLexiconL" );
       
  1277     
       
  1278     if ( iState == ESiPluginRecognize )
       
  1279         {
       
  1280         const CSILexicon* lexicon = NULL;
       
  1281         // iGrammarDB keeps a reference to the grammar object so we don't
       
  1282         // have to push it onto a cleanupstack.
       
  1283         
       
  1284         iSIDatabase->BeginTransactionL();
       
  1285         lexicon = iSILexiconDB->LexiconL( aLexiconID );
       
  1286         iSIDatabase->CommitChangesL( EFalse );
       
  1287         
       
  1288         iDevASR->LoadLexicon( *lexicon );
       
  1289         }
       
  1290     else
       
  1291         {
       
  1292         // Wrong state
       
  1293         User::Leave( KErrAsrInvalidState );
       
  1294         }
       
  1295     }
       
  1296 
       
  1297 // -----------------------------------------------------------------------------
       
  1298 // CSIControllerPlugin::LoadModelsL
       
  1299 // Requests the model database handler for a model bank object to be loaded into
       
  1300 // the recognizer for recognition purpose.
       
  1301 // -----------------------------------------------------------------------------
       
  1302 //
       
  1303 void CSIControllerPlugin::LoadModelsL( TSIModelBankID aModelBankID )
       
  1304     {
       
  1305     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::LoadModelsL" );
       
  1306     
       
  1307     if ( IsActive() )
       
  1308         {
       
  1309         User::Leave( KErrServerBusy );
       
  1310         }
       
  1311     iRequestFunction = KLoadModels;
       
  1312     iModelBankID = aModelBankID;
       
  1313     DoAsynch();
       
  1314     }
       
  1315 
       
  1316 // -----------------------------------------------------------------------------
       
  1317 // CSIControllerPlugin::HandleLoadModelsL
       
  1318 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
  1319 // -----------------------------------------------------------------------------
       
  1320 //
       
  1321 void CSIControllerPlugin::HandleLoadModelsL( TSIModelBankID aModelBankID )
       
  1322     {
       
  1323     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleLoadModelsL" );
       
  1324     
       
  1325     if ( iState == ESiPluginRecognize )
       
  1326         { 
       
  1327         
       
  1328         const CSIModelBank* modelBank = NULL;
       
  1329         // iModelBankDB keeps a reference to the modelBank object so we don't
       
  1330         // have to push it onto a cleanupstack.
       
  1331         iSIDatabase->BeginTransactionL();
       
  1332         modelBank = iSIModelBankDB->AllAcousticModelsL( aModelBankID );
       
  1333         iSIDatabase->CommitChangesL( EFalse );
       
  1334         
       
  1335         iDevASR->LoadModels( *modelBank );
       
  1336         
       
  1337         }
       
  1338     else
       
  1339         {
       
  1340         // Wrong state
       
  1341         User::Leave( KErrAsrInvalidState );
       
  1342         }
       
  1343     }
       
  1344 
       
  1345 // -----------------------------------------------------------------------------
       
  1346 // CSIControllerPlugin::PlayUtteranceL
       
  1347 // Plays the user utterance.
       
  1348 // -----------------------------------------------------------------------------
       
  1349 //
       
  1350 void CSIControllerPlugin::PlayUtteranceL( TSIModelBankID aModelBankID,
       
  1351                                          TSIModelID aModelID )
       
  1352     {
       
  1353     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::PlayUtteranceL" );
       
  1354     
       
  1355     if ( IsActive() )
       
  1356         {
       
  1357         User::Leave( KErrServerBusy );
       
  1358         }
       
  1359     iRequestFunction = KPlayUtterance;
       
  1360     iModelBankID = aModelBankID;
       
  1361     iModelID = aModelID;
       
  1362     DoAsynch();
       
  1363     }
       
  1364  
       
  1365 // -----------------------------------------------------------------------------
       
  1366 // CSIControllerPlugin::RecognizeL
       
  1367 // Initiates recognition towards the recognizer.
       
  1368 // -----------------------------------------------------------------------------
       
  1369 //
       
  1370 void CSIControllerPlugin::RecognizeL( CSDClientResultSet& /*aClientResultSet*/ )
       
  1371     {
       
  1372     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::RecognizeL(sd)" );
       
  1373     User::Leave( KErrNotSupported );
       
  1374     }
       
  1375 
       
  1376 // -----------------------------------------------------------------------------
       
  1377 // CSIControllerPlugin::RecognizeL
       
  1378 // Initiates recognition towards the recognizer.
       
  1379 // -----------------------------------------------------------------------------
       
  1380 //
       
  1381 void CSIControllerPlugin::RecognizeL( CSIClientResultSet& aResultSet )
       
  1382     {
       
  1383     RUBY_DEBUG_BLOCK("CSIControllerPlugin::RecognizeL(si)");
       
  1384     
       
  1385     if ( IsActive() )
       
  1386         {
       
  1387         User::Leave( KErrServerBusy );
       
  1388         }
       
  1389     iRequestFunction = KRecognize ;
       
  1390     
       
  1391     iSIClientResultSet=&aResultSet;
       
  1392     DoAsynch();
       
  1393     }
       
  1394 
       
  1395 // -----------------------------------------------------------------------------
       
  1396 // CSIControllerPlugin::HandleRecognizeL
       
  1397 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
  1398 // -----------------------------------------------------------------------------
       
  1399 //
       
  1400 void CSIControllerPlugin::HandleRecognizeL()
       
  1401     {
       
  1402     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleRecognizeL" );
       
  1403     
       
  1404     if ( iState == ESiPluginRecognize )
       
  1405         { 
       
  1406         iSIDatabase->BeginTransactionL();
       
  1407         
       
  1408         if ( iSIGrammarDB->IsGrammarLoaded() )			
       
  1409             {			 
       
  1410             // ClientResultSet was already saved by RunL 	 
       
  1411             iSIResultSet = &( iSIClientResultSet->SIResultSet() );
       
  1412             iDevASR->InitRecognizerBE( *iSIResultSet );
       
  1413             }
       
  1414         else
       
  1415             {
       
  1416             // Either the loaded grammars are empty (contains no rule) or
       
  1417             // all rules have been unloaded.  No need to proceed any further.
       
  1418             iSIDatabase->CommitChangesL( ETrue );
       
  1419             User::Leave( KErrNotReady );
       
  1420             }
       
  1421         
       
  1422         iSIDatabase->CommitChangesL( ETrue );
       
  1423         }
       
  1424     else
       
  1425         {
       
  1426         // Wrong state
       
  1427         User::Leave( KErrAsrInvalidState );
       
  1428         }
       
  1429     }
       
  1430 
       
  1431 // -----------------------------------------------------------------------------
       
  1432 // CSIControllerPlugin::RecordL
       
  1433 // Initiates recording of user utterance.
       
  1434 // -----------------------------------------------------------------------------
       
  1435 //
       
  1436 void CSIControllerPlugin::RecordL( TTimeIntervalMicroSeconds32 aRecordTime )
       
  1437     {
       
  1438     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::RecordL" );
       
  1439     
       
  1440     RUBY_DEBUG0( "CSIControllerPlugin::RecognizeL Returning thread priority back to normal" );
       
  1441     // the priority was lowered in the StartRecSessionL as a quick fix (voice ui 
       
  1442     // tone player had too low priority to run. Toi minimize the poorly tested
       
  1443     // consequences of the quick-fix, we return priority back to normal
       
  1444     // as soon as possible
       
  1445  
       
  1446 // Temporary fix that makes voiceui work in wk36-> sdks
       
  1447 //   RThread().SetPriority( EPriorityNormal );
       
  1448     if ( IsActive() )
       
  1449         {
       
  1450         User::Leave( KErrServerBusy );
       
  1451         }
       
  1452     iRequestFunction = KRecord;
       
  1453     iRecordTime = aRecordTime;
       
  1454     DoAsynch();
       
  1455     }
       
  1456 
       
  1457 // -----------------------------------------------------------------------------
       
  1458 // CSIControllerPlugin::HandleRecordL
       
  1459 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
  1460 // -----------------------------------------------------------------------------
       
  1461 //
       
  1462 void CSIControllerPlugin::HandleRecordL( TTimeIntervalMicroSeconds32 aRecordTime )
       
  1463     {
       
  1464     RUBY_DEBUG_BLOCK("CSIControllerPlugin::HandleRecordL");
       
  1465     
       
  1466     if ( iState == ESdPluginTrain || iState == ESiPluginRecognize )
       
  1467         {
       
  1468         iDevASR->Record(aRecordTime);
       
  1469         }
       
  1470     else
       
  1471         {
       
  1472         // Wrong state
       
  1473         User::Leave( KErrAsrInvalidState );
       
  1474         }
       
  1475     }
       
  1476 
       
  1477 // -----------------------------------------------------------------------------
       
  1478 // CSIControllerPlugin::PreStartSamplingL
       
  1479 // Pre-starts sampling before recognition start.
       
  1480 // -----------------------------------------------------------------------------
       
  1481 //
       
  1482 void CSIControllerPlugin::PreStartSamplingL()
       
  1483     {
       
  1484     RUBY_DEBUG_BLOCK( "" );
       
  1485 
       
  1486     if ( IsActive() )
       
  1487         {
       
  1488         User::Leave( KErrServerBusy );
       
  1489         }
       
  1490     iRequestFunction = KPreStartSampling;
       
  1491     DoAsynch();
       
  1492     }
       
  1493 
       
  1494 // -----------------------------------------------------------------------------
       
  1495 // CSIControllerPlugin::HandlePreStartSamplingL
       
  1496 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
  1497 // -----------------------------------------------------------------------------
       
  1498 //
       
  1499 void CSIControllerPlugin::HandlePreStartSamplingL()
       
  1500     {
       
  1501     RUBY_DEBUG_BLOCK( "" );
       
  1502     iDevASR->PreStartSamplingL();
       
  1503     }
       
  1504 
       
  1505 // -----------------------------------------------------------------------------
       
  1506 // CSIControllerPlugin::RemoveGrammarL
       
  1507 // Calls the grammar database handler to remove a grammar.
       
  1508 // -----------------------------------------------------------------------------
       
  1509 //
       
  1510 void CSIControllerPlugin::RemoveGrammarL( TSIGrammarID aGrammarID )
       
  1511     {
       
  1512     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::RemoveGrammarL" );
       
  1513     
       
  1514     if ( IsActive() )
       
  1515         {
       
  1516         User::Leave( KErrServerBusy );
       
  1517         }
       
  1518     iRequestFunction = KRemoveGrammar;
       
  1519     iGrammarID = aGrammarID;
       
  1520     DoAsynch();
       
  1521     }
       
  1522 
       
  1523 // -----------------------------------------------------------------------------
       
  1524 // CSIControllerPlugin::HandleRemoveGrammarL
       
  1525 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
  1526 // -----------------------------------------------------------------------------
       
  1527 //
       
  1528 void CSIControllerPlugin::HandleRemoveGrammarL( TSIGrammarID aGrammarID )
       
  1529     {
       
  1530     RUBY_DEBUG_BLOCK("CSIControllerPlugin::HandleRemoveGrammarL");
       
  1531     
       
  1532     if ( iClientRegistered )
       
  1533         {
       
  1534         iSIDatabase->BeginTransactionL();
       
  1535         iSIGrammarDB->RemoveGrammarL( iClientUid, aGrammarID );
       
  1536         iSIDatabase->CommitChangesL( ETrue ) ;  
       
  1537         }
       
  1538     else
       
  1539         {
       
  1540         User::Leave( KErrAsrNotRegisted );
       
  1541         }
       
  1542     }
       
  1543 
       
  1544 // -----------------------------------------------------------------------------
       
  1545 // CSIControllerPlugin::RemoveLexiconL
       
  1546 // Calls the lexicon database handler to remove a lexicon.
       
  1547 // -----------------------------------------------------------------------------
       
  1548 //
       
  1549 void CSIControllerPlugin::RemoveLexiconL( TSILexiconID aLexiconID )
       
  1550     {
       
  1551     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::RemoveLexiconL" );
       
  1552     
       
  1553     if ( IsActive() )
       
  1554         {
       
  1555         User::Leave( KErrServerBusy );
       
  1556         }
       
  1557     iRequestFunction = KRemoveLexicon;
       
  1558     iLexiconID = aLexiconID;
       
  1559     DoAsynch();
       
  1560     }
       
  1561 
       
  1562 // -----------------------------------------------------------------------------
       
  1563 // CSIControllerPlugin::HandleRemoveLexiconL
       
  1564 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
  1565 // -----------------------------------------------------------------------------
       
  1566 //
       
  1567 void CSIControllerPlugin::HandleRemoveLexiconL( TSILexiconID aLexiconID )
       
  1568     {
       
  1569     RUBY_DEBUG_BLOCK("CSIControllerPlugin::HandleRemoveLexiconL");
       
  1570     
       
  1571     if ( iClientRegistered )
       
  1572         {
       
  1573         iSIDatabase->BeginTransactionL();
       
  1574         iSILexiconDB->RemoveLexiconL( iClientUid, aLexiconID );		
       
  1575         iSIDatabase->CommitChangesL( ETrue ) ;  
       
  1576         }
       
  1577     else
       
  1578         {
       
  1579         User::Leave( KErrAsrNotRegisted );
       
  1580         }
       
  1581     }
       
  1582 
       
  1583 // -----------------------------------------------------------------------------
       
  1584 // CSIControllerPlugin::RemoveModelBankL
       
  1585 // Calls the model database handler to remove a model bank.
       
  1586 // -----------------------------------------------------------------------------
       
  1587 //
       
  1588 void CSIControllerPlugin::RemoveModelBankL( TSIModelBankID aModelBankID )
       
  1589     {
       
  1590     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::RemoveModelBankL" );
       
  1591     
       
  1592     if ( IsActive() )
       
  1593         {
       
  1594         User::Leave( KErrServerBusy );
       
  1595         }
       
  1596     iRequestFunction = KRemoveModelBank;
       
  1597     iModelBankID = aModelBankID;
       
  1598     DoAsynch();
       
  1599     }
       
  1600 
       
  1601 // -----------------------------------------------------------------------------
       
  1602 // CSIControllerPlugin::HandleRemoveModelBankL
       
  1603 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
  1604 // -----------------------------------------------------------------------------
       
  1605 //
       
  1606 void CSIControllerPlugin::HandleRemoveModelBankL( TSIModelBankID aModelBankID )
       
  1607     {
       
  1608     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleRemoveModelBankL" );
       
  1609     
       
  1610     if ( iClientRegistered )
       
  1611         {
       
  1612         iSIDatabase->BeginTransactionL();
       
  1613         iSIModelBankDB->RemoveModelBankL( iClientUid, aModelBankID );
       
  1614         iSIDatabase->CommitChangesL( ETrue ) ;  
       
  1615         }
       
  1616     else
       
  1617         {
       
  1618         User::Leave( KErrAsrNotRegisted );
       
  1619         }
       
  1620     }
       
  1621 
       
  1622 // -----------------------------------------------------------------------------
       
  1623 // CSIControllerPlugin::RemoveModelL
       
  1624 // Calls the model database handler to remove a model from a model bank.
       
  1625 // -----------------------------------------------------------------------------
       
  1626 //
       
  1627 void CSIControllerPlugin::RemoveModelL( TSIModelBankID aModelBankID,
       
  1628                                         TSIModelID aModelID )
       
  1629     {
       
  1630     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::RemoveModelL" );
       
  1631     
       
  1632     if ( IsActive() )
       
  1633         {
       
  1634         
       
  1635         User::Leave( KErrServerBusy );
       
  1636         }
       
  1637     iRequestFunction = KRemoveModel;
       
  1638     iModelBankID = aModelBankID;
       
  1639     iModelID = aModelID;
       
  1640     DoAsynch();
       
  1641     }
       
  1642 
       
  1643 // -----------------------------------------------------------------------------
       
  1644 // CSIControllerPlugin::HandleRemoveModelL
       
  1645 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
  1646 // -----------------------------------------------------------------------------
       
  1647 //
       
  1648 void CSIControllerPlugin::HandleRemoveModelL( TSIModelBankID /*aModelBankID*/,
       
  1649                                               TSIModelID /*aModelID*/ )
       
  1650     {
       
  1651     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleRemoveModelL" );
       
  1652     User::Leave( KErrNotSupported );  
       
  1653     }
       
  1654 
       
  1655 // -----------------------------------------------------------------------------
       
  1656 // CSIControllerPlugin::RemovePronunciationL
       
  1657 // Calls the lexicon database handler to remove a pronunciation from a lexicon.
       
  1658 // -----------------------------------------------------------------------------
       
  1659 //
       
  1660 void CSIControllerPlugin::RemovePronunciationL( TSILexiconID aLexiconID,
       
  1661                                                 TSIPronunciationID aPronunciationID )
       
  1662     {
       
  1663     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::RemovePronunciationL" );
       
  1664     
       
  1665     if ( IsActive() )
       
  1666         {
       
  1667         User::Leave( KErrServerBusy );
       
  1668         }
       
  1669     iRequestFunction = KRemovePronunciation;
       
  1670     iLexiconID = aLexiconID;
       
  1671     iPronunciationID = aPronunciationID;
       
  1672     DoAsynch();
       
  1673     }
       
  1674 
       
  1675 // -----------------------------------------------------------------------------
       
  1676 // CSIControllerPlugin::HandleRemovePronunciationL
       
  1677 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
  1678 // -----------------------------------------------------------------------------
       
  1679 //
       
  1680 void CSIControllerPlugin::HandleRemovePronunciationL( TSILexiconID aLexiconID,
       
  1681                                                       TSIPronunciationID aPronunciationID )
       
  1682     {
       
  1683     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleRemovePronunciationL" );
       
  1684     
       
  1685     if ( iClientRegistered )
       
  1686         {
       
  1687         iSIDatabase->BeginTransactionL();
       
  1688         iSILexiconDB->RemovePronunciationL( iClientUid, aLexiconID, aPronunciationID );
       
  1689         iSIDatabase->CommitChangesL( ETrue ) ; 
       
  1690         }
       
  1691     else
       
  1692         {
       
  1693         User::Leave( KErrAsrNotRegisted );
       
  1694         }
       
  1695     }
       
  1696 
       
  1697 // -----------------------------------------------------------------------------
       
  1698 // CSIControllerPlugin::RemoveRuleL
       
  1699 // Calls the grammar database handler to remove a rule from a grammar.
       
  1700 // -----------------------------------------------------------------------------
       
  1701 //
       
  1702 void CSIControllerPlugin::RemoveRuleL( TSIGrammarID aGrammarID,
       
  1703                                        TSIRuleID aRuleID )
       
  1704     {
       
  1705     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::RemoveRuleL" );
       
  1706     
       
  1707     if ( IsActive() )
       
  1708         {
       
  1709         User::Leave( KErrServerBusy );
       
  1710         }
       
  1711     iState=ESiPluginRemoveRule;  // handled by database modify function 
       
  1712     iRequestFunction = KRemoveRule;
       
  1713     iGrammarID = aGrammarID;
       
  1714     iRuleID = aRuleID;
       
  1715     DoAsynch();
       
  1716     }
       
  1717 
       
  1718 
       
  1719 // -----------------------------------------------------------------------------
       
  1720 // CSIControllerPlugin::HandleRemoveRuleL
       
  1721 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
  1722 // -----------------------------------------------------------------------------
       
  1723 //
       
  1724 void CSIControllerPlugin::HandleRemoveRuleL( TSIGrammarID aGrammarID,
       
  1725                                              TSIRuleID aRuleID )
       
  1726     {
       
  1727     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleRemoveRuleL" );
       
  1728     
       
  1729     if ( iClientRegistered )
       
  1730         {
       
  1731         iSIDatabase->BeginTransactionL();
       
  1732         
       
  1733         iSICompiledGrammarRecompile = ( CSICompiledGrammar* )iSIGrammarDB->GrammarL( aGrammarID );	
       
  1734         // if leave as KErrArgument
       
  1735         
       
  1736         CSIRule* aRule = &iSICompiledGrammarRecompile->AtL( iSICompiledGrammarRecompile->Find( aRuleID ) );
       
  1737         
       
  1738         
       
  1739         // in a rule , all the variant have same lexicon ID, that is because 
       
  1740         // the way it was added in addvoicetags()
       
  1741         CSIRuleVariant* rv = &aRule->AtL( 0 );	
       
  1742         TSILexiconID lexiconId = rv->LexiconID();	
       
  1743         
       
  1744         TSIPronunciationIDSequence pronunciationIdSequence;	
       
  1745         CleanupClosePushL( pronunciationIdSequence );
       
  1746         
       
  1747         CSILexicon* lexicon = iSILexiconDB->LexiconL( lexiconId );	
       
  1748         CleanupStack::PushL( lexicon ); 
       
  1749         
       
  1750         for ( TInt i = 0; i < aRule->Count(); i++ )
       
  1751             {
       
  1752             pronunciationIdSequence.Reset();
       
  1753             CSIRuleVariant* rv = &aRule->AtL( i );
       
  1754             rv->GetPronunciationIDsL( pronunciationIdSequence );
       
  1755             for ( TInt k = 0; k < pronunciationIdSequence.Count(); k++ )
       
  1756                 {
       
  1757                 lexicon->DeleteL( pronunciationIdSequence[k] );		
       
  1758                 }
       
  1759             }
       
  1760             
       
  1761         // Update the lexicon
       
  1762         iSILexiconDB->UpdateLexiconL( iClientUid, lexicon );
       
  1763         
       
  1764         CleanupStack::PopAndDestroy( lexicon );
       
  1765         CleanupStack::PopAndDestroy( &pronunciationIdSequence ); 
       
  1766         
       
  1767         
       
  1768         // Recompile the grammar after a deletion
       
  1769         // Async call, callback handled in handleeventtp
       
  1770         
       
  1771         iSICompiledGrammarRecompile->DeleteL( aRuleID );
       
  1772         if ( iSICompiledGrammarRecompile->Count() == 0 )
       
  1773             {
       
  1774             // Reset compiled data to null
       
  1775             iSICompiledGrammarRecompile->SetCompiledData( NULL );
       
  1776             delete iSICompiledGrammarRecompile;
       
  1777             iSICompiledGrammarRecompile = NULL;
       
  1778             // Special case, do the callback now since we're not expecting
       
  1779             // anything from the lower layers
       
  1780             iControllerIf.SendSrsEvent( KUidAsrEventRemoveRule, iResult );
       
  1781             }
       
  1782         else
       
  1783             {
       
  1784             iDevASR->CompileGrammarL( *iSICompiledGrammarRecompile );
       
  1785             }
       
  1786         
       
  1787         iSIDatabase->CommitChangesL( ETrue );
       
  1788         }
       
  1789     else
       
  1790         {
       
  1791         User::Leave( KErrAsrNotRegisted );
       
  1792         }
       
  1793     }
       
  1794 
       
  1795 
       
  1796 
       
  1797 // -----------------------------------------------------------------------------
       
  1798 // CSIControllerPlugin::RemoveRulesL
       
  1799 // Calls the grammar database handler to remove a set of rules from a grammar.
       
  1800 // -----------------------------------------------------------------------------
       
  1801 //
       
  1802 void CSIControllerPlugin::RemoveRulesL( TSIGrammarID aGrammarID,
       
  1803                                         RArray<TSIRuleID>& aRuleIDs )
       
  1804     {
       
  1805     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::RemoveRulesL" );
       
  1806     
       
  1807     if ( IsActive() )
       
  1808         {
       
  1809         User::Leave( KErrServerBusy );
       
  1810         }
       
  1811     iState=ESiPluginRemoveRules;  // handled by database modify function 
       
  1812     iRequestFunction = KRemoveRules;
       
  1813     iGrammarID = aGrammarID;
       
  1814     iRuleIDs = &aRuleIDs;
       
  1815     DoAsynch();
       
  1816     }
       
  1817 
       
  1818 // -----------------------------------------------------------------------------
       
  1819 // CSIControllerPlugin::HandleRemoveRulesL
       
  1820 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
  1821 // (other items were commented in a header).
       
  1822 // Note, that request to remove a non-existing rule completes successfully
       
  1823 // -----------------------------------------------------------------------------
       
  1824 //
       
  1825 void CSIControllerPlugin::HandleRemoveRulesL( TSIGrammarID aGrammarID,
       
  1826                                               RArray<TSIRuleID>& aRuleIDs )
       
  1827     {
       
  1828     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleRemoveRulesL" );
       
  1829     TInt i( 0 );
       
  1830 
       
  1831     RUBY_DEBUG1( "CSIControllerPlugin::HandleRemoveRulesL grammarId [%d]", aGrammarID );
       
  1832     RUBY_DEBUG1( "CSIControllerPlugin::HandleRemoveRulesL aRuleIDs.Count() = [%d]", aRuleIDs.Count() );
       
  1833     
       
  1834     if ( iClientRegistered )
       
  1835         {
       
  1836         iSIDatabase->BeginTransactionL();
       
  1837         
       
  1838         // 1.Get the lexion id 
       
  1839         // 2. For each rule, delete it's varant's prounication from lexicon and then delete the rule from grammar
       
  1840         // 3. Update lexicon DB.
       
  1841         // 4. Recompile the grammar
       
  1842         // Get the grammar ,use all rules, so it clean up when iSIGrammarDB is deleted	
       
  1843         RUBY_DEBUG1( "CSIControllerPlugin::HandleRemoveRulesL Getting All rules for grammarID [%d]", aGrammarID );
       
  1844         
       
  1845         iSICompiledGrammarRecompile = ( CSICompiledGrammar* )iSIGrammarDB->GrammarL( aGrammarID );	
       
  1846         __UHEAP_MARK;		
       
  1847         RPointerArray<CSILexicon> aLexiconArray;
       
  1848         CleanupClosePushL( aLexiconArray );
       
  1849         RArray<TSILexiconID> aLexiconIDs;
       
  1850         CleanupClosePushL( aLexiconIDs );
       
  1851         CSILexicon* aLexicon;
       
  1852         
       
  1853         // if leave as KErrArgument
       
  1854         for ( i = 0; i < aRuleIDs.Count(); i++ ) 
       
  1855             {		
       
  1856             TSIRuleID aRuleID = aRuleIDs[i];
       
  1857             TInt removeRuleIndex = iSICompiledGrammarRecompile->Find( aRuleID );
       
  1858             CSIRule* aRule = NULL;  // rule to remove
       
  1859             if ( removeRuleIndex == KErrNotFound ) 
       
  1860                 {
       
  1861                 RUBY_DEBUG1("CSIControllerPlugin::HandleRemoveRulesL RuleID [%d] not found. Probably already deleted", aRuleID );
       
  1862                 // Nothing to delete in this round.
       
  1863                 // Proceed to the next one
       
  1864                 continue;
       
  1865                 }
       
  1866             else if ( removeRuleIndex >= 0 ) 
       
  1867                 {
       
  1868                 aRule = &iSICompiledGrammarRecompile->AtL( removeRuleIndex );
       
  1869                 }
       
  1870             else 
       
  1871                 {
       
  1872                 // Unknown error
       
  1873                 User::Leave( removeRuleIndex );
       
  1874                 }
       
  1875             // in a rule , all the variant have same lexicon ID, 
       
  1876             // and all the rule's rule variant have same lexicon ID,
       
  1877             // one lexicon corresponding to one grammar
       
  1878             // that is because  the way it was added in addvoicetags()			
       
  1879             CSIRuleVariant* aRv = &aRule->AtL( 0 );	
       
  1880             TSILexiconID aLexiconID = aRv->LexiconID();	
       
  1881             
       
  1882             // This hack ensures 4 byte alignment in winscw
       
  1883             TSILexiconID* lexID = new ( ELeave ) TSILexiconID;
       
  1884             CleanupStack::PushL( lexID );
       
  1885             *lexID = aRv->LexiconID();       
       
  1886             // Uniq ids array
       
  1887             aLexiconIDs.InsertInSignedKeyOrder( *lexID );
       
  1888             CleanupStack::PopAndDestroy( lexID );
       
  1889             }	
       
  1890         // collect the lexicon according to the uniq lexicon array
       
  1891         for ( i = 0; i < aLexiconIDs.Count(); i++ ) 
       
  1892             {	
       
  1893             aLexicon = iSILexiconDB->LexiconL( aLexiconIDs[i] );				
       
  1894             aLexiconArray.Append( aLexicon );
       
  1895             }
       
  1896         
       
  1897         for ( i = 0; i < aRuleIDs.Count(); i++ ) 
       
  1898             {
       
  1899             TSIRuleID aRuleID = aRuleIDs[i];
       
  1900             
       
  1901             TInt removeRuleIndex = iSICompiledGrammarRecompile->Find( aRuleID );
       
  1902             CSIRule* aRule = NULL;  // rule to remove
       
  1903             if ( removeRuleIndex == KErrNotFound ) 
       
  1904                 {
       
  1905                 RUBY_DEBUG1("CSIControllerPlugin::HandleRemoveRulesL RuleID [%d] not found. Probably already deleted", aRuleID );
       
  1906                 // Nothing to delete in this round.
       
  1907                 // Proceed to the next one
       
  1908                 continue;
       
  1909                 }
       
  1910             else if ( removeRuleIndex >= 0 ) 
       
  1911                 {
       
  1912                 aRule = &iSICompiledGrammarRecompile->AtL( removeRuleIndex );
       
  1913                 }
       
  1914             else 
       
  1915                 {
       
  1916                 // Unknown error
       
  1917                 User::Leave( removeRuleIndex );
       
  1918                 }
       
  1919             
       
  1920             // in a rule , all the variant have same lexicon ID, that is because 
       
  1921             // the way it was added in addvoicetags()
       
  1922             
       
  1923             TSIPronunciationIDSequence aIPronunciationIDSequence;	
       
  1924             CleanupClosePushL( aIPronunciationIDSequence );
       
  1925             
       
  1926             for ( TInt i = 0 ; i < aRule->Count() ; i++ ) 
       
  1927                 {
       
  1928                 aIPronunciationIDSequence.Reset();
       
  1929                 CSIRuleVariant* aRv = &aRule->AtL( i );
       
  1930                 aRv->GetPronunciationIDsL( aIPronunciationIDSequence );
       
  1931                 TSILexiconID aLexiconID = aRv->LexiconID();
       
  1932                 TInt aLocation = 0;
       
  1933                 for ( TInt s = 0 ; s < aLexiconIDs.Count() ; s++ ) 
       
  1934                     {
       
  1935                     if ( aLexiconIDs[s] == aLexiconID ) 
       
  1936                         {
       
  1937                         aLocation = s;
       
  1938                         break;
       
  1939                         }
       
  1940                     }
       
  1941                 
       
  1942                 CSILexicon* aLexicon =aLexiconArray[aLocation];
       
  1943                 for ( TInt k = 0 ; k < aIPronunciationIDSequence.Count() ; k++ ) 
       
  1944                     {
       
  1945                     aLexicon->DeleteL( aIPronunciationIDSequence[k] );
       
  1946                     }
       
  1947                 }
       
  1948             
       
  1949             // delete aIPronunciationIDSequence
       
  1950             CleanupStack::PopAndDestroy( &aIPronunciationIDSequence ); 
       
  1951             
       
  1952             // Recompile the grammar after a deletion
       
  1953             // Async call, callback handled in handleeventtp
       
  1954             //delete iSICompiledGrammarRecompile;			
       
  1955             iSICompiledGrammarRecompile->DeleteL( aRuleID );
       
  1956             }
       
  1957         
       
  1958         // Update the lexicon
       
  1959         for ( i = 0; i < aLexiconArray.Count(); i++ )
       
  1960             {
       
  1961             iSILexiconDB->UpdateLexiconL( iClientUid, aLexiconArray[i] );
       
  1962             }	
       
  1963         
       
  1964         // clean up 					
       
  1965         // Close the arrays
       
  1966         //aLexiconIDs.Close();
       
  1967         CleanupStack::PopAndDestroy( &aLexiconIDs ); // CleanupClosePushL aLexiconIDs
       
  1968         aLexiconArray.ResetAndDestroy();
       
  1969         CleanupStack::PopAndDestroy( &aLexiconArray ); //aLexiconArray
       
  1970         __UHEAP_MARKEND;
       
  1971         
       
  1972         if ( iSICompiledGrammarRecompile->Count() == 0 )
       
  1973             {
       
  1974             // Reset compiled data to null
       
  1975             iSICompiledGrammarRecompile->SetCompiledData( NULL );
       
  1976             
       
  1977             iSIDatabase->BeginTransactionL();
       
  1978             // save the compiled grammar
       
  1979             TRAPD( error, iSIGrammarDB->UpdateGrammarL( iClientUid,iSICompiledGrammarRecompile ) );
       
  1980             iSIDatabase->CommitChangesL( ETrue );
       
  1981             if ( error )
       
  1982                 {
       
  1983                 iControllerIf.SendSrsEvent( KUidAsrEventRemoveRules, error );
       
  1984                 delete iSICompiledGrammarRecompile;
       
  1985                 iState = ESiPluginIdle;
       
  1986                 return;
       
  1987                 }	
       
  1988             TRAP( error, iSIDatabase->CommitChangesL( ETrue ) );  
       
  1989             
       
  1990             delete iSICompiledGrammarRecompile;
       
  1991             iSICompiledGrammarRecompile = NULL;
       
  1992             iState = ESiPluginIdle;
       
  1993             
       
  1994             // Special case, do the callback now since we're not expecting
       
  1995             // anything from the lower layers
       
  1996             iControllerIf.SendSrsEvent( KUidAsrEventRemoveRules, error );
       
  1997             } // if ( iSICompiledGrammarRecompile->Count() == 0 )
       
  1998         else
       
  1999             {
       
  2000             iDevASR->CompileGrammarL( *iSICompiledGrammarRecompile );
       
  2001             }
       
  2002         
       
  2003         } // if ( iClientRegistered )
       
  2004         else
       
  2005             {
       
  2006             User::Leave( KErrAsrNotRegisted );
       
  2007             }
       
  2008     }
       
  2009 
       
  2010 // -----------------------------------------------------------------------------
       
  2011 // CSIControllerPlugin::SetClientUid
       
  2012 // Sets the client's UID for data ownership identification.
       
  2013 // -----------------------------------------------------------------------------
       
  2014 //
       
  2015 void CSIControllerPlugin::SetClientUid( TUid aClientUid )
       
  2016     {
       
  2017     RUBY_DEBUG0( "CSIControllerPlugin::SetClientUid") ;
       
  2018     iClientUid = aClientUid;
       
  2019     iClientRegistered = ETrue;
       
  2020     }
       
  2021 
       
  2022 // -----------------------------------------------------------------------------
       
  2023 // CSIControllerPlugin::SetPrioritySettings
       
  2024 // Set the priority settings for this controller.  This is used during recording
       
  2025 // and playback.
       
  2026 // -----------------------------------------------------------------------------
       
  2027 //
       
  2028 void CSIControllerPlugin::SetPrioritySettings( const TMMFPrioritySettings& aPrioritySettings )
       
  2029     {
       
  2030     RUBY_DEBUG0( "CSIControllerPlugin::SetPrioritySettings" );
       
  2031     iDevASR->SetPrioritySettings( aPrioritySettings );
       
  2032     }
       
  2033 
       
  2034 // -----------------------------------------------------------------------------
       
  2035 // CSIControllerPlugin::StartRecSessionL
       
  2036 // Initiates a recognition session.
       
  2037 // -----------------------------------------------------------------------------
       
  2038 //
       
  2039 void CSIControllerPlugin::StartRecSessionL()
       
  2040     {
       
  2041     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::StartRecSessionL" );
       
  2042     RThread().SetPriority( EPriorityAbsoluteLow );
       
  2043     RUBY_DEBUG0( "CSIControllerPlugin::StartRecSessionL Lowered thread priority to absolute low" );
       
  2044     //RUBY_DEBUG1( "CSIControllerPlugin::StartRecSessionL Thread id is [%x]", RThread().Id() );
       
  2045     
       
  2046     if ( iState == ESiPluginIdle )
       
  2047         {
       
  2048         
       
  2049         User::LeaveIfError( iDevASR->StartRecSession( ESiRecognition ) );		
       
  2050         iState = ESiPluginRecognize;
       
  2051         }
       
  2052     else
       
  2053         {
       
  2054         // Wrong state
       
  2055         User::Leave( KErrAsrInvalidState );
       
  2056         }
       
  2057     }
       
  2058 
       
  2059 // -----------------------------------------------------------------------------
       
  2060 // CSIControllerPlugin::TrainL
       
  2061 // Initiates a speaker depedent training session.
       
  2062 // -----------------------------------------------------------------------------
       
  2063 //
       
  2064 void CSIControllerPlugin::TrainL( TSIModelBankID aModelBankID,
       
  2065                                   TSIModelID& aModelID )
       
  2066     {
       
  2067     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::TrainL" );
       
  2068     
       
  2069     if ( IsActive() )
       
  2070         {	
       
  2071         User::Leave( KErrServerBusy );
       
  2072         }
       
  2073     iRequestFunction = KTrain;
       
  2074     iModelBankID = aModelBankID;
       
  2075     iModelIDPtr = &aModelID;
       
  2076     DoAsynch();
       
  2077     }
       
  2078 
       
  2079 // -----------------------------------------------------------------------------
       
  2080 // CSIControllerPlugin::HandleTrainL
       
  2081 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
  2082 // -----------------------------------------------------------------------------
       
  2083 //
       
  2084 void CSIControllerPlugin::HandleTrainL( TSIModelBankID /*aModelBankID*/ )
       
  2085     {
       
  2086     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleTrainL" );
       
  2087     User::Leave( KErrNotSupported );  
       
  2088     }
       
  2089 
       
  2090 // -----------------------------------------------------------------------------
       
  2091 // CSIControllerPlugin::UnloadRuleL
       
  2092 // Unloads a rule from the recognizer for this recognition session.
       
  2093 // -----------------------------------------------------------------------------
       
  2094 //
       
  2095 void CSIControllerPlugin::UnloadRuleL( TSIGrammarID aGrammarID,
       
  2096                                        TSIRuleID aRuleID )
       
  2097     {
       
  2098     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::UnloadRuleL" );
       
  2099     
       
  2100     if ( IsActive() )
       
  2101         {
       
  2102         User::Leave( KErrServerBusy );
       
  2103         }
       
  2104     iRequestFunction = KUnloadRule;
       
  2105     iGrammarID = aGrammarID;
       
  2106     iRuleID = aRuleID;
       
  2107     DoAsynch();
       
  2108     }
       
  2109 
       
  2110 // -----------------------------------------------------------------------------
       
  2111 // CSIControllerPlugin::HandleUnloadRuleL
       
  2112 // This is the asynchronous handle called from the CSIAsyncHandler object.
       
  2113 // -----------------------------------------------------------------------------
       
  2114 //
       
  2115 void CSIControllerPlugin::HandleUnloadRuleL( TSIGrammarID aGrammarID,
       
  2116                                              TSIRuleID aRuleID )
       
  2117     {
       
  2118     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleUnloadRuleL" );
       
  2119     iDevASR->UnloadRule( aGrammarID, aRuleID ); // for blacklisting only
       
  2120     }
       
  2121 
       
  2122 // -----------------------------------------------------------------------------
       
  2123 // CSIControllerPlugin::HandleUnloadRuleL
       
  2124 // Notification from DevASR when feature vectors are available.
       
  2125 // -----------------------------------------------------------------------------
       
  2126 //
       
  2127 void CSIControllerPlugin::FeatureVectorDataRcvd( const TDesC8& /*aFV*/, 
       
  2128                                                  TInt32 /*aSNR*/, 
       
  2129                                                  TInt32 /*aPosition*/ ) 
       
  2130     {
       
  2131     RUBY_DEBUG0( "CSIControllerPlugin::FeatureVectorDataRcvd" ); 
       
  2132     }
       
  2133 
       
  2134 // -----------------------------------------------------------------------------
       
  2135 // CSIControllerPlugin::DevASREvent
       
  2136 // Event from DevASR interface.
       
  2137 // -----------------------------------------------------------------------------
       
  2138 //
       
  2139 void CSIControllerPlugin::DevASREvent( TDevASREvent aEvent,
       
  2140                                        TDevASRError aError )
       
  2141     {
       
  2142     RUBY_DEBUG2( "CSIControllerPlugin::DevASREvent (Event=%d, Error=%d)", aEvent, aError );
       
  2143     
       
  2144     switch ( iState )
       
  2145         {
       
  2146         case ESdPluginTrain:
       
  2147             TRAP_IGNORE( HandleEventTrainL( aEvent, aError ) );
       
  2148             break;
       
  2149         case ESiPluginRecognize:
       
  2150             HandleEventRecognize( aEvent, aError );
       
  2151             break;
       
  2152         case ESiPluginPlay:
       
  2153             TRAP_IGNORE( HandleEventPlayL( aEvent, aError ) );
       
  2154             break;
       
  2155         case ESiPluginTrainText:
       
  2156             HandleEventTTP( aEvent, aError );
       
  2157             break;
       
  2158 #ifdef __SINDE_TRAINING
       
  2159         case ESiPluginTrainSinde:
       
  2160             iSindeTrainer->HandleEvent( aEvent, aError );
       
  2161             break;
       
  2162 #endif // __SINDE_TRAINING
       
  2163         case ESiPluginRemoveRule:
       
  2164             HandleEventRemoveRule( aEvent, aError );
       
  2165             break;
       
  2166         case ESiPluginRemoveRules:
       
  2167             RUBY_DEBUG0( "CSIControllerPlugin::DevASREvent case iState ESiPluginRemoveRules" );
       
  2168             HandleEventRemoveRules( aEvent, aError );
       
  2169             break;
       
  2170         case ESiPluginIdle:
       
  2171             break;
       
  2172         default:
       
  2173             // Unexpected or cancelled message
       
  2174             break;
       
  2175         }
       
  2176     }
       
  2177 
       
  2178 // -----------------------------------------------------------------------------
       
  2179 // CSIControllerPlugin::RequestSpeechData
       
  2180 // Get speech data from audio buffer
       
  2181 // -----------------------------------------------------------------------------
       
  2182 //
       
  2183 void CSIControllerPlugin::RequestSpeechData()
       
  2184     {
       
  2185     const TInt KBufferMaxLength( 4000 );
       
  2186     TInt sizeLeft( iAudioBuffer->Size() - iNSamplesSent * 2 );
       
  2187     if ( sizeLeft == 0 )
       
  2188         {
       
  2189         // nothing
       
  2190         }
       
  2191     else if ( sizeLeft < KBufferMaxLength*2 )
       
  2192         {
       
  2193         // last buffer
       
  2194         iCurrentAudioBuffer.Set( iAudioBuffer->Right( sizeLeft ) );
       
  2195         iDevASR->SendSpeechData( iCurrentAudioBuffer, ETrue   );
       
  2196         
       
  2197         iNSamplesSent += sizeLeft/2;
       
  2198         }
       
  2199     else
       
  2200         {
       
  2201         iCurrentAudioBuffer.Set( iAudioBuffer->Mid( 2*iNSamplesSent, 2*KBufferMaxLength ) );
       
  2202         
       
  2203         iDevASR->SendSpeechData( iCurrentAudioBuffer, EFalse  );
       
  2204         iNSamplesSent += KBufferMaxLength;
       
  2205         }
       
  2206     }
       
  2207 
       
  2208 // -----------------------------------------------------------------------------
       
  2209 // CSIControllerPlugin::SILexiconL
       
  2210 // Load lexicon by this call back functionDevAsr Take the owner ship of the lexicon 
       
  2211 // -----------------------------------------------------------------------------
       
  2212 //
       
  2213 CSILexicon* CSIControllerPlugin::SILexiconL( TSILexiconID anID )
       
  2214     {
       
  2215     RUBY_DEBUG_BLOCK("CSIControllerPlugin::SILexiconL");
       
  2216     
       
  2217     iSIDatabase->BeginTransactionL();
       
  2218     CSILexicon* lexicon = iSILexiconDB->LexiconL(anID);
       
  2219     iSIDatabase->CommitChangesL( EFalse );
       
  2220     
       
  2221     return lexicon;
       
  2222     }
       
  2223 
       
  2224 // -----------------------------------------------------------------------------
       
  2225 // CSIControllerPlugin::ConfigurationData
       
  2226 // DevASR calls this method to get configuration data.
       
  2227 // -----------------------------------------------------------------------------
       
  2228 //
       
  2229 HBufC8* CSIControllerPlugin::ConfigurationData( TUint32 aPackageType, 
       
  2230                                                 TUint32 aPackageID,
       
  2231                                                 TUint32 aStartPosition ,
       
  2232                                                 TUint32 aEndPosition )
       
  2233     {
       
  2234     RUBY_DEBUG0( "ConfigurationData::MdtoConfigurationData" );
       
  2235     return ( iDataLoader->LoadData( aPackageType, aPackageID, 
       
  2236                                     aStartPosition, aEndPosition ) );
       
  2237     }
       
  2238 
       
  2239 // -----------------------------------------------------------------------------
       
  2240 // CSIControllerPlugin::DevASRMessage
       
  2241 // A message in response to a custom command.  This controller never sends a
       
  2242 // custom command.
       
  2243 // -----------------------------------------------------------------------------
       
  2244 //
       
  2245 void CSIControllerPlugin::DevASRMessage( TDesC8& /*aMsg*/ )
       
  2246     {
       
  2247     // SI Controller Plugin does not implement this event.
       
  2248     }
       
  2249 
       
  2250 // -----------------------------------------------------------------------------
       
  2251 // CSIControllerPlugin::PlayL
       
  2252 // Plays the trained user utterance.
       
  2253 // -----------------------------------------------------------------------------
       
  2254 //
       
  2255 void CSIControllerPlugin::PlayL( TSIModelBankID /*aModelBankID*/,
       
  2256                                  TSIModelID /*aModelID*/ )
       
  2257     {
       
  2258     RUBY_DEBUG_BLOCK("CSIControllerPlugin::PlayL");
       
  2259     User::Leave(KErrNotSupported);  
       
  2260     }
       
  2261 
       
  2262 // -----------------------------------------------------------------------------
       
  2263 // CSIControllerPlugin::HandleEventPlayL
       
  2264 // Handling of DevASR event in Play state.
       
  2265 // -----------------------------------------------------------------------------
       
  2266 //
       
  2267 void CSIControllerPlugin::HandleEventPlayL( TDevASREvent/* aEvent*/,
       
  2268                                             TDevASRError/* aError*/ )
       
  2269     {
       
  2270     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleEventPlayL" );
       
  2271     User::Leave( KErrNotSupported );
       
  2272     }
       
  2273 
       
  2274 // -----------------------------------------------------------------------------
       
  2275 // CSIControllerPlugin::HandleEventPlayL
       
  2276 // Handling of DevASR event in RemoveRule state.
       
  2277 // -----------------------------------------------------------------------------
       
  2278 //
       
  2279 void CSIControllerPlugin::HandleEventRemoveRule( TDevASREvent aEvent, 
       
  2280                                                  TDevASRError aError ) 
       
  2281     {
       
  2282     iState = ESiPluginIdle;
       
  2283     
       
  2284     if ( aError )
       
  2285         {
       
  2286         iControllerIf.SendSrsEvent(KUidAsrEventRemoveRule, aError);
       
  2287         delete iSICompiledGrammarRecompile;
       
  2288         iSICompiledGrammarRecompile = NULL;
       
  2289         return;
       
  2290         }
       
  2291     
       
  2292     switch (aEvent)
       
  2293         {
       
  2294         case EDevASRGrammarCompile:
       
  2295             {
       
  2296             TRAPD( error, 
       
  2297                 iSIDatabase->BeginTransactionL();
       
  2298                 // save the compiled grammar
       
  2299                 iSIGrammarDB->UpdateGrammarL( iClientUid, iSICompiledGrammarRecompile );
       
  2300                 iSIDatabase->CommitChangesL( ETrue );
       
  2301                 ); // TRAPD
       
  2302             if ( error )
       
  2303                 {
       
  2304                 iControllerIf.SendSrsEvent( KUidAsrEventRemoveRule, error );
       
  2305                 delete iSICompiledGrammarRecompile;
       
  2306                 iSICompiledGrammarRecompile = NULL;
       
  2307                 iState = ESiPluginIdle;
       
  2308                 return;
       
  2309                 }	
       
  2310             TRAP( error, iSIDatabase->CommitChangesL( ETrue ) );  
       
  2311             iControllerIf.SendSrsEvent( KUidAsrEventRemoveRule, error );
       
  2312             
       
  2313             delete iSICompiledGrammarRecompile;
       
  2314             iSICompiledGrammarRecompile = NULL;
       
  2315             iState = ESiPluginIdle;
       
  2316             break;
       
  2317             
       
  2318             }
       
  2319         default:
       
  2320             // Unexpected or cancelled message
       
  2321             RUBY_DEBUG0( "CSIControllerPlugin::HandleEventRemoveRule. Unexpected.") ;
       
  2322             break;
       
  2323         }
       
  2324     
       
  2325     }
       
  2326 
       
  2327 // -----------------------------------------------------------------------------
       
  2328 // CSIControllerPlugin::HandleEventRemoveRules
       
  2329 // Handling of DevASR event in RemoveRules state.
       
  2330 // -----------------------------------------------------------------------------
       
  2331 //
       
  2332 void CSIControllerPlugin::HandleEventRemoveRules( TDevASREvent aEvent, 
       
  2333                                                   TDevASRError aError ) 
       
  2334     {
       
  2335     RUBY_DEBUG2( "CSIControllerPlugin::HandleEventRemoveRules. Event [%d], Error [%d]", aEvent, aError );
       
  2336     
       
  2337     iState = ESiPluginIdle;
       
  2338     
       
  2339     if ( aError )
       
  2340         {
       
  2341         iControllerIf.SendSrsEvent(KUidAsrEventRemoveRules, aError);
       
  2342         delete iSICompiledGrammarRecompile;
       
  2343         iSICompiledGrammarRecompile = NULL;
       
  2344         return;
       
  2345         }
       
  2346     
       
  2347     switch ( aEvent )
       
  2348         {
       
  2349         case EDevASRGrammarCompile:
       
  2350             {
       
  2351             TRAPD( error, 
       
  2352                 iSIDatabase->BeginTransactionL();
       
  2353                 // save the compiled grammar
       
  2354                 iSIGrammarDB->UpdateGrammarL( iClientUid, iSICompiledGrammarRecompile );
       
  2355                 iSIDatabase->CommitChangesL( ETrue );
       
  2356                 ); // TRAPD
       
  2357             
       
  2358             if ( error )
       
  2359                 {
       
  2360                 iControllerIf.SendSrsEvent( KUidAsrEventRemoveRules, error );
       
  2361                 delete iSICompiledGrammarRecompile;
       
  2362                 iSICompiledGrammarRecompile = NULL;
       
  2363                 iState = ESiPluginIdle;
       
  2364                 return;
       
  2365                 }	
       
  2366             TRAP( error, iSIDatabase->CommitChangesL( ETrue ) );  
       
  2367             iControllerIf.SendSrsEvent( KUidAsrEventRemoveRules, error );
       
  2368             
       
  2369             delete iSICompiledGrammarRecompile;
       
  2370             iSICompiledGrammarRecompile = NULL;
       
  2371             iState = ESiPluginIdle;
       
  2372             break;
       
  2373             
       
  2374             }
       
  2375         default:
       
  2376             // Unexpected or cancelled message
       
  2377             RUBY_DEBUG0( "CSIControllerPlugin::HandleEventRemoveRules. Unexpected." );
       
  2378             break;
       
  2379         }
       
  2380     }
       
  2381 
       
  2382 // -----------------------------------------------------------------------------
       
  2383 // CSIControllerPlugin::HandleEventTTP
       
  2384 // Handling of DevASR event in TTP state.
       
  2385 // -----------------------------------------------------------------------------
       
  2386 //
       
  2387 void CSIControllerPlugin::HandleEventTTP( TDevASREvent aEvent,
       
  2388                                          TDevASRError aError ) 
       
  2389     {
       
  2390     if ( aError )
       
  2391         {
       
  2392         iControllerIf.SendSrsEvent(KUidAsrEventAddVoiceTags, aError);
       
  2393         iState = ESiPluginIdle;
       
  2394         return;
       
  2395         }
       
  2396     
       
  2397     switch ( aEvent )
       
  2398         {
       
  2399         case EDevASRTrainFromText:
       
  2400             //case EDevASRTrainFromTextFinished:
       
  2401             {
       
  2402             
       
  2403             TRAPD( error, 
       
  2404                 UpdateGrammarAndLexiconDBL( iSITtpWordList, iLexiconID, iGrammarID,*iRuleIDs ); 
       
  2405                 iSIDatabase->BeginTransactionL();
       
  2406                 iSICompiledGrammar = ( CSICompiledGrammar* )iSIGrammarDB->LoadGrammarL( iGrammarID );
       
  2407                 iSIDatabase->CommitChangesL( EFalse );
       
  2408                 iDevASR->CompileGrammarL( *iSICompiledGrammar );              
       
  2409                 );
       
  2410              if ( error )
       
  2411                 {
       
  2412                 iControllerIf.SendSrsEvent( KUidAsrEventAddVoiceTags, error );
       
  2413                 iState = ESiPluginIdle;
       
  2414                 return;
       
  2415                 }
       
  2416             
       
  2417             break;
       
  2418             }
       
  2419         case EDevASRGrammarCompile:
       
  2420             {
       
  2421             TInt error = KErrNone;
       
  2422             TRAP( error,
       
  2423                 iSIDatabase->BeginTransactionL();
       
  2424                 // save the compiled grammar
       
  2425                 iSIGrammarDB->UpdateGrammarL( iClientUid, iSICompiledGrammar );
       
  2426                 iSIDatabase->CommitChangesL( ETrue );
       
  2427                 ); // TRAP
       
  2428             iControllerIf.SendSrsEvent( KUidAsrEventAddVoiceTags, error );
       
  2429             iState = ESiPluginIdle;
       
  2430             break;
       
  2431             }
       
  2432         default:
       
  2433             // Unexpected or cancelled message
       
  2434             RUBY_DEBUG0( "CSIControllerPlugin::HandleEventTTP. Unexpected." );
       
  2435             break;
       
  2436             
       
  2437         }
       
  2438     }
       
  2439 
       
  2440 
       
  2441 // -----------------------------------------------------------------------------
       
  2442 // CSIControllerPlugin::HandleEventRecognizeL
       
  2443 // Handling of DevASR event in Recognition state.
       
  2444 // -----------------------------------------------------------------------------
       
  2445 //
       
  2446 void CSIControllerPlugin::HandleEventRecognize( TDevASREvent aEvent,
       
  2447 											    TDevASRError aError )
       
  2448     {
       
  2449     RUBY_DEBUG2( "CSIControllerPlugin::HandleEventRecognize - Event=%d, Error=%d", aEvent, aError);
       
  2450     
       
  2451     switch ( aEvent )
       
  2452         {
       
  2453         case EDevASRLoadModels:
       
  2454             //	case EDevASRModelsLoaded:
       
  2455             if ( aError != KErrNone )
       
  2456                 {
       
  2457                 RUBY_DEBUG1( "CSIControllerPlugin::HandleEventRecognize. EDevASRLoadModels failed with error [%d]", aError );
       
  2458                
       
  2459                 TRAP_IGNORE( 
       
  2460                     iSIDatabase->BeginTransactionL();
       
  2461                     iSIModelBankDB->ResetAndDestroy();
       
  2462                     iSIDatabase->CommitChangesL( ETrue );
       
  2463                     ); // TRAP_IGNORE
       
  2464                 
       
  2465                 iState = ESiPluginIdle;
       
  2466                 }
       
  2467             
       
  2468             iControllerIf.SendSrsEvent( KUidAsrEventLoadModels, aError );
       
  2469             break;
       
  2470         case EDevASRLoadLexicon:
       
  2471             //	case EDevASRLexiconLoaded:
       
  2472             
       
  2473             if ( aError != KErrNone )
       
  2474                 {
       
  2475                 RUBY_DEBUG1( "CSIControllerPlugin::HandleEventRecognize. EDevASRLoadLexicon failed with error [%d]", aError );
       
  2476                 iSIModelBankDB->ResetAndDestroy();
       
  2477                 iSILexiconDB->ResetAndDestroy();			
       
  2478                 iState = ESiPluginIdle;
       
  2479                 }
       
  2480             iControllerIf.SendSrsEvent( KUidAsrEventLoadLexicon, aError );
       
  2481             break;
       
  2482             
       
  2483         case EDevASRLoadGrammar:
       
  2484             //	case EDevASRGrammarLoaded:		
       
  2485             if ( aError != KErrNone )
       
  2486                 {
       
  2487                 RUBY_DEBUG1( "CSIControllerPlugin::HandleEventRecognize. EDevASRLoadGrammar failed with error [%d]", aError );
       
  2488 
       
  2489                 iSIModelBankDB->ResetAndDestroy();
       
  2490                 iSILexiconDB->ResetAndDestroy();
       
  2491                 iSIGrammarDB->ResetAndDestroy();
       
  2492                 iState = ESiPluginIdle;
       
  2493                 }
       
  2494             iControllerIf.SendSrsEvent( KUidAsrEventLoadGrammar, aError );
       
  2495             break;
       
  2496             
       
  2497         case EDevASRInitRecognitionBackend:
       
  2498             //case EDevASRRecBEInitialized:		
       
  2499             if ( aError == KErrNone )
       
  2500                 {
       
  2501                 iDevASR->InitFrontEnd( ESiRecognition );
       
  2502                 }
       
  2503             else
       
  2504                 {
       
  2505                 RUBY_DEBUG1( "CSIControllerPlugin::HandleEventRecognize. EDevASRInitRecognitionBackend failed with error [%d]", aError );
       
  2506 
       
  2507                 RecognitionReset();
       
  2508                 iControllerIf.SendSrsEvent( KUidAsrEventRecognition, KErrAsrInitializationFailure );
       
  2509                 }
       
  2510             break;
       
  2511             
       
  2512         case EDevASRInitFrontend:
       
  2513             //case EDevASRFEInitialized:
       
  2514             
       
  2515             if ( aError == KErrNone )
       
  2516                 {	
       
  2517                 iControllerIf.SendSrsEvent( KUidAsrEventRecognitionReady, KErrNone );
       
  2518                 }
       
  2519             else
       
  2520                 {
       
  2521                 RUBY_DEBUG1( "CSIControllerPlugin::HandleEventRecognize. EDevASRInitFrontend failed with error [%d]", aError );
       
  2522 
       
  2523                 RecognitionReset();
       
  2524                 iControllerIf.SendSrsEvent( KUidAsrEventRecognition, KErrAsrInitializationFailure );
       
  2525                 }
       
  2526             break;
       
  2527             
       
  2528         case EDevASRRecordStarted:
       
  2529             //case EDevASRRecordStarted:
       
  2530             
       
  2531             iControllerIf.SendSrsEvent( KUidAsrEventRecordStarted, aError );
       
  2532             break;
       
  2533             
       
  2534         case EDevASRRecord:
       
  2535             //case	EDevASRRecordFinished:
       
  2536             if ( aError != KErrNone )
       
  2537                 {
       
  2538                 RUBY_DEBUG1( "CSIControllerPlugin::HandleEventRecognize. EDevASRRecord failed with error [%d]", aError );
       
  2539 
       
  2540                 RecognitionReset();
       
  2541                 }
       
  2542             iControllerIf.SendSrsEvent( KUidAsrEventRecord, aError );
       
  2543             break;
       
  2544             
       
  2545         case EDevASRAdapt: // adaptation finished
       
  2546             //		case  EDevASRAdaptFinished: // adaptation finished
       
  2547             
       
  2548             if ( aError != KErrNone ) 
       
  2549                 {
       
  2550                 RUBY_DEBUG1( "CSIControllerPlugin::HandleEventRecognize. EDevASRAdapt failed with error [%d]", aError );
       
  2551 
       
  2552                 iControllerIf.SendSrsEvent( KUidAsrEventAdapt, aError );
       
  2553                 }
       
  2554             else 
       
  2555                 {
       
  2556                 // save the models , iModelBankID decided in loadmodel()
       
  2557                 TRAPD( error, 
       
  2558                     iSIDatabase->BeginTransactionL();
       
  2559                     iSIModelBankDB->SaveModelL( iModelBankID );
       
  2560                     iSIDatabase->CommitChangesL( ETrue );
       
  2561                     ); // TRAPD
       
  2562                 iControllerIf.SendSrsEvent( KUidAsrEventAdapt, error );
       
  2563                 }
       
  2564             break;
       
  2565         case EDevASREouDetected:
       
  2566             //case	EDevASRRecordFinished:
       
  2567             if ( aError != KErrNone )
       
  2568                 {
       
  2569                 RUBY_DEBUG1( "CSIControllerPlugin::HandleEventRecognize. EDevASREouDetected failed with error [%d]", aError );
       
  2570 
       
  2571                 RecognitionReset();
       
  2572                 }
       
  2573             iControllerIf.SendSrsEvent( KUidAsrEventEouDetected, aError );
       
  2574             break;
       
  2575             
       
  2576             //	case EDevASREouDetected:
       
  2577             
       
  2578         case EDevASRRecognize:
       
  2579             
       
  2580             {
       
  2581             if ( aError == KErrNone )
       
  2582                 {
       
  2583                 
       
  2584                 // Copy result into client result
       
  2585                 if ( ProcessRecognitionComplete() > 0 )
       
  2586                     {
       
  2587                     iResult = KErrNone;		 
       
  2588                     }
       
  2589                 else
       
  2590                     {
       
  2591                     iResult = KErrAsrNoMatch;
       
  2592                     }
       
  2593                 }
       
  2594             else if (aError ==KErrOverflow ||aError ==KErrArgument  )
       
  2595                 {
       
  2596                 RUBY_DEBUG1( "CSIControllerPlugin::HandleEventRecognize. EDevASRRecognize failed with error [%d]", aError );
       
  2597 
       
  2598                 iResult = aError;	 
       
  2599                 }
       
  2600             else 
       
  2601                 {
       
  2602                 RUBY_DEBUG1( "CSIControllerPlugin::HandleEventRecognize. EDevASRRecognize failed with error [%d]", aError );
       
  2603 
       
  2604                 if ( aError != KErrAsrNoSpeech &&
       
  2605                     aError != KErrAsrSpeechTooEarly &&
       
  2606                     aError != KErrAsrSpeechTooLong &&
       
  2607                     aError != KErrAsrSpeechTooShort &&
       
  2608                     aError != KErrTimedOut &&
       
  2609                     aError !=KErrOverflow &&
       
  2610                     aError !=KErrArgument 
       
  2611                     )
       
  2612                     {
       
  2613                     RecognitionReset();
       
  2614                     }
       
  2615                 iResult = aError;
       
  2616                 }
       
  2617             
       
  2618             iControllerIf.SendSrsEvent(KUidAsrEventRecognition, iResult);
       
  2619             
       
  2620             //	iControllerIf.SendSrsEvent(KUidAsrEventRecognition, iResult);
       
  2621             break;
       
  2622             }
       
  2623         case EDevASRPlayStarted:
       
  2624             iControllerIf.SendSrsEvent(KUidAsrEventPlayStarted, aError);
       
  2625             break;
       
  2626             
       
  2627         case EDevASRPlay:
       
  2628             //case EDevASRPlayFinished:
       
  2629             if ( aError != KErrNone )
       
  2630                 {
       
  2631                 RUBY_DEBUG1( "CSIControllerPlugin::HandleEventRecognize. EDevASRPlay failed with error [%d]", aError );
       
  2632 
       
  2633                 RecognitionReset();
       
  2634                 }
       
  2635             iControllerIf.SendSrsEvent(KUidAsrEventPlay, aError);
       
  2636             break;
       
  2637         case EDevASRActivateGrammar:
       
  2638             if ( aError != KErrNone )
       
  2639                 {
       
  2640                 RUBY_DEBUG1( "CSIControllerPlugin::HandleEventRecognize. EDevASRActivateGrammar failed with error [%d]", aError );
       
  2641 
       
  2642                 RecognitionReset();
       
  2643                 }
       
  2644             iControllerIf.SendSrsEvent(KUidAsrEventActivateGrammar, aError);
       
  2645             break;
       
  2646         case EDevASRDeactivateGrammar:
       
  2647             if ( aError != KErrNone )
       
  2648                 {
       
  2649                 RUBY_DEBUG1( "CSIControllerPlugin::HandleEventRecognize. EDevASRDeactivateGrammar failed with error [%d]", aError );
       
  2650 
       
  2651                 RecognitionReset();
       
  2652                 }
       
  2653             iControllerIf.SendSrsEvent(KUidAsrEventDeactivateGrammar, aError);
       
  2654             break;
       
  2655         case EDevASRUnloadGrammar:
       
  2656             if ( aError != KErrNone )
       
  2657                 {
       
  2658                 RUBY_DEBUG1( "CSIControllerPlugin::HandleEventRecognize. EDevASRUnloadGrammar failed with error [%d]", aError );
       
  2659 
       
  2660                 RecognitionReset();
       
  2661                 }
       
  2662             else 
       
  2663                 { 	
       
  2664                 TRAP_IGNORE( 
       
  2665                     iSIDatabase->BeginTransactionL();
       
  2666                     iSIGrammarDB->UnloadGrammarL( iGrammarID );
       
  2667                     iSIDatabase->CommitChangesL( ETrue );
       
  2668                     ); // TRAP_IGNORE
       
  2669                 }
       
  2670             
       
  2671             iControllerIf.SendSrsEvent(KUidAsrEventUnloadGrammar, aError);
       
  2672             break;
       
  2673         case EDevASRUnloadRule:
       
  2674             if ( aError != KErrNone )
       
  2675                 {
       
  2676                 RUBY_DEBUG1( "CSIControllerPlugin::HandleEventRecognize. EDevASRUnloadRule failed with error [%d]", aError );
       
  2677 
       
  2678                 //RecognitionReset();
       
  2679                 }
       
  2680             else 
       
  2681                 {
       
  2682                 TRAP_IGNORE( 
       
  2683                     iSIDatabase->BeginTransactionL();
       
  2684                     iSIGrammarDB->UnloadRuleL( iGrammarID, iRuleID );
       
  2685                     iSIDatabase->CommitChangesL( ETrue );
       
  2686                     ); // TRAPD
       
  2687                 }
       
  2688             iControllerIf.SendSrsEvent( KUidAsrEventUnloadRule, aError );
       
  2689             break;
       
  2690             
       
  2691         default:
       
  2692             // Unexpected or cancelled message
       
  2693             RUBY_DEBUG0( "CSIControllerPlugin::HandleEventRecognize. Unexpected." );
       
  2694 
       
  2695             break;
       
  2696         }
       
  2697         
       
  2698 }
       
  2699 
       
  2700 
       
  2701 // -----------------------------------------------------------------------------
       
  2702 // CSIControllerPlugin::HandleEventTrainL
       
  2703 // Handling of DevASR event in Train state.
       
  2704 // -----------------------------------------------------------------------------
       
  2705 //
       
  2706 void CSIControllerPlugin::HandleEventTrainL( TDevASREvent /*aEvent*/,
       
  2707 										     TDevASRError /*aError*/ )
       
  2708 {
       
  2709 	RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleEventTrainL" );
       
  2710 	User::Leave( KErrNotSupported );  
       
  2711 }
       
  2712 
       
  2713 
       
  2714 // -----------------------------------------------------------------------------
       
  2715 // CSIControllerPlugin::CreateNewRuleL
       
  2716 // Create a empty rule into a given grammar
       
  2717 // -----------------------------------------------------------------------------
       
  2718 //                                 
       
  2719 CSIRule* CSIControllerPlugin::CreateNewRuleL( CSICompiledGrammar* aGrammar ) 
       
  2720     {
       
  2721     RUBY_DEBUG_BLOCKL("CSIControllerPlugin::CreateNewRuleL");
       
  2722     TInt aID=0;
       
  2723     TInt Count=aGrammar->Count();
       
  2724     // existing myRuleVariantID
       
  2725     RArray<TSIRuleID> myIDs;
       
  2726     myIDs.Reset();
       
  2727     for( TInt i = 0; i < Count; i++ ) 
       
  2728         {
       
  2729         CSIRule* aRule=&(aGrammar->AtL(i));
       
  2730         myIDs.Append(aRule->RuleID());
       
  2731         } 
       
  2732     // Find a uniq new id 
       
  2733     iSIDatabase->BeginTransactionL();
       
  2734     aID=iSIGrammarDB->GetNewID(myIDs);
       
  2735     iSIDatabase->CommitChangesL( EFalse );
       
  2736     
       
  2737     myIDs.Close(); 
       
  2738     CSIRule* aRule = CSIRule::NewL(aID); 
       
  2739     return( aRule );
       
  2740     } 
       
  2741 
       
  2742 // -----------------------------------------------------------------------------
       
  2743 // CSIControllerPlugin::AddNewRuleVariantL
       
  2744 // Create new rule variant within a rule
       
  2745 // -----------------------------------------------------------------------------
       
  2746 //      
       
  2747 void CSIControllerPlugin::AddNewRuleVariantL( CSIRule& aRule, 
       
  2748                                               TSILexiconID aLexiconID, 
       
  2749                                               RArray<TSIPronunciationID>& aPronunciationIDs, 
       
  2750                                               CSIParameters& aParameters ) 	
       
  2751     {
       
  2752     RUBY_DEBUG_BLOCKL( "CSIControllerPlugin::AddNewRuleVariantL" );
       
  2753     // existing myRuleVariantID
       
  2754     RArray<TSIRuleID> myID;
       
  2755     myID.Reset();
       
  2756     TInt i( 0 );
       
  2757     for( i = 0; i < aRule.Count(); i++ ) {
       
  2758         CSIRuleVariant* aRuleVariant=&(aRule.AtL(i));
       
  2759         myID.Append(aRuleVariant->RuleVariantID());
       
  2760         }
       
  2761     
       
  2762     iSIDatabase->BeginTransactionL();
       
  2763     
       
  2764     // Find a uniq new id 
       
  2765     TSIRuleVariantID aRuleVariantID=STATIC_CAST(TSIRuleVariantID,iSIGrammarDB->GetNewID(myID));
       
  2766     myID.Close();	
       
  2767     
       
  2768     iSIDatabase->CommitChangesL( ETrue );
       
  2769     
       
  2770     // add the rule variant to the rule
       
  2771     CSIRuleVariant* ruleVariant= CSIRuleVariant::NewL(aRuleVariantID,aLexiconID);
       
  2772     CleanupStack::PushL( ruleVariant );	
       
  2773     ruleVariant->SetPronunciationIDsL( aPronunciationIDs );
       
  2774     RArray<TInt> parameterIDs;
       
  2775     CleanupClosePushL( parameterIDs );
       
  2776     RArray<TInt> parameterValues;
       
  2777     CleanupClosePushL( parameterValues );
       
  2778     
       
  2779     // copy parameters
       
  2780     aParameters.ListParametersL( parameterIDs, parameterValues );
       
  2781     
       
  2782     if ( parameterIDs.Count() != parameterValues.Count() )
       
  2783         {
       
  2784         User::Leave( KErrCorrupt );
       
  2785         }
       
  2786     for ( i = 0; i < parameterIDs.Count(); i++ )
       
  2787         {
       
  2788         ruleVariant->SetParameterL( parameterIDs[i], parameterValues[i] );
       
  2789         }
       
  2790     
       
  2791     CleanupStack::PopAndDestroy(); // parameterValues
       
  2792     CleanupStack::PopAndDestroy(); // parameterIDs
       
  2793     aRule.AddL( ruleVariant );
       
  2794     CleanupStack::Pop( ruleVariant );
       
  2795     }
       
  2796 
       
  2797 // -----------------------------------------------------------------------------
       
  2798 // CSIControllerPlugin::CreateNewPronunciationL
       
  2799 // Create a empty pronunciation into a given lexicon
       
  2800 // -----------------------------------------------------------------------------
       
  2801 //                                 
       
  2802 TSIPronunciationID CSIControllerPlugin::CreateNewPronunciationL( CSILexicon* aLexicon, 
       
  2803                                                                  TDesC8& aPronunciationPr,
       
  2804 												                 TSIModelBankID aModelBankID) 	
       
  2805     {
       
  2806     RUBY_DEBUG_BLOCKL( "CSIControllerPlugin::CreateNewPronunciationL" );
       
  2807     
       
  2808     TSIPronunciationID pronunciationID( 0 );
       
  2809     
       
  2810     if ( aLexicon->Count() )
       
  2811         {
       
  2812         pronunciationID = aLexicon->AtL( aLexicon->Count() - 1 ).PronunciationID() + 1;
       
  2813         }
       
  2814     
       
  2815     //    RUBY_DEBUG1("pronunID = %i", pronunciationID );
       
  2816     // add the Pronunciation  to the Pronunciation
       
  2817     CSIPronunciation* pronunciation = CSIPronunciation::NewL( pronunciationID, aModelBankID );
       
  2818     
       
  2819     CleanupStack::PushL( pronunciation );	
       
  2820     pronunciation->SetPhonemeSequenceL( aPronunciationPr );
       
  2821     TRAPD( error, aLexicon->AddL( pronunciation ) );
       
  2822     if ( error == KErrAlreadyExists )
       
  2823         {
       
  2824 #ifdef __SIND_LEXICON_OPT
       
  2825         // Take the existing pronunciation ID
       
  2826         TInt index = aLexicon->Find( aPronunciationPr );
       
  2827         pronunciationID = aLexicon->AtL( index ).PronunciationID();
       
  2828         
       
  2829         CleanupStack::PopAndDestroy( pronunciation ); 
       
  2830 #else       
       
  2831         // try to find non-existing id in the middle of range
       
  2832         RUBY_DEBUG0( "pronunID already exists" );
       
  2833         RArray<TSIPronunciationID> myPronunciationID;
       
  2834         myPronunciationID.Reset();
       
  2835         for ( TInt i = 0; i < aLexicon->Count(); i++ )
       
  2836             {
       
  2837             CSIPronunciation* tmpPronunciation=&( aLexicon->AtL( i ) );
       
  2838             myPronunciationID.Append( tmpPronunciation->PronunciationID() );
       
  2839             }
       
  2840         
       
  2841         // Find a uniq new id 
       
  2842         iSIDatabase->BeginTransactionL();
       
  2843         pronunciationID = iSILexiconDB->GetNewID( myPronunciationID );
       
  2844         iSIDatabase->CommitChangesL( ETrue );
       
  2845         
       
  2846         pronunciation->SetPronunciationID( pronunciationID );
       
  2847         
       
  2848         myPronunciationID.Close();
       
  2849         aLexicon->AddL( pronunciation );
       
  2850 #endif // __SIND_LEXICON_OPT
       
  2851         }
       
  2852     else
       
  2853         {
       
  2854         User::LeaveIfError( error );
       
  2855 #ifdef __SIND_LEXICON_OPT        
       
  2856         CleanupStack::Pop( pronunciation );     
       
  2857 #endif // __SIND_LEXICON_OPT
       
  2858         }
       
  2859 
       
  2860 #ifndef __SIND_LEXICON_OPT
       
  2861     CleanupStack::Pop( pronunciation );
       
  2862 #endif // __SIND_LEXICON_OPT
       
  2863     
       
  2864     RUBY_DEBUG1( "CSIControllerPlugin::CreateNewPronunciationL pronunciationID=%x", pronunciationID );
       
  2865     return pronunciationID;    
       
  2866     }	
       
  2867 
       
  2868 
       
  2869 // -----------------------------------------------------------------------------
       
  2870 // CSIControllerPlugin::UpdateGrammarAndLexiconDBL
       
  2871 // Save TTP result into the given grammar and lexicon of the plugin database
       
  2872 // -----------------------------------------------------------------------------
       
  2873 //                                 
       
  2874 void CSIControllerPlugin::UpdateGrammarAndLexiconDBL( CSITtpWordList* aSITtpWordList, 
       
  2875                                                       TSILexiconID aLexiconID, 
       
  2876                                                       TSIGrammarID aGrammarID, 
       
  2877                                                       RArray<TSIRuleID>& aRuleIDs )
       
  2878     {
       
  2879     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::UpdateGrammarAndLexiconDBL" );
       
  2880     iSITtpWordList = aSITtpWordList;
       
  2881     iLexiconID = aLexiconID;
       
  2882     iGrammarID = aGrammarID;
       
  2883     iRuleIDs = &aRuleIDs;
       
  2884     
       
  2885     aRuleIDs.Reset();
       
  2886     
       
  2887     iSIDatabase->BeginTransactionL();
       
  2888     
       
  2889     // is grammar already in database?
       
  2890     iSIGrammarDB->VerifyOwnershipL( iClientUid, KGrammarIdTable, KGrammarIndex, iGrammarID ); 
       
  2891     CSICompiledGrammar*  aGrammar = (CSICompiledGrammar* )iSIGrammarDB->GrammarL( iGrammarID );
       
  2892     CleanupStack::PushL( aGrammar );	
       
  2893     
       
  2894     //is lexicion already in database?
       
  2895     iSILexiconDB->VerifyOwnershipL( iClientUid, KLexiconIdTable, KLexiconIndex, iLexiconID );		   
       
  2896     CSILexicon* aLexicon = iSILexiconDB->LexiconL( iLexiconID );
       
  2897     CleanupStack::PushL( aLexicon );	
       
  2898     
       
  2899     iSIDatabase->CommitChangesL( EFalse );  
       
  2900     
       
  2901     
       
  2902     // unpack the iSITtpWordList;
       
  2903     for ( TInt i = 0; i < iSITtpWordList->Count(); i++ )
       
  2904         {
       
  2905         //iSIDatabase->BeginTransactionL();  
       
  2906         // Get pronunciations
       
  2907         RPointerArray<CSIPronunciationInfo> pronunciations;
       
  2908         RArray<TSIPronunciationID> pronunciationIDs;
       
  2909         CleanupClosePushL( pronunciationIDs );
       
  2910         CleanupClosePushL( pronunciations );
       
  2911         iSITtpWordList->GetPronunciationsL( i, pronunciations );
       
  2912    
       
  2913         RUBY_DEBUG1( "CSIControllerPlugin::UpdateGrammarAndLexiconDBL pronunciations.Count=%d", pronunciations.Count() );
       
  2914         
       
  2915         // if pronunciation generation failed, skip that word.
       
  2916         if ( pronunciations.Count() == 0 )
       
  2917             {
       
  2918             CleanupStack::PopAndDestroy( &pronunciations ); 
       
  2919             CleanupStack::PopAndDestroy( &pronunciationIDs );
       
  2920             User::LeaveIfError( aRuleIDs.Append( KInvalidRuleID ) );
       
  2921             continue;
       
  2922             }
       
  2923         
       
  2924         // Create a empty rule into grammar, one name -> one rule; 
       
  2925         TSIRuleID ruleID;
       
  2926         
       
  2927         
       
  2928         CSIRule* rule=CreateNewRuleL(aGrammar);
       
  2929         CleanupStack::PushL(  rule );	
       
  2930         ruleID=rule->RuleID();
       
  2931         User::LeaveIfError( aRuleIDs.Append( ruleID ) );
       
  2932         
       
  2933         for(TInt k=0;k<pronunciations.Count();k++) 
       
  2934             {
       
  2935             pronunciationIDs.Reset();
       
  2936             
       
  2937             for ( TInt n = 0; n < pronunciations[k]->Count(); n++ ) 
       
  2938                 {	
       
  2939                 // Add Prounication into lexicon
       
  2940                 
       
  2941                 TSIPronunciationID aPronunciationID = CreateNewPronunciationL( aLexicon,pronunciations[k]->PronunciationL( n ),
       
  2942                     iModelBankID );
       
  2943                 User::LeaveIfError( pronunciationIDs.Append( aPronunciationID ) ); 
       
  2944                 }
       
  2945             
       
  2946             // Add RuleVariant into grammar
       
  2947             AddNewRuleVariantL(*rule, aLexiconID, pronunciationIDs, *pronunciations[k] );                          
       
  2948             }
       
  2949         
       
  2950         
       
  2951         aGrammar->AddL( rule );
       
  2952         CleanupStack::Pop( rule );
       
  2953         
       
  2954         // Close the arrays
       
  2955         CleanupStack::PopAndDestroy( &pronunciations ); 
       
  2956         CleanupStack::PopAndDestroy( &pronunciationIDs );
       
  2957         }
       
  2958     
       
  2959     RUBY_DEBUG0( "CSIControllerPlugin::UpdateGrammarAndLexiconDBL" );
       
  2960     
       
  2961     iSIDatabase->BeginTransactionL();
       
  2962     iSIGrammarDB->UpdateGrammarL( iClientUid,aGrammar );
       
  2963     iSILexiconDB->UpdateLexiconL( iClientUid,aLexicon );
       
  2964     iSIDatabase->CommitChangesL( ETrue );  
       
  2965     CleanupStack::PopAndDestroy( 2 ); // aGrammar aLexicon	
       
  2966     }
       
  2967 
       
  2968 // -----------------------------------------------------------------------------
       
  2969 // CSIControllerPlugin::RecognitionReset
       
  2970 // Terminate recognition session.  Free up resources and return to IDLE state.
       
  2971 // -----------------------------------------------------------------------------
       
  2972 //                                 
       
  2973 void CSIControllerPlugin::RecognitionReset()
       
  2974     {
       
  2975     iSIModelBankDB->ResetAndDestroy();
       
  2976     iSILexiconDB->ResetAndDestroy();  // Grcompiler takes the ownership of it
       
  2977     iSIGrammarDB->ResetAndDestroy();
       
  2978     iState = ESiPluginIdle;
       
  2979     }
       
  2980 
       
  2981 // -----------------------------------------------------------------------------
       
  2982 // CSIControllerPlugin::ProcessTrainCompleteL
       
  2983 // Training is complete.  Store the acoustic model and user utterance into the
       
  2984 // model bank.
       
  2985 // -----------------------------------------------------------------------------
       
  2986 //
       
  2987 void CSIControllerPlugin::ProcessTrainCompleteL()
       
  2988     {
       
  2989     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::ProcessTrainCompleteL" );
       
  2990     // No need to train it at all  
       
  2991     }
       
  2992 
       
  2993 // -----------------------------------------------------------------------------
       
  2994 // CSIControllerPlugin::ProcessRecognitionCompleteL
       
  2995 // Recogntion is complete.  Transfer the result from the recognizer into the result
       
  2996 // object sent by the client.
       
  2997 // -----------------------------------------------------------------------------
       
  2998 //
       
  2999 TInt CSIControllerPlugin::ProcessRecognitionComplete()
       
  3000     {
       
  3001     RUBY_DEBUG0( "CSIControllerPlugin::ProcessRecognitionComplete" );
       
  3002     
       
  3003     const CSIResult* result = 0;
       
  3004     CSIClientResult* clientResult = 0;
       
  3005     
       
  3006     TInt resultsAvailable = iSIResultSet->Count();
       
  3007     TInt resultsWanted    = iSIClientResultSet->MaxResults();
       
  3008     TInt count            = Min( resultsAvailable, resultsWanted );
       
  3009     
       
  3010     // Copy the results from the recognizer into client's result set object
       
  3011     TInt resCount = 0;
       
  3012     
       
  3013     
       
  3014     for ( TInt i = 0; i < count; i++ )
       
  3015         {
       
  3016         TRAPD( err, result = &( iSIResultSet->AtL( i ) ) );
       
  3017         if ( err )
       
  3018             {
       
  3019             return 0; 	 	
       
  3020             }
       
  3021         RUBY_DEBUG2( "CSIControllerPlugin::ProcessRecognitionComplete rank(%d): grammarId(%d)", i+1, result->GrammarID() );
       
  3022         RUBY_DEBUG2( "CSIControllerPlugin::ProcessRecognitionComplete ruleId(%d), score(%d)", result->RuleID(), result->Score() );
       
  3023 
       
  3024         if ( result->Score() > 0 )
       
  3025             {
       
  3026             TRAP( err, clientResult = CSIClientResult::NewL( result->GrammarID(), 
       
  3027                                                              result->RuleID() ) );
       
  3028             if ( err ) 
       
  3029                 {
       
  3030                 return 0; 	 	
       
  3031                 }			
       
  3032             TRAP( err, iSIClientResultSet->AddL( clientResult ) );
       
  3033             if ( err ) 
       
  3034                 {
       
  3035                 return 0; 	 	
       
  3036                 }
       
  3037             resCount++;
       
  3038             }
       
  3039         else
       
  3040             {
       
  3041             break;
       
  3042             }
       
  3043         }
       
  3044     
       
  3045     iSIClientResultSet->SetResultCount( resCount );
       
  3046     return resCount;
       
  3047     } 
       
  3048 
       
  3049 // -----------------------------------------------------------------------------
       
  3050 // CSIControllerPlugin::DoCancel
       
  3051 // Cancel handle from CActive class.
       
  3052 // -----------------------------------------------------------------------------
       
  3053 //
       
  3054 void CSIControllerPlugin::DoCancel()
       
  3055     {
       
  3056     }
       
  3057 
       
  3058 // -----------------------------------------------------------------------------
       
  3059 // CSIControllerPlugin::RunL
       
  3060 // RunL from CActive class.  Check which message got saved and call the
       
  3061 // appropriate handle function.
       
  3062 // -----------------------------------------------------------------------------
       
  3063 //
       
  3064 void CSIControllerPlugin::RunL()
       
  3065     {
       
  3066     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::RunL" );
       
  3067     switch ( iRequestFunction )
       
  3068         {
       
  3069         case KAddRule:
       
  3070             TRAP( iResult, HandleAddRuleL( iGrammarID, iLexiconID, iPronunciationID, *iRuleIDPtr ) );
       
  3071             iControllerIf.SendSrsEvent( KUidAsrEventAddRule, iResult );
       
  3072             break;
       
  3073             
       
  3074         case KCreateGrammar:
       
  3075             TRAP( iResult, HandleCreateGrammarL( *iGrammarIDPtr ) );
       
  3076             iControllerIf.SendSrsEvent( KUidAsrEventCreateGrammar, iResult );
       
  3077             break;
       
  3078             
       
  3079         case KCreateLexicon: 
       
  3080             TRAP( iResult, HandleCreateLexiconL( *iLexiconIDPtr ) );
       
  3081             iControllerIf.SendSrsEvent( KUidAsrEventCreateLexicon, iResult );
       
  3082             break;
       
  3083             
       
  3084         case KCreateModelBank:
       
  3085             TRAP( iResult, HandleCreateModelBankL( *iModelBankIDPtr ) );
       
  3086             iControllerIf.SendSrsEvent( KUidAsrEventCreateModelBank, iResult );
       
  3087             break;
       
  3088             
       
  3089         case KGetAllClientGrammarIDs:
       
  3090             TRAP( iResult, HandleGetAllClientGrammarIDsL( *iGrammarIDs ) );
       
  3091             iControllerIf.SendSrsEvent( KUidAsrEventGetAllClientGrammarIDs, iResult );
       
  3092             break;
       
  3093             
       
  3094         case KGetAllClientLexiconIDs:
       
  3095             TRAP( iResult, HandleGetAllClientLexiconIDsL( *iLexiconIDs ) );
       
  3096             iControllerIf.SendSrsEvent( KUidAsrEventGetAllClientLexiconIDs, iResult );
       
  3097             break;
       
  3098             
       
  3099         case KGetAllClientModelBankIDs:
       
  3100             TRAP( iResult, HandleGetAllClientModelBankIDsL( *iModelBankIDs ) );
       
  3101             iControllerIf.SendSrsEvent( KUidAsrEventGetAllClientModelBankIDs, iResult );
       
  3102             break;
       
  3103             
       
  3104         case KGetAllGrammarIDs:
       
  3105             TRAP( iResult, HandleGetAllGrammarIDsL( *iGrammarIDs ) );
       
  3106             iControllerIf.SendSrsEvent( KUidAsrEventGetAllGrammarIDs, iResult );
       
  3107             break;
       
  3108             
       
  3109         case KGetAllLexiconIDs:
       
  3110             TRAP( iResult, HandleGetAllLexiconIDsL( *iLexiconIDs ) );
       
  3111             iControllerIf.SendSrsEvent( KUidAsrEventGetAllLexiconIDs, iResult );
       
  3112             break;
       
  3113             
       
  3114         case KGetAllModelBankIDs:
       
  3115             TRAP( iResult, HandleGetAllModelBankIDsL( *iModelBankIDs ) );
       
  3116             iControllerIf.SendSrsEvent( KUidAsrEventGetAllModelBankIDs, iResult );
       
  3117             break;
       
  3118             
       
  3119         case KGetAllModelIDs:
       
  3120             TRAP( iResult, HandleGetAllModelIDsL( iModelBankID, *iModelIDs ) );
       
  3121             iControllerIf.SendSrsEvent( KUidAsrEventGetAllModelIDs, iResult );
       
  3122             break;
       
  3123             
       
  3124         case KGetAllPronunciationIDs:
       
  3125             TRAP( iResult, HandleGetAllPronunciationIDsL( iLexiconID, *iPronunciationIDs ) );
       
  3126             iControllerIf.SendSrsEvent( KUidAsrEventGetAllPronunciationIDs, iResult );
       
  3127             break;
       
  3128             
       
  3129         case KGetAllRuleIDs:
       
  3130             TRAP( iResult, HandleGetAllRuleIDsL( iGrammarID, *iRuleIDs ) );
       
  3131             iControllerIf.SendSrsEvent( KUidAsrEventGetAllRuleIDs, iResult );
       
  3132             break;
       
  3133             
       
  3134         case KGetAvailableStorage:
       
  3135             TRAP( iResult, HandleGetAvailableStorageL( *iCountPtr ) );
       
  3136             iControllerIf.SendSrsEvent( KUidAsrEventGetAvailableStorage, iResult );
       
  3137             break;
       
  3138             
       
  3139         case KGetModelCount:
       
  3140             TRAP( iResult, HandleGetModelCountL( iModelBankID, *iCountPtr ) );
       
  3141             iControllerIf.SendSrsEvent( KUidAsrEventGetModelCount, iResult );
       
  3142             break;
       
  3143             
       
  3144         case KGetPronunciationCount:
       
  3145             TRAP( iResult, HandleGetPronunciationCountL( iLexiconID, *iCountPtr ) );
       
  3146             iControllerIf.SendSrsEvent( KUidAsrEventGetPronunciationCount, iResult );
       
  3147             break;
       
  3148             
       
  3149         case KGetRuleCount:
       
  3150             TRAP( iResult, HandleGetRuleCountL( iGrammarID, *iCountPtr ) );
       
  3151             iControllerIf.SendSrsEvent( KUidAsrEventGetRuleCount, iResult );
       
  3152             break;	
       
  3153         case KGetRuleValidity:
       
  3154             TRAP( iResult, HandleGetRuleValidityL( iGrammarID, iRuleID, *iValidPtr ) );
       
  3155             iControllerIf.SendSrsEvent( KUidAsrEventGetRuleValidity, iResult );
       
  3156             break;
       
  3157             
       
  3158         case KGetUtteranceDuration:
       
  3159             TRAP( iResult, HandleGetUtteranceDurationL( iModelBankID, iModelID, *iDurationPtr ) );
       
  3160             iControllerIf.SendSrsEvent( KUidAsrEventGetUtteranceDuration, iResult );
       
  3161             break;
       
  3162             
       
  3163         case KLoadGrammar:
       
  3164             TRAP( iResult, HandleLoadGrammarL( iGrammarID ) );
       
  3165             if ( iResult != KErrNone )
       
  3166                 {
       
  3167                 RUBY_DEBUG1( "CSIControllerPlugin::RunL. KLoadGrammar. HandleLoadGrammarL Left with error [%d]", iResult );
       
  3168                 // iGrammarDB needs to be reset also since multiple grammars can
       
  3169                 // be loaded and this may not be the first grammar.
       
  3170                 iSIModelBankDB->ResetAndDestroy();
       
  3171                 iSILexiconDB->ResetAndDestroy();
       
  3172                 iSIGrammarDB->ResetAndDestroy();
       
  3173                 iState = ESiPluginIdle;
       
  3174                 iControllerIf.SendSrsEvent( KUidAsrEventLoadGrammar, iResult );
       
  3175                 }
       
  3176             break;
       
  3177             
       
  3178         case KLoadLexicon:
       
  3179             TRAP(iResult, HandleLoadLexiconL( iLexiconID ) );
       
  3180             if ( iResult != KErrNone )
       
  3181                 {
       
  3182                 RUBY_DEBUG1( "CSIControllerPlugin::RunL. KLoadLexicon. HandleLoadGrammarL Left with error [%d]", iResult );
       
  3183 
       
  3184                 // iLexiconDB needs to be reset also since multiple lexicons can
       
  3185                 // be loaded and this may not be the first lexicon.
       
  3186                 iSIModelBankDB->ResetAndDestroy();
       
  3187                 iSILexiconDB->ResetAndDestroy();
       
  3188                 iState = ESiPluginIdle;
       
  3189                 iControllerIf.SendSrsEvent( KUidAsrEventLoadLexicon, iResult );
       
  3190                 }
       
  3191             break;
       
  3192             
       
  3193         case KLoadModels:
       
  3194             TRAP(iResult, HandleLoadModelsL( iModelBankID ) );
       
  3195             if ( iResult != KErrNone )
       
  3196                 {
       
  3197                 RUBY_DEBUG1( "CSIControllerPlugin::RunL. KLoadModels. HandleLoadGrammarL Left with error [%d]", iResult );
       
  3198                 
       
  3199                 if ( iResult == KErrCorrupt )
       
  3200                     {
       
  3201                     RUBY_DEBUG0( "CSIControllerPlugin::RunL. KLoadModels. Try to update models" );
       
  3202                     TRAP( iResult,
       
  3203                         iSIModelBankDB->UpdateModelBankIfInvalidL( iModelBankID );
       
  3204                         HandleLoadModelsL( iModelBankID );
       
  3205                     );
       
  3206                     }
       
  3207 
       
  3208                 if ( iResult != KErrNone )
       
  3209                     {
       
  3210                     // iModelBankDB needs to be reset also since multiple model banks can
       
  3211                     // be loaded and this may not be the first model bank.
       
  3212                     iSIModelBankDB->ResetAndDestroy();
       
  3213                     iState = ESiPluginIdle;
       
  3214                     
       
  3215                     iControllerIf.SendSrsEvent( KUidAsrEventLoadModels, iResult );
       
  3216                     }
       
  3217                 }
       
  3218             break;
       
  3219             
       
  3220         case KRecognize:
       
  3221             TRAP( iResult, HandleRecognizeL() );
       
  3222             if ( iResult != KErrNone )
       
  3223                 {
       
  3224                 RUBY_DEBUG1( "CSIControllerPlugin::RunL. KRecognize. HandleLoadGrammarL Left with error [%d]", iResult );
       
  3225 
       
  3226                 if ( iResult != KErrAsrNoMatch )
       
  3227                     {
       
  3228                     RecognitionReset();
       
  3229                     }
       
  3230                 iControllerIf.SendSrsEvent( KUidAsrEventRecognition, iResult );
       
  3231                 }
       
  3232             break;
       
  3233             
       
  3234         case KRecord:
       
  3235             TRAP( iResult, HandleRecordL( iRecordTime ) );
       
  3236             if ( iResult != KErrNone )
       
  3237                 {
       
  3238                 RUBY_DEBUG1( "CSIControllerPlugin::RunL. KRecord. HandleLoadGrammarL Left with error [%d]", iResult );
       
  3239 
       
  3240                 iControllerIf.SendSrsEvent( KUidAsrEventRecord, iResult );
       
  3241                 }
       
  3242             break;
       
  3243             
       
  3244         case KRemoveGrammar:
       
  3245             TRAP( iResult, HandleRemoveGrammarL( iGrammarID ) );
       
  3246             iControllerIf.SendSrsEvent( KUidAsrEventRemoveGrammar, iResult );
       
  3247             break;
       
  3248             
       
  3249         case KRemoveLexicon:
       
  3250             TRAP( iResult, HandleRemoveLexiconL( iLexiconID ) );
       
  3251             iControllerIf.SendSrsEvent( KUidAsrEventRemoveLexicon, iResult );
       
  3252             break;
       
  3253             
       
  3254         case KRemoveModelBank:
       
  3255             TRAP( iResult, HandleRemoveModelBankL( iModelBankID ) );
       
  3256             iControllerIf.SendSrsEvent( KUidAsrEventRemoveModelBank, iResult );
       
  3257             break;
       
  3258             
       
  3259         case KRemoveModel:
       
  3260             TRAP( iResult, HandleRemoveModelL( iModelBankID, iModelID ) );
       
  3261             iControllerIf.SendSrsEvent( KUidAsrEventRemoveModel, iResult );
       
  3262             break;
       
  3263             
       
  3264         case KRemovePronunciation:
       
  3265             TRAP( iResult, HandleRemovePronunciationL( iLexiconID, iPronunciationID ) );
       
  3266             iControllerIf.SendSrsEvent( KUidAsrEventRemovePronunciation, iResult );
       
  3267             break;
       
  3268             
       
  3269         case KRemoveRule:
       
  3270             TRAP( iResult, HandleRemoveRuleL( iGrammarID, iRuleID ) );
       
  3271             if ( iResult != KErrNone )
       
  3272                 {
       
  3273                 RUBY_DEBUG1( "CSIControllerPlugin::RunL. KRemoveRule. HandleLoadGrammarL Left with error [%d]", iResult );
       
  3274 
       
  3275                 iControllerIf.SendSrsEvent( KUidAsrEventRemoveRule, iResult );
       
  3276                 }
       
  3277             break;
       
  3278         case KRemoveRules:
       
  3279             TRAP( iResult, HandleRemoveRulesL( iGrammarID, *iRuleIDs ) );
       
  3280             if ( iResult != KErrNone )
       
  3281                 {
       
  3282                 RUBY_DEBUG1( "CSIControllerPlugin::RunL. KRemoveRules. HandleLoadGrammarL Left with error [%d]", iResult );
       
  3283 
       
  3284                 iControllerIf.SendSrsEvent( KUidAsrEventRemoveRules, iResult );
       
  3285                 }
       
  3286             break;
       
  3287             
       
  3288         case KTrain:
       
  3289             TRAP( iResult, HandleTrainL( iModelBankID ) );
       
  3290             if ( iResult != KErrNone )
       
  3291                 {
       
  3292                 RUBY_DEBUG1( "CSIControllerPlugin::RunL. KTrain. HandleLoadGrammarL Left with error [%d]", iResult );
       
  3293 
       
  3294                 iControllerIf.SendSrsEvent( KUidAsrEventTrain, iResult );
       
  3295                 }
       
  3296             break;
       
  3297             
       
  3298         case KUnloadRule:
       
  3299             TRAP( iResult, HandleUnloadRuleL(iGrammarID, iRuleID));
       
  3300             if ( iResult != KErrNone )
       
  3301                 {
       
  3302                 RUBY_DEBUG1( "CSIControllerPlugin::RunL. KUnloadRule. HandleLoadGrammarL Left with error [%d]", iResult );
       
  3303 
       
  3304                 iControllerIf.SendSrsEvent( KUidAsrEventUnloadRule, iResult );
       
  3305                 }
       
  3306             break;
       
  3307             
       
  3308             
       
  3309             // SI Component
       
  3310         case KAdapt:
       
  3311             TRAP( iResult, HandleAdaptL(*iSIClientResultSet ,iCorrect)) ;
       
  3312             if ( iResult != KErrNone )
       
  3313                 {
       
  3314                 RUBY_DEBUG1( "CSIControllerPlugin::RunL. KAdapt. HandleLoadGrammarL Left with error [%d]", iResult );
       
  3315 
       
  3316                 iControllerIf.SendSrsEvent( KUidAsrEventAdapt , iResult );
       
  3317                 }
       
  3318             break;
       
  3319             /*	case KSIAddPronunciation:
       
  3320             TRAP(iResult, HandleAddPronunciationL(iLexiconID, iTrainTextPtr,
       
  3321             iLanguage, iPronunciationIDPtr)) ;
       
  3322             iControllerIf.SendSrsEvent(KUidAsrEventAddPronunciation, iResult);
       
  3323             break;
       
  3324             */		
       
  3325         case KAddRuleVariant:
       
  3326             TRAP( iResult, HandleAddRuleVariantL( iGrammarID, iLexiconID, *iPronunciationIDs, iRuleID, *iRuleVariantIDPtr ) );
       
  3327             iControllerIf.SendSrsEvent( KUidAsrEventAddRuleVariant, iResult );				
       
  3328             break;
       
  3329             /*	
       
  3330             case KAddVoiceTag:
       
  3331             TRAP(iResult, HandleAddVoiceTagL( *iTrainArray, *iLanguageArray, 
       
  3332             iLexiconID, iGrammarID, iRuleIDPtr)) ;
       
  3333             iControllerIf.SendSrsEvent(KUidAsrEventAddVoiceTag, iResult);
       
  3334             break;
       
  3335             */	
       
  3336         case KAddVoiceTags:
       
  3337             TRAP( iResult, HandleAddVoiceTagsL( iTrainArrays, *iLanguageArray, iLexiconID,
       
  3338                 iGrammarID /* ,*iRuleIDs */) );
       
  3339             // AddVoiceTags contain several async functions,
       
  3340             // KUidAsrEventAddVoiceTags will be sent when the AddVoiceTags complete
       
  3341             if ( iResult != KErrNone )
       
  3342                 {
       
  3343                 RUBY_DEBUG1( "CSIControllerPlugin::RunL. KAddVoiceTags. HandleLoadGrammarL Left with error [%d]", iResult );
       
  3344 
       
  3345                 // Clean up the iTrainArrays
       
  3346                 iTrainArrays->ResetAndDestroy();
       
  3347                 iTrainArrays->Close();
       
  3348                 delete iTrainArrays;
       
  3349                 iTrainArrays = NULL;
       
  3350                 // Send error message
       
  3351                 iControllerIf.SendSrsEvent( KUidAsrEventAddVoiceTags, iResult );
       
  3352                 }
       
  3353             iTrainArrays = NULL;                
       
  3354             break;
       
  3355 
       
  3356 #ifdef __SINDE_TRAINING
       
  3357         case KAddSindeVoiceTags:
       
  3358             RUBY_DEBUG0( "CSIControllerPlugin::RunL KAddSindeVoiceTags" );
       
  3359             
       
  3360 #ifdef _DEBUG
       
  3361             for ( TInt i = 0; i < iLanguageArrayArray->Count(); i++ )
       
  3362                 {
       
  3363                 RUBY_DEBUG1( "Contents of element %d:", i );
       
  3364                 RLanguageArray langArr = (*iLanguageArrayArray)[i];
       
  3365                 for ( TInt j = 0; j < langArr.Count(); j++ )
       
  3366                     {
       
  3367                     RUBY_DEBUG2( "Index: %d Language: %d", j, langArr[j] );
       
  3368                    }
       
  3369                 }
       
  3370 #endif // _DEBUG
       
  3371 
       
  3372             TRAP( iResult, HandleAddSindeVoiceTagsL( iTrainArrays, *iLanguageArrayArray, iLexiconID, iGrammarID ) );
       
  3373 
       
  3374             if ( iResult != KErrNone ) 
       
  3375                 {
       
  3376                 iTrainArrays->ResetAndDestroy();
       
  3377                 iTrainArrays->Close();
       
  3378                 delete iTrainArrays;
       
  3379                 iTrainArrays = NULL;
       
  3380                 
       
  3381                 // Send error message
       
  3382                 iControllerIf.SendSrsEvent( KUidAsrEventAddVoiceTags, iResult );
       
  3383                 }
       
  3384             iTrainArrays = NULL;
       
  3385             break;
       
  3386 #endif
       
  3387         case KCreateRule:
       
  3388             TRAP(iResult, HandleCreateRuleL( iGrammarID,*iRuleIDPtr ) );
       
  3389             iControllerIf.SendSrsEvent( KUidAsrEventCreateRule, iResult );
       
  3390             break;
       
  3391             /*	case KSIRecognize:
       
  3392             TRAP(iResult, HandleRecognizeL(*(CSIClientResultSet*)iSIClientResultSet)) ;
       
  3393             //        iControllerIf.SendSrsEvent(KUidAsrEventSIRecognize, iResult);
       
  3394             break;
       
  3395             */
       
  3396         case KEndRecord:
       
  3397             TRAP( iResult, HandleEndRecordL( ) );
       
  3398             iControllerIf.SendSrsEvent( KUidAsrEventEndRecord, iResult );
       
  3399             break;
       
  3400         case KUnloadGrammar:
       
  3401             TRAP( iResult, HandleUnloadGrammarL( iGrammarID ) ) ;
       
  3402             // UnloadGrammar contain several async functions,
       
  3403             // KUidAsrEventUnloadGrammar will be sent when the UnloadGrammar complete		
       
  3404             //iControllerIf.SendSrsEvent(KUidAsrEventUnloadGrammar, iResult);
       
  3405             break;		
       
  3406             /*case KUpdateGrammarAndLexicon:
       
  3407             TRAP(iResult, HandleUpdateGrammarAndLexiconL(iSITtpWordList,iLexiconID,iGrammarID,*iRuleIDs));
       
  3408             iControllerIf.SendSrsEvent(KUidAsrEventUpdateGrammarAndLexicon, iResult);				
       
  3409             
       
  3410               break;
       
  3411             */
       
  3412             
       
  3413         case KPreStartSampling:
       
  3414             TRAP( iResult, HandlePreStartSamplingL() );
       
  3415             iControllerIf.SendSrsEvent( KUidAsrEventPreStartSampling, iResult );
       
  3416             break;
       
  3417             
       
  3418         default:
       
  3419             // No action
       
  3420             break;
       
  3421     }
       
  3422 }
       
  3423 
       
  3424 // -----------------------------------------------------------------------------
       
  3425 // CSIControllerPlugin::DoAsynch
       
  3426 // This method completes the request status and set the object active
       
  3427 // to provide asynchronous behavior.
       
  3428 // -----------------------------------------------------------------------------
       
  3429 //
       
  3430 void CSIControllerPlugin::DoAsynch()
       
  3431     {
       
  3432     TRequestStatus* pRS = &iStatus;
       
  3433     User::RequestComplete( pRS, KErrNone );
       
  3434     SetActive();
       
  3435     }
       
  3436 
       
  3437 // ============================SI MEMBER FUNCTIONS ===============================
       
  3438 
       
  3439 // -----------------------------------------------------------------------------
       
  3440 // CSIControllerPlugin::AdaptL
       
  3441 // Calls the devasr for adaptation
       
  3442 // -----------------------------------------------------------------------------
       
  3443 //
       
  3444 void CSIControllerPlugin::AdaptL( CSIClientResultSet& aResultSet ,TInt aCorrect ) 
       
  3445     {
       
  3446     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::AdaptL" );
       
  3447     
       
  3448     if ( IsActive() )
       
  3449         {
       
  3450         User::Leave( KErrServerBusy );
       
  3451         }
       
  3452     iRequestFunction = KAdapt;
       
  3453     
       
  3454     iSIClientResultSet  = &aResultSet;
       
  3455     iCorrect = aCorrect;
       
  3456     DoAsynch();
       
  3457     }
       
  3458 
       
  3459 // -----------------------------------------------------------------------------
       
  3460 // CSIControllerPlugin::HandleAdaptL
       
  3461 // Calls the devasr for adaptation
       
  3462 // -----------------------------------------------------------------------------
       
  3463 //
       
  3464 void CSIControllerPlugin::HandleAdaptL( CSIClientResultSet& aResultSet, 
       
  3465                                         TInt aCorrect ) 
       
  3466     {
       
  3467     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleAdaptL" );
       
  3468     if ( iState == ESiPluginRecognize )
       
  3469         {
       
  3470         iSIDatabase->BeginTransactionL();
       
  3471         if ( iSIGrammarDB->IsGrammarLoaded() )
       
  3472             
       
  3473             {
       
  3474             iDevASR->AdaptL( aResultSet.SIResultSet(), aCorrect );
       
  3475             }
       
  3476         else
       
  3477             {
       
  3478             // Either the loaded grammars are empty (contains no rule) or
       
  3479             // all rules have been unloaded.  No need to proceed any further.
       
  3480             iSIDatabase->CommitChangesL( EFalse );
       
  3481             
       
  3482             User::Leave( KErrAsrNoMatch );
       
  3483             }
       
  3484         
       
  3485         iSIDatabase->CommitChangesL( EFalse );
       
  3486         }
       
  3487     else
       
  3488         {
       
  3489         // Wrong state
       
  3490         User::Leave( KErrAsrInvalidState );
       
  3491         }
       
  3492     }
       
  3493 
       
  3494 // -----------------------------------------------------------------------------
       
  3495 // CSIControllerPlugin::AddPronunciationL
       
  3496 // Adds a new pronunciation for the given model into the specified lexicon.
       
  3497 // -----------------------------------------------------------------------------
       
  3498 //                                 
       
  3499 void CSIControllerPlugin::AddPronunciationL( TSILexiconID /*aLexiconID*/,
       
  3500                                              const TDesC& /*aTrainText*/,
       
  3501                                              TLanguage /*aLanguage*/,
       
  3502                                              TSIPronunciationID& /*aPronunciationID*/)  
       
  3503     {
       
  3504     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::AddPronunciationL" );
       
  3505     User::Leave( KErrNotSupported );
       
  3506     }
       
  3507 
       
  3508 
       
  3509 // -----------------------------------------------------------------------------
       
  3510 // CSIControllerPlugin::AddRuleVariantLtionL
       
  3511 // Adds a new rule variant for the given pronunciation into the specified grammar.
       
  3512 // -----------------------------------------------------------------------------
       
  3513 //                                 
       
  3514 void CSIControllerPlugin::AddRuleVariantL( TSIGrammarID aGrammarID,
       
  3515                                            TSILexiconID aLexiconID,
       
  3516                                            RArray<TSIPronunciationID>& aPronunciationIDs, 
       
  3517                                            TSIRuleID aRuleID,										  
       
  3518                                            TSIRuleVariantID& aRuleVariantID )
       
  3519     {
       
  3520     RUBY_DEBUG_BLOCK("CSIControllerPlugin::AddRuleVariantL");
       
  3521     
       
  3522     if ( IsActive() )
       
  3523         {
       
  3524         User::Leave(KErrServerBusy);
       
  3525         }
       
  3526     iRequestFunction = KAddRuleVariant;
       
  3527     iGrammarID = aGrammarID;
       
  3528     iRuleID = aRuleID;
       
  3529     iLexiconID = aLexiconID;
       
  3530     iRuleVariantIDPtr = &aRuleVariantID;	
       
  3531     iPronunciationIDs = &aPronunciationIDs;
       
  3532     DoAsynch();
       
  3533     }
       
  3534 
       
  3535 // -----------------------------------------------------------------------------
       
  3536 // CSIControllerPlugin::HandleAddRuleVariantL
       
  3537 // Adds a new rule variant for the given pronunciation into the specified grammar.
       
  3538 // -----------------------------------------------------------------------------
       
  3539 //                                 
       
  3540 void CSIControllerPlugin::HandleAddRuleVariantL( TSIGrammarID aGrammarID, 
       
  3541                                                  TSILexiconID aLexiconID,
       
  3542                                                  RArray<TSIPronunciationID>& aPronunciationIDs, 
       
  3543                                                  TSIRuleID aRuleID,
       
  3544                                                  TSIRuleVariantID& aRuleVariantID )
       
  3545     {
       
  3546     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleAddRuleVariantL" );
       
  3547     if ( iClientRegistered )
       
  3548         {
       
  3549         iSIDatabase->BeginTransactionL();
       
  3550         iSIGrammarDB->AddRuleVariantL( iClientUid, aGrammarID, aLexiconID, 
       
  3551                                        aPronunciationIDs, aRuleID, aRuleVariantID ); 
       
  3552         iSIDatabase->CommitChangesL( ETrue );
       
  3553         }
       
  3554     else
       
  3555         {
       
  3556         User::Leave( KErrAsrNotRegisted );
       
  3557         }
       
  3558     }
       
  3559 
       
  3560 // -----------------------------------------------------------------------------
       
  3561 // CSIControllerPlugin::AddVoiceTagL
       
  3562 // Trains a new voice tag.
       
  3563 // -----------------------------------------------------------------------------
       
  3564 //  
       
  3565 void CSIControllerPlugin::AddVoiceTagL( MDesCArray& /*aTrainArray*/, 
       
  3566                                         RArray<TLanguage>& /*aLanguageArray*/, 
       
  3567                                         TSILexiconID /*aLexiconID*/,
       
  3568                                         TSIGrammarID /*aGrammarID*/, 
       
  3569                                         TSIRuleID& /*aRuleID*/)
       
  3570     {
       
  3571     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::AddVoiceTagL" );
       
  3572     User::Leave( KErrNotSupported );
       
  3573     }
       
  3574 
       
  3575 
       
  3576 // -----------------------------------------------------------------------------
       
  3577 // CSIControllerPlugin::AddVoiceTagsL
       
  3578 // Adds several new voice tags.
       
  3579 // -----------------------------------------------------------------------------
       
  3580 //
       
  3581 void CSIControllerPlugin::AddVoiceTagsL( RPointerArray<MDesCArray>& aTrainArrays,
       
  3582                                          RArray<TLanguage>& aLanguageArray, 
       
  3583                                          TSILexiconID aLexiconID,
       
  3584                                          TSIGrammarID aGrammarID, 
       
  3585                                          RArray<TSIRuleID>& aRuleIDs)
       
  3586     {
       
  3587     RUBY_DEBUG_BLOCK("CSIControllerPlugin::AddVoiceTagsL");
       
  3588     
       
  3589     if ( IsActive() )
       
  3590         {
       
  3591         User::Leave( KErrServerBusy );
       
  3592         }
       
  3593         
       
  3594     // Use low priority for training (if training is not done from Voice Commands app)        
       
  3595     if ( RProcess().SecureId() != KVoiceCommandsAppUid )
       
  3596         {
       
  3597         RThread().SetPriority( EPriorityAbsoluteLow );
       
  3598         }
       
  3599     
       
  3600     iState = ESiPluginTrainText;
       
  3601     //	iTtpState=EAddVoiceTags;
       
  3602     
       
  3603     iRequestFunction = KAddVoiceTags ;
       
  3604     iLexiconID= aLexiconID;
       
  3605     iGrammarID = aGrammarID;
       
  3606     
       
  3607     iRuleIDs = &aRuleIDs;
       
  3608     iLanguageArray = &aLanguageArray;
       
  3609     iTrainArrays = &aTrainArrays;
       
  3610     
       
  3611     DoAsynch();
       
  3612     }
       
  3613 
       
  3614 #ifdef __SINDE_TRAINING
       
  3615 // -----------------------------------------------------------------------------
       
  3616 // CSIControllerPlugin::AddSindeVoiceTagL
       
  3617 // Trains a new voice tag.
       
  3618 // -----------------------------------------------------------------------------
       
  3619 //  
       
  3620 void CSIControllerPlugin::AddSindeVoiceTagL( MDesCArray& /*aTrainArray*/, 
       
  3621                                              RArray<RLanguageArray>& /*aLanguageArray*/, 
       
  3622                                              TSILexiconID /*aLexiconID*/,
       
  3623                                              TSIGrammarID /*aGrammarID*/, 
       
  3624                                              TSIRuleID& /*aRuleID*/ )
       
  3625     {
       
  3626     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::AddSindeVoiceTagL" );
       
  3627     User::Leave( KErrNotSupported );
       
  3628     }
       
  3629 
       
  3630 // -----------------------------------------------------------------------------
       
  3631 // CSIControllerPlugin::AddSindeVoiceTagsL
       
  3632 // Adds several new voice tags.
       
  3633 // -----------------------------------------------------------------------------
       
  3634 //
       
  3635 void CSIControllerPlugin::AddSindeVoiceTagsL( RPointerArray<MDesCArray>& aTrainArrays,
       
  3636                                               RArray<RLanguageArray>& aLanguageArray, 
       
  3637                                               TSILexiconID aLexiconID,
       
  3638                                               TSIGrammarID aGrammarID, 
       
  3639                                               RArray<TSIRuleID>& aRuleIDs )
       
  3640     {
       
  3641     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::AddSindeVoiceTagsL" );
       
  3642     
       
  3643     if ( IsActive() )
       
  3644         {
       
  3645         User::Leave( KErrServerBusy );
       
  3646         }
       
  3647 
       
  3648     // Use low priority for training (if training is not done from Voice Commands app)
       
  3649     if ( RProcess().SecureId() != KVoiceCommandsAppUid )
       
  3650         {
       
  3651         RThread().SetPriority( EPriorityAbsoluteLow );
       
  3652         }
       
  3653     
       
  3654     iState = ESiPluginTrainSinde;
       
  3655     
       
  3656     iRequestFunction = KAddSindeVoiceTags;
       
  3657     iLexiconID = aLexiconID;
       
  3658     iGrammarID = aGrammarID;
       
  3659     
       
  3660     // Store parameters for asynchronous processing
       
  3661     iRuleIDs = &aRuleIDs;
       
  3662     iLanguageArrayArray = &aLanguageArray;
       
  3663     iTrainArrays = &aTrainArrays;
       
  3664     
       
  3665     DoAsynch();
       
  3666     }
       
  3667 #endif // __SINDE_TRAINING
       
  3668 
       
  3669 // -----------------------------------------------------------------------------
       
  3670 // CSIControllerPlugin::HandleAddVoiceTagsL
       
  3671 // Adds several new voice tags.
       
  3672 // -----------------------------------------------------------------------------
       
  3673 //
       
  3674 void CSIControllerPlugin::HandleAddVoiceTagsL( RPointerArray<MDesCArray>* aTrainArrays,
       
  3675                                                RArray<TLanguage>& aLanguageArray, 
       
  3676                                                TSILexiconID aLexiconID,
       
  3677 											   TSIGrammarID aGrammarID ) 
       
  3678     {
       
  3679     RUBY_DEBUG_BLOCK("CSIControllerPlugin::HandleAddVoiceTagsL");
       
  3680     // 
       
  3681     // first test if both KLexiconID lexicon and KGrammarID are created
       
  3682     if ( IsLexiconIDInvalid( aLexiconID ) || IsGrammarIDInvalid( aGrammarID ) )
       
  3683         {
       
  3684         User::Leave( KErrNotFound ); // no such grammar exist in Database
       
  3685         }
       
  3686     // Need to clean the iSITtpWordList before adding new arrays
       
  3687     delete iSITtpWordList;
       
  3688     iSITtpWordList = NULL;
       
  3689     iSITtpWordList = CSITtpWordList::NewL();
       
  3690     if ( iState == ESiPluginTrainText )
       
  3691         { 
       
  3692         for ( TInt i=0; i < aTrainArrays->Count(); i++ )
       
  3693             {            
       
  3694             iSITtpWordList->AddL( ( *aTrainArrays )[i] );
       
  3695             // If next AddL fails, we must not delete the objects twice - once in
       
  3696             // aTrainArray->ResetAndDestroy() and second time in ~CSITtpWordList.
       
  3697             ( *aTrainArrays )[i] = NULL;
       
  3698             }
       
  3699         aTrainArrays->ResetAndDestroy();
       
  3700         aTrainArrays->Close();
       
  3701         delete aTrainArrays;
       
  3702         aTrainArrays = NULL;
       
  3703         
       
  3704         // Set Max prnounication per word 
       
  3705         iMaxNPronunsForWord.Reset();	
       
  3706         iMaxNPronunsForWord.Append( aLanguageArray.Count() );
       
  3707         
       
  3708         RUBY_DEBUG1( "CSIControllerPlugin::AddVoiceTagsL(aLanguageArray.Count=%d)", aLanguageArray.Count() );
       
  3709         
       
  3710         iDevASR->StartTrainingFromTextL(*iSITtpWordList,aLanguageArray,iMaxNPronunsForWord);
       
  3711         }
       
  3712     else
       
  3713         {   
       
  3714         // Wrong state
       
  3715         User::Leave( KErrAsrInvalidState );
       
  3716         }
       
  3717     }
       
  3718 
       
  3719 #ifdef __SINDE_TRAINING
       
  3720 // -----------------------------------------------------------------------------
       
  3721 // CSIControllerPlugin::HandleAddVoiceTagsL
       
  3722 // Adds several new voice tags.
       
  3723 // -----------------------------------------------------------------------------
       
  3724 //
       
  3725 void CSIControllerPlugin::HandleAddSindeVoiceTagsL( RPointerArray<MDesCArray>* aTrainArrays,
       
  3726                                                     RArray<RLanguageArray>& aLanguageArray, 
       
  3727                                                     TSILexiconID aLexiconID,
       
  3728 											        TSIGrammarID aGrammarID ) 
       
  3729     {
       
  3730     // First test if both KLexiconID lexicon and KGrammarID are created
       
  3731     if ( IsLexiconIDInvalid( aLexiconID ) || IsGrammarIDInvalid( aGrammarID ) )
       
  3732         {
       
  3733         User::Leave( KErrNotFound ); // no such grammar exist in Database
       
  3734         }
       
  3735   
       
  3736     iSindeTrainer->AddSindeVoiceTagsL( aTrainArrays, aLanguageArray, 
       
  3737                                        aLexiconID, aGrammarID, iModelBankID, 
       
  3738                                        iClientUid, *iRuleIDs );
       
  3739 
       
  3740     }
       
  3741 #endif // __SINDE_TRAINING
       
  3742 
       
  3743 // -----------------------------------------------------------------------------
       
  3744 // CSIControllerPlugin::IsGrammarIDInvalid
       
  3745 // is the grammar id valid in the database
       
  3746 // -----------------------------------------------------------------------------
       
  3747 //
       
  3748 TBool CSIControllerPlugin::IsGrammarIDInvalid( TSIGrammarID aGrammarID )
       
  3749     {
       
  3750     TInt i(0);
       
  3751     RArray<TSIGrammarID> aGrammarIDs;
       
  3752     
       
  3753     TRAPD( error, 
       
  3754         iSIDatabase->BeginTransactionL();
       
  3755         iSIGrammarDB->GetAllGrammarIDsL( aGrammarIDs );
       
  3756         iSIDatabase->CommitChangesL( EFalse );
       
  3757         ); // TRAPD
       
  3758     
       
  3759     if ( error == KErrNone )
       
  3760         {
       
  3761         for( i = 0; i < aGrammarIDs.Count(); i++ ) 
       
  3762             {
       
  3763             if ( aGrammarIDs[i] == aGrammarID )
       
  3764                 {
       
  3765                 break;
       
  3766                 }
       
  3767             if ( i == aGrammarIDs.Count() )
       
  3768                 {
       
  3769                 aGrammarIDs.Close();
       
  3770                 return ETrue; // no such Lexicon exist in Database
       
  3771                 }
       
  3772             }
       
  3773         }
       
  3774     aGrammarIDs.Close();
       
  3775     return EFalse;
       
  3776     }
       
  3777 
       
  3778 // -----------------------------------------------------------------------------
       
  3779 // CSIControllerPlugin::IsLexiconIDInvalid
       
  3780 // is the lexicon id valid in the database
       
  3781 // -----------------------------------------------------------------------------
       
  3782 //
       
  3783 TBool CSIControllerPlugin::IsLexiconIDInvalid( TSILexiconID aLexiconID )
       
  3784     {
       
  3785     // first test if both KLexiconID lexicon and KLexiconID are created
       
  3786     TInt i(0);
       
  3787     RArray<TSILexiconID> aLexiconIDs;
       
  3788 
       
  3789     TRAPD( error,     
       
  3790         iSIDatabase->BeginTransactionL();
       
  3791         iSILexiconDB->GetAllLexiconIDsL( aLexiconIDs );
       
  3792         iSIDatabase->CommitChangesL( EFalse );
       
  3793         ); //TRAPD
       
  3794         
       
  3795     if ( error == KErrNone )
       
  3796         {
       
  3797         for( i = 0; i < aLexiconIDs.Count(); i++ ) 
       
  3798             {
       
  3799             if ( aLexiconIDs[i] == aLexiconID ) 
       
  3800                 {
       
  3801                 break;
       
  3802                 }
       
  3803             if ( i == aLexiconIDs.Count() ) 
       
  3804                 {
       
  3805                 aLexiconIDs.Close();
       
  3806                 return ETrue; // no such Lexicon exist in Database
       
  3807                 }
       
  3808             }
       
  3809         }
       
  3810 
       
  3811     aLexiconIDs.Close();
       
  3812     return EFalse;
       
  3813     }
       
  3814 
       
  3815 
       
  3816 // -----------------------------------------------------------------------------
       
  3817 // CSIControllerPlugin::CreateRuleL
       
  3818 // Creates a new empty rule.
       
  3819 // -----------------------------------------------------------------------------
       
  3820 //
       
  3821 void CSIControllerPlugin::CreateRuleL( TSIGrammarID aGrammarID, 
       
  3822                                        TSIRuleID& aRuleID ) 
       
  3823     {
       
  3824     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::CreateRuleL" );
       
  3825     
       
  3826     if ( IsActive() )
       
  3827         {
       
  3828         User::Leave( KErrServerBusy );
       
  3829         }
       
  3830     iRequestFunction = KCreateRule ;
       
  3831     iGrammarID = aGrammarID; 
       
  3832     iRuleIDPtr= &aRuleID;
       
  3833     
       
  3834     DoAsynch();
       
  3835     }
       
  3836 
       
  3837 // -----------------------------------------------------------------------------
       
  3838 // CSIControllerPlugin::HandleCreateRuleL
       
  3839 // Creates a new empty rule.
       
  3840 // -----------------------------------------------------------------------------
       
  3841 //
       
  3842 void CSIControllerPlugin::HandleCreateRuleL( TSIGrammarID aGrammarID, 
       
  3843                                              TSIRuleID& aRuleID ) 
       
  3844     {
       
  3845     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleCreateRuleL" );
       
  3846     if ( iClientRegistered )
       
  3847         {
       
  3848         iSIDatabase->BeginTransactionL();
       
  3849         iSIGrammarDB->CreateRuleL( iClientUid, aGrammarID, aRuleID ); 
       
  3850         iSIDatabase->CommitChangesL( EFalse );
       
  3851         }
       
  3852     else
       
  3853         {
       
  3854         User::Leave( KErrAsrNotRegisted );
       
  3855         }
       
  3856     }
       
  3857 
       
  3858 // -----------------------------------------------------------------------------
       
  3859 // CSIControllerPlugin::EndRecordL
       
  3860 // End recordingEnds recording. Unlike Cancel(), this function may return a 
       
  3861 // recognition result if adequate number of samples was already recorded.
       
  3862 // -----------------------------------------------------------------------------
       
  3863 //
       
  3864 void CSIControllerPlugin::EndRecordL()
       
  3865     {
       
  3866     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::EndRecordL" );
       
  3867     
       
  3868     if ( IsActive() )
       
  3869         {
       
  3870         User::Leave( KErrServerBusy );
       
  3871         }
       
  3872     iRequestFunction = KEndRecord ; 
       
  3873     DoAsynch();
       
  3874 }
       
  3875 
       
  3876 // -----------------------------------------------------------------------------
       
  3877 // CSIControllerPlugin::HandleEndRecordL
       
  3878 // End recordingEnds recording. Unlike Cancel(), this function may return a 
       
  3879 // recognition result if adequate number of samples was already recorded.
       
  3880 // -----------------------------------------------------------------------------
       
  3881 //
       
  3882 void CSIControllerPlugin::HandleEndRecordL()
       
  3883     {
       
  3884     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleEndRecordL" );
       
  3885     iDevASR->EndRecord();	
       
  3886     }
       
  3887 
       
  3888 // -----------------------------------------------------------------------------
       
  3889 // CSIControllerPlugin::UnloadGrammarL
       
  3890 // Unload a grammar from memory
       
  3891 // -----------------------------------------------------------------------------
       
  3892 //
       
  3893 void CSIControllerPlugin::UnloadGrammarL( TSIGrammarID aGrammarID )
       
  3894     {
       
  3895     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::UnloadGrammarL" );
       
  3896     
       
  3897     if ( IsActive() )
       
  3898         {
       
  3899         User::Leave( KErrServerBusy );
       
  3900         }
       
  3901     iRequestFunction = KUnloadGrammar ;
       
  3902     iGrammarID=aGrammarID;
       
  3903     DoAsynch();
       
  3904     }
       
  3905 
       
  3906 // -----------------------------------------------------------------------------
       
  3907 // CSIControllerPlugin::HandleUnloadGrammarL
       
  3908 // Unload a grammar from memory
       
  3909 // -----------------------------------------------------------------------------
       
  3910 //
       
  3911 void CSIControllerPlugin::HandleUnloadGrammarL( TSIGrammarID aGrammarID )
       
  3912     {
       
  3913     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleUnloadGrammarL" );
       
  3914     
       
  3915     iSIDatabase->BeginTransactionL();
       
  3916     CSICompiledGrammar* LoadedGrammar =iSIGrammarDB->FindGrammarL( aGrammarID );	 
       
  3917     iSIDatabase->CommitChangesL( EFalse );
       
  3918         
       
  3919     iDevASR->UnloadGrammar( *LoadedGrammar );	// async call to unload grammar in devasr
       
  3920     }
       
  3921 
       
  3922 //  End of File