os/kernelhwsrv/kerneltest/f32test/smassstorage/src/cstatemachine.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2004-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 the License "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 // Implementation of finite state machine
    15 // 
    16 //
    17 
    18 /**
    19  @file
    20  @internalTechnology
    21 */
    22 
    23 #include "t_ms_main.h"
    24 #include "cstatemachine.h"
    25 #include "tstate.h"
    26 
    27 CStateMachine* 
    28 CStateMachine::NewL()
    29 	{
    30 	return new (ELeave) CStateMachine;
    31 	}
    32 	
    33 CStateMachine::CStateMachine()
    34 	{
    35     // Intentionally left blank
    36 	}
    37 
    38 CStateMachine::~CStateMachine()
    39 	{
    40     allStates.ResetAndDestroy();
    41 	}
    42 
    43 void
    44 CStateMachine::MoveTo(int aStateId)
    45 	{
    46     if (!iCurrentState)
    47     	{
    48         test.Printf(_L("The current state is undefined\n"));
    49         test(EFalse);
    50     	}
    51 
    52     iCurrentState->MoveTo(aStateId);
    53     iCurrentState = FindState(aStateId);
    54 	}
    55 
    56 const TState*
    57 CStateMachine::AddState(int aStateId)
    58 	{
    59     const TState* state = FindState(aStateId);
    60 
    61     if (state == 0)
    62     	{
    63          switch (aStateId)
    64          	{
    65          case EUsbMsDriveState_Disconnected:
    66              state = new TDisconnected();
    67              break;
    68          case EUsbMsDriveState_Connecting:
    69              state = new TConnecting();
    70              break;
    71          case EUsbMsDriveState_Connected:
    72              state = new TConnected();
    73              break;     
    74          case EUsbMsDriveState_Disconnecting:
    75              state = new TDisconnecting();
    76              break;
    77          case EUsbMsDriveState_Active:
    78              state = new TActive();
    79              break;
    80          case EUsbMsDriveState_Locked:
    81              state = new TLocked();
    82              break;
    83          case EUsbMsState_Read:
    84              state = new TRead();
    85              break;
    86          case EUsbMsState_Written:
    87              state = new TWritten();
    88              break;
    89          default:
    90              break;
    91          	}
    92     
    93          if (state)
    94          	{
    95              allStates.Append(state);
    96          	}
    97     	}
    98 
    99     return state;
   100 	}
   101 
   102 void
   103 CStateMachine::SetInitState(int aStateId)
   104 	{
   105     iCurrentState = FindState(aStateId);
   106     iFromStateId = aStateId;
   107 	}
   108 
   109 const TState* 
   110 CStateMachine::FindState(int aStateId) const
   111 	{
   112     TInt count = allStates.Count();
   113     for (TInt i = 0; i < count; i++)
   114     	{
   115         if (allStates[i]->GetStateId() == aStateId)
   116         	{
   117             return allStates[i];
   118         	}
   119     	}
   120 
   121     return 0;
   122 	}
   123 
   124 TInt CStateMachine::CurrentStateId() const
   125 	{
   126 	return iCurrentState->GetStateId();
   127 	}
   128 
   129 TInt CStateMachine::FromStateId() const
   130 	{
   131 	return iFromStateId;
   132 	}
   133 
   134 void CStateMachine::SetFromStateId(TInt aStateId)
   135 	{
   136 	iFromStateId = aStateId;
   137 	}