localconnectivityservice/dun/utils/src/DunSignalWaiter.cpp
branchRCL_3
changeset 42 0aa8cc770c8a
child 52 866b4af7ffbe
equal deleted inserted replaced
40:453dfc402455 42:0aa8cc770c8a
       
     1 /*
       
     2 * Copyright (c) 2006-2007 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:  Monitors for new data in RComm local media side
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifdef PRJ_USE_NETWORK_STUBS
       
    20 #include <c32comm_stub.h>
       
    21 #else
       
    22 #include <d32comm.h>
       
    23 #endif
       
    24 #include "DunTransporter.h"
       
    25 #include "DunSignalWaiter.h"
       
    26 #include "DunSignalCopy.h"
       
    27 #include "DunDebug.h"
       
    28 
       
    29 // ======== MEMBER FUNCTIONS ========
       
    30 
       
    31 // ---------------------------------------------------------------------------
       
    32 // Two-phased constructor.
       
    33 // ---------------------------------------------------------------------------
       
    34 //
       
    35 CDunSignalWaiter* CDunSignalWaiter::NewL( MDunChannelAllocator* aChannelCallback )
       
    36     {
       
    37     CDunSignalWaiter* self = new (ELeave) CDunSignalWaiter( aChannelCallback );
       
    38     CleanupStack::PushL( self );
       
    39     self->ConstructL();
       
    40     CleanupStack::Pop( self );
       
    41     return self;
       
    42     }
       
    43 
       
    44 // ---------------------------------------------------------------------------
       
    45 // Destructor.
       
    46 // ---------------------------------------------------------------------------
       
    47 //
       
    48 CDunSignalWaiter::~CDunSignalWaiter()
       
    49     {
       
    50     FTRACE(FPrint( _L("CDunSignalWaiter::~CDunSignalWaiter()") ));
       
    51     ResetData();
       
    52     FTRACE(FPrint( _L("CDunSignalWaiter::~CDunSignalWaiter() complete") ));
       
    53     }
       
    54 
       
    55 // ---------------------------------------------------------------------------
       
    56 // Resets data to initial values
       
    57 // ---------------------------------------------------------------------------
       
    58 //
       
    59 void CDunSignalWaiter::ResetData()
       
    60     {
       
    61     FTRACE(FPrint( _L("CDunSignalWaiter::ResetData()") ));
       
    62     // APIs affecting this:
       
    63     // IssueRequest()
       
    64     Stop();
       
    65     // AddConnMonCallbackL()
       
    66     iCallbacks.Close();
       
    67     // Internal
       
    68     Initialize();
       
    69     FTRACE(FPrint( _L("CDunSignalWaiter::ResetData() complete") ));
       
    70     }
       
    71 
       
    72 // ---------------------------------------------------------------------------
       
    73 // Adds callback for RunL error controlling
       
    74 // The callback will be called when error is detected in asynchronous
       
    75 // operation
       
    76 // ---------------------------------------------------------------------------
       
    77 //
       
    78 TInt CDunSignalWaiter::AddCallback( MDunConnMon* aCallback )
       
    79     {
       
    80     FTRACE(FPrint( _L("CDunSignalWaiter::AddCallback()" ) ));
       
    81     if ( !aCallback )
       
    82         {
       
    83         FTRACE(FPrint( _L("CDunSignalWaiter::AddCallback() (ERROR) complete" ) ));
       
    84         return KErrGeneral;
       
    85         }
       
    86     TInt retTemp = iCallbacks.Find( aCallback );
       
    87     if ( retTemp != KErrNotFound )
       
    88         {
       
    89         FTRACE(FPrint( _L("CDunSignalWaiter::AddCallback() (already exists) complete" ) ));
       
    90         return KErrAlreadyExists;
       
    91         }
       
    92     retTemp = iCallbacks.Append( aCallback );
       
    93     if ( retTemp != KErrNone )
       
    94         {
       
    95         FTRACE(FPrint( _L("CDunSignalWaiter::AddCallback() (append failed!) complete" ) ));
       
    96         return retTemp;
       
    97         }
       
    98     FTRACE(FPrint( _L("CDunSignalWaiter::AddCallback() complete" ) ));
       
    99     return KErrNone;
       
   100     }
       
   101 
       
   102 // ---------------------------------------------------------------------------
       
   103 // Sets media to use for this endpoint
       
   104 // ---------------------------------------------------------------------------
       
   105 //
       
   106 TInt CDunSignalWaiter::SetMedia( RComm* aComm )
       
   107     {
       
   108     FTRACE(FPrint( _L("CDunSignalWaiter::SetMedia(RComm) (addr=0x%08X)" ), aComm));
       
   109     if ( !aComm )
       
   110         {
       
   111         FTRACE(FPrint( _L("CDunSignalWaiter::SetMedia() (aComm) not initialized!" )));
       
   112         return KErrGeneral;
       
   113         }
       
   114     TCommCaps2 caps;
       
   115     aComm->Caps( caps );
       
   116     if ( !(caps().iNotificationCaps & KNotifySignalsChangeSupported) )
       
   117         {
       
   118         FTRACE(FPrint( _L("CDunSignalWaiter::SetMedia() (RComm) (not supported) complete" )));
       
   119         return KErrNotSupported;
       
   120         }
       
   121     iComm = aComm;
       
   122     FTRACE(FPrint( _L("CDunSignalWaiter::SetMedia() (RComm) complete" )));
       
   123     return KErrNone;
       
   124     }
       
   125 
       
   126 // ---------------------------------------------------------------------------
       
   127 // Issues request to start waiting for new data in RComm
       
   128 // ---------------------------------------------------------------------------
       
   129 //
       
   130 TInt CDunSignalWaiter::IssueRequest()
       
   131     {
       
   132     FTRACE(FPrint( _L("CDunSignalWaiter::IssueRequest()" )));
       
   133     if ( iSignalWaiterState != EDunStateIdle )
       
   134         {
       
   135         FTRACE(FPrint( _L("CDunSignalWaiter::IssueRequest() (not ready) complete" )));
       
   136         return KErrNotReady;
       
   137         }
       
   138     if ( !iComm )
       
   139         {
       
   140         FTRACE(FPrint( _L("CDunSignalWaiter::IssueRequest() (iComm) not initialized!" ) ));
       
   141         return KErrGeneral;
       
   142         }
       
   143     iStatus = KRequestPending;
       
   144     iComm->NotifySignalChange( iStatus, iSignals, KSignalDCEInputs );
       
   145     SetActive();
       
   146     iSignalWaiterState = EDunStateSignalWaiting;
       
   147     FTRACE(FPrint( _L("CDunSignalWaiter::IssueRequest() complete" )));
       
   148     return KErrNone;
       
   149     }
       
   150 
       
   151 // ---------------------------------------------------------------------------
       
   152 // Stops monitoring for new data
       
   153 // ---------------------------------------------------------------------------
       
   154 //
       
   155 TInt CDunSignalWaiter::Stop()
       
   156     {
       
   157     FTRACE(FPrint( _L("CDunSignalWaiter::Stop()" )));
       
   158     if ( iSignalWaiterState != EDunStateSignalWaiting )
       
   159         {
       
   160         FTRACE(FPrint( _L("CDunSignalWaiter::Stop() (not ready) complete" )));
       
   161         return KErrNotReady;
       
   162         }
       
   163     if ( !iComm )
       
   164         {
       
   165         FTRACE(FPrint( _L("CDunSignalWaiter::Stop() (iComm) not initialized!" )));
       
   166         return KErrGeneral;
       
   167         }
       
   168     iComm->NotifySignalChangeCancel();
       
   169     Cancel();
       
   170     iSignalWaiterState = EDunStateIdle;
       
   171     FTRACE(FPrint( _L("CDunSignalWaiter::Stop() complete" )));
       
   172     return KErrNone;
       
   173     }
       
   174 
       
   175 // ---------------------------------------------------------------------------
       
   176 // CDunSignalWaiter::CDunSignalWaiter
       
   177 // ---------------------------------------------------------------------------
       
   178 //
       
   179 CDunSignalWaiter::CDunSignalWaiter( MDunChannelAllocator* aChannelCallback ) :
       
   180     CActive( EPriorityHigh ),
       
   181     iChannelCallback( aChannelCallback )
       
   182     {
       
   183     Initialize();
       
   184     }
       
   185 
       
   186 // ---------------------------------------------------------------------------
       
   187 // CDunSignalWaiter::ConstructL
       
   188 // ---------------------------------------------------------------------------
       
   189 //
       
   190 void CDunSignalWaiter::ConstructL()
       
   191     {
       
   192     FTRACE(FPrint( _L("CDunSignalWaiter::ConstructL()" ) ));
       
   193     if ( !iChannelCallback )
       
   194         {
       
   195         User::Leave( KErrGeneral );
       
   196         }
       
   197     CActiveScheduler::Add( this );
       
   198     FTRACE(FPrint( _L("CDunSignalWaiter::ConstructL() complete" ) ));
       
   199     }
       
   200 
       
   201 // ---------------------------------------------------------------------------
       
   202 // Initializes this class
       
   203 // ---------------------------------------------------------------------------
       
   204 //
       
   205 void CDunSignalWaiter::Initialize()
       
   206     {
       
   207     FTRACE(FPrint( _L("CDunSignalWaiter::Initialize()" ) ));
       
   208     // Don't initialize iChannelCallback here (it is set through NewL)
       
   209     iSignalWaiterState = EDunStateIdle;
       
   210     iSignals = 0;
       
   211     iComm = NULL;
       
   212     FTRACE(FPrint( _L("CDunSignalWaiter::Initialize() complete" ) ));
       
   213     }
       
   214 
       
   215 // ---------------------------------------------------------------------------
       
   216 // From class CActive.
       
   217 // Gets called when signals changed in RComm object
       
   218 // ---------------------------------------------------------------------------
       
   219 //
       
   220 void CDunSignalWaiter::RunL()
       
   221     {
       
   222     FTRACE(FPrint( _L("CDunSignalWaiter::RunL()" ) ));
       
   223     iSignalWaiterState = EDunStateIdle;
       
   224     TInt retTemp = iStatus.Int();
       
   225     if ( retTemp != KErrNone )
       
   226         {
       
   227         FTRACE(FPrint( _L("CDunSignalWaiter::RunL() (failed) complete (%d)"), retTemp));
       
   228         TInt count = iCallbacks.Count();
       
   229         TDunConnectionReason connReason;
       
   230         connReason.iReasonType = EDunReasonTypeRunL;
       
   231         connReason.iContext = EDunMediaContextLocal;
       
   232         connReason.iSignalType = 0;
       
   233         connReason.iSignalHigh = EFalse;
       
   234         connReason.iDirection = EDunDirectionUndefined;
       
   235         connReason.iErrorCode = retTemp;
       
   236         for ( TInt i=0; i<count; i++ )
       
   237             {
       
   238             TRAP_IGNORE(
       
   239                 iCallbacks[i]->NotifyProgressChangeL( iComm, connReason ) );
       
   240             }
       
   241         return;
       
   242         }
       
   243     if ( iChannelCallback )
       
   244         {
       
   245         iChannelCallback->NotifyNewChannelRequest( iComm );
       
   246         }
       
   247     FTRACE(FPrint( _L("CDunSignalWaiter::RunL() complete" ) ));
       
   248     }
       
   249 
       
   250 // ---------------------------------------------------------------------------
       
   251 // From class CActive.
       
   252 // Gets called on cancel
       
   253 // ---------------------------------------------------------------------------
       
   254 //
       
   255 void CDunSignalWaiter::DoCancel()
       
   256     {
       
   257     }