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