usbengines/usbdevcon/src/cstatemachine.cpp
changeset 35 9d8b04ca6939
parent 0 1e05558e2206
child 87 18fe5224f0dc
equal deleted inserted replaced
34:7858bc6ead78 35:9d8b04ca6939
       
     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:  usbdevcon state machine
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "cusbdevcon.h"
       
    20 #include "cstatemachine.h"
       
    21 #include "cep0reader.h"
       
    22 #include "cep0writer.h"
       
    23 #include "crequestshandler.h"
       
    24 #include "debug.h"
       
    25 
       
    26 const TUint KOneByte = 8; // for shifting data to one byte
       
    27 
       
    28 // binary constant for checking if bit 7 is set
       
    29 const TUint KUsbDevConBit7 = 0x80;
       
    30 
       
    31 const TUint KLengthLoByte = 6;
       
    32 const TUint KLengthHiByte = 7;
       
    33         
       
    34 // ---------------------------------------------------------------------------
       
    35 // Two-phase construction
       
    36 // ---------------------------------------------------------------------------
       
    37 //
       
    38 CStateMachine* CStateMachine::NewL(CRequestsHandler& aRequestsHandler, RDevUsbcClient& iLdd)
       
    39     {
       
    40     FLOG( _L( "[USBDEVCON]\tCStateMachine::NewL" ) );
       
    41     
       
    42     CStateMachine* self = new (ELeave) CStateMachine(aRequestsHandler, iLdd);
       
    43     CleanupStack::PushL(self);
       
    44     self->ConstructL();
       
    45     CleanupStack::Pop(self);
       
    46     return self;    
       
    47     }
       
    48     
       
    49 // ---------------------------------------------------------------------------
       
    50 // Two-phase constructon
       
    51 // ---------------------------------------------------------------------------
       
    52 //
       
    53 void CStateMachine::ConstructL()
       
    54     {
       
    55     
       
    56     FLOG( _L( "[USBDEVCON]\tCStateMachine::ConstructL" ) );
       
    57     
       
    58     iBuffer.CreateL(0); // later will be reallocated with required size
       
    59     
       
    60     // reader
       
    61     iEP0Reader = CEP0Reader::NewL(*this,iLdd);
       
    62     
       
    63     // writer
       
    64     iEP0Writer = CEP0Writer::NewL(*this, iLdd);
       
    65     
       
    66     }
       
    67     
       
    68 // ---------------------------------------------------------------------------
       
    69 // Default construction
       
    70 // ---------------------------------------------------------------------------
       
    71 //
       
    72 CStateMachine::CStateMachine(CRequestsHandler& aRequestsHandler, RDevUsbcClient& aLdd) : 
       
    73                             iRequestsHandler(aRequestsHandler),
       
    74                             iLdd (aLdd),
       
    75                             iState(ENoneState)
       
    76     {
       
    77     }
       
    78 
       
    79 // ---------------------------------------------------------------------------
       
    80 // Destruction
       
    81 // ---------------------------------------------------------------------------
       
    82 //
       
    83 CStateMachine::~CStateMachine()
       
    84     {
       
    85     delete iEP0Writer;
       
    86     delete iEP0Reader;
       
    87     iBuffer.Close();    
       
    88     }   
       
    89 
       
    90 // ---------------------------------------------------------------------------
       
    91 // Starts state machine
       
    92 // ---------------------------------------------------------------------------
       
    93 //
       
    94 void CStateMachine::Start()
       
    95     {
       
    96     
       
    97     FLOG( _L( "[USBDEVCON]\tCStateMachine::Start" ) );
       
    98     
       
    99     // resetting, if it's been already started
       
   100     if(IsStarted())
       
   101         {
       
   102         Stop();
       
   103         }
       
   104     
       
   105     iState = ESetupStage;   
       
   106     iEP0Reader->ReadSetupPacket();
       
   107                 
       
   108     }
       
   109     
       
   110 // ---------------------------------------------------------------------------
       
   111 // Stops machine
       
   112 // ---------------------------------------------------------------------------
       
   113 //
       
   114 void CStateMachine::Stop()
       
   115     {
       
   116     
       
   117     FLOG( _L( "[USBDEVCON]\tCStateMachine::Stop" ) );
       
   118         
       
   119     iEP0Reader->Cancel();
       
   120     iEP0Writer->Cancel();
       
   121     
       
   122     iState = ENoneState;
       
   123     
       
   124     }
       
   125     
       
   126 // ---------------------------------------------------------------------------
       
   127 // ETrue if state machine is started
       
   128 // ---------------------------------------------------------------------------
       
   129 //
       
   130 TInt CStateMachine::IsStarted() const
       
   131     {
       
   132     
       
   133     if(ENoneState == iState)
       
   134         {
       
   135         FLOG( _L( "[USBDEVCON]\tCStateMachine::IsStarted == EFalse" ) );
       
   136         return EFalse;
       
   137         }
       
   138         else
       
   139         {
       
   140         FLOG( _L( "[USBDEVCON]\tCStateMachine::IsStarted == ETrue" ) );
       
   141         }
       
   142     return ETrue;
       
   143     }
       
   144     
       
   145 // ---------------------------------------------------------------------------
       
   146 // Something has been read from EP0 
       
   147 // ---------------------------------------------------------------------------
       
   148 //
       
   149 void CStateMachine::ReadEP0(RBuf8& aBuffer, const TRequestStatus& aStatus)
       
   150     {
       
   151     
       
   152     FTRACE(FPrint(
       
   153             _L("[USBDEVCON]\tCStateMachine::ReadEP0: BufferLength = %d, aStatus = %d" ),aBuffer.Length(), aStatus.Int()));
       
   154     
       
   155     // all errors while reading data lead to idle state (ESetupStage in our case)
       
   156     if(KErrNone != aStatus.Int())
       
   157         {
       
   158         // restart
       
   159         Start();
       
   160         return;
       
   161         }
       
   162     
       
   163     TInt err(KErrNone);
       
   164     
       
   165     switch(iState)
       
   166         {
       
   167         case ESetupStage: // setup transaction received
       
   168             {
       
   169             
       
   170             FLOG( _L( "[USBDEVCON]\tCStateMachine::ReadEP0 processing ESetupStage" ) );
       
   171             
       
   172             if(aBuffer.Length() != KSetupPacketLength) // SetupPacket is always 8 bytes
       
   173                 {
       
   174                 Start();
       
   175                 return;
       
   176                 }
       
   177             
       
   178             ProcessSetupPacket(aBuffer);
       
   179                                 
       
   180             break;
       
   181             }
       
   182         case EDataStage:  // some data received from host. This data is required for the request, received on setup stage
       
   183             {
       
   184             
       
   185             FLOG( _L( "[USBDEVCON]\tCStateMachine::ReadEP0 processing EDataStage" ) );
       
   186 
       
   187             // handle request, providing data together with request descriptor
       
   188             // iBuffer == request, aBuffer == data from host
       
   189             err = iRequestsHandler.Handle(iBuffer, aBuffer);
       
   190             
       
   191             if(KErrNone != err) // some error happened while handling request
       
   192                 {
       
   193                 iLdd.EndpointZeroRequestError(); // stall EP0
       
   194                 }
       
   195                 else
       
   196                 {
       
   197                 // send Status Packet, indicating that we received request, data, and handled request. OK
       
   198                 iLdd.SendEp0StatusPacket();
       
   199                 }
       
   200             
       
   201             // all done, go to idle state
       
   202             iState = ESetupStage;
       
   203             iEP0Reader->ReadSetupPacket();
       
   204             
       
   205             break;
       
   206             
       
   207             }
       
   208         default:
       
   209             {
       
   210             FLOG( _L( "[USBDEVCON]\tCStateMachine::ReadEP0 processing ***NOT_DEFINED state***" ) );
       
   211             }
       
   212             
       
   213         }
       
   214     
       
   215     }
       
   216 
       
   217 // ---------------------------------------------------------------------------
       
   218 // Processing setup packet 
       
   219 // ---------------------------------------------------------------------------
       
   220 //
       
   221 void CStateMachine::ProcessSetupPacket(RBuf8& aSetupPacket)
       
   222     {
       
   223     
       
   224     FLOG( _L( "[USBDEVCON]\tCStateMachine::ProcessSetupPacket" ) );
       
   225     
       
   226     TUint datalength(0); // data length, to be received from host
       
   227             
       
   228     if(IsDataFromHostRequired(aSetupPacket, datalength)) // then goes to data stage
       
   229         {
       
   230         
       
   231         FTRACE(FPrint(
       
   232             _L("[USBDEVCON]\tCStateMachine::ProcessSetupPacket. Data from host is required: %d bytes" ),datalength));
       
   233 
       
   234         // save request, until receiving following data
       
   235         iBuffer.Close();
       
   236         iBuffer.Create(aSetupPacket);
       
   237                 
       
   238         // switch to Data state
       
   239         iState = EDataStage;
       
   240         iEP0Reader->Read(datalength);
       
   241     
       
   242         return;
       
   243         
       
   244         }
       
   245             
       
   246     TInt err(KErrNone); 
       
   247                         
       
   248     // Handle request. It does not require data from host   
       
   249     // aSetupPacket == request, iBuffer = result of the request
       
   250     err = iRequestsHandler.Handle(aSetupPacket, iBuffer);
       
   251         
       
   252     if(KErrNone != err) // some error happened while handling request
       
   253         {
       
   254         
       
   255         FTRACE(FPrint(
       
   256             _L("[USBDEVCON]\tCStateMachine::ProcessSetupPacket. Error while handling request, errcode: %d" ), err));
       
   257 
       
   258         iLdd.EndpointZeroRequestError(); // stall EP0
       
   259             
       
   260         // listen to EP0
       
   261         iState = ESetupStage;
       
   262         iEP0Reader->ReadSetupPacket();
       
   263         
       
   264         return;
       
   265         }
       
   266             
       
   267     // send response, size of datalength
       
   268     if(IsDataFromDeviceRequired(aSetupPacket, datalength))
       
   269         {
       
   270         
       
   271         FTRACE(FPrint(
       
   272           _L("[USBDEVCON]\tCStateMachine::ProcessSetupPacket. Data from device is required: %d bytes" ),datalength));
       
   273 
       
   274         iState = EStatusStage;
       
   275         iEP0Writer->Write(iBuffer, datalength);
       
   276         
       
   277         return;
       
   278                 
       
   279         }
       
   280     
       
   281     // status stage 
       
   282     iLdd.SendEp0StatusPacket();
       
   283                 
       
   284     // all is done, listen to EP0, in setup stage
       
   285     iState = ESetupStage;
       
   286     iEP0Reader->ReadSetupPacket();
       
   287     
       
   288     }
       
   289     
       
   290 
       
   291 // ---------------------------------------------------------------------------
       
   292 // Something has been written to EP0 
       
   293 // ---------------------------------------------------------------------------
       
   294 //
       
   295 void CStateMachine::WroteEP0(const TRequestStatus& aStatus)
       
   296     {
       
   297     
       
   298     FTRACE(FPrint(
       
   299             _L("[USBDEVCON]\tCStateMachine::WroteEP0: iStatus = %d" ), aStatus.Int()));
       
   300 
       
   301     // all errors while writing data lead to idle state (ESetupStage in our case)
       
   302     if(KErrNone != aStatus.Int())
       
   303         {
       
   304         // restart
       
   305         Start();
       
   306         }
       
   307         
       
   308     switch(iState)
       
   309         {
       
   310         case EStatusStage:
       
   311             {
       
   312             
       
   313             FLOG( _L( "[USBDEVCON]\tCStateMachine::WroteEP0 EStatusStage -> ESetupStage" ) );
       
   314             
       
   315             // successfully wrote data to EP0
       
   316             // go to idle
       
   317             iState = ESetupStage;
       
   318             iEP0Reader->ReadSetupPacket();
       
   319             
       
   320             break;
       
   321             }
       
   322         default:
       
   323             {
       
   324             FLOG( _L( "[USBDEVCON]\tCStateMachine::WroteEP0 ***ENoneState***" ) );
       
   325             }
       
   326         }
       
   327     }
       
   328 
       
   329 // ---------------------------------------------------------------------------
       
   330 // ETrue if data required to be send from host 
       
   331 // ---------------------------------------------------------------------------
       
   332 //
       
   333 TBool CStateMachine::IsDataFromHostRequired(const RBuf8& aSetupPacket, TUint& aDataLength) const
       
   334     {
       
   335         // bits 6 and 7 of SetupPacket contain wLength - length of data in Data stage  
       
   336         aDataLength = static_cast<TUint16>(aSetupPacket[KLengthLoByte] |
       
   337                                             (aSetupPacket[KLengthHiByte] << KOneByte));
       
   338         if(0 == aDataLength)
       
   339             {
       
   340             // no data required in any direction
       
   341             return EFalse;
       
   342             }
       
   343             
       
   344         // direction of data
       
   345         // bit 7 of byte 0 of aSetupPacket (means bmRequestType one) contains:
       
   346         // 0, if no data or data goes from host to device
       
   347         // 1 means data goes from device to host
       
   348         if(KUsbDevConBit7 & aSetupPacket[0])
       
   349             { // bit 7 is set => data flow is from device to host
       
   350             return EFalse;
       
   351             }
       
   352             
       
   353         return ETrue;
       
   354         
       
   355     }
       
   356 
       
   357 // ---------------------------------------------------------------------------
       
   358 // ETrue if data required to be send to host
       
   359 // ---------------------------------------------------------------------------
       
   360 //
       
   361 TBool CStateMachine::IsDataFromDeviceRequired(const RBuf8& aSetupPacket, TUint& aDataLength) const
       
   362     {
       
   363         // bytes 6 and 7 of SetupPacket contain wLength - length of data in Data stage
       
   364         aDataLength = static_cast<TUint16>(aSetupPacket[KLengthLoByte] |
       
   365                                             (aSetupPacket[KLengthHiByte] << KOneByte));
       
   366         if(0 == aDataLength)
       
   367             {
       
   368             // no data required in any direction
       
   369             return EFalse;
       
   370             }
       
   371             
       
   372         // direction of data
       
   373         // bit 7 of byte 0 of aSetupPacket (means bmRequestType one) contains:
       
   374         // 0, if no data or data goes from host to device
       
   375         // 1 means data goes from device to host
       
   376         if(KUsbDevConBit7 & aSetupPacket[0])
       
   377             { // bit 7 is set => data flow is from device to host
       
   378             return ETrue;
       
   379             }
       
   380             
       
   381         return EFalse;
       
   382     }
       
   383 
       
   384