localconnectivityservice/dun/atext/src/DunAtCmdPusher.cpp
changeset 0 c3e98f10fcf4
child 7 a2f12998bb04
equal deleted inserted replaced
-1:000000000000 0:c3e98f10fcf4
       
     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:  AT command pusher for downstream
       
    15 *
       
    16 */
       
    17 
       
    18 /*
       
    19  * Filtering categories for multiple commands on one line (DunAtCmdPusher.cpp)
       
    20  * (here "OTHER" reply means a reply which is something else than "OK" and "ERROR")
       
    21  * One reply:     OK           -> OK
       
    22  * One reply:     OTHER        -> OTHER
       
    23  * One reply:     ERROR        -> ERROR
       
    24  * Two replies:   OK, OK       -> OK
       
    25  * Two replies:   OTHER, OTHER -> OTHER, OTHER
       
    26  * Two replies:   OK, OTHER    -> OTHER
       
    27  * Two replies:   OTHER, OK    -> OTHER
       
    28  * Two replies:   OK, ERROR    -> ERROR
       
    29  * Two replies:   OTHER, ERROR -> OTHER, ERROR
       
    30  * Note: "OK" replies are skipped. The "OK" string is stripped from the "OTHER"
       
    31  * replies and manually added the the downstream as the last operation if either
       
    32  * "OK" or "OTHER" was received before.
       
    33  */
       
    34 
       
    35 #include "DunAtCmdPusher.h"
       
    36 #include "DunDownstream.h"
       
    37 #include "DunDebug.h"
       
    38 
       
    39 // ---------------------------------------------------------------------------
       
    40 // Two-phased constructor.
       
    41 // ---------------------------------------------------------------------------
       
    42 //
       
    43 CDunAtCmdPusher* CDunAtCmdPusher::NewL(
       
    44     RATExt* aAtCmdExt,
       
    45     MDunAtCmdPusher* aCallback,
       
    46     MDunStreamManipulator* aDownstream,
       
    47     TDesC8* aOkBuffer )
       
    48     {
       
    49     CDunAtCmdPusher* self = NewLC( aAtCmdExt,
       
    50                                    aCallback,
       
    51                                    aDownstream,
       
    52                                    aOkBuffer );
       
    53     CleanupStack::Pop( self );
       
    54     return self;
       
    55     }
       
    56 
       
    57 // ---------------------------------------------------------------------------
       
    58 // Two-phased constructor.
       
    59 // ---------------------------------------------------------------------------
       
    60 //
       
    61 CDunAtCmdPusher* CDunAtCmdPusher::NewLC(
       
    62     RATExt* aAtCmdExt,
       
    63     MDunAtCmdPusher* aCallback,
       
    64     MDunStreamManipulator* aDownstream,
       
    65     TDesC8* aOkBuffer )
       
    66     {
       
    67     CDunAtCmdPusher* self = new (ELeave) CDunAtCmdPusher( aAtCmdExt,
       
    68                                                           aCallback,
       
    69                                                           aDownstream,
       
    70                                                           aOkBuffer );
       
    71     CleanupStack::PushL( self );
       
    72     self->ConstructL();
       
    73     return self;
       
    74     }
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // Destructor.
       
    78 // ---------------------------------------------------------------------------
       
    79 //
       
    80 CDunAtCmdPusher::~CDunAtCmdPusher()
       
    81     {
       
    82     FTRACE(FPrint( _L("CDunAtCmdPusher::~CDunAtCmdPusher()") ));
       
    83     ResetData();
       
    84     FTRACE(FPrint( _L("CDunAtCmdPusher::~CDunAtCmdPusher() complete") ));
       
    85     }
       
    86 
       
    87 // ---------------------------------------------------------------------------
       
    88 // Resets data to initial values
       
    89 // ---------------------------------------------------------------------------
       
    90 //
       
    91 void CDunAtCmdPusher::ResetData()
       
    92     {
       
    93     FTRACE(FPrint( _L("CDunAtCmdPusher::ResetData()") ));
       
    94     // APIs affecting this:
       
    95     // IssueRequest()
       
    96     Stop();
       
    97     // Internal
       
    98     Initialize();
       
    99     FTRACE(FPrint( _L("CDunAtCmdPusher::ResetData() complete") ));
       
   100     }
       
   101 
       
   102 // ---------------------------------------------------------------------------
       
   103 // Starts AT command handling
       
   104 // ---------------------------------------------------------------------------
       
   105 //
       
   106 TInt CDunAtCmdPusher::IssueRequest( TDesC8& aCommand )
       
   107     {
       
   108     FTRACE(FPrint( _L("CDunAtCmdPusher::IssueRequest()") ));
       
   109     FTRACE(FPrint( _L("CDunAtCmdPusher::IssueRequest() send ATEXT:") ));
       
   110     FTRACE(FPrintRaw(aCommand) );
       
   111     if ( iAtPushState != EDunStateIdle )
       
   112         {
       
   113         FTRACE(FPrint( _L("CDunAtCmdPusher::IssueRequest() (not ready) complete") ));
       
   114         return KErrNotReady;
       
   115         }
       
   116     iStatus = KRequestPending;
       
   117     iAtCmdExt->HandleCommand( iStatus,
       
   118                               aCommand,
       
   119                               iRecvBuffer,
       
   120                               iReplyLeftPckg,
       
   121                               iReplyTypePckg );
       
   122     SetActive();
       
   123     iAtPushState = EDunStateAtCmdPushing;
       
   124     FTRACE(FPrint( _L("CDunAtCmdPusher::IssueRequest() complete") ));
       
   125     return KErrNone;
       
   126     }
       
   127 
       
   128 // ---------------------------------------------------------------------------
       
   129 // Stops AT command handling
       
   130 // ---------------------------------------------------------------------------
       
   131 //
       
   132 TInt CDunAtCmdPusher::Stop()
       
   133     {
       
   134     FTRACE(FPrint( _L("CDunAtCmdPusher::Stop()") ));
       
   135     if ( iAtPushState != EDunStateAtCmdPushing )
       
   136         {
       
   137         FTRACE(FPrint( _L("CDunAtCmdHandler::Stop() (not ready) complete" )));
       
   138         return KErrNotReady;
       
   139         }
       
   140     // As the EDunStateAtCmdHandling can be set even when the actual request
       
   141     // has completed (when replying with NotifyDataPushComplete() and setting
       
   142     // idle eventually), cancel the actual operation in DoCancel()
       
   143     Cancel();
       
   144     iAtPushState = EDunStateIdle;
       
   145     SetEndOfCmdLine();
       
   146     FTRACE(FPrint( _L("CDunAtCmdPusher::Stop() complete") ));
       
   147     return KErrNone;
       
   148     }
       
   149 
       
   150 // ---------------------------------------------------------------------------
       
   151 // Manages request to abort command handling
       
   152 // ---------------------------------------------------------------------------
       
   153 //
       
   154 TInt CDunAtCmdPusher::ManageAbortRequest()
       
   155     {
       
   156     FTRACE(FPrint( _L("CDunAtCmdPusher::ManageAbortRequest()") ));
       
   157     if ( iAtPushState != EDunStateAtCmdPushing )
       
   158         {
       
   159         FTRACE(FPrint( _L("CDunAtCmdHandler::ManageAbortRequest() (not ready) complete" )));
       
   160         return KErrNotReady;
       
   161         }
       
   162     if ( iCmdAbort )
       
   163         {
       
   164         FTRACE(FPrint( _L("CDunAtCmdPusher::ManageAbortRequest() (already exists) complete") ));
       
   165         return KErrAlreadyExists;
       
   166         }
       
   167     TInt retTemp = iAtCmdExt->ReportHandleCommandAbort( iStop );
       
   168     if ( retTemp != KErrNone )
       
   169         {
       
   170         FTRACE(FPrint( _L("CDunAtCmdPusher::ManageAbortRequest() (ERROR) complete") ));
       
   171         return retTemp;
       
   172         }
       
   173     iCmdAbort = ETrue;
       
   174     FTRACE(FPrint( _L("CDunAtCmdPusher::ManageAbortRequest() complete") ));
       
   175     return KErrNone;
       
   176     }
       
   177 
       
   178 // ---------------------------------------------------------------------------
       
   179 // Sets end of command line marker on for the possible series of AT commands.
       
   180 // ---------------------------------------------------------------------------
       
   181 //
       
   182 void CDunAtCmdPusher::SetEndOfCmdLine()
       
   183     {
       
   184     FTRACE(FPrint( _L("CDunAtCmdPusher::SetEndOfCmdLine()") ));
       
   185     iNoErrorReceived = EFalse;
       
   186     iLastOkPush = EFalse;
       
   187     iCmdAbort = EFalse;
       
   188     iStop = EFalse;
       
   189     FTRACE(FPrint( _L("CDunAtCmdPusher::SetEndOfCmdLine() complete") ));
       
   190     }
       
   191 
       
   192 // ---------------------------------------------------------------------------
       
   193 // CDunAtCmdPusher::CDunAtCmdPusher
       
   194 // ---------------------------------------------------------------------------
       
   195 //
       
   196 CDunAtCmdPusher::CDunAtCmdPusher( RATExt* aAtCmdExt,
       
   197                                   MDunAtCmdPusher* aCallback,
       
   198                                   MDunStreamManipulator* aDownstream,
       
   199                                   TDesC8* aOkBuffer ) :
       
   200     CActive( EPriorityHigh ),
       
   201     iAtCmdExt( aAtCmdExt ),
       
   202     iCallback( aCallback ),
       
   203     iDownstream( aDownstream ),
       
   204     iOkBuffer( aOkBuffer ),
       
   205     iReplyLeftPckg( iReplyBytesLeft ),
       
   206     iReplyTypePckg( iReplyType )
       
   207     {
       
   208     Initialize();
       
   209     }
       
   210 
       
   211 // ---------------------------------------------------------------------------
       
   212 // CDunAtCmdPusher::ConstructL
       
   213 // ---------------------------------------------------------------------------
       
   214 //
       
   215 void CDunAtCmdPusher::ConstructL()
       
   216     {
       
   217     FTRACE(FPrint( _L("CDunAtCmdPusher::ConstructL()") ));
       
   218     if ( !iAtCmdExt || !iCallback || !iDownstream || !iOkBuffer )
       
   219         {
       
   220         User::Leave( KErrGeneral );
       
   221         }
       
   222     CActiveScheduler::Add( this );
       
   223     FTRACE(FPrint( _L("CDunAtCmdPusher::ConstructL() complete") ));
       
   224     }
       
   225 
       
   226 // ---------------------------------------------------------------------------
       
   227 // Initializes this class
       
   228 // ---------------------------------------------------------------------------
       
   229 //
       
   230 void CDunAtCmdPusher::Initialize()
       
   231     {
       
   232     // Don't initialize iAtCmdExt here (it is set through NewL)
       
   233     // Don't initialize iCallback here (it is set through NewL)
       
   234     // Don't initialize iDownstream here (it is set through NewL)
       
   235     // Don't initialize iOkBuffer here (it is set through NewL)
       
   236     iAtPushState = EDunStateIdle;
       
   237     iReplyBytesLeft = 0;
       
   238     iReplyType = EReplyTypeUndefined;
       
   239     SetEndOfCmdLine();
       
   240     }
       
   241 
       
   242 // ---------------------------------------------------------------------------
       
   243 // Sets state to idle and notifies about subcommand handling completion
       
   244 // ---------------------------------------------------------------------------
       
   245 //
       
   246 void CDunAtCmdPusher::SetToIdleAndNotifyEnd( TInt aError )
       
   247     {
       
   248     FTRACE(FPrint( _L("CDunAtCmdPusher::SetToIdleAndNotifyEnd()") ));
       
   249     iCmdAbort = EFalse;
       
   250     iAtPushState = EDunStateIdle;
       
   251     iCallback->NotifyEndOfProcessing( aError );
       
   252     FTRACE(FPrint( _L("CDunAtCmdPusher::SetToIdleAndNotifyEnd() complete") ));
       
   253     }
       
   254 
       
   255 // ---------------------------------------------------------------------------
       
   256 // Checks if "OK" (verbose) or "0" (numeric) string or exists at the end of
       
   257 // buffer and removes it
       
   258 // ---------------------------------------------------------------------------
       
   259 //
       
   260 TInt CDunAtCmdPusher::CheckAndRemoveOkString()
       
   261     {
       
   262     FTRACE(FPrint( _L("CDunAtCmdPusher::CheckAndRemoveOkString()") ));
       
   263     TInt recvBufferLength = iRecvBuffer.Length();
       
   264     TInt okBufferLength = iOkBuffer->Length();
       
   265     if ( recvBufferLength <= okBufferLength )
       
   266         {
       
   267         FTRACE(FPrint( _L("CDunAtCmdPusher::CheckAndRemoveOkString() (ERROR) complete") ));
       
   268         return KErrGeneral;
       
   269         }
       
   270     TInt lengthWithNoOk = recvBufferLength - okBufferLength;
       
   271     TPtr8 recvBufferDes( &iRecvBuffer[lengthWithNoOk], okBufferLength, okBufferLength );
       
   272     if ( recvBufferDes.Compare(*iOkBuffer) != 0 )
       
   273         {
       
   274         FTRACE(FPrint( _L("CDunAtCmdPusher::CheckAndRemoveOkString() (not found) complete") ));
       
   275         return KErrNotFound;
       
   276         }
       
   277     iRecvBuffer.SetLength( lengthWithNoOk );
       
   278     FTRACE(FPrint( _L("CDunAtCmdPusher::CheckAndRemoveOkString() complete") ));
       
   279     return KErrNone;
       
   280     }
       
   281 
       
   282 // ---------------------------------------------------------------------------
       
   283 // Sends reply data to downstream
       
   284 // ---------------------------------------------------------------------------
       
   285 //
       
   286 void CDunAtCmdPusher::SendReplyData( TBool aRecvBuffer )
       
   287     {
       
   288     FTRACE(FPrint( _L("CDunAtCmdPusher::SendReplyData()") ));
       
   289     TDesC8* sendBuffer = &iRecvBuffer;
       
   290     if ( !aRecvBuffer )
       
   291         {
       
   292         sendBuffer = iOkBuffer;
       
   293         }
       
   294     FTRACE(FPrint( _L("CDunAtCmdPusher::SendReplyData() send reply:") ));
       
   295     FTRACE(FPrintRaw(*sendBuffer) );
       
   296     iDownstream->NotifyDataPushRequest( sendBuffer, this );
       
   297     FTRACE(FPrint( _L("CDunAtCmdPusher::SendReplyData() complete") ));
       
   298     }
       
   299 
       
   300 // ---------------------------------------------------------------------------
       
   301 // Manages change in reply type
       
   302 // ---------------------------------------------------------------------------
       
   303 //
       
   304 void CDunAtCmdPusher::ManageReplyTypeChange()
       
   305     {
       
   306     FTRACE(FPrint( _L("CDunAtCmdPusher::ManageReplyTypeChange()") ));
       
   307     switch ( iReplyType )
       
   308         {
       
   309         case EReplyTypeOther:
       
   310             {
       
   311             FTRACE(FPrint( _L("CDunAtCmdPusher::ManageReplyTypeChange() EReplyTypeOther") ));
       
   312             iNoErrorReceived = ETrue;
       
   313             CheckAndRemoveOkString();
       
   314             SendReplyData();
       
   315             }
       
   316             break;
       
   317         case EReplyTypeOk:
       
   318             {
       
   319             FTRACE(FPrint( _L("CDunAtCmdPusher::ManageReplyTypeChange() EReplyTypeOk") ));
       
   320             // Skip the "OK" replies if not last. Only push the "OK" reply at the end.
       
   321             // iStop changes it so that the we have to send the "OK" immediately and
       
   322             // only stop with NotifyDataPushComplete()
       
   323             TBool found = iCallback->NotifyNextCommandPeekRequest();
       
   324             if ( !found || iStop )
       
   325                 {
       
   326                 SendReplyData();
       
   327                 }
       
   328             else
       
   329                 {
       
   330                 iNoErrorReceived = ETrue;
       
   331                 SetToIdleAndNotifyEnd( KErrNone );
       
   332                 }
       
   333             }
       
   334             break;
       
   335         case EReplyTypeError:
       
   336             {
       
   337             FTRACE(FPrint( _L("CDunAtCmdPusher::ManageReplyTypeChange() EReplyTypeError") ));
       
   338             if ( iNoErrorReceived )
       
   339                 {
       
   340                 iAtCmdExt->ReportExternalHandleCommandError();
       
   341                 }
       
   342             SendReplyData();
       
   343             }
       
   344             break;
       
   345         default:
       
   346             {
       
   347             FTRACE(FPrint( _L("CDunAtCmdPusher::ManageReplyTypeChange() EReplyTypeUndefined") ));
       
   348             SetToIdleAndNotifyEnd( KErrNone );
       
   349             }
       
   350             break;
       
   351         }
       
   352     FTRACE(FPrint( _L("CDunAtCmdPusher::ManageReplyTypeChange() complete") ));
       
   353     }
       
   354 
       
   355 // ---------------------------------------------------------------------------
       
   356 // From class CActive.
       
   357 // Gets called when AT command handled
       
   358 // ---------------------------------------------------------------------------
       
   359 //
       
   360 void CDunAtCmdPusher::RunL()
       
   361     {
       
   362     FTRACE(FPrint( _L("CDunAtCmdPusher::RunL()") ));
       
   363     TInt retTemp = iStatus.Int();
       
   364     if ( retTemp != KErrNone )
       
   365         {
       
   366         SetToIdleAndNotifyEnd( retTemp );
       
   367         FTRACE(FPrint( _L("CDunAtCmdPusher::RunL() (ERROR) complete (%d)"), retTemp));
       
   368         return;
       
   369         }
       
   370     ManageReplyTypeChange();
       
   371     FTRACE(FPrint( _L("CDunAtCmdPusher::RunL() complete") ));
       
   372     }
       
   373 
       
   374 // ---------------------------------------------------------------------------
       
   375 // From class CActive.
       
   376 // Gets called on cancel
       
   377 // ---------------------------------------------------------------------------
       
   378 //
       
   379 void CDunAtCmdPusher::DoCancel()
       
   380     {
       
   381     FTRACE(FPrint( _L("CDunAtCmdPusher::DoCancel()") ));
       
   382     iAtCmdExt->CancelHandleCommand();
       
   383     FTRACE(FPrint( _L("CDunAtCmdPusher::DoCancel() complete") ));
       
   384     }
       
   385 
       
   386 // ---------------------------------------------------------------------------
       
   387 // From class MDunCompletionReporter.
       
   388 // Gets called when data push is complete
       
   389 // ---------------------------------------------------------------------------
       
   390 //
       
   391 void CDunAtCmdPusher::NotifyDataPushComplete( TBool /*aAllPushed*/ )
       
   392     {
       
   393     FTRACE(FPrint( _L("CDunAtCmdPusher::NotifyDataPushComplete()") ));
       
   394     // First check if error or stop condition detected
       
   395     if ( iReplyType==EReplyTypeError || iStop )
       
   396         {
       
   397         iCallback->NotifyEndOfCmdLineProcessing();
       
   398         iAtPushState = EDunStateIdle;
       
   399         FTRACE(FPrint( _L("CDunAtCmdPusher::NotifyDataPushComplete() (error reply) complete") ));
       
   400         return;
       
   401         }
       
   402     // Secondly check only the case where push restart is required
       
   403     if ( iReplyType==EReplyTypeOther && iReplyBytesLeft>0 )
       
   404         {
       
   405         iAtCmdExt->GetNextPartOfReply( iRecvBuffer, iReplyBytesLeft );
       
   406         SendReplyData();
       
   407         FTRACE(FPrint( _L("CDunAtCmdPusher::NotifyDataPushComplete() (push restart) complete") ));
       
   408         return;
       
   409         }
       
   410     // Next check the case where other than "OK" and "ERROR" reply is received
       
   411     // and that is the last one in the command line. Then just send "OK".
       
   412     if ( !iLastOkPush && iReplyType==EReplyTypeOther )
       
   413         {
       
   414         TBool found = iCallback->NotifyNextCommandPeekRequest();
       
   415         if ( !found )
       
   416             {
       
   417             // Force iReplyType here to match the correct one in NotifyDataPushComplete()
       
   418             iReplyType = EReplyTypeOk;
       
   419             iLastOkPush = ETrue;
       
   420             SendReplyData( EFalse );
       
   421             FTRACE(FPrint( _L("CDunAtCmdPusher::NotifyDataPushComplete() (last OK) complete") ));
       
   422             return;
       
   423             }
       
   424         // Now the next command was found so just fall through
       
   425         }
       
   426     // As a last step just set to idle
       
   427     SetToIdleAndNotifyEnd( KErrNone );
       
   428     FTRACE(FPrint( _L("CDunAtCmdPusher::NotifyDataPushComplete() complete") ));
       
   429     }