boottimeintegritycheck/src/IntegrityCheckServer.cpp
changeset 0 164170e6151a
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     1 /*
       
     2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Server source file
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 
       
    21 #include <centralrepository.h>
       
    22 #include <startupreason.h>
       
    23 #include <starterdomaincrkeys.h>
       
    24 
       
    25 #include "IntegrityCheckServer.h"
       
    26 #include "IntegrityCheckDefs.h"
       
    27 #include "IntegrityCheckDebug.h"
       
    28 
       
    29 // MACROS
       
    30 
       
    31 // LOCAL CONSTANTS AND MACROS
       
    32 
       
    33 // MODULE DATA STRUCTURES
       
    34 
       
    35 // LOCAL FUNCTION PROTOTYPES
       
    36 
       
    37 // FORWARD DECLARATIONS
       
    38 
       
    39 
       
    40 // ---------------------------------------------------------------------------
       
    41 // RunServerL
       
    42 //
       
    43 // ---------------------------------------------------------------------------
       
    44 
       
    45 static void RunServerL()
       
    46     {
       
    47     BTIC_TRACE_PRINT("[BTIC Server] RunServerL");
       
    48         
       
    49     User::LeaveIfError( RThread::RenameMe(KIntecrityCheckServerName) );
       
    50     
       
    51     // Create and install the active scheduler
       
    52     CActiveScheduler* scheduler = new ( ELeave ) CActiveScheduler;   
       
    53     CleanupStack::PushL( scheduler );
       
    54     
       
    55     CActiveScheduler::Install( scheduler );
       
    56 
       
    57     // Create server
       
    58     CIntegrityCheckServer::NewLC();
       
    59 
       
    60     // Initialisation complete
       
    61     RProcess::Rendezvous( KErrNone );
       
    62 
       
    63     BTIC_TRACE_PRINT("[BTIC Server] RunServerL: CActiveScheduler::Start");
       
    64     // Start active scheduler
       
    65     CActiveScheduler::Start();
       
    66     
       
    67     CleanupStack::PopAndDestroy( 2 ); // scheduler, CIntegrityCheckServer
       
    68     }
       
    69 
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // E32Main()
       
    73 //
       
    74 // ---------------------------------------------------------------------------
       
    75 //
       
    76 GLDEF_C TInt E32Main()
       
    77     {
       
    78     __UHEAP_MARK;
       
    79     BTIC_TRACE_PRINT("[BTIC Server] E32Main");
       
    80     CTrapCleanup* cleanup = CTrapCleanup::New();
       
    81     
       
    82     if ( cleanup == NULL )
       
    83         {
       
    84         return KErrNoMemory;
       
    85         }
       
    86     
       
    87     TRAPD( err, RunServerL() );
       
    88     
       
    89     __ASSERT_ALWAYS( !err, User::Panic( KIntecrityCheckServerText, err ) );
       
    90     
       
    91     delete cleanup;
       
    92 
       
    93     __UHEAP_MARKEND;
       
    94 
       
    95     return KErrNone;
       
    96     }
       
    97 
       
    98 
       
    99 //======================== CIntegrityCheckServer =============================
       
   100 
       
   101 // ---------------------------------------------------------------------------
       
   102 // CIntegrityCheckServer::CIntegrityCheckServer
       
   103 //
       
   104 // ---------------------------------------------------------------------------
       
   105 //
       
   106 CIntegrityCheckServer::CIntegrityCheckServer()
       
   107     :CPolicyServer( 0, CPPolicy, ESharableSessions )
       
   108     {
       
   109     }
       
   110 
       
   111 
       
   112 // ---------------------------------------------------------------------------
       
   113 // CIntegrityCheckServer::NewLC
       
   114 //
       
   115 // ---------------------------------------------------------------------------
       
   116 //
       
   117 CServer2* CIntegrityCheckServer::NewLC()
       
   118     {
       
   119     CIntegrityCheckServer* self = new ( ELeave ) CIntegrityCheckServer;
       
   120     CleanupStack::PushL( self );
       
   121     
       
   122     self->ConstructL();
       
   123     
       
   124     return self;
       
   125     }
       
   126 
       
   127 
       
   128 // ---------------------------------------------------------------------------
       
   129 // CIntegrityCheckServer::ConstructL
       
   130 //
       
   131 // ---------------------------------------------------------------------------
       
   132 //
       
   133 void CIntegrityCheckServer::ConstructL()
       
   134     {
       
   135     BTIC_TRACE_PRINT("[BTIC Server] ConstructL");
       
   136     iSessionCount = 0;
       
   137     StartL( KIntecrityCheckServer );    
       
   138     iShutDown.ConstructL();
       
   139     BTIC_TRACE_PRINT("[BTIC Server] ConstructL: Start shutdown timer");
       
   140     iShutDown.Start();    
       
   141     }
       
   142 
       
   143 
       
   144 // ---------------------------------------------------------------------------
       
   145 // CIntegrityCheckServer::NewSessionL
       
   146 //
       
   147 // ---------------------------------------------------------------------------
       
   148 //
       
   149 CSession2* CIntegrityCheckServer::NewSessionL(
       
   150     const TVersion&,
       
   151     const RMessage2&) const
       
   152     {
       
   153     BTIC_TRACE_PRINT("[BTIC Server] NewSessionL");
       
   154     return new (ELeave) CIntegrityCheckSession();
       
   155     }
       
   156 
       
   157 // -----------------------------------------------------------------------------
       
   158 // CIntegrityCheckServer::AddSession()
       
   159 // Cancel the shutdown timer now, the new session is connected
       
   160 //
       
   161 // -----------------------------------------------------------------------------
       
   162 //
       
   163 void CIntegrityCheckServer::AddSession()
       
   164     {
       
   165     BTIC_TRACE_PRINT("[BTIC Server] AddSession");
       
   166     iSessionCount++;
       
   167     BTIC_TRACE_PRINT("[BTIC Server] Cancel shutdown timer");
       
   168     iShutDown.Cancel();  // Cancel any outstanding shutdown timer    
       
   169     }
       
   170  
       
   171 // -----------------------------------------------------------------------------
       
   172 // CIntegrityCheckServer::RemoveSession
       
   173 // Decrement the session counter and start the shutdown timer if the last client
       
   174 // has disconnected
       
   175 // -----------------------------------------------------------------------------
       
   176 //
       
   177 void CIntegrityCheckServer::RemoveSession()
       
   178     {
       
   179     BTIC_TRACE_PRINT("[BTIC Server] RemoveSession");
       
   180     BTIC_TRACE_PRINT_NUM("[BTIC Server] Session count = %d", iSessionCount );
       
   181     if ( --iSessionCount == 0 )
       
   182         {      
       
   183         BTIC_TRACE_PRINT("[BTIC Server] Remove session and shutdown ");
       
   184         iShutDown.Start();
       
   185         }
       
   186     }
       
   187 
       
   188 
       
   189 //======================== CIntegrityCheckSession ============================
       
   190 
       
   191 // ---------------------------------------------------------------------------
       
   192 // CIntegrityCheckSession::CIntegrityCheckSession
       
   193 //
       
   194 // ---------------------------------------------------------------------------
       
   195 //
       
   196 CIntegrityCheckSession::CIntegrityCheckSession()
       
   197     {    
       
   198     }
       
   199 
       
   200 
       
   201 // ---------------------------------------------------------------------------
       
   202 // CIntegrityCheckSession::CreateL
       
   203 // Called by the CServer2
       
   204 // ---------------------------------------------------------------------------
       
   205 //
       
   206 void CIntegrityCheckSession::CreateL()
       
   207     {
       
   208     BTIC_TRACE_PRINT("[BTIC Srv session] CIntegrityCheckSession");
       
   209     
       
   210     CIntegrityCheckServer* server( static_cast<CIntegrityCheckServer*>
       
   211             ( const_cast<CServer2*>( CSession2::Server() ) ) );    
       
   212     // Ok we have client so let's cancel shutdown timer.
       
   213     server->AddSession();    
       
   214     }
       
   215 
       
   216 
       
   217 // ---------------------------------------------------------------------------
       
   218 // CIntegrityCheckSession::~CIntegrityCheckSession
       
   219 //
       
   220 // ---------------------------------------------------------------------------
       
   221 //
       
   222 CIntegrityCheckSession::~CIntegrityCheckSession()
       
   223     {
       
   224     BTIC_TRACE_PRINT("[BTIC Srv session] ~CIntegrityCheckSession");
       
   225     
       
   226     CIntegrityCheckServer* server( static_cast<CIntegrityCheckServer*>
       
   227             ( const_cast<CServer2*>( CSession2::Server() ) ) );
       
   228     
       
   229     server->RemoveSession();
       
   230     }
       
   231 
       
   232 
       
   233 // ---------------------------------------------------------------------------
       
   234 // CIntegrityCheckSession::Disconnect
       
   235 //
       
   236 // ---------------------------------------------------------------------------
       
   237 //
       
   238 void CIntegrityCheckSession::Disconnect( const RMessage2 &aMessage )
       
   239     {
       
   240     BTIC_TRACE_PRINT("[BTIC Srv session] Disconnect");    
       
   241     CSession2::Disconnect( aMessage );
       
   242     }
       
   243 
       
   244 
       
   245 // ---------------------------------------------------------------------------
       
   246 // CIntegrityCheckSession::ServiceL
       
   247 //
       
   248 // ---------------------------------------------------------------------------
       
   249 //
       
   250 void CIntegrityCheckSession::ServiceL(const RMessage2& aMessage)
       
   251     {
       
   252     BTIC_TRACE_PRINT("[BTIC Srv session] ServiceL");
       
   253     
       
   254     switch ( aMessage.Function() )
       
   255         {
       
   256         case EBTICBootReasonSW:
       
   257             {            
       
   258             TRAPD( err, GetSWBootReasonL( aMessage ) );
       
   259             
       
   260             aMessage.Complete( err );
       
   261             break;
       
   262             }
       
   263         default:      
       
   264             aMessage.Complete( KErrGeneral );
       
   265             break;
       
   266         }
       
   267     }
       
   268 
       
   269 
       
   270 // ---------------------------------------------------------------------------
       
   271 // CIntegrityCheckSession::ServiceError
       
   272 //
       
   273 // ---------------------------------------------------------------------------
       
   274 //
       
   275 void CIntegrityCheckSession::ServiceError( 
       
   276     const RMessage2& aMessage,
       
   277     TInt aError )
       
   278     {
       
   279     BTIC_TRACE_PRINT("[BTIC Srv session] ServiceError");
       
   280     CSession2::ServiceError( aMessage, aError );
       
   281     }
       
   282 
       
   283 
       
   284 // ---------------------------------------------------------------------------
       
   285 // CIntegrityCheckSession::GetSWBootReasonL
       
   286 //
       
   287 // ---------------------------------------------------------------------------
       
   288 //
       
   289 void CIntegrityCheckSession::GetSWBootReasonL( const RMessage2& aMessage )
       
   290     {  
       
   291     BTIC_TRACE_PRINT("[BTIC Srv session] GetSWBootReasonL");  
       
   292     // Get sw boot reason from system.   
       
   293     TInt bootReason = KErrNone;     
       
   294     TInt startupReason = KErrNone;
       
   295     CRepository* repository( NULL );
       
   296     BTIC_TRACE_PRINT("[BTIC Srv session] Get startup reason from CRepository");
       
   297         
       
   298     TInt err = KErrNone;
       
   299             
       
   300     TRAP( err, repository = CRepository::NewL( KCRUidStartup ) );
       
   301     
       
   302     BTIC_TRACE_PRINT_NUM("[BTIC Srv session] CRepository TRAPD Err = %d", err );
       
   303 	
       
   304     if ( err == KErrNone )
       
   305         {
       
   306         CleanupStack::PushL( repository );
       
   307                                
       
   308         err = repository->Get( KStartupReason, startupReason );        
       
   309         
       
   310         BTIC_TRACE_PRINT_NUM("[BTIC Srv session] CRepository Get Err = %d", err );
       
   311         
       
   312 	    if ( err == KErrNone )
       
   313 		   {
       
   314 		   BTIC_TRACE_PRINT_NUM("Returned startup reason: %d ",startupReason );
       
   315 		    
       
   316             if ( startupReason == EDeepRFSReset )
       
   317 			    {
       
   318 			     // C drive will be formatted, so let's stop checking.
       
   319 			     bootReason = EBTICRestoreFactorySetDeep;
       
   320 			     BTIC_TRACE_PRINT("[BTIC Srv session] Startup reason is FORMAT");
       
   321 			     }
       
   322             else
       
   323                 {
       
   324                 // All other reasons are "normal" boot for BTIC.
       
   325                 bootReason = EBTICNormalBoot;
       
   326                 BTIC_TRACE_PRINT("[BTIC Srv session] Startup reason is NORMAL");
       
   327                 }    			    
       
   328             }
       
   329         else if ( err == KErrNotFound )
       
   330             {
       
   331             // Reason not found. Propably first boot etc.
       
   332             bootReason = EBTICNoSWReason;
       
   333             BTIC_TRACE_PRINT("[BTIC Srv session] No SW Startup reason found");            
       
   334             }
       
   335         else
       
   336             {
       
   337             // Some other error.
       
   338             bootReason = err;
       
   339             }                
       
   340                 
       
   341 	    CleanupStack::PopAndDestroy(); //repository    
       
   342 	    }
       
   343         
       
   344     TPckg<TInt> bootReasonValue( bootReason );
       
   345     
       
   346     BTIC_TRACE_PRINT("[BTIC Srv session] Write result to client side.");
       
   347     // Write boot reason to client side.
       
   348     aMessage.WriteL( KParam0, bootReasonValue );      
       
   349     }
       
   350  
       
   351 
       
   352 //============================== CShutdown =====================================
       
   353 
       
   354 // -----------------------------------------------------------------------------
       
   355 // CShutdown::CShutdown
       
   356 // 
       
   357 //
       
   358 // -----------------------------------------------------------------------------
       
   359 //
       
   360 CShutdown::CShutdown():CTimer(-1)
       
   361     {
       
   362     BTIC_TRACE_PRINT("[BTIC Shutdown] CShutdown: Add scheduler");
       
   363     CActiveScheduler::Add(this);
       
   364     }
       
   365 
       
   366 // -----------------------------------------------------------------------------
       
   367 // CShutdown::ConstructL
       
   368 // 
       
   369 //
       
   370 // -----------------------------------------------------------------------------
       
   371 //
       
   372 void CShutdown::ConstructL()
       
   373     {
       
   374     BTIC_TRACE_PRINT("[BTIC Shutdown] ConstructL");
       
   375     CTimer::ConstructL();
       
   376     }
       
   377 
       
   378 // -----------------------------------------------------------------------------
       
   379 // CShutdown::Start
       
   380 // Set shutdown timer for server.
       
   381 //
       
   382 // -----------------------------------------------------------------------------
       
   383 //
       
   384 void CShutdown::Start()
       
   385     {
       
   386     BTIC_TRACE_PRINT("[BTIC Shutdown] Start timer");
       
   387     After( KServerCloseTime );
       
   388     }
       
   389 
       
   390 // -----------------------------------------------------------------------------
       
   391 // CShutdown::RunServerL
       
   392 // Initiates server exit when the timer expires
       
   393 //
       
   394 // -----------------------------------------------------------------------------
       
   395 //
       
   396 void CShutdown::RunL()
       
   397     {
       
   398     BTIC_TRACE_PRINT("[BTIC Shutdown] RunL: Stop BTIC server");
       
   399     CActiveScheduler::Stop();
       
   400     }
       
   401 
       
   402 
       
   403 //EOF