IMPSengine/engsrv/src/impsactiveconnmonitor.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     1 /*
       
     2 * Copyright (c) 2003-2005 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: Imps connection manager.
       
    15 *
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 // INCLUDE FILES
       
    23 #include    <e32std.h>
       
    24 #include    "ImpsSendReceive.h"
       
    25 #include    "impsactiveconnmonitor.h"
       
    26 #include    "impsutils.h"
       
    27 #include    "impserrors.h"
       
    28 
       
    29 
       
    30 // CONSTANTS
       
    31 
       
    32 // MACROS
       
    33 #ifndef _DEBUG
       
    34 #define _NO_IMPS_LOGGING_
       
    35 #endif
       
    36 
       
    37 // LOCAL CONSTANTS AND MACROS
       
    38 
       
    39 // Wait time 8 seconds before IAP re-opened
       
    40 const TInt KImpsWaitIAPOpen = 8000000;
       
    41 
       
    42 
       
    43 
       
    44 // ================= MEMBER FUNCTIONS =======================
       
    45 
       
    46 // C++ default constructor can NOT contain any code, that
       
    47 // might leave.
       
    48 //
       
    49 
       
    50 //**********************************
       
    51 // CImpsConnManager
       
    52 //**********************************
       
    53 CImpsConnManager::CImpsConnManager(
       
    54     CImpsSendReceive2& aSender,
       
    55     TInt aPriority )
       
    56         : CActive( aPriority ),
       
    57         iSender( aSender ),
       
    58         iState( EImpsNoAP ),
       
    59         iCmd( EImpsTransNone ),
       
    60         iConnState( EMsgBearerActive ),
       
    61         iManager( NULL ),
       
    62         iRunL( EFalse )
       
    63     {
       
    64     ( void ) iTimer.CreateLocal();
       
    65     // Add this to the scheduler
       
    66     CActiveScheduler::Add( this );
       
    67     }
       
    68 
       
    69 // EPOC default constructor can leave.
       
    70 void CImpsConnManager::ConstructL()
       
    71     {
       
    72 #ifndef _NO_IMPS_LOGGING_
       
    73     CImpsClientLogger::Log( _L( "CImpsConnManager: CONSTRUCTL rel200540.3f cman=%d" ), ( TInt )this );
       
    74 #endif
       
    75     iManager = NewMsgConnManagerL( 0 );
       
    76     }
       
    77 
       
    78 // Two-phased constructor.
       
    79 CImpsConnManager* CImpsConnManager::NewL(
       
    80     CImpsSendReceive2& aSender, TInt aPriority )
       
    81     {
       
    82     CImpsConnManager* self = new ( ELeave ) CImpsConnManager(
       
    83         aSender, aPriority );
       
    84     CleanupStack::PushL( self );
       
    85     self->ConstructL();
       
    86     CleanupStack::Pop();
       
    87     return self;
       
    88     }
       
    89 
       
    90 
       
    91 // Destructor
       
    92 CImpsConnManager::~CImpsConnManager()
       
    93     {
       
    94 #ifndef _NO_IMPS_LOGGING_
       
    95     CImpsClientLogger::Log( _L( "CImpsConnManager: DESTRUCTOR begins cman=%d" ), ( TInt )this );
       
    96 #endif
       
    97     Cancel();
       
    98     iTimer.Close();
       
    99     if ( iManager )
       
   100         {
       
   101         iManager->Destroy();
       
   102         }
       
   103 
       
   104 #ifndef _NO_IMPS_LOGGING_
       
   105     CImpsClientLogger::Log( _L( "CImpsConnManager: DESTRUCTOR ends cman=%d" ), ( TInt )this );
       
   106 #endif
       
   107     }
       
   108 
       
   109 // ---------------------------------------------------------
       
   110 // CImpsConnManager::OpenAPL
       
   111 // ---------------------------------------------------------
       
   112 //
       
   113 void CImpsConnManager::OpenAPL(
       
   114     TInt  aIAP, TBool aDelayed )
       
   115     {
       
   116 #ifndef _NO_IMPS_LOGGING_
       
   117     CImpsClientLogger::Log( _L( "CImpsConnManager: OpenAPL iap=%d delay=%d cman=%d" ),
       
   118                             aIAP, aDelayed, ( TInt )this );
       
   119 #endif
       
   120     if ( IsActive () )
       
   121         {
       
   122         // Notice: We should not be here
       
   123 #ifndef _NO_IMPS_LOGGING_
       
   124         CImpsClientLogger::Log( _L( "CImpsConnManager: BUSY cman=%d ****" ), ( TInt )this );
       
   125 #endif
       
   126         User::Leave( KErrNotReady );
       
   127         }
       
   128 
       
   129     iConnState = EMsgBearerActive;
       
   130 
       
   131     if ( ( aDelayed || iRunL ) && iCmd != EImpsTransWaitOpen )
       
   132         {
       
   133 #ifndef _NO_IMPS_LOGGING_
       
   134         CImpsClientLogger::Log( _L( "CImpsConnManager: cman=%d OpenAPL extra wait ****" ), ( TInt )this );
       
   135 #endif
       
   136         iCmd = EImpsTransWaitOpen;
       
   137         iState = EImpsNoAP;
       
   138         iIAP = aIAP;
       
   139         SetActive();
       
   140         // Give time for Connection Manager
       
   141         iTimer.After( iStatus, KImpsWaitIAPOpen );
       
   142         return;
       
   143         }
       
   144     iCmd = EImpsTransNone;
       
   145 #ifndef _NO_IMPS_LOGGING_
       
   146     CImpsClientLogger::Log( _L( "CImpsConnManager: cman=%d calls SetAccessPointIDL" ), ( TInt )this );
       
   147 #endif
       
   148     iManager->SetAccessPointIDL( aIAP );
       
   149 #ifndef _NO_IMPS_LOGGING_
       
   150     CImpsClientLogger::Log( _L( "CImpsConnManager: cman=%d calls RemoveEventSubscriber and AddEventSubscriberL" ), ( TInt )this );
       
   151 #endif
       
   152     // Verify that in error cases nothing is left hanging
       
   153     iManager->RemoveEventSubscriber( this );
       
   154     iManager->AddEventSubscriberL( this );
       
   155     iStatus = KRequestPending;
       
   156 #ifndef _NO_IMPS_LOGGING_
       
   157     CImpsClientLogger::Log( _L( "CImpsConnManager: cman=%d calls StartConnection" ), ( TInt )this );
       
   158 #endif
       
   159     iManager->StartConnection( iStatus );
       
   160     iCmd = EImpsTransOpenAP;
       
   161     iState = EImpsNoAP;
       
   162     iIAP = aIAP;
       
   163     SetActive();
       
   164     }
       
   165 
       
   166 // ---------------------------------------------------------
       
   167 // CImpsConnManager::CloseAP
       
   168 // ---------------------------------------------------------
       
   169 //
       
   170 void CImpsConnManager::CloseAP( TBool /*aTotalClose*/ )
       
   171     {
       
   172 #ifndef _NO_IMPS_LOGGING_
       
   173     CImpsClientLogger::Log( _L( "CImpsConnManager: CloseAP state=%d %d cman=%d" ),
       
   174                             iState, IsActive(), ( TInt )this );
       
   175 #endif
       
   176 
       
   177     if ( iState == EImpsNoAP && !IsActive() )
       
   178         {
       
   179 #ifndef _NO_IMPS_LOGGING_
       
   180         CImpsClientLogger::Log( _L( "CImpsConnManager: cman=%d CloseAP already closed" ), ( TInt )this );
       
   181 #endif
       
   182         iCmd = EImpsTransNone;
       
   183         iConnState = EMsgBearerActive;
       
   184         // Engine core waits any kind of response in shut down
       
   185         iSender.APStatusEvent( EImpsNoAP, KErrNone, ETrue );
       
   186 #ifndef _NO_IMPS_LOGGING_
       
   187         CImpsClientLogger::Log( _L( "CImpsConnManager: cman=%d CloseAP ends" ), ( TInt )this );
       
   188 #endif
       
   189         return;
       
   190         }
       
   191     else if ( iState == EImpsNoAP && IsActive() )
       
   192         {
       
   193         // Waiting open timer must be cancelled
       
   194         if ( iCmd == EImpsTransWaitOpen )
       
   195             {
       
   196             // This cancels the timer
       
   197 #ifndef _NO_IMPS_LOGGING_
       
   198             CImpsClientLogger::Log( _L( "CImpsConnManager: cman=%d err: CANCEL ***" ), ( TInt )this );
       
   199 #endif
       
   200             Cancel();
       
   201             iCmd = EImpsTransCloseAP;
       
   202             iIAP = 0;
       
   203             // Now do the trick to go to own RunL and call there appropriate callback.
       
   204             ActivateMe();
       
   205             return;
       
   206             }
       
   207         else if ( iCmd == EImpsTransOpenAP )
       
   208             {
       
   209             // Open APL is pending and must be cancelled
       
   210 #ifndef _NO_IMPS_LOGGING_
       
   211             CImpsClientLogger::Log( _L( "CImpsConnManager: cman=%d calls CancelStartL ****" ), ( TInt )this );
       
   212 #endif
       
   213             TRAPD( errx, iManager->CancelStartL() );
       
   214             if ( errx )
       
   215                 {
       
   216                 // Normally this is not needed, but Cancel StartL may leave with
       
   217                 // KErrAbort if there is no pending request.
       
   218                 // VERY RARE situation.
       
   219                 iCmd = EImpsTransNone;
       
   220                 iSender.APStatusEvent( EImpsNoAP, KErrNone, ETrue );
       
   221                 return;
       
   222                 }
       
   223             // Normal case continues in RunL
       
   224 #ifndef _NO_IMPS_LOGGING_
       
   225             CImpsClientLogger::Log( _L( "CImpsConnManager: cman=%d CloseAP ends" ), ( TInt )this );
       
   226 #endif
       
   227             return;
       
   228             }
       
   229         }
       
   230     else if ( IsActive() )  //
       
   231         {
       
   232         // Previous close is not completed.
       
   233 #ifndef _NO_IMPS_LOGGING_
       
   234         CImpsClientLogger::Log( _L( "CImpsConnManager: cman=%d BUSY ***" ), ( TInt )this );
       
   235 #endif
       
   236         return;
       
   237         }
       
   238     iCmd = EImpsTransCloseAP;
       
   239     iIAP = 0;
       
   240     iStatus = KRequestPending;
       
   241     SetActive();
       
   242     // Notice: Rest of activities in RunL and destructor
       
   243 #ifndef _NO_IMPS_LOGGING_
       
   244     CImpsClientLogger::Log( _L( "CImpsConnManager: cman=%d calls RemoveEventSubscriber" ), ( TInt )this );
       
   245 #endif
       
   246     iManager->RemoveEventSubscriber( this );
       
   247 #ifndef _NO_IMPS_LOGGING_
       
   248     CImpsClientLogger::Log( _L( "CImpsConnManager: cman=%d calls StopConnection" ), ( TInt )this );
       
   249 #endif
       
   250     iManager->StopConnection( iStatus );
       
   251 
       
   252 #ifndef _NO_IMPS_LOGGING_
       
   253     CImpsClientLogger::Log( _L( "CImpsConnManager: cman=%d CloseAP ends" ), ( TInt )this );
       
   254 #endif
       
   255     }
       
   256 
       
   257 
       
   258 // ---------------------------------------------------------
       
   259 // CImpsConnManager::HandleBearerEventL
       
   260 // ---------------------------------------------------------
       
   261 //
       
   262 void CImpsConnManager::HandleBearerEventL( TBool aIsAuthClose, TMsgBearerEvent aBearerEvent )
       
   263     {
       
   264 #ifndef _NO_IMPS_LOGGING_
       
   265     CImpsClientLogger::Log(
       
   266         _L( "CImpsConnManager: HandleBearerEventL %d authClose=%d cman=%d" ), aBearerEvent, aIsAuthClose, ( TInt )this );
       
   267 #endif
       
   268     iConnState = aBearerEvent;
       
   269     if ( IsActive() )
       
   270         {
       
   271         // pending open/close, so wait that instead
       
   272 #ifndef _NO_IMPS_LOGGING_
       
   273         CImpsClientLogger::Log(
       
   274             _L( "CImpsConnManager: HandleBearerEventL SPECIAL cmd=%d cman=%d ***" ), iCmd, ( TInt )this );
       
   275 #endif
       
   276 
       
   277         /* Notice:
       
   278          This is a little bit tricky situation because of Msg ConnManager DLL
       
   279          cannot quarantee the right order of events for us! Thus it is
       
   280          possible that we first get bearer-event and after that response
       
   281          for open/close connection request although the actual order
       
   282          is that open/close is first completed and then the bearer event has
       
   283          taken a place.
       
   284 
       
   285                /@
       
   286                \ \
       
   287              ___> \
       
   288             (__O)  \
       
   289            (____@)  \
       
   290            (____@)   \
       
   291             (__o)_    \
       
   292                   \    \
       
   293         */
       
   294 
       
   295         switch ( iConnState )
       
   296             {
       
   297             case EMsgBearerActive:
       
   298                 iSender.APStatusEvent( EImpsOnline, KErrNone, EFalse );
       
   299                 break;
       
   300             case EMsgBearerSuspended:
       
   301                 iSender.APStatusEvent( EImpsOffline, KErrNone, EFalse );
       
   302                 break;
       
   303             case EMsgBearerLost:
       
   304             default:
       
   305                 {
       
   306                 // RunL handles rest of the activities
       
   307                 // when iConnState == EMsgBearerLost.
       
   308                 break;
       
   309                 }
       
   310             };
       
   311         }
       
   312     else // regular case
       
   313         {
       
   314         switch ( iConnState )
       
   315             {
       
   316             case EMsgBearerActive:
       
   317                 iSender.APStatusEvent( EImpsOnline, KErrNone, EFalse );
       
   318                 break;
       
   319             case EMsgBearerSuspended:
       
   320                 iSender.APStatusEvent( EImpsOffline, KErrNone, EFalse );
       
   321                 break;
       
   322             case EMsgBearerLost:
       
   323             default:
       
   324                 {
       
   325                 iState = EImpsNoAP;
       
   326                 iSender.APStatusEvent( EImpsNoAP, KErrNone, EFalse, aIsAuthClose );
       
   327                 break;
       
   328                 }
       
   329             }
       
   330         };
       
   331 #ifndef _NO_IMPS_LOGGING_
       
   332     CImpsClientLogger::Log( _L( "CImpsConnManager: cman=%d HandleBearerEventL ends" ), ( TInt )this );
       
   333 #endif
       
   334     }
       
   335 
       
   336 // ---------------------------------------------------------
       
   337 // CImpsConnManager::ManagerHandleL
       
   338 // ---------------------------------------------------------
       
   339 //
       
   340 MMsgConnManager& CImpsConnManager::ManagerHandleL() const
       
   341     {
       
   342     if ( !iManager )
       
   343         {
       
   344         User::Leave( KErrBadHandle );
       
   345         }
       
   346     return *iManager;
       
   347     }
       
   348 
       
   349 // ---------------------------------------------------------
       
   350 // CImpsConnManager::Destroy
       
   351 // ---------------------------------------------------------
       
   352 //
       
   353 void CImpsConnManager::Destroy()
       
   354     {
       
   355     if ( iManager )
       
   356         {
       
   357         iManager->Destroy();
       
   358         }
       
   359     iManager = NULL;
       
   360     }
       
   361 
       
   362 // ---------------------------------------------------------
       
   363 // CImpsConnManager::RunL
       
   364 // ---------------------------------------------------------
       
   365 //
       
   366 void CImpsConnManager::RunL( )
       
   367     {
       
   368 
       
   369 #ifndef _NO_IMPS_LOGGING_
       
   370     CImpsClientLogger::Log( _L( "CImpsConnManager: RunL ret=%d cmd=%d state=%d conn=%d cman=%d" ),
       
   371                             iStatus.Int(), iCmd, iState, iConnState, ( TInt )this );
       
   372 #endif
       
   373 
       
   374     iRunL = ETrue;
       
   375     TInt ret = iStatus.Int();
       
   376 
       
   377     // We have delayed and given time for connection manager to work properly
       
   378     if ( iCmd == EImpsTransWaitOpen )
       
   379         {
       
   380         if ( !ret )
       
   381             {
       
   382             TInt errx = KErrNone;
       
   383             TRAP( errx, OpenAPL( iIAP, EFalse ) );
       
   384             }
       
   385         else
       
   386             {
       
   387             iState = EImpsNoAP;
       
   388             iCmd = EImpsTransNone;
       
   389             iSender.APStatusEvent( iState, ret, ETrue );
       
   390             }
       
   391         }
       
   392     else if ( iCmd == EImpsTransOpenAP )
       
   393         {
       
   394         iCmd = EImpsTransNone;
       
   395         iState = EImpsNoAP;
       
   396         if ( ret == KErrNone )
       
   397             {
       
   398             if ( iConnState != EMsgBearerLost )
       
   399                 {
       
   400                 // AP is opened successfully
       
   401                 iState = EImpsOnline;
       
   402                 }
       
   403             else // iConnState == EMsgBearerLost
       
   404                 {
       
   405 #ifndef _NO_IMPS_LOGGING_
       
   406                 CImpsClientLogger::Log( _L( "CImpsConnManager: err: cman=%d iConnState==EMsgBearerLost ****" ), ( TInt )this );
       
   407 #endif
       
   408                 // Special case where order of events from ConnManager is not valid.
       
   409                 ret = KImpsErrorNoIAP;
       
   410                 // KImpsErrorNoIAP error code has a special error handling
       
   411                 // in CSPSession class.
       
   412                 }
       
   413             }
       
   414         iSender.APStatusEvent( iState, ret, ETrue );
       
   415         }
       
   416     else if ( iCmd == EImpsTransCloseAP )
       
   417         {
       
   418         iCmd = EImpsTransNone;
       
   419         iState = EImpsNoAP;
       
   420         // Notice that iSender must exist, i.e. DeleteCSP() cancels
       
   421         // everything before deleting CSP session entity referred by iSender.
       
   422         iSender.APStatusEvent( EImpsNoAP, ret, ETrue );
       
   423         }
       
   424     else if ( iCmd == EImpsTransCloseDelay )
       
   425         {
       
   426         // This is a special case where this object is used for yielding.
       
   427         // Actually it calls CloseAP
       
   428         iCmd = EImpsTransNone;
       
   429         iSender.CloseTr( );
       
   430         }
       
   431     else
       
   432         {
       
   433         iCmd = EImpsTransNone;
       
   434         }
       
   435     iRunL = EFalse;
       
   436 #ifndef _NO_IMPS_LOGGING_
       
   437     CImpsClientLogger::Log( _L( "CImpsConnManager: cman=%d RunL ends" ), ( TInt )this );
       
   438 #endif
       
   439     }
       
   440 
       
   441 // ---------------------------------------------------------
       
   442 // CImpsConnManager::DoCancel
       
   443 // ---------------------------------------------------------
       
   444 //
       
   445 void CImpsConnManager::DoCancel()
       
   446     {
       
   447 #ifndef _NO_IMPS_LOGGING_
       
   448     CImpsClientLogger::Log( _L( "CImpsConnManager: cman=%d DoCancel" ), ( TInt )this );
       
   449 #endif
       
   450     TImpsTransCmd orig = iCmd;
       
   451     iCmd = EImpsTransNone;
       
   452     if ( orig == EImpsTransOpenAP )
       
   453         {
       
   454         // cancel
       
   455 #ifndef _NO_IMPS_LOGGING_
       
   456         CImpsClientLogger::Log( _L( "CImpsConnManager: cman=%d calls CancelStartL *****" ), ( TInt )this );
       
   457 #endif
       
   458         TInt errx = KErrNone;
       
   459         TRAP( errx, iManager->CancelStartL() );
       
   460         }
       
   461     else if ( orig == EImpsTransCloseAP )
       
   462         {
       
   463         iState = EImpsNoAP;
       
   464         }
       
   465     // This just waits IAP connection operation to end.
       
   466     else if ( iCmd == EImpsTransWaitOpen )
       
   467         {
       
   468         iTimer.Cancel();
       
   469         }
       
   470     }
       
   471 
       
   472 TInt CImpsConnManager::ActivateMe(  )
       
   473     {
       
   474     // This yields the control to the server thread active scheduler
       
   475     if ( !IsActive() )
       
   476         {
       
   477         iStatus = KRequestPending;
       
   478         SetActive();
       
   479         TRequestStatus* s = &iStatus;
       
   480         User::RequestComplete( s, KErrNone );
       
   481         return KErrNone;
       
   482         }
       
   483     else
       
   484         {
       
   485 #ifndef _NO_IMPS_LOGGING_
       
   486         CImpsClientLogger::Log( _L( "CImpsConnManager: cman=%d KErrNotReady ****" ), ( TInt )this );
       
   487 #endif
       
   488         return KErrNotReady;
       
   489         }
       
   490     }
       
   491 
       
   492 TInt CImpsConnManager::StartDelayed( )
       
   493     {
       
   494 #ifndef _NO_IMPS_LOGGING_
       
   495     CImpsClientLogger::Log( _L( "CImpsConnManager: cman=%d StartDelayed" ), ( TInt )this );
       
   496 #endif
       
   497     iCmd = EImpsTransCloseDelay;
       
   498     return ActivateMe();
       
   499     }
       
   500 
       
   501 
       
   502 //  End of File