hti/HtiFramework/src/HtiCommAdapter.cpp
branchRCL_3
changeset 59 8ad140f3dd41
equal deleted inserted replaced
49:7fdc9a71d314 59:8ad140f3dd41
       
     1 /*
       
     2 * Copyright (c) 2009 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:  CHtiCommAdapter implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "HtiCommAdapter.h"
       
    20 #include "HtiDispatcher.h"
       
    21 #include "HtiCommPluginInterface.h"
       
    22 #include "HtiMessage.h"
       
    23 #include "HtiNotifier.h"
       
    24 
       
    25 #include "HtiLogging.h"
       
    26 
       
    27 //default value for max message size for incoming messages
       
    28 //used if value in constructor is not valid (<0)
       
    29 const static TInt KMaxMessageSize = 10 * 1024; // 10 KB
       
    30 
       
    31 const static TInt KHtiCommPriority = 2;
       
    32 
       
    33 _LIT( KCommPanic, "HtiCommAdapter" );
       
    34 
       
    35 CHtiCommAdapter* CHtiCommAdapter::NewLC( CHTICommPluginInterface* aCommPlugin,
       
    36     CHtiDispatcher* aDispatcher, TInt aMaxMsgSize )
       
    37     {
       
    38     CHtiCommAdapter* obj = new (ELeave) CHtiCommAdapter( aDispatcher,
       
    39                                         aCommPlugin, aMaxMsgSize );
       
    40     CleanupStack::PushL( obj );
       
    41     obj->ConstructL();
       
    42     return obj;
       
    43     }
       
    44 
       
    45 CHtiCommAdapter* CHtiCommAdapter::NewL( CHTICommPluginInterface* aCommPlugin,
       
    46     CHtiDispatcher* aDispatcher, TInt aMaxMsgSize )
       
    47     {
       
    48     CHtiCommAdapter* obj = NewLC( aCommPlugin, aDispatcher, aMaxMsgSize );
       
    49     CleanupStack::Pop();
       
    50     return obj;
       
    51     }
       
    52 
       
    53 CHtiCommAdapter::CHtiCommAdapter( CHtiDispatcher* aDispatcher,
       
    54                            CHTICommPluginInterface* aCommPlugin,
       
    55                            TInt aMaxMsgSize )
       
    56     :CActive( KHtiCommPriority ),
       
    57     iState( EIdle ),
       
    58     iDispatcher( aDispatcher ),
       
    59     iCommPlugin( aCommPlugin ),
       
    60     iBufferPtr( NULL, 0 ),
       
    61     iMsgToReceive( NULL ),
       
    62     iMsgToSend( NULL ),
       
    63     iMsgToSendPtr( NULL, 0 ),
       
    64     iMsgToSendOffset( 0 ),
       
    65     iSkipLength( 0 ),
       
    66     iMaxMessageSize( aMaxMsgSize > 0 ? aMaxMsgSize : KMaxMessageSize )
       
    67     {
       
    68     HTI_LOG_FORMAT( "MaxMsgSize %d", iMaxMessageSize );
       
    69     };
       
    70 
       
    71 CHtiCommAdapter::~CHtiCommAdapter()
       
    72     {
       
    73     HTI_LOG_FUNC_IN( "CHtiCommAdapter::~CHtiCommAdapter" );
       
    74     Cancel();
       
    75 
       
    76     delete iBuffer;
       
    77     delete iLeftovers;
       
    78 
       
    79     delete iMsgToReceive;
       
    80     delete iMsgToSend;
       
    81 
       
    82     HTI_LOG_FUNC_OUT( "CHtiCommAdapter::~CHtiCommAdapter" );
       
    83     }
       
    84 
       
    85 void CHtiCommAdapter::ConstructL()
       
    86     {
       
    87     HTI_LOG_FUNC_IN( "CHtiCommAdapter::ConstructL" );
       
    88 
       
    89     //allocate recommended buffer plus some extra space
       
    90     //needed to handle msg parsing
       
    91     iBuffer = HBufC8::NewL( iCommPlugin->GetReceiveBufferSize() +
       
    92                 CHtiMessage::MinHeaderSize() );
       
    93 
       
    94     iBuffer->Des().SetLength( iCommPlugin->GetReceiveBufferSize() );
       
    95     iBufferPtr.Set( iBuffer->Des().LeftTPtr(
       
    96             iCommPlugin->GetReceiveBufferSize() ) );
       
    97 
       
    98     iLeftovers = HBufC8::NewL( CHtiMessage::MinHeaderSize() );
       
    99 
       
   100     CActiveScheduler::Add( this );
       
   101     HTI_LOG_FUNC_OUT( "CHtiCommAdapter::ConstructL" );
       
   102     }
       
   103 
       
   104 void CHtiCommAdapter::ReceiveMessage()
       
   105     {
       
   106     ReceiveMessage( EFalse );
       
   107     }
       
   108 
       
   109 void CHtiCommAdapter::ReceiveMessage( TBool aContinue )
       
   110     {
       
   111     HTI_LOG_FUNC_IN( "CHtiCommAdapter::ReceiveMessage" );
       
   112 
       
   113     iState = aContinue?EReceivingCont:EReceiving;
       
   114     iCommPlugin->Receive( iBufferPtr, iStatus );
       
   115     SetActive();
       
   116 
       
   117     HTI_LOG_FUNC_OUT( "CHtiCommAdapter::ReceiveMessage" );
       
   118     }
       
   119 
       
   120 void CHtiCommAdapter::SendMessage( CHtiMessage* aMessage )
       
   121     {
       
   122     HTI_LOG_FUNC_IN( "CCommAdapter::SendMessage" );
       
   123     iState = ESending;
       
   124     if ( aMessage != NULL )
       
   125         {
       
   126         HTI_LOG_TEXT( "send first packet" );
       
   127         iMsgToSend = aMessage;
       
   128         iMsgToSendOffset = 0;
       
   129 
       
   130         //first send header
       
   131         iMsgToSendPtr.Set( iMsgToSend->Header() );
       
   132         }
       
   133     else if ( iMsgToSend != NULL )
       
   134         {
       
   135         //send next chunk
       
   136         if ( iMsgToSend->BodySize() <=
       
   137             ( iMsgToSendOffset + iCommPlugin->GetSendBufferSize() ) )
       
   138             {
       
   139             HTI_LOG_TEXT( "send remainder" );
       
   140             iMsgToSendPtr.Set( iMsgToSend->Body().Mid( iMsgToSendOffset ) );
       
   141             iMsgToSendOffset = iMsgToSend->BodySize(); //last sending
       
   142             }
       
   143         else
       
   144             {
       
   145             HTI_LOG_TEXT( "send packet" );
       
   146             iMsgToSendPtr.Set( iMsgToSend->Body().Mid( iMsgToSendOffset,
       
   147                                     iCommPlugin->GetSendBufferSize() ) );
       
   148             iMsgToSendOffset += iCommPlugin->GetSendBufferSize();
       
   149             }
       
   150         }
       
   151     else
       
   152         {
       
   153         User::Panic( KCommPanic, KErrGeneral );
       
   154         }
       
   155 
       
   156     iCommPlugin->Send( iMsgToSendPtr, iStatus );
       
   157     SetActive();
       
   158 
       
   159     HTI_LOG_FUNC_OUT( "CCommAdapter::SendMessage" );
       
   160     }
       
   161 
       
   162 void CHtiCommAdapter::Reset()
       
   163     {
       
   164     HTI_LOG_FUNC_IN( "CHtiCommAdapter::Reset" );
       
   165     Cancel();
       
   166     //receiving msg
       
   167     delete iMsgToReceive;
       
   168     iMsgToReceive = NULL;
       
   169 
       
   170     //send msg
       
   171     delete iMsgToSend;
       
   172     iMsgToSend = NULL;
       
   173     iMsgToSendOffset = 0;
       
   174 
       
   175     //empty buffers
       
   176     iLeftovers->Des().Zero();
       
   177     iBuffer->Des().Zero();
       
   178     iSkipLength = 0;
       
   179 
       
   180     //set idle
       
   181     iState = EIdle;
       
   182 
       
   183     HTI_LOG_FUNC_OUT( "CHtiCommAdapter::Reset" );
       
   184     }
       
   185 
       
   186 void CHtiCommAdapter::RunL()
       
   187     {
       
   188     HTI_LOG_FUNC_IN( "CHtiCommAdapter::RunL" );
       
   189     HTI_LOG_FORMAT( "iStatus %d", iStatus.Int() );
       
   190 
       
   191     //handle reset code common for all states
       
   192     if ( iStatus == KErrComModuleReset )
       
   193         {
       
   194         HTI_LOG_TEXT( "Reset received from comm module" );
       
   195         //reset dispatcher
       
   196         iDispatcher->Reset();
       
   197         //dispatcher will call Reset() for comm adapters and reissue requests
       
   198         return;
       
   199         }
       
   200 
       
   201     //check for critical (unrecoverable) communication errors
       
   202     else if ( iStatus == KErrServerTerminated )
       
   203         {
       
   204         if ( iDispatcher->GetShowErrorDialogs() )
       
   205             {
       
   206             User::Panic( KCommPanic, KErrServerTerminated );
       
   207             }
       
   208         else
       
   209             {
       
   210             User::Exit( KErrServerTerminated );
       
   211             }
       
   212         }
       
   213     // USB errors from d32usbc.h
       
   214     else if ( -6700 > iStatus.Int() && iStatus.Int() > -6712 )
       
   215         {
       
   216         if(iDispatcher->CommReconnect())
       
   217             {
       
   218             return;
       
   219             }
       
   220 
       
   221         if ( iDispatcher->GetShowErrorDialogs() )
       
   222             {
       
   223             TBuf<48> errorText;
       
   224             errorText.Append( _L( "USB connection lost (" ) );
       
   225             errorText.AppendNum( iStatus.Int() );
       
   226             errorText.Append( _L( "). HTI stopped." ) );
       
   227             CHtiNotifier::ShowErrorL( errorText );
       
   228             }
       
   229         User::Exit( KErrDisconnected );
       
   230         }
       
   231     else if ( iStatus == KErrDisconnected )
       
   232         {
       
   233         // This happens if Bluetooth connection is lost.
       
   234         if(iDispatcher->CommReconnect())
       
   235             {
       
   236             return;
       
   237             }
       
   238         
       
   239         if ( iDispatcher->GetShowErrorDialogs() )
       
   240             {
       
   241             CHtiNotifier::ShowErrorL(
       
   242                     _L( "Connection lost. HTI stopped." ), KErrDisconnected );
       
   243             }
       
   244         User::Exit( KErrDisconnected );
       
   245         }
       
   246 
       
   247     switch ( iState )
       
   248         {
       
   249         case EReceiving:
       
   250             //process
       
   251             HTI_LOG_TEXT( "EReceiving" );
       
   252             if ( iStatus == KErrNone )
       
   253                 {
       
   254                 iBuffer->Des().SetLength( iBufferPtr.Length() );
       
   255                 TRAPD( err, HandleReceiveL() );
       
   256                 if ( err != KErrNone )
       
   257                     {
       
   258                     HTI_LOG_FORMAT( "Error in HandleReceiveL %d", err );
       
   259 
       
   260                     delete iMsgToReceive;
       
   261                     iMsgToReceive = NULL;
       
   262 
       
   263                     iDispatcher->Notify( err );
       
   264 
       
   265                     ReceiveMessage();
       
   266                     }
       
   267                 }
       
   268             else
       
   269                 {
       
   270                 HTI_LOG_FORMAT( "Error %d, reissue request", iStatus.Int() );
       
   271                 iDispatcher->Notify( iStatus.Int() );
       
   272                 User::After(2000000);
       
   273                 ReceiveMessage();
       
   274                 }
       
   275             break;
       
   276         case EReceivingCont:
       
   277             //process
       
   278             HTI_LOG_TEXT( "EReceivingCont" );
       
   279             if ( iStatus == KErrNone )
       
   280                 {
       
   281                 iBuffer->Des().SetLength( iBufferPtr.Length() );
       
   282                 TRAPD( err, HandleReceiveContL() );
       
   283                 if ( err != KErrNone )
       
   284                     {
       
   285                     HTI_LOG_FORMAT( "Error in HandleReceiveContL %d", err );
       
   286 
       
   287                     delete iMsgToReceive;
       
   288                     iMsgToReceive = NULL;
       
   289 
       
   290                     iDispatcher->Notify( err );
       
   291 
       
   292                     ReceiveMessage();
       
   293                     }
       
   294                 }
       
   295             else
       
   296                 {
       
   297                 HTI_LOG_FORMAT( "Error %d, reissue request", iStatus.Int() );
       
   298                 HTI_LOG_TEXT( "and dismiss received HTI msg beginning" );
       
   299                 delete iMsgToReceive;
       
   300                 iMsgToReceive = NULL;
       
   301 
       
   302                 iDispatcher->Notify( iStatus.Int() );
       
   303                 User::After(2000000);
       
   304                 ReceiveMessage();
       
   305                 }
       
   306             break;
       
   307         case ESending:
       
   308             HTI_LOG_TEXT( "ESending" );
       
   309             if ( iStatus == KErrNone )
       
   310                 {
       
   311                 HandleSend();
       
   312                 }
       
   313             else
       
   314                 {
       
   315                 HTI_LOG_FORMAT( "Error %d, stop sending, go to EIdle", iStatus.Int() );
       
   316 
       
   317                 delete iMsgToSend;
       
   318                 iMsgToSend = NULL;
       
   319                 iMsgToSendOffset = 0;
       
   320                 iState = EIdle;
       
   321 
       
   322                 iDispatcher->Notify( iStatus.Int() );
       
   323                 }
       
   324             break;
       
   325         case EIdle:
       
   326             //ERROR
       
   327             HTI_LOG_TEXT( "EIdle" );
       
   328             User::Panic( KCommPanic, KErrGeneral );
       
   329             break;
       
   330         default:
       
   331             //ERROR
       
   332             HTI_LOG_TEXT( "default" );
       
   333             User::Panic( KCommPanic, KErrGeneral );
       
   334         };
       
   335 
       
   336     HTI_LOG_FUNC_OUT( "CHtiCommAdapter::RunL" );
       
   337     }
       
   338 
       
   339 void CHtiCommAdapter::HandleReceiveL()
       
   340     {
       
   341     TBool toContinue = EFalse; //for reissuing state
       
   342 
       
   343     if ( iLeftovers->Length() > 0 )
       
   344         {
       
   345         HTI_LOG_TEXT( "handle leftovers" );
       
   346         //if something left from previous time
       
   347         //insert it to the buffer beggining
       
   348         //check that there is enough space (should be)
       
   349         if ( iBuffer->Length() + iLeftovers->Length() <=
       
   350              iBuffer->Des().MaxLength() )
       
   351             {
       
   352             iBuffer->Des().Insert( 0, *iLeftovers );
       
   353             }
       
   354         else
       
   355             {
       
   356             //error
       
   357             HTI_LOG_TEXT( "iMsgToReceive contain too much leftovers, drop them" );
       
   358             }
       
   359 
       
   360         iLeftovers->Des().Zero();
       
   361         }
       
   362 
       
   363     //use loop cause can be several HTI messages in iBuffer
       
   364     while ( iBuffer->Length() >= CHtiMessage::MinHeaderSize() )
       
   365         {
       
   366         HTI_LOG_FORMAT( "iBuffer.Length() %d", iBuffer->Length() );
       
   367         HTI_LOG_TEXT( "check header" );
       
   368 
       
   369         if ( CHtiMessage::CheckValidHtiHeader( *iBuffer ) )
       
   370             {
       
   371             HTI_LOG_TEXT( "valid header" );
       
   372             TInt msgSize = CHtiMessage::Size( *iBuffer );
       
   373             HTI_LOG_FORMAT( "msgSize %d", msgSize );
       
   374             HTI_LOG_HEX( iBuffer->Des().Ptr(), 14 );
       
   375 
       
   376             __ASSERT_ALWAYS( iMsgToReceive == NULL,
       
   377                             User::Panic( KCommPanic, KErrGeneral ) );
       
   378 
       
   379             //check msgSize is acceptable
       
   380             if ( msgSize > iMaxMessageSize )
       
   381                 {
       
   382                 //send err message
       
   383                 iDispatcher->DispatchOutgoingErrorMessage( EHtiErrBigMessage );
       
   384                 iSkipLength = msgSize;
       
   385                 }
       
   386             else if ( msgSize > iDispatcher->GetFreeMemory() )
       
   387                 {
       
   388                 //send err message
       
   389                 iDispatcher->DispatchOutgoingErrorMessage( EHtiErrNoMemory );
       
   390                 iSkipLength = msgSize;
       
   391                 }
       
   392             else
       
   393                 {
       
   394                 iMsgToReceive = CHtiMessage::NewL( *iBuffer );
       
   395                 }
       
   396 
       
   397             if ( iMsgToReceive == NULL )
       
   398                 {
       
   399                 HTI_LOG_TEXT( "skip message" );
       
   400                 if ( iSkipLength > iBuffer->Length() )
       
   401                     {
       
   402                     iSkipLength -= iBuffer->Length();
       
   403                     iBuffer->Des().Zero();
       
   404                     // and continue to receive HTI message
       
   405                     //switch state to EReceiveCont
       
   406                     toContinue = ETrue;
       
   407                     }
       
   408                 else
       
   409                     {
       
   410                     //should not be here, but just in case
       
   411                     iBuffer->Des().Delete( 0, iSkipLength );
       
   412                     iSkipLength = 0;
       
   413                     }
       
   414                 }
       
   415             else if ( iMsgToReceive->IsBodyComplete() )
       
   416                 {
       
   417                 HTI_LOG_TEXT( "handle small message" );
       
   418                 //iMsgToReceive is already contain whole HTI message
       
   419                 iDispatcher->DispatchIncomingMessage( iMsgToReceive );
       
   420 
       
   421                 //delete sent msg from buffer (and procces the rest)
       
   422                 iBuffer->Des().Delete( 0, iMsgToReceive->Size() );
       
   423                 iMsgToReceive = NULL;
       
   424                 }
       
   425             else
       
   426                 {
       
   427                 HTI_LOG_TEXT( "start receive big message" );
       
   428                 //clear it to leave loop
       
   429                 iBuffer->Des().Zero();
       
   430                 // and continue to receive HTI message
       
   431                 //switch state to EReceiveCont
       
   432                 toContinue = ETrue;
       
   433                 }
       
   434             }
       
   435         else
       
   436             {
       
   437             HTI_LOG_TEXT( "invalid header, dismiss buffer and reissue request" );
       
   438             HTI_LOG_HEX( iBuffer->Des().Ptr(), 14 );
       
   439             // clear all buffer cause don't know where is valid header starts
       
   440             // and wait for the next packet
       
   441             iBuffer->Des().Zero(); //to leave loop
       
   442             }
       
   443         }
       
   444 
       
   445     if ( iBuffer->Length() > 0 )
       
   446         {
       
   447         HTI_LOG_FORMAT( "copy leftovers %d", iBuffer->Length() );
       
   448         //header beggining left in the buffer
       
   449         //copy it to iLeftovers
       
   450         ( *iLeftovers ) = ( *iBuffer );
       
   451         }
       
   452     //reissue request
       
   453     ReceiveMessage( toContinue );
       
   454     }
       
   455 
       
   456 void CHtiCommAdapter::HandleReceiveContL()
       
   457     {
       
   458     HTI_LOG_FORMAT( "In buffer: %d", iBuffer->Length() );
       
   459     TBool toContinue = ETrue;
       
   460 
       
   461     if ( iMsgToReceive != NULL && iSkipLength == 0 )
       
   462         {
       
   463         //add new packet to a message
       
   464         TInt copyLen = iMsgToReceive->AddToBody( *iBuffer );
       
   465 
       
   466         //delete what we copied to the message
       
   467         iBuffer->Des().Delete( 0, copyLen );
       
   468 
       
   469         if ( iMsgToReceive->IsBodyComplete() )
       
   470             {
       
   471             HTI_LOG_FORMAT( "body complete: %d", iMsgToReceive->BodySize() );
       
   472 
       
   473             iDispatcher->DispatchIncomingMessage( iMsgToReceive );
       
   474             iMsgToReceive = NULL;
       
   475 
       
   476             //message complete, go to state EReceive
       
   477             toContinue = EFalse;
       
   478             }
       
   479         }
       
   480     else if ( iSkipLength > 0 )
       
   481         {
       
   482         HTI_LOG_FORMAT( "continue skipping, remains %d", iSkipLength );
       
   483 
       
   484         if ( iSkipLength > iBuffer->Length() )
       
   485             {
       
   486             iSkipLength -= iBuffer->Length();
       
   487             iBuffer->Des().Zero();
       
   488             }
       
   489         else
       
   490             {
       
   491             //skip last bytes
       
   492             iBuffer->Des().Delete( 0, iSkipLength );
       
   493             iSkipLength = 0;
       
   494 
       
   495             toContinue = EFalse;
       
   496             }
       
   497         }
       
   498 
       
   499     if ( iBuffer->Length() > 0 )
       
   500         {
       
   501         //process remainder and reissue in HandleReceiveL()
       
   502         HandleReceiveL();
       
   503         }
       
   504     else
       
   505         {
       
   506         //reissue
       
   507         ReceiveMessage(toContinue);
       
   508         }
       
   509     }
       
   510 
       
   511 void CHtiCommAdapter::HandleSend()
       
   512     {
       
   513     if ( iMsgToSendOffset < iMsgToSend->BodySize() )
       
   514         {
       
   515         //reissuse sending
       
   516         SendMessage( NULL );
       
   517         }
       
   518     else
       
   519         {
       
   520         HTI_LOG_TEXT( "finish send, go to idle" );
       
   521         iMsgToSendOffset = 0;
       
   522         delete iMsgToSend;
       
   523         iMsgToSend = NULL;
       
   524         iState = EIdle;
       
   525         iDispatcher->Notify( KErrNone );
       
   526         }
       
   527     }
       
   528 
       
   529 TInt CHtiCommAdapter::RunError( TInt aError )
       
   530     {
       
   531     TInt err = aError;
       
   532     switch ( aError )
       
   533         {
       
   534         case KErrNoMemory:
       
   535             break;
       
   536         case KErrNotFound:
       
   537             break;
       
   538         }
       
   539     return err;
       
   540     }
       
   541 
       
   542 void CHtiCommAdapter::DoCancel()
       
   543     {
       
   544     switch ( iState )
       
   545         {
       
   546         case EReceiving:
       
   547         case EReceivingCont:
       
   548             iCommPlugin->CancelReceive();
       
   549             break;
       
   550         case ESending:
       
   551             iCommPlugin->CancelSend();
       
   552             break;
       
   553         default:
       
   554             break;
       
   555         }
       
   556     }