localconnectivityservice/dun/utils/src/DunDownstream.cpp
branchRCL_3
changeset 19 0aa8cc770c8a
equal deleted inserted replaced
18:453dfc402455 19:0aa8cc770c8a
       
     1 /*
       
     2 * Copyright (c) 2009-2010 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:  Definitions needed for one "stream" of CDunTransporter
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 /*
       
    20  * TODO: When local media is of type RComm, listening on it is started with
       
    21  * RComm::NotifyDataAvailable() call. Check that USB ACM port and Irda RCOMM
       
    22  * (and any other new media in the future) behaves correctly so that when
       
    23  * RComm::ReadOneOrMore() is issued, the read is issued immediately without
       
    24  * checking for new data. If waiting for new data happens in this
       
    25  * NotifyDataAvailable/ReadOneOrMore combination, raise a defect to Symbian.
       
    26  */
       
    27 
       
    28 #include "DunTransporter.h"
       
    29 #include "DunDownstream.h"
       
    30 #include "DunUpstream.h"
       
    31 #include "DunDebug.h"
       
    32 
       
    33 // ---------------------------------------------------------------------------
       
    34 // Two-phased constructor.
       
    35 // ---------------------------------------------------------------------------
       
    36 //
       
    37 CDunDownstream* CDunDownstream::NewL( MDunTransporterUtilityAux* aUtility )
       
    38     {
       
    39     CDunDownstream* self = new (ELeave) CDunDownstream( aUtility );
       
    40     CleanupStack::PushL( self );
       
    41     self->ConstructL();
       
    42     CleanupStack::Pop( self );
       
    43     return self;
       
    44     }
       
    45 
       
    46 // ---------------------------------------------------------------------------
       
    47 // Destructor.
       
    48 // ---------------------------------------------------------------------------
       
    49 //
       
    50 CDunDownstream::~CDunDownstream()
       
    51     {
       
    52     FTRACE(FPrint( _L("CDunDownstream::~CDunDownstream()" )));
       
    53     ResetData();
       
    54     FTRACE(FPrint( _L("CDunDownstream::~CDunDownstream() complete" )));
       
    55     }
       
    56 
       
    57 // ---------------------------------------------------------------------------
       
    58 // Resets data to initial values
       
    59 // ---------------------------------------------------------------------------
       
    60 //
       
    61 void CDunDownstream::ResetData()
       
    62     {
       
    63     // APIs affecting this:
       
    64     // IssueRequest()
       
    65     Stop();
       
    66     // InitializeForDataPushing()
       
    67     delete iPushData.iDataPusher;
       
    68     iPushData.iDataPusher = NULL;
       
    69     // AddConnMonCallbackL()
       
    70     iCallbacksR.Close();
       
    71     iCallbacksW.Close();
       
    72     // AddSkippedErrorL()
       
    73     iOkErrorsR.Close();
       
    74     iOkErrorsW.Close();
       
    75     // Internal
       
    76     Initialize();
       
    77     }
       
    78 
       
    79 // ---------------------------------------------------------------------------
       
    80 // Initializes this stream for AT command notifications
       
    81 // ---------------------------------------------------------------------------
       
    82 //
       
    83 TInt CDunDownstream::InitializeForDataPushing(
       
    84     MDunAtCmdHandler* aAtCmdHandler )
       
    85     {
       
    86     FTRACE(FPrint( _L("CDunDownstream::InitializeForDataPushing()" ) ));
       
    87     if ( iPushData.iDataPusher || iPushData.iAtCmdHandler )  // optional
       
    88         {
       
    89         FTRACE(FPrint( _L("CDunDownstream::InitializeForDataPushing() (already exists) complete" ) ));
       
    90         return KErrAlreadyExists;
       
    91         }
       
    92     if ( !aAtCmdHandler )
       
    93         {
       
    94         FTRACE(FPrint( _L("CDunDownstream::InitializeForDataPushing() (aAtCmdHandler) not initialized!" ) ));
       
    95         return KErrGeneral;
       
    96         }
       
    97     CDunDataPusher* dataPusher = NULL;
       
    98     TRAPD( retTrap, dataPusher = CDunDataPusher::NewL(*this,this) );
       
    99     if ( retTrap != KErrNone )
       
   100         {
       
   101         FTRACE(FPrint( _L("CDunDownstream::InitializeForDataPushing() (trapped!) complete" ) ));
       
   102         return retTrap;
       
   103         }
       
   104     if ( iComm )
       
   105         {
       
   106         dataPusher->SetMedia( iComm );
       
   107         }
       
   108     else if ( iSocket )
       
   109         {
       
   110         dataPusher->SetMedia( iSocket );
       
   111         }
       
   112     iPushData.iDataPusher = dataPusher;
       
   113     iPushData.iAtCmdHandler = aAtCmdHandler;
       
   114     FTRACE(FPrint( _L("CDunDownstream::InitializeForDataPushing() complete" ) ));
       
   115     return KErrNone;
       
   116     }
       
   117 
       
   118 // ---------------------------------------------------------------------------
       
   119 // Checks if data is in queue
       
   120 // ---------------------------------------------------------------------------
       
   121 //
       
   122 TBool CDunDownstream::IsDataInQueue( const TDesC8* aDataToPush )
       
   123     {
       
   124     FTRACE(FPrint( _L("CDunDownstream::IsDataInQueue()" ) ));
       
   125     if ( !iPushData.iDataPusher )
       
   126         {
       
   127         FTRACE(FPrint( _L("CDunDownstream::IsDataInQueue() (iPushData.iDataPusher not initialized!) complete" )));
       
   128         return EFalse;
       
   129         }
       
   130     TInt foundIndex = iPushData.iDataPusher->FindEventFromQueue( aDataToPush );
       
   131     FTRACE(FPrint( _L("CDunDownstream::IsDataInQueue() complete" ) ));
       
   132     return ( foundIndex >= 0 ) ? ETrue : EFalse;
       
   133     }
       
   134 
       
   135 // ---------------------------------------------------------------------------
       
   136 // Adds data to event queue and starts sending if needed
       
   137 // ---------------------------------------------------------------------------
       
   138 //
       
   139 TInt CDunDownstream::AddToQueueAndSend( const TDesC8* aDataToPush,
       
   140                                         MDunCompletionReporter* aCallback )
       
   141     {
       
   142     FTRACE(FPrint( _L("CDunDownstream::AddToQueueAndSend()" ) ));
       
   143     if ( !iPushData.iDataPusher )
       
   144         {
       
   145         FTRACE(FPrint( _L("CDunDownstream::AddToQueueAndSend() (iPushData.iDataPusher not initialized!) complete" )));
       
   146         return KErrGeneral;
       
   147         }
       
   148     // Add to event queue. If something went wrong, just return
       
   149     TInt retTemp = iPushData.iDataPusher->AddToEventQueue( aDataToPush, aCallback );
       
   150     if ( retTemp != KErrNone )
       
   151         {
       
   152         FTRACE(FPrint( _L("CDunDownstream::AddToQueueAndSend() (ERROR) complete" )));
       
   153         return retTemp;
       
   154         }
       
   155     // Now push the data. If already active, push will start later, if not
       
   156     // active it will start immediately.
       
   157     iPushData.iDataPusher->SendQueuedData();
       
   158     FTRACE(FPrint( _L("CDunDownstream::AddToQueueAndSend() complete" ) ));
       
   159     return KErrNone;
       
   160     }
       
   161 
       
   162 // ---------------------------------------------------------------------------
       
   163 // Starts downstream by issuing read request
       
   164 // ---------------------------------------------------------------------------
       
   165 //
       
   166 TInt CDunDownstream::StartStream()
       
   167     {
       
   168     FTRACE(FPrint( _L("CDunDownstream::StartStream()" ) ));
       
   169     FTRACE(FPrint( _L("CDunDownstream::StartStream() (buffer=0x%08X)" ), iBufferPtr ));
       
   170     // Note: only start URC here.
       
   171     // The downstream read request is started when command mode ends.
       
   172     // This is done to make the data arrive in the correct order (reply vs.
       
   173     // data) with "ATD" command.
       
   174     TInt retVal = KErrNone;
       
   175     if ( iPushData.iAtCmdHandler )
       
   176         {
       
   177         retVal = iPushData.iAtCmdHandler->StartUrc();
       
   178         }
       
   179     FTRACE(FPrint( _L("CDunDownstream::StartStream() complete" ) ));
       
   180     return retVal;
       
   181     }
       
   182 
       
   183 // ---------------------------------------------------------------------------
       
   184 // Stops transfer for read or write endpoints
       
   185 // ---------------------------------------------------------------------------
       
   186 //
       
   187 TInt CDunDownstream::Stop( TBool aStopMplex )
       
   188     {
       
   189     FTRACE(FPrint( _L("CDunDownstream::Stop() (Dir=%d)" ), iDirection));
       
   190     if ( !iPushData.iDataPusher )
       
   191         {
       
   192         FTRACE(FPrint( _L("CDunDownstream::Stop() (iPushData.iDatapusher not initialized!) complete" )));
       
   193         return KErrGeneral;
       
   194         }
       
   195     // Stop the downstream related AT command handling functionality
       
   196     if ( aStopMplex )  // optional
       
   197         {
       
   198         if ( iPushData.iAtCmdHandler )
       
   199             {
       
   200             iPushData.iAtCmdHandler->StopAtCmdHandling();
       
   201             }
       
   202         // Stop the multiplexer separately
       
   203         iPushData.iDataPusher->Stop();
       
   204         }
       
   205     if ( iTransferState != EDunStateTransferring )
       
   206         {
       
   207         FTRACE(FPrint( _L("CDunDownstream::Stop() (not ready) complete" )));
       
   208         return KErrNotReady;
       
   209         }
       
   210     // Stop only current operation
       
   211     if ( iOperationType == EDunOperationTypeRead )
       
   212         {
       
   213         if ( iNetwork )
       
   214             {
       
   215             iNetwork->ReadCancel();
       
   216             Cancel();
       
   217             FTRACE(FPrint( _L("CDunDownstream::Stop() (Network) cancelled" )));
       
   218             }
       
   219         }
       
   220     else if ( iOperationType == EDunOperationTypeWrite )
       
   221         {
       
   222         iPushData.iDataPusher->StopOneEvent( iBufferPtr );
       
   223         }
       
   224     iTransferState = EDunStateIdle;
       
   225     FTRACE(FPrint( _L("CDunDownstream::Stop() complete" )));
       
   226     return KErrNone;
       
   227     }
       
   228 
       
   229 // ---------------------------------------------------------------------------
       
   230 // CDunDownstream::CDunDownstream
       
   231 // ---------------------------------------------------------------------------
       
   232 //
       
   233 CDunDownstream::CDunDownstream( MDunTransporterUtilityAux* aUtility ) :
       
   234     iUtility( aUtility )
       
   235     {
       
   236     Initialize();
       
   237     }
       
   238 
       
   239 // ---------------------------------------------------------------------------
       
   240 // CDunDownstream::ConstructL
       
   241 // ---------------------------------------------------------------------------
       
   242 //
       
   243 void CDunDownstream::ConstructL()
       
   244     {
       
   245     FTRACE(FPrint( _L("CDunDownstream::ConstructL()" ) ));
       
   246     if ( !iUtility )
       
   247         {
       
   248         User::Leave( KErrGeneral );
       
   249         }
       
   250     FTRACE(FPrint( _L("CDunDownstream::ConstructL() complete" ) ));
       
   251     }
       
   252 
       
   253 // ---------------------------------------------------------------------------
       
   254 // Initializes this class
       
   255 // ---------------------------------------------------------------------------
       
   256 //
       
   257 void CDunDownstream::Initialize()
       
   258     {
       
   259     // Don't initialize iUtility here (it is set through NewL)
       
   260     iPushData.iDataMode = EFalse;
       
   261     iPushData.iDataPusher = NULL;
       
   262     iPushData.iAtCmdHandler = NULL;
       
   263     }
       
   264 
       
   265 // ---------------------------------------------------------------------------
       
   266 // Issues transfer request for this stream
       
   267 // ---------------------------------------------------------------------------
       
   268 //
       
   269 TInt CDunDownstream::IssueRequest()
       
   270     {
       
   271     // Set direction
       
   272     iDirection = static_cast<TDunDirection>( EDunStreamTypeDownstream | iOperationType );
       
   273 
       
   274     FTRACE(FPrint( _L("CDunDownstream::IssueRequest() (Dir=%d)" ), iDirection));
       
   275     if ( !iPushData.iDataPusher )
       
   276         {
       
   277         FTRACE(FPrint( _L("CDunDownstream::IssueRequest() (iPushData.iDataPusher not initialized!) complete" ) ));
       
   278         return KErrGeneral;
       
   279         }
       
   280 
       
   281     if ( iTransferState != EDunStateIdle )
       
   282         {
       
   283         FTRACE(FPrint( _L("CDunDownstream::IssueRequest() (not ready) complete" ) ));
       
   284         return KErrNotReady;
       
   285         }
       
   286 
       
   287     if ( iOperationType == EDunOperationTypeRead )
       
   288         {
       
   289         iBufferPtr->SetLength( iBufferPtr->MaxLength() );
       
   290         FTRACE(FPrint( _L("CDunDownstream::IssueRequest() trying to read %d bytes... (Dir=%d)" ), iBufferPtr->Length(), iDirection));
       
   291         }
       
   292     else // iOperationType == EDunOperationTypeWrite
       
   293         {
       
   294         FTRACE(FPrint( _L("CDunDownstream::IssueRequest() writing %d bytes... (Dir=%d)" ), iBufferPtr->Length(), iDirection));
       
   295         }
       
   296 
       
   297     switch ( iDirection )
       
   298         {
       
   299         case EDunReaderDownstream:
       
   300             iStatus = KRequestPending;
       
   301             iNetwork->ReadOneOrMore( iStatus, *iBufferPtr );
       
   302             SetActive();
       
   303             FTRACE(FPrint( _L("CDunDownstream::IssueRequest() RComm ReadOneOrMore() requested" ) ));
       
   304             break;
       
   305         case EDunWriterDownstream:
       
   306             AddToQueueAndSend( iBufferPtr, this );
       
   307             break;
       
   308         default:
       
   309             FTRACE(FPrint( _L("CDunDownstream::IssueRequest() (ERROR) complete" ) ));
       
   310             return KErrGeneral;
       
   311         }
       
   312 
       
   313     iTransferState = EDunStateTransferring;
       
   314 
       
   315     FTRACE(FPrint( _L("CDunDownstream::IssueRequest() (Dir=%d) complete" ), iDirection));
       
   316     return KErrNone;
       
   317     }
       
   318 
       
   319 // ---------------------------------------------------------------------------
       
   320 // From class CActive.
       
   321 // Gets called when endpoint data read complete
       
   322 // ---------------------------------------------------------------------------
       
   323 //
       
   324 void CDunDownstream::RunL()
       
   325     {
       
   326     FTRACE(FPrint( _L("CDunDownstream::RunL() (Dir=%d)" ), iDirection));
       
   327     iTransferState = EDunStateIdle;
       
   328     if ( iOperationType != EDunOperationTypeRead )
       
   329         {
       
   330         FTRACE(FPrint( _L("CDunDownstream::RunL() (wrong operation type!) complete" )));
       
   331         return;
       
   332         }
       
   333 
       
   334     TBool isError;
       
   335     TInt retTemp = iStatus.Int();
       
   336     TInt stop = ProcessErrorCondition( retTemp, isError );
       
   337 
       
   338     if ( !stop )  // no real error detected -> continue
       
   339         {
       
   340         if ( !isError )
       
   341             {
       
   342             // Operation type was read so now set to write
       
   343             iOperationType = EDunOperationTypeWrite;
       
   344             }  // if ( !isError )
       
   345 
       
   346         IssueRequest();
       
   347 
       
   348         }  // if ( !stop )
       
   349     else  // stop -> tear down connection
       
   350         {
       
   351         // Now CDunDataPusher notifies to write case so here we just notify the
       
   352         // read case.
       
   353         TDunConnectionReason connReason;
       
   354         connReason.iReasonType = EDunReasonTypeRW;
       
   355         connReason.iContext = GetMediaContext( EDunStreamTypeDownstream );
       
   356         connReason.iSignalType = 0;
       
   357         connReason.iSignalHigh = EFalse;
       
   358         connReason.iDirection = iDirection;
       
   359         connReason.iErrorCode = retTemp;
       
   360         iUtility->DoNotifyConnectionNotOk( iComm,
       
   361                                            iSocket,
       
   362                                            connReason,
       
   363                                            iCallbacksR );
       
   364         FTRACE(FPrint( _L("CDunDownstream::RunL() stop" )));
       
   365         }  // else
       
   366 
       
   367     FTRACE(FPrint( _L("CDunDownstream::RunL() complete" )));
       
   368     }
       
   369 
       
   370 // ---------------------------------------------------------------------------
       
   371 // From class CActive.
       
   372 // Gets called on cancel
       
   373 // ---------------------------------------------------------------------------
       
   374 //
       
   375 void CDunDownstream::DoCancel()
       
   376     {
       
   377     }
       
   378 
       
   379 // ---------------------------------------------------------------------------
       
   380 // From class MDunStreamManipulator.
       
   381 // Gets called when outside party wants to push data to the existing stream
       
   382 // ---------------------------------------------------------------------------
       
   383 //
       
   384 TInt CDunDownstream::NotifyDataPushRequest( const TDesC8* aDataToPush,
       
   385                                             MDunCompletionReporter* aCallback )
       
   386     {
       
   387     FTRACE(FPrint( _L("CDunDownstream::NotifyDataPushRequest()" )));
       
   388     // If in data mode push the reply anyway as "CONNECT" or "NO CARRIER"
       
   389     // reply could arrive before/after the command mode information itself.
       
   390     TInt retVal = AddToQueueAndSend( aDataToPush, aCallback );
       
   391     FTRACE(FPrint( _L("CDunDownstream::NotifyDataPushRequest() complete" )));
       
   392     return retVal;
       
   393     }
       
   394 
       
   395 // ---------------------------------------------------------------------------
       
   396 // From class MDunCompletionReporter.
       
   397 // Gets called when data push is complete
       
   398 // ---------------------------------------------------------------------------
       
   399 //
       
   400 void CDunDownstream::NotifyDataPushComplete( TBool aAllPushed )
       
   401     {
       
   402     FTRACE(FPrint( _L("CDunDownstream::NotifyDataPushComplete()" )));
       
   403     // Next just skip the notifications of atomic operations because also this
       
   404     // class initiates the pushing of atomic data.
       
   405     if ( !aAllPushed )
       
   406         {
       
   407         FTRACE(FPrint( _L("CDunDownstream::NotifyDataPushComplete() (continue) complete" )));
       
   408         return;
       
   409         }
       
   410     iTransferState = EDunStateIdle;
       
   411     iOperationType = EDunOperationTypeUndefined;
       
   412     if ( !iPushData.iDataPusher )
       
   413         {
       
   414         FTRACE(FPrint( _L("CDunDownstream::NotifyDataPushComplete() (iPushData.iDataPusher not initialized!) complete" )));
       
   415         return;
       
   416         }
       
   417     // Now the multiplexer might have pushed the contained data in this class
       
   418     // or it might have pushed only the external data. If the pushed data
       
   419     // contains this classes data then reissue request, otherwise just clear
       
   420     // the queue.
       
   421     TInt foundIndex = iPushData.iDataPusher->FindEventFromQueue( iBufferPtr );
       
   422     iPushData.iDataPusher->SignalCompletionAndClearQueue();
       
   423     FTRACE(FPrint( _L("CDunDownstream::NotifyDataPushComplete() (find event)" )));
       
   424     if ( foundIndex >= 0 )
       
   425         {
       
   426         // Restart the reading from Dataport only if in data mode
       
   427         FTRACE(FPrint( _L("CDunDownstream::NotifyDataPushComplete() (issue request)" )));
       
   428         if ( iPushData.iDataMode )
       
   429             {
       
   430             iOperationType = EDunOperationTypeRead;
       
   431             IssueRequest();
       
   432             }
       
   433         }
       
   434     FTRACE(FPrint( _L("CDunDownstream::NotifyDataPushComplete() complete" )));
       
   435     }
       
   436 
       
   437 // ---------------------------------------------------------------------------
       
   438 // From class MDunCmdModeMonitor.
       
   439 // Notifies about command mode start
       
   440 // ---------------------------------------------------------------------------
       
   441 //
       
   442 void CDunDownstream::NotifyCommandModeStart()
       
   443     {
       
   444     FTRACE(FPrint( _L("CDunDownstream::NotifyCommandModeStart()" )));
       
   445     iPushData.iDataMode = EFalse;
       
   446     // Now the data mode has ended.
       
   447     // If read operation then cancel it.
       
   448     if ( iOperationType == EDunOperationTypeRead )
       
   449         {
       
   450         Stop( EFalse );
       
   451         }
       
   452     FTRACE(FPrint( _L("CDunDownstream::NotifyCommandModeStart() complete" )));
       
   453     }
       
   454 
       
   455 // ---------------------------------------------------------------------------
       
   456 // From class MDunCmdModeMonitor.
       
   457 // Notifies about command mode end
       
   458 // ---------------------------------------------------------------------------
       
   459 //
       
   460 void CDunDownstream::NotifyCommandModeEnd()
       
   461     {
       
   462     FTRACE(FPrint( _L("CDunDownstream::NotifyCommandModeEnd()" )));
       
   463     iPushData.iDataMode = ETrue;
       
   464     // Command mode ends here so start reading from Dataport
       
   465     iOperationType = EDunOperationTypeRead;
       
   466     IssueRequest();
       
   467     FTRACE(FPrint( _L("CDunDownstream::NotifyCommandModeEnd() complete" )));
       
   468     }