upnp/upnpstack/upnpconnmanager/src/upnpconnmanagerengine.cpp
changeset 0 f5a58ecadc66
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 /** @file
       
     2 * Copyright (c) 2005-2006 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:  Defines CUpnpConnManagerEngine
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "upnpconnmanagerengine.h"
       
    21 #include "upnpsettings.h"
       
    22 #include "upnpconnmanagersession.h"
       
    23 
       
    24 #define KLogFile _L("UPnPConnManager.txt")
       
    25 #include "upnpcustomlog.h"
       
    26 
       
    27 const TTimeIntervalMicroSeconds32 KUnsuccesfulStartCacheTimeout( 2000000 );
       
    28 const TTimeIntervalMicroSeconds32 KWlanLostTimeout( 2000000 );
       
    29 const TInt KErrWlanLost = KErrNotReady;
       
    30 
       
    31 
       
    32 // ================= MEMBER FUNCTIONS =======================
       
    33 
       
    34 // -----------------------------------------------------------------------------
       
    35 // CUpnpConnManagerEngine::NewLC
       
    36 // Two-phased constructor.
       
    37 // -----------------------------------------------------------------------------
       
    38 //
       
    39 CUpnpConnManagerEngine* CUpnpConnManagerEngine::NewL( TDblQueIter<CSession2>& aIterator )
       
    40     {
       
    41     LOG_FUNC_NAME;
       
    42     CUpnpConnManagerEngine* self = new( ELeave ) CUpnpConnManagerEngine( aIterator );
       
    43     CleanupStack::PushL( self );
       
    44     self->ConstructL();
       
    45     CleanupStack::Pop( self );
       
    46     return self;
       
    47     }
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // CUpnpConnManagerEngine::CUpnpConnManagerEngine
       
    51 // C++ default constructor can NOT contain any code, that
       
    52 // might leave.
       
    53 // -----------------------------------------------------------------------------
       
    54 //
       
    55 CUpnpConnManagerEngine::CUpnpConnManagerEngine( TDblQueIter<CSession2>& aIterator )
       
    56     : iActiveIap( KErrNotFound ), iSessionIter( aIterator )
       
    57     {
       
    58     }
       
    59 
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // CUpnpConnManagerEngine::ConstructL
       
    63 // Symbian 2nd phase constructor can leave.
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 void CUpnpConnManagerEngine::ConstructL()
       
    67     {
       
    68     LOG_FUNC_NAME;
       
    69 
       
    70     // create a socket server connection
       
    71     User::LeaveIfError( iSocketServ.Connect() );
       
    72 
       
    73     iIfWatcher = CUpnpConnInterfaceWatcher::NewL( *this, iSocketServ, iConnection );
       
    74 
       
    75     iUnsuccesfulStartCacheTimer = CUpnpNotifyTimer::NewL( this );
       
    76     }
       
    77 
       
    78 // -----------------------------------------------------------------------------
       
    79 // CUpnpConnManagerEngine::CUpnpConnManagerEngine
       
    80 // Destructor.
       
    81 // -----------------------------------------------------------------------------
       
    82 //
       
    83 CUpnpConnManagerEngine::~CUpnpConnManagerEngine()
       
    84     {
       
    85     LOG_FUNC_NAME;
       
    86 
       
    87     delete iUnsuccesfulStartCacheTimer;
       
    88     
       
    89     delete iIfWatcher;
       
    90     CancelAllSubscription();
       
    91 
       
    92     iConnection.Close();
       
    93     iSocketServ.Close();
       
    94     }
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // CUpnpConnManagerEngine::EnsureStart
       
    98 // After call of this method the caller is sure that
       
    99 // RConnection was started.
       
   100 // -----------------------------------------------------------------------------
       
   101 //
       
   102 TInt CUpnpConnManagerEngine::EnsureStart( TInt& aAccessPoint )
       
   103     {
       
   104     TInt startError;
       
   105     if ( !iStartAttemptDone )
       
   106         {
       
   107         TRAP( startError, EnsureStartL() );
       
   108         if( KErrNone != startError )
       
   109             {
       
   110             iConnection.Close();
       
   111             }
       
   112         
       
   113         SetStartAttemptStatus( startError, KUnsuccesfulStartCacheTimeout );        
       
   114         }
       
   115     else
       
   116         {
       
   117         startError = iCachedStartError;
       
   118         }
       
   119     aAccessPoint = iActiveIap;
       
   120     return startError;
       
   121     }
       
   122 
       
   123 // -----------------------------------------------------------------------------
       
   124 // CUpnpConnManagerEngine::EnsureStartL
       
   125 // RConnection starting logic.
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 void CUpnpConnManagerEngine::EnsureStartL()
       
   129     {
       
   130     TUint32 activeIap;
       
   131     _LIT( KIAPId, "IAP\\Id" );
       
   132 
       
   133     SetupConnectionPreferencesL();
       
   134 
       
   135     User::LeaveIfError( iConnection.Open( iSocketServ ) );
       
   136     User::LeaveIfError( iConnection.Start( iCommDbConnPref ) );
       
   137     
       
   138     // because iCommDbConnPref is not updated by Start method
       
   139     // we retrieve active iap here
       
   140     // it is crucial for always ask case when predefined
       
   141     // wlan is chosen from unavailable wlan list
       
   142     User::LeaveIfError( iConnection.GetIntSetting( KIAPId, activeIap ) );
       
   143 
       
   144     iIfWatcher->ListenAddressChangeL( activeIap );
       
   145     iIfWatcher->SubscribeForConnectionServiceChangeNotifications();
       
   146     
       
   147     iActiveIap = activeIap;
       
   148     }
       
   149 
       
   150 // -----------------------------------------------------------------------------
       
   151 // CUpnpConnectionManager::SetupConnectionPreferencesL
       
   152 // Gets Iap from central repository and sets iCommDbConnPref.
       
   153 // -----------------------------------------------------------------------------
       
   154 //
       
   155 void CUpnpConnManagerEngine::SetupConnectionPreferencesL()
       
   156     {
       
   157     iCommDbConnPref.SetIapId( CUpnpSettings::GetIapL() );
       
   158     iCommDbConnPref.SetDialogPreference( ECommDbDialogPrefDoNotPrompt );
       
   159     iCommDbConnPref.SetBearerSet( ECommDbBearerWLAN );
       
   160     }
       
   161 
       
   162 // -----------------------------------------------------------------------------
       
   163 // CUpnpConnectionManager::IsStarted
       
   164 // Indicates whether RConnection was started.
       
   165 // -----------------------------------------------------------------------------
       
   166 //
       
   167 TBool CUpnpConnManagerEngine::IsStarted()
       
   168     {
       
   169     return iStartAttemptDone 
       
   170            && ( KErrNone == iCachedStartError ) 
       
   171            && ( iActiveIap > KErrNotFound );
       
   172     }
       
   173 
       
   174 // -----------------------------------------------------------------------------
       
   175 // CUpnpConnectionManager::ActiveIap
       
   176 // Returns active iap
       
   177 // -----------------------------------------------------------------------------
       
   178 //
       
   179 TInt CUpnpConnManagerEngine::ActiveIap()
       
   180     {
       
   181     return iActiveIap;
       
   182     }
       
   183 
       
   184 // -----------------------------------------------------------------------------
       
   185 // CUpnpConnManagerEngine::LocalAddress
       
   186 // -----------------------------------------------------------------------------
       
   187 //  
       
   188 const TInetAddr& CUpnpConnManagerEngine::LocalAddress()
       
   189     {
       
   190     return iIfWatcher->LocalAddress();
       
   191     }
       
   192 
       
   193 // -----------------------------------------------------------------------------
       
   194 // CUpnpConnectionManager::SetStartAttemptStatus
       
   195 // -----------------------------------------------------------------------------
       
   196 //
       
   197 void CUpnpConnManagerEngine::SetStartAttemptStatus( TInt aError,
       
   198                                                     TTimeIntervalMicroSeconds32 aUSec )
       
   199     {
       
   200     iStartAttemptDone = ETrue;
       
   201     iCachedStartError = aError;
       
   202 
       
   203     if ( iCachedStartError )
       
   204         {
       
   205         // start timer to clean start attempt status after aUSec microseconds
       
   206         iUnsuccesfulStartCacheTimer->Cancel();
       
   207         iUnsuccesfulStartCacheTimer->Start( aUSec );
       
   208         }
       
   209     }
       
   210 
       
   211 // -----------------------------------------------------------------------------
       
   212 // CUpnpConnectionManager::ClearStartAttemptStatus
       
   213 // -----------------------------------------------------------------------------
       
   214 //
       
   215 void CUpnpConnManagerEngine::ClearStartAttemptStatus()
       
   216     {
       
   217     iStartAttemptDone = EFalse;
       
   218     }
       
   219 
       
   220 // -----------------------------------------------------------------------------
       
   221 // CUpnpConnManagerEngine::TimerEventL
       
   222 // -----------------------------------------------------------------------------
       
   223 //
       
   224 void CUpnpConnManagerEngine::TimerEventL( CUpnpNotifyTimer* /*aTimer*/ )
       
   225     {
       
   226     ClearStartAttemptStatus();
       
   227     }
       
   228 
       
   229 // -----------------------------------------------------------------------------
       
   230 // CUpnpConnManagerEngine::WlanNetworkLost
       
   231 // -----------------------------------------------------------------------------
       
   232 //
       
   233 void CUpnpConnManagerEngine::WlanNetworkLost()
       
   234     {
       
   235     LOG_FUNC_NAME;
       
   236     NotifyAllAboutWlanLost();
       
   237     SetStartAttemptStatus( KErrWlanLost, KWlanLostTimeout );
       
   238     }
       
   239     
       
   240 // -----------------------------------------------------------------------------
       
   241 // CUpnpConnManagerEngine::NotifyAllAboutWlanLostL
       
   242 // -----------------------------------------------------------------------------
       
   243 //
       
   244 void CUpnpConnManagerEngine::NotifyAllAboutWlanLost()
       
   245     {
       
   246     iSessionIter.SetToFirst();
       
   247     CUpnpConnManagerSession* ses = 
       
   248               reinterpret_cast<CUpnpConnManagerSession*>( iSessionIter++ );
       
   249     while ( ses )
       
   250         {
       
   251         TRAP_IGNORE( ses->WLanLostOccuredL() );
       
   252         ses = reinterpret_cast<CUpnpConnManagerSession*>( iSessionIter++ );    
       
   253         }
       
   254     }    
       
   255 
       
   256 // -----------------------------------------------------------------------------
       
   257 // CUpnpConnManagerEngine::InterfaceAddressChange
       
   258 // -----------------------------------------------------------------------------
       
   259 //
       
   260 void CUpnpConnManagerEngine::InterfaceAddressChange( TInetAddr& aAddr )
       
   261     {
       
   262     LOG_FUNC_NAME;
       
   263     if ( IsStarted() )
       
   264         {
       
   265         NotifyAllAboutAddressChange( aAddr );
       
   266         }
       
   267     }
       
   268 
       
   269 // -----------------------------------------------------------------------------
       
   270 // CUpnpConnManagerEngine::NotifyAllAboutAddressChangeL
       
   271 // -----------------------------------------------------------------------------
       
   272 //
       
   273 void CUpnpConnManagerEngine::NotifyAllAboutAddressChange( TInetAddr& aAddr )
       
   274     {
       
   275     iSessionIter.SetToFirst();
       
   276     CUpnpConnManagerSession* ses = 
       
   277               reinterpret_cast<CUpnpConnManagerSession*>( iSessionIter++ );
       
   278     while ( ses )
       
   279         {
       
   280         TRAP_IGNORE( ses->AddressChangeOccuredL( aAddr ) );
       
   281         ses = reinterpret_cast<CUpnpConnManagerSession*>( iSessionIter++ );    
       
   282         }
       
   283     }    
       
   284 
       
   285 // -----------------------------------------------------------------------------
       
   286 // CUpnpConnManagerEngine::CancelAllSubscription
       
   287 // -----------------------------------------------------------------------------
       
   288 //    
       
   289 void CUpnpConnManagerEngine::CancelAllSubscription()
       
   290     {
       
   291     iSessionIter.SetToFirst();
       
   292     CUpnpConnManagerSession* ses = 
       
   293               reinterpret_cast<CUpnpConnManagerSession*>( iSessionIter++ );
       
   294     while ( ses )
       
   295         {
       
   296         ses->CancelSubscription();        
       
   297         ses = reinterpret_cast<CUpnpConnManagerSession*>( iSessionIter++ );    
       
   298         }
       
   299     }
       
   300 
       
   301 // -----------------------------------------------------------------------------
       
   302 // CUpnpConnManagerEngine::CloseConnection
       
   303 // -----------------------------------------------------------------------------
       
   304 //    
       
   305 void CUpnpConnManagerEngine::CloseConnection()
       
   306     {
       
   307     iIfWatcher->CancelSubscriptionForConnectionServiceChangeNotifications();
       
   308     iConnection.Close();
       
   309     iActiveIap = KErrNotFound;
       
   310     
       
   311     ClearStartAttemptStatus();
       
   312     NotifyAllAboutConnectionClosed();
       
   313     }
       
   314 
       
   315 // -----------------------------------------------------------------------------
       
   316 // CUpnpConnManagerEngine::CheckAndCloseConnection
       
   317 // -----------------------------------------------------------------------------
       
   318 //    
       
   319 void CUpnpConnManagerEngine::CheckAndCloseConnection(
       
   320                                              CUpnpConnManagerSession* aSession )
       
   321     {
       
   322     if ( !IsStarted() )
       
   323         {
       
   324         return;
       
   325         }
       
   326     
       
   327     iSessionIter.SetToFirst();
       
   328     CUpnpConnManagerSession* ses = 
       
   329               reinterpret_cast<CUpnpConnManagerSession*>( iSessionIter++ );
       
   330     while ( ses )
       
   331         {
       
   332         if ( ses != aSession && ses->SessionUseConnection() )
       
   333             {
       
   334             return;
       
   335             }
       
   336         ses = reinterpret_cast<CUpnpConnManagerSession*>( iSessionIter++ );    
       
   337         }
       
   338 
       
   339     CloseConnection();
       
   340     }
       
   341 
       
   342 // -----------------------------------------------------------------------------
       
   343 // CUpnpConnManagerEngine::NotifyAllAboutConnectionClosed
       
   344 // -----------------------------------------------------------------------------
       
   345 //    
       
   346 void CUpnpConnManagerEngine::NotifyAllAboutConnectionClosed()
       
   347     {
       
   348     iSessionIter.SetToFirst();
       
   349     CUpnpConnManagerSession* ses = 
       
   350               reinterpret_cast<CUpnpConnManagerSession*>( iSessionIter++ );
       
   351     while ( ses )
       
   352         {
       
   353         TRAP_IGNORE( ses->ConnectionClosedL() );
       
   354         ses = reinterpret_cast<CUpnpConnManagerSession*>( iSessionIter++ );    
       
   355         }
       
   356     }
       
   357 
       
   358 // End of File