dbgagents/trkagent/toolsstarter/toolsstarterserver/autolaunch/src/toolsstarterautolaunch.cpp
changeset 0 c6b0df440bee
equal deleted inserted replaced
-1:000000000000 0:c6b0df440bee
       
     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: 
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include <apmrec.h>
       
    20 #include <ecom/implementationproxy.h>
       
    21 #include <e32debug.h>
       
    22 #include <startupdomainpskeys.h>
       
    23 
       
    24 #include "toolsstarterautolaunch.h"
       
    25 #include "logging.h"
       
    26 
       
    27 #define KPSDelay 500000
       
    28 
       
    29 
       
    30 // CONSTANTS
       
    31 const static TUint KToolsStarterAutoLaunchImplUid = 0x200170B9;
       
    32 const static TUid KToolsStarterAutoLaunchUid = {0x200170B8};
       
    33 _LIT(KToolsStarterServerExe, "toolsstarterserver.exe");
       
    34 const TUid KToolsStarterServerExeUid = {0x200170B5};
       
    35 _LIT( KToolsStarterMatchPattern, "toolsstarterserver**" );
       
    36 
       
    37 const TImplementationProxy ImplementationTable[] =
       
    38     {
       
    39     IMPLEMENTATION_PROXY_ENTRY( KToolsStarterAutoLaunchImplUid,
       
    40     		CToolsStarterAutoLaunch::CreateRecognizerL )
       
    41     };
       
    42 
       
    43 
       
    44 // ============================ MEMBER FUNCTIONS ===============================
       
    45 
       
    46 EXPORT_C const TImplementationProxy* ImplementationGroupProxy( TInt& aTableCount )
       
    47     {
       
    48     aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy);
       
    49     return ImplementationTable;
       
    50     }
       
    51 
       
    52 
       
    53 CApaDataRecognizerType* CToolsStarterAutoLaunch::CreateRecognizerL()
       
    54     {
       
    55 	LOG_MSG("CToolsStarterAutoLaunch::CreateRecognizerL");
       
    56 
       
    57 	CApaDataRecognizerType* recognizer = new (ELeave) CToolsStarterAutoLaunch();
       
    58 	CToolsStarterAutoLaunch::CreateAutoStartThreadL();
       
    59     return recognizer;
       
    60     }
       
    61 
       
    62 
       
    63 CToolsStarterAutoLaunch::CToolsStarterAutoLaunch() :
       
    64     CApaDataRecognizerType( KToolsStarterAutoLaunchUid, CApaDataRecognizerType::ENormal )
       
    65     {
       
    66     iCountDataTypes = 1;
       
    67     }
       
    68 
       
    69 
       
    70 
       
    71 TUint CToolsStarterAutoLaunch::PreferredBufSize()
       
    72     {
       
    73     return 0;
       
    74     }
       
    75 
       
    76 
       
    77 TDataType CToolsStarterAutoLaunch::SupportedDataTypeL( TInt /*aIndex*/ ) const
       
    78     {
       
    79     return TDataType();
       
    80     }
       
    81 
       
    82 
       
    83 void CToolsStarterAutoLaunch::DoRecognizeL( const TDesC& /*aName*/, const TDesC8& /*aBuffer*/ )
       
    84     {
       
    85     }
       
    86 
       
    87 
       
    88 void CToolsStarterAutoLaunch::CreateAutoStartThreadL()
       
    89     {
       
    90     LOG_MSG("CToolsStarterAutoLaunch::CreateAutoStartThread");
       
    91     
       
    92     //create a new thread for starting our application
       
    93     RThread startAppThread;
       
    94 
       
    95     User::LeaveIfError( startAppThread.Create(
       
    96         _L( "ToolsStarterAutoLaunch" ),
       
    97         CToolsStarterAutoLaunch::StartAppThreadFunction,
       
    98         KDefaultStackSize,
       
    99         KMinHeapSize,
       
   100         KMinHeapSize,
       
   101         NULL,
       
   102         EOwnerThread ) );
       
   103 
       
   104     startAppThread.SetPriority( EPriorityNormal );
       
   105     startAppThread.Resume();
       
   106     startAppThread.Close();
       
   107     }
       
   108 
       
   109 
       
   110 TInt CToolsStarterAutoLaunch::StartAppThreadFunction( TAny* /*aParam*/ )
       
   111     {
       
   112     LOG_MSG("CToolsStarterAutoLaunch::StartAppThreadFunction");
       
   113     
       
   114      // create a trap cleanup and active scheduler
       
   115     CTrapCleanup* pC = CTrapCleanup::New();
       
   116     
       
   117     //CActiveScheduler* pS = new CActiveScheduler;
       
   118     //CActiveScheduler::Install(pS);
       
   119     
       
   120     if (pC==NULL)// || pS==NULL)
       
   121         LOG_MSG("Failed to create the trapcleanup and active scheduler");
       
   122         
       
   123             // create a server starter instance
       
   124     CToolsStarterLauncher* serverStarter = NULL;
       
   125     TRAPD(err, serverStarter = CToolsStarterLauncher::NewL());
       
   126     
       
   127     if (err == KErrNone)
       
   128     	{
       
   129 	    // wait for UI to be up
       
   130 	   // serverStarter->WaitForUiServices();
       
   131 		// now start the toolsstarter server
       
   132 		serverStarter->StartServer();
       
   133 		delete serverStarter;	    
       
   134 	   	}    
       
   135 
       
   136   	delete pC;
       
   137   	//delete pS;
       
   138   	
       
   139     return KErrNone;
       
   140     }
       
   141 
       
   142 
       
   143 CToolsStarterLauncher* CToolsStarterLauncher::NewL()
       
   144     {
       
   145     CToolsStarterLauncher* self = new(ELeave) CToolsStarterLauncher();
       
   146     CleanupStack::PushL(self);
       
   147     self->ConstructL();
       
   148     CleanupStack::Pop();
       
   149     return self;
       
   150     }
       
   151 
       
   152 
       
   153 
       
   154 CToolsStarterLauncher::CToolsStarterLauncher()
       
   155     {
       
   156     }
       
   157 
       
   158 
       
   159     
       
   160 CToolsStarterLauncher::~CToolsStarterLauncher()
       
   161     {
       
   162 
       
   163     }
       
   164 
       
   165 
       
   166  
       
   167 void CToolsStarterLauncher::ConstructL()
       
   168     {
       
   169     }
       
   170 
       
   171 void CToolsStarterLauncher::StartServer()
       
   172     {
       
   173     LOG_MSG("CToolsStarterLauncher::StartServer");
       
   174     
       
   175     // check if toolsstarter is already running
       
   176     TFullName processName;
       
   177     TFindProcess finder( KToolsStarterMatchPattern );
       
   178     TInt err = finder.Next( processName );
       
   179     if ( err == KErrNone )
       
   180         {
       
   181         LOG_MSG("KToolsStarterMatchPattern server already running");
       
   182         return;
       
   183         }
       
   184 
       
   185     // otherwise create a new process
       
   186     RProcess pr;
       
   187     err = pr.Create(KToolsStarterServerExe, TPtr(NULL, 0), TUidType(KNullUid, KNullUid, KToolsStarterServerExeUid));
       
   188     if ( err == KErrNone )
       
   189         {
       
   190         LOG_MSG("CToolsStarterLauncher::StartServer process created");
       
   191         pr.Resume();
       
   192         pr.Close();
       
   193         }
       
   194     else
       
   195         {
       
   196         LOG_MSG2("CToolsStarterLauncher::StartServer Could not create process %d", err);
       
   197         }
       
   198     }
       
   199 
       
   200 
       
   201 void CToolsStarterLauncher::WaitForUiServices()
       
   202     {
       
   203     LOG_MSG("ToolsStarter Launcher: WaitForUiServices");
       
   204     RProperty property;
       
   205     TInt err = KErrNotReady;
       
   206     while ( err != KErrNone )
       
   207         {
       
   208         User::After(KPSDelay); // Wait for the startup to define the key
       
   209         err = property.Attach(KPSUidStartup, KPSGlobalSystemState);
       
   210         LOG_MSG2("ToolsStarter Launcher: WaitForUiServices attach err %d \n", err);
       
   211         }
       
   212     // Wait for the correct startup state
       
   213     TRequestStatus status;
       
   214     property.Subscribe(status);
       
   215     TInt state = 0;
       
   216     err = property.Get(state);
       
   217     LOG_MSG3("ToolsStarter Launcher: WaitForUiServices state %d err %d \n", state, err);
       
   218     while ( err == KErrNone &&
       
   219             !( state == ESwStateCriticalPhaseOK ||
       
   220                state == ESwStateNormalRfOn ||
       
   221                state == ESwStateNormalRfOff ||
       
   222                state == ESwStateNormalBTSap ) )
       
   223         {
       
   224         state = 0; // Reset previous value
       
   225         User::WaitForRequest(status);
       
   226         err = status.Int();
       
   227         if ( err == KErrNone )
       
   228             {
       
   229             property.Subscribe( status );
       
   230             err = property.Get( state );
       
   231             }
       
   232         LOG_MSG3("ToolsStarter Launcher: WaitForUiServices state changed %d err %d", state, err);
       
   233         }
       
   234     property.Cancel();
       
   235     property.Close();
       
   236     LOG_MSG("ToolsStarter Launcher: WaitForUiServices end");
       
   237     }
       
   238 
       
   239