sysstatemgmt/systemstarter/src/DllStarter.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "DllStarter.h"
       
    17 #include <f32file.h>
       
    18 #include "SystemStartupDllInfo.h"
       
    19 #include "SysStartDebug.h"
       
    20 #include "SysStartDefs.h"
       
    21 
       
    22 #include "sysstartpanic.h"
       
    23 #include "restartsys.h"
       
    24 
       
    25 
       
    26 //
       
    27 // Standard Symbian factory functions/destructor
       
    28 //
       
    29  
       
    30 CDllStarter* CDllStarter::NewL(const CSystemStartupDllInfo* aDllInfo )
       
    31 	{
       
    32 	CDllStarter* self = CDllStarter::NewLC(aDllInfo );
       
    33 	CleanupStack::Pop(self);
       
    34 	return self;
       
    35 	}
       
    36 
       
    37 CDllStarter* CDllStarter::NewLC(const CSystemStartupDllInfo* aDllInfo )
       
    38 	{
       
    39 	CDllStarter* self = new (ELeave) CDllStarter(aDllInfo );
       
    40 	CleanupStack::PushL(self);
       
    41 	return self;
       
    42 	}
       
    43 	
       
    44 CDllStarter::~CDllStarter()
       
    45 	{
       
    46 	delete iDllInfo;
       
    47 	}
       
    48  	
       
    49 CDllStarter::CDllStarter(const CSystemStartupDllInfo* aDllInfo ):
       
    50 	iDllInfo(aDllInfo)  
       
    51 	{
       
    52 	}
       
    53  	
       
    54 	
       
    55 //
       
    56 // Public member functions
       
    57 //
       
    58 
       
    59 /** Implementation of MStartupCommand interface.
       
    60 This function retrieves the DLL name and ordinal number from its 
       
    61 CSystemStartupDllInfo data member. It attempts to load the DLL 
       
    62 and call the specified function. 
       
    63 
       
    64 @see MStartupCommand.
       
    65 */
       
    66 void CDllStarter::Execute(TRequestStatus& aStatus)
       
    67 	{
       
    68 	aStatus = KRequestPending; 
       
    69 	DEBUGPRINT1(_L("SysStart: CDllStarter::Execute"));
       
    70 	
       
    71 	// Find the name of the DLL to be loaded from the information 
       
    72 	// already retrieved from the SSC file.
       
    73 	TFileName dllName =iDllInfo->DllName();
       
    74 	_LIT(KExtension, ".dll");
       
    75 	dllName.Append(KExtension);
       
    76     
       
    77     // Initialise variables
       
    78     _LIT(KSysBin, "z:\\sys\\bin\\");
       
    79 	RLibrary lib;
       
    80 	TInt err = KErrGeneral; 
       
    81 	TBool dllLoaded = EFalse; 
       
    82 	
       
    83 	TUint8 ordinal = iDllInfo->Ordinal();
       
    84  
       
    85 	// The DLL may not be found or the DLL function may return an error 
       
    86 	// code. The number of times to re-attempt is specified in the SSC file.
       
    87 	TInt attemptsRequired = iDllInfo->NoOfRetries() + 1; 
       
    88   
       
    89  	while ((err != KErrNone)   && (attemptsRequired-- >0))
       
    90 	 	{
       
    91 		// Search for and load the DLL. 	 
       
    92        	if ((err = lib.Load(dllName, KSysBin)) == KErrNone)
       
    93  			{ 
       
    94  			DEBUGPRINT2(_L("SysStart: dll loaded - %S"), &dllName);
       
    95  			dllLoaded = ETrue;		 
       
    96  			}
       
    97  		else
       
    98 			{
       
    99 			DEBUGPRINT3(_L("SysStart: Unable to load DLL - %S, error - %d"), &dllName, err);
       
   100 			}
       
   101 			
       
   102   		// Call the function in the Dll
       
   103   		if (dllLoaded)
       
   104   			{	
       
   105   			// Identify the function to be called according to the ordinal 
       
   106  			// number specified in the SSC file.
       
   107  			TLibraryFunction ordinalfunction = lib.Lookup(ordinal);
       
   108  				
       
   109  			if (ordinalfunction != NULL)
       
   110  				{	
       
   111  				Dllfunctiontype getDllFunction = reinterpret_cast<Dllfunctiontype>(ordinalfunction);
       
   112   		 		DEBUGPRINT2(_L("SysStart: Calling function at ordinal %d."), ordinal);
       
   113   		 		err = (*getDllFunction)(iDllInfo->DllBuffer());
       
   114  				}
       
   115  			else
       
   116  				{
       
   117  				DEBUGPRINT2(_L("Sysstart: No function at specified ordinal %d"), ordinal);
       
   118  				err = KErrNotFound;
       
   119  				// No function exists at the ordinal specified so 
       
   120  				// there is no point in re-attempting execution.
       
   121  				attemptsRequired = 0;
       
   122  				}
       
   123   			}
       
   124 	 	}
       
   125  	
       
   126 				 	
       
   127 	// Take action on error condition
       
   128 	if (err != KErrNone) 
       
   129 		{	
       
   130  		// Check required behaviour on failure specified in SSC
       
   131  		if (iDllInfo->FailOnError())
       
   132  			{ 
       
   133 			err = RestartSys::RestartSystem();
       
   134 			if (KErrNone != err)
       
   135 				{
       
   136 				DEBUGPRINT2(_L("Sysstart: Restart System Call failed with error %d"), err);
       
   137  				PanicNow(KPanicDllStarter, ERestartSystemCallFailed);
       
   138 				}
       
   139 			User::After(5000000); // required by RestartSys API, see comments in RestartSys::RestartSystem()
       
   140  			}			 
       
   141 		}
       
   142  
       
   143  	lib.Close();
       
   144   
       
   145 	TRequestStatus* requestStatus = &aStatus;
       
   146 	User::RequestComplete(requestStatus, err);
       
   147 	}
       
   148 
       
   149 void CDllStarter::Release() 
       
   150 	{
       
   151 	delete this;
       
   152 	}