srsf/sisrscontrollerplugin/src/sicontrollerplugin.cpp
branchRCL_3
changeset 19 e36f3802f733
equal deleted inserted replaced
18:cad71a31b7fc 19:e36f3802f733
       
     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     CleanupClosePushL( aRuleIDs ); 
       
  2881     
       
  2882     iSITtpWordList = aSITtpWordList;
       
  2883     iLexiconID = aLexiconID;
       
  2884     iGrammarID = aGrammarID;
       
  2885     iRuleIDs = &aRuleIDs;
       
  2886     
       
  2887     aRuleIDs.Reset();
       
  2888     
       
  2889     iSIDatabase->BeginTransactionL();
       
  2890     
       
  2891     // is grammar already in database?
       
  2892     iSIGrammarDB->VerifyOwnershipL( iClientUid, KGrammarIdTable, KGrammarIndex, iGrammarID ); 
       
  2893     CSICompiledGrammar*  aGrammar = (CSICompiledGrammar* )iSIGrammarDB->GrammarL( iGrammarID );
       
  2894     CleanupStack::PushL( aGrammar );	
       
  2895     
       
  2896     //is lexicion already in database?
       
  2897     iSILexiconDB->VerifyOwnershipL( iClientUid, KLexiconIdTable, KLexiconIndex, iLexiconID );		   
       
  2898     CSILexicon* aLexicon = iSILexiconDB->LexiconL( iLexiconID );
       
  2899     CleanupStack::PushL( aLexicon );	
       
  2900     
       
  2901     iSIDatabase->CommitChangesL( EFalse );  
       
  2902     
       
  2903     
       
  2904     // unpack the iSITtpWordList;
       
  2905     for ( TInt i = 0; i < iSITtpWordList->Count(); i++ )
       
  2906         {
       
  2907         //iSIDatabase->BeginTransactionL();  
       
  2908         // Get pronunciations
       
  2909         RPointerArray<CSIPronunciationInfo> pronunciations;
       
  2910         RArray<TSIPronunciationID> pronunciationIDs;
       
  2911         CleanupClosePushL( pronunciationIDs );
       
  2912         CleanupClosePushL( pronunciations );
       
  2913         iSITtpWordList->GetPronunciationsL( i, pronunciations );
       
  2914    
       
  2915         RUBY_DEBUG1( "CSIControllerPlugin::UpdateGrammarAndLexiconDBL pronunciations.Count=%d", pronunciations.Count() );
       
  2916         
       
  2917         // if pronunciation generation failed, skip that word.
       
  2918         if ( pronunciations.Count() == 0 )
       
  2919             {
       
  2920             CleanupStack::PopAndDestroy( &pronunciations ); 
       
  2921             CleanupStack::PopAndDestroy( &pronunciationIDs );
       
  2922             User::LeaveIfError( aRuleIDs.Append( KInvalidRuleID ) );
       
  2923             continue;
       
  2924             }
       
  2925         
       
  2926         // Create a empty rule into grammar, one name -> one rule; 
       
  2927         TSIRuleID ruleID;
       
  2928         
       
  2929         
       
  2930         CSIRule* rule=CreateNewRuleL(aGrammar);
       
  2931         CleanupStack::PushL(  rule );	
       
  2932         ruleID=rule->RuleID();
       
  2933         User::LeaveIfError( aRuleIDs.Append( ruleID ) );
       
  2934         
       
  2935         for(TInt k=0;k<pronunciations.Count();k++) 
       
  2936             {
       
  2937             pronunciationIDs.Reset();
       
  2938             
       
  2939             for ( TInt n = 0; n < pronunciations[k]->Count(); n++ ) 
       
  2940                 {	
       
  2941                 // Add Prounication into lexicon
       
  2942                 
       
  2943                 TSIPronunciationID aPronunciationID = CreateNewPronunciationL( aLexicon,pronunciations[k]->PronunciationL( n ),
       
  2944                     iModelBankID );
       
  2945                 User::LeaveIfError( pronunciationIDs.Append( aPronunciationID ) ); 
       
  2946                 }
       
  2947             
       
  2948             // Add RuleVariant into grammar
       
  2949             AddNewRuleVariantL(*rule, aLexiconID, pronunciationIDs, *pronunciations[k] );                          
       
  2950             }
       
  2951         
       
  2952         
       
  2953         aGrammar->AddL( rule );
       
  2954         CleanupStack::Pop( rule );
       
  2955         
       
  2956         // Close the arrays
       
  2957         CleanupStack::PopAndDestroy( &pronunciations ); 
       
  2958         CleanupStack::PopAndDestroy( &pronunciationIDs );
       
  2959         }
       
  2960     
       
  2961     RUBY_DEBUG0( "CSIControllerPlugin::UpdateGrammarAndLexiconDBL" );
       
  2962     
       
  2963     iSIDatabase->BeginTransactionL();
       
  2964     iSIGrammarDB->UpdateGrammarL( iClientUid,aGrammar );
       
  2965     iSILexiconDB->UpdateLexiconL( iClientUid,aLexicon );
       
  2966     iSIDatabase->CommitChangesL( ETrue );  
       
  2967     CleanupStack::PopAndDestroy( 2 ); // aGrammar aLexicon	
       
  2968     
       
  2969     CleanupStack::Pop(); //aRuleIDs
       
  2970     }
       
  2971 
       
  2972 // -----------------------------------------------------------------------------
       
  2973 // CSIControllerPlugin::RecognitionReset
       
  2974 // Terminate recognition session.  Free up resources and return to IDLE state.
       
  2975 // -----------------------------------------------------------------------------
       
  2976 //                                 
       
  2977 void CSIControllerPlugin::RecognitionReset()
       
  2978     {
       
  2979     iSIModelBankDB->ResetAndDestroy();
       
  2980     iSILexiconDB->ResetAndDestroy();  // Grcompiler takes the ownership of it
       
  2981     iSIGrammarDB->ResetAndDestroy();
       
  2982     iState = ESiPluginIdle;
       
  2983     }
       
  2984 
       
  2985 // -----------------------------------------------------------------------------
       
  2986 // CSIControllerPlugin::ProcessTrainCompleteL
       
  2987 // Training is complete.  Store the acoustic model and user utterance into the
       
  2988 // model bank.
       
  2989 // -----------------------------------------------------------------------------
       
  2990 //
       
  2991 void CSIControllerPlugin::ProcessTrainCompleteL()
       
  2992     {
       
  2993     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::ProcessTrainCompleteL" );
       
  2994     // No need to train it at all  
       
  2995     }
       
  2996 
       
  2997 // -----------------------------------------------------------------------------
       
  2998 // CSIControllerPlugin::ProcessRecognitionCompleteL
       
  2999 // Recogntion is complete.  Transfer the result from the recognizer into the result
       
  3000 // object sent by the client.
       
  3001 // -----------------------------------------------------------------------------
       
  3002 //
       
  3003 TInt CSIControllerPlugin::ProcessRecognitionComplete()
       
  3004     {
       
  3005     RUBY_DEBUG0( "CSIControllerPlugin::ProcessRecognitionComplete" );
       
  3006     
       
  3007     const CSIResult* result = 0;
       
  3008     CSIClientResult* clientResult = 0;
       
  3009     
       
  3010     TInt resultsAvailable = iSIResultSet->Count();
       
  3011     TInt resultsWanted    = iSIClientResultSet->MaxResults();
       
  3012     TInt count            = Min( resultsAvailable, resultsWanted );
       
  3013     
       
  3014     // Copy the results from the recognizer into client's result set object
       
  3015     TInt resCount = 0;
       
  3016     
       
  3017     
       
  3018     for ( TInt i = 0; i < count; i++ )
       
  3019         {
       
  3020         TRAPD( err, result = &( iSIResultSet->AtL( i ) ) );
       
  3021         if ( err )
       
  3022             {
       
  3023             return 0; 	 	
       
  3024             }
       
  3025         RUBY_DEBUG2( "CSIControllerPlugin::ProcessRecognitionComplete rank(%d): grammarId(%d)", i+1, result->GrammarID() );
       
  3026         RUBY_DEBUG2( "CSIControllerPlugin::ProcessRecognitionComplete ruleId(%d), score(%d)", result->RuleID(), result->Score() );
       
  3027 
       
  3028         if ( result->Score() > 0 )
       
  3029             {
       
  3030             TRAP( err, clientResult = CSIClientResult::NewL( result->GrammarID(), 
       
  3031                                                              result->RuleID() ) );
       
  3032             if ( err ) 
       
  3033                 {
       
  3034                 return 0; 	 	
       
  3035                 }			
       
  3036             TRAP( err, iSIClientResultSet->AddL( clientResult ) );
       
  3037             if ( err ) 
       
  3038                 {
       
  3039                 return 0; 	 	
       
  3040                 }
       
  3041             resCount++;
       
  3042             }
       
  3043         else
       
  3044             {
       
  3045             break;
       
  3046             }
       
  3047         }
       
  3048     
       
  3049     iSIClientResultSet->SetResultCount( resCount );
       
  3050     return resCount;
       
  3051     } 
       
  3052 
       
  3053 // -----------------------------------------------------------------------------
       
  3054 // CSIControllerPlugin::DoCancel
       
  3055 // Cancel handle from CActive class.
       
  3056 // -----------------------------------------------------------------------------
       
  3057 //
       
  3058 void CSIControllerPlugin::DoCancel()
       
  3059     {
       
  3060     }
       
  3061 
       
  3062 // -----------------------------------------------------------------------------
       
  3063 // CSIControllerPlugin::RunL
       
  3064 // RunL from CActive class.  Check which message got saved and call the
       
  3065 // appropriate handle function.
       
  3066 // -----------------------------------------------------------------------------
       
  3067 //
       
  3068 void CSIControllerPlugin::RunL()
       
  3069     {
       
  3070     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::RunL" );
       
  3071     switch ( iRequestFunction )
       
  3072         {
       
  3073         case KAddRule:
       
  3074             TRAP( iResult, HandleAddRuleL( iGrammarID, iLexiconID, iPronunciationID, *iRuleIDPtr ) );
       
  3075             iControllerIf.SendSrsEvent( KUidAsrEventAddRule, iResult );
       
  3076             break;
       
  3077             
       
  3078         case KCreateGrammar:
       
  3079             TRAP( iResult, HandleCreateGrammarL( *iGrammarIDPtr ) );
       
  3080             iControllerIf.SendSrsEvent( KUidAsrEventCreateGrammar, iResult );
       
  3081             break;
       
  3082             
       
  3083         case KCreateLexicon: 
       
  3084             TRAP( iResult, HandleCreateLexiconL( *iLexiconIDPtr ) );
       
  3085             iControllerIf.SendSrsEvent( KUidAsrEventCreateLexicon, iResult );
       
  3086             break;
       
  3087             
       
  3088         case KCreateModelBank:
       
  3089             TRAP( iResult, HandleCreateModelBankL( *iModelBankIDPtr ) );
       
  3090             iControllerIf.SendSrsEvent( KUidAsrEventCreateModelBank, iResult );
       
  3091             break;
       
  3092             
       
  3093         case KGetAllClientGrammarIDs:
       
  3094             TRAP( iResult, HandleGetAllClientGrammarIDsL( *iGrammarIDs ) );
       
  3095             iControllerIf.SendSrsEvent( KUidAsrEventGetAllClientGrammarIDs, iResult );
       
  3096             break;
       
  3097             
       
  3098         case KGetAllClientLexiconIDs:
       
  3099             TRAP( iResult, HandleGetAllClientLexiconIDsL( *iLexiconIDs ) );
       
  3100             iControllerIf.SendSrsEvent( KUidAsrEventGetAllClientLexiconIDs, iResult );
       
  3101             break;
       
  3102             
       
  3103         case KGetAllClientModelBankIDs:
       
  3104             TRAP( iResult, HandleGetAllClientModelBankIDsL( *iModelBankIDs ) );
       
  3105             iControllerIf.SendSrsEvent( KUidAsrEventGetAllClientModelBankIDs, iResult );
       
  3106             break;
       
  3107             
       
  3108         case KGetAllGrammarIDs:
       
  3109             TRAP( iResult, HandleGetAllGrammarIDsL( *iGrammarIDs ) );
       
  3110             iControllerIf.SendSrsEvent( KUidAsrEventGetAllGrammarIDs, iResult );
       
  3111             break;
       
  3112             
       
  3113         case KGetAllLexiconIDs:
       
  3114             TRAP( iResult, HandleGetAllLexiconIDsL( *iLexiconIDs ) );
       
  3115             iControllerIf.SendSrsEvent( KUidAsrEventGetAllLexiconIDs, iResult );
       
  3116             break;
       
  3117             
       
  3118         case KGetAllModelBankIDs:
       
  3119             TRAP( iResult, HandleGetAllModelBankIDsL( *iModelBankIDs ) );
       
  3120             iControllerIf.SendSrsEvent( KUidAsrEventGetAllModelBankIDs, iResult );
       
  3121             break;
       
  3122             
       
  3123         case KGetAllModelIDs:
       
  3124             TRAP( iResult, HandleGetAllModelIDsL( iModelBankID, *iModelIDs ) );
       
  3125             iControllerIf.SendSrsEvent( KUidAsrEventGetAllModelIDs, iResult );
       
  3126             break;
       
  3127             
       
  3128         case KGetAllPronunciationIDs:
       
  3129             TRAP( iResult, HandleGetAllPronunciationIDsL( iLexiconID, *iPronunciationIDs ) );
       
  3130             iControllerIf.SendSrsEvent( KUidAsrEventGetAllPronunciationIDs, iResult );
       
  3131             break;
       
  3132             
       
  3133         case KGetAllRuleIDs:
       
  3134             TRAP( iResult, HandleGetAllRuleIDsL( iGrammarID, *iRuleIDs ) );
       
  3135             iControllerIf.SendSrsEvent( KUidAsrEventGetAllRuleIDs, iResult );
       
  3136             break;
       
  3137             
       
  3138         case KGetAvailableStorage:
       
  3139             TRAP( iResult, HandleGetAvailableStorageL( *iCountPtr ) );
       
  3140             iControllerIf.SendSrsEvent( KUidAsrEventGetAvailableStorage, iResult );
       
  3141             break;
       
  3142             
       
  3143         case KGetModelCount:
       
  3144             TRAP( iResult, HandleGetModelCountL( iModelBankID, *iCountPtr ) );
       
  3145             iControllerIf.SendSrsEvent( KUidAsrEventGetModelCount, iResult );
       
  3146             break;
       
  3147             
       
  3148         case KGetPronunciationCount:
       
  3149             TRAP( iResult, HandleGetPronunciationCountL( iLexiconID, *iCountPtr ) );
       
  3150             iControllerIf.SendSrsEvent( KUidAsrEventGetPronunciationCount, iResult );
       
  3151             break;
       
  3152             
       
  3153         case KGetRuleCount:
       
  3154             TRAP( iResult, HandleGetRuleCountL( iGrammarID, *iCountPtr ) );
       
  3155             iControllerIf.SendSrsEvent( KUidAsrEventGetRuleCount, iResult );
       
  3156             break;	
       
  3157         case KGetRuleValidity:
       
  3158             TRAP( iResult, HandleGetRuleValidityL( iGrammarID, iRuleID, *iValidPtr ) );
       
  3159             iControllerIf.SendSrsEvent( KUidAsrEventGetRuleValidity, iResult );
       
  3160             break;
       
  3161             
       
  3162         case KGetUtteranceDuration:
       
  3163             TRAP( iResult, HandleGetUtteranceDurationL( iModelBankID, iModelID, *iDurationPtr ) );
       
  3164             iControllerIf.SendSrsEvent( KUidAsrEventGetUtteranceDuration, iResult );
       
  3165             break;
       
  3166             
       
  3167         case KLoadGrammar:
       
  3168             TRAP( iResult, HandleLoadGrammarL( iGrammarID ) );
       
  3169             if ( iResult != KErrNone )
       
  3170                 {
       
  3171                 RUBY_DEBUG1( "CSIControllerPlugin::RunL. KLoadGrammar. HandleLoadGrammarL Left with error [%d]", iResult );
       
  3172                 // iGrammarDB needs to be reset also since multiple grammars can
       
  3173                 // be loaded and this may not be the first grammar.
       
  3174                 iSIModelBankDB->ResetAndDestroy();
       
  3175                 iSILexiconDB->ResetAndDestroy();
       
  3176                 iSIGrammarDB->ResetAndDestroy();
       
  3177                 iState = ESiPluginIdle;
       
  3178                 iControllerIf.SendSrsEvent( KUidAsrEventLoadGrammar, iResult );
       
  3179                 }
       
  3180             break;
       
  3181             
       
  3182         case KLoadLexicon:
       
  3183             TRAP(iResult, HandleLoadLexiconL( iLexiconID ) );
       
  3184             if ( iResult != KErrNone )
       
  3185                 {
       
  3186                 RUBY_DEBUG1( "CSIControllerPlugin::RunL. KLoadLexicon. HandleLoadGrammarL Left with error [%d]", iResult );
       
  3187 
       
  3188                 // iLexiconDB needs to be reset also since multiple lexicons can
       
  3189                 // be loaded and this may not be the first lexicon.
       
  3190                 iSIModelBankDB->ResetAndDestroy();
       
  3191                 iSILexiconDB->ResetAndDestroy();
       
  3192                 iState = ESiPluginIdle;
       
  3193                 iControllerIf.SendSrsEvent( KUidAsrEventLoadLexicon, iResult );
       
  3194                 }
       
  3195             break;
       
  3196             
       
  3197         case KLoadModels:
       
  3198             TRAP(iResult, HandleLoadModelsL( iModelBankID ) );
       
  3199             if ( iResult != KErrNone )
       
  3200                 {
       
  3201                 RUBY_DEBUG1( "CSIControllerPlugin::RunL. KLoadModels. HandleLoadGrammarL Left with error [%d]", iResult );
       
  3202                 
       
  3203                 if ( iResult == KErrCorrupt )
       
  3204                     {
       
  3205                     RUBY_DEBUG0( "CSIControllerPlugin::RunL. KLoadModels. Try to update models" );
       
  3206                     TRAP( iResult,
       
  3207                         iSIModelBankDB->UpdateModelBankIfInvalidL( iModelBankID );
       
  3208                         HandleLoadModelsL( iModelBankID );
       
  3209                     );
       
  3210                     }
       
  3211 
       
  3212                 if ( iResult != KErrNone )
       
  3213                     {
       
  3214                     // iModelBankDB needs to be reset also since multiple model banks can
       
  3215                     // be loaded and this may not be the first model bank.
       
  3216                     iSIModelBankDB->ResetAndDestroy();
       
  3217                     iState = ESiPluginIdle;
       
  3218                     
       
  3219                     iControllerIf.SendSrsEvent( KUidAsrEventLoadModels, iResult );
       
  3220                     }
       
  3221                 }
       
  3222             break;
       
  3223             
       
  3224         case KRecognize:
       
  3225             TRAP( iResult, HandleRecognizeL() );
       
  3226             if ( iResult != KErrNone )
       
  3227                 {
       
  3228                 RUBY_DEBUG1( "CSIControllerPlugin::RunL. KRecognize. HandleLoadGrammarL Left with error [%d]", iResult );
       
  3229 
       
  3230                 if ( iResult != KErrAsrNoMatch )
       
  3231                     {
       
  3232                     RecognitionReset();
       
  3233                     }
       
  3234                 iControllerIf.SendSrsEvent( KUidAsrEventRecognition, iResult );
       
  3235                 }
       
  3236             break;
       
  3237             
       
  3238         case KRecord:
       
  3239             TRAP( iResult, HandleRecordL( iRecordTime ) );
       
  3240             if ( iResult != KErrNone )
       
  3241                 {
       
  3242                 RUBY_DEBUG1( "CSIControllerPlugin::RunL. KRecord. HandleLoadGrammarL Left with error [%d]", iResult );
       
  3243 
       
  3244                 iControllerIf.SendSrsEvent( KUidAsrEventRecord, iResult );
       
  3245                 }
       
  3246             break;
       
  3247             
       
  3248         case KRemoveGrammar:
       
  3249             TRAP( iResult, HandleRemoveGrammarL( iGrammarID ) );
       
  3250             iControllerIf.SendSrsEvent( KUidAsrEventRemoveGrammar, iResult );
       
  3251             break;
       
  3252             
       
  3253         case KRemoveLexicon:
       
  3254             TRAP( iResult, HandleRemoveLexiconL( iLexiconID ) );
       
  3255             iControllerIf.SendSrsEvent( KUidAsrEventRemoveLexicon, iResult );
       
  3256             break;
       
  3257             
       
  3258         case KRemoveModelBank:
       
  3259             TRAP( iResult, HandleRemoveModelBankL( iModelBankID ) );
       
  3260             iControllerIf.SendSrsEvent( KUidAsrEventRemoveModelBank, iResult );
       
  3261             break;
       
  3262             
       
  3263         case KRemoveModel:
       
  3264             TRAP( iResult, HandleRemoveModelL( iModelBankID, iModelID ) );
       
  3265             iControllerIf.SendSrsEvent( KUidAsrEventRemoveModel, iResult );
       
  3266             break;
       
  3267             
       
  3268         case KRemovePronunciation:
       
  3269             TRAP( iResult, HandleRemovePronunciationL( iLexiconID, iPronunciationID ) );
       
  3270             iControllerIf.SendSrsEvent( KUidAsrEventRemovePronunciation, iResult );
       
  3271             break;
       
  3272             
       
  3273         case KRemoveRule:
       
  3274             TRAP( iResult, HandleRemoveRuleL( iGrammarID, iRuleID ) );
       
  3275             if ( iResult != KErrNone )
       
  3276                 {
       
  3277                 RUBY_DEBUG1( "CSIControllerPlugin::RunL. KRemoveRule. HandleLoadGrammarL Left with error [%d]", iResult );
       
  3278 
       
  3279                 iControllerIf.SendSrsEvent( KUidAsrEventRemoveRule, iResult );
       
  3280                 }
       
  3281             break;
       
  3282         case KRemoveRules:
       
  3283             TRAP( iResult, HandleRemoveRulesL( iGrammarID, *iRuleIDs ) );
       
  3284             if ( iResult != KErrNone )
       
  3285                 {
       
  3286                 RUBY_DEBUG1( "CSIControllerPlugin::RunL. KRemoveRules. HandleLoadGrammarL Left with error [%d]", iResult );
       
  3287 
       
  3288                 iControllerIf.SendSrsEvent( KUidAsrEventRemoveRules, iResult );
       
  3289                 }
       
  3290             break;
       
  3291             
       
  3292         case KTrain:
       
  3293             TRAP( iResult, HandleTrainL( iModelBankID ) );
       
  3294             if ( iResult != KErrNone )
       
  3295                 {
       
  3296                 RUBY_DEBUG1( "CSIControllerPlugin::RunL. KTrain. HandleLoadGrammarL Left with error [%d]", iResult );
       
  3297 
       
  3298                 iControllerIf.SendSrsEvent( KUidAsrEventTrain, iResult );
       
  3299                 }
       
  3300             break;
       
  3301             
       
  3302         case KUnloadRule:
       
  3303             TRAP( iResult, HandleUnloadRuleL(iGrammarID, iRuleID));
       
  3304             if ( iResult != KErrNone )
       
  3305                 {
       
  3306                 RUBY_DEBUG1( "CSIControllerPlugin::RunL. KUnloadRule. HandleLoadGrammarL Left with error [%d]", iResult );
       
  3307 
       
  3308                 iControllerIf.SendSrsEvent( KUidAsrEventUnloadRule, iResult );
       
  3309                 }
       
  3310             break;
       
  3311             
       
  3312             
       
  3313             // SI Component
       
  3314         case KAdapt:
       
  3315             TRAP( iResult, HandleAdaptL(*iSIClientResultSet ,iCorrect)) ;
       
  3316             if ( iResult != KErrNone )
       
  3317                 {
       
  3318                 RUBY_DEBUG1( "CSIControllerPlugin::RunL. KAdapt. HandleLoadGrammarL Left with error [%d]", iResult );
       
  3319 
       
  3320                 iControllerIf.SendSrsEvent( KUidAsrEventAdapt , iResult );
       
  3321                 }
       
  3322             break;
       
  3323             /*	case KSIAddPronunciation:
       
  3324             TRAP(iResult, HandleAddPronunciationL(iLexiconID, iTrainTextPtr,
       
  3325             iLanguage, iPronunciationIDPtr)) ;
       
  3326             iControllerIf.SendSrsEvent(KUidAsrEventAddPronunciation, iResult);
       
  3327             break;
       
  3328             */		
       
  3329         case KAddRuleVariant:
       
  3330             TRAP( iResult, HandleAddRuleVariantL( iGrammarID, iLexiconID, *iPronunciationIDs, iRuleID, *iRuleVariantIDPtr ) );
       
  3331             iControllerIf.SendSrsEvent( KUidAsrEventAddRuleVariant, iResult );				
       
  3332             break;
       
  3333             /*	
       
  3334             case KAddVoiceTag:
       
  3335             TRAP(iResult, HandleAddVoiceTagL( *iTrainArray, *iLanguageArray, 
       
  3336             iLexiconID, iGrammarID, iRuleIDPtr)) ;
       
  3337             iControllerIf.SendSrsEvent(KUidAsrEventAddVoiceTag, iResult);
       
  3338             break;
       
  3339             */	
       
  3340         case KAddVoiceTags:
       
  3341             TRAP( iResult, HandleAddVoiceTagsL( iTrainArrays, *iLanguageArray, iLexiconID,
       
  3342                 iGrammarID /* ,*iRuleIDs */) );
       
  3343             // AddVoiceTags contain several async functions,
       
  3344             // KUidAsrEventAddVoiceTags will be sent when the AddVoiceTags complete
       
  3345             if ( iResult != KErrNone )
       
  3346                 {
       
  3347                 RUBY_DEBUG1( "CSIControllerPlugin::RunL. KAddVoiceTags. HandleLoadGrammarL Left with error [%d]", iResult );
       
  3348 
       
  3349                 // Clean up the iTrainArrays
       
  3350                 if ( iTrainArrays )
       
  3351                     {
       
  3352                     iTrainArrays->ResetAndDestroy();
       
  3353                     iTrainArrays->Close();
       
  3354                     delete iTrainArrays;
       
  3355                     iTrainArrays = NULL;
       
  3356                     }
       
  3357                 // Send error message
       
  3358                 iControllerIf.SendSrsEvent( KUidAsrEventAddVoiceTags, iResult );
       
  3359                 }
       
  3360             iTrainArrays = NULL;                
       
  3361             break;
       
  3362 
       
  3363 #ifdef __SINDE_TRAINING
       
  3364         case KAddSindeVoiceTags:
       
  3365             RUBY_DEBUG0( "CSIControllerPlugin::RunL KAddSindeVoiceTags" );
       
  3366             
       
  3367 #ifdef _DEBUG
       
  3368             for ( TInt i = 0; i < iLanguageArrayArray->Count(); i++ )
       
  3369                 {
       
  3370                 RUBY_DEBUG1( "Contents of element %d:", i );
       
  3371                 RLanguageArray langArr = (*iLanguageArrayArray)[i];
       
  3372                 for ( TInt j = 0; j < langArr.Count(); j++ )
       
  3373                     {
       
  3374                     RUBY_DEBUG2( "Index: %d Language: %d", j, langArr[j] );
       
  3375                    }
       
  3376                 }
       
  3377 #endif // _DEBUG
       
  3378 
       
  3379             TRAP( iResult, HandleAddSindeVoiceTagsL( iTrainArrays, *iLanguageArrayArray, iLexiconID, iGrammarID ) );
       
  3380 
       
  3381             if ( iResult != KErrNone ) 
       
  3382                 {
       
  3383                 iTrainArrays->ResetAndDestroy();
       
  3384                 iTrainArrays->Close();
       
  3385                 delete iTrainArrays;
       
  3386                 iTrainArrays = NULL;
       
  3387                 
       
  3388                 // Send error message
       
  3389                 iControllerIf.SendSrsEvent( KUidAsrEventAddVoiceTags, iResult );
       
  3390                 }
       
  3391             iTrainArrays = NULL;
       
  3392             break;
       
  3393 #endif
       
  3394         case KCreateRule:
       
  3395             TRAP(iResult, HandleCreateRuleL( iGrammarID,*iRuleIDPtr ) );
       
  3396             iControllerIf.SendSrsEvent( KUidAsrEventCreateRule, iResult );
       
  3397             break;
       
  3398             /*	case KSIRecognize:
       
  3399             TRAP(iResult, HandleRecognizeL(*(CSIClientResultSet*)iSIClientResultSet)) ;
       
  3400             //        iControllerIf.SendSrsEvent(KUidAsrEventSIRecognize, iResult);
       
  3401             break;
       
  3402             */
       
  3403         case KEndRecord:
       
  3404             TRAP( iResult, HandleEndRecordL( ) );
       
  3405             iControllerIf.SendSrsEvent( KUidAsrEventEndRecord, iResult );
       
  3406             break;
       
  3407         case KUnloadGrammar:
       
  3408             TRAP( iResult, HandleUnloadGrammarL( iGrammarID ) ) ;
       
  3409             // UnloadGrammar contain several async functions,
       
  3410             // KUidAsrEventUnloadGrammar will be sent when the UnloadGrammar complete		
       
  3411             //iControllerIf.SendSrsEvent(KUidAsrEventUnloadGrammar, iResult);
       
  3412             break;		
       
  3413             /*case KUpdateGrammarAndLexicon:
       
  3414             TRAP(iResult, HandleUpdateGrammarAndLexiconL(iSITtpWordList,iLexiconID,iGrammarID,*iRuleIDs));
       
  3415             iControllerIf.SendSrsEvent(KUidAsrEventUpdateGrammarAndLexicon, iResult);				
       
  3416             
       
  3417               break;
       
  3418             */
       
  3419             
       
  3420         case KPreStartSampling:
       
  3421             TRAP( iResult, HandlePreStartSamplingL() );
       
  3422             iControllerIf.SendSrsEvent( KUidAsrEventPreStartSampling, iResult );
       
  3423             break;
       
  3424             
       
  3425         default:
       
  3426             // No action
       
  3427             break;
       
  3428     }
       
  3429 }
       
  3430 
       
  3431 // -----------------------------------------------------------------------------
       
  3432 // CSIControllerPlugin::DoAsynch
       
  3433 // This method completes the request status and set the object active
       
  3434 // to provide asynchronous behavior.
       
  3435 // -----------------------------------------------------------------------------
       
  3436 //
       
  3437 void CSIControllerPlugin::DoAsynch()
       
  3438     {
       
  3439     TRequestStatus* pRS = &iStatus;
       
  3440     User::RequestComplete( pRS, KErrNone );
       
  3441     SetActive();
       
  3442     }
       
  3443 
       
  3444 // ============================SI MEMBER FUNCTIONS ===============================
       
  3445 
       
  3446 // -----------------------------------------------------------------------------
       
  3447 // CSIControllerPlugin::AdaptL
       
  3448 // Calls the devasr for adaptation
       
  3449 // -----------------------------------------------------------------------------
       
  3450 //
       
  3451 void CSIControllerPlugin::AdaptL( CSIClientResultSet& aResultSet ,TInt aCorrect ) 
       
  3452     {
       
  3453     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::AdaptL" );
       
  3454     
       
  3455     if ( IsActive() )
       
  3456         {
       
  3457         User::Leave( KErrServerBusy );
       
  3458         }
       
  3459     iRequestFunction = KAdapt;
       
  3460     
       
  3461     iSIClientResultSet  = &aResultSet;
       
  3462     iCorrect = aCorrect;
       
  3463     DoAsynch();
       
  3464     }
       
  3465 
       
  3466 // -----------------------------------------------------------------------------
       
  3467 // CSIControllerPlugin::HandleAdaptL
       
  3468 // Calls the devasr for adaptation
       
  3469 // -----------------------------------------------------------------------------
       
  3470 //
       
  3471 void CSIControllerPlugin::HandleAdaptL( CSIClientResultSet& aResultSet, 
       
  3472                                         TInt aCorrect ) 
       
  3473     {
       
  3474     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleAdaptL" );
       
  3475     if ( iState == ESiPluginRecognize )
       
  3476         {
       
  3477         iSIDatabase->BeginTransactionL();
       
  3478         if ( iSIGrammarDB->IsGrammarLoaded() )
       
  3479             
       
  3480             {
       
  3481             iDevASR->AdaptL( aResultSet.SIResultSet(), aCorrect );
       
  3482             }
       
  3483         else
       
  3484             {
       
  3485             // Either the loaded grammars are empty (contains no rule) or
       
  3486             // all rules have been unloaded.  No need to proceed any further.
       
  3487             iSIDatabase->CommitChangesL( EFalse );
       
  3488             
       
  3489             User::Leave( KErrAsrNoMatch );
       
  3490             }
       
  3491         
       
  3492         iSIDatabase->CommitChangesL( EFalse );
       
  3493         }
       
  3494     else
       
  3495         {
       
  3496         // Wrong state
       
  3497         User::Leave( KErrAsrInvalidState );
       
  3498         }
       
  3499     }
       
  3500 
       
  3501 // -----------------------------------------------------------------------------
       
  3502 // CSIControllerPlugin::AddPronunciationL
       
  3503 // Adds a new pronunciation for the given model into the specified lexicon.
       
  3504 // -----------------------------------------------------------------------------
       
  3505 //                                 
       
  3506 void CSIControllerPlugin::AddPronunciationL( TSILexiconID /*aLexiconID*/,
       
  3507                                              const TDesC& /*aTrainText*/,
       
  3508                                              TLanguage /*aLanguage*/,
       
  3509                                              TSIPronunciationID& /*aPronunciationID*/)  
       
  3510     {
       
  3511     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::AddPronunciationL" );
       
  3512     User::Leave( KErrNotSupported );
       
  3513     }
       
  3514 
       
  3515 
       
  3516 // -----------------------------------------------------------------------------
       
  3517 // CSIControllerPlugin::AddRuleVariantLtionL
       
  3518 // Adds a new rule variant for the given pronunciation into the specified grammar.
       
  3519 // -----------------------------------------------------------------------------
       
  3520 //                                 
       
  3521 void CSIControllerPlugin::AddRuleVariantL( TSIGrammarID aGrammarID,
       
  3522                                            TSILexiconID aLexiconID,
       
  3523                                            RArray<TSIPronunciationID>& aPronunciationIDs, 
       
  3524                                            TSIRuleID aRuleID,										  
       
  3525                                            TSIRuleVariantID& aRuleVariantID )
       
  3526     {
       
  3527     RUBY_DEBUG_BLOCK("CSIControllerPlugin::AddRuleVariantL");
       
  3528     
       
  3529     if ( IsActive() )
       
  3530         {
       
  3531         User::Leave(KErrServerBusy);
       
  3532         }
       
  3533     iRequestFunction = KAddRuleVariant;
       
  3534     iGrammarID = aGrammarID;
       
  3535     iRuleID = aRuleID;
       
  3536     iLexiconID = aLexiconID;
       
  3537     iRuleVariantIDPtr = &aRuleVariantID;	
       
  3538     iPronunciationIDs = &aPronunciationIDs;
       
  3539     DoAsynch();
       
  3540     }
       
  3541 
       
  3542 // -----------------------------------------------------------------------------
       
  3543 // CSIControllerPlugin::HandleAddRuleVariantL
       
  3544 // Adds a new rule variant for the given pronunciation into the specified grammar.
       
  3545 // -----------------------------------------------------------------------------
       
  3546 //                                 
       
  3547 void CSIControllerPlugin::HandleAddRuleVariantL( TSIGrammarID aGrammarID, 
       
  3548                                                  TSILexiconID aLexiconID,
       
  3549                                                  RArray<TSIPronunciationID>& aPronunciationIDs, 
       
  3550                                                  TSIRuleID aRuleID,
       
  3551                                                  TSIRuleVariantID& aRuleVariantID )
       
  3552     {
       
  3553     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleAddRuleVariantL" );
       
  3554     if ( iClientRegistered )
       
  3555         {
       
  3556         iSIDatabase->BeginTransactionL();
       
  3557         iSIGrammarDB->AddRuleVariantL( iClientUid, aGrammarID, aLexiconID, 
       
  3558                                        aPronunciationIDs, aRuleID, aRuleVariantID ); 
       
  3559         iSIDatabase->CommitChangesL( ETrue );
       
  3560         }
       
  3561     else
       
  3562         {
       
  3563         User::Leave( KErrAsrNotRegisted );
       
  3564         }
       
  3565     }
       
  3566 
       
  3567 // -----------------------------------------------------------------------------
       
  3568 // CSIControllerPlugin::AddVoiceTagL
       
  3569 // Trains a new voice tag.
       
  3570 // -----------------------------------------------------------------------------
       
  3571 //  
       
  3572 void CSIControllerPlugin::AddVoiceTagL( MDesCArray& /*aTrainArray*/, 
       
  3573                                         RArray<TLanguage>& /*aLanguageArray*/, 
       
  3574                                         TSILexiconID /*aLexiconID*/,
       
  3575                                         TSIGrammarID /*aGrammarID*/, 
       
  3576                                         TSIRuleID& /*aRuleID*/)
       
  3577     {
       
  3578     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::AddVoiceTagL" );
       
  3579     User::Leave( KErrNotSupported );
       
  3580     }
       
  3581 
       
  3582 
       
  3583 // -----------------------------------------------------------------------------
       
  3584 // CSIControllerPlugin::AddVoiceTagsL
       
  3585 // Adds several new voice tags.
       
  3586 // -----------------------------------------------------------------------------
       
  3587 //
       
  3588 void CSIControllerPlugin::AddVoiceTagsL( RPointerArray<MDesCArray>& aTrainArrays,
       
  3589                                          RArray<TLanguage>& aLanguageArray, 
       
  3590                                          TSILexiconID aLexiconID,
       
  3591                                          TSIGrammarID aGrammarID, 
       
  3592                                          RArray<TSIRuleID>& aRuleIDs)
       
  3593     {
       
  3594     RUBY_DEBUG_BLOCK("CSIControllerPlugin::AddVoiceTagsL");
       
  3595     
       
  3596     if ( IsActive() )
       
  3597         {
       
  3598         User::Leave( KErrServerBusy );
       
  3599         }
       
  3600         
       
  3601     // Use low priority for training (if training is not done from Voice Commands app)        
       
  3602     if ( RProcess().SecureId() != KVoiceCommandsAppUid )
       
  3603         {
       
  3604         RThread().SetPriority( EPriorityAbsoluteLow );
       
  3605         }
       
  3606     
       
  3607     iState = ESiPluginTrainText;
       
  3608     //	iTtpState=EAddVoiceTags;
       
  3609     
       
  3610     iRequestFunction = KAddVoiceTags ;
       
  3611     iLexiconID= aLexiconID;
       
  3612     iGrammarID = aGrammarID;
       
  3613     
       
  3614     iRuleIDs = &aRuleIDs;
       
  3615     iLanguageArray = &aLanguageArray;
       
  3616     iTrainArrays = &aTrainArrays;
       
  3617     
       
  3618     DoAsynch();
       
  3619     }
       
  3620 
       
  3621 #ifdef __SINDE_TRAINING
       
  3622 // -----------------------------------------------------------------------------
       
  3623 // CSIControllerPlugin::AddSindeVoiceTagL
       
  3624 // Trains a new voice tag.
       
  3625 // -----------------------------------------------------------------------------
       
  3626 //  
       
  3627 void CSIControllerPlugin::AddSindeVoiceTagL( MDesCArray& /*aTrainArray*/, 
       
  3628                                              RArray<RLanguageArray>& /*aLanguageArray*/, 
       
  3629                                              TSILexiconID /*aLexiconID*/,
       
  3630                                              TSIGrammarID /*aGrammarID*/, 
       
  3631                                              TSIRuleID& /*aRuleID*/ )
       
  3632     {
       
  3633     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::AddSindeVoiceTagL" );
       
  3634     User::Leave( KErrNotSupported );
       
  3635     }
       
  3636 
       
  3637 // -----------------------------------------------------------------------------
       
  3638 // CSIControllerPlugin::AddSindeVoiceTagsL
       
  3639 // Adds several new voice tags.
       
  3640 // -----------------------------------------------------------------------------
       
  3641 //
       
  3642 void CSIControllerPlugin::AddSindeVoiceTagsL( RPointerArray<MDesCArray>& aTrainArrays,
       
  3643                                               RArray<RLanguageArray>& aLanguageArray, 
       
  3644                                               TSILexiconID aLexiconID,
       
  3645                                               TSIGrammarID aGrammarID, 
       
  3646                                               RArray<TSIRuleID>& aRuleIDs )
       
  3647     {
       
  3648     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::AddSindeVoiceTagsL" );
       
  3649     
       
  3650     if ( IsActive() )
       
  3651         {
       
  3652         User::Leave( KErrServerBusy );
       
  3653         }
       
  3654 
       
  3655     // Use low priority for training (if training is not done from Voice Commands app)
       
  3656     if ( RProcess().SecureId() != KVoiceCommandsAppUid )
       
  3657         {
       
  3658         RThread().SetPriority( EPriorityAbsoluteLow );
       
  3659         }
       
  3660     
       
  3661     iState = ESiPluginTrainSinde;
       
  3662     
       
  3663     iRequestFunction = KAddSindeVoiceTags;
       
  3664     iLexiconID = aLexiconID;
       
  3665     iGrammarID = aGrammarID;
       
  3666     
       
  3667     // Store parameters for asynchronous processing
       
  3668     iRuleIDs = &aRuleIDs;
       
  3669     iLanguageArrayArray = &aLanguageArray;
       
  3670     iTrainArrays = &aTrainArrays;
       
  3671     
       
  3672     DoAsynch();
       
  3673     }
       
  3674 #endif // __SINDE_TRAINING
       
  3675 
       
  3676 // -----------------------------------------------------------------------------
       
  3677 // CSIControllerPlugin::HandleAddVoiceTagsL
       
  3678 // Adds several new voice tags.
       
  3679 // -----------------------------------------------------------------------------
       
  3680 //
       
  3681 void CSIControllerPlugin::HandleAddVoiceTagsL( RPointerArray<MDesCArray>* aTrainArrays,
       
  3682                                                RArray<TLanguage>& aLanguageArray, 
       
  3683                                                TSILexiconID aLexiconID,
       
  3684 											   TSIGrammarID aGrammarID ) 
       
  3685     {
       
  3686     RUBY_DEBUG_BLOCK("CSIControllerPlugin::HandleAddVoiceTagsL");
       
  3687     // 
       
  3688     // first test if both KLexiconID lexicon and KGrammarID are created
       
  3689     if ( IsLexiconIDInvalid( aLexiconID ) || IsGrammarIDInvalid( aGrammarID ) )
       
  3690         {
       
  3691         User::Leave( KErrNotFound ); // no such grammar exist in Database
       
  3692         }
       
  3693     // Need to clean the iSITtpWordList before adding new arrays
       
  3694     delete iSITtpWordList;
       
  3695     iSITtpWordList = NULL;
       
  3696     iSITtpWordList = CSITtpWordList::NewL();
       
  3697     if ( iState == ESiPluginTrainText )
       
  3698         { 
       
  3699         for ( TInt i=0; i < aTrainArrays->Count(); i++ )
       
  3700             {            
       
  3701             iSITtpWordList->AddL( ( *aTrainArrays )[i] );
       
  3702             // If next AddL fails, we must not delete the objects twice - once in
       
  3703             // aTrainArray->ResetAndDestroy() and second time in ~CSITtpWordList.
       
  3704             ( *aTrainArrays )[i] = NULL;
       
  3705             }
       
  3706         aTrainArrays->ResetAndDestroy();
       
  3707         aTrainArrays->Close();
       
  3708         delete aTrainArrays;
       
  3709         aTrainArrays = NULL;
       
  3710         
       
  3711         // Set Max prnounication per word 
       
  3712         iMaxNPronunsForWord.Reset();	
       
  3713         iMaxNPronunsForWord.Append( aLanguageArray.Count() );
       
  3714         
       
  3715         RUBY_DEBUG1( "CSIControllerPlugin::AddVoiceTagsL(aLanguageArray.Count=%d)", aLanguageArray.Count() );
       
  3716         
       
  3717         iDevASR->StartTrainingFromTextL(*iSITtpWordList,aLanguageArray,iMaxNPronunsForWord);
       
  3718         }
       
  3719     else
       
  3720         {   
       
  3721         // Wrong state
       
  3722         User::Leave( KErrAsrInvalidState );
       
  3723         }
       
  3724     }
       
  3725 
       
  3726 #ifdef __SINDE_TRAINING
       
  3727 // -----------------------------------------------------------------------------
       
  3728 // CSIControllerPlugin::HandleAddVoiceTagsL
       
  3729 // Adds several new voice tags.
       
  3730 // -----------------------------------------------------------------------------
       
  3731 //
       
  3732 void CSIControllerPlugin::HandleAddSindeVoiceTagsL( RPointerArray<MDesCArray>* aTrainArrays,
       
  3733                                                     RArray<RLanguageArray>& aLanguageArray, 
       
  3734                                                     TSILexiconID aLexiconID,
       
  3735 											        TSIGrammarID aGrammarID ) 
       
  3736     {
       
  3737     // First test if both KLexiconID lexicon and KGrammarID are created
       
  3738     if ( IsLexiconIDInvalid( aLexiconID ) || IsGrammarIDInvalid( aGrammarID ) )
       
  3739         {
       
  3740         User::Leave( KErrNotFound ); // no such grammar exist in Database
       
  3741         }
       
  3742   
       
  3743     iSindeTrainer->AddSindeVoiceTagsL( aTrainArrays, aLanguageArray, 
       
  3744                                        aLexiconID, aGrammarID, iModelBankID, 
       
  3745                                        iClientUid, *iRuleIDs );
       
  3746 
       
  3747     }
       
  3748 #endif // __SINDE_TRAINING
       
  3749 
       
  3750 // -----------------------------------------------------------------------------
       
  3751 // CSIControllerPlugin::IsGrammarIDInvalid
       
  3752 // is the grammar id valid in the database
       
  3753 // -----------------------------------------------------------------------------
       
  3754 //
       
  3755 TBool CSIControllerPlugin::IsGrammarIDInvalid( TSIGrammarID aGrammarID )
       
  3756     {
       
  3757     TInt i(0);
       
  3758     RArray<TSIGrammarID> aGrammarIDs;
       
  3759     
       
  3760     TRAPD( error, 
       
  3761         iSIDatabase->BeginTransactionL();
       
  3762         iSIGrammarDB->GetAllGrammarIDsL( aGrammarIDs );
       
  3763         iSIDatabase->CommitChangesL( EFalse );
       
  3764         ); // TRAPD
       
  3765     
       
  3766     if ( error == KErrNone )
       
  3767         {
       
  3768         for( i = 0; i < aGrammarIDs.Count(); i++ ) 
       
  3769             {
       
  3770             if ( aGrammarIDs[i] == aGrammarID )
       
  3771                 {
       
  3772                 break;
       
  3773                 }
       
  3774             if ( i == aGrammarIDs.Count() )
       
  3775                 {
       
  3776                 aGrammarIDs.Close();
       
  3777                 return ETrue; // no such Lexicon exist in Database
       
  3778                 }
       
  3779             }
       
  3780         }
       
  3781     aGrammarIDs.Close();
       
  3782     return EFalse;
       
  3783     }
       
  3784 
       
  3785 // -----------------------------------------------------------------------------
       
  3786 // CSIControllerPlugin::IsLexiconIDInvalid
       
  3787 // is the lexicon id valid in the database
       
  3788 // -----------------------------------------------------------------------------
       
  3789 //
       
  3790 TBool CSIControllerPlugin::IsLexiconIDInvalid( TSILexiconID aLexiconID )
       
  3791     {
       
  3792     // first test if both KLexiconID lexicon and KLexiconID are created
       
  3793     TInt i(0);
       
  3794     RArray<TSILexiconID> aLexiconIDs;
       
  3795 
       
  3796     TRAPD( error,     
       
  3797         iSIDatabase->BeginTransactionL();
       
  3798         iSILexiconDB->GetAllLexiconIDsL( aLexiconIDs );
       
  3799         iSIDatabase->CommitChangesL( EFalse );
       
  3800         ); //TRAPD
       
  3801         
       
  3802     if ( error == KErrNone )
       
  3803         {
       
  3804         for( i = 0; i < aLexiconIDs.Count(); i++ ) 
       
  3805             {
       
  3806             if ( aLexiconIDs[i] == aLexiconID ) 
       
  3807                 {
       
  3808                 break;
       
  3809                 }
       
  3810             if ( i == aLexiconIDs.Count() ) 
       
  3811                 {
       
  3812                 aLexiconIDs.Close();
       
  3813                 return ETrue; // no such Lexicon exist in Database
       
  3814                 }
       
  3815             }
       
  3816         }
       
  3817 
       
  3818     aLexiconIDs.Close();
       
  3819     return EFalse;
       
  3820     }
       
  3821 
       
  3822 
       
  3823 // -----------------------------------------------------------------------------
       
  3824 // CSIControllerPlugin::CreateRuleL
       
  3825 // Creates a new empty rule.
       
  3826 // -----------------------------------------------------------------------------
       
  3827 //
       
  3828 void CSIControllerPlugin::CreateRuleL( TSIGrammarID aGrammarID, 
       
  3829                                        TSIRuleID& aRuleID ) 
       
  3830     {
       
  3831     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::CreateRuleL" );
       
  3832     
       
  3833     if ( IsActive() )
       
  3834         {
       
  3835         User::Leave( KErrServerBusy );
       
  3836         }
       
  3837     iRequestFunction = KCreateRule ;
       
  3838     iGrammarID = aGrammarID; 
       
  3839     iRuleIDPtr= &aRuleID;
       
  3840     
       
  3841     DoAsynch();
       
  3842     }
       
  3843 
       
  3844 // -----------------------------------------------------------------------------
       
  3845 // CSIControllerPlugin::HandleCreateRuleL
       
  3846 // Creates a new empty rule.
       
  3847 // -----------------------------------------------------------------------------
       
  3848 //
       
  3849 void CSIControllerPlugin::HandleCreateRuleL( TSIGrammarID aGrammarID, 
       
  3850                                              TSIRuleID& aRuleID ) 
       
  3851     {
       
  3852     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleCreateRuleL" );
       
  3853     if ( iClientRegistered )
       
  3854         {
       
  3855         iSIDatabase->BeginTransactionL();
       
  3856         iSIGrammarDB->CreateRuleL( iClientUid, aGrammarID, aRuleID ); 
       
  3857         iSIDatabase->CommitChangesL( EFalse );
       
  3858         }
       
  3859     else
       
  3860         {
       
  3861         User::Leave( KErrAsrNotRegisted );
       
  3862         }
       
  3863     }
       
  3864 
       
  3865 // -----------------------------------------------------------------------------
       
  3866 // CSIControllerPlugin::EndRecordL
       
  3867 // End recordingEnds recording. Unlike Cancel(), this function may return a 
       
  3868 // recognition result if adequate number of samples was already recorded.
       
  3869 // -----------------------------------------------------------------------------
       
  3870 //
       
  3871 void CSIControllerPlugin::EndRecordL()
       
  3872     {
       
  3873     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::EndRecordL" );
       
  3874     
       
  3875     if ( IsActive() )
       
  3876         {
       
  3877         User::Leave( KErrServerBusy );
       
  3878         }
       
  3879     iRequestFunction = KEndRecord ; 
       
  3880     DoAsynch();
       
  3881 }
       
  3882 
       
  3883 // -----------------------------------------------------------------------------
       
  3884 // CSIControllerPlugin::HandleEndRecordL
       
  3885 // End recordingEnds recording. Unlike Cancel(), this function may return a 
       
  3886 // recognition result if adequate number of samples was already recorded.
       
  3887 // -----------------------------------------------------------------------------
       
  3888 //
       
  3889 void CSIControllerPlugin::HandleEndRecordL()
       
  3890     {
       
  3891     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleEndRecordL" );
       
  3892     iDevASR->EndRecord();	
       
  3893     }
       
  3894 
       
  3895 // -----------------------------------------------------------------------------
       
  3896 // CSIControllerPlugin::UnloadGrammarL
       
  3897 // Unload a grammar from memory
       
  3898 // -----------------------------------------------------------------------------
       
  3899 //
       
  3900 void CSIControllerPlugin::UnloadGrammarL( TSIGrammarID aGrammarID )
       
  3901     {
       
  3902     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::UnloadGrammarL" );
       
  3903     
       
  3904     if ( IsActive() )
       
  3905         {
       
  3906         User::Leave( KErrServerBusy );
       
  3907         }
       
  3908     iRequestFunction = KUnloadGrammar ;
       
  3909     iGrammarID=aGrammarID;
       
  3910     DoAsynch();
       
  3911     }
       
  3912 
       
  3913 // -----------------------------------------------------------------------------
       
  3914 // CSIControllerPlugin::HandleUnloadGrammarL
       
  3915 // Unload a grammar from memory
       
  3916 // -----------------------------------------------------------------------------
       
  3917 //
       
  3918 void CSIControllerPlugin::HandleUnloadGrammarL( TSIGrammarID aGrammarID )
       
  3919     {
       
  3920     RUBY_DEBUG_BLOCK( "CSIControllerPlugin::HandleUnloadGrammarL" );
       
  3921     
       
  3922     iSIDatabase->BeginTransactionL();
       
  3923     CSICompiledGrammar* LoadedGrammar =iSIGrammarDB->FindGrammarL( aGrammarID );	 
       
  3924     iSIDatabase->CommitChangesL( EFalse );
       
  3925         
       
  3926     iDevASR->UnloadGrammar( *LoadedGrammar );	// async call to unload grammar in devasr
       
  3927     }
       
  3928 
       
  3929 //  End of File