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