devicediagnosticsfw/diagframework/src/diagenginestatemachine.cpp
changeset 32 5d0ec111abfc
parent 27 2c8580950a26
child 35 0deca9b72b62
equal deleted inserted replaced
27:2c8580950a26 32:5d0ec111abfc
     1 /*
       
     2 * Copyright (c) 2007 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:  Class definition of CStateMachine
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // CLASS DECLARATION
       
    20 #include "diagenginestatemachine.h"         // DiagFwInternal::CStateMachine
       
    21 
       
    22 // SYSTEM INCLUDE FILES
       
    23 #include <DiagFrameworkDebug.h>             // LOGSTRING
       
    24 
       
    25 // USER INCLUDE FILES
       
    26 #include "diagenginestatemachineobserver.h" // DiagFwInternal::MStateMachineObserver
       
    27 #include "diagengineeventbasic.h"           // DiagFwInternal::CEventBasic
       
    28 #include "diagframework.pan"                // Panics
       
    29 
       
    30 
       
    31 namespace DiagFwInternal
       
    32     {
       
    33 // ======== LOCAL DATA ==========
       
    34 struct TStateTableEntry
       
    35     {
       
    36     TState  iInputState;
       
    37     TEvent  iEventType;
       
    38     TState  iOutputState;
       
    39     };
       
    40 
       
    41 
       
    42 static const TStateTableEntry  KStateTable[] = 
       
    43     {
       
    44         // current state    input event                 output state
       
    45         {
       
    46         EStateNotReady,     EEventExecute,              EStateCreatingPlan
       
    47         },
       
    48         {
       
    49         EStateCreatingPlan, EEventPlanCreated,          EStateRunning
       
    50         },
       
    51         {
       
    52         EStateSuspended,    EEventResumeToRunning,      EStateRunning
       
    53         },
       
    54         {
       
    55         EStateSuspended,    EEventResumeToCreatingPlan, EStateCreatingPlan
       
    56         },
       
    57         {
       
    58         EStateFinalizing,   EEventFinalized,            EStateStopped,
       
    59         },
       
    60         // ALL EStateAny MUST be listed in below.
       
    61         // This makes sure that more specific transition happens before
       
    62         // any state transitions occur.
       
    63         {
       
    64         EStateAny,          EEventAllPluginsCompleted,  EStateFinalizing
       
    65         },
       
    66         {
       
    67         EStateAny,          EEventCancelAll,            EStateCancelAll
       
    68         },
       
    69         {
       
    70         EStateAny,          EEventSuspend,              EStateSuspended
       
    71         },
       
    72         {
       
    73         EStateAny,          EEventVoiceCallActive,      EStateSuspended
       
    74         }
       
    75     };
       
    76 
       
    77 static const TInt KStateTableSize = sizeof( KStateTable )/sizeof( TStateTableEntry );
       
    78 
       
    79 
       
    80 // ======== LOCAL FUNCTIONS ========
       
    81 
       
    82 
       
    83 // ======== MEMBER FUNCTIONS ========
       
    84 
       
    85 // ---------------------------------------------------------------------------
       
    86 // CStateMachine::ConstructL()
       
    87 // ---------------------------------------------------------------------------
       
    88 //
       
    89 void CStateMachine::ConstructL()
       
    90     {
       
    91     }
       
    92 
       
    93 
       
    94 // ---------------------------------------------------------------------------
       
    95 // CStateMachine::NewL()
       
    96 // ---------------------------------------------------------------------------
       
    97 //
       
    98 CStateMachine* CStateMachine::NewL( MStateMachineObserver& aObserver )
       
    99     {
       
   100     CStateMachine* self = CStateMachine::NewLC( aObserver );
       
   101     CleanupStack::Pop( self );
       
   102     return self;
       
   103     }
       
   104 
       
   105 
       
   106 // ---------------------------------------------------------------------------
       
   107 // CStateMachine::NewLC()
       
   108 // ---------------------------------------------------------------------------
       
   109 //
       
   110 CStateMachine* CStateMachine::NewLC( MStateMachineObserver& aObserver )
       
   111     {
       
   112     CStateMachine* self = new( ELeave )CStateMachine( aObserver );
       
   113     CleanupStack::PushL( self );
       
   114     self->ConstructL();
       
   115     return self;
       
   116     }
       
   117 
       
   118 // ---------------------------------------------------------------------------
       
   119 // CStateMachine::CStateMachine
       
   120 // ---------------------------------------------------------------------------
       
   121 //
       
   122 CStateMachine::CStateMachine( MStateMachineObserver& aObserver )
       
   123     :   CActive( EPriorityStandard ),
       
   124         iObserver( aObserver ),
       
   125         iEventQueue(),
       
   126         iState( EStateNotReady )
       
   127     {
       
   128     CActiveScheduler::Add( this );
       
   129     }
       
   130 
       
   131 
       
   132 // ---------------------------------------------------------------------------
       
   133 // CStateMachine::~CStateMachine
       
   134 // ---------------------------------------------------------------------------
       
   135 //
       
   136 CStateMachine::~CStateMachine() 
       
   137     {
       
   138     Cancel();
       
   139     iEventQueue.ResetAndDestroy();
       
   140     iEventQueue.Close();
       
   141     }
       
   142     
       
   143 
       
   144 // ---------------------------------------------------------------------------
       
   145 // CStateMachine::AddEventL
       
   146 // ---------------------------------------------------------------------------
       
   147 //
       
   148 void CStateMachine::AddEventL( CEventBasic* aEvent )
       
   149     {
       
   150     __ASSERT_ALWAYS( aEvent, Panic( EDiagFrameworkBadArgument ) );
       
   151 
       
   152     LOGSTRING2( "CStateMachine::AddEventL: Type = %S", &( aEvent->ToString() ) )
       
   153 
       
   154     CleanupStack::PushL( aEvent );
       
   155     iEventQueue.AppendL( aEvent );     // owership transfer
       
   156     CleanupStack::Pop( aEvent );
       
   157     // do not set aEvent to NULL, since it is used later
       
   158 
       
   159     TState nextState = CheckStateTable( iState, aEvent->GetType() );
       
   160 
       
   161     if ( nextState != EStateAny && nextState != iState )
       
   162         {
       
   163         // state change happened.
       
   164         TState prevState = iState;
       
   165         iState = nextState;
       
   166 
       
   167         iObserver.HandleStateChangedL( prevState, iState, *aEvent );
       
   168         }
       
   169 
       
   170     ReactivateQueue();
       
   171     }
       
   172 
       
   173 
       
   174 // ---------------------------------------------------------------------------
       
   175 // CStateMachine::AddEventL
       
   176 // ---------------------------------------------------------------------------
       
   177 //
       
   178 void CStateMachine::AddEventL( TEvent aEventId )
       
   179     {
       
   180     // simple event. Create a basic event type
       
   181     AddEventL( new( ELeave )CEventBasic( aEventId ) );
       
   182     }
       
   183 
       
   184 
       
   185 // ---------------------------------------------------------------------------
       
   186 // CStateMachine::CurrentState
       
   187 // ---------------------------------------------------------------------------
       
   188 //
       
   189 TState CStateMachine::CurrentState() const
       
   190     {
       
   191     return iState;
       
   192     }
       
   193 
       
   194 // ---------------------------------------------------------------------------
       
   195 // CStateMachine::HandleError
       
   196 // ---------------------------------------------------------------------------
       
   197 //
       
   198 void CStateMachine::HandleError( TInt aError )
       
   199     {
       
   200     iState = iObserver.HandleError( iState, aError );
       
   201     }
       
   202 
       
   203 // ---------------------------------------------------------------------------
       
   204 // From CActive
       
   205 // CStateMachine::RunL
       
   206 // ---------------------------------------------------------------------------
       
   207 //
       
   208 void CStateMachine::RunL() 
       
   209     {
       
   210     // Note that ReactivateQueue() must always be called before observer method
       
   211     // is called. This is because observer may delete state machine
       
   212     // during the callback. To prevent crashes, callback MUST BE the
       
   213     // last funciton to call before returning.
       
   214     if ( iEventQueue.Count() > 0 )
       
   215         {
       
   216         // there is an item in the event queue.
       
   217         // Pop and execute the event.
       
   218         CEventBasic* event = iEventQueue[0];
       
   219         iEventQueue.Remove( 0 );
       
   220         
       
   221         // always reactivate the queue before calling observer
       
   222         ReactivateQueue();
       
   223 
       
   224         CleanupStack::PushL( event );
       
   225         iObserver.HandleEventL( *event );
       
   226         CleanupStack::PopAndDestroy( event );
       
   227 
       
   228         // must return immediately to make sure that no member variables
       
   229         // are accessed after observer is called.
       
   230         return;
       
   231         }
       
   232     else
       
   233         {
       
   234         LOGSTRING( "CStateMachine::RunL(). Called for no reason?" )
       
   235         __ASSERT_DEBUG( 0, Panic( EDiagFrameworkCorruptStateMachine ) );
       
   236         }
       
   237     }
       
   238 
       
   239 // ---------------------------------------------------------------------------
       
   240 // From CActive
       
   241 // CStateMachine::DoCancel
       
   242 // ---------------------------------------------------------------------------
       
   243 //
       
   244 void CStateMachine::DoCancel() 
       
   245     {
       
   246     // Nothing to do here..
       
   247     }
       
   248 
       
   249 // ---------------------------------------------------------------------------
       
   250 // From CActive
       
   251 // CStateMachine::RunError
       
   252 // ---------------------------------------------------------------------------
       
   253 //
       
   254 TInt CStateMachine::RunError( TInt aError ) 
       
   255     {
       
   256     HandleError( aError );
       
   257     return KErrNone;
       
   258     }
       
   259 
       
   260 
       
   261 // ---------------------------------------------------------------------------
       
   262 // CStateMachine::CheckStateTable
       
   263 // ---------------------------------------------------------------------------
       
   264 //
       
   265 TState CStateMachine::CheckStateTable( TState aCurrState, TEvent aEvent ) const
       
   266     {
       
   267     TState outputState = EStateAny;
       
   268     TBool isFound = EFalse;
       
   269 
       
   270     for ( TInt i = 0; i < KStateTableSize && !isFound; i++ )
       
   271         {
       
   272         if ( ( KStateTable[i].iInputState == EStateAny || 
       
   273                     aCurrState == KStateTable[i].iInputState ) &&
       
   274                 aEvent == KStateTable[i].iEventType )
       
   275             {
       
   276             outputState = KStateTable[i].iOutputState;
       
   277             isFound = ETrue;
       
   278             }
       
   279         }
       
   280     return outputState;
       
   281     }
       
   282 
       
   283 // ---------------------------------------------------------------------------
       
   284 // CStateMachine::ReactivateQueue
       
   285 // ---------------------------------------------------------------------------
       
   286 //
       
   287 void CStateMachine::ReactivateQueue()
       
   288     {
       
   289     if ( !IsActive() && iEventQueue.Count() > 0 )
       
   290         {
       
   291         // reactivate only if it is not already active, and there is something
       
   292         // in the queue
       
   293         TRequestStatus* stat = &iStatus;
       
   294         User::RequestComplete( stat, KErrNone );
       
   295         SetActive();
       
   296         }
       
   297     }
       
   298 
       
   299 // ---------------------------------------------------------------------------
       
   300 // CStateMachine::StateName
       
   301 // ---------------------------------------------------------------------------
       
   302 //
       
   303 
       
   304 #if _DEBUG
       
   305 const TDesC& CStateMachine::StateName( TState aState ) const
       
   306     {
       
   307     _LIT( KStateAny,         "EStateAny" );
       
   308     _LIT( KStateNotReady,    "EStateNotReady" );
       
   309     _LIT( KStateCreatingPlan,"EStateCreatingPlan" );
       
   310     _LIT( KStateRunning,     "EStateRunning" );
       
   311     _LIT( KStateStopped,     "EStateStopped" );
       
   312     _LIT( KStateCancelAll,   "EStateCancelAll" );
       
   313     _LIT( KStateSuspended,   "EStateSuspended" );
       
   314     _LIT( KStateFinalizing,  "EStateFinalizing" );
       
   315 
       
   316     switch ( aState )
       
   317         {
       
   318         case EStateAny:
       
   319             return KStateAny();
       
   320 
       
   321         case EStateNotReady:
       
   322             return KStateNotReady();
       
   323 
       
   324         case EStateCreatingPlan:
       
   325             return KStateCreatingPlan();
       
   326 
       
   327         case EStateRunning:
       
   328             return KStateRunning();
       
   329 
       
   330         case EStateStopped:
       
   331             return KStateStopped();
       
   332 
       
   333         case EStateCancelAll:
       
   334             return KStateCancelAll();
       
   335 
       
   336         case EStateSuspended:
       
   337             return KStateSuspended();
       
   338 
       
   339         case EStateFinalizing:
       
   340             return KStateFinalizing();
       
   341         
       
   342         default:
       
   343             _LIT( KUnknownState, "* Unknown State *");
       
   344             return KUnknownState(); 
       
   345         }
       
   346     }
       
   347 
       
   348 #else   // #if _DEBUG
       
   349 
       
   350 // non-debug version.
       
   351 const TDesC& CStateMachine::StateName( TState /* aState */ ) const
       
   352     {
       
   353     _LIT( KNonDebugStateName, "?" );
       
   354     return KNonDebugStateName();
       
   355     }
       
   356 
       
   357 #endif // #else _DEBUG
       
   358 
       
   359     } // end of namespace DiagFwInternal
       
   360 
       
   361 // End of File
       
   362