sl@0: // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // Implementation of finite state machine sl@0: // sl@0: // sl@0: sl@0: /** sl@0: @file sl@0: @internalTechnology sl@0: */ sl@0: sl@0: #include "t_ms_main.h" sl@0: #include "cstatemachine.h" sl@0: #include "tstate.h" sl@0: sl@0: CStateMachine* sl@0: CStateMachine::NewL() sl@0: { sl@0: return new (ELeave) CStateMachine; sl@0: } sl@0: sl@0: CStateMachine::CStateMachine() sl@0: { sl@0: // Intentionally left blank sl@0: } sl@0: sl@0: CStateMachine::~CStateMachine() sl@0: { sl@0: allStates.ResetAndDestroy(); sl@0: } sl@0: sl@0: void sl@0: CStateMachine::MoveTo(int aStateId) sl@0: { sl@0: if (!iCurrentState) sl@0: { sl@0: test.Printf(_L("The current state is undefined\n")); sl@0: test(EFalse); sl@0: } sl@0: sl@0: iCurrentState->MoveTo(aStateId); sl@0: iCurrentState = FindState(aStateId); sl@0: } sl@0: sl@0: const TState* sl@0: CStateMachine::AddState(int aStateId) sl@0: { sl@0: const TState* state = FindState(aStateId); sl@0: sl@0: if (state == 0) sl@0: { sl@0: switch (aStateId) sl@0: { sl@0: case EUsbMsDriveState_Disconnected: sl@0: state = new TDisconnected(); sl@0: break; sl@0: case EUsbMsDriveState_Connecting: sl@0: state = new TConnecting(); sl@0: break; sl@0: case EUsbMsDriveState_Connected: sl@0: state = new TConnected(); sl@0: break; sl@0: case EUsbMsDriveState_Disconnecting: sl@0: state = new TDisconnecting(); sl@0: break; sl@0: case EUsbMsDriveState_Active: sl@0: state = new TActive(); sl@0: break; sl@0: case EUsbMsDriveState_Locked: sl@0: state = new TLocked(); sl@0: break; sl@0: case EUsbMsState_Read: sl@0: state = new TRead(); sl@0: break; sl@0: case EUsbMsState_Written: sl@0: state = new TWritten(); sl@0: break; sl@0: default: sl@0: break; sl@0: } sl@0: sl@0: if (state) sl@0: { sl@0: allStates.Append(state); sl@0: } sl@0: } sl@0: sl@0: return state; sl@0: } sl@0: sl@0: void sl@0: CStateMachine::SetInitState(int aStateId) sl@0: { sl@0: iCurrentState = FindState(aStateId); sl@0: iFromStateId = aStateId; sl@0: } sl@0: sl@0: const TState* sl@0: CStateMachine::FindState(int aStateId) const sl@0: { sl@0: TInt count = allStates.Count(); sl@0: for (TInt i = 0; i < count; i++) sl@0: { sl@0: if (allStates[i]->GetStateId() == aStateId) sl@0: { sl@0: return allStates[i]; sl@0: } sl@0: } sl@0: sl@0: return 0; sl@0: } sl@0: sl@0: TInt CStateMachine::CurrentStateId() const sl@0: { sl@0: return iCurrentState->GetStateId(); sl@0: } sl@0: sl@0: TInt CStateMachine::FromStateId() const sl@0: { sl@0: return iFromStateId; sl@0: } sl@0: sl@0: void CStateMachine::SetFromStateId(TInt aStateId) sl@0: { sl@0: iFromStateId = aStateId; sl@0: }