hti/HtiRestart/src/HtiRestart.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:  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,   "htirestart.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( KHtiRestartName,        "HtiRestart" );
       
    44 _LIT( KHtiAdminStartParameter, "admin" );
       
    45 
       
    46 
       
    47 // MACROS
       
    48 
       
    49 // LOCAL CONSTANTS AND MACROS
       
    50 
       
    51 // MODULE DATA STRUCTURES
       
    52 
       
    53 // LOCAL FUNCTION PROTOTYPES
       
    54 
       
    55 // FORWARD DECLARATIONS
       
    56 
       
    57 // ============================ LOCAL FUNCTIONS ===============================
       
    58 
       
    59 LOCAL_C void StartHtiProcessL()
       
    60     {
       
    61     RProcess htiProcess;
       
    62     User::LeaveIfError( htiProcess.Create(
       
    63             KHtiFrameworkExeName, KHtiAdminStartParameter ) );
       
    64     htiProcess.Resume();
       
    65     htiProcess.Close();
       
    66     }
       
    67 
       
    68 
       
    69 LOCAL_C TInt StartL()
       
    70     {
       
    71     HTI_LOG_TEXT( "HtiRestart starting..." );
       
    72     TFullName threadName;
       
    73     TFullName matchPattern;
       
    74     matchPattern.Append(_L( "*" ));
       
    75     matchPattern.Append(KHtiMainThreadName);
       
    76     matchPattern.Append(_L( "*" ));
       
    77 
       
    78     // Use thread finder to find the HTI main thread
       
    79     TFindThread threadFinder;
       
    80     threadFinder.Find(matchPattern);
       
    81     HTI_LOG_TEXT( "Trying to find HTI main thread" );
       
    82     TInt err = threadFinder.Next(threadName);
       
    83 
       
    84     if (err == KErrNone)
       
    85         {
       
    86         HTI_LOG_TEXT( "HTI main thread found, opening it" );
       
    87         RThread thread;
       
    88         err = thread.Open(threadName);
       
    89         if (err)
       
    90             {
       
    91             HTI_LOG_FORMAT( "Could not open HTI main thread, err: %d", err );
       
    92             User::Panic(_L( "HTI open err" ), err);
       
    93             }
       
    94 
       
    95         // Logon to HTI main thread and wait for its death
       
    96         HTI_LOG_TEXT( "HTI main thread opened, waiting for its death" );
       
    97         TRequestStatus status;
       
    98         thread.Logon(status);
       
    99         User::WaitForRequest(status);
       
   100         thread.Close();
       
   101 
       
   102         HTI_LOG_TEXT( "HTI died");
       
   103         }
       
   104 
       
   105     TBuf<0x20> cmd;
       
   106     User::CommandLine(cmd);
       
   107 
       
   108     TLex lex(cmd);
       
   109     TInt microseconds = 0;
       
   110     lex.Val(microseconds);
       
   111     HTI_LOG_FORMAT("After %d milliseconds...", microseconds);
       
   112     User::After(microseconds);
       
   113 
       
   114     // try to restart HTI
       
   115     HTI_LOG_TEXT( "Trying to restart it" );
       
   116     TRAP( err, StartHtiProcessL() );
       
   117     if (err)
       
   118         {
       
   119         HTI_LOG_FORMAT( "Could not restart HTI, err: %d", err );
       
   120         User::Panic(_L( "HTI start err" ), err);
       
   121         }
       
   122 
       
   123     HTI_LOG_TEXT( "HtiRestart shutting down" );
       
   124     return KErrNone;
       
   125     }
       
   126 
       
   127 GLDEF_C TInt E32Main()
       
   128     {
       
   129     __UHEAP_MARK;
       
   130 
       
   131     CTrapCleanup* cleanup = CTrapCleanup::New();
       
   132     CActiveScheduler *scheduler = new(ELeave) CActiveScheduler;
       
   133     CActiveScheduler::Install( scheduler );
       
   134 
       
   135     User::RenameThread( KHtiRestartName );
       
   136 
       
   137     TRAPD( err, StartL() );
       
   138 
       
   139     delete scheduler;
       
   140     delete cleanup;
       
   141 
       
   142    __UHEAP_MARKEND;
       
   143 
       
   144     return err;
       
   145     }
       
   146 
       
   147 
       
   148 // End of File