localconnectivityservice/dun/utils/src/DunDownstream.cpp
changeset 28 85e0c0339cc3
child 33 883e91c086aa
equal deleted inserted replaced
25:48a2e0d8a4ce 28:85e0c0339cc3
       
     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 // Adds data to event queue and starts sending if needed
       
   120 // ---------------------------------------------------------------------------
       
   121 //
       
   122 TInt CDunDownstream::AddToQueueAndSend( const TDesC8 *aPushedData,
       
   123                                         MDunCompletionReporter* aCallback )
       
   124     {
       
   125     FTRACE(FPrint( _L("CDunDownstream::AddToQueueAndSend()" ) ));
       
   126     if ( !iPushData.iDataPusher )
       
   127         {
       
   128         FTRACE(FPrint( _L("CDunDownstream::AddToQueueAndSend() (iPushData.iDataPusher not initialized!) complete" )));
       
   129         return KErrGeneral;
       
   130         }
       
   131     // Add to event queue. If something went wrong, just return
       
   132     TInt retTemp = iPushData.iDataPusher->AddToEventQueue( aPushedData, aCallback );
       
   133     if ( retTemp != KErrNone )
       
   134         {
       
   135         FTRACE(FPrint( _L("CDunDownstream::AddToQueueAndSend() (ERROR) complete" )));
       
   136         return retTemp;
       
   137         }
       
   138     // Now push the data. If already active, push will start later, if not
       
   139     // active it will start immediately.
       
   140     iPushData.iDataPusher->SendQueuedData();
       
   141     FTRACE(FPrint( _L("CDunDownstream::AddToQueueAndSend() complete" ) ));
       
   142     return KErrNone;
       
   143     }
       
   144 
       
   145 // ---------------------------------------------------------------------------
       
   146 // Starts downstream by issuing read request
       
   147 // ---------------------------------------------------------------------------
       
   148 //
       
   149 TInt CDunDownstream::StartStream()
       
   150     {
       
   151     FTRACE(FPrint( _L("CDunDownstream::StartStream()" ) ));
       
   152     // Note: only start URC here.
       
   153     // The downstream read request is started when command mode ends.
       
   154     // This is done to make the data arrive in the correct order (reply vs.
       
   155     // data) with "ATD" command.
       
   156     TInt retVal = KErrNone;
       
   157     if ( iPushData.iAtCmdHandler )
       
   158         {
       
   159         retVal = iPushData.iAtCmdHandler->StartUrc();
       
   160         }
       
   161     FTRACE(FPrint( _L("CDunDownstream::StartStream() complete" ) ));
       
   162     return retVal;
       
   163     }
       
   164 
       
   165 // ---------------------------------------------------------------------------
       
   166 // Stops transfer for read or write endpoints
       
   167 // ---------------------------------------------------------------------------
       
   168 //
       
   169 TInt CDunDownstream::Stop( TBool aStopMplex )
       
   170     {
       
   171     FTRACE(FPrint( _L("CDunDownstream::Stop() (Dir=%d)" ), iDirection));
       
   172     if ( !iPushData.iDataPusher )
       
   173         {
       
   174         FTRACE(FPrint( _L("CDunDownstream::Stop() (iPushData.iDatapusher not initialized!) complete" )));
       
   175         return KErrGeneral;
       
   176         }
       
   177     // Stop the downstream related AT command handling functionality
       
   178     if ( aStopMplex )  // optional
       
   179         {
       
   180         if ( iPushData.iAtCmdHandler )
       
   181             {
       
   182             iPushData.iAtCmdHandler->StopAtCmdHandling();
       
   183             }
       
   184         // Stop the multiplexer separately
       
   185         iPushData.iDataPusher->Stop();
       
   186         }
       
   187     if ( iTransferState != EDunStateTransferring )
       
   188         {
       
   189         FTRACE(FPrint( _L("CDunDownstream::Stop() (not ready) complete" )));
       
   190         return KErrNotReady;
       
   191         }
       
   192     // Stop only current operation
       
   193     if ( iOperationType == EDunOperationTypeRead )
       
   194         {
       
   195         if ( iNetwork )
       
   196             {
       
   197             iNetwork->ReadCancel();
       
   198             Cancel();
       
   199             FTRACE(FPrint( _L("CDunDownstream::Stop() (Network) cancelled" )));
       
   200             }
       
   201         }
       
   202     else if ( iOperationType == EDunOperationTypeWrite )
       
   203         {
       
   204         iPushData.iDataPusher->StopOneEvent( iBufferPtr );
       
   205         }
       
   206     iTransferState = EDunStateIdle;
       
   207     FTRACE(FPrint( _L("CDunDownstream::Stop() complete" )));
       
   208     return KErrNone;
       
   209     }
       
   210 
       
   211 // ---------------------------------------------------------------------------
       
   212 // CDunDownstream::CDunDownstream
       
   213 // ---------------------------------------------------------------------------
       
   214 //
       
   215 CDunDownstream::CDunDownstream( MDunTransporterUtilityAux* aUtility ) :
       
   216     iUtility( aUtility )
       
   217     {
       
   218     Initialize();
       
   219     }
       
   220 
       
   221 // ---------------------------------------------------------------------------
       
   222 // CDunDownstream::ConstructL
       
   223 // ---------------------------------------------------------------------------
       
   224 //
       
   225 void CDunDownstream::ConstructL()
       
   226     {
       
   227     FTRACE(FPrint( _L("CDunDownstream::ConstructL()" ) ));
       
   228     if ( !iUtility )
       
   229         {
       
   230         User::Leave( KErrGeneral );
       
   231         }
       
   232     FTRACE(FPrint( _L("CDunDownstream::ConstructL() complete" ) ));
       
   233     }
       
   234 
       
   235 // ---------------------------------------------------------------------------
       
   236 // Initializes this class
       
   237 // ---------------------------------------------------------------------------
       
   238 //
       
   239 void CDunDownstream::Initialize()
       
   240     {
       
   241     // Don't initialize iUtility here (it is set through NewL)
       
   242     iPushData.iDataMode = EFalse;
       
   243     iPushData.iDataPusher = NULL;
       
   244     iPushData.iAtCmdHandler = NULL;
       
   245     }
       
   246 
       
   247 // ---------------------------------------------------------------------------
       
   248 // Issues transfer request for this stream
       
   249 // ---------------------------------------------------------------------------
       
   250 //
       
   251 TInt CDunDownstream::IssueRequest()
       
   252     {
       
   253     // Set direction
       
   254     iDirection = static_cast<TDunDirection>( EDunStreamTypeDownstream | iOperationType );
       
   255 
       
   256     FTRACE(FPrint( _L("CDunDownstream::IssueRequest() (Dir=%d)" ), iDirection));
       
   257     if ( !iPushData.iDataPusher )
       
   258         {
       
   259         FTRACE(FPrint( _L("CDunDownstream::IssueRequest() (iPushData.iDataPusher not initialized!) complete" ) ));
       
   260         return KErrGeneral;
       
   261         }
       
   262 
       
   263     if ( iTransferState != EDunStateIdle )
       
   264         {
       
   265         FTRACE(FPrint( _L("CDunDownstream::IssueRequest() (not ready) complete" ) ));
       
   266         return KErrNotReady;
       
   267         }
       
   268 
       
   269     if ( iOperationType == EDunOperationTypeRead )
       
   270         {
       
   271         iBufferPtr->SetLength( iBufferPtr->MaxLength() );
       
   272         FTRACE(FPrint( _L("CDunDownstream::IssueRequest() trying to read %d bytes... (Dir=%d)" ), iBufferPtr->Length(), iDirection));
       
   273         }
       
   274     else // iOperationType == EDunOperationTypeWrite
       
   275         {
       
   276         FTRACE(FPrint( _L("CDunDownstream::IssueRequest() writing %d bytes... (Dir=%d)" ), iBufferPtr->Length(), iDirection));
       
   277         }
       
   278 
       
   279     switch ( iDirection )
       
   280         {
       
   281         case EDunReaderDownstream:
       
   282             iStatus = KRequestPending;
       
   283             iNetwork->ReadOneOrMore( iStatus, *iBufferPtr );
       
   284             SetActive();
       
   285             FTRACE(FPrint( _L("CDunDownstream::IssueRequest() RComm ReadOneOrMore() requested" ) ));
       
   286             break;
       
   287         case EDunWriterDownstream:
       
   288             AddToQueueAndSend( iBufferPtr, this );
       
   289             break;
       
   290         default:
       
   291             FTRACE(FPrint( _L("CDunDownstream::IssueRequest() (ERROR) complete" ) ));
       
   292             return KErrGeneral;
       
   293         }
       
   294 
       
   295     iTransferState = EDunStateTransferring;
       
   296 
       
   297     FTRACE(FPrint( _L("CDunDownstream::IssueRequest() (Dir=%d) complete" ), iDirection));
       
   298     return KErrNone;
       
   299     }
       
   300 
       
   301 // ---------------------------------------------------------------------------
       
   302 // From class CActive.
       
   303 // Gets called when endpoint data read complete
       
   304 // ---------------------------------------------------------------------------
       
   305 //
       
   306 void CDunDownstream::RunL()
       
   307     {
       
   308     FTRACE(FPrint( _L("CDunDownstream::RunL() (Dir=%d)" ), iDirection));
       
   309     iTransferState = EDunStateIdle;
       
   310     if ( iOperationType != EDunOperationTypeRead )
       
   311         {
       
   312         FTRACE(FPrint( _L("CDunDownstream::RunL() (wrong operation type!) complete" )));
       
   313         return;
       
   314         }
       
   315 
       
   316     TBool isError;
       
   317     TInt retTemp = iStatus.Int();
       
   318     TInt stop = ProcessErrorCondition( retTemp, isError );
       
   319 
       
   320     if ( !stop )  // no real error detected -> continue
       
   321         {
       
   322         if ( !isError )
       
   323             {
       
   324             // Operation type was read so now set to write
       
   325             iOperationType = EDunOperationTypeWrite;
       
   326             }  // if ( !isError )
       
   327 
       
   328         IssueRequest();
       
   329 
       
   330         }  // if ( !stop )
       
   331     else  // stop -> tear down connection
       
   332         {
       
   333         // Now CDunDataPusher notifies to write case so here we just notify the
       
   334         // read case.
       
   335         TDunConnectionReason connReason;
       
   336         connReason.iReasonType = EDunReasonTypeRW;
       
   337         connReason.iContext = GetMediaContext( EDunStreamTypeDownstream );
       
   338         connReason.iSignalType = 0;
       
   339         connReason.iSignalHigh = EFalse;
       
   340         connReason.iDirection = iDirection;
       
   341         connReason.iErrorCode = retTemp;
       
   342         iUtility->DoNotifyConnectionNotOk( iComm,
       
   343                                            iSocket,
       
   344                                            connReason,
       
   345                                            iCallbacksR );
       
   346         FTRACE(FPrint( _L("CDunDownstream::RunL() stop" )));
       
   347         }  // else
       
   348 
       
   349     FTRACE(FPrint( _L("CDunDownstream::RunL() complete" )));
       
   350     }
       
   351 
       
   352 // ---------------------------------------------------------------------------
       
   353 // From class CActive.
       
   354 // Gets called on cancel
       
   355 // ---------------------------------------------------------------------------
       
   356 //
       
   357 void CDunDownstream::DoCancel()
       
   358     {
       
   359     }
       
   360 
       
   361 // ---------------------------------------------------------------------------
       
   362 // From class MDunStreamManipulator.
       
   363 // Gets called when outside party wants to push data to the existing stream
       
   364 // ---------------------------------------------------------------------------
       
   365 //
       
   366 TInt CDunDownstream::NotifyDataPushRequest( const TDesC8 *aPushedData,
       
   367                                             MDunCompletionReporter* aCallback )
       
   368     {
       
   369     FTRACE(FPrint( _L("CDunDownstream::NotifyDataPushRequest()" )));
       
   370     // If in data mode push the reply anyway as "CONNECT" or "NO CARRIER"
       
   371     // reply could arrive before/after the command mode information itself.
       
   372     TInt retVal = AddToQueueAndSend( aPushedData, aCallback );
       
   373     FTRACE(FPrint( _L("CDunDownstream::NotifyDataPushRequest() complete" )));
       
   374     return retVal;
       
   375     }
       
   376 
       
   377 // ---------------------------------------------------------------------------
       
   378 // From class MDunCompletionReporter.
       
   379 // Gets called when data push is complete
       
   380 // ---------------------------------------------------------------------------
       
   381 //
       
   382 void CDunDownstream::NotifyDataPushComplete( TBool aAllPushed )
       
   383     {
       
   384     FTRACE(FPrint( _L("CDunDownstream::NotifyDataPushComplete()" )));
       
   385     // Next just skip the notifications of atomic operations because also this
       
   386     // class initiates the pushing of atomic data.
       
   387     if ( !aAllPushed )
       
   388         {
       
   389         FTRACE(FPrint( _L("CDunDownstream::NotifyDataPushComplete() (continue) complete" )));
       
   390         return;
       
   391         }
       
   392     iTransferState = EDunStateIdle;
       
   393     iOperationType = EDunOperationTypeUndefined;
       
   394     if ( !iPushData.iDataPusher )
       
   395         {
       
   396         FTRACE(FPrint( _L("CDunDownstream::NotifyDataPushComplete() (iPushData.iDataPusher not initialized!) complete" )));
       
   397         return;
       
   398         }
       
   399     // Now the multiplexer might have pushed the contained data in this class
       
   400     // or it might have pushed only the external data. If the pushed data
       
   401     // contains this classes data then reissue request, otherwise just clear
       
   402     // the queue.
       
   403     TInt foundIndex = iPushData.iDataPusher->FindEventFromQueue( iBufferPtr );
       
   404     iPushData.iDataPusher->SignalCompletionAndClearQueue();
       
   405     FTRACE(FPrint( _L("CDunDownstream::NotifyDataPushComplete() (find event)" )));
       
   406     if ( foundIndex >= 0 )
       
   407         {
       
   408         // Restart the reading from Dataport only if in data mode
       
   409         FTRACE(FPrint( _L("CDunDownstream::NotifyDataPushComplete() (issue request)" )));
       
   410         if ( iPushData.iDataMode )
       
   411             {
       
   412             iOperationType = EDunOperationTypeRead;
       
   413             IssueRequest();
       
   414             }
       
   415         }
       
   416     FTRACE(FPrint( _L("CDunDownstream::NotifyDataPushComplete() complete" )));
       
   417     }
       
   418 
       
   419 // ---------------------------------------------------------------------------
       
   420 // From class MDunCmdModeMonitor.
       
   421 // Notifies about command mode start
       
   422 // ---------------------------------------------------------------------------
       
   423 //
       
   424 void CDunDownstream::NotifyCommandModeStart()
       
   425     {
       
   426     FTRACE(FPrint( _L("CDunDownstream::NotifyCommandModeStart()" )));
       
   427     iPushData.iDataMode = EFalse;
       
   428     // Now the data mode has ended.
       
   429     // If read operation then cancel it.
       
   430     if ( iOperationType == EDunOperationTypeRead )
       
   431         {
       
   432         Stop( EFalse );
       
   433         }
       
   434     FTRACE(FPrint( _L("CDunDownstream::NotifyCommandModeStart() complete" )));
       
   435     }
       
   436 
       
   437 // ---------------------------------------------------------------------------
       
   438 // From class MDunCmdModeMonitor.
       
   439 // Notifies about command mode end
       
   440 // ---------------------------------------------------------------------------
       
   441 //
       
   442 void CDunDownstream::NotifyCommandModeEnd()
       
   443     {
       
   444     FTRACE(FPrint( _L("CDunDownstream::NotifyCommandModeEnd()" )));
       
   445     iPushData.iDataMode = ETrue;
       
   446     // Command mode ends here so start reading from Dataport
       
   447     iOperationType = EDunOperationTypeRead;
       
   448     IssueRequest();
       
   449     FTRACE(FPrint( _L("CDunDownstream::NotifyCommandModeEnd() complete" )));
       
   450     }