sysstatemgmt/systemstatereferenceplugins/clayer/src/starterclient.cpp
changeset 0 4e1aa6a622a0
child 12 4a5c47a50617
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2007-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 "ssmclayer.h"
       
    17 #include "ssmdebug.h"
       
    18 #include <e32cmn.h>
       
    19 #include <ssm/ssmstatemanager.h>
       
    20 #include <ssm/ssmstateawaresession.h>
       
    21 #include <ssm/ssmdomaindefs.h>
       
    22 #include <ssm/ssmsubstates.hrh>
       
    23 #include <ssm/ssmadaptationcli.h>
       
    24 
       
    25 #include <ssm/starterclient.h>
       
    26 #include <ssm/startupdomainpskeys.h>
       
    27 #include <ssm/ssmuiproviderdll.h>
       
    28 
       
    29 #include <ssm/startupadaptationcommands.h>
       
    30 #include <ssm/clayerswp.hrh>
       
    31 #include <ssm/ssmsystemwideproperty.h>
       
    32 #include <ssm/ssmstates.hrh>
       
    33 #include <ssm/ssmpatchableconstants.h>
       
    34 
       
    35 // The following assert ensures that it's ok to just copy the handle from the 
       
    36 // RSsmStateManager class as there are no other data members if RSsmStateManage
       
    37 // is the same size as RSessionBase.
       
    38 //
       
    39 // This assert will fail if extra data members are added to RSsmStateManager
       
    40 // This ensures that the code in RStarterSession is correct.
       
    41 
       
    42 __ASSERT_COMPILE(sizeof(RSessionBase) == sizeof(RSsmStateManager)); //lint -e752 Suppress local declarator not referenced 
       
    43 	
       
    44 
       
    45 EXPORT_C TInt RStarterSession::Connect()
       
    46 	{
       
    47 	RSsmStateManager session;
       
    48 	TInt err = session.Connect();
       
    49 	if (err == KErrNone)
       
    50 		{
       
    51 		// Connection successful, copy handle from session to this handle
       
    52 		SetHandle(session.Handle());
       
    53 		}
       
    54 	// session is not closed before returning as this now owns the handle 
       
    55     return err;
       
    56     }
       
    57 	
       
    58 /*
       
    59      * Helper Function that reads KRFStatusPropertyKey SWP using RSsmSystemWideProperty and returns.
       
    60      * @return KErrNone on Success
       
    61      * Returns an error code from RSsmSystemWideProperty API - Connect & GetValue. 
       
    62      * On resulting error return, aRFSwpStatus contains ESsmRfOff
       
    63      * 
       
    64      * @internalComponent
       
    65 */	
       
    66 static TInt GetRFSwpStatus ( TInt& aRFSwpStatus )
       
    67 	{
       
    68 	TUint rfStatusPropertyKey = CSsmUiSpecific::RFStatusPropertyKey ();
       
    69 	
       
    70 	RSsmSystemWideProperty swp;
       
    71 	TInt err = swp.Connect ( rfStatusPropertyKey );
       
    72 	DEBUGPRINT2A ( "RF SWP connect error = %d ",err );
       
    73 	TInt rfStatus ( ESsmRfOff );
       
    74 	if ( err == KErrNone )
       
    75 		{
       
    76 		err = swp.GetValue ( rfStatus );
       
    77 		DEBUGPRINT3A ( "RF SWP status = %d ( err %d )", rfStatus, err );
       
    78 		}
       
    79 		
       
    80 	aRFSwpStatus = rfStatus;
       
    81 	swp.Close ();
       
    82 	return err;
       
    83 	}
       
    84 
       
    85 /**
       
    86  * This adjustment specifies how to convert start-up sub-states
       
    87  * to the states defined in StartupAdaptation::TGlobalState
       
    88  * 
       
    89  * It is calculated using the same method specified in CSaaStateAdaptation::MapToStartupAdaptationState(),
       
    90  * see that method for further details.
       
    91  * 
       
    92  * @internalComponent
       
    93  */
       
    94 const TInt KStartupSubStateAdjustment = static_cast<TInt>(StartupAdaptation::ESWStateStartingUiServices) 
       
    95                                     - static_cast<TInt>(ESsmStartupSubStateNonCritical);
       
    96 
       
    97 /**
       
    98  * Helper Function that is used for performing transition to startup substates .
       
    99  * @return KErrNone on Success and returns an error if RequestStateTransition fails or transition is not supported 
       
   100  * 
       
   101  * @internalComponent
       
   102 */
       
   103 static TInt TransitionToStartupSubState(TSsmState &aCurrentState, RSsmStateManager& aSession)
       
   104     {    
       
   105     TInt err = KErrNone;
       
   106     TInt reason = KSsmCLayerNullReason;
       
   107     TUint16 mainstate = aCurrentState.MainState();
       
   108     TUint16 substate = 0; 
       
   109 
       
   110     DEBUGPRINT2A( "RStarterSession::SetState, mainstate = %d", mainstate );                
       
   111     if( mainstate == ESsmStartup)
       
   112         {
       
   113         switch(aCurrentState.SubState())
       
   114             {
       
   115             case StartupAdaptation::ESWStateCharging - KStartupSubStateAdjustment:
       
   116                 {
       
   117                 substate = StartupAdaptation::ESWStateChargingToNormal - KStartupSubStateAdjustment;
       
   118                 TSsmStateTransition stateinfo(mainstate, substate, reason);
       
   119                 // Make request for state transition
       
   120                 err = aSession.RequestStateTransition(stateinfo);
       
   121                 break;
       
   122                 }
       
   123             case StartupAdaptation::ESWStateAlarm - KStartupSubStateAdjustment:
       
   124                 {
       
   125                 substate = StartupAdaptation::ESWStateAlarmToNormal - KStartupSubStateAdjustment;
       
   126                 TSsmStateTransition stateinfo(mainstate, substate, reason);
       
   127                 // Make request for state transition
       
   128                 err = aSession.RequestStateTransition(stateinfo);
       
   129                 break;
       
   130                 }
       
   131             default:
       
   132                 {
       
   133                 err = KErrArgument;
       
   134                 }
       
   135             }
       
   136         }
       
   137     else
       
   138        {
       
   139        err = KErrArgument;
       
   140        }                    
       
   141     return err;    
       
   142     }
       
   143 
       
   144 /**
       
   145  * Helper Function that is used for obtaining the currentstate.
       
   146  * @return KErrNone on Success and returns an error if RSsmStateAwareSession fails to connect to domainId KSM2UiServicesDomain3
       
   147  * @internalComponent
       
   148 */
       
   149 static TInt GetCurrentState( TSsmState &aCurrentState )
       
   150     {    
       
   151     RSsmStateAwareSession sas;
       
   152     const TInt err = sas.Connect(KSM2UiServicesDomain3);
       
   153     if (KErrNone == err)
       
   154         {
       
   155         aCurrentState = sas.State();
       
   156         sas.Close();       
       
   157         }
       
   158     return err;
       
   159     }
       
   160 
       
   161 EXPORT_C TInt RStarterSession::SetState( const TGlobalState aState )
       
   162 	{
       
   163 	RSsmStateManager session;
       
   164 	// Set session to use the handle stored in this
       
   165 	session.SetHandle(Handle());
       
   166 
       
   167 	DEBUGPRINT2A( "RStarterSession::SetState( %d )", aState );
       
   168     TInt err = KErrNone;
       
   169     //Initialising main state with an invalid main state (i.e ESsmMainSystemStateMax)
       
   170     TUint16 mainstate = ESsmMainSystemStateMax;
       
   171     TUint16 substate = 0;
       
   172     TSsmState currentState;
       
   173     TUint rfStatusPropertyKey = CSsmUiSpecific::RFStatusPropertyKey();
       
   174     switch (aState)
       
   175 		{
       
   176 		case ENormal:
       
   177 		    {
       
   178 		    //Switch depending on the patchable constant KSsmGracefulOffline is enabled
       
   179             if (IsSsmGracefulOffline())
       
   180                 {
       
   181                 err = GetCurrentState(currentState);
       
   182                 if ( err == KErrNone )
       
   183                     {
       
   184                     TInt globalSystemState;
       
   185                     err =RProperty::Get(KPSUidStartup, KPSGlobalSystemState, globalSystemState);
       
   186                     if( err == KErrNone )
       
   187                         {
       
   188                         mainstate = currentState.MainState();
       
   189                         //Allow state transition to normal RF ON if global state is not ESwStateNormalRfOn
       
   190                         if ((mainstate == ESsmNormal) && (globalSystemState != ESwStateNormalRfOn))
       
   191                             {
       
   192                             substate = ESsmNormalRfOnSubState;
       
   193                             TInt reason = KSsmCLayerNullReason;
       
   194                             TSsmStateTransition stateinfo(ESsmNormal,substate, reason);
       
   195                             err = session.RequestStateTransition(stateinfo);
       
   196                             }
       
   197                         else
       
   198                             {
       
   199                             //Transition to startup substate
       
   200                             err = TransitionToStartupSubState(currentState, session);
       
   201                             }
       
   202                         }
       
   203                     }
       
   204                 }
       
   205             else
       
   206                 {
       
   207                 TInt rfSwpValue;
       
   208                 err = GetRFSwpStatus ( rfSwpValue );
       
   209                 // Change value of RF Swp (to ESsmRfOn) only if it is not ESsmRfOn.
       
   210                 if (err == KErrNone && rfSwpValue != ESsmRfOn)
       
   211                     {
       
   212                     TSsmSwp swpRFStatus ( rfStatusPropertyKey, ESsmRfOn );
       
   213                     err = session.RequestSwpChange ( swpRFStatus );
       
   214                     }
       
   215                 else
       
   216                     {
       
   217                     err = GetCurrentState(currentState);
       
   218                     if(err == KErrNone)
       
   219                         {
       
   220                         //Transition to startup substate
       
   221                         err = TransitionToStartupSubState(currentState, session);
       
   222                         }
       
   223                     }
       
   224                 }
       
   225             break;
       
   226             }
       
   227 		case EAlarm:
       
   228 			{
       
   229 			// Charging to alarm
       
   230 			mainstate = ESsmStartup;
       
   231 			substate = StartupAdaptation::ESWStateChargingToAlarm - KStartupSubStateAdjustment;
       
   232 			TInt reason = KSsmCLayerNullReason;
       
   233 			TSsmStateTransition stateinfo(mainstate, substate, reason);
       
   234 			// Make request and return success.
       
   235 			err = session.RequestStateTransition(stateinfo);
       
   236 			break;
       
   237 			}
       
   238             case ECharging:
       
   239 			{
       
   240 			// Alarm to charging
       
   241 			mainstate = ESsmStartup;
       
   242 			substate = StartupAdaptation::ESWStateAlarmToCharging - KStartupSubStateAdjustment;
       
   243 			TInt reason = KSsmCLayerNullReason;
       
   244 			TSsmStateTransition stateinfo(mainstate, substate, reason);
       
   245 			// Make request and return success.
       
   246 			err = session.RequestStateTransition(stateinfo);
       
   247 			break;
       
   248 			}
       
   249 		case EOffline:
       
   250 			{
       
   251 			if (IsSsmGracefulOffline())
       
   252                 {
       
   253                 TInt globalSystemState;
       
   254                 err =RProperty::Get(KPSUidStartup, KPSGlobalSystemState, globalSystemState); 
       
   255                 if( err == KErrNone )
       
   256                     {
       
   257                     //Allow state transition if global state is not ESwStateNormalRfOff
       
   258                     if (globalSystemState != ESwStateNormalRfOff)
       
   259                         {
       
   260                         const TInt reason = KSsmCLayerNullReason;
       
   261                         substate = ESsmNormalRfOffSubState;
       
   262                         TSsmStateTransition stateinfo(ESsmNormal, substate, reason);
       
   263                         // Make request for substate transition to offline
       
   264                         err = session.RequestStateTransition(stateinfo);
       
   265                         }
       
   266                     }                  
       
   267                 }
       
   268             else
       
   269                 {
       
   270                 TInt rfSwpValue;
       
   271                 err = GetRFSwpStatus ( rfSwpValue );
       
   272                 // Change value of RF SwP (to ESsmRfOff) only if it is not ESsmRfOff.
       
   273                 if ( err == KErrNone && rfSwpValue != ESsmRfOff )
       
   274                     {
       
   275                     TSsmSwp swpRFStatus ( rfStatusPropertyKey, ESsmRfOff );
       
   276                     err = session.RequestSwpChange ( swpRFStatus );
       
   277                     }
       
   278                 }
       
   279             
       
   280             break;
       
   281 			}
       
   282 		case EBTSap:
       
   283 			{
       
   284 			TInt rfSwpValue;
       
   285 			err = GetRFSwpStatus ( rfSwpValue );
       
   286 			// Change value of RF Swp (to ESsmBtSap) only if it is notESsmBtSap.
       
   287 			if ( err == KErrNone &&	rfSwpValue != ESsmBtSap )
       
   288 				{
       
   289 				TSsmSwp swpRFStatus ( rfStatusPropertyKey, ESsmBtSap );
       
   290 				err = session.RequestSwpChange ( swpRFStatus );
       
   291 				}
       
   292 			break;
       
   293 			}
       
   294 		default:
       
   295 			{
       
   296 			err = KErrArgument;
       
   297 			}
       
   298 		}
       
   299 
       
   300 	// Session is not closed before returning as this owns the handle
       
   301 	return err;
       
   302 	}
       
   303 
       
   304 EXPORT_C void RStarterSession::Reset( const TResetReason aReason )
       
   305 	{
       
   306 	RSsmStateManager session;
       
   307     // Set session to use the handle stored in this
       
   308     session.SetHandle(Handle());
       
   309     
       
   310     // Package up the state transition
       
   311     TUint16 mainstate = ESsmShutdown;
       
   312     TUint16 substate = KSsmAnySubState;
       
   313     TInt reason = aReason;
       
   314     TSsmStateTransition stateinfo(mainstate, substate, reason);
       
   315     
       
   316     // Make request and ignore return value
       
   317     session.RequestStateTransition(stateinfo);
       
   318     // Session is not closed before returning as this owns the handle
       
   319     }
       
   320 
       
   321 EXPORT_C void RStarterSession::Shutdown()
       
   322 	{
       
   323 	RSsmStateManager session;
       
   324     // Set session to use the handle stored in this
       
   325     session.SetHandle(Handle());
       
   326     
       
   327     // Package up the state transition
       
   328     TUint16 mainstate = ESsmShutdown;
       
   329     TUint16 substate = KSsmAnySubState;
       
   330     TInt reason = KSsmCLayerPowerOffReason; // A null reason on shutdown will lead to a poweroff
       
   331     TSsmStateTransition stateinfo(mainstate, substate, reason);
       
   332     
       
   333     // Make request and ignore return value
       
   334     session.RequestStateTransition(stateinfo);
       
   335     // Session is not closed before returning as this owns the handle
       
   336     }
       
   337 
       
   338 EXPORT_C TInt RStarterSession::ResetNetwork()
       
   339 	{
       
   340 	RSsmStateManager session;
       
   341 	// Set session to use the handle stored in this
       
   342 	session.SetHandle(Handle());
       
   343 
       
   344 	// Make request and return success.
       
   345 	TUint rfStatusPropertyKey = CSsmUiSpecific::RFStatusPropertyKey();
       
   346 	TSsmSwp swpRFStatus(rfStatusPropertyKey, ESsmRfOff);
       
   347 
       
   348 	RSsmSystemWideProperty swp;
       
   349 	swp.Connect(rfStatusPropertyKey);
       
   350 
       
   351 	TRequestStatus status;
       
   352 	//Subscribe for SwP changes
       
   353 	swp.Subscribe(status);
       
   354 
       
   355 	TInt requestResult= session.RequestSwpChange(swpRFStatus);
       
   356 	if(KErrNone == requestResult)
       
   357 		{
       
   358 		// Wait for SwP Change
       
   359 	    User::WaitForRequest(status);
       
   360 		swpRFStatus.Set(rfStatusPropertyKey, ESsmRfOn);
       
   361 		requestResult = session.RequestSwpChange(swpRFStatus);
       
   362 		}
       
   363 	swp.Close();
       
   364 	// Session is not closed before returning as this owns the handle
       
   365 	return requestResult;
       
   366 	}
       
   367 
       
   368 EXPORT_C TBool RStarterSession::IsRTCTimeValid()
       
   369 	{
       
   370     TBool isRTCValid = EFalse;
       
   371 
       
   372 	//Ignoring return code for compatability reasons
       
   373     RProperty::Get(CSsmUiSpecific::ValidateRTCPropertyCategory(), 
       
   374                     CSsmUiSpecific::ValidateRTCPropertyKey(), isRTCValid);
       
   375     return isRTCValid;
       
   376     }
       
   377 
       
   378 EXPORT_C TInt RStarterSession::ActivateRfForEmergencyCall()
       
   379 	{
       
   380     RSsmEmergencyCallRfAdaptation ecradaptation;
       
   381     TInt err = ecradaptation.Connect();
       
   382     if (err != KErrNone)
       
   383     	{
       
   384     	return err;
       
   385     	}
       
   386     TRequestStatus status;
       
   387     // Request activation
       
   388     ecradaptation.ActivateRfForEmergencyCall(status);
       
   389     // Wait for completion
       
   390     User::WaitForRequest(status);
       
   391     ecradaptation.Close();
       
   392     // Return the completion value of status
       
   393     return status.Int();
       
   394     }
       
   395 
       
   396 EXPORT_C TInt RStarterSession::DeactivateRfAfterEmergencyCall()
       
   397 	{
       
   398 	RSsmEmergencyCallRfAdaptation ecradaptation;
       
   399     TInt err = ecradaptation.Connect();
       
   400     if (err != KErrNone)
       
   401     	{
       
   402     	return err;
       
   403     	}
       
   404     TRequestStatus status;
       
   405     // Request activation
       
   406     ecradaptation.DeactivateRfForEmergencyCall(status);
       
   407     // Wait for completion
       
   408     User::WaitForRequest(status);
       
   409     ecradaptation.Close();
       
   410     // Return the completion value of status
       
   411     return status.Int();
       
   412     }
       
   413 
       
   414 EXPORT_C TInt RStarterSession::EndSplashScreen()
       
   415 	{
       
   416 	RProperty splashscreen;
       
   417 	TInt err = splashscreen.Attach(KPSUidStartup, KPSSplashShutdown, EOwnerThread);
       
   418 	if (err != KErrNone)
       
   419 		{
       
   420 		return err;
       
   421 		}
       
   422 	
       
   423 	err = splashscreen.Set(ESplashShutdown);
       
   424 	
       
   425 	splashscreen.Close();
       
   426 	return err;
       
   427     }