utilities/aristatemachine/src/aristatemachine.cpp
changeset 0 bb31fbe78861
equal deleted inserted replaced
-1:000000000000 0:bb31fbe78861
       
     1 	/*
       
     2 * Copyright (c) 2009 Aricent and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "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 * Aricent - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 * State machine for all video hwdevice plugins.
       
    16 *
       
    17 */
       
    18 
       
    19 //User includes
       
    20 #include "aristatemachine.h"
       
    21 
       
    22 //---------------------------------------------------------------------------
       
    23 //Default Constrcutor.
       
    24 //---------------------------------------------------------------------------
       
    25 //
       
    26 CStateMachine::CStateMachine():iState( EUnInitialized )
       
    27 	{
       
    28 	PRINT_ENTRY;
       
    29 	// Initailization
       
    30 	for ( TInt i = 0; i < KNumOfStates; i++ )
       
    31 		{
       
    32 		for ( TInt j = 0; j < KNumOfCommands; j++ )
       
    33 			{
       
    34 			iStateChanges [i][j] = EInvalidState;
       
    35 			}
       
    36 		}
       
    37 
       
    38 	// set the valid transition states in the table
       
    39 
       
    40 	// valid state changes from Uninitailize
       
    41 	iStateChanges [EUnInitialized][EInitializeCommand] = EInitialized;
       
    42 	iStateChanges [EUnInitialized][EInitializingCommand] = EInitializing;
       
    43 	iStateChanges [EUnInitialized][EDeadStateCommand]
       
    44 	                               = ENonInitializedDeadState;
       
    45 
       
    46 
       
    47 	// valid state changes from Initailize
       
    48 	iStateChanges [EInitialized][EStartCommand]		= EStart;
       
    49 	iStateChanges [EInitialized][EPauseCommand]		= EPause;
       
    50 	iStateChanges [EInitialized][EResumeCommand]	= EStart;
       
    51 	iStateChanges [EInitialized][EInputEndCommand]	= EInitializedInStopping;
       
    52 	iStateChanges [EInitialized][EStopCommand]		= EStop;
       
    53 	iStateChanges [EInitialized][EDeadStateCommand]	= EInitializedDeadState;
       
    54 
       
    55 	iStateChanges [EInitializing][EDeadStateCommand] = EInitializedDeadState;
       
    56 	iStateChanges [EInitializing][EInitializeCommand]	= EInitialized;
       
    57 
       
    58 	// valid state changes from Start
       
    59 	iStateChanges [EStart][EStartCommand]		= EStart;
       
    60 	iStateChanges [EStart][EPauseCommand]		= EPause;
       
    61 	iStateChanges [EStart][EInputEndCommand]	= EStopping;
       
    62 	iStateChanges [EStart][EStopCommand]		= EStop;
       
    63 	iStateChanges [EStart][EDeadStateCommand]	= ENonInitializedDeadState;
       
    64 
       
    65 	// valid  State changes from pause
       
    66 	iStateChanges [EPause][EPauseCommand]		= EPause;
       
    67 	iStateChanges [EPause][EResumeCommand]		= EStart;
       
    68 	iStateChanges [EPause][EInputEndCommand]	= EPauseInStopping;
       
    69 	iStateChanges [EPause][EStopCommand]		= EStop;
       
    70 	iStateChanges [EPause][EDeadStateCommand]	= ENonInitializedDeadState;
       
    71 
       
    72 	// valid state changes from Stop
       
    73 	iStateChanges [EStop][EStartCommand]	= EStart;
       
    74 	iStateChanges [EStop][EInputEndCommand] = EStopInInputEnd;
       
    75 	iStateChanges [EStop][EStopCommand]		= EStop;
       
    76 	iStateChanges [EStop][EDeadStateCommand]= ENonInitializedDeadState;
       
    77 
       
    78 	// valid state changes from InitailizeInStopping
       
    79 	iStateChanges [EInitializedInStopping][EStartCommand] = EStopping;
       
    80 	iStateChanges [EInitializedInStopping][EDeadStateCommand]
       
    81 	                                       = ENonInitializedDeadState;
       
    82 
       
    83 	// valid  State Changes from EStopping
       
    84 	iStateChanges [EStopping][EPauseCommand] = EPauseInStopping;
       
    85 	iStateChanges [EStopping][EStopCommand]	 = EStop;
       
    86 	iStateChanges [EStopping][EDeadStateCommand] = ENonInitializedDeadState;
       
    87 
       
    88 	// valid state changes from PauseInStopping
       
    89 	iStateChanges [EPauseInStopping][EResumeCommand] = EStopping;
       
    90 	iStateChanges [EPauseInStopping][EStopCommand] = EStop;
       
    91 	iStateChanges [EPauseInStopping][EDeadStateCommand]
       
    92 	                                 = ENonInitializedDeadState;
       
    93 
       
    94 	// valid state changes frm StopInInputEnd
       
    95 	iStateChanges [EStopInInputEnd][EStartCommand] = EStopping;
       
    96 	iStateChanges [EStopInInputEnd][EStopCommand] = EStop;
       
    97 	iStateChanges [EStopInInputEnd][EDeadStateCommand]
       
    98 	                                = ENonInitializedDeadState;
       
    99 	}
       
   100 
       
   101 //---------------------------------------------------------------------------
       
   102 //Constrcutor.
       
   103 //---------------------------------------------------------------------------
       
   104 //
       
   105 EXPORT_C CStateMachine* CStateMachine::NewL()
       
   106 	{
       
   107 	PRINT_ENTRY;
       
   108 	CStateMachine* self = new ( ELeave ) CStateMachine();
       
   109 	CleanupStack::PushL( self );
       
   110 	self->ConstructL();
       
   111 	CleanupStack::Pop();
       
   112 	PRINT_EXIT;
       
   113 	return( self );
       
   114 	}
       
   115 
       
   116 //---------------------------------------------------------------------------
       
   117 //Symbian 2nd Phase Constrcutor.
       
   118 //---------------------------------------------------------------------------
       
   119 //
       
   120 void CStateMachine::ConstructL()
       
   121 	{
       
   122 	PRINT_ENTRY;
       
   123 	PRINT_EXIT;
       
   124 	}
       
   125 
       
   126 //---------------------------------------------------------------------------
       
   127 //Checks whether transition to new state is possible or not.
       
   128 //---------------------------------------------------------------------------
       
   129 //
       
   130 EXPORT_C TBool CStateMachine::IsTransitionValid( TCommand aCommand )
       
   131 	{
       
   132 	PRINT_ENTRY;
       
   133 
       
   134 	if ( iStateChanges[iState][aCommand] != EInvalidState )
       
   135 		{
       
   136 		PRINT_EXIT;
       
   137 		return( ETrue );
       
   138 		}
       
   139 	else
       
   140 		{
       
   141 		PRINT_EXIT;
       
   142 		return( EFalse );
       
   143 		}
       
   144 	}
       
   145 
       
   146 //---------------------------------------------------------------------------
       
   147 //Transits to new state. If transition cann't be done, object will remain in
       
   148 //the previous state.
       
   149 //---------------------------------------------------------------------------
       
   150 //
       
   151 EXPORT_C TInt CStateMachine::Transit( TCommand aCommand )
       
   152 	{
       
   153 	PRINT_ENTRY;
       
   154 	if ( !IsTransitionValid ( aCommand ) )
       
   155 		{
       
   156 		return KErrGeneral;
       
   157 		}
       
   158 	iState = iStateChanges[iState][aCommand];
       
   159 	PRINT_EXIT;
       
   160 	return KErrNone;
       
   161 	}
       
   162 
       
   163 //---------------------------------------------------------------------------
       
   164 //Tells whether the state is initialized or not. Here initilized means if the
       
   165 //state is in any state other than EUnInitialized
       
   166 //---------------------------------------------------------------------------
       
   167 //
       
   168 EXPORT_C TBool CStateMachine::IsInitialized()
       
   169 	{
       
   170 	PRINT_ENTRY;
       
   171 	if ( iState	!=	EUnInitialized )
       
   172 		{
       
   173 		PRINT_EXIT;
       
   174 		return ETrue;
       
   175 		}
       
   176 	else
       
   177 		{
       
   178 		PRINT_EXIT;
       
   179 		return EFalse;
       
   180 		}
       
   181 	}
       
   182 
       
   183 //---------------------------------------------------------------------------
       
   184 //Tells whether the state is exactly in EInitialized state or not.
       
   185 //---------------------------------------------------------------------------
       
   186 //
       
   187 EXPORT_C TBool CStateMachine::IsInInitializedState()
       
   188 	{
       
   189 	PRINT_ENTRY;
       
   190 	if ( iState	==	EInitialized )
       
   191 		{
       
   192 		PRINT_EXIT;
       
   193 		return ETrue;
       
   194 		}
       
   195 	else
       
   196 		{
       
   197 		PRINT_EXIT;
       
   198 		return EFalse;
       
   199 		}
       
   200 	}
       
   201 
       
   202 //---------------------------------------------------------------------------
       
   203 //Tells whether the state is initializing or not.
       
   204 //---------------------------------------------------------------------------
       
   205 //
       
   206 EXPORT_C TBool CStateMachine::IsInitializing()
       
   207 	{
       
   208 	PRINT_ENTRY;
       
   209 	if ( iState	==	EInitializing )
       
   210 		{
       
   211 		PRINT_EXIT;
       
   212 		return ETrue;
       
   213 		}
       
   214 	else
       
   215 		{
       
   216 		PRINT_EXIT;
       
   217 		return EFalse;
       
   218 		}
       
   219 	}
       
   220 
       
   221 //---------------------------------------------------------------------------
       
   222 //Tells whether the state is in any one of the input ending
       
   223 //states i.e
       
   224 //EInitializedInStopping,
       
   225 //EStopping,
       
   226 //EPauseInStopping,
       
   227 //EStopInInputEnd
       
   228 //---------------------------------------------------------------------------
       
   229 //
       
   230 EXPORT_C TBool CStateMachine::IsInputEndPending()
       
   231 	{
       
   232 	PRINT_ENTRY;
       
   233 	if ( iState == EInitializedInStopping ||
       
   234 		iState == EStopping ||
       
   235 		iState == EPauseInStopping ||
       
   236 		iState == EStopInInputEnd )
       
   237 		{
       
   238 		PRINT_EXIT;
       
   239 		return ETrue;
       
   240 		}
       
   241 	else
       
   242 		{
       
   243 		PRINT_EXIT;
       
   244 		return EFalse;
       
   245 		}
       
   246 	}
       
   247 
       
   248 //---------------------------------------------------------------------------
       
   249 //Tells whether state is in EStart state
       
   250 //---------------------------------------------------------------------------
       
   251 //
       
   252 EXPORT_C TBool CStateMachine::IsStarted()
       
   253 	{
       
   254 	PRINT_ENTRY;
       
   255 	if ( iState == EStart )
       
   256 		{
       
   257 		PRINT_EXIT;
       
   258 		return ETrue;
       
   259 		}
       
   260 	else
       
   261 		{
       
   262 		PRINT_EXIT;
       
   263 		return EFalse;
       
   264 		}
       
   265 	}
       
   266 
       
   267 //---------------------------------------------------------------------------
       
   268 //Tells whether stae is in EPlaying state
       
   269 //---------------------------------------------------------------------------
       
   270 //
       
   271 EXPORT_C TBool CStateMachine::IsPlaying()
       
   272 	{
       
   273 	PRINT_ENTRY;
       
   274 	if ( iState == EStart || iState == EStopping )
       
   275 		{
       
   276 		PRINT_EXIT;
       
   277 		return ETrue;
       
   278 		}
       
   279 	else
       
   280 		{
       
   281 		PRINT_EXIT;
       
   282 		return EFalse;
       
   283 		}
       
   284 	}
       
   285 
       
   286 //---------------------------------------------------------------------------
       
   287 //Tells whether state is in EPause state
       
   288 //---------------------------------------------------------------------------
       
   289 //
       
   290 EXPORT_C TBool CStateMachine::IsPaused()
       
   291 	{
       
   292 	PRINT_ENTRY;
       
   293 	if ( ( iState == EPause ) || ( iState == EPauseInStopping ) )
       
   294 		{
       
   295 		PRINT_EXIT;
       
   296 		return ETrue;
       
   297 		}
       
   298 	else
       
   299 		{
       
   300 		PRINT_EXIT;
       
   301 		return EFalse;
       
   302 		}
       
   303 	}
       
   304 
       
   305 //---------------------------------------------------------------------------
       
   306 //Tells whether stae is in EStop state
       
   307 //---------------------------------------------------------------------------
       
   308 //
       
   309 EXPORT_C TBool CStateMachine::IsStopped()
       
   310 	{
       
   311 	PRINT_ENTRY;
       
   312 	if ( iState == EStop )
       
   313 		{
       
   314 		PRINT_EXIT;
       
   315 		return ETrue;
       
   316 		}
       
   317 	else
       
   318 		{
       
   319 		PRINT_EXIT;
       
   320 		return EFalse;
       
   321 		}
       
   322 	}
       
   323 
       
   324 //---------------------------------------------------------------------------
       
   325 //Tells whether stae is in EInitailize or not
       
   326 //---------------------------------------------------------------------------
       
   327 //
       
   328 EXPORT_C TBool CStateMachine::IfIsStateInInitailize()
       
   329 	{
       
   330 	PRINT_ENTRY;
       
   331 	if ( iState == EInitialized )
       
   332 		{
       
   333 		PRINT_EXIT;
       
   334 		return ETrue;
       
   335 		}
       
   336 	else
       
   337 		{
       
   338 		PRINT_EXIT;
       
   339 		return EFalse;
       
   340 		}
       
   341 	}
       
   342 
       
   343 //---------------------------------------------------------------------------
       
   344 //Resets the state machine to EUnInitialized state.
       
   345 //---------------------------------------------------------------------------
       
   346 //
       
   347 EXPORT_C void CStateMachine::Reset()
       
   348 	{
       
   349 	PRINT_ENTRY;
       
   350 	iState	=	EUnInitialized;
       
   351 	PRINT_EXIT;
       
   352 	}
       
   353 
       
   354 
       
   355 
       
   356 //---------------------------------------------------------------------------
       
   357 //Tells whether stae is in EDeadState state
       
   358 //---------------------------------------------------------------------------
       
   359 //
       
   360 EXPORT_C TBool CStateMachine::IsInDeadState()
       
   361 	{
       
   362 	PRINT_ENTRY;
       
   363 	if ( ( iState == EInitializedDeadState ) ||
       
   364 		 ( iState == ENonInitializedDeadState ) )
       
   365 		{
       
   366 		PRINT_EXIT;
       
   367 		return ETrue;
       
   368 		}
       
   369 	else
       
   370 		{
       
   371 		PRINT_EXIT;
       
   372 		return EFalse;
       
   373 		}
       
   374 	}
       
   375 
       
   376 //---------------------------------------------------------------------------
       
   377 //Destructor for StateMachine
       
   378 //---------------------------------------------------------------------------
       
   379 //
       
   380 CStateMachine::~CStateMachine()
       
   381 	{}