hti/HtiFramework/src/HtiDispatcher.cpp
changeset 0 a03f92240627
child 4 73ff0d268e1d
equal deleted inserted replaced
-1:000000000000 0:a03f92240627
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  This file contains the implementations of the CHTIDispatcher
       
    15 *                class.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "HtiDispatcher.h"
       
    21 #include "HtiMessage.h"
       
    22 #include "HtiMessageQueue.h"
       
    23 #include "HTILogging.h"
       
    24 #include "HtiCommAdapter.h"
       
    25 #include "HTIServicePluginInterface.h"
       
    26 #include "HTICommPluginInterface.h"
       
    27 #include "HtiSecurityManager.h"
       
    28 #include "HtiVersion.h"
       
    29 
       
    30 #include <e32debug.h>
       
    31 
       
    32 //active objects priorities
       
    33 const static TInt KHtiIdlePriority = 2;
       
    34 
       
    35 const static TInt KDefaultMaxQueueSize = 4 * 1024 * 1024; // 4 MB
       
    36 
       
    37 //HTI system service commands
       
    38 enum THtiCommand
       
    39     {
       
    40     EHtiAuthentication = 0x01,
       
    41     EHtiVersion        = 0x02,
       
    42     EHtiServiceList    = 0x03,
       
    43     EHtiStop           = 0x04,
       
    44     EHtiReboot         = 0x05,
       
    45     EHtiFormat         = 0x06,
       
    46     EHtiReset          = 0x07,
       
    47     EHtiShowConsole    = 0x08,
       
    48     EHtiHideConsole    = 0x09,
       
    49     EHtiInstanceId     = 0x0A,
       
    50     EHtiDebugPrint     = 0x0B,
       
    51     EHtiError          = 0xFF
       
    52     };
       
    53 
       
    54 //HTI error messages
       
    55 const static TInt KMaxErrMessageLength = KErrorDescriptionMaxLength + 10;
       
    56 
       
    57 //error descriptions
       
    58 _LIT8( KHtiSystemCmdErrDescr, "Unknown HTI command" );
       
    59 _LIT8( KErrDescrDispatchOut, "Failed to dispatch message" );
       
    60 _LIT8( KErrDescrWrap, "Failed to wrap message" );
       
    61 _LIT8( KErrDescrDispatchOutError, "Failed to dispatch error message" );
       
    62 _LIT8( KErrDescrInvalidParameter, "Invalid command parameter" );
       
    63 _LIT8( KErrDescrNotInRom, "Command supported only if HTI is running from ROM" );
       
    64 
       
    65 const static TChar KSpChar = ' ';
       
    66 const static TChar KNewLineChar = '\n';
       
    67 const static TInt KHtiServiceNameLength = 124;
       
    68 const static TInt KHtiServiceListRecordLength = KHtiServiceNameLength + 4;
       
    69 
       
    70 _LIT( KHtiWatchDogMatchPattern, "HtiWatchDog*" );
       
    71 _LIT( KHtiDeviceRebootExeOS,    "HtiDeviceRebootOS.exe" );
       
    72 _LIT( KHtiDeviceRebootExeUI,    "HtiDeviceRebootUI.exe" );
       
    73 _LIT( KParamNormalRfs, "rfsnormal" );
       
    74 _LIT( KParamDeepRfs,   "rfsdeep" );
       
    75 
       
    76 TDesC8* THtiSystemProtocolHelper::ErrorMessageL( TInt aHtiErrorCode,
       
    77                                   const TUid aTargetServiceUid )
       
    78     {
       
    79     HBufC8* msg = HBufC8::NewL( KMaxErrMessageLength );
       
    80     msg->Des().Append( EHtiError ); //one byte
       
    81     msg->Des().Append( aHtiErrorCode ); //one byte
       
    82     msg->Des().AppendFill( 0x00, 4 ); //missed service error code
       
    83     //append uid as TInt32 by copying 4 bytes through pointer
       
    84     msg->Des().Append( ( TUint8* )( &( aTargetServiceUid.iUid ) ), 4 );
       
    85     return msg;
       
    86     }
       
    87 
       
    88 TDesC8* THtiSystemProtocolHelper::ErrorMessageL( TInt aHtiErrorCode,
       
    89                                   const TUid aTargetServiceUid,
       
    90                                   TInt aErrorCode,
       
    91                                   const TDesC8& aErrorDescription )
       
    92     {
       
    93     HBufC8* msg = HBufC8::NewL( KMaxErrMessageLength );
       
    94     msg->Des().Append( EHtiError ); //one byte
       
    95     msg->Des().Append( aHtiErrorCode ); //one byte
       
    96     msg->Des().Append( ( TUint8* ) ( &aErrorCode ), 4 ); //4 bytes
       
    97     //append uid as TInt32 by copying 4 bytes through pointer
       
    98     msg->Des().Append( ( TUint8* )( &( aTargetServiceUid.iUid ) ), 4 );
       
    99     msg->Des().Append( aErrorDescription );
       
   100     return msg;
       
   101     }
       
   102 
       
   103 TDesC8* THtiSystemProtocolHelper::AuthMessageL( const TDesC8& aToken )
       
   104     {
       
   105     HBufC8* msg = HBufC8::NewL( aToken.Length() + 1 );
       
   106     msg->Des().Append( EHtiAuthentication );
       
   107     msg->Des().Append( aToken );
       
   108     return msg;
       
   109     }
       
   110 
       
   111 /*************************************************************************
       
   112 *   CHtiDispatcher implementation
       
   113 *
       
   114 **************************************************************************/
       
   115 CHtiDispatcher::CHtiDispatcher( TInt aMaxQueueMemorySize,
       
   116                                 TBool aShowErrorDialogs ):
       
   117     iIdleActive( EFalse ),
       
   118     iMaxQueueMemorySize( aMaxQueueMemorySize > 0 ?
       
   119                          aMaxQueueMemorySize : KDefaultMaxQueueSize ),
       
   120     iToReboot( EFalse ),
       
   121     iRfsMode( ERfsUnknown ),
       
   122     iConsole( NULL ),
       
   123     iIdleOverCommAdapter( EFalse ),
       
   124     iHtiInstanceId( 0 ),
       
   125     iShowErrorDialogs( aShowErrorDialogs )
       
   126     {
       
   127     HTI_LOG_FORMAT( "MaxQueueMemorySize %d", iMaxQueueMemorySize );
       
   128     iQueueSizeLowThresold = ( iMaxQueueMemorySize / 2 ) / 2;
       
   129     iQueueSizeHighThresold = ( iMaxQueueMemorySize / 2 ) * 4 / 5;
       
   130     HTI_LOG_FORMAT( "QueueSizeThresholds low : %d", iQueueSizeLowThresold );
       
   131     HTI_LOG_FORMAT( "QueueSizeThresholds high: %d", iQueueSizeHighThresold );
       
   132     }
       
   133 
       
   134 void CHtiDispatcher::ConstructL( const TDesC8& aCommPlugin,
       
   135                                  TInt aMaxMsgSize,
       
   136                                  TBool aShowConsole )
       
   137     {
       
   138     HTI_LOG_FUNC_IN( "CHTIDispatcher::ConstructL()" );
       
   139 #ifdef __ENABLE_LOGGING__
       
   140     DebugListPlugins();
       
   141 #endif
       
   142 
       
   143     if ( aShowConsole )
       
   144         {
       
   145         // Create the HTI console
       
   146         iConsole = Console::NewL( _L( "HtiFramework" ),
       
   147                                   TSize( KConsFullScreen, KConsFullScreen ) );
       
   148         iConsole->Printf( _L( "HTI Framework\n" ) );
       
   149         iConsole->Printf( _L( "=============\n\n" ) );
       
   150         iConsole->Printf( _L( "Version %u.%u\n" ),
       
   151                 KHtiVersionMajor, KHtiVersionMinor );
       
   152         iConsole->Printf( _L( "Starting up...\n" ) );
       
   153         }
       
   154 
       
   155     //create queues
       
   156     iIncomingQueue = CHtiMessageQueue::NewL();
       
   157     iOutgoingQueue = CHtiMessageQueue::NewL();
       
   158 
       
   159     //security manager init
       
   160     iSecurityManager = CHtiSecurityManager::NewL();
       
   161 
       
   162     //plugins array
       
   163     iLoadedServices = new (ELeave)
       
   164                         RArray<TServiceItem>( KServiceArrayGranularity );
       
   165 
       
   166     iMemoryObservers = new (ELeave)
       
   167                         RPointerArray<MHtiMemoryObserver>( KServiceArrayGranularity );
       
   168 
       
   169     //try to load default plugin if specified can't be found
       
   170     if ( aCommPlugin.Length() == 0 )
       
   171         {
       
   172         HTI_LOG_TEXT( "load default comm plugin" );
       
   173         iCommPlugin = CHTICommPluginInterface::NewL();
       
   174         }
       
   175     else
       
   176         {
       
   177         HTI_LOG_TEXT( "load Comm plugin" );
       
   178         HTI_LOG_DES( aCommPlugin );
       
   179         iCommPlugin = CHTICommPluginInterface::NewL( aCommPlugin );
       
   180         }
       
   181 
       
   182     if ( iConsole )
       
   183         {
       
   184         iConsole->Printf( _L( "Communication plugin loaded.\n" ) );
       
   185         }
       
   186 
       
   187     iListener = CHtiCommAdapter::NewL( iCommPlugin, this, aMaxMsgSize );
       
   188     iSender = CHtiCommAdapter::NewL( iCommPlugin, this, aMaxMsgSize );
       
   189 
       
   190     //!!!!!!!!!!!!! -=IMPORTANT=- !!!!!!!!!!!!!!!!!!!!!!!!!!!!
       
   191     // idle should be created (added to AS) AFTER comm adapters
       
   192     //!!!!!!!!!!!!! -=IMPORTANT=- !!!!!!!!!!!!!!!!!!!!!!!!!!!!
       
   193     //create CIdle object
       
   194     iIdle = CIdle::NewL( KHtiIdlePriority );
       
   195 
       
   196     // start listening for incoming messages
       
   197     Reset();
       
   198 
       
   199     HTI_LOG_FUNC_OUT( "CHTIDispatcher::ConstructL()" );
       
   200     }
       
   201 
       
   202 CHtiDispatcher::~CHtiDispatcher()
       
   203     {
       
   204     HTI_LOG_FUNC_IN( "CHTIDispatcher::~CHTIDispatcher" );
       
   205 
       
   206     if ( iIdle )
       
   207     {
       
   208         iIdle->Cancel();
       
   209         delete iIdle;
       
   210     }
       
   211 
       
   212     UnloadAllServices();
       
   213 
       
   214     if ( iLoadedServices )
       
   215         {
       
   216         iLoadedServices->Close();
       
   217         delete iLoadedServices;
       
   218         }
       
   219 
       
   220     if ( iMemoryObservers )
       
   221     {
       
   222         iMemoryObservers->Close();
       
   223         delete iMemoryObservers;
       
   224     }
       
   225 
       
   226     delete iIncomingQueue;
       
   227     delete iOutgoingQueue;
       
   228 
       
   229     delete iListener;
       
   230     delete iSender;
       
   231 
       
   232     delete iCommPlugin;
       
   233 
       
   234     REComSession::FinalClose();
       
   235 
       
   236     delete iSecurityManager;
       
   237 
       
   238     if ( iRfsMode == ERfsNormal || iRfsMode == ERfsDeep )
       
   239         {
       
   240         HTI_LOG_FORMAT( "Activating Restore Factory Settings %d", iRfsMode );
       
   241         RestoreFactorySettings();
       
   242         }
       
   243 
       
   244     if ( iToReboot )
       
   245         {
       
   246         HTI_LOG_TEXT( "Reboot now" );
       
   247         Reboot();
       
   248         }
       
   249 
       
   250     delete iConsole;
       
   251 
       
   252     HTI_LOG_FUNC_OUT( "CHTIDispatcher::~CHTIDispatcher" );
       
   253     }
       
   254 
       
   255 
       
   256 CHtiDispatcher* CHtiDispatcher::NewLC( const TDesC8& aCommPlugin,
       
   257                                 TInt aMaxMsgSize,
       
   258                                 TInt aMaxQueueMemory,
       
   259                                 TBool aShowConsole,
       
   260                                 TBool aShowErrorDialogs )
       
   261     {
       
   262     CHtiDispatcher* obj = new (ELeave) CHtiDispatcher(
       
   263             aMaxQueueMemory, aShowErrorDialogs );
       
   264     CleanupStack::PushL( obj );
       
   265     obj->ConstructL( aCommPlugin, aMaxMsgSize, aShowConsole );
       
   266     return obj;
       
   267     }
       
   268 
       
   269 CHtiDispatcher* CHtiDispatcher::NewL( const TDesC8& aCommPlugin,
       
   270                                 TInt aMaxMsgSize,
       
   271                                 TInt aMaxQueueMemory,
       
   272                                 TBool aShowConsole,
       
   273                                 TBool aShowErrorDialogs )
       
   274     {
       
   275     CHtiDispatcher* obj = NewLC( aCommPlugin, aMaxMsgSize, aMaxQueueMemory,
       
   276             aShowConsole, aShowErrorDialogs );
       
   277     CleanupStack::Pop();
       
   278     return obj;
       
   279     }
       
   280 
       
   281 CConsoleBase* CHtiDispatcher::GetConsole()
       
   282     {
       
   283     return iConsole;
       
   284     }
       
   285 
       
   286 TBool CHtiDispatcher::GetShowErrorDialogs()
       
   287     {
       
   288     return iShowErrorDialogs;
       
   289     }
       
   290 
       
   291 void CHtiDispatcher::CheckPriorities()
       
   292     {
       
   293     // If incoming queue reaches some high limit then lower its priority
       
   294     // below idle object priority.
       
   295     // Make opposite when incoming queue size reaches some low limit
       
   296     if ( iIncomingQueue->QueueSize() > iQueueSizeHighThresold )
       
   297         {
       
   298         if ( !( iListener->IsActive() || iIdleOverCommAdapter ) )
       
   299             {
       
   300             HTI_LOG_TEXT( "Set listener priority low" );
       
   301             iListener->Deque();
       
   302             CActiveScheduler::Add( iListener );
       
   303             iIdleOverCommAdapter = ETrue;
       
   304             }
       
   305         }
       
   306     }
       
   307 
       
   308 void CHtiDispatcher::DispatchIncomingMessage( CHtiMessage* aMessage )
       
   309     {
       
   310     HTI_LOG_FUNC_IN( "DispatchIncomingMessage" );
       
   311 
       
   312     iIncomingQueue->Add( *aMessage );
       
   313 
       
   314     //start CIdle if needed
       
   315     Start();
       
   316     CheckPriorities();
       
   317 
       
   318     HTI_LOG_FUNC_OUT( "DispatchIncomingMessage" );
       
   319     }
       
   320 
       
   321 TInt CHtiDispatcher::DispatchOutgoingMessage( TDesC8* aMessage,
       
   322                     const TUid aTargetServiceUid,
       
   323                     TBool aWrappedFlag,
       
   324                     THtiMessagePriority aPriority )
       
   325     {
       
   326     HTI_LOG_FUNC_IN( "DispatchOutgoingMessage" );
       
   327     HTI_LOG_TEXT( "Construct HTI message" );
       
   328 
       
   329     //send only if enough memory
       
   330     TInt returnErr = KErrNone;
       
   331     if ( aMessage->Size() <= GetFreeMemory() )
       
   332         {
       
   333         //call here wrapping
       
   334         CHtiMessage* msg = NULL;
       
   335         if ( aWrappedFlag )
       
   336             {
       
   337             TDesC8* wrapped = NULL;
       
   338             TRAP( returnErr, wrapped = iSecurityManager->WrapL( *aMessage ) );
       
   339             if ( returnErr == KErrNone )
       
   340                 {
       
   341                 TRAP( returnErr,
       
   342                       msg = CHtiMessage::NewL( wrapped, aTargetServiceUid,
       
   343                             aWrappedFlag, aPriority ) );
       
   344                 if ( returnErr != KErrNone )
       
   345                     {
       
   346                     UrgentReboot( returnErr, KErrDescrDispatchOut );
       
   347                     }
       
   348                 //wrapped message is kept, original is deleted
       
   349                 delete aMessage;
       
   350                 }
       
   351             else
       
   352                 {
       
   353                 UrgentReboot( returnErr, KErrDescrWrap );
       
   354                 }
       
   355             }
       
   356         else
       
   357             {
       
   358             TRAP( returnErr, msg = CHtiMessage::NewL( aMessage,
       
   359                                 aTargetServiceUid, aWrappedFlag, aPriority ) );
       
   360 
       
   361             if ( returnErr != KErrNone )
       
   362                 {
       
   363                 UrgentReboot( returnErr, KErrDescrDispatchOut );
       
   364                 }
       
   365             }
       
   366 
       
   367         // put in a queue
       
   368         if ( msg )
       
   369             iOutgoingQueue->Add( *msg );
       
   370 
       
   371         //start CIdle if needed
       
   372         Start();
       
   373         }
       
   374     else
       
   375         {
       
   376         returnErr = KErrNoMemory;
       
   377         }
       
   378 
       
   379     HTI_LOG_FUNC_OUT( "DispatchOutgoingMessage" );
       
   380     return returnErr;
       
   381     }
       
   382 
       
   383 TInt CHtiDispatcher::DispatchOutgoingErrorMessage( TInt aErrorCode,
       
   384                     const TDesC8& aErrorDescription,
       
   385                     const TUid aTargetServiceUid )
       
   386     {
       
   387     HTI_LOG_FUNC_IN( "DispatchError" );
       
   388 
       
   389     if ( aTargetServiceUid == TUid::Null() )
       
   390         {
       
   391         return KErrArgument;
       
   392         }
       
   393 
       
   394     HTI_LOG_FORMAT( "ErrorCode %d", aErrorCode );
       
   395     TInt err = KErrNone;
       
   396     TDesC8* msg = NULL;
       
   397     TRAP( err, msg = THtiSystemProtocolHelper::ErrorMessageL(
       
   398                         EHtiErrServiceError,
       
   399                         aTargetServiceUid,
       
   400                         aErrorCode,
       
   401                         aErrorDescription ) );
       
   402 
       
   403     if ( err != KErrNone )
       
   404         {
       
   405         UrgentReboot( err, KErrDescrDispatchOutError );
       
   406         }
       
   407 
       
   408     err = DispatchOutgoingMessage( msg, KHtiSystemServiceUid,
       
   409                              EFalse, EHtiPriorityDefault );
       
   410 
       
   411     if ( err == KErrNoMemory )
       
   412         {
       
   413         delete msg;
       
   414         }
       
   415 
       
   416     HTI_LOG_FUNC_OUT( "DispatchError" );
       
   417     return err;
       
   418     }
       
   419 
       
   420 TInt CHtiDispatcher::DispatchOutgoingErrorMessage( THtiError aHtiErroreCode,
       
   421                                     TInt aLeaveCode,
       
   422                                     const TUid aTargetServiceUid )
       
   423     {
       
   424     HTI_LOG_FORMAT( "leaveCode %d", aLeaveCode );
       
   425     TInt err = KErrNone;
       
   426     TDesC8* msg = NULL;
       
   427 
       
   428     TRAP( err, msg = THtiSystemProtocolHelper::ErrorMessageL(
       
   429                         aHtiErroreCode,
       
   430                         aTargetServiceUid,
       
   431                         aLeaveCode,
       
   432                         KNullDesC8 ) );
       
   433 
       
   434     if ( err != KErrNone )
       
   435         {
       
   436         UrgentReboot( err, KErrDescrDispatchOutError );
       
   437         }
       
   438 
       
   439     err = DispatchOutgoingMessage( msg, KHtiSystemServiceUid,
       
   440                              EFalse, EHtiPriorityDefault );
       
   441 
       
   442     if ( err == KErrNoMemory )
       
   443         {
       
   444         delete msg;
       
   445         }
       
   446 
       
   447     return err;
       
   448     }
       
   449 
       
   450 TInt CHtiDispatcher::DispatchOutgoingErrorMessage( THtiError aHtiErrorCode,
       
   451                                     const TUid aTargetServiceUid )
       
   452     {
       
   453     HTI_LOG_FORMAT( "HtiErrorCode %d", aHtiErrorCode );
       
   454     TInt err = KErrNone;
       
   455     TDesC8* msg = NULL;
       
   456 
       
   457     TRAP( err, msg = THtiSystemProtocolHelper::ErrorMessageL( aHtiErrorCode,
       
   458                                                 aTargetServiceUid ) );
       
   459 
       
   460     if ( err != KErrNone )
       
   461         {
       
   462         UrgentReboot( err, KErrDescrDispatchOutError );
       
   463         }
       
   464 
       
   465     err = DispatchOutgoingMessage( msg, KHtiSystemServiceUid,
       
   466                              EFalse, EHtiPriorityDefault );
       
   467 
       
   468     if ( err == KErrNoMemory )
       
   469         {
       
   470         delete msg;
       
   471         }
       
   472 
       
   473     return err;
       
   474     }
       
   475 
       
   476 TInt CHtiDispatcher::DispatchOutgoingMessage(TDesC8* aMessage,
       
   477                     const TUid aTargetServiceUid)
       
   478     {
       
   479     return DispatchOutgoingMessage( aMessage, aTargetServiceUid,
       
   480                              EFalse, EHtiPriorityDefault );
       
   481     }
       
   482 
       
   483 void CHtiDispatcher::AddMemoryObserver( MHtiMemoryObserver* anObserver )
       
   484     {
       
   485     if ( iMemoryObservers->FindInAddressOrder( anObserver ) == KErrNotFound )
       
   486         {
       
   487         iMemoryObservers->InsertInAddressOrder( anObserver );
       
   488         }
       
   489     }
       
   490 
       
   491 void CHtiDispatcher::RemoveMemoryObserver( MHtiMemoryObserver* anObserver )
       
   492     {
       
   493     TInt removeIndex = iMemoryObservers->FindInAddressOrder( anObserver );
       
   494     if ( removeIndex != KErrNotFound )
       
   495         {
       
   496         iMemoryObservers->Remove( removeIndex );
       
   497         }
       
   498     }
       
   499 
       
   500 void CHtiDispatcher::DoMemoryNotification()
       
   501     {
       
   502     if ( iMemoryObservers->Count() > 0 )
       
   503         {
       
   504         TInt memory = GetFreeMemory();
       
   505         for ( TInt i = 0; i < iMemoryObservers->Count(); ++i )
       
   506             {
       
   507             ( *iMemoryObservers )[i]->NotifyMemoryChange( memory );
       
   508             }
       
   509         }
       
   510     }
       
   511 
       
   512 TInt CHtiDispatcher::GetFreeMemory()
       
   513     {
       
   514     return iMaxQueueMemorySize
       
   515            - iIncomingQueue->QueueSize()
       
   516            - iOutgoingQueue->QueueSize();
       
   517     }
       
   518 
       
   519 void CHtiDispatcher::Start()
       
   520     {
       
   521     if ( !( iIdle->IsActive() || iIdleActive ) )
       
   522         {
       
   523         HTI_LOG_TEXT( "Start CIdle" );
       
   524         iIdleActive = ETrue;
       
   525         iIdle->Start( TCallBack( DispatchCallback, this ) );
       
   526         }
       
   527     }
       
   528 
       
   529 void CHtiDispatcher::Notify( TInt anError )
       
   530     {
       
   531     HTI_LOG_FORMAT( "CHtiDispatcher::Notify: %d", anError );
       
   532     anError = anError;
       
   533     //start CIdle to check for messages
       
   534     Start();
       
   535     }
       
   536 
       
   537 TInt CHtiDispatcher::DoDispatch()
       
   538     {
       
   539     HTI_LOG_FUNC_IN( "DoDispatch" );
       
   540 
       
   541     TBool isFailed = ETrue;
       
   542 
       
   543     //dispatch
       
   544     CHtiMessage* msg = NULL;
       
   545 
       
   546     //Process message from the queues
       
   547     if ( !iSender->IsActive() )
       
   548         {
       
   549         msg = iOutgoingQueue->Remove();
       
   550         if ( msg )
       
   551             {
       
   552             //process outgoing
       
   553             iSender->SendMessage( msg );
       
   554             isFailed = EFalse;
       
   555             }
       
   556 
       
   557         //after some messages removed do memory notification
       
   558         DoMemoryNotification();
       
   559         }
       
   560 
       
   561     iIncomingQueue->StartServiceIteration();
       
   562     TBool msgProcessed = EFalse;
       
   563     while ( ( msg = iIncomingQueue->GetNext() ) != NULL && !msgProcessed )
       
   564         {
       
   565         //processing of incoming HTI message
       
   566         HTI_LOG_TEXT( "incoming msg" );
       
   567 
       
   568         //1. find service
       
   569         HTI_LOG_TEXT( "service uid" );
       
   570         TUid cmd = msg->DestinationServiceUid();
       
   571         THtiMessagePriority msgPriority = ( msg->Priority() && KPriorityMask ) ?
       
   572                                             EHtiPriorityControl:
       
   573                                             EHtiPriorityData;
       
   574         HTI_LOG_FORMAT( "UID: %d", cmd.iUid );
       
   575 
       
   576         //check if it's a system message
       
   577         if ( cmd == KHtiSystemServiceUid )
       
   578             {
       
   579             TPtrC8 body = msg->Body();
       
   580             TRAPD( err, HandleSystemMessageL( body ) );
       
   581             if ( err != KErrNone )
       
   582                 {
       
   583                 //do nothing
       
   584                 HTI_LOG_FORMAT( "Failed handle HTI service, err %d", err );
       
   585                 }
       
   586             msgProcessed = ETrue;
       
   587             }
       
   588         else
       
   589             {
       
   590             if ( iSecurityManager->IsContextEstablashed() )
       
   591                 {
       
   592                 CHTIServicePluginInterface* service = GetService( cmd );
       
   593                 if ( service )
       
   594                     {
       
   595                     //2. call service
       
   596                     if ( !service->IsBusy() )
       
   597                         {
       
   598                         TInt err;
       
   599                         if ( msg->IsWrapped() )
       
   600                             {
       
   601                             TDesC8* unwrapped = NULL;
       
   602                             TRAP( err,
       
   603                                   unwrapped = iSecurityManager->UnwrapL(
       
   604                                                 msg->Body() ) );
       
   605                             if ( err == KErrNone && unwrapped )
       
   606                                 {
       
   607                                 TRAP( err, service->ProcessMessageL(
       
   608                                                 *unwrapped,
       
   609                                                 msgPriority ) );
       
   610 
       
   611                                 delete unwrapped;
       
   612                                 }
       
   613                             else
       
   614                                 {
       
   615                                 HTI_LOG_FORMAT( "ERROR: Unwrap %d", err );
       
   616                                 DispatchOutgoingErrorMessage( EHtiErrUnwrap,
       
   617                                     err, cmd );
       
   618                                 err = KErrNone;
       
   619                                 }
       
   620                             }
       
   621                         else
       
   622                             {
       
   623                             TPtrC8 body = msg->Body();
       
   624                             TRAP( err, service->ProcessMessageL( body,
       
   625                                                                 msgPriority ) );
       
   626                             }
       
   627 
       
   628                         if ( err != KErrNone )
       
   629                             {
       
   630                             HTI_LOG_FORMAT( "ERROR: Service Error %d", err );
       
   631                             DispatchOutgoingErrorMessage( EHtiErrServiceError,
       
   632                                                 err, cmd );
       
   633                             }
       
   634 
       
   635                         msgProcessed = ETrue;
       
   636                         }
       
   637                     else
       
   638                         {
       
   639                         HTI_LOG_TEXT( "service is busy" );
       
   640                         }
       
   641                     }
       
   642                 else
       
   643                     {
       
   644                     //send error message ServiceNotFound
       
   645                     HTI_LOG_TEXT( "ERROR: service not found" );
       
   646                     DispatchOutgoingErrorMessage( EHtiErrServiceNotFound, cmd );
       
   647                     msgProcessed = ETrue;
       
   648                     }
       
   649                 }
       
   650             else
       
   651                 {
       
   652                 //not authorized acces
       
   653                 HTI_LOG_TEXT( "ERROR: not authorized acces" );
       
   654                 DispatchOutgoingErrorMessage( EHtiErrNotAuthorized, cmd );
       
   655                 msgProcessed = ETrue;
       
   656                 }
       
   657             }
       
   658         if ( msgProcessed )
       
   659             {
       
   660             //remove msg from dispatcher
       
   661             if ( iIncomingQueue->Remove( msg ) )
       
   662                 {
       
   663                 delete msg;
       
   664                 }
       
   665 
       
   666             isFailed = EFalse;
       
   667             }
       
   668         }
       
   669 
       
   670     HTI_LOG_FORMAT( "IQ:%d", iIncomingQueue->QueueSize() );
       
   671     HTI_LOG_FORMAT( "OQ:%d", iOutgoingQueue->QueueSize() );
       
   672 
       
   673     HTI_LOG_FREE_MEM();
       
   674     HTI_LOG_ALLOC_HEAP_MEM();
       
   675 
       
   676     //if queues are empty & listener stopped - stop all
       
   677     if ( iIncomingQueue->IsEmpty() && iOutgoingQueue->IsEmpty() &&
       
   678         !iListener->IsActive() )
       
   679         {
       
   680         CActiveScheduler::Stop();
       
   681         }
       
   682 
       
   683     if ( iIncomingQueue->QueueSize() < iQueueSizeLowThresold && iIdleOverCommAdapter )
       
   684         {
       
   685         HTI_LOG_TEXT( "set listener priority high" );
       
   686         iIdle->Deque();
       
   687         CActiveScheduler::Add( iIdle );
       
   688         iIdleOverCommAdapter = EFalse;
       
   689         }
       
   690 
       
   691     if ( isFailed )
       
   692         {
       
   693         HTI_LOG_TEXT( "dispatch failed, stop CIdle" );
       
   694         //stop iIdle if there are long outgoing requests
       
   695         iIdleActive = EFalse;
       
   696         }
       
   697     else
       
   698         {
       
   699         iIdleActive = !iIncomingQueue->IsEmpty() || !iOutgoingQueue->IsEmpty();
       
   700         }
       
   701 
       
   702     HTI_LOG_FUNC_OUT( "DoDispatch" );
       
   703     return iIdleActive;
       
   704     }
       
   705 
       
   706 CHTIServicePluginInterface* CHtiDispatcher::GetService(
       
   707     const TUid aServiceUid )
       
   708     {
       
   709     CHTIServicePluginInterface* result = NULL;
       
   710     for ( TInt i = 0; i < iLoadedServices->Count(); ++i )
       
   711         {
       
   712         if ( aServiceUid == (*iLoadedServices)[i].iServiceUid )
       
   713             return (*iLoadedServices)[i].iService;
       
   714         }
       
   715 
       
   716     HTI_LOG_FORMAT( "Load service: %d", aServiceUid.iUid );
       
   717 
       
   718     TRAPD( err, result = CHTIServicePluginInterface::NewL(
       
   719         MapServicePluginUid( aServiceUid ) ) );
       
   720     if ( err == KErrNone )
       
   721         {
       
   722         //set dispatcher
       
   723         result->SetDispatcher( this );
       
   724         //call InitL()
       
   725         TRAP( err, result->InitL() );
       
   726         if ( err != KErrNone )
       
   727             {
       
   728             HTI_LOG_TEXT( "Failed at InitL()" );
       
   729             delete result;
       
   730             result = NULL;
       
   731             }
       
   732         else
       
   733             {
       
   734             //add service to the array
       
   735             TServiceItem serviceItem;
       
   736             serviceItem.iServiceUid = aServiceUid;
       
   737             serviceItem.iService = result;
       
   738 
       
   739             if ( iLoadedServices->Append( serviceItem ) != KErrNone )
       
   740                 {
       
   741                 HTI_LOG_TEXT( "Failed to load service" );
       
   742                 delete result;
       
   743                 result = NULL;
       
   744                 }
       
   745             }
       
   746         }
       
   747     else
       
   748         {
       
   749         result = NULL;
       
   750         HTI_LOG_FORMAT( "Failed to load service %d", err );
       
   751         }
       
   752 
       
   753     return result;
       
   754     }
       
   755 
       
   756 void CHtiDispatcher::UnloadAllServices()
       
   757     {
       
   758     if ( iLoadedServices )
       
   759         {
       
   760         for ( TInt i=0; i < iLoadedServices->Count(); ++i )
       
   761             {
       
   762             delete (*iLoadedServices)[i].iService;
       
   763             }
       
   764         iLoadedServices->Reset();
       
   765         }
       
   766     if ( iMemoryObservers )
       
   767         {
       
   768         iMemoryObservers->Reset();
       
   769         }
       
   770     }
       
   771 
       
   772 TInt CHtiDispatcher::DispatchCallback( TAny* aObj )
       
   773     {
       
   774     return ( reinterpret_cast<CHtiDispatcher*>( aObj ) )->DoDispatch();
       
   775     }
       
   776 
       
   777 void CHtiDispatcher::Reset()
       
   778     {
       
   779     UnloadAllServices();
       
   780     iIncomingQueue->RemoveAll();
       
   781     iOutgoingQueue->RemoveAll();
       
   782     iSecurityManager->ResetSecurityContext();
       
   783 
       
   784     iListener->Reset();
       
   785     iSender->Reset();
       
   786 
       
   787     iListener->ReceiveMessage();
       
   788     }
       
   789 
       
   790 void CHtiDispatcher::HandleSystemMessageL( const TDesC8& aMessage )
       
   791     {
       
   792     HTI_LOG_FUNC_IN( "HandleSystemMessage" );
       
   793 
       
   794     if ( aMessage.Length() > 0 )
       
   795         {
       
   796         HTI_LOG_FORMAT( "cmd %d", aMessage[0] );
       
   797         if ( aMessage[0] == EHtiAuthentication )
       
   798             {
       
   799             //pass token to security manager
       
   800             TPtrC8 token = aMessage.Mid( 1 );//token start at the second byte
       
   801 
       
   802             TDesC8* replyToken = iSecurityManager->SetSecurityContext( token );
       
   803             CleanupStack::PushL( replyToken );
       
   804             //prepare reply message
       
   805             TDesC8* reply = THtiSystemProtocolHelper::AuthMessageL(
       
   806                                 *replyToken );
       
   807 
       
   808             CleanupStack::PushL( reply );
       
   809 
       
   810             User::LeaveIfError( DispatchOutgoingMessage(
       
   811                                     reply,
       
   812                                     KHtiSystemServiceUid ) );
       
   813 
       
   814             CleanupStack::Pop(); //reply
       
   815             CleanupStack::PopAndDestroy(); //replyToken
       
   816             }
       
   817         else if ( iSecurityManager->IsContextEstablashed() )
       
   818             {
       
   819             switch ( aMessage[0] )
       
   820                 {
       
   821                 case EHtiServiceList:
       
   822                     {
       
   823                     TDesC8* list = ServicePluginsListL();
       
   824                     CleanupStack::PushL( list );
       
   825                     User::LeaveIfError( DispatchOutgoingMessage( list,
       
   826                                         KHtiSystemServiceUid ) );
       
   827                     CleanupStack::Pop();
       
   828                     }
       
   829                     break;
       
   830 
       
   831                 case EHtiVersion:
       
   832                     {
       
   833                     HBufC8* msg =  HBufC8::NewLC( 2 );
       
   834                     msg->Des().Append( KHtiVersionMajor );
       
   835                     msg->Des().Append( KHtiVersionMinor );
       
   836                     User::LeaveIfError(
       
   837                         DispatchOutgoingMessage( msg, KHtiSystemServiceUid ) );
       
   838                     CleanupStack::Pop(); // msg
       
   839                     }
       
   840                     break;
       
   841 
       
   842                 case EHtiInstanceId:
       
   843                     {
       
   844                     if ( iHtiInstanceId == 0 )
       
   845                         {
       
   846                         CreateInstanceId();
       
   847                         }
       
   848                     HBufC8* msg = HBufC8::NewLC( sizeof( TUint32 ) );
       
   849                     msg->Des().Append( ( TUint8* ) ( &iHtiInstanceId ), sizeof( TUint32 ) );
       
   850                     User::LeaveIfError(
       
   851                         DispatchOutgoingMessage( msg, KHtiSystemServiceUid ) );
       
   852                     CleanupStack::Pop(); // msg
       
   853                     if ( iConsole )
       
   854                         {
       
   855                         iConsole->Printf( _L( "Instance ID = %u\n" ), iHtiInstanceId );
       
   856                         }
       
   857                     }
       
   858                     break;
       
   859 
       
   860                 case EHtiReboot:
       
   861                     ShutdownAndRebootDeviceL();
       
   862                     break;
       
   863 
       
   864                 case EHtiStop:
       
   865                     {
       
   866                     HTI_LOG_TEXT( "STOP" );
       
   867                     //stop all requests
       
   868                     //cancel just incoming request
       
   869                     //after all outgoing messages sent system will go down
       
   870                     iListener->Cancel();
       
   871 
       
   872                     // kill the watchdog, so HTI stays stopped
       
   873                     KillHtiWatchDogL();
       
   874                     }
       
   875                     break;
       
   876 
       
   877                 case EHtiReset:
       
   878                     {
       
   879                     HTI_LOG_TEXT( "RESET" );
       
   880                     Reset();
       
   881                     }
       
   882                     break;
       
   883 
       
   884                 case EHtiFormat:
       
   885                     {
       
   886                     HTI_LOG_TEXT( "RESET FACTORY SETTINGS" );
       
   887                     if ( aMessage.Length() == 2 )
       
   888                         {
       
   889                         //set the flag to do rfs
       
   890                         iRfsMode = ( TRfsType ) aMessage[1];
       
   891                         }
       
   892                     if ( iRfsMode != ERfsNormal && iRfsMode != ERfsDeep )
       
   893                         {
       
   894                         iRfsMode = ERfsUnknown;
       
   895                         User::LeaveIfError(
       
   896                             DispatchOutgoingErrorMessage( KErrArgument,
       
   897                                           KErrDescrInvalidParameter,
       
   898                                           KHtiSystemServiceUid ) );
       
   899                         }
       
   900                     else
       
   901                         {
       
   902                         RProcess thisProcess;
       
   903                         // ERfsDeep is supported only if HTI running from ROM
       
   904                         if ( iRfsMode == ERfsNormal ||
       
   905                                  IsFileInRom( thisProcess.FileName() ) )
       
   906                             {
       
   907                             //stop
       
   908                             iListener->Cancel();
       
   909                             }
       
   910                         else
       
   911                             {
       
   912                             iRfsMode = ERfsUnknown;
       
   913                             User::LeaveIfError(
       
   914                                 DispatchOutgoingErrorMessage( KErrNotSupported,
       
   915                                               KErrDescrNotInRom,
       
   916                                               KHtiSystemServiceUid ) );
       
   917                             }
       
   918                         }
       
   919                     }
       
   920                     break;
       
   921 
       
   922                 case EHtiShowConsole:
       
   923                     {
       
   924                     HTI_LOG_TEXT( "SHOW CONSOLE" );
       
   925                     if ( !iConsole )
       
   926                         {
       
   927                         iConsole = Console::NewL( _L( "HtiFramework" ),
       
   928                                                   TSize( KConsFullScreen,
       
   929                                                          KConsFullScreen ) );
       
   930                         iConsole->Printf( _L( "HTI Framework\n" ) );
       
   931                         iConsole->Printf( _L( "=============\n\n" ) );
       
   932                         }
       
   933 
       
   934                     HBufC8* msg =  HBufC8::NewLC( 1 );
       
   935                     msg->Des().Append( 0 );
       
   936                     User::LeaveIfError(
       
   937                         DispatchOutgoingMessage( msg, KHtiSystemServiceUid ) );
       
   938                     CleanupStack::Pop(); // msg
       
   939                     }
       
   940                     break;
       
   941 
       
   942                 case EHtiHideConsole:
       
   943                     {
       
   944                     HTI_LOG_TEXT( "HIDE CONSOLE" );
       
   945                     delete iConsole;
       
   946                     iConsole = NULL;
       
   947 
       
   948                     HBufC8* msg =  HBufC8::NewLC( 1 );
       
   949                     msg->Des().Append( 0 );
       
   950                     User::LeaveIfError(
       
   951                         DispatchOutgoingMessage( msg, KHtiSystemServiceUid ) );
       
   952                     CleanupStack::Pop(); // msg
       
   953                     }
       
   954                     break;
       
   955 
       
   956                 case EHtiDebugPrint:
       
   957                     {
       
   958                     if ( aMessage.Length() > 1 )
       
   959                         {
       
   960                         RDebug::RawPrint( aMessage.Mid( 1 ) );
       
   961                         }
       
   962                     HBufC8* msg =  HBufC8::NewLC( 1 );
       
   963                     msg->Des().Append( 0 );
       
   964                     User::LeaveIfError(
       
   965                         DispatchOutgoingMessage( msg, KHtiSystemServiceUid ) );
       
   966                     CleanupStack::Pop(); // msg
       
   967                     }
       
   968                     break;
       
   969 
       
   970                 default:
       
   971                     {
       
   972                     //unknown command
       
   973                     HTI_LOG_TEXT( "Error: Unknown HTI system command:" );
       
   974                     DispatchOutgoingErrorMessage( KErrArgument,
       
   975                         KHtiSystemCmdErrDescr,
       
   976                         KHtiSystemServiceUid );
       
   977                     }
       
   978                 }
       
   979             }
       
   980         else
       
   981             {
       
   982             HTI_LOG_TEXT( "ERROR: not authorized acces" );
       
   983             DispatchOutgoingErrorMessage( EHtiErrNotAuthorized );
       
   984             }
       
   985         }
       
   986     else
       
   987         {
       
   988         HTI_LOG_TEXT( "Error: empty command" );
       
   989         DispatchOutgoingErrorMessage( KErrArgument,
       
   990             KHtiSystemCmdErrDescr,
       
   991             KHtiSystemServiceUid );
       
   992         }
       
   993     HTI_LOG_FUNC_OUT( "HandleSystemMessage" );
       
   994     }
       
   995 
       
   996 void CHtiDispatcher::UrgentReboot( TInt aReason, const TDesC8& aReasonDescr )
       
   997     {
       
   998     HTI_LOG_FORMAT( "UrgentReboot: %d", aReason );
       
   999     HTI_LOG_DES( aReasonDescr );
       
  1000     aReason = aReason;
       
  1001     aReasonDescr.Size();
       
  1002     //empty queues
       
  1003     delete iIncomingQueue;
       
  1004     iIncomingQueue = NULL;
       
  1005     delete iOutgoingQueue;
       
  1006     iOutgoingQueue = NULL;
       
  1007     //stop
       
  1008     iListener->Cancel();
       
  1009     //reboot
       
  1010     Reboot();
       
  1011     }
       
  1012 
       
  1013 void CHtiDispatcher::Reboot()
       
  1014     {
       
  1015     if ( iConsole )
       
  1016         {
       
  1017         iConsole->Printf( _L( "Reboot requested.\n" ) );
       
  1018         }
       
  1019     TInt err = KErrNone;
       
  1020     RProcess rebootProcess;
       
  1021     // First try the UI layer rebooter
       
  1022     err = rebootProcess.Create( KHtiDeviceRebootExeUI, KNullDesC );
       
  1023     if ( err != KErrNone )
       
  1024         {
       
  1025         HTI_LOG_FORMAT( "UI layer rebooter failed with %d", err );
       
  1026         // Try if there is OS layer rebooter present
       
  1027         err = rebootProcess.Create( KHtiDeviceRebootExeOS, KNullDesC );
       
  1028         }
       
  1029     if ( err == KErrNone )
       
  1030         {
       
  1031         rebootProcess.Resume();
       
  1032         rebootProcess.Close();
       
  1033         }
       
  1034     else
       
  1035         {
       
  1036         HTI_LOG_FORMAT( "Reboot err %d", err );
       
  1037         if ( iConsole )
       
  1038             {
       
  1039             iConsole->Printf( _L( "Reboot error %d.\n" ), err );
       
  1040             User::After( 3000000 ); // to let the console display a while
       
  1041             }
       
  1042         // Can't send any error message here - communications have been stopped
       
  1043         }
       
  1044     }
       
  1045 
       
  1046 void CHtiDispatcher::RestoreFactorySettings()
       
  1047     {
       
  1048     HTI_LOG_FUNC_IN( "CHtiDispatcher::RestoreFactorySettings" );
       
  1049     if ( iConsole )
       
  1050         {
       
  1051         iConsole->Printf( _L( "RFS requested, type %d.\n" ), iRfsMode );
       
  1052         }
       
  1053     TInt err = KErrNone;
       
  1054     RProcess rebootProcess;
       
  1055     if ( iRfsMode == ERfsNormal )
       
  1056         {
       
  1057         err = rebootProcess.Create( KHtiDeviceRebootExeUI, KParamNormalRfs );
       
  1058         }
       
  1059     else if ( iRfsMode == ERfsDeep )
       
  1060         {
       
  1061         err = rebootProcess.Create( KHtiDeviceRebootExeUI, KParamDeepRfs );
       
  1062         }
       
  1063 
       
  1064 
       
  1065     if ( err == KErrNone )
       
  1066         {
       
  1067         rebootProcess.Resume();
       
  1068         rebootProcess.Close();
       
  1069         }
       
  1070     else
       
  1071         {
       
  1072         HTI_LOG_FORMAT( "RFS err %d", err );
       
  1073         if ( iConsole )
       
  1074             {
       
  1075             iConsole->Printf( _L( "RFS error %d.\n" ), err );
       
  1076             User::After( 3000000 ); // to let the console display a while
       
  1077             }
       
  1078         // Can't send any error message here - communications have been stopped
       
  1079         }
       
  1080     HTI_LOG_FUNC_OUT( "CHtiDispatcher::RestoreFactorySettings" );
       
  1081     }
       
  1082 
       
  1083 TBool CHtiDispatcher::IsFileInRom( const TDesC& aFileName )
       
  1084     {
       
  1085     HTI_LOG_FUNC_IN( "CHtiDispatcher::IsFileInRom" );
       
  1086     HTI_LOG_DES( aFileName );
       
  1087     TBool isInRom = EFalse;
       
  1088     _LIT( KDriveZ, "z:" );
       
  1089     if ( aFileName.FindF( KDriveZ ) == 0 )
       
  1090         {
       
  1091         isInRom = ETrue;
       
  1092         }
       
  1093 
       
  1094     HTI_LOG_FORMAT( "IsFileInRom returning %d", isInRom );
       
  1095     HTI_LOG_FUNC_OUT( "CHtiDispatcher::IsFileInRom" );
       
  1096     return isInRom;
       
  1097     }
       
  1098 
       
  1099 void CleanupRArray( TAny* object )
       
  1100     {
       
  1101     ( ( RImplInfoPtrArray* ) object )->ResetAndDestroy();
       
  1102     }
       
  1103 
       
  1104 TDesC8* CHtiDispatcher::ServicePluginsListL()
       
  1105     {
       
  1106     RImplInfoPtrArray aImplInfoArray;
       
  1107     CleanupStack::PushL( TCleanupItem( CleanupRArray, &aImplInfoArray ) );
       
  1108 
       
  1109     REComSession::ListImplementationsL(
       
  1110         KHTIServiceInterfaceUid,
       
  1111         aImplInfoArray );
       
  1112 
       
  1113     //alloc memory for the list
       
  1114     TInt maxMemory = aImplInfoArray.Count() * KHtiServiceListRecordLength;
       
  1115     HBufC8* list = HBufC8::NewLC( maxMemory );
       
  1116 
       
  1117     for ( TInt i = 0; i < aImplInfoArray.Count(); ++i )
       
  1118         {
       
  1119         //add uid
       
  1120         TUid uid = MapServicePluginUid(
       
  1121             aImplInfoArray[i]->ImplementationUid() );
       
  1122 
       
  1123         list->Des().Append( ( TUint8* )( &( uid.iUid ) ), 4 );
       
  1124 
       
  1125         //add display name, converted to 8-bit text
       
  1126         TBuf8<KHtiServiceNameLength> serviceName8;
       
  1127         serviceName8.Copy(
       
  1128             aImplInfoArray[i]->DisplayName().Left( KHtiServiceNameLength ) );
       
  1129 
       
  1130         list->Des().Append( serviceName8 );
       
  1131         list->Des().AppendFill( 0,
       
  1132                        KHtiServiceNameLength - serviceName8.Length() );
       
  1133         }
       
  1134 
       
  1135     CleanupStack::Pop();//list
       
  1136     CleanupStack::PopAndDestroy();//aImplInfoArray
       
  1137 
       
  1138     return list;
       
  1139     }
       
  1140 
       
  1141 void CHtiDispatcher::KillHtiWatchDogL()
       
  1142     {
       
  1143     HTI_LOG_FUNC_IN( "CHtiDispatcher::KillHtiWatchDogL" );
       
  1144 
       
  1145     TFullName processName;
       
  1146     TFindProcess finder( KHtiWatchDogMatchPattern );
       
  1147     TInt err = finder.Next( processName );
       
  1148     if ( err == KErrNone )
       
  1149         {
       
  1150         HTI_LOG_TEXT( "HTI watchdog process found. Trying to open and kill it..." );
       
  1151         RProcess prs;
       
  1152         User::LeaveIfError( prs.Open( finder ) );
       
  1153         prs.Kill( 1 );
       
  1154         prs.Close();
       
  1155         HTI_LOG_TEXT( "HTI watchdog killed" );
       
  1156         }
       
  1157 
       
  1158     HTI_LOG_FUNC_OUT( "CHtiDispatcher::KillHtiWatchDogL" );
       
  1159     }
       
  1160 
       
  1161 
       
  1162 #ifdef __ENABLE_LOGGING__
       
  1163 void CHtiDispatcher::DebugListPlugins()
       
  1164     {
       
  1165     HTI_LOG_FUNC_IN( "ListPlugins" );
       
  1166     RImplInfoPtrArray aImplInfoArray;
       
  1167     HTI_LOG_TEXT( "COMM PLUGINS" );
       
  1168     REComSession::ListImplementationsL( KHTICommInterfaceUid, aImplInfoArray );
       
  1169     HTI_LOG_FORMAT( "Num of implementations: %d", aImplInfoArray.Count() );
       
  1170     TInt i;
       
  1171     for ( i = 0; i < aImplInfoArray.Count(); ++i )
       
  1172         {
       
  1173         HTI_LOG_FORMAT( "uid: %d", aImplInfoArray[i]->ImplementationUid().iUid );
       
  1174         HTI_LOG_DES( aImplInfoArray[i]->DataType() );
       
  1175         HTI_LOG_DES( aImplInfoArray[i]->DisplayName() );
       
  1176         }
       
  1177     aImplInfoArray.ResetAndDestroy();
       
  1178 
       
  1179     HTI_LOG_TEXT( "SERVICE PLUGINS" );
       
  1180     REComSession::ListImplementationsL( KHTIServiceInterfaceUid, aImplInfoArray );
       
  1181     HTI_LOG_FORMAT( "Num of implementations: %d", aImplInfoArray.Count() );
       
  1182     for ( i = 0; i < aImplInfoArray.Count(); ++i )
       
  1183         {
       
  1184         HTI_LOG_FORMAT( "uid: %d", aImplInfoArray[i]->ImplementationUid().iUid );
       
  1185         HTI_LOG_DES( aImplInfoArray[i]->DataType() );
       
  1186         HTI_LOG_DES( aImplInfoArray[i]->DisplayName() );
       
  1187 
       
  1188         }
       
  1189     aImplInfoArray.ResetAndDestroy();
       
  1190     HTI_LOG_FUNC_OUT( "ListPlugins" );
       
  1191     }
       
  1192 #endif
       
  1193 
       
  1194 void CHtiDispatcher::ShutdownAndRebootDeviceL()
       
  1195     {
       
  1196     HTI_LOG_TEXT( "REBOOT" );
       
  1197     //stop
       
  1198     iListener->Cancel();
       
  1199     //and set flag to reboot
       
  1200     iToReboot = ETrue;
       
  1201     }
       
  1202 
       
  1203 void CHtiDispatcher::CreateInstanceId()
       
  1204     {
       
  1205     iHtiInstanceId = User::FastCounter();
       
  1206     HTI_LOG_FORMAT( "Generated instance ID %u", iHtiInstanceId );
       
  1207     }
       
  1208 
       
  1209 TUid CHtiDispatcher::MapServicePluginUid( const TUid aUid )
       
  1210     {
       
  1211     TUid mappedUid = TUid::Uid( aUid.iUid );
       
  1212     switch ( aUid.iUid )
       
  1213         {
       
  1214         case 0x10210CCD:
       
  1215             mappedUid.iUid = 0x200212C4;
       
  1216             break;
       
  1217         case 0x10210CCF:
       
  1218             mappedUid.iUid = 0x200212C6;
       
  1219             break;
       
  1220         case 0x10210CD1:
       
  1221             mappedUid.iUid = 0x200212C8;
       
  1222             break;
       
  1223         case 0x10210CD3:
       
  1224             mappedUid.iUid = 0x200212CA;
       
  1225             break;
       
  1226         case 0x200212C4:
       
  1227             mappedUid.iUid = 0x10210CCD;
       
  1228             break;
       
  1229         case 0x200212C6:
       
  1230             mappedUid.iUid = 0x10210CCF;
       
  1231             break;
       
  1232         case 0x200212C8:
       
  1233             mappedUid.iUid = 0x10210CD1;
       
  1234             break;
       
  1235         case 0x200212CA:
       
  1236             mappedUid.iUid = 0x10210CD3;
       
  1237             break;
       
  1238         default:
       
  1239             break;
       
  1240         }
       
  1241     return mappedUid;
       
  1242     }