hti/HtiWatchDog/src/HtiWatchDog.cpp
changeset 36 813b186005b6
parent 30 86a2e675b80a
child 41 838cdffd57ce
equal deleted inserted replaced
30:86a2e675b80a 36:813b186005b6
     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:  HtiWatchDog implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <e32base.h>
       
    21 
       
    22 #ifdef __ENABLE_LOGGING__
       
    23 
       
    24 #include <flogger.h>
       
    25 _LIT( KLogFolder, "hti" );
       
    26 _LIT( KLogFile,   "htiwatchdog.txt" );
       
    27 
       
    28 #define HTI_LOG_TEXT(a1) {_LIT(temp, a1); RFileLogger::Write(KLogFolder, KLogFile, EFileLoggingModeAppend, temp);}
       
    29 #define HTI_LOG_DES(a1) {RFileLogger::Write(KLogFolder, KLogFile, EFileLoggingModeAppend, a1);}
       
    30 #define HTI_LOG_FORMAT(a1,a2) {_LIT(temp, a1); RFileLogger::WriteFormat(KLogFolder, KLogFile, EFileLoggingModeAppend, temp, (a2));}
       
    31 
       
    32 #else   // !__ENABLE_LOGGING__
       
    33 
       
    34 #define HTI_LOG_TEXT(a1)
       
    35 #define HTI_LOG_DES(a1)
       
    36 #define HTI_LOG_FORMAT(a1,a2)
       
    37 
       
    38 #endif // __ENABLE_LOGGING__
       
    39 
       
    40 // CONSTANTS
       
    41 _LIT( KHtiFrameworkExeName,    "HtiFramework.exe" );
       
    42 _LIT( KHtiMainThreadName,      "HtiMain" );
       
    43 _LIT( KHtiWatchDogName,        "HtiWatchDog" );
       
    44 _LIT( KHtiAdminStartParameter, "admin" );
       
    45 
       
    46 const TTimeIntervalSeconds minInterval( 60 );
       
    47 
       
    48 // MACROS
       
    49 
       
    50 // LOCAL CONSTANTS AND MACROS
       
    51 
       
    52 // MODULE DATA STRUCTURES
       
    53 
       
    54 // LOCAL FUNCTION PROTOTYPES
       
    55 
       
    56 // FORWARD DECLARATIONS
       
    57 
       
    58 // ============================ LOCAL FUNCTIONS ===============================
       
    59 
       
    60 LOCAL_C void StartHtiProcessL()
       
    61     {
       
    62     RProcess htiProcess;
       
    63     User::LeaveIfError( htiProcess.Create(
       
    64             KHtiFrameworkExeName, KHtiAdminStartParameter ) );
       
    65     htiProcess.Resume();
       
    66     htiProcess.Close();
       
    67     }
       
    68 
       
    69 
       
    70 LOCAL_C TInt StartL()
       
    71     {
       
    72     HTI_LOG_TEXT( "HtiWatchDog starting..." );
       
    73     TFullName threadName;
       
    74     TFullName matchPattern;
       
    75     matchPattern.Append( _L( "*" ) );
       
    76     matchPattern.Append( KHtiMainThreadName );
       
    77     matchPattern.Append( _L( "*" ) );
       
    78 
       
    79     TTime startTime;
       
    80     startTime.HomeTime();
       
    81 
       
    82     TTimeIntervalSeconds elapsedTime;
       
    83     TTime now;
       
    84 
       
    85     while ( true )
       
    86         {
       
    87         // Use thread finder to find the HTI main thread
       
    88         TFindThread threadFinder;
       
    89         threadFinder.Find( matchPattern );
       
    90         HTI_LOG_TEXT( "Trying to find HTI main thread" );
       
    91         TInt err =  threadFinder.Next( threadName );
       
    92         if ( err )
       
    93             {
       
    94             HTI_LOG_FORMAT( "HTI main thread not found, err: %d", err );
       
    95             User::Panic( _L( "HTI find err" ), err );
       
    96             }
       
    97 
       
    98         HTI_LOG_TEXT( "HTI main thread found, opening it" );
       
    99         RThread thread;
       
   100         err = thread.Open( threadName );
       
   101         if ( err )
       
   102             {
       
   103             HTI_LOG_FORMAT( "Could not open HTI main thread, err: %d", err );
       
   104             User::Panic( _L( "HTI open err" ), err );
       
   105             }
       
   106 
       
   107         // Logon to HTI main thread and wait for its death
       
   108         HTI_LOG_TEXT( "HTI main thread opened, waiting for its death" );
       
   109         TRequestStatus status;
       
   110         thread.Logon( status );
       
   111         User::WaitForRequest( status );
       
   112         thread.Close();
       
   113 
       
   114         // try to restart only if minimum interval time has elapsed
       
   115         now.HomeTime();
       
   116         now.SecondsFrom( startTime, elapsedTime );
       
   117         if ( elapsedTime < minInterval )
       
   118             {
       
   119             HTI_LOG_FORMAT( "HTI died too quickly, in %d seconds", elapsedTime.Int() );
       
   120             HTI_LOG_TEXT( "Giving up" );
       
   121             break;
       
   122             }
       
   123 
       
   124         // reset the startTime
       
   125         startTime.HomeTime();
       
   126 
       
   127         // try to restart HTI
       
   128         HTI_LOG_TEXT( "HTI died, trying to restart it" );
       
   129         TRAP( err, StartHtiProcessL() );
       
   130         if ( err )
       
   131             {
       
   132             HTI_LOG_FORMAT( "Could not restart HTI, err: %d", err );
       
   133             User::Panic( _L( "HTI start err" ), err );
       
   134             }
       
   135 
       
   136         // wait a while for HTI process to start
       
   137         User::After( 5 * 100 * 1000 );
       
   138         }
       
   139 
       
   140     HTI_LOG_TEXT( "HtiWatchDog shutting down" );
       
   141     return KErrNone;
       
   142     }
       
   143 
       
   144 GLDEF_C TInt E32Main()
       
   145     {
       
   146     __UHEAP_MARK;
       
   147 
       
   148     CTrapCleanup* cleanup = CTrapCleanup::New();
       
   149     CActiveScheduler *scheduler = new(ELeave) CActiveScheduler;
       
   150     CActiveScheduler::Install( scheduler );
       
   151 
       
   152     User::RenameThread( KHtiWatchDogName );
       
   153 
       
   154     TRAPD( err, StartL() );
       
   155 
       
   156     delete scheduler;
       
   157     delete cleanup;
       
   158 
       
   159    __UHEAP_MARKEND;
       
   160 
       
   161     return err;
       
   162     }
       
   163 
       
   164 
       
   165 // End of File